1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Debug.h>
#include <AK/Demangle.h>
#include <AK/Memory.h>
#include <AK/QuickSort.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <LibELF/Image.h>
#include <LibELF/Validation.h>
namespace ELF {
Image::Image(ReadonlyBytes bytes, bool verbose_logging)
: m_buffer(bytes.data())
, m_size(bytes.size())
, m_verbose_logging(verbose_logging)
{
parse();
}
Image::Image(const u8* buffer, size_t size, bool verbose_logging)
: Image(ReadonlyBytes { buffer, size }, verbose_logging)
{
}
Image::~Image()
{
}
#if ELF_IMAGE_DEBUG
static const char* object_file_type_to_string(Elf32_Half type)
{
switch (type) {
case ET_NONE:
return "None";
case ET_REL:
return "Relocatable";
case ET_EXEC:
return "Executable";
case ET_DYN:
return "Shared object";
case ET_CORE:
return "Core";
default:
return "(?)";
}
}
#endif
StringView Image::section_index_to_string(unsigned index) const
{
ASSERT(m_valid);
if (index == SHN_UNDEF)
return "Undefined";
if (index >= SHN_LORESERVE)
return "Reserved";
return section(index).name();
}
unsigned Image::symbol_count() const
{
ASSERT(m_valid);
if (!section_count())
return 0;
return section(m_symbol_table_section_index).entry_count();
}
void Image::dump() const
{
#if ELF_IMAGE_DEBUG
dbgln("ELF::Image({:p}) {{", this);
dbgln(" is_valid: {}", is_valid());
if (!is_valid()) {
dbgln("}}");
return;
}
dbgln(" type: {}", object_file_type_to_string(header().e_type));
dbgln(" machine: {}", header().e_machine);
dbgln(" entry: {:x}", header().e_entry);
dbgln(" shoff: {}", header().e_shoff);
dbgln(" shnum: {}", header().e_shnum);
dbgln(" phoff: {}", header().e_phoff);
dbgln(" phnum: {}", header().e_phnum);
dbgln(" shstrndx: {}", header().e_shstrndx);
for_each_program_header([&](const ProgramHeader& program_header) {
dbgln(" Program Header {}: {{", program_header.index());
dbgln(" type: {:x}", program_header.type());
dbgln(" offset: {:x}", program_header.offset());
dbgln(" flags: {:x}", program_header.flags());
dbgln(" }}");
return IterationDecision::Continue;
});
for (unsigned i = 0; i < header().e_shnum; ++i) {
auto& section = this->section(i);
dbgln(" Section {}: {{", i);
dbgln(" name: {}", section.name());
dbgln(" type: {:x}", section.type());
dbgln(" offset: {:x}", section.offset());
dbgln(" size: {}", section.size());
dbgln(" ");
dbgln(" }}");
}
dbgln("Symbol count: {} (table is {})", symbol_count(), m_symbol_table_section_index);
for (unsigned i = 1; i < symbol_count(); ++i) {
auto& sym = symbol(i);
dbgln("Symbol @{}:", i);
dbgln(" Name: {}", sym.name());
dbgln(" In section: {}", section_index_to_string(sym.section_index()));
dbgln(" Value: {}", sym.value());
dbgln(" Size: {}", sym.size());
}
dbgln("}}");
#endif
}
unsigned Image::section_count() const
{
ASSERT(m_valid);
return header().e_shnum;
}
unsigned Image::program_header_count() const
{
ASSERT(m_valid);
return header().e_phnum;
}
bool Image::parse()
{
if (m_size < sizeof(Elf32_Ehdr) || !validate_elf_header(header(), m_size, m_verbose_logging)) {
if (m_verbose_logging)
dbgln("ELF::Image::parse(): ELF Header not valid");
return m_valid = false;
}
if (!validate_program_headers(header(), m_size, m_buffer, m_size, nullptr, m_verbose_logging)) {
if (m_verbose_logging)
dbgln("ELF::Image::parse(): ELF Program Headers not valid");
return m_valid = false;
}
m_valid = true;
// First locate the string tables.
for (unsigned i = 0; i < section_count(); ++i) {
auto& sh = section_header(i);
if (sh.sh_type == SHT_SYMTAB) {
if (m_symbol_table_section_index && m_symbol_table_section_index != i)
return m_valid = false;
m_symbol_table_section_index = i;
}
if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
if (section_header_table_string(sh.sh_name) == ELF_STRTAB)
m_string_table_section_index = i;
}
}
// Then create a name-to-index map.
for (unsigned i = 0; i < section_count(); ++i) {
auto section = this->section(i);
m_sections.set(section.name(), move(i));
}
return m_valid;
}
StringView Image::table_string(unsigned table_index, unsigned offset) const
{
ASSERT(m_valid);
auto& sh = section_header(table_index);
if (sh.sh_type != SHT_STRTAB)
return nullptr;
size_t computed_offset = sh.sh_offset + offset;
if (computed_offset >= m_size) {
if (m_verbose_logging)
dbgln("SHENANIGANS! Image::table_string() computed offset outside image.");
return {};
}
size_t max_length = min(m_size - computed_offset, (size_t)PAGE_SIZE);
size_t length = strnlen(raw_data(sh.sh_offset + offset), max_length);
return { raw_data(sh.sh_offset + offset), length };
}
StringView Image::section_header_table_string(unsigned offset) const
{
ASSERT(m_valid);
return table_string(header().e_shstrndx, offset);
}
StringView Image::table_string(unsigned offset) const
{
ASSERT(m_valid);
return table_string(m_string_table_section_index, offset);
}
const char* Image::raw_data(unsigned offset) const
{
ASSERT(offset < m_size); // Callers must check indices into raw_data()'s result are also in bounds.
return reinterpret_cast<const char*>(m_buffer) + offset;
}
const Elf32_Ehdr& Image::header() const
{
ASSERT(m_size >= sizeof(Elf32_Ehdr));
return *reinterpret_cast<const Elf32_Ehdr*>(raw_data(0));
}
const Elf32_Phdr& Image::program_header_internal(unsigned index) const
{
ASSERT(m_valid);
ASSERT(index < header().e_phnum);
return *reinterpret_cast<const Elf32_Phdr*>(raw_data(header().e_phoff + (index * sizeof(Elf32_Phdr))));
}
const Elf32_Shdr& Image::section_header(unsigned index) const
{
ASSERT(m_valid);
ASSERT(index < header().e_shnum);
return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * header().e_shentsize)));
}
Image::Symbol Image::symbol(unsigned index) const
{
ASSERT(m_valid);
ASSERT(index < symbol_count());
auto* raw_syms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_symbol_table_section_index).offset()));
return Symbol(*this, index, raw_syms[index]);
}
Image::Section Image::section(unsigned index) const
{
ASSERT(m_valid);
ASSERT(index < section_count());
return Section(*this, index);
}
Image::ProgramHeader Image::program_header(unsigned index) const
{
ASSERT(m_valid);
ASSERT(index < program_header_count());
return ProgramHeader(*this, index);
}
Image::Relocation Image::RelocationSection::relocation(unsigned index) const
{
ASSERT(index < relocation_count());
auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
return Relocation(m_image, rels[index]);
}
Image::RelocationSection Image::Section::relocations() const
{
StringBuilder builder;
builder.append(".rel");
builder.append(name());
auto relocation_section = m_image.lookup_section(builder.to_string());
if (relocation_section.type() != SHT_REL)
return static_cast<const RelocationSection>(m_image.section(0));
#if ELF_IMAGE_DEBUG
dbgln("Found relocations for {} in {}", name(), relocation_section.name());
#endif
return static_cast<const RelocationSection>(relocation_section);
}
Image::Section Image::lookup_section(const String& name) const
{
ASSERT(m_valid);
if (auto it = m_sections.find(name); it != m_sections.end())
return section((*it).value);
return section(0);
}
StringView Image::Symbol::raw_data() const
{
auto section = this->section();
return { section.raw_data() + (value() - section.address()), size() };
}
Optional<Image::Symbol> Image::find_demangled_function(const String& name) const
{
Optional<Image::Symbol> found;
for_each_symbol([&](const Image::Symbol& symbol) {
if (symbol.type() != STT_FUNC)
return IterationDecision::Continue;
if (symbol.is_undefined())
return IterationDecision::Continue;
auto demangled = demangle(symbol.name());
auto index_of_paren = demangled.index_of("(");
if (index_of_paren.has_value()) {
demangled = demangled.substring(0, index_of_paren.value());
}
if (demangled != name)
return IterationDecision::Continue;
found = symbol;
return IterationDecision::Break;
});
return found;
}
Optional<Image::Symbol> Image::find_symbol(u32 address, u32* out_offset) const
{
auto symbol_count = this->symbol_count();
if (!symbol_count)
return {};
SortedSymbol* sorted_symbols = nullptr;
if (m_sorted_symbols.is_empty()) {
m_sorted_symbols.ensure_capacity(symbol_count);
for_each_symbol([this](const auto& symbol) {
m_sorted_symbols.append({ symbol.value(), symbol.name(), {}, symbol });
return IterationDecision::Continue;
});
quick_sort(m_sorted_symbols, [](auto& a, auto& b) {
return a.address < b.address;
});
}
sorted_symbols = m_sorted_symbols.data();
for (size_t i = 0; i < symbol_count; ++i) {
if (sorted_symbols[i].address > address) {
if (i == 0)
return {};
auto& symbol = sorted_symbols[i - 1];
if (out_offset)
*out_offset = address - symbol.address;
return symbol.symbol;
}
}
return {};
}
String Image::symbolicate(u32 address, u32* out_offset) const
{
auto symbol_count = this->symbol_count();
if (!symbol_count) {
if (out_offset)
*out_offset = 0;
return "??";
}
SortedSymbol* sorted_symbols = nullptr;
if (m_sorted_symbols.is_empty()) {
m_sorted_symbols.ensure_capacity(symbol_count);
for_each_symbol([this](const auto& symbol) {
m_sorted_symbols.append({ symbol.value(), symbol.name(), {}, symbol });
return IterationDecision::Continue;
});
quick_sort(m_sorted_symbols, [](auto& a, auto& b) {
return a.address < b.address;
});
}
sorted_symbols = m_sorted_symbols.data();
for (size_t i = 0; i < symbol_count; ++i) {
if (sorted_symbols[i].address > address) {
if (i == 0) {
if (out_offset)
*out_offset = 0;
return "!!";
}
auto& symbol = sorted_symbols[i - 1];
auto& demangled_name = symbol.demangled_name;
if (demangled_name.is_null()) {
demangled_name = demangle(symbol.name);
}
if (out_offset) {
*out_offset = address - symbol.address;
return demangled_name;
}
return String::format("%s +0x%x", demangled_name.characters(), address - symbol.address);
}
}
if (out_offset)
*out_offset = 0;
return "??";
}
} // end namespace ELF
|