DynamicObject.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/String.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibELF/DynamicObject.h>
  29. #include <LibELF/exec_elf.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. namespace ELF {
  33. static const char* name_for_dtag(Elf32_Sword d_tag);
  34. DynamicObject::DynamicObject(VirtualAddress base_address, VirtualAddress dynamic_section_addresss)
  35. : m_base_address(base_address)
  36. , m_dynamic_address(dynamic_section_addresss)
  37. {
  38. parse();
  39. }
  40. DynamicObject::~DynamicObject()
  41. {
  42. }
  43. void DynamicObject::dump() const
  44. {
  45. StringBuilder builder;
  46. builder.append("\nd_tag tag_name value\n");
  47. size_t num_dynamic_sections = 0;
  48. for_each_dynamic_entry([&](const DynamicObject::DynamicEntry& entry) {
  49. String name_field = String::format("(%s)", name_for_dtag(entry.tag()));
  50. builder.appendf("0x%08X %-17s0x%X\n", entry.tag(), name_field.characters(), entry.val());
  51. num_dynamic_sections++;
  52. return IterationDecision::Continue;
  53. });
  54. if (m_has_soname)
  55. builder.appendf("DT_SONAME: %s\n", soname()); // FIXME: Valdidate that this string is null terminated?
  56. dbgprintf("Dynamic section at address %p contains %zu entries:\n", m_dynamic_address.as_ptr(), num_dynamic_sections);
  57. dbgprintf("%s", builder.to_string().characters());
  58. }
  59. void DynamicObject::parse()
  60. {
  61. for_each_dynamic_entry([&](const DynamicEntry& entry) {
  62. switch (entry.tag()) {
  63. case DT_INIT:
  64. m_init_offset = entry.ptr();
  65. break;
  66. case DT_FINI:
  67. m_fini_offset = entry.ptr();
  68. break;
  69. case DT_INIT_ARRAY:
  70. m_init_array_offset = entry.ptr();
  71. break;
  72. case DT_INIT_ARRAYSZ:
  73. m_init_array_size = entry.val();
  74. break;
  75. case DT_FINI_ARRAY:
  76. m_fini_array_offset = entry.ptr();
  77. break;
  78. case DT_FINI_ARRAYSZ:
  79. m_fini_array_size = entry.val();
  80. break;
  81. case DT_HASH:
  82. m_hash_table_offset = entry.ptr();
  83. break;
  84. case DT_SYMTAB:
  85. m_symbol_table_offset = entry.ptr();
  86. break;
  87. case DT_STRTAB:
  88. m_string_table_offset = entry.ptr();
  89. break;
  90. case DT_STRSZ:
  91. m_size_of_string_table = entry.val();
  92. break;
  93. case DT_SYMENT:
  94. m_size_of_symbol_table_entry = entry.val();
  95. break;
  96. case DT_PLTGOT:
  97. m_procedure_linkage_table_offset = entry.ptr();
  98. break;
  99. case DT_PLTRELSZ:
  100. m_size_of_plt_relocation_entry_list = entry.val();
  101. break;
  102. case DT_PLTREL:
  103. m_procedure_linkage_table_relocation_type = entry.val();
  104. ASSERT(m_procedure_linkage_table_relocation_type & (DT_REL | DT_RELA));
  105. break;
  106. case DT_JMPREL:
  107. m_plt_relocation_offset_location = entry.ptr();
  108. break;
  109. case DT_RELA:
  110. case DT_REL:
  111. m_relocation_table_offset = entry.ptr();
  112. break;
  113. case DT_RELASZ:
  114. case DT_RELSZ:
  115. m_size_of_relocation_table = entry.val();
  116. break;
  117. case DT_RELAENT:
  118. case DT_RELENT:
  119. m_size_of_relocation_entry = entry.val();
  120. break;
  121. case DT_RELACOUNT:
  122. case DT_RELCOUNT:
  123. m_number_of_relocations = entry.val();
  124. break;
  125. case DT_FLAGS:
  126. m_dt_flags = entry.val();
  127. break;
  128. case DT_TEXTREL:
  129. m_dt_flags |= DF_TEXTREL; // This tag seems to exist for legacy reasons only?
  130. break;
  131. case DT_SONAME:
  132. m_soname_index = entry.val();
  133. m_has_soname = true;
  134. break;
  135. default:
  136. dbgprintf("DynamicObject: DYNAMIC tag handling not implemented for DT_%s\n", name_for_dtag(entry.tag()));
  137. printf("DynamicObject: DYNAMIC tag handling not implemented for DT_%s\n", name_for_dtag(entry.tag()));
  138. ASSERT_NOT_REACHED(); // FIXME: Maybe just break out here and return false?
  139. break;
  140. }
  141. return IterationDecision::Continue;
  142. });
  143. auto hash_section_address = hash_section().address().as_ptr();
  144. auto num_hash_chains = ((u32*)hash_section_address)[1];
  145. m_symbol_count = num_hash_chains;
  146. }
  147. const DynamicObject::Relocation DynamicObject::RelocationSection::relocation(unsigned index) const
  148. {
  149. ASSERT(index < entry_count());
  150. unsigned offset_in_section = index * entry_size();
  151. auto relocation_address = (Elf32_Rel*)address().offset(offset_in_section).as_ptr();
  152. return Relocation(m_dynamic, *relocation_address, offset_in_section);
  153. }
  154. const DynamicObject::Relocation DynamicObject::RelocationSection::relocation_at_offset(unsigned offset) const
  155. {
  156. ASSERT(offset <= (m_section_size_bytes - m_entry_size));
  157. auto relocation_address = (Elf32_Rel*)address().offset(offset).as_ptr();
  158. return Relocation(m_dynamic, *relocation_address, offset);
  159. }
  160. const DynamicObject::Symbol DynamicObject::symbol(unsigned index) const
  161. {
  162. 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");
  163. auto symbol_entry = (Elf32_Sym*)symbol_section.address().offset(index * symbol_section.entry_size()).as_ptr();
  164. return Symbol(*this, index, *symbol_entry);
  165. }
  166. const DynamicObject::Section DynamicObject::init_section() const
  167. {
  168. return Section(*this, m_init_offset, sizeof(void (*)()), sizeof(void (*)()), "DT_INIT");
  169. }
  170. const DynamicObject::Section DynamicObject::fini_section() const
  171. {
  172. return Section(*this, m_fini_offset, sizeof(void (*)()), sizeof(void (*)()), "DT_FINI");
  173. }
  174. const DynamicObject::Section DynamicObject::init_array_section() const
  175. {
  176. return Section(*this, m_init_array_offset, m_init_array_size, sizeof(void (*)()), "DT_INIT_ARRAY");
  177. }
  178. const DynamicObject::Section DynamicObject::fini_array_section() const
  179. {
  180. return Section(*this, m_fini_array_offset, m_fini_array_size, sizeof(void (*)()), "DT_FINI_ARRAY");
  181. }
  182. const DynamicObject::HashSection DynamicObject::hash_section() const
  183. {
  184. return HashSection(Section(*this, m_hash_table_offset, 0, 0, "DT_HASH"), HashType::SYSV);
  185. }
  186. const DynamicObject::RelocationSection DynamicObject::relocation_section() const
  187. {
  188. return RelocationSection(Section(*this, m_relocation_table_offset, m_size_of_relocation_table, m_size_of_relocation_entry, "DT_REL"));
  189. }
  190. const DynamicObject::RelocationSection DynamicObject::plt_relocation_section() const
  191. {
  192. return RelocationSection(Section(*this, m_plt_relocation_offset_location, m_size_of_plt_relocation_entry_list, m_size_of_relocation_entry, "DT_JMPREL"));
  193. }
  194. u32 DynamicObject::HashSection::calculate_elf_hash(const char* name) const
  195. {
  196. // SYSV ELF hash algorithm
  197. // Note that the GNU HASH algorithm has less collisions
  198. uint32_t hash = 0;
  199. uint32_t top_nibble_of_hash = 0;
  200. while (*name != '\0') {
  201. hash = hash << 4;
  202. hash += *name;
  203. name++;
  204. top_nibble_of_hash = hash & 0xF0000000U;
  205. if (top_nibble_of_hash != 0)
  206. hash ^= top_nibble_of_hash >> 24;
  207. hash &= ~top_nibble_of_hash;
  208. }
  209. return hash;
  210. }
  211. u32 DynamicObject::HashSection::calculate_gnu_hash(const char*) const
  212. {
  213. // FIXME: Implement the GNU hash algorithm
  214. ASSERT_NOT_REACHED();
  215. }
  216. const DynamicObject::Symbol DynamicObject::HashSection::lookup_symbol(const char* name) const
  217. {
  218. // FIXME: If we enable gnu hash in the compiler, we should use that here instead
  219. // The algo is way better with less collisions
  220. u32 hash_value = (this->*(m_hash_function))(name);
  221. u32* hash_table_begin = (u32*)address().as_ptr();
  222. size_t num_buckets = hash_table_begin[0];
  223. // This is here for completeness, but, since we're using the fact that every chain
  224. // will end at chain 0 (which means 'not found'), we don't need to check num_chains.
  225. // Interestingly, num_chains is required to be num_symbols
  226. //size_t num_chains = hash_table_begin[1];
  227. u32* buckets = &hash_table_begin[2];
  228. u32* chains = &buckets[num_buckets];
  229. for (u32 i = buckets[hash_value % num_buckets]; i; i = chains[i]) {
  230. auto symbol = m_dynamic.symbol(i);
  231. if (strcmp(name, symbol.name()) == 0) {
  232. #ifdef DYNAMIC_LOAD_DEBUG
  233. dbgprintf("Returning dynamic symbol with index %u for %s: %p\n", i, symbol.name(), symbol.address().as_ptr());
  234. #endif
  235. return symbol;
  236. }
  237. }
  238. return m_dynamic.the_undefined_symbol();
  239. }
  240. const char* DynamicObject::symbol_string_table_string(Elf32_Word index) const
  241. {
  242. return (const char*)base_address().offset(m_string_table_offset + index).as_ptr();
  243. }
  244. static const char* name_for_dtag(Elf32_Sword d_tag)
  245. {
  246. switch (d_tag) {
  247. case DT_NULL:
  248. return "NULL"; /* marks end of _DYNAMIC array */
  249. case DT_NEEDED:
  250. return "NEEDED"; /* string table offset of needed lib */
  251. case DT_PLTRELSZ:
  252. return "PLTRELSZ"; /* size of relocation entries in PLT */
  253. case DT_PLTGOT:
  254. return "PLTGOT"; /* address PLT/GOT */
  255. case DT_HASH:
  256. return "HASH"; /* address of symbol hash table */
  257. case DT_STRTAB:
  258. return "STRTAB"; /* address of string table */
  259. case DT_SYMTAB:
  260. return "SYMTAB"; /* address of symbol table */
  261. case DT_RELA:
  262. return "RELA"; /* address of relocation table */
  263. case DT_RELASZ:
  264. return "RELASZ"; /* size of relocation table */
  265. case DT_RELAENT:
  266. return "RELAENT"; /* size of relocation entry */
  267. case DT_STRSZ:
  268. return "STRSZ"; /* size of string table */
  269. case DT_SYMENT:
  270. return "SYMENT"; /* size of symbol table entry */
  271. case DT_INIT:
  272. return "INIT"; /* address of initialization func. */
  273. case DT_FINI:
  274. return "FINI"; /* address of termination function */
  275. case DT_SONAME:
  276. return "SONAME"; /* string table offset of shared obj */
  277. case DT_RPATH:
  278. return "RPATH"; /* string table offset of library search path */
  279. case DT_SYMBOLIC:
  280. return "SYMBOLIC"; /* start sym search in shared obj. */
  281. case DT_REL:
  282. return "REL"; /* address of rel. tbl. w addends */
  283. case DT_RELSZ:
  284. return "RELSZ"; /* size of DT_REL relocation table */
  285. case DT_RELENT:
  286. return "RELENT"; /* size of DT_REL relocation entry */
  287. case DT_PLTREL:
  288. return "PLTREL"; /* PLT referenced relocation entry */
  289. case DT_DEBUG:
  290. return "DEBUG"; /* bugger */
  291. case DT_TEXTREL:
  292. return "TEXTREL"; /* Allow rel. mod. to unwritable seg */
  293. case DT_JMPREL:
  294. return "JMPREL"; /* add. of PLT's relocation entries */
  295. case DT_BIND_NOW:
  296. return "BIND_NOW"; /* Bind now regardless of env setting */
  297. case DT_INIT_ARRAY:
  298. return "INIT_ARRAY"; /* address of array of init func */
  299. case DT_FINI_ARRAY:
  300. return "FINI_ARRAY"; /* address of array of term func */
  301. case DT_INIT_ARRAYSZ:
  302. return "INIT_ARRAYSZ"; /* size of array of init func */
  303. case DT_FINI_ARRAYSZ:
  304. return "FINI_ARRAYSZ"; /* size of array of term func */
  305. case DT_RUNPATH:
  306. return "RUNPATH"; /* strtab offset of lib search path */
  307. case DT_FLAGS:
  308. return "FLAGS"; /* Set of DF_* flags */
  309. case DT_ENCODING:
  310. return "ENCODING"; /* further DT_* follow encoding rules */
  311. case DT_PREINIT_ARRAY:
  312. return "PREINIT_ARRAY"; /* address of array of preinit func */
  313. case DT_PREINIT_ARRAYSZ:
  314. return "PREINIT_ARRAYSZ"; /* size of array of preinit func */
  315. case DT_LOOS:
  316. return "LOOS"; /* reserved range for OS */
  317. case DT_HIOS:
  318. return "HIOS"; /* specific dynamic array tags */
  319. case DT_LOPROC:
  320. return "LOPROC"; /* reserved range for processor */
  321. case DT_HIPROC:
  322. return "HIPROC"; /* specific dynamic array tags */
  323. case DT_GNU_HASH:
  324. return "GNU_HASH"; /* address of GNU hash table */
  325. case DT_RELACOUNT:
  326. return "RELACOUNT"; /* if present, number of RELATIVE */
  327. case DT_RELCOUNT:
  328. return "RELCOUNT"; /* relocs, which must come first */
  329. case DT_FLAGS_1:
  330. return "FLAGS_1";
  331. default:
  332. return "??";
  333. }
  334. }
  335. } // end namespace ELF