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