xref: /freebsd/stand/common/load_elf_obj.c (revision 86077f4fd11070518a6d04eee7fdb93cbbfb1b52)
1 /*-
2  * Copyright (c) 2004 Ian Dowse <iedowse@freebsd.org>
3  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
4  * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/exec.h>
31 #include <sys/linker.h>
32 #include <sys/module.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <machine/elf.h>
36 #include <stand.h>
37 #include <sys/link_elf.h>
38 
39 #include "bootstrap.h"
40 #include "modinfo.h"
41 
42 #define COPYOUT(s,d,l)	archsw.arch_copyout((vm_offset_t)(s), d, l)
43 
44 #if defined(__i386__) && __ELF_WORD_SIZE == 64
45 #undef ELF_TARG_CLASS
46 #undef ELF_TARG_MACH
47 #define ELF_TARG_CLASS  ELFCLASS64
48 #define ELF_TARG_MACH   EM_X86_64
49 #endif
50 
51 typedef struct elf_file {
52 	Elf_Ehdr	hdr;
53 	Elf_Shdr	*e_shdr;
54 
55 	int		symtabindex;	/* Index of symbol table */
56 	int		shstrindex;	/* Index of section name string table */
57 
58 	int		fd;
59 	vm_offset_t	off;
60 #ifdef LOADER_VERIEXEC_VECTX
61 	struct vectx	*vctx;
62 #endif
63 } *elf_file_t;
64 
65 #ifdef LOADER_VERIEXEC_VECTX
66 #define VECTX_HANDLE(ef) (ef)->vctx
67 #else
68 #define VECTX_HANDLE(ef) (ef)->fd
69 #endif
70 
71 static int __elfN(obj_loadimage)(struct preloaded_file *mp, elf_file_t ef,
72     uint64_t loadaddr);
73 static int __elfN(obj_lookup_set)(struct preloaded_file *mp, elf_file_t ef,
74     const char *name, Elf_Addr *startp, Elf_Addr *stopp, int *countp);
75 static int __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
76     Elf_Addr p, void *val, size_t len);
77 static int __elfN(obj_parse_modmetadata)(struct preloaded_file *mp,
78     elf_file_t ef);
79 static Elf_Addr __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx);
80 
81 /*
82  * Attempt to load the file (file) as an ELF module.  It will be stored at
83  * (dest), and a pointer to a module structure describing the loaded object
84  * will be saved in (result).
85  */
86 int
__elfN(obj_loadfile)87 __elfN(obj_loadfile)(char *filename, uint64_t dest,
88     struct preloaded_file **result)
89 {
90 	struct preloaded_file *fp, *kfp;
91 	struct elf_file	ef;
92 	Elf_Ehdr *hdr;
93 	int err;
94 	ssize_t bytes_read;
95 
96 	fp = NULL;
97 	bzero(&ef, sizeof(struct elf_file));
98 
99 	/*
100 	 * Open the image, read and validate the ELF header
101 	 */
102 	if (filename == NULL)	/* can't handle nameless */
103 		return(EFTYPE);
104 	if ((ef.fd = open(filename, O_RDONLY)) == -1)
105 		return(errno);
106 #ifdef LOADER_VERIEXEC_VECTX
107 	{
108 		int verror;
109 
110 		ef.vctx = vectx_open(ef.fd, filename, 0L, NULL, &verror, __func__);
111 		if (verror) {
112 			printf("Unverified %s: %s\n", filename, ve_error_get());
113 			close(ef.fd);
114 			free(ef.vctx);
115 			return (EAUTH);
116 		}
117 	}
118 #endif
119 
120 	hdr = &ef.hdr;
121 	bytes_read = VECTX_READ(VECTX_HANDLE(&ef), hdr, sizeof(*hdr));
122 	if (bytes_read != sizeof(*hdr)) {
123 		err = EFTYPE;	/* could be EIO, but may be small file */
124 		goto oerr;
125 	}
126 
127 	/* Is it ELF? */
128 	if (!IS_ELF(*hdr)) {
129 		err = EFTYPE;
130 		goto oerr;
131 	}
132 	if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||	/* Layout ? */
133 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
134 	    hdr->e_ident[EI_VERSION] != EV_CURRENT ||	/* Version ? */
135 	    hdr->e_version != EV_CURRENT ||
136 	    hdr->e_machine != ELF_TARG_MACH ||		/* Machine ? */
137 	    hdr->e_type != ET_REL) {
138 		err = EFTYPE;
139 		goto oerr;
140 	}
141 
142 	if (hdr->e_shnum * hdr->e_shentsize == 0 || hdr->e_shoff == 0 ||
143 	    hdr->e_shentsize != sizeof(Elf_Shdr)) {
144 		err = EFTYPE;
145 		goto oerr;
146 	}
147 
148 #if defined(LOADER_VERIEXEC) && !defined(LOADER_VERIEXEC_VECTX)
149 	if (verify_file(ef.fd, filename, bytes_read, VE_MUST, __func__) < 0) {
150 		err = EAUTH;
151 		goto oerr;
152 	}
153 #endif
154 
155 	kfp = file_findfile(NULL, md_kerntype);
156 	if (kfp == NULL) {
157 		printf("elf" __XSTRING(__ELF_WORD_SIZE)
158 		    "_obj_loadfile: can't load module before kernel\n");
159 		err = EPERM;
160 		goto oerr;
161 	}
162 
163 	if (archsw.arch_loadaddr != NULL)
164 		dest = archsw.arch_loadaddr(LOAD_ELF, hdr, dest);
165 	else
166 		dest = roundup(dest, PAGE_SIZE);
167 
168 	/*
169 	 * Ok, we think we should handle this.
170 	 */
171 	fp = file_alloc();
172 	if (fp == NULL) {
173 		printf("elf" __XSTRING(__ELF_WORD_SIZE)
174 		    "_obj_loadfile: cannot allocate module info\n");
175 		err = EPERM;
176 		goto out;
177 	}
178 	fp->f_name = strdup(filename);
179 	fp->f_type = strdup(md_modtype_obj);
180 
181 	if (module_verbose > MODULE_VERBOSE_SILENT)
182 		printf("%s ", filename);
183 
184 	fp->f_size = __elfN(obj_loadimage)(fp, &ef, dest);
185 	if (fp->f_size == 0 || fp->f_addr == 0)
186 		goto ioerr;
187 
188 	/* save exec header as metadata */
189 	file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*hdr), hdr);
190 
191 	/* Load OK, return module pointer */
192 	*result = (struct preloaded_file *)fp;
193 	err = 0;
194 	goto out;
195 
196 ioerr:
197 	err = EIO;
198 oerr:
199 	file_discard(fp);
200 out:
201 #ifdef LOADER_VERIEXEC_VECTX
202 	if (!err && ef.vctx) {
203 		int verror;
204 
205 		verror = vectx_close(ef.vctx, VE_MUST, __func__);
206 		if (verror) {
207 			err = EAUTH;
208 			file_discard(fp);
209 		}
210 	}
211 #endif
212 	close(ef.fd);
213 	if (ef.e_shdr != NULL)
214 		free(ef.e_shdr);
215 
216 	return(err);
217 }
218 
219 /*
220  * With the file (fd) open on the image, and (ehdr) containing
221  * the Elf header, load the image at (off)
222  */
223 static int
__elfN(obj_loadimage)224 __elfN(obj_loadimage)(struct preloaded_file *fp, elf_file_t ef, uint64_t off)
225 {
226 	Elf_Ehdr *hdr;
227 	Elf_Shdr *shdr, *cshdr, *lshdr;
228 	vm_offset_t firstaddr, lastaddr;
229 	int i, nsym, res, ret, shdrbytes, symstrindex;
230 
231 	ret = 0;
232 	firstaddr = lastaddr = (vm_offset_t)off;
233 	hdr = &ef->hdr;
234 	ef->off = (vm_offset_t)off;
235 
236 	/* Read in the section headers. */
237 	shdrbytes = hdr->e_shnum * hdr->e_shentsize;
238 	shdr = alloc_pread(VECTX_HANDLE(ef), (off_t)hdr->e_shoff, shdrbytes);
239 	if (shdr == NULL) {
240 		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
241 		    "_obj_loadimage: read section headers failed\n");
242 		goto out;
243 	}
244 	ef->e_shdr = shdr;
245 
246 	/*
247 	 * Decide where to load everything, but don't read it yet.
248 	 * We store the load address as a non-zero sh_addr value.
249 	 * Start with the code/data and bss.
250 	 */
251 	for (i = 0; i < hdr->e_shnum; i++)
252 		shdr[i].sh_addr = 0;
253 	for (i = 0; i < hdr->e_shnum; i++) {
254 		if (shdr[i].sh_size == 0)
255 			continue;
256 		switch (shdr[i].sh_type) {
257 		case SHT_PROGBITS:
258 		case SHT_NOBITS:
259 #if defined(__i386__) || defined(__amd64__)
260 		case SHT_X86_64_UNWIND:
261 #endif
262 		case SHT_INIT_ARRAY:
263 		case SHT_FINI_ARRAY:
264 			if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
265 				break;
266 			lastaddr = roundup(lastaddr, shdr[i].sh_addralign);
267 			shdr[i].sh_addr = (Elf_Addr)lastaddr;
268 			lastaddr += shdr[i].sh_size;
269 			break;
270 		}
271 	}
272 
273 	/* Symbols. */
274 	nsym = 0;
275 	for (i = 0; i < hdr->e_shnum; i++) {
276 		switch (shdr[i].sh_type) {
277 		case SHT_SYMTAB:
278 			nsym++;
279 			ef->symtabindex = i;
280 			break;
281 		}
282 	}
283 	if (nsym != 1) {
284 		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
285 		    "_obj_loadimage: file has no valid symbol table\n");
286 		goto out;
287 	}
288 	lastaddr = roundup(lastaddr, shdr[ef->symtabindex].sh_addralign);
289 	shdr[ef->symtabindex].sh_addr = (Elf_Addr)lastaddr;
290 	lastaddr += shdr[ef->symtabindex].sh_size;
291 
292 	symstrindex = shdr[ef->symtabindex].sh_link;
293 	if (symstrindex < 0 || symstrindex >= hdr->e_shnum ||
294 	    shdr[symstrindex].sh_type != SHT_STRTAB) {
295 		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
296 		    "_obj_loadimage: file has invalid symbol strings\n");
297 		goto out;
298 	}
299 	lastaddr = roundup(lastaddr, shdr[symstrindex].sh_addralign);
300 	shdr[symstrindex].sh_addr = (Elf_Addr)lastaddr;
301 	lastaddr += shdr[symstrindex].sh_size;
302 
303 	/* Section names. */
304 	if (hdr->e_shstrndx == 0 || hdr->e_shstrndx >= hdr->e_shnum ||
305 	    shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) {
306 		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
307 		    "_obj_loadimage: file has no section names\n");
308 		goto out;
309 	}
310 	ef->shstrindex = hdr->e_shstrndx;
311 	lastaddr = roundup(lastaddr, shdr[ef->shstrindex].sh_addralign);
312 	shdr[ef->shstrindex].sh_addr = (Elf_Addr)lastaddr;
313 	lastaddr += shdr[ef->shstrindex].sh_size;
314 
315 	/* Relocation tables. */
316 	for (i = 0; i < hdr->e_shnum; i++) {
317 		switch (shdr[i].sh_type) {
318 		case SHT_REL:
319 		case SHT_RELA:
320 			if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
321 				break;
322 			lastaddr = roundup(lastaddr, shdr[i].sh_addralign);
323 			shdr[i].sh_addr = (Elf_Addr)lastaddr;
324 			lastaddr += shdr[i].sh_size;
325 			break;
326 		}
327 	}
328 
329 	/* Clear the whole area, including bss regions. */
330 	kern_bzero(firstaddr, lastaddr - firstaddr);
331 
332 	/* Figure section with the lowest file offset we haven't loaded yet. */
333 	for (cshdr = NULL; /* none */; /* none */)
334 	{
335 		/*
336 		 * Find next section to load. The complexity of this loop is
337 		 * O(n^2), but with  the number of sections being typically
338 		 * small, we do not care.
339 		 */
340 		lshdr = cshdr;
341 
342 		for (i = 0; i < hdr->e_shnum; i++) {
343 			if (shdr[i].sh_addr == 0 ||
344 			    shdr[i].sh_type == SHT_NOBITS)
345 				continue;
346 			/* Skip sections that were loaded already. */
347 			if (lshdr != NULL &&
348 			    lshdr->sh_offset >= shdr[i].sh_offset)
349 				continue;
350 			/* Find section with smallest offset. */
351 			if (cshdr == lshdr ||
352 			    cshdr->sh_offset > shdr[i].sh_offset)
353 				cshdr = &shdr[i];
354 		}
355 
356 		if (cshdr == lshdr)
357 			break;
358 
359 		if (kern_pread(VECTX_HANDLE(ef), (vm_offset_t)cshdr->sh_addr,
360 		    cshdr->sh_size, (off_t)cshdr->sh_offset) != 0) {
361 			printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
362 			    "_obj_loadimage: read failed\n");
363 			goto out;
364 		}
365 	}
366 
367 	file_addmetadata(fp, MODINFOMD_SHDR, shdrbytes, shdr);
368 
369 	res = __elfN(obj_parse_modmetadata)(fp, ef);
370 	if (res != 0)
371 		goto out;
372 
373 	ret = lastaddr - firstaddr;
374 	fp->f_addr = firstaddr;
375 
376 	if (module_verbose > MODULE_VERBOSE_SILENT)
377 		printf("size 0x%lx at 0x%lx", (u_long)ret, (u_long)firstaddr);
378 
379 out:
380 	if (module_verbose > MODULE_VERBOSE_SILENT)
381 		printf("\n");
382 	return ret;
383 }
384 
385 #if defined(__i386__) && __ELF_WORD_SIZE == 64
386 struct mod_metadata64 {
387 	int		md_version;	/* structure version MDTV_* */
388 	int		md_type;	/* type of entry MDT_* */
389 	uint64_t	md_data;	/* specific data */
390 	uint64_t	md_cval;	/* common string label */
391 };
392 #endif
393 
394 int
__elfN(obj_parse_modmetadata)395 __elfN(obj_parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef)
396 {
397 	struct mod_metadata md;
398 #if defined(__i386__) && __ELF_WORD_SIZE == 64
399 	struct mod_metadata64 md64;
400 #endif
401 	struct mod_depend *mdepend;
402 	struct mod_version mver;
403 	char *s;
404 	int error, modcnt, minfolen;
405 	Elf_Addr v, p, p_stop;
406 
407 	if (__elfN(obj_lookup_set)(fp, ef, "modmetadata_set", &p, &p_stop,
408 	    &modcnt) != 0)
409 		return 0;
410 
411 	modcnt = 0;
412 	while (p < p_stop) {
413 		COPYOUT(p, &v, sizeof(v));
414 		error = __elfN(obj_reloc_ptr)(fp, ef, p, &v, sizeof(v));
415 		if (error != 0)
416 			return (error);
417 #if defined(__i386__) && __ELF_WORD_SIZE == 64
418 		COPYOUT(v, &md64, sizeof(md64));
419 		error = __elfN(obj_reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
420 		if (error != 0)
421 			return (error);
422 		md.md_version = md64.md_version;
423 		md.md_type = md64.md_type;
424 		md.md_cval = (const char *)(uintptr_t)md64.md_cval;
425 		md.md_data = (void *)(uintptr_t)md64.md_data;
426 #else
427 		COPYOUT(v, &md, sizeof(md));
428 		error = __elfN(obj_reloc_ptr)(fp, ef, v, &md, sizeof(md));
429 		if (error != 0)
430 			return (error);
431 #endif
432 		p += sizeof(Elf_Addr);
433 		switch(md.md_type) {
434 		case MDT_DEPEND:
435 			s = strdupout((vm_offset_t)md.md_cval);
436 			minfolen = sizeof(*mdepend) + strlen(s) + 1;
437 			mdepend = malloc(minfolen);
438 			if (mdepend == NULL)
439 				return ENOMEM;
440 			COPYOUT((vm_offset_t)md.md_data, mdepend,
441 			    sizeof(*mdepend));
442 			strcpy((char*)(mdepend + 1), s);
443 			free(s);
444 			file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
445 			    mdepend);
446 			free(mdepend);
447 			break;
448 		case MDT_VERSION:
449 			s = strdupout((vm_offset_t)md.md_cval);
450 			COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
451 			file_addmodule(fp, s, mver.mv_version, NULL);
452 			free(s);
453 			modcnt++;
454 			break;
455 		case MDT_MODULE:
456 		case MDT_PNP_INFO:
457 			break;
458 		default:
459 			printf("unknown type %d\n", md.md_type);
460 			break;
461 		}
462 	}
463 	return 0;
464 }
465 
466 static int
__elfN(obj_lookup_set)467 __elfN(obj_lookup_set)(struct preloaded_file *fp, elf_file_t ef,
468     const char* name, Elf_Addr *startp, Elf_Addr *stopp, int *countp)
469 {
470 	Elf_Ehdr *hdr;
471 	Elf_Shdr *shdr;
472 	char *p;
473 	vm_offset_t shstrtab;
474 	int i;
475 
476 	hdr = &ef->hdr;
477 	shdr = ef->e_shdr;
478 	shstrtab = shdr[ef->shstrindex].sh_addr;
479 
480 	for (i = 0; i < hdr->e_shnum; i++) {
481 		if (shdr[i].sh_type != SHT_PROGBITS)
482 			continue;
483 		if (shdr[i].sh_name == 0)
484 			continue;
485 		p = strdupout(shstrtab + shdr[i].sh_name);
486 		if (strncmp(p, "set_", 4) == 0 && strcmp(p + 4, name) == 0) {
487 			*startp = shdr[i].sh_addr;
488 			*stopp = shdr[i].sh_addr +  shdr[i].sh_size;
489 			*countp = (*stopp - *startp) / sizeof(Elf_Addr);
490 			free(p);
491 			return (0);
492 		}
493 		free(p);
494 	}
495 
496 	return (ESRCH);
497 }
498 
499 /*
500  * Apply any intra-module relocations to the value. p is the load address
501  * of the value and val/len is the value to be modified. This does NOT modify
502  * the image in-place, because this is done by kern_linker later on.
503  */
504 static int
__elfN(obj_reloc_ptr)505 __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, Elf_Addr p,
506     void *val, size_t len)
507 {
508 	Elf_Ehdr *hdr;
509 	Elf_Shdr *shdr;
510 	Elf_Addr off = p;
511 	Elf_Addr base;
512 	Elf_Rela a, *abase;
513 	Elf_Rel r, *rbase;
514 	int error, i, j, nrel, nrela;
515 
516 	hdr = &ef->hdr;
517 	shdr = ef->e_shdr;
518 
519 	for (i = 0; i < hdr->e_shnum; i++) {
520 		if (shdr[i].sh_type != SHT_RELA && shdr[i].sh_type != SHT_REL)
521 			continue;
522 		base = shdr[shdr[i].sh_info].sh_addr;
523 		if (base == 0 || shdr[i].sh_addr == 0)
524 			continue;
525 		if (off < base || off + len > base +
526 		    shdr[shdr[i].sh_info].sh_size)
527 			continue;
528 
529 		switch (shdr[i].sh_type) {
530 		case SHT_RELA:
531 			abase = (Elf_Rela *)(intptr_t)shdr[i].sh_addr;
532 
533 			nrela = shdr[i].sh_size / sizeof(Elf_Rela);
534 			for (j = 0; j < nrela; j++) {
535 				COPYOUT(abase + j, &a, sizeof(a));
536 
537 				error = __elfN(reloc)(ef, __elfN(obj_symaddr),
538 				    &a, ELF_RELOC_RELA, base, off, val, len);
539 				if (error != 0)
540 					return (error);
541 			}
542 			break;
543 		case SHT_REL:
544 			rbase = (Elf_Rel *)(intptr_t)shdr[i].sh_addr;
545 
546 			nrel = shdr[i].sh_size / sizeof(Elf_Rel);
547 			for (j = 0; j < nrel; j++) {
548 				COPYOUT(rbase + j, &r, sizeof(r));
549 
550 				error = __elfN(reloc)(ef, __elfN(obj_symaddr),
551 				    &r, ELF_RELOC_REL, base, off, val, len);
552 				if (error != 0)
553 					return (error);
554 			}
555 			break;
556 		}
557 	}
558 	return (0);
559 }
560 
561 /* Look up the address of a specified symbol. */
562 static Elf_Addr
__elfN(obj_symaddr)563 __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx)
564 {
565 	Elf_Sym sym;
566 	Elf_Addr base;
567 
568 	if (symidx >= ef->e_shdr[ef->symtabindex].sh_size / sizeof(Elf_Sym))
569 		return (0);
570 	COPYOUT(ef->e_shdr[ef->symtabindex].sh_addr + symidx * sizeof(Elf_Sym),
571 	    &sym, sizeof(sym));
572 	if (sym.st_shndx == SHN_UNDEF || sym.st_shndx >= ef->hdr.e_shnum)
573 		return (0);
574 	base = ef->e_shdr[sym.st_shndx].sh_addr;
575 	if (base == 0)
576 		return (0);
577 	return (base + sym.st_value);
578 }
579