DynamicObject.h 17 KB

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