1 //===-- ELFDump.cpp - ELF-specific dumper -----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file implements the ELF-specific dumper for llvm-objdump.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm-objdump.h"
16 #include "llvm/Object/ELF.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/MathExtras.h"
19 #include "llvm/Support/raw_ostream.h"
20
21 using namespace llvm;
22 using namespace llvm::object;
23
24 template<class ELFT>
printProgramHeaders(const ELFObjectFile<ELFT> * o)25 void printProgramHeaders(
26 const ELFObjectFile<ELFT> *o) {
27 typedef ELFObjectFile<ELFT> ELFO;
28 outs() << "Program Header:\n";
29 for (typename ELFO::Elf_Phdr_Iter pi = o->begin_program_headers(),
30 pe = o->end_program_headers();
31 pi != pe; ++pi) {
32 switch (pi->p_type) {
33 case ELF::PT_LOAD:
34 outs() << " LOAD ";
35 break;
36 case ELF::PT_GNU_STACK:
37 outs() << " STACK ";
38 break;
39 case ELF::PT_GNU_EH_FRAME:
40 outs() << "EH_FRAME ";
41 break;
42 case ELF::PT_INTERP:
43 outs() << " INTERP ";
44 break;
45 case ELF::PT_DYNAMIC:
46 outs() << " DYNAMIC ";
47 break;
48 case ELF::PT_PHDR:
49 outs() << " PHDR ";
50 break;
51 case ELF::PT_TLS:
52 outs() << " TLS ";
53 break;
54 default:
55 outs() << " UNKNOWN ";
56 }
57
58 const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
59
60 outs() << "off "
61 << format(Fmt, (uint64_t)pi->p_offset)
62 << "vaddr "
63 << format(Fmt, (uint64_t)pi->p_vaddr)
64 << "paddr "
65 << format(Fmt, (uint64_t)pi->p_paddr)
66 << format("align 2**%u\n", CountTrailingZeros_64(pi->p_align))
67 << " filesz "
68 << format(Fmt, (uint64_t)pi->p_filesz)
69 << "memsz "
70 << format(Fmt, (uint64_t)pi->p_memsz)
71 << "flags "
72 << ((pi->p_flags & ELF::PF_R) ? "r" : "-")
73 << ((pi->p_flags & ELF::PF_W) ? "w" : "-")
74 << ((pi->p_flags & ELF::PF_X) ? "x" : "-")
75 << "\n";
76 }
77 outs() << "\n";
78 }
79
printELFFileHeader(const object::ObjectFile * Obj)80 void llvm::printELFFileHeader(const object::ObjectFile *Obj) {
81 // Little-endian 32-bit
82 if (const ELFObjectFile<ELFType<support::little, 4, false> > *ELFObj =
83 dyn_cast<ELFObjectFile<ELFType<support::little, 4, false> > >(Obj))
84 printProgramHeaders(ELFObj);
85
86 // Big-endian 32-bit
87 if (const ELFObjectFile<ELFType<support::big, 4, false> > *ELFObj =
88 dyn_cast<ELFObjectFile<ELFType<support::big, 4, false> > >(Obj))
89 printProgramHeaders(ELFObj);
90
91 // Little-endian 64-bit
92 if (const ELFObjectFile<ELFType<support::little, 8, true> > *ELFObj =
93 dyn_cast<ELFObjectFile<ELFType<support::little, 8, true> > >(Obj))
94 printProgramHeaders(ELFObj);
95
96 // Big-endian 64-bit
97 if (const ELFObjectFile<ELFType<support::big, 8, true> > *ELFObj =
98 dyn_cast<ELFObjectFile<ELFType<support::big, 8, true> > >(Obj))
99 printProgramHeaders(ELFObj);
100 }
101