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