ELFImage.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "ELFImage.h"
  2. #include <AK/kstdio.h>
  3. ELFImage::ELFImage(const u8* buffer)
  4. : m_buffer(buffer)
  5. {
  6. m_valid = parse();
  7. }
  8. ELFImage::~ELFImage()
  9. {
  10. }
  11. static const char* object_file_type_to_string(Elf32_Half type)
  12. {
  13. switch (type) {
  14. case ET_NONE:
  15. return "None";
  16. case ET_REL:
  17. return "Relocatable";
  18. case ET_EXEC:
  19. return "Executable";
  20. case ET_DYN:
  21. return "Shared object";
  22. case ET_CORE:
  23. return "Core";
  24. default:
  25. return "(?)";
  26. }
  27. }
  28. const char* ELFImage::section_index_to_string(unsigned index) const
  29. {
  30. if (index == SHN_UNDEF)
  31. return "Undefined";
  32. if (index >= SHN_LORESERVE)
  33. return "Reserved";
  34. return section(index).name();
  35. }
  36. unsigned ELFImage::symbol_count() const
  37. {
  38. return section(m_symbol_table_section_index).entry_count();
  39. }
  40. void ELFImage::dump() const
  41. {
  42. dbgprintf("ELFImage{%p} {\n", this);
  43. dbgprintf(" is_valid: %u\n", is_valid());
  44. if (!is_valid()) {
  45. dbgprintf("}\n");
  46. return;
  47. }
  48. dbgprintf(" type: %s\n", object_file_type_to_string(header().e_type));
  49. dbgprintf(" machine: %u\n", header().e_machine);
  50. dbgprintf(" entry: %x\n", header().e_entry);
  51. dbgprintf(" shoff: %u\n", header().e_shoff);
  52. dbgprintf(" shnum: %u\n", header().e_shnum);
  53. dbgprintf(" shstrndx: %u\n", header().e_shstrndx);
  54. for (unsigned i = 0; i < header().e_shnum; ++i) {
  55. auto& section = this->section(i);
  56. dbgprintf(" Section %u: {\n", i);
  57. dbgprintf(" name: %s\n", section.name());
  58. dbgprintf(" type: %x\n", section.type());
  59. dbgprintf(" offset: %x\n", section.offset());
  60. dbgprintf(" size: %u\n", section.size());
  61. dbgprintf(" \n");
  62. dbgprintf(" }\n");
  63. }
  64. dbgprintf("Symbol count: %u (table is %u)\n", symbol_count(), m_symbol_table_section_index);
  65. for (unsigned i = 1; i < symbol_count(); ++i) {
  66. auto& sym = symbol(i);
  67. dbgprintf("Symbol @%u:\n", i);
  68. dbgprintf(" Name: %s\n", sym.name());
  69. dbgprintf(" In section: %s\n", section_index_to_string(sym.section_index()));
  70. dbgprintf(" Value: %x\n", sym.value());
  71. dbgprintf(" Size: %u\n", sym.size());
  72. }
  73. dbgprintf("}\n");
  74. }
  75. unsigned ELFImage::section_count() const
  76. {
  77. return header().e_shnum;
  78. }
  79. unsigned ELFImage::program_header_count() const
  80. {
  81. return header().e_phnum;
  82. }
  83. bool ELFImage::parse()
  84. {
  85. // We only support i386.
  86. if (header().e_machine != 3) {
  87. dbgprintf("ELFImage::parse(): e_machine=%u not supported!\n", header().e_machine);
  88. return false;
  89. }
  90. // First locate the string tables.
  91. for (unsigned i = 0; i < section_count(); ++i) {
  92. auto& sh = section_header(i);
  93. if (sh.sh_type == SHT_SYMTAB) {
  94. ASSERT(!m_symbol_table_section_index || m_symbol_table_section_index == i);
  95. m_symbol_table_section_index = i;
  96. }
  97. if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
  98. if (StringView(".strtab") == section_header_table_string(sh.sh_name))
  99. m_string_table_section_index = i;
  100. }
  101. if (sh.sh_type == SHT_DYNAMIC) {
  102. ASSERT(!m_dynamic_section_index || m_dynamic_section_index == i);
  103. m_dynamic_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. // FIXME: This is ugly.
  170. char relocation_sectionName[128];
  171. sprintf(relocation_sectionName, ".rel%s", name());
  172. #ifdef ELFIMAGE_DEBUG
  173. dbgprintf("looking for '%s'\n", relocation_sectionName);
  174. #endif
  175. auto relocation_section = m_image.lookup_section(relocation_sectionName);
  176. if (relocation_section.type() != SHT_REL)
  177. return static_cast<const RelocationSection>(m_image.section(0));
  178. #ifdef ELFIMAGE_DEBUG
  179. dbgprintf("Found relocations for %s in %s\n", name(), relocation_section.name());
  180. #endif
  181. return static_cast<const RelocationSection>(relocation_section);
  182. }
  183. const ELFImage::Section ELFImage::lookup_section(const char* name) const
  184. {
  185. if (auto it = m_sections.find(name); it != m_sections.end())
  186. return section((*it).value);
  187. return section(0);
  188. }
  189. const ELFImage::DynamicSection ELFImage::dynamic_section() const
  190. {
  191. ASSERT(is_dynamic());
  192. return section(m_dynamic_section_index);
  193. }