ELFImage.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. return false;
  82. // First locate the string tables.
  83. for (unsigned i = 0; i < section_count(); ++i) {
  84. auto& sh = section_header(i);
  85. if (sh.sh_type == SHT_SYMTAB) {
  86. ASSERT(!m_symbol_table_section_index);
  87. m_symbol_table_section_index = i;
  88. }
  89. if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
  90. ASSERT(!m_string_table_section_index);
  91. m_string_table_section_index = i;
  92. }
  93. }
  94. #ifdef SUPPORT_RELOCATIONS
  95. // Then create a name-to-index map.
  96. for (unsigned i = 0; i < section_count(); ++i) {
  97. auto& section = this->section(i);
  98. m_sections.set(section.name(), move(i));
  99. }
  100. #endif
  101. return true;
  102. }
  103. const char* ELFImage::section_header_table_string(unsigned offset) const
  104. {
  105. auto& sh = section_header(header().e_shstrndx);
  106. if (sh.sh_type != SHT_STRTAB)
  107. return nullptr;
  108. return raw_data(sh.sh_offset + offset);
  109. }
  110. const char* ELFImage::table_string(unsigned offset) const
  111. {
  112. auto& sh = section_header(m_string_table_section_index);
  113. if (sh.sh_type != SHT_STRTAB)
  114. return nullptr;
  115. return raw_data(sh.sh_offset + offset);
  116. }
  117. const char* ELFImage::raw_data(unsigned offset) const
  118. {
  119. return reinterpret_cast<const char*>(m_buffer) + offset;
  120. }
  121. const Elf32_Ehdr& ELFImage::header() const
  122. {
  123. return *reinterpret_cast<const Elf32_Ehdr*>(raw_data(0));
  124. }
  125. const Elf32_Phdr& ELFImage::program_header_internal(unsigned index) const
  126. {
  127. ASSERT(index < header().e_phnum);
  128. return *reinterpret_cast<const Elf32_Phdr*>(raw_data(header().e_phoff + (index * sizeof(Elf32_Phdr))));
  129. }
  130. const Elf32_Shdr& ELFImage::section_header(unsigned index) const
  131. {
  132. ASSERT(index < header().e_shnum);
  133. return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * sizeof(Elf32_Shdr))));
  134. }
  135. const ELFImage::Symbol ELFImage::symbol(unsigned index) const
  136. {
  137. ASSERT(index < symbol_count());
  138. auto* raw_syms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_symbol_table_section_index).offset()));
  139. return Symbol(*this, index, raw_syms[index]);
  140. }
  141. const ELFImage::Section ELFImage::section(unsigned index) const
  142. {
  143. ASSERT(index < section_count());
  144. return Section(*this, index);
  145. }
  146. const ELFImage::ProgramHeader ELFImage::program_header(unsigned index) const
  147. {
  148. ASSERT(index < program_header_count());
  149. return ProgramHeader(*this, index);
  150. }
  151. #ifdef SUPPORT_RELOCATIONS
  152. const ELFImage::Relocation ELFImage::RelocationSection::relocation(unsigned index) const
  153. {
  154. ASSERT(index < relocation_count());
  155. auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
  156. return Relocation(m_image, rels[index]);
  157. }
  158. const ELFImage::RelocationSection ELFImage::Section::relocations() const
  159. {
  160. // FIXME: This is ugly.
  161. char relocation_sectionName[128];
  162. ksprintf(relocation_sectionName, ".rel%s", name());
  163. #ifdef ELFIMAGE_DEBUG
  164. kprintf("looking for '%s'\n", relocation_sectionName);
  165. #endif
  166. auto relocation_section = m_image.lookup_section(relocation_sectionName);
  167. if (relocation_section.type() != SHT_REL)
  168. return static_cast<const RelocationSection>(m_image.section(0));
  169. #ifdef ELFIMAGE_DEBUG
  170. kprintf("Found relocations for %s in %s\n", name(), relocation_section.name());
  171. #endif
  172. return static_cast<const RelocationSection>(relocation_section);
  173. }
  174. const ELFImage::Section ELFImage::lookup_section(const char* name) const
  175. {
  176. if (auto it = m_sections.find(name); it != m_sections.end())
  177. return section((*it).value);
  178. return section(0);
  179. }
  180. #endif