Index: include/llvm/ADT/STLExtras.h =================================================================== --- include/llvm/ADT/STLExtras.h +++ include/llvm/ADT/STLExtras.h @@ -1038,6 +1038,11 @@ return std::distance(Range.begin(), Range.end()); } +template +constexpr size_t size(const T (&array)[N]) noexcept { + return N; +} + //===----------------------------------------------------------------------===// // Extra additions to //===----------------------------------------------------------------------===// 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,60 @@ return ELFFile(Object); } +template +Expected ELFFile::dynamicEntries() const { + ArrayRef Dyn; + size_t DynSecSize = 0; + + auto ProgramHeadersOrError = program_headers(); + if (!ProgramHeadersOrError) + return ProgramHeadersOrError.takeError(); + + for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) { + if (Phdr.p_type == ELF::PT_DYNAMIC) { + Dyn = makeArrayRef( + reinterpret_cast(base() + Phdr.p_offset), + Phdr.p_filesz / sizeof(Elf_Dyn)); + DynSecSize = Phdr.p_filesz; + break; + } + } + + // If we can't find the dynamic section in the program headers, we just fall + // back on the sections. + if (Dyn.empty()) { + auto SectionsOrError = sections(); + if (!SectionsOrError) + return SectionsOrError.takeError(); + + for (const Elf_Shdr &Sec : *SectionsOrError) { + if (Sec.sh_type == ELF::SHT_DYNAMIC) { + Expected> DynOrError = + getSectionContentsAsArray(&Sec); + if (!DynOrError) + return DynOrError.takeError(); + Dyn = *DynOrError; + DynSecSize = Sec.sh_size; + break; + } + } + + if (!Dyn.data()) + return ArrayRef(); + } + + if (Dyn.empty()) + return createError("invalid empty dynamic section"); + + if (DynSecSize % sizeof(Elf_Dyn) != 0) + return createError("malformed dynamic section"); + + if (Dyn.back().d_tag != ELF::DT_NULL) + return createError("dynamic sections must be DT_NULL terminated"); + + return Dyn; +} + 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,27 @@ +# RUN: llvm-objdump -p %p/Inputs/private-headers-x86_64.elf | FileCheck %s + +# CHECK: NEEDED libstdc++.so.6 +# CHECK: NEEDED libm.so.6 +# CHECK: NEEDED libgcc_s.so.1 +# CHECK: NEEDED libc.so.6 +# CHECK: INIT 0x00000000000004d8 +# CHECK: FINI 0x00000000000006b4 +# CHECK: INIT_ARRAY 0x0000000000200de8 +# CHECK: INIT_ARRAYSZ 0x0000000000000008 +# CHECK: FINI_ARRAY 0x0000000000200df0 +# CHECK: FINI_ARRAYSZ 0x0000000000000008 +# CHECK: GNU_HASH 0x0000000000000298 +# CHECK: STRTAB 0x0000000000000348 +# CHECK: SYMTAB 0x00000000000002b8 +# CHECK: STRSZ 0x00000000000000a4 +# CHECK: SYMENT 0x0000000000000018 +# CHECK: DEBUG 0x0000000000000000 +# CHECK: PLTGOT 0x0000000000201000 +# CHECK: RELA 0x0000000000000418 +# CHECK: RELASZ 0x00000000000000c0 +# CHECK: RELAENT 0x0000000000000018 +# CHECK: FLAGS_1 0x0000000008000000 +# CHECK: VERNEED 0x00000000000003f8 +# CHECK: VERNEEDNUM 0x0000000000000001 +# CHECK: VERSYM 0x00000000000003ec +# CHECK: RELACOUNT 0x0000000000000003 Index: tools/llvm-objdump/ELFDump.cpp =================================================================== --- tools/llvm-objdump/ELFDump.cpp +++ tools/llvm-objdump/ELFDump.cpp @@ -18,9 +18,103 @@ #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 PPC64_DYNAMIC_TAG(str, val) +#define MIPS_DYNAMIC_TAG(str, val) +#define HEXAGON_DYNAMIC_TAG(str, val) +#define DYNAMIC_TAG_MARKER(str, val) +#define DYNAMIC_TAG(str, val) {#str, val}, +#include "llvm/BinaryFormat/DynamicTags.def" +#undef DYNAMIC_TAG +#undef PPC64_DYNAMIC_TAG +#undef MIPS_DYNAMIC_TAG +#undef HEXAGON_DYNAMIC_TAG +#undef DYNAMIC_TAG_MARKER +}; + +template +Expected getDynamicStrTab(const ELFFile *o, + const typename ELFFile::Elf_Dyn *Dyn, + uint64_t VirtBase) { + typedef ELFFile ELFO; + auto DynamicEntriesOrError = o->dynamicEntries(); + if (!DynamicEntriesOrError) + return DynamicEntriesOrError.takeError() + : + + 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, StringRef Filename) { + 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_error(Filename, DynamicEntriesOrError.takeError()); + + 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 < size(DynamicTagsValues); ++i) { + if (DynamicTagsValues[i].Val == Dyn.d_tag) { + Str = StringRef(DynamicTagsValues[i].ToStr); + break; + } + } + + if (Str.empty()) { + std::string HexStr = utohexstr(static_cast(Dyn.d_tag), true); + outs() << format(" 0x%-19s", HexStr.c_str()); + } else { + // We use "-21" in order to match GNU objdump's output. + 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; + } + warn(errorToErrorCode(StrTabOrErr.takeError()).message()); + 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 +197,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(), Obj->getFileName()); + + // Big-endian 32-bit + if (const ELF32BEObjectFile *ELFObj = dyn_cast(Obj)) + printDynamicSection(ELFObj->getELFFile(), Obj->getFileName()); + + // Little-endian 64-bit + if (const ELF64LEObjectFile *ELFObj = dyn_cast(Obj)) + printDynamicSection(ELFObj->getELFFile(), Obj->getFileName()); + + // Big-endian 64-bit + if (const ELF64BEObjectFile *ELFObj = dyn_cast(Obj)) + printDynamicSection(ELFObj->getELFFile(), Obj->getFileName()); +} 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); @@ -96,6 +97,7 @@ void PrintSectionContents(const object::ObjectFile *o); void PrintSymbolTable(const object::ObjectFile *o, StringRef ArchiveName, StringRef ArchitectureName = StringRef()); +void warn(StringRef Message); LLVM_ATTRIBUTE_NORETURN void error(Twine Message); LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message); LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC); Index: tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- tools/llvm-objdump/llvm-objdump.cpp +++ tools/llvm-objdump/llvm-objdump.cpp @@ -330,6 +330,11 @@ exit(1); } +void llvm::warn(StringRef Message) { + errs() << ToolName << ": warning: " << Message << ".\n"; + errs().flush(); +} + LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, Twine Message) { errs() << ToolName << ": '" << File << "': " << Message << ".\n"; @@ -2123,8 +2128,10 @@ } static void printPrivateFileHeaders(const ObjectFile *o, bool onlyFirst) { - if (o->isELF()) - return printELFFileHeader(o); + if (o->isELF()) { + printELFFileHeader(o); + return printELFDynamicSection(o); + } if (o->isCOFF()) return printCOFFFileHeader(o); if (o->isWasm())