xref: /freebsd/stand/common/load_elf.c (revision b00ab754)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/exec.h>
34 #include <sys/linker.h>
35 #include <sys/module.h>
36 #include <sys/stdint.h>
37 #include <string.h>
38 #include <machine/elf.h>
39 #include <stand.h>
40 #define FREEBSD_ELF
41 #include <sys/link_elf.h>
42 
43 #include "bootstrap.h"
44 
45 #define COPYOUT(s,d,l)	archsw.arch_copyout((vm_offset_t)(s), d, l)
46 
47 #if defined(__i386__) && __ELF_WORD_SIZE == 64
48 #undef ELF_TARG_CLASS
49 #undef ELF_TARG_MACH
50 #define ELF_TARG_CLASS  ELFCLASS64
51 #define ELF_TARG_MACH   EM_X86_64
52 #endif
53 
54 typedef struct elf_file {
55 	Elf_Phdr	*ph;
56 	Elf_Ehdr	*ehdr;
57 	Elf_Sym		*symtab;
58 	Elf_Hashelt	*hashtab;
59 	Elf_Hashelt	nbuckets;
60 	Elf_Hashelt	nchains;
61 	Elf_Hashelt	*buckets;
62 	Elf_Hashelt	*chains;
63 	Elf_Rel	*rel;
64 	size_t	relsz;
65 	Elf_Rela	*rela;
66 	size_t	relasz;
67 	char	*strtab;
68 	size_t	strsz;
69 	int		fd;
70 	caddr_t	firstpage;
71 	size_t	firstlen;
72 	int		kernel;
73 	uint64_t	off;
74 } *elf_file_t;
75 
76 static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef,
77     uint64_t loadaddr);
78 static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef,
79     const char* name, Elf_Sym* sym);
80 static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
81     Elf_Addr p, void *val, size_t len);
82 static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef,
83     Elf_Addr p_start, Elf_Addr p_end);
84 static symaddr_fn __elfN(symaddr);
85 static char	*fake_modname(const char *name);
86 
87 const char	*__elfN(kerneltype) = "elf kernel";
88 const char	*__elfN(moduletype) = "elf module";
89 
90 uint64_t	__elfN(relocation_offset) = 0;
91 
92 extern void elf_wrong_field_size(void);
93 #define CONVERT_FIELD(b, f, e)			\
94 	switch (sizeof((b)->f)) {		\
95 	case 2:					\
96 		(b)->f = e ## 16toh((b)->f);	\
97 		break;				\
98 	case 4:					\
99 		(b)->f = e ## 32toh((b)->f);	\
100 		break;				\
101 	case 8:					\
102 		(b)->f = e ## 64toh((b)->f);	\
103 		break;				\
104 	default:				\
105 		/* Force a link time error. */	\
106 		elf_wrong_field_size();		\
107 		break;				\
108 	}
109 
110 #define CONVERT_SWITCH(h, d, f)			\
111 	switch ((h)->e_ident[EI_DATA]) {	\
112 	case ELFDATA2MSB:			\
113 		f(d, be);			\
114 		break;				\
115 	case ELFDATA2LSB:			\
116 		f(d, le);			\
117 		break;				\
118 	default:				\
119 		return (EINVAL);		\
120 	}
121 
122 
123 static int elf_header_convert(Elf_Ehdr *ehdr)
124 {
125 	/*
126 	 * Fixup ELF header endianness.
127 	 *
128 	 * The Xhdr structure was loaded using block read call to optimize file
129 	 * accesses. It might happen, that the endianness of the system memory
130 	 * is different that endianness of the ELF header.  Swap fields here to
131 	 * guarantee that Xhdr always contain valid data regardless of
132 	 * architecture.
133 	 */
134 #define HEADER_FIELDS(b, e)			\
135 	CONVERT_FIELD(b, e_type, e);		\
136 	CONVERT_FIELD(b, e_machine, e);		\
137 	CONVERT_FIELD(b, e_version, e);		\
138 	CONVERT_FIELD(b, e_entry, e);		\
139 	CONVERT_FIELD(b, e_phoff, e);		\
140 	CONVERT_FIELD(b, e_shoff, e);		\
141 	CONVERT_FIELD(b, e_flags, e);		\
142 	CONVERT_FIELD(b, e_ehsize, e);		\
143 	CONVERT_FIELD(b, e_phentsize, e);	\
144 	CONVERT_FIELD(b, e_phnum, e);		\
145 	CONVERT_FIELD(b, e_shentsize, e);	\
146 	CONVERT_FIELD(b, e_shnum, e);		\
147 	CONVERT_FIELD(b, e_shstrndx, e)
148 
149 	CONVERT_SWITCH(ehdr, ehdr, HEADER_FIELDS);
150 
151 #undef HEADER_FIELDS
152 
153 	return (0);
154 }
155 
156 static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr)
157 {
158 #define PROGRAM_HEADER_FIELDS(b, e)		\
159 	CONVERT_FIELD(b, p_type, e);		\
160 	CONVERT_FIELD(b, p_flags, e);		\
161 	CONVERT_FIELD(b, p_offset, e);		\
162 	CONVERT_FIELD(b, p_vaddr, e);		\
163 	CONVERT_FIELD(b, p_paddr, e);		\
164 	CONVERT_FIELD(b, p_filesz, e);		\
165 	CONVERT_FIELD(b, p_memsz, e);		\
166 	CONVERT_FIELD(b, p_align, e)
167 
168 	CONVERT_SWITCH(ehdr, phdr, PROGRAM_HEADER_FIELDS);
169 
170 #undef PROGRAM_HEADER_FIELDS
171 
172 	return (0);
173 }
174 
175 static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr)
176 {
177 #define SECTION_HEADER_FIELDS(b, e)		\
178 	CONVERT_FIELD(b, sh_name, e);		\
179 	CONVERT_FIELD(b, sh_type, e);		\
180 	CONVERT_FIELD(b, sh_link, e);		\
181 	CONVERT_FIELD(b, sh_info, e);		\
182 	CONVERT_FIELD(b, sh_flags, e);		\
183 	CONVERT_FIELD(b, sh_addr, e);		\
184 	CONVERT_FIELD(b, sh_offset, e);		\
185 	CONVERT_FIELD(b, sh_size, e);		\
186 	CONVERT_FIELD(b, sh_addralign, e);	\
187 	CONVERT_FIELD(b, sh_entsize, e)
188 
189 	CONVERT_SWITCH(ehdr, shdr, SECTION_HEADER_FIELDS);
190 
191 #undef SECTION_HEADER_FIELDS
192 
193 	return (0);
194 }
195 #undef CONVERT_SWITCH
196 #undef CONVERT_FIELD
197 
198 static int
199 __elfN(load_elf_header)(char *filename, elf_file_t ef)
200 {
201 	ssize_t			 bytes_read;
202 	Elf_Ehdr		*ehdr;
203 	int			 err;
204 
205 	/*
206 	 * Open the image, read and validate the ELF header
207 	 */
208 	if (filename == NULL)	/* can't handle nameless */
209 		return (EFTYPE);
210 	if ((ef->fd = open(filename, O_RDONLY)) == -1)
211 		return (errno);
212 	ef->firstpage = malloc(PAGE_SIZE);
213 	if (ef->firstpage == NULL) {
214 		close(ef->fd);
215 		return (ENOMEM);
216 	}
217 	bytes_read = read(ef->fd, ef->firstpage, PAGE_SIZE);
218 	ef->firstlen = (size_t)bytes_read;
219 	if (bytes_read < 0 || ef->firstlen <= sizeof(Elf_Ehdr)) {
220 		err = EFTYPE; /* could be EIO, but may be small file */
221 		goto error;
222 	}
223 	ehdr = ef->ehdr = (Elf_Ehdr *)ef->firstpage;
224 
225 	/* Is it ELF? */
226 	if (!IS_ELF(*ehdr)) {
227 		err = EFTYPE;
228 		goto error;
229 	}
230 
231 	if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
232 	    ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
233 	    ehdr->e_ident[EI_VERSION] != EV_CURRENT) /* Version ? */ {
234 		err = EFTYPE;
235 		goto error;
236 	}
237 
238 	err = elf_header_convert(ehdr);
239 	if (err)
240 		goto error;
241 
242 	if (ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) {
243 		/* Machine ? */
244 		err = EFTYPE;
245 		goto error;
246 	}
247 
248 	return (0);
249 
250 error:
251 	if (ef->firstpage != NULL) {
252 		free(ef->firstpage);
253 		ef->firstpage = NULL;
254 	}
255 	if (ef->fd != -1) {
256 		close(ef->fd);
257 		ef->fd = -1;
258 	}
259 	return (err);
260 }
261 
262 /*
263  * Attempt to load the file (file) as an ELF module.  It will be stored at
264  * (dest), and a pointer to a module structure describing the loaded object
265  * will be saved in (result).
266  */
267 int
268 __elfN(loadfile)(char *filename, uint64_t dest, struct preloaded_file **result)
269 {
270 	return (__elfN(loadfile_raw)(filename, dest, result, 0));
271 }
272 
273 int
274 __elfN(loadfile_raw)(char *filename, uint64_t dest,
275     struct preloaded_file **result, int multiboot)
276 {
277 	struct preloaded_file	*fp, *kfp;
278 	struct elf_file		ef;
279 	Elf_Ehdr		*ehdr;
280 	int			err;
281 
282 	fp = NULL;
283 	bzero(&ef, sizeof(struct elf_file));
284 	ef.fd = -1;
285 
286 	err = __elfN(load_elf_header)(filename, &ef);
287 	if (err != 0)
288 		return (err);
289 
290 	ehdr = ef.ehdr;
291 
292 	/*
293 	 * Check to see what sort of module we are.
294 	 */
295 	kfp = file_findfile(NULL, __elfN(kerneltype));
296 #ifdef __powerpc__
297 	/*
298 	 * Kernels can be ET_DYN, so just assume the first loaded object is the
299 	 * kernel. This assumption will be checked later.
300 	 */
301 	if (kfp == NULL)
302 		ef.kernel = 1;
303 #endif
304 	if (ef.kernel || ehdr->e_type == ET_EXEC) {
305 		/* Looks like a kernel */
306 		if (kfp != NULL) {
307 			printf("elf" __XSTRING(__ELF_WORD_SIZE)
308 			    "_loadfile: kernel already loaded\n");
309 			err = EPERM;
310 			goto oerr;
311 		}
312 		/*
313 		 * Calculate destination address based on kernel entrypoint.
314 		 *
315 		 * For ARM, the destination address is independent of any values
316 		 * in the elf header (an ARM kernel can be loaded at any 2MB
317 		 * boundary), so we leave dest set to the value calculated by
318 		 * archsw.arch_loadaddr() and passed in to this function.
319 		 */
320 #ifndef __arm__
321 		if (ehdr->e_type == ET_EXEC)
322 			dest = (ehdr->e_entry & ~PAGE_MASK);
323 #endif
324 		if ((ehdr->e_entry & ~PAGE_MASK) == 0) {
325 			printf("elf" __XSTRING(__ELF_WORD_SIZE)
326 			    "_loadfile: not a kernel (maybe static binary?)\n");
327 			err = EPERM;
328 			goto oerr;
329 		}
330 		ef.kernel = 1;
331 
332 	} else if (ehdr->e_type == ET_DYN) {
333 		/* Looks like a kld module */
334 		if (multiboot != 0) {
335 			printf("elf" __XSTRING(__ELF_WORD_SIZE)
336 			    "_loadfile: can't load module as multiboot\n");
337 			err = EPERM;
338 			goto oerr;
339 		}
340 		if (kfp == NULL) {
341 			printf("elf" __XSTRING(__ELF_WORD_SIZE)
342 			    "_loadfile: can't load module before kernel\n");
343 			err = EPERM;
344 			goto oerr;
345 		}
346 		if (strcmp(__elfN(kerneltype), kfp->f_type)) {
347 			printf("elf" __XSTRING(__ELF_WORD_SIZE)
348 			 "_loadfile: can't load module with kernel type '%s'\n",
349 			    kfp->f_type);
350 			err = EPERM;
351 			goto oerr;
352 		}
353 		/* Looks OK, got ahead */
354 		ef.kernel = 0;
355 
356 	} else {
357 		err = EFTYPE;
358 		goto oerr;
359 	}
360 
361 	if (archsw.arch_loadaddr != NULL)
362 		dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest);
363 	else
364 		dest = roundup(dest, PAGE_SIZE);
365 
366 	/*
367 	 * Ok, we think we should handle this.
368 	 */
369 	fp = file_alloc();
370 	if (fp == NULL) {
371 		printf("elf" __XSTRING(__ELF_WORD_SIZE)
372 		    "_loadfile: cannot allocate module info\n");
373 		err = EPERM;
374 		goto out;
375 	}
376 	if (ef.kernel == 1 && multiboot == 0)
377 		setenv("kernelname", filename, 1);
378 	fp->f_name = strdup(filename);
379 	if (multiboot == 0)
380 		fp->f_type = strdup(ef.kernel ?
381 		    __elfN(kerneltype) : __elfN(moduletype));
382 	else
383 		fp->f_type = strdup("elf multiboot kernel");
384 
385 #ifdef ELF_VERBOSE
386 	if (ef.kernel)
387 		printf("%s entry at 0x%jx\n", filename,
388 		    (uintmax_t)ehdr->e_entry);
389 #else
390 	printf("%s ", filename);
391 #endif
392 
393 	fp->f_size = __elfN(loadimage)(fp, &ef, dest);
394 	if (fp->f_size == 0 || fp->f_addr == 0)
395 		goto ioerr;
396 
397 	/* save exec header as metadata */
398 	file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
399 
400 	/* Load OK, return module pointer */
401 	*result = (struct preloaded_file *)fp;
402 	err = 0;
403 	goto out;
404 
405 ioerr:
406 	err = EIO;
407 oerr:
408 	file_discard(fp);
409 out:
410 	if (ef.firstpage)
411 		free(ef.firstpage);
412 	if (ef.fd != -1)
413 		close(ef.fd);
414 	return (err);
415 }
416 
417 /*
418  * With the file (fd) open on the image, and (ehdr) containing
419  * the Elf header, load the image at (off)
420  */
421 static int
422 __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, uint64_t off)
423 {
424 	int		i;
425 	u_int		j;
426 	Elf_Ehdr	*ehdr;
427 	Elf_Phdr	*phdr, *php;
428 	Elf_Shdr	*shdr;
429 	char		*shstr;
430 	int		ret;
431 	vm_offset_t	firstaddr;
432 	vm_offset_t	lastaddr;
433 	size_t		chunk;
434 	ssize_t		result;
435 	Elf_Addr	ssym, esym;
436 	Elf_Dyn		*dp;
437 	Elf_Addr	adp;
438 	Elf_Addr	ctors;
439 	int		ndp;
440 	int		symstrindex;
441 	int		symtabindex;
442 	Elf_Size	size;
443 	u_int		fpcopy;
444 	Elf_Sym		sym;
445 	Elf_Addr	p_start, p_end;
446 
447 	dp = NULL;
448 	shdr = NULL;
449 	ret = 0;
450 	firstaddr = lastaddr = 0;
451 	ehdr = ef->ehdr;
452 	if (ehdr->e_type == ET_EXEC) {
453 #if defined(__i386__) || defined(__amd64__)
454 #if __ELF_WORD_SIZE == 64
455 		/* x86_64 relocates after locore */
456 		off = - (off & 0xffffffffff000000ull);
457 #else
458 		/* i386 relocates after locore */
459 		off = - (off & 0xff000000u);
460 #endif
461 #elif defined(__powerpc__)
462 		/*
463 		 * On the purely virtual memory machines like e500, the kernel
464 		 * is linked against its final VA range, which is most often
465 		 * not available at the loader stage, but only after kernel
466 		 * initializes and completes its VM settings. In such cases we
467 		 * cannot use p_vaddr field directly to load ELF segments, but
468 		 * put them at some 'load-time' locations.
469 		 */
470 		if (off & 0xf0000000u) {
471 			off = -(off & 0xf0000000u);
472 			/*
473 			 * XXX the physical load address should not be
474 			 * hardcoded. Note that the Book-E kernel assumes that
475 			 * it's loaded at a 16MB boundary for now...
476 			 */
477 			off += 0x01000000;
478 			ehdr->e_entry += off;
479 #ifdef ELF_VERBOSE
480 			printf("Converted entry 0x%08x\n", ehdr->e_entry);
481 #endif
482 		} else
483 			off = 0;
484 #elif defined(__arm__) && !defined(EFI)
485 		/*
486 		 * The elf headers in arm kernels specify virtual addresses in
487 		 * all header fields, even the ones that should be physical
488 		 * addresses.  We assume the entry point is in the first page,
489 		 * and masking the page offset will leave us with the virtual
490 		 * address the kernel was linked at.  We subtract that from the
491 		 * load offset, making 'off' into the value which, when added
492 		 * to a virtual address in an elf header, translates it to a
493 		 * physical address.  We do the va->pa conversion on the entry
494 		 * point address in the header now, so that later we can launch
495 		 * the kernel by just jumping to that address.
496 		 *
497 		 * When booting from UEFI the copyin and copyout functions
498 		 * handle adjusting the location relative to the first virtual
499 		 * address.  Because of this there is no need to adjust the
500 		 * offset or entry point address as these will both be handled
501 		 * by the efi code.
502 		 */
503 		off -= ehdr->e_entry & ~PAGE_MASK;
504 		ehdr->e_entry += off;
505 #ifdef ELF_VERBOSE
506 		printf("ehdr->e_entry 0x%08x, va<->pa off %llx\n",
507 		    ehdr->e_entry, off);
508 #endif
509 #else
510 		off = 0;	/* other archs use direct mapped kernels */
511 #endif
512 	}
513 	ef->off = off;
514 
515 	if (ef->kernel)
516 		__elfN(relocation_offset) = off;
517 
518 	if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
519 		printf("elf" __XSTRING(__ELF_WORD_SIZE)
520 		    "_loadimage: program header not within first page\n");
521 		goto out;
522 	}
523 	phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
524 
525 	for (i = 0; i < ehdr->e_phnum; i++) {
526 		if (elf_program_header_convert(ehdr, phdr))
527 			continue;
528 
529 		/* We want to load PT_LOAD segments only.. */
530 		if (phdr[i].p_type != PT_LOAD)
531 			continue;
532 
533 #ifdef ELF_VERBOSE
534 		printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
535 		    (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
536 		    (long)(phdr[i].p_vaddr + off),
537 		    (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
538 #else
539 		if ((phdr[i].p_flags & PF_W) == 0) {
540 			printf("text=0x%lx ", (long)phdr[i].p_filesz);
541 		} else {
542 			printf("data=0x%lx", (long)phdr[i].p_filesz);
543 			if (phdr[i].p_filesz < phdr[i].p_memsz)
544 				printf("+0x%lx", (long)(phdr[i].p_memsz -
545 				    phdr[i].p_filesz));
546 			printf(" ");
547 		}
548 #endif
549 		fpcopy = 0;
550 		if (ef->firstlen > phdr[i].p_offset) {
551 			fpcopy = ef->firstlen - phdr[i].p_offset;
552 			archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
553 			    phdr[i].p_vaddr + off, fpcopy);
554 		}
555 		if (phdr[i].p_filesz > fpcopy) {
556 			if (kern_pread(ef->fd, phdr[i].p_vaddr + off + fpcopy,
557 			    phdr[i].p_filesz - fpcopy,
558 			    phdr[i].p_offset + fpcopy) != 0) {
559 				printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
560 				    "_loadimage: read failed\n");
561 				goto out;
562 			}
563 		}
564 		/* clear space from oversized segments; eg: bss */
565 		if (phdr[i].p_filesz < phdr[i].p_memsz) {
566 #ifdef ELF_VERBOSE
567 			printf(" (bss: 0x%lx-0x%lx)",
568 			    (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
569 			    (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz -1));
570 #endif
571 
572 			kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
573 			    phdr[i].p_memsz - phdr[i].p_filesz);
574 		}
575 #ifdef ELF_VERBOSE
576 		printf("\n");
577 #endif
578 
579 		if (archsw.arch_loadseg != NULL)
580 			archsw.arch_loadseg(ehdr, phdr + i, off);
581 
582 		if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
583 			firstaddr = phdr[i].p_vaddr + off;
584 		if (lastaddr == 0 || lastaddr <
585 		    (phdr[i].p_vaddr + off + phdr[i].p_memsz))
586 			lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
587 	}
588 	lastaddr = roundup(lastaddr, sizeof(long));
589 
590 	/*
591 	 * Get the section headers.  We need this for finding the .ctors
592 	 * section as well as for loading any symbols.  Both may be hard
593 	 * to do if reading from a .gz file as it involves seeking.  I
594 	 * think the rule is going to have to be that you must strip a
595 	 * file to remove symbols before gzipping it.
596 	 */
597 	chunk = (size_t)ehdr->e_shnum * (size_t)ehdr->e_shentsize;
598 	if (chunk == 0 || ehdr->e_shoff == 0)
599 		goto nosyms;
600 	shdr = alloc_pread(ef->fd, ehdr->e_shoff, chunk);
601 	if (shdr == NULL) {
602 		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
603 		    "_loadimage: failed to read section headers");
604 		goto nosyms;
605 	}
606 
607 	for (i = 0; i < ehdr->e_shnum; i++)
608 		elf_section_header_convert(ehdr, &shdr[i]);
609 
610 	file_addmetadata(fp, MODINFOMD_SHDR, chunk, shdr);
611 
612 	/*
613 	 * Read the section string table and look for the .ctors section.
614 	 * We need to tell the kernel where it is so that it can call the
615 	 * ctors.
616 	 */
617 	chunk = shdr[ehdr->e_shstrndx].sh_size;
618 	if (chunk) {
619 		shstr = alloc_pread(ef->fd, shdr[ehdr->e_shstrndx].sh_offset,
620 		    chunk);
621 		if (shstr) {
622 			for (i = 0; i < ehdr->e_shnum; i++) {
623 				if (strcmp(shstr + shdr[i].sh_name,
624 				    ".ctors") != 0)
625 					continue;
626 				ctors = shdr[i].sh_addr;
627 				file_addmetadata(fp, MODINFOMD_CTORS_ADDR,
628 				    sizeof(ctors), &ctors);
629 				size = shdr[i].sh_size;
630 				file_addmetadata(fp, MODINFOMD_CTORS_SIZE,
631 				    sizeof(size), &size);
632 				break;
633 			}
634 			free(shstr);
635 		}
636 	}
637 
638 	/*
639 	 * Now load any symbols.
640 	 */
641 	symtabindex = -1;
642 	symstrindex = -1;
643 	for (i = 0; i < ehdr->e_shnum; i++) {
644 		if (shdr[i].sh_type != SHT_SYMTAB)
645 			continue;
646 		for (j = 0; j < ehdr->e_phnum; j++) {
647 			if (phdr[j].p_type != PT_LOAD)
648 				continue;
649 			if (shdr[i].sh_offset >= phdr[j].p_offset &&
650 			    (shdr[i].sh_offset + shdr[i].sh_size <=
651 			    phdr[j].p_offset + phdr[j].p_filesz)) {
652 				shdr[i].sh_offset = 0;
653 				shdr[i].sh_size = 0;
654 				break;
655 			}
656 		}
657 		if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
658 			continue;	/* alread loaded in a PT_LOAD above */
659 		/* Save it for loading below */
660 		symtabindex = i;
661 		symstrindex = shdr[i].sh_link;
662 	}
663 	if (symtabindex < 0 || symstrindex < 0)
664 		goto nosyms;
665 
666 	/* Ok, committed to a load. */
667 #ifndef ELF_VERBOSE
668 	printf("syms=[");
669 #endif
670 	ssym = lastaddr;
671 	for (i = symtabindex; i >= 0; i = symstrindex) {
672 #ifdef ELF_VERBOSE
673 		char	*secname;
674 
675 		switch(shdr[i].sh_type) {
676 		case SHT_SYMTAB:		/* Symbol table */
677 			secname = "symtab";
678 			break;
679 		case SHT_STRTAB:		/* String table */
680 			secname = "strtab";
681 			break;
682 		default:
683 			secname = "WHOA!!";
684 			break;
685 		}
686 #endif
687 		size = shdr[i].sh_size;
688 #if defined(__powerpc__)
689   #if __ELF_WORD_SIZE == 64
690 		size = htobe64(size);
691   #else
692 		size = htobe32(size);
693   #endif
694 #endif
695 
696 		archsw.arch_copyin(&size, lastaddr, sizeof(size));
697 		lastaddr += sizeof(size);
698 
699 #ifdef ELF_VERBOSE
700 		printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
701 		    (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
702 		    (uintmax_t)lastaddr,
703 		    (uintmax_t)(lastaddr + shdr[i].sh_size));
704 #else
705 		if (i == symstrindex)
706 			printf("+");
707 		printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
708 #endif
709 
710 		if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
711 			printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
712 			   "_loadimage: could not seek for symbols - skipped!");
713 			lastaddr = ssym;
714 			ssym = 0;
715 			goto nosyms;
716 		}
717 		result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
718 		if (result < 0 || (size_t)result != shdr[i].sh_size) {
719 			printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
720 			    "_loadimage: could not read symbols - skipped! "
721 			    "(%ju != %ju)", (uintmax_t)result,
722 			    (uintmax_t)shdr[i].sh_size);
723 			lastaddr = ssym;
724 			ssym = 0;
725 			goto nosyms;
726 		}
727 		/* Reset offsets relative to ssym */
728 		lastaddr += shdr[i].sh_size;
729 		lastaddr = roundup(lastaddr, sizeof(size));
730 		if (i == symtabindex)
731 			symtabindex = -1;
732 		else if (i == symstrindex)
733 			symstrindex = -1;
734 	}
735 	esym = lastaddr;
736 #ifndef ELF_VERBOSE
737 	printf("]");
738 #endif
739 
740 #if defined(__powerpc__)
741   /* On PowerPC we always need to provide BE data to the kernel */
742   #if __ELF_WORD_SIZE == 64
743 	ssym = htobe64((uint64_t)ssym);
744 	esym = htobe64((uint64_t)esym);
745   #else
746 	ssym = htobe32((uint32_t)ssym);
747 	esym = htobe32((uint32_t)esym);
748   #endif
749 #endif
750 
751 	file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
752 	file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
753 
754 nosyms:
755 	printf("\n");
756 
757 	ret = lastaddr - firstaddr;
758 	fp->f_addr = firstaddr;
759 
760 	php = NULL;
761 	for (i = 0; i < ehdr->e_phnum; i++) {
762 		if (phdr[i].p_type == PT_DYNAMIC) {
763 			php = phdr + i;
764 			adp = php->p_vaddr;
765 			file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp),
766 			    &adp);
767 			break;
768 		}
769 	}
770 
771 	if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
772 		goto out;
773 
774 	ndp = php->p_filesz / sizeof(Elf_Dyn);
775 	if (ndp == 0)
776 		goto out;
777 	dp = malloc(php->p_filesz);
778 	if (dp == NULL)
779 		goto out;
780 	archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
781 
782 	ef->strsz = 0;
783 	for (i = 0; i < ndp; i++) {
784 		if (dp[i].d_tag == 0)
785 			break;
786 		switch (dp[i].d_tag) {
787 		case DT_HASH:
788 			ef->hashtab =
789 			    (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
790 			break;
791 		case DT_STRTAB:
792 			ef->strtab =
793 			    (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
794 			break;
795 		case DT_STRSZ:
796 			ef->strsz = dp[i].d_un.d_val;
797 			break;
798 		case DT_SYMTAB:
799 			ef->symtab =
800 			    (Elf_Sym *)(uintptr_t)(dp[i].d_un.d_ptr + off);
801 			break;
802 		case DT_REL:
803 			ef->rel =
804 			    (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
805 			break;
806 		case DT_RELSZ:
807 			ef->relsz = dp[i].d_un.d_val;
808 			break;
809 		case DT_RELA:
810 			ef->rela =
811 			    (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
812 			break;
813 		case DT_RELASZ:
814 			ef->relasz = dp[i].d_un.d_val;
815 			break;
816 		default:
817 			break;
818 		}
819 	}
820 	if (ef->hashtab == NULL || ef->symtab == NULL ||
821 	    ef->strtab == NULL || ef->strsz == 0)
822 		goto out;
823 	COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
824 	COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
825 	ef->buckets = ef->hashtab + 2;
826 	ef->chains = ef->buckets + ef->nbuckets;
827 
828 	if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set",
829 	    &sym) != 0)
830 		return 0;
831 	p_start = sym.st_value + ef->off;
832 	if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set",
833 	    &sym) != 0)
834 		return ENOENT;
835 	p_end = sym.st_value + ef->off;
836 
837 	if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0)
838 		goto out;
839 
840 	if (ef->kernel)		/* kernel must not depend on anything */
841 		goto out;
842 
843 out:
844 	if (dp)
845 		free(dp);
846 	if (shdr)
847 		free(shdr);
848 	return ret;
849 }
850 
851 static char invalid_name[] = "bad";
852 
853 char *
854 fake_modname(const char *name)
855 {
856 	const char *sp, *ep;
857 	char *fp;
858 	size_t len;
859 
860 	sp = strrchr(name, '/');
861 	if (sp)
862 		sp++;
863 	else
864 		sp = name;
865 	ep = strrchr(name, '.');
866 	if (ep) {
867 		if (ep == name) {
868 			sp = invalid_name;
869 			ep = invalid_name + sizeof(invalid_name) - 1;
870 		}
871 	} else
872 		ep = name + strlen(name);
873 	len = ep - sp;
874 	fp = malloc(len + 1);
875 	if (fp == NULL)
876 		return NULL;
877 	memcpy(fp, sp, len);
878 	fp[len] = '\0';
879 	return fp;
880 }
881 
882 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
883 struct mod_metadata64 {
884 	int		md_version;	/* structure version MDTV_* */
885 	int		md_type;	/* type of entry MDT_* */
886 	uint64_t	md_data;	/* specific data */
887 	uint64_t	md_cval;	/* common string label */
888 };
889 #endif
890 #if defined(__amd64__) && __ELF_WORD_SIZE == 32
891 struct mod_metadata32 {
892 	int		md_version;	/* structure version MDTV_* */
893 	int		md_type;	/* type of entry MDT_* */
894 	uint32_t	md_data;	/* specific data */
895 	uint32_t	md_cval;	/* common string label */
896 };
897 #endif
898 
899 int
900 __elfN(load_modmetadata)(struct preloaded_file *fp, uint64_t dest)
901 {
902 	struct elf_file		 ef;
903 	int			 err, i, j;
904 	Elf_Shdr		*sh_meta, *shdr = NULL;
905 	Elf_Shdr		*sh_data[2];
906 	char			*shstrtab = NULL;
907 	size_t			 size;
908 	Elf_Addr		 p_start, p_end;
909 
910 	bzero(&ef, sizeof(struct elf_file));
911 	ef.fd = -1;
912 
913 	err = __elfN(load_elf_header)(fp->f_name, &ef);
914 	if (err != 0)
915 		goto out;
916 
917 	if (ef.kernel == 1 || ef.ehdr->e_type == ET_EXEC) {
918 		ef.kernel = 1;
919 	} else if (ef.ehdr->e_type != ET_DYN) {
920 		err = EFTYPE;
921 		goto out;
922 	}
923 
924 	size = (size_t)ef.ehdr->e_shnum * (size_t)ef.ehdr->e_shentsize;
925 	shdr = alloc_pread(ef.fd, ef.ehdr->e_shoff, size);
926 	if (shdr == NULL) {
927 		err = ENOMEM;
928 		goto out;
929 	}
930 
931 	/* Load shstrtab. */
932 	shstrtab = alloc_pread(ef.fd, shdr[ef.ehdr->e_shstrndx].sh_offset,
933 	    shdr[ef.ehdr->e_shstrndx].sh_size);
934 	if (shstrtab == NULL) {
935 		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
936 		    "load_modmetadata: unable to load shstrtab\n");
937 		err = EFTYPE;
938 		goto out;
939 	}
940 
941 	/* Find set_modmetadata_set and data sections. */
942 	sh_data[0] = sh_data[1] = sh_meta = NULL;
943 	for (i = 0, j = 0; i < ef.ehdr->e_shnum; i++) {
944 		if (strcmp(&shstrtab[shdr[i].sh_name],
945 		    "set_modmetadata_set") == 0) {
946 			sh_meta = &shdr[i];
947 		}
948 		if ((strcmp(&shstrtab[shdr[i].sh_name], ".data") == 0) ||
949 		    (strcmp(&shstrtab[shdr[i].sh_name], ".rodata") == 0)) {
950 			sh_data[j++] = &shdr[i];
951 		}
952 	}
953 	if (sh_meta == NULL || sh_data[0] == NULL || sh_data[1] == NULL) {
954 		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
955     "load_modmetadata: unable to find set_modmetadata_set or data sections\n");
956 		err = EFTYPE;
957 		goto out;
958 	}
959 
960 	/* Load set_modmetadata_set into memory */
961 	err = kern_pread(ef.fd, dest, sh_meta->sh_size, sh_meta->sh_offset);
962 	if (err != 0) {
963 		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
964     "load_modmetadata: unable to load set_modmetadata_set: %d\n", err);
965 		goto out;
966 	}
967 	p_start = dest;
968 	p_end = dest + sh_meta->sh_size;
969 	dest += sh_meta->sh_size;
970 
971 	/* Load data sections into memory. */
972 	err = kern_pread(ef.fd, dest, sh_data[0]->sh_size,
973 	    sh_data[0]->sh_offset);
974 	if (err != 0) {
975 		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
976 		    "load_modmetadata: unable to load data: %d\n", err);
977 		goto out;
978 	}
979 
980 	/*
981 	 * We have to increment the dest, so that the offset is the same into
982 	 * both the .rodata and .data sections.
983 	 */
984 	ef.off = -(sh_data[0]->sh_addr - dest);
985 	dest +=	(sh_data[1]->sh_addr - sh_data[0]->sh_addr);
986 
987 	err = kern_pread(ef.fd, dest, sh_data[1]->sh_size,
988 	    sh_data[1]->sh_offset);
989 	if (err != 0) {
990 		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
991 		    "load_modmetadata: unable to load data: %d\n", err);
992 		goto out;
993 	}
994 
995 	err = __elfN(parse_modmetadata)(fp, &ef, p_start, p_end);
996 	if (err != 0) {
997 		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
998 		    "load_modmetadata: unable to parse metadata: %d\n", err);
999 		goto out;
1000 	}
1001 
1002 out:
1003 	if (shstrtab != NULL)
1004 		free(shstrtab);
1005 	if (shdr != NULL)
1006 		free(shdr);
1007 	if (ef.firstpage != NULL)
1008 		free(ef.firstpage);
1009 	if (ef.fd != -1)
1010 		close(ef.fd);
1011 	return (err);
1012 }
1013 
1014 int
1015 __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
1016     Elf_Addr p_start, Elf_Addr p_end)
1017 {
1018 	struct mod_metadata md;
1019 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
1020 	struct mod_metadata64 md64;
1021 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
1022 	struct mod_metadata32 md32;
1023 #endif
1024 	struct mod_depend *mdepend;
1025 	struct mod_version mver;
1026 	char *s;
1027 	int error, modcnt, minfolen;
1028 	Elf_Addr v, p;
1029 
1030 	modcnt = 0;
1031 	p = p_start;
1032 	while (p < p_end) {
1033 		COPYOUT(p, &v, sizeof(v));
1034 		error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
1035 		if (error == EOPNOTSUPP)
1036 			v += ef->off;
1037 		else if (error != 0)
1038 			return (error);
1039 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
1040 		COPYOUT(v, &md64, sizeof(md64));
1041 		error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
1042 		if (error == EOPNOTSUPP) {
1043 			md64.md_cval += ef->off;
1044 			md64.md_data += ef->off;
1045 		} else if (error != 0)
1046 			return (error);
1047 		md.md_version = md64.md_version;
1048 		md.md_type = md64.md_type;
1049 		md.md_cval = (const char *)(uintptr_t)md64.md_cval;
1050 		md.md_data = (void *)(uintptr_t)md64.md_data;
1051 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
1052 		COPYOUT(v, &md32, sizeof(md32));
1053 		error = __elfN(reloc_ptr)(fp, ef, v, &md32, sizeof(md32));
1054 		if (error == EOPNOTSUPP) {
1055 			md32.md_cval += ef->off;
1056 			md32.md_data += ef->off;
1057 		} else if (error != 0)
1058 			return (error);
1059 		md.md_version = md32.md_version;
1060 		md.md_type = md32.md_type;
1061 		md.md_cval = (const char *)(uintptr_t)md32.md_cval;
1062 		md.md_data = (void *)(uintptr_t)md32.md_data;
1063 #else
1064 		COPYOUT(v, &md, sizeof(md));
1065 		error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
1066 		if (error == EOPNOTSUPP) {
1067 			md.md_cval += ef->off;
1068 			md.md_data = (void *)((uintptr_t)md.md_data +
1069 			    (uintptr_t)ef->off);
1070 		} else if (error != 0)
1071 			return (error);
1072 #endif
1073 		p += sizeof(Elf_Addr);
1074 		switch(md.md_type) {
1075 		case MDT_DEPEND:
1076 			if (ef->kernel) /* kernel must not depend on anything */
1077 				break;
1078 			s = strdupout((vm_offset_t)md.md_cval);
1079 			minfolen = sizeof(*mdepend) + strlen(s) + 1;
1080 			mdepend = malloc(minfolen);
1081 			if (mdepend == NULL)
1082 				return ENOMEM;
1083 			COPYOUT((vm_offset_t)md.md_data, mdepend,
1084 			    sizeof(*mdepend));
1085 			strcpy((char*)(mdepend + 1), s);
1086 			free(s);
1087 			file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
1088 			    mdepend);
1089 			free(mdepend);
1090 			break;
1091 		case MDT_VERSION:
1092 			s = strdupout((vm_offset_t)md.md_cval);
1093 			COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
1094 			file_addmodule(fp, s, mver.mv_version, NULL);
1095 			free(s);
1096 			modcnt++;
1097 			break;
1098 		}
1099 	}
1100 	if (modcnt == 0) {
1101 		s = fake_modname(fp->f_name);
1102 		file_addmodule(fp, s, 1, NULL);
1103 		free(s);
1104 	}
1105 	return 0;
1106 }
1107 
1108 static unsigned long
1109 elf_hash(const char *name)
1110 {
1111 	const unsigned char *p = (const unsigned char *) name;
1112 	unsigned long h = 0;
1113 	unsigned long g;
1114 
1115 	while (*p != '\0') {
1116 		h = (h << 4) + *p++;
1117 		if ((g = h & 0xf0000000) != 0)
1118 			h ^= g >> 24;
1119 		h &= ~g;
1120 	}
1121 	return h;
1122 }
1123 
1124 static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE)
1125     "_lookup_symbol: corrupt symbol table\n";
1126 int
1127 __elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef,
1128     const char* name, Elf_Sym *symp)
1129 {
1130 	Elf_Hashelt symnum;
1131 	Elf_Sym sym;
1132 	char *strp;
1133 	unsigned long hash;
1134 
1135 	hash = elf_hash(name);
1136 	COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
1137 
1138 	while (symnum != STN_UNDEF) {
1139 		if (symnum >= ef->nchains) {
1140 			printf(__elfN(bad_symtable));
1141 			return ENOENT;
1142 		}
1143 
1144 		COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
1145 		if (sym.st_name == 0) {
1146 			printf(__elfN(bad_symtable));
1147 			return ENOENT;
1148 		}
1149 
1150 		strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
1151 		if (strcmp(name, strp) == 0) {
1152 			free(strp);
1153 			if (sym.st_shndx != SHN_UNDEF ||
1154 			    (sym.st_value != 0 &&
1155 			    ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
1156 				*symp = sym;
1157 				return 0;
1158 			}
1159 			return ENOENT;
1160 		}
1161 		free(strp);
1162 		COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
1163 	}
1164 	return ENOENT;
1165 }
1166 
1167 /*
1168  * Apply any intra-module relocations to the value. p is the load address
1169  * of the value and val/len is the value to be modified. This does NOT modify
1170  * the image in-place, because this is done by kern_linker later on.
1171  *
1172  * Returns EOPNOTSUPP if no relocation method is supplied.
1173  */
1174 static int
1175 __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
1176     Elf_Addr p, void *val, size_t len)
1177 {
1178 	size_t n;
1179 	Elf_Rela a;
1180 	Elf_Rel r;
1181 	int error;
1182 
1183 	/*
1184 	 * The kernel is already relocated, but we still want to apply
1185 	 * offset adjustments.
1186 	 */
1187 	if (ef->kernel)
1188 		return (EOPNOTSUPP);
1189 
1190 	for (n = 0; n < ef->relsz / sizeof(r); n++) {
1191 		COPYOUT(ef->rel + n, &r, sizeof(r));
1192 
1193 		error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
1194 		    ef->off, p, val, len);
1195 		if (error != 0)
1196 			return (error);
1197 	}
1198 	for (n = 0; n < ef->relasz / sizeof(a); n++) {
1199 		COPYOUT(ef->rela + n, &a, sizeof(a));
1200 
1201 		error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
1202 		    ef->off, p, val, len);
1203 		if (error != 0)
1204 			return (error);
1205 	}
1206 
1207 	return (0);
1208 }
1209 
1210 static Elf_Addr
1211 __elfN(symaddr)(struct elf_file *ef, Elf_Size symidx)
1212 {
1213 
1214 	/* Symbol lookup by index not required here. */
1215 	return (0);
1216 }
1217