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