2 * Copyright (c) 2010,2011 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/param.h>
39 ELFTC_VCSID("$Id: binary.c 3757 2019-06-28 01:15:28Z emaste $");
42 * Convert ELF object to `binary'. Sections with SHF_ALLOC flag set
43 * are copied to the result binary. The relative offsets for each section
44 * are retained, so the result binary file might contain "holes".
47 create_binary(int ifd, int ofd)
57 if ((e = elf_begin(ifd, ELF_C_READ, NULL)) == NULL)
58 errx(EXIT_FAILURE, "elf_begin() failed: %s",
63 if (lseek(ofd, baseoff, SEEK_SET) < 0)
64 err(EXIT_FAILURE, "lseek failed");
67 * Find base offset in the first iteration.
71 while ((scn = elf_nextscn(e, scn)) != NULL) {
72 if (gelf_getshdr(scn, &sh) == NULL) {
73 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
77 if ((sh.sh_flags & SHF_ALLOC) == 0 ||
78 sh.sh_type == SHT_NOBITS ||
81 if (baseoff == -1 || (off_t) sh.sh_offset < baseoff) {
82 baseoff = sh.sh_offset;
83 baseaddr = sh.sh_addr;
88 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
94 * Write out sections in the second iteration.
97 while ((scn = elf_nextscn(e, scn)) != NULL) {
98 if (gelf_getshdr(scn, &sh) == NULL) {
99 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
103 if ((sh.sh_flags & SHF_ALLOC) == 0 ||
104 sh.sh_type == SHT_NOBITS ||
108 if ((d = elf_rawdata(scn, NULL)) == NULL) {
109 elferr = elf_errno();
111 warnx("elf_rawdata failed: %s", elf_errmsg(-1));
114 if (d->d_buf == NULL || d->d_size == 0)
117 /* lseek to section offset relative to `baseaddr'. */
118 off = sh.sh_addr - baseaddr;
119 if (lseek(ofd, off, SEEK_SET) < 0)
120 err(EXIT_FAILURE, "lseek failed");
122 /* Write out section contents. */
123 if (write(ofd, d->d_buf, d->d_size) != (ssize_t) d->d_size)
124 err(EXIT_FAILURE, "write failed");
126 elferr = elf_errno();
128 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
131 #define _SYMBOL_NAMSZ 1024
134 * Convert `binary' to ELF object. The input `binary' is converted to
135 * a relocatable (.o) file, a few symbols will also be created to make
136 * it easier to access the binary data in other compilation units.
139 create_elf_from_binary(struct elfcopy *ecp, int ifd, const char *ifn)
141 char name[_SYMBOL_NAMSZ];
142 struct section *sec, *sec_temp, *shtab;
147 uint64_t off, data_start, data_end, data_size;
148 char *sym_basename, *p;
150 /* Reset internal section list. */
151 if (!TAILQ_EMPTY(&ecp->v_sec))
152 TAILQ_FOREACH_SAFE(sec, &ecp->v_sec, sec_list, sec_temp) {
153 TAILQ_REMOVE(&ecp->v_sec, sec, sec_list);
157 if (fstat(ifd, &sb) == -1)
158 err(EXIT_FAILURE, "fstat failed");
160 /* Read the input binary file to a internal buffer. */
161 if ((content = malloc(sb.st_size)) == NULL)
162 err(EXIT_FAILURE, "malloc failed");
163 if (read(ifd, content, sb.st_size) != sb.st_size)
164 err(EXIT_FAILURE, "read failed");
167 * TODO: copy the input binary to output binary verbatim if -O is not
171 /* Create EHDR for output .o file. */
172 if (gelf_newehdr(ecp->eout, ecp->oec) == NULL)
173 errx(EXIT_FAILURE, "gelf_newehdr failed: %s",
175 if (gelf_getehdr(ecp->eout, &oeh) == NULL)
176 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
179 /* Initialise e_ident fields. */
180 oeh.e_ident[EI_CLASS] = ecp->oec;
181 oeh.e_ident[EI_DATA] = ecp->oed;
183 * TODO: Set OSABI according to the OS platform where elfcopy(1)
184 * was build. (probably)
186 oeh.e_ident[EI_OSABI] = ELFOSABI_NONE;
187 oeh.e_machine = ecp->oem;
191 ecp->flags |= RELOCATABLE;
193 /* Create .shstrtab section */
195 ecp->shstrtab->off = 0;
198 * Create `.data' section which contains the binary data. The
199 * section is inserted immediately after EHDR.
201 off = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT);
203 errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1));
204 (void) create_external_section(ecp, ".data", NULL, content, sb.st_size,
205 off, SHT_PROGBITS, ELF_T_BYTE, SHF_ALLOC | SHF_WRITE, 1, 0, 1);
207 /* Insert .shstrtab after .data section. */
208 if ((ecp->shstrtab->os = elf_newscn(ecp->eout)) == NULL)
209 errx(EXIT_FAILURE, "elf_newscn failed: %s",
211 insert_to_sec_list(ecp, ecp->shstrtab, 1);
213 /* Insert section header table here. */
214 shtab = insert_shtab(ecp, 1);
216 /* Count in .symtab and .strtab section headers. */
217 shtab->sz += gelf_fsize(ecp->eout, ELF_T_SHDR, 2, EV_CURRENT);
219 if ((sym_basename = strdup(ifn)) == NULL)
221 for (p = sym_basename; *p != '\0'; p++)
222 if (!isalnum(*p & 0xFF))
224 #define _GEN_SYMNAME(S) do { \
225 snprintf(name, sizeof(name), "%s%s%s", "_binary_", sym_basename, S); \
229 * Create symbol table.
231 create_external_symtab(ecp);
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);
250 * Write the underlying ehdr. Note that it should be called
251 * before elf_setshstrndx() since it will overwrite e->e_shstrndx.
253 if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
254 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
257 /* Update sh_name pointer for each section header entry. */
258 ecp->flags |= SYMTAB_EXIST;
261 /* Properly set sh_link field of .symtab section. */
262 if (gelf_getshdr(ecp->symtab->os, &sh) == NULL)
263 errx(EXIT_FAILURE, "692 gelf_getshdr() failed: %s",
265 sh.sh_link = elf_ndxscn(ecp->strtab->os);
266 if (!gelf_update_shdr(ecp->symtab->os, &sh))
267 errx(EXIT_FAILURE, "gelf_update_shdr() failed: %s",
270 /* Renew oeh to get the updated e_shstrndx. */
271 if (gelf_getehdr(ecp->eout, &oeh) == NULL)
272 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
275 /* Resync section offsets. */
276 resync_sections(ecp);
278 /* Store SHDR offset in EHDR. */
279 oeh.e_shoff = shtab->off;
281 /* Update ehdr since we modified e_shoff. */
282 if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
283 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
286 /* Write out the output elf object. */
287 if (elf_update(ecp->eout, ELF_C_WRITE) < 0)
288 errx(EXIT_FAILURE, "elf_update() failed: %s",
291 /* Release allocated resource. */