DynamicObject.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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/Arch/GenericDynamicRelocationType.h>
  15. #include <LibELF/ELFABI.h>
  16. #include <link.h>
  17. namespace ELF {
  18. class DynamicObject : public RefCounted<DynamicObject> {
  19. public:
  20. static NonnullRefPtr<DynamicObject> create(ByteString const& filepath, VirtualAddress base_address, VirtualAddress dynamic_section_address);
  21. static char const* name_for_dtag(Elf_Sword d_tag);
  22. ~DynamicObject();
  23. void dump() const;
  24. class DynamicEntry;
  25. class Section;
  26. class RelocationSection;
  27. class Symbol;
  28. class Relocation;
  29. class HashSection;
  30. class DynamicEntry {
  31. public:
  32. explicit DynamicEntry(Elf_Dyn const& dyn)
  33. : m_dyn(dyn)
  34. {
  35. }
  36. ~DynamicEntry() = default;
  37. Elf_Sword tag() const { return m_dyn.d_tag; }
  38. Elf_Addr ptr() const { return m_dyn.d_un.d_ptr; }
  39. Elf_Word val() const { return m_dyn.d_un.d_val; }
  40. private:
  41. Elf_Dyn const& m_dyn;
  42. };
  43. class Symbol {
  44. public:
  45. Symbol(DynamicObject const& dynamic, unsigned index, Elf_Sym const& sym)
  46. : m_dynamic(dynamic)
  47. , m_sym(sym)
  48. , m_index(index)
  49. {
  50. }
  51. StringView name() const { return m_dynamic.symbol_string_table_string(m_sym.st_name); }
  52. char const* raw_name() const { return m_dynamic.raw_symbol_string_table_string(m_sym.st_name); }
  53. unsigned section_index() const { return m_sym.st_shndx; }
  54. FlatPtr value() const { return m_sym.st_value; }
  55. size_t size() const { return m_sym.st_size; }
  56. unsigned index() const { return m_index; }
  57. unsigned type() const
  58. {
  59. return ELF64_ST_TYPE(m_sym.st_info);
  60. }
  61. unsigned bind() const { return ELF64_ST_BIND(m_sym.st_info); }
  62. bool is_undefined() const
  63. {
  64. return section_index() == 0;
  65. }
  66. VirtualAddress address() const
  67. {
  68. if (m_dynamic.elf_is_dynamic())
  69. return m_dynamic.base_address().offset(value());
  70. return VirtualAddress { value() };
  71. }
  72. DynamicObject const& object() const { return m_dynamic; }
  73. // This might return false even if the two Symbol objects resolve to the same thing.
  74. bool definitely_equals(Symbol const& other) const
  75. {
  76. return &m_dynamic == &other.m_dynamic && &m_sym == &other.m_sym && m_index == other.m_index;
  77. }
  78. private:
  79. DynamicObject const& m_dynamic;
  80. Elf_Sym const& m_sym;
  81. unsigned const m_index;
  82. };
  83. class Section {
  84. public:
  85. Section(DynamicObject const& dynamic, unsigned section_offset, unsigned section_size_bytes, unsigned entry_size, 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() = default;
  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. DynamicObject const& 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(Section const& 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. bool const m_addend_used;
  131. };
  132. class Relocation {
  133. public:
  134. Relocation(DynamicObject const& dynamic, Elf_Rela const& 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() = default;
  142. unsigned offset_in_section() const { return m_offset_in_section; }
  143. unsigned offset() const { return m_rel.r_offset; }
  144. unsigned type() const
  145. {
  146. return ELF64_R_TYPE(m_rel.r_info);
  147. }
  148. unsigned symbol_index() const { return ELF64_R_SYM(m_rel.r_info); }
  149. unsigned addend() const
  150. {
  151. VERIFY(m_addend_used);
  152. return m_rel.r_addend;
  153. }
  154. bool addend_used() const { return m_addend_used; }
  155. Symbol symbol() const
  156. {
  157. return m_dynamic.symbol(symbol_index());
  158. }
  159. VirtualAddress address() const
  160. {
  161. if (m_dynamic.elf_is_dynamic())
  162. return m_dynamic.base_address().offset(offset());
  163. return VirtualAddress { offset() };
  164. }
  165. [[nodiscard]] DynamicObject const& dynamic_object() const { return m_dynamic; }
  166. private:
  167. DynamicObject const& m_dynamic;
  168. Elf_Rela const& m_rel;
  169. unsigned const m_offset_in_section;
  170. bool const m_addend_used;
  171. };
  172. enum class HashType {
  173. SYSV,
  174. GNU
  175. };
  176. class HashSymbol {
  177. public:
  178. HashSymbol(StringView name)
  179. : m_name(name)
  180. {
  181. }
  182. StringView name() const { return m_name; }
  183. u32 gnu_hash() const;
  184. u32 sysv_hash() const;
  185. private:
  186. StringView m_name;
  187. mutable Optional<u32> m_gnu_hash;
  188. mutable Optional<u32> m_sysv_hash;
  189. };
  190. class HashSection : public Section {
  191. public:
  192. HashSection(Section const& section, HashType hash_type)
  193. : Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
  194. , m_hash_type(hash_type)
  195. {
  196. }
  197. Optional<Symbol> lookup_symbol(HashSymbol const& symbol) const
  198. {
  199. if (m_hash_type == HashType::SYSV)
  200. return lookup_sysv_symbol(symbol.name(), symbol.sysv_hash());
  201. return lookup_gnu_symbol(symbol.name(), symbol.gnu_hash());
  202. }
  203. private:
  204. Optional<Symbol> lookup_sysv_symbol(StringView name, u32 hash_value) const;
  205. Optional<Symbol> lookup_gnu_symbol(StringView name, u32 hash) const;
  206. HashType m_hash_type {};
  207. };
  208. unsigned symbol_count() const { return m_symbol_count; }
  209. Symbol symbol(unsigned) const;
  210. typedef void (*InitializationFunction)();
  211. typedef void (*FinalizationFunction)();
  212. typedef Elf_Addr (*IfuncResolver)();
  213. bool has_init_section() const { return m_init_offset != 0; }
  214. bool has_init_array_section() const { return m_init_array_offset != 0; }
  215. Section init_section() const;
  216. InitializationFunction init_section_function() const;
  217. Section init_array_section() const;
  218. bool is_pie() const { return m_is_pie; }
  219. bool has_fini_section() const { return m_fini_offset != 0; }
  220. bool has_fini_array_section() const { return m_fini_array_offset != 0; }
  221. Section fini_section() const;
  222. FinalizationFunction fini_section_function() const;
  223. Section fini_array_section() const;
  224. HashSection hash_section() const
  225. {
  226. auto section_name = m_hash_type == HashType::SYSV ? "DT_HASH"sv : "DT_GNU_HASH"sv;
  227. return HashSection(Section(*this, m_hash_table_offset, 0, 0, section_name), m_hash_type);
  228. }
  229. RelocationSection relocation_section() const;
  230. RelocationSection plt_relocation_section() const;
  231. Section relr_relocation_section() const;
  232. bool should_process_origin() const { return m_dt_flags & DF_ORIGIN; }
  233. bool requires_symbolic_symbol_resolution() const { return m_dt_flags & DF_SYMBOLIC; }
  234. // Text relocations meaning: we need to edit the .text section which is normally mapped PROT_READ
  235. bool has_text_relocations() const { return m_dt_flags & DF_TEXTREL; }
  236. bool must_bind_now() const { return m_dt_flags & DF_BIND_NOW; }
  237. bool has_static_thread_local_storage() const { return m_dt_flags & DF_STATIC_TLS; }
  238. bool has_plt() const { return m_procedure_linkage_table_offset.has_value(); }
  239. VirtualAddress plt_got_base_address() const { return m_base_address.offset(m_procedure_linkage_table_offset.value()); }
  240. VirtualAddress base_address() const { return m_base_address; }
  241. ByteString const& filepath() const { return m_filepath; }
  242. StringView rpath() const { return m_has_rpath ? symbol_string_table_string(m_rpath_index) : StringView {}; }
  243. StringView runpath() const { return m_has_runpath ? symbol_string_table_string(m_runpath_index) : StringView {}; }
  244. StringView soname() const { return m_has_soname ? symbol_string_table_string(m_soname_index) : StringView {}; }
  245. Optional<FlatPtr> tls_offset() const { return m_tls_offset; }
  246. Optional<FlatPtr> tls_size() const { return m_tls_size; }
  247. void set_tls_offset(FlatPtr offset) { m_tls_offset = offset; }
  248. void set_tls_size(FlatPtr size) { m_tls_size = size; }
  249. Elf_Half program_header_count() const;
  250. Elf_Phdr const* program_headers() const;
  251. template<VoidFunction<StringView> F>
  252. void for_each_needed_library(F) const;
  253. template<VoidFunction<InitializationFunction&> F>
  254. void for_each_initialization_array_function(F f) const;
  255. template<IteratorFunction<DynamicEntry&> F>
  256. void for_each_dynamic_entry(F) const;
  257. template<VoidFunction<DynamicEntry&> F>
  258. void for_each_dynamic_entry(F func) const;
  259. template<VoidFunction<Symbol&> F>
  260. void for_each_symbol(F) const;
  261. template<typename F>
  262. void for_each_relr_relocation(F) const;
  263. struct SymbolLookupResult {
  264. FlatPtr value { 0 };
  265. size_t size { 0 };
  266. VirtualAddress address;
  267. unsigned bind { STB_LOCAL };
  268. unsigned type { STT_FUNC };
  269. const ELF::DynamicObject* dynamic_object { nullptr }; // The object in which the symbol is defined
  270. };
  271. Optional<SymbolLookupResult> lookup_symbol(StringView name) const;
  272. Optional<SymbolLookupResult> lookup_symbol(HashSymbol const& symbol) const;
  273. bool elf_is_dynamic() const { return m_is_elf_dynamic; }
  274. void* symbol_for_name(StringView name);
  275. private:
  276. explicit DynamicObject(ByteString const& filepath, VirtualAddress base_address, VirtualAddress dynamic_section_address);
  277. StringView symbol_string_table_string(Elf_Word) const;
  278. char const* raw_symbol_string_table_string(Elf_Word) const;
  279. void parse();
  280. ByteString m_filepath;
  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. Elf_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. size_t m_size_of_relr_relocations_entry { 0 };
  310. size_t m_size_of_relr_relocation_table { 0 };
  311. FlatPtr m_relr_relocation_table_offset { 0 };
  312. bool m_is_elf_dynamic { false };
  313. bool m_is_pie { false };
  314. // DT_FLAGS
  315. Elf_Word m_dt_flags { 0 };
  316. bool m_has_soname { false };
  317. Elf_Word m_soname_index { 0 }; // Index into dynstr table for SONAME
  318. bool m_has_rpath { false };
  319. Elf_Word m_rpath_index { 0 }; // Index into dynstr table for RPATH
  320. bool m_has_runpath { false };
  321. Elf_Word m_runpath_index { 0 }; // Index into dynstr table for RUNPATH
  322. Optional<FlatPtr> m_tls_offset;
  323. Optional<FlatPtr> m_tls_size;
  324. // End Section information from DT_* entries
  325. };
  326. template<IteratorFunction<DynamicObject::Relocation&> F>
  327. inline void DynamicObject::RelocationSection::for_each_relocation(F func) const
  328. {
  329. for (unsigned i = 0; i < relocation_count(); ++i) {
  330. auto const reloc = relocation(i);
  331. if (static_cast<GenericDynamicRelocationType>(reloc.type()) == GenericDynamicRelocationType::NONE)
  332. continue;
  333. if (func(reloc) == IterationDecision::Break)
  334. break;
  335. }
  336. }
  337. template<VoidFunction<DynamicObject::Relocation&> F>
  338. inline void DynamicObject::RelocationSection::for_each_relocation(F func) const
  339. {
  340. for_each_relocation([&](auto& reloc) {
  341. func(reloc);
  342. return IterationDecision::Continue;
  343. });
  344. }
  345. template<typename F>
  346. inline void DynamicObject::for_each_relr_relocation(F f) const
  347. {
  348. auto section = relr_relocation_section();
  349. if (section.entry_count() == 0)
  350. return;
  351. VERIFY(section.entry_size() == sizeof(FlatPtr));
  352. VERIFY(section.size() >= section.entry_size() * section.entry_count());
  353. auto* entries = reinterpret_cast<Elf_Relr*>(section.address().get());
  354. auto base = base_address().get();
  355. FlatPtr patch_addr = 0;
  356. for (unsigned i = 0; i < section.entry_count(); ++i) {
  357. if ((entries[i] & 1u) == 0) {
  358. patch_addr = base + entries[i];
  359. f(patch_addr);
  360. patch_addr += sizeof(FlatPtr);
  361. } else {
  362. unsigned j = 0;
  363. for (auto bitmap = entries[i]; (bitmap >>= 1u) != 0; ++j)
  364. if (bitmap & 1u)
  365. f(patch_addr + j * sizeof(FlatPtr));
  366. patch_addr += (8 * sizeof(FlatPtr) - 1) * sizeof(FlatPtr);
  367. }
  368. }
  369. }
  370. template<VoidFunction<DynamicObject::Symbol&> F>
  371. inline void DynamicObject::for_each_symbol(F func) const
  372. {
  373. for (unsigned i = 0; i < symbol_count(); ++i) {
  374. func(symbol(i));
  375. }
  376. }
  377. template<IteratorFunction<DynamicObject::DynamicEntry&> F>
  378. inline void DynamicObject::for_each_dynamic_entry(F func) const
  379. {
  380. auto* dyns = reinterpret_cast<Elf_Dyn const*>(m_dynamic_address.as_ptr());
  381. for (unsigned i = 0;; ++i) {
  382. auto&& dyn = DynamicEntry(dyns[i]);
  383. if (dyn.tag() == DT_NULL)
  384. break;
  385. if (func(dyn) == IterationDecision::Break)
  386. break;
  387. }
  388. }
  389. template<VoidFunction<DynamicObject::DynamicEntry&> F>
  390. inline void DynamicObject::for_each_dynamic_entry(F func) const
  391. {
  392. for_each_dynamic_entry([&](auto& dyn) {
  393. func(dyn);
  394. return IterationDecision::Continue;
  395. });
  396. }
  397. template<VoidFunction<StringView> F>
  398. inline void DynamicObject::for_each_needed_library(F func) const
  399. {
  400. for_each_dynamic_entry([func, this](auto entry) {
  401. if (entry.tag() != DT_NEEDED)
  402. return;
  403. Elf_Word offset = entry.val();
  404. func(symbol_string_table_string(offset));
  405. });
  406. }
  407. template<VoidFunction<DynamicObject::InitializationFunction&> F>
  408. void DynamicObject::for_each_initialization_array_function(F f) const
  409. {
  410. if (!has_init_array_section())
  411. return;
  412. FlatPtr init_array = (FlatPtr)init_array_section().address().as_ptr();
  413. for (size_t i = 0; i < (m_init_array_size / sizeof(void*)); ++i) {
  414. InitializationFunction current = ((InitializationFunction*)(init_array))[i];
  415. f(current);
  416. }
  417. }
  418. } // end namespace ELF