@@ -63,14 +63,22 @@ 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;
74
+ }
75
+ if (Dyn.d_tag == ELF::DT_STRSZ) {
76
+ StringTableSize = Dyn.getVal ();
72
77
}
73
78
}
79
+ if (MappedAddr && StringTableSize)
80
+ return StringRef (reinterpret_cast <const char *>(MappedAddr),
81
+ StringTableSize);
74
82
75
83
// If the dynamic segment is not present, we fall back on the sections.
76
84
auto SectionsOrError = Elf.sections ();
@@ -221,28 +229,6 @@ 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 " ;
224
- typename ELFT::Xword StringTableSize{0 };
225
- for (const typename ELFT::Shdr &Sec : cantFail (Elf.sections ())) {
226
- if (Sec.sh_type == ELF::SHT_DYNAMIC || Sec.sh_type == ELF::SHT_DYNSYM) {
227
- Expected<const typename ELFT::Shdr *> StringTableSecOrError =
228
- getSection<ELFT>(cantFail (Elf.sections ()), Sec.sh_link );
229
- if (!StringTableSecOrError) {
230
- reportWarning (toString (StringTableSecOrError.takeError ()),
231
- Obj.getFileName ());
232
- continue ;
233
- }
234
- StringTableSize = StringTableSize < (*StringTableSecOrError)->sh_size
235
- ? (*StringTableSecOrError)->sh_size
236
- : StringTableSize;
237
- }
238
- }
239
- for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
240
- if (Dyn.d_tag == ELF::DT_STRSZ) {
241
- StringTableSize =
242
- StringTableSize < Dyn.getVal () ? Dyn.getVal () : StringTableSize;
243
- break ;
244
- }
245
- }
246
232
247
233
for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
248
234
if (Dyn.d_tag == ELF::DT_NULL)
@@ -258,9 +244,9 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
258
244
Expected<StringRef> StrTabOrErr = getDynamicStrTab (Elf);
259
245
if (StrTabOrErr) {
260
246
const char *Data = StrTabOrErr->data ();
261
- if (Dyn.getVal () >= StringTableSize ) {
247
+ if (Dyn.getVal () >= StrTabOrErr-> size () ) {
262
248
reportWarning (" invalid string table offset, string table size: 0x" +
263
- Twine::utohexstr (StringTableSize ),
249
+ Twine::utohexstr (StrTabOrErr-> size () ),
264
250
Obj.getFileName ());
265
251
outs () << format (TagFmt.c_str (), Str.c_str ())
266
252
<< format (Fmt, (uint64_t )Dyn.getVal ());
0 commit comments