ELFImage.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #pragma once
  2. #include <AK/OwnPtr.h>
  3. #include <AK/HashMap.h>
  4. #include <AK/AKString.h>
  5. #include "elf.h"
  6. #include "types.h"
  7. class ELFImage {
  8. public:
  9. explicit ELFImage(const byte*);
  10. ~ELFImage();
  11. void dump();
  12. bool is_valid() const { return m_valid; }
  13. bool parse();
  14. class Section;
  15. class RelocationSection;
  16. class Symbol;
  17. class Relocation;
  18. class Symbol {
  19. public:
  20. Symbol(const ELFImage& image, unsigned index, const Elf32_Sym& sym)
  21. : m_image(image)
  22. , m_sym(sym)
  23. , m_index(index)
  24. {
  25. }
  26. ~Symbol() { }
  27. const char* name() const { return m_image.table_string(m_sym.st_name); }
  28. unsigned section_index() const { return m_sym.st_shndx; }
  29. unsigned value() const { return m_sym.st_value; }
  30. unsigned size() const { return m_sym.st_size; }
  31. unsigned index() const { return m_index; }
  32. unsigned type() const { return ELF32_ST_TYPE(m_sym.st_info); }
  33. const Section section() const { return m_image.section(section_index()); }
  34. private:
  35. const ELFImage& m_image;
  36. const Elf32_Sym& m_sym;
  37. const unsigned m_index;
  38. };
  39. class ProgramHeader {
  40. public:
  41. ProgramHeader(const ELFImage& image, unsigned program_header_index)
  42. : m_program_header(image.program_header_internal(program_header_index))
  43. , m_program_header_index(program_header_index)
  44. {
  45. }
  46. ~ProgramHeader() { }
  47. unsigned index() const { return m_program_header_index; }
  48. dword type() const { return m_program_header.p_type; }
  49. dword flags() const { return m_program_header.p_flags; }
  50. dword offset() const { return m_program_header.p_offset; }
  51. LinearAddress laddr() const { return LinearAddress(m_program_header.p_vaddr); }
  52. dword size_in_memory() const { return m_program_header.p_memsz; }
  53. dword size_in_image() const { return m_program_header.p_filesz; }
  54. dword alignment() const { return m_program_header.p_align; }
  55. bool is_readable() const { return flags() & PF_R; }
  56. bool is_writable() const { return flags() & PF_W; }
  57. bool is_executable() const { return flags() & PF_X; }
  58. private:
  59. const Elf32_Phdr& m_program_header;
  60. unsigned m_program_header_index { 0 };
  61. };
  62. class Section {
  63. public:
  64. Section(const ELFImage& image, unsigned sectionIndex)
  65. : m_image(image)
  66. , m_section_header(image.section_header(sectionIndex))
  67. , m_section_index(sectionIndex)
  68. {
  69. }
  70. ~Section() { }
  71. const char* name() const { return m_image.section_header_table_string(m_section_header.sh_name); }
  72. unsigned type() const { return m_section_header.sh_type; }
  73. unsigned offset() const { return m_section_header.sh_offset; }
  74. unsigned size() const { return m_section_header.sh_size; }
  75. unsigned entry_size() const { return m_section_header.sh_entsize; }
  76. unsigned entry_count() const { return size() / entry_size(); }
  77. dword address() const { return m_section_header.sh_addr; }
  78. const char* raw_data() const { return m_image.raw_data(m_section_header.sh_offset); }
  79. bool is_undefined() const { return m_section_index == SHN_UNDEF; }
  80. #ifdef SUPPORT_RELOCATIONS
  81. const RelocationSection relocations() const;
  82. #endif
  83. dword flags() const { return m_section_header.sh_flags; }
  84. bool is_writable() const { return flags() & SHF_WRITE; }
  85. bool is_executable() const { return flags() & PF_X; }
  86. protected:
  87. friend class RelocationSection;
  88. const ELFImage& m_image;
  89. const Elf32_Shdr& m_section_header;
  90. unsigned m_section_index;
  91. };
  92. #ifdef SUPPORT_RELOCATIONS
  93. class RelocationSection : public Section {
  94. public:
  95. RelocationSection(const Section& section)
  96. : Section(section.m_image, section.m_section_index)
  97. {
  98. }
  99. unsigned relocation_count() const { return entry_count(); }
  100. const Relocation relocation(unsigned index) const;
  101. template<typename F> void for_each_relocation(F) const;
  102. };
  103. class Relocation {
  104. public:
  105. Relocation(const ELFImage& image, const Elf32_Rel& rel)
  106. : m_image(image)
  107. , m_rel(rel)
  108. {
  109. }
  110. ~Relocation() { }
  111. unsigned offset() const { return m_rel.r_offset; }
  112. unsigned type() const { return ELF32_R_TYPE(m_rel.r_info); }
  113. unsigned symbol_index() const { return ELF32_R_SYM(m_rel.r_info); }
  114. const Symbol symbol() const { return m_image.symbol(symbol_index()); }
  115. private:
  116. const ELFImage& m_image;
  117. const Elf32_Rel& m_rel;
  118. };
  119. #endif
  120. unsigned symbol_count() const;
  121. unsigned section_count() const;
  122. unsigned program_header_count() const;
  123. const Symbol symbol(unsigned) const;
  124. const Section section(unsigned) const;
  125. const ProgramHeader program_header(unsigned const) const;
  126. template<typename F> void for_each_section(F) const;
  127. template<typename F> void for_each_section_of_type(unsigned, F) const;
  128. template<typename F> void for_each_symbol(F) const;
  129. template<typename F> void for_each_program_header(F) const;
  130. // NOTE: Returns section(0) if section with name is not found.
  131. // FIXME: I don't love this API.
  132. const Section lookupSection(const char* name) const;
  133. bool is_executable() const { return header().e_type == ET_EXEC; }
  134. bool is_relocatable() const { return header().e_type == ET_REL; }
  135. LinearAddress entry() const { return LinearAddress(header().e_entry); }
  136. private:
  137. bool parseHeader();
  138. const char* raw_data(unsigned offset) const;
  139. const Elf32_Ehdr& header() const;
  140. const Elf32_Shdr& section_header(unsigned) const;
  141. const Elf32_Phdr& program_header_internal(unsigned) const;
  142. const char* table_string(unsigned offset) const;
  143. const char* section_header_table_string(unsigned offset) const;
  144. const char* section_index_to_string(unsigned index);
  145. const byte* m_buffer { nullptr };
  146. #ifdef SUPPORT_RELOCATIONS
  147. HashMap<String, unsigned> m_sections;
  148. #endif
  149. bool m_valid { false };
  150. unsigned m_symbol_table_section_index { 0 };
  151. unsigned m_string_table_section_index { 0 };
  152. };
  153. template<typename F>
  154. inline void ELFImage::for_each_section(F func) const
  155. {
  156. for (unsigned i = 0; i < section_count(); ++i)
  157. func(section(i));
  158. }
  159. template<typename F>
  160. inline void ELFImage::for_each_section_of_type(unsigned type, F func) const
  161. {
  162. for (unsigned i = 0; i < section_count(); ++i) {
  163. auto& section = this->section(i);
  164. if (section.type() == type) {
  165. if (!func(section))
  166. break;
  167. }
  168. }
  169. }
  170. #ifdef SUPPORT_RELOCATIONS
  171. template<typename F>
  172. inline void ELFImage::RelocationSection::for_each_relocation(F func) const
  173. {
  174. for (unsigned i = 0; i < relocation_count(); ++i) {
  175. if (!func(relocation(i)))
  176. break;
  177. }
  178. }
  179. #endif
  180. template<typename F>
  181. inline void ELFImage::for_each_symbol(F func) const
  182. {
  183. for (unsigned i = 0; i < symbol_count(); ++i) {
  184. if (!func(symbol(i)))
  185. break;
  186. }
  187. }
  188. template<typename F>
  189. inline void ELFImage::for_each_program_header(F func) const
  190. {
  191. for (unsigned i = 0; i < program_header_count(); ++i)
  192. func(program_header(i));
  193. }