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