Image.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/VirtualAddress.h>
  11. #include <LibC/elf.h>
  12. #ifndef KERNEL
  13. # include <AK/String.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, const ElfW(Sym) & 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. #if ARCH(I386)
  51. unsigned type() const
  52. {
  53. return ELF32_ST_TYPE(m_sym.st_info);
  54. }
  55. unsigned bind() const { return ELF32_ST_BIND(m_sym.st_info); }
  56. #else
  57. unsigned type() const
  58. {
  59. return ELF64_ST_TYPE(m_sym.st_info);
  60. }
  61. unsigned bind() const { return ELF64_ST_BIND(m_sym.st_info); }
  62. #endif
  63. Section section() const
  64. {
  65. return m_image.section(section_index());
  66. }
  67. bool is_undefined() const { return section_index() == 0; }
  68. StringView raw_data() const;
  69. private:
  70. Image const& m_image;
  71. const ElfW(Sym) & m_sym;
  72. unsigned const m_index;
  73. };
  74. class ProgramHeader {
  75. public:
  76. ProgramHeader(Image const& image, unsigned program_header_index)
  77. : m_image(image)
  78. , m_program_header(image.program_header_internal(program_header_index))
  79. , m_program_header_index(program_header_index)
  80. {
  81. }
  82. ~ProgramHeader() = default;
  83. unsigned index() const { return m_program_header_index; }
  84. u32 type() const { return m_program_header.p_type; }
  85. u32 flags() const { return m_program_header.p_flags; }
  86. size_t offset() const { return m_program_header.p_offset; }
  87. VirtualAddress vaddr() const { return VirtualAddress(m_program_header.p_vaddr); }
  88. size_t size_in_memory() const { return m_program_header.p_memsz; }
  89. size_t size_in_image() const { return m_program_header.p_filesz; }
  90. size_t alignment() const { return m_program_header.p_align; }
  91. bool is_readable() const { return flags() & PF_R; }
  92. bool is_writable() const { return flags() & PF_W; }
  93. bool is_executable() const { return flags() & PF_X; }
  94. char const* raw_data() const { return m_image.raw_data(m_program_header.p_offset); }
  95. ElfW(Phdr) raw_header() const { return m_program_header; }
  96. private:
  97. Image const& m_image;
  98. const ElfW(Phdr) & m_program_header;
  99. unsigned m_program_header_index { 0 };
  100. };
  101. class Section {
  102. public:
  103. Section(Image const& image, unsigned sectionIndex)
  104. : m_image(image)
  105. , m_section_header(image.section_header(sectionIndex))
  106. , m_section_index(sectionIndex)
  107. {
  108. }
  109. ~Section() = default;
  110. StringView name() const { return m_image.section_header_table_string(m_section_header.sh_name); }
  111. u32 type() const { return m_section_header.sh_type; }
  112. size_t offset() const { return m_section_header.sh_offset; }
  113. size_t size() const { return m_section_header.sh_size; }
  114. size_t entry_size() const { return m_section_header.sh_entsize; }
  115. size_t entry_count() const { return !entry_size() ? 0 : size() / entry_size(); }
  116. FlatPtr address() const { return m_section_header.sh_addr; }
  117. char const* raw_data() const { return m_image.raw_data(m_section_header.sh_offset); }
  118. ReadonlyBytes bytes() const { return { raw_data(), size() }; }
  119. Optional<RelocationSection> relocations() const;
  120. auto flags() const { return m_section_header.sh_flags; }
  121. bool is_writable() const { return flags() & SHF_WRITE; }
  122. bool is_executable() const { return flags() & PF_X; }
  123. protected:
  124. friend class RelocationSection;
  125. Image const& m_image;
  126. const ElfW(Shdr) & m_section_header;
  127. unsigned m_section_index;
  128. };
  129. class RelocationSection : public Section {
  130. public:
  131. explicit RelocationSection(Section const& section)
  132. : Section(section.m_image, section.m_section_index)
  133. {
  134. }
  135. size_t relocation_count() const { return entry_count(); }
  136. Relocation relocation(unsigned index) const;
  137. template<VoidFunction<Image::Relocation&> F>
  138. void for_each_relocation(F) const;
  139. };
  140. class Relocation {
  141. public:
  142. Relocation(Image const& image, const ElfW(Rel) & rel)
  143. : m_image(image)
  144. , m_rel(rel)
  145. {
  146. }
  147. ~Relocation() = default;
  148. size_t offset() const { return m_rel.r_offset; }
  149. #if ARCH(I386)
  150. unsigned type() const
  151. {
  152. return ELF32_R_TYPE(m_rel.r_info);
  153. }
  154. unsigned symbol_index() const { return ELF32_R_SYM(m_rel.r_info); }
  155. #else
  156. unsigned type() const
  157. {
  158. return ELF64_R_TYPE(m_rel.r_info);
  159. }
  160. unsigned symbol_index() const { return ELF64_R_SYM(m_rel.r_info); }
  161. #endif
  162. Symbol symbol() const
  163. {
  164. return m_image.symbol(symbol_index());
  165. }
  166. private:
  167. Image const& m_image;
  168. const ElfW(Rel) & m_rel;
  169. };
  170. unsigned symbol_count() const;
  171. unsigned section_count() const;
  172. unsigned program_header_count() const;
  173. Symbol symbol(unsigned) const;
  174. Section section(unsigned) const;
  175. ProgramHeader program_header(unsigned) const;
  176. FlatPtr program_header_table_offset() const;
  177. template<IteratorFunction<Image::Section> F>
  178. void for_each_section(F) const;
  179. template<VoidFunction<Section> F>
  180. void for_each_section(F) const;
  181. template<IteratorFunction<Section&> F>
  182. void for_each_section_of_type(unsigned, F) const;
  183. template<VoidFunction<Section&> F>
  184. void for_each_section_of_type(unsigned, F) const;
  185. template<IteratorFunction<Symbol> F>
  186. void for_each_symbol(F) const;
  187. template<VoidFunction<Symbol> F>
  188. void for_each_symbol(F) const;
  189. template<IteratorFunction<ProgramHeader> F>
  190. void for_each_program_header(F func) const;
  191. template<VoidFunction<ProgramHeader> F>
  192. void for_each_program_header(F) const;
  193. Optional<Section> lookup_section(StringView name) const;
  194. bool is_executable() const { return header().e_type == ET_EXEC; }
  195. bool is_relocatable() const { return header().e_type == ET_REL; }
  196. bool is_dynamic() const { return header().e_type == ET_DYN; }
  197. VirtualAddress entry() const { return VirtualAddress(header().e_entry); }
  198. FlatPtr base_address() const { return (FlatPtr)m_buffer; }
  199. size_t size() const { return m_size; }
  200. static Optional<StringView> object_file_type_to_string(ElfW(Half) type);
  201. static Optional<StringView> object_machine_type_to_string(ElfW(Half) type);
  202. static Optional<StringView> object_abi_type_to_string(Elf_Byte type);
  203. bool has_symbols() const { return symbol_count(); }
  204. #ifndef KERNEL
  205. Optional<Symbol> find_demangled_function(StringView name) const;
  206. String symbolicate(FlatPtr address, u32* offset = nullptr) const;
  207. #endif
  208. Optional<Image::Symbol> find_symbol(FlatPtr address, u32* offset = nullptr) const;
  209. private:
  210. char const* raw_data(unsigned offset) const;
  211. const ElfW(Ehdr) & header() const;
  212. const ElfW(Shdr) & section_header(unsigned) const;
  213. const ElfW(Phdr) & program_header_internal(unsigned) const;
  214. StringView table_string(unsigned offset) const;
  215. StringView section_header_table_string(unsigned offset) const;
  216. StringView section_index_to_string(unsigned index) const;
  217. StringView table_string(unsigned table_index, unsigned offset) const;
  218. u8 const* m_buffer { nullptr };
  219. size_t m_size { 0 };
  220. bool m_verbose_logging { true };
  221. bool m_valid { false };
  222. unsigned m_symbol_table_section_index { 0 };
  223. unsigned m_string_table_section_index { 0 };
  224. #ifndef KERNEL
  225. struct SortedSymbol {
  226. FlatPtr address;
  227. StringView name;
  228. String demangled_name;
  229. Optional<Image::Symbol> symbol;
  230. };
  231. void sort_symbols() const;
  232. SortedSymbol* find_sorted_symbol(FlatPtr) const;
  233. mutable Vector<SortedSymbol> m_sorted_symbols;
  234. #endif
  235. };
  236. template<IteratorFunction<Image::Section> F>
  237. inline void Image::for_each_section(F func) const
  238. {
  239. auto section_count = this->section_count();
  240. for (unsigned i = 0; i < section_count; ++i) {
  241. if (func(section(i)) == IterationDecision::Break)
  242. break;
  243. }
  244. }
  245. template<VoidFunction<Image::Section> F>
  246. inline void Image::for_each_section(F func) const
  247. {
  248. for_each_section([&](auto section) {
  249. func(move(section));
  250. return IterationDecision::Continue;
  251. });
  252. }
  253. template<IteratorFunction<Image::Section&> F>
  254. inline void Image::for_each_section_of_type(unsigned type, F func) const
  255. {
  256. auto section_count = this->section_count();
  257. for (unsigned i = 0; i < section_count; ++i) {
  258. auto section = this->section(i);
  259. if (section.type() == type) {
  260. if (func(section) == IterationDecision::Break)
  261. break;
  262. }
  263. }
  264. }
  265. template<VoidFunction<Image::Section&> F>
  266. inline void Image::for_each_section_of_type(unsigned type, F func) const
  267. {
  268. for_each_section_of_type(type, [&](auto& section) {
  269. func(section);
  270. return IterationDecision::Continue;
  271. });
  272. }
  273. template<VoidFunction<Image::Relocation&> F>
  274. inline void Image::RelocationSection::for_each_relocation(F func) const
  275. {
  276. auto relocation_count = this->relocation_count();
  277. for (unsigned i = 0; i < relocation_count; ++i) {
  278. func(relocation(i));
  279. }
  280. }
  281. template<IteratorFunction<Image::Symbol> F>
  282. inline void Image::for_each_symbol(F func) const
  283. {
  284. auto symbol_count = this->symbol_count();
  285. for (unsigned i = 0; i < symbol_count; ++i) {
  286. if (func(symbol(i)) == IterationDecision::Break)
  287. break;
  288. }
  289. }
  290. template<VoidFunction<Image::Symbol> F>
  291. inline void Image::for_each_symbol(F func) const
  292. {
  293. for_each_symbol([&](auto symbol) {
  294. func(move(symbol));
  295. return IterationDecision::Continue;
  296. });
  297. }
  298. template<IteratorFunction<Image::ProgramHeader> F>
  299. inline void Image::for_each_program_header(F func) const
  300. {
  301. auto program_header_count = this->program_header_count();
  302. for (unsigned i = 0; i < program_header_count; ++i) {
  303. if (func(program_header(i)) == IterationDecision::Break)
  304. break;
  305. }
  306. }
  307. template<VoidFunction<Image::ProgramHeader> F>
  308. inline void Image::for_each_program_header(F func) const
  309. {
  310. for_each_program_header([&](auto header) {
  311. func(move(header));
  312. return IterationDecision::Continue;
  313. });
  314. }
  315. } // end namespace ELF