ELFImage.cpp 6.0 KB

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