DynamicObject.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. #include <AK/ByteString.h>
  8. #include <AK/Debug.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibELF/DynamicLoader.h>
  11. #include <LibELF/DynamicObject.h>
  12. #include <LibELF/ELFABI.h>
  13. #include <LibELF/Hashes.h>
  14. #include <string.h>
  15. namespace ELF {
  16. DynamicObject::DynamicObject(ByteString const& filepath, VirtualAddress base_address, VirtualAddress dynamic_section_address)
  17. : m_filepath(filepath)
  18. , m_base_address(base_address)
  19. , m_dynamic_address(dynamic_section_address)
  20. {
  21. auto* header = (Elf_Ehdr*)base_address.as_ptr();
  22. auto* const phdrs = program_headers();
  23. // Calculate the base address using the PT_LOAD element with the lowest `p_vaddr` (which is the first element)
  24. for (size_t i = 0; i < program_header_count(); ++i) {
  25. auto pheader = phdrs[i];
  26. if (pheader.p_type == PT_LOAD) {
  27. m_elf_base_address = VirtualAddress { pheader.p_vaddr - pheader.p_offset };
  28. break;
  29. }
  30. if (i == program_header_count() - 1) {
  31. VERIFY_NOT_REACHED();
  32. }
  33. }
  34. if (header->e_type == ET_DYN)
  35. m_is_elf_dynamic = true;
  36. else
  37. m_is_elf_dynamic = false;
  38. parse();
  39. }
  40. DynamicObject::~DynamicObject()
  41. {
  42. // TODO: unmap the object
  43. }
  44. void DynamicObject::dump() const
  45. {
  46. if constexpr (DYNAMIC_LOAD_DEBUG) {
  47. StringBuilder builder;
  48. builder.append("\nd_tag tag_name value\n"sv);
  49. size_t num_dynamic_sections = 0;
  50. for_each_dynamic_entry([&](DynamicObject::DynamicEntry const& entry) {
  51. ByteString name_field = ByteString::formatted("({})", name_for_dtag(entry.tag()));
  52. builder.appendff("{:#08x} {:17} {:#08x}\n", entry.tag(), name_field, entry.val());
  53. num_dynamic_sections++;
  54. });
  55. if (m_has_soname)
  56. builder.appendff("DT_SONAME: {}\n", soname()); // FIXME: Validate that this string is null terminated?
  57. if (m_has_rpath)
  58. builder.appendff("DT_RPATH: {}\n", rpath());
  59. if (m_has_runpath)
  60. builder.appendff("DT_RUNPATH: {}\n", runpath());
  61. dbgln("Dynamic section at address {} contains {} entries:", m_dynamic_address.as_ptr(), num_dynamic_sections);
  62. dbgln("{}", builder.string_view());
  63. }
  64. }
  65. void DynamicObject::parse()
  66. {
  67. for_each_dynamic_entry([&](DynamicEntry const& entry) {
  68. switch (entry.tag()) {
  69. case DT_INIT:
  70. m_init_offset = entry.ptr() - m_elf_base_address.get();
  71. break;
  72. case DT_FINI:
  73. m_fini_offset = entry.ptr() - m_elf_base_address.get();
  74. break;
  75. case DT_INIT_ARRAY:
  76. m_init_array_offset = entry.ptr() - m_elf_base_address.get();
  77. break;
  78. case DT_INIT_ARRAYSZ:
  79. m_init_array_size = entry.val();
  80. break;
  81. case DT_FINI_ARRAY:
  82. m_fini_array_offset = entry.ptr() - m_elf_base_address.get();
  83. break;
  84. case DT_FINI_ARRAYSZ:
  85. m_fini_array_size = entry.val();
  86. break;
  87. case DT_HASH:
  88. // Use SYSV hash only if GNU hash is not available
  89. if (m_hash_type == HashType::SYSV) {
  90. m_hash_table_offset = entry.ptr() - m_elf_base_address.get();
  91. }
  92. break;
  93. case DT_GNU_HASH:
  94. m_hash_type = HashType::GNU;
  95. m_hash_table_offset = entry.ptr() - m_elf_base_address.get();
  96. break;
  97. case DT_SYMTAB:
  98. m_symbol_table_offset = entry.ptr() - m_elf_base_address.get();
  99. break;
  100. case DT_STRTAB:
  101. m_string_table_offset = entry.ptr() - m_elf_base_address.get();
  102. break;
  103. case DT_STRSZ:
  104. m_size_of_string_table = entry.val();
  105. break;
  106. case DT_SYMENT:
  107. m_size_of_symbol_table_entry = entry.val();
  108. break;
  109. case DT_PLTGOT:
  110. m_procedure_linkage_table_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  111. break;
  112. case DT_PLTRELSZ:
  113. m_size_of_plt_relocation_entry_list = entry.val();
  114. break;
  115. case DT_PLTREL:
  116. m_procedure_linkage_table_relocation_type = entry.val();
  117. VERIFY(m_procedure_linkage_table_relocation_type & (DT_REL | DT_RELA));
  118. break;
  119. case DT_JMPREL:
  120. m_plt_relocation_offset_location = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  121. break;
  122. case DT_RELA:
  123. m_addend_used = true;
  124. [[fallthrough]];
  125. case DT_REL:
  126. m_relocation_table_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  127. break;
  128. case DT_RELASZ:
  129. case DT_RELSZ:
  130. m_size_of_relocation_table = entry.val();
  131. break;
  132. case DT_RELAENT:
  133. case DT_RELENT:
  134. m_size_of_relocation_entry = entry.val();
  135. break;
  136. case DT_RELACOUNT:
  137. case DT_RELCOUNT:
  138. m_number_of_relocations = entry.val();
  139. break;
  140. case DT_RELR:
  141. m_relr_relocation_table_offset = entry.ptr() - m_elf_base_address.get();
  142. break;
  143. case DT_RELRSZ:
  144. m_size_of_relr_relocation_table = entry.val();
  145. break;
  146. case DT_RELRENT:
  147. m_size_of_relr_relocations_entry = entry.val();
  148. break;
  149. case DT_FLAGS:
  150. m_dt_flags = entry.val();
  151. break;
  152. case DT_TEXTREL:
  153. m_dt_flags |= DF_TEXTREL; // This tag seems to exist for legacy reasons only?
  154. break;
  155. case DT_SONAME:
  156. m_soname_index = entry.val();
  157. m_has_soname = true;
  158. break;
  159. case DT_BIND_NOW:
  160. m_dt_flags |= DF_BIND_NOW;
  161. break;
  162. case DT_RPATH:
  163. m_rpath_index = entry.val();
  164. m_has_rpath = true;
  165. break;
  166. case DT_RUNPATH:
  167. m_runpath_index = entry.val();
  168. m_has_runpath = true;
  169. break;
  170. case DT_DEBUG:
  171. break;
  172. case DT_FLAGS_1:
  173. m_is_pie = true;
  174. break;
  175. case DT_NEEDED:
  176. // We handle these in for_each_needed_library
  177. break;
  178. case DT_SYMBOLIC:
  179. break;
  180. default:
  181. dbgln("DynamicObject: DYNAMIC tag handling not implemented for DT_{} ({}) in {}", name_for_dtag(entry.tag()), entry.tag(), m_filepath);
  182. break;
  183. }
  184. });
  185. if (!m_size_of_relocation_entry) {
  186. // TODO: FIXME, this shouldn't be hardcoded
  187. // The reason we need this here is that for some reason, when there only PLT relocations, the compiler
  188. // doesn't insert a 'PLTRELSZ' entry to the dynamic section
  189. m_size_of_relocation_entry = sizeof(Elf_Rel);
  190. }
  191. // Whether or not RELASZ (stored in m_size_of_relocation_table) only refers to non-PLT entries is not clearly specified.
  192. // So check if [JMPREL, JMPREL+PLTRELSZ) is in [RELA, RELA+RELASZ).
  193. // If so, change the size of the non-PLT relocation table.
  194. if (m_plt_relocation_offset_location >= m_relocation_table_offset // JMPREL >= RELA
  195. && m_plt_relocation_offset_location < (m_relocation_table_offset + m_size_of_relocation_table)) { // JMPREL < (RELA + RELASZ)
  196. // [JMPREL, JMPREL+PLTRELSZ) is in [RELA, RELA+RELASZ)
  197. // Verify that the ends of the tables match up
  198. VERIFY(m_plt_relocation_offset_location + m_size_of_plt_relocation_entry_list == m_relocation_table_offset + m_size_of_relocation_table);
  199. m_size_of_relocation_table -= m_size_of_plt_relocation_entry_list;
  200. }
  201. u32 const* hash_table_begin = reinterpret_cast<u32 const*>(hash_section().address().as_ptr());
  202. if (m_hash_type == HashType::SYSV) {
  203. u32 n_chain = hash_table_begin[1];
  204. m_symbol_count = n_chain;
  205. return;
  206. }
  207. // Determine amount of symbols by finding the chain with the highest
  208. // starting index and walking this chain until the end to find the
  209. // maximum index = amount of symbols.
  210. using BloomWord = FlatPtr;
  211. size_t const num_buckets = hash_table_begin[0];
  212. size_t const num_omitted_symbols = hash_table_begin[1];
  213. u32 const num_maskwords = hash_table_begin[2];
  214. BloomWord const* bloom_words = reinterpret_cast<BloomWord const*>(&hash_table_begin[4]);
  215. u32 const* const buckets = reinterpret_cast<u32 const*>(&bloom_words[num_maskwords]);
  216. u32 const* const chains = &buckets[num_buckets];
  217. size_t highest_chain_idx = 0;
  218. for (size_t i = 0; i < num_buckets; i++) {
  219. if (buckets[i] > highest_chain_idx) {
  220. highest_chain_idx = buckets[i];
  221. }
  222. }
  223. size_t amount_symbols = highest_chain_idx;
  224. u32 const* last_chain = &chains[highest_chain_idx - num_omitted_symbols];
  225. while ((*(last_chain++) & 1) == 0) {
  226. amount_symbols++;
  227. }
  228. m_symbol_count = amount_symbols + 1;
  229. }
  230. DynamicObject::Relocation DynamicObject::RelocationSection::relocation(unsigned index) const
  231. {
  232. VERIFY(index < entry_count());
  233. unsigned offset_in_section = index * entry_size();
  234. auto relocation_address = (Elf_Rela*)address().offset(offset_in_section).as_ptr();
  235. return Relocation(m_dynamic, *relocation_address, offset_in_section, m_addend_used);
  236. }
  237. DynamicObject::Relocation DynamicObject::RelocationSection::relocation_at_offset(unsigned offset) const
  238. {
  239. VERIFY(offset <= (m_section_size_bytes - m_entry_size));
  240. auto relocation_address = (Elf_Rela*)address().offset(offset).as_ptr();
  241. return Relocation(m_dynamic, *relocation_address, offset, m_addend_used);
  242. }
  243. DynamicObject::Symbol DynamicObject::symbol(unsigned index) const
  244. {
  245. auto symbol_section = Section(*this, m_symbol_table_offset, (m_symbol_count * m_size_of_symbol_table_entry), m_size_of_symbol_table_entry, "DT_SYMTAB"sv);
  246. auto symbol_entry = (Elf_Sym*)symbol_section.address().offset(index * symbol_section.entry_size()).as_ptr();
  247. return Symbol(*this, index, *symbol_entry);
  248. }
  249. DynamicObject::Section DynamicObject::init_section() const
  250. {
  251. return Section(*this, m_init_offset, sizeof(void (*)()), sizeof(void (*)()), "DT_INIT"sv);
  252. }
  253. DynamicObject::Section DynamicObject::fini_section() const
  254. {
  255. return Section(*this, m_fini_offset, sizeof(void (*)()), sizeof(void (*)()), "DT_FINI"sv);
  256. }
  257. DynamicObject::Section DynamicObject::init_array_section() const
  258. {
  259. return Section(*this, m_init_array_offset, m_init_array_size, sizeof(void (*)()), "DT_INIT_ARRAY"sv);
  260. }
  261. DynamicObject::Section DynamicObject::fini_array_section() const
  262. {
  263. return Section(*this, m_fini_array_offset, m_fini_array_size, sizeof(void (*)()), "DT_FINI_ARRAY"sv);
  264. }
  265. DynamicObject::RelocationSection DynamicObject::relocation_section() const
  266. {
  267. return RelocationSection(Section(*this, m_relocation_table_offset, m_size_of_relocation_table, m_size_of_relocation_entry, "DT_REL"sv), m_addend_used);
  268. }
  269. DynamicObject::RelocationSection DynamicObject::plt_relocation_section() const
  270. {
  271. return RelocationSection(Section(*this, m_plt_relocation_offset_location, m_size_of_plt_relocation_entry_list, m_size_of_relocation_entry, "DT_JMPREL"sv), m_procedure_linkage_table_relocation_type & DT_RELA);
  272. }
  273. DynamicObject::Section DynamicObject::relr_relocation_section() const
  274. {
  275. return Section(*this, m_relr_relocation_table_offset, m_size_of_relr_relocation_table, m_size_of_relr_relocations_entry, "DT_RELR"sv);
  276. }
  277. Elf_Half DynamicObject::program_header_count() const
  278. {
  279. auto* header = (Elf_Ehdr const*)m_base_address.as_ptr();
  280. return header->e_phnum;
  281. }
  282. Elf_Phdr const* DynamicObject::program_headers() const
  283. {
  284. auto* header = (Elf_Ehdr const*)m_base_address.as_ptr();
  285. return (Elf_Phdr const*)(m_base_address.as_ptr() + header->e_phoff);
  286. }
  287. auto DynamicObject::HashSection::lookup_sysv_symbol(StringView name, u32 hash_value) const -> Optional<Symbol>
  288. {
  289. u32* hash_table_begin = (u32*)address().as_ptr();
  290. size_t num_buckets = hash_table_begin[0];
  291. // This is here for completeness, but, since we're using the fact that every chain
  292. // will end at chain 0 (which means 'not found'), we don't need to check num_chains.
  293. // Interestingly, num_chains is required to be num_symbols
  294. // size_t num_chains = hash_table_begin[1];
  295. u32* buckets = &hash_table_begin[2];
  296. u32* chains = &buckets[num_buckets];
  297. for (u32 i = buckets[hash_value % num_buckets]; i; i = chains[i]) {
  298. auto symbol = m_dynamic.symbol(i);
  299. if (name == symbol.raw_name()) {
  300. dbgln_if(DYNAMIC_LOAD_DEBUG, "Returning SYSV dynamic symbol with index {} for {}: {}", i, symbol.name(), symbol.address().as_ptr());
  301. return symbol;
  302. }
  303. }
  304. return {};
  305. }
  306. auto DynamicObject::HashSection::lookup_gnu_symbol(StringView name, u32 hash_value) const -> Optional<Symbol>
  307. {
  308. // Algorithm reference: https://ent-voy.blogspot.com/2011/02/
  309. using BloomWord = FlatPtr;
  310. constexpr size_t bloom_word_size = sizeof(BloomWord) * 8;
  311. u32 const* hash_table_begin = (u32*)address().as_ptr();
  312. size_t const num_buckets = hash_table_begin[0];
  313. size_t const num_omitted_symbols = hash_table_begin[1];
  314. u32 const num_maskwords = hash_table_begin[2];
  315. // This works because num_maskwords is required to be a power of 2
  316. u32 const num_maskwords_bitmask = num_maskwords - 1;
  317. u32 const shift2 = hash_table_begin[3];
  318. BloomWord const* bloom_words = (BloomWord const*)&hash_table_begin[4];
  319. u32 const* const buckets = (u32 const*)&bloom_words[num_maskwords];
  320. u32 const* const chains = &buckets[num_buckets];
  321. BloomWord hash1 = hash_value;
  322. BloomWord hash2 = hash1 >> shift2;
  323. BloomWord const bitmask = ((BloomWord)1 << (hash1 % bloom_word_size)) | ((BloomWord)1 << (hash2 % bloom_word_size));
  324. if ((bloom_words[(hash1 / bloom_word_size) & num_maskwords_bitmask] & bitmask) != bitmask)
  325. return {};
  326. size_t current_sym = buckets[hash1 % num_buckets];
  327. if (current_sym == 0)
  328. return {};
  329. u32 const* current_chain = &chains[current_sym - num_omitted_symbols];
  330. for (hash1 &= ~1;; ++current_sym) {
  331. hash2 = *(current_chain++);
  332. if (hash1 == (hash2 & ~1)) {
  333. auto symbol = m_dynamic.symbol(current_sym);
  334. if (name == symbol.raw_name())
  335. return symbol;
  336. }
  337. if (hash2 & 1)
  338. break;
  339. }
  340. return {};
  341. }
  342. StringView DynamicObject::symbol_string_table_string(Elf_Word index) const
  343. {
  344. auto const* symbol_string_table_ptr = reinterpret_cast<char const*>(base_address().offset(m_string_table_offset + index).as_ptr());
  345. return StringView { symbol_string_table_ptr, strlen(symbol_string_table_ptr) };
  346. }
  347. char const* DynamicObject::raw_symbol_string_table_string(Elf_Word index) const
  348. {
  349. return (char const*)base_address().offset(m_string_table_offset + index).as_ptr();
  350. }
  351. DynamicObject::InitializationFunction DynamicObject::init_section_function() const
  352. {
  353. VERIFY(has_init_section());
  354. return (InitializationFunction)init_section().address().as_ptr();
  355. }
  356. DynamicObject::FinalizationFunction DynamicObject::fini_section_function() const
  357. {
  358. VERIFY(has_fini_section());
  359. return (FinalizationFunction)fini_section().address().as_ptr();
  360. }
  361. char const* DynamicObject::name_for_dtag(Elf_Sword d_tag)
  362. {
  363. switch (d_tag) {
  364. case DT_NULL:
  365. return "NULL"; /* marks end of _DYNAMIC array */
  366. case DT_NEEDED:
  367. return "NEEDED"; /* string table offset of needed lib */
  368. case DT_PLTRELSZ:
  369. return "PLTRELSZ"; /* size of relocation entries in PLT */
  370. case DT_PLTGOT:
  371. return "PLTGOT"; /* address PLT/GOT */
  372. case DT_HASH:
  373. return "HASH"; /* address of symbol hash table */
  374. case DT_STRTAB:
  375. return "STRTAB"; /* address of string table */
  376. case DT_SYMTAB:
  377. return "SYMTAB"; /* address of symbol table */
  378. case DT_RELA:
  379. return "RELA"; /* address of relocation table */
  380. case DT_RELASZ:
  381. return "RELASZ"; /* size of relocation table */
  382. case DT_RELAENT:
  383. return "RELAENT"; /* size of relocation entry */
  384. case DT_STRSZ:
  385. return "STRSZ"; /* size of string table */
  386. case DT_SYMENT:
  387. return "SYMENT"; /* size of symbol table entry */
  388. case DT_INIT:
  389. return "INIT"; /* address of initialization func. */
  390. case DT_FINI:
  391. return "FINI"; /* address of termination function */
  392. case DT_SONAME:
  393. return "SONAME"; /* string table offset of shared obj */
  394. case DT_RPATH:
  395. return "RPATH"; /* string table offset of library search path */
  396. case DT_SYMBOLIC:
  397. return "SYMBOLIC"; /* start sym search in shared obj. */
  398. case DT_REL:
  399. return "REL"; /* address of rel. tbl. w addends */
  400. case DT_RELSZ:
  401. return "RELSZ"; /* size of DT_REL relocation table */
  402. case DT_RELENT:
  403. return "RELENT"; /* size of DT_REL relocation entry */
  404. case DT_PLTREL:
  405. return "PLTREL"; /* PLT referenced relocation entry */
  406. case DT_DEBUG:
  407. return "DEBUG"; /* bugger */
  408. case DT_TEXTREL:
  409. return "TEXTREL"; /* Allow rel. mod. to unwritable seg */
  410. case DT_JMPREL:
  411. return "JMPREL"; /* add. of PLT's relocation entries */
  412. case DT_BIND_NOW:
  413. return "BIND_NOW"; /* Bind now regardless of env setting */
  414. case DT_INIT_ARRAY:
  415. return "INIT_ARRAY"; /* address of array of init func */
  416. case DT_FINI_ARRAY:
  417. return "FINI_ARRAY"; /* address of array of term func */
  418. case DT_INIT_ARRAYSZ:
  419. return "INIT_ARRAYSZ"; /* size of array of init func */
  420. case DT_FINI_ARRAYSZ:
  421. return "FINI_ARRAYSZ"; /* size of array of term func */
  422. case DT_RUNPATH:
  423. return "RUNPATH"; /* strtab offset of lib search path */
  424. case DT_FLAGS:
  425. return "FLAGS"; /* Set of DF_* flags */
  426. case DT_ENCODING:
  427. return "ENCODING"; /* further DT_* follow encoding rules */
  428. case DT_PREINIT_ARRAY:
  429. return "PREINIT_ARRAY"; /* address of array of preinit func */
  430. case DT_PREINIT_ARRAYSZ:
  431. return "PREINIT_ARRAYSZ"; /* size of array of preinit func */
  432. case DT_LOOS:
  433. return "LOOS"; /* reserved range for OS */
  434. case DT_HIOS:
  435. return "HIOS"; /* specific dynamic array tags */
  436. case DT_LOPROC:
  437. return "LOPROC"; /* reserved range for processor */
  438. case DT_HIPROC:
  439. return "HIPROC"; /* specific dynamic array tags */
  440. case DT_GNU_HASH:
  441. return "GNU_HASH"; /* address of GNU hash table */
  442. case DT_RELACOUNT:
  443. return "RELACOUNT"; /* if present, number of RELATIVE */
  444. case DT_RELCOUNT:
  445. return "RELCOUNT"; /* relocs, which must come first */
  446. case DT_FLAGS_1:
  447. return "FLAGS_1";
  448. case DT_VERDEF:
  449. return "VERDEF";
  450. case DT_VERDEFNUM:
  451. return "VERDEFNUM";
  452. case DT_VERSYM:
  453. return "VERSYM";
  454. case DT_VERNEEDED:
  455. return "VERNEEDED";
  456. case DT_VERNEEDEDNUM:
  457. return "VERNEEDEDNUM";
  458. case DT_RELR:
  459. return "DT_RELR";
  460. case DT_RELRSZ:
  461. return "DT_RELRSZ";
  462. case DT_RELRENT:
  463. return "DT_RELRENT";
  464. default:
  465. return "??";
  466. }
  467. }
  468. auto DynamicObject::lookup_symbol(StringView name) const -> Optional<SymbolLookupResult>
  469. {
  470. return lookup_symbol(HashSymbol { name });
  471. }
  472. auto DynamicObject::lookup_symbol(HashSymbol const& symbol) const -> Optional<SymbolLookupResult>
  473. {
  474. auto result = hash_section().lookup_symbol(symbol);
  475. if (!result.has_value())
  476. return {};
  477. auto symbol_result = result.value();
  478. if (symbol_result.is_undefined())
  479. return {};
  480. return SymbolLookupResult { symbol_result.value(), symbol_result.size(), symbol_result.address(), symbol_result.bind(), symbol_result.type(), this };
  481. }
  482. NonnullRefPtr<DynamicObject> DynamicObject::create(ByteString const& filepath, VirtualAddress base_address, VirtualAddress dynamic_section_address)
  483. {
  484. return adopt_ref(*new DynamicObject(filepath, base_address, dynamic_section_address));
  485. }
  486. u32 DynamicObject::HashSymbol::gnu_hash() const
  487. {
  488. if (!m_gnu_hash.has_value())
  489. m_gnu_hash = compute_gnu_hash(m_name);
  490. return m_gnu_hash.value();
  491. }
  492. u32 DynamicObject::HashSymbol::sysv_hash() const
  493. {
  494. if (!m_sysv_hash.has_value())
  495. m_sysv_hash = compute_sysv_hash(m_name);
  496. return m_sysv_hash.value();
  497. }
  498. void* DynamicObject::symbol_for_name(StringView name)
  499. {
  500. auto result = hash_section().lookup_symbol(name);
  501. if (!result.has_value())
  502. return nullptr;
  503. auto symbol = result.value();
  504. if (symbol.is_undefined())
  505. return nullptr;
  506. return base_address().offset(symbol.value()).as_ptr();
  507. }
  508. } // end namespace ELF