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, FALSE);
223 KASSERT(error == KERN_SUCCESS,
224 ("link_elf_protect_range: vm_map_protect() returned %d", error));
228 * Restrict permissions on linker file memory based on section flags.
229 * Sections need not be page-aligned, so overlap within a page is possible.
232 link_elf_protect(elf_file_t ef)
234 vm_offset_t end, segend, segstart, start;
235 vm_prot_t gapprot, prot, segprot;
239 * If the file was preloaded, the last page may contain other preloaded
240 * data which may need to be writeable. ELF files are always
241 * page-aligned, but other preloaded data, such as entropy or CPU
242 * microcode may be loaded with a smaller alignment.
244 gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ;
246 start = end = (vm_offset_t)ef->address;
248 for (i = 0; i < ef->nprogtab; i++) {
250 * VNET and DPCPU sections have their memory allocated by their
251 * respective subsystems.
253 if (ef->progtab[i].name != NULL && (
255 strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 ||
257 strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0))
260 segstart = trunc_page((vm_offset_t)ef->progtab[i].addr);
261 segend = round_page((vm_offset_t)ef->progtab[i].addr +
262 ef->progtab[i].size);
263 segprot = VM_PROT_READ;
264 if ((ef->progtab[i].flags & SHF_WRITE) != 0)
265 segprot |= VM_PROT_WRITE;
266 if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0)
267 segprot |= VM_PROT_EXECUTE;
269 if (end <= segstart) {
271 * Case 1: there is no overlap between the previous
272 * segment and this one. Apply protections to the
273 * previous segment, and protect the gap between the
274 * previous and current segments, if any.
276 link_elf_protect_range(ef, start, end, prot);
277 link_elf_protect_range(ef, end, segstart, gapprot);
282 } else if (start < segstart && end == segend) {
284 * Case 2: the current segment is a subrange of the
285 * previous segment. Apply protections to the
286 * non-overlapping portion of the previous segment.
288 link_elf_protect_range(ef, start, segstart, prot);
292 } else if (end < segend) {
294 * Case 3: there is partial overlap between the previous
295 * and current segments. Apply protections to the
296 * non-overlapping portion of the previous segment, and
297 * then the overlap, which must use the union of the two
298 * segments' protections.
300 link_elf_protect_range(ef, start, segstart, prot);
301 link_elf_protect_range(ef, segstart, end,
308 * Case 4: the two segments reside in the same page.
315 * Fix up the last unprotected segment and trailing data.
317 link_elf_protect_range(ef, start, end, prot);
318 link_elf_protect_range(ef, end,
319 round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
323 link_elf_link_preload(linker_class_t cls, const char *filename,
324 linker_file_t *result)
329 void *modptr, *baseptr, *sizeptr;
334 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
336 /* Look to see if we have the file preloaded */
337 modptr = preload_search_by_name(filename);
341 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
342 baseptr = preload_search_info(modptr, MODINFO_ADDR);
343 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
344 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
346 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
348 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
349 " obj module") != 0 &&
350 strcmp(type, "elf obj module") != 0)) {
353 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
357 lf = linker_make_file(filename, &link_elf_class);
363 ef->address = *(caddr_t *)baseptr;
364 lf->address = *(caddr_t *)baseptr;
365 lf->size = *(size_t *)sizeptr;
367 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
368 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
369 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
370 hdr->e_version != EV_CURRENT ||
371 hdr->e_type != ET_REL ||
372 hdr->e_machine != ELF_TARG_MACH) {
378 /* Scan the section header for information and table sizing. */
381 for (i = 0; i < hdr->e_shnum; i++) {
382 switch (shdr[i].sh_type) {
386 case SHT_X86_64_UNWIND:
388 /* Ignore sections not loaded by the loader. */
389 if (shdr[i].sh_addr == 0)
395 symstrindex = shdr[i].sh_link;
399 * Ignore relocation tables for sections not
400 * loaded by the loader.
402 if (shdr[shdr[i].sh_info].sh_addr == 0)
407 if (shdr[shdr[i].sh_info].sh_addr == 0)
414 shstrindex = hdr->e_shstrndx;
415 if (ef->nprogtab == 0 || symstrindex < 0 ||
416 symstrindex >= hdr->e_shnum ||
417 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
418 shstrindex >= hdr->e_shnum ||
419 shdr[shstrindex].sh_type != SHT_STRTAB) {
420 printf("%s: bad/missing section headers\n", filename);
425 /* Allocate space for tracking the load chunks */
426 if (ef->nprogtab != 0)
427 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
428 M_LINKER, M_WAITOK | M_ZERO);
429 if (ef->nreltab != 0)
430 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
431 M_LINKER, M_WAITOK | M_ZERO);
432 if (ef->nrelatab != 0)
433 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
434 M_LINKER, M_WAITOK | M_ZERO);
435 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
436 (ef->nreltab != 0 && ef->reltab == NULL) ||
437 (ef->nrelatab != 0 && ef->relatab == NULL)) {
442 /* XXX, relocate the sh_addr fields saved by the loader. */
444 for (i = 0; i < hdr->e_shnum; i++) {
445 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
446 off = shdr[i].sh_addr;
448 for (i = 0; i < hdr->e_shnum; i++) {
449 if (shdr[i].sh_addr != 0)
450 shdr[i].sh_addr = shdr[i].sh_addr - off +
451 (Elf_Addr)ef->address;
454 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
455 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
456 ef->ddbstrcnt = shdr[symstrindex].sh_size;
457 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
458 ef->shstrcnt = shdr[shstrindex].sh_size;
459 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
461 /* Now fill out progtab and the relocation tables. */
465 for (i = 0; i < hdr->e_shnum; i++) {
466 switch (shdr[i].sh_type) {
470 case SHT_X86_64_UNWIND:
472 if (shdr[i].sh_addr == 0)
474 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
475 if (shdr[i].sh_type == SHT_PROGBITS)
476 ef->progtab[pb].name = "<<PROGBITS>>";
478 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
479 ef->progtab[pb].name = "<<UNWIND>>";
482 ef->progtab[pb].name = "<<NOBITS>>";
483 ef->progtab[pb].size = shdr[i].sh_size;
484 ef->progtab[pb].flags = shdr[i].sh_flags;
485 ef->progtab[pb].sec = i;
486 if (ef->shstrtab && shdr[i].sh_name != 0)
487 ef->progtab[pb].name =
488 ef->shstrtab + shdr[i].sh_name;
489 if (ef->progtab[pb].name != NULL &&
490 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
493 dpcpu = dpcpu_alloc(shdr[i].sh_size);
495 printf("%s: pcpu module space is out "
496 "of space; cannot allocate %#jx "
497 "for %s\n", __func__,
498 (uintmax_t)shdr[i].sh_size,
503 memcpy(dpcpu, ef->progtab[pb].addr,
504 ef->progtab[pb].size);
505 dpcpu_copy(dpcpu, shdr[i].sh_size);
506 ef->progtab[pb].addr = dpcpu;
508 } else if (ef->progtab[pb].name != NULL &&
509 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
512 vnet_data = vnet_data_alloc(shdr[i].sh_size);
513 if (vnet_data == NULL) {
514 printf("%s: vnet module space is out "
515 "of space; cannot allocate %#jx "
516 "for %s\n", __func__,
517 (uintmax_t)shdr[i].sh_size,
522 memcpy(vnet_data, ef->progtab[pb].addr,
523 ef->progtab[pb].size);
524 vnet_data_copy(vnet_data, shdr[i].sh_size);
525 ef->progtab[pb].addr = vnet_data;
527 } else if (ef->progtab[pb].name != NULL &&
528 !strcmp(ef->progtab[pb].name, ".ctors")) {
529 lf->ctors_addr = ef->progtab[pb].addr;
530 lf->ctors_size = shdr[i].sh_size;
533 /* Update all symbol values with the offset. */
534 for (j = 0; j < ef->ddbsymcnt; j++) {
535 es = &ef->ddbsymtab[j];
536 if (es->st_shndx != i)
538 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
543 if (shdr[shdr[i].sh_info].sh_addr == 0)
545 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
546 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
547 ef->reltab[rl].sec = shdr[i].sh_info;
551 if (shdr[shdr[i].sh_info].sh_addr == 0)
553 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
554 ef->relatab[ra].nrela =
555 shdr[i].sh_size / sizeof(Elf_Rela);
556 ef->relatab[ra].sec = shdr[i].sh_info;
561 if (pb != ef->nprogtab) {
562 printf("%s: lost progbits\n", filename);
566 if (rl != ef->nreltab) {
567 printf("%s: lost reltab\n", filename);
571 if (ra != ef->nrelatab) {
572 printf("%s: lost relatab\n", filename);
578 * The file needs to be writeable and executable while applying
579 * relocations. Mapping protections are applied once relocation
580 * processing is complete.
582 link_elf_protect_range(ef, (vm_offset_t)ef->address,
583 round_page((vm_offset_t)ef->address + ef->lf.size), VM_PROT_ALL);
585 /* Local intra-module relocations */
586 error = link_elf_reloc_local(lf, false);
593 /* preload not done this way */
594 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
599 link_elf_invoke_ctors(caddr_t addr, size_t size)
604 if (addr == NULL || size == 0)
606 cnt = size / sizeof(*ctor);
608 for (i = 0; i < cnt; i++) {
615 link_elf_link_preload_finish(linker_file_t lf)
621 error = relocate_file(ef);
625 /* Notify MD code that a module is being loaded. */
626 error = elf_cpu_load_file(lf);
630 #if defined(__i386__) || defined(__amd64__)
632 error = link_elf_reloc_local(lf, true);
637 /* Apply protections now that relocation processing is complete. */
638 link_elf_protect(ef);
640 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
645 link_elf_load_file(linker_class_t cls, const char *filename,
646 linker_file_t *result)
648 struct nameidata *nd;
649 struct thread *td = curthread; /* XXX */
673 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
674 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
676 error = vn_open(nd, &flags, 0, NULL);
681 NDFREE(nd, NDF_ONLY_PNBUF);
682 if (nd->ni_vp->v_type != VREG) {
687 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
693 /* Read the elf header from the file. */
694 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
695 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
696 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
710 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
711 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
712 link_elf_error(filename, "Unsupported file layout");
716 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
717 || hdr->e_version != EV_CURRENT) {
718 link_elf_error(filename, "Unsupported file version");
722 if (hdr->e_type != ET_REL) {
726 if (hdr->e_machine != ELF_TARG_MACH) {
727 link_elf_error(filename, "Unsupported machine");
732 lf = linker_make_file(filename, &link_elf_class);
737 ef = (elf_file_t) lf;
743 /* Allocate and read in the section header */
744 nbytes = hdr->e_shnum * hdr->e_shentsize;
745 if (nbytes == 0 || hdr->e_shoff == 0 ||
746 hdr->e_shentsize != sizeof(Elf_Shdr)) {
750 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
752 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
753 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
762 /* Scan the section header for information and table sizing. */
766 for (i = 0; i < hdr->e_shnum; i++) {
767 if (shdr[i].sh_size == 0)
769 switch (shdr[i].sh_type) {
773 case SHT_X86_64_UNWIND:
775 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
782 symstrindex = shdr[i].sh_link;
786 * Ignore relocation tables for unallocated
789 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
794 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
802 if (ef->nprogtab == 0) {
803 link_elf_error(filename, "file has no contents");
808 /* Only allow one symbol table for now */
809 link_elf_error(filename,
810 "file must have exactly one symbol table");
814 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
815 shdr[symstrindex].sh_type != SHT_STRTAB) {
816 link_elf_error(filename, "file has invalid symbol strings");
821 /* Allocate space for tracking the load chunks */
822 if (ef->nprogtab != 0)
823 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
824 M_LINKER, M_WAITOK | M_ZERO);
825 if (ef->nreltab != 0)
826 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
827 M_LINKER, M_WAITOK | M_ZERO);
828 if (ef->nrelatab != 0)
829 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
830 M_LINKER, M_WAITOK | M_ZERO);
832 if (symtabindex == -1) {
833 link_elf_error(filename, "lost symbol table index");
837 /* Allocate space for and load the symbol table */
838 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
839 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
840 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
841 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
842 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
851 /* Allocate space for and load the symbol strings */
852 ef->ddbstrcnt = shdr[symstrindex].sh_size;
853 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
854 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
855 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
856 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
865 /* Do we have a string table for the section names? */
867 if (hdr->e_shstrndx != 0 &&
868 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
869 shstrindex = hdr->e_shstrndx;
870 ef->shstrcnt = shdr[shstrindex].sh_size;
871 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
873 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
874 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
875 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
885 /* Size up code/data(progbits) and bss(nobits). */
887 for (i = 0; i < hdr->e_shnum; i++) {
888 if (shdr[i].sh_size == 0)
890 switch (shdr[i].sh_type) {
894 case SHT_X86_64_UNWIND:
896 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
898 alignmask = shdr[i].sh_addralign - 1;
899 mapsize += alignmask;
900 mapsize &= ~alignmask;
901 mapsize += shdr[i].sh_size;
907 * We know how much space we need for the text/data/bss/etc.
908 * This stuff needs to be in a single chunk so that profiling etc
909 * can get the bounds and gdb can associate offsets with modules
911 ef->object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(mapsize),
912 VM_PROT_ALL, 0, thread0.td_ucred);
913 if (ef->object == NULL) {
919 * In order to satisfy amd64's architectural requirements on the
920 * location of code and data in the kernel's address space, request a
921 * mapping that is above the kernel.
923 * Protections will be restricted once relocations are applied.
928 mapbase = VM_MIN_KERNEL_ADDRESS;
930 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
931 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
933 if (error != KERN_SUCCESS) {
934 vm_object_deallocate(ef->object);
941 error = vm_map_wire(kernel_map, mapbase,
942 mapbase + round_page(mapsize),
943 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
944 if (error != KERN_SUCCESS) {
949 /* Inform the kld system about the situation */
950 lf->address = ef->address = (caddr_t)mapbase;
954 * Now load code/data(progbits), zero bss(nobits), allocate space for
961 for (i = 0; i < hdr->e_shnum; i++) {
962 if (shdr[i].sh_size == 0)
964 switch (shdr[i].sh_type) {
968 case SHT_X86_64_UNWIND:
970 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
972 alignmask = shdr[i].sh_addralign - 1;
973 mapbase += alignmask;
974 mapbase &= ~alignmask;
975 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
976 ef->progtab[pb].name =
977 ef->shstrtab + shdr[i].sh_name;
978 if (!strcmp(ef->progtab[pb].name, ".ctors")) {
979 lf->ctors_addr = (caddr_t)mapbase;
980 lf->ctors_size = shdr[i].sh_size;
982 } else if (shdr[i].sh_type == SHT_PROGBITS)
983 ef->progtab[pb].name = "<<PROGBITS>>";
985 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
986 ef->progtab[pb].name = "<<UNWIND>>";
989 ef->progtab[pb].name = "<<NOBITS>>";
990 if (ef->progtab[pb].name != NULL &&
991 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
992 ef->progtab[pb].addr =
993 dpcpu_alloc(shdr[i].sh_size);
994 if (ef->progtab[pb].addr == NULL) {
995 printf("%s: pcpu module space is out "
996 "of space; cannot allocate %#jx "
997 "for %s\n", __func__,
998 (uintmax_t)shdr[i].sh_size,
1003 else if (ef->progtab[pb].name != NULL &&
1004 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
1005 ef->progtab[pb].addr =
1006 vnet_data_alloc(shdr[i].sh_size);
1007 if (ef->progtab[pb].addr == NULL) {
1008 printf("%s: vnet module space is out "
1009 "of space; cannot allocate %#jx "
1010 "for %s\n", __func__,
1011 (uintmax_t)shdr[i].sh_size,
1017 ef->progtab[pb].addr =
1018 (void *)(uintptr_t)mapbase;
1019 if (ef->progtab[pb].addr == NULL) {
1023 ef->progtab[pb].size = shdr[i].sh_size;
1024 ef->progtab[pb].flags = shdr[i].sh_flags;
1025 ef->progtab[pb].sec = i;
1026 if (shdr[i].sh_type == SHT_PROGBITS
1028 || shdr[i].sh_type == SHT_X86_64_UNWIND
1031 error = vn_rdwr(UIO_READ, nd->ni_vp,
1032 ef->progtab[pb].addr,
1033 shdr[i].sh_size, shdr[i].sh_offset,
1034 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1035 NOCRED, &resid, td);
1042 /* Initialize the per-cpu or vnet area. */
1043 if (ef->progtab[pb].addr != (void *)mapbase &&
1044 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1045 dpcpu_copy(ef->progtab[pb].addr,
1048 else if (ef->progtab[pb].addr !=
1050 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
1051 vnet_data_copy(ef->progtab[pb].addr,
1055 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1057 /* Update all symbol values with the offset. */
1058 for (j = 0; j < ef->ddbsymcnt; j++) {
1059 es = &ef->ddbsymtab[j];
1060 if (es->st_shndx != i)
1062 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1064 mapbase += shdr[i].sh_size;
1068 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1070 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1072 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1073 ef->reltab[rl].sec = shdr[i].sh_info;
1074 error = vn_rdwr(UIO_READ, nd->ni_vp,
1075 (void *)ef->reltab[rl].rel,
1076 shdr[i].sh_size, shdr[i].sh_offset,
1077 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1088 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1090 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1092 ef->relatab[ra].nrela =
1093 shdr[i].sh_size / sizeof(Elf_Rela);
1094 ef->relatab[ra].sec = shdr[i].sh_info;
1095 error = vn_rdwr(UIO_READ, nd->ni_vp,
1096 (void *)ef->relatab[ra].rela,
1097 shdr[i].sh_size, shdr[i].sh_offset,
1098 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1110 if (pb != ef->nprogtab) {
1111 link_elf_error(filename, "lost progbits");
1115 if (rl != ef->nreltab) {
1116 link_elf_error(filename, "lost reltab");
1120 if (ra != ef->nrelatab) {
1121 link_elf_error(filename, "lost relatab");
1125 if (mapbase != (vm_offset_t)ef->address + mapsize) {
1127 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1128 filename != NULL ? filename : "<none>",
1129 (u_long)mapbase, ef->address, (u_long)mapsize,
1130 (u_long)(vm_offset_t)ef->address + mapsize);
1135 /* Local intra-module relocations */
1136 error = link_elf_reloc_local(lf, false);
1140 /* Pull in dependencies */
1141 VOP_UNLOCK(nd->ni_vp);
1142 error = linker_load_dependencies(lf);
1143 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1147 /* External relocations */
1148 error = relocate_file(ef);
1152 /* Notify MD code that a module is being loaded. */
1153 error = elf_cpu_load_file(lf);
1157 #if defined(__i386__) || defined(__amd64__)
1159 error = link_elf_reloc_local(lf, true);
1164 link_elf_protect(ef);
1165 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
1169 VOP_UNLOCK(nd->ni_vp);
1170 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1173 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1174 free(hdr, M_LINKER);
1180 link_elf_unload_file(linker_file_t file)
1182 elf_file_t ef = (elf_file_t) file;
1185 /* Notify MD code that a module is being unloaded. */
1186 elf_cpu_unload_file(file);
1189 for (i = 0; i < ef->nprogtab; i++) {
1190 if (ef->progtab[i].size == 0)
1192 if (ef->progtab[i].name == NULL)
1194 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1195 dpcpu_free(ef->progtab[i].addr,
1196 ef->progtab[i].size);
1198 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1199 vnet_data_free(ef->progtab[i].addr,
1200 ef->progtab[i].size);
1204 if (ef->preloaded) {
1205 free(ef->reltab, M_LINKER);
1206 free(ef->relatab, M_LINKER);
1207 free(ef->progtab, M_LINKER);
1208 free(ef->ctftab, M_LINKER);
1209 free(ef->ctfoff, M_LINKER);
1210 free(ef->typoff, M_LINKER);
1211 if (file->pathname != NULL)
1212 preload_delete_name(file->pathname);
1216 for (i = 0; i < ef->nreltab; i++)
1217 free(ef->reltab[i].rel, M_LINKER);
1218 for (i = 0; i < ef->nrelatab; i++)
1219 free(ef->relatab[i].rela, M_LINKER);
1220 free(ef->reltab, M_LINKER);
1221 free(ef->relatab, M_LINKER);
1222 free(ef->progtab, M_LINKER);
1224 if (ef->object != NULL)
1225 vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1226 (vm_offset_t)ef->address + ptoa(ef->object->size));
1227 free(ef->e_shdr, M_LINKER);
1228 free(ef->ddbsymtab, M_LINKER);
1229 free(ef->ddbstrtab, M_LINKER);
1230 free(ef->shstrtab, M_LINKER);
1231 free(ef->ctftab, M_LINKER);
1232 free(ef->ctfoff, M_LINKER);
1233 free(ef->typoff, M_LINKER);
1237 symbol_name(elf_file_t ef, Elf_Size r_info)
1241 if (ELF_R_SYM(r_info)) {
1242 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1243 return ef->ddbstrtab + ref->st_name;
1249 findbase(elf_file_t ef, int sec)
1254 for (i = 0; i < ef->nprogtab; i++) {
1255 if (sec == ef->progtab[i].sec) {
1256 base = (Elf_Addr)ef->progtab[i].addr;
1264 relocate_file(elf_file_t ef)
1266 const Elf_Rel *rellim;
1268 const Elf_Rela *relalim;
1269 const Elf_Rela *rela;
1270 const char *symname;
1276 /* Perform relocations without addend if there are any: */
1277 for (i = 0; i < ef->nreltab; i++) {
1278 rel = ef->reltab[i].rel;
1280 link_elf_error(ef->lf.filename, "lost a reltab!");
1283 rellim = rel + ef->reltab[i].nrel;
1284 base = findbase(ef, ef->reltab[i].sec);
1286 link_elf_error(ef->lf.filename, "lost base for reltab");
1289 for ( ; rel < rellim; rel++) {
1290 symidx = ELF_R_SYM(rel->r_info);
1291 if (symidx >= ef->ddbsymcnt)
1293 sym = ef->ddbsymtab + symidx;
1294 /* Local relocs are already done */
1295 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1297 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1299 symname = symbol_name(ef, rel->r_info);
1300 printf("link_elf_obj: symbol %s undefined\n",
1307 /* Perform relocations with addend if there are any: */
1308 for (i = 0; i < ef->nrelatab; i++) {
1309 rela = ef->relatab[i].rela;
1311 link_elf_error(ef->lf.filename, "lost a relatab!");
1314 relalim = rela + ef->relatab[i].nrela;
1315 base = findbase(ef, ef->relatab[i].sec);
1317 link_elf_error(ef->lf.filename,
1318 "lost base for relatab");
1321 for ( ; rela < relalim; rela++) {
1322 symidx = ELF_R_SYM(rela->r_info);
1323 if (symidx >= ef->ddbsymcnt)
1325 sym = ef->ddbsymtab + symidx;
1326 /* Local relocs are already done */
1327 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1329 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1331 symname = symbol_name(ef, rela->r_info);
1332 printf("link_elf_obj: symbol %s undefined\n",
1340 * Only clean SHN_FBSD_CACHED for successful return. If we
1341 * modified symbol table for the object but found an
1342 * unresolved symbol, there is no reason to roll back.
1344 elf_obj_cleanup_globals_cache(ef);
1350 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1352 elf_file_t ef = (elf_file_t) lf;
1353 const Elf_Sym *symp;
1357 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1358 strp = ef->ddbstrtab + symp->st_name;
1359 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1360 *sym = (c_linker_sym_t) symp;
1368 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1369 linker_symval_t *symval)
1375 ef = (elf_file_t) lf;
1376 es = (const Elf_Sym*) sym;
1377 val = (caddr_t)es->st_value;
1378 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1379 symval->name = ef->ddbstrtab + es->st_name;
1380 val = (caddr_t)es->st_value;
1381 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1382 val = ((caddr_t (*)(void))val)();
1383 symval->value = val;
1384 symval->size = es->st_size;
1391 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1392 c_linker_sym_t *sym, long *diffp)
1394 elf_file_t ef = (elf_file_t) lf;
1395 u_long off = (uintptr_t) (void *) value;
1399 const Elf_Sym *best = NULL;
1402 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1403 if (es->st_name == 0)
1405 st_value = es->st_value;
1406 if (off >= st_value) {
1407 if (off - st_value < diff) {
1408 diff = off - st_value;
1412 } else if (off - st_value == diff) {
1421 *sym = (c_linker_sym_t) best;
1427 * Look up a linker set on an ELF system.
1430 link_elf_lookup_set(linker_file_t lf, const char *name,
1431 void ***startp, void ***stopp, int *countp)
1433 elf_file_t ef = (elf_file_t)lf;
1434 void **start, **stop;
1437 /* Relative to section number */
1438 for (i = 0; i < ef->nprogtab; i++) {
1439 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1440 strcmp(ef->progtab[i].name + 4, name) == 0) {
1441 start = (void **)ef->progtab[i].addr;
1442 stop = (void **)((char *)ef->progtab[i].addr +
1443 ef->progtab[i].size);
1444 count = stop - start;
1458 link_elf_each_function_name(linker_file_t file,
1459 int (*callback)(const char *, void *), void *opaque)
1461 elf_file_t ef = (elf_file_t)file;
1462 const Elf_Sym *symp;
1465 /* Exhaustive search */
1466 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1467 if (symp->st_value != 0 &&
1468 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1469 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1470 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1479 link_elf_each_function_nameval(linker_file_t file,
1480 linker_function_nameval_callback_t callback, void *opaque)
1482 linker_symval_t symval;
1483 elf_file_t ef = (elf_file_t)file;
1484 const Elf_Sym* symp;
1487 /* Exhaustive search */
1488 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1489 if (symp->st_value != 0 &&
1490 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1491 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1492 error = link_elf_symbol_values(file,
1493 (c_linker_sym_t)symp, &symval);
1496 error = callback(file, i, &symval, opaque);
1505 elf_obj_cleanup_globals_cache(elf_file_t ef)
1510 for (i = 0; i < ef->ddbsymcnt; i++) {
1511 sym = ef->ddbsymtab + i;
1512 if (sym->st_shndx == SHN_FBSD_CACHED) {
1513 sym->st_shndx = SHN_UNDEF;
1520 * Symbol lookup function that can be used when the symbol index is known (ie
1521 * in relocations). It uses the symbol index instead of doing a fully fledged
1522 * hash table based lookup when such is valid. For example for local symbols.
1523 * This is not only more efficient, it's also more correct. It's not always
1524 * the case that the symbol can be found through the hash table.
1527 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1529 elf_file_t ef = (elf_file_t)lf;
1534 /* Don't even try to lookup the symbol if the index is bogus. */
1535 if (symidx >= ef->ddbsymcnt) {
1540 sym = ef->ddbsymtab + symidx;
1542 /* Quick answer if there is a definition included. */
1543 if (sym->st_shndx != SHN_UNDEF) {
1544 res1 = (Elf_Addr)sym->st_value;
1545 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1546 res1 = ((Elf_Addr (*)(void))res1)();
1551 /* If we get here, then it is undefined and needs a lookup. */
1552 switch (ELF_ST_BIND(sym->st_info)) {
1554 /* Local, but undefined? huh? */
1560 /* Relative to Data or Function name */
1561 symbol = ef->ddbstrtab + sym->st_name;
1563 /* Force a lookup failure if the symbol name is bogus. */
1568 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1571 * Cache global lookups during module relocation. The failure
1572 * case is particularly expensive for callers, who must scan
1573 * through the entire globals table doing strcmp(). Cache to
1574 * avoid doing such work repeatedly.
1576 * After relocation is complete, undefined globals will be
1577 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1581 sym->st_shndx = SHN_FBSD_CACHED;
1582 sym->st_value = res1;
1585 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1598 link_elf_fix_link_set(elf_file_t ef)
1600 static const char startn[] = "__start_";
1601 static const char stopn[] = "__stop_";
1603 const char *sym_name, *linkset_name;
1604 Elf_Addr startp, stopp;
1609 for (symidx = 1 /* zero entry is special */;
1610 symidx < ef->ddbsymcnt; symidx++) {
1611 sym = ef->ddbsymtab + symidx;
1612 if (sym->st_shndx != SHN_UNDEF)
1615 sym_name = ef->ddbstrtab + sym->st_name;
1616 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1618 linkset_name = sym_name + sizeof(startn) - 1;
1620 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1622 linkset_name = sym_name + sizeof(stopn) - 1;
1627 for (i = 0; i < ef->nprogtab; i++) {
1628 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1629 startp = (Elf_Addr)ef->progtab[i].addr;
1630 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1634 if (i == ef->nprogtab)
1637 sym->st_value = start ? startp : stopp;
1643 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1645 elf_file_t ef = (elf_file_t)lf;
1646 const Elf_Rel *rellim;
1648 const Elf_Rela *relalim;
1649 const Elf_Rela *rela;
1655 link_elf_fix_link_set(ef);
1657 /* Perform relocations without addend if there are any: */
1658 for (i = 0; i < ef->nreltab; i++) {
1659 rel = ef->reltab[i].rel;
1661 link_elf_error(ef->lf.filename, "lost a reltab");
1664 rellim = rel + ef->reltab[i].nrel;
1665 base = findbase(ef, ef->reltab[i].sec);
1667 link_elf_error(ef->lf.filename, "lost base for reltab");
1670 for ( ; rel < rellim; rel++) {
1671 symidx = ELF_R_SYM(rel->r_info);
1672 if (symidx >= ef->ddbsymcnt)
1674 sym = ef->ddbsymtab + symidx;
1675 /* Only do local relocs */
1676 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1678 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1679 elf_is_ifunc_reloc(rel->r_info)) == ifuncs)
1680 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1685 /* Perform relocations with addend if there are any: */
1686 for (i = 0; i < ef->nrelatab; i++) {
1687 rela = ef->relatab[i].rela;
1689 link_elf_error(ef->lf.filename, "lost a relatab!");
1692 relalim = rela + ef->relatab[i].nrela;
1693 base = findbase(ef, ef->relatab[i].sec);
1695 link_elf_error(ef->lf.filename, "lost base for reltab");
1698 for ( ; rela < relalim; rela++) {
1699 symidx = ELF_R_SYM(rela->r_info);
1700 if (symidx >= ef->ddbsymcnt)
1702 sym = ef->ddbsymtab + symidx;
1703 /* Only do local relocs */
1704 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1706 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1707 elf_is_ifunc_reloc(rela->r_info)) == ifuncs)
1708 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1716 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1718 elf_file_t ef = (elf_file_t)lf;
1720 *symtab = ef->ddbsymtab;
1722 if (*symtab == NULL)
1725 return (ef->ddbsymcnt);
1729 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1731 elf_file_t ef = (elf_file_t)lf;
1733 *strtab = ef->ddbstrtab;
1735 if (*strtab == NULL)
1738 return (ef->ddbstrcnt);