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,10 @@ Expected sections() const; + Expected dynamicEntries() const; + + Expected toMappedAddr(uint64_t VAddr) const; + Expected symbols(const Elf_Shdr *Sec) const { if (!Sec) return makeArrayRef(nullptr, nullptr); Index: lib/Object/ELF.cpp =================================================================== --- lib/Object/ELF.cpp +++ lib/Object/ELF.cpp @@ -420,6 +420,144 @@ return Relocs; } +template +const char *ELFFile::getDynamicTagAsString(unsigned Arch, + uint64_t Type) const { +#define DYNAMIC_STRINGIFY_ENUM(tag, value) \ + case value: \ + return #tag; + +#define DYNAMIC_TAG(n, v) + switch (Arch) { + case ELF::EM_HEXAGON: + switch (Type) { +#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) +#include "llvm/BinaryFormat/DynamicTags.def" +#undef HEXAGON_DYNAMIC_TAG + } + + case ELF::EM_MIPS: + switch (Type) { +#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) +#include "llvm/BinaryFormat/DynamicTags.def" +#undef MIPS_DYNAMIC_TAG + } + + case ELF::EM_PPC64: + switch (Type) { +#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) +#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) DYNAMIC_STRINGIFY_ENUM(name, value) +#include "llvm/BinaryFormat/DynamicTags.def" +#undef DYNAMIC_TAG +#undef MIPS_DYNAMIC_TAG +#undef HEXAGON_DYNAMIC_TAG +#undef PPC64_DYNAMIC_TAG +#undef DYNAMIC_TAG_MARKER +#undef DYNAMIC_STRINGIFY_ENUM + default: + return "unknown"; + } +} + +template +const char *ELFFile::getDynamicTagAsString(uint64_t Type) const { + return getDynamicTagAsString(getHeader()->e_machine, Type); +} + +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::toMappedAddr(uint64_t VAddr) const { + auto ProgramHeadersOrError = program_headers(); + if (!ProgramHeadersOrError) + return ProgramHeadersOrError.takeError(); + + llvm::SmallVector LoadSegments; + + for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) + if (Phdr.p_type == ELF::PT_LOAD) + LoadSegments.push_back(const_cast(&Phdr)); + + const Elf_Phdr *const *I = + std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr, + [](uint64_t VAddr, const Elf_Phdr_Impl *Phdr) { + return VAddr < Phdr->p_vaddr; + }); + + if (I == LoadSegments.begin()) + return createError("Virtual address is not in any segment"); + --I; + const Elf_Phdr &Phdr = **I; + uint64_t Delta = VAddr - Phdr.p_vaddr; + if (Delta >= Phdr.p_filesz) + return createError("Virtual address is not in any segment"); + return base() + Phdr.p_offset + Delta; +} + template class llvm::object::ELFFile; template class llvm::object::ELFFile; template class llvm::object::ELFFile; Index: test/tools/llvm-objdump/private-headers-dynamic-section.test =================================================================== --- /dev/null +++ test/tools/llvm-objdump/private-headers-dynamic-section.test @@ -0,0 +1,35 @@ +# RUN: llvm-objdump -p %p/Inputs/private-headers-x86_64.elf | FileCheck %s +# RUN: llvm-objcopy --strip-sections %p/Inputs/private-headers-x86_64.elf %t +# RUN: llvm-objdump -p %t | 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 +# CHECK: RELRSZ 0x0000000000000014 +# CHECK: RELR 0x0000000087654321 +# CHECK: RELRENT 0x0000000000000010 Index: test/tools/llvm-objdump/private-headers-no-dynamic-segment.test =================================================================== --- /dev/null +++ test/tools/llvm-objdump/private-headers-no-dynamic-segment.test @@ -0,0 +1,42 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objdump -p %t | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .dynamic + Type: SHT_DYNAMIC + Flags: [ SHF_ALLOC, SHF_WRITE ] + Content: 0c00000000000000a0060000000000000d0000000000000024090000000000001900000000000000a80d2000000000001b0000000000000010000000000000001a00000000000000b80d2000000000001c000000000000000800000000000000f5feff6f0000000098020000000000000500000000000000c8030000000000000600000000000000c0020000000000000a000000000000002f010000000000000b0000000000000018000000000000001500000000000000000000000000000003000000000000000010200000000000020000000000000048000000000000001400000000000000070000000000000017000000000000005806000000000000070000000000000050050000000000000800000000000000080100000000000009000000000000001800000000000000fbffff6f000000000000000800000000feffff6f000000001005000000000000ffffff6f000000000200000000000000f0ffff6f00000000f804000000000000f9ffff6f0000000004000000000000002300000000000000140000000000000024000000000000002143658700000000250000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000 + +# 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 +# CHECK: RELRSZ 0x0000000000000014 +# CHECK: RELR 0x0000000087654321 +# CHECK: RELRENT 0x0000000000000010 Index: test/tools/llvm-objdump/private-headers-no-dynamic.test =================================================================== --- /dev/null +++ test/tools/llvm-objdump/private-headers-no-dynamic.test @@ -0,0 +1,19 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objdump -p %t | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x1000 + AddressAlign: 0x0000000000000010 + Size: 64 + +# CHECK: Dynamic Section: +# CHECK-NOT: {{.}} Index: tools/llvm-objdump/ELFDump.cpp =================================================================== --- tools/llvm-objdump/ELFDump.cpp +++ tools/llvm-objdump/ELFDump.cpp @@ -21,6 +21,77 @@ using namespace llvm; using namespace llvm::object; +template +Expected getDynamicStrTab(const ELFFile *Elf) { + typedef ELFFile ELFO; + + auto DynamicEntriesOrError = Elf->dynamicEntries(); + if (!DynamicEntriesOrError) + return DynamicEntriesOrError.takeError(); + + for (const typename ELFO::Elf_Dyn &Dyn : *DynamicEntriesOrError) { + if (Dyn.d_tag == ELF::DT_STRTAB) { + auto MappedAddrOrError = Elf->toMappedAddr(Dyn.getPtr()); + if (!MappedAddrOrError) + consumeError(MappedAddrOrError.takeError()); + return StringRef(reinterpret_cast(*MappedAddrOrError)); + } + } + + // If the dynamic segment is not present, we fall back on the sections. + 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("dynamic string table not found"); +} + +template +void printDynamicSection(const ELFFile *Elf, StringRef Filename) { + auto ProgramHeaderOrError = Elf->program_headers(); + if (!ProgramHeaderOrError) + report_error(Filename, ProgramHeaderOrError.takeError()); + + auto DynamicEntriesOrError = Elf->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(Elf->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(Elf); + 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 +174,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 @@ -79,6 +79,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 @@ -2153,8 +2153,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())