14
14
#include " ELFDump.h"
15
15
16
16
#include " llvm-objdump.h"
17
+ #include " llvm/BinaryFormat/ELF.h"
17
18
#include " llvm/Demangle/Demangle.h"
19
+ #include " llvm/Object/ELF.h"
18
20
#include " llvm/Object/ELFObjectFile.h"
21
+ #include " llvm/Object/ELFTypes.h"
19
22
#include " llvm/Support/Format.h"
20
23
#include " llvm/Support/raw_ostream.h"
21
24
@@ -221,6 +224,20 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
221
224
std::string TagFmt = " %-" + std::to_string (MaxLen) + " s " ;
222
225
223
226
outs () << " \n Dynamic Section:\n " ;
227
+ auto StringTableSize = (typename ELFT::Xword)0 ;
228
+ for (const auto &Sec : cantFail (Elf.sections ())) {
229
+ if (Sec.sh_type == ELF::SHT_STRTAB)
230
+ StringTableSize =
231
+ StringTableSize < Sec.sh_size ? Sec.sh_size : StringTableSize;
232
+ }
233
+ for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
234
+ if (Dyn.d_tag == ELF::DT_STRSZ) {
235
+ StringTableSize =
236
+ StringTableSize < Dyn.getVal () ? Dyn.getVal () : StringTableSize;
237
+ break ;
238
+ }
239
+ }
240
+
224
241
for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
225
242
if (Dyn.d_tag == ELF::DT_NULL)
226
243
continue ;
@@ -235,6 +252,12 @@ template <class ELFT> void ELFDumper<ELFT>::printDynamicSection() {
235
252
Expected<StringRef> StrTabOrErr = getDynamicStrTab (Elf);
236
253
if (StrTabOrErr) {
237
254
const char *Data = StrTabOrErr->data ();
255
+ if (Dyn.getVal () > StringTableSize) {
256
+ reportWarning (" Invalid string table offset" , Obj.getFileName ());
257
+ outs () << format (TagFmt.c_str (), Str.c_str ())
258
+ << format (Fmt, (uint64_t )Dyn.getVal ());
259
+ continue ;
260
+ }
238
261
outs () << format (TagFmt.c_str (), Str.c_str ()) << Data + Dyn.getVal ()
239
262
<< " \n " ;
240
263
continue ;
0 commit comments