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