[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
llvm-svn: 355073
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 3597acf..410ab3f 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -1914,35 +1914,29 @@
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());