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