ELFImage.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <AK/StringBuilder.h>
  2. #include <AK/kstdio.h>
  3. #include <LibELF/ELFImage.h>
  4. ELFImage::ELFImage(const u8* buffer, size_t size)
  5. : m_buffer(buffer)
  6. , m_size(size)
  7. {
  8. m_valid = parse();
  9. }
  10. ELFImage::~ELFImage()
  11. {
  12. }
  13. static const char* object_file_type_to_string(Elf32_Half type)
  14. {
  15. switch (type) {
  16. case ET_NONE:
  17. return "None";
  18. case ET_REL:
  19. return "Relocatable";
  20. case ET_EXEC:
  21. return "Executable";
  22. case ET_DYN:
  23. return "Shared object";
  24. case ET_CORE:
  25. return "Core";
  26. default:
  27. return "(?)";
  28. }
  29. }
  30. const char* ELFImage::section_index_to_string(unsigned index) const
  31. {
  32. if (index == SHN_UNDEF)
  33. return "Undefined";
  34. if (index >= SHN_LORESERVE)
  35. return "Reserved";
  36. return section(index).name();
  37. }
  38. unsigned ELFImage::symbol_count() const
  39. {
  40. return section(m_symbol_table_section_index).entry_count();
  41. }
  42. void ELFImage::dump() const
  43. {
  44. dbgprintf("ELFImage{%p} {\n", this);
  45. dbgprintf(" is_valid: %u\n", is_valid());
  46. if (!is_valid()) {
  47. dbgprintf("}\n");
  48. return;
  49. }
  50. dbgprintf(" type: %s\n", object_file_type_to_string(header().e_type));
  51. dbgprintf(" machine: %u\n", header().e_machine);
  52. dbgprintf(" entry: %x\n", header().e_entry);
  53. dbgprintf(" shoff: %u\n", header().e_shoff);
  54. dbgprintf(" shnum: %u\n", header().e_shnum);
  55. dbgprintf(" phoff: %u\n", header().e_phoff);
  56. dbgprintf(" phnum: %u\n", header().e_phnum);
  57. dbgprintf(" shstrndx: %u\n", header().e_shstrndx);
  58. for (unsigned i = 0; i < header().e_shnum; ++i) {
  59. auto& section = this->section(i);
  60. dbgprintf(" Section %u: {\n", i);
  61. dbgprintf(" name: %s\n", section.name());
  62. dbgprintf(" type: %x\n", section.type());
  63. dbgprintf(" offset: %x\n", section.offset());
  64. dbgprintf(" size: %u\n", section.size());
  65. dbgprintf(" \n");
  66. dbgprintf(" }\n");
  67. }
  68. dbgprintf("Symbol count: %u (table is %u)\n", symbol_count(), m_symbol_table_section_index);
  69. for (unsigned i = 1; i < symbol_count(); ++i) {
  70. auto& sym = symbol(i);
  71. dbgprintf("Symbol @%u:\n", i);
  72. dbgprintf(" Name: %s\n", sym.name());
  73. dbgprintf(" In section: %s\n", section_index_to_string(sym.section_index()));
  74. dbgprintf(" Value: %x\n", sym.value());
  75. dbgprintf(" Size: %u\n", sym.size());
  76. }
  77. dbgprintf("}\n");
  78. }
  79. unsigned ELFImage::section_count() const
  80. {
  81. return header().e_shnum;
  82. }
  83. unsigned ELFImage::program_header_count() const
  84. {
  85. return header().e_phnum;
  86. }
  87. bool ELFImage::parse()
  88. {
  89. // We only support i386.
  90. if (header().e_machine != 3) {
  91. dbgprintf("ELFImage::parse(): e_machine=%u not supported!\n", header().e_machine);
  92. return false;
  93. }
  94. // First locate the string tables.
  95. for (unsigned i = 0; i < section_count(); ++i) {
  96. auto& sh = section_header(i);
  97. if (sh.sh_type == SHT_SYMTAB) {
  98. ASSERT(!m_symbol_table_section_index || m_symbol_table_section_index == i);
  99. m_symbol_table_section_index = i;
  100. }
  101. if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
  102. if (StringView(".strtab") == section_header_table_string(sh.sh_name))
  103. m_string_table_section_index = i;
  104. }
  105. }
  106. // Then create a name-to-index map.
  107. for (unsigned i = 0; i < section_count(); ++i) {
  108. auto& section = this->section(i);
  109. m_sections.set(section.name(), move(i));
  110. }
  111. return true;
  112. }
  113. const char* ELFImage::section_header_table_string(unsigned offset) const
  114. {
  115. auto& sh = section_header(header().e_shstrndx);
  116. if (sh.sh_type != SHT_STRTAB)
  117. return nullptr;
  118. return raw_data(sh.sh_offset + offset);
  119. }
  120. const char* ELFImage::table_string(unsigned offset) const
  121. {
  122. auto& sh = section_header(m_string_table_section_index);
  123. if (sh.sh_type != SHT_STRTAB)
  124. return nullptr;
  125. return raw_data(sh.sh_offset + offset);
  126. }
  127. const char* ELFImage::raw_data(unsigned offset) const
  128. {
  129. return reinterpret_cast<const char*>(m_buffer) + offset;
  130. }
  131. const Elf32_Ehdr& ELFImage::header() const
  132. {
  133. return *reinterpret_cast<const Elf32_Ehdr*>(raw_data(0));
  134. }
  135. const Elf32_Phdr& ELFImage::program_header_internal(unsigned index) const
  136. {
  137. ASSERT(index < header().e_phnum);
  138. return *reinterpret_cast<const Elf32_Phdr*>(raw_data(header().e_phoff + (index * sizeof(Elf32_Phdr))));
  139. }
  140. const Elf32_Shdr& ELFImage::section_header(unsigned index) const
  141. {
  142. ASSERT(index < header().e_shnum);
  143. return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * header().e_shentsize)));
  144. }
  145. const ELFImage::Symbol ELFImage::symbol(unsigned index) const
  146. {
  147. ASSERT(index < symbol_count());
  148. auto* raw_syms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_symbol_table_section_index).offset()));
  149. return Symbol(*this, index, raw_syms[index]);
  150. }
  151. const ELFImage::Section ELFImage::section(unsigned index) const
  152. {
  153. ASSERT(index < section_count());
  154. return Section(*this, index);
  155. }
  156. const ELFImage::ProgramHeader ELFImage::program_header(unsigned index) const
  157. {
  158. ASSERT(index < program_header_count());
  159. return ProgramHeader(*this, index);
  160. }
  161. const ELFImage::Relocation ELFImage::RelocationSection::relocation(unsigned index) const
  162. {
  163. ASSERT(index < relocation_count());
  164. auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
  165. return Relocation(m_image, rels[index]);
  166. }
  167. const ELFImage::RelocationSection ELFImage::Section::relocations() const
  168. {
  169. StringBuilder builder;
  170. builder.append(".rel");
  171. builder.append(name());
  172. auto relocation_section = m_image.lookup_section(builder.to_string());
  173. if (relocation_section.type() != SHT_REL)
  174. return static_cast<const RelocationSection>(m_image.section(0));
  175. #ifdef ELFIMAGE_DEBUG
  176. dbgprintf("Found relocations for %s in %s\n", name(), relocation_section.name());
  177. #endif
  178. return static_cast<const RelocationSection>(relocation_section);
  179. }
  180. const ELFImage::Section ELFImage::lookup_section(const String& name) const
  181. {
  182. if (auto it = m_sections.find(name); it != m_sections.end())
  183. return section((*it).value);
  184. return section(0);
  185. }