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_symbol_values(linker_file_t, c_linker_sym_t,
136 static int link_elf_debug_symbol_values(linker_file_t, c_linker_sym_t,
138 static int link_elf_search_symbol(linker_file_t, caddr_t value,
139 c_linker_sym_t *sym, long *diffp);
141 static void link_elf_unload_file(linker_file_t);
142 static int link_elf_lookup_set(linker_file_t, const char *,
143 void ***, void ***, int *);
144 static int link_elf_each_function_name(linker_file_t,
145 int (*)(const char *, void *), void *);
146 static int link_elf_each_function_nameval(linker_file_t,
147 linker_function_nameval_callback_t,
149 static int link_elf_reloc_local(linker_file_t, bool);
150 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
151 static long link_elf_strtab_get(linker_file_t, caddr_t *);
153 static void link_elf_propagate_vnets(linker_file_t);
156 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps,
159 static kobj_method_t link_elf_methods[] = {
160 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
161 KOBJMETHOD(linker_lookup_debug_symbol, link_elf_lookup_debug_symbol),
162 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
163 KOBJMETHOD(linker_debug_symbol_values, link_elf_debug_symbol_values),
164 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
165 KOBJMETHOD(linker_unload, link_elf_unload_file),
166 KOBJMETHOD(linker_load_file, link_elf_load_file),
167 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
168 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
169 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
170 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
171 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
172 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
173 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
174 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
176 KOBJMETHOD(linker_propagate_vnets, link_elf_propagate_vnets),
181 static struct linker_class link_elf_class = {
182 #if ELF_TARG_CLASS == ELFCLASS32
187 link_elf_methods, sizeof(struct elf_file)
190 static bool link_elf_obj_leak_locals = true;
191 SYSCTL_BOOL(_debug, OID_AUTO, link_elf_obj_leak_locals,
192 CTLFLAG_RWTUN, &link_elf_obj_leak_locals, 0,
193 "Allow local symbols to participate in global module symbol resolution");
195 static int relocate_file(elf_file_t ef);
196 static void elf_obj_cleanup_globals_cache(elf_file_t);
199 link_elf_error(const char *filename, const char *s)
201 if (filename == NULL)
202 printf("kldload: %s\n", s);
204 printf("kldload: %s: %s\n", filename, s);
208 link_elf_init(void *arg)
211 linker_add_class(&link_elf_class);
213 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, NULL);
216 link_elf_protect_range(elf_file_t ef, vm_offset_t start, vm_offset_t end,
221 KASSERT(start <= end && start >= (vm_offset_t)ef->address &&
222 end <= round_page((vm_offset_t)ef->address + ef->lf.size),
223 ("link_elf_protect_range: invalid range %#jx-%#jx",
224 (uintmax_t)start, (uintmax_t)end));
230 error = pmap_change_prot(start, end - start, prot);
232 ("link_elf_protect_range: pmap_change_prot() returned %d",
237 error = vm_map_protect(kernel_map, start, end, prot, 0,
238 VM_MAP_PROTECT_SET_PROT);
239 KASSERT(error == KERN_SUCCESS,
240 ("link_elf_protect_range: vm_map_protect() returned %d", error));
244 * Restrict permissions on linker file memory based on section flags.
245 * Sections need not be page-aligned, so overlap within a page is possible.
248 link_elf_protect(elf_file_t ef)
250 vm_offset_t end, segend, segstart, start;
251 vm_prot_t gapprot, prot, segprot;
255 * If the file was preloaded, the last page may contain other preloaded
256 * data which may need to be writeable. ELF files are always
257 * page-aligned, but other preloaded data, such as entropy or CPU
258 * microcode may be loaded with a smaller alignment.
260 gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ;
262 start = end = (vm_offset_t)ef->address;
264 for (i = 0; i < ef->nprogtab; i++) {
266 * VNET and DPCPU sections have their memory allocated by their
267 * respective subsystems.
269 if (ef->progtab[i].name != NULL && (
271 strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 ||
273 strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0))
276 segstart = trunc_page((vm_offset_t)ef->progtab[i].addr);
277 segend = round_page((vm_offset_t)ef->progtab[i].addr +
278 ef->progtab[i].size);
279 segprot = VM_PROT_READ;
280 if ((ef->progtab[i].flags & SHF_WRITE) != 0)
281 segprot |= VM_PROT_WRITE;
282 if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0)
283 segprot |= VM_PROT_EXECUTE;
285 if (end <= segstart) {
287 * Case 1: there is no overlap between the previous
288 * segment and this one. Apply protections to the
289 * previous segment, and protect the gap between the
290 * previous and current segments, if any.
292 link_elf_protect_range(ef, start, end, prot);
293 link_elf_protect_range(ef, end, segstart, gapprot);
298 } else if (start < segstart && end == segend) {
300 * Case 2: the current segment is a subrange of the
301 * previous segment. Apply protections to the
302 * non-overlapping portion of the previous segment.
304 link_elf_protect_range(ef, start, segstart, prot);
308 } else if (end < segend) {
310 * Case 3: there is partial overlap between the previous
311 * and current segments. Apply protections to the
312 * non-overlapping portion of the previous segment, and
313 * then the overlap, which must use the union of the two
314 * segments' protections.
316 link_elf_protect_range(ef, start, segstart, prot);
317 link_elf_protect_range(ef, segstart, end,
324 * Case 4: the two segments reside in the same page.
331 * Fix up the last unprotected segment and trailing data.
333 link_elf_protect_range(ef, start, end, prot);
334 link_elf_protect_range(ef, end,
335 round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
339 link_elf_link_preload(linker_class_t cls, const char *filename,
340 linker_file_t *result)
345 void *modptr, *baseptr, *sizeptr;
350 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
352 /* Look to see if we have the file preloaded */
353 modptr = preload_search_by_name(filename);
357 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
358 baseptr = preload_search_info(modptr, MODINFO_ADDR);
359 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
360 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
362 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
364 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
365 " obj module") != 0 &&
366 strcmp(type, "elf obj module") != 0)) {
369 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
373 lf = linker_make_file(filename, &link_elf_class);
379 ef->address = *(caddr_t *)baseptr;
380 lf->address = *(caddr_t *)baseptr;
381 lf->size = *(size_t *)sizeptr;
383 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
384 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
385 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
386 hdr->e_version != EV_CURRENT ||
387 hdr->e_type != ET_REL ||
388 hdr->e_machine != ELF_TARG_MACH) {
394 /* Scan the section header for information and table sizing. */
397 for (i = 0; i < hdr->e_shnum; i++) {
398 switch (shdr[i].sh_type) {
402 case SHT_X86_64_UNWIND:
406 /* Ignore sections not loaded by the loader. */
407 if (shdr[i].sh_addr == 0)
413 symstrindex = shdr[i].sh_link;
417 * Ignore relocation tables for sections not
418 * loaded by the loader.
420 if (shdr[shdr[i].sh_info].sh_addr == 0)
425 if (shdr[shdr[i].sh_info].sh_addr == 0)
432 shstrindex = hdr->e_shstrndx;
433 if (ef->nprogtab == 0 || symstrindex < 0 ||
434 symstrindex >= hdr->e_shnum ||
435 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
436 shstrindex >= hdr->e_shnum ||
437 shdr[shstrindex].sh_type != SHT_STRTAB) {
438 printf("%s: bad/missing section headers\n", filename);
443 /* Allocate space for tracking the load chunks */
444 if (ef->nprogtab != 0)
445 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
446 M_LINKER, M_WAITOK | M_ZERO);
447 if (ef->nreltab != 0)
448 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
449 M_LINKER, M_WAITOK | M_ZERO);
450 if (ef->nrelatab != 0)
451 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
452 M_LINKER, M_WAITOK | M_ZERO);
453 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
454 (ef->nreltab != 0 && ef->reltab == NULL) ||
455 (ef->nrelatab != 0 && ef->relatab == NULL)) {
460 /* XXX, relocate the sh_addr fields saved by the loader. */
462 for (i = 0; i < hdr->e_shnum; i++) {
463 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
464 off = shdr[i].sh_addr;
466 for (i = 0; i < hdr->e_shnum; i++) {
467 if (shdr[i].sh_addr != 0)
468 shdr[i].sh_addr = shdr[i].sh_addr - off +
469 (Elf_Addr)ef->address;
472 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
473 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
474 ef->ddbstrcnt = shdr[symstrindex].sh_size;
475 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
476 ef->shstrcnt = shdr[shstrindex].sh_size;
477 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
479 /* Now fill out progtab and the relocation tables. */
483 for (i = 0; i < hdr->e_shnum; i++) {
484 switch (shdr[i].sh_type) {
488 case SHT_X86_64_UNWIND:
492 if (shdr[i].sh_addr == 0)
494 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
495 if (shdr[i].sh_type == SHT_PROGBITS)
496 ef->progtab[pb].name = "<<PROGBITS>>";
498 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
499 ef->progtab[pb].name = "<<UNWIND>>";
501 else if (shdr[i].sh_type == SHT_INIT_ARRAY)
502 ef->progtab[pb].name = "<<INIT_ARRAY>>";
503 else if (shdr[i].sh_type == SHT_FINI_ARRAY)
504 ef->progtab[pb].name = "<<FINI_ARRAY>>";
506 ef->progtab[pb].name = "<<NOBITS>>";
507 ef->progtab[pb].size = shdr[i].sh_size;
508 ef->progtab[pb].flags = shdr[i].sh_flags;
509 ef->progtab[pb].sec = i;
510 if (ef->shstrtab && shdr[i].sh_name != 0)
511 ef->progtab[pb].name =
512 ef->shstrtab + shdr[i].sh_name;
513 if (ef->progtab[pb].name != NULL &&
514 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
517 dpcpu = dpcpu_alloc(shdr[i].sh_size);
519 printf("%s: pcpu module space is out "
520 "of space; cannot allocate %#jx "
521 "for %s\n", __func__,
522 (uintmax_t)shdr[i].sh_size,
527 memcpy(dpcpu, ef->progtab[pb].addr,
528 ef->progtab[pb].size);
529 dpcpu_copy(dpcpu, shdr[i].sh_size);
530 ef->progtab[pb].addr = dpcpu;
532 } else if (ef->progtab[pb].name != NULL &&
533 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
536 vnet_data = vnet_data_alloc(shdr[i].sh_size);
537 if (vnet_data == NULL) {
538 printf("%s: vnet module space is out "
539 "of space; cannot allocate %#jx "
540 "for %s\n", __func__,
541 (uintmax_t)shdr[i].sh_size,
546 memcpy(vnet_data, ef->progtab[pb].addr,
547 ef->progtab[pb].size);
548 ef->progtab[pb].addr = vnet_data;
549 vnet_save_init(ef->progtab[pb].addr,
550 ef->progtab[pb].size);
552 } else if ((ef->progtab[pb].name != NULL &&
553 strcmp(ef->progtab[pb].name, ".ctors") == 0) ||
554 shdr[i].sh_type == SHT_INIT_ARRAY) {
555 if (lf->ctors_addr != 0) {
557 "%s: multiple ctor sections in %s\n",
560 lf->ctors_addr = ef->progtab[pb].addr;
561 lf->ctors_size = shdr[i].sh_size;
563 } else if ((ef->progtab[pb].name != NULL &&
564 strcmp(ef->progtab[pb].name, ".dtors") == 0) ||
565 shdr[i].sh_type == SHT_FINI_ARRAY) {
566 if (lf->dtors_addr != 0) {
568 "%s: multiple dtor sections in %s\n",
571 lf->dtors_addr = ef->progtab[pb].addr;
572 lf->dtors_size = shdr[i].sh_size;
576 /* Update all symbol values with the offset. */
577 for (j = 0; j < ef->ddbsymcnt; j++) {
578 es = &ef->ddbsymtab[j];
579 if (es->st_shndx != i)
581 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
586 if (shdr[shdr[i].sh_info].sh_addr == 0)
588 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
589 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
590 ef->reltab[rl].sec = shdr[i].sh_info;
594 if (shdr[shdr[i].sh_info].sh_addr == 0)
596 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
597 ef->relatab[ra].nrela =
598 shdr[i].sh_size / sizeof(Elf_Rela);
599 ef->relatab[ra].sec = shdr[i].sh_info;
604 if (pb != ef->nprogtab) {
605 printf("%s: lost progbits\n", filename);
609 if (rl != ef->nreltab) {
610 printf("%s: lost reltab\n", filename);
614 if (ra != ef->nrelatab) {
615 printf("%s: lost relatab\n", filename);
621 * The file needs to be writeable and executable while applying
622 * relocations. Mapping protections are applied once relocation
623 * processing is complete.
625 link_elf_protect_range(ef, (vm_offset_t)ef->address,
626 round_page((vm_offset_t)ef->address + ef->lf.size), VM_PROT_ALL);
628 /* Local intra-module relocations */
629 error = link_elf_reloc_local(lf, false);
636 /* preload not done this way */
637 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
642 link_elf_invoke_cbs(caddr_t addr, size_t size)
647 if (addr == NULL || size == 0)
649 cnt = size / sizeof(*ctor);
651 for (i = 0; i < cnt; i++) {
658 link_elf_link_preload_finish(linker_file_t lf)
664 error = relocate_file(ef);
668 /* Notify MD code that a module is being loaded. */
669 error = elf_cpu_load_file(lf);
673 #if defined(__i386__) || defined(__amd64__)
675 error = link_elf_reloc_local(lf, true);
680 /* Apply protections now that relocation processing is complete. */
681 link_elf_protect(ef);
683 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
688 link_elf_load_file(linker_class_t cls, const char *filename,
689 linker_file_t *result)
691 struct nameidata *nd;
692 struct thread *td = curthread; /* XXX */
716 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
717 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename);
719 error = vn_open(nd, &flags, 0, NULL);
725 if (nd->ni_vp->v_type != VREG) {
730 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
736 /* Read the elf header from the file. */
737 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
738 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
739 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
753 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
754 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
755 link_elf_error(filename, "Unsupported file layout");
759 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
760 || hdr->e_version != EV_CURRENT) {
761 link_elf_error(filename, "Unsupported file version");
765 if (hdr->e_type != ET_REL) {
769 if (hdr->e_machine != ELF_TARG_MACH) {
770 link_elf_error(filename, "Unsupported machine");
775 lf = linker_make_file(filename, &link_elf_class);
780 ef = (elf_file_t) lf;
786 /* Allocate and read in the section header */
787 nbytes = hdr->e_shnum * hdr->e_shentsize;
788 if (nbytes == 0 || hdr->e_shoff == 0 ||
789 hdr->e_shentsize != sizeof(Elf_Shdr)) {
793 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
795 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
796 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
805 /* Scan the section header for information and table sizing. */
809 for (i = 0; i < hdr->e_shnum; i++) {
810 if (shdr[i].sh_size == 0)
812 switch (shdr[i].sh_type) {
816 case SHT_X86_64_UNWIND:
820 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
827 symstrindex = shdr[i].sh_link;
831 * Ignore relocation tables for unallocated
834 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
839 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
847 if (ef->nprogtab == 0) {
848 link_elf_error(filename, "file has no contents");
853 /* Only allow one symbol table for now */
854 link_elf_error(filename,
855 "file must have exactly one symbol table");
859 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
860 shdr[symstrindex].sh_type != SHT_STRTAB) {
861 link_elf_error(filename, "file has invalid symbol strings");
866 /* Allocate space for tracking the load chunks */
867 if (ef->nprogtab != 0)
868 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
869 M_LINKER, M_WAITOK | M_ZERO);
870 if (ef->nreltab != 0)
871 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
872 M_LINKER, M_WAITOK | M_ZERO);
873 if (ef->nrelatab != 0)
874 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
875 M_LINKER, M_WAITOK | M_ZERO);
877 if (symtabindex == -1) {
878 link_elf_error(filename, "lost symbol table index");
882 /* Allocate space for and load the symbol table */
883 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
884 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
885 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
886 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
887 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
896 /* Allocate space for and load the symbol strings */
897 ef->ddbstrcnt = shdr[symstrindex].sh_size;
898 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
899 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
900 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
901 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
910 /* Do we have a string table for the section names? */
912 if (hdr->e_shstrndx != 0 &&
913 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
914 shstrindex = hdr->e_shstrndx;
915 ef->shstrcnt = shdr[shstrindex].sh_size;
916 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
918 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
919 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
920 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
930 /* Size up code/data(progbits) and bss(nobits). */
932 for (i = 0; i < hdr->e_shnum; i++) {
933 if (shdr[i].sh_size == 0)
935 switch (shdr[i].sh_type) {
939 case SHT_X86_64_UNWIND:
943 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
945 alignmask = shdr[i].sh_addralign - 1;
946 mapsize += alignmask;
947 mapsize &= ~alignmask;
948 mapsize += shdr[i].sh_size;
954 * We know how much space we need for the text/data/bss/etc.
955 * This stuff needs to be in a single chunk so that profiling etc
956 * can get the bounds and gdb can associate offsets with modules
958 ef->object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(mapsize),
959 VM_PROT_ALL, 0, thread0.td_ucred);
960 if (ef->object == NULL) {
964 #if VM_NRESERVLEVEL > 0
965 vm_object_color(ef->object, 0);
969 * In order to satisfy amd64's architectural requirements on the
970 * location of code and data in the kernel's address space, request a
971 * mapping that is above the kernel.
973 * Protections will be restricted once relocations are applied.
978 mapbase = VM_MIN_KERNEL_ADDRESS;
980 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
981 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
983 if (error != KERN_SUCCESS) {
984 vm_object_deallocate(ef->object);
991 error = vm_map_wire(kernel_map, mapbase,
992 mapbase + round_page(mapsize),
993 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
994 if (error != KERN_SUCCESS) {
999 /* Inform the kld system about the situation */
1000 lf->address = ef->address = (caddr_t)mapbase;
1004 * Now load code/data(progbits), zero bss(nobits), allocate space for
1011 for (i = 0; i < hdr->e_shnum; i++) {
1012 if (shdr[i].sh_size == 0)
1014 switch (shdr[i].sh_type) {
1018 case SHT_X86_64_UNWIND:
1020 case SHT_INIT_ARRAY:
1021 case SHT_FINI_ARRAY:
1022 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
1024 alignmask = shdr[i].sh_addralign - 1;
1025 mapbase += alignmask;
1026 mapbase &= ~alignmask;
1027 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
1028 ef->progtab[pb].name =
1029 ef->shstrtab + shdr[i].sh_name;
1030 if (!strcmp(ef->progtab[pb].name, ".ctors") ||
1031 shdr[i].sh_type == SHT_INIT_ARRAY) {
1032 if (lf->ctors_addr != 0) {
1034 "%s: multiple ctor sections in %s\n",
1035 __func__, filename);
1042 } else if (!strcmp(ef->progtab[pb].name,
1044 shdr[i].sh_type == SHT_FINI_ARRAY) {
1045 if (lf->dtors_addr != 0) {
1047 "%s: multiple dtor sections in %s\n",
1048 __func__, filename);
1056 } else if (shdr[i].sh_type == SHT_PROGBITS)
1057 ef->progtab[pb].name = "<<PROGBITS>>";
1059 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
1060 ef->progtab[pb].name = "<<UNWIND>>";
1063 ef->progtab[pb].name = "<<NOBITS>>";
1064 if (ef->progtab[pb].name != NULL &&
1065 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
1066 ef->progtab[pb].addr =
1067 dpcpu_alloc(shdr[i].sh_size);
1068 if (ef->progtab[pb].addr == NULL) {
1069 printf("%s: pcpu module space is out "
1070 "of space; cannot allocate %#jx "
1071 "for %s\n", __func__,
1072 (uintmax_t)shdr[i].sh_size,
1077 else if (ef->progtab[pb].name != NULL &&
1078 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
1079 ef->progtab[pb].addr =
1080 vnet_data_alloc(shdr[i].sh_size);
1081 if (ef->progtab[pb].addr == NULL) {
1082 printf("%s: vnet module space is out "
1083 "of space; cannot allocate %#jx "
1084 "for %s\n", __func__,
1085 (uintmax_t)shdr[i].sh_size,
1091 ef->progtab[pb].addr =
1092 (void *)(uintptr_t)mapbase;
1093 if (ef->progtab[pb].addr == NULL) {
1097 ef->progtab[pb].size = shdr[i].sh_size;
1098 ef->progtab[pb].flags = shdr[i].sh_flags;
1099 ef->progtab[pb].sec = i;
1100 if (shdr[i].sh_type == SHT_PROGBITS
1102 || shdr[i].sh_type == SHT_X86_64_UNWIND
1105 error = vn_rdwr(UIO_READ, nd->ni_vp,
1106 ef->progtab[pb].addr,
1107 shdr[i].sh_size, shdr[i].sh_offset,
1108 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1109 NOCRED, &resid, td);
1116 /* Initialize the per-cpu area. */
1117 if (ef->progtab[pb].addr != (void *)mapbase &&
1118 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1119 dpcpu_copy(ef->progtab[pb].addr,
1122 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1125 if (ef->progtab[pb].addr != (void *)mapbase &&
1126 strcmp(ef->progtab[pb].name, VNET_SETNAME) == 0)
1127 vnet_save_init(ef->progtab[pb].addr,
1128 ef->progtab[pb].size);
1130 /* Update all symbol values with the offset. */
1131 for (j = 0; j < ef->ddbsymcnt; j++) {
1132 es = &ef->ddbsymtab[j];
1133 if (es->st_shndx != i)
1135 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1137 mapbase += shdr[i].sh_size;
1141 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1143 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1145 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1146 ef->reltab[rl].sec = shdr[i].sh_info;
1147 error = vn_rdwr(UIO_READ, nd->ni_vp,
1148 (void *)ef->reltab[rl].rel,
1149 shdr[i].sh_size, shdr[i].sh_offset,
1150 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1161 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1163 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1165 ef->relatab[ra].nrela =
1166 shdr[i].sh_size / sizeof(Elf_Rela);
1167 ef->relatab[ra].sec = shdr[i].sh_info;
1168 error = vn_rdwr(UIO_READ, nd->ni_vp,
1169 (void *)ef->relatab[ra].rela,
1170 shdr[i].sh_size, shdr[i].sh_offset,
1171 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1183 if (pb != ef->nprogtab) {
1184 link_elf_error(filename, "lost progbits");
1188 if (rl != ef->nreltab) {
1189 link_elf_error(filename, "lost reltab");
1193 if (ra != ef->nrelatab) {
1194 link_elf_error(filename, "lost relatab");
1198 if (mapbase != (vm_offset_t)ef->address + mapsize) {
1200 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1201 filename != NULL ? filename : "<none>",
1202 (u_long)mapbase, ef->address, (u_long)mapsize,
1203 (u_long)(vm_offset_t)ef->address + mapsize);
1208 /* Local intra-module relocations */
1209 error = link_elf_reloc_local(lf, false);
1213 /* Pull in dependencies */
1214 VOP_UNLOCK(nd->ni_vp);
1215 error = linker_load_dependencies(lf);
1216 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1220 /* External relocations */
1221 error = relocate_file(ef);
1225 /* Notify MD code that a module is being loaded. */
1226 error = elf_cpu_load_file(lf);
1230 #if defined(__i386__) || defined(__amd64__)
1232 error = link_elf_reloc_local(lf, true);
1237 link_elf_protect(ef);
1238 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
1242 VOP_UNLOCK(nd->ni_vp);
1243 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1246 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1247 free(hdr, M_LINKER);
1253 link_elf_unload_file(linker_file_t file)
1255 elf_file_t ef = (elf_file_t) file;
1258 link_elf_invoke_cbs(file->dtors_addr, file->dtors_size);
1260 /* Notify MD code that a module is being unloaded. */
1261 elf_cpu_unload_file(file);
1264 for (i = 0; i < ef->nprogtab; i++) {
1265 if (ef->progtab[i].size == 0)
1267 if (ef->progtab[i].name == NULL)
1269 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1270 dpcpu_free(ef->progtab[i].addr,
1271 ef->progtab[i].size);
1273 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1274 vnet_data_free(ef->progtab[i].addr,
1275 ef->progtab[i].size);
1279 if (ef->preloaded) {
1280 free(ef->reltab, M_LINKER);
1281 free(ef->relatab, M_LINKER);
1282 free(ef->progtab, M_LINKER);
1283 free(ef->ctftab, M_LINKER);
1284 free(ef->ctfoff, M_LINKER);
1285 free(ef->typoff, M_LINKER);
1286 if (file->pathname != NULL)
1287 preload_delete_name(file->pathname);
1291 for (i = 0; i < ef->nreltab; i++)
1292 free(ef->reltab[i].rel, M_LINKER);
1293 for (i = 0; i < ef->nrelatab; i++)
1294 free(ef->relatab[i].rela, M_LINKER);
1295 free(ef->reltab, M_LINKER);
1296 free(ef->relatab, M_LINKER);
1297 free(ef->progtab, M_LINKER);
1299 if (ef->object != NULL)
1300 vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1301 (vm_offset_t)ef->address + ptoa(ef->object->size));
1302 free(ef->e_shdr, M_LINKER);
1303 free(ef->ddbsymtab, M_LINKER);
1304 free(ef->ddbstrtab, M_LINKER);
1305 free(ef->shstrtab, M_LINKER);
1306 free(ef->ctftab, M_LINKER);
1307 free(ef->ctfoff, M_LINKER);
1308 free(ef->typoff, M_LINKER);
1312 symbol_name(elf_file_t ef, Elf_Size r_info)
1316 if (ELF_R_SYM(r_info)) {
1317 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1318 return ef->ddbstrtab + ref->st_name;
1324 findbase(elf_file_t ef, int sec)
1329 for (i = 0; i < ef->nprogtab; i++) {
1330 if (sec == ef->progtab[i].sec) {
1331 base = (Elf_Addr)ef->progtab[i].addr;
1339 relocate_file1(elf_file_t ef, bool ifuncs)
1341 const Elf_Rel *rellim;
1343 const Elf_Rela *relalim;
1344 const Elf_Rela *rela;
1345 const char *symname;
1351 /* Perform relocations without addend if there are any: */
1352 for (i = 0; i < ef->nreltab; i++) {
1353 rel = ef->reltab[i].rel;
1355 link_elf_error(ef->lf.filename, "lost a reltab!");
1358 rellim = rel + ef->reltab[i].nrel;
1359 base = findbase(ef, ef->reltab[i].sec);
1361 link_elf_error(ef->lf.filename, "lost base for reltab");
1364 for ( ; rel < rellim; rel++) {
1365 symidx = ELF_R_SYM(rel->r_info);
1366 if (symidx >= ef->ddbsymcnt)
1368 sym = ef->ddbsymtab + symidx;
1369 /* Local relocs are already done */
1370 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1372 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1373 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1375 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1377 symname = symbol_name(ef, rel->r_info);
1378 printf("link_elf_obj: symbol %s undefined\n",
1385 /* Perform relocations with addend if there are any: */
1386 for (i = 0; i < ef->nrelatab; i++) {
1387 rela = ef->relatab[i].rela;
1389 link_elf_error(ef->lf.filename, "lost a relatab!");
1392 relalim = rela + ef->relatab[i].nrela;
1393 base = findbase(ef, ef->relatab[i].sec);
1395 link_elf_error(ef->lf.filename,
1396 "lost base for relatab");
1399 for ( ; rela < relalim; rela++) {
1400 symidx = ELF_R_SYM(rela->r_info);
1401 if (symidx >= ef->ddbsymcnt)
1403 sym = ef->ddbsymtab + symidx;
1404 /* Local relocs are already done */
1405 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1407 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1408 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1410 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1412 symname = symbol_name(ef, rela->r_info);
1413 printf("link_elf_obj: symbol %s undefined\n",
1421 * Only clean SHN_FBSD_CACHED for successful return. If we
1422 * modified symbol table for the object but found an
1423 * unresolved symbol, there is no reason to roll back.
1425 elf_obj_cleanup_globals_cache(ef);
1431 relocate_file(elf_file_t ef)
1435 error = relocate_file1(ef, false);
1437 error = relocate_file1(ef, true);
1442 link_elf_lookup_symbol1(linker_file_t lf, const char *name, c_linker_sym_t *sym,
1445 elf_file_t ef = (elf_file_t)lf;
1446 const Elf_Sym *symp;
1450 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1451 strp = ef->ddbstrtab + symp->st_name;
1452 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1454 ELF_ST_BIND(symp->st_info) == STB_GLOBAL) {
1455 *sym = (c_linker_sym_t) symp;
1465 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1467 return (link_elf_lookup_symbol1(lf, name, sym,
1468 link_elf_obj_leak_locals));
1472 link_elf_lookup_debug_symbol(linker_file_t lf, const char *name,
1473 c_linker_sym_t *sym)
1475 return (link_elf_lookup_symbol1(lf, name, sym, true));
1479 link_elf_symbol_values1(linker_file_t lf, c_linker_sym_t sym,
1480 linker_symval_t *symval, bool see_local)
1486 ef = (elf_file_t) lf;
1487 es = (const Elf_Sym*) sym;
1488 val = (caddr_t)es->st_value;
1489 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1490 if (!see_local && ELF_ST_BIND(es->st_info) == STB_LOCAL)
1492 symval->name = ef->ddbstrtab + es->st_name;
1493 val = (caddr_t)es->st_value;
1494 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1495 val = ((caddr_t (*)(void))val)();
1496 symval->value = val;
1497 symval->size = es->st_size;
1504 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1505 linker_symval_t *symval)
1507 return (link_elf_symbol_values1(lf, sym, symval,
1508 link_elf_obj_leak_locals));
1512 link_elf_debug_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1513 linker_symval_t *symval)
1515 return (link_elf_symbol_values1(lf, sym, symval, true));
1519 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1520 c_linker_sym_t *sym, long *diffp)
1522 elf_file_t ef = (elf_file_t)lf;
1523 u_long off = (uintptr_t)(void *)value;
1527 const Elf_Sym *best = NULL;
1530 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1531 if (es->st_name == 0)
1533 st_value = es->st_value;
1534 if (off >= st_value) {
1535 if (off - st_value < diff) {
1536 diff = off - st_value;
1540 } else if (off - st_value == diff) {
1549 *sym = (c_linker_sym_t) best;
1555 * Look up a linker set on an ELF system.
1558 link_elf_lookup_set(linker_file_t lf, const char *name,
1559 void ***startp, void ***stopp, int *countp)
1561 elf_file_t ef = (elf_file_t)lf;
1562 void **start, **stop;
1565 /* Relative to section number */
1566 for (i = 0; i < ef->nprogtab; i++) {
1567 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1568 strcmp(ef->progtab[i].name + 4, name) == 0) {
1569 start = (void **)ef->progtab[i].addr;
1570 stop = (void **)((char *)ef->progtab[i].addr +
1571 ef->progtab[i].size);
1572 count = stop - start;
1586 link_elf_each_function_name(linker_file_t file,
1587 int (*callback)(const char *, void *), void *opaque)
1589 elf_file_t ef = (elf_file_t)file;
1590 const Elf_Sym *symp;
1593 /* Exhaustive search */
1594 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1595 if (symp->st_value != 0 &&
1596 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1597 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1598 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1607 link_elf_each_function_nameval(linker_file_t file,
1608 linker_function_nameval_callback_t callback, void *opaque)
1610 linker_symval_t symval;
1611 elf_file_t ef = (elf_file_t)file;
1612 const Elf_Sym *symp;
1615 /* Exhaustive search */
1616 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1617 if (symp->st_value != 0 &&
1618 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1619 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1620 error = link_elf_debug_symbol_values(file,
1621 (c_linker_sym_t)symp, &symval);
1623 error = callback(file, i, &symval, opaque);
1632 elf_obj_cleanup_globals_cache(elf_file_t ef)
1637 for (i = 0; i < ef->ddbsymcnt; i++) {
1638 sym = ef->ddbsymtab + i;
1639 if (sym->st_shndx == SHN_FBSD_CACHED) {
1640 sym->st_shndx = SHN_UNDEF;
1647 * Symbol lookup function that can be used when the symbol index is known (ie
1648 * in relocations). It uses the symbol index instead of doing a fully fledged
1649 * hash table based lookup when such is valid. For example for local symbols.
1650 * This is not only more efficient, it's also more correct. It's not always
1651 * the case that the symbol can be found through the hash table.
1654 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1656 elf_file_t ef = (elf_file_t)lf;
1661 /* Don't even try to lookup the symbol if the index is bogus. */
1662 if (symidx >= ef->ddbsymcnt) {
1667 sym = ef->ddbsymtab + symidx;
1669 /* Quick answer if there is a definition included. */
1670 if (sym->st_shndx != SHN_UNDEF) {
1671 res1 = (Elf_Addr)sym->st_value;
1672 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1673 res1 = ((Elf_Addr (*)(void))res1)();
1678 /* If we get here, then it is undefined and needs a lookup. */
1679 switch (ELF_ST_BIND(sym->st_info)) {
1681 /* Local, but undefined? huh? */
1687 /* Relative to Data or Function name */
1688 symbol = ef->ddbstrtab + sym->st_name;
1690 /* Force a lookup failure if the symbol name is bogus. */
1695 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1698 * Cache global lookups during module relocation. The failure
1699 * case is particularly expensive for callers, who must scan
1700 * through the entire globals table doing strcmp(). Cache to
1701 * avoid doing such work repeatedly.
1703 * After relocation is complete, undefined globals will be
1704 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1708 sym->st_shndx = SHN_FBSD_CACHED;
1709 sym->st_value = res1;
1712 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1725 link_elf_fix_link_set(elf_file_t ef)
1727 static const char startn[] = "__start_";
1728 static const char stopn[] = "__stop_";
1730 const char *sym_name, *linkset_name;
1731 Elf_Addr startp, stopp;
1736 for (symidx = 1 /* zero entry is special */;
1737 symidx < ef->ddbsymcnt; symidx++) {
1738 sym = ef->ddbsymtab + symidx;
1739 if (sym->st_shndx != SHN_UNDEF)
1742 sym_name = ef->ddbstrtab + sym->st_name;
1743 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1745 linkset_name = sym_name + sizeof(startn) - 1;
1747 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1749 linkset_name = sym_name + sizeof(stopn) - 1;
1754 for (i = 0; i < ef->nprogtab; i++) {
1755 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1756 startp = (Elf_Addr)ef->progtab[i].addr;
1757 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1761 if (i == ef->nprogtab)
1764 sym->st_value = start ? startp : stopp;
1770 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1772 elf_file_t ef = (elf_file_t)lf;
1773 const Elf_Rel *rellim;
1775 const Elf_Rela *relalim;
1776 const Elf_Rela *rela;
1782 link_elf_fix_link_set(ef);
1784 /* Perform relocations without addend if there are any: */
1785 for (i = 0; i < ef->nreltab; i++) {
1786 rel = ef->reltab[i].rel;
1788 link_elf_error(ef->lf.filename, "lost a reltab");
1791 rellim = rel + ef->reltab[i].nrel;
1792 base = findbase(ef, ef->reltab[i].sec);
1794 link_elf_error(ef->lf.filename, "lost base for reltab");
1797 for ( ; rel < rellim; rel++) {
1798 symidx = ELF_R_SYM(rel->r_info);
1799 if (symidx >= ef->ddbsymcnt)
1801 sym = ef->ddbsymtab + symidx;
1802 /* Only do local relocs */
1803 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1805 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1806 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1808 if (elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1809 elf_obj_lookup) != 0)
1814 /* Perform relocations with addend if there are any: */
1815 for (i = 0; i < ef->nrelatab; i++) {
1816 rela = ef->relatab[i].rela;
1818 link_elf_error(ef->lf.filename, "lost a relatab!");
1821 relalim = rela + ef->relatab[i].nrela;
1822 base = findbase(ef, ef->relatab[i].sec);
1824 link_elf_error(ef->lf.filename, "lost base for reltab");
1827 for ( ; rela < relalim; rela++) {
1828 symidx = ELF_R_SYM(rela->r_info);
1829 if (symidx >= ef->ddbsymcnt)
1831 sym = ef->ddbsymtab + symidx;
1832 /* Only do local relocs */
1833 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1835 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1836 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1838 if (elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1839 elf_obj_lookup) != 0)
1847 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1849 elf_file_t ef = (elf_file_t)lf;
1851 *symtab = ef->ddbsymtab;
1852 if (*symtab == NULL)
1854 return (ef->ddbsymcnt);
1858 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1860 elf_file_t ef = (elf_file_t)lf;
1862 *strtab = ef->ddbstrtab;
1863 if (*strtab == NULL)
1865 return (ef->ddbstrcnt);
1870 link_elf_propagate_vnets(linker_file_t lf)
1872 elf_file_t ef = (elf_file_t) lf;
1875 for (int i = 0; i < ef->nprogtab; i++) {
1876 if (ef->progtab[i].size == 0)
1878 if (ef->progtab[i].name == NULL)
1880 if (strcmp(ef->progtab[i].name, VNET_SETNAME) == 0) {
1881 vnet_data_copy(ef->progtab[i].addr,
1882 ef->progtab[i].size);