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