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