Commit 547530cc authored by Georgii Rymar's avatar Georgii Rymar
Browse files

[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
parent 93175a5c
Loading
Loading
Loading
Loading
+46 −47
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ 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

@@ -60,8 +61,6 @@ ProgramHeaders:
# 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_NO     0x0000000000000001
# MIPS-NEXT:  MIPS_DELTA_RELOC           0x0000000000001000
# MIPS-NEXT:  MIPS_DELTA_RELOC_NO        0x0000000000000001
+42 −0
Original line number Diff line number Diff line
@@ -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
+8 −1
Original line number Diff line number Diff line
@@ -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";