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