Index: include/llvm/Object/ELF.h =================================================================== --- include/llvm/Object/ELF.h +++ include/llvm/Object/ELF.h @@ -133,6 +133,8 @@ Expected sections() const; + Expected dynamicEntries() const; + Expected symbols(const Elf_Shdr *Sec) const { if (!Sec) return makeArrayRef(nullptr, nullptr); @@ -445,6 +447,37 @@ return ELFFile(Object); } +template +Expected ELFFile::dynamicEntries() const { + const Elf_Dyn *Dyn = nullptr; + size_t DynSecSize = 0; + auto SectionsOrError = sections(); + if (!SectionsOrError) + return SectionsOrError.takeError(); + + for (auto &Sec : *SectionsOrError) { + if (Sec.sh_type == ELF::SHT_DYNAMIC) { + Dyn = reinterpret_cast(base() + Sec.sh_offset); + DynSecSize = Sec.sh_size; + break; + } + } + + if (!Dyn) + return ArrayRef(); + + size_t numEntry = DynSecSize / sizeof(Elf_Dyn); + + if (!numEntry) + return createError("invalid dynamic number of entries"); + + const Elf_Dyn *End = Dyn + (numEntry - 1); + if (End->d_tag != ELF::DT_NULL) + return createError("dynamic sections must be DT_NULL terminated"); + + return makeArrayRef(Dyn, numEntry); +} + template Expected ELFFile::sections() const { const uintX_t SectionTableOffset = getHeader()->e_shoff; Index: test/tools/llvm-objdump/private-headers-dynamic-section.test =================================================================== --- /dev/null +++ test/tools/llvm-objdump/private-headers-dynamic-section.test @@ -0,0 +1,34 @@ +# RUN: llvm-objdump -p %p/Inputs/eh_frame.elf-mipsel | FileCheck %s + +# CHECK: Dynamic Section: +# CHECK: NEEDED libstdc++.so.6 +# CHECK: NEEDED libm.so.6 +# CHECK: NEEDED libgcc_s.so.1 +# CHECK: NEEDED libc.so.6 +# CHECK: INIT 0x00400660 +# CHECK: FINI 0x004009f0 +# CHECK: HASH 0x00400328 +# CHECK: STRTAB 0x0040047c +# CHECK: SYMTAB 0x0040037c +# CHECK: STRSZ 0x00000144 +# CHECK: SYMENT 0x00000010 +# CHECK: 0x70000016 0x00410ba0 +# CHECK: DEBUG 0x00000000 +# CHECK: PLTGOT 0x00410bd0 +# CHECK: REL 0x00400620 +# CHECK: RELSZ 0x00000018 +# CHECK: RELENT 0x00000008 +# CHECK: 0x70000001 0x00000001 +# CHECK: 0x70000005 0x00000002 +# CHECK: 0x70000006 0x00400000 +# CHECK: 0x7000000a 0x00000005 +# CHECK: 0x70000011 0x00000010 +# CHECK: 0x70000012 0x00000025 +# CHECK: 0x70000013 0x0000000e +# CHECK: PLTREL 0x00000011 +# CHECK: JMPREL 0x00400638 +# CHECK: PLTRELSZ 0x00000028 +# CHECK: 0x70000032 0x00410b64 +# CHECK: VERNEED 0x004005e0 +# CHECK: VERNEEDNUM 0x00000002 +# CHECK: VERSYM 0x004005c0 Index: tools/llvm-objdump/ELFDump.cpp =================================================================== --- tools/llvm-objdump/ELFDump.cpp +++ tools/llvm-objdump/ELFDump.cpp @@ -18,9 +18,93 @@ #include "llvm/Support/MathExtras.h" #include "llvm/Support/raw_ostream.h" +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) + using namespace llvm; using namespace llvm::object; +static const struct { + const char *ToStr; + int64_t Val; +} DynamicTagsValues[] = { +#define DYNAMIC_TAG(str, val) {#str, val}, +#include "llvm/BinaryFormat/DynamicTags.def" +#undef DYNAMIC_TAG +}; + +template +Expected getDynamicStrTab(const ELFFile *o, + const typename ELFFile::Elf_Dyn *Dyn, + uint64_t VirtBase) { + typedef ELFFile ELFO; + auto DynamicEntriesOrError = o->dynamicEntries(); + if (!DynamicEntriesOrError) + report_fatal_error( + errorToErrorCode(DynamicEntriesOrError.takeError()).message()); + + for (const typename ELFO::Elf_Dyn &Dyn : *DynamicEntriesOrError) { + if (Dyn.d_tag == ELF::DT_STRTAB) + return StringRef(reinterpret_cast( + o->base() + (Dyn.d_un.d_val - VirtBase))); + } + + return createError("DT_STRTAB entry not present"); +} + +template void printDynamicSection(const ELFFile *o) { + typedef ELFFile ELFO; + auto ProgramHeaderOrError = o->program_headers(); + uint64_t VirtBase = 0; + if (!ProgramHeaderOrError) + report_fatal_error( + errorToErrorCode(ProgramHeaderOrError.takeError()).message()); + + for (const typename ELFO::Elf_Phdr &Phdr : *ProgramHeaderOrError) { + if (Phdr.p_type == ELF::PT_PHDR) { + VirtBase = Phdr.p_vaddr - Phdr.p_offset; + break; + } + } + + auto DynamicEntriesOrError = o->dynamicEntries(); + if (!DynamicEntriesOrError) + report_fatal_error( + errorToErrorCode(DynamicEntriesOrError.takeError()).message()); + + outs() << "Dynamic Section:\n"; + for (const auto &Dyn : *DynamicEntriesOrError) { + if (Dyn.d_tag == ELF::DT_NULL) + continue; + StringRef Str; + for (size_t i = 0; i < ARRAY_SIZE(DynamicTagsValues); ++i) { + if (DynamicTagsValues[i].Val == Dyn.d_tag) { + Str = StringRef(DynamicTagsValues[i].ToStr); + break; + } + } + + if (Str.empty()) + outs() << utohexstr(static_cast(Dyn.d_tag), true); + else { + // We want to fit with GNU objdump out + outs() << format(" %-21s", Str.data()); + } + + const char *Fmt = + ELFT::Is64Bits ? "0x%016" PRIx64 "\n" : "0x%08" PRIx64 "\n"; + if (Dyn.d_tag == ELF::DT_NEEDED) { + Expected StrTabOrErr = getDynamicStrTab(o, &Dyn, VirtBase); + if (StrTabOrErr) { + const char *Data = StrTabOrErr.get().data(); + outs() << (Data + Dyn.d_un.d_val) << "\n"; + continue; + } else + consumeError(StrTabOrErr.takeError()); + } + outs() << format(Fmt, (uint64_t)Dyn.d_un.d_val); + } +} + template void printProgramHeaders(const ELFFile *o) { typedef ELFFile ELFO; outs() << "Program Header:\n"; @@ -103,3 +187,21 @@ if (const ELF64BEObjectFile *ELFObj = dyn_cast(Obj)) printProgramHeaders(ELFObj->getELFFile()); } + +void llvm::printELFDynamicSection(const object::ObjectFile *Obj) { + // Little-endian 32-bit + if (const ELF32LEObjectFile *ELFObj = dyn_cast(Obj)) + printDynamicSection(ELFObj->getELFFile()); + + // Big-endian 32-bit + if (const ELF32BEObjectFile *ELFObj = dyn_cast(Obj)) + printDynamicSection(ELFObj->getELFFile()); + + // Little-endian 64-bit + if (const ELF64LEObjectFile *ELFObj = dyn_cast(Obj)) + printDynamicSection(ELFObj->getELFFile()); + + // Big-endian 64-bit + if (const ELF64BEObjectFile *ELFObj = dyn_cast(Obj)) + printDynamicSection(ELFObj->getELFFile()); +} Index: tools/llvm-objdump/llvm-objdump.h =================================================================== --- tools/llvm-objdump/llvm-objdump.h +++ tools/llvm-objdump/llvm-objdump.h @@ -78,6 +78,7 @@ void printMachOLazyBindTable(object::MachOObjectFile* o); void printMachOWeakBindTable(object::MachOObjectFile* o); void printELFFileHeader(const object::ObjectFile *o); +void printELFDynamicSection(const object::ObjectFile *Obj); void printCOFFFileHeader(const object::ObjectFile *o); void printCOFFSymbolTable(const object::COFFImportFile *i); void printCOFFSymbolTable(const object::COFFObjectFile *o); Index: tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- tools/llvm-objdump/llvm-objdump.cpp +++ tools/llvm-objdump/llvm-objdump.cpp @@ -2123,8 +2123,10 @@ } static void printPrivateFileHeaders(const ObjectFile *o, bool onlyFirst) { - if (o->isELF()) - return printELFFileHeader(o); + if (o->isELF()) { + printELFFileHeader(o); + printELFDynamicSection(o); + } if (o->isCOFF()) return printCOFFFileHeader(o); if (o->isWasm())