[llvm-objdump] - Fix the indentation when printing dynamic tags.

We have a bug currently: printed tag names might overlap the
value column. It happens for MIPS now.

This patch adds a logic to calculate the size of indentation on fly
to fix such issues.

Differential revision: https://reviews.llvm.org/D72838
This commit is contained in:
Georgii Rymar 2020-01-16 16:13:23 +03:00
parent 93175a5caa
commit 547530cc6a
3 changed files with 96 additions and 48 deletions

View File

@ -36,57 +36,56 @@ ProgramHeaders:
- Section: .dynamic
## Case 2: Test that MIPS machine-specific tags can be dumped.
## MIPS has a few long tag names. Show that we indent columns properly.
# RUN: yaml2obj --docnum=2 -o %t.mips %s
# RUN: llvm-objdump -p %t.mips | FileCheck %s --strict-whitespace --match-full-lines --check-prefix=MIPS
# MIPS:Dynamic Section:
# MIPS-NEXT: MIPS_RLD_VERSION 0x0000000012345678
# MIPS-NEXT: MIPS_TIME_STAMP 0x0000000011223344
# MIPS-NEXT: MIPS_ICHECKSUM 0x0000000011112222
# MIPS-NEXT: MIPS_IVERSION 0x0000000000000001
# MIPS-NEXT: MIPS_FLAGS 0x0000000011111111
# MIPS-NEXT: MIPS_BASE_ADDRESS 0x0000000087654321
# MIPS-NEXT: MIPS_MSYM 0x0000000000001000
# MIPS-NEXT: MIPS_CONFLICT 0x0000000000001000
# MIPS-NEXT: MIPS_LIBLIST 0x0000000000001000
# MIPS-NEXT: MIPS_LOCAL_GOTNO 0x0000000000000001
# MIPS-NEXT: MIPS_CONFLICTNO 0x0000000000000001
# MIPS-NEXT: MIPS_LIBLISTNO 0x0000000000000001
# MIPS-NEXT: MIPS_SYMTABNO 0x0000000000000001
# MIPS-NEXT: MIPS_UNREFEXTNO 0x0000000000000000
# MIPS-NEXT: MIPS_GOTSYM 0x0000000000000000
# MIPS-NEXT: MIPS_HIPAGENO 0x0000000088776655
# MIPS-NEXT: MIPS_RLD_MAP 0x0000000000001000
# MIPS-NEXT: MIPS_DELTA_CLASS 0x0000000000001000
# MIPS-NEXT: MIPS_DELTA_CLASS_NO 0x0000000000000001
# MIPS-NEXT: MIPS_DELTA_INSTANCE 0x0000000000001000
## FIXME: llvm-objdump does not print spaces after MIPS_DELTA_INSTANCE_NO, MIPS_PROTECTED_GOTIDX
## and other long tags. The output looks broken because of that.
# MIPS-NEXT: MIPS_DELTA_INSTANCE_NO0x0000000000000001
# MIPS-NEXT: MIPS_DELTA_RELOC 0x0000000000001000
# MIPS-NEXT: MIPS_DELTA_RELOC_NO 0x0000000000000001
# MIPS-NEXT: MIPS_DELTA_SYM 0x0000000000001000
# MIPS-NEXT: MIPS_DELTA_SYM_NO 0x0000000000000001
# MIPS-NEXT: MIPS_DELTA_CLASSSYM 0x0000000000001000
# MIPS-NEXT: MIPS_DELTA_CLASSSYM_NO0x0000000000000001
# MIPS-NEXT: MIPS_CXX_FLAGS 0x0000000088887777
# MIPS-NEXT: MIPS_PIXIE_INIT 0x0000000000001000
# MIPS-NEXT: MIPS_LOCALPAGE_GOTIDX0x0000000000000001
# MIPS-NEXT: MIPS_LOCAL_GOTIDX 0x0000000000000001
# MIPS-NEXT: MIPS_HIDDEN_GOTIDX 0x0000000000000001
# MIPS-NEXT: MIPS_PROTECTED_GOTIDX0x0000000000000001
# MIPS-NEXT: MIPS_OPTIONS 0x0000000000001000
# MIPS-NEXT: MIPS_INTERFACE 0x0000000000001000
# MIPS-NEXT: MIPS_DYNSTR_ALIGN 0x0000000088888888
# MIPS-NEXT: MIPS_INTERFACE_SIZE 0x0000000000000010
# MIPS-NEXT: MIPS_RLD_TEXT_RESOLVE_ADDR0x0000000000000008
# MIPS-NEXT: MIPS_PERF_SUFFIX 0x0000000000000000
# MIPS-NEXT: MIPS_COMPACT_SIZE 0x0000000000000010
# MIPS-NEXT: MIPS_GP_VALUE 0x0000000000000001
# MIPS-NEXT: MIPS_AUX_DYNAMIC 0x0000000000001000
# MIPS-NEXT: MIPS_PLTGOT 0x0000000000001000
# MIPS-NEXT: MIPS_RWPLT 0x0000000000001000
# MIPS-NEXT: MIPS_RLD_MAP_REL 0x0000000000001000
# MIPS-NEXT: MIPS_RLD_VERSION 0x0000000012345678
# MIPS-NEXT: MIPS_TIME_STAMP 0x0000000011223344
# MIPS-NEXT: MIPS_ICHECKSUM 0x0000000011112222
# MIPS-NEXT: MIPS_IVERSION 0x0000000000000001
# MIPS-NEXT: MIPS_FLAGS 0x0000000011111111
# MIPS-NEXT: MIPS_BASE_ADDRESS 0x0000000087654321
# MIPS-NEXT: MIPS_MSYM 0x0000000000001000
# MIPS-NEXT: MIPS_CONFLICT 0x0000000000001000
# MIPS-NEXT: MIPS_LIBLIST 0x0000000000001000
# MIPS-NEXT: MIPS_LOCAL_GOTNO 0x0000000000000001
# MIPS-NEXT: MIPS_CONFLICTNO 0x0000000000000001
# MIPS-NEXT: MIPS_LIBLISTNO 0x0000000000000001
# MIPS-NEXT: MIPS_SYMTABNO 0x0000000000000001
# MIPS-NEXT: MIPS_UNREFEXTNO 0x0000000000000000
# MIPS-NEXT: MIPS_GOTSYM 0x0000000000000000
# MIPS-NEXT: MIPS_HIPAGENO 0x0000000088776655
# MIPS-NEXT: MIPS_RLD_MAP 0x0000000000001000
# MIPS-NEXT: MIPS_DELTA_CLASS 0x0000000000001000
# MIPS-NEXT: MIPS_DELTA_CLASS_NO 0x0000000000000001
# MIPS-NEXT: MIPS_DELTA_INSTANCE 0x0000000000001000
# MIPS-NEXT: MIPS_DELTA_INSTANCE_NO 0x0000000000000001
# MIPS-NEXT: MIPS_DELTA_RELOC 0x0000000000001000
# MIPS-NEXT: MIPS_DELTA_RELOC_NO 0x0000000000000001
# MIPS-NEXT: MIPS_DELTA_SYM 0x0000000000001000
# MIPS-NEXT: MIPS_DELTA_SYM_NO 0x0000000000000001
# MIPS-NEXT: MIPS_DELTA_CLASSSYM 0x0000000000001000
# MIPS-NEXT: MIPS_DELTA_CLASSSYM_NO 0x0000000000000001
# MIPS-NEXT: MIPS_CXX_FLAGS 0x0000000088887777
# MIPS-NEXT: MIPS_PIXIE_INIT 0x0000000000001000
# MIPS-NEXT: MIPS_LOCALPAGE_GOTIDX 0x0000000000000001
# MIPS-NEXT: MIPS_LOCAL_GOTIDX 0x0000000000000001
# MIPS-NEXT: MIPS_HIDDEN_GOTIDX 0x0000000000000001
# MIPS-NEXT: MIPS_PROTECTED_GOTIDX 0x0000000000000001
# MIPS-NEXT: MIPS_OPTIONS 0x0000000000001000
# MIPS-NEXT: MIPS_INTERFACE 0x0000000000001000
# MIPS-NEXT: MIPS_DYNSTR_ALIGN 0x0000000088888888
# MIPS-NEXT: MIPS_INTERFACE_SIZE 0x0000000000000010
# MIPS-NEXT: MIPS_RLD_TEXT_RESOLVE_ADDR 0x0000000000000008
# MIPS-NEXT: MIPS_PERF_SUFFIX 0x0000000000000000
# MIPS-NEXT: MIPS_COMPACT_SIZE 0x0000000000000010
# MIPS-NEXT: MIPS_GP_VALUE 0x0000000000000001
# MIPS-NEXT: MIPS_AUX_DYNAMIC 0x0000000000001000
# MIPS-NEXT: MIPS_PLTGOT 0x0000000000001000
# MIPS-NEXT: MIPS_RWPLT 0x0000000000001000
# MIPS-NEXT: MIPS_RLD_MAP_REL 0x0000000000001000
--- !ELF
FileHeader:

View File

@ -430,3 +430,45 @@ ProgramHeaders:
VAddr: 0x1010
Sections:
- Section: .dynamic
## When printing the value column we want to have the minimal possible indentation.
## Use an arbitrary dynamic tag to demonstrate that.
# RUN: yaml2obj --docnum=3 %s -o %t3
# RUN: llvm-objdump -p %t3 | FileCheck %s --strict-whitespace --check-prefix=INDENT
# RUN: yaml2obj --docnum=4 %s -o %t4
# RUN: llvm-objdump -p %t4 | FileCheck %s --strict-whitespace --check-prefix=INDENT
# INDENT: {{^}}Dynamic Section:
# INDENT: {{^}} NEEDED 0x
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64
Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
Entries:
- Tag: DT_NEEDED
Value: 0x1
- Tag: DT_NULL
Value: 0x0
--- !ELF
FileHeader:
Class: ELFCLASS32
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_386
Sections:
- Name: .dynamic
Type: SHT_DYNAMIC
Entries:
- Tag: DT_NEEDED
Value: 0x1
- Tag: DT_NULL
Value: 0x0

View File

@ -162,13 +162,20 @@ template <class ELFT>
void printDynamicSection(const ELFFile<ELFT> *Elf, StringRef Filename) {
ArrayRef<typename ELFT::Dyn> DynamicEntries =
unwrapOrError(Elf->dynamicEntries(), Filename);
// Find the maximum tag name length to format the value column properly.
size_t MaxLen = 0;
for (const typename ELFT::Dyn &Dyn : DynamicEntries)
MaxLen = std::max(MaxLen, Elf->getDynamicTagAsString(Dyn.d_tag).size());
std::string TagFmt = " %-" + std::to_string(MaxLen) + "s ";
outs() << "Dynamic Section:\n";
for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
if (Dyn.d_tag == ELF::DT_NULL)
continue;
std::string Str = Elf->getDynamicTagAsString(Dyn.d_tag);
outs() << format(" %-21s", Str.c_str());
outs() << format(TagFmt.c_str(), Str.c_str());
const char *Fmt =
ELFT::Is64Bits ? "0x%016" PRIx64 "\n" : "0x%08" PRIx64 "\n";