2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1998-2000 Doug Rabson
5 * Copyright (c) 2004 Peter Wemm
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/fcntl.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/linker.h>
39 #include <sys/mutex.h>
40 #include <sys/mount.h>
41 #include <sys/namei.h>
43 #include <sys/rwlock.h>
44 #include <sys/sysctl.h>
45 #include <sys/vnode.h>
47 #include <machine/elf.h>
51 #include <security/mac/mac_framework.h>
54 #include <vm/vm_param.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_object.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_pager.h>
63 #include <sys/link_elf.h>
66 #include <contrib/zlib/zlib.h>
69 #include "linker_if.h"
74 int flags; /* Section flags. */
75 int sec; /* Original section number. */
91 typedef struct elf_file {
92 struct linker_file lf; /* Common fields */
95 caddr_t address; /* Relocation address */
96 vm_object_t object; /* VM object to hold file pages */
102 Elf_relaent *relatab;
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 */
113 caddr_t shstrtab; /* Section name string table */
114 long shstrcnt; /* number of bytes in string table */
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. */
124 #include <kern/kern_ctf.c>
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 *,
132 static int link_elf_lookup_debug_symbol(linker_file_t, const char *,
134 static int link_elf_lookup_debug_symbol_ctf(linker_file_t lf,
135 const char *name, c_linker_sym_t *sym, linker_ctf_t *lc);
136 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t,
138 static int link_elf_debug_symbol_values(linker_file_t, c_linker_sym_t,
140 static int link_elf_search_symbol(linker_file_t, caddr_t value,
141 c_linker_sym_t *sym, long *diffp);
143 static void link_elf_unload_file(linker_file_t);
144 static int link_elf_lookup_set(linker_file_t, const char *,
145 void ***, void ***, int *);
146 static int link_elf_each_function_name(linker_file_t,
147 int (*)(const char *, void *), void *);
148 static int link_elf_each_function_nameval(linker_file_t,
149 linker_function_nameval_callback_t,
151 static int link_elf_reloc_local(linker_file_t, bool);
152 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
153 static long link_elf_strtab_get(linker_file_t, caddr_t *);
155 static void link_elf_propagate_vnets(linker_file_t);
158 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps,
161 static kobj_method_t link_elf_methods[] = {
162 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
163 KOBJMETHOD(linker_lookup_debug_symbol, link_elf_lookup_debug_symbol),
164 KOBJMETHOD(linker_lookup_debug_symbol_ctf, link_elf_lookup_debug_symbol_ctf),
165 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
166 KOBJMETHOD(linker_debug_symbol_values, link_elf_debug_symbol_values),
167 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
168 KOBJMETHOD(linker_unload, link_elf_unload_file),
169 KOBJMETHOD(linker_load_file, link_elf_load_file),
170 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
171 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
172 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
173 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
174 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
175 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
176 KOBJMETHOD(linker_ctf_lookup_typename, link_elf_ctf_lookup_typename),
177 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
178 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
180 KOBJMETHOD(linker_propagate_vnets, link_elf_propagate_vnets),
185 static struct linker_class link_elf_class = {
186 #if ELF_TARG_CLASS == ELFCLASS32
191 link_elf_methods, sizeof(struct elf_file)
194 static bool link_elf_obj_leak_locals = true;
195 SYSCTL_BOOL(_debug, OID_AUTO, link_elf_obj_leak_locals,
196 CTLFLAG_RWTUN, &link_elf_obj_leak_locals, 0,
197 "Allow local symbols to participate in global module symbol resolution");
199 static int relocate_file(elf_file_t ef);
200 static void elf_obj_cleanup_globals_cache(elf_file_t);
203 link_elf_error(const char *filename, const char *s)
205 if (filename == NULL)
206 printf("kldload: %s\n", s);
208 printf("kldload: %s: %s\n", filename, s);
212 link_elf_init(void *arg)
215 linker_add_class(&link_elf_class);
217 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, NULL);
220 link_elf_protect_range(elf_file_t ef, vm_offset_t start, vm_offset_t end,
225 KASSERT(start <= end && start >= (vm_offset_t)ef->address &&
226 end <= round_page((vm_offset_t)ef->address + ef->lf.size),
227 ("link_elf_protect_range: invalid range %#jx-%#jx",
228 (uintmax_t)start, (uintmax_t)end));
234 error = pmap_change_prot(start, end - start, prot);
236 ("link_elf_protect_range: pmap_change_prot() returned %d",
241 error = vm_map_protect(kernel_map, start, end, prot, 0,
242 VM_MAP_PROTECT_SET_PROT);
243 KASSERT(error == KERN_SUCCESS,
244 ("link_elf_protect_range: vm_map_protect() returned %d", error));
248 * Restrict permissions on linker file memory based on section flags.
249 * Sections need not be page-aligned, so overlap within a page is possible.
252 link_elf_protect(elf_file_t ef)
254 vm_offset_t end, segend, segstart, start;
255 vm_prot_t gapprot, prot, segprot;
259 * If the file was preloaded, the last page may contain other preloaded
260 * data which may need to be writeable. ELF files are always
261 * page-aligned, but other preloaded data, such as entropy or CPU
262 * microcode may be loaded with a smaller alignment.
264 gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ;
266 start = end = (vm_offset_t)ef->address;
268 for (i = 0; i < ef->nprogtab; i++) {
270 * VNET and DPCPU sections have their memory allocated by their
271 * respective subsystems.
273 if (ef->progtab[i].name != NULL && (
275 strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 ||
277 strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0))
280 segstart = trunc_page((vm_offset_t)ef->progtab[i].addr);
281 segend = round_page((vm_offset_t)ef->progtab[i].addr +
282 ef->progtab[i].size);
283 segprot = VM_PROT_READ;
284 if ((ef->progtab[i].flags & SHF_WRITE) != 0)
285 segprot |= VM_PROT_WRITE;
286 if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0)
287 segprot |= VM_PROT_EXECUTE;
289 if (end <= segstart) {
291 * Case 1: there is no overlap between the previous
292 * segment and this one. Apply protections to the
293 * previous segment, and protect the gap between the
294 * previous and current segments, if any.
296 link_elf_protect_range(ef, start, end, prot);
297 link_elf_protect_range(ef, end, segstart, gapprot);
302 } else if (start < segstart && end == segend) {
304 * Case 2: the current segment is a subrange of the
305 * previous segment. Apply protections to the
306 * non-overlapping portion of the previous segment.
308 link_elf_protect_range(ef, start, segstart, prot);
312 } else if (end < segend) {
314 * Case 3: there is partial overlap between the previous
315 * and current segments. Apply protections to the
316 * non-overlapping portion of the previous segment, and
317 * then the overlap, which must use the union of the two
318 * segments' protections.
320 link_elf_protect_range(ef, start, segstart, prot);
321 link_elf_protect_range(ef, segstart, end,
328 * Case 4: the two segments reside in the same page.
335 * Fix up the last unprotected segment and trailing data.
337 link_elf_protect_range(ef, start, end, prot);
338 link_elf_protect_range(ef, end,
339 round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
343 link_elf_link_preload(linker_class_t cls, const char *filename,
344 linker_file_t *result)
349 void *modptr, *baseptr, *sizeptr;
354 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
356 /* Look to see if we have the file preloaded */
357 modptr = preload_search_by_name(filename);
361 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
362 baseptr = preload_search_info(modptr, MODINFO_ADDR);
363 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
364 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
366 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
368 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
369 " obj module") != 0 &&
370 strcmp(type, "elf obj module") != 0)) {
373 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
377 lf = linker_make_file(filename, &link_elf_class);
383 ef->address = *(caddr_t *)baseptr;
384 lf->address = *(caddr_t *)baseptr;
385 lf->size = *(size_t *)sizeptr;
387 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
388 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
389 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
390 hdr->e_version != EV_CURRENT ||
391 hdr->e_type != ET_REL ||
392 hdr->e_machine != ELF_TARG_MACH) {
398 /* Scan the section header for information and table sizing. */
401 for (i = 0; i < hdr->e_shnum; i++) {
402 switch (shdr[i].sh_type) {
406 case SHT_X86_64_UNWIND:
410 /* Ignore sections not loaded by the loader. */
411 if (shdr[i].sh_addr == 0)
417 symstrindex = shdr[i].sh_link;
421 * Ignore relocation tables for sections not
422 * loaded by the loader.
424 if (shdr[shdr[i].sh_info].sh_addr == 0)
429 if (shdr[shdr[i].sh_info].sh_addr == 0)
436 shstrindex = hdr->e_shstrndx;
437 if (ef->nprogtab == 0 || symstrindex < 0 ||
438 symstrindex >= hdr->e_shnum ||
439 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
440 shstrindex >= hdr->e_shnum ||
441 shdr[shstrindex].sh_type != SHT_STRTAB) {
442 printf("%s: bad/missing section headers\n", filename);
447 /* Allocate space for tracking the load chunks */
448 if (ef->nprogtab != 0)
449 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
450 M_LINKER, M_WAITOK | M_ZERO);
451 if (ef->nreltab != 0)
452 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
453 M_LINKER, M_WAITOK | M_ZERO);
454 if (ef->nrelatab != 0)
455 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
456 M_LINKER, M_WAITOK | M_ZERO);
457 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
458 (ef->nreltab != 0 && ef->reltab == NULL) ||
459 (ef->nrelatab != 0 && ef->relatab == NULL)) {
464 /* XXX, relocate the sh_addr fields saved by the loader. */
466 for (i = 0; i < hdr->e_shnum; i++) {
467 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
468 off = shdr[i].sh_addr;
470 for (i = 0; i < hdr->e_shnum; i++) {
471 if (shdr[i].sh_addr != 0)
472 shdr[i].sh_addr = shdr[i].sh_addr - off +
473 (Elf_Addr)ef->address;
476 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
477 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
478 ef->ddbstrcnt = shdr[symstrindex].sh_size;
479 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
480 ef->shstrcnt = shdr[shstrindex].sh_size;
481 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
483 /* Now fill out progtab and the relocation tables. */
487 for (i = 0; i < hdr->e_shnum; i++) {
488 switch (shdr[i].sh_type) {
492 case SHT_X86_64_UNWIND:
496 if (shdr[i].sh_addr == 0)
498 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
499 if (shdr[i].sh_type == SHT_PROGBITS)
500 ef->progtab[pb].name = "<<PROGBITS>>";
502 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
503 ef->progtab[pb].name = "<<UNWIND>>";
505 else if (shdr[i].sh_type == SHT_INIT_ARRAY)
506 ef->progtab[pb].name = "<<INIT_ARRAY>>";
507 else if (shdr[i].sh_type == SHT_FINI_ARRAY)
508 ef->progtab[pb].name = "<<FINI_ARRAY>>";
510 ef->progtab[pb].name = "<<NOBITS>>";
511 ef->progtab[pb].size = shdr[i].sh_size;
512 ef->progtab[pb].flags = shdr[i].sh_flags;
513 ef->progtab[pb].sec = i;
514 if (ef->shstrtab && shdr[i].sh_name != 0)
515 ef->progtab[pb].name =
516 ef->shstrtab + shdr[i].sh_name;
517 if (ef->progtab[pb].name != NULL &&
518 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
521 dpcpu = dpcpu_alloc(shdr[i].sh_size);
523 printf("%s: pcpu module space is out "
524 "of space; cannot allocate %#jx "
525 "for %s\n", __func__,
526 (uintmax_t)shdr[i].sh_size,
531 memcpy(dpcpu, ef->progtab[pb].addr,
532 ef->progtab[pb].size);
533 dpcpu_copy(dpcpu, shdr[i].sh_size);
534 ef->progtab[pb].addr = dpcpu;
536 } else if (ef->progtab[pb].name != NULL &&
537 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
540 vnet_data = vnet_data_alloc(shdr[i].sh_size);
541 if (vnet_data == NULL) {
542 printf("%s: vnet module space is out "
543 "of space; cannot allocate %#jx "
544 "for %s\n", __func__,
545 (uintmax_t)shdr[i].sh_size,
550 memcpy(vnet_data, ef->progtab[pb].addr,
551 ef->progtab[pb].size);
552 ef->progtab[pb].addr = vnet_data;
553 vnet_save_init(ef->progtab[pb].addr,
554 ef->progtab[pb].size);
556 } else if ((ef->progtab[pb].name != NULL &&
557 strcmp(ef->progtab[pb].name, ".ctors") == 0) ||
558 shdr[i].sh_type == SHT_INIT_ARRAY) {
559 if (lf->ctors_addr != 0) {
561 "%s: multiple ctor sections in %s\n",
564 lf->ctors_addr = ef->progtab[pb].addr;
565 lf->ctors_size = shdr[i].sh_size;
567 } else if ((ef->progtab[pb].name != NULL &&
568 strcmp(ef->progtab[pb].name, ".dtors") == 0) ||
569 shdr[i].sh_type == SHT_FINI_ARRAY) {
570 if (lf->dtors_addr != 0) {
572 "%s: multiple dtor sections in %s\n",
575 lf->dtors_addr = ef->progtab[pb].addr;
576 lf->dtors_size = shdr[i].sh_size;
580 /* Update all symbol values with the offset. */
581 for (j = 0; j < ef->ddbsymcnt; j++) {
582 es = &ef->ddbsymtab[j];
583 if (es->st_shndx != i)
585 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
590 if (shdr[shdr[i].sh_info].sh_addr == 0)
592 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
593 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
594 ef->reltab[rl].sec = shdr[i].sh_info;
598 if (shdr[shdr[i].sh_info].sh_addr == 0)
600 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
601 ef->relatab[ra].nrela =
602 shdr[i].sh_size / sizeof(Elf_Rela);
603 ef->relatab[ra].sec = shdr[i].sh_info;
608 if (pb != ef->nprogtab) {
609 printf("%s: lost progbits\n", filename);
613 if (rl != ef->nreltab) {
614 printf("%s: lost reltab\n", filename);
618 if (ra != ef->nrelatab) {
619 printf("%s: lost relatab\n", filename);
625 * The file needs to be writeable and executable while applying
626 * relocations. Mapping protections are applied once relocation
627 * processing is complete.
629 link_elf_protect_range(ef, (vm_offset_t)ef->address,
630 round_page((vm_offset_t)ef->address + ef->lf.size), VM_PROT_ALL);
632 /* Local intra-module relocations */
633 error = link_elf_reloc_local(lf, false);
640 /* preload not done this way */
641 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
646 link_elf_invoke_cbs(caddr_t addr, size_t size)
651 if (addr == NULL || size == 0)
653 cnt = size / sizeof(*ctor);
655 for (i = 0; i < cnt; i++) {
662 link_elf_invoke_ctors(linker_file_t lf)
664 KASSERT(lf->ctors_invoked == LF_NONE,
665 ("%s: file %s ctor state %d",
666 __func__, lf->filename, lf->ctors_invoked));
668 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
669 lf->ctors_invoked = LF_CTORS;
673 link_elf_invoke_dtors(linker_file_t lf)
675 KASSERT(lf->ctors_invoked != LF_DTORS,
676 ("%s: file %s ctor state %d",
677 __func__, lf->filename, lf->ctors_invoked));
679 if (lf->ctors_invoked == LF_CTORS) {
680 link_elf_invoke_cbs(lf->dtors_addr, lf->dtors_size);
681 lf->ctors_invoked = LF_DTORS;
686 link_elf_link_preload_finish(linker_file_t lf)
692 error = relocate_file(ef);
696 /* Notify MD code that a module is being loaded. */
697 error = elf_cpu_load_file(lf);
701 #if defined(__i386__) || defined(__amd64__)
703 error = link_elf_reloc_local(lf, true);
708 /* Apply protections now that relocation processing is complete. */
709 link_elf_protect(ef);
711 link_elf_invoke_ctors(lf);
716 link_elf_load_file(linker_class_t cls, const char *filename,
717 linker_file_t *result)
719 struct nameidata *nd;
720 struct thread *td = curthread; /* XXX */
744 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
745 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename);
747 error = vn_open(nd, &flags, 0, NULL);
753 if (nd->ni_vp->v_type != VREG) {
758 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
764 /* Read the elf header from the file. */
765 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
766 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
767 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
781 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
782 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
783 link_elf_error(filename, "Unsupported file layout");
787 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
788 || hdr->e_version != EV_CURRENT) {
789 link_elf_error(filename, "Unsupported file version");
793 if (hdr->e_type != ET_REL) {
797 if (hdr->e_machine != ELF_TARG_MACH) {
798 link_elf_error(filename, "Unsupported machine");
803 lf = linker_make_file(filename, &link_elf_class);
808 ef = (elf_file_t) lf;
814 /* Allocate and read in the section header */
815 nbytes = hdr->e_shnum * hdr->e_shentsize;
816 if (nbytes == 0 || hdr->e_shoff == 0 ||
817 hdr->e_shentsize != sizeof(Elf_Shdr)) {
821 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
823 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
824 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
833 /* Scan the section header for information and table sizing. */
837 for (i = 0; i < hdr->e_shnum; i++) {
838 if (shdr[i].sh_size == 0)
840 switch (shdr[i].sh_type) {
844 case SHT_X86_64_UNWIND:
848 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
855 symstrindex = shdr[i].sh_link;
859 * Ignore relocation tables for unallocated
862 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
867 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
875 if (ef->nprogtab == 0) {
876 link_elf_error(filename, "file has no contents");
881 /* Only allow one symbol table for now */
882 link_elf_error(filename,
883 "file must have exactly one symbol table");
887 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
888 shdr[symstrindex].sh_type != SHT_STRTAB) {
889 link_elf_error(filename, "file has invalid symbol strings");
894 /* Allocate space for tracking the load chunks */
895 if (ef->nprogtab != 0)
896 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
897 M_LINKER, M_WAITOK | M_ZERO);
898 if (ef->nreltab != 0)
899 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
900 M_LINKER, M_WAITOK | M_ZERO);
901 if (ef->nrelatab != 0)
902 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
903 M_LINKER, M_WAITOK | M_ZERO);
905 if (symtabindex == -1) {
906 link_elf_error(filename, "lost symbol table index");
910 /* Allocate space for and load the symbol table */
911 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
912 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
913 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
914 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
915 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
924 /* Allocate space for and load the symbol strings */
925 ef->ddbstrcnt = shdr[symstrindex].sh_size;
926 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
927 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
928 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
929 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
938 /* Do we have a string table for the section names? */
940 if (hdr->e_shstrndx != 0 &&
941 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
942 shstrindex = hdr->e_shstrndx;
943 ef->shstrcnt = shdr[shstrindex].sh_size;
944 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
946 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
947 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
948 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
958 /* Size up code/data(progbits) and bss(nobits). */
960 for (i = 0; i < hdr->e_shnum; i++) {
961 if (shdr[i].sh_size == 0)
963 switch (shdr[i].sh_type) {
967 case SHT_X86_64_UNWIND:
971 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
973 alignmask = shdr[i].sh_addralign - 1;
974 mapsize += alignmask;
975 mapsize &= ~alignmask;
976 mapsize += shdr[i].sh_size;
982 * We know how much space we need for the text/data/bss/etc.
983 * This stuff needs to be in a single chunk so that profiling etc
984 * can get the bounds and gdb can associate offsets with modules
986 ef->object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(mapsize),
987 VM_PROT_ALL, 0, thread0.td_ucred);
988 if (ef->object == NULL) {
992 #if VM_NRESERVLEVEL > 0
993 vm_object_color(ef->object, 0);
997 * In order to satisfy amd64's architectural requirements on the
998 * location of code and data in the kernel's address space, request a
999 * mapping that is above the kernel.
1001 * Protections will be restricted once relocations are applied.
1006 mapbase = VM_MIN_KERNEL_ADDRESS;
1008 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
1009 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
1011 if (error != KERN_SUCCESS) {
1012 vm_object_deallocate(ef->object);
1018 /* Wire the pages */
1019 error = vm_map_wire(kernel_map, mapbase,
1020 mapbase + round_page(mapsize),
1021 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
1022 if (error != KERN_SUCCESS) {
1027 /* Inform the kld system about the situation */
1028 lf->address = ef->address = (caddr_t)mapbase;
1032 * Now load code/data(progbits), zero bss(nobits), allocate space for
1039 for (i = 0; i < hdr->e_shnum; i++) {
1040 if (shdr[i].sh_size == 0)
1042 switch (shdr[i].sh_type) {
1046 case SHT_X86_64_UNWIND:
1048 case SHT_INIT_ARRAY:
1049 case SHT_FINI_ARRAY:
1050 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
1052 alignmask = shdr[i].sh_addralign - 1;
1053 mapbase += alignmask;
1054 mapbase &= ~alignmask;
1055 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
1056 ef->progtab[pb].name =
1057 ef->shstrtab + shdr[i].sh_name;
1058 if (!strcmp(ef->progtab[pb].name, ".ctors") ||
1059 shdr[i].sh_type == SHT_INIT_ARRAY) {
1060 if (lf->ctors_addr != 0) {
1062 "%s: multiple ctor sections in %s\n",
1063 __func__, filename);
1070 } else if (!strcmp(ef->progtab[pb].name,
1072 shdr[i].sh_type == SHT_FINI_ARRAY) {
1073 if (lf->dtors_addr != 0) {
1075 "%s: multiple dtor sections in %s\n",
1076 __func__, filename);
1084 } else if (shdr[i].sh_type == SHT_PROGBITS)
1085 ef->progtab[pb].name = "<<PROGBITS>>";
1087 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
1088 ef->progtab[pb].name = "<<UNWIND>>";
1091 ef->progtab[pb].name = "<<NOBITS>>";
1092 if (ef->progtab[pb].name != NULL &&
1093 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
1094 ef->progtab[pb].addr =
1095 dpcpu_alloc(shdr[i].sh_size);
1096 if (ef->progtab[pb].addr == NULL) {
1097 printf("%s: pcpu module space is out "
1098 "of space; cannot allocate %#jx "
1099 "for %s\n", __func__,
1100 (uintmax_t)shdr[i].sh_size,
1105 else if (ef->progtab[pb].name != NULL &&
1106 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
1107 ef->progtab[pb].addr =
1108 vnet_data_alloc(shdr[i].sh_size);
1109 if (ef->progtab[pb].addr == NULL) {
1110 printf("%s: vnet module space is out "
1111 "of space; cannot allocate %#jx "
1112 "for %s\n", __func__,
1113 (uintmax_t)shdr[i].sh_size,
1119 ef->progtab[pb].addr =
1120 (void *)(uintptr_t)mapbase;
1121 if (ef->progtab[pb].addr == NULL) {
1125 ef->progtab[pb].size = shdr[i].sh_size;
1126 ef->progtab[pb].flags = shdr[i].sh_flags;
1127 ef->progtab[pb].sec = i;
1128 if (shdr[i].sh_type == SHT_PROGBITS
1130 || shdr[i].sh_type == SHT_X86_64_UNWIND
1133 error = vn_rdwr(UIO_READ, nd->ni_vp,
1134 ef->progtab[pb].addr,
1135 shdr[i].sh_size, shdr[i].sh_offset,
1136 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1137 NOCRED, &resid, td);
1144 /* Initialize the per-cpu area. */
1145 if (ef->progtab[pb].addr != (void *)mapbase &&
1146 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1147 dpcpu_copy(ef->progtab[pb].addr,
1150 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1153 if (ef->progtab[pb].addr != (void *)mapbase &&
1154 strcmp(ef->progtab[pb].name, VNET_SETNAME) == 0)
1155 vnet_save_init(ef->progtab[pb].addr,
1156 ef->progtab[pb].size);
1158 /* Update all symbol values with the offset. */
1159 for (j = 0; j < ef->ddbsymcnt; j++) {
1160 es = &ef->ddbsymtab[j];
1161 if (es->st_shndx != i)
1163 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1165 mapbase += shdr[i].sh_size;
1169 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1171 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1173 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1174 ef->reltab[rl].sec = shdr[i].sh_info;
1175 error = vn_rdwr(UIO_READ, nd->ni_vp,
1176 (void *)ef->reltab[rl].rel,
1177 shdr[i].sh_size, shdr[i].sh_offset,
1178 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1189 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1191 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1193 ef->relatab[ra].nrela =
1194 shdr[i].sh_size / sizeof(Elf_Rela);
1195 ef->relatab[ra].sec = shdr[i].sh_info;
1196 error = vn_rdwr(UIO_READ, nd->ni_vp,
1197 (void *)ef->relatab[ra].rela,
1198 shdr[i].sh_size, shdr[i].sh_offset,
1199 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1211 if (pb != ef->nprogtab) {
1212 link_elf_error(filename, "lost progbits");
1216 if (rl != ef->nreltab) {
1217 link_elf_error(filename, "lost reltab");
1221 if (ra != ef->nrelatab) {
1222 link_elf_error(filename, "lost relatab");
1226 if (mapbase != (vm_offset_t)ef->address + mapsize) {
1228 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1229 filename != NULL ? filename : "<none>",
1230 (u_long)mapbase, ef->address, (u_long)mapsize,
1231 (u_long)(vm_offset_t)ef->address + mapsize);
1236 /* Local intra-module relocations */
1237 error = link_elf_reloc_local(lf, false);
1241 /* Pull in dependencies */
1242 VOP_UNLOCK(nd->ni_vp);
1243 error = linker_load_dependencies(lf);
1244 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1248 /* External relocations */
1249 error = relocate_file(ef);
1253 /* Notify MD code that a module is being loaded. */
1254 error = elf_cpu_load_file(lf);
1258 #if defined(__i386__) || defined(__amd64__)
1260 error = link_elf_reloc_local(lf, true);
1265 link_elf_protect(ef);
1266 link_elf_invoke_ctors(lf);
1270 VOP_UNLOCK(nd->ni_vp);
1271 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1274 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1275 free(hdr, M_LINKER);
1281 link_elf_unload_file(linker_file_t file)
1283 elf_file_t ef = (elf_file_t) file;
1286 link_elf_invoke_dtors(file);
1288 /* Notify MD code that a module is being unloaded. */
1289 elf_cpu_unload_file(file);
1292 for (i = 0; i < ef->nprogtab; i++) {
1293 if (ef->progtab[i].size == 0)
1295 if (ef->progtab[i].name == NULL)
1297 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1298 dpcpu_free(ef->progtab[i].addr,
1299 ef->progtab[i].size);
1301 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1302 vnet_data_free(ef->progtab[i].addr,
1303 ef->progtab[i].size);
1307 if (ef->preloaded) {
1308 free(ef->reltab, M_LINKER);
1309 free(ef->relatab, M_LINKER);
1310 free(ef->progtab, M_LINKER);
1311 free(ef->ctftab, M_LINKER);
1312 free(ef->ctfoff, M_LINKER);
1313 free(ef->typoff, M_LINKER);
1314 if (file->pathname != NULL)
1315 preload_delete_name(file->pathname);
1319 for (i = 0; i < ef->nreltab; i++)
1320 free(ef->reltab[i].rel, M_LINKER);
1321 for (i = 0; i < ef->nrelatab; i++)
1322 free(ef->relatab[i].rela, M_LINKER);
1323 free(ef->reltab, M_LINKER);
1324 free(ef->relatab, M_LINKER);
1325 free(ef->progtab, M_LINKER);
1327 if (ef->object != NULL)
1328 vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1329 (vm_offset_t)ef->address + ptoa(ef->object->size));
1330 free(ef->e_shdr, M_LINKER);
1331 free(ef->ddbsymtab, M_LINKER);
1332 free(ef->ddbstrtab, M_LINKER);
1333 free(ef->shstrtab, M_LINKER);
1334 free(ef->ctftab, M_LINKER);
1335 free(ef->ctfoff, M_LINKER);
1336 free(ef->typoff, M_LINKER);
1340 symbol_name(elf_file_t ef, Elf_Size r_info)
1344 if (ELF_R_SYM(r_info)) {
1345 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1346 return ef->ddbstrtab + ref->st_name;
1352 findbase(elf_file_t ef, int sec)
1357 for (i = 0; i < ef->nprogtab; i++) {
1358 if (sec == ef->progtab[i].sec) {
1359 base = (Elf_Addr)ef->progtab[i].addr;
1367 relocate_file1(elf_file_t ef, bool ifuncs)
1369 const Elf_Rel *rellim;
1371 const Elf_Rela *relalim;
1372 const Elf_Rela *rela;
1373 const char *symname;
1379 /* Perform relocations without addend if there are any: */
1380 for (i = 0; i < ef->nreltab; i++) {
1381 rel = ef->reltab[i].rel;
1383 link_elf_error(ef->lf.filename, "lost a reltab!");
1386 rellim = rel + ef->reltab[i].nrel;
1387 base = findbase(ef, ef->reltab[i].sec);
1389 link_elf_error(ef->lf.filename, "lost base for reltab");
1392 for ( ; rel < rellim; rel++) {
1393 symidx = ELF_R_SYM(rel->r_info);
1394 if (symidx >= ef->ddbsymcnt)
1396 sym = ef->ddbsymtab + symidx;
1397 /* Local relocs are already done */
1398 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1400 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1401 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1403 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1405 symname = symbol_name(ef, rel->r_info);
1406 printf("link_elf_obj: symbol %s undefined\n",
1413 /* Perform relocations with addend if there are any: */
1414 for (i = 0; i < ef->nrelatab; i++) {
1415 rela = ef->relatab[i].rela;
1417 link_elf_error(ef->lf.filename, "lost a relatab!");
1420 relalim = rela + ef->relatab[i].nrela;
1421 base = findbase(ef, ef->relatab[i].sec);
1423 link_elf_error(ef->lf.filename,
1424 "lost base for relatab");
1427 for ( ; rela < relalim; rela++) {
1428 symidx = ELF_R_SYM(rela->r_info);
1429 if (symidx >= ef->ddbsymcnt)
1431 sym = ef->ddbsymtab + symidx;
1432 /* Local relocs are already done */
1433 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1435 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1436 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1438 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1440 symname = symbol_name(ef, rela->r_info);
1441 printf("link_elf_obj: symbol %s undefined\n",
1449 * Only clean SHN_FBSD_CACHED for successful return. If we
1450 * modified symbol table for the object but found an
1451 * unresolved symbol, there is no reason to roll back.
1453 elf_obj_cleanup_globals_cache(ef);
1459 relocate_file(elf_file_t ef)
1463 error = relocate_file1(ef, false);
1465 error = relocate_file1(ef, true);
1470 link_elf_lookup_symbol1(linker_file_t lf, const char *name, c_linker_sym_t *sym,
1473 elf_file_t ef = (elf_file_t)lf;
1474 const Elf_Sym *symp;
1478 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1479 strp = ef->ddbstrtab + symp->st_name;
1480 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1482 ELF_ST_BIND(symp->st_info) == STB_GLOBAL) {
1483 *sym = (c_linker_sym_t) symp;
1493 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1495 return (link_elf_lookup_symbol1(lf, name, sym,
1496 link_elf_obj_leak_locals));
1500 link_elf_lookup_debug_symbol(linker_file_t lf, const char *name,
1501 c_linker_sym_t *sym)
1503 return (link_elf_lookup_symbol1(lf, name, sym, true));
1507 link_elf_lookup_debug_symbol_ctf(linker_file_t lf, const char *name,
1508 c_linker_sym_t *sym, linker_ctf_t *lc)
1510 if (link_elf_lookup_debug_symbol(lf, name, sym))
1513 return (link_elf_ctf_get_ddb(lf, lc));
1517 link_elf_symbol_values1(linker_file_t lf, c_linker_sym_t sym,
1518 linker_symval_t *symval, bool see_local)
1524 ef = (elf_file_t) lf;
1525 es = (const Elf_Sym*) sym;
1526 val = (caddr_t)es->st_value;
1527 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1528 if (!see_local && ELF_ST_BIND(es->st_info) == STB_LOCAL)
1530 symval->name = ef->ddbstrtab + es->st_name;
1531 val = (caddr_t)es->st_value;
1532 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1533 val = ((caddr_t (*)(void))val)();
1534 symval->value = val;
1535 symval->size = es->st_size;
1542 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1543 linker_symval_t *symval)
1545 return (link_elf_symbol_values1(lf, sym, symval,
1546 link_elf_obj_leak_locals));
1550 link_elf_debug_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1551 linker_symval_t *symval)
1553 return (link_elf_symbol_values1(lf, sym, symval, true));
1557 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1558 c_linker_sym_t *sym, long *diffp)
1560 elf_file_t ef = (elf_file_t)lf;
1561 u_long off = (uintptr_t)(void *)value;
1565 const Elf_Sym *best = NULL;
1568 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1569 if (es->st_name == 0)
1571 st_value = es->st_value;
1572 if (off >= st_value) {
1573 if (off - st_value < diff) {
1574 diff = off - st_value;
1578 } else if (off - st_value == diff) {
1587 *sym = (c_linker_sym_t) best;
1593 * Look up a linker set on an ELF system.
1596 link_elf_lookup_set(linker_file_t lf, const char *name,
1597 void ***startp, void ***stopp, int *countp)
1599 elf_file_t ef = (elf_file_t)lf;
1600 void **start, **stop;
1603 /* Relative to section number */
1604 for (i = 0; i < ef->nprogtab; i++) {
1605 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1606 strcmp(ef->progtab[i].name + 4, name) == 0) {
1607 start = (void **)ef->progtab[i].addr;
1608 stop = (void **)((char *)ef->progtab[i].addr +
1609 ef->progtab[i].size);
1610 count = stop - start;
1624 link_elf_each_function_name(linker_file_t file,
1625 int (*callback)(const char *, void *), void *opaque)
1627 elf_file_t ef = (elf_file_t)file;
1628 const Elf_Sym *symp;
1631 /* Exhaustive search */
1632 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1633 if (symp->st_value != 0 &&
1634 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1635 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1636 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1645 link_elf_each_function_nameval(linker_file_t file,
1646 linker_function_nameval_callback_t callback, void *opaque)
1648 linker_symval_t symval;
1649 elf_file_t ef = (elf_file_t)file;
1650 const Elf_Sym *symp;
1653 /* Exhaustive search */
1654 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1655 if (symp->st_value != 0 &&
1656 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1657 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1658 error = link_elf_debug_symbol_values(file,
1659 (c_linker_sym_t)symp, &symval);
1661 error = callback(file, i, &symval, opaque);
1670 elf_obj_cleanup_globals_cache(elf_file_t ef)
1675 for (i = 0; i < ef->ddbsymcnt; i++) {
1676 sym = ef->ddbsymtab + i;
1677 if (sym->st_shndx == SHN_FBSD_CACHED) {
1678 sym->st_shndx = SHN_UNDEF;
1685 * Symbol lookup function that can be used when the symbol index is known (ie
1686 * in relocations). It uses the symbol index instead of doing a fully fledged
1687 * hash table based lookup when such is valid. For example for local symbols.
1688 * This is not only more efficient, it's also more correct. It's not always
1689 * the case that the symbol can be found through the hash table.
1692 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1694 elf_file_t ef = (elf_file_t)lf;
1699 /* Don't even try to lookup the symbol if the index is bogus. */
1700 if (symidx >= ef->ddbsymcnt) {
1705 sym = ef->ddbsymtab + symidx;
1707 /* Quick answer if there is a definition included. */
1708 if (sym->st_shndx != SHN_UNDEF) {
1709 res1 = (Elf_Addr)sym->st_value;
1710 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1711 res1 = ((Elf_Addr (*)(void))res1)();
1716 /* If we get here, then it is undefined and needs a lookup. */
1717 switch (ELF_ST_BIND(sym->st_info)) {
1719 /* Local, but undefined? huh? */
1725 /* Relative to Data or Function name */
1726 symbol = ef->ddbstrtab + sym->st_name;
1728 /* Force a lookup failure if the symbol name is bogus. */
1733 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1736 * Cache global lookups during module relocation. The failure
1737 * case is particularly expensive for callers, who must scan
1738 * through the entire globals table doing strcmp(). Cache to
1739 * avoid doing such work repeatedly.
1741 * After relocation is complete, undefined globals will be
1742 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1746 sym->st_shndx = SHN_FBSD_CACHED;
1747 sym->st_value = res1;
1750 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1763 link_elf_fix_link_set(elf_file_t ef)
1765 static const char startn[] = "__start_";
1766 static const char stopn[] = "__stop_";
1768 const char *sym_name, *linkset_name;
1769 Elf_Addr startp, stopp;
1774 for (symidx = 1 /* zero entry is special */;
1775 symidx < ef->ddbsymcnt; symidx++) {
1776 sym = ef->ddbsymtab + symidx;
1777 if (sym->st_shndx != SHN_UNDEF)
1780 sym_name = ef->ddbstrtab + sym->st_name;
1781 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1783 linkset_name = sym_name + sizeof(startn) - 1;
1785 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1787 linkset_name = sym_name + sizeof(stopn) - 1;
1792 for (i = 0; i < ef->nprogtab; i++) {
1793 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1794 startp = (Elf_Addr)ef->progtab[i].addr;
1795 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1799 if (i == ef->nprogtab)
1802 sym->st_value = start ? startp : stopp;
1808 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1810 elf_file_t ef = (elf_file_t)lf;
1811 const Elf_Rel *rellim;
1813 const Elf_Rela *relalim;
1814 const Elf_Rela *rela;
1820 link_elf_fix_link_set(ef);
1822 /* Perform relocations without addend if there are any: */
1823 for (i = 0; i < ef->nreltab; i++) {
1824 rel = ef->reltab[i].rel;
1826 link_elf_error(ef->lf.filename, "lost a reltab");
1829 rellim = rel + ef->reltab[i].nrel;
1830 base = findbase(ef, ef->reltab[i].sec);
1832 link_elf_error(ef->lf.filename, "lost base for reltab");
1835 for ( ; rel < rellim; rel++) {
1836 symidx = ELF_R_SYM(rel->r_info);
1837 if (symidx >= ef->ddbsymcnt)
1839 sym = ef->ddbsymtab + symidx;
1840 /* Only do local relocs */
1841 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1843 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1844 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1846 if (elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1847 elf_obj_lookup) != 0)
1852 /* Perform relocations with addend if there are any: */
1853 for (i = 0; i < ef->nrelatab; i++) {
1854 rela = ef->relatab[i].rela;
1856 link_elf_error(ef->lf.filename, "lost a relatab!");
1859 relalim = rela + ef->relatab[i].nrela;
1860 base = findbase(ef, ef->relatab[i].sec);
1862 link_elf_error(ef->lf.filename, "lost base for reltab");
1865 for ( ; rela < relalim; rela++) {
1866 symidx = ELF_R_SYM(rela->r_info);
1867 if (symidx >= ef->ddbsymcnt)
1869 sym = ef->ddbsymtab + symidx;
1870 /* Only do local relocs */
1871 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1873 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1874 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1876 if (elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1877 elf_obj_lookup) != 0)
1885 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1887 elf_file_t ef = (elf_file_t)lf;
1889 *symtab = ef->ddbsymtab;
1890 if (*symtab == NULL)
1892 return (ef->ddbsymcnt);
1896 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1898 elf_file_t ef = (elf_file_t)lf;
1900 *strtab = ef->ddbstrtab;
1901 if (*strtab == NULL)
1903 return (ef->ddbstrcnt);
1908 link_elf_propagate_vnets(linker_file_t lf)
1910 elf_file_t ef = (elf_file_t) lf;
1913 for (int i = 0; i < ef->nprogtab; i++) {
1914 if (ef->progtab[i].size == 0)
1916 if (ef->progtab[i].name == NULL)
1918 if (strcmp(ef->progtab[i].name, VNET_SETNAME) == 0) {
1919 vnet_data_copy(ef->progtab[i].addr,
1920 ef->progtab[i].size);