DynamicObject.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 <AK/String.h>
  31. #include <Kernel/VirtualAddress.h>
  32. #include <LibC/elf.h>
  33. namespace ELF {
  34. class DynamicObject : public RefCounted<DynamicObject> {
  35. public:
  36. static NonnullRefPtr<DynamicObject> create(const String& filename, VirtualAddress base_address, VirtualAddress dynamic_section_address);
  37. ~DynamicObject();
  38. void dump() const;
  39. class DynamicEntry;
  40. class Section;
  41. class RelocationSection;
  42. class Symbol;
  43. class Relocation;
  44. class HashSection;
  45. class DynamicEntry {
  46. public:
  47. explicit DynamicEntry(const Elf32_Dyn& dyn)
  48. : m_dyn(dyn)
  49. {
  50. }
  51. ~DynamicEntry() { }
  52. Elf32_Sword tag() const { return m_dyn.d_tag; }
  53. Elf32_Addr ptr() const { return m_dyn.d_un.d_ptr; }
  54. Elf32_Word val() const { return m_dyn.d_un.d_val; }
  55. private:
  56. const Elf32_Dyn& m_dyn;
  57. };
  58. class Symbol {
  59. public:
  60. Symbol(const DynamicObject& dynamic, unsigned index, const Elf32_Sym& sym)
  61. : m_dynamic(dynamic)
  62. , m_sym(sym)
  63. , m_index(index)
  64. {
  65. }
  66. StringView name() const { return m_dynamic.symbol_string_table_string(m_sym.st_name); }
  67. const char* raw_name() const { return m_dynamic.raw_symbol_string_table_string(m_sym.st_name); }
  68. unsigned section_index() const { return m_sym.st_shndx; }
  69. unsigned value() const { return m_sym.st_value; }
  70. unsigned size() const { return m_sym.st_size; }
  71. unsigned index() const { return m_index; }
  72. unsigned type() const { return ELF32_ST_TYPE(m_sym.st_info); }
  73. unsigned bind() const { return ELF32_ST_BIND(m_sym.st_info); }
  74. bool is_undefined() const { return section_index() == 0; }
  75. VirtualAddress address() const
  76. {
  77. if (m_dynamic.elf_is_dynamic())
  78. return m_dynamic.base_address().offset(value());
  79. return VirtualAddress { value() };
  80. }
  81. const DynamicObject& object() const { return m_dynamic; }
  82. private:
  83. const DynamicObject& m_dynamic;
  84. const Elf32_Sym& m_sym;
  85. const unsigned m_index;
  86. };
  87. class Section {
  88. public:
  89. Section(const DynamicObject& dynamic, unsigned section_offset, unsigned section_size_bytes, unsigned entry_size, const StringView& name)
  90. : m_dynamic(dynamic)
  91. , m_section_offset(section_offset)
  92. , m_section_size_bytes(section_size_bytes)
  93. , m_entry_size(entry_size)
  94. , m_name(name)
  95. {
  96. }
  97. ~Section() { }
  98. StringView name() const { return m_name; }
  99. unsigned offset() const { return m_section_offset; }
  100. unsigned size() const { return m_section_size_bytes; }
  101. unsigned entry_size() const { return m_entry_size; }
  102. unsigned entry_count() const
  103. {
  104. return !entry_size() ? 0 : size() / entry_size();
  105. }
  106. VirtualAddress address() const
  107. {
  108. return m_dynamic.base_address().offset(m_section_offset);
  109. }
  110. protected:
  111. friend class RelocationSection;
  112. friend class HashSection;
  113. const DynamicObject& m_dynamic;
  114. unsigned m_section_offset;
  115. unsigned m_section_size_bytes;
  116. unsigned m_entry_size;
  117. StringView m_name;
  118. };
  119. class RelocationSection : public Section {
  120. public:
  121. explicit RelocationSection(const Section& section)
  122. : Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
  123. {
  124. }
  125. unsigned relocation_count() const { return entry_count(); }
  126. Relocation relocation(unsigned index) const;
  127. Relocation relocation_at_offset(unsigned offset) const;
  128. template<typename F>
  129. void for_each_relocation(F) const;
  130. };
  131. class Relocation {
  132. public:
  133. Relocation(const DynamicObject& dynamic, const Elf32_Rel& rel, unsigned offset_in_section)
  134. : m_dynamic(dynamic)
  135. , m_rel(rel)
  136. , m_offset_in_section(offset_in_section)
  137. {
  138. }
  139. ~Relocation() { }
  140. unsigned offset_in_section() const { return m_offset_in_section; }
  141. unsigned offset() const { return m_rel.r_offset; }
  142. unsigned type() const { return ELF32_R_TYPE(m_rel.r_info); }
  143. unsigned symbol_index() const { return ELF32_R_SYM(m_rel.r_info); }
  144. Symbol symbol() const { return m_dynamic.symbol(symbol_index()); }
  145. VirtualAddress address() const
  146. {
  147. if (m_dynamic.elf_is_dynamic())
  148. return m_dynamic.base_address().offset(offset());
  149. return VirtualAddress { offset() };
  150. }
  151. private:
  152. const DynamicObject& m_dynamic;
  153. const Elf32_Rel& m_rel;
  154. const unsigned m_offset_in_section;
  155. };
  156. enum class HashType {
  157. SYSV,
  158. GNU
  159. };
  160. class HashSection : public Section {
  161. public:
  162. HashSection(const Section& section, HashType hash_type)
  163. : Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
  164. , m_hash_type(hash_type)
  165. {
  166. }
  167. Optional<Symbol> lookup_symbol(const StringView& name, u32 gnu_hash, u32 sysv_hash) const
  168. {
  169. if (m_hash_type == HashType::SYSV)
  170. return lookup_sysv_symbol(name, sysv_hash);
  171. return lookup_gnu_symbol(name, gnu_hash);
  172. }
  173. private:
  174. Optional<Symbol> lookup_sysv_symbol(const StringView& name, u32 hash_value) const;
  175. Optional<Symbol> lookup_gnu_symbol(const StringView& name, u32 hash) const;
  176. HashType m_hash_type {};
  177. };
  178. unsigned symbol_count() const { return m_symbol_count; }
  179. Symbol symbol(unsigned) const;
  180. typedef void (*InitializationFunction)();
  181. bool has_init_section() const { return m_init_offset != 0; }
  182. bool has_init_array_section() const { return m_init_array_offset != 0; }
  183. Section init_section() const;
  184. InitializationFunction init_section_function() const;
  185. Section fini_section() const;
  186. Section init_array_section() const;
  187. Section fini_array_section() const;
  188. HashSection hash_section() const
  189. {
  190. auto section_name = m_hash_type == HashType::SYSV ? "DT_HASH"sv : "DT_GNU_HASH"sv;
  191. return HashSection(Section(*this, m_hash_table_offset, 0, 0, section_name), m_hash_type);
  192. }
  193. RelocationSection relocation_section() const;
  194. RelocationSection plt_relocation_section() const;
  195. bool should_process_origin() const { return m_dt_flags & DF_ORIGIN; }
  196. bool requires_symbolic_symbol_resolution() const { return m_dt_flags & DF_SYMBOLIC; }
  197. // Text relocations meaning: we need to edit the .text section which is normally mapped PROT_READ
  198. bool has_text_relocations() const { return m_dt_flags & DF_TEXTREL; }
  199. bool must_bind_now() const { return m_dt_flags & DF_BIND_NOW; }
  200. bool has_static_thread_local_storage() const { return m_dt_flags & DF_STATIC_TLS; }
  201. bool has_plt() const { return m_procedure_linkage_table_offset.has_value(); }
  202. VirtualAddress plt_got_base_address() const { return m_base_address.offset(m_procedure_linkage_table_offset.value()); }
  203. VirtualAddress base_address() const { return m_base_address; }
  204. const String& filename() const { return m_filename; }
  205. StringView rpath() const { return m_has_rpath ? symbol_string_table_string(m_rpath_index) : StringView {}; }
  206. StringView runpath() const { return m_has_runpath ? symbol_string_table_string(m_runpath_index) : StringView {}; }
  207. StringView soname() const { return m_has_soname ? symbol_string_table_string(m_soname_index) : StringView {}; }
  208. Optional<FlatPtr> tls_offset() const { return m_tls_offset; }
  209. Optional<FlatPtr> tls_size() const { return m_tls_size; }
  210. void set_tls_offset(FlatPtr offset) { m_tls_offset = offset; }
  211. void set_tls_size(FlatPtr size) { m_tls_size = size; }
  212. Elf32_Half program_header_count() const;
  213. const Elf32_Phdr* program_headers() const;
  214. template<typename F>
  215. void for_each_needed_library(F) const;
  216. template<typename F>
  217. void for_each_initialization_array_function(F f) const;
  218. template<typename F>
  219. void for_each_dynamic_entry(F) const;
  220. template<typename F>
  221. void for_each_symbol(F) const;
  222. struct SymbolLookupResult {
  223. FlatPtr value { 0 };
  224. VirtualAddress address;
  225. unsigned bind { STB_LOCAL };
  226. const ELF::DynamicObject* dynamic_object { nullptr }; // The object in which the symbol is defined
  227. };
  228. Optional<SymbolLookupResult> lookup_symbol(const StringView& name) const;
  229. Optional<SymbolLookupResult> lookup_symbol(const StringView& name, u32 gnu_hash, u32 sysv_hash) const;
  230. // Will be called from _fixup_plt_entry, as part of the PLT trampoline
  231. VirtualAddress patch_plt_entry(u32 relocation_offset);
  232. bool elf_is_dynamic() const { return m_is_elf_dynamic; }
  233. private:
  234. explicit DynamicObject(const String& filename, VirtualAddress base_address, VirtualAddress dynamic_section_address);
  235. StringView symbol_string_table_string(Elf32_Word) const;
  236. const char* raw_symbol_string_table_string(Elf32_Word) const;
  237. void parse();
  238. String m_filename;
  239. VirtualAddress m_base_address;
  240. VirtualAddress m_dynamic_address;
  241. VirtualAddress m_elf_base_address;
  242. unsigned m_symbol_count { 0 };
  243. // Begin Section information collected from DT_* entries
  244. FlatPtr m_init_offset { 0 };
  245. FlatPtr m_fini_offset { 0 };
  246. FlatPtr m_init_array_offset { 0 };
  247. size_t m_init_array_size { 0 };
  248. FlatPtr m_fini_array_offset { 0 };
  249. size_t m_fini_array_size { 0 };
  250. FlatPtr m_hash_table_offset { 0 };
  251. HashType m_hash_type { HashType::SYSV };
  252. FlatPtr m_string_table_offset { 0 };
  253. size_t m_size_of_string_table { 0 };
  254. FlatPtr m_symbol_table_offset { 0 };
  255. size_t m_size_of_symbol_table_entry { 0 };
  256. Elf32_Sword m_procedure_linkage_table_relocation_type { -1 };
  257. FlatPtr m_plt_relocation_offset_location { 0 }; // offset of PLT relocations, at end of relocations
  258. size_t m_size_of_plt_relocation_entry_list { 0 };
  259. Optional<FlatPtr> m_procedure_linkage_table_offset;
  260. // NOTE: We'll only ever either RELA or REL entries, not both (thank god)
  261. // NOTE: The x86 ABI will only ever genrerate REL entries.
  262. size_t m_number_of_relocations { 0 };
  263. size_t m_size_of_relocation_entry { 0 };
  264. size_t m_size_of_relocation_table { 0 };
  265. FlatPtr m_relocation_table_offset { 0 };
  266. bool m_is_elf_dynamic { false };
  267. // DT_FLAGS
  268. Elf32_Word m_dt_flags { 0 };
  269. bool m_has_soname { false };
  270. Elf32_Word m_soname_index { 0 }; // Index into dynstr table for SONAME
  271. bool m_has_rpath { false };
  272. Elf32_Word m_rpath_index { 0 }; // Index into dynstr table for RPATH
  273. bool m_has_runpath { false };
  274. Elf32_Word m_runpath_index { 0 }; // Index into dynstr table for RUNPATH
  275. Optional<FlatPtr> m_tls_offset;
  276. Optional<FlatPtr> m_tls_size;
  277. // End Section information from DT_* entries
  278. };
  279. template<typename F>
  280. inline void DynamicObject::RelocationSection::for_each_relocation(F func) const
  281. {
  282. for (unsigned i = 0; i < relocation_count(); ++i) {
  283. const auto reloc = relocation(i);
  284. if (reloc.type() == 0)
  285. continue;
  286. if (func(reloc) == IterationDecision::Break)
  287. break;
  288. }
  289. }
  290. template<typename F>
  291. inline void DynamicObject::for_each_symbol(F func) const
  292. {
  293. for (unsigned i = 0; i < symbol_count(); ++i) {
  294. if (func(symbol(i)) == IterationDecision::Break)
  295. break;
  296. }
  297. }
  298. template<typename F>
  299. inline void DynamicObject::for_each_dynamic_entry(F func) const
  300. {
  301. auto* dyns = reinterpret_cast<const Elf32_Dyn*>(m_dynamic_address.as_ptr());
  302. for (unsigned i = 0;; ++i) {
  303. auto&& dyn = DynamicEntry(dyns[i]);
  304. if (dyn.tag() == DT_NULL)
  305. break;
  306. if (func(dyn) == IterationDecision::Break)
  307. break;
  308. }
  309. }
  310. template<typename F>
  311. inline void DynamicObject::for_each_needed_library(F func) const
  312. {
  313. for_each_dynamic_entry([func, this](auto entry) {
  314. if (entry.tag() != DT_NEEDED)
  315. return IterationDecision::Continue;
  316. Elf32_Word offset = entry.val();
  317. StringView name { (const char*)(m_base_address.offset(m_string_table_offset).offset(offset)).as_ptr() };
  318. if (func(StringView(name)) == IterationDecision::Break)
  319. return IterationDecision::Break;
  320. return IterationDecision::Continue;
  321. });
  322. }
  323. template<typename F>
  324. void DynamicObject::for_each_initialization_array_function(F f) const
  325. {
  326. if (!has_init_array_section())
  327. return;
  328. FlatPtr init_array = (FlatPtr)init_array_section().address().as_ptr();
  329. for (size_t i = 0; i < (m_init_array_size / sizeof(void*)); ++i) {
  330. InitializationFunction current = ((InitializationFunction*)(init_array))[i];
  331. f(current);
  332. }
  333. }
  334. } // end namespace ELF