diff --git a/llvm/test/tools/llvm-readobj/elf-dynamic-malformed.test b/llvm/test/tools/llvm-readobj/elf-dynamic-malformed.test --- a/llvm/test/tools/llvm-readobj/elf-dynamic-malformed.test +++ b/llvm/test/tools/llvm-readobj/elf-dynamic-malformed.test @@ -97,22 +97,22 @@ # RUN: llvm-readelf --dynamic-table %t.bad-string | FileCheck %s --check-prefix BAD-STRING-GNU # BAD-STRING-LLVM: 0x000000000000000A STRSZ 1 (bytes) -# BAD-STRING-LLVM: 0x0000000000000001 NEEDED Shared library: -# BAD-STRING-LLVM: 0x000000007FFFFFFF FILTER Filter library: -# BAD-STRING-LLVM: 0x000000007FFFFFFD AUXILIARY Auxiliary library: -# BAD-STRING-LLVM: 0x000000007FFFFFFE USED Not needed object: -# BAD-STRING-LLVM: 0x000000000000000E SONAME Library soname: -# BAD-STRING-LLVM: 0x000000000000000F RPATH Library rpath: -# BAD-STRING-LLVM: 0x000000000000001D RUNPATH Library runpath: +# BAD-STRING-LLVM: 0x0000000000000001 NEEDED Shared library: [] +# BAD-STRING-LLVM: 0x000000007FFFFFFF FILTER Filter library: [] +# BAD-STRING-LLVM: 0x000000007FFFFFFD AUXILIARY Auxiliary library: [] +# BAD-STRING-LLVM: 0x000000007FFFFFFE USED Not needed object: [] +# BAD-STRING-LLVM: 0x000000000000000E SONAME Library soname: [] +# BAD-STRING-LLVM: 0x000000000000000F RPATH Library rpath: [] +# BAD-STRING-LLVM: 0x000000000000001D RUNPATH Library runpath: [] # BAD-STRING-GNU: 0x000000000000000a (STRSZ) 1 (bytes) -# BAD-STRING-GNU: 0x0000000000000001 (NEEDED) Shared library: -# BAD-STRING-GNU: 0x000000007fffffff (FILTER) Filter library: -# BAD-STRING-GNU: 0x000000007ffffffd (AUXILIARY) Auxiliary library: -# BAD-STRING-GNU: 0x000000007ffffffe (USED) Not needed object: -# BAD-STRING-GNU: 0x000000000000000e (SONAME) Library soname: -# BAD-STRING-GNU: 0x000000000000000f (RPATH) Library rpath: -# BAD-STRING-GNU: 0x000000000000001d (RUNPATH) Library runpath: +# BAD-STRING-GNU: 0x0000000000000001 (NEEDED) Shared library: [] +# BAD-STRING-GNU: 0x000000007fffffff (FILTER) Filter library: [] +# BAD-STRING-GNU: 0x000000007ffffffd (AUXILIARY) Auxiliary library: [] +# BAD-STRING-GNU: 0x000000007ffffffe (USED) Not needed object: [] +# BAD-STRING-GNU: 0x000000000000000e (SONAME) Library soname: [] +# BAD-STRING-GNU: 0x000000000000000f (RPATH) Library rpath: [] +# BAD-STRING-GNU: 0x000000000000001d (RUNPATH) Library runpath: [] --- !ELF FileHeader: @@ -168,11 +168,11 @@ # RUN: llvm-readobj --dynamic-table --needed-libs %t.bad-strtab | FileCheck %s --check-prefixes=BAD-STRTAB,BAD-STRTAB-LLVM # RUN: llvm-readelf --dynamic-table --needed-libs %t.bad-strtab | FileCheck %s --check-prefixes=BAD-STRTAB,BAD-STRTAB-GNU -# BAD-STRTAB-LLVM: LoadName: -# BAD-STRTAB-LLVM: 0x0000000000000001 NEEDED Shared library: -# BAD-STRTAB-GNU: 0x0000000000000001 (NEEDED) Shared library: +# BAD-STRTAB-LLVM: LoadName: +# BAD-STRTAB-LLVM: 0x0000000000000001 NEEDED Shared library: [] +# BAD-STRTAB-GNU: 0x0000000000000001 (NEEDED) Shared library: [] # BAD-STRTAB: NeededLibraries [ -# BAD-STRTAB: +# BAD-STRTAB: # BAD-STRTAB: ] --- !ELF diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -224,7 +224,7 @@ DynRegionInfo DynSymRegion; DynRegionInfo DynamicTable; StringRef DynamicStringTable; - StringRef SOName = ""; + std::string SOName = ""; const Elf_Hash *HashTable = nullptr; const Elf_GnuHash *GnuHashTable = nullptr; const Elf_Shdr *DotSymtabSec = nullptr; @@ -290,6 +290,7 @@ StringRef &SectionName, unsigned &SectionIndex) const; std::string getStaticSymbolName(uint32_t Index) const; + std::string getDynamicString(uint64_t Value) const; StringRef getSymbolVersionByIndex(StringRef StrTab, uint32_t VersionSymbolIndex, bool &IsDefault) const; @@ -1630,8 +1631,7 @@ } if (StringTableBegin) DynamicStringTable = StringRef(StringTableBegin, StringTableSize); - if (SONameOffset && SONameOffset < DynamicStringTable.size()) - SOName = DynamicStringTable.data() + SONameOffset; + SOName = getDynamicString(SONameOffset); } template @@ -1951,13 +1951,7 @@ {DT_RPATH, "Library rpath"}, {DT_RUNPATH, "Library runpath"}, }; - OS << TagNames.at(Type) << ": "; - if (DynamicStringTable.empty()) - OS << " "; - else if (Value < DynamicStringTable.size()) - OS << "[" << StringRef(DynamicStringTable.data() + Value) << "]"; - else - OS << ""; + OS << TagNames.at(Type) << ": [" << getDynamicString(Value) << "]"; break; } case DT_FLAGS: @@ -1972,6 +1966,15 @@ } } +template +std::string ELFDumper::getDynamicString(uint64_t Value) const { + if (DynamicStringTable.empty()) + return ""; + else if (Value < DynamicStringTable.size()) + return DynamicStringTable.data() + Value; + return Twine("").str(); +} + template void ELFDumper::printUnwindInfo() { DwarfCFIEH::PrinterContext Ctx(W, ObjF); Ctx.printUnwindInformation(); @@ -1999,17 +2002,10 @@ template void ELFDumper::printNeededLibraries() { ListScope D(W, "NeededLibraries"); - using LibsTy = std::vector; - LibsTy Libs; - + std::vector Libs; for (const auto &Entry : dynamic_table()) - if (Entry.d_tag == ELF::DT_NEEDED) { - uint64_t Value = Entry.d_un.d_val; - if (Value < DynamicStringTable.size()) - Libs.push_back(StringRef(DynamicStringTable.data() + Value)); - else - Libs.push_back(""); - } + if (Entry.d_tag == ELF::DT_NEEDED) + Libs.push_back(getDynamicString(Entry.d_un.d_val)); llvm::stable_sort(Libs);