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
30 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/linker.h>
40 #include <sys/mutex.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
44 #include <sys/rwlock.h>
45 #include <sys/sysctl.h>
46 #include <sys/vnode.h>
48 #include <machine/elf.h>
52 #include <security/mac/mac_framework.h>
55 #include <vm/vm_param.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_pager.h>
64 #include <sys/link_elf.h>
67 #include <contrib/zlib/zlib.h>
70 #include "linker_if.h"
75 int flags; /* Section flags. */
76 int sec; /* Original section number. */
92 typedef struct elf_file {
93 struct linker_file lf; /* Common fields */
96 caddr_t address; /* Relocation address */
97 vm_object_t object; /* VM object to hold file pages */
100 Elf_progent *progtab;
103 Elf_relaent *relatab;
109 Elf_Sym *ddbsymtab; /* The symbol table we are using */
110 long ddbsymcnt; /* Number of symbols */
111 caddr_t ddbstrtab; /* String table */
112 long ddbstrcnt; /* number of bytes in string table */
114 caddr_t shstrtab; /* Section name string table */
115 long shstrcnt; /* number of bytes in string table */
117 caddr_t ctftab; /* CTF table */
118 long ctfcnt; /* number of bytes in CTF table */
119 caddr_t ctfoff; /* CTF offset table */
120 caddr_t typoff; /* Type offset table */
121 long typlen; /* Number of type entries. */
125 #include <kern/kern_ctf.c>
127 static int link_elf_link_preload(linker_class_t cls,
128 const char *, linker_file_t *);
129 static int link_elf_link_preload_finish(linker_file_t);
130 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *);
131 static int link_elf_lookup_symbol(linker_file_t, const char *,
133 static int link_elf_lookup_debug_symbol(linker_file_t, const char *,
135 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t,
137 static int link_elf_debug_symbol_values(linker_file_t, c_linker_sym_t,
139 static int link_elf_search_symbol(linker_file_t, caddr_t value,
140 c_linker_sym_t *sym, long *diffp);
142 static void link_elf_unload_file(linker_file_t);
143 static int link_elf_lookup_set(linker_file_t, const char *,
144 void ***, void ***, int *);
145 static int link_elf_each_function_name(linker_file_t,
146 int (*)(const char *, void *), void *);
147 static int link_elf_each_function_nameval(linker_file_t,
148 linker_function_nameval_callback_t,
150 static int link_elf_reloc_local(linker_file_t, bool);
151 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
152 static long link_elf_strtab_get(linker_file_t, caddr_t *);
154 static void link_elf_propagate_vnets(linker_file_t);
157 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps,
160 static kobj_method_t link_elf_methods[] = {
161 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
162 KOBJMETHOD(linker_lookup_debug_symbol, link_elf_lookup_debug_symbol),
163 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
164 KOBJMETHOD(linker_debug_symbol_values, link_elf_debug_symbol_values),
165 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
166 KOBJMETHOD(linker_unload, link_elf_unload_file),
167 KOBJMETHOD(linker_load_file, link_elf_load_file),
168 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
169 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
170 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
171 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
172 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
173 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
174 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
175 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
177 KOBJMETHOD(linker_propagate_vnets, link_elf_propagate_vnets),
182 static struct linker_class link_elf_class = {
183 #if ELF_TARG_CLASS == ELFCLASS32
188 link_elf_methods, sizeof(struct elf_file)
191 static bool link_elf_obj_leak_locals = true;
192 SYSCTL_BOOL(_debug, OID_AUTO, link_elf_obj_leak_locals,
193 CTLFLAG_RWTUN, &link_elf_obj_leak_locals, 0,
194 "Allow local symbols to participate in global module symbol resolution");
196 static int relocate_file(elf_file_t ef);
197 static void elf_obj_cleanup_globals_cache(elf_file_t);
200 link_elf_error(const char *filename, const char *s)
202 if (filename == NULL)
203 printf("kldload: %s\n", s);
205 printf("kldload: %s: %s\n", filename, s);
209 link_elf_init(void *arg)
212 linker_add_class(&link_elf_class);
214 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, NULL);
217 link_elf_protect_range(elf_file_t ef, vm_offset_t start, vm_offset_t end,
222 KASSERT(start <= end && start >= (vm_offset_t)ef->address &&
223 end <= round_page((vm_offset_t)ef->address + ef->lf.size),
224 ("link_elf_protect_range: invalid range %#jx-%#jx",
225 (uintmax_t)start, (uintmax_t)end));
231 error = pmap_change_prot(start, end - start, prot);
233 ("link_elf_protect_range: pmap_change_prot() returned %d",
238 error = vm_map_protect(kernel_map, start, end, prot, 0,
239 VM_MAP_PROTECT_SET_PROT);
240 KASSERT(error == KERN_SUCCESS,
241 ("link_elf_protect_range: vm_map_protect() returned %d", error));
245 * Restrict permissions on linker file memory based on section flags.
246 * Sections need not be page-aligned, so overlap within a page is possible.
249 link_elf_protect(elf_file_t ef)
251 vm_offset_t end, segend, segstart, start;
252 vm_prot_t gapprot, prot, segprot;
256 * If the file was preloaded, the last page may contain other preloaded
257 * data which may need to be writeable. ELF files are always
258 * page-aligned, but other preloaded data, such as entropy or CPU
259 * microcode may be loaded with a smaller alignment.
261 gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ;
263 start = end = (vm_offset_t)ef->address;
265 for (i = 0; i < ef->nprogtab; i++) {
267 * VNET and DPCPU sections have their memory allocated by their
268 * respective subsystems.
270 if (ef->progtab[i].name != NULL && (
272 strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 ||
274 strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0))
277 segstart = trunc_page((vm_offset_t)ef->progtab[i].addr);
278 segend = round_page((vm_offset_t)ef->progtab[i].addr +
279 ef->progtab[i].size);
280 segprot = VM_PROT_READ;
281 if ((ef->progtab[i].flags & SHF_WRITE) != 0)
282 segprot |= VM_PROT_WRITE;
283 if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0)
284 segprot |= VM_PROT_EXECUTE;
286 if (end <= segstart) {
288 * Case 1: there is no overlap between the previous
289 * segment and this one. Apply protections to the
290 * previous segment, and protect the gap between the
291 * previous and current segments, if any.
293 link_elf_protect_range(ef, start, end, prot);
294 link_elf_protect_range(ef, end, segstart, gapprot);
299 } else if (start < segstart && end == segend) {
301 * Case 2: the current segment is a subrange of the
302 * previous segment. Apply protections to the
303 * non-overlapping portion of the previous segment.
305 link_elf_protect_range(ef, start, segstart, prot);
309 } else if (end < segend) {
311 * Case 3: there is partial overlap between the previous
312 * and current segments. Apply protections to the
313 * non-overlapping portion of the previous segment, and
314 * then the overlap, which must use the union of the two
315 * segments' protections.
317 link_elf_protect_range(ef, start, segstart, prot);
318 link_elf_protect_range(ef, segstart, end,
325 * Case 4: the two segments reside in the same page.
332 * Fix up the last unprotected segment and trailing data.
334 link_elf_protect_range(ef, start, end, prot);
335 link_elf_protect_range(ef, end,
336 round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
340 link_elf_link_preload(linker_class_t cls, const char *filename,
341 linker_file_t *result)
346 void *modptr, *baseptr, *sizeptr;
351 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
353 /* Look to see if we have the file preloaded */
354 modptr = preload_search_by_name(filename);
358 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
359 baseptr = preload_search_info(modptr, MODINFO_ADDR);
360 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
361 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
363 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
365 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
366 " obj module") != 0 &&
367 strcmp(type, "elf obj module") != 0)) {
370 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
374 lf = linker_make_file(filename, &link_elf_class);
380 ef->address = *(caddr_t *)baseptr;
381 lf->address = *(caddr_t *)baseptr;
382 lf->size = *(size_t *)sizeptr;
384 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
385 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
386 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
387 hdr->e_version != EV_CURRENT ||
388 hdr->e_type != ET_REL ||
389 hdr->e_machine != ELF_TARG_MACH) {
395 /* Scan the section header for information and table sizing. */
398 for (i = 0; i < hdr->e_shnum; i++) {
399 switch (shdr[i].sh_type) {
403 case SHT_X86_64_UNWIND:
407 /* Ignore sections not loaded by the loader. */
408 if (shdr[i].sh_addr == 0)
414 symstrindex = shdr[i].sh_link;
418 * Ignore relocation tables for sections not
419 * loaded by the loader.
421 if (shdr[shdr[i].sh_info].sh_addr == 0)
426 if (shdr[shdr[i].sh_info].sh_addr == 0)
433 shstrindex = hdr->e_shstrndx;
434 if (ef->nprogtab == 0 || symstrindex < 0 ||
435 symstrindex >= hdr->e_shnum ||
436 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
437 shstrindex >= hdr->e_shnum ||
438 shdr[shstrindex].sh_type != SHT_STRTAB) {
439 printf("%s: bad/missing section headers\n", filename);
444 /* Allocate space for tracking the load chunks */
445 if (ef->nprogtab != 0)
446 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
447 M_LINKER, M_WAITOK | M_ZERO);
448 if (ef->nreltab != 0)
449 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
450 M_LINKER, M_WAITOK | M_ZERO);
451 if (ef->nrelatab != 0)
452 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
453 M_LINKER, M_WAITOK | M_ZERO);
454 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
455 (ef->nreltab != 0 && ef->reltab == NULL) ||
456 (ef->nrelatab != 0 && ef->relatab == NULL)) {
461 /* XXX, relocate the sh_addr fields saved by the loader. */
463 for (i = 0; i < hdr->e_shnum; i++) {
464 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
465 off = shdr[i].sh_addr;
467 for (i = 0; i < hdr->e_shnum; i++) {
468 if (shdr[i].sh_addr != 0)
469 shdr[i].sh_addr = shdr[i].sh_addr - off +
470 (Elf_Addr)ef->address;
473 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
474 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
475 ef->ddbstrcnt = shdr[symstrindex].sh_size;
476 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
477 ef->shstrcnt = shdr[shstrindex].sh_size;
478 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
480 /* Now fill out progtab and the relocation tables. */
484 for (i = 0; i < hdr->e_shnum; i++) {
485 switch (shdr[i].sh_type) {
489 case SHT_X86_64_UNWIND:
493 if (shdr[i].sh_addr == 0)
495 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
496 if (shdr[i].sh_type == SHT_PROGBITS)
497 ef->progtab[pb].name = "<<PROGBITS>>";
499 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
500 ef->progtab[pb].name = "<<UNWIND>>";
502 else if (shdr[i].sh_type == SHT_INIT_ARRAY)
503 ef->progtab[pb].name = "<<INIT_ARRAY>>";
504 else if (shdr[i].sh_type == SHT_FINI_ARRAY)
505 ef->progtab[pb].name = "<<FINI_ARRAY>>";
507 ef->progtab[pb].name = "<<NOBITS>>";
508 ef->progtab[pb].size = shdr[i].sh_size;
509 ef->progtab[pb].flags = shdr[i].sh_flags;
510 ef->progtab[pb].sec = i;
511 if (ef->shstrtab && shdr[i].sh_name != 0)
512 ef->progtab[pb].name =
513 ef->shstrtab + shdr[i].sh_name;
514 if (ef->progtab[pb].name != NULL &&
515 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
518 dpcpu = dpcpu_alloc(shdr[i].sh_size);
520 printf("%s: pcpu module space is out "
521 "of space; cannot allocate %#jx "
522 "for %s\n", __func__,
523 (uintmax_t)shdr[i].sh_size,
528 memcpy(dpcpu, ef->progtab[pb].addr,
529 ef->progtab[pb].size);
530 dpcpu_copy(dpcpu, shdr[i].sh_size);
531 ef->progtab[pb].addr = dpcpu;
533 } else if (ef->progtab[pb].name != NULL &&
534 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
537 vnet_data = vnet_data_alloc(shdr[i].sh_size);
538 if (vnet_data == NULL) {
539 printf("%s: vnet module space is out "
540 "of space; cannot allocate %#jx "
541 "for %s\n", __func__,
542 (uintmax_t)shdr[i].sh_size,
547 memcpy(vnet_data, ef->progtab[pb].addr,
548 ef->progtab[pb].size);
549 ef->progtab[pb].addr = vnet_data;
550 vnet_save_init(ef->progtab[pb].addr,
551 ef->progtab[pb].size);
553 } else if ((ef->progtab[pb].name != NULL &&
554 strcmp(ef->progtab[pb].name, ".ctors") == 0) ||
555 shdr[i].sh_type == SHT_INIT_ARRAY) {
556 if (lf->ctors_addr != 0) {
558 "%s: multiple ctor sections in %s\n",
561 lf->ctors_addr = ef->progtab[pb].addr;
562 lf->ctors_size = shdr[i].sh_size;
564 } else if ((ef->progtab[pb].name != NULL &&
565 strcmp(ef->progtab[pb].name, ".dtors") == 0) ||
566 shdr[i].sh_type == SHT_FINI_ARRAY) {
567 if (lf->dtors_addr != 0) {
569 "%s: multiple dtor sections in %s\n",
572 lf->dtors_addr = ef->progtab[pb].addr;
573 lf->dtors_size = shdr[i].sh_size;
577 /* Update all symbol values with the offset. */
578 for (j = 0; j < ef->ddbsymcnt; j++) {
579 es = &ef->ddbsymtab[j];
580 if (es->st_shndx != i)
582 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
587 if (shdr[shdr[i].sh_info].sh_addr == 0)
589 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
590 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
591 ef->reltab[rl].sec = shdr[i].sh_info;
595 if (shdr[shdr[i].sh_info].sh_addr == 0)
597 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
598 ef->relatab[ra].nrela =
599 shdr[i].sh_size / sizeof(Elf_Rela);
600 ef->relatab[ra].sec = shdr[i].sh_info;
605 if (pb != ef->nprogtab) {
606 printf("%s: lost progbits\n", filename);
610 if (rl != ef->nreltab) {
611 printf("%s: lost reltab\n", filename);
615 if (ra != ef->nrelatab) {
616 printf("%s: lost relatab\n", filename);
622 * The file needs to be writeable and executable while applying
623 * relocations. Mapping protections are applied once relocation
624 * processing is complete.
626 link_elf_protect_range(ef, (vm_offset_t)ef->address,
627 round_page((vm_offset_t)ef->address + ef->lf.size), VM_PROT_ALL);
629 /* Local intra-module relocations */
630 error = link_elf_reloc_local(lf, false);
637 /* preload not done this way */
638 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
643 link_elf_invoke_cbs(caddr_t addr, size_t size)
648 if (addr == NULL || size == 0)
650 cnt = size / sizeof(*ctor);
652 for (i = 0; i < cnt; i++) {
659 link_elf_link_preload_finish(linker_file_t lf)
665 error = relocate_file(ef);
669 /* Notify MD code that a module is being loaded. */
670 error = elf_cpu_load_file(lf);
674 #if defined(__i386__) || defined(__amd64__)
676 error = link_elf_reloc_local(lf, true);
681 /* Apply protections now that relocation processing is complete. */
682 link_elf_protect(ef);
684 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
689 link_elf_load_file(linker_class_t cls, const char *filename,
690 linker_file_t *result)
692 struct nameidata *nd;
693 struct thread *td = curthread; /* XXX */
717 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
718 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename);
720 error = vn_open(nd, &flags, 0, NULL);
726 if (nd->ni_vp->v_type != VREG) {
731 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
737 /* Read the elf header from the file. */
738 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
739 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
740 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
754 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
755 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
756 link_elf_error(filename, "Unsupported file layout");
760 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
761 || hdr->e_version != EV_CURRENT) {
762 link_elf_error(filename, "Unsupported file version");
766 if (hdr->e_type != ET_REL) {
770 if (hdr->e_machine != ELF_TARG_MACH) {
771 link_elf_error(filename, "Unsupported machine");
776 lf = linker_make_file(filename, &link_elf_class);
781 ef = (elf_file_t) lf;
787 /* Allocate and read in the section header */
788 nbytes = hdr->e_shnum * hdr->e_shentsize;
789 if (nbytes == 0 || hdr->e_shoff == 0 ||
790 hdr->e_shentsize != sizeof(Elf_Shdr)) {
794 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
796 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
797 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
806 /* Scan the section header for information and table sizing. */
810 for (i = 0; i < hdr->e_shnum; i++) {
811 if (shdr[i].sh_size == 0)
813 switch (shdr[i].sh_type) {
817 case SHT_X86_64_UNWIND:
821 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
828 symstrindex = shdr[i].sh_link;
832 * Ignore relocation tables for unallocated
835 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
840 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
848 if (ef->nprogtab == 0) {
849 link_elf_error(filename, "file has no contents");
854 /* Only allow one symbol table for now */
855 link_elf_error(filename,
856 "file must have exactly one symbol table");
860 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
861 shdr[symstrindex].sh_type != SHT_STRTAB) {
862 link_elf_error(filename, "file has invalid symbol strings");
867 /* Allocate space for tracking the load chunks */
868 if (ef->nprogtab != 0)
869 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
870 M_LINKER, M_WAITOK | M_ZERO);
871 if (ef->nreltab != 0)
872 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
873 M_LINKER, M_WAITOK | M_ZERO);
874 if (ef->nrelatab != 0)
875 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
876 M_LINKER, M_WAITOK | M_ZERO);
878 if (symtabindex == -1) {
879 link_elf_error(filename, "lost symbol table index");
883 /* Allocate space for and load the symbol table */
884 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
885 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
886 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
887 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
888 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
897 /* Allocate space for and load the symbol strings */
898 ef->ddbstrcnt = shdr[symstrindex].sh_size;
899 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
900 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
901 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
902 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
911 /* Do we have a string table for the section names? */
913 if (hdr->e_shstrndx != 0 &&
914 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
915 shstrindex = hdr->e_shstrndx;
916 ef->shstrcnt = shdr[shstrindex].sh_size;
917 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
919 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
920 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
921 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
931 /* Size up code/data(progbits) and bss(nobits). */
933 for (i = 0; i < hdr->e_shnum; i++) {
934 if (shdr[i].sh_size == 0)
936 switch (shdr[i].sh_type) {
940 case SHT_X86_64_UNWIND:
944 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
946 alignmask = shdr[i].sh_addralign - 1;
947 mapsize += alignmask;
948 mapsize &= ~alignmask;
949 mapsize += shdr[i].sh_size;
955 * We know how much space we need for the text/data/bss/etc.
956 * This stuff needs to be in a single chunk so that profiling etc
957 * can get the bounds and gdb can associate offsets with modules
959 ef->object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(mapsize),
960 VM_PROT_ALL, 0, thread0.td_ucred);
961 if (ef->object == NULL) {
965 #if VM_NRESERVLEVEL > 0
966 vm_object_color(ef->object, 0);
970 * In order to satisfy amd64's architectural requirements on the
971 * location of code and data in the kernel's address space, request a
972 * mapping that is above the kernel.
974 * Protections will be restricted once relocations are applied.
979 mapbase = VM_MIN_KERNEL_ADDRESS;
981 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
982 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
984 if (error != KERN_SUCCESS) {
985 vm_object_deallocate(ef->object);
992 error = vm_map_wire(kernel_map, mapbase,
993 mapbase + round_page(mapsize),
994 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
995 if (error != KERN_SUCCESS) {
1000 /* Inform the kld system about the situation */
1001 lf->address = ef->address = (caddr_t)mapbase;
1005 * Now load code/data(progbits), zero bss(nobits), allocate space for
1012 for (i = 0; i < hdr->e_shnum; i++) {
1013 if (shdr[i].sh_size == 0)
1015 switch (shdr[i].sh_type) {
1019 case SHT_X86_64_UNWIND:
1021 case SHT_INIT_ARRAY:
1022 case SHT_FINI_ARRAY:
1023 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
1025 alignmask = shdr[i].sh_addralign - 1;
1026 mapbase += alignmask;
1027 mapbase &= ~alignmask;
1028 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
1029 ef->progtab[pb].name =
1030 ef->shstrtab + shdr[i].sh_name;
1031 if (!strcmp(ef->progtab[pb].name, ".ctors") ||
1032 shdr[i].sh_type == SHT_INIT_ARRAY) {
1033 if (lf->ctors_addr != 0) {
1035 "%s: multiple ctor sections in %s\n",
1036 __func__, filename);
1043 } else if (!strcmp(ef->progtab[pb].name,
1045 shdr[i].sh_type == SHT_FINI_ARRAY) {
1046 if (lf->dtors_addr != 0) {
1048 "%s: multiple dtor sections in %s\n",
1049 __func__, filename);
1057 } else if (shdr[i].sh_type == SHT_PROGBITS)
1058 ef->progtab[pb].name = "<<PROGBITS>>";
1060 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
1061 ef->progtab[pb].name = "<<UNWIND>>";
1064 ef->progtab[pb].name = "<<NOBITS>>";
1065 if (ef->progtab[pb].name != NULL &&
1066 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
1067 ef->progtab[pb].addr =
1068 dpcpu_alloc(shdr[i].sh_size);
1069 if (ef->progtab[pb].addr == NULL) {
1070 printf("%s: pcpu module space is out "
1071 "of space; cannot allocate %#jx "
1072 "for %s\n", __func__,
1073 (uintmax_t)shdr[i].sh_size,
1078 else if (ef->progtab[pb].name != NULL &&
1079 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
1080 ef->progtab[pb].addr =
1081 vnet_data_alloc(shdr[i].sh_size);
1082 if (ef->progtab[pb].addr == NULL) {
1083 printf("%s: vnet module space is out "
1084 "of space; cannot allocate %#jx "
1085 "for %s\n", __func__,
1086 (uintmax_t)shdr[i].sh_size,
1092 ef->progtab[pb].addr =
1093 (void *)(uintptr_t)mapbase;
1094 if (ef->progtab[pb].addr == NULL) {
1098 ef->progtab[pb].size = shdr[i].sh_size;
1099 ef->progtab[pb].flags = shdr[i].sh_flags;
1100 ef->progtab[pb].sec = i;
1101 if (shdr[i].sh_type == SHT_PROGBITS
1103 || shdr[i].sh_type == SHT_X86_64_UNWIND
1106 error = vn_rdwr(UIO_READ, nd->ni_vp,
1107 ef->progtab[pb].addr,
1108 shdr[i].sh_size, shdr[i].sh_offset,
1109 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1110 NOCRED, &resid, td);
1117 /* Initialize the per-cpu area. */
1118 if (ef->progtab[pb].addr != (void *)mapbase &&
1119 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1120 dpcpu_copy(ef->progtab[pb].addr,
1123 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1126 if (ef->progtab[pb].addr != (void *)mapbase &&
1127 strcmp(ef->progtab[pb].name, VNET_SETNAME) == 0)
1128 vnet_save_init(ef->progtab[pb].addr,
1129 ef->progtab[pb].size);
1131 /* Update all symbol values with the offset. */
1132 for (j = 0; j < ef->ddbsymcnt; j++) {
1133 es = &ef->ddbsymtab[j];
1134 if (es->st_shndx != i)
1136 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1138 mapbase += shdr[i].sh_size;
1142 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1144 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1146 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1147 ef->reltab[rl].sec = shdr[i].sh_info;
1148 error = vn_rdwr(UIO_READ, nd->ni_vp,
1149 (void *)ef->reltab[rl].rel,
1150 shdr[i].sh_size, shdr[i].sh_offset,
1151 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1162 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1164 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1166 ef->relatab[ra].nrela =
1167 shdr[i].sh_size / sizeof(Elf_Rela);
1168 ef->relatab[ra].sec = shdr[i].sh_info;
1169 error = vn_rdwr(UIO_READ, nd->ni_vp,
1170 (void *)ef->relatab[ra].rela,
1171 shdr[i].sh_size, shdr[i].sh_offset,
1172 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1184 if (pb != ef->nprogtab) {
1185 link_elf_error(filename, "lost progbits");
1189 if (rl != ef->nreltab) {
1190 link_elf_error(filename, "lost reltab");
1194 if (ra != ef->nrelatab) {
1195 link_elf_error(filename, "lost relatab");
1199 if (mapbase != (vm_offset_t)ef->address + mapsize) {
1201 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1202 filename != NULL ? filename : "<none>",
1203 (u_long)mapbase, ef->address, (u_long)mapsize,
1204 (u_long)(vm_offset_t)ef->address + mapsize);
1209 /* Local intra-module relocations */
1210 error = link_elf_reloc_local(lf, false);
1214 /* Pull in dependencies */
1215 VOP_UNLOCK(nd->ni_vp);
1216 error = linker_load_dependencies(lf);
1217 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1221 /* External relocations */
1222 error = relocate_file(ef);
1226 /* Notify MD code that a module is being loaded. */
1227 error = elf_cpu_load_file(lf);
1231 #if defined(__i386__) || defined(__amd64__)
1233 error = link_elf_reloc_local(lf, true);
1238 link_elf_protect(ef);
1239 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
1243 VOP_UNLOCK(nd->ni_vp);
1244 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1247 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1248 free(hdr, M_LINKER);
1254 link_elf_unload_file(linker_file_t file)
1256 elf_file_t ef = (elf_file_t) file;
1259 link_elf_invoke_cbs(file->dtors_addr, file->dtors_size);
1261 /* Notify MD code that a module is being unloaded. */
1262 elf_cpu_unload_file(file);
1265 for (i = 0; i < ef->nprogtab; i++) {
1266 if (ef->progtab[i].size == 0)
1268 if (ef->progtab[i].name == NULL)
1270 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1271 dpcpu_free(ef->progtab[i].addr,
1272 ef->progtab[i].size);
1274 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1275 vnet_data_free(ef->progtab[i].addr,
1276 ef->progtab[i].size);
1280 if (ef->preloaded) {
1281 free(ef->reltab, M_LINKER);
1282 free(ef->relatab, M_LINKER);
1283 free(ef->progtab, M_LINKER);
1284 free(ef->ctftab, M_LINKER);
1285 free(ef->ctfoff, M_LINKER);
1286 free(ef->typoff, M_LINKER);
1287 if (file->pathname != NULL)
1288 preload_delete_name(file->pathname);
1292 for (i = 0; i < ef->nreltab; i++)
1293 free(ef->reltab[i].rel, M_LINKER);
1294 for (i = 0; i < ef->nrelatab; i++)
1295 free(ef->relatab[i].rela, M_LINKER);
1296 free(ef->reltab, M_LINKER);
1297 free(ef->relatab, M_LINKER);
1298 free(ef->progtab, M_LINKER);
1300 if (ef->object != NULL)
1301 vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1302 (vm_offset_t)ef->address + ptoa(ef->object->size));
1303 free(ef->e_shdr, M_LINKER);
1304 free(ef->ddbsymtab, M_LINKER);
1305 free(ef->ddbstrtab, M_LINKER);
1306 free(ef->shstrtab, M_LINKER);
1307 free(ef->ctftab, M_LINKER);
1308 free(ef->ctfoff, M_LINKER);
1309 free(ef->typoff, M_LINKER);
1313 symbol_name(elf_file_t ef, Elf_Size r_info)
1317 if (ELF_R_SYM(r_info)) {
1318 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1319 return ef->ddbstrtab + ref->st_name;
1325 findbase(elf_file_t ef, int sec)
1330 for (i = 0; i < ef->nprogtab; i++) {
1331 if (sec == ef->progtab[i].sec) {
1332 base = (Elf_Addr)ef->progtab[i].addr;
1340 relocate_file1(elf_file_t ef, bool ifuncs)
1342 const Elf_Rel *rellim;
1344 const Elf_Rela *relalim;
1345 const Elf_Rela *rela;
1346 const char *symname;
1352 /* Perform relocations without addend if there are any: */
1353 for (i = 0; i < ef->nreltab; i++) {
1354 rel = ef->reltab[i].rel;
1356 link_elf_error(ef->lf.filename, "lost a reltab!");
1359 rellim = rel + ef->reltab[i].nrel;
1360 base = findbase(ef, ef->reltab[i].sec);
1362 link_elf_error(ef->lf.filename, "lost base for reltab");
1365 for ( ; rel < rellim; rel++) {
1366 symidx = ELF_R_SYM(rel->r_info);
1367 if (symidx >= ef->ddbsymcnt)
1369 sym = ef->ddbsymtab + symidx;
1370 /* Local relocs are already done */
1371 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1373 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1374 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1376 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1378 symname = symbol_name(ef, rel->r_info);
1379 printf("link_elf_obj: symbol %s undefined\n",
1386 /* Perform relocations with addend if there are any: */
1387 for (i = 0; i < ef->nrelatab; i++) {
1388 rela = ef->relatab[i].rela;
1390 link_elf_error(ef->lf.filename, "lost a relatab!");
1393 relalim = rela + ef->relatab[i].nrela;
1394 base = findbase(ef, ef->relatab[i].sec);
1396 link_elf_error(ef->lf.filename,
1397 "lost base for relatab");
1400 for ( ; rela < relalim; rela++) {
1401 symidx = ELF_R_SYM(rela->r_info);
1402 if (symidx >= ef->ddbsymcnt)
1404 sym = ef->ddbsymtab + symidx;
1405 /* Local relocs are already done */
1406 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1408 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1409 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1411 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1413 symname = symbol_name(ef, rela->r_info);
1414 printf("link_elf_obj: symbol %s undefined\n",
1422 * Only clean SHN_FBSD_CACHED for successful return. If we
1423 * modified symbol table for the object but found an
1424 * unresolved symbol, there is no reason to roll back.
1426 elf_obj_cleanup_globals_cache(ef);
1432 relocate_file(elf_file_t ef)
1436 error = relocate_file1(ef, false);
1438 error = relocate_file1(ef, true);
1443 link_elf_lookup_symbol1(linker_file_t lf, const char *name, c_linker_sym_t *sym,
1446 elf_file_t ef = (elf_file_t)lf;
1447 const Elf_Sym *symp;
1451 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1452 strp = ef->ddbstrtab + symp->st_name;
1453 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1455 ELF_ST_BIND(symp->st_info) == STB_GLOBAL) {
1456 *sym = (c_linker_sym_t) symp;
1466 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1468 return (link_elf_lookup_symbol1(lf, name, sym,
1469 link_elf_obj_leak_locals));
1473 link_elf_lookup_debug_symbol(linker_file_t lf, const char *name,
1474 c_linker_sym_t *sym)
1476 return (link_elf_lookup_symbol1(lf, name, sym, true));
1480 link_elf_symbol_values1(linker_file_t lf, c_linker_sym_t sym,
1481 linker_symval_t *symval, bool see_local)
1487 ef = (elf_file_t) lf;
1488 es = (const Elf_Sym*) sym;
1489 val = (caddr_t)es->st_value;
1490 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1491 if (!see_local && ELF_ST_BIND(es->st_info) == STB_LOCAL)
1493 symval->name = ef->ddbstrtab + es->st_name;
1494 val = (caddr_t)es->st_value;
1495 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1496 val = ((caddr_t (*)(void))val)();
1497 symval->value = val;
1498 symval->size = es->st_size;
1505 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1506 linker_symval_t *symval)
1508 return (link_elf_symbol_values1(lf, sym, symval,
1509 link_elf_obj_leak_locals));
1513 link_elf_debug_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1514 linker_symval_t *symval)
1516 return (link_elf_symbol_values1(lf, sym, symval, true));
1520 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1521 c_linker_sym_t *sym, long *diffp)
1523 elf_file_t ef = (elf_file_t)lf;
1524 u_long off = (uintptr_t)(void *)value;
1528 const Elf_Sym *best = NULL;
1531 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1532 if (es->st_name == 0)
1534 st_value = es->st_value;
1535 if (off >= st_value) {
1536 if (off - st_value < diff) {
1537 diff = off - st_value;
1541 } else if (off - st_value == diff) {
1550 *sym = (c_linker_sym_t) best;
1556 * Look up a linker set on an ELF system.
1559 link_elf_lookup_set(linker_file_t lf, const char *name,
1560 void ***startp, void ***stopp, int *countp)
1562 elf_file_t ef = (elf_file_t)lf;
1563 void **start, **stop;
1566 /* Relative to section number */
1567 for (i = 0; i < ef->nprogtab; i++) {
1568 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1569 strcmp(ef->progtab[i].name + 4, name) == 0) {
1570 start = (void **)ef->progtab[i].addr;
1571 stop = (void **)((char *)ef->progtab[i].addr +
1572 ef->progtab[i].size);
1573 count = stop - start;
1587 link_elf_each_function_name(linker_file_t file,
1588 int (*callback)(const char *, void *), void *opaque)
1590 elf_file_t ef = (elf_file_t)file;
1591 const Elf_Sym *symp;
1594 /* Exhaustive search */
1595 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1596 if (symp->st_value != 0 &&
1597 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1598 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1599 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1608 link_elf_each_function_nameval(linker_file_t file,
1609 linker_function_nameval_callback_t callback, void *opaque)
1611 linker_symval_t symval;
1612 elf_file_t ef = (elf_file_t)file;
1613 const Elf_Sym *symp;
1616 /* Exhaustive search */
1617 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1618 if (symp->st_value != 0 &&
1619 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1620 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1621 error = link_elf_debug_symbol_values(file,
1622 (c_linker_sym_t)symp, &symval);
1624 error = callback(file, i, &symval, opaque);
1633 elf_obj_cleanup_globals_cache(elf_file_t ef)
1638 for (i = 0; i < ef->ddbsymcnt; i++) {
1639 sym = ef->ddbsymtab + i;
1640 if (sym->st_shndx == SHN_FBSD_CACHED) {
1641 sym->st_shndx = SHN_UNDEF;
1648 * Symbol lookup function that can be used when the symbol index is known (ie
1649 * in relocations). It uses the symbol index instead of doing a fully fledged
1650 * hash table based lookup when such is valid. For example for local symbols.
1651 * This is not only more efficient, it's also more correct. It's not always
1652 * the case that the symbol can be found through the hash table.
1655 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1657 elf_file_t ef = (elf_file_t)lf;
1662 /* Don't even try to lookup the symbol if the index is bogus. */
1663 if (symidx >= ef->ddbsymcnt) {
1668 sym = ef->ddbsymtab + symidx;
1670 /* Quick answer if there is a definition included. */
1671 if (sym->st_shndx != SHN_UNDEF) {
1672 res1 = (Elf_Addr)sym->st_value;
1673 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1674 res1 = ((Elf_Addr (*)(void))res1)();
1679 /* If we get here, then it is undefined and needs a lookup. */
1680 switch (ELF_ST_BIND(sym->st_info)) {
1682 /* Local, but undefined? huh? */
1688 /* Relative to Data or Function name */
1689 symbol = ef->ddbstrtab + sym->st_name;
1691 /* Force a lookup failure if the symbol name is bogus. */
1696 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1699 * Cache global lookups during module relocation. The failure
1700 * case is particularly expensive for callers, who must scan
1701 * through the entire globals table doing strcmp(). Cache to
1702 * avoid doing such work repeatedly.
1704 * After relocation is complete, undefined globals will be
1705 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1709 sym->st_shndx = SHN_FBSD_CACHED;
1710 sym->st_value = res1;
1713 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1726 link_elf_fix_link_set(elf_file_t ef)
1728 static const char startn[] = "__start_";
1729 static const char stopn[] = "__stop_";
1731 const char *sym_name, *linkset_name;
1732 Elf_Addr startp, stopp;
1737 for (symidx = 1 /* zero entry is special */;
1738 symidx < ef->ddbsymcnt; symidx++) {
1739 sym = ef->ddbsymtab + symidx;
1740 if (sym->st_shndx != SHN_UNDEF)
1743 sym_name = ef->ddbstrtab + sym->st_name;
1744 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1746 linkset_name = sym_name + sizeof(startn) - 1;
1748 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1750 linkset_name = sym_name + sizeof(stopn) - 1;
1755 for (i = 0; i < ef->nprogtab; i++) {
1756 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1757 startp = (Elf_Addr)ef->progtab[i].addr;
1758 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1762 if (i == ef->nprogtab)
1765 sym->st_value = start ? startp : stopp;
1771 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1773 elf_file_t ef = (elf_file_t)lf;
1774 const Elf_Rel *rellim;
1776 const Elf_Rela *relalim;
1777 const Elf_Rela *rela;
1783 link_elf_fix_link_set(ef);
1785 /* Perform relocations without addend if there are any: */
1786 for (i = 0; i < ef->nreltab; i++) {
1787 rel = ef->reltab[i].rel;
1789 link_elf_error(ef->lf.filename, "lost a reltab");
1792 rellim = rel + ef->reltab[i].nrel;
1793 base = findbase(ef, ef->reltab[i].sec);
1795 link_elf_error(ef->lf.filename, "lost base for reltab");
1798 for ( ; rel < rellim; rel++) {
1799 symidx = ELF_R_SYM(rel->r_info);
1800 if (symidx >= ef->ddbsymcnt)
1802 sym = ef->ddbsymtab + symidx;
1803 /* Only do local relocs */
1804 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1806 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1807 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1809 if (elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1810 elf_obj_lookup) != 0)
1815 /* Perform relocations with addend if there are any: */
1816 for (i = 0; i < ef->nrelatab; i++) {
1817 rela = ef->relatab[i].rela;
1819 link_elf_error(ef->lf.filename, "lost a relatab!");
1822 relalim = rela + ef->relatab[i].nrela;
1823 base = findbase(ef, ef->relatab[i].sec);
1825 link_elf_error(ef->lf.filename, "lost base for reltab");
1828 for ( ; rela < relalim; rela++) {
1829 symidx = ELF_R_SYM(rela->r_info);
1830 if (symidx >= ef->ddbsymcnt)
1832 sym = ef->ddbsymtab + symidx;
1833 /* Only do local relocs */
1834 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1836 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1837 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1839 if (elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1840 elf_obj_lookup) != 0)
1848 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1850 elf_file_t ef = (elf_file_t)lf;
1852 *symtab = ef->ddbsymtab;
1853 if (*symtab == NULL)
1855 return (ef->ddbsymcnt);
1859 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1861 elf_file_t ef = (elf_file_t)lf;
1863 *strtab = ef->ddbstrtab;
1864 if (*strtab == NULL)
1866 return (ef->ddbstrcnt);
1871 link_elf_propagate_vnets(linker_file_t lf)
1873 elf_file_t ef = (elf_file_t) lf;
1876 for (int i = 0; i < ef->nprogtab; i++) {
1877 if (ef->progtab[i].size == 0)
1879 if (ef->progtab[i].name == NULL)
1881 if (strcmp(ef->progtab[i].name, VNET_SETNAME) == 0) {
1882 vnet_data_copy(ef->progtab[i].addr,
1883 ef->progtab[i].size);