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 const char *Fmt = is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " "; 48 49 outs() << "off " 50 << format(Fmt, (uint64_t)pi->p_offset) 51 << "vaddr " 52 << format(Fmt, (uint64_t)pi->p_vaddr) 53 << "paddr " 54 << format(Fmt, (uint64_t)pi->p_paddr) 55 << format("align 2**%u\n", CountTrailingZeros_64(pi->p_align)) 56 << " filesz " 57 << format(Fmt, (uint64_t)pi->p_filesz) 58 << "memsz " 59 << format(Fmt, (uint64_t)pi->p_memsz) 60 << "flags " 61 << ((pi->p_flags & ELF::PF_R) ? "r" : "-") 62 << ((pi->p_flags & ELF::PF_W) ? "w" : "-") 63 << ((pi->p_flags & ELF::PF_X) ? "x" : "-") 64 << "\n"; 65 } 66 outs() << "\n"; 67 } 68 69 void llvm::printELFFileHeader(const object::ObjectFile *Obj) { 70 // Little-endian 32-bit 71 if (const ELFObjectFile<support::little, 4, false> *ELFObj = 72 dyn_cast<ELFObjectFile<support::little, 4, false> >(Obj)) 73 printProgramHeaders(ELFObj); 74 75 // Big-endian 32-bit 76 if (const ELFObjectFile<support::big, 4, false> *ELFObj = 77 dyn_cast<ELFObjectFile<support::big, 4, false> >(Obj)) 78 printProgramHeaders(ELFObj); 79 80 // Little-endian 64-bit 81 if (const ELFObjectFile<support::little, 8, true> *ELFObj = 82 dyn_cast<ELFObjectFile<support::little, 8, true> >(Obj)) 83 printProgramHeaders(ELFObj); 84 85 // Big-endian 64-bit 86 if (const ELFObjectFile<support::big, 8, true> *ELFObj = 87 dyn_cast<ELFObjectFile<support::big, 8, true> >(Obj)) 88 printProgramHeaders(ELFObj); 89 } 90