ELFDynamicObject.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Assertions.h>
  28. #include <LibBareMetal/Memory/VirtualAddress.h>
  29. #include <LibELF/exec_elf.h>
  30. class ELFDynamicObject {
  31. public:
  32. explicit ELFDynamicObject(VirtualAddress base_address, VirtualAddress dynamic_section_address);
  33. ~ELFDynamicObject();
  34. void dump() const;
  35. class DynamicEntry;
  36. class Section;
  37. class RelocationSection;
  38. class Symbol;
  39. class Relocation;
  40. class HashSection;
  41. class DynamicEntry {
  42. public:
  43. DynamicEntry(const Elf32_Dyn& dyn)
  44. : m_dyn(dyn)
  45. {
  46. }
  47. ~DynamicEntry() {}
  48. Elf32_Sword tag() const { return m_dyn.d_tag; }
  49. Elf32_Addr ptr() const { return m_dyn.d_un.d_ptr; }
  50. Elf32_Word val() const { return m_dyn.d_un.d_val; }
  51. private:
  52. const Elf32_Dyn& m_dyn;
  53. };
  54. class Symbol {
  55. public:
  56. Symbol(const ELFDynamicObject& dynamic, unsigned index, const Elf32_Sym& sym)
  57. : m_dynamic(dynamic)
  58. , m_sym(sym)
  59. , m_index(index)
  60. {
  61. }
  62. ~Symbol() {}
  63. const char* name() const { return m_dynamic.symbol_string_table_string(m_sym.st_name); }
  64. unsigned section_index() const { return m_sym.st_shndx; }
  65. unsigned value() const { return m_sym.st_value; }
  66. unsigned size() const { return m_sym.st_size; }
  67. unsigned index() const { return m_index; }
  68. unsigned type() const { return ELF32_ST_TYPE(m_sym.st_info); }
  69. unsigned bind() const { return ELF32_ST_BIND(m_sym.st_info); }
  70. bool is_undefined() const { return this == &m_dynamic.the_undefined_symbol(); }
  71. VirtualAddress address() const { return m_dynamic.base_address().offset(value()); }
  72. private:
  73. const ELFDynamicObject& m_dynamic;
  74. const Elf32_Sym& m_sym;
  75. const unsigned m_index;
  76. };
  77. class Section {
  78. public:
  79. Section(const ELFDynamicObject& dynamic, unsigned section_offset, unsigned section_size_bytes, unsigned entry_size, const char* name)
  80. : m_dynamic(dynamic)
  81. , m_section_offset(section_offset)
  82. , m_section_size_bytes(section_size_bytes)
  83. , m_entry_size(entry_size)
  84. , m_name(name)
  85. {
  86. }
  87. ~Section() {}
  88. const char* name() const { return m_name; }
  89. unsigned offset() const { return m_section_offset; }
  90. unsigned size() const { return m_section_size_bytes; }
  91. unsigned entry_size() const { return m_entry_size; }
  92. unsigned entry_count() const { return !entry_size() ? 0 : size() / entry_size(); }
  93. VirtualAddress address() const { return m_dynamic.base_address().offset(m_section_offset); }
  94. protected:
  95. friend class RelocationSection;
  96. friend class HashSection;
  97. const ELFDynamicObject& m_dynamic;
  98. unsigned m_section_offset;
  99. unsigned m_section_size_bytes;
  100. unsigned m_entry_size;
  101. const char* m_name { nullptr };
  102. };
  103. class RelocationSection : public Section {
  104. public:
  105. RelocationSection(const Section& section)
  106. : Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
  107. {
  108. }
  109. unsigned relocation_count() const { return entry_count(); }
  110. const Relocation relocation(unsigned index) const;
  111. const Relocation relocation_at_offset(unsigned offset) const;
  112. template<typename F>
  113. void for_each_relocation(F) const;
  114. };
  115. class Relocation {
  116. public:
  117. Relocation(const ELFDynamicObject& dynamic, const Elf32_Rel& rel, unsigned offset_in_section)
  118. : m_dynamic(dynamic)
  119. , m_rel(rel)
  120. , m_offset_in_section(offset_in_section)
  121. {
  122. }
  123. ~Relocation() {}
  124. unsigned offset_in_section() const { return m_offset_in_section; }
  125. unsigned offset() const { return m_rel.r_offset; }
  126. unsigned type() const { return ELF32_R_TYPE(m_rel.r_info); }
  127. unsigned symbol_index() const { return ELF32_R_SYM(m_rel.r_info); }
  128. const Symbol symbol() const { return m_dynamic.symbol(symbol_index()); }
  129. VirtualAddress address() const { return m_dynamic.base_address().offset(offset()); }
  130. private:
  131. const ELFDynamicObject& m_dynamic;
  132. const Elf32_Rel& m_rel;
  133. const unsigned m_offset_in_section;
  134. };
  135. enum class HashType {
  136. SYSV,
  137. GNU
  138. };
  139. class HashSection : public Section {
  140. public:
  141. HashSection(const Section& section, HashType hash_type = HashType::SYSV)
  142. : Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
  143. {
  144. switch (hash_type) {
  145. case HashType::SYSV:
  146. m_hash_function = &HashSection::calculate_elf_hash;
  147. break;
  148. case HashType::GNU:
  149. m_hash_function = &HashSection::calculate_gnu_hash;
  150. break;
  151. default:
  152. ASSERT_NOT_REACHED();
  153. break;
  154. }
  155. }
  156. const Symbol lookup_symbol(const char*) const;
  157. private:
  158. u32 calculate_elf_hash(const char* name) const;
  159. u32 calculate_gnu_hash(const char* name) const;
  160. typedef u32 (HashSection::*HashFunction)(const char*) const;
  161. HashFunction m_hash_function;
  162. };
  163. unsigned symbol_count() const { return m_symbol_count; }
  164. const Symbol symbol(unsigned) const;
  165. const Symbol& the_undefined_symbol() const { return m_the_undefined_symbol; }
  166. const Section init_section() const;
  167. const Section fini_section() const;
  168. const Section init_array_section() const;
  169. const Section fini_array_section() const;
  170. const HashSection hash_section() const;
  171. const RelocationSection relocation_section() const;
  172. const RelocationSection plt_relocation_section() const;
  173. bool should_process_origin() const { return m_dt_flags & DF_ORIGIN; }
  174. bool requires_symbolic_symbol_resolution() const { return m_dt_flags & DF_SYMBOLIC; }
  175. // Text relocations meaning: we need to edit the .text section which is normally mapped PROT_READ
  176. bool has_text_relocations() const { return m_dt_flags & DF_TEXTREL; }
  177. bool must_bind_now() const { return m_dt_flags & DF_BIND_NOW; }
  178. bool has_static_thread_local_storage() const { return m_dt_flags & DF_STATIC_TLS; }
  179. VirtualAddress plt_got_base_address() const { return m_base_address.offset(m_procedure_linkage_table_offset); }
  180. VirtualAddress base_address() const { return m_base_address; }
  181. private:
  182. const char* symbol_string_table_string(Elf32_Word) const;
  183. void parse();
  184. template<typename F>
  185. void for_each_symbol(F) const;
  186. template<typename F>
  187. void for_each_dynamic_entry(F) const;
  188. VirtualAddress m_base_address;
  189. VirtualAddress m_dynamic_address;
  190. Symbol m_the_undefined_symbol { *this, 0, {} };
  191. unsigned m_symbol_count { 0 };
  192. // Begin Section information collected from DT_* entries
  193. uintptr_t m_init_offset { 0 };
  194. uintptr_t m_fini_offset { 0 };
  195. uintptr_t m_init_array_offset { 0 };
  196. size_t m_init_array_size { 0 };
  197. uintptr_t m_fini_array_offset { 0 };
  198. size_t m_fini_array_size { 0 };
  199. uintptr_t m_hash_table_offset { 0 };
  200. uintptr_t m_string_table_offset { 0 };
  201. size_t m_size_of_string_table { 0 };
  202. uintptr_t m_symbol_table_offset { 0 };
  203. size_t m_size_of_symbol_table_entry { 0 };
  204. Elf32_Sword m_procedure_linkage_table_relocation_type { -1 };
  205. uintptr_t m_plt_relocation_offset_location { 0 }; // offset of PLT relocations, at end of relocations
  206. size_t m_size_of_plt_relocation_entry_list { 0 };
  207. uintptr_t m_procedure_linkage_table_offset { 0 };
  208. // NOTE: We'll only ever either RELA or REL entries, not both (thank god)
  209. // NOTE: The x86 ABI will only ever genrerate REL entries.
  210. size_t m_number_of_relocations { 0 };
  211. size_t m_size_of_relocation_entry { 0 };
  212. size_t m_size_of_relocation_table { 0 };
  213. uintptr_t m_relocation_table_offset { 0 };
  214. // DT_FLAGS
  215. Elf32_Word m_dt_flags { 0 };
  216. // End Section information from DT_* entries
  217. };
  218. template<typename F>
  219. inline void ELFDynamicObject::RelocationSection::for_each_relocation(F func) const
  220. {
  221. for (unsigned i = 0; i < relocation_count(); ++i) {
  222. if (func(relocation(i)) == IterationDecision::Break)
  223. break;
  224. }
  225. }
  226. template<typename F>
  227. inline void ELFDynamicObject::for_each_symbol(F func) const
  228. {
  229. for (unsigned i = 0; i < symbol_count(); ++i) {
  230. if (func(symbol(i)) == IterationDecision::Break)
  231. break;
  232. }
  233. }
  234. template<typename F>
  235. inline void ELFDynamicObject::for_each_dynamic_entry(F func) const
  236. {
  237. auto* dyns = reinterpret_cast<const Elf32_Dyn*>(m_dynamic_address.as_ptr());
  238. for (unsigned i = 0;; ++i) {
  239. auto&& dyn = DynamicEntry(dyns[i]);
  240. if (dyn.tag() == DT_NULL)
  241. break;
  242. if (func(dyn) == IterationDecision::Break)
  243. break;
  244. }
  245. }