2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/fcntl.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/linker.h>
42 #include <sys/mutex.h>
43 #include <sys/mount.h>
44 #include <sys/namei.h>
46 #include <sys/rwlock.h>
47 #include <sys/vnode.h>
49 #include <machine/elf.h>
53 #include <security/mac/mac_framework.h>
56 #include <vm/vm_param.h>
58 #include <vm/vm_extern.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_page.h>
63 #include <vm/vm_pager.h>
65 #include <sys/link_elf.h>
68 #include <contrib/zlib/zlib.h>
71 #include "linker_if.h"
76 int flags; /* Section flags. */
77 int sec; /* Original section number. */
93 typedef struct elf_file {
94 struct linker_file lf; /* Common fields */
97 caddr_t address; /* Relocation address */
98 vm_object_t object; /* VM object to hold file pages */
101 Elf_progent *progtab;
104 Elf_relaent *relatab;
110 Elf_Sym *ddbsymtab; /* The symbol table we are using */
111 long ddbsymcnt; /* Number of symbols */
112 caddr_t ddbstrtab; /* String table */
113 long ddbstrcnt; /* number of bytes in string table */
115 caddr_t shstrtab; /* Section name string table */
116 long shstrcnt; /* number of bytes in string table */
118 caddr_t ctftab; /* CTF table */
119 long ctfcnt; /* number of bytes in CTF table */
120 caddr_t ctfoff; /* CTF offset table */
121 caddr_t typoff; /* Type offset table */
122 long typlen; /* Number of type entries. */
126 #include <kern/kern_ctf.c>
128 static int link_elf_link_preload(linker_class_t cls,
129 const char *, linker_file_t *);
130 static int link_elf_link_preload_finish(linker_file_t);
131 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *);
132 static int link_elf_lookup_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_search_symbol(linker_file_t, caddr_t value,
137 c_linker_sym_t *sym, long *diffp);
139 static void link_elf_unload_file(linker_file_t);
140 static int link_elf_lookup_set(linker_file_t, const char *,
141 void ***, void ***, int *);
142 static int link_elf_each_function_name(linker_file_t,
143 int (*)(const char *, void *), void *);
144 static int link_elf_each_function_nameval(linker_file_t,
145 linker_function_nameval_callback_t,
147 static int link_elf_reloc_local(linker_file_t, bool);
148 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
149 static long link_elf_strtab_get(linker_file_t, caddr_t *);
151 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps,
154 static kobj_method_t link_elf_methods[] = {
155 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
156 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
157 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
158 KOBJMETHOD(linker_unload, link_elf_unload_file),
159 KOBJMETHOD(linker_load_file, link_elf_load_file),
160 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
161 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
162 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
163 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
164 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
165 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
166 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
167 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
171 static struct linker_class link_elf_class = {
172 #if ELF_TARG_CLASS == ELFCLASS32
177 link_elf_methods, sizeof(struct elf_file)
180 static int relocate_file(elf_file_t ef);
181 static void elf_obj_cleanup_globals_cache(elf_file_t);
184 link_elf_error(const char *filename, const char *s)
186 if (filename == NULL)
187 printf("kldload: %s\n", s);
189 printf("kldload: %s: %s\n", filename, s);
193 link_elf_init(void *arg)
196 linker_add_class(&link_elf_class);
198 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, NULL);
201 link_elf_protect_range(elf_file_t ef, vm_offset_t start, vm_offset_t end,
206 KASSERT(start <= end && start >= (vm_offset_t)ef->address &&
207 end <= round_page((vm_offset_t)ef->address + ef->lf.size),
208 ("link_elf_protect_range: invalid range %#jx-%#jx",
209 (uintmax_t)start, (uintmax_t)end));
215 error = pmap_change_prot(start, end - start, prot);
217 ("link_elf_protect_range: pmap_change_prot() returned %d",
222 error = vm_map_protect(kernel_map, start, end, prot, 0,
223 VM_MAP_PROTECT_SET_PROT);
224 KASSERT(error == KERN_SUCCESS,
225 ("link_elf_protect_range: vm_map_protect() returned %d", error));
229 * Restrict permissions on linker file memory based on section flags.
230 * Sections need not be page-aligned, so overlap within a page is possible.
233 link_elf_protect(elf_file_t ef)
235 vm_offset_t end, segend, segstart, start;
236 vm_prot_t gapprot, prot, segprot;
240 * If the file was preloaded, the last page may contain other preloaded
241 * data which may need to be writeable. ELF files are always
242 * page-aligned, but other preloaded data, such as entropy or CPU
243 * microcode may be loaded with a smaller alignment.
245 gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ;
247 start = end = (vm_offset_t)ef->address;
249 for (i = 0; i < ef->nprogtab; i++) {
251 * VNET and DPCPU sections have their memory allocated by their
252 * respective subsystems.
254 if (ef->progtab[i].name != NULL && (
256 strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 ||
258 strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0))
261 segstart = trunc_page((vm_offset_t)ef->progtab[i].addr);
262 segend = round_page((vm_offset_t)ef->progtab[i].addr +
263 ef->progtab[i].size);
264 segprot = VM_PROT_READ;
265 if ((ef->progtab[i].flags & SHF_WRITE) != 0)
266 segprot |= VM_PROT_WRITE;
267 if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0)
268 segprot |= VM_PROT_EXECUTE;
270 if (end <= segstart) {
272 * Case 1: there is no overlap between the previous
273 * segment and this one. Apply protections to the
274 * previous segment, and protect the gap between the
275 * previous and current segments, if any.
277 link_elf_protect_range(ef, start, end, prot);
278 link_elf_protect_range(ef, end, segstart, gapprot);
283 } else if (start < segstart && end == segend) {
285 * Case 2: the current segment is a subrange of the
286 * previous segment. Apply protections to the
287 * non-overlapping portion of the previous segment.
289 link_elf_protect_range(ef, start, segstart, prot);
293 } else if (end < segend) {
295 * Case 3: there is partial overlap between the previous
296 * and current segments. Apply protections to the
297 * non-overlapping portion of the previous segment, and
298 * then the overlap, which must use the union of the two
299 * segments' protections.
301 link_elf_protect_range(ef, start, segstart, prot);
302 link_elf_protect_range(ef, segstart, end,
309 * Case 4: the two segments reside in the same page.
316 * Fix up the last unprotected segment and trailing data.
318 link_elf_protect_range(ef, start, end, prot);
319 link_elf_protect_range(ef, end,
320 round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
324 link_elf_link_preload(linker_class_t cls, const char *filename,
325 linker_file_t *result)
330 void *modptr, *baseptr, *sizeptr;
335 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
337 /* Look to see if we have the file preloaded */
338 modptr = preload_search_by_name(filename);
342 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
343 baseptr = preload_search_info(modptr, MODINFO_ADDR);
344 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
345 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
347 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
349 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
350 " obj module") != 0 &&
351 strcmp(type, "elf obj module") != 0)) {
354 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
358 lf = linker_make_file(filename, &link_elf_class);
364 ef->address = *(caddr_t *)baseptr;
365 lf->address = *(caddr_t *)baseptr;
366 lf->size = *(size_t *)sizeptr;
368 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
369 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
370 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
371 hdr->e_version != EV_CURRENT ||
372 hdr->e_type != ET_REL ||
373 hdr->e_machine != ELF_TARG_MACH) {
379 /* Scan the section header for information and table sizing. */
382 for (i = 0; i < hdr->e_shnum; i++) {
383 switch (shdr[i].sh_type) {
387 case SHT_X86_64_UNWIND:
391 /* Ignore sections not loaded by the loader. */
392 if (shdr[i].sh_addr == 0)
398 symstrindex = shdr[i].sh_link;
402 * Ignore relocation tables for sections not
403 * loaded by the loader.
405 if (shdr[shdr[i].sh_info].sh_addr == 0)
410 if (shdr[shdr[i].sh_info].sh_addr == 0)
417 shstrindex = hdr->e_shstrndx;
418 if (ef->nprogtab == 0 || symstrindex < 0 ||
419 symstrindex >= hdr->e_shnum ||
420 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
421 shstrindex >= hdr->e_shnum ||
422 shdr[shstrindex].sh_type != SHT_STRTAB) {
423 printf("%s: bad/missing section headers\n", filename);
428 /* Allocate space for tracking the load chunks */
429 if (ef->nprogtab != 0)
430 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
431 M_LINKER, M_WAITOK | M_ZERO);
432 if (ef->nreltab != 0)
433 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
434 M_LINKER, M_WAITOK | M_ZERO);
435 if (ef->nrelatab != 0)
436 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
437 M_LINKER, M_WAITOK | M_ZERO);
438 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
439 (ef->nreltab != 0 && ef->reltab == NULL) ||
440 (ef->nrelatab != 0 && ef->relatab == NULL)) {
445 /* XXX, relocate the sh_addr fields saved by the loader. */
447 for (i = 0; i < hdr->e_shnum; i++) {
448 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
449 off = shdr[i].sh_addr;
451 for (i = 0; i < hdr->e_shnum; i++) {
452 if (shdr[i].sh_addr != 0)
453 shdr[i].sh_addr = shdr[i].sh_addr - off +
454 (Elf_Addr)ef->address;
457 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
458 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
459 ef->ddbstrcnt = shdr[symstrindex].sh_size;
460 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
461 ef->shstrcnt = shdr[shstrindex].sh_size;
462 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
464 /* Now fill out progtab and the relocation tables. */
468 for (i = 0; i < hdr->e_shnum; i++) {
469 switch (shdr[i].sh_type) {
473 case SHT_X86_64_UNWIND:
477 if (shdr[i].sh_addr == 0)
479 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
480 if (shdr[i].sh_type == SHT_PROGBITS)
481 ef->progtab[pb].name = "<<PROGBITS>>";
483 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
484 ef->progtab[pb].name = "<<UNWIND>>";
486 else if (shdr[i].sh_type == SHT_INIT_ARRAY)
487 ef->progtab[pb].name = "<<INIT_ARRAY>>";
488 else if (shdr[i].sh_type == SHT_FINI_ARRAY)
489 ef->progtab[pb].name = "<<FINI_ARRAY>>";
491 ef->progtab[pb].name = "<<NOBITS>>";
492 ef->progtab[pb].size = shdr[i].sh_size;
493 ef->progtab[pb].flags = shdr[i].sh_flags;
494 ef->progtab[pb].sec = i;
495 if (ef->shstrtab && shdr[i].sh_name != 0)
496 ef->progtab[pb].name =
497 ef->shstrtab + shdr[i].sh_name;
498 if (ef->progtab[pb].name != NULL &&
499 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
502 dpcpu = dpcpu_alloc(shdr[i].sh_size);
504 printf("%s: pcpu module space is out "
505 "of space; cannot allocate %#jx "
506 "for %s\n", __func__,
507 (uintmax_t)shdr[i].sh_size,
512 memcpy(dpcpu, ef->progtab[pb].addr,
513 ef->progtab[pb].size);
514 dpcpu_copy(dpcpu, shdr[i].sh_size);
515 ef->progtab[pb].addr = dpcpu;
517 } else if (ef->progtab[pb].name != NULL &&
518 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
521 vnet_data = vnet_data_alloc(shdr[i].sh_size);
522 if (vnet_data == NULL) {
523 printf("%s: vnet module space is out "
524 "of space; cannot allocate %#jx "
525 "for %s\n", __func__,
526 (uintmax_t)shdr[i].sh_size,
531 memcpy(vnet_data, ef->progtab[pb].addr,
532 ef->progtab[pb].size);
533 vnet_data_copy(vnet_data, shdr[i].sh_size);
534 ef->progtab[pb].addr = vnet_data;
536 } else if ((ef->progtab[pb].name != NULL &&
537 strcmp(ef->progtab[pb].name, ".ctors") == 0) ||
538 shdr[i].sh_type == SHT_INIT_ARRAY) {
539 if (lf->ctors_addr != 0) {
541 "%s: multiple ctor sections in %s\n",
544 lf->ctors_addr = ef->progtab[pb].addr;
545 lf->ctors_size = shdr[i].sh_size;
547 } else if ((ef->progtab[pb].name != NULL &&
548 strcmp(ef->progtab[pb].name, ".dtors") == 0) ||
549 shdr[i].sh_type == SHT_FINI_ARRAY) {
550 if (lf->dtors_addr != 0) {
552 "%s: multiple dtor sections in %s\n",
555 lf->dtors_addr = ef->progtab[pb].addr;
556 lf->dtors_size = shdr[i].sh_size;
560 /* Update all symbol values with the offset. */
561 for (j = 0; j < ef->ddbsymcnt; j++) {
562 es = &ef->ddbsymtab[j];
563 if (es->st_shndx != i)
565 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
570 if (shdr[shdr[i].sh_info].sh_addr == 0)
572 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
573 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
574 ef->reltab[rl].sec = shdr[i].sh_info;
578 if (shdr[shdr[i].sh_info].sh_addr == 0)
580 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
581 ef->relatab[ra].nrela =
582 shdr[i].sh_size / sizeof(Elf_Rela);
583 ef->relatab[ra].sec = shdr[i].sh_info;
588 if (pb != ef->nprogtab) {
589 printf("%s: lost progbits\n", filename);
593 if (rl != ef->nreltab) {
594 printf("%s: lost reltab\n", filename);
598 if (ra != ef->nrelatab) {
599 printf("%s: lost relatab\n", filename);
605 * The file needs to be writeable and executable while applying
606 * relocations. Mapping protections are applied once relocation
607 * processing is complete.
609 link_elf_protect_range(ef, (vm_offset_t)ef->address,
610 round_page((vm_offset_t)ef->address + ef->lf.size), VM_PROT_ALL);
612 /* Local intra-module relocations */
613 error = link_elf_reloc_local(lf, false);
620 /* preload not done this way */
621 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
626 link_elf_invoke_cbs(caddr_t addr, size_t size)
631 if (addr == NULL || size == 0)
633 cnt = size / sizeof(*ctor);
635 for (i = 0; i < cnt; i++) {
642 link_elf_link_preload_finish(linker_file_t lf)
648 error = relocate_file(ef);
652 /* Notify MD code that a module is being loaded. */
653 error = elf_cpu_load_file(lf);
657 #if defined(__i386__) || defined(__amd64__)
659 error = link_elf_reloc_local(lf, true);
664 /* Apply protections now that relocation processing is complete. */
665 link_elf_protect(ef);
667 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
672 link_elf_load_file(linker_class_t cls, const char *filename,
673 linker_file_t *result)
675 struct nameidata *nd;
676 struct thread *td = curthread; /* XXX */
700 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
701 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
703 error = vn_open(nd, &flags, 0, NULL);
708 NDFREE(nd, NDF_ONLY_PNBUF);
709 if (nd->ni_vp->v_type != VREG) {
714 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
720 /* Read the elf header from the file. */
721 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
722 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
723 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
737 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
738 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
739 link_elf_error(filename, "Unsupported file layout");
743 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
744 || hdr->e_version != EV_CURRENT) {
745 link_elf_error(filename, "Unsupported file version");
749 if (hdr->e_type != ET_REL) {
753 if (hdr->e_machine != ELF_TARG_MACH) {
754 link_elf_error(filename, "Unsupported machine");
759 lf = linker_make_file(filename, &link_elf_class);
764 ef = (elf_file_t) lf;
770 /* Allocate and read in the section header */
771 nbytes = hdr->e_shnum * hdr->e_shentsize;
772 if (nbytes == 0 || hdr->e_shoff == 0 ||
773 hdr->e_shentsize != sizeof(Elf_Shdr)) {
777 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
779 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
780 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
789 /* Scan the section header for information and table sizing. */
793 for (i = 0; i < hdr->e_shnum; i++) {
794 if (shdr[i].sh_size == 0)
796 switch (shdr[i].sh_type) {
800 case SHT_X86_64_UNWIND:
804 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
811 symstrindex = shdr[i].sh_link;
815 * Ignore relocation tables for unallocated
818 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
823 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
831 if (ef->nprogtab == 0) {
832 link_elf_error(filename, "file has no contents");
837 /* Only allow one symbol table for now */
838 link_elf_error(filename,
839 "file must have exactly one symbol table");
843 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
844 shdr[symstrindex].sh_type != SHT_STRTAB) {
845 link_elf_error(filename, "file has invalid symbol strings");
850 /* Allocate space for tracking the load chunks */
851 if (ef->nprogtab != 0)
852 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
853 M_LINKER, M_WAITOK | M_ZERO);
854 if (ef->nreltab != 0)
855 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
856 M_LINKER, M_WAITOK | M_ZERO);
857 if (ef->nrelatab != 0)
858 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
859 M_LINKER, M_WAITOK | M_ZERO);
861 if (symtabindex == -1) {
862 link_elf_error(filename, "lost symbol table index");
866 /* Allocate space for and load the symbol table */
867 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
868 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
869 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
870 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
871 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
880 /* Allocate space for and load the symbol strings */
881 ef->ddbstrcnt = shdr[symstrindex].sh_size;
882 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
883 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
884 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
885 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
894 /* Do we have a string table for the section names? */
896 if (hdr->e_shstrndx != 0 &&
897 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
898 shstrindex = hdr->e_shstrndx;
899 ef->shstrcnt = shdr[shstrindex].sh_size;
900 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
902 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
903 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
904 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
914 /* Size up code/data(progbits) and bss(nobits). */
916 for (i = 0; i < hdr->e_shnum; i++) {
917 if (shdr[i].sh_size == 0)
919 switch (shdr[i].sh_type) {
923 case SHT_X86_64_UNWIND:
927 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
929 alignmask = shdr[i].sh_addralign - 1;
930 mapsize += alignmask;
931 mapsize &= ~alignmask;
932 mapsize += shdr[i].sh_size;
938 * We know how much space we need for the text/data/bss/etc.
939 * This stuff needs to be in a single chunk so that profiling etc
940 * can get the bounds and gdb can associate offsets with modules
942 ef->object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(mapsize),
943 VM_PROT_ALL, 0, thread0.td_ucred);
944 if (ef->object == NULL) {
948 #if VM_NRESERVLEVEL > 0
949 vm_object_color(ef->object, 0);
953 * In order to satisfy amd64's architectural requirements on the
954 * location of code and data in the kernel's address space, request a
955 * mapping that is above the kernel.
957 * Protections will be restricted once relocations are applied.
962 mapbase = VM_MIN_KERNEL_ADDRESS;
964 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
965 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
967 if (error != KERN_SUCCESS) {
968 vm_object_deallocate(ef->object);
975 error = vm_map_wire(kernel_map, mapbase,
976 mapbase + round_page(mapsize),
977 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
978 if (error != KERN_SUCCESS) {
983 /* Inform the kld system about the situation */
984 lf->address = ef->address = (caddr_t)mapbase;
988 * Now load code/data(progbits), zero bss(nobits), allocate space for
995 for (i = 0; i < hdr->e_shnum; i++) {
996 if (shdr[i].sh_size == 0)
998 switch (shdr[i].sh_type) {
1002 case SHT_X86_64_UNWIND:
1004 case SHT_INIT_ARRAY:
1005 case SHT_FINI_ARRAY:
1006 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
1008 alignmask = shdr[i].sh_addralign - 1;
1009 mapbase += alignmask;
1010 mapbase &= ~alignmask;
1011 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
1012 ef->progtab[pb].name =
1013 ef->shstrtab + shdr[i].sh_name;
1014 if (!strcmp(ef->progtab[pb].name, ".ctors") ||
1015 shdr[i].sh_type == SHT_INIT_ARRAY) {
1016 if (lf->ctors_addr != 0) {
1018 "%s: multiple ctor sections in %s\n",
1019 __func__, filename);
1026 } else if (!strcmp(ef->progtab[pb].name,
1028 shdr[i].sh_type == SHT_FINI_ARRAY) {
1029 if (lf->dtors_addr != 0) {
1031 "%s: multiple dtor sections in %s\n",
1032 __func__, filename);
1040 } else if (shdr[i].sh_type == SHT_PROGBITS)
1041 ef->progtab[pb].name = "<<PROGBITS>>";
1043 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
1044 ef->progtab[pb].name = "<<UNWIND>>";
1047 ef->progtab[pb].name = "<<NOBITS>>";
1048 if (ef->progtab[pb].name != NULL &&
1049 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
1050 ef->progtab[pb].addr =
1051 dpcpu_alloc(shdr[i].sh_size);
1052 if (ef->progtab[pb].addr == NULL) {
1053 printf("%s: pcpu module space is out "
1054 "of space; cannot allocate %#jx "
1055 "for %s\n", __func__,
1056 (uintmax_t)shdr[i].sh_size,
1061 else if (ef->progtab[pb].name != NULL &&
1062 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
1063 ef->progtab[pb].addr =
1064 vnet_data_alloc(shdr[i].sh_size);
1065 if (ef->progtab[pb].addr == NULL) {
1066 printf("%s: vnet module space is out "
1067 "of space; cannot allocate %#jx "
1068 "for %s\n", __func__,
1069 (uintmax_t)shdr[i].sh_size,
1075 ef->progtab[pb].addr =
1076 (void *)(uintptr_t)mapbase;
1077 if (ef->progtab[pb].addr == NULL) {
1081 ef->progtab[pb].size = shdr[i].sh_size;
1082 ef->progtab[pb].flags = shdr[i].sh_flags;
1083 ef->progtab[pb].sec = i;
1084 if (shdr[i].sh_type == SHT_PROGBITS
1086 || shdr[i].sh_type == SHT_X86_64_UNWIND
1089 error = vn_rdwr(UIO_READ, nd->ni_vp,
1090 ef->progtab[pb].addr,
1091 shdr[i].sh_size, shdr[i].sh_offset,
1092 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1093 NOCRED, &resid, td);
1100 /* Initialize the per-cpu or vnet area. */
1101 if (ef->progtab[pb].addr != (void *)mapbase &&
1102 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1103 dpcpu_copy(ef->progtab[pb].addr,
1106 else if (ef->progtab[pb].addr !=
1108 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
1109 vnet_data_copy(ef->progtab[pb].addr,
1113 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1115 /* Update all symbol values with the offset. */
1116 for (j = 0; j < ef->ddbsymcnt; j++) {
1117 es = &ef->ddbsymtab[j];
1118 if (es->st_shndx != i)
1120 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1122 mapbase += shdr[i].sh_size;
1126 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1128 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1130 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1131 ef->reltab[rl].sec = shdr[i].sh_info;
1132 error = vn_rdwr(UIO_READ, nd->ni_vp,
1133 (void *)ef->reltab[rl].rel,
1134 shdr[i].sh_size, shdr[i].sh_offset,
1135 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1146 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1148 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1150 ef->relatab[ra].nrela =
1151 shdr[i].sh_size / sizeof(Elf_Rela);
1152 ef->relatab[ra].sec = shdr[i].sh_info;
1153 error = vn_rdwr(UIO_READ, nd->ni_vp,
1154 (void *)ef->relatab[ra].rela,
1155 shdr[i].sh_size, shdr[i].sh_offset,
1156 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1168 if (pb != ef->nprogtab) {
1169 link_elf_error(filename, "lost progbits");
1173 if (rl != ef->nreltab) {
1174 link_elf_error(filename, "lost reltab");
1178 if (ra != ef->nrelatab) {
1179 link_elf_error(filename, "lost relatab");
1183 if (mapbase != (vm_offset_t)ef->address + mapsize) {
1185 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1186 filename != NULL ? filename : "<none>",
1187 (u_long)mapbase, ef->address, (u_long)mapsize,
1188 (u_long)(vm_offset_t)ef->address + mapsize);
1193 /* Local intra-module relocations */
1194 error = link_elf_reloc_local(lf, false);
1198 /* Pull in dependencies */
1199 VOP_UNLOCK(nd->ni_vp);
1200 error = linker_load_dependencies(lf);
1201 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1205 /* External relocations */
1206 error = relocate_file(ef);
1210 /* Notify MD code that a module is being loaded. */
1211 error = elf_cpu_load_file(lf);
1215 #if defined(__i386__) || defined(__amd64__)
1217 error = link_elf_reloc_local(lf, true);
1222 link_elf_protect(ef);
1223 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
1227 VOP_UNLOCK(nd->ni_vp);
1228 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1231 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1232 free(hdr, M_LINKER);
1238 link_elf_unload_file(linker_file_t file)
1240 elf_file_t ef = (elf_file_t) file;
1243 link_elf_invoke_cbs(file->dtors_addr, file->dtors_size);
1245 /* Notify MD code that a module is being unloaded. */
1246 elf_cpu_unload_file(file);
1249 for (i = 0; i < ef->nprogtab; i++) {
1250 if (ef->progtab[i].size == 0)
1252 if (ef->progtab[i].name == NULL)
1254 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1255 dpcpu_free(ef->progtab[i].addr,
1256 ef->progtab[i].size);
1258 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1259 vnet_data_free(ef->progtab[i].addr,
1260 ef->progtab[i].size);
1264 if (ef->preloaded) {
1265 free(ef->reltab, M_LINKER);
1266 free(ef->relatab, M_LINKER);
1267 free(ef->progtab, M_LINKER);
1268 free(ef->ctftab, M_LINKER);
1269 free(ef->ctfoff, M_LINKER);
1270 free(ef->typoff, M_LINKER);
1271 if (file->pathname != NULL)
1272 preload_delete_name(file->pathname);
1276 for (i = 0; i < ef->nreltab; i++)
1277 free(ef->reltab[i].rel, M_LINKER);
1278 for (i = 0; i < ef->nrelatab; i++)
1279 free(ef->relatab[i].rela, M_LINKER);
1280 free(ef->reltab, M_LINKER);
1281 free(ef->relatab, M_LINKER);
1282 free(ef->progtab, M_LINKER);
1284 if (ef->object != NULL)
1285 vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1286 (vm_offset_t)ef->address + ptoa(ef->object->size));
1287 free(ef->e_shdr, M_LINKER);
1288 free(ef->ddbsymtab, M_LINKER);
1289 free(ef->ddbstrtab, M_LINKER);
1290 free(ef->shstrtab, M_LINKER);
1291 free(ef->ctftab, M_LINKER);
1292 free(ef->ctfoff, M_LINKER);
1293 free(ef->typoff, M_LINKER);
1297 symbol_name(elf_file_t ef, Elf_Size r_info)
1301 if (ELF_R_SYM(r_info)) {
1302 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1303 return ef->ddbstrtab + ref->st_name;
1309 findbase(elf_file_t ef, int sec)
1314 for (i = 0; i < ef->nprogtab; i++) {
1315 if (sec == ef->progtab[i].sec) {
1316 base = (Elf_Addr)ef->progtab[i].addr;
1324 relocate_file1(elf_file_t ef, bool ifuncs)
1326 const Elf_Rel *rellim;
1328 const Elf_Rela *relalim;
1329 const Elf_Rela *rela;
1330 const char *symname;
1336 /* Perform relocations without addend if there are any: */
1337 for (i = 0; i < ef->nreltab; i++) {
1338 rel = ef->reltab[i].rel;
1340 link_elf_error(ef->lf.filename, "lost a reltab!");
1343 rellim = rel + ef->reltab[i].nrel;
1344 base = findbase(ef, ef->reltab[i].sec);
1346 link_elf_error(ef->lf.filename, "lost base for reltab");
1349 for ( ; rel < rellim; rel++) {
1350 symidx = ELF_R_SYM(rel->r_info);
1351 if (symidx >= ef->ddbsymcnt)
1353 sym = ef->ddbsymtab + symidx;
1354 /* Local relocs are already done */
1355 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1357 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1358 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1360 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1362 symname = symbol_name(ef, rel->r_info);
1363 printf("link_elf_obj: symbol %s undefined\n",
1370 /* Perform relocations with addend if there are any: */
1371 for (i = 0; i < ef->nrelatab; i++) {
1372 rela = ef->relatab[i].rela;
1374 link_elf_error(ef->lf.filename, "lost a relatab!");
1377 relalim = rela + ef->relatab[i].nrela;
1378 base = findbase(ef, ef->relatab[i].sec);
1380 link_elf_error(ef->lf.filename,
1381 "lost base for relatab");
1384 for ( ; rela < relalim; rela++) {
1385 symidx = ELF_R_SYM(rela->r_info);
1386 if (symidx >= ef->ddbsymcnt)
1388 sym = ef->ddbsymtab + symidx;
1389 /* Local relocs are already done */
1390 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1392 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1393 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1395 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1397 symname = symbol_name(ef, rela->r_info);
1398 printf("link_elf_obj: symbol %s undefined\n",
1406 * Only clean SHN_FBSD_CACHED for successful return. If we
1407 * modified symbol table for the object but found an
1408 * unresolved symbol, there is no reason to roll back.
1410 elf_obj_cleanup_globals_cache(ef);
1416 relocate_file(elf_file_t ef)
1420 error = relocate_file1(ef, false);
1422 error = relocate_file1(ef, true);
1427 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1429 elf_file_t ef = (elf_file_t) lf;
1430 const Elf_Sym *symp;
1434 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1435 strp = ef->ddbstrtab + symp->st_name;
1436 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1437 *sym = (c_linker_sym_t) symp;
1445 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1446 linker_symval_t *symval)
1452 ef = (elf_file_t) lf;
1453 es = (const Elf_Sym*) sym;
1454 val = (caddr_t)es->st_value;
1455 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1456 symval->name = ef->ddbstrtab + es->st_name;
1457 val = (caddr_t)es->st_value;
1458 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1459 val = ((caddr_t (*)(void))val)();
1460 symval->value = val;
1461 symval->size = es->st_size;
1468 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1469 c_linker_sym_t *sym, long *diffp)
1471 elf_file_t ef = (elf_file_t)lf;
1472 u_long off = (uintptr_t)(void *)value;
1476 const Elf_Sym *best = NULL;
1479 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1480 if (es->st_name == 0)
1482 st_value = es->st_value;
1483 if (off >= st_value) {
1484 if (off - st_value < diff) {
1485 diff = off - st_value;
1489 } else if (off - st_value == diff) {
1498 *sym = (c_linker_sym_t) best;
1504 * Look up a linker set on an ELF system.
1507 link_elf_lookup_set(linker_file_t lf, const char *name,
1508 void ***startp, void ***stopp, int *countp)
1510 elf_file_t ef = (elf_file_t)lf;
1511 void **start, **stop;
1514 /* Relative to section number */
1515 for (i = 0; i < ef->nprogtab; i++) {
1516 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1517 strcmp(ef->progtab[i].name + 4, name) == 0) {
1518 start = (void **)ef->progtab[i].addr;
1519 stop = (void **)((char *)ef->progtab[i].addr +
1520 ef->progtab[i].size);
1521 count = stop - start;
1535 link_elf_each_function_name(linker_file_t file,
1536 int (*callback)(const char *, void *), void *opaque)
1538 elf_file_t ef = (elf_file_t)file;
1539 const Elf_Sym *symp;
1542 /* Exhaustive search */
1543 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1544 if (symp->st_value != 0 &&
1545 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1546 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1547 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1556 link_elf_each_function_nameval(linker_file_t file,
1557 linker_function_nameval_callback_t callback, void *opaque)
1559 linker_symval_t symval;
1560 elf_file_t ef = (elf_file_t)file;
1561 const Elf_Sym* symp;
1564 /* Exhaustive search */
1565 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1566 if (symp->st_value != 0 &&
1567 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1568 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1569 error = link_elf_symbol_values(file,
1570 (c_linker_sym_t)symp, &symval);
1573 error = callback(file, i, &symval, opaque);
1582 elf_obj_cleanup_globals_cache(elf_file_t ef)
1587 for (i = 0; i < ef->ddbsymcnt; i++) {
1588 sym = ef->ddbsymtab + i;
1589 if (sym->st_shndx == SHN_FBSD_CACHED) {
1590 sym->st_shndx = SHN_UNDEF;
1597 * Symbol lookup function that can be used when the symbol index is known (ie
1598 * in relocations). It uses the symbol index instead of doing a fully fledged
1599 * hash table based lookup when such is valid. For example for local symbols.
1600 * This is not only more efficient, it's also more correct. It's not always
1601 * the case that the symbol can be found through the hash table.
1604 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1606 elf_file_t ef = (elf_file_t)lf;
1611 /* Don't even try to lookup the symbol if the index is bogus. */
1612 if (symidx >= ef->ddbsymcnt) {
1617 sym = ef->ddbsymtab + symidx;
1619 /* Quick answer if there is a definition included. */
1620 if (sym->st_shndx != SHN_UNDEF) {
1621 res1 = (Elf_Addr)sym->st_value;
1622 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1623 res1 = ((Elf_Addr (*)(void))res1)();
1628 /* If we get here, then it is undefined and needs a lookup. */
1629 switch (ELF_ST_BIND(sym->st_info)) {
1631 /* Local, but undefined? huh? */
1637 /* Relative to Data or Function name */
1638 symbol = ef->ddbstrtab + sym->st_name;
1640 /* Force a lookup failure if the symbol name is bogus. */
1645 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1648 * Cache global lookups during module relocation. The failure
1649 * case is particularly expensive for callers, who must scan
1650 * through the entire globals table doing strcmp(). Cache to
1651 * avoid doing such work repeatedly.
1653 * After relocation is complete, undefined globals will be
1654 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1658 sym->st_shndx = SHN_FBSD_CACHED;
1659 sym->st_value = res1;
1662 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1675 link_elf_fix_link_set(elf_file_t ef)
1677 static const char startn[] = "__start_";
1678 static const char stopn[] = "__stop_";
1680 const char *sym_name, *linkset_name;
1681 Elf_Addr startp, stopp;
1686 for (symidx = 1 /* zero entry is special */;
1687 symidx < ef->ddbsymcnt; symidx++) {
1688 sym = ef->ddbsymtab + symidx;
1689 if (sym->st_shndx != SHN_UNDEF)
1692 sym_name = ef->ddbstrtab + sym->st_name;
1693 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1695 linkset_name = sym_name + sizeof(startn) - 1;
1697 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1699 linkset_name = sym_name + sizeof(stopn) - 1;
1704 for (i = 0; i < ef->nprogtab; i++) {
1705 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1706 startp = (Elf_Addr)ef->progtab[i].addr;
1707 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1711 if (i == ef->nprogtab)
1714 sym->st_value = start ? startp : stopp;
1720 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1722 elf_file_t ef = (elf_file_t)lf;
1723 const Elf_Rel *rellim;
1725 const Elf_Rela *relalim;
1726 const Elf_Rela *rela;
1732 link_elf_fix_link_set(ef);
1734 /* Perform relocations without addend if there are any: */
1735 for (i = 0; i < ef->nreltab; i++) {
1736 rel = ef->reltab[i].rel;
1738 link_elf_error(ef->lf.filename, "lost a reltab");
1741 rellim = rel + ef->reltab[i].nrel;
1742 base = findbase(ef, ef->reltab[i].sec);
1744 link_elf_error(ef->lf.filename, "lost base for reltab");
1747 for ( ; rel < rellim; rel++) {
1748 symidx = ELF_R_SYM(rel->r_info);
1749 if (symidx >= ef->ddbsymcnt)
1751 sym = ef->ddbsymtab + symidx;
1752 /* Only do local relocs */
1753 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1755 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1756 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1758 if (elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1759 elf_obj_lookup) != 0)
1764 /* Perform relocations with addend if there are any: */
1765 for (i = 0; i < ef->nrelatab; i++) {
1766 rela = ef->relatab[i].rela;
1768 link_elf_error(ef->lf.filename, "lost a relatab!");
1771 relalim = rela + ef->relatab[i].nrela;
1772 base = findbase(ef, ef->relatab[i].sec);
1774 link_elf_error(ef->lf.filename, "lost base for reltab");
1777 for ( ; rela < relalim; rela++) {
1778 symidx = ELF_R_SYM(rela->r_info);
1779 if (symidx >= ef->ddbsymcnt)
1781 sym = ef->ddbsymtab + symidx;
1782 /* Only do local relocs */
1783 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1785 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1786 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1788 if (elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1789 elf_obj_lookup) != 0)
1797 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1799 elf_file_t ef = (elf_file_t)lf;
1801 *symtab = ef->ddbsymtab;
1802 if (*symtab == NULL)
1804 return (ef->ddbsymcnt);
1808 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1810 elf_file_t ef = (elf_file_t)lf;
1812 *strtab = ef->ddbstrtab;
1813 if (*strtab == NULL)
1815 return (ef->ddbstrcnt);