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. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #ifndef ELFPARSING_H |
| 19 | #define ELFPARSING_H |
| 20 | |
| 21 | #include "elf.h" |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 22 | #include "common.h" |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 23 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 24 | struct parsed_elf { |
| 25 | Elf64_Ehdr ehdr; |
| 26 | Elf64_Phdr *phdr; |
| 27 | Elf64_Shdr *shdr; |
Aaron Durbin | ccb5ad8 | 2014-03-05 13:57:30 -0600 | [diff] [blame] | 28 | /* |
| 29 | * The relocs array contains pointers to arrays of relocation |
| 30 | * structures. Each index into the relocs array corresponds to its |
| 31 | * corresponding section index. i.e. if a section i is of type SHT_REL |
| 32 | * or SHT_RELA then the corresponding index into the relocs array will |
| 33 | * contain the associated relocations. Otherwise thee entry will be |
| 34 | * NULL. |
| 35 | */ |
| 36 | Elf64_Rela **relocs; |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 37 | /* |
| 38 | * Similarly to the relocs array the strtabs array consists of an |
| 39 | * array of pointers where each entry represents a potential struct |
| 40 | * buffer pointer. Only setions of type SHT_STRTAB will have a non-NULL |
| 41 | * entry. |
| 42 | */ |
| 43 | struct buffer **strtabs; |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 44 | /* Parsed symbols. */ |
| 45 | Elf64_Sym *syms; |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | #define ELF_PARSE_PHDR (1 << 0) |
| 49 | #define ELF_PARSE_SHDR (1 << 1) |
Aaron Durbin | ccb5ad8 | 2014-03-05 13:57:30 -0600 | [diff] [blame] | 50 | #define ELF_PARSE_RELOC (1 << 2) |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 51 | #define ELF_PARSE_STRTAB (1 << 3) |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 52 | #define ELF_PARSE_SYMTAB (1 << 4) |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 53 | |
| 54 | #define ELF_PARSE_ALL (-1) |
| 55 | |
| 56 | /* |
| 57 | * Parse an ELF file contained within provide struct buffer. The ELF header |
| 58 | * is always parsed while the flags value containing the ELF_PARSE_* values |
| 59 | * determine if other parts of the ELF file will be parsed as well. |
| 60 | * Returns 0 on success, < 0 error. |
| 61 | */ |
| 62 | int parse_elf(const struct buffer *pinput, struct parsed_elf *pelf, int flags); |
| 63 | |
| 64 | /* |
| 65 | * Clean up memory associated with parsed_elf. |
| 66 | */ |
| 67 | void parsed_elf_destroy(struct parsed_elf *pelf); |
| 68 | |
| 69 | |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 70 | int |
| 71 | elf_headers(const struct buffer *pinput, |
| 72 | uint32_t arch, |
| 73 | Elf64_Ehdr *ehdr, |
| 74 | Elf64_Phdr **pphdr, |
| 75 | Elf64_Shdr **pshdr); |
| 76 | |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 77 | /* ELF writing support. */ |
| 78 | struct elf_writer; |
| 79 | |
| 80 | /* |
| 81 | * Initialize a new ELF writer. Deafult machine type, endianness, etc is |
| 82 | * copied from the passed in Elf64_Ehdr. Returns NULL on failure, valid |
| 83 | * pointer on success. |
| 84 | */ |
| 85 | struct elf_writer *elf_writer_init(const Elf64_Ehdr *ehdr); |
| 86 | |
| 87 | /* |
| 88 | * Clean up any internal state represented by ew. Aftewards the elf_writer |
| 89 | * is invalid. |
| 90 | */ |
| 91 | void elf_writer_destroy(struct elf_writer *ew); |
| 92 | |
| 93 | /* |
| 94 | * Add a section to the ELF file. Section type, flags, and memsize are |
| 95 | * maintained from the passed in Elf64_Shdr. The buffer represents the |
| 96 | * content of the section while the name is the name of section itself. |
| 97 | * Returns < 0 on error, 0 on success. |
| 98 | */ |
| 99 | int elf_writer_add_section(struct elf_writer *ew, const Elf64_Shdr *shdr, |
| 100 | struct buffer *contents, const char *name); |
| 101 | |
| 102 | /* |
| 103 | * Serialize the ELF file to the output buffer. Return < 0 on error, |
| 104 | * 0 on success. |
| 105 | */ |
| 106 | int elf_writer_serialize(struct elf_writer *ew, struct buffer *out); |
| 107 | |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 108 | #endif /* ELFPARSING_H */ |