summaryrefslogtreecommitdiff
path: root/sbin/modload/elf.c
blob: 17dc2e3ef5c95a39e275ab8bc972ea4080197394 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*	$OpenBSD: elf.c,v 1.3 2002/06/09 08:13:07 todd Exp $	*/
/*	$NetBSD: elf.c,v 1.8 2002/01/03 21:45:58 jdolecek Exp $	*/

/*
 * Copyright (c) 1998 Johan Danielsson <joda@pdc.kth.se>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/param.h>

#if defined(__alpha__) || defined(__arch64__) || defined(__x86_64__)
#define ELFSIZE 64
#else
#define ELFSIZE 32
#endif
#include <sys/exec_elf.h>
#ifndef ELF_HDR_SIZE
#define ELF_HDR_SIZE sizeof(Elf_Ehdr)
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/lkm.h>

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

#include "modload.h"

char *strtab;

static void
read_section_header(int fd, Elf_Ehdr *ehdr, int num, Elf_Shdr *shdr)
{

	if (lseek(fd, ehdr->e_shoff + num * ehdr->e_shentsize, SEEK_SET) < 0)
		err(1, "lseek");
	if (read(fd, shdr, sizeof(*shdr)) != sizeof(*shdr))
		err(1, "read");
}

struct elf_section {
	char *name;		/* name of section; points into string table */
	unsigned long type;	/* type of section */
	void *addr;		/* load address of section */
	off_t offset;		/* offset in file */
	size_t size;		/* size of section */
	size_t align;
	struct elf_section *next;
};

/* adds the section `s' at the correct (sorted by address) place in
   the list ponted to by head; *head may be NULL */
static void
add_section(struct elf_section **head, struct elf_section *s)
{
	struct elf_section *p, **q;
	q = head;
	p = *head;

	while (1) {
		if (p == NULL || p->addr > s->addr) {
			s->next = p;
			*q = s;
			return;
		}
		q = &p->next;
		p = p->next;
	}
}

/* make a linked list of all sections containing ALLOCatable data */
static void
read_sections(int fd, Elf_Ehdr *ehdr, char *shstrtab, struct elf_section **head)
{
	int i;
	Elf_Shdr shdr;

	*head = NULL;
	/* scan through section headers */
	for (i = 0; i < ehdr->e_shnum; i++) {
		struct elf_section *s;
		read_section_header(fd, ehdr, i, &shdr);
		if (((shdr.sh_flags & SHF_ALLOC) == 0)
		    && (shdr.sh_type != SHT_STRTAB)
		    && (shdr.sh_type != SHT_SYMTAB)
		    && (shdr.sh_type != SHT_DYNSYM)) {
			/* skip non-ALLOC sections */
			continue;
		}
		s = malloc(sizeof(*s));
		if (s == NULL)
			errx(1, "failed to allocate %lu bytes",
			     (u_long)sizeof(*s));
		s->name = shstrtab + shdr.sh_name;
		s->type = shdr.sh_type;
		s->addr = (void*)shdr.sh_addr;
		s->offset = shdr.sh_offset;
		s->size = shdr.sh_size;
		s->align = shdr.sh_addralign;
		add_section(head, s);
	}
}

/* get the symbol table sections and free the rest of them */
static void
get_symtab(struct elf_section **stab)
{
	struct elf_section *head, *cur, *prev;

	head = NULL;
	prev = NULL;
	cur = *stab;
	while (cur) {
		if ((cur->type == SHT_SYMTAB) || (cur->type == SHT_DYNSYM)) {
			if (head == NULL) {
				head = cur;
			}
			if (prev != NULL) {
				prev->next = cur;
			}
			prev = cur;
			cur = cur->next;
		} else {
			struct elf_section *p = cur;
			cur = cur->next;
			p->next = NULL;
			free(p);
		}
	}

	if (prev) {
		prev->next = NULL;
	}
	*stab = head;
}

/* free a list of section headers */
static void
free_sections(struct elf_section *head)
{

	while (head) {
		struct elf_section *p = head;
		head = head->next;
		free(p);
	}
}

/* read section header's string table */
static char *
read_shstring_table(int fd, Elf_Ehdr *ehdr)
{
	Elf_Shdr shdr;
	char *shstrtab;

	read_section_header(fd, ehdr, ehdr->e_shstrndx, &shdr);

	shstrtab = malloc(shdr.sh_size);
	if (shstrtab == NULL)
		errx(1, "failed to allocate %lu bytes", (u_long)shdr.sh_size);
	if (lseek(fd, shdr.sh_offset, SEEK_SET) < 0)
		err(1, "lseek");
	if (read(fd, shstrtab, shdr.sh_size) != shdr.sh_size)
		err(1, "read");
	return shstrtab;
}

/* read string table */
static char *
read_string_table(int fd, struct elf_section *head, int *strtablen)
{
	char *string_table=NULL;

	while (head) {
		if ((strcmp(head->name, ".strtab") == 0 )
		    && (head->type == SHT_STRTAB)) {
			string_table = malloc(head->size);
			if (string_table == NULL)
				errx(1, "failed to allocate %lu bytes",
				    (u_long)head->size);
			if (lseek(fd, head->offset, SEEK_SET) < 0)
				err(1, "lseek");
			if (read(fd, string_table, head->size) != head->size)
				err(1, "read");
			*strtablen = head->size;
			break;
		} else {
			head = head->next;
		}
	}
	return string_table;
}

static int
read_elf_header(int fd, Elf_Ehdr *ehdr)
{
	ssize_t n;

	n = read(fd, ehdr, sizeof(*ehdr));
	if (n < 0)
		err(1, "failed reading %lu bytes", (u_long)sizeof(*ehdr));
	if (n != sizeof(*ehdr)) {
		if (debug)
			warnx("failed to read %lu bytes", (u_long)sizeof(*ehdr));
		return -1;
	}
	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
	    ehdr->e_ident[EI_CLASS] != ELFCLASS)
		errx(4, "not in ELF%u format", ELFSIZE);
	if (ehdr->e_ehsize != ELF_HDR_SIZE)
		errx(4, "file has ELF%u identity, but wrong header size",
		    ELFSIZE);

	return 0;
}

/* offset of data segment; this is horrible, but keeps the size of the
   module to a minimum */
static ssize_t data_offset;

/* return size needed by the module */
int
elf_mod_sizes(fd, modsize, strtablen, resrvp, sp)
	int fd;
	size_t *modsize;
	int *strtablen;
	struct lmc_resrv *resrvp;
	struct stat *sp;
{
	Elf_Ehdr ehdr;
	ssize_t off = 0;
	size_t data_hole = 0;
	char *shstrtab, *strtab;
	struct elf_section *head, *s, *stab;
	
	if (read_elf_header(fd, &ehdr) < 0)
		return -1;
	shstrtab = read_shstring_table(fd, &ehdr);
	read_sections(fd, &ehdr, shstrtab, &head);

	for (s = head; s; s = s->next) {
                if ((s->type == SHT_STRTAB) && (s->type == SHT_SYMTAB)
                    && (s->type == SHT_DYNSYM)) {
			continue;
		}
		if (debug)
			fprintf(stderr,
			    "%s: addr = %p size = %#lx align = %#lx\n",
			    s->name, s->addr, (u_long)s->size, (u_long)s->align);
		/* XXX try to get rid of the hole before the data
                   section that GNU-ld likes to put there */
		if (strcmp(s->name, ".data") == 0 && s->addr > (void*)off) {
			data_offset = roundup(off, s->align);
			if (debug)
				fprintf(stderr, ".data section forced to "
				    "offset %p (was %p)\n",
				    (void*)data_offset,
				    s->addr);
			/* later remove size of compressed hole from off */
			data_hole = (ssize_t)s->addr - data_offset;
		}
		off = (ssize_t)s->addr + s->size;
	}
	off -= data_hole;
	
	/* XXX round to pagesize? */
	*modsize = roundup(off, sysconf(_SC_PAGESIZE));
	free(shstrtab);

	/* get string table length */
	strtab = read_string_table(fd, head, strtablen);
	free(strtab);

	/* get symbol table sections */
	get_symtab(&head);
	stab = head;
	resrvp->sym_symsize = 0;
	while (stab) {
		resrvp->sym_symsize += stab->size;
		stab = stab->next;
	}
	resrvp->sym_size = resrvp->sym_symsize + *strtablen;
	free_sections(head);

	return (0);
}

/*
 * Expected linker options:
 *
 * -R		executable to link against
 * -e		entry point
 * -o		output file
 * -Ttext	address to link text segment to in hex (assumes it's
 *		a page boundry)
 * -Tdata	address to link data segment to in hex
 * <target>	object file */

#define	LINKCMD		"ld -R %s -e %s -o %s -Ttext %p %s"
#define	LINKCMD2	"ld -R %s -e %s -o %s -Ttext %p -Tdata %p %s"

/* make a link command; XXX if data_offset above is non-zero, force
   data address to be at start of text + offset */
void
elf_linkcmd(char *buf,
	    size_t len,
	    const char *kernel,
	    const char *entry,
	    const char *outfile,
	    const void *address,
	    const char *object)
{
	ssize_t n;

	if (data_offset == NULL)
		n = snprintf(buf, len, LINKCMD, kernel, entry,
			     outfile, address, object);
	else
		n = snprintf(buf, len, LINKCMD2, kernel, entry,
			     outfile, address,
			     (const char*)address + data_offset, object);
	if (n >= len)
		errx(1, "link command longer than %lu bytes", (u_long)len);
}

/* load a prelinked module; returns entry point */
void *
elf_mod_load(int fd)
{
	Elf_Ehdr ehdr;
	size_t zero_size = 0;
	size_t b;
	ssize_t n;
	char *shstrtab;
	struct elf_section *head, *s;
	char buf[10 * BUFSIZ];
	void *addr = NULL;
		
	if (read_elf_header(fd, &ehdr) < 0)
		return NULL;

	shstrtab = read_shstring_table(fd, &ehdr);
	read_sections(fd, &ehdr, shstrtab, &head);
	
	for (s = head; s; s = s->next) {
		if ((s->type != SHT_STRTAB) && (s->type != SHT_SYMTAB)
		    && (s->type != SHT_DYNSYM)) {
			if (debug)
				fprintf(stderr, "loading `%s': addr = %p, "
					"size = %#lx\n",
					s->name, s->addr, (u_long)s->size);
			if (s->type == SHT_NOBITS) {
				/* skip some space */
				zero_size += s->size;
			} else {
				if (addr != NULL) {
					/*
					 * if there is a gap in the prelinked
					 * module, transfer some empty space.
					 */
					zero_size += (char*)s->addr
					 		- (char*)addr;
				}
				if (zero_size) {
					loadspace(zero_size);
					zero_size = 0;
				}
				b = s->size;
				if (lseek(fd, s->offset, SEEK_SET) == -1)
					err(1, "lseek");
				while (b) {
					n = read(fd, buf, MIN(b, sizeof(buf)));
					if (n == 0)
						errx(1, "unexpected EOF");
					if (n < 0)
						err(1, "read");
					loadbuf(buf, n);
					b -= n;
				}
				addr = (char*)s->addr + s->size;
			}
		}
	}
	if (zero_size)
		loadspace(zero_size);
	
	free_sections(head);
	free(shstrtab);
	return (void*)ehdr.e_entry;
}

extern int devfd, modfd;

void
elf_mod_symload(strtablen)
	int strtablen;
{
	Elf_Ehdr ehdr;
        char *shstrtab;
        struct elf_section *head, *s;
	char *symbuf, *strbuf;

        /*
         * Seek to the text offset to start loading...
         */
        if (lseek(modfd, 0, SEEK_SET) == -1)
                err(12, "lseek");
        if (read_elf_header(modfd, &ehdr) < 0)
                return;

        shstrtab = read_shstring_table(modfd, &ehdr);
        read_sections(modfd, &ehdr, shstrtab, &head);

        for (s = head; s; s = s->next) {
		struct elf_section *p = s;

                if ((p->type == SHT_SYMTAB) || (p->type == SHT_DYNSYM)) {
                        if (debug)
                                fprintf(stderr, "loading `%s': addr = %p, "
                                        "size = %#lx\n",
                                        s->name, s->addr, (u_long)s->size);
			/*
         		 * Seek to the file offset to start loading it...
         		 */
        		if (lseek(modfd, p->offset, SEEK_SET) == -1)
                		err(12, "lseek");
			symbuf = malloc(p->size);
			if (symbuf == 0)
                		err(13, "malloc");
			if (read(modfd, symbuf, p->size) != p->size)
                		err(14, "read");

			loadsym(symbuf, p->size);
			free(symbuf);
                }
        }

	for (s = head; s; s = s->next) {
                struct elf_section *p = s;

                if ((p->type == SHT_STRTAB)
		    && (strcmp(p->name, ".strtab") == 0 )) {
                        if (debug)
                                fprintf(stderr, "loading `%s': addr = %p, "
                                        "size = %#lx\n",
                                        s->name, s->addr, (u_long)s->size);
                        /*
                         * Seek to the file offset to start loading it...
                         */
                        if (lseek(modfd, p->offset, SEEK_SET) == -1)
                                err(12, "lseek");
                        strbuf = malloc(p->size);
                        if (strbuf == 0)
                                err(13, "malloc");
                        if (read(modfd, strbuf, p->size) != p->size)
                                err(14, "read");

                        loadsym(strbuf, p->size);
                        free(strbuf);
                }
        }

        free(shstrtab);
        free_sections(head);
        return;
}