DynamicObject.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. ~DynamicObject();
  20. void dump() const;
  21. class DynamicEntry;
  22. class Section;
  23. class RelocationSection;
  24. class Symbol;
  25. class Relocation;
  26. class HashSection;
  27. class DynamicEntry {
  28. public:
  29. explicit DynamicEntry(const ElfW(Dyn) & dyn)
  30. : m_dyn(dyn)
  31. {
  32. }
  33. ~DynamicEntry() { }
  34. ElfW(Sword) tag() const { return m_dyn.d_tag; }
  35. ElfW(Addr) ptr() const { return m_dyn.d_un.d_ptr; }
  36. ElfW(Word) val() const { return m_dyn.d_un.d_val; }
  37. private:
  38. const ElfW(Dyn) & m_dyn;
  39. };
  40. class Symbol {
  41. public:
  42. Symbol(const DynamicObject& dynamic, unsigned index, const ElfW(Sym) & sym)
  43. : m_dynamic(dynamic)
  44. , m_sym(sym)
  45. , m_index(index)
  46. {
  47. }
  48. StringView name() const { return m_dynamic.symbol_string_table_string(m_sym.st_name); }
  49. const char* raw_name() const { return m_dynamic.raw_symbol_string_table_string(m_sym.st_name); }
  50. unsigned section_index() const { return m_sym.st_shndx; }
  51. unsigned value() const { return m_sym.st_value; }
  52. unsigned size() const { return m_sym.st_size; }
  53. unsigned index() const { return m_index; }
  54. #if ARCH(I386)
  55. unsigned type() const
  56. {
  57. return ELF32_ST_TYPE(m_sym.st_info);
  58. }
  59. unsigned bind() const { return ELF32_ST_BIND(m_sym.st_info); }
  60. #else
  61. unsigned type() const
  62. {
  63. return ELF64_ST_TYPE(m_sym.st_info);
  64. }
  65. unsigned bind() const { return ELF64_ST_BIND(m_sym.st_info); }
  66. #endif
  67. bool is_undefined() const
  68. {
  69. return section_index() == 0;
  70. }
  71. VirtualAddress address() const
  72. {
  73. if (m_dynamic.elf_is_dynamic())
  74. return m_dynamic.base_address().offset(value());
  75. return VirtualAddress { value() };
  76. }
  77. const DynamicObject& object() const { return m_dynamic; }
  78. private:
  79. const DynamicObject& m_dynamic;
  80. const ElfW(Sym) & m_sym;
  81. const unsigned m_index;
  82. };
  83. class Section {
  84. public:
  85. Section(const DynamicObject& dynamic, unsigned section_offset, unsigned section_size_bytes, unsigned entry_size, const 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() { }
  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. const DynamicObject& 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(const Section& 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. const bool m_addend_used;
  131. };
  132. class Relocation {
  133. public:
  134. Relocation(const DynamicObject& dynamic, const ElfW(Rela) & 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() { }
  142. unsigned offset_in_section() const { return m_offset_in_section; }
  143. unsigned offset() const { return m_rel.r_offset; }
  144. #if ARCH(I386)
  145. unsigned type() const
  146. {
  147. return ELF32_R_TYPE(m_rel.r_info);
  148. }
  149. unsigned symbol_index() const { return ELF32_R_SYM(m_rel.r_info); }
  150. #else
  151. unsigned type() const
  152. {
  153. return ELF64_R_TYPE(m_rel.r_info);
  154. }
  155. unsigned symbol_index() const { return ELF64_R_SYM(m_rel.r_info); }
  156. #endif
  157. unsigned addend() const
  158. {
  159. VERIFY(m_addend_used);
  160. return m_rel.r_addend;
  161. }
  162. bool addend_used() const { return m_addend_used; }
  163. Symbol symbol() const
  164. {
  165. return m_dynamic.symbol(symbol_index());
  166. }
  167. VirtualAddress address() const
  168. {
  169. if (m_dynamic.elf_is_dynamic())
  170. return m_dynamic.base_address().offset(offset());
  171. return VirtualAddress { offset() };
  172. }
  173. private:
  174. const DynamicObject& m_dynamic;
  175. const ElfW(Rela) & m_rel;
  176. const unsigned m_offset_in_section;
  177. const bool m_addend_used;
  178. };
  179. enum class HashType {
  180. SYSV,
  181. GNU
  182. };
  183. class HashSymbol {
  184. public:
  185. HashSymbol(const StringView& name)
  186. : m_name(name)
  187. {
  188. }
  189. StringView name() const { return m_name; }
  190. u32 gnu_hash() const;
  191. u32 sysv_hash() const;
  192. private:
  193. StringView m_name;
  194. mutable Optional<u32> m_gnu_hash;
  195. mutable Optional<u32> m_sysv_hash;
  196. };
  197. class HashSection : public Section {
  198. public:
  199. HashSection(const Section& section, HashType hash_type)
  200. : Section(section.m_dynamic, section.m_section_offset, section.m_section_size_bytes, section.m_entry_size, section.m_name)
  201. , m_hash_type(hash_type)
  202. {
  203. }
  204. Optional<Symbol> lookup_symbol(const HashSymbol& symbol) const
  205. {
  206. if (m_hash_type == HashType::SYSV)
  207. return lookup_sysv_symbol(symbol.name(), symbol.sysv_hash());
  208. return lookup_gnu_symbol(symbol.name(), symbol.gnu_hash());
  209. }
  210. private:
  211. Optional<Symbol> lookup_sysv_symbol(const StringView& name, u32 hash_value) const;
  212. Optional<Symbol> lookup_gnu_symbol(const StringView& name, u32 hash) const;
  213. HashType m_hash_type {};
  214. };
  215. unsigned symbol_count() const { return m_symbol_count; }
  216. Symbol symbol(unsigned) const;
  217. typedef void (*InitializationFunction)();
  218. bool has_init_section() const { return m_init_offset != 0; }
  219. bool has_init_array_section() const { return m_init_array_offset != 0; }
  220. Section init_section() const;
  221. InitializationFunction init_section_function() const;
  222. Section fini_section() const;
  223. Section init_array_section() const;
  224. Section fini_array_section() const;
  225. HashSection hash_section() const
  226. {
  227. auto section_name = m_hash_type == HashType::SYSV ? "DT_HASH"sv : "DT_GNU_HASH"sv;
  228. return HashSection(Section(*this, m_hash_table_offset, 0, 0, section_name), m_hash_type);
  229. }
  230. RelocationSection relocation_section() const;
  231. RelocationSection plt_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. const String& filename() const { return m_filename; }
  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. ElfW(Half) program_header_count() const;
  250. const ElfW(Phdr) * 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. struct SymbolLookupResult {
  262. FlatPtr value { 0 };
  263. size_t size { 0 };
  264. VirtualAddress address;
  265. unsigned bind { STB_LOCAL };
  266. const ELF::DynamicObject* dynamic_object { nullptr }; // The object in which the symbol is defined
  267. };
  268. Optional<SymbolLookupResult> lookup_symbol(const StringView& name) const;
  269. Optional<SymbolLookupResult> lookup_symbol(const HashSymbol& symbol) const;
  270. // Will be called from _fixup_plt_entry, as part of the PLT trampoline
  271. VirtualAddress patch_plt_entry(u32 relocation_offset);
  272. bool elf_is_dynamic() const { return m_is_elf_dynamic; }
  273. void* symbol_for_name(const StringView& name);
  274. private:
  275. explicit DynamicObject(const String& filename, VirtualAddress base_address, VirtualAddress dynamic_section_address);
  276. StringView symbol_string_table_string(ElfW(Word)) const;
  277. const char* raw_symbol_string_table_string(ElfW(Word)) const;
  278. void parse();
  279. String m_filename;
  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. ElfW(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. bool m_is_elf_dynamic { false };
  309. // DT_FLAGS
  310. ElfW(Word) m_dt_flags { 0 };
  311. bool m_has_soname { false };
  312. ElfW(Word) m_soname_index { 0 }; // Index into dynstr table for SONAME
  313. bool m_has_rpath { false };
  314. ElfW(Word) m_rpath_index { 0 }; // Index into dynstr table for RPATH
  315. bool m_has_runpath { false };
  316. ElfW(Word) m_runpath_index { 0 }; // Index into dynstr table for RUNPATH
  317. Optional<FlatPtr> m_tls_offset;
  318. Optional<FlatPtr> m_tls_size;
  319. // End Section information from DT_* entries
  320. };
  321. template<IteratorFunction<DynamicObject::Relocation&> F>
  322. inline void DynamicObject::RelocationSection::for_each_relocation(F func) const
  323. {
  324. for (unsigned i = 0; i < relocation_count(); ++i) {
  325. const auto reloc = relocation(i);
  326. if (reloc.type() == 0)
  327. continue;
  328. if (func(reloc) == IterationDecision::Break)
  329. break;
  330. }
  331. }
  332. template<VoidFunction<DynamicObject::Relocation&> F>
  333. inline void DynamicObject::RelocationSection::for_each_relocation(F func) const
  334. {
  335. for_each_relocation([&](auto& reloc) {
  336. func(reloc);
  337. return IterationDecision::Continue;
  338. });
  339. }
  340. template<VoidFunction<DynamicObject::Symbol&> F>
  341. inline void DynamicObject::for_each_symbol(F func) const
  342. {
  343. for (unsigned i = 0; i < symbol_count(); ++i) {
  344. func(symbol(i));
  345. }
  346. }
  347. template<IteratorFunction<DynamicObject::DynamicEntry&> F>
  348. inline void DynamicObject::for_each_dynamic_entry(F func) const
  349. {
  350. auto* dyns = reinterpret_cast<const ElfW(Dyn)*>(m_dynamic_address.as_ptr());
  351. for (unsigned i = 0;; ++i) {
  352. auto&& dyn = DynamicEntry(dyns[i]);
  353. if (dyn.tag() == DT_NULL)
  354. break;
  355. if (func(dyn) == IterationDecision::Break)
  356. break;
  357. }
  358. }
  359. template<VoidFunction<DynamicObject::DynamicEntry&> F>
  360. inline void DynamicObject::for_each_dynamic_entry(F func) const
  361. {
  362. for_each_dynamic_entry([&](auto& dyn) {
  363. func(dyn);
  364. return IterationDecision::Continue;
  365. });
  366. }
  367. template<VoidFunction<StringView> F>
  368. inline void DynamicObject::for_each_needed_library(F func) const
  369. {
  370. for_each_dynamic_entry([func, this](auto entry) {
  371. if (entry.tag() != DT_NEEDED)
  372. return;
  373. ElfW(Word) offset = entry.val();
  374. StringView name { (const char*)(m_base_address.offset(m_string_table_offset).offset(offset)).as_ptr() };
  375. func(name);
  376. });
  377. }
  378. template<VoidFunction<DynamicObject::InitializationFunction&> F>
  379. void DynamicObject::for_each_initialization_array_function(F f) const
  380. {
  381. if (!has_init_array_section())
  382. return;
  383. FlatPtr init_array = (FlatPtr)init_array_section().address().as_ptr();
  384. for (size_t i = 0; i < (m_init_array_size / sizeof(void*)); ++i) {
  385. InitializationFunction current = ((InitializationFunction*)(init_array))[i];
  386. f(current);
  387. }
  388. }
  389. } // end namespace ELF