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/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/mount.h>
43 #include <sys/namei.h>
44 #include <sys/fcntl.h>
45 #include <sys/vnode.h>
46 #include <sys/linker.h>
48 #include <machine/elf.h>
52 #include <security/mac/mac_framework.h>
55 #include <vm/vm_param.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_extern.h>
60 #include <vm/vm_map.h>
62 #include <sys/link_elf.h>
68 #include "linker_if.h"
73 int flags; /* Section flags. */
74 int sec; /* Original section number. */
91 typedef struct elf_file {
92 struct linker_file lf; /* Common fields */
95 caddr_t address; /* Relocation address */
96 vm_object_t object; /* VM object to hold file pages */
102 Elf_relaent *relatab;
108 Elf_Sym *ddbsymtab; /* The symbol table we are using */
109 long ddbsymcnt; /* Number of symbols */
110 caddr_t ddbstrtab; /* String table */
111 long ddbstrcnt; /* number of bytes in string table */
113 caddr_t shstrtab; /* Section name string table */
114 long shstrcnt; /* number of bytes in string table */
116 caddr_t ctftab; /* CTF table */
117 long ctfcnt; /* number of bytes in CTF table */
118 caddr_t ctfoff; /* CTF offset table */
119 caddr_t typoff; /* Type offset table */
120 long typlen; /* Number of type entries. */
124 #include <kern/kern_ctf.c>
126 static int link_elf_link_preload(linker_class_t cls,
127 const char *, linker_file_t *);
128 static int link_elf_link_preload_finish(linker_file_t);
129 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *);
130 static int link_elf_lookup_symbol(linker_file_t, const char *,
132 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t,
134 static int link_elf_search_symbol(linker_file_t, caddr_t value,
135 c_linker_sym_t *sym, long *diffp);
137 static void link_elf_unload_file(linker_file_t);
138 static int link_elf_lookup_set(linker_file_t, const char *,
139 void ***, void ***, int *);
140 static int link_elf_each_function_name(linker_file_t,
141 int (*)(const char *, void *), void *);
142 static int link_elf_each_function_nameval(linker_file_t,
143 linker_function_nameval_callback_t,
145 static int link_elf_reloc_local(linker_file_t, bool);
146 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
147 static long link_elf_strtab_get(linker_file_t, caddr_t *);
149 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps,
152 static kobj_method_t link_elf_methods[] = {
153 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
154 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
155 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
156 KOBJMETHOD(linker_unload, link_elf_unload_file),
157 KOBJMETHOD(linker_load_file, link_elf_load_file),
158 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
159 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
160 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
161 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
162 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
163 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
164 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
165 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
169 static struct linker_class link_elf_class = {
170 #if ELF_TARG_CLASS == ELFCLASS32
175 link_elf_methods, sizeof(struct elf_file)
178 static int relocate_file(elf_file_t ef);
179 static void elf_obj_cleanup_globals_cache(elf_file_t);
182 link_elf_error(const char *filename, const char *s)
184 if (filename == NULL)
185 printf("kldload: %s\n", s);
187 printf("kldload: %s: %s\n", filename, s);
191 link_elf_init(void *arg)
194 linker_add_class(&link_elf_class);
197 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, NULL);
200 link_elf_protect_range(elf_file_t ef, vm_offset_t start, vm_offset_t end,
205 KASSERT(start <= end && start >= (vm_offset_t)ef->address &&
206 end <= round_page((vm_offset_t)ef->address + ef->lf.size),
207 ("link_elf_protect_range: invalid range %#jx-%#jx",
208 (uintmax_t)start, (uintmax_t)end));
212 error = vm_map_protect(kernel_map, start, end, prot, FALSE);
213 KASSERT(error == KERN_SUCCESS,
214 ("link_elf_protect_range: vm_map_protect() returned %d", error));
218 * Restrict permissions on linker file memory based on section flags.
219 * Sections need not be page-aligned, so overlap within a page is possible.
222 link_elf_protect(elf_file_t ef)
224 vm_offset_t end, segend, segstart, start;
225 vm_prot_t gapprot, prot, segprot;
229 * If the file was preloaded, the last page may contain other preloaded
230 * data which may need to be writeable. ELF files are always
231 * page-aligned, but other preloaded data, such as entropy or CPU
232 * microcode may be loaded with a smaller alignment.
234 gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ;
236 start = end = (vm_offset_t)ef->address;
238 for (i = 0; i < ef->nprogtab; i++) {
240 * VNET and DPCPU sections have their memory allocated by their
241 * respective subsystems.
243 if (ef->progtab[i].name != NULL && (
245 strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 ||
247 strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0))
250 segstart = trunc_page((vm_offset_t)ef->progtab[i].addr);
251 segend = round_page((vm_offset_t)ef->progtab[i].addr +
252 ef->progtab[i].size);
253 segprot = VM_PROT_READ;
254 if ((ef->progtab[i].flags & SHF_WRITE) != 0)
255 segprot |= VM_PROT_WRITE;
256 if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0)
257 segprot |= VM_PROT_EXECUTE;
259 if (end <= segstart) {
261 * Case 1: there is no overlap between the previous
262 * segment and this one. Apply protections to the
263 * previous segment, and protect the gap between the
264 * previous and current segments, if any.
266 link_elf_protect_range(ef, start, end, prot);
267 link_elf_protect_range(ef, end, segstart, gapprot);
272 } else if (start < segstart && end == segend) {
274 * Case 2: the current segment is a subrange of the
275 * previous segment. Apply protections to the
276 * non-overlapping portion of the previous segment.
278 link_elf_protect_range(ef, start, segstart, prot);
282 } else if (end < segend) {
284 * Case 3: there is partial overlap between the previous
285 * and current segments. Apply protections to the
286 * non-overlapping portion of the previous segment, and
287 * then the overlap, which must use the union of the two
288 * segments' protections.
290 link_elf_protect_range(ef, start, segstart, prot);
291 link_elf_protect_range(ef, segstart, end,
298 * Case 4: the two segments reside in the same page.
305 * Fix up the last unprotected segment and trailing data.
307 link_elf_protect_range(ef, start, end, prot);
308 link_elf_protect_range(ef, end,
309 round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
313 link_elf_link_preload(linker_class_t cls, const char *filename,
314 linker_file_t *result)
319 void *modptr, *baseptr, *sizeptr;
324 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
326 /* Look to see if we have the file preloaded */
327 modptr = preload_search_by_name(filename);
331 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
332 baseptr = preload_search_info(modptr, MODINFO_ADDR);
333 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
334 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
336 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
338 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
339 " obj module") != 0 &&
340 strcmp(type, "elf obj module") != 0)) {
343 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
347 lf = linker_make_file(filename, &link_elf_class);
353 ef->address = *(caddr_t *)baseptr;
354 lf->address = *(caddr_t *)baseptr;
355 lf->size = *(size_t *)sizeptr;
357 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
358 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
359 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
360 hdr->e_version != EV_CURRENT ||
361 hdr->e_type != ET_REL ||
362 hdr->e_machine != ELF_TARG_MACH) {
368 /* Scan the section header for information and table sizing. */
371 for (i = 0; i < hdr->e_shnum; i++) {
372 switch (shdr[i].sh_type) {
376 case SHT_X86_64_UNWIND:
378 /* Ignore sections not loaded by the loader. */
379 if (shdr[i].sh_addr == 0)
385 symstrindex = shdr[i].sh_link;
389 * Ignore relocation tables for sections not
390 * loaded by the loader.
392 if (shdr[shdr[i].sh_info].sh_addr == 0)
397 if (shdr[shdr[i].sh_info].sh_addr == 0)
404 shstrindex = hdr->e_shstrndx;
405 if (ef->nprogtab == 0 || symstrindex < 0 ||
406 symstrindex >= hdr->e_shnum ||
407 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
408 shstrindex >= hdr->e_shnum ||
409 shdr[shstrindex].sh_type != SHT_STRTAB) {
410 printf("%s: bad/missing section headers\n", filename);
415 /* Allocate space for tracking the load chunks */
416 if (ef->nprogtab != 0)
417 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
418 M_LINKER, M_WAITOK | M_ZERO);
419 if (ef->nreltab != 0)
420 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
421 M_LINKER, M_WAITOK | M_ZERO);
422 if (ef->nrelatab != 0)
423 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
424 M_LINKER, M_WAITOK | M_ZERO);
425 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
426 (ef->nreltab != 0 && ef->reltab == NULL) ||
427 (ef->nrelatab != 0 && ef->relatab == NULL)) {
432 /* XXX, relocate the sh_addr fields saved by the loader. */
434 for (i = 0; i < hdr->e_shnum; i++) {
435 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
436 off = shdr[i].sh_addr;
438 for (i = 0; i < hdr->e_shnum; i++) {
439 if (shdr[i].sh_addr != 0)
440 shdr[i].sh_addr = shdr[i].sh_addr - off +
441 (Elf_Addr)ef->address;
444 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
445 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
446 ef->ddbstrcnt = shdr[symstrindex].sh_size;
447 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
448 ef->shstrcnt = shdr[shstrindex].sh_size;
449 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
451 /* Now fill out progtab and the relocation tables. */
455 for (i = 0; i < hdr->e_shnum; i++) {
456 switch (shdr[i].sh_type) {
460 case SHT_X86_64_UNWIND:
462 if (shdr[i].sh_addr == 0)
464 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
465 if (shdr[i].sh_type == SHT_PROGBITS)
466 ef->progtab[pb].name = "<<PROGBITS>>";
468 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
469 ef->progtab[pb].name = "<<UNWIND>>";
472 ef->progtab[pb].name = "<<NOBITS>>";
473 ef->progtab[pb].size = shdr[i].sh_size;
474 ef->progtab[pb].flags = shdr[i].sh_flags;
475 ef->progtab[pb].sec = i;
476 if (ef->shstrtab && shdr[i].sh_name != 0)
477 ef->progtab[pb].name =
478 ef->shstrtab + shdr[i].sh_name;
479 if (ef->progtab[pb].name != NULL &&
480 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
483 dpcpu = dpcpu_alloc(shdr[i].sh_size);
485 printf("%s: pcpu module space is out "
486 "of space; cannot allocate %#jx "
487 "for %s\n", __func__,
488 (uintmax_t)shdr[i].sh_size,
493 memcpy(dpcpu, ef->progtab[pb].addr,
494 ef->progtab[pb].size);
495 dpcpu_copy(dpcpu, shdr[i].sh_size);
496 ef->progtab[pb].addr = dpcpu;
498 } else if (ef->progtab[pb].name != NULL &&
499 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
502 vnet_data = vnet_data_alloc(shdr[i].sh_size);
503 if (vnet_data == NULL) {
504 printf("%s: vnet module space is out "
505 "of space; cannot allocate %#jx "
506 "for %s\n", __func__,
507 (uintmax_t)shdr[i].sh_size,
512 memcpy(vnet_data, ef->progtab[pb].addr,
513 ef->progtab[pb].size);
514 vnet_data_copy(vnet_data, shdr[i].sh_size);
515 ef->progtab[pb].addr = vnet_data;
517 } else if (ef->progtab[pb].name != NULL &&
518 !strcmp(ef->progtab[pb].name, ".ctors")) {
519 lf->ctors_addr = ef->progtab[pb].addr;
520 lf->ctors_size = shdr[i].sh_size;
523 /* Update all symbol values with the offset. */
524 for (j = 0; j < ef->ddbsymcnt; j++) {
525 es = &ef->ddbsymtab[j];
526 if (es->st_shndx != i)
528 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
533 if (shdr[shdr[i].sh_info].sh_addr == 0)
535 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
536 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
537 ef->reltab[rl].sec = shdr[i].sh_info;
541 if (shdr[shdr[i].sh_info].sh_addr == 0)
543 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
544 ef->relatab[ra].nrela =
545 shdr[i].sh_size / sizeof(Elf_Rela);
546 ef->relatab[ra].sec = shdr[i].sh_info;
551 if (pb != ef->nprogtab) {
552 printf("%s: lost progbits\n", filename);
556 if (rl != ef->nreltab) {
557 printf("%s: lost reltab\n", filename);
561 if (ra != ef->nrelatab) {
562 printf("%s: lost relatab\n", filename);
567 /* Local intra-module relocations */
568 error = link_elf_reloc_local(lf, false);
575 /* preload not done this way */
576 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
581 link_elf_invoke_ctors(caddr_t addr, size_t size)
586 if (addr == NULL || size == 0)
588 cnt = size / sizeof(*ctor);
590 for (i = 0; i < cnt; i++) {
597 link_elf_link_preload_finish(linker_file_t lf)
603 error = relocate_file(ef);
607 /* Notify MD code that a module is being loaded. */
608 error = elf_cpu_load_file(lf);
612 #if defined(__i386__) || defined(__amd64__)
614 error = link_elf_reloc_local(lf, true);
620 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
625 link_elf_load_file(linker_class_t cls, const char *filename,
626 linker_file_t *result)
628 struct nameidata *nd;
629 struct thread *td = curthread; /* XXX */
653 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
654 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
656 error = vn_open(nd, &flags, 0, NULL);
661 NDFREE(nd, NDF_ONLY_PNBUF);
662 if (nd->ni_vp->v_type != VREG) {
667 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
673 /* Read the elf header from the file. */
674 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
675 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
676 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
690 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
691 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
692 link_elf_error(filename, "Unsupported file layout");
696 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
697 || hdr->e_version != EV_CURRENT) {
698 link_elf_error(filename, "Unsupported file version");
702 if (hdr->e_type != ET_REL) {
706 if (hdr->e_machine != ELF_TARG_MACH) {
707 link_elf_error(filename, "Unsupported machine");
712 lf = linker_make_file(filename, &link_elf_class);
717 ef = (elf_file_t) lf;
723 /* Allocate and read in the section header */
724 nbytes = hdr->e_shnum * hdr->e_shentsize;
725 if (nbytes == 0 || hdr->e_shoff == 0 ||
726 hdr->e_shentsize != sizeof(Elf_Shdr)) {
730 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
732 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
733 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
742 /* Scan the section header for information and table sizing. */
746 for (i = 0; i < hdr->e_shnum; i++) {
747 if (shdr[i].sh_size == 0)
749 switch (shdr[i].sh_type) {
753 case SHT_X86_64_UNWIND:
755 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
762 symstrindex = shdr[i].sh_link;
766 * Ignore relocation tables for unallocated
769 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
774 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
782 if (ef->nprogtab == 0) {
783 link_elf_error(filename, "file has no contents");
788 /* Only allow one symbol table for now */
789 link_elf_error(filename,
790 "file must have exactly one symbol table");
794 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
795 shdr[symstrindex].sh_type != SHT_STRTAB) {
796 link_elf_error(filename, "file has invalid symbol strings");
801 /* Allocate space for tracking the load chunks */
802 if (ef->nprogtab != 0)
803 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
804 M_LINKER, M_WAITOK | M_ZERO);
805 if (ef->nreltab != 0)
806 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
807 M_LINKER, M_WAITOK | M_ZERO);
808 if (ef->nrelatab != 0)
809 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
810 M_LINKER, M_WAITOK | M_ZERO);
812 if (symtabindex == -1) {
813 link_elf_error(filename, "lost symbol table index");
817 /* Allocate space for and load the symbol table */
818 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
819 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
820 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
821 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
822 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
831 /* Allocate space for and load the symbol strings */
832 ef->ddbstrcnt = shdr[symstrindex].sh_size;
833 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
834 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
835 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
836 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
845 /* Do we have a string table for the section names? */
847 if (hdr->e_shstrndx != 0 &&
848 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
849 shstrindex = hdr->e_shstrndx;
850 ef->shstrcnt = shdr[shstrindex].sh_size;
851 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
853 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
854 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
855 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
865 /* Size up code/data(progbits) and bss(nobits). */
867 for (i = 0; i < hdr->e_shnum; i++) {
868 if (shdr[i].sh_size == 0)
870 switch (shdr[i].sh_type) {
874 case SHT_X86_64_UNWIND:
876 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
878 alignmask = shdr[i].sh_addralign - 1;
879 mapsize += alignmask;
880 mapsize &= ~alignmask;
881 mapsize += shdr[i].sh_size;
887 * We know how much space we need for the text/data/bss/etc.
888 * This stuff needs to be in a single chunk so that profiling etc
889 * can get the bounds and gdb can associate offsets with modules
891 ef->object = vm_object_allocate(OBJT_PHYS, atop(round_page(mapsize)));
892 if (ef->object == NULL) {
898 * In order to satisfy amd64's architectural requirements on the
899 * location of code and data in the kernel's address space, request a
900 * mapping that is above the kernel.
902 * Protections will be restricted once relocations are applied.
907 mapbase = VM_MIN_KERNEL_ADDRESS;
909 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
910 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
912 if (error != KERN_SUCCESS) {
913 vm_object_deallocate(ef->object);
920 error = vm_map_wire(kernel_map, mapbase,
921 mapbase + round_page(mapsize),
922 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
923 if (error != KERN_SUCCESS) {
928 /* Inform the kld system about the situation */
929 lf->address = ef->address = (caddr_t)mapbase;
933 * Now load code/data(progbits), zero bss(nobits), allocate space for
940 for (i = 0; i < hdr->e_shnum; i++) {
941 if (shdr[i].sh_size == 0)
943 switch (shdr[i].sh_type) {
947 case SHT_X86_64_UNWIND:
949 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
951 alignmask = shdr[i].sh_addralign - 1;
952 mapbase += alignmask;
953 mapbase &= ~alignmask;
954 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
955 ef->progtab[pb].name =
956 ef->shstrtab + shdr[i].sh_name;
957 if (!strcmp(ef->progtab[pb].name, ".ctors")) {
958 lf->ctors_addr = (caddr_t)mapbase;
959 lf->ctors_size = shdr[i].sh_size;
961 } else if (shdr[i].sh_type == SHT_PROGBITS)
962 ef->progtab[pb].name = "<<PROGBITS>>";
964 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
965 ef->progtab[pb].name = "<<UNWIND>>";
968 ef->progtab[pb].name = "<<NOBITS>>";
969 if (ef->progtab[pb].name != NULL &&
970 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
971 ef->progtab[pb].addr =
972 dpcpu_alloc(shdr[i].sh_size);
973 if (ef->progtab[pb].addr == NULL) {
974 printf("%s: pcpu module space is out "
975 "of space; cannot allocate %#jx "
976 "for %s\n", __func__,
977 (uintmax_t)shdr[i].sh_size,
982 else if (ef->progtab[pb].name != NULL &&
983 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
984 ef->progtab[pb].addr =
985 vnet_data_alloc(shdr[i].sh_size);
986 if (ef->progtab[pb].addr == NULL) {
987 printf("%s: vnet module space is out "
988 "of space; cannot allocate %#jx "
989 "for %s\n", __func__,
990 (uintmax_t)shdr[i].sh_size,
996 ef->progtab[pb].addr =
997 (void *)(uintptr_t)mapbase;
998 if (ef->progtab[pb].addr == NULL) {
1002 ef->progtab[pb].size = shdr[i].sh_size;
1003 ef->progtab[pb].flags = shdr[i].sh_flags;
1004 ef->progtab[pb].sec = i;
1005 if (shdr[i].sh_type == SHT_PROGBITS
1007 || shdr[i].sh_type == SHT_X86_64_UNWIND
1010 error = vn_rdwr(UIO_READ, nd->ni_vp,
1011 ef->progtab[pb].addr,
1012 shdr[i].sh_size, shdr[i].sh_offset,
1013 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1014 NOCRED, &resid, td);
1021 /* Initialize the per-cpu or vnet area. */
1022 if (ef->progtab[pb].addr != (void *)mapbase &&
1023 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1024 dpcpu_copy(ef->progtab[pb].addr,
1027 else if (ef->progtab[pb].addr !=
1029 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
1030 vnet_data_copy(ef->progtab[pb].addr,
1034 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1036 /* Update all symbol values with the offset. */
1037 for (j = 0; j < ef->ddbsymcnt; j++) {
1038 es = &ef->ddbsymtab[j];
1039 if (es->st_shndx != i)
1041 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1043 mapbase += shdr[i].sh_size;
1047 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1049 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1051 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1052 ef->reltab[rl].sec = shdr[i].sh_info;
1053 error = vn_rdwr(UIO_READ, nd->ni_vp,
1054 (void *)ef->reltab[rl].rel,
1055 shdr[i].sh_size, shdr[i].sh_offset,
1056 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1067 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1069 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1071 ef->relatab[ra].nrela =
1072 shdr[i].sh_size / sizeof(Elf_Rela);
1073 ef->relatab[ra].sec = shdr[i].sh_info;
1074 error = vn_rdwr(UIO_READ, nd->ni_vp,
1075 (void *)ef->relatab[ra].rela,
1076 shdr[i].sh_size, shdr[i].sh_offset,
1077 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1089 if (pb != ef->nprogtab) {
1090 link_elf_error(filename, "lost progbits");
1094 if (rl != ef->nreltab) {
1095 link_elf_error(filename, "lost reltab");
1099 if (ra != ef->nrelatab) {
1100 link_elf_error(filename, "lost relatab");
1104 if (mapbase != (vm_offset_t)ef->address + mapsize) {
1106 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1107 filename != NULL ? filename : "<none>",
1108 (u_long)mapbase, ef->address, (u_long)mapsize,
1109 (u_long)(vm_offset_t)ef->address + mapsize);
1114 /* Local intra-module relocations */
1115 error = link_elf_reloc_local(lf, false);
1119 /* Pull in dependencies */
1120 VOP_UNLOCK(nd->ni_vp, 0);
1121 error = linker_load_dependencies(lf);
1122 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1126 /* External relocations */
1127 error = relocate_file(ef);
1131 /* Notify MD code that a module is being loaded. */
1132 error = elf_cpu_load_file(lf);
1136 #if defined(__i386__) || defined(__amd64__)
1138 error = link_elf_reloc_local(lf, true);
1143 link_elf_protect(ef);
1144 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
1148 VOP_UNLOCK(nd->ni_vp, 0);
1149 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1152 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1153 free(hdr, M_LINKER);
1159 link_elf_unload_file(linker_file_t file)
1161 elf_file_t ef = (elf_file_t) file;
1164 /* Notify MD code that a module is being unloaded. */
1165 elf_cpu_unload_file(file);
1168 for (i = 0; i < ef->nprogtab; i++) {
1169 if (ef->progtab[i].size == 0)
1171 if (ef->progtab[i].name == NULL)
1173 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1174 dpcpu_free(ef->progtab[i].addr,
1175 ef->progtab[i].size);
1177 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1178 vnet_data_free(ef->progtab[i].addr,
1179 ef->progtab[i].size);
1183 if (ef->preloaded) {
1184 free(ef->reltab, M_LINKER);
1185 free(ef->relatab, M_LINKER);
1186 free(ef->progtab, M_LINKER);
1187 free(ef->ctftab, M_LINKER);
1188 free(ef->ctfoff, M_LINKER);
1189 free(ef->typoff, M_LINKER);
1190 if (file->pathname != NULL)
1191 preload_delete_name(file->pathname);
1195 for (i = 0; i < ef->nreltab; i++)
1196 free(ef->reltab[i].rel, M_LINKER);
1197 for (i = 0; i < ef->nrelatab; i++)
1198 free(ef->relatab[i].rela, M_LINKER);
1199 free(ef->reltab, M_LINKER);
1200 free(ef->relatab, M_LINKER);
1201 free(ef->progtab, M_LINKER);
1203 if (ef->object != NULL)
1204 vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1205 (vm_offset_t)ef->address + ptoa(ef->object->size));
1206 free(ef->e_shdr, M_LINKER);
1207 free(ef->ddbsymtab, M_LINKER);
1208 free(ef->ddbstrtab, M_LINKER);
1209 free(ef->shstrtab, M_LINKER);
1210 free(ef->ctftab, M_LINKER);
1211 free(ef->ctfoff, M_LINKER);
1212 free(ef->typoff, M_LINKER);
1216 symbol_name(elf_file_t ef, Elf_Size r_info)
1220 if (ELF_R_SYM(r_info)) {
1221 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1222 return ef->ddbstrtab + ref->st_name;
1228 findbase(elf_file_t ef, int sec)
1233 for (i = 0; i < ef->nprogtab; i++) {
1234 if (sec == ef->progtab[i].sec) {
1235 base = (Elf_Addr)ef->progtab[i].addr;
1243 relocate_file(elf_file_t ef)
1245 const Elf_Rel *rellim;
1247 const Elf_Rela *relalim;
1248 const Elf_Rela *rela;
1249 const char *symname;
1256 /* Perform relocations without addend if there are any: */
1257 for (i = 0; i < ef->nreltab; i++) {
1258 rel = ef->reltab[i].rel;
1260 link_elf_error(ef->lf.filename, "lost a reltab!");
1263 rellim = rel + ef->reltab[i].nrel;
1264 base = findbase(ef, ef->reltab[i].sec);
1266 link_elf_error(ef->lf.filename, "lost base for reltab");
1269 for ( ; rel < rellim; rel++) {
1270 symidx = ELF_R_SYM(rel->r_info);
1271 if (symidx >= ef->ddbsymcnt)
1273 sym = ef->ddbsymtab + symidx;
1274 /* Local relocs are already done */
1275 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1277 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1279 symname = symbol_name(ef, rel->r_info);
1280 printf("link_elf_obj: symbol %s undefined\n",
1287 /* Perform relocations with addend if there are any: */
1288 for (i = 0; i < ef->nrelatab; i++) {
1289 rela = ef->relatab[i].rela;
1291 link_elf_error(ef->lf.filename, "lost a relatab!");
1294 relalim = rela + ef->relatab[i].nrela;
1295 base = findbase(ef, ef->relatab[i].sec);
1297 link_elf_error(ef->lf.filename,
1298 "lost base for relatab");
1301 for ( ; rela < relalim; rela++) {
1302 symidx = ELF_R_SYM(rela->r_info);
1303 if (symidx >= ef->ddbsymcnt)
1305 sym = ef->ddbsymtab + symidx;
1306 /* Local relocs are already done */
1307 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1309 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1311 symname = symbol_name(ef, rela->r_info);
1312 printf("link_elf_obj: symbol %s undefined\n",
1320 * Only clean SHN_FBSD_CACHED for successful return. If we
1321 * modified symbol table for the object but found an
1322 * unresolved symbol, there is no reason to roll back.
1324 elf_obj_cleanup_globals_cache(ef);
1330 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1332 elf_file_t ef = (elf_file_t) lf;
1333 const Elf_Sym *symp;
1337 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1338 strp = ef->ddbstrtab + symp->st_name;
1339 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1340 *sym = (c_linker_sym_t) symp;
1348 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1349 linker_symval_t *symval)
1355 ef = (elf_file_t) lf;
1356 es = (const Elf_Sym*) sym;
1357 val = (caddr_t)es->st_value;
1358 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1359 symval->name = ef->ddbstrtab + es->st_name;
1360 val = (caddr_t)es->st_value;
1361 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1362 val = ((caddr_t (*)(void))val)();
1363 symval->value = val;
1364 symval->size = es->st_size;
1371 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1372 c_linker_sym_t *sym, long *diffp)
1374 elf_file_t ef = (elf_file_t) lf;
1375 u_long off = (uintptr_t) (void *) value;
1379 const Elf_Sym *best = NULL;
1382 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1383 if (es->st_name == 0)
1385 st_value = es->st_value;
1386 if (off >= st_value) {
1387 if (off - st_value < diff) {
1388 diff = off - st_value;
1392 } else if (off - st_value == diff) {
1401 *sym = (c_linker_sym_t) best;
1407 * Look up a linker set on an ELF system.
1410 link_elf_lookup_set(linker_file_t lf, const char *name,
1411 void ***startp, void ***stopp, int *countp)
1413 elf_file_t ef = (elf_file_t)lf;
1414 void **start, **stop;
1417 /* Relative to section number */
1418 for (i = 0; i < ef->nprogtab; i++) {
1419 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1420 strcmp(ef->progtab[i].name + 4, name) == 0) {
1421 start = (void **)ef->progtab[i].addr;
1422 stop = (void **)((char *)ef->progtab[i].addr +
1423 ef->progtab[i].size);
1424 count = stop - start;
1438 link_elf_each_function_name(linker_file_t file,
1439 int (*callback)(const char *, void *), void *opaque)
1441 elf_file_t ef = (elf_file_t)file;
1442 const Elf_Sym *symp;
1445 /* Exhaustive search */
1446 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1447 if (symp->st_value != 0 &&
1448 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1449 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1450 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1459 link_elf_each_function_nameval(linker_file_t file,
1460 linker_function_nameval_callback_t callback, void *opaque)
1462 linker_symval_t symval;
1463 elf_file_t ef = (elf_file_t)file;
1464 const Elf_Sym* symp;
1467 /* Exhaustive search */
1468 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1469 if (symp->st_value != 0 &&
1470 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1471 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1472 error = link_elf_symbol_values(file,
1473 (c_linker_sym_t)symp, &symval);
1476 error = callback(file, i, &symval, opaque);
1485 elf_obj_cleanup_globals_cache(elf_file_t ef)
1490 for (i = 0; i < ef->ddbsymcnt; i++) {
1491 sym = ef->ddbsymtab + i;
1492 if (sym->st_shndx == SHN_FBSD_CACHED) {
1493 sym->st_shndx = SHN_UNDEF;
1500 * Symbol lookup function that can be used when the symbol index is known (ie
1501 * in relocations). It uses the symbol index instead of doing a fully fledged
1502 * hash table based lookup when such is valid. For example for local symbols.
1503 * This is not only more efficient, it's also more correct. It's not always
1504 * the case that the symbol can be found through the hash table.
1507 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1509 elf_file_t ef = (elf_file_t)lf;
1514 /* Don't even try to lookup the symbol if the index is bogus. */
1515 if (symidx >= ef->ddbsymcnt) {
1520 sym = ef->ddbsymtab + symidx;
1522 /* Quick answer if there is a definition included. */
1523 if (sym->st_shndx != SHN_UNDEF) {
1524 res1 = (Elf_Addr)sym->st_value;
1525 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1526 res1 = ((Elf_Addr (*)(void))res1)();
1531 /* If we get here, then it is undefined and needs a lookup. */
1532 switch (ELF_ST_BIND(sym->st_info)) {
1534 /* Local, but undefined? huh? */
1540 /* Relative to Data or Function name */
1541 symbol = ef->ddbstrtab + sym->st_name;
1543 /* Force a lookup failure if the symbol name is bogus. */
1548 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1551 * Cache global lookups during module relocation. The failure
1552 * case is particularly expensive for callers, who must scan
1553 * through the entire globals table doing strcmp(). Cache to
1554 * avoid doing such work repeatedly.
1556 * After relocation is complete, undefined globals will be
1557 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1561 sym->st_shndx = SHN_FBSD_CACHED;
1562 sym->st_value = res1;
1565 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1578 link_elf_fix_link_set(elf_file_t ef)
1580 static const char startn[] = "__start_";
1581 static const char stopn[] = "__stop_";
1583 const char *sym_name, *linkset_name;
1584 Elf_Addr startp, stopp;
1589 for (symidx = 1 /* zero entry is special */;
1590 symidx < ef->ddbsymcnt; symidx++) {
1591 sym = ef->ddbsymtab + symidx;
1592 if (sym->st_shndx != SHN_UNDEF)
1595 sym_name = ef->ddbstrtab + sym->st_name;
1596 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1598 linkset_name = sym_name + sizeof(startn) - 1;
1600 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1602 linkset_name = sym_name + sizeof(stopn) - 1;
1607 for (i = 0; i < ef->nprogtab; i++) {
1608 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1609 startp = (Elf_Addr)ef->progtab[i].addr;
1610 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1614 if (i == ef->nprogtab)
1617 sym->st_value = start ? startp : stopp;
1623 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1625 elf_file_t ef = (elf_file_t)lf;
1626 const Elf_Rel *rellim;
1628 const Elf_Rela *relalim;
1629 const Elf_Rela *rela;
1635 link_elf_fix_link_set(ef);
1637 /* Perform relocations without addend if there are any: */
1638 for (i = 0; i < ef->nreltab; i++) {
1639 rel = ef->reltab[i].rel;
1641 link_elf_error(ef->lf.filename, "lost a reltab");
1644 rellim = rel + ef->reltab[i].nrel;
1645 base = findbase(ef, ef->reltab[i].sec);
1647 link_elf_error(ef->lf.filename, "lost base for reltab");
1650 for ( ; rel < rellim; rel++) {
1651 symidx = ELF_R_SYM(rel->r_info);
1652 if (symidx >= ef->ddbsymcnt)
1654 sym = ef->ddbsymtab + symidx;
1655 /* Only do local relocs */
1656 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1658 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1659 elf_is_ifunc_reloc(rel->r_info)) == ifuncs)
1660 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1665 /* Perform relocations with addend if there are any: */
1666 for (i = 0; i < ef->nrelatab; i++) {
1667 rela = ef->relatab[i].rela;
1669 link_elf_error(ef->lf.filename, "lost a relatab!");
1672 relalim = rela + ef->relatab[i].nrela;
1673 base = findbase(ef, ef->relatab[i].sec);
1675 link_elf_error(ef->lf.filename, "lost base for reltab");
1678 for ( ; rela < relalim; rela++) {
1679 symidx = ELF_R_SYM(rela->r_info);
1680 if (symidx >= ef->ddbsymcnt)
1682 sym = ef->ddbsymtab + symidx;
1683 /* Only do local relocs */
1684 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1686 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1687 elf_is_ifunc_reloc(rela->r_info)) == ifuncs)
1688 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1696 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1698 elf_file_t ef = (elf_file_t)lf;
1700 *symtab = ef->ddbsymtab;
1702 if (*symtab == NULL)
1705 return (ef->ddbsymcnt);
1709 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1711 elf_file_t ef = (elf_file_t)lf;
1713 *strtab = ef->ddbstrtab;
1715 if (*strtab == NULL)
1718 return (ef->ddbstrcnt);