summaryrefslogtreecommitdiff
path: root/usr.sbin/mkuboot/copy_elf.c
blob: 60e5e77e531f79fd274ba662dcda4f5e54ec02dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*      $OpenBSD: copy_elf.c,v 1.5 2014/01/01 09:24:54 mcbride Exp $       */

/*
 * Copyright (c) 2013 Miodrag Vallat.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <err.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include <sys/exec_elf.h>

#if defined(ELFSIZE) && (ELFSIZE == 32)
#define elfoff2h(x) letoh32(x)
#define h2elfoff(x) htole32(x)
#elif defined(ELFSIZE) && (ELFSIZE == 64)
#define elfoff2h(x) letoh64(x)
#define h2elfoff(x) htole64(x)
#else
#error "unknown elf size"
#endif

struct image_header;

#define        roundup(x, y)   ((((x)+((y)-1))/(y))*(y))

extern u_long copy_data(int, const char *, int, const char *, u_long,
	    struct image_header *, Elf_Word);
u_long	copy_mem(void *, int, const char *, u_long, struct image_header *,
	    Elf_Word);
extern u_long fill_zeroes(int, const char *, u_long, struct image_header *,
	    Elf_Word);

u_long
ELFNAME(copy_elf)(int ifd, const char *iname, int ofd, const char *oname,
    u_long crc, struct image_header *ih)
{
	ssize_t nbytes;
	Elf_Ehdr ehdr, elf;
	Elf_Phdr phdr;
	Elf_Addr vaddr, ovaddr, svaddr, off, ssym;
	Elf_Shdr *shp, *wshp;
	Elf_Addr esym = 0, esymval;
	int i, sz, havesyms;

	nbytes = read(ifd, &ehdr, sizeof ehdr);
	if (nbytes == -1)
		err(1, "%s", iname);
	if (nbytes != sizeof ehdr)
		return 0;

	elf = ehdr;

	if (lseek(ifd, (off_t)elfoff2h(elf.e_shoff), SEEK_SET) == -1)
		err(1, "%s unable to seek to section header", iname);

	sz = letoh16(elf.e_shnum) * sizeof(Elf_Shdr);
	shp = calloc(sz, 1);
	if (read(ifd, shp, sz) != sz)
		err(1, "%s: read section headers", iname);

	wshp = calloc(sz, 1);
	memcpy(wshp, shp, sz);

	/* first walk the load sections to find the kernel addresses */
	/* next we walk the sections to find the
	 * location of esym (first address of data space
	 */
	for (i = 0; i < letoh16(ehdr.e_phnum); i++) {
		if (lseek(ifd, elfoff2h(ehdr.e_phoff) + i *
		    letoh16(ehdr.e_phentsize), SEEK_SET) == (off_t)-1)
			err(1, "%s", iname);
		if (read(ifd, &phdr, sizeof phdr) != sizeof(phdr))
			err(1, "%s", iname);
		/* assumes it loads in incrementing address order */
		if (letoh32(phdr.p_type) == PT_LOAD)
			vaddr = elfoff2h(phdr.p_vaddr) +
			    elfoff2h(phdr.p_memsz);
	}

	/* ok, we need to write the elf header and section header
	 * which contains info about the not yet written section data
	 * however due to crc the data all has to be written in order
	 * which means walking the structures twice once to precompute
	 * the data, once to write the data.
	 */
	ssym = vaddr;
	vaddr += roundup((sizeof(Elf_Ehdr) + sz), sizeof(Elf_Addr));
	off = roundup((sizeof(Elf_Ehdr) + sz), sizeof(Elf_Addr));
	for (i = 0; i < letoh16(elf.e_shnum); i++) {
		if (esym == 0 && elfoff2h(shp[i].sh_flags) & SHF_WRITE &&
		    elfoff2h(shp[i].sh_flags) & SHF_ALLOC)
			esym = elfoff2h(shp[i].sh_addr);

		if (letoh32(shp[i].sh_type) == SHT_SYMTAB ||
		    letoh32(shp[i].sh_type) == SHT_STRTAB) {
#ifdef DEBUG
		fprintf(stderr, "shdr %d %d/%d off %lx\n", i,
		    letoh32(shp[i].sh_type), roundup(elfoff2h(shp[i].sh_size),
		    sizeof(Elf_Addr)), off);
#endif
			/* data is at shp[i].sh_offset of len shp[i].sh_size */
			wshp[i].sh_offset = h2elfoff(off);
			off += roundup(elfoff2h(shp[i].sh_size),
			    sizeof(Elf_Addr));
			vaddr += roundup(elfoff2h(shp[i].sh_size),
			    sizeof(Elf_Addr));
		}
	}
	esymval = vaddr;
#ifdef DEBUG
	fprintf(stderr, "esymval %lx size %ld\n", esymval, esymval - ssym);
#endif

	for (i = 0; i < letoh16(ehdr.e_phnum); i++) {
#ifdef DEBUG
		fprintf(stderr, "phdr %d/%d\n", i, letoh16(ehdr.e_phnum));
#endif
		if (lseek(ifd, elfoff2h(ehdr.e_phoff) + i *
		    letoh16(ehdr.e_phentsize), SEEK_SET) == (off_t)-1)
			err(1, "%s", iname);
		if (read(ifd, &phdr, sizeof phdr) != sizeof(phdr))
			err(1, "%s", iname);

#ifdef DEBUG
		fprintf(stderr,
		    "vaddr %p type %#x offset %p filesz %p memsz %p\n",
		    elfoff2h(phdr.p_vaddr), letoh32(phdr.p_type),
	            elfoff2h(phdr.p_offset), elfoff2h(phdr.p_filesz),
                    elfoff2h(phdr.p_memsz));
#endif

		switch (letoh32(phdr.p_type)) {
		case PT_LOAD:
			break;
		case PT_NULL:
		case PT_NOTE:
		case PT_OPENBSD_RANDOMIZE:
#ifdef DEBUG
			fprintf(stderr, "skipping segment type %#x\n",
			    letoh32(phdr.p_type));
#endif
			continue;
		default:
			errx(1, "unexpected segment type %#x",
			    letoh32(phdr.p_type));
		}

		if (i == 0) 
			vaddr = elfoff2h(phdr.p_vaddr);
		else if (vaddr != elfoff2h(phdr.p_vaddr)) {
#ifdef DEBUG
			fprintf(stderr, "gap %p->%p\n", vaddr,
			    elfoff2h(phdr.p_vaddr));
#endif
			/* fill the gap between the previous phdr if any */
			crc = fill_zeroes(ofd, oname, crc, ih,
			    elfoff2h(phdr.p_vaddr) - vaddr);
			vaddr = elfoff2h(phdr.p_vaddr);
		}

		if (elfoff2h(phdr.p_filesz) != 0) {
#ifdef DEBUG
			fprintf(stderr, "copying %p from infile %p\n",
			   elfoff2h(phdr.p_filesz), elfoff2h(phdr.p_offset));
#endif
			/* esym will be in the data portion of a region */
			if (esym >= elfoff2h(phdr.p_vaddr) &&
			    esym < elfoff2h(phdr.p_vaddr) +
			    elfoff2h(phdr.p_filesz)) {
				/* load the region up to the esym
				 * (may be empty)
				 */
				Elf_Addr loadlen = esym -
				    elfoff2h(phdr.p_vaddr);

				if (lseek(ifd, elfoff2h(phdr.p_offset),
				    SEEK_SET) == (off_t)-1)
					err(1, "%s", iname);
				crc = copy_data(ifd, iname, ofd, oname, crc,
				    ih, loadlen);

				crc = copy_mem(&esymval, ofd, oname, crc, ih,
				    sizeof(esymval));

				if (lseek(ifd, elfoff2h(phdr.p_offset) +
				    loadlen + sizeof(esymval), SEEK_SET) ==
				    (off_t)-1)
					err(1, "%s", iname);
				crc = copy_data(ifd, iname, ofd, oname, crc,
				    ih, elfoff2h(phdr.p_filesz) - loadlen -
				    sizeof(esymval));
			} else {

				if (lseek(ifd, elfoff2h(phdr.p_offset),
				    SEEK_SET) == (off_t)-1)
					err(1, "%s", iname);
				crc = copy_data(ifd, iname, ofd, oname, crc,
				    ih, elfoff2h(phdr.p_filesz));
			}
			if (elfoff2h(phdr.p_memsz) - elfoff2h(phdr.p_filesz)
			    != 0) {
#ifdef DEBUG
				fprintf(stderr, "zeroing %p\n",
				    elfoff2h(phdr.p_memsz) -
				    elfoff2h(phdr.p_filesz));
#endif
				crc = fill_zeroes(ofd, oname, crc, ih,
				    elfoff2h(phdr.p_memsz) -
				    elfoff2h(phdr.p_filesz));
			}
			ovaddr = vaddr + elfoff2h(phdr.p_memsz);
		} else {
			ovaddr = vaddr;
		}
		/*
		 * If p_filesz == 0, this is likely .bss, which we do not
		 * need to provide. If it's not the last phdr, the gap
		 * filling code will output the necessary zeroes anyway.
		 */
		vaddr += elfoff2h(phdr.p_memsz);
	}

	vaddr = roundup(vaddr, sizeof(Elf_Addr));
	if (vaddr != ovaddr) {
#ifdef DEBUG
		fprintf(stderr, "gap %p->%p\n", vaddr, elfoff2h(phdr.p_vaddr));
#endif
		/* fill the gap between the previous phdr if not aligned */
		crc = fill_zeroes(ofd, oname, crc, ih, vaddr - ovaddr);
	}

	for (havesyms = i = 0; i < letoh16(elf.e_shnum); i++)
		if (letoh32(shp[i].sh_type) == SHT_SYMTAB)
			havesyms = 1;

	if (havesyms == 0)
		return crc;

	elf.e_phoff = 0;
	elf.e_shoff = h2elfoff(sizeof(Elf_Ehdr));
	elf.e_phentsize = 0;
	elf.e_phnum = 0;
	crc = copy_mem(&elf, ofd, oname, crc, ih, sizeof(elf));
	crc = copy_mem(wshp, ofd, oname, crc, ih, sz);
	off = sizeof(elf) + sz;
	vaddr += sizeof(elf) + sz;

	off = roundup((sizeof(Elf_Ehdr) + sz), sizeof(Elf_Addr));
	for (i = 0; i < letoh16(elf.e_shnum); i++) {
		if (letoh32(shp[i].sh_type) == SHT_SYMTAB ||
		    letoh32(shp[i].sh_type) == SHT_STRTAB) {
			Elf_Addr align;
			/* data is at shp[i].sh_offset of len shp[i].sh_size */
			if (lseek(ifd, elfoff2h(shp[i].sh_offset), SEEK_SET)
			    == -1)
				err(1, "%s", iname);

			off += elfoff2h(shp[i].sh_size);
			vaddr += elfoff2h(shp[i].sh_size);
			crc = copy_data(ifd, iname, ofd, oname, crc, ih,
			    elfoff2h(shp[i].sh_size));
			align = roundup(elfoff2h(shp[i].sh_size),
			    sizeof(Elf_Addr)) - elfoff2h(shp[i].sh_size);
			if (align != 0) {
				vaddr += align;
				crc = fill_zeroes(ofd, oname, crc, ih, align);
			}
		}
	}

	if (vaddr != esymval)
		warnx("esymval and vaddr mismatch %lx %lx\n", esymval, vaddr);

	return crc;
}