ELFDynamicObject.cpp 12 KB

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