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