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 @@ -115,6 +115,9 @@ SmallVectorImpl &Result) const; uint32_t getRelrRelocationType() const; + const char *getDynamicTagAsString(unsigned Arch, uint64_t Type) const; + const char *getDynamicTagAsString(uint64_t Type) const; + /// Get the symbol for a given relocation. Expected getRelocationSymbol(const Elf_Rel *Rel, const Elf_Shdr *SymTab) const; @@ -133,6 +136,8 @@ Expected sections() const; + Expected dynamicEntries() const; + Expected symbols(const Elf_Shdr *Sec) const { if (!Sec) return makeArrayRef(nullptr, nullptr); @@ -412,6 +417,65 @@ return getELFRelrRelocationType(getHeader()->e_machine); } +template +const char *ELFFile::getDynamicTagAsString(unsigned Arch, + uint64_t Type) const { +#define DYNAMIC_TAG(n, v) + switch (Arch) { + case ELF::EM_HEXAGON: + switch (Type) { +#define HEXAGON_DYNAMIC_TAG(name, value) \ + case ELF::DT_##name: \ + return #name; +#include "llvm/BinaryFormat/DynamicTags.def" +#undef HEXAGON_DYNAMIC_TAG + } + + case ELF::EM_MIPS: + switch (Type) { +#define MIPS_DYNAMIC_TAG(name, value) \ + case ELF::DT_##name: \ + return #name; +#include "llvm/BinaryFormat/DynamicTags.def" +#undef MIPS_DYNAMIC_TAG + } + + case ELF::EM_PPC64: + switch (Type) { +#define PPC64_DYNAMIC_TAG(name, value) \ + case ELF::DT_##name: \ + return #name; +#include "llvm/BinaryFormat/DynamicTags.def" +#undef PPC64_DYNAMIC_TAG + } + } +#undef DYNAMIC_TAG + switch (Type) { +// Now handle all dynamic tags except the architecture specific ones +#define MIPS_DYNAMIC_TAG(name, value) +#define HEXAGON_DYNAMIC_TAG(name, value) +#define PPC64_DYNAMIC_TAG(name, value) +// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc. +#define DYNAMIC_TAG_MARKER(name, value) +#define DYNAMIC_TAG(name, value) \ + case ELF::DT_##name: \ + return #name; +#include "llvm/BinaryFormat/DynamicTags.def" +#undef DYNAMIC_TAG +#undef MIPS_DYNAMIC_TAG +#undef HEXAGON_DYNAMIC_TAG +#undef PPC64_DYNAMIC_TAG +#undef DYNAMIC_TAG_MARKER + default: + return "unknown"; + } +} + +template +const char *ELFFile::getDynamicTagAsString(uint64_t Type) const { + return getDynamicTagAsString(getHeader()->e_machine, Type); +} + template Expected ELFFile::getRelocationSymbol(const Elf_Rel *Rel, @@ -445,6 +509,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,30 @@ +# 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 0x00000000000006a0 +# CHECK: FINI 0x0000000000000924 +# CHECK: INIT_ARRAY 0x0000000000200da8 +# CHECK: INIT_ARRAYSZ 0x0000000000000010 +# CHECK: FINI_ARRAY 0x0000000000200db8 +# CHECK: FINI_ARRAYSZ 0x0000000000000008 +# CHECK: GNU_HASH 0x0000000000000298 +# CHECK: STRTAB 0x00000000000003c8 +# CHECK: SYMTAB 0x00000000000002c0 +# CHECK: STRSZ 0x000000000000012f +# CHECK: SYMENT 0x0000000000000018 +# CHECK: DEBUG 0x0000000000000000 +# CHECK: PLTGOT 0x0000000000201000 +# CHECK: PLTRELSZ 0x0000000000000048 +# CHECK: PLTREL 0x0000000000000007 +# CHECK: JMPREL 0x0000000000000658 +# CHECK: RELA 0x0000000000000550 +# CHECK: RELASZ 0x0000000000000108 +# CHECK: RELAENT 0x0000000000000018 +# CHECK: FLAGS_1 0x0000000008000000 +# CHECK: VERNEED 0x0000000000000510 +# CHECK: VERNEEDNUM 0x0000000000000002 +# CHECK: VERSYM 0x00000000000004f8 +# CHECK: RELACOUNT 0x0000000000000004 Index: tools/llvm-objdump/ELFDump.cpp =================================================================== --- tools/llvm-objdump/ELFDump.cpp +++ tools/llvm-objdump/ELFDump.cpp @@ -21,6 +21,64 @@ using namespace llvm; using namespace llvm::object; +template +Expected getDynamicStrTab(const ELFFile *Elf) { + typedef ELFFile ELFO; + + auto SectionsOrError = Elf->sections(); + if (!SectionsOrError) + return SectionsOrError.takeError(); + + for (const typename ELFO::Elf_Shdr &Sec : *SectionsOrError) { + if (Sec.sh_type == ELF::SHT_DYNSYM) + return Elf->getStringTableForSymtab(Sec); + } + + return createError("SHT_DYNSYM entry not present"); +} + +template +void printDynamicSection(const ELFFile *o, StringRef Filename) { + typedef ELFFile ELFO; + auto ProgramHeaderOrError = o->program_headers(); + if (!ProgramHeaderOrError) + report_error(Filename, ProgramHeaderOrError.takeError()); + + 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 = StringRef(o->getDynamicTagAsString(Dyn.d_tag)); + + 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); + 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 +161,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())