@@ -63,16 +63,24 @@ static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> &Elf) {
6363 if (!DynamicEntriesOrError)
6464 return DynamicEntriesOrError.takeError ();
6565
66+ typename ELFT::Xword StringTableSize{0 };
67+ const uint8_t *MappedAddr = nullptr ;
6668 for (const typename ELFT::Dyn &Dyn : *DynamicEntriesOrError) {
6769 if (Dyn.d_tag == ELF::DT_STRTAB) {
6870 auto MappedAddrOrError = Elf.toMappedAddr (Dyn.getPtr ());
6971 if (!MappedAddrOrError)
7072 return MappedAddrOrError.takeError ();
71- return StringRef ( reinterpret_cast < const char *>(* MappedAddrOrError)) ;
73+ MappedAddr = * MappedAddrOrError;
7274 }
75+ if (Dyn.d_tag == ELF::DT_STRSZ)
76+ StringTableSize = Dyn.getVal ();
7377 }
78+ if (MappedAddr && StringTableSize)
79+ return StringRef (reinterpret_cast <const char *>(MappedAddr),
80+ StringTableSize);
7481
75- // If the dynamic segment is not present, we fall back on the sections.
82+ // If the dynamic segment is not present, or is missing the important tags, we
83+ // fall back on the sections.
7684 auto SectionsOrError = Elf.sections ();
7785 if (!SectionsOrError)
7886 return SectionsOrError.takeError ();
@@ -221,6 +229,7 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
221229 std::string TagFmt = " %-" + std::to_string (MaxLen) + " s " ;
222230
223231 outs () << " \n Dynamic Section:\n " ;
232+
224233 for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
225234 if (Dyn.d_tag == ELF::DT_NULL)
226235 continue ;
@@ -235,6 +244,14 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
235244 Expected<StringRef> StrTabOrErr = getDynamicStrTab (Elf);
236245 if (StrTabOrErr) {
237246 const char *Data = StrTabOrErr->data ();
247+ if (Dyn.getVal () >= StrTabOrErr->size ()) {
248+ reportWarning (" invalid string table offset, string table size: 0x" +
249+ Twine::utohexstr (StrTabOrErr->size ()),
250+ Obj.getFileName ());
251+ outs () << format (TagFmt.c_str (), Str.c_str ())
252+ << format (Fmt, (uint64_t )Dyn.getVal ());
253+ continue ;
254+ }
238255 outs () << format (TagFmt.c_str (), Str.c_str ()) << Data + Dyn.getVal ()
239256 << " \n " ;
240257 continue ;
0 commit comments