summaryrefslogtreecommitdiff
path: root/Userland/Utilities/readelf.cpp
blob: b75119b49b381e8d69a0812e40726c2d614b83f7 (plain)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
 * Copyright (c) 2020, the SerenityOS developers.
 * 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/MappedFile.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <LibCore/ArgsParser.h>
#include <LibELF/Image.h>
#include <LibELF/Validation.h>
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>

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 "(?)";
    }
}

static const char* object_machine_type_to_string(Elf32_Half type)
{
    switch (type) {
    case ET_NONE:
        return "None";
    case EM_M32:
        return "AT&T WE 32100";
    case EM_SPARC:
        return "SPARC";
    case EM_386:
        return "Intel 80386";
    case EM_68K:
        return "Motorola 68000";
    case EM_88K:
        return "Motorola 88000";
    case EM_486:
        return "Intel 80486";
    case EM_860:
        return "Intel 80860";
    case EM_MIPS:
        return "MIPS R3000 Big-Endian only";
    default:
        return "(?)";
    }
}

static const char* object_program_header_type_to_string(Elf32_Word type)
{
    switch (type) {
    case PT_NULL:
        return "NULL";
    case PT_LOAD:
        return "LOAD";
    case PT_DYNAMIC:
        return "DYNAMIC";
    case PT_INTERP:
        return "INTERP";
    case PT_NOTE:
        return "NOTE";
    case PT_SHLIB:
        return "SHLIB";
    case PT_PHDR:
        return "PHDR";
    case PT_TLS:
        return "TLS";
    case PT_LOOS:
        return "LOOS";
    case PT_HIOS:
        return "HIOS";
    case PT_LOPROC:
        return "LOPROC";
    case PT_HIPROC:
        return "HIPROC";
    case PT_GNU_EH_FRAME:
        return "GNU_EH_FRAME";
    case PT_GNU_RELRO:
        return "GNU_RELRO";
    case PT_GNU_STACK:
        return "GNU_STACK";
    case PT_OPENBSD_RANDOMIZE:
        return "OPENBSD_RANDOMIZE";
    case PT_OPENBSD_WXNEEDED:
        return "OPENBSD_WXNEEDED";
    case PT_OPENBSD_BOOTDATA:
        return "OPENBSD_BOOTDATA";
    default:
        return "(?)";
    }
}

static const char* object_section_header_type_to_string(Elf32_Word type)
{
    switch (type) {
    case SHT_NULL:
        return "NULL";
    case SHT_PROGBITS:
        return "PROGBITS";
    case SHT_SYMTAB:
        return "SYMTAB";
    case SHT_STRTAB:
        return "STRTAB";
    case SHT_RELA:
        return "RELA";
    case SHT_HASH:
        return "HASH";
    case SHT_DYNAMIC:
        return "DYNAMIC";
    case SHT_NOTE:
        return "NOTE";
    case SHT_NOBITS:
        return "NOBITS";
    case SHT_REL:
        return "REL";
    case SHT_SHLIB:
        return "SHLIB";
    case SHT_DYNSYM:
        return "DYNSYM";
    case SHT_NUM:
        return "NUM";
    case SHT_INIT_ARRAY:
        return "INIT_ARRAY";
    case SHT_FINI_ARRAY:
        return "FINI_ARRAY";
    case SHT_PREINIT_ARRAY:
        return "PREINIT_ARRAY";
    case SHT_GROUP:
        return "GROUP";
    case SHT_SYMTAB_SHNDX:
        return "SYMTAB_SHNDX";
    case SHT_LOOS:
        return "SOOS";
    case SHT_SUNW_dof:
        return "SUNW_dof";
    case SHT_GNU_LIBLIST:
        return "GNU_LIBLIST";
    case SHT_SUNW_move:
        return "SUNW_move";
    case SHT_SUNW_syminfo:
        return "SUNW_syminfo";
    case SHT_SUNW_verdef:
        return "SUNW_verdef";
    case SHT_SUNW_verneed:
        return "SUNW_verneed";
    case SHT_SUNW_versym: // or SHT_HIOS
        return "SUNW_versym";
    case SHT_LOPROC:
        return "LOPROC";
    case SHT_HIPROC:
        return "HIPROC";
    case SHT_LOUSER:
        return "LOUSER";
    case SHT_HIUSER:
        return "HIUSER";
    case SHT_GNU_HASH:
        return "GNU_HASH";
    default:
        return "(?)";
    }
}

static const char* object_symbol_type_to_string(Elf32_Word type)
{
    switch (type) {
    case STT_NOTYPE:
        return "NOTYPE";
    case STT_OBJECT:
        return "OBJECT";
    case STT_FUNC:
        return "FUNC";
    case STT_SECTION:
        return "SECTION";
    case STT_FILE:
        return "FILE";
    case STT_TLS:
        return "TLS";
    case STT_LOPROC:
        return "LOPROC";
    case STT_HIPROC:
        return "HIPROC";
    default:
        return "(?)";
    }
}

static const char* object_symbol_binding_to_string(Elf32_Word type)
{
    switch (type) {
    case STB_LOCAL:
        return "LOCAL";
    case STB_GLOBAL:
        return "GLOBAL";
    case STB_WEAK:
        return "WEAK";
    case STB_NUM:
        return "NUM";
    case STB_LOPROC:
        return "LOPROC";
    case STB_HIPROC:
        return "HIPROC";
    default:
        return "(?)";
    }
}

static const char* object_relocation_type_to_string(Elf32_Word type)
{
    switch (type) {
    case R_386_NONE:
        return "R_386_NONE";
    case R_386_32:
        return "R_386_32";
    case R_386_PC32:
        return "R_386_PC32";
    case R_386_GOT32:
        return "R_386_GOT32";
    case R_386_PLT32:
        return "R_386_PLT32";
    case R_386_COPY:
        return "R_386_COPY";
    case R_386_GLOB_DAT:
        return "R_386_GLOB_DAT";
    case R_386_JMP_SLOT:
        return "R_386_JMP_SLOT";
    case R_386_RELATIVE:
        return "R_386_RELATIVE";
    case R_386_TLS_TPOFF:
        return "R_386_TLS_TPOFF";
    case R_386_TLS_TPOFF32:
        return "R_386_TLS_TPOFF32";
    default:
        return "(?)";
    }
}

int main(int argc, char** argv)
{
    if (pledge("stdio rpath", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    const char* path;
    static bool display_all = false;
    static bool display_elf_header = false;
    static bool display_program_headers = false;
    static bool display_section_headers = false;
    static bool display_headers = false;
    static bool display_symbol_table = false;
    static bool display_dynamic_symbol_table = false;
    static bool display_core_notes = false;
    static bool display_relocations = false;
    static bool display_unwind_info = false;
    static bool display_dynamic_section = false;

    Core::ArgsParser args_parser;
    args_parser.add_option(display_all, "Display all", "all", 'a');
    args_parser.add_option(display_elf_header, "Display ELF header", "file-header", 'h');
    args_parser.add_option(display_program_headers, "Display program headers", "program-headers", 'l');
    args_parser.add_option(display_section_headers, "Display section headers", "section-headers", 'S');
    args_parser.add_option(display_headers, "Equivalent to: -h -l -S -s -r -d -n -u", "headers", 'e');
    args_parser.add_option(display_symbol_table, "Display the symbol table", "syms", 's');
    args_parser.add_option(display_dynamic_section, "Display the dynamic section", "dynamic", 'd');
    args_parser.add_option(display_core_notes, "Display core notes", "notes", 'n');
    args_parser.add_option(display_relocations, "Display relocations", "relocs", 'r');
    args_parser.add_option(display_unwind_info, "Display unwind info", "unwind", 'u');
    args_parser.add_positional_argument(path, "ELF path", "path");
    args_parser.parse(argc, argv);

    if (argc < 3) {
        args_parser.print_usage(stderr, argv[0]);
        return -1;
    }

    if (display_headers) {
        display_elf_header = true;
        display_program_headers = true;
        display_section_headers = true;
    }

    if (display_all) {
        display_elf_header = true;
        display_program_headers = true;
        display_section_headers = true;
        display_core_notes = true;
        display_relocations = true;
        display_unwind_info = true;
        display_symbol_table = true;
    }

    auto file_or_error = MappedFile::map(path);

    if (file_or_error.is_error()) {
        warnln("Unable to map file {}: {}", path, file_or_error.error());
        return -1;
    }

    auto elf_image_data = file_or_error.value()->bytes();
    ELF::Image executable_elf(elf_image_data);

    if (!executable_elf.is_valid()) {
        fprintf(stderr, "File is not a valid ELF object\n");
        return -1;
    }

    String interpreter_path;

    if (!ELF::validate_program_headers(*(const Elf32_Ehdr*)elf_image_data.data(), elf_image_data.size(), (const u8*)elf_image_data.data(), elf_image_data.size(), &interpreter_path)) {
        fprintf(stderr, "Invalid ELF headers\n");
        return -1;
    }

    if (executable_elf.is_dynamic() && interpreter_path.is_null()) {
        fprintf(stderr, "Warning: Dynamic ELF object has no interpreter path\n");
    }

    ELF::Image interpreter_image(elf_image_data);

    if (!interpreter_image.is_valid()) {
        fprintf(stderr, "ELF image is invalid\n");
        return -1;
    }

    auto& header = *reinterpret_cast<const Elf32_Ehdr*>(elf_image_data.data());

    if (display_elf_header) {
        printf("ELF header:\n");

        String magic = String::format("%s", header.e_ident);
        printf("  Magic:                             ");
        for (size_t i = 0; i < magic.length(); i++) {
            if (isprint(magic[i])) {
                printf("%c ", magic[i]);
            } else {
                printf("%02x ", magic[i]);
            }
        }
        printf("\n");

        printf("  Type:                              %d (%s)\n", header.e_type, object_file_type_to_string(header.e_type));
        printf("  Machine:                           %u (%s)\n", header.e_machine, object_machine_type_to_string(header.e_machine));
        printf("  Version:                           0x%x\n", header.e_version);
        printf("  Entry point address:               0x%x\n", header.e_entry);
        printf("  Start of program headers:          %u (bytes into file)\n", header.e_phoff);
        printf("  Start of section headers:          %u (bytes into file)\n", header.e_shoff);
        printf("  Flags:                             0x%x\n", header.e_flags);
        printf("  Size of this header:               %u (bytes)\n", header.e_ehsize);
        printf("  Size of program headers:           %u (bytes)\n", header.e_phentsize);
        printf("  Number of program headers:         %u\n", header.e_phnum);
        printf("  Size of section headers:           %u (bytes)\n", header.e_shentsize);
        printf("  Number of section headers:         %u\n", header.e_shnum);
        printf("  Section header string table index: %u\n", header.e_shstrndx);
        printf("\n");
    }

    if (display_section_headers) {
        if (!display_all) {
            printf("There are %u section headers, starting at offset 0x%x:\n", header.e_shnum, header.e_shoff);
            printf("\n");
        }

        if (!interpreter_image.section_count()) {
            printf("There are no sections in this file.\n");
        } else {
            printf("Section Headers:\n");
            printf("  Name                Type            Address  Offset   Size     Flags\n");

            interpreter_image.for_each_section([](const ELF::Image::Section& section) {
                printf("  %-19s ", StringView(section.name()).to_string().characters());
                printf("%-15s ", object_section_header_type_to_string(section.type()));
                printf("%08x ", section.address());
                printf("%08x ", section.offset());
                printf("%08x ", section.size());
                printf("%u", section.flags());
                printf("\n");
                return IterationDecision::Continue;
            });
        }
        printf("\n");
    }

    if (display_program_headers) {
        if (!display_all) {
            printf("Elf file type is %d (%s)\n", header.e_type, object_file_type_to_string(header.e_type));
            printf("Entry point 0x%x\n", header.e_entry);
            printf("There are %u program headers, starting at offset %u\n", header.e_phnum, header.e_phoff);
            printf("\n");
        }

        printf("Program Headers:\n");
        printf("  Type           Offset     VirtAddr   PhysAddr   FileSiz    MemSiz     Flg  Align\n");

        interpreter_image.for_each_program_header([](const ELF::Image::ProgramHeader& program_header) {
            printf("  %-14s ", object_program_header_type_to_string(program_header.type()));
            printf("0x%08x ", program_header.offset());
            printf("%p ", program_header.vaddr().as_ptr());
            printf("%p ", program_header.vaddr().as_ptr()); // FIXME: assumes PhysAddr = VirtAddr
            printf("0x%08x ", program_header.size_in_image());
            printf("0x%08x ", program_header.size_in_memory());
            printf("%04x ", program_header.flags());
            printf("0x%08x", program_header.alignment());
            printf("\n");

            if (program_header.type() == PT_INTERP)
                printf("      [Interpreter: %s]\n", program_header.raw_data());

            return IterationDecision::Continue;
        });

        // TODO: Display section to segment mapping
        printf("\n");
    }

    if (display_dynamic_section) {
        auto found_dynamic_section = false;
        interpreter_image.for_each_section([&found_dynamic_section](const ELF::Image::Section& section) {
            if (section.name() != ELF_DYNAMIC)
                return IterationDecision::Continue;

            found_dynamic_section = true;

            if (!section.entry_count()) {
                printf("Dynamic section '%s' at offset 0x%08x contains zero entries.\n", section.name().to_string().characters(), section.offset());
                return IterationDecision::Continue;
            }

            printf("Dynamic section '%s' at offset 0x%08x contains %u entries.\n", section.name().to_string().characters(), section.offset(), section.entry_count());

            // TODO: Display dynamic section

            return IterationDecision::Continue;
        });

        if (!found_dynamic_section)
            printf("No dynamic section in this file.\n");

        printf("\n");
    }

    if (display_relocations) {
        auto found_relocations = false;
        interpreter_image.for_each_section([&found_relocations](const ELF::Image::Section& section) {
            // FIXME: support dynamic relocations (.rel.dyn)
            if (!section.relocations().relocation_count())
                return IterationDecision::Continue;

            found_relocations = true;

            if (!section.relocations().entry_count()) {
                printf("Relocation section '%s' at offset 0x%08x contains zero entries.\n", section.name().to_string().characters(), section.offset());
                return IterationDecision::Continue;
            }

            printf("Relocation section '%s' at offset 0x%08x contains %u entries:\n", section.relocations().name().to_string().characters(), section.relocations().offset(), section.relocations().entry_count());
            printf("  Offset      Type               Sym Value   Sym Name\n");
            section.relocations().for_each_relocation([](const ELF::Image::Relocation& reloc) {
                printf("  0x%08x ", reloc.offset());
                printf(" %-17s ", object_relocation_type_to_string(reloc.type()));
                printf(" 0x%08x ", reloc.symbol().value());
                printf(" %s", reloc.symbol().name().to_string().characters());
                printf("\n");
                return IterationDecision::Continue;
            });

            printf("\n");
            return IterationDecision::Continue;
        });

        if (!found_relocations)
            printf("No relocations in this file.\n");

        printf("\n");
    }

    if (display_unwind_info) {
        // TODO: Unwind info
        printf("Decoding of unwind sections for machine type %s is not supported.\n", object_machine_type_to_string(header.e_machine));
        printf("\n");
    }

    if (display_core_notes) {
        auto found_notes = false;
        interpreter_image.for_each_program_header([&found_notes](const ELF::Image::ProgramHeader& program_header) {
            if (program_header.type() != PT_NOTE)
                return IterationDecision::Continue;

            found_notes = true;

            printf("Displaying notes section '%s' at offset 0x%08x of length 0x%08x:\n", object_program_header_type_to_string(program_header.type()), program_header.offset(), program_header.size_in_image());

            // FIXME: Parse CORE notes. Notes are in JSON format on SerenityOS, but vary between systems.
            printf("%s\n", program_header.raw_data());

            return IterationDecision::Continue;
        });

        if (!found_notes)
            printf("No core notes in this file.\n");

        printf("\n");
    }

    if (display_dynamic_symbol_table || display_symbol_table) {
        auto found_dynamic_symbol_table = false;
        interpreter_image.for_each_section([&found_dynamic_symbol_table](const ELF::Image::Section& section) {
            if (section.name() != ELF_DYNSYM)
                return IterationDecision::Continue;

            found_dynamic_symbol_table = true;

            if (!section.entry_count()) {
                printf("Symbol table '%s' contains zero entries.\n", ELF_DYNSYM);
                return IterationDecision::Continue;
            }

            printf("Symbol table '%s' contains %u entries.\n", ELF_DYNSYM, section.entry_count());

            // TODO: display dynamic symbol table

            return IterationDecision::Continue;
        });

        if (!found_dynamic_symbol_table)
            printf("No dynamic symbol information for this file.\n");

        printf("\n");
    }

    if (display_symbol_table) {
        if (interpreter_image.symbol_count()) {
            printf("Symbol table '%s' contains %u entries:\n", ELF_SYMTAB, interpreter_image.symbol_count());
            printf("   Num: Value    Size     Type     Bind     Name\n");

            interpreter_image.for_each_symbol([](const ELF::Image::Symbol& sym) {
                printf("  %4u: ", sym.index());
                printf("%08x ", sym.value());
                printf("%08x ", sym.size());
                printf("%-8s ", object_symbol_type_to_string(sym.type()));
                printf("%-8s ", object_symbol_binding_to_string(sym.bind()));
                printf("%s", StringView(sym.name()).to_string().characters());
                printf("\n");
                return IterationDecision::Continue;
            });
        }
        printf("\n");
    }

    return 0;
}