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