DynamicObject.h 15 KB

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