1 //===-- ELFDump.cpp - ELF-specific dumper -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements the ELF-specific dumper for llvm-objdump.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-objdump.h"
16 #include "llvm/Object/ELFObjectFile.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/MathExtras.h"
19 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm::object;
24 template <class ELFT> void printProgramHeaders(const ELFFile<ELFT> *o) {
25 typedef ELFFile<ELFT> ELFO;
26 outs() << "Program Header:\n";
27 for (typename ELFO::Elf_Phdr_Iter pi = o->program_header_begin(),
28 pe = o->program_header_end();
34 case ELF::PT_GNU_STACK:
37 case ELF::PT_GNU_EH_FRAME:
38 outs() << "EH_FRAME ";
44 outs() << " DYNAMIC ";
53 outs() << " UNKNOWN ";
56 const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
59 << format(Fmt, (uint64_t)pi->p_offset)
61 << format(Fmt, (uint64_t)pi->p_vaddr)
63 << format(Fmt, (uint64_t)pi->p_paddr)
64 << format("align 2**%u\n", countTrailingZeros<uint64_t>(pi->p_align))
66 << format(Fmt, (uint64_t)pi->p_filesz)
68 << format(Fmt, (uint64_t)pi->p_memsz)
70 << ((pi->p_flags & ELF::PF_R) ? "r" : "-")
71 << ((pi->p_flags & ELF::PF_W) ? "w" : "-")
72 << ((pi->p_flags & ELF::PF_X) ? "x" : "-")
78 void llvm::printELFFileHeader(const object::ObjectFile *Obj) {
79 // Little-endian 32-bit
80 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
81 printProgramHeaders(ELFObj->getELFFile());
84 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
85 printProgramHeaders(ELFObj->getELFFile());
87 // Little-endian 64-bit
88 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
89 printProgramHeaders(ELFObj->getELFFile());
92 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
93 printProgramHeaders(ELFObj->getELFFile());