blob: 72c512e0b0e488a5b27f178b9c2028776e4e4a66 [file] [log] [blame]
Michael J. Spencerb2c064c2013-01-06 03:56:49 +00001//===-- 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"
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000016#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
21using namespace llvm;
22using namespace llvm::object;
23
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000024template<class ELFT>
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000025void printProgramHeaders(
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000026 const ELFObjectFile<ELFT> *o) {
27 typedef ELFObjectFile<ELFT> ELFO;
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000028 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;
Michael J. Spencer8a3a1de2013-02-20 20:18:10 +000042 case ELF::PT_INTERP:
43 outs() << " INTERP ";
44 break;
45 case ELF::PT_DYNAMIC:
46 outs() << " DYNAMIC ";
47 break;
Michael J. Spencer56182302013-02-21 02:21:29 +000048 case ELF::PT_PHDR:
49 outs() << " PHDR ";
50 break;
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000051 default:
52 outs() << " UNKNOWN ";
53 }
54
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000055 const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
Michael J. Spencer46418792013-01-06 05:23:59 +000056
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000057 outs() << "off "
Michael J. Spencer46418792013-01-06 05:23:59 +000058 << format(Fmt, (uint64_t)pi->p_offset)
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000059 << "vaddr "
Michael J. Spencer46418792013-01-06 05:23:59 +000060 << format(Fmt, (uint64_t)pi->p_vaddr)
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000061 << "paddr "
Michael J. Spencer46418792013-01-06 05:23:59 +000062 << format(Fmt, (uint64_t)pi->p_paddr)
63 << format("align 2**%u\n", CountTrailingZeros_64(pi->p_align))
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000064 << " filesz "
Michael J. Spencer46418792013-01-06 05:23:59 +000065 << format(Fmt, (uint64_t)pi->p_filesz)
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000066 << "memsz "
Michael J. Spencer46418792013-01-06 05:23:59 +000067 << format(Fmt, (uint64_t)pi->p_memsz)
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000068 << "flags "
69 << ((pi->p_flags & ELF::PF_R) ? "r" : "-")
70 << ((pi->p_flags & ELF::PF_W) ? "w" : "-")
71 << ((pi->p_flags & ELF::PF_X) ? "x" : "-")
72 << "\n";
73 }
74 outs() << "\n";
75}
76
77void llvm::printELFFileHeader(const object::ObjectFile *Obj) {
78 // Little-endian 32-bit
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000079 if (const ELFObjectFile<ELFType<support::little, 4, false> > *ELFObj =
80 dyn_cast<ELFObjectFile<ELFType<support::little, 4, false> > >(Obj))
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000081 printProgramHeaders(ELFObj);
82
83 // Big-endian 32-bit
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000084 if (const ELFObjectFile<ELFType<support::big, 4, false> > *ELFObj =
85 dyn_cast<ELFObjectFile<ELFType<support::big, 4, false> > >(Obj))
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000086 printProgramHeaders(ELFObj);
87
88 // Little-endian 64-bit
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000089 if (const ELFObjectFile<ELFType<support::little, 8, true> > *ELFObj =
90 dyn_cast<ELFObjectFile<ELFType<support::little, 8, true> > >(Obj))
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000091 printProgramHeaders(ELFObj);
92
93 // Big-endian 64-bit
Michael J. Spencerac97f5c2013-01-15 07:44:25 +000094 if (const ELFObjectFile<ELFType<support::big, 8, true> > *ELFObj =
95 dyn_cast<ELFObjectFile<ELFType<support::big, 8, true> > >(Obj))
Michael J. Spencerb2c064c2013-01-06 03:56:49 +000096 printProgramHeaders(ELFObj);
97}