1a85fe12eSEd Maste /*- 2a85fe12eSEd Maste * Copyright (c) 2010,2011 Kai Wang 3a85fe12eSEd Maste * All rights reserved. 4a85fe12eSEd Maste * 5a85fe12eSEd Maste * Redistribution and use in source and binary forms, with or without 6a85fe12eSEd Maste * modification, are permitted provided that the following conditions 7a85fe12eSEd Maste * are met: 8a85fe12eSEd Maste * 1. Redistributions of source code must retain the above copyright 9a85fe12eSEd Maste * notice, this list of conditions and the following disclaimer. 10a85fe12eSEd Maste * 2. Redistributions in binary form must reproduce the above copyright 11a85fe12eSEd Maste * notice, this list of conditions and the following disclaimer in the 12a85fe12eSEd Maste * documentation and/or other materials provided with the distribution. 13a85fe12eSEd Maste * 14a85fe12eSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15a85fe12eSEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16a85fe12eSEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17a85fe12eSEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18a85fe12eSEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19a85fe12eSEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20a85fe12eSEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21a85fe12eSEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22a85fe12eSEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23a85fe12eSEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24a85fe12eSEd Maste * SUCH DAMAGE. 25a85fe12eSEd Maste */ 26a85fe12eSEd Maste 27a85fe12eSEd Maste #include <sys/param.h> 28a85fe12eSEd Maste #include <sys/stat.h> 29e5c4075fSEd Maste #include <ctype.h> 30a85fe12eSEd Maste #include <err.h> 31a85fe12eSEd Maste #include <gelf.h> 32a85fe12eSEd Maste #include <stdio.h> 33a85fe12eSEd Maste #include <stdlib.h> 34a85fe12eSEd Maste #include <string.h> 35a85fe12eSEd Maste #include <unistd.h> 36a85fe12eSEd Maste 37a85fe12eSEd Maste #include "elfcopy.h" 38a85fe12eSEd Maste 39715d1396SEd Maste ELFTC_VCSID("$Id: binary.c 3611 2018-04-16 21:35:18Z jkoshy $"); 40a85fe12eSEd Maste 41a85fe12eSEd Maste /* 42a85fe12eSEd Maste * Convert ELF object to `binary'. Sections with SHF_ALLOC flag set 43a85fe12eSEd Maste * are copied to the result binary. The relative offsets for each section 44a85fe12eSEd Maste * are retained, so the result binary file might contain "holes". 45a85fe12eSEd Maste */ 46a85fe12eSEd Maste void 47a85fe12eSEd Maste create_binary(int ifd, int ofd) 48a85fe12eSEd Maste { 49a85fe12eSEd Maste Elf *e; 50a85fe12eSEd Maste Elf_Scn *scn; 51a85fe12eSEd Maste Elf_Data *d; 52*0705bb9dSAleksandr Rybalko Elf64_Addr baseaddr; 53a85fe12eSEd Maste GElf_Shdr sh; 54*0705bb9dSAleksandr Rybalko off_t baseoff, off; 55a85fe12eSEd Maste int elferr; 56a85fe12eSEd Maste 57a85fe12eSEd Maste if ((e = elf_begin(ifd, ELF_C_READ, NULL)) == NULL) 58a85fe12eSEd Maste errx(EXIT_FAILURE, "elf_begin() failed: %s", 59a85fe12eSEd Maste elf_errmsg(-1)); 60a85fe12eSEd Maste 61*0705bb9dSAleksandr Rybalko baseoff = 0; 62*0705bb9dSAleksandr Rybalko if (lseek(ofd, baseoff, SEEK_SET) < 0) 63a85fe12eSEd Maste err(EXIT_FAILURE, "lseek failed"); 64a85fe12eSEd Maste 65a85fe12eSEd Maste /* 66a85fe12eSEd Maste * Find base offset in the first iteration. 67a85fe12eSEd Maste */ 68*0705bb9dSAleksandr Rybalko baseoff = -1; 69a85fe12eSEd Maste scn = NULL; 70a85fe12eSEd Maste while ((scn = elf_nextscn(e, scn)) != NULL) { 71a85fe12eSEd Maste if (gelf_getshdr(scn, &sh) == NULL) { 72a85fe12eSEd Maste warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 73a85fe12eSEd Maste (void) elf_errno(); 74a85fe12eSEd Maste continue; 75a85fe12eSEd Maste } 76a85fe12eSEd Maste if ((sh.sh_flags & SHF_ALLOC) == 0 || 77a85fe12eSEd Maste sh.sh_type == SHT_NOBITS || 78a85fe12eSEd Maste sh.sh_size == 0) 79a85fe12eSEd Maste continue; 80*0705bb9dSAleksandr Rybalko if (baseoff == -1 || (off_t) sh.sh_offset < baseoff) { 81*0705bb9dSAleksandr Rybalko baseoff = sh.sh_offset; 82*0705bb9dSAleksandr Rybalko baseaddr = sh.sh_addr; 83*0705bb9dSAleksandr Rybalko } 84a85fe12eSEd Maste } 85a85fe12eSEd Maste elferr = elf_errno(); 86a85fe12eSEd Maste if (elferr != 0) 87a85fe12eSEd Maste warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 88a85fe12eSEd Maste 89*0705bb9dSAleksandr Rybalko if (baseoff == -1) 90a85fe12eSEd Maste return; 91a85fe12eSEd Maste 92a85fe12eSEd Maste /* 93a85fe12eSEd Maste * Write out sections in the second iteration. 94a85fe12eSEd Maste */ 95a85fe12eSEd Maste scn = NULL; 96a85fe12eSEd Maste while ((scn = elf_nextscn(e, scn)) != NULL) { 97a85fe12eSEd Maste if (gelf_getshdr(scn, &sh) == NULL) { 98a85fe12eSEd Maste warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 99a85fe12eSEd Maste (void) elf_errno(); 100a85fe12eSEd Maste continue; 101a85fe12eSEd Maste } 102a85fe12eSEd Maste if ((sh.sh_flags & SHF_ALLOC) == 0 || 103a85fe12eSEd Maste sh.sh_type == SHT_NOBITS || 104a85fe12eSEd Maste sh.sh_size == 0) 105a85fe12eSEd Maste continue; 106a85fe12eSEd Maste (void) elf_errno(); 107aadb6884SEd Maste if ((d = elf_rawdata(scn, NULL)) == NULL) { 108a85fe12eSEd Maste elferr = elf_errno(); 109a85fe12eSEd Maste if (elferr != 0) 110aadb6884SEd Maste warnx("elf_rawdata failed: %s", elf_errmsg(-1)); 111a85fe12eSEd Maste continue; 112a85fe12eSEd Maste } 113a85fe12eSEd Maste if (d->d_buf == NULL || d->d_size == 0) 114a85fe12eSEd Maste continue; 115a85fe12eSEd Maste 116*0705bb9dSAleksandr Rybalko /* lseek to section offset relative to `baseaddr'. */ 117*0705bb9dSAleksandr Rybalko off = sh.sh_addr - baseaddr; 118a85fe12eSEd Maste if (lseek(ofd, off, SEEK_SET) < 0) 119a85fe12eSEd Maste err(EXIT_FAILURE, "lseek failed"); 120a85fe12eSEd Maste 121a85fe12eSEd Maste /* Write out section contents. */ 122a85fe12eSEd Maste if (write(ofd, d->d_buf, d->d_size) != (ssize_t) d->d_size) 123a85fe12eSEd Maste err(EXIT_FAILURE, "write failed"); 124a85fe12eSEd Maste } 125a85fe12eSEd Maste elferr = elf_errno(); 126a85fe12eSEd Maste if (elferr != 0) 127a85fe12eSEd Maste warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 128a85fe12eSEd Maste } 129a85fe12eSEd Maste 130a85fe12eSEd Maste #define _SYMBOL_NAMSZ 1024 131a85fe12eSEd Maste 132a85fe12eSEd Maste /* 133a85fe12eSEd Maste * Convert `binary' to ELF object. The input `binary' is converted to 134a85fe12eSEd Maste * a relocatable (.o) file, a few symbols will also be created to make 135a85fe12eSEd Maste * it easier to access the binary data in other compilation units. 136a85fe12eSEd Maste */ 137a85fe12eSEd Maste void 138a85fe12eSEd Maste create_elf_from_binary(struct elfcopy *ecp, int ifd, const char *ifn) 139a85fe12eSEd Maste { 140a85fe12eSEd Maste char name[_SYMBOL_NAMSZ]; 141a85fe12eSEd Maste struct section *sec, *sec_temp, *shtab; 142a85fe12eSEd Maste struct stat sb; 143a85fe12eSEd Maste GElf_Ehdr oeh; 144a85fe12eSEd Maste GElf_Shdr sh; 145a85fe12eSEd Maste void *content; 146a85fe12eSEd Maste uint64_t off, data_start, data_end, data_size; 1471ce1c689SEd Maste char *sym_basename, *p; 148a85fe12eSEd Maste 149a85fe12eSEd Maste /* Reset internal section list. */ 150a85fe12eSEd Maste if (!TAILQ_EMPTY(&ecp->v_sec)) 151a85fe12eSEd Maste TAILQ_FOREACH_SAFE(sec, &ecp->v_sec, sec_list, sec_temp) { 152a85fe12eSEd Maste TAILQ_REMOVE(&ecp->v_sec, sec, sec_list); 153a85fe12eSEd Maste free(sec); 154a85fe12eSEd Maste } 155a85fe12eSEd Maste 156a85fe12eSEd Maste if (fstat(ifd, &sb) == -1) 157a85fe12eSEd Maste err(EXIT_FAILURE, "fstat failed"); 158a85fe12eSEd Maste 159a85fe12eSEd Maste /* Read the input binary file to a internal buffer. */ 160a85fe12eSEd Maste if ((content = malloc(sb.st_size)) == NULL) 161a85fe12eSEd Maste err(EXIT_FAILURE, "malloc failed"); 162a85fe12eSEd Maste if (read(ifd, content, sb.st_size) != sb.st_size) 163a85fe12eSEd Maste err(EXIT_FAILURE, "read failed"); 164a85fe12eSEd Maste 165a85fe12eSEd Maste /* 166a85fe12eSEd Maste * TODO: copy the input binary to output binary verbatim if -O is not 167a85fe12eSEd Maste * specified. 168a85fe12eSEd Maste */ 169a85fe12eSEd Maste 170a85fe12eSEd Maste /* Create EHDR for output .o file. */ 171a85fe12eSEd Maste if (gelf_newehdr(ecp->eout, ecp->oec) == NULL) 172a85fe12eSEd Maste errx(EXIT_FAILURE, "gelf_newehdr failed: %s", 173a85fe12eSEd Maste elf_errmsg(-1)); 174a85fe12eSEd Maste if (gelf_getehdr(ecp->eout, &oeh) == NULL) 175a85fe12eSEd Maste errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", 176a85fe12eSEd Maste elf_errmsg(-1)); 177a85fe12eSEd Maste 178a85fe12eSEd Maste /* Initialise e_ident fields. */ 179a85fe12eSEd Maste oeh.e_ident[EI_CLASS] = ecp->oec; 180a85fe12eSEd Maste oeh.e_ident[EI_DATA] = ecp->oed; 181a85fe12eSEd Maste /* 182a85fe12eSEd Maste * TODO: Set OSABI according to the OS platform where elfcopy(1) 183a85fe12eSEd Maste * was build. (probably) 184a85fe12eSEd Maste */ 185a85fe12eSEd Maste oeh.e_ident[EI_OSABI] = ELFOSABI_NONE; 186a85fe12eSEd Maste oeh.e_machine = ecp->oem; 187a85fe12eSEd Maste oeh.e_type = ET_REL; 188a85fe12eSEd Maste oeh.e_entry = 0; 189a85fe12eSEd Maste 190a85fe12eSEd Maste ecp->flags |= RELOCATABLE; 191a85fe12eSEd Maste 192a85fe12eSEd Maste /* Create .shstrtab section */ 193a85fe12eSEd Maste init_shstrtab(ecp); 194a85fe12eSEd Maste ecp->shstrtab->off = 0; 195a85fe12eSEd Maste 196a85fe12eSEd Maste /* 197a85fe12eSEd Maste * Create `.data' section which contains the binary data. The 198a85fe12eSEd Maste * section is inserted immediately after EHDR. 199a85fe12eSEd Maste */ 200a85fe12eSEd Maste off = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT); 201a85fe12eSEd Maste if (off == 0) 202a85fe12eSEd Maste errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1)); 203a85fe12eSEd Maste (void) create_external_section(ecp, ".data", NULL, content, sb.st_size, 204a85fe12eSEd Maste off, SHT_PROGBITS, ELF_T_BYTE, SHF_ALLOC | SHF_WRITE, 1, 0, 1); 205a85fe12eSEd Maste 206a85fe12eSEd Maste /* Insert .shstrtab after .data section. */ 207a85fe12eSEd Maste if ((ecp->shstrtab->os = elf_newscn(ecp->eout)) == NULL) 208a85fe12eSEd Maste errx(EXIT_FAILURE, "elf_newscn failed: %s", 209a85fe12eSEd Maste elf_errmsg(-1)); 210a85fe12eSEd Maste insert_to_sec_list(ecp, ecp->shstrtab, 1); 211a85fe12eSEd Maste 212a85fe12eSEd Maste /* Insert section header table here. */ 213a85fe12eSEd Maste shtab = insert_shtab(ecp, 1); 214a85fe12eSEd Maste 215a85fe12eSEd Maste /* Count in .symtab and .strtab section headers. */ 216a85fe12eSEd Maste shtab->sz += gelf_fsize(ecp->eout, ELF_T_SHDR, 2, EV_CURRENT); 217a85fe12eSEd Maste 2181ce1c689SEd Maste if ((sym_basename = strdup(ifn)) == NULL) 2191ce1c689SEd Maste err(1, "strdup"); 220e5c4075fSEd Maste for (p = sym_basename; *p != '\0'; p++) 221715d1396SEd Maste if (!isalnum(*p & 0xFF)) 222e5c4075fSEd Maste *p = '_'; 223a85fe12eSEd Maste #define _GEN_SYMNAME(S) do { \ 2241ce1c689SEd Maste snprintf(name, sizeof(name), "%s%s%s", "_binary_", sym_basename, S); \ 225a85fe12eSEd Maste } while (0) 226a85fe12eSEd Maste 227a85fe12eSEd Maste /* 228a85fe12eSEd Maste * Create symbol table. 229a85fe12eSEd Maste */ 230a85fe12eSEd Maste create_external_symtab(ecp); 231a85fe12eSEd Maste data_start = 0; 232a85fe12eSEd Maste data_end = data_start + sb.st_size; 233a85fe12eSEd Maste data_size = sb.st_size; 234a85fe12eSEd Maste _GEN_SYMNAME("_start"); 235a85fe12eSEd Maste add_to_symtab(ecp, name, data_start, 0, 1, 236a85fe12eSEd Maste ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1); 237a85fe12eSEd Maste _GEN_SYMNAME("_end"); 238a85fe12eSEd Maste add_to_symtab(ecp, name, data_end, 0, 1, 239a85fe12eSEd Maste ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1); 240a85fe12eSEd Maste _GEN_SYMNAME("_size"); 241a85fe12eSEd Maste add_to_symtab(ecp, name, data_size, 0, SHN_ABS, 242a85fe12eSEd Maste ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1); 243a85fe12eSEd Maste finalize_external_symtab(ecp); 244a85fe12eSEd Maste create_symtab_data(ecp); 245a85fe12eSEd Maste #undef _GEN_SYMNAME 2461ce1c689SEd Maste free(sym_basename); 247a85fe12eSEd Maste 248a85fe12eSEd Maste /* 249a85fe12eSEd Maste * Write the underlying ehdr. Note that it should be called 250a85fe12eSEd Maste * before elf_setshstrndx() since it will overwrite e->e_shstrndx. 251a85fe12eSEd Maste */ 252a85fe12eSEd Maste if (gelf_update_ehdr(ecp->eout, &oeh) == 0) 253a85fe12eSEd Maste errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", 254a85fe12eSEd Maste elf_errmsg(-1)); 255a85fe12eSEd Maste 256a85fe12eSEd Maste /* Update sh_name pointer for each section header entry. */ 257bc589b72SMark Johnston ecp->flags |= SYMTAB_EXIST; 258a85fe12eSEd Maste update_shdr(ecp, 0); 259a85fe12eSEd Maste 260a85fe12eSEd Maste /* Properly set sh_link field of .symtab section. */ 261a85fe12eSEd Maste if (gelf_getshdr(ecp->symtab->os, &sh) == NULL) 262a85fe12eSEd Maste errx(EXIT_FAILURE, "692 gelf_getshdr() failed: %s", 263a85fe12eSEd Maste elf_errmsg(-1)); 264a85fe12eSEd Maste sh.sh_link = elf_ndxscn(ecp->strtab->os); 265a85fe12eSEd Maste if (!gelf_update_shdr(ecp->symtab->os, &sh)) 266a85fe12eSEd Maste errx(EXIT_FAILURE, "gelf_update_shdr() failed: %s", 267a85fe12eSEd Maste elf_errmsg(-1)); 268a85fe12eSEd Maste 269a85fe12eSEd Maste /* Renew oeh to get the updated e_shstrndx. */ 270a85fe12eSEd Maste if (gelf_getehdr(ecp->eout, &oeh) == NULL) 271a85fe12eSEd Maste errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", 272a85fe12eSEd Maste elf_errmsg(-1)); 273a85fe12eSEd Maste 274a85fe12eSEd Maste /* Resync section offsets. */ 275a85fe12eSEd Maste resync_sections(ecp); 276a85fe12eSEd Maste 277a85fe12eSEd Maste /* Store SHDR offset in EHDR. */ 278a85fe12eSEd Maste oeh.e_shoff = shtab->off; 279a85fe12eSEd Maste 280a85fe12eSEd Maste /* Update ehdr since we modified e_shoff. */ 281a85fe12eSEd Maste if (gelf_update_ehdr(ecp->eout, &oeh) == 0) 282a85fe12eSEd Maste errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", 283a85fe12eSEd Maste elf_errmsg(-1)); 284a85fe12eSEd Maste 285a85fe12eSEd Maste /* Write out the output elf object. */ 286a85fe12eSEd Maste if (elf_update(ecp->eout, ELF_C_WRITE) < 0) 287a85fe12eSEd Maste errx(EXIT_FAILURE, "elf_update() failed: %s", 288a85fe12eSEd Maste elf_errmsg(-1)); 289a85fe12eSEd Maste 290a85fe12eSEd Maste /* Release allocated resource. */ 291a85fe12eSEd Maste free(content); 292a85fe12eSEd Maste free_elf(ecp); 293a85fe12eSEd Maste } 294