@@ -63,16 +63,24 @@ static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> &Elf) {
63
63
if (!DynamicEntriesOrError)
64
64
return DynamicEntriesOrError.takeError ();
65
65
66
+ typename ELFT::Xword StringTableSize{0 };
67
+ const uint8_t *MappedAddr = nullptr ;
66
68
for (const typename ELFT::Dyn &Dyn : *DynamicEntriesOrError) {
67
69
if (Dyn.d_tag == ELF::DT_STRTAB) {
68
70
auto MappedAddrOrError = Elf.toMappedAddr (Dyn.getPtr ());
69
71
if (!MappedAddrOrError)
70
72
return MappedAddrOrError.takeError ();
71
- return StringRef ( reinterpret_cast < const char *>(* MappedAddrOrError)) ;
73
+ MappedAddr = * MappedAddrOrError;
72
74
}
75
+ if (Dyn.d_tag == ELF::DT_STRSZ)
76
+ StringTableSize = Dyn.getVal ();
73
77
}
78
+ if (MappedAddr && StringTableSize)
79
+ return StringRef (reinterpret_cast <const char *>(MappedAddr),
80
+ StringTableSize);
74
81
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.
76
84
auto SectionsOrError = Elf.sections ();
77
85
if (!SectionsOrError)
78
86
return SectionsOrError.takeError ();
@@ -221,6 +229,7 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
221
229
std::string TagFmt = " %-" + std::to_string (MaxLen) + " s " ;
222
230
223
231
outs () << " \n Dynamic Section:\n " ;
232
+
224
233
for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
225
234
if (Dyn.d_tag == ELF::DT_NULL)
226
235
continue ;
@@ -235,6 +244,14 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
235
244
Expected<StringRef> StrTabOrErr = getDynamicStrTab (Elf);
236
245
if (StrTabOrErr) {
237
246
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
+ }
238
255
outs () << format (TagFmt.c_str (), Str.c_str ()) << Data + Dyn.getVal ()
239
256
<< " \n " ;
240
257
continue ;
0 commit comments