Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * elf header parsing. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
Aaron Durbin | 54ef306 | 2014-03-05 12:12:09 -0600 | [diff] [blame] | 20 | #include "elfparsing.h" |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 21 | #include "common.h" |
| 22 | #include "cbfs.h" |
| 23 | |
| 24 | /* |
| 25 | * Short form: this is complicated, but we've tried making it simple |
| 26 | * and we keep hitting problems with our ELF parsing. |
| 27 | * |
| 28 | * The ELF parsing situation has always been a bit tricky. In fact, |
| 29 | * we (and most others) have been getting it wrong in small ways for |
| 30 | * years. Recently this has caused real trouble for the ARM V8 build. |
| 31 | * In this file we attempt to finally get it right for all variations |
| 32 | * of endian-ness and word size and target architectures and |
| 33 | * architectures we might get run on. Phew!. To do this we borrow a |
| 34 | * page from the FreeBSD NFS xdr model (see elf_ehdr and elf_phdr), |
| 35 | * the Plan 9 endianness functions (see xdr.c), and Go interfaces (see |
| 36 | * how we use buffer structs in this file). This ends up being a bit |
| 37 | * wordy at the lowest level, but greatly simplifies the elf parsing |
| 38 | * code and removes a common source of bugs, namely, forgetting to |
| 39 | * flip type endianness when referencing a struct member. |
| 40 | * |
| 41 | * ELF files can have four combinations of data layout: 32/64, and |
| 42 | * big/little endian. Further, to add to the fun, depending on the |
| 43 | * word size, the size of the ELF structs varies. The coreboot SELF |
| 44 | * format is simpler in theory: it's supposed to be always BE, and the |
| 45 | * various struct members allow room for growth: the entry point is |
| 46 | * always 64 bits, for example, so the size of a SELF struct is |
| 47 | * constant, regardless of target architecture word size. Hence, we |
| 48 | * need to do some transformation of the ELF files. |
| 49 | * |
| 50 | * A given architecture, realistically, only supports one of the four |
| 51 | * combinations at a time as the 'native' format. Hence, our code has |
| 52 | * been sprinkled with every variation of [nh]to[hn][sll] over the |
| 53 | * years. We've never quite gotten it all right, however, and a quick |
| 54 | * pass over this code revealed another bug. It's all worked because, |
| 55 | * until now, all the working platforms that had CBFS were 32 LE. Even then, |
| 56 | * however, bugs crept in: we recently realized that we're not |
| 57 | * transforming the entry point to big format when we store into the |
| 58 | * SELF image. |
| 59 | * |
| 60 | * The problem is essentially an XDR operation: |
| 61 | * we have something in a foreign format and need to transform it. |
| 62 | * It's most like XDR because: |
| 63 | * 1) the byte order can be wrong |
| 64 | * 2) the word size can be wrong |
| 65 | * 3) the size of elements in the stream depends on the value |
| 66 | * of other elements in the stream |
| 67 | * it's not like XDR because: |
| 68 | * 1) the byte order can be right |
| 69 | * 2) the word size can be right |
| 70 | * 3) the struct members are all on a natural alignment |
| 71 | * |
| 72 | * Hence, this new approach. To cover word size issues, we *always* |
| 73 | * transform the two structs we care about, the file header and |
| 74 | * program header, into a native struct in the 64 bit format: |
| 75 | * |
| 76 | * [32,little] -> [Elf64_Ehdr, Elf64_Phdr] |
| 77 | * [64,little] -> [Elf64_Ehdr, Elf64_Phdr] |
| 78 | * [32,big] -> [Elf64_Ehdr, Elf64_Phdr] |
| 79 | * [64,big] -> [Elf64_Ehdr, Elf64_Phdr] |
| 80 | * Then we just use those structs, and all the need for inline ntoh* goes away, |
| 81 | * as well as all the chances for error. |
| 82 | * This works because all the SELF structs have fields large enough for |
| 83 | * the largest ELF 64 struct members, and all the Elf64 struct members |
| 84 | * are at least large enough for all ELF 32 struct members. |
| 85 | * We end up with one function to do all our ELF parsing, and two functions |
| 86 | * to transform the headers. For the put case, we also have |
| 87 | * XDR functions, and hopefully we'll never again spend 5 years with the |
| 88 | * wrong endian-ness on an output value :-) |
| 89 | * This should work for all word sizes and endianness we hope to target. |
| 90 | * I *really* don't want to be here for 128 bit addresses. |
| 91 | * |
| 92 | * The parse functions are called with a pointer to an input buffer |
| 93 | * struct. One might ask: are there enough bytes in the input buffer? |
| 94 | * We know there need to be at *least* sizeof(Elf32_Ehdr) + |
| 95 | * sizeof(Elf32_Phdr) bytes. Realistically, there has to be some data |
| 96 | * too. If we start to worry, though we have not in the past, we |
| 97 | * might apply the simple test: the input buffer needs to be at least |
| 98 | * sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) bytes because, even if it's |
| 99 | * ELF 32, there's got to be *some* data! This is not theoretically |
| 100 | * accurate but it is actually good enough in practice. It allows the |
| 101 | * header transformation code to ignore the possibility of underrun. |
| 102 | * |
Elyes HAOUAS | 3db0198 | 2018-08-23 18:08:20 +0200 | [diff] [blame] | 103 | * We also must accommodate different ELF files, and hence formats, |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 104 | * in the same cbfs invocation. We might load a 64-bit payload |
| 105 | * on a 32-bit machine; we might even have a mixed armv7/armv8 |
| 106 | * SOC or even a system with an x86/ARM! |
| 107 | * |
| 108 | * A possibly problematic (though unlikely to be so) assumption |
| 109 | * is that we expect the BIOS to remain in the lowest 32 bits |
| 110 | * of the physical address space. Since ARMV8 has standardized |
| 111 | * on that, and x86_64 also has, this seems a safe assumption. |
| 112 | * |
| 113 | * To repeat, ELF structs are different sizes because ELF struct |
| 114 | * members are different sizes, depending on values in the ELF file |
| 115 | * header. For this we use the functions defined in xdr.c, which |
| 116 | * consume bytes, convert the endianness, and advance the data pointer |
| 117 | * in the buffer struct. |
| 118 | */ |
| 119 | |
Aaron Durbin | aa8784c | 2014-03-05 12:01:36 -0600 | [diff] [blame] | 120 | |
| 121 | static int iself(const void *input) |
| 122 | { |
| 123 | const Elf32_Ehdr *ehdr = input; |
| 124 | return !memcmp(ehdr->e_ident, ELFMAG, 4); |
| 125 | } |
| 126 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 127 | /* Get the ident array, so we can figure out |
| 128 | * endian-ness, word size, and in future other useful |
| 129 | * parameters |
| 130 | */ |
| 131 | static void |
| 132 | elf_eident(struct buffer *input, Elf64_Ehdr *ehdr) |
| 133 | { |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 134 | bgets(input, ehdr->e_ident, sizeof(ehdr->e_ident)); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 138 | static int |
| 139 | check_size(const struct buffer *b, size_t offset, size_t size, const char *desc) |
| 140 | { |
| 141 | if (size == 0) |
| 142 | return 0; |
| 143 | |
| 144 | if (offset >= buffer_size(b) || (offset + size) > buffer_size(b)) { |
| 145 | ERROR("The file is not large enough for the '%s'. " |
Paul Menzel | 470c37c | 2014-03-16 00:15:57 +0100 | [diff] [blame] | 146 | "%zu bytes @ offset %zu, input %zu bytes.\n", |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 147 | desc, size, offset, buffer_size(b)); |
| 148 | return -1; |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 153 | static void |
| 154 | elf_ehdr(struct buffer *input, Elf64_Ehdr *ehdr, struct xdr *xdr, int bit64) |
| 155 | { |
| 156 | ehdr->e_type = xdr->get16(input); |
| 157 | ehdr->e_machine = xdr->get16(input); |
| 158 | ehdr->e_version = xdr->get32(input); |
| 159 | if (bit64){ |
| 160 | ehdr->e_entry = xdr->get64(input); |
| 161 | ehdr->e_phoff = xdr->get64(input); |
| 162 | ehdr->e_shoff = xdr->get64(input); |
| 163 | } else { |
| 164 | ehdr->e_entry = xdr->get32(input); |
| 165 | ehdr->e_phoff = xdr->get32(input); |
| 166 | ehdr->e_shoff = xdr->get32(input); |
| 167 | } |
| 168 | ehdr->e_flags = xdr->get32(input); |
| 169 | ehdr->e_ehsize = xdr->get16(input); |
| 170 | ehdr->e_phentsize = xdr->get16(input); |
| 171 | ehdr->e_phnum = xdr->get16(input); |
| 172 | ehdr->e_shentsize = xdr->get16(input); |
| 173 | ehdr->e_shnum = xdr->get16(input); |
| 174 | ehdr->e_shstrndx = xdr->get16(input); |
| 175 | } |
| 176 | |
| 177 | static void |
| 178 | elf_phdr(struct buffer *pinput, Elf64_Phdr *phdr, |
| 179 | int entsize, struct xdr *xdr, int bit64) |
| 180 | { |
| 181 | /* |
| 182 | * The entsize need not be sizeof(*phdr). |
| 183 | * Hence, it is easier to keep a copy of the input, |
| 184 | * as the xdr functions may not advance the input |
| 185 | * pointer the full entsize; rather than get tricky |
| 186 | * we just advance it below. |
| 187 | */ |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 188 | struct buffer input; |
| 189 | buffer_clone(&input, pinput); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 190 | if (bit64){ |
| 191 | phdr->p_type = xdr->get32(&input); |
| 192 | phdr->p_flags = xdr->get32(&input); |
| 193 | phdr->p_offset = xdr->get64(&input); |
| 194 | phdr->p_vaddr = xdr->get64(&input); |
| 195 | phdr->p_paddr = xdr->get64(&input); |
| 196 | phdr->p_filesz = xdr->get64(&input); |
| 197 | phdr->p_memsz = xdr->get64(&input); |
| 198 | phdr->p_align = xdr->get64(&input); |
| 199 | } else { |
| 200 | phdr->p_type = xdr->get32(&input); |
| 201 | phdr->p_offset = xdr->get32(&input); |
| 202 | phdr->p_vaddr = xdr->get32(&input); |
| 203 | phdr->p_paddr = xdr->get32(&input); |
| 204 | phdr->p_filesz = xdr->get32(&input); |
| 205 | phdr->p_memsz = xdr->get32(&input); |
| 206 | phdr->p_flags = xdr->get32(&input); |
| 207 | phdr->p_align = xdr->get32(&input); |
| 208 | } |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 209 | buffer_seek(pinput, entsize); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static void |
| 213 | elf_shdr(struct buffer *pinput, Elf64_Shdr *shdr, |
| 214 | int entsize, struct xdr *xdr, int bit64) |
| 215 | { |
| 216 | /* |
| 217 | * The entsize need not be sizeof(*shdr). |
| 218 | * Hence, it is easier to keep a copy of the input, |
| 219 | * as the xdr functions may not advance the input |
| 220 | * pointer the full entsize; rather than get tricky |
| 221 | * we just advance it below. |
| 222 | */ |
| 223 | struct buffer input = *pinput; |
| 224 | if (bit64){ |
| 225 | shdr->sh_name = xdr->get32(&input); |
| 226 | shdr->sh_type = xdr->get32(&input); |
| 227 | shdr->sh_flags = xdr->get64(&input); |
| 228 | shdr->sh_addr = xdr->get64(&input); |
| 229 | shdr->sh_offset = xdr->get64(&input); |
| 230 | shdr->sh_size= xdr->get64(&input); |
| 231 | shdr->sh_link = xdr->get32(&input); |
| 232 | shdr->sh_info = xdr->get32(&input); |
| 233 | shdr->sh_addralign = xdr->get64(&input); |
| 234 | shdr->sh_entsize = xdr->get64(&input); |
| 235 | } else { |
| 236 | shdr->sh_name = xdr->get32(&input); |
| 237 | shdr->sh_type = xdr->get32(&input); |
| 238 | shdr->sh_flags = xdr->get32(&input); |
| 239 | shdr->sh_addr = xdr->get32(&input); |
| 240 | shdr->sh_offset = xdr->get32(&input); |
| 241 | shdr->sh_size = xdr->get32(&input); |
| 242 | shdr->sh_link = xdr->get32(&input); |
| 243 | shdr->sh_info = xdr->get32(&input); |
| 244 | shdr->sh_addralign = xdr->get32(&input); |
| 245 | shdr->sh_entsize = xdr->get32(&input); |
| 246 | } |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 247 | buffer_seek(pinput, entsize); |
| 248 | } |
| 249 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 250 | static int |
| 251 | phdr_read(const struct buffer *in, struct parsed_elf *pelf, |
| 252 | struct xdr *xdr, int bit64) |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 253 | { |
| 254 | struct buffer b; |
| 255 | Elf64_Phdr *phdr; |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 256 | Elf64_Ehdr *ehdr; |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 257 | int i; |
| 258 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 259 | ehdr = &pelf->ehdr; |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 260 | /* cons up an input buffer for the headers. |
| 261 | * Note that the program headers can be anywhere, |
| 262 | * per the ELF spec, You'd be surprised how many ELF |
| 263 | * readers miss this little detail. |
| 264 | */ |
| 265 | buffer_splice(&b, in, ehdr->e_phoff, ehdr->e_phentsize * ehdr->e_phnum); |
| 266 | if (check_size(in, ehdr->e_phoff, buffer_size(&b), "program headers")) |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 267 | return -1; |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 268 | |
| 269 | /* gather up all the phdrs. |
| 270 | * We do them all at once because there is more |
| 271 | * than one loop over all the phdrs. |
| 272 | */ |
Aaron Durbin | b1b5118 | 2014-03-05 14:54:13 -0600 | [diff] [blame] | 273 | phdr = calloc(ehdr->e_phnum, sizeof(*phdr)); |
Aaron Durbin | a31ff73 | 2014-03-07 15:23:05 -0600 | [diff] [blame] | 274 | for (i = 0; i < ehdr->e_phnum; i++) { |
| 275 | DEBUG("Parsing segment %d\n", i); |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 276 | elf_phdr(&b, &phdr[i], ehdr->e_phentsize, xdr, bit64); |
| 277 | |
Aaron Durbin | a31ff73 | 2014-03-07 15:23:05 -0600 | [diff] [blame] | 278 | /* Ensure the contents are valid within the elf file. */ |
| 279 | if (check_size(in, phdr[i].p_offset, phdr[i].p_filesz, |
Patrick Georgi | a65c21e | 2014-08-09 16:58:00 +0200 | [diff] [blame] | 280 | "segment contents")) { |
| 281 | free(phdr); |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 282 | return -1; |
Patrick Georgi | a65c21e | 2014-08-09 16:58:00 +0200 | [diff] [blame] | 283 | } |
Aaron Durbin | a31ff73 | 2014-03-07 15:23:05 -0600 | [diff] [blame] | 284 | } |
| 285 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 286 | pelf->phdr = phdr; |
| 287 | |
| 288 | return 0; |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 289 | } |
| 290 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 291 | static int |
| 292 | shdr_read(const struct buffer *in, struct parsed_elf *pelf, |
| 293 | struct xdr *xdr, int bit64) |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 294 | { |
| 295 | struct buffer b; |
| 296 | Elf64_Shdr *shdr; |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 297 | Elf64_Ehdr *ehdr; |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 298 | int i; |
| 299 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 300 | ehdr = &pelf->ehdr; |
| 301 | |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 302 | /* cons up an input buffer for the section headers. |
| 303 | * Note that the section headers can be anywhere, |
| 304 | * per the ELF spec, You'd be surprised how many ELF |
| 305 | * readers miss this little detail. |
| 306 | */ |
| 307 | buffer_splice(&b, in, ehdr->e_shoff, ehdr->e_shentsize * ehdr->e_shnum); |
| 308 | if (check_size(in, ehdr->e_shoff, buffer_size(&b), "section headers")) |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 309 | return -1; |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 310 | |
| 311 | /* gather up all the shdrs. */ |
Aaron Durbin | b1b5118 | 2014-03-05 14:54:13 -0600 | [diff] [blame] | 312 | shdr = calloc(ehdr->e_shnum, sizeof(*shdr)); |
Aaron Durbin | a31ff73 | 2014-03-07 15:23:05 -0600 | [diff] [blame] | 313 | for (i = 0; i < ehdr->e_shnum; i++) { |
| 314 | DEBUG("Parsing section %d\n", i); |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 315 | elf_shdr(&b, &shdr[i], ehdr->e_shentsize, xdr, bit64); |
Aaron Durbin | a31ff73 | 2014-03-07 15:23:05 -0600 | [diff] [blame] | 316 | } |
| 317 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 318 | pelf->shdr = shdr; |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
Aaron Durbin | ccb5ad8 | 2014-03-05 13:57:30 -0600 | [diff] [blame] | 323 | static int |
| 324 | reloc_read(const struct buffer *in, struct parsed_elf *pelf, |
| 325 | struct xdr *xdr, int bit64) |
| 326 | { |
| 327 | struct buffer b; |
| 328 | Elf64_Word i; |
| 329 | Elf64_Ehdr *ehdr; |
| 330 | |
| 331 | ehdr = &pelf->ehdr; |
| 332 | pelf->relocs = calloc(ehdr->e_shnum, sizeof(Elf64_Rela *)); |
| 333 | |
| 334 | /* Allocate array for each section that contains relocation entries. */ |
| 335 | for (i = 0; i < ehdr->e_shnum; i++) { |
| 336 | Elf64_Shdr *shdr; |
| 337 | Elf64_Rela *rela; |
| 338 | Elf64_Xword j; |
| 339 | Elf64_Xword nrelocs; |
| 340 | int is_rela; |
| 341 | |
| 342 | shdr = &pelf->shdr[i]; |
| 343 | |
| 344 | /* Only process REL and RELA sections. */ |
| 345 | if (shdr->sh_type != SHT_REL && shdr->sh_type != SHT_RELA) |
| 346 | continue; |
| 347 | |
| 348 | DEBUG("Checking relocation section %u\n", i); |
| 349 | |
| 350 | /* Ensure the section that relocations apply is a valid. */ |
| 351 | if (shdr->sh_info >= ehdr->e_shnum || |
| 352 | shdr->sh_info == SHN_UNDEF) { |
| 353 | ERROR("Relocations apply to an invalid section: %u\n", |
| 354 | shdr[i].sh_info); |
| 355 | return -1; |
| 356 | } |
| 357 | |
| 358 | is_rela = shdr->sh_type == SHT_RELA; |
| 359 | |
| 360 | /* Determine the number relocations in this section. */ |
| 361 | nrelocs = shdr->sh_size / shdr->sh_entsize; |
| 362 | |
| 363 | pelf->relocs[i] = calloc(nrelocs, sizeof(Elf64_Rela)); |
| 364 | |
| 365 | buffer_splice(&b, in, shdr->sh_offset, shdr->sh_size); |
| 366 | if (check_size(in, shdr->sh_offset, buffer_size(&b), |
| 367 | "relocation section")) { |
| 368 | ERROR("Relocation section %u failed.\n", i); |
| 369 | return -1; |
| 370 | } |
| 371 | |
| 372 | rela = pelf->relocs[i]; |
| 373 | for (j = 0; j < nrelocs; j++) { |
| 374 | if (bit64) { |
| 375 | rela->r_offset = xdr->get64(&b); |
| 376 | rela->r_info = xdr->get64(&b); |
| 377 | if (is_rela) |
| 378 | rela->r_addend = xdr->get64(&b); |
| 379 | } else { |
| 380 | uint32_t r_info; |
| 381 | |
| 382 | rela->r_offset = xdr->get32(&b); |
| 383 | r_info = xdr->get32(&b); |
| 384 | rela->r_info = ELF64_R_INFO(ELF32_R_SYM(r_info), |
| 385 | ELF32_R_TYPE(r_info)); |
| 386 | if (is_rela) |
| 387 | rela->r_addend = xdr->get32(&b); |
| 388 | } |
| 389 | rela++; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 396 | static int strtab_read(const struct buffer *in, struct parsed_elf *pelf) |
| 397 | { |
| 398 | Elf64_Ehdr *ehdr; |
| 399 | Elf64_Word i; |
| 400 | |
| 401 | ehdr = &pelf->ehdr; |
| 402 | |
| 403 | if (ehdr->e_shstrndx >= ehdr->e_shnum) { |
| 404 | ERROR("Section header string table index out of range: %d\n", |
| 405 | ehdr->e_shstrndx); |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | /* For each section of type SHT_STRTAB create a symtab buffer. */ |
| 410 | pelf->strtabs = calloc(ehdr->e_shnum, sizeof(struct buffer *)); |
| 411 | |
| 412 | for (i = 0; i < ehdr->e_shnum; i++) { |
| 413 | struct buffer *b; |
| 414 | Elf64_Shdr *shdr = &pelf->shdr[i]; |
| 415 | |
| 416 | if (shdr->sh_type != SHT_STRTAB) |
| 417 | continue; |
| 418 | |
| 419 | b = calloc(1, sizeof(*b)); |
| 420 | buffer_splice(b, in, shdr->sh_offset, shdr->sh_size); |
| 421 | if (check_size(in, shdr->sh_offset, buffer_size(b), "strtab")) { |
| 422 | ERROR("STRTAB section not within bounds: %d\n", i); |
Patrick Georgi | a65c21e | 2014-08-09 16:58:00 +0200 | [diff] [blame] | 423 | free(b); |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 424 | return -1; |
| 425 | } |
| 426 | pelf->strtabs[i] = b; |
| 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 432 | static int |
| 433 | symtab_read(const struct buffer *in, struct parsed_elf *pelf, |
| 434 | struct xdr *xdr, int bit64) |
| 435 | { |
| 436 | Elf64_Ehdr *ehdr; |
| 437 | Elf64_Shdr *shdr; |
Damien Zammit | 0025247 | 2017-09-03 21:04:41 +1000 | [diff] [blame] | 438 | Elf64_Half shnum; |
| 439 | Elf64_Xword i; |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 440 | Elf64_Xword nsyms; |
| 441 | Elf64_Sym *sym; |
| 442 | struct buffer b; |
| 443 | |
| 444 | ehdr = &pelf->ehdr; |
| 445 | |
| 446 | shdr = NULL; |
Damien Zammit | 0025247 | 2017-09-03 21:04:41 +1000 | [diff] [blame] | 447 | for (shnum = 0; shnum < ehdr->e_shnum; shnum++) { |
| 448 | if (pelf->shdr[shnum].sh_type != SHT_SYMTAB) |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 449 | continue; |
| 450 | |
| 451 | if (shdr != NULL) { |
| 452 | ERROR("Multiple symbol sections found. %u and %u\n", |
Damien Zammit | 0025247 | 2017-09-03 21:04:41 +1000 | [diff] [blame] | 453 | (unsigned int)(shdr - pelf->shdr), shnum); |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 454 | return -1; |
| 455 | } |
| 456 | |
Damien Zammit | 0025247 | 2017-09-03 21:04:41 +1000 | [diff] [blame] | 457 | shdr = &pelf->shdr[shnum]; |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | if (shdr == NULL) { |
| 461 | ERROR("No symbol table found.\n"); |
| 462 | return -1; |
| 463 | } |
| 464 | |
| 465 | buffer_splice(&b, in, shdr->sh_offset, shdr->sh_size); |
| 466 | if (check_size(in, shdr->sh_offset, buffer_size(&b), "symtab")) |
| 467 | return -1; |
| 468 | |
| 469 | nsyms = shdr->sh_size / shdr->sh_entsize; |
| 470 | |
| 471 | pelf->syms = calloc(nsyms, sizeof(Elf64_Sym)); |
| 472 | |
| 473 | for (i = 0; i < nsyms; i++) { |
| 474 | sym = &pelf->syms[i]; |
| 475 | |
| 476 | if (bit64) { |
| 477 | sym->st_name = xdr->get32(&b); |
| 478 | sym->st_info = xdr->get8(&b); |
| 479 | sym->st_other = xdr->get8(&b); |
| 480 | sym->st_shndx = xdr->get16(&b); |
| 481 | sym->st_value = xdr->get64(&b); |
| 482 | sym->st_size = xdr->get64(&b); |
| 483 | } else { |
| 484 | sym->st_name = xdr->get32(&b); |
| 485 | sym->st_value = xdr->get32(&b); |
| 486 | sym->st_size = xdr->get32(&b); |
| 487 | sym->st_info = xdr->get8(&b); |
| 488 | sym->st_other = xdr->get8(&b); |
| 489 | sym->st_shndx = xdr->get16(&b); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 496 | int parse_elf(const struct buffer *pinput, struct parsed_elf *pelf, int flags) |
| 497 | { |
| 498 | struct xdr *xdr = &xdr_le; |
| 499 | int bit64 = 0; |
| 500 | struct buffer input; |
| 501 | Elf64_Ehdr *ehdr; |
| 502 | |
| 503 | /* Zero out the parsed elf structure. */ |
| 504 | memset(pelf, 0, sizeof(*pelf)); |
| 505 | |
| 506 | if (!iself(buffer_get(pinput))) { |
Patrick Georgi | 8320b9a | 2017-07-05 13:28:24 +0200 | [diff] [blame] | 507 | DEBUG("The stage file is not in ELF format!\n"); |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 508 | return -1; |
| 509 | } |
| 510 | |
| 511 | buffer_clone(&input, pinput); |
| 512 | ehdr = &pelf->ehdr; |
| 513 | elf_eident(&input, ehdr); |
| 514 | bit64 = ehdr->e_ident[EI_CLASS] == ELFCLASS64; |
| 515 | /* Assume LE unless we are sure otherwise. |
| 516 | * We're not going to take on the task of |
| 517 | * fully validating the ELF file. That way |
| 518 | * lies madness. |
| 519 | */ |
| 520 | if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) |
| 521 | xdr = &xdr_be; |
| 522 | |
| 523 | elf_ehdr(&input, ehdr, xdr, bit64); |
| 524 | |
Aaron Durbin | ccb5ad8 | 2014-03-05 13:57:30 -0600 | [diff] [blame] | 525 | /* Relocation processing requires section header parsing. */ |
| 526 | if (flags & ELF_PARSE_RELOC) |
| 527 | flags |= ELF_PARSE_SHDR; |
| 528 | |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 529 | /* String table processing requires section header parsing. */ |
| 530 | if (flags & ELF_PARSE_STRTAB) |
| 531 | flags |= ELF_PARSE_SHDR; |
| 532 | |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 533 | /* Symbole table processing requires section header parsing. */ |
| 534 | if (flags & ELF_PARSE_SYMTAB) |
| 535 | flags |= ELF_PARSE_SHDR; |
| 536 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 537 | if ((flags & ELF_PARSE_PHDR) && phdr_read(pinput, pelf, xdr, bit64)) |
| 538 | goto fail; |
| 539 | |
| 540 | if ((flags & ELF_PARSE_SHDR) && shdr_read(pinput, pelf, xdr, bit64)) |
| 541 | goto fail; |
| 542 | |
Aaron Durbin | ccb5ad8 | 2014-03-05 13:57:30 -0600 | [diff] [blame] | 543 | if ((flags & ELF_PARSE_RELOC) && reloc_read(pinput, pelf, xdr, bit64)) |
| 544 | goto fail; |
| 545 | |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 546 | if ((flags & ELF_PARSE_STRTAB) && strtab_read(pinput, pelf)) |
| 547 | goto fail; |
| 548 | |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 549 | if ((flags & ELF_PARSE_SYMTAB) && symtab_read(pinput, pelf, xdr, bit64)) |
| 550 | goto fail; |
| 551 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 552 | return 0; |
| 553 | |
| 554 | fail: |
| 555 | parsed_elf_destroy(pelf); |
| 556 | return -1; |
| 557 | } |
| 558 | |
| 559 | void parsed_elf_destroy(struct parsed_elf *pelf) |
| 560 | { |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 561 | Elf64_Half i; |
| 562 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 563 | free(pelf->phdr); |
| 564 | free(pelf->shdr); |
Aaron Durbin | ccb5ad8 | 2014-03-05 13:57:30 -0600 | [diff] [blame] | 565 | if (pelf->relocs != NULL) { |
Aaron Durbin | ccb5ad8 | 2014-03-05 13:57:30 -0600 | [diff] [blame] | 566 | for (i = 0; i < pelf->ehdr.e_shnum; i++) |
| 567 | free(pelf->relocs[i]); |
| 568 | } |
| 569 | free(pelf->relocs); |
Aaron Durbin | c3e6e14 | 2014-03-05 14:33:42 -0600 | [diff] [blame] | 570 | |
| 571 | if (pelf->strtabs != NULL) { |
| 572 | for (i = 0; i < pelf->ehdr.e_shnum; i++) |
| 573 | free(pelf->strtabs[i]); |
| 574 | } |
| 575 | free(pelf->strtabs); |
Aaron Durbin | c078094 | 2014-03-05 16:41:27 -0600 | [diff] [blame] | 576 | free(pelf->syms); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* Get the headers from the buffer. |
| 580 | * Return -1 in the event of an error. |
| 581 | * The section headers are optional; if NULL |
| 582 | * is passed in for pshdr they won't be parsed. |
| 583 | * We don't (yet) make payload parsing optional |
| 584 | * because we've never seen a use case. |
| 585 | */ |
| 586 | int |
| 587 | elf_headers(const struct buffer *pinput, |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 588 | Elf64_Ehdr *ehdr, |
| 589 | Elf64_Phdr **pphdr, |
| 590 | Elf64_Shdr **pshdr) |
| 591 | { |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 592 | struct parsed_elf pelf; |
| 593 | int flags; |
Aaron Durbin | a983cea | 2014-03-04 22:08:05 -0600 | [diff] [blame] | 594 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 595 | flags = ELF_PARSE_PHDR; |
| 596 | |
| 597 | if (pshdr != NULL) |
| 598 | flags |= ELF_PARSE_SHDR; |
| 599 | |
| 600 | if (parse_elf(pinput, &pelf, flags)) |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 601 | return -1; |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 602 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 603 | /* Copy out the parsed elf header. */ |
| 604 | memcpy(ehdr, &pelf.ehdr, sizeof(*ehdr)); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 605 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 606 | *pphdr = calloc(ehdr->e_phnum, sizeof(Elf64_Phdr)); |
| 607 | memcpy(*pphdr, pelf.phdr, ehdr->e_phnum * sizeof(Elf64_Phdr)); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 608 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 609 | if (pshdr != NULL) { |
| 610 | *pshdr = calloc(ehdr->e_shnum, sizeof(Elf64_Shdr)); |
| 611 | memcpy(*pshdr, pelf.shdr, ehdr->e_shnum * sizeof(Elf64_Shdr)); |
| 612 | } |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 613 | |
Aaron Durbin | d0f6165 | 2014-03-05 13:09:55 -0600 | [diff] [blame] | 614 | parsed_elf_destroy(&pelf); |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame] | 615 | |
| 616 | return 0; |
| 617 | } |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 618 | |
| 619 | /* ELF Writing Support |
| 620 | * |
| 621 | * The ELF file is written according to the following layout: |
| 622 | * +------------------+ |
| 623 | * | ELF Header | |
| 624 | * +------------------+ |
| 625 | * | Section Headers | |
| 626 | * +------------------+ |
| 627 | * | Program Headers | |
| 628 | * +------------------+ |
| 629 | * | String table | |
| 630 | * +------------------+ <- 4KiB Aligned |
| 631 | * | Code/Data | |
| 632 | * +------------------+ |
| 633 | */ |
| 634 | |
Aaron Durbin | 4f930c9 | 2015-10-27 16:21:55 -0500 | [diff] [blame] | 635 | void elf_init_eheader(Elf64_Ehdr *ehdr, int machine, int nbits, int endian) |
| 636 | { |
| 637 | memset(ehdr, 0, sizeof(*ehdr)); |
| 638 | ehdr->e_ident[EI_MAG0] = ELFMAG0; |
| 639 | ehdr->e_ident[EI_MAG1] = ELFMAG1; |
| 640 | ehdr->e_ident[EI_MAG2] = ELFMAG2; |
| 641 | ehdr->e_ident[EI_MAG3] = ELFMAG3; |
| 642 | ehdr->e_ident[EI_CLASS] = nbits; |
| 643 | ehdr->e_ident[EI_DATA] = endian; |
| 644 | ehdr->e_ident[EI_VERSION] = EV_CURRENT; |
| 645 | ehdr->e_type = ET_EXEC; |
| 646 | ehdr->e_machine = machine; |
| 647 | ehdr->e_version = EV_CURRENT; |
| 648 | if (nbits == ELFCLASS64) { |
| 649 | ehdr->e_ehsize = sizeof(Elf64_Ehdr); |
| 650 | ehdr->e_phentsize = sizeof(Elf64_Phdr); |
| 651 | ehdr->e_shentsize = sizeof(Elf64_Shdr); |
| 652 | } else { |
| 653 | ehdr->e_ehsize = sizeof(Elf32_Ehdr); |
| 654 | ehdr->e_phentsize = sizeof(Elf32_Phdr); |
| 655 | ehdr->e_shentsize = sizeof(Elf32_Shdr); |
| 656 | } |
| 657 | } |
| 658 | |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 659 | /* Arbitray maximum number of sections. */ |
| 660 | #define MAX_SECTIONS 16 |
| 661 | struct elf_writer_section { |
| 662 | Elf64_Shdr shdr; |
| 663 | struct buffer content; |
| 664 | const char *name; |
| 665 | }; |
| 666 | |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 667 | struct elf_writer_string_table { |
| 668 | size_t next_offset; |
| 669 | size_t max_size; |
| 670 | char *buffer; |
| 671 | }; |
| 672 | |
| 673 | struct elf_writer_sym_table { |
| 674 | size_t max_entries; |
| 675 | size_t num_entries; |
| 676 | Elf64_Sym *syms; |
| 677 | }; |
| 678 | |
| 679 | #define MAX_REL_NAME 32 |
| 680 | struct elf_writer_rel { |
| 681 | size_t num_entries; |
| 682 | size_t max_entries; |
| 683 | Elf64_Rel *rels; |
| 684 | struct elf_writer_section *sec; |
| 685 | char name[MAX_REL_NAME]; |
| 686 | }; |
| 687 | |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 688 | struct elf_writer |
| 689 | { |
| 690 | Elf64_Ehdr ehdr; |
| 691 | struct xdr *xdr; |
| 692 | size_t num_secs; |
| 693 | struct elf_writer_section sections[MAX_SECTIONS]; |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 694 | struct elf_writer_rel rel_sections[MAX_SECTIONS]; |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 695 | Elf64_Phdr *phdrs; |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 696 | struct elf_writer_section *shstrtab_sec; |
| 697 | struct elf_writer_section *strtab_sec; |
| 698 | struct elf_writer_section *symtab_sec; |
| 699 | struct elf_writer_string_table strtab; |
| 700 | struct elf_writer_sym_table symtab; |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 701 | int bit64; |
| 702 | }; |
| 703 | |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 704 | static size_t section_index(struct elf_writer *ew, |
| 705 | struct elf_writer_section *sec) |
| 706 | { |
| 707 | return sec - &ew->sections[0]; |
| 708 | } |
| 709 | |
| 710 | static struct elf_writer_section *last_section(struct elf_writer *ew) |
| 711 | { |
| 712 | return &ew->sections[ew->num_secs - 1]; |
| 713 | } |
| 714 | |
| 715 | static void strtab_init(struct elf_writer *ew, size_t size) |
| 716 | { |
| 717 | struct buffer b; |
| 718 | Elf64_Shdr shdr; |
| 719 | |
| 720 | /* Start adding strings after the initial NUL entry. */ |
| 721 | ew->strtab.next_offset = 1; |
| 722 | ew->strtab.max_size = size; |
| 723 | ew->strtab.buffer = calloc(1, ew->strtab.max_size); |
| 724 | |
| 725 | buffer_init(&b, NULL, ew->strtab.buffer, ew->strtab.max_size); |
| 726 | memset(&shdr, 0, sizeof(shdr)); |
| 727 | shdr.sh_type = SHT_STRTAB; |
| 728 | shdr.sh_addralign = 1; |
| 729 | shdr.sh_size = ew->strtab.max_size; |
| 730 | elf_writer_add_section(ew, &shdr, &b, ".strtab"); |
| 731 | ew->strtab_sec = last_section(ew); |
| 732 | } |
| 733 | |
| 734 | static void symtab_init(struct elf_writer *ew, size_t max_entries) |
| 735 | { |
| 736 | struct buffer b; |
| 737 | Elf64_Shdr shdr; |
| 738 | |
| 739 | memset(&shdr, 0, sizeof(shdr)); |
| 740 | shdr.sh_type = SHT_SYMTAB; |
| 741 | |
| 742 | if (ew->bit64) { |
| 743 | shdr.sh_entsize = sizeof(Elf64_Sym); |
| 744 | shdr.sh_addralign = sizeof(Elf64_Addr); |
| 745 | } else { |
| 746 | shdr.sh_entsize = sizeof(Elf32_Sym); |
| 747 | shdr.sh_addralign = sizeof(Elf32_Addr); |
| 748 | } |
| 749 | |
| 750 | shdr.sh_size = shdr.sh_entsize * max_entries; |
| 751 | |
| 752 | ew->symtab.syms = calloc(max_entries, sizeof(Elf64_Sym)); |
| 753 | ew->symtab.num_entries = 1; |
| 754 | ew->symtab.max_entries = max_entries; |
| 755 | |
| 756 | buffer_init(&b, NULL, ew->symtab.syms, shdr.sh_size); |
| 757 | |
| 758 | elf_writer_add_section(ew, &shdr, &b, ".symtab"); |
| 759 | ew->symtab_sec = last_section(ew); |
| 760 | } |
| 761 | |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 762 | struct elf_writer *elf_writer_init(const Elf64_Ehdr *ehdr) |
| 763 | { |
| 764 | struct elf_writer *ew; |
| 765 | Elf64_Shdr shdr; |
| 766 | struct buffer empty_buffer; |
| 767 | |
| 768 | if (!iself(ehdr)) |
| 769 | return NULL; |
| 770 | |
| 771 | ew = calloc(1, sizeof(*ew)); |
| 772 | |
| 773 | memcpy(&ew->ehdr, ehdr, sizeof(ew->ehdr)); |
| 774 | |
| 775 | ew->bit64 = ew->ehdr.e_ident[EI_CLASS] == ELFCLASS64; |
| 776 | |
| 777 | /* Set the endinan ops. */ |
| 778 | if (ew->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) |
| 779 | ew->xdr = &xdr_be; |
| 780 | else |
| 781 | ew->xdr = &xdr_le; |
| 782 | |
| 783 | /* Reset count and offsets */ |
| 784 | ew->ehdr.e_phoff = 0; |
| 785 | ew->ehdr.e_shoff = 0; |
| 786 | ew->ehdr.e_shnum = 0; |
| 787 | ew->ehdr.e_phnum = 0; |
| 788 | |
| 789 | memset(&empty_buffer, 0, sizeof(empty_buffer)); |
| 790 | memset(&shdr, 0, sizeof(shdr)); |
| 791 | |
| 792 | /* Add SHT_NULL section header. */ |
| 793 | shdr.sh_type = SHT_NULL; |
| 794 | elf_writer_add_section(ew, &shdr, &empty_buffer, NULL); |
| 795 | |
| 796 | /* Add section header string table and maintain reference to it. */ |
| 797 | shdr.sh_type = SHT_STRTAB; |
| 798 | elf_writer_add_section(ew, &shdr, &empty_buffer, ".shstrtab"); |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 799 | ew->shstrtab_sec = last_section(ew); |
| 800 | ew->ehdr.e_shstrndx = section_index(ew, ew->shstrtab_sec); |
| 801 | |
| 802 | /* Add a small string table and symbol table. */ |
| 803 | strtab_init(ew, 4096); |
| 804 | symtab_init(ew, 100); |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 805 | |
| 806 | return ew; |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | * Clean up any internal state represented by ew. Aftewards the elf_writer |
| 811 | * is invalid. |
Furquan Shaikh | b927bec | 2016-08-05 12:04:55 -0700 | [diff] [blame] | 812 | * It is safe to call elf_writer_destroy with ew as NULL. It returns without |
| 813 | * performing any action. |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 814 | */ |
| 815 | void elf_writer_destroy(struct elf_writer *ew) |
| 816 | { |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 817 | int i; |
Furquan Shaikh | b927bec | 2016-08-05 12:04:55 -0700 | [diff] [blame] | 818 | if (ew == NULL) |
| 819 | return; |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 820 | if (ew->phdrs != NULL) |
| 821 | free(ew->phdrs); |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 822 | free(ew->strtab.buffer); |
| 823 | free(ew->symtab.syms); |
| 824 | for (i = 0; i < MAX_SECTIONS; i++) |
| 825 | free(ew->rel_sections[i].rels); |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 826 | free(ew); |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * Add a section to the ELF file. Section type, flags, and memsize are |
| 831 | * maintained from the passed in Elf64_Shdr. The buffer represents the |
| 832 | * content of the section while the name is the name of section itself. |
| 833 | * Returns < 0 on error, 0 on success. |
| 834 | */ |
| 835 | int elf_writer_add_section(struct elf_writer *ew, const Elf64_Shdr *shdr, |
| 836 | struct buffer *contents, const char *name) |
| 837 | { |
| 838 | struct elf_writer_section *newsh; |
| 839 | |
| 840 | if (ew->num_secs == MAX_SECTIONS) |
| 841 | return -1; |
| 842 | |
| 843 | newsh = &ew->sections[ew->num_secs]; |
| 844 | ew->num_secs++; |
| 845 | |
| 846 | memcpy(&newsh->shdr, shdr, sizeof(newsh->shdr)); |
| 847 | newsh->shdr.sh_offset = 0; |
| 848 | |
| 849 | newsh->name = name; |
| 850 | if (contents != NULL) |
| 851 | buffer_clone(&newsh->content, contents); |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static void ehdr_write(struct elf_writer *ew, struct buffer *m) |
| 857 | { |
| 858 | int i; |
| 859 | |
| 860 | for (i = 0; i < EI_NIDENT; i++) |
| 861 | ew->xdr->put8(m, ew->ehdr.e_ident[i]); |
| 862 | ew->xdr->put16(m, ew->ehdr.e_type); |
| 863 | ew->xdr->put16(m, ew->ehdr.e_machine); |
| 864 | ew->xdr->put32(m, ew->ehdr.e_version); |
| 865 | if (ew->bit64) { |
| 866 | ew->xdr->put64(m, ew->ehdr.e_entry); |
| 867 | ew->xdr->put64(m, ew->ehdr.e_phoff); |
| 868 | ew->xdr->put64(m, ew->ehdr.e_shoff); |
| 869 | } else { |
| 870 | ew->xdr->put32(m, ew->ehdr.e_entry); |
| 871 | ew->xdr->put32(m, ew->ehdr.e_phoff); |
| 872 | ew->xdr->put32(m, ew->ehdr.e_shoff); |
| 873 | } |
| 874 | ew->xdr->put32(m, ew->ehdr.e_flags); |
| 875 | ew->xdr->put16(m, ew->ehdr.e_ehsize); |
| 876 | ew->xdr->put16(m, ew->ehdr.e_phentsize); |
| 877 | ew->xdr->put16(m, ew->ehdr.e_phnum); |
| 878 | ew->xdr->put16(m, ew->ehdr.e_shentsize); |
| 879 | ew->xdr->put16(m, ew->ehdr.e_shnum); |
| 880 | ew->xdr->put16(m, ew->ehdr.e_shstrndx); |
| 881 | } |
| 882 | |
| 883 | static void shdr_write(struct elf_writer *ew, size_t n, struct buffer *m) |
| 884 | { |
| 885 | struct xdr *xdr = ew->xdr; |
| 886 | int bit64 = ew->bit64; |
| 887 | struct elf_writer_section *sec = &ew->sections[n]; |
| 888 | Elf64_Shdr *shdr = &sec->shdr; |
| 889 | |
| 890 | xdr->put32(m, shdr->sh_name); |
| 891 | xdr->put32(m, shdr->sh_type); |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 892 | if (bit64) { |
Aaron Durbin | bc349b8 | 2014-08-22 14:05:00 -0500 | [diff] [blame] | 893 | xdr->put64(m, shdr->sh_flags); |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 894 | xdr->put64(m, shdr->sh_addr); |
| 895 | xdr->put64(m, shdr->sh_offset); |
| 896 | xdr->put64(m, shdr->sh_size); |
| 897 | xdr->put32(m, shdr->sh_link); |
| 898 | xdr->put32(m, shdr->sh_info); |
| 899 | xdr->put64(m, shdr->sh_addralign); |
| 900 | xdr->put64(m, shdr->sh_entsize); |
| 901 | } else { |
Aaron Durbin | bc349b8 | 2014-08-22 14:05:00 -0500 | [diff] [blame] | 902 | xdr->put32(m, shdr->sh_flags); |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 903 | xdr->put32(m, shdr->sh_addr); |
| 904 | xdr->put32(m, shdr->sh_offset); |
| 905 | xdr->put32(m, shdr->sh_size); |
| 906 | xdr->put32(m, shdr->sh_link); |
| 907 | xdr->put32(m, shdr->sh_info); |
| 908 | xdr->put32(m, shdr->sh_addralign); |
| 909 | xdr->put32(m, shdr->sh_entsize); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | static void |
| 914 | phdr_write(struct elf_writer *ew, struct buffer *m, Elf64_Phdr *phdr) |
| 915 | { |
| 916 | if (ew->bit64) { |
| 917 | ew->xdr->put32(m, phdr->p_type); |
| 918 | ew->xdr->put32(m, phdr->p_flags); |
| 919 | ew->xdr->put64(m, phdr->p_offset); |
| 920 | ew->xdr->put64(m, phdr->p_vaddr); |
| 921 | ew->xdr->put64(m, phdr->p_paddr); |
| 922 | ew->xdr->put64(m, phdr->p_filesz); |
| 923 | ew->xdr->put64(m, phdr->p_memsz); |
| 924 | ew->xdr->put64(m, phdr->p_align); |
| 925 | } else { |
| 926 | ew->xdr->put32(m, phdr->p_type); |
| 927 | ew->xdr->put32(m, phdr->p_offset); |
| 928 | ew->xdr->put32(m, phdr->p_vaddr); |
| 929 | ew->xdr->put32(m, phdr->p_paddr); |
| 930 | ew->xdr->put32(m, phdr->p_filesz); |
| 931 | ew->xdr->put32(m, phdr->p_memsz); |
| 932 | ew->xdr->put32(m, phdr->p_flags); |
| 933 | ew->xdr->put32(m, phdr->p_align); |
| 934 | } |
| 935 | |
| 936 | } |
| 937 | |
Aaron Durbin | 8e982ea | 2015-10-28 10:09:07 -0500 | [diff] [blame] | 938 | static int section_consecutive(struct elf_writer *ew, Elf64_Half secidx) |
| 939 | { |
| 940 | Elf64_Half i; |
| 941 | struct elf_writer_section *prev_alloc = NULL; |
| 942 | |
| 943 | if (secidx == 0) |
| 944 | return 0; |
| 945 | |
| 946 | for (i = 0; i < secidx; i++) { |
| 947 | if (ew->sections[i].shdr.sh_flags & SHF_ALLOC) |
| 948 | prev_alloc = &ew->sections[i]; |
| 949 | } |
| 950 | |
| 951 | if (prev_alloc == NULL) |
| 952 | return 0; |
| 953 | |
| 954 | if (prev_alloc->shdr.sh_addr + prev_alloc->shdr.sh_size == |
| 955 | ew->sections[secidx].shdr.sh_addr) |
| 956 | return 1; |
| 957 | |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | static void write_phdrs(struct elf_writer *ew, struct buffer *phdrs) |
| 962 | { |
| 963 | Elf64_Half i; |
| 964 | Elf64_Phdr phdr; |
| 965 | size_t num_written = 0; |
Antonello Dettori | 3bded7d | 2016-06-16 13:40:24 +0200 | [diff] [blame] | 966 | size_t num_needs_write = 0; |
Aaron Durbin | 8e982ea | 2015-10-28 10:09:07 -0500 | [diff] [blame] | 967 | |
| 968 | for (i = 0; i < ew->num_secs; i++) { |
| 969 | struct elf_writer_section *sec = &ew->sections[i]; |
| 970 | |
| 971 | if (!(sec->shdr.sh_flags & SHF_ALLOC)) |
| 972 | continue; |
| 973 | |
| 974 | if (!section_consecutive(ew, i)) { |
| 975 | /* Write out previously set phdr. */ |
Antonello Dettori | 3bded7d | 2016-06-16 13:40:24 +0200 | [diff] [blame] | 976 | if (num_needs_write != num_written) { |
Aaron Durbin | 8e982ea | 2015-10-28 10:09:07 -0500 | [diff] [blame] | 977 | phdr_write(ew, phdrs, &phdr); |
| 978 | num_written++; |
| 979 | } |
| 980 | phdr.p_type = PT_LOAD; |
| 981 | phdr.p_offset = sec->shdr.sh_offset; |
| 982 | phdr.p_vaddr = sec->shdr.sh_addr; |
| 983 | phdr.p_paddr = sec->shdr.sh_addr; |
| 984 | phdr.p_filesz = buffer_size(&sec->content); |
| 985 | phdr.p_memsz = sec->shdr.sh_size; |
| 986 | phdr.p_flags = 0; |
| 987 | if (sec->shdr.sh_flags & SHF_EXECINSTR) |
| 988 | phdr.p_flags |= PF_X | PF_R; |
| 989 | if (sec->shdr.sh_flags & SHF_WRITE) |
| 990 | phdr.p_flags |= PF_W; |
| 991 | phdr.p_align = sec->shdr.sh_addralign; |
Antonello Dettori | 3bded7d | 2016-06-16 13:40:24 +0200 | [diff] [blame] | 992 | num_needs_write++; |
| 993 | |
Aaron Durbin | 8e982ea | 2015-10-28 10:09:07 -0500 | [diff] [blame] | 994 | } else { |
| 995 | /* Accumulate file size and memsize. The assumption |
| 996 | * is that each section is either NOBITS or full |
| 997 | * (sh_size == file size). This is standard in that |
| 998 | * an ELF section doesn't have a file size component. */ |
| 999 | if (sec->shdr.sh_flags & SHF_EXECINSTR) |
| 1000 | phdr.p_flags |= PF_X | PF_R; |
| 1001 | if (sec->shdr.sh_flags & SHF_WRITE) |
| 1002 | phdr.p_flags |= PF_W; |
| 1003 | phdr.p_filesz += buffer_size(&sec->content); |
| 1004 | phdr.p_memsz += sec->shdr.sh_size; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /* Write out the last phdr. */ |
Antonello Dettori | 3bded7d | 2016-06-16 13:40:24 +0200 | [diff] [blame] | 1009 | if (num_needs_write != num_written) { |
Aaron Durbin | 8e982ea | 2015-10-28 10:09:07 -0500 | [diff] [blame] | 1010 | phdr_write(ew, phdrs, &phdr); |
Antonello Dettori | 3bded7d | 2016-06-16 13:40:24 +0200 | [diff] [blame] | 1011 | num_written++; |
| 1012 | } |
| 1013 | assert(num_written == ew->ehdr.e_phnum); |
Aaron Durbin | 8e982ea | 2015-10-28 10:09:07 -0500 | [diff] [blame] | 1014 | } |
| 1015 | |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1016 | static void fixup_symbol_table(struct elf_writer *ew) |
| 1017 | { |
| 1018 | struct elf_writer_section *sec = ew->symtab_sec; |
| 1019 | |
| 1020 | /* If there is only the NULL section, mark section as inactive. */ |
| 1021 | if (ew->symtab.num_entries == 1) { |
| 1022 | sec->shdr.sh_type = SHT_NULL; |
| 1023 | sec->shdr.sh_size = 0; |
| 1024 | } else { |
| 1025 | size_t i; |
| 1026 | struct buffer wr; |
| 1027 | |
| 1028 | buffer_clone(&wr, &sec->content); |
| 1029 | /* To appease xdr. */ |
| 1030 | buffer_set_size(&wr, 0); |
| 1031 | for (i = 0; i < ew->symtab.num_entries; i++) { |
| 1032 | /* Create local copy as were over-writing backing |
| 1033 | * store of the symbol. */ |
| 1034 | Elf64_Sym sym = ew->symtab.syms[i]; |
| 1035 | if (ew->bit64) { |
| 1036 | ew->xdr->put32(&wr, sym.st_name); |
| 1037 | ew->xdr->put8(&wr, sym.st_info); |
| 1038 | ew->xdr->put8(&wr, sym.st_other); |
| 1039 | ew->xdr->put16(&wr, sym.st_shndx); |
| 1040 | ew->xdr->put64(&wr, sym.st_value); |
| 1041 | ew->xdr->put64(&wr, sym.st_size); |
| 1042 | } else { |
| 1043 | ew->xdr->put32(&wr, sym.st_name); |
| 1044 | ew->xdr->put32(&wr, sym.st_value); |
| 1045 | ew->xdr->put32(&wr, sym.st_size); |
| 1046 | ew->xdr->put8(&wr, sym.st_info); |
| 1047 | ew->xdr->put8(&wr, sym.st_other); |
| 1048 | ew->xdr->put16(&wr, sym.st_shndx); |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | /* Update section size. */ |
| 1053 | sec->shdr.sh_size = sec->shdr.sh_entsize; |
| 1054 | sec->shdr.sh_size *= ew->symtab.num_entries; |
| 1055 | |
| 1056 | /* Fix up sh_link to point to string table. */ |
| 1057 | sec->shdr.sh_link = section_index(ew, ew->strtab_sec); |
| 1058 | /* sh_info is supposed to be 1 greater than symbol table |
| 1059 | * index of last local binding. Just use max symbols. */ |
| 1060 | sec->shdr.sh_info = ew->symtab.num_entries; |
| 1061 | } |
| 1062 | |
| 1063 | buffer_set_size(&sec->content, sec->shdr.sh_size); |
| 1064 | } |
| 1065 | |
| 1066 | static void fixup_relocations(struct elf_writer *ew) |
| 1067 | { |
| 1068 | int i; |
| 1069 | Elf64_Xword type; |
| 1070 | |
| 1071 | switch (ew->ehdr.e_machine) { |
| 1072 | case EM_386: |
| 1073 | type = R_386_32; |
| 1074 | break; |
Patrick Rudolph | 565bebe | 2018-11-26 15:54:21 +0100 | [diff] [blame^] | 1075 | case EM_X86_64: |
| 1076 | type = R_AMD64_64; |
| 1077 | break; |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1078 | case EM_ARM: |
| 1079 | type = R_ARM_ABS32; |
| 1080 | break; |
| 1081 | case EM_AARCH64: |
| 1082 | type = R_AARCH64_ABS64; |
| 1083 | break; |
| 1084 | case EM_MIPS: |
| 1085 | type = R_MIPS_32; |
| 1086 | break; |
| 1087 | case EM_RISCV: |
| 1088 | type = R_RISCV_32; |
| 1089 | break; |
Ronald G. Minnich | ed4aa04 | 2015-12-11 18:19:52 +0000 | [diff] [blame] | 1090 | case EM_PPC64: |
| 1091 | type = R_PPC64_ADDR32; |
| 1092 | break; |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1093 | default: |
| 1094 | ERROR("Unable to handle relocations for e_machine %x\n", |
| 1095 | ew->ehdr.e_machine); |
| 1096 | return; |
| 1097 | } |
| 1098 | |
| 1099 | for (i = 0; i < MAX_SECTIONS; i++) { |
| 1100 | struct elf_writer_rel *rel_sec = &ew->rel_sections[i]; |
| 1101 | struct elf_writer_section *sec = rel_sec->sec; |
| 1102 | struct buffer writer; |
| 1103 | size_t j; |
| 1104 | |
| 1105 | if (sec == NULL) |
| 1106 | continue; |
| 1107 | |
| 1108 | /* Update section header size as well as content size. */ |
| 1109 | buffer_init(&sec->content, sec->content.name, rel_sec->rels, |
| 1110 | rel_sec->num_entries * sec->shdr.sh_entsize); |
| 1111 | sec->shdr.sh_size = buffer_size(&sec->content); |
| 1112 | buffer_clone(&writer, &sec->content); |
| 1113 | /* To make xdr happy. */ |
| 1114 | buffer_set_size(&writer, 0); |
| 1115 | |
| 1116 | for (j = 0; j < ew->rel_sections[i].num_entries; j++) { |
| 1117 | /* Make copy as we're overwriting backing store. */ |
| 1118 | Elf64_Rel rel = rel_sec->rels[j]; |
| 1119 | rel.r_info = ELF64_R_INFO(ELF64_R_SYM(rel.r_info), |
| 1120 | ELF64_R_TYPE(type)); |
| 1121 | |
| 1122 | if (ew->bit64) { |
| 1123 | ew->xdr->put64(&writer, rel.r_offset); |
| 1124 | ew->xdr->put64(&writer, rel.r_info); |
| 1125 | } else { |
| 1126 | Elf32_Rel rel32; |
| 1127 | rel32.r_offset = rel.r_offset; |
| 1128 | rel32.r_info = |
| 1129 | ELF32_R_INFO(ELF64_R_SYM(rel.r_info), |
| 1130 | ELF64_R_TYPE(rel.r_info)); |
| 1131 | ew->xdr->put32(&writer, rel32.r_offset); |
| 1132 | ew->xdr->put32(&writer, rel32.r_info); |
| 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 1138 | /* |
| 1139 | * Serialize the ELF file to the output buffer. Return < 0 on error, |
| 1140 | * 0 on success. |
| 1141 | */ |
| 1142 | int elf_writer_serialize(struct elf_writer *ew, struct buffer *out) |
| 1143 | { |
| 1144 | Elf64_Half i; |
| 1145 | Elf64_Xword metadata_size; |
| 1146 | Elf64_Xword program_size; |
| 1147 | Elf64_Off shstroffset; |
| 1148 | size_t shstrlen; |
| 1149 | struct buffer metadata; |
| 1150 | struct buffer phdrs; |
| 1151 | struct buffer data; |
| 1152 | struct buffer *strtab; |
| 1153 | |
| 1154 | INFO("Writing %zu sections.\n", ew->num_secs); |
| 1155 | |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1156 | /* Perform any necessary work for special sections. */ |
| 1157 | fixup_symbol_table(ew); |
| 1158 | fixup_relocations(ew); |
| 1159 | |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 1160 | /* Determine size of sections to be written. */ |
| 1161 | program_size = 0; |
| 1162 | /* Start with 1 byte for first byte of section header string table. */ |
| 1163 | shstrlen = 1; |
| 1164 | for (i = 0; i < ew->num_secs; i++) { |
| 1165 | struct elf_writer_section *sec = &ew->sections[i]; |
| 1166 | |
Aaron Durbin | 8e982ea | 2015-10-28 10:09:07 -0500 | [diff] [blame] | 1167 | if (sec->shdr.sh_flags & SHF_ALLOC) { |
| 1168 | if (!section_consecutive(ew, i)) |
| 1169 | ew->ehdr.e_phnum++; |
| 1170 | } |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 1171 | |
| 1172 | program_size += buffer_size(&sec->content); |
| 1173 | |
| 1174 | /* Keep track of the length sections' names. */ |
| 1175 | if (sec->name != NULL) { |
| 1176 | sec->shdr.sh_name = shstrlen; |
| 1177 | shstrlen += strlen(sec->name) + 1; |
| 1178 | } |
| 1179 | } |
| 1180 | ew->ehdr.e_shnum = ew->num_secs; |
| 1181 | metadata_size = 0; |
| 1182 | metadata_size += ew->ehdr.e_ehsize; |
| 1183 | metadata_size += ew->ehdr.e_shnum * ew->ehdr.e_shentsize; |
| 1184 | metadata_size += ew->ehdr.e_phnum * ew->ehdr.e_phentsize; |
| 1185 | shstroffset = metadata_size; |
| 1186 | /* Align up section header string size and metadata size to 4KiB */ |
| 1187 | metadata_size = ALIGN(metadata_size + shstrlen, 4096); |
| 1188 | |
| 1189 | if (buffer_create(out, metadata_size + program_size, "elfout")) { |
| 1190 | ERROR("Could not create output buffer for ELF.\n"); |
| 1191 | return -1; |
| 1192 | } |
| 1193 | |
| 1194 | INFO("Created %zu output buffer for ELF file.\n", buffer_size(out)); |
| 1195 | |
| 1196 | /* |
| 1197 | * Write out ELF header. Section headers come right after ELF header |
| 1198 | * followed by the program headers. Buffers need to be created first |
| 1199 | * to do the writing. |
| 1200 | */ |
| 1201 | ew->ehdr.e_shoff = ew->ehdr.e_ehsize; |
| 1202 | ew->ehdr.e_phoff = ew->ehdr.e_shoff + |
| 1203 | ew->ehdr.e_shnum * ew->ehdr.e_shentsize; |
| 1204 | |
| 1205 | buffer_splice(&metadata, out, 0, metadata_size); |
| 1206 | buffer_splice(&phdrs, out, ew->ehdr.e_phoff, |
| 1207 | ew->ehdr.e_phnum * ew->ehdr.e_phentsize); |
| 1208 | buffer_splice(&data, out, metadata_size, program_size); |
| 1209 | /* Set up the section header string table contents. */ |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1210 | strtab = &ew->shstrtab_sec->content; |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 1211 | buffer_splice(strtab, out, shstroffset, shstrlen); |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1212 | ew->shstrtab_sec->shdr.sh_size = shstrlen; |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 1213 | |
| 1214 | /* Reset current locations. */ |
| 1215 | buffer_set_size(&metadata, 0); |
| 1216 | buffer_set_size(&data, 0); |
| 1217 | buffer_set_size(&phdrs, 0); |
| 1218 | buffer_set_size(strtab, 0); |
| 1219 | |
| 1220 | /* ELF Header */ |
| 1221 | ehdr_write(ew, &metadata); |
| 1222 | |
| 1223 | /* Write out section headers, section strings, section content, and |
| 1224 | * program headers. */ |
| 1225 | ew->xdr->put8(strtab, 0); |
| 1226 | for (i = 0; i < ew->num_secs; i++) { |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 1227 | struct elf_writer_section *sec = &ew->sections[i]; |
| 1228 | |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1229 | /* Update section offsets. Be sure to not update SHN_UNDEF. */ |
| 1230 | if (sec == ew->shstrtab_sec) |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 1231 | sec->shdr.sh_offset = shstroffset; |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1232 | else if (i != SHN_UNDEF) |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 1233 | sec->shdr.sh_offset = buffer_size(&data) + |
| 1234 | metadata_size; |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1235 | |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 1236 | shdr_write(ew, i, &metadata); |
| 1237 | |
| 1238 | /* Add section name to string table. */ |
| 1239 | if (sec->name != NULL) |
| 1240 | bputs(strtab, sec->name, strlen(sec->name) + 1); |
| 1241 | |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1242 | /* Output section data for all sections but SHN_UNDEF and |
| 1243 | * section header string table. */ |
| 1244 | if (i != SHN_UNDEF && sec != ew->shstrtab_sec) |
| 1245 | bputs(&data, buffer_get(&sec->content), |
| 1246 | buffer_size(&sec->content)); |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 1247 | } |
| 1248 | |
Aaron Durbin | 8e982ea | 2015-10-28 10:09:07 -0500 | [diff] [blame] | 1249 | write_phdrs(ew, &phdrs); |
| 1250 | |
Aaron Durbin | 36be813 | 2014-03-11 11:48:56 -0500 | [diff] [blame] | 1251 | return 0; |
| 1252 | } |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1253 | |
| 1254 | /* Add a string to the string table returning index on success, < 0 on error. */ |
| 1255 | static int elf_writer_add_string(struct elf_writer *ew, const char *new) |
| 1256 | { |
| 1257 | size_t current_offset; |
| 1258 | size_t new_len; |
| 1259 | |
| 1260 | for (current_offset = 0; current_offset < ew->strtab.next_offset; ) { |
| 1261 | const char *str = ew->strtab.buffer + current_offset; |
| 1262 | size_t len = strlen(str) + 1; |
| 1263 | |
| 1264 | if (!strcmp(str, new)) |
| 1265 | return current_offset; |
| 1266 | current_offset += len; |
| 1267 | } |
| 1268 | |
| 1269 | new_len = strlen(new) + 1; |
| 1270 | |
| 1271 | if (current_offset + new_len > ew->strtab.max_size) { |
| 1272 | ERROR("No space for string in .strtab.\n"); |
| 1273 | return -1; |
| 1274 | } |
| 1275 | |
| 1276 | memcpy(ew->strtab.buffer + current_offset, new, new_len); |
| 1277 | ew->strtab.next_offset = current_offset + new_len; |
| 1278 | |
| 1279 | return current_offset; |
| 1280 | } |
| 1281 | |
| 1282 | static int elf_writer_section_index(struct elf_writer *ew, const char *name) |
| 1283 | { |
| 1284 | size_t i; |
| 1285 | |
| 1286 | for (i = 0; i < ew->num_secs; i++) { |
| 1287 | if (ew->sections[i].name == NULL) |
| 1288 | continue; |
| 1289 | if (!strcmp(ew->sections[i].name, name)) |
| 1290 | return i; |
| 1291 | } |
| 1292 | |
| 1293 | ERROR("ELF Section not found: %s\n", name); |
| 1294 | |
| 1295 | return -1; |
| 1296 | } |
| 1297 | |
| 1298 | int elf_writer_add_symbol(struct elf_writer *ew, const char *name, |
| 1299 | const char *section_name, |
| 1300 | Elf64_Addr value, Elf64_Word size, |
| 1301 | int binding, int type) |
| 1302 | { |
Patrick Georgi | 172a14d | 2015-10-31 01:35:20 +0100 | [diff] [blame] | 1303 | int i; |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1304 | Elf64_Sym sym = { |
| 1305 | .st_value = value, |
| 1306 | .st_size = size, |
| 1307 | .st_info = ELF64_ST_INFO(binding, type), |
| 1308 | }; |
| 1309 | |
| 1310 | if (ew->symtab.max_entries == ew->symtab.num_entries) { |
| 1311 | ERROR("No more symbol entries left.\n"); |
| 1312 | return -1; |
| 1313 | } |
| 1314 | |
Patrick Georgi | 172a14d | 2015-10-31 01:35:20 +0100 | [diff] [blame] | 1315 | i = elf_writer_add_string(ew, name); |
| 1316 | if (i < 0) |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1317 | return -1; |
Patrick Georgi | 172a14d | 2015-10-31 01:35:20 +0100 | [diff] [blame] | 1318 | sym.st_name = i; |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1319 | |
Patrick Georgi | 172a14d | 2015-10-31 01:35:20 +0100 | [diff] [blame] | 1320 | i = elf_writer_section_index(ew, section_name); |
| 1321 | if (i < 0) |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1322 | return -1; |
Patrick Georgi | 172a14d | 2015-10-31 01:35:20 +0100 | [diff] [blame] | 1323 | sym.st_shndx = i; |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1324 | |
| 1325 | ew->symtab.syms[ew->symtab.num_entries++] = sym; |
| 1326 | |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | static int elf_sym_index(struct elf_writer *ew, const char *sym) |
| 1331 | { |
Patrick Georgi | 172a14d | 2015-10-31 01:35:20 +0100 | [diff] [blame] | 1332 | int j; |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1333 | size_t i; |
| 1334 | Elf64_Word st_name; |
| 1335 | |
| 1336 | /* Determine index of symbol in the string table. */ |
Patrick Georgi | 172a14d | 2015-10-31 01:35:20 +0100 | [diff] [blame] | 1337 | j = elf_writer_add_string(ew, sym); |
| 1338 | if (j < 0) |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1339 | return -1; |
| 1340 | |
Patrick Georgi | 172a14d | 2015-10-31 01:35:20 +0100 | [diff] [blame] | 1341 | st_name = j; |
Aaron Durbin | cedcb88 | 2015-10-28 11:26:40 -0500 | [diff] [blame] | 1342 | |
| 1343 | for (i = 0; i < ew->symtab.num_entries; i++) |
| 1344 | if (ew->symtab.syms[i].st_name == st_name) |
| 1345 | return i; |
| 1346 | |
| 1347 | return -1; |
| 1348 | } |
| 1349 | |
| 1350 | static struct elf_writer_rel *rel_section(struct elf_writer *ew, |
| 1351 | const Elf64_Rel *r) |
| 1352 | { |
| 1353 | Elf64_Sym *sym; |
| 1354 | struct elf_writer_rel *rel; |
| 1355 | Elf64_Shdr shdr; |
| 1356 | struct buffer b; |
| 1357 | |
| 1358 | sym = &ew->symtab.syms[ELF64_R_SYM(r->r_info)]; |
| 1359 | |
| 1360 | /* Determine if section has been initialized yet. */ |
| 1361 | rel = &ew->rel_sections[sym->st_shndx]; |
| 1362 | if (rel->sec != NULL) |
| 1363 | return rel; |
| 1364 | |
| 1365 | memset(&shdr, 0, sizeof(shdr)); |
| 1366 | shdr.sh_type = SHT_REL; |
| 1367 | shdr.sh_link = section_index(ew, ew->symtab_sec); |
| 1368 | shdr.sh_info = sym->st_shndx; |
| 1369 | |
| 1370 | if (ew->bit64) { |
| 1371 | shdr.sh_addralign = sizeof(Elf64_Addr); |
| 1372 | shdr.sh_entsize = sizeof(Elf64_Rel); |
| 1373 | } else { |
| 1374 | shdr.sh_addralign = sizeof(Elf32_Addr); |
| 1375 | shdr.sh_entsize = sizeof(Elf32_Rel); |
| 1376 | } |
| 1377 | |
| 1378 | if ((strlen(".rel") + strlen(ew->sections[sym->st_shndx].name) + 1) > |
| 1379 | MAX_REL_NAME) { |
| 1380 | ERROR("Rel Section name won't fit\n"); |
| 1381 | return NULL; |
| 1382 | } |
| 1383 | |
| 1384 | strcat(rel->name, ".rel"); |
| 1385 | strcat(rel->name, ew->sections[sym->st_shndx].name); |
| 1386 | buffer_init(&b, rel->name, NULL, 0); |
| 1387 | |
| 1388 | elf_writer_add_section(ew, &shdr, &b, rel->name); |
| 1389 | rel->sec = last_section(ew); |
| 1390 | |
| 1391 | return rel; |
| 1392 | } |
| 1393 | |
| 1394 | static int add_rel(struct elf_writer_rel *rel_sec, const Elf64_Rel *rel) |
| 1395 | { |
| 1396 | if (rel_sec->num_entries == rel_sec->max_entries) { |
| 1397 | size_t num = rel_sec->max_entries * 2; |
| 1398 | Elf64_Rel *old_rels; |
| 1399 | |
| 1400 | if (num == 0) |
| 1401 | num = 128; |
| 1402 | |
| 1403 | old_rels = rel_sec->rels; |
| 1404 | rel_sec->rels = calloc(num, sizeof(Elf64_Rel)); |
| 1405 | |
| 1406 | memcpy(rel_sec->rels, old_rels, |
| 1407 | rel_sec->num_entries * sizeof(Elf64_Rel)); |
| 1408 | free(old_rels); |
| 1409 | |
| 1410 | rel_sec->max_entries = num; |
| 1411 | } |
| 1412 | |
| 1413 | rel_sec->rels[rel_sec->num_entries] = *rel; |
| 1414 | rel_sec->num_entries++; |
| 1415 | |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | int elf_writer_add_rel(struct elf_writer *ew, const char *sym, Elf64_Addr addr) |
| 1420 | { |
| 1421 | Elf64_Rel rel; |
| 1422 | Elf64_Xword sym_info; |
| 1423 | int sym_index; |
| 1424 | struct elf_writer_rel *rel_sec; |
| 1425 | |
| 1426 | sym_index = elf_sym_index(ew, sym); |
| 1427 | |
| 1428 | if (sym_index < 0) { |
| 1429 | ERROR("Unable to locate symbol: %s\n", sym); |
| 1430 | return -1; |
| 1431 | } |
| 1432 | |
| 1433 | sym_info = sym_index; |
| 1434 | |
| 1435 | /* The relocation type will get fixed prior to serialization. */ |
| 1436 | rel.r_offset = addr; |
| 1437 | rel.r_info = ELF64_R_INFO(sym_info, 0); |
| 1438 | |
| 1439 | rel_sec = rel_section(ew, &rel); |
| 1440 | |
| 1441 | if (rel_sec == NULL) |
| 1442 | return -1; |
| 1443 | |
| 1444 | return add_rel(rel_sec, &rel); |
| 1445 | } |
Aaron Durbin | facf149 | 2017-12-18 14:50:22 -0700 | [diff] [blame] | 1446 | |
| 1447 | int elf_program_file_size(const struct buffer *input, size_t *file_size) |
| 1448 | { |
| 1449 | Elf64_Ehdr ehdr; |
| 1450 | Elf64_Phdr *phdr; |
| 1451 | int i; |
| 1452 | size_t loadable_file_size = 0; |
| 1453 | |
| 1454 | if (elf_headers(input, &ehdr, &phdr, NULL)) |
| 1455 | return -1; |
| 1456 | |
| 1457 | for (i = 0; i < ehdr.e_phnum; i++) { |
| 1458 | if (phdr[i].p_type != PT_LOAD) |
| 1459 | continue; |
| 1460 | loadable_file_size += phdr[i].p_filesz; |
| 1461 | } |
| 1462 | |
| 1463 | *file_size = loadable_file_size; |
| 1464 | |
| 1465 | free(phdr); |
| 1466 | |
| 1467 | return 0; |
| 1468 | } |