ELFImage.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "ELFImage.h"
  2. #include <AK/kstdio.h>
  3. #ifdef SERENITY_KERNEL
  4. ELFImage::ELFImage(const byte* data)
  5. : m_data(data)
  6. {
  7. m_isValid = parse();
  8. }
  9. #else
  10. ELFImage::ELFImage(MappedFile&& file)
  11. : m_file(move(file))
  12. {
  13. m_isValid = parse();
  14. }
  15. #endif
  16. ELFImage::~ELFImage()
  17. {
  18. }
  19. static const char* objectFileTypeToString(Elf32_Half type)
  20. {
  21. switch (type) {
  22. case ET_NONE: return "None";
  23. case ET_REL: return "Relocatable";
  24. case ET_EXEC: return "Executable";
  25. case ET_DYN: return "Shared object";
  26. case ET_CORE: return "Core";
  27. default: return "(?)";
  28. }
  29. }
  30. const char* ELFImage::sectionIndexToString(unsigned index)
  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::symbolCount() const
  39. {
  40. return section(m_symbolTableSectionIndex).entryCount();
  41. }
  42. void ELFImage::dump()
  43. {
  44. kprintf("AK::ELFImage{%p} {\n", this);
  45. kprintf(" isValid: %u\n", isValid());
  46. if (!isValid()) {
  47. kprintf("}\n");
  48. return;
  49. }
  50. kprintf(" type: %s\n", objectFileTypeToString(header().e_type));
  51. kprintf(" machine: %u\n", header().e_machine);
  52. kprintf(" entry: %08x\n", header().e_entry);
  53. kprintf(" shoff: %u\n", header().e_shoff);
  54. kprintf(" shnum: %u\n", header().e_shnum);
  55. kprintf(" shstrndx: %u\n", header().e_shstrndx);
  56. for (unsigned i = 0; i < header().e_shnum; ++i) {
  57. auto& section = this->section(i);
  58. kprintf(" Section %u: {\n", i);
  59. kprintf(" name: %s\n", section.name());
  60. kprintf(" type: %x\n", section.type());
  61. kprintf(" offset: %x\n", section.offset());
  62. kprintf(" size: %u\n", section.size());
  63. kprintf(" \n");
  64. kprintf(" }\n");
  65. }
  66. kprintf("Symbol count: %u (table is %u)\n", symbolCount(), m_symbolTableSectionIndex);
  67. for (unsigned i = 1; i < symbolCount(); ++i) {
  68. auto& sym = symbol(i);
  69. kprintf("Symbol @%u:\n", i);
  70. kprintf(" Name: %s\n", sym.name());
  71. kprintf(" In section: %s\n", sectionIndexToString(sym.sectionIndex()));
  72. kprintf(" Value: %08x\n", sym.value());
  73. kprintf(" Size: %u\n", sym.size());
  74. }
  75. kprintf("}\n");
  76. }
  77. unsigned ELFImage::sectionCount() const
  78. {
  79. return header().e_shnum;
  80. }
  81. bool ELFImage::parse()
  82. {
  83. // We only support i386.
  84. if (header().e_machine != 3)
  85. return false;
  86. // First locate the string tables.
  87. for (unsigned i = 0; i < sectionCount(); ++i) {
  88. auto& sh = sectionHeader(i);
  89. if (sh.sh_type == SHT_SYMTAB) {
  90. ASSERT(!m_symbolTableSectionIndex);
  91. m_symbolTableSectionIndex = i;
  92. }
  93. if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
  94. ASSERT(!m_stringTableSectionIndex);
  95. m_stringTableSectionIndex = i;
  96. }
  97. }
  98. // Then create a name-to-index map.
  99. for (unsigned i = 0; i < sectionCount(); ++i) {
  100. auto& section = this->section(i);
  101. m_sections.set(section.name(), move(i));
  102. }
  103. return true;
  104. }
  105. const char* ELFImage::sectionHeaderTableString(unsigned offset) const
  106. {
  107. auto& sh = sectionHeader(header().e_shstrndx);
  108. if (sh.sh_type != SHT_STRTAB)
  109. return nullptr;
  110. return rawData(sh.sh_offset + offset);
  111. }
  112. const char* ELFImage::tableString(unsigned offset) const
  113. {
  114. auto& sh = sectionHeader(m_stringTableSectionIndex);
  115. if (sh.sh_type != SHT_STRTAB)
  116. return nullptr;
  117. return rawData(sh.sh_offset + offset);
  118. }
  119. const char* ELFImage::rawData(unsigned offset) const
  120. {
  121. #ifdef SERENITY_KERNEL
  122. return reinterpret_cast<const char*>(m_data) + offset;
  123. #else
  124. return reinterpret_cast<const char*>(m_file.pointer()) + offset;
  125. #endif
  126. }
  127. const Elf32_Ehdr& ELFImage::header() const
  128. {
  129. return *reinterpret_cast<const Elf32_Ehdr*>(rawData(0));
  130. }
  131. const Elf32_Shdr& ELFImage::sectionHeader(unsigned index) const
  132. {
  133. ASSERT(index < header().e_shnum);
  134. return *reinterpret_cast<const Elf32_Shdr*>(rawData(header().e_shoff + (index * sizeof(Elf32_Shdr))));
  135. }
  136. const ELFImage::Symbol ELFImage::symbol(unsigned index) const
  137. {
  138. ASSERT(index < symbolCount());
  139. auto* rawSyms = reinterpret_cast<const Elf32_Sym*>(rawData(section(m_symbolTableSectionIndex).offset()));
  140. return Symbol(*this, index, rawSyms[index]);
  141. }
  142. const ELFImage::Section ELFImage::section(unsigned index) const
  143. {
  144. ASSERT(index < sectionCount());
  145. return Section(*this, index);
  146. }
  147. const ELFImage::Relocation ELFImage::RelocationSection::relocation(unsigned index) const
  148. {
  149. ASSERT(index < relocationCount());
  150. auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.rawData(offset()));
  151. return Relocation(m_image, rels[index]);
  152. }
  153. const ELFImage::RelocationSection ELFImage::Section::relocations() const
  154. {
  155. // FIXME: This is ugly.
  156. char relocationSectionName[128];
  157. ksprintf(relocationSectionName, ".rel%s", name());
  158. kprintf("looking for '%s'\n", relocationSectionName);
  159. auto relocationSection = m_image.lookupSection(relocationSectionName);
  160. if (relocationSection.type() != SHT_REL)
  161. return static_cast<const RelocationSection>(m_image.section(0));
  162. kprintf("Found relocations for %s in %s\n", name(), relocationSection.name());
  163. return static_cast<const RelocationSection>(relocationSection);
  164. }
  165. const ELFImage::Section ELFImage::lookupSection(const char* name) const
  166. {
  167. if (auto it = m_sections.find(name); it != m_sections.end())
  168. return section((*it).value);
  169. return section(0);
  170. }