xref: /freebsd/sys/kern/link_elf_obj.c (revision a91a2465)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998-2000 Doug Rabson
5  * Copyright (c) 2004 Peter Wemm
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include "opt_ddb.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/fcntl.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/linker.h>
39 #include <sys/mutex.h>
40 #include <sys/mount.h>
41 #include <sys/namei.h>
42 #include <sys/proc.h>
43 #include <sys/rwlock.h>
44 #include <sys/sysctl.h>
45 #include <sys/vnode.h>
46 
47 #include <machine/elf.h>
48 
49 #include <net/vnet.h>
50 
51 #include <security/mac/mac_framework.h>
52 
53 #include <vm/vm.h>
54 #include <vm/vm_param.h>
55 #include <vm/pmap.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_object.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_pager.h>
62 
63 #include <sys/link_elf.h>
64 
65 #ifdef DDB_CTF
66 #include <contrib/zlib/zlib.h>
67 #endif
68 
69 #include "linker_if.h"
70 
71 typedef struct {
72 	void		*addr;
73 	Elf_Off		size;
74 	int		flags;	/* Section flags. */
75 	int		sec;	/* Original section number. */
76 	char		*name;
77 } Elf_progent;
78 
79 typedef struct {
80 	Elf_Rel		*rel;
81 	int		nrel;
82 	int		sec;
83 } Elf_relent;
84 
85 typedef struct {
86 	Elf_Rela	*rela;
87 	int		nrela;
88 	int		sec;
89 } Elf_relaent;
90 
91 typedef struct elf_file {
92 	struct linker_file lf;		/* Common fields */
93 
94 	int		preloaded;
95 	caddr_t		address;	/* Relocation address */
96 	vm_object_t	object;		/* VM object to hold file pages */
97 	Elf_Shdr	*e_shdr;
98 
99 	Elf_progent	*progtab;
100 	u_int		nprogtab;
101 
102 	Elf_relaent	*relatab;
103 	u_int		nrelatab;
104 
105 	Elf_relent	*reltab;
106 	int		nreltab;
107 
108 	Elf_Sym		*ddbsymtab;	/* The symbol table we are using */
109 	long		ddbsymcnt;	/* Number of symbols */
110 	caddr_t		ddbstrtab;	/* String table */
111 	long		ddbstrcnt;	/* number of bytes in string table */
112 
113 	caddr_t		shstrtab;	/* Section name string table */
114 	long		shstrcnt;	/* number of bytes in string table */
115 
116 	caddr_t		ctftab;		/* CTF table */
117 	long		ctfcnt;		/* number of bytes in CTF table */
118 	caddr_t		ctfoff;		/* CTF offset table */
119 	caddr_t		typoff;		/* Type offset table */
120 	long		typlen;		/* Number of type entries. */
121 
122 } *elf_file_t;
123 
124 #include <kern/kern_ctf.c>
125 
126 static int	link_elf_link_preload(linker_class_t cls,
127 		    const char *, linker_file_t *);
128 static int	link_elf_link_preload_finish(linker_file_t);
129 static int	link_elf_load_file(linker_class_t, const char *, linker_file_t *);
130 static int	link_elf_lookup_symbol(linker_file_t, const char *,
131 		    c_linker_sym_t *);
132 static int	link_elf_lookup_debug_symbol(linker_file_t, const char *,
133 		    c_linker_sym_t *);
134 static int	link_elf_symbol_values(linker_file_t, c_linker_sym_t,
135 		    linker_symval_t *);
136 static int	link_elf_debug_symbol_values(linker_file_t, c_linker_sym_t,
137 		    linker_symval_t *);
138 static int	link_elf_search_symbol(linker_file_t, caddr_t value,
139 		    c_linker_sym_t *sym, long *diffp);
140 
141 static void	link_elf_unload_file(linker_file_t);
142 static int	link_elf_lookup_set(linker_file_t, const char *,
143 		    void ***, void ***, int *);
144 static int	link_elf_each_function_name(linker_file_t,
145 		    int (*)(const char *, void *), void *);
146 static int	link_elf_each_function_nameval(linker_file_t,
147 				linker_function_nameval_callback_t,
148 				void *);
149 static int	link_elf_reloc_local(linker_file_t, bool);
150 static long	link_elf_symtab_get(linker_file_t, const Elf_Sym **);
151 static long	link_elf_strtab_get(linker_file_t, caddr_t *);
152 #ifdef VIMAGE
153 static void	link_elf_propagate_vnets(linker_file_t);
154 #endif
155 
156 static int	elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps,
157 		    Elf_Addr *);
158 
159 static kobj_method_t link_elf_methods[] = {
160 	KOBJMETHOD(linker_lookup_symbol,	link_elf_lookup_symbol),
161 	KOBJMETHOD(linker_lookup_debug_symbol,	link_elf_lookup_debug_symbol),
162 	KOBJMETHOD(linker_symbol_values,	link_elf_symbol_values),
163 	KOBJMETHOD(linker_debug_symbol_values,	link_elf_debug_symbol_values),
164 	KOBJMETHOD(linker_search_symbol,	link_elf_search_symbol),
165 	KOBJMETHOD(linker_unload,		link_elf_unload_file),
166 	KOBJMETHOD(linker_load_file,		link_elf_load_file),
167 	KOBJMETHOD(linker_link_preload,		link_elf_link_preload),
168 	KOBJMETHOD(linker_link_preload_finish,	link_elf_link_preload_finish),
169 	KOBJMETHOD(linker_lookup_set,		link_elf_lookup_set),
170 	KOBJMETHOD(linker_each_function_name,	link_elf_each_function_name),
171 	KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
172 	KOBJMETHOD(linker_ctf_get,		link_elf_ctf_get),
173 	KOBJMETHOD(linker_symtab_get, 		link_elf_symtab_get),
174 	KOBJMETHOD(linker_strtab_get, 		link_elf_strtab_get),
175 #ifdef VIMAGE
176 	KOBJMETHOD(linker_propagate_vnets,	link_elf_propagate_vnets),
177 #endif
178 	KOBJMETHOD_END
179 };
180 
181 static struct linker_class link_elf_class = {
182 #if ELF_TARG_CLASS == ELFCLASS32
183 	"elf32_obj",
184 #else
185 	"elf64_obj",
186 #endif
187 	link_elf_methods, sizeof(struct elf_file)
188 };
189 
190 static bool link_elf_obj_leak_locals = true;
191 SYSCTL_BOOL(_debug, OID_AUTO, link_elf_obj_leak_locals,
192     CTLFLAG_RWTUN, &link_elf_obj_leak_locals, 0,
193     "Allow local symbols to participate in global module symbol resolution");
194 
195 static int	relocate_file(elf_file_t ef);
196 static void	elf_obj_cleanup_globals_cache(elf_file_t);
197 
198 static void
199 link_elf_error(const char *filename, const char *s)
200 {
201 	if (filename == NULL)
202 		printf("kldload: %s\n", s);
203 	else
204 		printf("kldload: %s: %s\n", filename, s);
205 }
206 
207 static void
208 link_elf_init(void *arg)
209 {
210 
211 	linker_add_class(&link_elf_class);
212 }
213 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, NULL);
214 
215 static void
216 link_elf_protect_range(elf_file_t ef, vm_offset_t start, vm_offset_t end,
217     vm_prot_t prot)
218 {
219 	int error __unused;
220 
221 	KASSERT(start <= end && start >= (vm_offset_t)ef->address &&
222 	    end <= round_page((vm_offset_t)ef->address + ef->lf.size),
223 	    ("link_elf_protect_range: invalid range %#jx-%#jx",
224 	    (uintmax_t)start, (uintmax_t)end));
225 
226 	if (start == end)
227 		return;
228 	if (ef->preloaded) {
229 #ifdef __amd64__
230 		error = pmap_change_prot(start, end - start, prot);
231 		KASSERT(error == 0,
232 		    ("link_elf_protect_range: pmap_change_prot() returned %d",
233 		    error));
234 #endif
235 		return;
236 	}
237 	error = vm_map_protect(kernel_map, start, end, prot, 0,
238 	    VM_MAP_PROTECT_SET_PROT);
239 	KASSERT(error == KERN_SUCCESS,
240 	    ("link_elf_protect_range: vm_map_protect() returned %d", error));
241 }
242 
243 /*
244  * Restrict permissions on linker file memory based on section flags.
245  * Sections need not be page-aligned, so overlap within a page is possible.
246  */
247 static void
248 link_elf_protect(elf_file_t ef)
249 {
250 	vm_offset_t end, segend, segstart, start;
251 	vm_prot_t gapprot, prot, segprot;
252 	int i;
253 
254 	/*
255 	 * If the file was preloaded, the last page may contain other preloaded
256 	 * data which may need to be writeable.  ELF files are always
257 	 * page-aligned, but other preloaded data, such as entropy or CPU
258 	 * microcode may be loaded with a smaller alignment.
259 	 */
260 	gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ;
261 
262 	start = end = (vm_offset_t)ef->address;
263 	prot = VM_PROT_READ;
264 	for (i = 0; i < ef->nprogtab; i++) {
265 		/*
266 		 * VNET and DPCPU sections have their memory allocated by their
267 		 * respective subsystems.
268 		 */
269 		if (ef->progtab[i].name != NULL && (
270 #ifdef VIMAGE
271 		    strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 ||
272 #endif
273 		    strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0))
274 			continue;
275 
276 		segstart = trunc_page((vm_offset_t)ef->progtab[i].addr);
277 		segend = round_page((vm_offset_t)ef->progtab[i].addr +
278 		    ef->progtab[i].size);
279 		segprot = VM_PROT_READ;
280 		if ((ef->progtab[i].flags & SHF_WRITE) != 0)
281 			segprot |= VM_PROT_WRITE;
282 		if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0)
283 			segprot |= VM_PROT_EXECUTE;
284 
285 		if (end <= segstart) {
286 			/*
287 			 * Case 1: there is no overlap between the previous
288 			 * segment and this one.  Apply protections to the
289 			 * previous segment, and protect the gap between the
290 			 * previous and current segments, if any.
291 			 */
292 			link_elf_protect_range(ef, start, end, prot);
293 			link_elf_protect_range(ef, end, segstart, gapprot);
294 
295 			start = segstart;
296 			end = segend;
297 			prot = segprot;
298 		} else if (start < segstart && end == segend) {
299 			/*
300 			 * Case 2: the current segment is a subrange of the
301 			 * previous segment.  Apply protections to the
302 			 * non-overlapping portion of the previous segment.
303 			 */
304 			link_elf_protect_range(ef, start, segstart, prot);
305 
306 			start = segstart;
307 			prot |= segprot;
308 		} else if (end < segend) {
309 			/*
310 			 * Case 3: there is partial overlap between the previous
311 			 * and current segments.  Apply protections to the
312 			 * non-overlapping portion of the previous segment, and
313 			 * then the overlap, which must use the union of the two
314 			 * segments' protections.
315 			 */
316 			link_elf_protect_range(ef, start, segstart, prot);
317 			link_elf_protect_range(ef, segstart, end,
318 			    prot | segprot);
319 			start = end;
320 			end = segend;
321 			prot = segprot;
322 		} else {
323 			/*
324 			 * Case 4: the two segments reside in the same page.
325 			 */
326 			prot |= segprot;
327 		}
328 	}
329 
330 	/*
331 	 * Fix up the last unprotected segment and trailing data.
332 	 */
333 	link_elf_protect_range(ef, start, end, prot);
334 	link_elf_protect_range(ef, end,
335 	    round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
336 }
337 
338 static int
339 link_elf_link_preload(linker_class_t cls, const char *filename,
340     linker_file_t *result)
341 {
342 	Elf_Ehdr *hdr;
343 	Elf_Shdr *shdr;
344 	Elf_Sym *es;
345 	void *modptr, *baseptr, *sizeptr;
346 	char *type;
347 	elf_file_t ef;
348 	linker_file_t lf;
349 	Elf_Addr off;
350 	int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
351 
352 	/* Look to see if we have the file preloaded */
353 	modptr = preload_search_by_name(filename);
354 	if (modptr == NULL)
355 		return ENOENT;
356 
357 	type = (char *)preload_search_info(modptr, MODINFO_TYPE);
358 	baseptr = preload_search_info(modptr, MODINFO_ADDR);
359 	sizeptr = preload_search_info(modptr, MODINFO_SIZE);
360 	hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
361 	    MODINFOMD_ELFHDR);
362 	shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
363 	    MODINFOMD_SHDR);
364 	if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
365 	    " obj module") != 0 &&
366 	    strcmp(type, "elf obj module") != 0)) {
367 		return (EFTYPE);
368 	}
369 	if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
370 	    shdr == NULL)
371 		return (EINVAL);
372 
373 	lf = linker_make_file(filename, &link_elf_class);
374 	if (lf == NULL)
375 		return (ENOMEM);
376 
377 	ef = (elf_file_t)lf;
378 	ef->preloaded = 1;
379 	ef->address = *(caddr_t *)baseptr;
380 	lf->address = *(caddr_t *)baseptr;
381 	lf->size = *(size_t *)sizeptr;
382 
383 	if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
384 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
385 	    hdr->e_ident[EI_VERSION] != EV_CURRENT ||
386 	    hdr->e_version != EV_CURRENT ||
387 	    hdr->e_type != ET_REL ||
388 	    hdr->e_machine != ELF_TARG_MACH) {
389 		error = EFTYPE;
390 		goto out;
391 	}
392 	ef->e_shdr = shdr;
393 
394 	/* Scan the section header for information and table sizing. */
395 	symtabindex = -1;
396 	symstrindex = -1;
397 	for (i = 0; i < hdr->e_shnum; i++) {
398 		switch (shdr[i].sh_type) {
399 		case SHT_PROGBITS:
400 		case SHT_NOBITS:
401 #ifdef __amd64__
402 		case SHT_X86_64_UNWIND:
403 #endif
404 		case SHT_INIT_ARRAY:
405 		case SHT_FINI_ARRAY:
406 			/* Ignore sections not loaded by the loader. */
407 			if (shdr[i].sh_addr == 0)
408 				break;
409 			ef->nprogtab++;
410 			break;
411 		case SHT_SYMTAB:
412 			symtabindex = i;
413 			symstrindex = shdr[i].sh_link;
414 			break;
415 		case SHT_REL:
416 			/*
417 			 * Ignore relocation tables for sections not
418 			 * loaded by the loader.
419 			 */
420 			if (shdr[shdr[i].sh_info].sh_addr == 0)
421 				break;
422 			ef->nreltab++;
423 			break;
424 		case SHT_RELA:
425 			if (shdr[shdr[i].sh_info].sh_addr == 0)
426 				break;
427 			ef->nrelatab++;
428 			break;
429 		}
430 	}
431 
432 	shstrindex = hdr->e_shstrndx;
433 	if (ef->nprogtab == 0 || symstrindex < 0 ||
434 	    symstrindex >= hdr->e_shnum ||
435 	    shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
436 	    shstrindex >= hdr->e_shnum ||
437 	    shdr[shstrindex].sh_type != SHT_STRTAB) {
438 		printf("%s: bad/missing section headers\n", filename);
439 		error = ENOEXEC;
440 		goto out;
441 	}
442 
443 	/* Allocate space for tracking the load chunks */
444 	if (ef->nprogtab != 0)
445 		ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
446 		    M_LINKER, M_WAITOK | M_ZERO);
447 	if (ef->nreltab != 0)
448 		ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
449 		    M_LINKER, M_WAITOK | M_ZERO);
450 	if (ef->nrelatab != 0)
451 		ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
452 		    M_LINKER, M_WAITOK | M_ZERO);
453 	if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
454 	    (ef->nreltab != 0 && ef->reltab == NULL) ||
455 	    (ef->nrelatab != 0 && ef->relatab == NULL)) {
456 		error = ENOMEM;
457 		goto out;
458 	}
459 
460 	/* XXX, relocate the sh_addr fields saved by the loader. */
461 	off = 0;
462 	for (i = 0; i < hdr->e_shnum; i++) {
463 		if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
464 			off = shdr[i].sh_addr;
465 	}
466 	for (i = 0; i < hdr->e_shnum; i++) {
467 		if (shdr[i].sh_addr != 0)
468 			shdr[i].sh_addr = shdr[i].sh_addr - off +
469 			    (Elf_Addr)ef->address;
470 	}
471 
472 	ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
473 	ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
474 	ef->ddbstrcnt = shdr[symstrindex].sh_size;
475 	ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
476 	ef->shstrcnt = shdr[shstrindex].sh_size;
477 	ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
478 
479 	/* Now fill out progtab and the relocation tables. */
480 	pb = 0;
481 	rl = 0;
482 	ra = 0;
483 	for (i = 0; i < hdr->e_shnum; i++) {
484 		switch (shdr[i].sh_type) {
485 		case SHT_PROGBITS:
486 		case SHT_NOBITS:
487 #ifdef __amd64__
488 		case SHT_X86_64_UNWIND:
489 #endif
490 		case SHT_INIT_ARRAY:
491 		case SHT_FINI_ARRAY:
492 			if (shdr[i].sh_addr == 0)
493 				break;
494 			ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
495 			if (shdr[i].sh_type == SHT_PROGBITS)
496 				ef->progtab[pb].name = "<<PROGBITS>>";
497 #ifdef __amd64__
498 			else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
499 				ef->progtab[pb].name = "<<UNWIND>>";
500 #endif
501 			else if (shdr[i].sh_type == SHT_INIT_ARRAY)
502 				ef->progtab[pb].name = "<<INIT_ARRAY>>";
503 			else if (shdr[i].sh_type == SHT_FINI_ARRAY)
504 				ef->progtab[pb].name = "<<FINI_ARRAY>>";
505 			else
506 				ef->progtab[pb].name = "<<NOBITS>>";
507 			ef->progtab[pb].size = shdr[i].sh_size;
508 			ef->progtab[pb].flags = shdr[i].sh_flags;
509 			ef->progtab[pb].sec = i;
510 			if (ef->shstrtab && shdr[i].sh_name != 0)
511 				ef->progtab[pb].name =
512 				    ef->shstrtab + shdr[i].sh_name;
513 			if (ef->progtab[pb].name != NULL &&
514 			    !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
515 				void *dpcpu;
516 
517 				dpcpu = dpcpu_alloc(shdr[i].sh_size);
518 				if (dpcpu == NULL) {
519 					printf("%s: pcpu module space is out "
520 					    "of space; cannot allocate %#jx "
521 					    "for %s\n", __func__,
522 					    (uintmax_t)shdr[i].sh_size,
523 					    filename);
524 					error = ENOSPC;
525 					goto out;
526 				}
527 				memcpy(dpcpu, ef->progtab[pb].addr,
528 				    ef->progtab[pb].size);
529 				dpcpu_copy(dpcpu, shdr[i].sh_size);
530 				ef->progtab[pb].addr = dpcpu;
531 #ifdef VIMAGE
532 			} else if (ef->progtab[pb].name != NULL &&
533 			    !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
534 				void *vnet_data;
535 
536 				vnet_data = vnet_data_alloc(shdr[i].sh_size);
537 				if (vnet_data == NULL) {
538 					printf("%s: vnet module space is out "
539 					    "of space; cannot allocate %#jx "
540 					    "for %s\n", __func__,
541 					    (uintmax_t)shdr[i].sh_size,
542 					    filename);
543 					error = ENOSPC;
544 					goto out;
545 				}
546 				memcpy(vnet_data, ef->progtab[pb].addr,
547 				    ef->progtab[pb].size);
548 				ef->progtab[pb].addr = vnet_data;
549 				vnet_save_init(ef->progtab[pb].addr,
550 				    ef->progtab[pb].size);
551 #endif
552 			} else if ((ef->progtab[pb].name != NULL &&
553 			    strcmp(ef->progtab[pb].name, ".ctors") == 0) ||
554 			    shdr[i].sh_type == SHT_INIT_ARRAY) {
555 				if (lf->ctors_addr != 0) {
556 					printf(
557 				    "%s: multiple ctor sections in %s\n",
558 					    __func__, filename);
559 				} else {
560 					lf->ctors_addr = ef->progtab[pb].addr;
561 					lf->ctors_size = shdr[i].sh_size;
562 				}
563 			} else if ((ef->progtab[pb].name != NULL &&
564 			    strcmp(ef->progtab[pb].name, ".dtors") == 0) ||
565 			    shdr[i].sh_type == SHT_FINI_ARRAY) {
566 				if (lf->dtors_addr != 0) {
567 					printf(
568 				    "%s: multiple dtor sections in %s\n",
569 					    __func__, filename);
570 				} else {
571 					lf->dtors_addr = ef->progtab[pb].addr;
572 					lf->dtors_size = shdr[i].sh_size;
573 				}
574 			}
575 
576 			/* Update all symbol values with the offset. */
577 			for (j = 0; j < ef->ddbsymcnt; j++) {
578 				es = &ef->ddbsymtab[j];
579 				if (es->st_shndx != i)
580 					continue;
581 				es->st_value += (Elf_Addr)ef->progtab[pb].addr;
582 			}
583 			pb++;
584 			break;
585 		case SHT_REL:
586 			if (shdr[shdr[i].sh_info].sh_addr == 0)
587 				break;
588 			ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
589 			ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
590 			ef->reltab[rl].sec = shdr[i].sh_info;
591 			rl++;
592 			break;
593 		case SHT_RELA:
594 			if (shdr[shdr[i].sh_info].sh_addr == 0)
595 				break;
596 			ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
597 			ef->relatab[ra].nrela =
598 			    shdr[i].sh_size / sizeof(Elf_Rela);
599 			ef->relatab[ra].sec = shdr[i].sh_info;
600 			ra++;
601 			break;
602 		}
603 	}
604 	if (pb != ef->nprogtab) {
605 		printf("%s: lost progbits\n", filename);
606 		error = ENOEXEC;
607 		goto out;
608 	}
609 	if (rl != ef->nreltab) {
610 		printf("%s: lost reltab\n", filename);
611 		error = ENOEXEC;
612 		goto out;
613 	}
614 	if (ra != ef->nrelatab) {
615 		printf("%s: lost relatab\n", filename);
616 		error = ENOEXEC;
617 		goto out;
618 	}
619 
620 	/*
621 	 * The file needs to be writeable and executable while applying
622 	 * relocations.  Mapping protections are applied once relocation
623 	 * processing is complete.
624 	 */
625 	link_elf_protect_range(ef, (vm_offset_t)ef->address,
626 	    round_page((vm_offset_t)ef->address + ef->lf.size), VM_PROT_ALL);
627 
628 	/* Local intra-module relocations */
629 	error = link_elf_reloc_local(lf, false);
630 	if (error != 0)
631 		goto out;
632 	*result = lf;
633 	return (0);
634 
635 out:
636 	/* preload not done this way */
637 	linker_file_unload(lf, LINKER_UNLOAD_FORCE);
638 	return (error);
639 }
640 
641 static void
642 link_elf_invoke_cbs(caddr_t addr, size_t size)
643 {
644 	void (**ctor)(void);
645 	size_t i, cnt;
646 
647 	if (addr == NULL || size == 0)
648 		return;
649 	cnt = size / sizeof(*ctor);
650 	ctor = (void *)addr;
651 	for (i = 0; i < cnt; i++) {
652 		if (ctor[i] != NULL)
653 			(*ctor[i])();
654 	}
655 }
656 
657 static int
658 link_elf_link_preload_finish(linker_file_t lf)
659 {
660 	elf_file_t ef;
661 	int error;
662 
663 	ef = (elf_file_t)lf;
664 	error = relocate_file(ef);
665 	if (error)
666 		return (error);
667 
668 	/* Notify MD code that a module is being loaded. */
669 	error = elf_cpu_load_file(lf);
670 	if (error)
671 		return (error);
672 
673 #if defined(__i386__) || defined(__amd64__)
674 	/* Now ifuncs. */
675 	error = link_elf_reloc_local(lf, true);
676 	if (error != 0)
677 		return (error);
678 #endif
679 
680 	/* Apply protections now that relocation processing is complete. */
681 	link_elf_protect(ef);
682 
683 	link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
684 	return (0);
685 }
686 
687 static int
688 link_elf_load_file(linker_class_t cls, const char *filename,
689     linker_file_t *result)
690 {
691 	struct nameidata *nd;
692 	struct thread *td = curthread;	/* XXX */
693 	Elf_Ehdr *hdr;
694 	Elf_Shdr *shdr;
695 	Elf_Sym *es;
696 	int nbytes, i, j;
697 	vm_offset_t mapbase;
698 	size_t mapsize;
699 	int error = 0;
700 	ssize_t resid;
701 	int flags;
702 	elf_file_t ef;
703 	linker_file_t lf;
704 	int symtabindex;
705 	int symstrindex;
706 	int shstrindex;
707 	int nsym;
708 	int pb, rl, ra;
709 	int alignmask;
710 
711 	shdr = NULL;
712 	lf = NULL;
713 	mapsize = 0;
714 	hdr = NULL;
715 
716 	nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
717 	NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename);
718 	flags = FREAD;
719 	error = vn_open(nd, &flags, 0, NULL);
720 	if (error) {
721 		free(nd, M_TEMP);
722 		return error;
723 	}
724 	NDFREE_PNBUF(nd);
725 	if (nd->ni_vp->v_type != VREG) {
726 		error = ENOEXEC;
727 		goto out;
728 	}
729 #ifdef MAC
730 	error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
731 	if (error) {
732 		goto out;
733 	}
734 #endif
735 
736 	/* Read the elf header from the file. */
737 	hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
738 	error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
739 	    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
740 	    &resid, td);
741 	if (error)
742 		goto out;
743 	if (resid != 0){
744 		error = ENOEXEC;
745 		goto out;
746 	}
747 
748 	if (!IS_ELF(*hdr)) {
749 		error = ENOEXEC;
750 		goto out;
751 	}
752 
753 	if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
754 	    || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
755 		link_elf_error(filename, "Unsupported file layout");
756 		error = ENOEXEC;
757 		goto out;
758 	}
759 	if (hdr->e_ident[EI_VERSION] != EV_CURRENT
760 	    || hdr->e_version != EV_CURRENT) {
761 		link_elf_error(filename, "Unsupported file version");
762 		error = ENOEXEC;
763 		goto out;
764 	}
765 	if (hdr->e_type != ET_REL) {
766 		error = ENOSYS;
767 		goto out;
768 	}
769 	if (hdr->e_machine != ELF_TARG_MACH) {
770 		link_elf_error(filename, "Unsupported machine");
771 		error = ENOEXEC;
772 		goto out;
773 	}
774 
775 	lf = linker_make_file(filename, &link_elf_class);
776 	if (!lf) {
777 		error = ENOMEM;
778 		goto out;
779 	}
780 	ef = (elf_file_t) lf;
781 	ef->nprogtab = 0;
782 	ef->e_shdr = 0;
783 	ef->nreltab = 0;
784 	ef->nrelatab = 0;
785 
786 	/* Allocate and read in the section header */
787 	nbytes = hdr->e_shnum * hdr->e_shentsize;
788 	if (nbytes == 0 || hdr->e_shoff == 0 ||
789 	    hdr->e_shentsize != sizeof(Elf_Shdr)) {
790 		error = ENOEXEC;
791 		goto out;
792 	}
793 	shdr = malloc(nbytes, M_LINKER, M_WAITOK);
794 	ef->e_shdr = shdr;
795 	error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
796 	    hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
797 	    NOCRED, &resid, td);
798 	if (error)
799 		goto out;
800 	if (resid) {
801 		error = ENOEXEC;
802 		goto out;
803 	}
804 
805 	/* Scan the section header for information and table sizing. */
806 	nsym = 0;
807 	symtabindex = -1;
808 	symstrindex = -1;
809 	for (i = 0; i < hdr->e_shnum; i++) {
810 		if (shdr[i].sh_size == 0)
811 			continue;
812 		switch (shdr[i].sh_type) {
813 		case SHT_PROGBITS:
814 		case SHT_NOBITS:
815 #ifdef __amd64__
816 		case SHT_X86_64_UNWIND:
817 #endif
818 		case SHT_INIT_ARRAY:
819 		case SHT_FINI_ARRAY:
820 			if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
821 				break;
822 			ef->nprogtab++;
823 			break;
824 		case SHT_SYMTAB:
825 			nsym++;
826 			symtabindex = i;
827 			symstrindex = shdr[i].sh_link;
828 			break;
829 		case SHT_REL:
830 			/*
831 			 * Ignore relocation tables for unallocated
832 			 * sections.
833 			 */
834 			if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
835 				break;
836 			ef->nreltab++;
837 			break;
838 		case SHT_RELA:
839 			if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
840 				break;
841 			ef->nrelatab++;
842 			break;
843 		case SHT_STRTAB:
844 			break;
845 		}
846 	}
847 	if (ef->nprogtab == 0) {
848 		link_elf_error(filename, "file has no contents");
849 		error = ENOEXEC;
850 		goto out;
851 	}
852 	if (nsym != 1) {
853 		/* Only allow one symbol table for now */
854 		link_elf_error(filename,
855 		    "file must have exactly one symbol table");
856 		error = ENOEXEC;
857 		goto out;
858 	}
859 	if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
860 	    shdr[symstrindex].sh_type != SHT_STRTAB) {
861 		link_elf_error(filename, "file has invalid symbol strings");
862 		error = ENOEXEC;
863 		goto out;
864 	}
865 
866 	/* Allocate space for tracking the load chunks */
867 	if (ef->nprogtab != 0)
868 		ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
869 		    M_LINKER, M_WAITOK | M_ZERO);
870 	if (ef->nreltab != 0)
871 		ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
872 		    M_LINKER, M_WAITOK | M_ZERO);
873 	if (ef->nrelatab != 0)
874 		ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
875 		    M_LINKER, M_WAITOK | M_ZERO);
876 
877 	if (symtabindex == -1) {
878 		link_elf_error(filename, "lost symbol table index");
879 		error = ENOEXEC;
880 		goto out;
881 	}
882 	/* Allocate space for and load the symbol table */
883 	ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
884 	ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
885 	error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
886 	    shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
887 	    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
888 	    &resid, td);
889 	if (error)
890 		goto out;
891 	if (resid != 0){
892 		error = EINVAL;
893 		goto out;
894 	}
895 
896 	/* Allocate space for and load the symbol strings */
897 	ef->ddbstrcnt = shdr[symstrindex].sh_size;
898 	ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
899 	error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
900 	    shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
901 	    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
902 	    &resid, td);
903 	if (error)
904 		goto out;
905 	if (resid != 0){
906 		error = EINVAL;
907 		goto out;
908 	}
909 
910 	/* Do we have a string table for the section names?  */
911 	shstrindex = -1;
912 	if (hdr->e_shstrndx != 0 &&
913 	    shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
914 		shstrindex = hdr->e_shstrndx;
915 		ef->shstrcnt = shdr[shstrindex].sh_size;
916 		ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
917 		    M_WAITOK);
918 		error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
919 		    shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
920 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
921 		    &resid, td);
922 		if (error)
923 			goto out;
924 		if (resid != 0){
925 			error = EINVAL;
926 			goto out;
927 		}
928 	}
929 
930 	/* Size up code/data(progbits) and bss(nobits). */
931 	alignmask = 0;
932 	for (i = 0; i < hdr->e_shnum; i++) {
933 		if (shdr[i].sh_size == 0)
934 			continue;
935 		switch (shdr[i].sh_type) {
936 		case SHT_PROGBITS:
937 		case SHT_NOBITS:
938 #ifdef __amd64__
939 		case SHT_X86_64_UNWIND:
940 #endif
941 		case SHT_INIT_ARRAY:
942 		case SHT_FINI_ARRAY:
943 			if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
944 				break;
945 			alignmask = shdr[i].sh_addralign - 1;
946 			mapsize += alignmask;
947 			mapsize &= ~alignmask;
948 			mapsize += shdr[i].sh_size;
949 			break;
950 		}
951 	}
952 
953 	/*
954 	 * We know how much space we need for the text/data/bss/etc.
955 	 * This stuff needs to be in a single chunk so that profiling etc
956 	 * can get the bounds and gdb can associate offsets with modules
957 	 */
958 	ef->object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(mapsize),
959 	    VM_PROT_ALL, 0, thread0.td_ucred);
960 	if (ef->object == NULL) {
961 		error = ENOMEM;
962 		goto out;
963 	}
964 #if VM_NRESERVLEVEL > 0
965 	vm_object_color(ef->object, 0);
966 #endif
967 
968 	/*
969 	 * In order to satisfy amd64's architectural requirements on the
970 	 * location of code and data in the kernel's address space, request a
971 	 * mapping that is above the kernel.
972 	 *
973 	 * Protections will be restricted once relocations are applied.
974 	 */
975 #ifdef __amd64__
976 	mapbase = KERNBASE;
977 #else
978 	mapbase = VM_MIN_KERNEL_ADDRESS;
979 #endif
980 	error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
981 	    round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
982 	    VM_PROT_ALL, 0);
983 	if (error != KERN_SUCCESS) {
984 		vm_object_deallocate(ef->object);
985 		ef->object = NULL;
986 		error = ENOMEM;
987 		goto out;
988 	}
989 
990 	/* Wire the pages */
991 	error = vm_map_wire(kernel_map, mapbase,
992 	    mapbase + round_page(mapsize),
993 	    VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
994 	if (error != KERN_SUCCESS) {
995 		error = ENOMEM;
996 		goto out;
997 	}
998 
999 	/* Inform the kld system about the situation */
1000 	lf->address = ef->address = (caddr_t)mapbase;
1001 	lf->size = mapsize;
1002 
1003 	/*
1004 	 * Now load code/data(progbits), zero bss(nobits), allocate space for
1005 	 * and load relocs
1006 	 */
1007 	pb = 0;
1008 	rl = 0;
1009 	ra = 0;
1010 	alignmask = 0;
1011 	for (i = 0; i < hdr->e_shnum; i++) {
1012 		if (shdr[i].sh_size == 0)
1013 			continue;
1014 		switch (shdr[i].sh_type) {
1015 		case SHT_PROGBITS:
1016 		case SHT_NOBITS:
1017 #ifdef __amd64__
1018 		case SHT_X86_64_UNWIND:
1019 #endif
1020 		case SHT_INIT_ARRAY:
1021 		case SHT_FINI_ARRAY:
1022 			if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
1023 				break;
1024 			alignmask = shdr[i].sh_addralign - 1;
1025 			mapbase += alignmask;
1026 			mapbase &= ~alignmask;
1027 			if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
1028 				ef->progtab[pb].name =
1029 				    ef->shstrtab + shdr[i].sh_name;
1030 				if (!strcmp(ef->progtab[pb].name, ".ctors") ||
1031 				    shdr[i].sh_type == SHT_INIT_ARRAY) {
1032 					if (lf->ctors_addr != 0) {
1033 						printf(
1034 				    "%s: multiple ctor sections in %s\n",
1035 						    __func__, filename);
1036 					} else {
1037 						lf->ctors_addr =
1038 						    (caddr_t)mapbase;
1039 						lf->ctors_size =
1040 						    shdr[i].sh_size;
1041 					}
1042 				} else if (!strcmp(ef->progtab[pb].name,
1043 				    ".dtors") ||
1044 				    shdr[i].sh_type == SHT_FINI_ARRAY) {
1045 					if (lf->dtors_addr != 0) {
1046 						printf(
1047 				    "%s: multiple dtor sections in %s\n",
1048 						    __func__, filename);
1049 					} else {
1050 						lf->dtors_addr =
1051 						    (caddr_t)mapbase;
1052 						lf->dtors_size =
1053 						    shdr[i].sh_size;
1054 					}
1055 				}
1056 			} else if (shdr[i].sh_type == SHT_PROGBITS)
1057 				ef->progtab[pb].name = "<<PROGBITS>>";
1058 #ifdef __amd64__
1059 			else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
1060 				ef->progtab[pb].name = "<<UNWIND>>";
1061 #endif
1062 			else
1063 				ef->progtab[pb].name = "<<NOBITS>>";
1064 			if (ef->progtab[pb].name != NULL &&
1065 			    !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
1066 				ef->progtab[pb].addr =
1067 				    dpcpu_alloc(shdr[i].sh_size);
1068 				if (ef->progtab[pb].addr == NULL) {
1069 					printf("%s: pcpu module space is out "
1070 					    "of space; cannot allocate %#jx "
1071 					    "for %s\n", __func__,
1072 					    (uintmax_t)shdr[i].sh_size,
1073 					    filename);
1074 				}
1075 			}
1076 #ifdef VIMAGE
1077 			else if (ef->progtab[pb].name != NULL &&
1078 			    !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
1079 				ef->progtab[pb].addr =
1080 				    vnet_data_alloc(shdr[i].sh_size);
1081 				if (ef->progtab[pb].addr == NULL) {
1082 					printf("%s: vnet module space is out "
1083 					    "of space; cannot allocate %#jx "
1084 					    "for %s\n", __func__,
1085 					    (uintmax_t)shdr[i].sh_size,
1086 					    filename);
1087 				}
1088 			}
1089 #endif
1090 			else
1091 				ef->progtab[pb].addr =
1092 				    (void *)(uintptr_t)mapbase;
1093 			if (ef->progtab[pb].addr == NULL) {
1094 				error = ENOSPC;
1095 				goto out;
1096 			}
1097 			ef->progtab[pb].size = shdr[i].sh_size;
1098 			ef->progtab[pb].flags = shdr[i].sh_flags;
1099 			ef->progtab[pb].sec = i;
1100 			if (shdr[i].sh_type == SHT_PROGBITS
1101 #ifdef __amd64__
1102 			    || shdr[i].sh_type == SHT_X86_64_UNWIND
1103 #endif
1104 			    ) {
1105 				error = vn_rdwr(UIO_READ, nd->ni_vp,
1106 				    ef->progtab[pb].addr,
1107 				    shdr[i].sh_size, shdr[i].sh_offset,
1108 				    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1109 				    NOCRED, &resid, td);
1110 				if (error)
1111 					goto out;
1112 				if (resid != 0){
1113 					error = EINVAL;
1114 					goto out;
1115 				}
1116 				/* Initialize the per-cpu area. */
1117 				if (ef->progtab[pb].addr != (void *)mapbase &&
1118 				    !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1119 					dpcpu_copy(ef->progtab[pb].addr,
1120 					    shdr[i].sh_size);
1121 			} else
1122 				bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1123 
1124 #ifdef VIMAGE
1125 			if (ef->progtab[pb].addr != (void *)mapbase &&
1126 			    strcmp(ef->progtab[pb].name, VNET_SETNAME) == 0)
1127 				vnet_save_init(ef->progtab[pb].addr,
1128 				    ef->progtab[pb].size);
1129 #endif
1130 			/* Update all symbol values with the offset. */
1131 			for (j = 0; j < ef->ddbsymcnt; j++) {
1132 				es = &ef->ddbsymtab[j];
1133 				if (es->st_shndx != i)
1134 					continue;
1135 				es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1136 			}
1137 			mapbase += shdr[i].sh_size;
1138 			pb++;
1139 			break;
1140 		case SHT_REL:
1141 			if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1142 				break;
1143 			ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1144 			    M_WAITOK);
1145 			ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1146 			ef->reltab[rl].sec = shdr[i].sh_info;
1147 			error = vn_rdwr(UIO_READ, nd->ni_vp,
1148 			    (void *)ef->reltab[rl].rel,
1149 			    shdr[i].sh_size, shdr[i].sh_offset,
1150 			    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1151 			    &resid, td);
1152 			if (error)
1153 				goto out;
1154 			if (resid != 0){
1155 				error = EINVAL;
1156 				goto out;
1157 			}
1158 			rl++;
1159 			break;
1160 		case SHT_RELA:
1161 			if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1162 				break;
1163 			ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1164 			    M_WAITOK);
1165 			ef->relatab[ra].nrela =
1166 			    shdr[i].sh_size / sizeof(Elf_Rela);
1167 			ef->relatab[ra].sec = shdr[i].sh_info;
1168 			error = vn_rdwr(UIO_READ, nd->ni_vp,
1169 			    (void *)ef->relatab[ra].rela,
1170 			    shdr[i].sh_size, shdr[i].sh_offset,
1171 			    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1172 			    &resid, td);
1173 			if (error)
1174 				goto out;
1175 			if (resid != 0){
1176 				error = EINVAL;
1177 				goto out;
1178 			}
1179 			ra++;
1180 			break;
1181 		}
1182 	}
1183 	if (pb != ef->nprogtab) {
1184 		link_elf_error(filename, "lost progbits");
1185 		error = ENOEXEC;
1186 		goto out;
1187 	}
1188 	if (rl != ef->nreltab) {
1189 		link_elf_error(filename, "lost reltab");
1190 		error = ENOEXEC;
1191 		goto out;
1192 	}
1193 	if (ra != ef->nrelatab) {
1194 		link_elf_error(filename, "lost relatab");
1195 		error = ENOEXEC;
1196 		goto out;
1197 	}
1198 	if (mapbase != (vm_offset_t)ef->address + mapsize) {
1199 		printf(
1200 		    "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1201 		    filename != NULL ? filename : "<none>",
1202 		    (u_long)mapbase, ef->address, (u_long)mapsize,
1203 		    (u_long)(vm_offset_t)ef->address + mapsize);
1204 		error = ENOMEM;
1205 		goto out;
1206 	}
1207 
1208 	/* Local intra-module relocations */
1209 	error = link_elf_reloc_local(lf, false);
1210 	if (error != 0)
1211 		goto out;
1212 
1213 	/* Pull in dependencies */
1214 	VOP_UNLOCK(nd->ni_vp);
1215 	error = linker_load_dependencies(lf);
1216 	vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1217 	if (error)
1218 		goto out;
1219 
1220 	/* External relocations */
1221 	error = relocate_file(ef);
1222 	if (error)
1223 		goto out;
1224 
1225 	/* Notify MD code that a module is being loaded. */
1226 	error = elf_cpu_load_file(lf);
1227 	if (error)
1228 		goto out;
1229 
1230 #if defined(__i386__) || defined(__amd64__)
1231 	/* Now ifuncs. */
1232 	error = link_elf_reloc_local(lf, true);
1233 	if (error != 0)
1234 		goto out;
1235 #endif
1236 
1237 	link_elf_protect(ef);
1238 	link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
1239 	*result = lf;
1240 
1241 out:
1242 	VOP_UNLOCK(nd->ni_vp);
1243 	vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1244 	free(nd, M_TEMP);
1245 	if (error && lf)
1246 		linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1247 	free(hdr, M_LINKER);
1248 
1249 	return error;
1250 }
1251 
1252 static void
1253 link_elf_unload_file(linker_file_t file)
1254 {
1255 	elf_file_t ef = (elf_file_t) file;
1256 	u_int i;
1257 
1258 	link_elf_invoke_cbs(file->dtors_addr, file->dtors_size);
1259 
1260 	/* Notify MD code that a module is being unloaded. */
1261 	elf_cpu_unload_file(file);
1262 
1263 	if (ef->progtab) {
1264 		for (i = 0; i < ef->nprogtab; i++) {
1265 			if (ef->progtab[i].size == 0)
1266 				continue;
1267 			if (ef->progtab[i].name == NULL)
1268 				continue;
1269 			if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1270 				dpcpu_free(ef->progtab[i].addr,
1271 				    ef->progtab[i].size);
1272 #ifdef VIMAGE
1273 			else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1274 				vnet_data_free(ef->progtab[i].addr,
1275 				    ef->progtab[i].size);
1276 #endif
1277 		}
1278 	}
1279 	if (ef->preloaded) {
1280 		free(ef->reltab, M_LINKER);
1281 		free(ef->relatab, M_LINKER);
1282 		free(ef->progtab, M_LINKER);
1283 		free(ef->ctftab, M_LINKER);
1284 		free(ef->ctfoff, M_LINKER);
1285 		free(ef->typoff, M_LINKER);
1286 		if (file->pathname != NULL)
1287 			preload_delete_name(file->pathname);
1288 		return;
1289 	}
1290 
1291 	for (i = 0; i < ef->nreltab; i++)
1292 		free(ef->reltab[i].rel, M_LINKER);
1293 	for (i = 0; i < ef->nrelatab; i++)
1294 		free(ef->relatab[i].rela, M_LINKER);
1295 	free(ef->reltab, M_LINKER);
1296 	free(ef->relatab, M_LINKER);
1297 	free(ef->progtab, M_LINKER);
1298 
1299 	if (ef->object != NULL)
1300 		vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1301 		    (vm_offset_t)ef->address + ptoa(ef->object->size));
1302 	free(ef->e_shdr, M_LINKER);
1303 	free(ef->ddbsymtab, M_LINKER);
1304 	free(ef->ddbstrtab, M_LINKER);
1305 	free(ef->shstrtab, M_LINKER);
1306 	free(ef->ctftab, M_LINKER);
1307 	free(ef->ctfoff, M_LINKER);
1308 	free(ef->typoff, M_LINKER);
1309 }
1310 
1311 static const char *
1312 symbol_name(elf_file_t ef, Elf_Size r_info)
1313 {
1314 	const Elf_Sym *ref;
1315 
1316 	if (ELF_R_SYM(r_info)) {
1317 		ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1318 		return ef->ddbstrtab + ref->st_name;
1319 	} else
1320 		return NULL;
1321 }
1322 
1323 static Elf_Addr
1324 findbase(elf_file_t ef, int sec)
1325 {
1326 	int i;
1327 	Elf_Addr base = 0;
1328 
1329 	for (i = 0; i < ef->nprogtab; i++) {
1330 		if (sec == ef->progtab[i].sec) {
1331 			base = (Elf_Addr)ef->progtab[i].addr;
1332 			break;
1333 		}
1334 	}
1335 	return base;
1336 }
1337 
1338 static int
1339 relocate_file1(elf_file_t ef, bool ifuncs)
1340 {
1341 	const Elf_Rel *rellim;
1342 	const Elf_Rel *rel;
1343 	const Elf_Rela *relalim;
1344 	const Elf_Rela *rela;
1345 	const char *symname;
1346 	const Elf_Sym *sym;
1347 	int i;
1348 	Elf_Size symidx;
1349 	Elf_Addr base;
1350 
1351 	/* Perform relocations without addend if there are any: */
1352 	for (i = 0; i < ef->nreltab; i++) {
1353 		rel = ef->reltab[i].rel;
1354 		if (rel == NULL) {
1355 			link_elf_error(ef->lf.filename, "lost a reltab!");
1356 			return (ENOEXEC);
1357 		}
1358 		rellim = rel + ef->reltab[i].nrel;
1359 		base = findbase(ef, ef->reltab[i].sec);
1360 		if (base == 0) {
1361 			link_elf_error(ef->lf.filename, "lost base for reltab");
1362 			return (ENOEXEC);
1363 		}
1364 		for ( ; rel < rellim; rel++) {
1365 			symidx = ELF_R_SYM(rel->r_info);
1366 			if (symidx >= ef->ddbsymcnt)
1367 				continue;
1368 			sym = ef->ddbsymtab + symidx;
1369 			/* Local relocs are already done */
1370 			if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1371 				continue;
1372 			if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1373 			    elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1374 				continue;
1375 			if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1376 			    elf_obj_lookup)) {
1377 				symname = symbol_name(ef, rel->r_info);
1378 				printf("link_elf_obj: symbol %s undefined\n",
1379 				    symname);
1380 				return (ENOENT);
1381 			}
1382 		}
1383 	}
1384 
1385 	/* Perform relocations with addend if there are any: */
1386 	for (i = 0; i < ef->nrelatab; i++) {
1387 		rela = ef->relatab[i].rela;
1388 		if (rela == NULL) {
1389 			link_elf_error(ef->lf.filename, "lost a relatab!");
1390 			return (ENOEXEC);
1391 		}
1392 		relalim = rela + ef->relatab[i].nrela;
1393 		base = findbase(ef, ef->relatab[i].sec);
1394 		if (base == 0) {
1395 			link_elf_error(ef->lf.filename,
1396 			    "lost base for relatab");
1397 			return (ENOEXEC);
1398 		}
1399 		for ( ; rela < relalim; rela++) {
1400 			symidx = ELF_R_SYM(rela->r_info);
1401 			if (symidx >= ef->ddbsymcnt)
1402 				continue;
1403 			sym = ef->ddbsymtab + symidx;
1404 			/* Local relocs are already done */
1405 			if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1406 				continue;
1407 			if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1408 			    elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1409 				continue;
1410 			if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1411 			    elf_obj_lookup)) {
1412 				symname = symbol_name(ef, rela->r_info);
1413 				printf("link_elf_obj: symbol %s undefined\n",
1414 				    symname);
1415 				return (ENOENT);
1416 			}
1417 		}
1418 	}
1419 
1420 	/*
1421 	 * Only clean SHN_FBSD_CACHED for successful return.  If we
1422 	 * modified symbol table for the object but found an
1423 	 * unresolved symbol, there is no reason to roll back.
1424 	 */
1425 	elf_obj_cleanup_globals_cache(ef);
1426 
1427 	return (0);
1428 }
1429 
1430 static int
1431 relocate_file(elf_file_t ef)
1432 {
1433 	int error;
1434 
1435 	error = relocate_file1(ef, false);
1436 	if (error == 0)
1437 		error = relocate_file1(ef, true);
1438 	return (error);
1439 }
1440 
1441 static int
1442 link_elf_lookup_symbol1(linker_file_t lf, const char *name, c_linker_sym_t *sym,
1443     bool see_local)
1444 {
1445 	elf_file_t ef = (elf_file_t)lf;
1446 	const Elf_Sym *symp;
1447 	const char *strp;
1448 	int i;
1449 
1450 	for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1451 		strp = ef->ddbstrtab + symp->st_name;
1452 		if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1453 			if (see_local ||
1454 			    ELF_ST_BIND(symp->st_info) == STB_GLOBAL) {
1455 				*sym = (c_linker_sym_t) symp;
1456 				return (0);
1457 			}
1458 			return (ENOENT);
1459 		}
1460 	}
1461 	return (ENOENT);
1462 }
1463 
1464 static int
1465 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1466 {
1467 	return (link_elf_lookup_symbol1(lf, name, sym,
1468 	    link_elf_obj_leak_locals));
1469 }
1470 
1471 static int
1472 link_elf_lookup_debug_symbol(linker_file_t lf, const char *name,
1473     c_linker_sym_t *sym)
1474 {
1475 	return (link_elf_lookup_symbol1(lf, name, sym, true));
1476 }
1477 
1478 static int
1479 link_elf_symbol_values1(linker_file_t lf, c_linker_sym_t sym,
1480     linker_symval_t *symval, bool see_local)
1481 {
1482 	elf_file_t ef;
1483 	const Elf_Sym *es;
1484 	caddr_t val;
1485 
1486 	ef = (elf_file_t) lf;
1487 	es = (const Elf_Sym*) sym;
1488 	val = (caddr_t)es->st_value;
1489 	if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1490 		if (!see_local && ELF_ST_BIND(es->st_info) == STB_LOCAL)
1491 			return (ENOENT);
1492 		symval->name = ef->ddbstrtab + es->st_name;
1493 		val = (caddr_t)es->st_value;
1494 		if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1495 			val = ((caddr_t (*)(void))val)();
1496 		symval->value = val;
1497 		symval->size = es->st_size;
1498 		return (0);
1499 	}
1500 	return (ENOENT);
1501 }
1502 
1503 static int
1504 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1505     linker_symval_t *symval)
1506 {
1507 	return (link_elf_symbol_values1(lf, sym, symval,
1508 	    link_elf_obj_leak_locals));
1509 }
1510 
1511 static int
1512 link_elf_debug_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1513     linker_symval_t *symval)
1514 {
1515 	return (link_elf_symbol_values1(lf, sym, symval, true));
1516 }
1517 
1518 static int
1519 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1520     c_linker_sym_t *sym, long *diffp)
1521 {
1522 	elf_file_t ef = (elf_file_t)lf;
1523 	u_long off = (uintptr_t)(void *)value;
1524 	u_long diff = off;
1525 	u_long st_value;
1526 	const Elf_Sym *es;
1527 	const Elf_Sym *best = NULL;
1528 	int i;
1529 
1530 	for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1531 		if (es->st_name == 0)
1532 			continue;
1533 		st_value = es->st_value;
1534 		if (off >= st_value) {
1535 			if (off - st_value < diff) {
1536 				diff = off - st_value;
1537 				best = es;
1538 				if (diff == 0)
1539 					break;
1540 			} else if (off - st_value == diff) {
1541 				best = es;
1542 			}
1543 		}
1544 	}
1545 	if (best == NULL)
1546 		*diffp = off;
1547 	else
1548 		*diffp = diff;
1549 	*sym = (c_linker_sym_t) best;
1550 
1551 	return (0);
1552 }
1553 
1554 /*
1555  * Look up a linker set on an ELF system.
1556  */
1557 static int
1558 link_elf_lookup_set(linker_file_t lf, const char *name,
1559     void ***startp, void ***stopp, int *countp)
1560 {
1561 	elf_file_t ef = (elf_file_t)lf;
1562 	void **start, **stop;
1563 	int i, count;
1564 
1565 	/* Relative to section number */
1566 	for (i = 0; i < ef->nprogtab; i++) {
1567 		if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1568 		    strcmp(ef->progtab[i].name + 4, name) == 0) {
1569 			start  = (void **)ef->progtab[i].addr;
1570 			stop = (void **)((char *)ef->progtab[i].addr +
1571 			    ef->progtab[i].size);
1572 			count = stop - start;
1573 			if (startp)
1574 				*startp = start;
1575 			if (stopp)
1576 				*stopp = stop;
1577 			if (countp)
1578 				*countp = count;
1579 			return (0);
1580 		}
1581 	}
1582 	return (ESRCH);
1583 }
1584 
1585 static int
1586 link_elf_each_function_name(linker_file_t file,
1587     int (*callback)(const char *, void *), void *opaque)
1588 {
1589 	elf_file_t ef = (elf_file_t)file;
1590 	const Elf_Sym *symp;
1591 	int i, error;
1592 
1593 	/* Exhaustive search */
1594 	for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1595 		if (symp->st_value != 0 &&
1596 		    (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1597 		    ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1598 			error = callback(ef->ddbstrtab + symp->st_name, opaque);
1599 			if (error)
1600 				return (error);
1601 		}
1602 	}
1603 	return (0);
1604 }
1605 
1606 static int
1607 link_elf_each_function_nameval(linker_file_t file,
1608     linker_function_nameval_callback_t callback, void *opaque)
1609 {
1610 	linker_symval_t symval;
1611 	elf_file_t ef = (elf_file_t)file;
1612 	const Elf_Sym *symp;
1613 	int i, error;
1614 
1615 	/* Exhaustive search */
1616 	for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1617 		if (symp->st_value != 0 &&
1618 		    (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1619 		    ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1620 			error = link_elf_debug_symbol_values(file,
1621 			    (c_linker_sym_t)symp, &symval);
1622 			if (error == 0)
1623 				error = callback(file, i, &symval, opaque);
1624 			if (error != 0)
1625 				return (error);
1626 		}
1627 	}
1628 	return (0);
1629 }
1630 
1631 static void
1632 elf_obj_cleanup_globals_cache(elf_file_t ef)
1633 {
1634 	Elf_Sym *sym;
1635 	Elf_Size i;
1636 
1637 	for (i = 0; i < ef->ddbsymcnt; i++) {
1638 		sym = ef->ddbsymtab + i;
1639 		if (sym->st_shndx == SHN_FBSD_CACHED) {
1640 			sym->st_shndx = SHN_UNDEF;
1641 			sym->st_value = 0;
1642 		}
1643 	}
1644 }
1645 
1646 /*
1647  * Symbol lookup function that can be used when the symbol index is known (ie
1648  * in relocations). It uses the symbol index instead of doing a fully fledged
1649  * hash table based lookup when such is valid. For example for local symbols.
1650  * This is not only more efficient, it's also more correct. It's not always
1651  * the case that the symbol can be found through the hash table.
1652  */
1653 static int
1654 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1655 {
1656 	elf_file_t ef = (elf_file_t)lf;
1657 	Elf_Sym *sym;
1658 	const char *symbol;
1659 	Elf_Addr res1;
1660 
1661 	/* Don't even try to lookup the symbol if the index is bogus. */
1662 	if (symidx >= ef->ddbsymcnt) {
1663 		*res = 0;
1664 		return (EINVAL);
1665 	}
1666 
1667 	sym = ef->ddbsymtab + symidx;
1668 
1669 	/* Quick answer if there is a definition included. */
1670 	if (sym->st_shndx != SHN_UNDEF) {
1671 		res1 = (Elf_Addr)sym->st_value;
1672 		if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1673 			res1 = ((Elf_Addr (*)(void))res1)();
1674 		*res = res1;
1675 		return (0);
1676 	}
1677 
1678 	/* If we get here, then it is undefined and needs a lookup. */
1679 	switch (ELF_ST_BIND(sym->st_info)) {
1680 	case STB_LOCAL:
1681 		/* Local, but undefined? huh? */
1682 		*res = 0;
1683 		return (EINVAL);
1684 
1685 	case STB_GLOBAL:
1686 	case STB_WEAK:
1687 		/* Relative to Data or Function name */
1688 		symbol = ef->ddbstrtab + sym->st_name;
1689 
1690 		/* Force a lookup failure if the symbol name is bogus. */
1691 		if (*symbol == 0) {
1692 			*res = 0;
1693 			return (EINVAL);
1694 		}
1695 		res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1696 
1697 		/*
1698 		 * Cache global lookups during module relocation. The failure
1699 		 * case is particularly expensive for callers, who must scan
1700 		 * through the entire globals table doing strcmp(). Cache to
1701 		 * avoid doing such work repeatedly.
1702 		 *
1703 		 * After relocation is complete, undefined globals will be
1704 		 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1705 		 * above.
1706 		 */
1707 		if (res1 != 0) {
1708 			sym->st_shndx = SHN_FBSD_CACHED;
1709 			sym->st_value = res1;
1710 			*res = res1;
1711 			return (0);
1712 		} else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1713 			sym->st_value = 0;
1714 			*res = 0;
1715 			return (0);
1716 		}
1717 		return (EINVAL);
1718 
1719 	default:
1720 		return (EINVAL);
1721 	}
1722 }
1723 
1724 static void
1725 link_elf_fix_link_set(elf_file_t ef)
1726 {
1727 	static const char startn[] = "__start_";
1728 	static const char stopn[] = "__stop_";
1729 	Elf_Sym *sym;
1730 	const char *sym_name, *linkset_name;
1731 	Elf_Addr startp, stopp;
1732 	Elf_Size symidx;
1733 	int start, i;
1734 
1735 	startp = stopp = 0;
1736 	for (symidx = 1 /* zero entry is special */;
1737 		symidx < ef->ddbsymcnt; symidx++) {
1738 		sym = ef->ddbsymtab + symidx;
1739 		if (sym->st_shndx != SHN_UNDEF)
1740 			continue;
1741 
1742 		sym_name = ef->ddbstrtab + sym->st_name;
1743 		if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1744 			start = 1;
1745 			linkset_name = sym_name + sizeof(startn) - 1;
1746 		}
1747 		else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1748 			start = 0;
1749 			linkset_name = sym_name + sizeof(stopn) - 1;
1750 		}
1751 		else
1752 			continue;
1753 
1754 		for (i = 0; i < ef->nprogtab; i++) {
1755 			if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1756 				startp = (Elf_Addr)ef->progtab[i].addr;
1757 				stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1758 				break;
1759 			}
1760 		}
1761 		if (i == ef->nprogtab)
1762 			continue;
1763 
1764 		sym->st_value = start ? startp : stopp;
1765 		sym->st_shndx = i;
1766 	}
1767 }
1768 
1769 static int
1770 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1771 {
1772 	elf_file_t ef = (elf_file_t)lf;
1773 	const Elf_Rel *rellim;
1774 	const Elf_Rel *rel;
1775 	const Elf_Rela *relalim;
1776 	const Elf_Rela *rela;
1777 	const Elf_Sym *sym;
1778 	Elf_Addr base;
1779 	int i;
1780 	Elf_Size symidx;
1781 
1782 	link_elf_fix_link_set(ef);
1783 
1784 	/* Perform relocations without addend if there are any: */
1785 	for (i = 0; i < ef->nreltab; i++) {
1786 		rel = ef->reltab[i].rel;
1787 		if (rel == NULL) {
1788 			link_elf_error(ef->lf.filename, "lost a reltab");
1789 			return (ENOEXEC);
1790 		}
1791 		rellim = rel + ef->reltab[i].nrel;
1792 		base = findbase(ef, ef->reltab[i].sec);
1793 		if (base == 0) {
1794 			link_elf_error(ef->lf.filename, "lost base for reltab");
1795 			return (ENOEXEC);
1796 		}
1797 		for ( ; rel < rellim; rel++) {
1798 			symidx = ELF_R_SYM(rel->r_info);
1799 			if (symidx >= ef->ddbsymcnt)
1800 				continue;
1801 			sym = ef->ddbsymtab + symidx;
1802 			/* Only do local relocs */
1803 			if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1804 				continue;
1805 			if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1806 			    elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1807 				continue;
1808 			if (elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1809 			    elf_obj_lookup) != 0)
1810 				return (ENOEXEC);
1811 		}
1812 	}
1813 
1814 	/* Perform relocations with addend if there are any: */
1815 	for (i = 0; i < ef->nrelatab; i++) {
1816 		rela = ef->relatab[i].rela;
1817 		if (rela == NULL) {
1818 			link_elf_error(ef->lf.filename, "lost a relatab!");
1819 			return (ENOEXEC);
1820 		}
1821 		relalim = rela + ef->relatab[i].nrela;
1822 		base = findbase(ef, ef->relatab[i].sec);
1823 		if (base == 0) {
1824 			link_elf_error(ef->lf.filename, "lost base for reltab");
1825 			return (ENOEXEC);
1826 		}
1827 		for ( ; rela < relalim; rela++) {
1828 			symidx = ELF_R_SYM(rela->r_info);
1829 			if (symidx >= ef->ddbsymcnt)
1830 				continue;
1831 			sym = ef->ddbsymtab + symidx;
1832 			/* Only do local relocs */
1833 			if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1834 				continue;
1835 			if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1836 			    elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1837 				continue;
1838 			if (elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1839 			    elf_obj_lookup) != 0)
1840 				return (ENOEXEC);
1841 		}
1842 	}
1843 	return (0);
1844 }
1845 
1846 static long
1847 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1848 {
1849 	elf_file_t ef = (elf_file_t)lf;
1850 
1851 	*symtab = ef->ddbsymtab;
1852 	if (*symtab == NULL)
1853 		return (0);
1854 	return (ef->ddbsymcnt);
1855 }
1856 
1857 static long
1858 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1859 {
1860 	elf_file_t ef = (elf_file_t)lf;
1861 
1862 	*strtab = ef->ddbstrtab;
1863 	if (*strtab == NULL)
1864 		return (0);
1865 	return (ef->ddbstrcnt);
1866 }
1867 
1868 #ifdef VIMAGE
1869 static void
1870 link_elf_propagate_vnets(linker_file_t lf)
1871 {
1872 	elf_file_t ef = (elf_file_t) lf;
1873 
1874 	if (ef->progtab) {
1875 		for (int i = 0; i < ef->nprogtab; i++) {
1876 			if (ef->progtab[i].size == 0)
1877 				continue;
1878 			if (ef->progtab[i].name == NULL)
1879 				continue;
1880 			if (strcmp(ef->progtab[i].name, VNET_SETNAME) == 0) {
1881 				vnet_data_copy(ef->progtab[i].addr,
1882 				    ef->progtab[i].size);
1883 				break;
1884 			}
1885 		}
1886 	}
1887 }
1888 #endif
1889