DynamicObject.h 17 KB

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