DynamicObject.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
  3. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #pragma once
  28. #include <AK/Assertions.h>
  29. #include <AK/RefCounted.h>
  30. #include <Kernel/VirtualAddress.h>
  31. #include <LibELF/exec_elf.h>
  32. namespace ELF {
  33. class DynamicObject : public RefCounted<DynamicObject> {
  34. public:
  35. static NonnullRefPtr<DynamicObject> create(VirtualAddress base_address, VirtualAddress dynamic_section_address);
  36. ~DynamicObject();
  37. void dump() const;
  38. class DynamicEntry;
  39. class Section;
  40. class RelocationSection;
  41. class Symbol;
  42. class Relocation;
  43. class HashSection;
  44. class DynamicEntry {
  45. public:
  46. explicit DynamicEntry(const Elf32_Dyn& dyn)
  47. : m_dyn(dyn)
  48. {
  49. }
  50. ~DynamicEntry() { }
  51. Elf32_Sword tag() const { return m_dyn.d_tag; }
  52. Elf32_Addr ptr() const { return m_dyn.d_un.d_ptr; }
  53. Elf32_Word val() const { return m_dyn.d_un.d_val; }
  54. private:
  55. const Elf32_Dyn& m_dyn;
  56. };
  57. class Symbol {
  58. public:
  59. Symbol(const DynamicObject& dynamic, unsigned index, const Elf32_Sym& sym)
  60. : m_dynamic(dynamic)
  61. , m_sym(sym)
  62. , m_index(index)
  63. {
  64. if (section_index() == 0)
  65. m_is_undefined = true;
  66. }
  67. Symbol(const Symbol& other)
  68. : m_dynamic(other.m_dynamic)
  69. , m_sym(other.m_sym)
  70. , m_index(other.m_index)
  71. , m_is_undefined(other.m_is_undefined)
  72. {
  73. }
  74. static Symbol create_undefined(const DynamicObject& dynamic)
  75. {
  76. auto s = Symbol(dynamic, 0, {});
  77. s.m_is_undefined = true;
  78. return s;
  79. }
  80. ~Symbol() { }
  81. StringView name() const { return m_dynamic.symbol_string_table_string(m_sym.st_name); }
  82. unsigned section_index() const { return m_sym.st_shndx; }
  83. unsigned value() const { return m_sym.st_value; }
  84. unsigned size() const { return m_sym.st_size; }
  85. unsigned index() const { return m_index; }
  86. unsigned type() const { return ELF32_ST_TYPE(m_sym.st_info); }
  87. unsigned bind() const { return ELF32_ST_BIND(m_sym.st_info); }
  88. bool is_undefined() const
  89. {
  90. return m_is_undefined;
  91. }
  92. VirtualAddress address() const
  93. {
  94. if (m_dynamic.elf_is_dynamic())
  95. return m_dynamic.base_address().offset(value());
  96. return VirtualAddress { value() };
  97. }
  98. const DynamicObject& object() const { return m_dynamic; }
  99. private:
  100. const DynamicObject& m_dynamic;
  101. const Elf32_Sym& m_sym;
  102. const unsigned m_index;
  103. bool m_is_undefined { false };
  104. };
  105. class Section {
  106. public:
  107. Section(const DynamicObject& dynamic, unsigned section_offset, unsigned section_size_bytes, unsigned entry_size, const StringView& name)
  108. : m_dynamic(dynamic)
  109. , m_section_offset(section_offset)
  110. , m_section_size_bytes(section_size_bytes)
  111. , m_entry_size(entry_size)
  112. , m_name(name)
  113. {
  114. }
  115. ~Section() { }
  116. StringView name() const { return m_name; }
  117. unsigned offset() const { return m_section_offset; }
  118. unsigned size() const { return m_section_size_bytes; }
  119. unsigned entry_size() const { return m_entry_size; }
  120. unsigned entry_count() const
  121. {
  122. return !entry_size() ? 0 : size() / entry_size();
  123. }
  124. VirtualAddress address() const
  125. {
  126. return m_dynamic.base_address().offset(m_section_offset);
  127. }
  128. protected:
  129. friend class RelocationSection;
  130. friend class HashSection;
  131. const DynamicObject& m_dynamic;
  132. unsigned m_section_offset;
  133. unsigned m_section_size_bytes;
  134. unsigned m_entry_size;
  135. StringView m_name;
  136. };
  137. class RelocationSection : public Section {
  138. public:
  139. explicit RelocationSection(const Section& section)
  140. : Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
  141. {
  142. }
  143. unsigned relocation_count() const { return entry_count(); }
  144. Relocation relocation(unsigned index) const;
  145. Relocation relocation_at_offset(unsigned offset) const;
  146. template<typename F>
  147. void for_each_relocation(F) const;
  148. };
  149. class Relocation {
  150. public:
  151. Relocation(const DynamicObject& dynamic, const Elf32_Rel& rel, unsigned offset_in_section)
  152. : m_dynamic(dynamic)
  153. , m_rel(rel)
  154. , m_offset_in_section(offset_in_section)
  155. {
  156. }
  157. ~Relocation() { }
  158. unsigned offset_in_section() const { return m_offset_in_section; }
  159. unsigned offset() const { return m_rel.r_offset; }
  160. unsigned type() const { return ELF32_R_TYPE(m_rel.r_info); }
  161. unsigned symbol_index() const { return ELF32_R_SYM(m_rel.r_info); }
  162. Symbol symbol() const { return m_dynamic.symbol(symbol_index()); }
  163. VirtualAddress address() const
  164. {
  165. if (m_dynamic.elf_is_dynamic())
  166. return m_dynamic.base_address().offset(offset());
  167. return VirtualAddress { offset() };
  168. }
  169. private:
  170. const DynamicObject& m_dynamic;
  171. const Elf32_Rel& m_rel;
  172. const unsigned m_offset_in_section;
  173. };
  174. enum class HashType {
  175. SYSV,
  176. GNU
  177. };
  178. class HashSection : public Section {
  179. public:
  180. HashSection(const Section& section, HashType hash_type)
  181. : Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
  182. {
  183. switch (hash_type) {
  184. case HashType::SYSV:
  185. m_lookup_function = &HashSection::lookup_elf_symbol;
  186. break;
  187. case HashType::GNU:
  188. m_lookup_function = &HashSection::lookup_gnu_symbol;
  189. break;
  190. default:
  191. ASSERT_NOT_REACHED();
  192. }
  193. }
  194. Symbol lookup_symbol(const StringView& name) const;
  195. private:
  196. static u32 calculate_elf_hash(const StringView& name);
  197. static u32 calculate_gnu_hash(const StringView& name);
  198. DynamicObject::Symbol lookup_elf_symbol(const StringView& name) const;
  199. DynamicObject::Symbol lookup_gnu_symbol(const StringView& name) const;
  200. typedef DynamicObject::Symbol (HashSection::*LookupFunction)(const StringView&) const;
  201. LookupFunction m_lookup_function {};
  202. };
  203. unsigned symbol_count() const { return m_symbol_count; }
  204. Symbol symbol(unsigned) const;
  205. typedef void (*InitializationFunction)();
  206. bool has_init_section() const { return m_init_offset != 0; }
  207. bool has_init_array_section() const { return m_init_array_offset != 0; }
  208. Section init_section() const;
  209. InitializationFunction init_section_function() const;
  210. Section fini_section() const;
  211. Section init_array_section() const;
  212. Section fini_array_section() const;
  213. HashSection hash_section() const;
  214. RelocationSection relocation_section() const;
  215. RelocationSection plt_relocation_section() const;
  216. bool should_process_origin() const { return m_dt_flags & DF_ORIGIN; }
  217. bool requires_symbolic_symbol_resolution() const { return m_dt_flags & DF_SYMBOLIC; }
  218. // Text relocations meaning: we need to edit the .text section which is normally mapped PROT_READ
  219. bool has_text_relocations() const { return m_dt_flags & DF_TEXTREL; }
  220. bool must_bind_now() const { return m_dt_flags & DF_BIND_NOW; }
  221. bool has_static_thread_local_storage() const { return m_dt_flags & DF_STATIC_TLS; }
  222. bool has_plt() const { return m_procedure_linkage_table_offset.has_value(); }
  223. VirtualAddress plt_got_base_address() const { return m_base_address.offset(m_procedure_linkage_table_offset.value()); }
  224. VirtualAddress base_address() const { return m_base_address; }
  225. StringView soname() const { return m_has_soname ? symbol_string_table_string(m_soname_index) : StringView {}; }
  226. Optional<FlatPtr> tls_offset() const { return m_tls_offset; }
  227. Optional<FlatPtr> tls_size() const { return m_tls_size; }
  228. void set_tls_offset(FlatPtr offset) { m_tls_offset = offset; }
  229. void set_tls_size(FlatPtr size) { m_tls_size = size; }
  230. template<typename F>
  231. void for_each_needed_library(F) const;
  232. template<typename F>
  233. void for_each_initialization_array_function(F f) const;
  234. struct SymbolLookupResult {
  235. FlatPtr value { 0 };
  236. FlatPtr address { 0 };
  237. unsigned bind { STB_LOCAL };
  238. const ELF::DynamicObject* dynamic_object { nullptr }; // The object in which the symbol is defined
  239. };
  240. Optional<SymbolLookupResult> lookup_symbol(const StringView& name) const;
  241. // Will be called from _fixup_plt_entry, as part of the PLT trampoline
  242. Elf32_Addr patch_plt_entry(u32 relocation_offset);
  243. Optional<SymbolLookupResult> lookup_symbol(const ELF::DynamicObject::Symbol&) const;
  244. bool elf_is_dynamic() const { return m_is_elf_dynamic; }
  245. private:
  246. explicit DynamicObject(VirtualAddress base_address, VirtualAddress dynamic_section_address);
  247. StringView symbol_string_table_string(Elf32_Word) const;
  248. void parse();
  249. template<typename F>
  250. void for_each_symbol(F) const;
  251. template<typename F>
  252. void for_each_dynamic_entry(F) const;
  253. VirtualAddress m_base_address;
  254. VirtualAddress m_dynamic_address;
  255. VirtualAddress m_elf_base_address;
  256. unsigned m_symbol_count { 0 };
  257. // Begin Section information collected from DT_* entries
  258. FlatPtr m_init_offset { 0 };
  259. FlatPtr m_fini_offset { 0 };
  260. FlatPtr m_init_array_offset { 0 };
  261. size_t m_init_array_size { 0 };
  262. FlatPtr m_fini_array_offset { 0 };
  263. size_t m_fini_array_size { 0 };
  264. FlatPtr m_hash_table_offset { 0 };
  265. HashType m_hash_type { HashType::SYSV };
  266. FlatPtr m_string_table_offset { 0 };
  267. size_t m_size_of_string_table { 0 };
  268. FlatPtr m_symbol_table_offset { 0 };
  269. size_t m_size_of_symbol_table_entry { 0 };
  270. Elf32_Sword m_procedure_linkage_table_relocation_type { -1 };
  271. FlatPtr m_plt_relocation_offset_location { 0 }; // offset of PLT relocations, at end of relocations
  272. size_t m_size_of_plt_relocation_entry_list { 0 };
  273. Optional<FlatPtr> m_procedure_linkage_table_offset;
  274. // NOTE: We'll only ever either RELA or REL entries, not both (thank god)
  275. // NOTE: The x86 ABI will only ever genrerate REL entries.
  276. size_t m_number_of_relocations { 0 };
  277. size_t m_size_of_relocation_entry { 0 };
  278. size_t m_size_of_relocation_table { 0 };
  279. FlatPtr m_relocation_table_offset { 0 };
  280. bool m_is_elf_dynamic { false };
  281. // DT_FLAGS
  282. Elf32_Word m_dt_flags { 0 };
  283. bool m_has_soname { false };
  284. Elf32_Word m_soname_index { 0 }; // Index into dynstr table for SONAME
  285. Optional<FlatPtr> m_tls_offset;
  286. Optional<FlatPtr> m_tls_size;
  287. // End Section information from DT_* entries
  288. };
  289. template<typename F>
  290. inline void DynamicObject::RelocationSection::for_each_relocation(F func) const
  291. {
  292. for (unsigned i = 0; i < relocation_count(); ++i) {
  293. const auto reloc = relocation(i);
  294. if (reloc.type() == 0)
  295. continue;
  296. if (func(reloc) == IterationDecision::Break)
  297. break;
  298. }
  299. }
  300. template<typename F>
  301. inline void DynamicObject::for_each_symbol(F func) const
  302. {
  303. for (unsigned i = 0; i < symbol_count(); ++i) {
  304. if (func(symbol(i)) == IterationDecision::Break)
  305. break;
  306. }
  307. }
  308. template<typename F>
  309. inline void DynamicObject::for_each_dynamic_entry(F func) const
  310. {
  311. auto* dyns = reinterpret_cast<const Elf32_Dyn*>(m_dynamic_address.as_ptr());
  312. for (unsigned i = 0;; ++i) {
  313. auto&& dyn = DynamicEntry(dyns[i]);
  314. if (dyn.tag() == DT_NULL)
  315. break;
  316. if (func(dyn) == IterationDecision::Break)
  317. break;
  318. }
  319. }
  320. template<typename F>
  321. inline void DynamicObject::for_each_needed_library(F func) const
  322. {
  323. for_each_dynamic_entry([func, this](auto entry) {
  324. if (entry.tag() != DT_NEEDED)
  325. return IterationDecision::Continue;
  326. Elf32_Word offset = entry.val();
  327. StringView name { (const char*)(m_base_address.offset(m_string_table_offset).offset(offset)).as_ptr() };
  328. if (func(StringView(name)) == IterationDecision::Break)
  329. return IterationDecision::Break;
  330. return IterationDecision::Continue;
  331. });
  332. }
  333. template<typename F>
  334. void DynamicObject::for_each_initialization_array_function(F f) const
  335. {
  336. if (!has_init_array_section())
  337. return;
  338. FlatPtr init_array = (FlatPtr)init_array_section().address().as_ptr();
  339. for (size_t i = 0; i < (m_init_array_size / sizeof(void*)); ++i) {
  340. InitializationFunction current = ((InitializationFunction*)(init_array))[i];
  341. f(current);
  342. }
  343. }
  344. } // end namespace ELF