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