ELFImage.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. unsigned ELFImage::dynamic_symbol_count() const
  41. {
  42. return section(m_dynamic_symbol_table_section_index).entry_count();
  43. }
  44. void ELFImage::dump() const
  45. {
  46. dbgprintf("ELFImage{%p} {\n", this);
  47. dbgprintf(" is_valid: %u\n", is_valid());
  48. if (!is_valid()) {
  49. dbgprintf("}\n");
  50. return;
  51. }
  52. dbgprintf(" type: %s\n", object_file_type_to_string(header().e_type));
  53. dbgprintf(" machine: %u\n", header().e_machine);
  54. dbgprintf(" entry: %x\n", header().e_entry);
  55. dbgprintf(" shoff: %u\n", header().e_shoff);
  56. dbgprintf(" shnum: %u\n", header().e_shnum);
  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. else if (StringView(".dynstr") == section_header_table_string(sh.sh_name))
  105. m_dynamic_string_table_section_index = i;
  106. else
  107. ASSERT_NOT_REACHED();
  108. }
  109. if (sh.sh_type == SHT_DYNAMIC) {
  110. ASSERT(!m_dynamic_section_index || m_dynamic_section_index == i);
  111. m_dynamic_section_index = i;
  112. }
  113. if (sh.sh_type == SHT_DYNSYM) {
  114. ASSERT(!m_dynamic_symbol_table_section_index || m_dynamic_symbol_table_section_index == i);
  115. m_dynamic_symbol_table_section_index = i;
  116. }
  117. if (sh.sh_type == SHT_REL) {
  118. if (StringView(".rel.dyn") == section_header_table_string(sh.sh_name)) {
  119. m_dynamic_relocation_section_index = i;
  120. }
  121. }
  122. }
  123. // Then create a name-to-index map.
  124. for (unsigned i = 0; i < section_count(); ++i) {
  125. auto& section = this->section(i);
  126. m_sections.set(section.name(), move(i));
  127. }
  128. return true;
  129. }
  130. const char* ELFImage::section_header_table_string(unsigned offset) const
  131. {
  132. auto& sh = section_header(header().e_shstrndx);
  133. if (sh.sh_type != SHT_STRTAB)
  134. return nullptr;
  135. return raw_data(sh.sh_offset + offset);
  136. }
  137. const char* ELFImage::table_string(unsigned offset) const
  138. {
  139. auto& sh = section_header(m_string_table_section_index);
  140. if (sh.sh_type != SHT_STRTAB)
  141. return nullptr;
  142. return raw_data(sh.sh_offset + offset);
  143. }
  144. const char* ELFImage::dynamic_table_string(unsigned offset) const
  145. {
  146. auto& sh = section_header(m_dynamic_string_table_section_index);
  147. if (sh.sh_type != SHT_STRTAB)
  148. return nullptr;
  149. return raw_data(sh.sh_offset + offset);
  150. }
  151. const char* ELFImage::raw_data(unsigned offset) const
  152. {
  153. return reinterpret_cast<const char*>(m_buffer) + offset;
  154. }
  155. const Elf32_Ehdr& ELFImage::header() const
  156. {
  157. return *reinterpret_cast<const Elf32_Ehdr*>(raw_data(0));
  158. }
  159. const Elf32_Phdr& ELFImage::program_header_internal(unsigned index) const
  160. {
  161. ASSERT(index < header().e_phnum);
  162. return *reinterpret_cast<const Elf32_Phdr*>(raw_data(header().e_phoff + (index * sizeof(Elf32_Phdr))));
  163. }
  164. const Elf32_Shdr& ELFImage::section_header(unsigned index) const
  165. {
  166. ASSERT(index < header().e_shnum);
  167. return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * header().e_shentsize)));
  168. }
  169. const ELFImage::Symbol ELFImage::symbol(unsigned index) const
  170. {
  171. ASSERT(index < symbol_count());
  172. auto* raw_syms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_symbol_table_section_index).offset()));
  173. return Symbol(*this, index, raw_syms[index]);
  174. }
  175. const ELFImage::DynamicSymbol ELFImage::dynamic_symbol(unsigned index) const
  176. {
  177. ASSERT(index < symbol_count());
  178. auto* raw_syms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_dynamic_symbol_table_section_index).offset()));
  179. return DynamicSymbol(*this, index, raw_syms[index]);
  180. }
  181. const ELFImage::Section ELFImage::section(unsigned index) const
  182. {
  183. ASSERT(index < section_count());
  184. return Section(*this, index);
  185. }
  186. const ELFImage::ProgramHeader ELFImage::program_header(unsigned index) const
  187. {
  188. ASSERT(index < program_header_count());
  189. return ProgramHeader(*this, index);
  190. }
  191. const ELFImage::Relocation ELFImage::RelocationSection::relocation(unsigned index) const
  192. {
  193. ASSERT(index < relocation_count());
  194. auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
  195. return Relocation(m_image, rels[index]);
  196. }
  197. const ELFImage::DynamicRelocation ELFImage::DynamicRelocationSection::relocation(unsigned index) const
  198. {
  199. ASSERT(index < relocation_count());
  200. auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
  201. return DynamicRelocation(m_image, rels[index]);
  202. }
  203. const ELFImage::RelocationSection ELFImage::Section::relocations() const
  204. {
  205. // FIXME: This is ugly.
  206. char relocation_sectionName[128];
  207. sprintf(relocation_sectionName, ".rel%s", name());
  208. #ifdef ELFIMAGE_DEBUG
  209. dbgprintf("looking for '%s'\n", relocation_sectionName);
  210. #endif
  211. auto relocation_section = m_image.lookup_section(relocation_sectionName);
  212. if (relocation_section.type() != SHT_REL)
  213. return static_cast<const RelocationSection>(m_image.section(0));
  214. #ifdef ELFIMAGE_DEBUG
  215. dbgprintf("Found relocations for %s in %s\n", name(), relocation_section.name());
  216. #endif
  217. return static_cast<const RelocationSection>(relocation_section);
  218. }
  219. const ELFImage::Section ELFImage::lookup_section(const char* name) const
  220. {
  221. if (auto it = m_sections.find(name); it != m_sections.end())
  222. return section((*it).value);
  223. return section(0);
  224. }
  225. const ELFImage::DynamicSection ELFImage::dynamic_section() const
  226. {
  227. ASSERT(is_dynamic());
  228. return section(m_dynamic_section_index);
  229. }
  230. const ELFImage::DynamicRelocationSection ELFImage::dynamic_relocation_section() const
  231. {
  232. ASSERT(is_dynamic());
  233. return section(m_dynamic_relocation_section_index);
  234. }