xref: /freebsd/sys/kern/link_elf.c (revision 6683132d54bd6d589889e43dabdc53d35e38a028)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998-2000 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_ddb.h"
33 #include "opt_gdb.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #ifdef GPROF
38 #include <sys/gmon.h>
39 #endif
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/mount.h>
45 #include <sys/pcpu.h>
46 #include <sys/proc.h>
47 #include <sys/namei.h>
48 #include <sys/fcntl.h>
49 #include <sys/vnode.h>
50 #include <sys/linker.h>
51 
52 #include <machine/elf.h>
53 
54 #include <net/vnet.h>
55 
56 #include <security/mac/mac_framework.h>
57 
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60 #ifdef SPARSE_MAPPING
61 #include <vm/vm_object.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_extern.h>
64 #endif
65 #include <vm/pmap.h>
66 #include <vm/vm_map.h>
67 
68 #include <sys/link_elf.h>
69 
70 #ifdef DDB_CTF
71 #include <sys/zlib.h>
72 #endif
73 
74 #include "linker_if.h"
75 
76 #define MAXSEGS 4
77 
78 typedef struct elf_file {
79 	struct linker_file lf;		/* Common fields */
80 	int		preloaded;	/* Was file pre-loaded */
81 	caddr_t		address;	/* Relocation address */
82 #ifdef SPARSE_MAPPING
83 	vm_object_t	object;		/* VM object to hold file pages */
84 #endif
85 	Elf_Dyn		*dynamic;	/* Symbol table etc. */
86 	Elf_Hashelt	nbuckets;	/* DT_HASH info */
87 	Elf_Hashelt	nchains;
88 	const Elf_Hashelt *buckets;
89 	const Elf_Hashelt *chains;
90 	caddr_t		hash;
91 	caddr_t		strtab;		/* DT_STRTAB */
92 	int		strsz;		/* DT_STRSZ */
93 	const Elf_Sym	*symtab;		/* DT_SYMTAB */
94 	Elf_Addr	*got;		/* DT_PLTGOT */
95 	const Elf_Rel	*pltrel;	/* DT_JMPREL */
96 	int		pltrelsize;	/* DT_PLTRELSZ */
97 	const Elf_Rela	*pltrela;	/* DT_JMPREL */
98 	int		pltrelasize;	/* DT_PLTRELSZ */
99 	const Elf_Rel	*rel;		/* DT_REL */
100 	int		relsize;	/* DT_RELSZ */
101 	const Elf_Rela	*rela;		/* DT_RELA */
102 	int		relasize;	/* DT_RELASZ */
103 	caddr_t		modptr;
104 	const Elf_Sym	*ddbsymtab;	/* The symbol table we are using */
105 	long		ddbsymcnt;	/* Number of symbols */
106 	caddr_t		ddbstrtab;	/* String table */
107 	long		ddbstrcnt;	/* number of bytes in string table */
108 	caddr_t		symbase;	/* malloc'ed symbold base */
109 	caddr_t		strbase;	/* malloc'ed string base */
110 	caddr_t		ctftab;		/* CTF table */
111 	long		ctfcnt;		/* number of bytes in CTF table */
112 	caddr_t		ctfoff;		/* CTF offset table */
113 	caddr_t		typoff;		/* Type offset table */
114 	long		typlen;		/* Number of type entries. */
115 	Elf_Addr	pcpu_start;	/* Pre-relocation pcpu set start. */
116 	Elf_Addr	pcpu_stop;	/* Pre-relocation pcpu set stop. */
117 	Elf_Addr	pcpu_base;	/* Relocated pcpu set address. */
118 #ifdef VIMAGE
119 	Elf_Addr	vnet_start;	/* Pre-relocation vnet set start. */
120 	Elf_Addr	vnet_stop;	/* Pre-relocation vnet set stop. */
121 	Elf_Addr	vnet_base;	/* Relocated vnet set address. */
122 #endif
123 #ifdef GDB
124 	struct link_map	gdb;		/* hooks for gdb */
125 #endif
126 } *elf_file_t;
127 
128 struct elf_set {
129 	Elf_Addr	es_start;
130 	Elf_Addr	es_stop;
131 	Elf_Addr	es_base;
132 	TAILQ_ENTRY(elf_set)	es_link;
133 };
134 
135 TAILQ_HEAD(elf_set_head, elf_set);
136 
137 #include <kern/kern_ctf.c>
138 
139 static int	link_elf_link_common_finish(linker_file_t);
140 static int	link_elf_link_preload(linker_class_t cls,
141 				      const char *, linker_file_t *);
142 static int	link_elf_link_preload_finish(linker_file_t);
143 static int	link_elf_load_file(linker_class_t, const char *,
144 		    linker_file_t *);
145 static int	link_elf_lookup_symbol(linker_file_t, const char *,
146 		    c_linker_sym_t *);
147 static int	link_elf_symbol_values(linker_file_t, c_linker_sym_t,
148 		    linker_symval_t *);
149 static int	link_elf_search_symbol(linker_file_t, caddr_t,
150 		    c_linker_sym_t *, long *);
151 
152 static void	link_elf_unload_file(linker_file_t);
153 static void	link_elf_unload_preload(linker_file_t);
154 static int	link_elf_lookup_set(linker_file_t, const char *,
155 		    void ***, void ***, int *);
156 static int	link_elf_each_function_name(linker_file_t,
157 		    int (*)(const char *, void *), void *);
158 static int	link_elf_each_function_nameval(linker_file_t,
159 		    linker_function_nameval_callback_t, void *);
160 static void	link_elf_reloc_local(linker_file_t);
161 static long	link_elf_symtab_get(linker_file_t, const Elf_Sym **);
162 static long	link_elf_strtab_get(linker_file_t, caddr_t *);
163 static int	elf_lookup(linker_file_t, Elf_Size, int, Elf_Addr *);
164 
165 static kobj_method_t link_elf_methods[] = {
166 	KOBJMETHOD(linker_lookup_symbol,	link_elf_lookup_symbol),
167 	KOBJMETHOD(linker_symbol_values,	link_elf_symbol_values),
168 	KOBJMETHOD(linker_search_symbol,	link_elf_search_symbol),
169 	KOBJMETHOD(linker_unload,		link_elf_unload_file),
170 	KOBJMETHOD(linker_load_file,		link_elf_load_file),
171 	KOBJMETHOD(linker_link_preload,		link_elf_link_preload),
172 	KOBJMETHOD(linker_link_preload_finish,	link_elf_link_preload_finish),
173 	KOBJMETHOD(linker_lookup_set,		link_elf_lookup_set),
174 	KOBJMETHOD(linker_each_function_name,	link_elf_each_function_name),
175 	KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
176 	KOBJMETHOD(linker_ctf_get,		link_elf_ctf_get),
177 	KOBJMETHOD(linker_symtab_get,		link_elf_symtab_get),
178 	KOBJMETHOD(linker_strtab_get,		link_elf_strtab_get),
179 	{ 0, 0 }
180 };
181 
182 static struct linker_class link_elf_class = {
183 #if ELF_TARG_CLASS == ELFCLASS32
184 	"elf32",
185 #else
186 	"elf64",
187 #endif
188 	link_elf_methods, sizeof(struct elf_file)
189 };
190 
191 typedef int (*elf_reloc_fn)(linker_file_t lf, Elf_Addr relocbase,
192     const void *data, int type, elf_lookup_fn lookup);
193 
194 static int	parse_dynamic(elf_file_t);
195 static int	relocate_file(elf_file_t);
196 static int	relocate_file1(elf_file_t ef, elf_lookup_fn lookup,
197 		    elf_reloc_fn reloc, bool ifuncs);
198 static int	link_elf_preload_parse_symbols(elf_file_t);
199 
200 static struct elf_set_head set_pcpu_list;
201 #ifdef VIMAGE
202 static struct elf_set_head set_vnet_list;
203 #endif
204 
205 static void
206 elf_set_add(struct elf_set_head *list, Elf_Addr start, Elf_Addr stop, Elf_Addr base)
207 {
208 	struct elf_set *set, *iter;
209 
210 	set = malloc(sizeof(*set), M_LINKER, M_WAITOK);
211 	set->es_start = start;
212 	set->es_stop = stop;
213 	set->es_base = base;
214 
215 	TAILQ_FOREACH(iter, list, es_link) {
216 
217 		KASSERT((set->es_start < iter->es_start && set->es_stop < iter->es_stop) ||
218 		    (set->es_start > iter->es_start && set->es_stop > iter->es_stop),
219 		    ("linker sets intersection: to insert: 0x%jx-0x%jx; inserted: 0x%jx-0x%jx",
220 		    (uintmax_t)set->es_start, (uintmax_t)set->es_stop,
221 		    (uintmax_t)iter->es_start, (uintmax_t)iter->es_stop));
222 
223 		if (iter->es_start > set->es_start) {
224 			TAILQ_INSERT_BEFORE(iter, set, es_link);
225 			break;
226 		}
227 	}
228 
229 	if (iter == NULL)
230 		TAILQ_INSERT_TAIL(list, set, es_link);
231 }
232 
233 static int
234 elf_set_find(struct elf_set_head *list, Elf_Addr addr, Elf_Addr *start, Elf_Addr *base)
235 {
236 	struct elf_set *set;
237 
238 	TAILQ_FOREACH(set, list, es_link) {
239 		if (addr < set->es_start)
240 			return (0);
241 		if (addr < set->es_stop) {
242 			*start = set->es_start;
243 			*base = set->es_base;
244 			return (1);
245 		}
246 	}
247 
248 	return (0);
249 }
250 
251 static void
252 elf_set_delete(struct elf_set_head *list, Elf_Addr start)
253 {
254 	struct elf_set *set;
255 
256 	TAILQ_FOREACH(set, list, es_link) {
257 		if (start < set->es_start)
258 			break;
259 		if (start == set->es_start) {
260 			TAILQ_REMOVE(list, set, es_link);
261 			free(set, M_LINKER);
262 			return;
263 		}
264 	}
265 	KASSERT(0, ("deleting unknown linker set (start = 0x%jx)",
266 	    (uintmax_t)start));
267 }
268 
269 #ifdef GDB
270 static void	r_debug_state(struct r_debug *, struct link_map *);
271 
272 /*
273  * A list of loaded modules for GDB to use for loading symbols.
274  */
275 struct r_debug r_debug;
276 
277 #define GDB_STATE(s) do {				\
278 	r_debug.r_state = s; r_debug_state(NULL, NULL);	\
279 } while (0)
280 
281 /*
282  * Function for the debugger to set a breakpoint on to gain control.
283  */
284 static void
285 r_debug_state(struct r_debug *dummy_one __unused,
286 	      struct link_map *dummy_two __unused)
287 {
288 }
289 
290 static void
291 link_elf_add_gdb(struct link_map *l)
292 {
293 	struct link_map *prev;
294 
295 	l->l_next = NULL;
296 
297 	if (r_debug.r_map == NULL) {
298 		/* Add first. */
299 		l->l_prev = NULL;
300 		r_debug.r_map = l;
301 	} else {
302 		/* Append to list. */
303 		for (prev = r_debug.r_map;
304 		    prev->l_next != NULL;
305 		    prev = prev->l_next)
306 			;
307 		l->l_prev = prev;
308 		prev->l_next = l;
309 	}
310 }
311 
312 static void
313 link_elf_delete_gdb(struct link_map *l)
314 {
315 	if (l->l_prev == NULL) {
316 		/* Remove first. */
317 		if ((r_debug.r_map = l->l_next) != NULL)
318 			l->l_next->l_prev = NULL;
319 	} else {
320 		/* Remove any but first. */
321 		if ((l->l_prev->l_next = l->l_next) != NULL)
322 			l->l_next->l_prev = l->l_prev;
323 	}
324 }
325 #endif /* GDB */
326 
327 /*
328  * The kernel symbol table starts here.
329  */
330 extern struct _dynamic _DYNAMIC;
331 
332 static void
333 link_elf_error(const char *filename, const char *s)
334 {
335 	if (filename == NULL)
336 		printf("kldload: %s\n", s);
337 	else
338 		printf("kldload: %s: %s\n", filename, s);
339 }
340 
341 static void
342 link_elf_invoke_ctors(caddr_t addr, size_t size)
343 {
344 	void (**ctor)(void);
345 	size_t i, cnt;
346 
347 	if (addr == NULL || size == 0)
348 		return;
349 	cnt = size / sizeof(*ctor);
350 	ctor = (void *)addr;
351 	for (i = 0; i < cnt; i++) {
352 		if (ctor[i] != NULL)
353 			(*ctor[i])();
354 	}
355 }
356 
357 /*
358  * Actions performed after linking/loading both the preloaded kernel and any
359  * modules; whether preloaded or dynamicly loaded.
360  */
361 static int
362 link_elf_link_common_finish(linker_file_t lf)
363 {
364 #ifdef GDB
365 	elf_file_t ef = (elf_file_t)lf;
366 	char *newfilename;
367 #endif
368 	int error;
369 
370 	/* Notify MD code that a module is being loaded. */
371 	error = elf_cpu_load_file(lf);
372 	if (error != 0)
373 		return (error);
374 
375 #ifdef GDB
376 	GDB_STATE(RT_ADD);
377 	ef->gdb.l_addr = lf->address;
378 	newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
379 	strcpy(newfilename, lf->filename);
380 	ef->gdb.l_name = newfilename;
381 	ef->gdb.l_ld = ef->dynamic;
382 	link_elf_add_gdb(&ef->gdb);
383 	GDB_STATE(RT_CONSISTENT);
384 #endif
385 
386 	/* Invoke .ctors */
387 	link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
388 	return (0);
389 }
390 
391 extern vm_offset_t __startkernel, __endkernel;
392 
393 static void
394 link_elf_init(void* arg)
395 {
396 	Elf_Dyn *dp;
397 	Elf_Addr *ctors_addrp;
398 	Elf_Size *ctors_sizep;
399 	caddr_t modptr, baseptr, sizeptr;
400 	elf_file_t ef;
401 	char *modname;
402 
403 	linker_add_class(&link_elf_class);
404 
405 	dp = (Elf_Dyn *)&_DYNAMIC;
406 	modname = NULL;
407 	modptr = preload_search_by_type("elf" __XSTRING(__ELF_WORD_SIZE) " kernel");
408 	if (modptr == NULL)
409 		modptr = preload_search_by_type("elf kernel");
410 	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
411 	if (modname == NULL)
412 		modname = "kernel";
413 	linker_kernel_file = linker_make_file(modname, &link_elf_class);
414 	if (linker_kernel_file == NULL)
415 		panic("%s: Can't create linker structures for kernel",
416 		    __func__);
417 
418 	ef = (elf_file_t) linker_kernel_file;
419 	ef->preloaded = 1;
420 #ifdef __powerpc__
421 	ef->address = (caddr_t) (__startkernel - KERNBASE);
422 #else
423 	ef->address = 0;
424 #endif
425 #ifdef SPARSE_MAPPING
426 	ef->object = 0;
427 #endif
428 	ef->dynamic = dp;
429 
430 	if (dp != NULL)
431 		parse_dynamic(ef);
432 #ifdef __powerpc__
433 	linker_kernel_file->address = (caddr_t)__startkernel;
434 	linker_kernel_file->size = (intptr_t)(__endkernel - __startkernel);
435 #else
436 	linker_kernel_file->address += KERNBASE;
437 	linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
438 #endif
439 
440 	if (modptr != NULL) {
441 		ef->modptr = modptr;
442 		baseptr = preload_search_info(modptr, MODINFO_ADDR);
443 		if (baseptr != NULL)
444 			linker_kernel_file->address = *(caddr_t *)baseptr;
445 		sizeptr = preload_search_info(modptr, MODINFO_SIZE);
446 		if (sizeptr != NULL)
447 			linker_kernel_file->size = *(size_t *)sizeptr;
448 		ctors_addrp = (Elf_Addr *)preload_search_info(modptr,
449 			MODINFO_METADATA | MODINFOMD_CTORS_ADDR);
450 		ctors_sizep = (Elf_Size *)preload_search_info(modptr,
451 			MODINFO_METADATA | MODINFOMD_CTORS_SIZE);
452 		if (ctors_addrp != NULL && ctors_sizep != NULL) {
453 			linker_kernel_file->ctors_addr = ef->address +
454 			    *ctors_addrp;
455 			linker_kernel_file->ctors_size = *ctors_sizep;
456 		}
457 	}
458 	(void)link_elf_preload_parse_symbols(ef);
459 
460 #ifdef GDB
461 	r_debug.r_map = NULL;
462 	r_debug.r_brk = r_debug_state;
463 	r_debug.r_state = RT_CONSISTENT;
464 #endif
465 
466 	(void)link_elf_link_common_finish(linker_kernel_file);
467 	linker_kernel_file->flags |= LINKER_FILE_LINKED;
468 	TAILQ_INIT(&set_pcpu_list);
469 #ifdef VIMAGE
470 	TAILQ_INIT(&set_vnet_list);
471 #endif
472 }
473 
474 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_THIRD, link_elf_init, NULL);
475 
476 static int
477 link_elf_preload_parse_symbols(elf_file_t ef)
478 {
479 	caddr_t pointer;
480 	caddr_t ssym, esym, base;
481 	caddr_t strtab;
482 	int strcnt;
483 	Elf_Sym *symtab;
484 	int symcnt;
485 
486 	if (ef->modptr == NULL)
487 		return (0);
488 	pointer = preload_search_info(ef->modptr,
489 	    MODINFO_METADATA | MODINFOMD_SSYM);
490 	if (pointer == NULL)
491 		return (0);
492 	ssym = *(caddr_t *)pointer;
493 	pointer = preload_search_info(ef->modptr,
494 	    MODINFO_METADATA | MODINFOMD_ESYM);
495 	if (pointer == NULL)
496 		return (0);
497 	esym = *(caddr_t *)pointer;
498 
499 	base = ssym;
500 
501 	symcnt = *(long *)base;
502 	base += sizeof(long);
503 	symtab = (Elf_Sym *)base;
504 	base += roundup(symcnt, sizeof(long));
505 
506 	if (base > esym || base < ssym) {
507 		printf("Symbols are corrupt!\n");
508 		return (EINVAL);
509 	}
510 
511 	strcnt = *(long *)base;
512 	base += sizeof(long);
513 	strtab = base;
514 	base += roundup(strcnt, sizeof(long));
515 
516 	if (base > esym || base < ssym) {
517 		printf("Symbols are corrupt!\n");
518 		return (EINVAL);
519 	}
520 
521 	ef->ddbsymtab = symtab;
522 	ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
523 	ef->ddbstrtab = strtab;
524 	ef->ddbstrcnt = strcnt;
525 
526 	return (0);
527 }
528 
529 static int
530 parse_dynamic(elf_file_t ef)
531 {
532 	Elf_Dyn *dp;
533 	int plttype = DT_REL;
534 
535 	for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
536 		switch (dp->d_tag) {
537 		case DT_HASH:
538 		{
539 			/* From src/libexec/rtld-elf/rtld.c */
540 			const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
541 			    (ef->address + dp->d_un.d_ptr);
542 			ef->nbuckets = hashtab[0];
543 			ef->nchains = hashtab[1];
544 			ef->buckets = hashtab + 2;
545 			ef->chains = ef->buckets + ef->nbuckets;
546 			break;
547 		}
548 		case DT_STRTAB:
549 			ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
550 			break;
551 		case DT_STRSZ:
552 			ef->strsz = dp->d_un.d_val;
553 			break;
554 		case DT_SYMTAB:
555 			ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
556 			break;
557 		case DT_SYMENT:
558 			if (dp->d_un.d_val != sizeof(Elf_Sym))
559 				return (ENOEXEC);
560 			break;
561 		case DT_PLTGOT:
562 			ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
563 			break;
564 		case DT_REL:
565 			ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
566 			break;
567 		case DT_RELSZ:
568 			ef->relsize = dp->d_un.d_val;
569 			break;
570 		case DT_RELENT:
571 			if (dp->d_un.d_val != sizeof(Elf_Rel))
572 				return (ENOEXEC);
573 			break;
574 		case DT_JMPREL:
575 			ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
576 			break;
577 		case DT_PLTRELSZ:
578 			ef->pltrelsize = dp->d_un.d_val;
579 			break;
580 		case DT_RELA:
581 			ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
582 			break;
583 		case DT_RELASZ:
584 			ef->relasize = dp->d_un.d_val;
585 			break;
586 		case DT_RELAENT:
587 			if (dp->d_un.d_val != sizeof(Elf_Rela))
588 				return (ENOEXEC);
589 			break;
590 		case DT_PLTREL:
591 			plttype = dp->d_un.d_val;
592 			if (plttype != DT_REL && plttype != DT_RELA)
593 				return (ENOEXEC);
594 			break;
595 #ifdef GDB
596 		case DT_DEBUG:
597 			dp->d_un.d_ptr = (Elf_Addr)&r_debug;
598 			break;
599 #endif
600 		}
601 	}
602 
603 	if (plttype == DT_RELA) {
604 		ef->pltrela = (const Elf_Rela *)ef->pltrel;
605 		ef->pltrel = NULL;
606 		ef->pltrelasize = ef->pltrelsize;
607 		ef->pltrelsize = 0;
608 	}
609 
610 	ef->ddbsymtab = ef->symtab;
611 	ef->ddbsymcnt = ef->nchains;
612 	ef->ddbstrtab = ef->strtab;
613 	ef->ddbstrcnt = ef->strsz;
614 
615 	return (0);
616 }
617 
618 #define	LS_PADDING	0x90909090
619 static int
620 parse_dpcpu(elf_file_t ef)
621 {
622 	int error, size;
623 #if defined(__i386__)
624 	uint32_t pad;
625 #endif
626 
627 	ef->pcpu_start = 0;
628 	ef->pcpu_stop = 0;
629 	error = link_elf_lookup_set(&ef->lf, "pcpu", (void ***)&ef->pcpu_start,
630 	    (void ***)&ef->pcpu_stop, NULL);
631 	/* Error just means there is no pcpu set to relocate. */
632 	if (error != 0)
633 		return (0);
634 	size = (uintptr_t)ef->pcpu_stop - (uintptr_t)ef->pcpu_start;
635 	/* Empty set? */
636 	if (size < 1)
637 		return (0);
638 #if defined(__i386__)
639 	/* In case we do find __start/stop_set_ symbols double-check. */
640 	if (size < 4) {
641 		uprintf("Kernel module '%s' must be recompiled with "
642 		    "linker script\n", ef->lf.pathname);
643 		return (ENOEXEC);
644 	}
645 
646 	/* Padding from linker-script correct? */
647 	pad = *(uint32_t *)((uintptr_t)ef->pcpu_stop - sizeof(pad));
648 	if (pad != LS_PADDING) {
649 		uprintf("Kernel module '%s' must be recompiled with "
650 		    "linker script, invalid padding %#04x (%#04x)\n",
651 		    ef->lf.pathname, pad, LS_PADDING);
652 		return (ENOEXEC);
653 	}
654 	/* If we only have valid padding, nothing to do. */
655 	if (size == 4)
656 		return (0);
657 #endif
658 	/*
659 	 * Allocate space in the primary pcpu area.  Copy in our
660 	 * initialization from the data section and then initialize
661 	 * all per-cpu storage from that.
662 	 */
663 	ef->pcpu_base = (Elf_Addr)(uintptr_t)dpcpu_alloc(size);
664 	if (ef->pcpu_base == 0) {
665 		printf("%s: pcpu module space is out of space; "
666 		    "cannot allocate %d for %s\n",
667 		    __func__, size, ef->lf.pathname);
668 		return (ENOSPC);
669 	}
670 	memcpy((void *)ef->pcpu_base, (void *)ef->pcpu_start, size);
671 	dpcpu_copy((void *)ef->pcpu_base, size);
672 	elf_set_add(&set_pcpu_list, ef->pcpu_start, ef->pcpu_stop,
673 	    ef->pcpu_base);
674 
675 	return (0);
676 }
677 
678 #ifdef VIMAGE
679 static int
680 parse_vnet(elf_file_t ef)
681 {
682 	int error, size;
683 #if defined(__i386__)
684 	uint32_t pad;
685 #endif
686 
687 	ef->vnet_start = 0;
688 	ef->vnet_stop = 0;
689 	error = link_elf_lookup_set(&ef->lf, "vnet", (void ***)&ef->vnet_start,
690 	    (void ***)&ef->vnet_stop, NULL);
691 	/* Error just means there is no vnet data set to relocate. */
692 	if (error != 0)
693 		return (0);
694 	size = (uintptr_t)ef->vnet_stop - (uintptr_t)ef->vnet_start;
695 	/* Empty set? */
696 	if (size < 1)
697 		return (0);
698 #if defined(__i386__)
699 	/* In case we do find __start/stop_set_ symbols double-check. */
700 	if (size < 4) {
701 		uprintf("Kernel module '%s' must be recompiled with "
702 		    "linker script\n", ef->lf.pathname);
703 		return (ENOEXEC);
704 	}
705 
706 	/* Padding from linker-script correct? */
707 	pad = *(uint32_t *)((uintptr_t)ef->vnet_stop - sizeof(pad));
708 	if (pad != LS_PADDING) {
709 		uprintf("Kernel module '%s' must be recompiled with "
710 		    "linker script, invalid padding %#04x (%#04x)\n",
711 		    ef->lf.pathname, pad, LS_PADDING);
712 		return (ENOEXEC);
713 	}
714 	/* If we only have valid padding, nothing to do. */
715 	if (size == 4)
716 		return (0);
717 #endif
718 	/*
719 	 * Allocate space in the primary vnet area.  Copy in our
720 	 * initialization from the data section and then initialize
721 	 * all per-vnet storage from that.
722 	 */
723 	ef->vnet_base = (Elf_Addr)(uintptr_t)vnet_data_alloc(size);
724 	if (ef->vnet_base == 0) {
725 		printf("%s: vnet module space is out of space; "
726 		    "cannot allocate %d for %s\n",
727 		    __func__, size, ef->lf.pathname);
728 		return (ENOSPC);
729 	}
730 	memcpy((void *)ef->vnet_base, (void *)ef->vnet_start, size);
731 	vnet_data_copy((void *)ef->vnet_base, size);
732 	elf_set_add(&set_vnet_list, ef->vnet_start, ef->vnet_stop,
733 	    ef->vnet_base);
734 
735 	return (0);
736 }
737 #endif
738 #undef LS_PADDING
739 
740 static int
741 link_elf_link_preload(linker_class_t cls,
742     const char* filename, linker_file_t *result)
743 {
744 	Elf_Addr *ctors_addrp;
745 	Elf_Size *ctors_sizep;
746 	caddr_t modptr, baseptr, sizeptr, dynptr;
747 	char *type;
748 	elf_file_t ef;
749 	linker_file_t lf;
750 	int error;
751 	vm_offset_t dp;
752 
753 	/* Look to see if we have the file preloaded */
754 	modptr = preload_search_by_name(filename);
755 	if (modptr == NULL)
756 		return (ENOENT);
757 
758 	type = (char *)preload_search_info(modptr, MODINFO_TYPE);
759 	baseptr = preload_search_info(modptr, MODINFO_ADDR);
760 	sizeptr = preload_search_info(modptr, MODINFO_SIZE);
761 	dynptr = preload_search_info(modptr,
762 	    MODINFO_METADATA | MODINFOMD_DYNAMIC);
763 	if (type == NULL ||
764 	    (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " module") != 0 &&
765 	     strcmp(type, "elf module") != 0))
766 		return (EFTYPE);
767 	if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
768 		return (EINVAL);
769 
770 	lf = linker_make_file(filename, &link_elf_class);
771 	if (lf == NULL)
772 		return (ENOMEM);
773 
774 	ef = (elf_file_t) lf;
775 	ef->preloaded = 1;
776 	ef->modptr = modptr;
777 	ef->address = *(caddr_t *)baseptr;
778 #ifdef SPARSE_MAPPING
779 	ef->object = 0;
780 #endif
781 	dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
782 	ef->dynamic = (Elf_Dyn *)dp;
783 	lf->address = ef->address;
784 	lf->size = *(size_t *)sizeptr;
785 
786 	ctors_addrp = (Elf_Addr *)preload_search_info(modptr,
787 	    MODINFO_METADATA | MODINFOMD_CTORS_ADDR);
788 	ctors_sizep = (Elf_Size *)preload_search_info(modptr,
789 	    MODINFO_METADATA | MODINFOMD_CTORS_SIZE);
790 	if (ctors_addrp != NULL && ctors_sizep != NULL) {
791 		lf->ctors_addr = ef->address + *ctors_addrp;
792 		lf->ctors_size = *ctors_sizep;
793 	}
794 
795 	error = parse_dynamic(ef);
796 	if (error == 0)
797 		error = parse_dpcpu(ef);
798 #ifdef VIMAGE
799 	if (error == 0)
800 		error = parse_vnet(ef);
801 #endif
802 	if (error != 0) {
803 		linker_file_unload(lf, LINKER_UNLOAD_FORCE);
804 		return (error);
805 	}
806 	link_elf_reloc_local(lf);
807 	*result = lf;
808 	return (0);
809 }
810 
811 static int
812 link_elf_link_preload_finish(linker_file_t lf)
813 {
814 	elf_file_t ef;
815 	int error;
816 
817 	ef = (elf_file_t) lf;
818 	error = relocate_file(ef);
819 	if (error != 0)
820 		return (error);
821 	(void)link_elf_preload_parse_symbols(ef);
822 
823 	return (link_elf_link_common_finish(lf));
824 }
825 
826 static int
827 link_elf_load_file(linker_class_t cls, const char* filename,
828     linker_file_t* result)
829 {
830 	struct nameidata nd;
831 	struct thread* td = curthread;	/* XXX */
832 	Elf_Ehdr *hdr;
833 	caddr_t firstpage;
834 	int nbytes, i;
835 	Elf_Phdr *phdr;
836 	Elf_Phdr *phlimit;
837 	Elf_Phdr *segs[MAXSEGS];
838 	int nsegs;
839 	Elf_Phdr *phdyn;
840 	caddr_t mapbase;
841 	size_t mapsize;
842 	Elf_Addr base_vaddr;
843 	Elf_Addr base_vlimit;
844 	int error = 0;
845 	ssize_t resid;
846 	int flags;
847 	elf_file_t ef;
848 	linker_file_t lf;
849 	Elf_Shdr *shdr;
850 	int symtabindex;
851 	int symstrindex;
852 	int shstrindex;
853 	int symcnt;
854 	int strcnt;
855 	char *shstrs;
856 
857 	shdr = NULL;
858 	lf = NULL;
859 	shstrs = NULL;
860 
861 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
862 	flags = FREAD;
863 	error = vn_open(&nd, &flags, 0, NULL);
864 	if (error != 0)
865 		return (error);
866 	NDFREE(&nd, NDF_ONLY_PNBUF);
867 	if (nd.ni_vp->v_type != VREG) {
868 		error = ENOEXEC;
869 		firstpage = NULL;
870 		goto out;
871 	}
872 #ifdef MAC
873 	error = mac_kld_check_load(curthread->td_ucred, nd.ni_vp);
874 	if (error != 0) {
875 		firstpage = NULL;
876 		goto out;
877 	}
878 #endif
879 
880 	/*
881 	 * Read the elf header from the file.
882 	 */
883 	firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
884 	hdr = (Elf_Ehdr *)firstpage;
885 	error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
886 	    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
887 	    &resid, td);
888 	nbytes = PAGE_SIZE - resid;
889 	if (error != 0)
890 		goto out;
891 
892 	if (!IS_ELF(*hdr)) {
893 		error = ENOEXEC;
894 		goto out;
895 	}
896 
897 	if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
898 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
899 		link_elf_error(filename, "Unsupported file layout");
900 		error = ENOEXEC;
901 		goto out;
902 	}
903 	if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
904 	    hdr->e_version != EV_CURRENT) {
905 		link_elf_error(filename, "Unsupported file version");
906 		error = ENOEXEC;
907 		goto out;
908 	}
909 	if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
910 		error = ENOSYS;
911 		goto out;
912 	}
913 	if (hdr->e_machine != ELF_TARG_MACH) {
914 		link_elf_error(filename, "Unsupported machine");
915 		error = ENOEXEC;
916 		goto out;
917 	}
918 
919 	/*
920 	 * We rely on the program header being in the first page.
921 	 * This is not strictly required by the ABI specification, but
922 	 * it seems to always true in practice.  And, it simplifies
923 	 * things considerably.
924 	 */
925 	if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
926 	      (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
927 	      (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
928 		link_elf_error(filename, "Unreadable program headers");
929 
930 	/*
931 	 * Scan the program header entries, and save key information.
932 	 *
933 	 * We rely on there being exactly two load segments, text and data,
934 	 * in that order.
935 	 */
936 	phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
937 	phlimit = phdr + hdr->e_phnum;
938 	nsegs = 0;
939 	phdyn = NULL;
940 	while (phdr < phlimit) {
941 		switch (phdr->p_type) {
942 		case PT_LOAD:
943 			if (nsegs == MAXSEGS) {
944 				link_elf_error(filename, "Too many sections");
945 				error = ENOEXEC;
946 				goto out;
947 			}
948 			/*
949 			 * XXX: We just trust they come in right order ??
950 			 */
951 			segs[nsegs] = phdr;
952 			++nsegs;
953 			break;
954 
955 		case PT_DYNAMIC:
956 			phdyn = phdr;
957 			break;
958 
959 		case PT_INTERP:
960 			error = ENOSYS;
961 			goto out;
962 		}
963 
964 		++phdr;
965 	}
966 	if (phdyn == NULL) {
967 		link_elf_error(filename, "Object is not dynamically-linked");
968 		error = ENOEXEC;
969 		goto out;
970 	}
971 	if (nsegs == 0) {
972 		link_elf_error(filename, "No sections");
973 		error = ENOEXEC;
974 		goto out;
975 	}
976 
977 	/*
978 	 * Allocate the entire address space of the object, to stake
979 	 * out our contiguous region, and to establish the base
980 	 * address for relocation.
981 	 */
982 	base_vaddr = trunc_page(segs[0]->p_vaddr);
983 	base_vlimit = round_page(segs[nsegs - 1]->p_vaddr +
984 	    segs[nsegs - 1]->p_memsz);
985 	mapsize = base_vlimit - base_vaddr;
986 
987 	lf = linker_make_file(filename, &link_elf_class);
988 	if (lf == NULL) {
989 		error = ENOMEM;
990 		goto out;
991 	}
992 
993 	ef = (elf_file_t) lf;
994 #ifdef SPARSE_MAPPING
995 	ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
996 	if (ef->object == NULL) {
997 		error = ENOMEM;
998 		goto out;
999 	}
1000 	ef->address = (caddr_t) vm_map_min(kernel_map);
1001 	error = vm_map_find(kernel_map, ef->object, 0,
1002 	    (vm_offset_t *) &ef->address, mapsize, 0, VMFS_OPTIMAL_SPACE,
1003 	    VM_PROT_ALL, VM_PROT_ALL, 0);
1004 	if (error != 0) {
1005 		vm_object_deallocate(ef->object);
1006 		ef->object = 0;
1007 		goto out;
1008 	}
1009 #else
1010 	ef->address = malloc(mapsize, M_LINKER, M_EXEC | M_WAITOK);
1011 #endif
1012 	mapbase = ef->address;
1013 
1014 	/*
1015 	 * Read the text and data sections and zero the bss.
1016 	 */
1017 	for (i = 0; i < nsegs; i++) {
1018 		caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
1019 		error = vn_rdwr(UIO_READ, nd.ni_vp,
1020 		    segbase, segs[i]->p_filesz, segs[i]->p_offset,
1021 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1022 		    &resid, td);
1023 		if (error != 0)
1024 			goto out;
1025 		bzero(segbase + segs[i]->p_filesz,
1026 		    segs[i]->p_memsz - segs[i]->p_filesz);
1027 
1028 #ifdef SPARSE_MAPPING
1029 		/*
1030 		 * Wire down the pages
1031 		 */
1032 		error = vm_map_wire(kernel_map,
1033 		    (vm_offset_t) segbase,
1034 		    (vm_offset_t) segbase + segs[i]->p_memsz,
1035 		    VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
1036 		if (error != KERN_SUCCESS) {
1037 			error = ENOMEM;
1038 			goto out;
1039 		}
1040 #endif
1041 	}
1042 
1043 #ifdef GPROF
1044 	/* Update profiling information with the new text segment. */
1045 	mtx_lock(&Giant);
1046 	kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr +
1047 	    segs[0]->p_memsz));
1048 	mtx_unlock(&Giant);
1049 #endif
1050 
1051 	ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
1052 
1053 	lf->address = ef->address;
1054 	lf->size = mapsize;
1055 
1056 	error = parse_dynamic(ef);
1057 	if (error != 0)
1058 		goto out;
1059 	error = parse_dpcpu(ef);
1060 	if (error != 0)
1061 		goto out;
1062 #ifdef VIMAGE
1063 	error = parse_vnet(ef);
1064 	if (error != 0)
1065 		goto out;
1066 #endif
1067 	link_elf_reloc_local(lf);
1068 
1069 	VOP_UNLOCK(nd.ni_vp, 0);
1070 	error = linker_load_dependencies(lf);
1071 	vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1072 	if (error != 0)
1073 		goto out;
1074 	error = relocate_file(ef);
1075 	if (error != 0)
1076 		goto out;
1077 
1078 	/*
1079 	 * Try and load the symbol table if it's present.  (you can
1080 	 * strip it!)
1081 	 */
1082 	nbytes = hdr->e_shnum * hdr->e_shentsize;
1083 	if (nbytes == 0 || hdr->e_shoff == 0)
1084 		goto nosyms;
1085 	shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
1086 	error = vn_rdwr(UIO_READ, nd.ni_vp,
1087 	    (caddr_t)shdr, nbytes, hdr->e_shoff,
1088 	    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1089 	    &resid, td);
1090 	if (error != 0)
1091 		goto out;
1092 
1093 	/* Read section string table */
1094 	shstrindex = hdr->e_shstrndx;
1095 	if (shstrindex != 0 && shdr[shstrindex].sh_type == SHT_STRTAB &&
1096 	    shdr[shstrindex].sh_size != 0) {
1097 		nbytes = shdr[shstrindex].sh_size;
1098 		shstrs = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
1099 		error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shstrs, nbytes,
1100 		    shdr[shstrindex].sh_offset, UIO_SYSSPACE, IO_NODELOCKED,
1101 		    td->td_ucred, NOCRED, &resid, td);
1102 		if (error)
1103 			goto out;
1104 	}
1105 
1106 	symtabindex = -1;
1107 	symstrindex = -1;
1108 	for (i = 0; i < hdr->e_shnum; i++) {
1109 		if (shdr[i].sh_type == SHT_SYMTAB) {
1110 			symtabindex = i;
1111 			symstrindex = shdr[i].sh_link;
1112 		} else if (shstrs != NULL && shdr[i].sh_name != 0 &&
1113 		    strcmp(shstrs + shdr[i].sh_name, ".ctors") == 0) {
1114 			/* Record relocated address and size of .ctors. */
1115 			lf->ctors_addr = mapbase + shdr[i].sh_addr - base_vaddr;
1116 			lf->ctors_size = shdr[i].sh_size;
1117 		}
1118 	}
1119 	if (symtabindex < 0 || symstrindex < 0)
1120 		goto nosyms;
1121 
1122 	symcnt = shdr[symtabindex].sh_size;
1123 	ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
1124 	strcnt = shdr[symstrindex].sh_size;
1125 	ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
1126 
1127 	error = vn_rdwr(UIO_READ, nd.ni_vp,
1128 	    ef->symbase, symcnt, shdr[symtabindex].sh_offset,
1129 	    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1130 	    &resid, td);
1131 	if (error != 0)
1132 		goto out;
1133 	error = vn_rdwr(UIO_READ, nd.ni_vp,
1134 	    ef->strbase, strcnt, shdr[symstrindex].sh_offset,
1135 	    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1136 	    &resid, td);
1137 	if (error != 0)
1138 		goto out;
1139 
1140 	ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
1141 	ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
1142 	ef->ddbstrcnt = strcnt;
1143 	ef->ddbstrtab = ef->strbase;
1144 
1145 nosyms:
1146 	error = link_elf_link_common_finish(lf);
1147 	if (error != 0)
1148 		goto out;
1149 
1150 	*result = lf;
1151 
1152 out:
1153 	VOP_UNLOCK(nd.ni_vp, 0);
1154 	vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1155 	if (error != 0 && lf != NULL)
1156 		linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1157 	free(shdr, M_LINKER);
1158 	free(firstpage, M_LINKER);
1159 	free(shstrs, M_LINKER);
1160 
1161 	return (error);
1162 }
1163 
1164 Elf_Addr
1165 elf_relocaddr(linker_file_t lf, Elf_Addr x)
1166 {
1167 	elf_file_t ef;
1168 
1169 	ef = (elf_file_t)lf;
1170 	if (x >= ef->pcpu_start && x < ef->pcpu_stop)
1171 		return ((x - ef->pcpu_start) + ef->pcpu_base);
1172 #ifdef VIMAGE
1173 	if (x >= ef->vnet_start && x < ef->vnet_stop)
1174 		return ((x - ef->vnet_start) + ef->vnet_base);
1175 #endif
1176 	return (x);
1177 }
1178 
1179 
1180 static void
1181 link_elf_unload_file(linker_file_t file)
1182 {
1183 	elf_file_t ef = (elf_file_t) file;
1184 
1185 	if (ef->pcpu_base != 0) {
1186 		dpcpu_free((void *)ef->pcpu_base,
1187 		    ef->pcpu_stop - ef->pcpu_start);
1188 		elf_set_delete(&set_pcpu_list, ef->pcpu_start);
1189 	}
1190 #ifdef VIMAGE
1191 	if (ef->vnet_base != 0) {
1192 		vnet_data_free((void *)ef->vnet_base,
1193 		    ef->vnet_stop - ef->vnet_start);
1194 		elf_set_delete(&set_vnet_list, ef->vnet_start);
1195 	}
1196 #endif
1197 #ifdef GDB
1198 	if (ef->gdb.l_ld != NULL) {
1199 		GDB_STATE(RT_DELETE);
1200 		free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
1201 		link_elf_delete_gdb(&ef->gdb);
1202 		GDB_STATE(RT_CONSISTENT);
1203 	}
1204 #endif
1205 
1206 	/* Notify MD code that a module is being unloaded. */
1207 	elf_cpu_unload_file(file);
1208 
1209 	if (ef->preloaded) {
1210 		link_elf_unload_preload(file);
1211 		return;
1212 	}
1213 
1214 #ifdef SPARSE_MAPPING
1215 	if (ef->object != NULL) {
1216 		vm_map_remove(kernel_map, (vm_offset_t) ef->address,
1217 		    (vm_offset_t) ef->address
1218 		    + (ef->object->size << PAGE_SHIFT));
1219 	}
1220 #else
1221 	free(ef->address, M_LINKER);
1222 #endif
1223 	free(ef->symbase, M_LINKER);
1224 	free(ef->strbase, M_LINKER);
1225 	free(ef->ctftab, M_LINKER);
1226 	free(ef->ctfoff, M_LINKER);
1227 	free(ef->typoff, M_LINKER);
1228 }
1229 
1230 static void
1231 link_elf_unload_preload(linker_file_t file)
1232 {
1233 	if (file->pathname != NULL)
1234 		preload_delete_name(file->pathname);
1235 }
1236 
1237 static const char *
1238 symbol_name(elf_file_t ef, Elf_Size r_info)
1239 {
1240 	const Elf_Sym *ref;
1241 
1242 	if (ELF_R_SYM(r_info)) {
1243 		ref = ef->symtab + ELF_R_SYM(r_info);
1244 		return (ef->strtab + ref->st_name);
1245 	}
1246 	return (NULL);
1247 }
1248 
1249 static int
1250 symbol_type(elf_file_t ef, Elf_Size r_info)
1251 {
1252 	const Elf_Sym *ref;
1253 
1254 	if (ELF_R_SYM(r_info)) {
1255 		ref = ef->symtab + ELF_R_SYM(r_info);
1256 		return (ELF_ST_TYPE(ref->st_info));
1257 	}
1258 	return (STT_NOTYPE);
1259 }
1260 
1261 static int
1262 relocate_file1(elf_file_t ef, elf_lookup_fn lookup, elf_reloc_fn reloc,
1263     bool ifuncs)
1264 {
1265 	const Elf_Rel *rel;
1266 	const Elf_Rela *rela;
1267 	const char *symname;
1268 
1269 #define	APPLY_RELOCS(iter, tbl, tblsize, type) do {			\
1270 	for ((iter) = (tbl); (iter) != NULL &&				\
1271 	    (iter) < (tbl) + (tblsize) / sizeof(*(iter)); (iter)++) {	\
1272 		if ((symbol_type(ef, (iter)->r_info) ==			\
1273 		    STT_GNU_IFUNC ||					\
1274 		    elf_is_ifunc_reloc((iter)->r_info)) != ifuncs)	\
1275 			continue;					\
1276 		if (reloc(&ef->lf, (Elf_Addr)ef->address,		\
1277 		    (iter), (type), lookup)) {				\
1278 			symname = symbol_name(ef, (iter)->r_info);	\
1279 			printf("link_elf: symbol %s undefined\n",	\
1280 			    symname);					\
1281 			return (ENOENT);				\
1282 		}							\
1283 	}								\
1284 } while (0)
1285 
1286 	APPLY_RELOCS(rel, ef->rel, ef->relsize, ELF_RELOC_REL);
1287 	APPLY_RELOCS(rela, ef->rela, ef->relasize, ELF_RELOC_RELA);
1288 	APPLY_RELOCS(rel, ef->pltrel, ef->pltrelsize, ELF_RELOC_REL);
1289 	APPLY_RELOCS(rela, ef->pltrela, ef->pltrelasize, ELF_RELOC_RELA);
1290 
1291 #undef APPLY_RELOCS
1292 
1293 	return (0);
1294 }
1295 
1296 static int
1297 relocate_file(elf_file_t ef)
1298 {
1299 	int error;
1300 
1301 	error = relocate_file1(ef, elf_lookup, elf_reloc, false);
1302 	if (error == 0)
1303 		error = relocate_file1(ef, elf_lookup, elf_reloc, true);
1304 	return (error);
1305 }
1306 
1307 /*
1308  * Hash function for symbol table lookup.  Don't even think about changing
1309  * this.  It is specified by the System V ABI.
1310  */
1311 static unsigned long
1312 elf_hash(const char *name)
1313 {
1314 	const unsigned char *p = (const unsigned char *) name;
1315 	unsigned long h = 0;
1316 	unsigned long g;
1317 
1318 	while (*p != '\0') {
1319 		h = (h << 4) + *p++;
1320 		if ((g = h & 0xf0000000) != 0)
1321 			h ^= g >> 24;
1322 		h &= ~g;
1323 	}
1324 	return (h);
1325 }
1326 
1327 static int
1328 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1329 {
1330 	elf_file_t ef = (elf_file_t) lf;
1331 	unsigned long symnum;
1332 	const Elf_Sym* symp;
1333 	const char *strp;
1334 	unsigned long hash;
1335 	int i;
1336 
1337 	/* If we don't have a hash, bail. */
1338 	if (ef->buckets == NULL || ef->nbuckets == 0) {
1339 		printf("link_elf_lookup_symbol: missing symbol hash table\n");
1340 		return (ENOENT);
1341 	}
1342 
1343 	/* First, search hashed global symbols */
1344 	hash = elf_hash(name);
1345 	symnum = ef->buckets[hash % ef->nbuckets];
1346 
1347 	while (symnum != STN_UNDEF) {
1348 		if (symnum >= ef->nchains) {
1349 			printf("%s: corrupt symbol table\n", __func__);
1350 			return (ENOENT);
1351 		}
1352 
1353 		symp = ef->symtab + symnum;
1354 		if (symp->st_name == 0) {
1355 			printf("%s: corrupt symbol table\n", __func__);
1356 			return (ENOENT);
1357 		}
1358 
1359 		strp = ef->strtab + symp->st_name;
1360 
1361 		if (strcmp(name, strp) == 0) {
1362 			if (symp->st_shndx != SHN_UNDEF ||
1363 			    (symp->st_value != 0 &&
1364 			    (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1365 			    ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC))) {
1366 				*sym = (c_linker_sym_t) symp;
1367 				return (0);
1368 			}
1369 			return (ENOENT);
1370 		}
1371 
1372 		symnum = ef->chains[symnum];
1373 	}
1374 
1375 	/* If we have not found it, look at the full table (if loaded) */
1376 	if (ef->symtab == ef->ddbsymtab)
1377 		return (ENOENT);
1378 
1379 	/* Exhaustive search */
1380 	for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1381 		strp = ef->ddbstrtab + symp->st_name;
1382 		if (strcmp(name, strp) == 0) {
1383 			if (symp->st_shndx != SHN_UNDEF ||
1384 			    (symp->st_value != 0 &&
1385 			    (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1386 			    ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC))) {
1387 				*sym = (c_linker_sym_t) symp;
1388 				return (0);
1389 			}
1390 			return (ENOENT);
1391 		}
1392 	}
1393 
1394 	return (ENOENT);
1395 }
1396 
1397 static int
1398 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1399     linker_symval_t *symval)
1400 {
1401 	elf_file_t ef;
1402 	const Elf_Sym *es;
1403 	caddr_t val;
1404 
1405 	ef = (elf_file_t)lf;
1406 	es = (const Elf_Sym *)sym;
1407 	if (es >= ef->symtab && es < (ef->symtab + ef->nchains)) {
1408 		symval->name = ef->strtab + es->st_name;
1409 		val = (caddr_t)ef->address + es->st_value;
1410 		if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1411 			val = ((caddr_t (*)(void))val)();
1412 		symval->value = val;
1413 		symval->size = es->st_size;
1414 		return (0);
1415 	}
1416 	if (ef->symtab == ef->ddbsymtab)
1417 		return (ENOENT);
1418 	if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1419 		symval->name = ef->ddbstrtab + es->st_name;
1420 		val = (caddr_t)ef->address + es->st_value;
1421 		if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1422 			val = ((caddr_t (*)(void))val)();
1423 		symval->value = val;
1424 		symval->size = es->st_size;
1425 		return (0);
1426 	}
1427 	return (ENOENT);
1428 }
1429 
1430 static int
1431 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1432     c_linker_sym_t *sym, long *diffp)
1433 {
1434 	elf_file_t ef = (elf_file_t) lf;
1435 	u_long off = (uintptr_t) (void *) value;
1436 	u_long diff = off;
1437 	u_long st_value;
1438 	const Elf_Sym* es;
1439 	const Elf_Sym* best = NULL;
1440 	int i;
1441 
1442 	for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1443 		if (es->st_name == 0)
1444 			continue;
1445 		st_value = es->st_value + (uintptr_t) (void *) ef->address;
1446 		if (off >= st_value) {
1447 			if (off - st_value < diff) {
1448 				diff = off - st_value;
1449 				best = es;
1450 				if (diff == 0)
1451 					break;
1452 			} else if (off - st_value == diff) {
1453 				best = es;
1454 			}
1455 		}
1456 	}
1457 	if (best == NULL)
1458 		*diffp = off;
1459 	else
1460 		*diffp = diff;
1461 	*sym = (c_linker_sym_t) best;
1462 
1463 	return (0);
1464 }
1465 
1466 /*
1467  * Look up a linker set on an ELF system.
1468  */
1469 static int
1470 link_elf_lookup_set(linker_file_t lf, const char *name,
1471     void ***startp, void ***stopp, int *countp)
1472 {
1473 	c_linker_sym_t sym;
1474 	linker_symval_t symval;
1475 	char *setsym;
1476 	void **start, **stop;
1477 	int len, error = 0, count;
1478 
1479 	len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1480 	setsym = malloc(len, M_LINKER, M_WAITOK);
1481 
1482 	/* get address of first entry */
1483 	snprintf(setsym, len, "%s%s", "__start_set_", name);
1484 	error = link_elf_lookup_symbol(lf, setsym, &sym);
1485 	if (error != 0)
1486 		goto out;
1487 	link_elf_symbol_values(lf, sym, &symval);
1488 	if (symval.value == 0) {
1489 		error = ESRCH;
1490 		goto out;
1491 	}
1492 	start = (void **)symval.value;
1493 
1494 	/* get address of last entry */
1495 	snprintf(setsym, len, "%s%s", "__stop_set_", name);
1496 	error = link_elf_lookup_symbol(lf, setsym, &sym);
1497 	if (error != 0)
1498 		goto out;
1499 	link_elf_symbol_values(lf, sym, &symval);
1500 	if (symval.value == 0) {
1501 		error = ESRCH;
1502 		goto out;
1503 	}
1504 	stop = (void **)symval.value;
1505 
1506 	/* and the number of entries */
1507 	count = stop - start;
1508 
1509 	/* and copy out */
1510 	if (startp != NULL)
1511 		*startp = start;
1512 	if (stopp != NULL)
1513 		*stopp = stop;
1514 	if (countp != NULL)
1515 		*countp = count;
1516 
1517 out:
1518 	free(setsym, M_LINKER);
1519 	return (error);
1520 }
1521 
1522 static int
1523 link_elf_each_function_name(linker_file_t file,
1524   int (*callback)(const char *, void *), void *opaque)
1525 {
1526 	elf_file_t ef = (elf_file_t)file;
1527 	const Elf_Sym *symp;
1528 	int i, error;
1529 
1530 	/* Exhaustive search */
1531 	for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1532 		if (symp->st_value != 0 &&
1533 		    (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1534 		    ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1535 			error = callback(ef->ddbstrtab + symp->st_name, opaque);
1536 			if (error != 0)
1537 				return (error);
1538 		}
1539 	}
1540 	return (0);
1541 }
1542 
1543 static int
1544 link_elf_each_function_nameval(linker_file_t file,
1545     linker_function_nameval_callback_t callback, void *opaque)
1546 {
1547 	linker_symval_t symval;
1548 	elf_file_t ef = (elf_file_t)file;
1549 	const Elf_Sym* symp;
1550 	int i, error;
1551 
1552 	/* Exhaustive search */
1553 	for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1554 		if (symp->st_value != 0 &&
1555 		    (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1556 		    ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1557 			error = link_elf_symbol_values(file,
1558 			    (c_linker_sym_t) symp, &symval);
1559 			if (error != 0)
1560 				return (error);
1561 			error = callback(file, i, &symval, opaque);
1562 			if (error != 0)
1563 				return (error);
1564 		}
1565 	}
1566 	return (0);
1567 }
1568 
1569 const Elf_Sym *
1570 elf_get_sym(linker_file_t lf, Elf_Size symidx)
1571 {
1572 	elf_file_t ef = (elf_file_t)lf;
1573 
1574 	if (symidx >= ef->nchains)
1575 		return (NULL);
1576 	return (ef->symtab + symidx);
1577 }
1578 
1579 const char *
1580 elf_get_symname(linker_file_t lf, Elf_Size symidx)
1581 {
1582 	elf_file_t ef = (elf_file_t)lf;
1583 	const Elf_Sym *sym;
1584 
1585 	if (symidx >= ef->nchains)
1586 		return (NULL);
1587 	sym = ef->symtab + symidx;
1588 	return (ef->strtab + sym->st_name);
1589 }
1590 
1591 /*
1592  * Symbol lookup function that can be used when the symbol index is known (ie
1593  * in relocations). It uses the symbol index instead of doing a fully fledged
1594  * hash table based lookup when such is valid. For example for local symbols.
1595  * This is not only more efficient, it's also more correct. It's not always
1596  * the case that the symbol can be found through the hash table.
1597  */
1598 static int
1599 elf_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1600 {
1601 	elf_file_t ef = (elf_file_t)lf;
1602 	const Elf_Sym *sym;
1603 	const char *symbol;
1604 	Elf_Addr addr, start, base;
1605 
1606 	/* Don't even try to lookup the symbol if the index is bogus. */
1607 	if (symidx >= ef->nchains) {
1608 		*res = 0;
1609 		return (EINVAL);
1610 	}
1611 
1612 	sym = ef->symtab + symidx;
1613 
1614 	/*
1615 	 * Don't do a full lookup when the symbol is local. It may even
1616 	 * fail because it may not be found through the hash table.
1617 	 */
1618 	if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1619 		/* Force lookup failure when we have an insanity. */
1620 		if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0) {
1621 			*res = 0;
1622 			return (EINVAL);
1623 		}
1624 		*res = ((Elf_Addr)ef->address + sym->st_value);
1625 		return (0);
1626 	}
1627 
1628 	/*
1629 	 * XXX we can avoid doing a hash table based lookup for global
1630 	 * symbols as well. This however is not always valid, so we'll
1631 	 * just do it the hard way for now. Performance tweaks can
1632 	 * always be added.
1633 	 */
1634 
1635 	symbol = ef->strtab + sym->st_name;
1636 
1637 	/* Force a lookup failure if the symbol name is bogus. */
1638 	if (*symbol == 0) {
1639 		*res = 0;
1640 		return (EINVAL);
1641 	}
1642 
1643 	addr = ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1644 	if (addr == 0 && ELF_ST_BIND(sym->st_info) != STB_WEAK) {
1645 		*res = 0;
1646 		return (EINVAL);
1647 	}
1648 
1649 	if (elf_set_find(&set_pcpu_list, addr, &start, &base))
1650 		addr = addr - start + base;
1651 #ifdef VIMAGE
1652 	else if (elf_set_find(&set_vnet_list, addr, &start, &base))
1653 		addr = addr - start + base;
1654 #endif
1655 	*res = addr;
1656 	return (0);
1657 }
1658 
1659 static void
1660 link_elf_reloc_local(linker_file_t lf)
1661 {
1662 	const Elf_Rel *rellim;
1663 	const Elf_Rel *rel;
1664 	const Elf_Rela *relalim;
1665 	const Elf_Rela *rela;
1666 	elf_file_t ef = (elf_file_t)lf;
1667 
1668 	/* Perform relocations without addend if there are any: */
1669 	if ((rel = ef->rel) != NULL) {
1670 		rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
1671 		while (rel < rellim) {
1672 			elf_reloc_local(lf, (Elf_Addr)ef->address, rel,
1673 			    ELF_RELOC_REL, elf_lookup);
1674 			rel++;
1675 		}
1676 	}
1677 
1678 	/* Perform relocations with addend if there are any: */
1679 	if ((rela = ef->rela) != NULL) {
1680 		relalim = (const Elf_Rela *)
1681 		    ((const char *)ef->rela + ef->relasize);
1682 		while (rela < relalim) {
1683 			elf_reloc_local(lf, (Elf_Addr)ef->address, rela,
1684 			    ELF_RELOC_RELA, elf_lookup);
1685 			rela++;
1686 		}
1687 	}
1688 }
1689 
1690 static long
1691 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1692 {
1693 	elf_file_t ef = (elf_file_t)lf;
1694 
1695 	*symtab = ef->ddbsymtab;
1696 
1697 	if (*symtab == NULL)
1698 		return (0);
1699 
1700 	return (ef->ddbsymcnt);
1701 }
1702 
1703 static long
1704 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1705 {
1706 	elf_file_t ef = (elf_file_t)lf;
1707 
1708 	*strtab = ef->ddbstrtab;
1709 
1710 	if (*strtab == NULL)
1711 		return (0);
1712 
1713 	return (ef->ddbstrcnt);
1714 }
1715 
1716 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
1717 /*
1718  * Use this lookup routine when performing relocations early during boot.
1719  * The generic lookup routine depends on kobj, which is not initialized
1720  * at that point.
1721  */
1722 static int
1723 elf_lookup_ifunc(linker_file_t lf, Elf_Size symidx, int deps __unused,
1724     Elf_Addr *res)
1725 {
1726 	elf_file_t ef;
1727 	const Elf_Sym *symp;
1728 	caddr_t val;
1729 
1730 	ef = (elf_file_t)lf;
1731 	symp = ef->symtab + symidx;
1732 	if (ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC) {
1733 		val = (caddr_t)ef->address + symp->st_value;
1734 		*res = ((Elf_Addr (*)(void))val)();
1735 		return (0);
1736 	}
1737 	return (ENOENT);
1738 }
1739 
1740 void
1741 link_elf_ireloc(caddr_t kmdp)
1742 {
1743 	struct elf_file eff;
1744 	elf_file_t ef;
1745 
1746 	ef = &eff;
1747 
1748 	bzero_early(ef, sizeof(*ef));
1749 
1750 	ef->modptr = kmdp;
1751 	ef->dynamic = (Elf_Dyn *)&_DYNAMIC;
1752 	parse_dynamic(ef);
1753 	ef->address = 0;
1754 	link_elf_preload_parse_symbols(ef);
1755 	relocate_file1(ef, elf_lookup_ifunc, elf_reloc, true);
1756 }
1757 #endif
1758