Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Google, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #ifndef ELFPARSING_H |
| 15 | #define ELFPARSING_H |
| 16 | |
| 17 | #include "elf.h" |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 18 | #include "common.h" |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 19 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 20 | struct parsed_elf { |
| 21 | Elf64_Ehdr ehdr; |
| 22 | Elf64_Phdr *phdr; |
| 23 | Elf64_Shdr *shdr; |
Aaron Durbin | ccb5ad8 | 2014-03-05 13:57:30 -0600 | [diff] [blame] | 24 | /* |
| 25 | * The relocs array contains pointers to arrays of relocation |
| 26 | * structures. Each index into the relocs array corresponds to its |
| 27 | * corresponding section index. i.e. if a section i is of type SHT_REL |
| 28 | * or SHT_RELA then the corresponding index into the relocs array will |
| 29 | * contain the associated relocations. Otherwise thee entry will be |
| 30 | * NULL. |
| 31 | */ |
| 32 | Elf64_Rela **relocs; |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 33 | /* |
| 34 | * Similarly to the relocs array the strtabs array consists of an |
| 35 | * array of pointers where each entry represents a potential struct |
| 36 | * buffer pointer. Only setions of type SHT_STRTAB will have a non-NULL |
| 37 | * entry. |
| 38 | */ |
| 39 | struct buffer **strtabs; |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 40 | /* Parsed symbols. */ |
| 41 | Elf64_Sym *syms; |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | #define ELF_PARSE_PHDR (1 << 0) |
| 45 | #define ELF_PARSE_SHDR (1 << 1) |
Aaron Durbin | ccb5ad8 | 2014-03-05 13:57:30 -0600 | [diff] [blame] | 46 | #define ELF_PARSE_RELOC (1 << 2) |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 47 | #define ELF_PARSE_STRTAB (1 << 3) |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 48 | #define ELF_PARSE_SYMTAB (1 << 4) |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 49 | |
| 50 | #define ELF_PARSE_ALL (-1) |
| 51 | |
| 52 | /* |
| 53 | * Parse an ELF file contained within provide struct buffer. The ELF header |
| 54 | * is always parsed while the flags value containing the ELF_PARSE_* values |
| 55 | * determine if other parts of the ELF file will be parsed as well. |
| 56 | * Returns 0 on success, < 0 error. |
| 57 | */ |
| 58 | int parse_elf(const struct buffer *pinput, struct parsed_elf *pelf, int flags); |
| 59 | |
| 60 | /* |
| 61 | * Clean up memory associated with parsed_elf. |
| 62 | */ |
| 63 | void parsed_elf_destroy(struct parsed_elf *pelf); |
| 64 | |
| 65 | |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 66 | int |
| 67 | elf_headers(const struct buffer *pinput, |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 68 | Elf64_Ehdr *ehdr, |
| 69 | Elf64_Phdr **pphdr, |
| 70 | Elf64_Shdr **pshdr); |
| 71 | |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 72 | /* ELF writing support. */ |
| 73 | struct elf_writer; |
| 74 | |
| 75 | /* |
Aaron Durbin | 4f930c9 | 2015-10-27 16:21:55 -0500 | [diff] [blame] | 76 | * Initialize a 64-bit ELF header provided the inputs. While the structure |
| 77 | * is a 64-bit header one can specify a 32-bit machine. The 64-bit version |
| 78 | * is just used as a common structure. If one wants to specify the entry |
| 79 | * point, for example, the caller can set it after filling in the common |
| 80 | * bits. The machine, nbits, and endian values should be from the ELF |
| 81 | * definitions (e.g. EM_386, ELFCLASS32, and ELFDATA2LSB) found in elf.h |
| 82 | * with no endian conversion required. |
| 83 | */ |
| 84 | void elf_init_eheader(Elf64_Ehdr *ehdr, int machine, int nbits, int endian); |
| 85 | |
| 86 | /* |
Elyes HAOUAS | 3db0198 | 2018-08-23 18:08:20 +0200 | [diff] [blame] | 87 | * Initialize a new ELF writer. Default machine type, endianness, etc is |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 88 | * copied from the passed in Elf64_Ehdr. Returns NULL on failure, valid |
| 89 | * pointer on success. |
| 90 | */ |
| 91 | struct elf_writer *elf_writer_init(const Elf64_Ehdr *ehdr); |
| 92 | |
| 93 | /* |
| 94 | * Clean up any internal state represented by ew. Aftewards the elf_writer |
| 95 | * is invalid. |
Furquan Shaikh | b927bec | 2016-08-05 12:04:55 -0700 | [diff] [blame] | 96 | * It is safe to call elf_writer_destroy with ew as NULL. It returns without |
| 97 | * performing any action. |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 98 | */ |
| 99 | void elf_writer_destroy(struct elf_writer *ew); |
| 100 | |
| 101 | /* |
| 102 | * Add a section to the ELF file. Section type, flags, and memsize are |
| 103 | * maintained from the passed in Elf64_Shdr. The buffer represents the |
| 104 | * content of the section while the name is the name of section itself. |
| 105 | * Returns < 0 on error, 0 on success. |
| 106 | */ |
| 107 | int elf_writer_add_section(struct elf_writer *ew, const Elf64_Shdr *shdr, |
| 108 | struct buffer *contents, const char *name); |
| 109 | |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 110 | /* Add an absolute symbol to the ELF file returning < 0 on error, index of |
| 111 | * symbol otherwise. */ |
| 112 | int elf_writer_add_symbol(struct elf_writer *ew, const char *name, |
| 113 | const char *section_name, |
| 114 | Elf64_Addr value, Elf64_Word size, |
| 115 | int binding, int type); |
| 116 | |
| 117 | /* Add an absolute relocation referencing the provided symbol name. Returns < 0 |
| 118 | * on error, 0 on success. */ |
| 119 | int elf_writer_add_rel(struct elf_writer *ew, const char *sym, Elf64_Addr addr); |
| 120 | |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 121 | /* |
| 122 | * Serialize the ELF file to the output buffer. Return < 0 on error, |
| 123 | * 0 on success. |
| 124 | */ |
| 125 | int elf_writer_serialize(struct elf_writer *ew, struct buffer *out); |
| 126 | |
Aaron Durbin | facf149 | 2017-12-18 14:50:22 -0700 | [diff] [blame] | 127 | /* |
| 128 | * Calculate the loadable program's file size footprint. Returns < 0 on error, |
| 129 | * 0 on success. |
| 130 | */ |
| 131 | int elf_program_file_size(const struct buffer *input, size_t *file_size); |
| 132 | |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 133 | #endif /* ELFPARSING_H */ |