DynamicObject.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 <Kernel/VirtualAddress.h>
  29. #include <LibELF/exec_elf.h>
  30. namespace ELF {
  31. class DynamicObject {
  32. public:
  33. explicit DynamicObject(VirtualAddress base_address, VirtualAddress dynamic_section_address);
  34. ~DynamicObject();
  35. void dump() const;
  36. class DynamicEntry;
  37. class Section;
  38. class RelocationSection;
  39. class Symbol;
  40. class Relocation;
  41. class HashSection;
  42. class DynamicEntry {
  43. public:
  44. DynamicEntry(const Elf32_Dyn& dyn)
  45. : m_dyn(dyn)
  46. {
  47. }
  48. ~DynamicEntry() { }
  49. Elf32_Sword tag() const { return m_dyn.d_tag; }
  50. Elf32_Addr ptr() const { return m_dyn.d_un.d_ptr; }
  51. Elf32_Word val() const { return m_dyn.d_un.d_val; }
  52. private:
  53. const Elf32_Dyn& m_dyn;
  54. };
  55. class Symbol {
  56. public:
  57. Symbol(const DynamicObject& dynamic, unsigned index, const Elf32_Sym& sym)
  58. : m_dynamic(dynamic)
  59. , m_sym(sym)
  60. , m_index(index)
  61. {
  62. }
  63. ~Symbol() { }
  64. const char* name() const { return m_dynamic.symbol_string_table_string(m_sym.st_name); }
  65. unsigned section_index() const { return m_sym.st_shndx; }
  66. unsigned value() const { return m_sym.st_value; }
  67. unsigned size() const { return m_sym.st_size; }
  68. unsigned index() const { return m_index; }
  69. unsigned type() const { return ELF32_ST_TYPE(m_sym.st_info); }
  70. unsigned bind() const { return ELF32_ST_BIND(m_sym.st_info); }
  71. bool is_undefined() const { return this == &m_dynamic.the_undefined_symbol(); }
  72. VirtualAddress address() const { return m_dynamic.base_address().offset(value()); }
  73. private:
  74. const DynamicObject& m_dynamic;
  75. const Elf32_Sym& m_sym;
  76. const unsigned m_index;
  77. };
  78. class Section {
  79. public:
  80. Section(const DynamicObject& dynamic, unsigned section_offset, unsigned section_size_bytes, unsigned entry_size, const char* name)
  81. : m_dynamic(dynamic)
  82. , m_section_offset(section_offset)
  83. , m_section_size_bytes(section_size_bytes)
  84. , m_entry_size(entry_size)
  85. , m_name(name)
  86. {
  87. }
  88. ~Section() { }
  89. const char* name() const { return m_name; }
  90. unsigned offset() const { return m_section_offset; }
  91. unsigned size() const { return m_section_size_bytes; }
  92. unsigned entry_size() const { return m_entry_size; }
  93. unsigned entry_count() const { return !entry_size() ? 0 : size() / entry_size(); }
  94. VirtualAddress address() const { return m_dynamic.base_address().offset(m_section_offset); }
  95. protected:
  96. friend class RelocationSection;
  97. friend class HashSection;
  98. const DynamicObject& m_dynamic;
  99. unsigned m_section_offset;
  100. unsigned m_section_size_bytes;
  101. unsigned m_entry_size;
  102. const char* m_name { nullptr };
  103. };
  104. class RelocationSection : public Section {
  105. public:
  106. RelocationSection(const Section& section)
  107. : Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
  108. {
  109. }
  110. unsigned relocation_count() const { return entry_count(); }
  111. const Relocation relocation(unsigned index) const;
  112. const Relocation relocation_at_offset(unsigned offset) const;
  113. template<typename F>
  114. void for_each_relocation(F) const;
  115. };
  116. class Relocation {
  117. public:
  118. Relocation(const DynamicObject& dynamic, const Elf32_Rel& rel, unsigned offset_in_section)
  119. : m_dynamic(dynamic)
  120. , m_rel(rel)
  121. , m_offset_in_section(offset_in_section)
  122. {
  123. }
  124. ~Relocation() { }
  125. unsigned offset_in_section() const { return m_offset_in_section; }
  126. unsigned offset() const { return m_rel.r_offset; }
  127. unsigned type() const { return ELF32_R_TYPE(m_rel.r_info); }
  128. unsigned symbol_index() const { return ELF32_R_SYM(m_rel.r_info); }
  129. const Symbol symbol() const { return m_dynamic.symbol(symbol_index()); }
  130. VirtualAddress address() const { return m_dynamic.base_address().offset(offset()); }
  131. private:
  132. const DynamicObject& m_dynamic;
  133. const Elf32_Rel& m_rel;
  134. const unsigned m_offset_in_section;
  135. };
  136. enum class HashType {
  137. SYSV,
  138. GNU
  139. };
  140. class HashSection : public Section {
  141. public:
  142. HashSection(const Section& section, HashType hash_type = HashType::SYSV)
  143. : Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
  144. {
  145. switch (hash_type) {
  146. case HashType::SYSV:
  147. m_hash_function = &HashSection::calculate_elf_hash;
  148. break;
  149. case HashType::GNU:
  150. m_hash_function = &HashSection::calculate_gnu_hash;
  151. break;
  152. default:
  153. ASSERT_NOT_REACHED();
  154. break;
  155. }
  156. }
  157. const Symbol lookup_symbol(const char*) const;
  158. private:
  159. u32 calculate_elf_hash(const char* name) const;
  160. u32 calculate_gnu_hash(const char* name) const;
  161. typedef u32 (HashSection::*HashFunction)(const char*) const;
  162. HashFunction m_hash_function;
  163. };
  164. unsigned symbol_count() const { return m_symbol_count; }
  165. const Symbol symbol(unsigned) const;
  166. const Symbol& the_undefined_symbol() const { return m_the_undefined_symbol; }
  167. const Section init_section() const;
  168. const Section fini_section() const;
  169. const Section init_array_section() const;
  170. const Section fini_array_section() const;
  171. const HashSection hash_section() const;
  172. const RelocationSection relocation_section() const;
  173. const RelocationSection plt_relocation_section() const;
  174. bool should_process_origin() const { return m_dt_flags & DF_ORIGIN; }
  175. bool requires_symbolic_symbol_resolution() const { return m_dt_flags & DF_SYMBOLIC; }
  176. // Text relocations meaning: we need to edit the .text section which is normally mapped PROT_READ
  177. bool has_text_relocations() const { return m_dt_flags & DF_TEXTREL; }
  178. bool must_bind_now() const { return m_dt_flags & DF_BIND_NOW; }
  179. bool has_static_thread_local_storage() const { return m_dt_flags & DF_STATIC_TLS; }
  180. VirtualAddress plt_got_base_address() const { return m_base_address.offset(m_procedure_linkage_table_offset); }
  181. VirtualAddress base_address() const { return m_base_address; }
  182. const char* soname() const { return m_has_soname ? symbol_string_table_string(m_soname_index) : nullptr; }
  183. private:
  184. const char* symbol_string_table_string(Elf32_Word) const;
  185. void parse();
  186. template<typename F>
  187. void for_each_symbol(F) const;
  188. template<typename F>
  189. void for_each_dynamic_entry(F) const;
  190. VirtualAddress m_base_address;
  191. VirtualAddress m_dynamic_address;
  192. Symbol m_the_undefined_symbol { *this, 0, {} };
  193. unsigned m_symbol_count { 0 };
  194. // Begin Section information collected from DT_* entries
  195. FlatPtr m_init_offset { 0 };
  196. FlatPtr m_fini_offset { 0 };
  197. FlatPtr m_init_array_offset { 0 };
  198. size_t m_init_array_size { 0 };
  199. FlatPtr m_fini_array_offset { 0 };
  200. size_t m_fini_array_size { 0 };
  201. FlatPtr m_hash_table_offset { 0 };
  202. FlatPtr m_string_table_offset { 0 };
  203. size_t m_size_of_string_table { 0 };
  204. FlatPtr m_symbol_table_offset { 0 };
  205. size_t m_size_of_symbol_table_entry { 0 };
  206. Elf32_Sword m_procedure_linkage_table_relocation_type { -1 };
  207. FlatPtr m_plt_relocation_offset_location { 0 }; // offset of PLT relocations, at end of relocations
  208. size_t m_size_of_plt_relocation_entry_list { 0 };
  209. FlatPtr m_procedure_linkage_table_offset { 0 };
  210. // NOTE: We'll only ever either RELA or REL entries, not both (thank god)
  211. // NOTE: The x86 ABI will only ever genrerate REL entries.
  212. size_t m_number_of_relocations { 0 };
  213. size_t m_size_of_relocation_entry { 0 };
  214. size_t m_size_of_relocation_table { 0 };
  215. FlatPtr m_relocation_table_offset { 0 };
  216. // DT_FLAGS
  217. Elf32_Word m_dt_flags { 0 };
  218. bool m_has_soname { false };
  219. Elf32_Word m_soname_index { 0 }; // Index into dynstr table for SONAME
  220. // End Section information from DT_* entries
  221. };
  222. template<typename F>
  223. inline void DynamicObject::RelocationSection::for_each_relocation(F func) const
  224. {
  225. for (unsigned i = 0; i < relocation_count(); ++i) {
  226. if (func(relocation(i)) == IterationDecision::Break)
  227. break;
  228. }
  229. }
  230. template<typename F>
  231. inline void DynamicObject::for_each_symbol(F func) const
  232. {
  233. for (unsigned i = 0; i < symbol_count(); ++i) {
  234. if (func(symbol(i)) == IterationDecision::Break)
  235. break;
  236. }
  237. }
  238. template<typename F>
  239. inline void DynamicObject::for_each_dynamic_entry(F func) const
  240. {
  241. auto* dyns = reinterpret_cast<const Elf32_Dyn*>(m_dynamic_address.as_ptr());
  242. for (unsigned i = 0;; ++i) {
  243. auto&& dyn = DynamicEntry(dyns[i]);
  244. if (dyn.tag() == DT_NULL)
  245. break;
  246. if (func(dyn) == IterationDecision::Break)
  247. break;
  248. }
  249. }
  250. } // end namespace ELF