DynamicObject.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
  3. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Debug.h>
  28. #include <AK/String.h>
  29. #include <AK/StringBuilder.h>
  30. #include <LibC/elf.h>
  31. #include <LibELF/DynamicLoader.h>
  32. #include <LibELF/DynamicObject.h>
  33. #include <LibELF/Hashes.h>
  34. #include <string.h>
  35. namespace ELF {
  36. static const char* name_for_dtag(Elf32_Sword d_tag);
  37. DynamicObject::DynamicObject(const String& filename, VirtualAddress base_address, VirtualAddress dynamic_section_address)
  38. : m_filename(filename)
  39. , m_base_address(base_address)
  40. , m_dynamic_address(dynamic_section_address)
  41. {
  42. auto* header = (Elf32_Ehdr*)base_address.as_ptr();
  43. auto* pheader = (Elf32_Phdr*)(base_address.as_ptr() + header->e_phoff);
  44. m_elf_base_address = VirtualAddress(pheader->p_vaddr - pheader->p_offset);
  45. if (header->e_type == ET_DYN)
  46. m_is_elf_dynamic = true;
  47. else
  48. m_is_elf_dynamic = false;
  49. parse();
  50. }
  51. DynamicObject::~DynamicObject()
  52. {
  53. }
  54. void DynamicObject::dump() const
  55. {
  56. StringBuilder builder;
  57. builder.append("\nd_tag tag_name value\n");
  58. size_t num_dynamic_sections = 0;
  59. for_each_dynamic_entry([&](const DynamicObject::DynamicEntry& entry) {
  60. String name_field = String::formatted("({})", name_for_dtag(entry.tag()));
  61. builder.appendf("0x%08X %-17s0x%X\n", entry.tag(), name_field.characters(), entry.val());
  62. num_dynamic_sections++;
  63. return IterationDecision::Continue;
  64. });
  65. if (m_has_soname)
  66. builder.appendff("DT_SONAME: {}\n", soname()); // FIXME: Validate that this string is null terminated?
  67. if (m_has_rpath)
  68. builder.appendff("DT_RPATH: {}\n", rpath());
  69. if (m_has_runpath)
  70. builder.appendff("DT_RUNPATH: {}\n", runpath());
  71. dbgln_if(DYNAMIC_LOAD_DEBUG, "Dynamic section at address {} contains {} entries:", m_dynamic_address.as_ptr(), num_dynamic_sections);
  72. dbgln_if(DYNAMIC_LOAD_DEBUG, "{}", builder.string_view());
  73. }
  74. void DynamicObject::parse()
  75. {
  76. for_each_dynamic_entry([&](const DynamicEntry& entry) {
  77. switch (entry.tag()) {
  78. case DT_INIT:
  79. m_init_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  80. break;
  81. case DT_FINI:
  82. m_fini_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  83. break;
  84. case DT_INIT_ARRAY:
  85. m_init_array_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  86. break;
  87. case DT_INIT_ARRAYSZ:
  88. m_init_array_size = entry.val();
  89. break;
  90. case DT_FINI_ARRAY:
  91. m_fini_array_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  92. break;
  93. case DT_FINI_ARRAYSZ:
  94. m_fini_array_size = entry.val();
  95. break;
  96. case DT_HASH:
  97. // Use SYSV hash only if GNU hash is not available
  98. if (m_hash_type == HashType::SYSV) {
  99. m_hash_table_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  100. }
  101. break;
  102. case DT_GNU_HASH:
  103. m_hash_type = HashType::GNU;
  104. m_hash_table_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  105. break;
  106. case DT_SYMTAB:
  107. m_symbol_table_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  108. break;
  109. case DT_STRTAB:
  110. m_string_table_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  111. break;
  112. case DT_STRSZ:
  113. m_size_of_string_table = entry.val();
  114. break;
  115. case DT_SYMENT:
  116. m_size_of_symbol_table_entry = entry.val();
  117. break;
  118. case DT_PLTGOT:
  119. m_procedure_linkage_table_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  120. break;
  121. case DT_PLTRELSZ:
  122. m_size_of_plt_relocation_entry_list = entry.val();
  123. break;
  124. case DT_PLTREL:
  125. m_procedure_linkage_table_relocation_type = entry.val();
  126. VERIFY(m_procedure_linkage_table_relocation_type & (DT_REL | DT_RELA));
  127. break;
  128. case DT_JMPREL:
  129. m_plt_relocation_offset_location = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  130. break;
  131. case DT_RELA:
  132. case DT_REL:
  133. m_relocation_table_offset = entry.ptr() - (FlatPtr)m_elf_base_address.as_ptr();
  134. break;
  135. case DT_RELASZ:
  136. case DT_RELSZ:
  137. m_size_of_relocation_table = entry.val();
  138. break;
  139. case DT_RELAENT:
  140. case DT_RELENT:
  141. m_size_of_relocation_entry = entry.val();
  142. break;
  143. case DT_RELACOUNT:
  144. case DT_RELCOUNT:
  145. m_number_of_relocations = entry.val();
  146. break;
  147. case DT_FLAGS:
  148. m_dt_flags = entry.val();
  149. break;
  150. case DT_TEXTREL:
  151. m_dt_flags |= DF_TEXTREL; // This tag seems to exist for legacy reasons only?
  152. break;
  153. case DT_SONAME:
  154. m_soname_index = entry.val();
  155. m_has_soname = true;
  156. break;
  157. case DT_BIND_NOW:
  158. m_dt_flags |= DF_BIND_NOW;
  159. break;
  160. case DT_RPATH:
  161. m_rpath_index = entry.val();
  162. m_has_rpath = true;
  163. break;
  164. case DT_RUNPATH:
  165. m_runpath_index = entry.val();
  166. m_has_runpath = true;
  167. break;
  168. case DT_DEBUG:
  169. break;
  170. case DT_FLAGS_1:
  171. break;
  172. case DT_NEEDED:
  173. // We handle these in for_each_needed_library
  174. break;
  175. default:
  176. dbgln("DynamicObject: DYNAMIC tag handling not implemented for DT_{}", name_for_dtag(entry.tag()));
  177. VERIFY_NOT_REACHED(); // FIXME: Maybe just break out here and return false?
  178. break;
  179. }
  180. return IterationDecision::Continue;
  181. });
  182. if (!m_size_of_relocation_entry) {
  183. // TODO: FIXME, this shouldn't be hardcoded
  184. // The reason we need this here is that for some reason, when there only PLT relocations, the compiler
  185. // doesn't insert a 'PLTRELSZ' entry to the dynamic section
  186. m_size_of_relocation_entry = sizeof(Elf32_Rel);
  187. }
  188. auto hash_section_address = hash_section().address().as_ptr();
  189. // TODO: consider base address - it might not be zero
  190. auto num_hash_chains = ((u32*)hash_section_address)[1];
  191. m_symbol_count = num_hash_chains;
  192. }
  193. DynamicObject::Relocation DynamicObject::RelocationSection::relocation(unsigned index) const
  194. {
  195. VERIFY(index < entry_count());
  196. unsigned offset_in_section = index * entry_size();
  197. auto relocation_address = (Elf32_Rel*)address().offset(offset_in_section).as_ptr();
  198. return Relocation(m_dynamic, *relocation_address, offset_in_section);
  199. }
  200. DynamicObject::Relocation DynamicObject::RelocationSection::relocation_at_offset(unsigned offset) const
  201. {
  202. VERIFY(offset <= (m_section_size_bytes - m_entry_size));
  203. auto relocation_address = (Elf32_Rel*)address().offset(offset).as_ptr();
  204. return Relocation(m_dynamic, *relocation_address, offset);
  205. }
  206. DynamicObject::Symbol DynamicObject::symbol(unsigned index) const
  207. {
  208. 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");
  209. auto symbol_entry = (Elf32_Sym*)symbol_section.address().offset(index * symbol_section.entry_size()).as_ptr();
  210. return Symbol(*this, index, *symbol_entry);
  211. }
  212. DynamicObject::Section DynamicObject::init_section() const
  213. {
  214. return Section(*this, m_init_offset, sizeof(void (*)()), sizeof(void (*)()), "DT_INIT"sv);
  215. }
  216. DynamicObject::Section DynamicObject::fini_section() const
  217. {
  218. return Section(*this, m_fini_offset, sizeof(void (*)()), sizeof(void (*)()), "DT_FINI"sv);
  219. }
  220. DynamicObject::Section DynamicObject::init_array_section() const
  221. {
  222. return Section(*this, m_init_array_offset, m_init_array_size, sizeof(void (*)()), "DT_INIT_ARRAY"sv);
  223. }
  224. DynamicObject::Section DynamicObject::fini_array_section() const
  225. {
  226. return Section(*this, m_fini_array_offset, m_fini_array_size, sizeof(void (*)()), "DT_FINI_ARRAY"sv);
  227. }
  228. DynamicObject::RelocationSection DynamicObject::relocation_section() const
  229. {
  230. return RelocationSection(Section(*this, m_relocation_table_offset, m_size_of_relocation_table, m_size_of_relocation_entry, "DT_REL"sv));
  231. }
  232. DynamicObject::RelocationSection DynamicObject::plt_relocation_section() const
  233. {
  234. return RelocationSection(Section(*this, m_plt_relocation_offset_location, m_size_of_plt_relocation_entry_list, m_size_of_relocation_entry, "DT_JMPREL"sv));
  235. }
  236. Elf32_Half DynamicObject::program_header_count() const
  237. {
  238. auto* header = (const Elf32_Ehdr*)m_base_address.as_ptr();
  239. return header->e_phnum;
  240. }
  241. const Elf32_Phdr* DynamicObject::program_headers() const
  242. {
  243. auto* header = (const Elf32_Ehdr*)m_base_address.as_ptr();
  244. return (const Elf32_Phdr*)(m_base_address.as_ptr() + header->e_phoff);
  245. }
  246. auto DynamicObject::HashSection::lookup_sysv_symbol(const StringView& name, u32 hash_value) const -> Optional<Symbol>
  247. {
  248. u32* hash_table_begin = (u32*)address().as_ptr();
  249. size_t num_buckets = hash_table_begin[0];
  250. // This is here for completeness, but, since we're using the fact that every chain
  251. // will end at chain 0 (which means 'not found'), we don't need to check num_chains.
  252. // Interestingly, num_chains is required to be num_symbols
  253. // size_t num_chains = hash_table_begin[1];
  254. u32* buckets = &hash_table_begin[2];
  255. u32* chains = &buckets[num_buckets];
  256. for (u32 i = buckets[hash_value % num_buckets]; i; i = chains[i]) {
  257. auto symbol = m_dynamic.symbol(i);
  258. if (name == symbol.name()) {
  259. dbgln_if(DYNAMIC_LOAD_DEBUG, "Returning SYSV dynamic symbol with index {} for {}: {}", i, symbol.name(), symbol.address().as_ptr());
  260. return symbol;
  261. }
  262. }
  263. return {};
  264. }
  265. auto DynamicObject::HashSection::lookup_gnu_symbol(const StringView& name, u32 hash_value) const -> Optional<Symbol>
  266. {
  267. // Algorithm reference: https://ent-voy.blogspot.com/2011/02/
  268. // TODO: Handle 64bit bloomwords for ELF_CLASS64
  269. using BloomWord = u32;
  270. constexpr size_t bloom_word_size = sizeof(BloomWord) * 8;
  271. const u32* hash_table_begin = (u32*)address().as_ptr();
  272. const size_t num_buckets = hash_table_begin[0];
  273. const size_t num_omitted_symbols = hash_table_begin[1];
  274. const u32 num_maskwords = hash_table_begin[2];
  275. // This works because num_maskwords is required to be a power of 2
  276. const u32 num_maskwords_bitmask = num_maskwords - 1;
  277. const u32 shift2 = hash_table_begin[3];
  278. const BloomWord* bloom_words = &hash_table_begin[4];
  279. const u32* const buckets = &bloom_words[num_maskwords];
  280. const u32* const chains = &buckets[num_buckets];
  281. BloomWord hash1 = hash_value;
  282. BloomWord hash2 = hash1 >> shift2;
  283. const BloomWord bitmask = (1 << (hash1 % bloom_word_size)) | (1 << (hash2 % bloom_word_size));
  284. if ((bloom_words[(hash1 / bloom_word_size) & num_maskwords_bitmask] & bitmask) != bitmask)
  285. return {};
  286. size_t current_sym = buckets[hash1 % num_buckets];
  287. if (current_sym == 0)
  288. return {};
  289. const u32* current_chain = &chains[current_sym - num_omitted_symbols];
  290. for (hash1 &= ~1;; ++current_sym) {
  291. hash2 = *(current_chain++);
  292. auto symbol = m_dynamic.symbol(current_sym);
  293. if ((hash1 == (hash2 & ~1)) && name == symbol.raw_name())
  294. return symbol;
  295. if (hash2 & 1)
  296. break;
  297. }
  298. return {};
  299. }
  300. StringView DynamicObject::symbol_string_table_string(Elf32_Word index) const
  301. {
  302. return StringView { (const char*)base_address().offset(m_string_table_offset + index).as_ptr() };
  303. }
  304. const char* DynamicObject::raw_symbol_string_table_string(Elf32_Word index) const
  305. {
  306. return (const char*)base_address().offset(m_string_table_offset + index).as_ptr();
  307. }
  308. DynamicObject::InitializationFunction DynamicObject::init_section_function() const
  309. {
  310. VERIFY(has_init_section());
  311. return (InitializationFunction)init_section().address().as_ptr();
  312. }
  313. static const char* name_for_dtag(Elf32_Sword d_tag)
  314. {
  315. switch (d_tag) {
  316. case DT_NULL:
  317. return "NULL"; /* marks end of _DYNAMIC array */
  318. case DT_NEEDED:
  319. return "NEEDED"; /* string table offset of needed lib */
  320. case DT_PLTRELSZ:
  321. return "PLTRELSZ"; /* size of relocation entries in PLT */
  322. case DT_PLTGOT:
  323. return "PLTGOT"; /* address PLT/GOT */
  324. case DT_HASH:
  325. return "HASH"; /* address of symbol hash table */
  326. case DT_STRTAB:
  327. return "STRTAB"; /* address of string table */
  328. case DT_SYMTAB:
  329. return "SYMTAB"; /* address of symbol table */
  330. case DT_RELA:
  331. return "RELA"; /* address of relocation table */
  332. case DT_RELASZ:
  333. return "RELASZ"; /* size of relocation table */
  334. case DT_RELAENT:
  335. return "RELAENT"; /* size of relocation entry */
  336. case DT_STRSZ:
  337. return "STRSZ"; /* size of string table */
  338. case DT_SYMENT:
  339. return "SYMENT"; /* size of symbol table entry */
  340. case DT_INIT:
  341. return "INIT"; /* address of initialization func. */
  342. case DT_FINI:
  343. return "FINI"; /* address of termination function */
  344. case DT_SONAME:
  345. return "SONAME"; /* string table offset of shared obj */
  346. case DT_RPATH:
  347. return "RPATH"; /* string table offset of library search path */
  348. case DT_SYMBOLIC:
  349. return "SYMBOLIC"; /* start sym search in shared obj. */
  350. case DT_REL:
  351. return "REL"; /* address of rel. tbl. w addends */
  352. case DT_RELSZ:
  353. return "RELSZ"; /* size of DT_REL relocation table */
  354. case DT_RELENT:
  355. return "RELENT"; /* size of DT_REL relocation entry */
  356. case DT_PLTREL:
  357. return "PLTREL"; /* PLT referenced relocation entry */
  358. case DT_DEBUG:
  359. return "DEBUG"; /* bugger */
  360. case DT_TEXTREL:
  361. return "TEXTREL"; /* Allow rel. mod. to unwritable seg */
  362. case DT_JMPREL:
  363. return "JMPREL"; /* add. of PLT's relocation entries */
  364. case DT_BIND_NOW:
  365. return "BIND_NOW"; /* Bind now regardless of env setting */
  366. case DT_INIT_ARRAY:
  367. return "INIT_ARRAY"; /* address of array of init func */
  368. case DT_FINI_ARRAY:
  369. return "FINI_ARRAY"; /* address of array of term func */
  370. case DT_INIT_ARRAYSZ:
  371. return "INIT_ARRAYSZ"; /* size of array of init func */
  372. case DT_FINI_ARRAYSZ:
  373. return "FINI_ARRAYSZ"; /* size of array of term func */
  374. case DT_RUNPATH:
  375. return "RUNPATH"; /* strtab offset of lib search path */
  376. case DT_FLAGS:
  377. return "FLAGS"; /* Set of DF_* flags */
  378. case DT_ENCODING:
  379. return "ENCODING"; /* further DT_* follow encoding rules */
  380. case DT_PREINIT_ARRAY:
  381. return "PREINIT_ARRAY"; /* address of array of preinit func */
  382. case DT_PREINIT_ARRAYSZ:
  383. return "PREINIT_ARRAYSZ"; /* size of array of preinit func */
  384. case DT_LOOS:
  385. return "LOOS"; /* reserved range for OS */
  386. case DT_HIOS:
  387. return "HIOS"; /* specific dynamic array tags */
  388. case DT_LOPROC:
  389. return "LOPROC"; /* reserved range for processor */
  390. case DT_HIPROC:
  391. return "HIPROC"; /* specific dynamic array tags */
  392. case DT_GNU_HASH:
  393. return "GNU_HASH"; /* address of GNU hash table */
  394. case DT_RELACOUNT:
  395. return "RELACOUNT"; /* if present, number of RELATIVE */
  396. case DT_RELCOUNT:
  397. return "RELCOUNT"; /* relocs, which must come first */
  398. case DT_FLAGS_1:
  399. return "FLAGS_1";
  400. default:
  401. return "??";
  402. }
  403. }
  404. auto DynamicObject::lookup_symbol(const StringView& name) const -> Optional<SymbolLookupResult>
  405. {
  406. return lookup_symbol(name, compute_gnu_hash(name), compute_sysv_hash(name));
  407. }
  408. auto DynamicObject::lookup_symbol(const StringView& name, u32 gnu_hash, u32 sysv_hash) const -> Optional<SymbolLookupResult>
  409. {
  410. auto result = hash_section().lookup_symbol(name, gnu_hash, sysv_hash);
  411. if (!result.has_value())
  412. return {};
  413. auto symbol = result.value();
  414. if (symbol.is_undefined())
  415. return {};
  416. return SymbolLookupResult { symbol.value(), symbol.address(), symbol.bind(), this };
  417. }
  418. NonnullRefPtr<DynamicObject> DynamicObject::create(const String& filename, VirtualAddress base_address, VirtualAddress dynamic_section_address)
  419. {
  420. return adopt(*new DynamicObject(filename, base_address, dynamic_section_address));
  421. }
  422. // offset is in PLT relocation table
  423. VirtualAddress DynamicObject::patch_plt_entry(u32 relocation_offset)
  424. {
  425. auto relocation = plt_relocation_section().relocation_at_offset(relocation_offset);
  426. VERIFY(relocation.type() == R_386_JMP_SLOT);
  427. auto symbol = relocation.symbol();
  428. u8* relocation_address = relocation.address().as_ptr();
  429. auto result = DynamicLoader::lookup_symbol(symbol);
  430. if (!result.has_value()) {
  431. dbgln("did not find symbol: {}", symbol.name());
  432. VERIFY_NOT_REACHED();
  433. }
  434. auto symbol_location = result.value().address;
  435. dbgln_if(DYNAMIC_LOAD_DEBUG, "DynamicLoader: Jump slot relocation: putting {} ({}) into PLT at {}", symbol.name(), symbol_location, (void*)relocation_address);
  436. *(FlatPtr*)relocation_address = symbol_location.get();
  437. return symbol_location;
  438. }
  439. } // end namespace ELF