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/ELFObjectFile.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 
printProgramHeaders(const ELFFile<ELFT> * o)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->begin_program_headers(),
28                                     pe = o->end_program_headers();
29                                     pi != pe; ++pi) {
30     switch (pi->p_type) {
31     case ELF::PT_LOAD:
32       outs() << "    LOAD ";
33       break;
34     case ELF::PT_GNU_STACK:
35       outs() << "   STACK ";
36       break;
37     case ELF::PT_GNU_EH_FRAME:
38       outs() << "EH_FRAME ";
39       break;
40     case ELF::PT_INTERP:
41       outs() << "  INTERP ";
42       break;
43     case ELF::PT_DYNAMIC:
44       outs() << " DYNAMIC ";
45       break;
46     case ELF::PT_PHDR:
47       outs() << "    PHDR ";
48       break;
49     case ELF::PT_TLS:
50       outs() << "    TLS ";
51       break;
52     default:
53       outs() << " UNKNOWN ";
54     }
55 
56     const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
57 
58     outs() << "off    "
59            << format(Fmt, (uint64_t)pi->p_offset)
60            << "vaddr "
61            << format(Fmt, (uint64_t)pi->p_vaddr)
62            << "paddr "
63            << format(Fmt, (uint64_t)pi->p_paddr)
64            << format("align 2**%u\n", countTrailingZeros<uint64_t>(pi->p_align))
65            << "         filesz "
66            << format(Fmt, (uint64_t)pi->p_filesz)
67            << "memsz "
68            << format(Fmt, (uint64_t)pi->p_memsz)
69            << "flags "
70            << ((pi->p_flags & ELF::PF_R) ? "r" : "-")
71            << ((pi->p_flags & ELF::PF_W) ? "w" : "-")
72            << ((pi->p_flags & ELF::PF_X) ? "x" : "-")
73            << "\n";
74   }
75   outs() << "\n";
76 }
77 
printELFFileHeader(const object::ObjectFile * Obj)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());
82 
83   // Big-endian 32-bit
84   if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
85     printProgramHeaders(ELFObj->getELFFile());
86 
87   // Little-endian 64-bit
88   if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
89     printProgramHeaders(ELFObj->getELFFile());
90 
91   // Big-endian 64-bit
92   if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
93     printProgramHeaders(ELFObj->getELFFile());
94 }
95