DynamicObject.h 15 KB

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