[2c4e1cc] | 1 | /*
|
---|
| 2 | * Copyright (c) 2018 CZ.NIC, z.s.p.o.
|
---|
| 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
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <assert.h>
|
---|
[55092672] | 30 | #include <elf/elf.h>
|
---|
| 31 | #include <stdbool.h>
|
---|
[2c4e1cc] | 32 | #include <stdlib.h>
|
---|
| 33 |
|
---|
[174156fd] | 34 | /** @addtogroup libc
|
---|
[2c4e1cc] | 35 | * @{
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Checks that the ELF header is valid for the running system.
|
---|
| 40 | */
|
---|
| 41 | static bool elf_is_valid(const elf_header_t *header)
|
---|
| 42 | {
|
---|
| 43 | // TODO: check more things
|
---|
| 44 | // TODO: debug output
|
---|
| 45 |
|
---|
| 46 | if (header->e_ident[EI_MAG0] != ELFMAG0 ||
|
---|
| 47 | header->e_ident[EI_MAG1] != ELFMAG1 ||
|
---|
| 48 | header->e_ident[EI_MAG2] != ELFMAG2 ||
|
---|
| 49 | header->e_ident[EI_MAG3] != ELFMAG3) {
|
---|
| 50 | return false;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING ||
|
---|
| 54 | header->e_machine != ELF_MACHINE ||
|
---|
| 55 | header->e_ident[EI_VERSION] != EV_CURRENT ||
|
---|
| 56 | header->e_version != EV_CURRENT ||
|
---|
| 57 | header->e_ident[EI_CLASS] != ELF_CLASS) {
|
---|
| 58 | return false;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | if (header->e_phentsize != sizeof(elf_segment_header_t)) {
|
---|
| 62 | return false;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | if (header->e_type != ET_EXEC && header->e_type != ET_DYN) {
|
---|
| 66 | return false;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | return true;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
[7900e5d] | 73 | * Given the base of an ELF image in memory (i.e. pointer to the file
|
---|
[2c4e1cc] | 74 | * header at the beginning of the text segment), returns pointer to the
|
---|
| 75 | * first segment header with the given p_type.
|
---|
| 76 | */
|
---|
| 77 | const elf_segment_header_t *elf_get_phdr(const void *base, unsigned p_type)
|
---|
| 78 | {
|
---|
| 79 | const elf_header_t *hdr = base;
|
---|
| 80 | assert(elf_is_valid(hdr));
|
---|
| 81 |
|
---|
| 82 | const elf_segment_header_t *phdr =
|
---|
| 83 | (const elf_segment_header_t *) ((uintptr_t) base + hdr->e_phoff);
|
---|
| 84 |
|
---|
| 85 | for (int i = 0; i < hdr->e_phnum; i++) {
|
---|
| 86 | if (phdr[i].p_type == p_type)
|
---|
| 87 | return &phdr[i];
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | return NULL;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | uintptr_t elf_get_bias(const void *base)
|
---|
| 94 | {
|
---|
| 95 | const elf_header_t *hdr = base;
|
---|
| 96 | assert(elf_is_valid(hdr));
|
---|
| 97 |
|
---|
[7c3fb9b] | 98 | /*
|
---|
| 99 | * There are two legal options for a HelenOS ELF file.
|
---|
[2c4e1cc] | 100 | * Either the file is ET_DYN (shared library or PIE), and the base is
|
---|
| 101 | * (required to be) at vaddr 0. Or the file is ET_EXEC (non-relocatable)
|
---|
| 102 | * and the bias is trivially 0.
|
---|
| 103 | */
|
---|
| 104 |
|
---|
| 105 | if (hdr->e_type == ET_DYN)
|
---|
| 106 | return (uintptr_t) base;
|
---|
| 107 |
|
---|
| 108 | return 0;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /** @}
|
---|
| 112 | */
|
---|