2013-01-06 04:56:49 +01:00
|
|
|
//===-- ELFDump.cpp - ELF-specific dumper -----------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 17:54:18 +02:00
|
|
|
/// This file implements the ELF-specific dumper for llvm-objdump.
|
2013-01-06 04:56:49 +01:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-objdump.h"
|
2013-08-09 00:27:13 +02:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2013-01-06 04:56:49 +01:00
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
2018-07-25 13:09:20 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
template <class ELFT> void printProgramHeaders(const ELFFile<ELFT> *o) {
|
|
|
|
typedef ELFFile<ELFT> ELFO;
|
2013-01-06 04:56:49 +01:00
|
|
|
outs() << "Program Header:\n";
|
2016-11-03 18:28:33 +01:00
|
|
|
auto ProgramHeaderOrError = o->program_headers();
|
2016-11-16 06:10:28 +01:00
|
|
|
if (!ProgramHeaderOrError)
|
|
|
|
report_fatal_error(
|
|
|
|
errorToErrorCode(ProgramHeaderOrError.takeError()).message());
|
2016-11-03 18:28:33 +01:00
|
|
|
for (const typename ELFO::Elf_Phdr &Phdr : *ProgramHeaderOrError) {
|
2015-07-20 15:35:33 +02:00
|
|
|
switch (Phdr.p_type) {
|
2016-12-24 15:53:45 +01:00
|
|
|
case ELF::PT_DYNAMIC:
|
|
|
|
outs() << " DYNAMIC ";
|
2013-01-06 04:56:49 +01:00
|
|
|
break;
|
|
|
|
case ELF::PT_GNU_EH_FRAME:
|
|
|
|
outs() << "EH_FRAME ";
|
|
|
|
break;
|
2017-01-16 23:58:26 +01:00
|
|
|
case ELF::PT_GNU_RELRO:
|
|
|
|
outs() << " RELRO ";
|
|
|
|
break;
|
2016-12-24 15:53:45 +01:00
|
|
|
case ELF::PT_GNU_STACK:
|
|
|
|
outs() << " STACK ";
|
|
|
|
break;
|
2013-02-20 21:18:10 +01:00
|
|
|
case ELF::PT_INTERP:
|
|
|
|
outs() << " INTERP ";
|
|
|
|
break;
|
2016-12-24 15:53:45 +01:00
|
|
|
case ELF::PT_LOAD:
|
|
|
|
outs() << " LOAD ";
|
2013-02-20 21:18:10 +01:00
|
|
|
break;
|
2017-01-17 00:13:46 +01:00
|
|
|
case ELF::PT_NOTE:
|
|
|
|
outs() << " NOTE ";
|
|
|
|
break;
|
2017-01-16 23:01:41 +01:00
|
|
|
case ELF::PT_OPENBSD_BOOTDATA:
|
|
|
|
outs() << " OPENBSD_BOOTDATA ";
|
|
|
|
break;
|
|
|
|
case ELF::PT_OPENBSD_RANDOMIZE:
|
|
|
|
outs() << " OPENBSD_RANDOMIZE ";
|
|
|
|
break;
|
|
|
|
case ELF::PT_OPENBSD_WXNEEDED:
|
|
|
|
outs() << " OPENBSD_WXNEEDED ";
|
|
|
|
break;
|
2013-02-21 03:21:29 +01:00
|
|
|
case ELF::PT_PHDR:
|
|
|
|
outs() << " PHDR ";
|
|
|
|
break;
|
2013-02-27 18:57:17 +01:00
|
|
|
case ELF::PT_TLS:
|
|
|
|
outs() << " TLS ";
|
|
|
|
break;
|
2013-01-06 04:56:49 +01:00
|
|
|
default:
|
|
|
|
outs() << " UNKNOWN ";
|
|
|
|
}
|
|
|
|
|
2013-01-15 08:44:25 +01:00
|
|
|
const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
|
2013-01-06 06:23:59 +01:00
|
|
|
|
2015-07-20 15:35:33 +02:00
|
|
|
outs() << "off " << format(Fmt, (uint64_t)Phdr.p_offset) << "vaddr "
|
|
|
|
<< format(Fmt, (uint64_t)Phdr.p_vaddr) << "paddr "
|
|
|
|
<< format(Fmt, (uint64_t)Phdr.p_paddr)
|
|
|
|
<< format("align 2**%u\n",
|
|
|
|
countTrailingZeros<uint64_t>(Phdr.p_align))
|
|
|
|
<< " filesz " << format(Fmt, (uint64_t)Phdr.p_filesz)
|
|
|
|
<< "memsz " << format(Fmt, (uint64_t)Phdr.p_memsz) << "flags "
|
|
|
|
<< ((Phdr.p_flags & ELF::PF_R) ? "r" : "-")
|
|
|
|
<< ((Phdr.p_flags & ELF::PF_W) ? "w" : "-")
|
|
|
|
<< ((Phdr.p_flags & ELF::PF_X) ? "x" : "-") << "\n";
|
2013-01-06 04:56:49 +01:00
|
|
|
}
|
|
|
|
outs() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::printELFFileHeader(const object::ObjectFile *Obj) {
|
|
|
|
// Little-endian 32-bit
|
2013-05-09 15:13:28 +02:00
|
|
|
if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
|
2013-08-09 00:27:13 +02:00
|
|
|
printProgramHeaders(ELFObj->getELFFile());
|
2013-01-06 04:56:49 +01:00
|
|
|
|
|
|
|
// Big-endian 32-bit
|
2013-05-09 15:13:28 +02:00
|
|
|
if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
|
2013-08-09 00:27:13 +02:00
|
|
|
printProgramHeaders(ELFObj->getELFFile());
|
2013-01-06 04:56:49 +01:00
|
|
|
|
|
|
|
// Little-endian 64-bit
|
2013-05-09 15:13:28 +02:00
|
|
|
if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
|
2013-08-09 00:27:13 +02:00
|
|
|
printProgramHeaders(ELFObj->getELFFile());
|
2013-01-06 04:56:49 +01:00
|
|
|
|
|
|
|
// Big-endian 64-bit
|
2013-05-09 15:13:28 +02:00
|
|
|
if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
|
2013-08-09 00:27:13 +02:00
|
|
|
printProgramHeaders(ELFObj->getELFFile());
|
2013-01-06 04:56:49 +01:00
|
|
|
}
|
2018-07-25 13:09:20 +02:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|