190792Sgshapiro//===-- ELFDump.cpp - ELF-specific dumper -----------------------*- C++ -*-===//
2261194Sgshapiro//
390792Sgshapiro//                     The LLVM Compiler Infrastructure
490792Sgshapiro//
590792Sgshapiro// This file is distributed under the University of Illinois Open Source
690792Sgshapiro// License. See LICENSE.TXT for details.
790792Sgshapiro//
890792Sgshapiro//===----------------------------------------------------------------------===//
990792Sgshapiro///
1090792Sgshapiro/// \file
11261194Sgshapiro/// \brief This file implements the ELF-specific dumper for llvm-objdump.
1290792Sgshapiro///
1390792Sgshapiro//===----------------------------------------------------------------------===//
1490792Sgshapiro
1590792Sgshapiro#include "llvm-objdump.h"
1690792Sgshapiro#include "llvm/Object/ELFObjectFile.h"
1790792Sgshapiro#include "llvm/Support/Format.h"
1890792Sgshapiro#include "llvm/Support/MathExtras.h"
1990792Sgshapiro#include "llvm/Support/raw_ostream.h"
2090792Sgshapiro
2190792Sgshapirousing namespace llvm;
2290792Sgshapirousing namespace llvm::object;
2390792Sgshapiro
2490792Sgshapirotemplate <class ELFT> void printProgramHeaders(const ELFFile<ELFT> *o) {
2590792Sgshapiro  typedef ELFFile<ELFT> ELFO;
2690792Sgshapiro  outs() << "Program Header:\n";
2790792Sgshapiro  for (typename ELFO::Elf_Phdr_Iter pi = o->begin_program_headers(),
2890792Sgshapiro                                    pe = o->end_program_headers();
2990792Sgshapiro                                    pi != pe; ++pi) {
3090792Sgshapiro    switch (pi->p_type) {
3190792Sgshapiro    case ELF::PT_LOAD:
3290792Sgshapiro      outs() << "    LOAD ";
3390792Sgshapiro      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
78void 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