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"
17 #include "llvm/Object/ELF.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm::object;
25 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
26 void printProgramHeaders(
27 const ELFObjectFile<target_endianness, max_alignment, is64Bits> *o) {
28 typedef ELFObjectFile<target_endianness, max_alignment, is64Bits> ELFO;
29 outs() << "Program Header:\n";
30 for (typename ELFO::Elf_Phdr_Iter pi = o->begin_program_headers(),
31 pe = o->end_program_headers();
37 case ELF::PT_GNU_STACK:
40 case ELF::PT_GNU_EH_FRAME:
41 outs() << "EH_FRAME ";
44 outs() << " UNKNOWN ";
48 << format(is64Bits ? "0x%016x " : "0x%08x ", pi->p_offset)
50 << format(is64Bits ? "0x%016x " : "0x%08x ", pi->p_vaddr)
52 << format(is64Bits ? "0x%016x " : "0x%08x ", pi->p_paddr)
53 << format("align 2**%d\n", CountTrailingZeros_32(pi->p_align))
55 << format(is64Bits ? "0x%016x " : "0x%08x ", pi->p_filesz)
57 << format(is64Bits ? "0x%016x " : "0x%08x ", pi->p_memsz)
59 << ((pi->p_flags & ELF::PF_R) ? "r" : "-")
60 << ((pi->p_flags & ELF::PF_W) ? "w" : "-")
61 << ((pi->p_flags & ELF::PF_X) ? "x" : "-")
67 void llvm::printELFFileHeader(const object::ObjectFile *Obj) {
68 // Little-endian 32-bit
69 if (const ELFObjectFile<support::little, 4, false> *ELFObj =
70 dyn_cast<ELFObjectFile<support::little, 4, false> >(Obj))
71 printProgramHeaders(ELFObj);
74 if (const ELFObjectFile<support::big, 4, false> *ELFObj =
75 dyn_cast<ELFObjectFile<support::big, 4, false> >(Obj))
76 printProgramHeaders(ELFObj);
78 // Little-endian 64-bit
79 if (const ELFObjectFile<support::little, 8, true> *ELFObj =
80 dyn_cast<ELFObjectFile<support::little, 8, true> >(Obj))
81 printProgramHeaders(ELFObj);
84 if (const ELFObjectFile<support::big, 8, true> *ELFObj =
85 dyn_cast<ELFObjectFile<support::big, 8, true> >(Obj))
86 printProgramHeaders(ELFObj);