[llvm] r355073 - [llvm-readobj] - Fix the invalid dumping of the dynamic sections without terminating DT_NULL entry.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 28 00:15:59 PST 2019


Author: grimar
Date: Thu Feb 28 00:15:59 2019
New Revision: 355073

URL: http://llvm.org/viewvc/llvm-project?rev=355073&view=rev
Log:
[llvm-readobj] - Fix the invalid dumping of the dynamic sections without terminating DT_NULL entry.

This is https://bugs.llvm.org/show_bug.cgi?id=40861,

Previously llvm-readobj would print the DT_NULL sometimes
for the dynamic section that has no terminator entry.

The logic of printDynamicTable was a bit overcomplicated.
I rewrote it slightly to fix the issue and commented.

Differential revision: https://reviews.llvm.org/D58716

Added:
    llvm/trunk/test/tools/llvm-readobj/elf-dynamic-table-dtnull.s
Modified:
    llvm/trunk/tools/llvm-readobj/ELFDumper.cpp

Added: llvm/trunk/test/tools/llvm-readobj/elf-dynamic-table-dtnull.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/elf-dynamic-table-dtnull.s?rev=355073&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-readobj/elf-dynamic-table-dtnull.s (added)
+++ llvm/trunk/test/tools/llvm-readobj/elf-dynamic-table-dtnull.s Thu Feb 28 00:15:59 2019
@@ -0,0 +1,78 @@
+# Check we are able to dump the dynamic section without a DT_NULL entry correctly.
+
+# RUN: yaml2obj -docnum=1 %s -o %t.o
+# RUN: llvm-readobj --dynamic-table %t.o | FileCheck %s --check-prefix=NONULL
+# RUN: llvm-readelf --dynamic-table %t.o | FileCheck %s --check-prefix=NONULL
+
+# NONULL:      DynamicSection [ (1 entries)
+# NONULL-NEXT:   Tag                Type   Name/Value
+# NONULL-NEXT:   0x0000000000000015 DEBUG  0x0
+# NONULL-NEXT: ]
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .dynamic
+    Type:            SHT_DYNAMIC
+    Address:         0x0000000000001010
+    AddressAlign:    0x0000000000000010
+    EntSize:         0x0000000000000010
+    Entries:
+      - Tag:             DT_DEBUG
+        Value:           0x0000000000000000
+ProgramHeaders:
+  - Type: PT_LOAD
+    VAddr: 0x1000
+    Sections:
+      - Section: .dynamic
+  - Type: PT_DYNAMIC
+    VAddr: 0x1010
+    Sections:
+      - Section: .dynamic
+
+# Sometimes .dynamic section content length can be greater than the
+# length of its entries. In this case, we should not try to dump anything
+# past the DT_NULL entry, which works as a terminator.
+
+# RUN: yaml2obj -docnum=2 %s -o %t.o
+# RUN: llvm-readobj --dynamic-table %t.o | FileCheck %s --check-prefix=LONG
+# RUN: llvm-readelf --dynamic-table %t.o | FileCheck %s --check-prefix=LONG
+
+# LONG:      DynamicSection [ (2 entries)
+# LONG-NEXT:   Tag                Type                 Name/Value
+# LONG-NEXT:   0x0000000000000015 DEBUG                0x0
+# LONG-NEXT:   0x0000000000000000 NULL                 0x0
+# LONG-NEXT: ]
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_EXEC
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .dynamic
+    Type:            SHT_DYNAMIC
+    Address:         0x0000000000001010
+    AddressAlign:    0x0000000000000010
+    EntSize:         0x0000000000000010
+    Entries:
+      - Tag:             DT_DEBUG
+        Value:           0x0000000000000000
+      - Tag:             DT_NULL
+        Value:           0x0000000000000000
+      - Tag:             DT_NULL
+        Value:           0x0000000000000000
+ProgramHeaders:
+  - Type: PT_LOAD
+    VAddr: 0x1000
+    Sections:
+      - Section: .dynamic
+  - Type: PT_DYNAMIC
+    VAddr: 0x1010
+    Sections:
+      - Section: .dynamic

Modified: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ELFDumper.cpp?rev=355073&r1=355072&r2=355073&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp Thu Feb 28 00:15:59 2019
@@ -1914,35 +1914,29 @@ template <> void ELFDumper<ELF32LE>::pri
 
 template<class ELFT>
 void ELFDumper<ELFT>::printDynamicTable() {
-  auto I = dynamic_table().begin();
-  auto E = dynamic_table().end();
+  // A valid .dynamic section contains an array of entries terminated with
+  // a DT_NULL entry. However, sometimes the section content may continue
+  // past the DT_NULL entry, so to dump the section correctly, we first find
+  // the end of the entries by iterating over them.
+  size_t Size = 0;
+  Elf_Dyn_Range DynTableEntries = dynamic_table();
+  for (; Size < DynTableEntries.size();)
+    if (DynTableEntries[Size++].getTag() == DT_NULL)
+      break;
 
-  if (I == E)
-    return;
-
-  --E;
-  while (I != E && E->getTag() == ELF::DT_NULL)
-    --E;
-  if (E->getTag() != ELF::DT_NULL)
-    ++E;
-  ++E;
-
-  ptrdiff_t Total = std::distance(I, E);
-  if (Total == 0)
+  if (!Size)
     return;
 
   raw_ostream &OS = W.getOStream();
-  W.startLine() << "DynamicSection [ (" << Total << " entries)\n";
+  W.startLine() << "DynamicSection [ (" << Size << " entries)\n";
 
   bool Is64 = ELFT::Is64Bits;
-
   W.startLine()
      << "  Tag" << (Is64 ? "                " : "        ") << "Type"
      << "                 " << "Name/Value\n";
-  while (I != E) {
-    const Elf_Dyn &Entry = *I;
+  for (size_t I = 0; I < Size; ++I) {
+    const Elf_Dyn &Entry = DynTableEntries[I];
     uintX_t Tag = Entry.getTag();
-    ++I;
     W.startLine() << "  " << format_hex(Tag, Is64 ? 18 : 10, opts::Output != opts::GNU) << " "
                   << format("%-21s", getTypeString(ObjF->getELFFile()->getHeader()->e_machine, Tag));
     printValue(Tag, Entry.getVal());




More information about the llvm-commits mailing list