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_link_preload_finish(linker_file_t lf)
668 error = relocate_file(ef);
672 /* Notify MD code that a module is being loaded. */
673 error = elf_cpu_load_file(lf);
677 #if defined(__i386__) || defined(__amd64__)
679 error = link_elf_reloc_local(lf, true);
684 /* Apply protections now that relocation processing is complete. */
685 link_elf_protect(ef);
687 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
692 link_elf_load_file(linker_class_t cls, const char *filename,
693 linker_file_t *result)
695 struct nameidata *nd;
696 struct thread *td = curthread; /* XXX */
720 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
721 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename);
723 error = vn_open(nd, &flags, 0, NULL);
729 if (nd->ni_vp->v_type != VREG) {
734 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
740 /* Read the elf header from the file. */
741 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
742 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
743 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
757 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
758 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
759 link_elf_error(filename, "Unsupported file layout");
763 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
764 || hdr->e_version != EV_CURRENT) {
765 link_elf_error(filename, "Unsupported file version");
769 if (hdr->e_type != ET_REL) {
773 if (hdr->e_machine != ELF_TARG_MACH) {
774 link_elf_error(filename, "Unsupported machine");
779 lf = linker_make_file(filename, &link_elf_class);
784 ef = (elf_file_t) lf;
790 /* Allocate and read in the section header */
791 nbytes = hdr->e_shnum * hdr->e_shentsize;
792 if (nbytes == 0 || hdr->e_shoff == 0 ||
793 hdr->e_shentsize != sizeof(Elf_Shdr)) {
797 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
799 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
800 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
809 /* Scan the section header for information and table sizing. */
813 for (i = 0; i < hdr->e_shnum; i++) {
814 if (shdr[i].sh_size == 0)
816 switch (shdr[i].sh_type) {
820 case SHT_X86_64_UNWIND:
824 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
831 symstrindex = shdr[i].sh_link;
835 * Ignore relocation tables for unallocated
838 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
843 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
851 if (ef->nprogtab == 0) {
852 link_elf_error(filename, "file has no contents");
857 /* Only allow one symbol table for now */
858 link_elf_error(filename,
859 "file must have exactly one symbol table");
863 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
864 shdr[symstrindex].sh_type != SHT_STRTAB) {
865 link_elf_error(filename, "file has invalid symbol strings");
870 /* Allocate space for tracking the load chunks */
871 if (ef->nprogtab != 0)
872 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
873 M_LINKER, M_WAITOK | M_ZERO);
874 if (ef->nreltab != 0)
875 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
876 M_LINKER, M_WAITOK | M_ZERO);
877 if (ef->nrelatab != 0)
878 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
879 M_LINKER, M_WAITOK | M_ZERO);
881 if (symtabindex == -1) {
882 link_elf_error(filename, "lost symbol table index");
886 /* Allocate space for and load the symbol table */
887 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
888 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
889 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
890 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
891 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
900 /* Allocate space for and load the symbol strings */
901 ef->ddbstrcnt = shdr[symstrindex].sh_size;
902 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
903 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
904 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
905 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
914 /* Do we have a string table for the section names? */
916 if (hdr->e_shstrndx != 0 &&
917 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
918 shstrindex = hdr->e_shstrndx;
919 ef->shstrcnt = shdr[shstrindex].sh_size;
920 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
922 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
923 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
924 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
934 /* Size up code/data(progbits) and bss(nobits). */
936 for (i = 0; i < hdr->e_shnum; i++) {
937 if (shdr[i].sh_size == 0)
939 switch (shdr[i].sh_type) {
943 case SHT_X86_64_UNWIND:
947 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
949 alignmask = shdr[i].sh_addralign - 1;
950 mapsize += alignmask;
951 mapsize &= ~alignmask;
952 mapsize += shdr[i].sh_size;
958 * We know how much space we need for the text/data/bss/etc.
959 * This stuff needs to be in a single chunk so that profiling etc
960 * can get the bounds and gdb can associate offsets with modules
962 ef->object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(mapsize),
963 VM_PROT_ALL, 0, thread0.td_ucred);
964 if (ef->object == NULL) {
968 #if VM_NRESERVLEVEL > 0
969 vm_object_color(ef->object, 0);
973 * In order to satisfy amd64's architectural requirements on the
974 * location of code and data in the kernel's address space, request a
975 * mapping that is above the kernel.
977 * Protections will be restricted once relocations are applied.
982 mapbase = VM_MIN_KERNEL_ADDRESS;
984 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
985 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
987 if (error != KERN_SUCCESS) {
988 vm_object_deallocate(ef->object);
995 error = vm_map_wire(kernel_map, mapbase,
996 mapbase + round_page(mapsize),
997 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
998 if (error != KERN_SUCCESS) {
1003 /* Inform the kld system about the situation */
1004 lf->address = ef->address = (caddr_t)mapbase;
1008 * Now load code/data(progbits), zero bss(nobits), allocate space for
1015 for (i = 0; i < hdr->e_shnum; i++) {
1016 if (shdr[i].sh_size == 0)
1018 switch (shdr[i].sh_type) {
1022 case SHT_X86_64_UNWIND:
1024 case SHT_INIT_ARRAY:
1025 case SHT_FINI_ARRAY:
1026 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
1028 alignmask = shdr[i].sh_addralign - 1;
1029 mapbase += alignmask;
1030 mapbase &= ~alignmask;
1031 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
1032 ef->progtab[pb].name =
1033 ef->shstrtab + shdr[i].sh_name;
1034 if (!strcmp(ef->progtab[pb].name, ".ctors") ||
1035 shdr[i].sh_type == SHT_INIT_ARRAY) {
1036 if (lf->ctors_addr != 0) {
1038 "%s: multiple ctor sections in %s\n",
1039 __func__, filename);
1046 } else if (!strcmp(ef->progtab[pb].name,
1048 shdr[i].sh_type == SHT_FINI_ARRAY) {
1049 if (lf->dtors_addr != 0) {
1051 "%s: multiple dtor sections in %s\n",
1052 __func__, filename);
1060 } else if (shdr[i].sh_type == SHT_PROGBITS)
1061 ef->progtab[pb].name = "<<PROGBITS>>";
1063 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
1064 ef->progtab[pb].name = "<<UNWIND>>";
1067 ef->progtab[pb].name = "<<NOBITS>>";
1068 if (ef->progtab[pb].name != NULL &&
1069 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
1070 ef->progtab[pb].addr =
1071 dpcpu_alloc(shdr[i].sh_size);
1072 if (ef->progtab[pb].addr == NULL) {
1073 printf("%s: pcpu module space is out "
1074 "of space; cannot allocate %#jx "
1075 "for %s\n", __func__,
1076 (uintmax_t)shdr[i].sh_size,
1081 else if (ef->progtab[pb].name != NULL &&
1082 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
1083 ef->progtab[pb].addr =
1084 vnet_data_alloc(shdr[i].sh_size);
1085 if (ef->progtab[pb].addr == NULL) {
1086 printf("%s: vnet module space is out "
1087 "of space; cannot allocate %#jx "
1088 "for %s\n", __func__,
1089 (uintmax_t)shdr[i].sh_size,
1095 ef->progtab[pb].addr =
1096 (void *)(uintptr_t)mapbase;
1097 if (ef->progtab[pb].addr == NULL) {
1101 ef->progtab[pb].size = shdr[i].sh_size;
1102 ef->progtab[pb].flags = shdr[i].sh_flags;
1103 ef->progtab[pb].sec = i;
1104 if (shdr[i].sh_type == SHT_PROGBITS
1106 || shdr[i].sh_type == SHT_X86_64_UNWIND
1109 error = vn_rdwr(UIO_READ, nd->ni_vp,
1110 ef->progtab[pb].addr,
1111 shdr[i].sh_size, shdr[i].sh_offset,
1112 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1113 NOCRED, &resid, td);
1120 /* Initialize the per-cpu area. */
1121 if (ef->progtab[pb].addr != (void *)mapbase &&
1122 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1123 dpcpu_copy(ef->progtab[pb].addr,
1126 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1129 if (ef->progtab[pb].addr != (void *)mapbase &&
1130 strcmp(ef->progtab[pb].name, VNET_SETNAME) == 0)
1131 vnet_save_init(ef->progtab[pb].addr,
1132 ef->progtab[pb].size);
1134 /* Update all symbol values with the offset. */
1135 for (j = 0; j < ef->ddbsymcnt; j++) {
1136 es = &ef->ddbsymtab[j];
1137 if (es->st_shndx != i)
1139 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1141 mapbase += shdr[i].sh_size;
1145 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1147 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1149 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1150 ef->reltab[rl].sec = shdr[i].sh_info;
1151 error = vn_rdwr(UIO_READ, nd->ni_vp,
1152 (void *)ef->reltab[rl].rel,
1153 shdr[i].sh_size, shdr[i].sh_offset,
1154 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1165 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1167 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1169 ef->relatab[ra].nrela =
1170 shdr[i].sh_size / sizeof(Elf_Rela);
1171 ef->relatab[ra].sec = shdr[i].sh_info;
1172 error = vn_rdwr(UIO_READ, nd->ni_vp,
1173 (void *)ef->relatab[ra].rela,
1174 shdr[i].sh_size, shdr[i].sh_offset,
1175 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1187 if (pb != ef->nprogtab) {
1188 link_elf_error(filename, "lost progbits");
1192 if (rl != ef->nreltab) {
1193 link_elf_error(filename, "lost reltab");
1197 if (ra != ef->nrelatab) {
1198 link_elf_error(filename, "lost relatab");
1202 if (mapbase != (vm_offset_t)ef->address + mapsize) {
1204 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1205 filename != NULL ? filename : "<none>",
1206 (u_long)mapbase, ef->address, (u_long)mapsize,
1207 (u_long)(vm_offset_t)ef->address + mapsize);
1212 /* Local intra-module relocations */
1213 error = link_elf_reloc_local(lf, false);
1217 /* Pull in dependencies */
1218 VOP_UNLOCK(nd->ni_vp);
1219 error = linker_load_dependencies(lf);
1220 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1224 /* External relocations */
1225 error = relocate_file(ef);
1229 /* Notify MD code that a module is being loaded. */
1230 error = elf_cpu_load_file(lf);
1234 #if defined(__i386__) || defined(__amd64__)
1236 error = link_elf_reloc_local(lf, true);
1241 link_elf_protect(ef);
1242 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
1246 VOP_UNLOCK(nd->ni_vp);
1247 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1250 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1251 free(hdr, M_LINKER);
1257 link_elf_unload_file(linker_file_t file)
1259 elf_file_t ef = (elf_file_t) file;
1262 link_elf_invoke_cbs(file->dtors_addr, file->dtors_size);
1264 /* Notify MD code that a module is being unloaded. */
1265 elf_cpu_unload_file(file);
1268 for (i = 0; i < ef->nprogtab; i++) {
1269 if (ef->progtab[i].size == 0)
1271 if (ef->progtab[i].name == NULL)
1273 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1274 dpcpu_free(ef->progtab[i].addr,
1275 ef->progtab[i].size);
1277 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1278 vnet_data_free(ef->progtab[i].addr,
1279 ef->progtab[i].size);
1283 if (ef->preloaded) {
1284 free(ef->reltab, M_LINKER);
1285 free(ef->relatab, M_LINKER);
1286 free(ef->progtab, M_LINKER);
1287 free(ef->ctftab, M_LINKER);
1288 free(ef->ctfoff, M_LINKER);
1289 free(ef->typoff, M_LINKER);
1290 if (file->pathname != NULL)
1291 preload_delete_name(file->pathname);
1295 for (i = 0; i < ef->nreltab; i++)
1296 free(ef->reltab[i].rel, M_LINKER);
1297 for (i = 0; i < ef->nrelatab; i++)
1298 free(ef->relatab[i].rela, M_LINKER);
1299 free(ef->reltab, M_LINKER);
1300 free(ef->relatab, M_LINKER);
1301 free(ef->progtab, M_LINKER);
1303 if (ef->object != NULL)
1304 vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1305 (vm_offset_t)ef->address + ptoa(ef->object->size));
1306 free(ef->e_shdr, M_LINKER);
1307 free(ef->ddbsymtab, M_LINKER);
1308 free(ef->ddbstrtab, M_LINKER);
1309 free(ef->shstrtab, M_LINKER);
1310 free(ef->ctftab, M_LINKER);
1311 free(ef->ctfoff, M_LINKER);
1312 free(ef->typoff, M_LINKER);
1316 symbol_name(elf_file_t ef, Elf_Size r_info)
1320 if (ELF_R_SYM(r_info)) {
1321 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1322 return ef->ddbstrtab + ref->st_name;
1328 findbase(elf_file_t ef, int sec)
1333 for (i = 0; i < ef->nprogtab; i++) {
1334 if (sec == ef->progtab[i].sec) {
1335 base = (Elf_Addr)ef->progtab[i].addr;
1343 relocate_file1(elf_file_t ef, bool ifuncs)
1345 const Elf_Rel *rellim;
1347 const Elf_Rela *relalim;
1348 const Elf_Rela *rela;
1349 const char *symname;
1355 /* Perform relocations without addend if there are any: */
1356 for (i = 0; i < ef->nreltab; i++) {
1357 rel = ef->reltab[i].rel;
1359 link_elf_error(ef->lf.filename, "lost a reltab!");
1362 rellim = rel + ef->reltab[i].nrel;
1363 base = findbase(ef, ef->reltab[i].sec);
1365 link_elf_error(ef->lf.filename, "lost base for reltab");
1368 for ( ; rel < rellim; rel++) {
1369 symidx = ELF_R_SYM(rel->r_info);
1370 if (symidx >= ef->ddbsymcnt)
1372 sym = ef->ddbsymtab + symidx;
1373 /* Local relocs are already done */
1374 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1376 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1377 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1379 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1381 symname = symbol_name(ef, rel->r_info);
1382 printf("link_elf_obj: symbol %s undefined\n",
1389 /* Perform relocations with addend if there are any: */
1390 for (i = 0; i < ef->nrelatab; i++) {
1391 rela = ef->relatab[i].rela;
1393 link_elf_error(ef->lf.filename, "lost a relatab!");
1396 relalim = rela + ef->relatab[i].nrela;
1397 base = findbase(ef, ef->relatab[i].sec);
1399 link_elf_error(ef->lf.filename,
1400 "lost base for relatab");
1403 for ( ; rela < relalim; rela++) {
1404 symidx = ELF_R_SYM(rela->r_info);
1405 if (symidx >= ef->ddbsymcnt)
1407 sym = ef->ddbsymtab + symidx;
1408 /* Local relocs are already done */
1409 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1411 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1412 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1414 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1416 symname = symbol_name(ef, rela->r_info);
1417 printf("link_elf_obj: symbol %s undefined\n",
1425 * Only clean SHN_FBSD_CACHED for successful return. If we
1426 * modified symbol table for the object but found an
1427 * unresolved symbol, there is no reason to roll back.
1429 elf_obj_cleanup_globals_cache(ef);
1435 relocate_file(elf_file_t ef)
1439 error = relocate_file1(ef, false);
1441 error = relocate_file1(ef, true);
1446 link_elf_lookup_symbol1(linker_file_t lf, const char *name, c_linker_sym_t *sym,
1449 elf_file_t ef = (elf_file_t)lf;
1450 const Elf_Sym *symp;
1454 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1455 strp = ef->ddbstrtab + symp->st_name;
1456 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1458 ELF_ST_BIND(symp->st_info) == STB_GLOBAL) {
1459 *sym = (c_linker_sym_t) symp;
1469 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1471 return (link_elf_lookup_symbol1(lf, name, sym,
1472 link_elf_obj_leak_locals));
1476 link_elf_lookup_debug_symbol(linker_file_t lf, const char *name,
1477 c_linker_sym_t *sym)
1479 return (link_elf_lookup_symbol1(lf, name, sym, true));
1483 link_elf_lookup_debug_symbol_ctf(linker_file_t lf, const char *name,
1484 c_linker_sym_t *sym, linker_ctf_t *lc)
1486 if (link_elf_lookup_debug_symbol(lf, name, sym))
1489 return (link_elf_ctf_get_ddb(lf, lc));
1493 link_elf_symbol_values1(linker_file_t lf, c_linker_sym_t sym,
1494 linker_symval_t *symval, bool see_local)
1500 ef = (elf_file_t) lf;
1501 es = (const Elf_Sym*) sym;
1502 val = (caddr_t)es->st_value;
1503 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1504 if (!see_local && ELF_ST_BIND(es->st_info) == STB_LOCAL)
1506 symval->name = ef->ddbstrtab + es->st_name;
1507 val = (caddr_t)es->st_value;
1508 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1509 val = ((caddr_t (*)(void))val)();
1510 symval->value = val;
1511 symval->size = es->st_size;
1518 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1519 linker_symval_t *symval)
1521 return (link_elf_symbol_values1(lf, sym, symval,
1522 link_elf_obj_leak_locals));
1526 link_elf_debug_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1527 linker_symval_t *symval)
1529 return (link_elf_symbol_values1(lf, sym, symval, true));
1533 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1534 c_linker_sym_t *sym, long *diffp)
1536 elf_file_t ef = (elf_file_t)lf;
1537 u_long off = (uintptr_t)(void *)value;
1541 const Elf_Sym *best = NULL;
1544 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1545 if (es->st_name == 0)
1547 st_value = es->st_value;
1548 if (off >= st_value) {
1549 if (off - st_value < diff) {
1550 diff = off - st_value;
1554 } else if (off - st_value == diff) {
1563 *sym = (c_linker_sym_t) best;
1569 * Look up a linker set on an ELF system.
1572 link_elf_lookup_set(linker_file_t lf, const char *name,
1573 void ***startp, void ***stopp, int *countp)
1575 elf_file_t ef = (elf_file_t)lf;
1576 void **start, **stop;
1579 /* Relative to section number */
1580 for (i = 0; i < ef->nprogtab; i++) {
1581 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1582 strcmp(ef->progtab[i].name + 4, name) == 0) {
1583 start = (void **)ef->progtab[i].addr;
1584 stop = (void **)((char *)ef->progtab[i].addr +
1585 ef->progtab[i].size);
1586 count = stop - start;
1600 link_elf_each_function_name(linker_file_t file,
1601 int (*callback)(const char *, void *), void *opaque)
1603 elf_file_t ef = (elf_file_t)file;
1604 const Elf_Sym *symp;
1607 /* Exhaustive search */
1608 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1609 if (symp->st_value != 0 &&
1610 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1611 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1612 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1621 link_elf_each_function_nameval(linker_file_t file,
1622 linker_function_nameval_callback_t callback, void *opaque)
1624 linker_symval_t symval;
1625 elf_file_t ef = (elf_file_t)file;
1626 const Elf_Sym *symp;
1629 /* Exhaustive search */
1630 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1631 if (symp->st_value != 0 &&
1632 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1633 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1634 error = link_elf_debug_symbol_values(file,
1635 (c_linker_sym_t)symp, &symval);
1637 error = callback(file, i, &symval, opaque);
1646 elf_obj_cleanup_globals_cache(elf_file_t ef)
1651 for (i = 0; i < ef->ddbsymcnt; i++) {
1652 sym = ef->ddbsymtab + i;
1653 if (sym->st_shndx == SHN_FBSD_CACHED) {
1654 sym->st_shndx = SHN_UNDEF;
1661 * Symbol lookup function that can be used when the symbol index is known (ie
1662 * in relocations). It uses the symbol index instead of doing a fully fledged
1663 * hash table based lookup when such is valid. For example for local symbols.
1664 * This is not only more efficient, it's also more correct. It's not always
1665 * the case that the symbol can be found through the hash table.
1668 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1670 elf_file_t ef = (elf_file_t)lf;
1675 /* Don't even try to lookup the symbol if the index is bogus. */
1676 if (symidx >= ef->ddbsymcnt) {
1681 sym = ef->ddbsymtab + symidx;
1683 /* Quick answer if there is a definition included. */
1684 if (sym->st_shndx != SHN_UNDEF) {
1685 res1 = (Elf_Addr)sym->st_value;
1686 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1687 res1 = ((Elf_Addr (*)(void))res1)();
1692 /* If we get here, then it is undefined and needs a lookup. */
1693 switch (ELF_ST_BIND(sym->st_info)) {
1695 /* Local, but undefined? huh? */
1701 /* Relative to Data or Function name */
1702 symbol = ef->ddbstrtab + sym->st_name;
1704 /* Force a lookup failure if the symbol name is bogus. */
1709 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1712 * Cache global lookups during module relocation. The failure
1713 * case is particularly expensive for callers, who must scan
1714 * through the entire globals table doing strcmp(). Cache to
1715 * avoid doing such work repeatedly.
1717 * After relocation is complete, undefined globals will be
1718 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1722 sym->st_shndx = SHN_FBSD_CACHED;
1723 sym->st_value = res1;
1726 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1739 link_elf_fix_link_set(elf_file_t ef)
1741 static const char startn[] = "__start_";
1742 static const char stopn[] = "__stop_";
1744 const char *sym_name, *linkset_name;
1745 Elf_Addr startp, stopp;
1750 for (symidx = 1 /* zero entry is special */;
1751 symidx < ef->ddbsymcnt; symidx++) {
1752 sym = ef->ddbsymtab + symidx;
1753 if (sym->st_shndx != SHN_UNDEF)
1756 sym_name = ef->ddbstrtab + sym->st_name;
1757 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1759 linkset_name = sym_name + sizeof(startn) - 1;
1761 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1763 linkset_name = sym_name + sizeof(stopn) - 1;
1768 for (i = 0; i < ef->nprogtab; i++) {
1769 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1770 startp = (Elf_Addr)ef->progtab[i].addr;
1771 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1775 if (i == ef->nprogtab)
1778 sym->st_value = start ? startp : stopp;
1784 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1786 elf_file_t ef = (elf_file_t)lf;
1787 const Elf_Rel *rellim;
1789 const Elf_Rela *relalim;
1790 const Elf_Rela *rela;
1796 link_elf_fix_link_set(ef);
1798 /* Perform relocations without addend if there are any: */
1799 for (i = 0; i < ef->nreltab; i++) {
1800 rel = ef->reltab[i].rel;
1802 link_elf_error(ef->lf.filename, "lost a reltab");
1805 rellim = rel + ef->reltab[i].nrel;
1806 base = findbase(ef, ef->reltab[i].sec);
1808 link_elf_error(ef->lf.filename, "lost base for reltab");
1811 for ( ; rel < rellim; rel++) {
1812 symidx = ELF_R_SYM(rel->r_info);
1813 if (symidx >= ef->ddbsymcnt)
1815 sym = ef->ddbsymtab + symidx;
1816 /* Only do local relocs */
1817 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1819 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1820 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1822 if (elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1823 elf_obj_lookup) != 0)
1828 /* Perform relocations with addend if there are any: */
1829 for (i = 0; i < ef->nrelatab; i++) {
1830 rela = ef->relatab[i].rela;
1832 link_elf_error(ef->lf.filename, "lost a relatab!");
1835 relalim = rela + ef->relatab[i].nrela;
1836 base = findbase(ef, ef->relatab[i].sec);
1838 link_elf_error(ef->lf.filename, "lost base for reltab");
1841 for ( ; rela < relalim; rela++) {
1842 symidx = ELF_R_SYM(rela->r_info);
1843 if (symidx >= ef->ddbsymcnt)
1845 sym = ef->ddbsymtab + symidx;
1846 /* Only do local relocs */
1847 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1849 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1850 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1852 if (elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1853 elf_obj_lookup) != 0)
1861 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1863 elf_file_t ef = (elf_file_t)lf;
1865 *symtab = ef->ddbsymtab;
1866 if (*symtab == NULL)
1868 return (ef->ddbsymcnt);
1872 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1874 elf_file_t ef = (elf_file_t)lf;
1876 *strtab = ef->ddbstrtab;
1877 if (*strtab == NULL)
1879 return (ef->ddbstrcnt);
1884 link_elf_propagate_vnets(linker_file_t lf)
1886 elf_file_t ef = (elf_file_t) lf;
1889 for (int i = 0; i < ef->nprogtab; i++) {
1890 if (ef->progtab[i].size == 0)
1892 if (ef->progtab[i].name == NULL)
1894 if (strcmp(ef->progtab[i].name, VNET_SETNAME) == 0) {
1895 vnet_data_copy(ef->progtab[i].addr,
1896 ef->progtab[i].size);