ELFDump.cpp 2.9 KB

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