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/sysctl.h>
48 #include <sys/vnode.h>
50 #include <machine/elf.h>
54 #include <security/mac/mac_framework.h>
57 #include <vm/vm_param.h>
59 #include <vm/vm_extern.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_pager.h>
66 #include <sys/link_elf.h>
69 #include <contrib/zlib/zlib.h>
72 #include "linker_if.h"
77 int flags; /* Section flags. */
78 int sec; /* Original section number. */
94 typedef struct elf_file {
95 struct linker_file lf; /* Common fields */
98 caddr_t address; /* Relocation address */
99 vm_object_t object; /* VM object to hold file pages */
102 Elf_progent *progtab;
105 Elf_relaent *relatab;
111 Elf_Sym *ddbsymtab; /* The symbol table we are using */
112 long ddbsymcnt; /* Number of symbols */
113 caddr_t ddbstrtab; /* String table */
114 long ddbstrcnt; /* number of bytes in string table */
116 caddr_t shstrtab; /* Section name string table */
117 long shstrcnt; /* number of bytes in string table */
119 caddr_t ctftab; /* CTF table */
120 long ctfcnt; /* number of bytes in CTF table */
121 caddr_t ctfoff; /* CTF offset table */
122 caddr_t typoff; /* Type offset table */
123 long typlen; /* Number of type entries. */
127 #include <kern/kern_ctf.c>
129 static int link_elf_link_preload(linker_class_t cls,
130 const char *, linker_file_t *);
131 static int link_elf_link_preload_finish(linker_file_t);
132 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *);
133 static int link_elf_lookup_symbol(linker_file_t, const char *,
135 static int link_elf_lookup_debug_symbol(linker_file_t, const char *,
137 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t,
139 static int link_elf_debug_symbol_values(linker_file_t, c_linker_sym_t,
141 static int link_elf_search_symbol(linker_file_t, caddr_t value,
142 c_linker_sym_t *sym, long *diffp);
144 static void link_elf_unload_file(linker_file_t);
145 static int link_elf_lookup_set(linker_file_t, const char *,
146 void ***, void ***, int *);
147 static int link_elf_each_function_name(linker_file_t,
148 int (*)(const char *, void *), void *);
149 static int link_elf_each_function_nameval(linker_file_t,
150 linker_function_nameval_callback_t,
152 static int link_elf_reloc_local(linker_file_t, bool);
153 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
154 static long link_elf_strtab_get(linker_file_t, caddr_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),
178 static struct linker_class link_elf_class = {
179 #if ELF_TARG_CLASS == ELFCLASS32
184 link_elf_methods, sizeof(struct elf_file)
187 static bool link_elf_obj_leak_locals = true;
188 SYSCTL_BOOL(_debug, OID_AUTO, link_elf_obj_leak_locals,
189 CTLFLAG_RWTUN, &link_elf_obj_leak_locals, 0,
190 "Allow local symbols to participate in global module symbol resolution");
192 static int relocate_file(elf_file_t ef);
193 static void elf_obj_cleanup_globals_cache(elf_file_t);
196 link_elf_error(const char *filename, const char *s)
198 if (filename == NULL)
199 printf("kldload: %s\n", s);
201 printf("kldload: %s: %s\n", filename, s);
205 link_elf_init(void *arg)
208 linker_add_class(&link_elf_class);
210 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, NULL);
213 link_elf_protect_range(elf_file_t ef, vm_offset_t start, vm_offset_t end,
218 KASSERT(start <= end && start >= (vm_offset_t)ef->address &&
219 end <= round_page((vm_offset_t)ef->address + ef->lf.size),
220 ("link_elf_protect_range: invalid range %#jx-%#jx",
221 (uintmax_t)start, (uintmax_t)end));
227 error = pmap_change_prot(start, end - start, prot);
229 ("link_elf_protect_range: pmap_change_prot() returned %d",
234 error = vm_map_protect(kernel_map, start, end, prot, 0,
235 VM_MAP_PROTECT_SET_PROT);
236 KASSERT(error == KERN_SUCCESS,
237 ("link_elf_protect_range: vm_map_protect() returned %d", error));
241 * Restrict permissions on linker file memory based on section flags.
242 * Sections need not be page-aligned, so overlap within a page is possible.
245 link_elf_protect(elf_file_t ef)
247 vm_offset_t end, segend, segstart, start;
248 vm_prot_t gapprot, prot, segprot;
252 * If the file was preloaded, the last page may contain other preloaded
253 * data which may need to be writeable. ELF files are always
254 * page-aligned, but other preloaded data, such as entropy or CPU
255 * microcode may be loaded with a smaller alignment.
257 gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ;
259 start = end = (vm_offset_t)ef->address;
261 for (i = 0; i < ef->nprogtab; i++) {
263 * VNET and DPCPU sections have their memory allocated by their
264 * respective subsystems.
266 if (ef->progtab[i].name != NULL && (
268 strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 ||
270 strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0))
273 segstart = trunc_page((vm_offset_t)ef->progtab[i].addr);
274 segend = round_page((vm_offset_t)ef->progtab[i].addr +
275 ef->progtab[i].size);
276 segprot = VM_PROT_READ;
277 if ((ef->progtab[i].flags & SHF_WRITE) != 0)
278 segprot |= VM_PROT_WRITE;
279 if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0)
280 segprot |= VM_PROT_EXECUTE;
282 if (end <= segstart) {
284 * Case 1: there is no overlap between the previous
285 * segment and this one. Apply protections to the
286 * previous segment, and protect the gap between the
287 * previous and current segments, if any.
289 link_elf_protect_range(ef, start, end, prot);
290 link_elf_protect_range(ef, end, segstart, gapprot);
295 } else if (start < segstart && end == segend) {
297 * Case 2: the current segment is a subrange of the
298 * previous segment. Apply protections to the
299 * non-overlapping portion of the previous segment.
301 link_elf_protect_range(ef, start, segstart, prot);
305 } else if (end < segend) {
307 * Case 3: there is partial overlap between the previous
308 * and current segments. Apply protections to the
309 * non-overlapping portion of the previous segment, and
310 * then the overlap, which must use the union of the two
311 * segments' protections.
313 link_elf_protect_range(ef, start, segstart, prot);
314 link_elf_protect_range(ef, segstart, end,
321 * Case 4: the two segments reside in the same page.
328 * Fix up the last unprotected segment and trailing data.
330 link_elf_protect_range(ef, start, end, prot);
331 link_elf_protect_range(ef, end,
332 round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
336 link_elf_link_preload(linker_class_t cls, const char *filename,
337 linker_file_t *result)
342 void *modptr, *baseptr, *sizeptr;
347 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
349 /* Look to see if we have the file preloaded */
350 modptr = preload_search_by_name(filename);
354 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
355 baseptr = preload_search_info(modptr, MODINFO_ADDR);
356 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
357 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
359 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
361 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
362 " obj module") != 0 &&
363 strcmp(type, "elf obj module") != 0)) {
366 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
370 lf = linker_make_file(filename, &link_elf_class);
376 ef->address = *(caddr_t *)baseptr;
377 lf->address = *(caddr_t *)baseptr;
378 lf->size = *(size_t *)sizeptr;
380 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
381 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
382 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
383 hdr->e_version != EV_CURRENT ||
384 hdr->e_type != ET_REL ||
385 hdr->e_machine != ELF_TARG_MACH) {
391 /* Scan the section header for information and table sizing. */
394 for (i = 0; i < hdr->e_shnum; i++) {
395 switch (shdr[i].sh_type) {
399 case SHT_X86_64_UNWIND:
403 /* Ignore sections not loaded by the loader. */
404 if (shdr[i].sh_addr == 0)
410 symstrindex = shdr[i].sh_link;
414 * Ignore relocation tables for sections not
415 * loaded by the loader.
417 if (shdr[shdr[i].sh_info].sh_addr == 0)
422 if (shdr[shdr[i].sh_info].sh_addr == 0)
429 shstrindex = hdr->e_shstrndx;
430 if (ef->nprogtab == 0 || symstrindex < 0 ||
431 symstrindex >= hdr->e_shnum ||
432 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
433 shstrindex >= hdr->e_shnum ||
434 shdr[shstrindex].sh_type != SHT_STRTAB) {
435 printf("%s: bad/missing section headers\n", filename);
440 /* Allocate space for tracking the load chunks */
441 if (ef->nprogtab != 0)
442 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
443 M_LINKER, M_WAITOK | M_ZERO);
444 if (ef->nreltab != 0)
445 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
446 M_LINKER, M_WAITOK | M_ZERO);
447 if (ef->nrelatab != 0)
448 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
449 M_LINKER, M_WAITOK | M_ZERO);
450 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
451 (ef->nreltab != 0 && ef->reltab == NULL) ||
452 (ef->nrelatab != 0 && ef->relatab == NULL)) {
457 /* XXX, relocate the sh_addr fields saved by the loader. */
459 for (i = 0; i < hdr->e_shnum; i++) {
460 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
461 off = shdr[i].sh_addr;
463 for (i = 0; i < hdr->e_shnum; i++) {
464 if (shdr[i].sh_addr != 0)
465 shdr[i].sh_addr = shdr[i].sh_addr - off +
466 (Elf_Addr)ef->address;
469 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
470 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
471 ef->ddbstrcnt = shdr[symstrindex].sh_size;
472 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
473 ef->shstrcnt = shdr[shstrindex].sh_size;
474 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
476 /* Now fill out progtab and the relocation tables. */
480 for (i = 0; i < hdr->e_shnum; i++) {
481 switch (shdr[i].sh_type) {
485 case SHT_X86_64_UNWIND:
489 if (shdr[i].sh_addr == 0)
491 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
492 if (shdr[i].sh_type == SHT_PROGBITS)
493 ef->progtab[pb].name = "<<PROGBITS>>";
495 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
496 ef->progtab[pb].name = "<<UNWIND>>";
498 else if (shdr[i].sh_type == SHT_INIT_ARRAY)
499 ef->progtab[pb].name = "<<INIT_ARRAY>>";
500 else if (shdr[i].sh_type == SHT_FINI_ARRAY)
501 ef->progtab[pb].name = "<<FINI_ARRAY>>";
503 ef->progtab[pb].name = "<<NOBITS>>";
504 ef->progtab[pb].size = shdr[i].sh_size;
505 ef->progtab[pb].flags = shdr[i].sh_flags;
506 ef->progtab[pb].sec = i;
507 if (ef->shstrtab && shdr[i].sh_name != 0)
508 ef->progtab[pb].name =
509 ef->shstrtab + shdr[i].sh_name;
510 if (ef->progtab[pb].name != NULL &&
511 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
514 dpcpu = dpcpu_alloc(shdr[i].sh_size);
516 printf("%s: pcpu module space is out "
517 "of space; cannot allocate %#jx "
518 "for %s\n", __func__,
519 (uintmax_t)shdr[i].sh_size,
524 memcpy(dpcpu, ef->progtab[pb].addr,
525 ef->progtab[pb].size);
526 dpcpu_copy(dpcpu, shdr[i].sh_size);
527 ef->progtab[pb].addr = dpcpu;
529 } else if (ef->progtab[pb].name != NULL &&
530 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
533 vnet_data = vnet_data_alloc(shdr[i].sh_size);
534 if (vnet_data == NULL) {
535 printf("%s: vnet module space is out "
536 "of space; cannot allocate %#jx "
537 "for %s\n", __func__,
538 (uintmax_t)shdr[i].sh_size,
543 memcpy(vnet_data, ef->progtab[pb].addr,
544 ef->progtab[pb].size);
545 vnet_data_copy(vnet_data, shdr[i].sh_size);
546 ef->progtab[pb].addr = vnet_data;
548 } else if ((ef->progtab[pb].name != NULL &&
549 strcmp(ef->progtab[pb].name, ".ctors") == 0) ||
550 shdr[i].sh_type == SHT_INIT_ARRAY) {
551 if (lf->ctors_addr != 0) {
553 "%s: multiple ctor sections in %s\n",
556 lf->ctors_addr = ef->progtab[pb].addr;
557 lf->ctors_size = shdr[i].sh_size;
559 } else if ((ef->progtab[pb].name != NULL &&
560 strcmp(ef->progtab[pb].name, ".dtors") == 0) ||
561 shdr[i].sh_type == SHT_FINI_ARRAY) {
562 if (lf->dtors_addr != 0) {
564 "%s: multiple dtor sections in %s\n",
567 lf->dtors_addr = ef->progtab[pb].addr;
568 lf->dtors_size = shdr[i].sh_size;
572 /* Update all symbol values with the offset. */
573 for (j = 0; j < ef->ddbsymcnt; j++) {
574 es = &ef->ddbsymtab[j];
575 if (es->st_shndx != i)
577 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
582 if (shdr[shdr[i].sh_info].sh_addr == 0)
584 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
585 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
586 ef->reltab[rl].sec = shdr[i].sh_info;
590 if (shdr[shdr[i].sh_info].sh_addr == 0)
592 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
593 ef->relatab[ra].nrela =
594 shdr[i].sh_size / sizeof(Elf_Rela);
595 ef->relatab[ra].sec = shdr[i].sh_info;
600 if (pb != ef->nprogtab) {
601 printf("%s: lost progbits\n", filename);
605 if (rl != ef->nreltab) {
606 printf("%s: lost reltab\n", filename);
610 if (ra != ef->nrelatab) {
611 printf("%s: lost relatab\n", filename);
617 * The file needs to be writeable and executable while applying
618 * relocations. Mapping protections are applied once relocation
619 * processing is complete.
621 link_elf_protect_range(ef, (vm_offset_t)ef->address,
622 round_page((vm_offset_t)ef->address + ef->lf.size), VM_PROT_ALL);
624 /* Local intra-module relocations */
625 error = link_elf_reloc_local(lf, false);
632 /* preload not done this way */
633 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
638 link_elf_invoke_cbs(caddr_t addr, size_t size)
643 if (addr == NULL || size == 0)
645 cnt = size / sizeof(*ctor);
647 for (i = 0; i < cnt; i++) {
654 link_elf_link_preload_finish(linker_file_t lf)
660 error = relocate_file(ef);
664 /* Notify MD code that a module is being loaded. */
665 error = elf_cpu_load_file(lf);
669 #if defined(__i386__) || defined(__amd64__)
671 error = link_elf_reloc_local(lf, true);
676 /* Apply protections now that relocation processing is complete. */
677 link_elf_protect(ef);
679 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
684 link_elf_load_file(linker_class_t cls, const char *filename,
685 linker_file_t *result)
687 struct nameidata *nd;
688 struct thread *td = curthread; /* XXX */
712 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
713 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
715 error = vn_open(nd, &flags, 0, NULL);
720 NDFREE(nd, NDF_ONLY_PNBUF);
721 if (nd->ni_vp->v_type != VREG) {
726 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
732 /* Read the elf header from the file. */
733 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
734 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
735 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
749 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
750 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
751 link_elf_error(filename, "Unsupported file layout");
755 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
756 || hdr->e_version != EV_CURRENT) {
757 link_elf_error(filename, "Unsupported file version");
761 if (hdr->e_type != ET_REL) {
765 if (hdr->e_machine != ELF_TARG_MACH) {
766 link_elf_error(filename, "Unsupported machine");
771 lf = linker_make_file(filename, &link_elf_class);
776 ef = (elf_file_t) lf;
782 /* Allocate and read in the section header */
783 nbytes = hdr->e_shnum * hdr->e_shentsize;
784 if (nbytes == 0 || hdr->e_shoff == 0 ||
785 hdr->e_shentsize != sizeof(Elf_Shdr)) {
789 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
791 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
792 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
801 /* Scan the section header for information and table sizing. */
805 for (i = 0; i < hdr->e_shnum; i++) {
806 if (shdr[i].sh_size == 0)
808 switch (shdr[i].sh_type) {
812 case SHT_X86_64_UNWIND:
816 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
823 symstrindex = shdr[i].sh_link;
827 * Ignore relocation tables for unallocated
830 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
835 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
843 if (ef->nprogtab == 0) {
844 link_elf_error(filename, "file has no contents");
849 /* Only allow one symbol table for now */
850 link_elf_error(filename,
851 "file must have exactly one symbol table");
855 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
856 shdr[symstrindex].sh_type != SHT_STRTAB) {
857 link_elf_error(filename, "file has invalid symbol strings");
862 /* Allocate space for tracking the load chunks */
863 if (ef->nprogtab != 0)
864 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
865 M_LINKER, M_WAITOK | M_ZERO);
866 if (ef->nreltab != 0)
867 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
868 M_LINKER, M_WAITOK | M_ZERO);
869 if (ef->nrelatab != 0)
870 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
871 M_LINKER, M_WAITOK | M_ZERO);
873 if (symtabindex == -1) {
874 link_elf_error(filename, "lost symbol table index");
878 /* Allocate space for and load the symbol table */
879 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
880 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
881 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
882 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
883 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
892 /* Allocate space for and load the symbol strings */
893 ef->ddbstrcnt = shdr[symstrindex].sh_size;
894 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
895 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
896 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
897 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
906 /* Do we have a string table for the section names? */
908 if (hdr->e_shstrndx != 0 &&
909 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
910 shstrindex = hdr->e_shstrndx;
911 ef->shstrcnt = shdr[shstrindex].sh_size;
912 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
914 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
915 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
916 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
926 /* Size up code/data(progbits) and bss(nobits). */
928 for (i = 0; i < hdr->e_shnum; i++) {
929 if (shdr[i].sh_size == 0)
931 switch (shdr[i].sh_type) {
935 case SHT_X86_64_UNWIND:
939 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
941 alignmask = shdr[i].sh_addralign - 1;
942 mapsize += alignmask;
943 mapsize &= ~alignmask;
944 mapsize += shdr[i].sh_size;
950 * We know how much space we need for the text/data/bss/etc.
951 * This stuff needs to be in a single chunk so that profiling etc
952 * can get the bounds and gdb can associate offsets with modules
954 ef->object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(mapsize),
955 VM_PROT_ALL, 0, thread0.td_ucred);
956 if (ef->object == NULL) {
960 #if VM_NRESERVLEVEL > 0
961 vm_object_color(ef->object, 0);
965 * In order to satisfy amd64's architectural requirements on the
966 * location of code and data in the kernel's address space, request a
967 * mapping that is above the kernel.
969 * Protections will be restricted once relocations are applied.
974 mapbase = VM_MIN_KERNEL_ADDRESS;
976 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
977 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
979 if (error != KERN_SUCCESS) {
980 vm_object_deallocate(ef->object);
987 error = vm_map_wire(kernel_map, mapbase,
988 mapbase + round_page(mapsize),
989 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
990 if (error != KERN_SUCCESS) {
995 /* Inform the kld system about the situation */
996 lf->address = ef->address = (caddr_t)mapbase;
1000 * Now load code/data(progbits), zero bss(nobits), allocate space for
1007 for (i = 0; i < hdr->e_shnum; i++) {
1008 if (shdr[i].sh_size == 0)
1010 switch (shdr[i].sh_type) {
1014 case SHT_X86_64_UNWIND:
1016 case SHT_INIT_ARRAY:
1017 case SHT_FINI_ARRAY:
1018 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
1020 alignmask = shdr[i].sh_addralign - 1;
1021 mapbase += alignmask;
1022 mapbase &= ~alignmask;
1023 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
1024 ef->progtab[pb].name =
1025 ef->shstrtab + shdr[i].sh_name;
1026 if (!strcmp(ef->progtab[pb].name, ".ctors") ||
1027 shdr[i].sh_type == SHT_INIT_ARRAY) {
1028 if (lf->ctors_addr != 0) {
1030 "%s: multiple ctor sections in %s\n",
1031 __func__, filename);
1038 } else if (!strcmp(ef->progtab[pb].name,
1040 shdr[i].sh_type == SHT_FINI_ARRAY) {
1041 if (lf->dtors_addr != 0) {
1043 "%s: multiple dtor sections in %s\n",
1044 __func__, filename);
1052 } else if (shdr[i].sh_type == SHT_PROGBITS)
1053 ef->progtab[pb].name = "<<PROGBITS>>";
1055 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
1056 ef->progtab[pb].name = "<<UNWIND>>";
1059 ef->progtab[pb].name = "<<NOBITS>>";
1060 if (ef->progtab[pb].name != NULL &&
1061 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
1062 ef->progtab[pb].addr =
1063 dpcpu_alloc(shdr[i].sh_size);
1064 if (ef->progtab[pb].addr == NULL) {
1065 printf("%s: pcpu module space is out "
1066 "of space; cannot allocate %#jx "
1067 "for %s\n", __func__,
1068 (uintmax_t)shdr[i].sh_size,
1073 else if (ef->progtab[pb].name != NULL &&
1074 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
1075 ef->progtab[pb].addr =
1076 vnet_data_alloc(shdr[i].sh_size);
1077 if (ef->progtab[pb].addr == NULL) {
1078 printf("%s: vnet module space is out "
1079 "of space; cannot allocate %#jx "
1080 "for %s\n", __func__,
1081 (uintmax_t)shdr[i].sh_size,
1087 ef->progtab[pb].addr =
1088 (void *)(uintptr_t)mapbase;
1089 if (ef->progtab[pb].addr == NULL) {
1093 ef->progtab[pb].size = shdr[i].sh_size;
1094 ef->progtab[pb].flags = shdr[i].sh_flags;
1095 ef->progtab[pb].sec = i;
1096 if (shdr[i].sh_type == SHT_PROGBITS
1098 || shdr[i].sh_type == SHT_X86_64_UNWIND
1101 error = vn_rdwr(UIO_READ, nd->ni_vp,
1102 ef->progtab[pb].addr,
1103 shdr[i].sh_size, shdr[i].sh_offset,
1104 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1105 NOCRED, &resid, td);
1112 /* Initialize the per-cpu or vnet area. */
1113 if (ef->progtab[pb].addr != (void *)mapbase &&
1114 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1115 dpcpu_copy(ef->progtab[pb].addr,
1118 else if (ef->progtab[pb].addr !=
1120 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
1121 vnet_data_copy(ef->progtab[pb].addr,
1125 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1127 /* Update all symbol values with the offset. */
1128 for (j = 0; j < ef->ddbsymcnt; j++) {
1129 es = &ef->ddbsymtab[j];
1130 if (es->st_shndx != i)
1132 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1134 mapbase += shdr[i].sh_size;
1138 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1140 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1142 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1143 ef->reltab[rl].sec = shdr[i].sh_info;
1144 error = vn_rdwr(UIO_READ, nd->ni_vp,
1145 (void *)ef->reltab[rl].rel,
1146 shdr[i].sh_size, shdr[i].sh_offset,
1147 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1158 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1160 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1162 ef->relatab[ra].nrela =
1163 shdr[i].sh_size / sizeof(Elf_Rela);
1164 ef->relatab[ra].sec = shdr[i].sh_info;
1165 error = vn_rdwr(UIO_READ, nd->ni_vp,
1166 (void *)ef->relatab[ra].rela,
1167 shdr[i].sh_size, shdr[i].sh_offset,
1168 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1180 if (pb != ef->nprogtab) {
1181 link_elf_error(filename, "lost progbits");
1185 if (rl != ef->nreltab) {
1186 link_elf_error(filename, "lost reltab");
1190 if (ra != ef->nrelatab) {
1191 link_elf_error(filename, "lost relatab");
1195 if (mapbase != (vm_offset_t)ef->address + mapsize) {
1197 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1198 filename != NULL ? filename : "<none>",
1199 (u_long)mapbase, ef->address, (u_long)mapsize,
1200 (u_long)(vm_offset_t)ef->address + mapsize);
1205 /* Local intra-module relocations */
1206 error = link_elf_reloc_local(lf, false);
1210 /* Pull in dependencies */
1211 VOP_UNLOCK(nd->ni_vp);
1212 error = linker_load_dependencies(lf);
1213 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1217 /* External relocations */
1218 error = relocate_file(ef);
1222 /* Notify MD code that a module is being loaded. */
1223 error = elf_cpu_load_file(lf);
1227 #if defined(__i386__) || defined(__amd64__)
1229 error = link_elf_reloc_local(lf, true);
1234 link_elf_protect(ef);
1235 link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
1239 VOP_UNLOCK(nd->ni_vp);
1240 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1243 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1244 free(hdr, M_LINKER);
1250 link_elf_unload_file(linker_file_t file)
1252 elf_file_t ef = (elf_file_t) file;
1255 link_elf_invoke_cbs(file->dtors_addr, file->dtors_size);
1257 /* Notify MD code that a module is being unloaded. */
1258 elf_cpu_unload_file(file);
1261 for (i = 0; i < ef->nprogtab; i++) {
1262 if (ef->progtab[i].size == 0)
1264 if (ef->progtab[i].name == NULL)
1266 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1267 dpcpu_free(ef->progtab[i].addr,
1268 ef->progtab[i].size);
1270 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1271 vnet_data_free(ef->progtab[i].addr,
1272 ef->progtab[i].size);
1276 if (ef->preloaded) {
1277 free(ef->reltab, M_LINKER);
1278 free(ef->relatab, M_LINKER);
1279 free(ef->progtab, M_LINKER);
1280 free(ef->ctftab, M_LINKER);
1281 free(ef->ctfoff, M_LINKER);
1282 free(ef->typoff, M_LINKER);
1283 if (file->pathname != NULL)
1284 preload_delete_name(file->pathname);
1288 for (i = 0; i < ef->nreltab; i++)
1289 free(ef->reltab[i].rel, M_LINKER);
1290 for (i = 0; i < ef->nrelatab; i++)
1291 free(ef->relatab[i].rela, M_LINKER);
1292 free(ef->reltab, M_LINKER);
1293 free(ef->relatab, M_LINKER);
1294 free(ef->progtab, M_LINKER);
1296 if (ef->object != NULL)
1297 vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1298 (vm_offset_t)ef->address + ptoa(ef->object->size));
1299 free(ef->e_shdr, M_LINKER);
1300 free(ef->ddbsymtab, M_LINKER);
1301 free(ef->ddbstrtab, M_LINKER);
1302 free(ef->shstrtab, M_LINKER);
1303 free(ef->ctftab, M_LINKER);
1304 free(ef->ctfoff, M_LINKER);
1305 free(ef->typoff, M_LINKER);
1309 symbol_name(elf_file_t ef, Elf_Size r_info)
1313 if (ELF_R_SYM(r_info)) {
1314 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1315 return ef->ddbstrtab + ref->st_name;
1321 findbase(elf_file_t ef, int sec)
1326 for (i = 0; i < ef->nprogtab; i++) {
1327 if (sec == ef->progtab[i].sec) {
1328 base = (Elf_Addr)ef->progtab[i].addr;
1336 relocate_file1(elf_file_t ef, bool ifuncs)
1338 const Elf_Rel *rellim;
1340 const Elf_Rela *relalim;
1341 const Elf_Rela *rela;
1342 const char *symname;
1348 /* Perform relocations without addend if there are any: */
1349 for (i = 0; i < ef->nreltab; i++) {
1350 rel = ef->reltab[i].rel;
1352 link_elf_error(ef->lf.filename, "lost a reltab!");
1355 rellim = rel + ef->reltab[i].nrel;
1356 base = findbase(ef, ef->reltab[i].sec);
1358 link_elf_error(ef->lf.filename, "lost base for reltab");
1361 for ( ; rel < rellim; rel++) {
1362 symidx = ELF_R_SYM(rel->r_info);
1363 if (symidx >= ef->ddbsymcnt)
1365 sym = ef->ddbsymtab + symidx;
1366 /* Local relocs are already done */
1367 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1369 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1370 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1372 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1374 symname = symbol_name(ef, rel->r_info);
1375 printf("link_elf_obj: symbol %s undefined\n",
1382 /* Perform relocations with addend if there are any: */
1383 for (i = 0; i < ef->nrelatab; i++) {
1384 rela = ef->relatab[i].rela;
1386 link_elf_error(ef->lf.filename, "lost a relatab!");
1389 relalim = rela + ef->relatab[i].nrela;
1390 base = findbase(ef, ef->relatab[i].sec);
1392 link_elf_error(ef->lf.filename,
1393 "lost base for relatab");
1396 for ( ; rela < relalim; rela++) {
1397 symidx = ELF_R_SYM(rela->r_info);
1398 if (symidx >= ef->ddbsymcnt)
1400 sym = ef->ddbsymtab + symidx;
1401 /* Local relocs are already done */
1402 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1404 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1405 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1407 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1409 symname = symbol_name(ef, rela->r_info);
1410 printf("link_elf_obj: symbol %s undefined\n",
1418 * Only clean SHN_FBSD_CACHED for successful return. If we
1419 * modified symbol table for the object but found an
1420 * unresolved symbol, there is no reason to roll back.
1422 elf_obj_cleanup_globals_cache(ef);
1428 relocate_file(elf_file_t ef)
1432 error = relocate_file1(ef, false);
1434 error = relocate_file1(ef, true);
1439 link_elf_lookup_symbol1(linker_file_t lf, const char *name, c_linker_sym_t *sym,
1442 elf_file_t ef = (elf_file_t)lf;
1443 const Elf_Sym *symp;
1447 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1448 strp = ef->ddbstrtab + symp->st_name;
1449 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1451 ELF_ST_BIND(symp->st_info) == STB_GLOBAL) {
1452 *sym = (c_linker_sym_t) symp;
1462 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1464 return (link_elf_lookup_symbol1(lf, name, sym,
1465 link_elf_obj_leak_locals));
1469 link_elf_lookup_debug_symbol(linker_file_t lf, const char *name,
1470 c_linker_sym_t *sym)
1472 return (link_elf_lookup_symbol1(lf, name, sym, true));
1476 link_elf_symbol_values1(linker_file_t lf, c_linker_sym_t sym,
1477 linker_symval_t *symval, bool see_local)
1483 ef = (elf_file_t) lf;
1484 es = (const Elf_Sym*) sym;
1485 val = (caddr_t)es->st_value;
1486 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1487 if (!see_local && ELF_ST_BIND(es->st_info) == STB_LOCAL)
1489 symval->name = ef->ddbstrtab + es->st_name;
1490 val = (caddr_t)es->st_value;
1491 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1492 val = ((caddr_t (*)(void))val)();
1493 symval->value = val;
1494 symval->size = es->st_size;
1501 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1502 linker_symval_t *symval)
1504 return (link_elf_symbol_values1(lf, sym, symval,
1505 link_elf_obj_leak_locals));
1509 link_elf_debug_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1510 linker_symval_t *symval)
1512 return (link_elf_symbol_values1(lf, sym, symval, true));
1516 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1517 c_linker_sym_t *sym, long *diffp)
1519 elf_file_t ef = (elf_file_t)lf;
1520 u_long off = (uintptr_t)(void *)value;
1524 const Elf_Sym *best = NULL;
1527 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1528 if (es->st_name == 0)
1530 st_value = es->st_value;
1531 if (off >= st_value) {
1532 if (off - st_value < diff) {
1533 diff = off - st_value;
1537 } else if (off - st_value == diff) {
1546 *sym = (c_linker_sym_t) best;
1552 * Look up a linker set on an ELF system.
1555 link_elf_lookup_set(linker_file_t lf, const char *name,
1556 void ***startp, void ***stopp, int *countp)
1558 elf_file_t ef = (elf_file_t)lf;
1559 void **start, **stop;
1562 /* Relative to section number */
1563 for (i = 0; i < ef->nprogtab; i++) {
1564 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1565 strcmp(ef->progtab[i].name + 4, name) == 0) {
1566 start = (void **)ef->progtab[i].addr;
1567 stop = (void **)((char *)ef->progtab[i].addr +
1568 ef->progtab[i].size);
1569 count = stop - start;
1583 link_elf_each_function_name(linker_file_t file,
1584 int (*callback)(const char *, void *), void *opaque)
1586 elf_file_t ef = (elf_file_t)file;
1587 const Elf_Sym *symp;
1590 /* Exhaustive search */
1591 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1592 if (symp->st_value != 0 &&
1593 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1594 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1595 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1604 link_elf_each_function_nameval(linker_file_t file,
1605 linker_function_nameval_callback_t callback, void *opaque)
1607 linker_symval_t symval;
1608 elf_file_t ef = (elf_file_t)file;
1609 const Elf_Sym *symp;
1612 /* Exhaustive search */
1613 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1614 if (symp->st_value != 0 &&
1615 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1616 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1617 error = link_elf_debug_symbol_values(file,
1618 (c_linker_sym_t)symp, &symval);
1620 error = callback(file, i, &symval, opaque);
1629 elf_obj_cleanup_globals_cache(elf_file_t ef)
1634 for (i = 0; i < ef->ddbsymcnt; i++) {
1635 sym = ef->ddbsymtab + i;
1636 if (sym->st_shndx == SHN_FBSD_CACHED) {
1637 sym->st_shndx = SHN_UNDEF;
1644 * Symbol lookup function that can be used when the symbol index is known (ie
1645 * in relocations). It uses the symbol index instead of doing a fully fledged
1646 * hash table based lookup when such is valid. For example for local symbols.
1647 * This is not only more efficient, it's also more correct. It's not always
1648 * the case that the symbol can be found through the hash table.
1651 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1653 elf_file_t ef = (elf_file_t)lf;
1658 /* Don't even try to lookup the symbol if the index is bogus. */
1659 if (symidx >= ef->ddbsymcnt) {
1664 sym = ef->ddbsymtab + symidx;
1666 /* Quick answer if there is a definition included. */
1667 if (sym->st_shndx != SHN_UNDEF) {
1668 res1 = (Elf_Addr)sym->st_value;
1669 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1670 res1 = ((Elf_Addr (*)(void))res1)();
1675 /* If we get here, then it is undefined and needs a lookup. */
1676 switch (ELF_ST_BIND(sym->st_info)) {
1678 /* Local, but undefined? huh? */
1684 /* Relative to Data or Function name */
1685 symbol = ef->ddbstrtab + sym->st_name;
1687 /* Force a lookup failure if the symbol name is bogus. */
1692 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1695 * Cache global lookups during module relocation. The failure
1696 * case is particularly expensive for callers, who must scan
1697 * through the entire globals table doing strcmp(). Cache to
1698 * avoid doing such work repeatedly.
1700 * After relocation is complete, undefined globals will be
1701 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1705 sym->st_shndx = SHN_FBSD_CACHED;
1706 sym->st_value = res1;
1709 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1722 link_elf_fix_link_set(elf_file_t ef)
1724 static const char startn[] = "__start_";
1725 static const char stopn[] = "__stop_";
1727 const char *sym_name, *linkset_name;
1728 Elf_Addr startp, stopp;
1733 for (symidx = 1 /* zero entry is special */;
1734 symidx < ef->ddbsymcnt; symidx++) {
1735 sym = ef->ddbsymtab + symidx;
1736 if (sym->st_shndx != SHN_UNDEF)
1739 sym_name = ef->ddbstrtab + sym->st_name;
1740 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1742 linkset_name = sym_name + sizeof(startn) - 1;
1744 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1746 linkset_name = sym_name + sizeof(stopn) - 1;
1751 for (i = 0; i < ef->nprogtab; i++) {
1752 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1753 startp = (Elf_Addr)ef->progtab[i].addr;
1754 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1758 if (i == ef->nprogtab)
1761 sym->st_value = start ? startp : stopp;
1767 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1769 elf_file_t ef = (elf_file_t)lf;
1770 const Elf_Rel *rellim;
1772 const Elf_Rela *relalim;
1773 const Elf_Rela *rela;
1779 link_elf_fix_link_set(ef);
1781 /* Perform relocations without addend if there are any: */
1782 for (i = 0; i < ef->nreltab; i++) {
1783 rel = ef->reltab[i].rel;
1785 link_elf_error(ef->lf.filename, "lost a reltab");
1788 rellim = rel + ef->reltab[i].nrel;
1789 base = findbase(ef, ef->reltab[i].sec);
1791 link_elf_error(ef->lf.filename, "lost base for reltab");
1794 for ( ; rel < rellim; rel++) {
1795 symidx = ELF_R_SYM(rel->r_info);
1796 if (symidx >= ef->ddbsymcnt)
1798 sym = ef->ddbsymtab + symidx;
1799 /* Only do local relocs */
1800 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1802 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1803 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1805 if (elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1806 elf_obj_lookup) != 0)
1811 /* Perform relocations with addend if there are any: */
1812 for (i = 0; i < ef->nrelatab; i++) {
1813 rela = ef->relatab[i].rela;
1815 link_elf_error(ef->lf.filename, "lost a relatab!");
1818 relalim = rela + ef->relatab[i].nrela;
1819 base = findbase(ef, ef->relatab[i].sec);
1821 link_elf_error(ef->lf.filename, "lost base for reltab");
1824 for ( ; rela < relalim; rela++) {
1825 symidx = ELF_R_SYM(rela->r_info);
1826 if (symidx >= ef->ddbsymcnt)
1828 sym = ef->ddbsymtab + symidx;
1829 /* Only do local relocs */
1830 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1832 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1833 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1835 if (elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1836 elf_obj_lookup) != 0)
1844 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1846 elf_file_t ef = (elf_file_t)lf;
1848 *symtab = ef->ddbsymtab;
1849 if (*symtab == NULL)
1851 return (ef->ddbsymcnt);
1855 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1857 elf_file_t ef = (elf_file_t)lf;
1859 *strtab = ef->ddbstrtab;
1860 if (*strtab == NULL)
1862 return (ef->ddbstrcnt);