DynamicObject.h 13 KB

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