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