DynamicObject.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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& filename, 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. bool has_init_section() const { return m_init_offset != 0; }
  222. bool has_init_array_section() const { return m_init_array_offset != 0; }
  223. Section init_section() const;
  224. InitializationFunction init_section_function() const;
  225. Section fini_section() const;
  226. Section init_array_section() const;
  227. Section fini_array_section() const;
  228. HashSection hash_section() const
  229. {
  230. auto section_name = m_hash_type == HashType::SYSV ? "DT_HASH"sv : "DT_GNU_HASH"sv;
  231. return HashSection(Section(*this, m_hash_table_offset, 0, 0, section_name), m_hash_type);
  232. }
  233. RelocationSection relocation_section() const;
  234. RelocationSection plt_relocation_section() const;
  235. Section relr_relocation_section() const;
  236. bool should_process_origin() const { return m_dt_flags & DF_ORIGIN; }
  237. bool requires_symbolic_symbol_resolution() const { return m_dt_flags & DF_SYMBOLIC; }
  238. // Text relocations meaning: we need to edit the .text section which is normally mapped PROT_READ
  239. bool has_text_relocations() const { return m_dt_flags & DF_TEXTREL; }
  240. bool must_bind_now() const { return m_dt_flags & DF_BIND_NOW; }
  241. bool has_static_thread_local_storage() const { return m_dt_flags & DF_STATIC_TLS; }
  242. bool has_plt() const { return m_procedure_linkage_table_offset.has_value(); }
  243. VirtualAddress plt_got_base_address() const { return m_base_address.offset(m_procedure_linkage_table_offset.value()); }
  244. VirtualAddress base_address() const { return m_base_address; }
  245. String const& filename() const { return m_filename; }
  246. StringView rpath() const { return m_has_rpath ? symbol_string_table_string(m_rpath_index) : StringView {}; }
  247. StringView runpath() const { return m_has_runpath ? symbol_string_table_string(m_runpath_index) : StringView {}; }
  248. StringView soname() const { return m_has_soname ? symbol_string_table_string(m_soname_index) : StringView {}; }
  249. Optional<FlatPtr> tls_offset() const { return m_tls_offset; }
  250. Optional<FlatPtr> tls_size() const { return m_tls_size; }
  251. void set_tls_offset(FlatPtr offset) { m_tls_offset = offset; }
  252. void set_tls_size(FlatPtr size) { m_tls_size = size; }
  253. ElfW(Half) program_header_count() const;
  254. const ElfW(Phdr) * program_headers() const;
  255. template<VoidFunction<StringView> F>
  256. void for_each_needed_library(F) const;
  257. template<VoidFunction<InitializationFunction&> F>
  258. void for_each_initialization_array_function(F f) const;
  259. template<IteratorFunction<DynamicEntry&> F>
  260. void for_each_dynamic_entry(F) const;
  261. template<VoidFunction<DynamicEntry&> F>
  262. void for_each_dynamic_entry(F func) const;
  263. template<VoidFunction<Symbol&> F>
  264. void for_each_symbol(F) const;
  265. template<typename F>
  266. void for_each_relr_relocation(F) const;
  267. struct SymbolLookupResult {
  268. FlatPtr value { 0 };
  269. size_t size { 0 };
  270. VirtualAddress address;
  271. unsigned bind { STB_LOCAL };
  272. const ELF::DynamicObject* dynamic_object { nullptr }; // The object in which the symbol is defined
  273. };
  274. Optional<SymbolLookupResult> lookup_symbol(StringView name) const;
  275. Optional<SymbolLookupResult> lookup_symbol(HashSymbol const& symbol) const;
  276. // Will be called from _fixup_plt_entry, as part of the PLT trampoline
  277. VirtualAddress patch_plt_entry(u32 relocation_offset);
  278. bool elf_is_dynamic() const { return m_is_elf_dynamic; }
  279. void* symbol_for_name(StringView name);
  280. private:
  281. explicit DynamicObject(String const& filename, VirtualAddress base_address, VirtualAddress dynamic_section_address);
  282. StringView symbol_string_table_string(ElfW(Word)) const;
  283. char const* raw_symbol_string_table_string(ElfW(Word)) const;
  284. void parse();
  285. String m_filename;
  286. VirtualAddress m_base_address;
  287. VirtualAddress m_dynamic_address;
  288. VirtualAddress m_elf_base_address;
  289. unsigned m_symbol_count { 0 };
  290. // Begin Section information collected from DT_* entries
  291. FlatPtr m_init_offset { 0 };
  292. FlatPtr m_fini_offset { 0 };
  293. FlatPtr m_init_array_offset { 0 };
  294. size_t m_init_array_size { 0 };
  295. FlatPtr m_fini_array_offset { 0 };
  296. size_t m_fini_array_size { 0 };
  297. FlatPtr m_hash_table_offset { 0 };
  298. HashType m_hash_type { HashType::SYSV };
  299. FlatPtr m_string_table_offset { 0 };
  300. size_t m_size_of_string_table { 0 };
  301. FlatPtr m_symbol_table_offset { 0 };
  302. size_t m_size_of_symbol_table_entry { 0 };
  303. ElfW(Sword) m_procedure_linkage_table_relocation_type { -1 };
  304. FlatPtr m_plt_relocation_offset_location { 0 }; // offset of PLT relocations, at end of relocations
  305. size_t m_size_of_plt_relocation_entry_list { 0 };
  306. Optional<FlatPtr> m_procedure_linkage_table_offset;
  307. // NOTE: We'll only ever either RELA or REL entries, not both (thank god)
  308. // NOTE: The x86 ABI will only ever genrerate REL entries.
  309. size_t m_number_of_relocations { 0 };
  310. size_t m_size_of_relocation_entry { 0 };
  311. size_t m_size_of_relocation_table { 0 };
  312. bool m_addend_used { false };
  313. FlatPtr m_relocation_table_offset { 0 };
  314. size_t m_size_of_relr_relocations_entry { 0 };
  315. size_t m_size_of_relr_relocation_table { 0 };
  316. FlatPtr m_relr_relocation_table_offset { 0 };
  317. bool m_is_elf_dynamic { false };
  318. // DT_FLAGS
  319. ElfW(Word) m_dt_flags { 0 };
  320. bool m_has_soname { false };
  321. ElfW(Word) m_soname_index { 0 }; // Index into dynstr table for SONAME
  322. bool m_has_rpath { false };
  323. ElfW(Word) m_rpath_index { 0 }; // Index into dynstr table for RPATH
  324. bool m_has_runpath { false };
  325. ElfW(Word) m_runpath_index { 0 }; // Index into dynstr table for RUNPATH
  326. Optional<FlatPtr> m_tls_offset;
  327. Optional<FlatPtr> m_tls_size;
  328. // End Section information from DT_* entries
  329. };
  330. template<IteratorFunction<DynamicObject::Relocation&> F>
  331. inline void DynamicObject::RelocationSection::for_each_relocation(F func) const
  332. {
  333. for (unsigned i = 0; i < relocation_count(); ++i) {
  334. auto const reloc = relocation(i);
  335. if (reloc.type() == 0)
  336. continue;
  337. if (func(reloc) == IterationDecision::Break)
  338. break;
  339. }
  340. }
  341. template<VoidFunction<DynamicObject::Relocation&> F>
  342. inline void DynamicObject::RelocationSection::for_each_relocation(F func) const
  343. {
  344. for_each_relocation([&](auto& reloc) {
  345. func(reloc);
  346. return IterationDecision::Continue;
  347. });
  348. }
  349. template<typename F>
  350. inline void DynamicObject::for_each_relr_relocation(F f) const
  351. {
  352. auto section = relr_relocation_section();
  353. if (section.entry_count() == 0)
  354. return;
  355. VERIFY(section.entry_size() == sizeof(FlatPtr));
  356. VERIFY(section.size() >= section.entry_size() * section.entry_count());
  357. auto* entries = reinterpret_cast<ElfW(Relr)*>(section.address().get());
  358. auto base = base_address().get();
  359. FlatPtr patch_addr = 0;
  360. for (unsigned i = 0; i < section.entry_count(); ++i) {
  361. if ((entries[i] & 1u) == 0) {
  362. patch_addr = base + entries[i];
  363. f(patch_addr);
  364. patch_addr += sizeof(FlatPtr);
  365. } else {
  366. unsigned j = 0;
  367. for (auto bitmap = entries[i]; (bitmap >>= 1u) != 0; ++j)
  368. if (bitmap & 1u)
  369. f(patch_addr + j * sizeof(FlatPtr));
  370. patch_addr += (8 * sizeof(FlatPtr) - 1) * sizeof(FlatPtr);
  371. }
  372. }
  373. }
  374. template<VoidFunction<DynamicObject::Symbol&> F>
  375. inline void DynamicObject::for_each_symbol(F func) const
  376. {
  377. for (unsigned i = 0; i < symbol_count(); ++i) {
  378. func(symbol(i));
  379. }
  380. }
  381. template<IteratorFunction<DynamicObject::DynamicEntry&> F>
  382. inline void DynamicObject::for_each_dynamic_entry(F func) const
  383. {
  384. auto* dyns = reinterpret_cast<const ElfW(Dyn)*>(m_dynamic_address.as_ptr());
  385. for (unsigned i = 0;; ++i) {
  386. auto&& dyn = DynamicEntry(dyns[i]);
  387. if (dyn.tag() == DT_NULL)
  388. break;
  389. if (func(dyn) == IterationDecision::Break)
  390. break;
  391. }
  392. }
  393. template<VoidFunction<DynamicObject::DynamicEntry&> F>
  394. inline void DynamicObject::for_each_dynamic_entry(F func) const
  395. {
  396. for_each_dynamic_entry([&](auto& dyn) {
  397. func(dyn);
  398. return IterationDecision::Continue;
  399. });
  400. }
  401. template<VoidFunction<StringView> F>
  402. inline void DynamicObject::for_each_needed_library(F func) const
  403. {
  404. for_each_dynamic_entry([func, this](auto entry) {
  405. if (entry.tag() != DT_NEEDED)
  406. return;
  407. ElfW(Word) offset = entry.val();
  408. StringView name { (const char*)(m_base_address.offset(m_string_table_offset).offset(offset)).as_ptr() };
  409. func(name);
  410. });
  411. }
  412. template<VoidFunction<DynamicObject::InitializationFunction&> F>
  413. void DynamicObject::for_each_initialization_array_function(F f) const
  414. {
  415. if (!has_init_array_section())
  416. return;
  417. FlatPtr init_array = (FlatPtr)init_array_section().address().as_ptr();
  418. for (size_t i = 0; i < (m_init_array_size / sizeof(void*)); ++i) {
  419. InitializationFunction current = ((InitializationFunction*)(init_array))[i];
  420. f(current);
  421. }
  422. }
  423. } // end namespace ELF