Image.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Concepts.h>
  9. #include <AK/Vector.h>
  10. #include <Kernel/Memory/VirtualAddress.h>
  11. #include <LibELF/ELFABI.h>
  12. #ifndef KERNEL
  13. # include <AK/ByteString.h>
  14. #endif
  15. namespace ELF {
  16. class Image {
  17. public:
  18. explicit Image(ReadonlyBytes, bool verbose_logging = true);
  19. explicit Image(u8 const*, size_t, bool verbose_logging = true);
  20. ~Image() = default;
  21. void dump() const;
  22. bool is_valid() const { return m_valid; }
  23. bool parse();
  24. bool is_within_image(void const* address, size_t size) const
  25. {
  26. if (address < m_buffer)
  27. return false;
  28. if (((u8 const*)address + size) > m_buffer + m_size)
  29. return false;
  30. return true;
  31. }
  32. class Section;
  33. class RelocationSection;
  34. class Symbol;
  35. class Relocation;
  36. class Symbol {
  37. public:
  38. Symbol(Image const& image, unsigned index, Elf_Sym const& sym)
  39. : m_image(image)
  40. , m_sym(sym)
  41. , m_index(index)
  42. {
  43. }
  44. ~Symbol() = default;
  45. StringView name() const { return m_image.table_string(m_sym.st_name); }
  46. unsigned section_index() const { return m_sym.st_shndx; }
  47. FlatPtr value() const { return m_sym.st_value; }
  48. size_t size() const { return m_sym.st_size; }
  49. unsigned index() const { return m_index; }
  50. unsigned type() const
  51. {
  52. return ELF64_ST_TYPE(m_sym.st_info);
  53. }
  54. unsigned bind() const { return ELF64_ST_BIND(m_sym.st_info); }
  55. Section section() const
  56. {
  57. return m_image.section(section_index());
  58. }
  59. bool is_undefined() const { return section_index() == 0; }
  60. StringView raw_data() const;
  61. private:
  62. Image const& m_image;
  63. Elf_Sym const& m_sym;
  64. unsigned const m_index;
  65. };
  66. class ProgramHeader {
  67. public:
  68. ProgramHeader(Image const& image, unsigned program_header_index)
  69. : m_image(image)
  70. , m_program_header(image.program_header_internal(program_header_index))
  71. , m_program_header_index(program_header_index)
  72. {
  73. }
  74. ~ProgramHeader() = default;
  75. unsigned index() const { return m_program_header_index; }
  76. u32 type() const { return m_program_header.p_type; }
  77. u32 flags() const { return m_program_header.p_flags; }
  78. size_t offset() const { return m_program_header.p_offset; }
  79. VirtualAddress vaddr() const { return VirtualAddress(m_program_header.p_vaddr); }
  80. size_t size_in_memory() const { return m_program_header.p_memsz; }
  81. size_t size_in_image() const { return m_program_header.p_filesz; }
  82. size_t alignment() const { return m_program_header.p_align; }
  83. bool is_readable() const { return flags() & PF_R; }
  84. bool is_writable() const { return flags() & PF_W; }
  85. bool is_executable() const { return flags() & PF_X; }
  86. char const* raw_data() const { return m_image.raw_data(m_program_header.p_offset); }
  87. Elf_Phdr raw_header() const { return m_program_header; }
  88. private:
  89. Image const& m_image;
  90. Elf_Phdr const& m_program_header;
  91. unsigned m_program_header_index { 0 };
  92. };
  93. class Section {
  94. public:
  95. Section(Image const& image, unsigned sectionIndex)
  96. : m_image(image)
  97. , m_section_header(image.section_header(sectionIndex))
  98. , m_section_index(sectionIndex)
  99. {
  100. }
  101. ~Section() = default;
  102. StringView name() const { return m_image.section_header_table_string(m_section_header.sh_name); }
  103. u32 type() const { return m_section_header.sh_type; }
  104. size_t offset() const { return m_section_header.sh_offset; }
  105. size_t size() const { return m_section_header.sh_size; }
  106. size_t entry_size() const { return m_section_header.sh_entsize; }
  107. size_t entry_count() const { return !entry_size() ? 0 : size() / entry_size(); }
  108. FlatPtr address() const { return m_section_header.sh_addr; }
  109. char const* raw_data() const { return m_image.raw_data(m_section_header.sh_offset); }
  110. ReadonlyBytes bytes() const { return { raw_data(), size() }; }
  111. Optional<RelocationSection> relocations() const;
  112. auto flags() const { return m_section_header.sh_flags; }
  113. bool is_writable() const { return flags() & SHF_WRITE; }
  114. bool is_executable() const { return flags() & PF_X; }
  115. protected:
  116. friend class RelocationSection;
  117. Image const& m_image;
  118. Elf_Shdr const& m_section_header;
  119. unsigned m_section_index;
  120. };
  121. class RelocationSection : public Section {
  122. public:
  123. explicit RelocationSection(Section const& section)
  124. : Section(section.m_image, section.m_section_index)
  125. {
  126. }
  127. size_t relocation_count() const { return entry_count(); }
  128. Relocation relocation(unsigned index) const;
  129. template<VoidFunction<Image::Relocation&> F>
  130. void for_each_relocation(F) const;
  131. };
  132. class Relocation {
  133. public:
  134. Relocation(Image const& image, Elf_Rel const& rel)
  135. : m_image(image)
  136. , m_rel(rel)
  137. {
  138. }
  139. ~Relocation() = default;
  140. size_t offset() const { return m_rel.r_offset; }
  141. unsigned type() const
  142. {
  143. return ELF64_R_TYPE(m_rel.r_info);
  144. }
  145. unsigned symbol_index() const { return ELF64_R_SYM(m_rel.r_info); }
  146. Symbol symbol() const
  147. {
  148. return m_image.symbol(symbol_index());
  149. }
  150. private:
  151. Image const& m_image;
  152. Elf_Rel const& m_rel;
  153. };
  154. unsigned symbol_count() const;
  155. unsigned section_count() const;
  156. unsigned program_header_count() const;
  157. Symbol symbol(unsigned) const;
  158. Section section(unsigned) const;
  159. ProgramHeader program_header(unsigned) const;
  160. template<IteratorFunction<Image::Section> F>
  161. void for_each_section(F) const;
  162. template<VoidFunction<Section> F>
  163. void for_each_section(F) const;
  164. template<IteratorFunction<Section&> F>
  165. void for_each_section_of_type(unsigned, F) const;
  166. template<VoidFunction<Section&> F>
  167. void for_each_section_of_type(unsigned, F) const;
  168. template<IteratorFunction<Symbol> F>
  169. void for_each_symbol(F) const;
  170. template<VoidFunction<Symbol> F>
  171. void for_each_symbol(F) const;
  172. template<IteratorFunction<ProgramHeader> F>
  173. void for_each_program_header(F func) const;
  174. template<VoidFunction<ProgramHeader> F>
  175. void for_each_program_header(F) const;
  176. Optional<Section> lookup_section(StringView name) const;
  177. bool is_executable() const { return header().e_type == ET_EXEC; }
  178. bool is_relocatable() const { return header().e_type == ET_REL; }
  179. bool is_dynamic() const { return header().e_type == ET_DYN; }
  180. VirtualAddress entry() const { return VirtualAddress(header().e_entry); }
  181. FlatPtr base_address() const { return (FlatPtr)m_buffer; }
  182. size_t size() const { return m_size; }
  183. static Optional<StringView> object_file_type_to_string(Elf_Half type);
  184. static Optional<StringView> object_machine_type_to_string(Elf_Half type);
  185. static Optional<StringView> object_abi_type_to_string(Elf_Byte type);
  186. bool has_symbols() const { return symbol_count(); }
  187. #ifndef KERNEL
  188. Optional<Symbol> find_demangled_function(StringView name) const;
  189. ByteString symbolicate(FlatPtr address, u32* offset = nullptr) const;
  190. #endif
  191. Optional<Image::Symbol> find_symbol(FlatPtr address, u32* offset = nullptr) const;
  192. private:
  193. char const* raw_data(unsigned offset) const;
  194. Elf_Ehdr const& header() const;
  195. Elf_Shdr const& section_header(unsigned) const;
  196. Elf_Phdr const& program_header_internal(unsigned) const;
  197. StringView table_string(unsigned offset) const;
  198. StringView section_header_table_string(unsigned offset) const;
  199. StringView section_index_to_string(unsigned index) const;
  200. StringView table_string(unsigned table_index, unsigned offset) const;
  201. u8 const* m_buffer { nullptr };
  202. size_t m_size { 0 };
  203. bool m_verbose_logging { true };
  204. bool m_valid { false };
  205. unsigned m_symbol_table_section_index { 0 };
  206. unsigned m_string_table_section_index { 0 };
  207. #ifndef KERNEL
  208. struct SortedSymbol {
  209. FlatPtr address;
  210. StringView name;
  211. ByteString demangled_name;
  212. Optional<Image::Symbol> symbol;
  213. };
  214. void sort_symbols() const;
  215. SortedSymbol* find_sorted_symbol(FlatPtr) const;
  216. mutable Vector<SortedSymbol> m_sorted_symbols;
  217. #endif
  218. };
  219. template<IteratorFunction<Image::Section> F>
  220. inline void Image::for_each_section(F func) const
  221. {
  222. auto section_count = this->section_count();
  223. for (unsigned i = 0; i < section_count; ++i) {
  224. if (func(section(i)) == IterationDecision::Break)
  225. break;
  226. }
  227. }
  228. template<VoidFunction<Image::Section> F>
  229. inline void Image::for_each_section(F func) const
  230. {
  231. for_each_section([&](auto section) {
  232. func(move(section));
  233. return IterationDecision::Continue;
  234. });
  235. }
  236. template<IteratorFunction<Image::Section&> F>
  237. inline void Image::for_each_section_of_type(unsigned type, F func) const
  238. {
  239. auto section_count = this->section_count();
  240. for (unsigned i = 0; i < section_count; ++i) {
  241. auto section = this->section(i);
  242. if (section.type() == type) {
  243. if (func(section) == IterationDecision::Break)
  244. break;
  245. }
  246. }
  247. }
  248. template<VoidFunction<Image::Section&> F>
  249. inline void Image::for_each_section_of_type(unsigned type, F func) const
  250. {
  251. for_each_section_of_type(type, [&](auto& section) {
  252. func(section);
  253. return IterationDecision::Continue;
  254. });
  255. }
  256. template<VoidFunction<Image::Relocation&> F>
  257. inline void Image::RelocationSection::for_each_relocation(F func) const
  258. {
  259. auto relocation_count = this->relocation_count();
  260. for (unsigned i = 0; i < relocation_count; ++i) {
  261. func(relocation(i));
  262. }
  263. }
  264. template<IteratorFunction<Image::Symbol> F>
  265. inline void Image::for_each_symbol(F func) const
  266. {
  267. auto symbol_count = this->symbol_count();
  268. for (unsigned i = 0; i < symbol_count; ++i) {
  269. if (func(symbol(i)) == IterationDecision::Break)
  270. break;
  271. }
  272. }
  273. template<VoidFunction<Image::Symbol> F>
  274. inline void Image::for_each_symbol(F func) const
  275. {
  276. for_each_symbol([&](auto symbol) {
  277. func(move(symbol));
  278. return IterationDecision::Continue;
  279. });
  280. }
  281. template<IteratorFunction<Image::ProgramHeader> F>
  282. inline void Image::for_each_program_header(F func) const
  283. {
  284. auto program_header_count = this->program_header_count();
  285. for (unsigned i = 0; i < program_header_count; ++i) {
  286. if (func(program_header(i)) == IterationDecision::Break)
  287. break;
  288. }
  289. }
  290. template<VoidFunction<Image::ProgramHeader> F>
  291. inline void Image::for_each_program_header(F func) const
  292. {
  293. for_each_program_header([&](auto header) {
  294. func(move(header));
  295. return IterationDecision::Continue;
  296. });
  297. }
  298. } // end namespace ELF