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