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;
26 void printProgramHeaders(
27 const ELFObjectFile<ELFT> *o) {
28 typedef ELFObjectFile<ELFT> 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 ";
47 const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
50 << format(Fmt, (uint64_t)pi->p_offset)
52 << format(Fmt, (uint64_t)pi->p_vaddr)
54 << format(Fmt, (uint64_t)pi->p_paddr)
55 << format("align 2**%u\n", CountTrailingZeros_64(pi->p_align))
57 << format(Fmt, (uint64_t)pi->p_filesz)
59 << format(Fmt, (uint64_t)pi->p_memsz)
61 << ((pi->p_flags & ELF::PF_R) ? "r" : "-")
62 << ((pi->p_flags & ELF::PF_W) ? "w" : "-")
63 << ((pi->p_flags & ELF::PF_X) ? "x" : "-")
69 void llvm::printELFFileHeader(const object::ObjectFile *Obj) {
70 // Little-endian 32-bit
71 if (const ELFObjectFile<ELFType<support::little, 4, false> > *ELFObj =
72 dyn_cast<ELFObjectFile<ELFType<support::little, 4, false> > >(Obj))
73 printProgramHeaders(ELFObj);
76 if (const ELFObjectFile<ELFType<support::big, 4, false> > *ELFObj =
77 dyn_cast<ELFObjectFile<ELFType<support::big, 4, false> > >(Obj))
78 printProgramHeaders(ELFObj);
80 // Little-endian 64-bit
81 if (const ELFObjectFile<ELFType<support::little, 8, true> > *ELFObj =
82 dyn_cast<ELFObjectFile<ELFType<support::little, 8, true> > >(Obj))
83 printProgramHeaders(ELFObj);
86 if (const ELFObjectFile<ELFType<support::big, 8, true> > *ELFObj =
87 dyn_cast<ELFObjectFile<ELFType<support::big, 8, true> > >(Obj))
88 printProgramHeaders(ELFObj);