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