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 static int 41 basename_length(const char *filename) 42 { 43 char *p; 44 45 if ((p = strchr(filename, '.')) != NULL) 46 return (p - filename); 47 return (strlen(filename)); 48 } 49 50 /* 51 * Convert ELF object to `binary'. Sections with SHF_ALLOC flag set 52 * are copied to the result binary. The relative offsets for each section 53 * are retained, so the result binary file might contain "holes". 54 */ 55 void 56 create_binary(int ifd, int ofd) 57 { 58 Elf *e; 59 Elf_Scn *scn; 60 Elf_Data *d; 61 GElf_Shdr sh; 62 off_t base, off; 63 int elferr; 64 65 if ((e = elf_begin(ifd, ELF_C_READ, NULL)) == NULL) 66 errx(EXIT_FAILURE, "elf_begin() failed: %s", 67 elf_errmsg(-1)); 68 69 base = 0; 70 if (lseek(ofd, base, SEEK_SET) < 0) 71 err(EXIT_FAILURE, "lseek failed"); 72 73 /* 74 * Find base offset in the first iteration. 75 */ 76 base = -1; 77 scn = NULL; 78 while ((scn = elf_nextscn(e, scn)) != NULL) { 79 if (gelf_getshdr(scn, &sh) == NULL) { 80 warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 81 (void) elf_errno(); 82 continue; 83 } 84 if ((sh.sh_flags & SHF_ALLOC) == 0 || 85 sh.sh_type == SHT_NOBITS || 86 sh.sh_size == 0) 87 continue; 88 if (base == -1 || (off_t) sh.sh_offset < base) 89 base = sh.sh_offset; 90 } 91 elferr = elf_errno(); 92 if (elferr != 0) 93 warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 94 95 if (base == -1) 96 return; 97 98 /* 99 * Write out sections in the second iteration. 100 */ 101 scn = NULL; 102 while ((scn = elf_nextscn(e, scn)) != NULL) { 103 if (gelf_getshdr(scn, &sh) == NULL) { 104 warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 105 (void) elf_errno(); 106 continue; 107 } 108 if ((sh.sh_flags & SHF_ALLOC) == 0 || 109 sh.sh_type == SHT_NOBITS || 110 sh.sh_size == 0) 111 continue; 112 (void) elf_errno(); 113 if ((d = elf_getdata(scn, NULL)) == NULL) { 114 elferr = elf_errno(); 115 if (elferr != 0) 116 warnx("elf_getdata failed: %s", elf_errmsg(-1)); 117 continue; 118 } 119 if (d->d_buf == NULL || d->d_size == 0) 120 continue; 121 122 /* lseek to section offset relative to `base'. */ 123 off = sh.sh_offset - base; 124 if (lseek(ofd, off, SEEK_SET) < 0) 125 err(EXIT_FAILURE, "lseek failed"); 126 127 /* Write out section contents. */ 128 if (write(ofd, d->d_buf, d->d_size) != (ssize_t) d->d_size) 129 err(EXIT_FAILURE, "write failed"); 130 } 131 elferr = elf_errno(); 132 if (elferr != 0) 133 warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 134 } 135 136 #define _SYMBOL_NAMSZ 1024 137 138 /* 139 * Convert `binary' to ELF object. The input `binary' is converted to 140 * a relocatable (.o) file, a few symbols will also be created to make 141 * it easier to access the binary data in other compilation units. 142 */ 143 void 144 create_elf_from_binary(struct elfcopy *ecp, int ifd, const char *ifn) 145 { 146 char name[_SYMBOL_NAMSZ]; 147 struct section *sec, *sec_temp, *shtab; 148 struct stat sb; 149 GElf_Ehdr oeh; 150 GElf_Shdr sh; 151 void *content; 152 uint64_t off, data_start, data_end, data_size; 153 154 /* Reset internal section list. */ 155 if (!TAILQ_EMPTY(&ecp->v_sec)) 156 TAILQ_FOREACH_SAFE(sec, &ecp->v_sec, sec_list, sec_temp) { 157 TAILQ_REMOVE(&ecp->v_sec, sec, sec_list); 158 free(sec); 159 } 160 161 if (fstat(ifd, &sb) == -1) 162 err(EXIT_FAILURE, "fstat failed"); 163 164 /* Read the input binary file to a internal buffer. */ 165 if ((content = malloc(sb.st_size)) == NULL) 166 err(EXIT_FAILURE, "malloc failed"); 167 if (read(ifd, content, sb.st_size) != sb.st_size) 168 err(EXIT_FAILURE, "read failed"); 169 170 /* 171 * TODO: copy the input binary to output binary verbatim if -O is not 172 * specified. 173 */ 174 175 /* Create EHDR for output .o file. */ 176 if (gelf_newehdr(ecp->eout, ecp->oec) == NULL) 177 errx(EXIT_FAILURE, "gelf_newehdr failed: %s", 178 elf_errmsg(-1)); 179 if (gelf_getehdr(ecp->eout, &oeh) == NULL) 180 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", 181 elf_errmsg(-1)); 182 183 /* Initialise e_ident fields. */ 184 oeh.e_ident[EI_CLASS] = ecp->oec; 185 oeh.e_ident[EI_DATA] = ecp->oed; 186 /* 187 * TODO: Set OSABI according to the OS platform where elfcopy(1) 188 * was build. (probably) 189 */ 190 oeh.e_ident[EI_OSABI] = ELFOSABI_NONE; 191 oeh.e_machine = ecp->oem; 192 oeh.e_type = ET_REL; 193 oeh.e_entry = 0; 194 195 ecp->flags |= RELOCATABLE; 196 197 /* Create .shstrtab section */ 198 init_shstrtab(ecp); 199 ecp->shstrtab->off = 0; 200 201 /* 202 * Create `.data' section which contains the binary data. The 203 * section is inserted immediately after EHDR. 204 */ 205 off = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT); 206 if (off == 0) 207 errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1)); 208 (void) create_external_section(ecp, ".data", NULL, content, sb.st_size, 209 off, SHT_PROGBITS, ELF_T_BYTE, SHF_ALLOC | SHF_WRITE, 1, 0, 1); 210 211 /* Insert .shstrtab after .data section. */ 212 if ((ecp->shstrtab->os = elf_newscn(ecp->eout)) == NULL) 213 errx(EXIT_FAILURE, "elf_newscn failed: %s", 214 elf_errmsg(-1)); 215 insert_to_sec_list(ecp, ecp->shstrtab, 1); 216 217 /* Insert section header table here. */ 218 shtab = insert_shtab(ecp, 1); 219 220 /* Count in .symtab and .strtab section headers. */ 221 shtab->sz += gelf_fsize(ecp->eout, ELF_T_SHDR, 2, EV_CURRENT); 222 223 #define _GEN_SYMNAME(S) do { \ 224 snprintf(name, sizeof(name), "%s%.*s%s", "_binary_", \ 225 basename_length(ifn), ifn, S); \ 226 } while (0) 227 228 /* 229 * Create symbol table. 230 */ 231 create_external_symtab(ecp); 232 data_start = 0; 233 data_end = data_start + sb.st_size; 234 data_size = sb.st_size; 235 _GEN_SYMNAME("_start"); 236 add_to_symtab(ecp, name, data_start, 0, 1, 237 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1); 238 _GEN_SYMNAME("_end"); 239 add_to_symtab(ecp, name, data_end, 0, 1, 240 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1); 241 _GEN_SYMNAME("_size"); 242 add_to_symtab(ecp, name, data_size, 0, SHN_ABS, 243 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1); 244 finalize_external_symtab(ecp); 245 create_symtab_data(ecp); 246 #undef _GEN_SYMNAME 247 248 /* 249 * Write the underlying ehdr. Note that it should be called 250 * before elf_setshstrndx() since it will overwrite e->e_shstrndx. 251 */ 252 if (gelf_update_ehdr(ecp->eout, &oeh) == 0) 253 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", 254 elf_errmsg(-1)); 255 256 /* Generate section name string table (.shstrtab). */ 257 ecp->flags |= SYMTAB_EXIST; 258 set_shstrtab(ecp); 259 260 /* Update sh_name pointer for each section header entry. */ 261 update_shdr(ecp, 0); 262 263 /* Properly set sh_link field of .symtab section. */ 264 if (gelf_getshdr(ecp->symtab->os, &sh) == NULL) 265 errx(EXIT_FAILURE, "692 gelf_getshdr() failed: %s", 266 elf_errmsg(-1)); 267 sh.sh_link = elf_ndxscn(ecp->strtab->os); 268 if (!gelf_update_shdr(ecp->symtab->os, &sh)) 269 errx(EXIT_FAILURE, "gelf_update_shdr() failed: %s", 270 elf_errmsg(-1)); 271 272 /* Renew oeh to get the updated e_shstrndx. */ 273 if (gelf_getehdr(ecp->eout, &oeh) == NULL) 274 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", 275 elf_errmsg(-1)); 276 277 /* Resync section offsets. */ 278 resync_sections(ecp); 279 280 /* Store SHDR offset in EHDR. */ 281 oeh.e_shoff = shtab->off; 282 283 /* Update ehdr since we modified e_shoff. */ 284 if (gelf_update_ehdr(ecp->eout, &oeh) == 0) 285 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", 286 elf_errmsg(-1)); 287 288 /* Write out the output elf object. */ 289 if (elf_update(ecp->eout, ELF_C_WRITE) < 0) 290 errx(EXIT_FAILURE, "elf_update() failed: %s", 291 elf_errmsg(-1)); 292 293 /* Release allocated resource. */ 294 free(content); 295 free_elf(ecp); 296 } 297