[objdump] Add --private-headers, -p.
[oota-llvm.git] / tools / llvm-objdump / ELFDump.cpp
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
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"
21
22 using namespace llvm;
23 using namespace llvm::object;
24
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();
32                                     pi != pe; ++pi) {
33     switch (pi->p_type) {
34     case ELF::PT_LOAD:
35       outs() << "    LOAD ";
36       break;
37     case ELF::PT_GNU_STACK:
38       outs() << "   STACK ";
39       break;
40     case ELF::PT_GNU_EH_FRAME:
41       outs() << "EH_FRAME ";
42       break;
43     default:
44       outs() << " UNKNOWN ";
45     }
46
47     outs() << "off    "
48            << format(is64Bits ? "0x%016x " : "0x%08x ", pi->p_offset)
49            << "vaddr "
50            << format(is64Bits ? "0x%016x " : "0x%08x ", pi->p_vaddr)
51            << "paddr "
52            << format(is64Bits ? "0x%016x " : "0x%08x ", pi->p_paddr)
53            << format("align 2**%d\n", CountTrailingZeros_32(pi->p_align))
54            << "         filesz "
55            << format(is64Bits ? "0x%016x " : "0x%08x ", pi->p_filesz)
56            << "memsz "
57            << format(is64Bits ? "0x%016x " : "0x%08x ", pi->p_memsz)
58            << "flags "
59            << ((pi->p_flags & ELF::PF_R) ? "r" : "-")
60            << ((pi->p_flags & ELF::PF_W) ? "w" : "-")
61            << ((pi->p_flags & ELF::PF_X) ? "x" : "-")
62            << "\n";
63   }
64   outs() << "\n";
65 }
66
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);
72
73   // Big-endian 32-bit
74   if (const ELFObjectFile<support::big, 4, false> *ELFObj =
75           dyn_cast<ELFObjectFile<support::big, 4, false> >(Obj))
76     printProgramHeaders(ELFObj);
77
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);
82
83   // Big-endian 64-bit
84   if (const ELFObjectFile<support::big, 8, true> *ELFObj =
85           dyn_cast<ELFObjectFile<support::big, 8, true> >(Obj))
86     printProgramHeaders(ELFObj);
87 }