[llvm] r337902 - [llvm-objdump] Add dynamic section printing to private-headers option
Paul Semel via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 25 04:09:20 PDT 2018
Author: paulsemel
Date: Wed Jul 25 04:09:20 2018
New Revision: 337902
URL: http://llvm.org/viewvc/llvm-project?rev=337902&view=rev
Log:
[llvm-objdump] Add dynamic section printing to private-headers option
Differential Revision: https://reviews.llvm.org/D49016
Added:
llvm/trunk/test/tools/llvm-objdump/Inputs/private-headers-x86_64.elf (with props)
llvm/trunk/test/tools/llvm-objdump/private-headers-dynamic-section.test
llvm/trunk/test/tools/llvm-objdump/private-headers-no-dynamic-segment.test
llvm/trunk/test/tools/llvm-objdump/private-headers-no-dynamic.test
Modified:
llvm/trunk/include/llvm/Object/ELF.h
llvm/trunk/lib/Object/ELF.cpp
llvm/trunk/tools/llvm-objdump/ELFDump.cpp
llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
llvm/trunk/tools/llvm-objdump/llvm-objdump.h
Modified: llvm/trunk/include/llvm/Object/ELF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ELF.h?rev=337902&r1=337901&r2=337902&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ELF.h (original)
+++ llvm/trunk/include/llvm/Object/ELF.h Wed Jul 25 04:09:20 2018
@@ -115,6 +115,9 @@ public:
SmallVectorImpl<char> &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<const Elf_Sym *> getRelocationSymbol(const Elf_Rel *Rel,
const Elf_Shdr *SymTab) const;
@@ -133,6 +136,10 @@ public:
Expected<Elf_Shdr_Range> sections() const;
+ Expected<Elf_Dyn_Range> dynamicEntries() const;
+
+ Expected<const uint8_t *> toMappedAddr(uint64_t VAddr) const;
+
Expected<Elf_Sym_Range> symbols(const Elf_Shdr *Sec) const {
if (!Sec)
return makeArrayRef<Elf_Sym>(nullptr, nullptr);
Modified: llvm/trunk/lib/Object/ELF.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/ELF.cpp?rev=337902&r1=337901&r2=337902&view=diff
==============================================================================
--- llvm/trunk/lib/Object/ELF.cpp (original)
+++ llvm/trunk/lib/Object/ELF.cpp Wed Jul 25 04:09:20 2018
@@ -420,6 +420,144 @@ ELFFile<ELFT>::android_relas(const Elf_S
return Relocs;
}
+template <class ELFT>
+const char *ELFFile<ELFT>::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 <class ELFT>
+const char *ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
+ return getDynamicTagAsString(getHeader()->e_machine, Type);
+}
+
+template <class ELFT>
+Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
+ ArrayRef<Elf_Dyn> 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<const Elf_Dyn *>(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<ArrayRef<Elf_Dyn>> DynOrError =
+ getSectionContentsAsArray<Elf_Dyn>(&Sec);
+ if (!DynOrError)
+ return DynOrError.takeError();
+ Dyn = *DynOrError;
+ DynSecSize = Sec.sh_size;
+ break;
+ }
+ }
+
+ if (!Dyn.data())
+ return ArrayRef<Elf_Dyn>();
+ }
+
+ 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 <class ELFT>
+Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
+ auto ProgramHeadersOrError = program_headers();
+ if (!ProgramHeadersOrError)
+ return ProgramHeadersOrError.takeError();
+
+ llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
+
+ for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
+ if (Phdr.p_type == ELF::PT_LOAD)
+ LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
+
+ const Elf_Phdr *const *I =
+ std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
+ [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *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<ELF32LE>;
template class llvm::object::ELFFile<ELF32BE>;
template class llvm::object::ELFFile<ELF64LE>;
Added: llvm/trunk/test/tools/llvm-objdump/Inputs/private-headers-x86_64.elf
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/Inputs/private-headers-x86_64.elf?rev=337902&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-objdump/Inputs/private-headers-x86_64.elf (added) and llvm/trunk/test/tools/llvm-objdump/Inputs/private-headers-x86_64.elf Wed Jul 25 04:09:20 2018 differ
Propchange: llvm/trunk/test/tools/llvm-objdump/Inputs/private-headers-x86_64.elf
------------------------------------------------------------------------------
svn:executable = *
Added: llvm/trunk/test/tools/llvm-objdump/private-headers-dynamic-section.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/private-headers-dynamic-section.test?rev=337902&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/private-headers-dynamic-section.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/private-headers-dynamic-section.test Wed Jul 25 04:09:20 2018
@@ -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
Added: llvm/trunk/test/tools/llvm-objdump/private-headers-no-dynamic-segment.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/private-headers-no-dynamic-segment.test?rev=337902&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/private-headers-no-dynamic-segment.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/private-headers-no-dynamic-segment.test Wed Jul 25 04:09:20 2018
@@ -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
Added: llvm/trunk/test/tools/llvm-objdump/private-headers-no-dynamic.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/private-headers-no-dynamic.test?rev=337902&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/private-headers-no-dynamic.test (added)
+++ llvm/trunk/test/tools/llvm-objdump/private-headers-no-dynamic.test Wed Jul 25 04:09:20 2018
@@ -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: {{.}}
Modified: llvm/trunk/tools/llvm-objdump/ELFDump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/ELFDump.cpp?rev=337902&r1=337901&r2=337902&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/ELFDump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/ELFDump.cpp Wed Jul 25 04:09:20 2018
@@ -21,6 +21,77 @@
using namespace llvm;
using namespace llvm::object;
+template <class ELFT>
+Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> *Elf) {
+ typedef ELFFile<ELFT> 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<const char *>(*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 <class ELFT>
+void printDynamicSection(const ELFFile<ELFT> *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<uint64_t>(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<StringRef> 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 <class ELFT> void printProgramHeaders(const ELFFile<ELFT> *o) {
typedef ELFFile<ELFT> ELFO;
outs() << "Program Header:\n";
@@ -103,3 +174,21 @@ void llvm::printELFFileHeader(const obje
if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
printProgramHeaders(ELFObj->getELFFile());
}
+
+void llvm::printELFDynamicSection(const object::ObjectFile *Obj) {
+ // Little-endian 32-bit
+ if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
+ printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
+
+ // Big-endian 32-bit
+ if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
+ printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
+
+ // Little-endian 64-bit
+ if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
+ printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
+
+ // Big-endian 64-bit
+ if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
+ printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
+}
Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=337902&r1=337901&r2=337902&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Wed Jul 25 04:09:20 2018
@@ -2153,8 +2153,10 @@ static void printFaultMaps(const ObjectF
}
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())
Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.h?rev=337902&r1=337901&r2=337902&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.h (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.h Wed Jul 25 04:09:20 2018
@@ -79,6 +79,7 @@ void printMachOBindTable(object::MachOOb
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);
More information about the llvm-commits
mailing list