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