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