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>
71 #include "linker_if.h"
76 int flags; /* Section flags. */
77 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_symbol_values(linker_file_t, c_linker_sym_t,
137 static int link_elf_search_symbol(linker_file_t, caddr_t value,
138 c_linker_sym_t *sym, long *diffp);
140 static void link_elf_unload_file(linker_file_t);
141 static int link_elf_lookup_set(linker_file_t, const char *,
142 void ***, void ***, int *);
143 static int link_elf_each_function_name(linker_file_t,
144 int (*)(const char *, void *), void *);
145 static int link_elf_each_function_nameval(linker_file_t,
146 linker_function_nameval_callback_t,
148 static int link_elf_reloc_local(linker_file_t, bool);
149 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
150 static long link_elf_strtab_get(linker_file_t, caddr_t *);
152 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps,
155 static kobj_method_t link_elf_methods[] = {
156 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
157 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
158 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
159 KOBJMETHOD(linker_unload, link_elf_unload_file),
160 KOBJMETHOD(linker_load_file, link_elf_load_file),
161 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
162 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
163 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
164 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
165 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
166 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
167 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
168 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
172 static struct linker_class link_elf_class = {
173 #if ELF_TARG_CLASS == ELFCLASS32
178 link_elf_methods, sizeof(struct elf_file)
181 static int relocate_file(elf_file_t ef);
182 static void elf_obj_cleanup_globals_cache(elf_file_t);
185 link_elf_error(const char *filename, const char *s)
187 if (filename == NULL)
188 printf("kldload: %s\n", s);
190 printf("kldload: %s: %s\n", filename, s);
194 link_elf_init(void *arg)
197 linker_add_class(&link_elf_class);
200 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, NULL);
203 link_elf_protect_range(elf_file_t ef, vm_offset_t start, vm_offset_t end,
208 KASSERT(start <= end && start >= (vm_offset_t)ef->address &&
209 end <= round_page((vm_offset_t)ef->address + ef->lf.size),
210 ("link_elf_protect_range: invalid range %#jx-%#jx",
211 (uintmax_t)start, (uintmax_t)end));
215 error = vm_map_protect(kernel_map, start, end, prot, FALSE);
216 KASSERT(error == KERN_SUCCESS,
217 ("link_elf_protect_range: vm_map_protect() returned %d", error));
221 * Restrict permissions on linker file memory based on section flags.
222 * Sections need not be page-aligned, so overlap within a page is possible.
225 link_elf_protect(elf_file_t ef)
227 vm_offset_t end, segend, segstart, start;
228 vm_prot_t gapprot, prot, segprot;
232 * If the file was preloaded, the last page may contain other preloaded
233 * data which may need to be writeable. ELF files are always
234 * page-aligned, but other preloaded data, such as entropy or CPU
235 * microcode may be loaded with a smaller alignment.
237 gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ;
239 start = end = (vm_offset_t)ef->address;
241 for (i = 0; i < ef->nprogtab; i++) {
243 * VNET and DPCPU sections have their memory allocated by their
244 * respective subsystems.
246 if (ef->progtab[i].name != NULL && (
248 strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 ||
250 strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0))
253 segstart = trunc_page((vm_offset_t)ef->progtab[i].addr);
254 segend = round_page((vm_offset_t)ef->progtab[i].addr +
255 ef->progtab[i].size);
256 segprot = VM_PROT_READ;
257 if ((ef->progtab[i].flags & SHF_WRITE) != 0)
258 segprot |= VM_PROT_WRITE;
259 if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0)
260 segprot |= VM_PROT_EXECUTE;
262 if (end <= segstart) {
264 * Case 1: there is no overlap between the previous
265 * segment and this one. Apply protections to the
266 * previous segment, and protect the gap between the
267 * previous and current segments, if any.
269 link_elf_protect_range(ef, start, end, prot);
270 link_elf_protect_range(ef, end, segstart, gapprot);
275 } else if (start < segstart && end == segend) {
277 * Case 2: the current segment is a subrange of the
278 * previous segment. Apply protections to the
279 * non-overlapping portion of the previous segment.
281 link_elf_protect_range(ef, start, segstart, prot);
285 } else if (end < segend) {
287 * Case 3: there is partial overlap between the previous
288 * and current segments. Apply protections to the
289 * non-overlapping portion of the previous segment, and
290 * then the overlap, which must use the union of the two
291 * segments' protections.
293 link_elf_protect_range(ef, start, segstart, prot);
294 link_elf_protect_range(ef, segstart, end,
301 * Case 4: the two segments reside in the same page.
308 * Fix up the last unprotected segment and trailing data.
310 link_elf_protect_range(ef, start, end, prot);
311 link_elf_protect_range(ef, end,
312 round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
316 link_elf_link_preload(linker_class_t cls, const char *filename,
317 linker_file_t *result)
322 void *modptr, *baseptr, *sizeptr;
327 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
329 /* Look to see if we have the file preloaded */
330 modptr = preload_search_by_name(filename);
334 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
335 baseptr = preload_search_info(modptr, MODINFO_ADDR);
336 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
337 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
339 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
341 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
342 " obj module") != 0 &&
343 strcmp(type, "elf obj module") != 0)) {
346 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
350 lf = linker_make_file(filename, &link_elf_class);
356 ef->address = *(caddr_t *)baseptr;
357 lf->address = *(caddr_t *)baseptr;
358 lf->size = *(size_t *)sizeptr;
360 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
361 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
362 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
363 hdr->e_version != EV_CURRENT ||
364 hdr->e_type != ET_REL ||
365 hdr->e_machine != ELF_TARG_MACH) {
371 /* Scan the section header for information and table sizing. */
374 for (i = 0; i < hdr->e_shnum; i++) {
375 switch (shdr[i].sh_type) {
379 case SHT_X86_64_UNWIND:
381 /* Ignore sections not loaded by the loader. */
382 if (shdr[i].sh_addr == 0)
388 symstrindex = shdr[i].sh_link;
392 * Ignore relocation tables for sections not
393 * loaded by the loader.
395 if (shdr[shdr[i].sh_info].sh_addr == 0)
400 if (shdr[shdr[i].sh_info].sh_addr == 0)
407 shstrindex = hdr->e_shstrndx;
408 if (ef->nprogtab == 0 || symstrindex < 0 ||
409 symstrindex >= hdr->e_shnum ||
410 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
411 shstrindex >= hdr->e_shnum ||
412 shdr[shstrindex].sh_type != SHT_STRTAB) {
413 printf("%s: bad/missing section headers\n", filename);
418 /* Allocate space for tracking the load chunks */
419 if (ef->nprogtab != 0)
420 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
421 M_LINKER, M_WAITOK | M_ZERO);
422 if (ef->nreltab != 0)
423 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
424 M_LINKER, M_WAITOK | M_ZERO);
425 if (ef->nrelatab != 0)
426 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
427 M_LINKER, M_WAITOK | M_ZERO);
428 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
429 (ef->nreltab != 0 && ef->reltab == NULL) ||
430 (ef->nrelatab != 0 && ef->relatab == NULL)) {
435 /* XXX, relocate the sh_addr fields saved by the loader. */
437 for (i = 0; i < hdr->e_shnum; i++) {
438 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
439 off = shdr[i].sh_addr;
441 for (i = 0; i < hdr->e_shnum; i++) {
442 if (shdr[i].sh_addr != 0)
443 shdr[i].sh_addr = shdr[i].sh_addr - off +
444 (Elf_Addr)ef->address;
447 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
448 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
449 ef->ddbstrcnt = shdr[symstrindex].sh_size;
450 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
451 ef->shstrcnt = shdr[shstrindex].sh_size;
452 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
454 /* Now fill out progtab and the relocation tables. */
458 for (i = 0; i < hdr->e_shnum; i++) {
459 switch (shdr[i].sh_type) {
463 case SHT_X86_64_UNWIND:
465 if (shdr[i].sh_addr == 0)
467 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
468 if (shdr[i].sh_type == SHT_PROGBITS)
469 ef->progtab[pb].name = "<<PROGBITS>>";
471 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
472 ef->progtab[pb].name = "<<UNWIND>>";
475 ef->progtab[pb].name = "<<NOBITS>>";
476 ef->progtab[pb].size = shdr[i].sh_size;
477 ef->progtab[pb].flags = shdr[i].sh_flags;
478 ef->progtab[pb].sec = i;
479 if (ef->shstrtab && shdr[i].sh_name != 0)
480 ef->progtab[pb].name =
481 ef->shstrtab + shdr[i].sh_name;
482 if (ef->progtab[pb].name != NULL &&
483 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
486 dpcpu = dpcpu_alloc(shdr[i].sh_size);
488 printf("%s: pcpu module space is out "
489 "of space; cannot allocate %#jx "
490 "for %s\n", __func__,
491 (uintmax_t)shdr[i].sh_size,
496 memcpy(dpcpu, ef->progtab[pb].addr,
497 ef->progtab[pb].size);
498 dpcpu_copy(dpcpu, shdr[i].sh_size);
499 ef->progtab[pb].addr = dpcpu;
501 } else if (ef->progtab[pb].name != NULL &&
502 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
505 vnet_data = vnet_data_alloc(shdr[i].sh_size);
506 if (vnet_data == NULL) {
507 printf("%s: vnet module space is out "
508 "of space; cannot allocate %#jx "
509 "for %s\n", __func__,
510 (uintmax_t)shdr[i].sh_size,
515 memcpy(vnet_data, ef->progtab[pb].addr,
516 ef->progtab[pb].size);
517 vnet_data_copy(vnet_data, shdr[i].sh_size);
518 ef->progtab[pb].addr = vnet_data;
520 } else if (ef->progtab[pb].name != NULL &&
521 !strcmp(ef->progtab[pb].name, ".ctors")) {
522 lf->ctors_addr = ef->progtab[pb].addr;
523 lf->ctors_size = shdr[i].sh_size;
526 /* Update all symbol values with the offset. */
527 for (j = 0; j < ef->ddbsymcnt; j++) {
528 es = &ef->ddbsymtab[j];
529 if (es->st_shndx != i)
531 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
536 if (shdr[shdr[i].sh_info].sh_addr == 0)
538 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
539 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
540 ef->reltab[rl].sec = shdr[i].sh_info;
544 if (shdr[shdr[i].sh_info].sh_addr == 0)
546 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
547 ef->relatab[ra].nrela =
548 shdr[i].sh_size / sizeof(Elf_Rela);
549 ef->relatab[ra].sec = shdr[i].sh_info;
554 if (pb != ef->nprogtab) {
555 printf("%s: lost progbits\n", filename);
559 if (rl != ef->nreltab) {
560 printf("%s: lost reltab\n", filename);
564 if (ra != ef->nrelatab) {
565 printf("%s: lost relatab\n", filename);
570 /* Local intra-module relocations */
571 error = link_elf_reloc_local(lf, false);
578 /* preload not done this way */
579 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
584 link_elf_invoke_ctors(caddr_t addr, size_t size)
589 if (addr == NULL || size == 0)
591 cnt = size / sizeof(*ctor);
593 for (i = 0; i < cnt; i++) {
600 link_elf_link_preload_finish(linker_file_t lf)
606 error = relocate_file(ef);
610 /* Notify MD code that a module is being loaded. */
611 error = elf_cpu_load_file(lf);
615 #if defined(__i386__) || defined(__amd64__)
617 error = link_elf_reloc_local(lf, true);
623 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
628 link_elf_load_file(linker_class_t cls, const char *filename,
629 linker_file_t *result)
631 struct nameidata *nd;
632 struct thread *td = curthread; /* XXX */
656 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
657 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
659 error = vn_open(nd, &flags, 0, NULL);
664 NDFREE(nd, NDF_ONLY_PNBUF);
665 if (nd->ni_vp->v_type != VREG) {
670 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
676 /* Read the elf header from the file. */
677 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
678 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
679 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
693 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
694 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
695 link_elf_error(filename, "Unsupported file layout");
699 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
700 || hdr->e_version != EV_CURRENT) {
701 link_elf_error(filename, "Unsupported file version");
705 if (hdr->e_type != ET_REL) {
709 if (hdr->e_machine != ELF_TARG_MACH) {
710 link_elf_error(filename, "Unsupported machine");
715 lf = linker_make_file(filename, &link_elf_class);
720 ef = (elf_file_t) lf;
726 /* Allocate and read in the section header */
727 nbytes = hdr->e_shnum * hdr->e_shentsize;
728 if (nbytes == 0 || hdr->e_shoff == 0 ||
729 hdr->e_shentsize != sizeof(Elf_Shdr)) {
733 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
735 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
736 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
745 /* Scan the section header for information and table sizing. */
749 for (i = 0; i < hdr->e_shnum; i++) {
750 if (shdr[i].sh_size == 0)
752 switch (shdr[i].sh_type) {
756 case SHT_X86_64_UNWIND:
758 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
765 symstrindex = shdr[i].sh_link;
769 * Ignore relocation tables for unallocated
772 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
777 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
785 if (ef->nprogtab == 0) {
786 link_elf_error(filename, "file has no contents");
791 /* Only allow one symbol table for now */
792 link_elf_error(filename,
793 "file must have exactly one symbol table");
797 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
798 shdr[symstrindex].sh_type != SHT_STRTAB) {
799 link_elf_error(filename, "file has invalid symbol strings");
804 /* Allocate space for tracking the load chunks */
805 if (ef->nprogtab != 0)
806 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
807 M_LINKER, M_WAITOK | M_ZERO);
808 if (ef->nreltab != 0)
809 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
810 M_LINKER, M_WAITOK | M_ZERO);
811 if (ef->nrelatab != 0)
812 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
813 M_LINKER, M_WAITOK | M_ZERO);
815 if (symtabindex == -1) {
816 link_elf_error(filename, "lost symbol table index");
820 /* Allocate space for and load the symbol table */
821 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
822 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
823 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
824 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
825 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
834 /* Allocate space for and load the symbol strings */
835 ef->ddbstrcnt = shdr[symstrindex].sh_size;
836 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
837 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
838 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
839 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
848 /* Do we have a string table for the section names? */
850 if (hdr->e_shstrndx != 0 &&
851 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
852 shstrindex = hdr->e_shstrndx;
853 ef->shstrcnt = shdr[shstrindex].sh_size;
854 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
856 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
857 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
858 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
868 /* Size up code/data(progbits) and bss(nobits). */
870 for (i = 0; i < hdr->e_shnum; i++) {
871 if (shdr[i].sh_size == 0)
873 switch (shdr[i].sh_type) {
877 case SHT_X86_64_UNWIND:
879 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
881 alignmask = shdr[i].sh_addralign - 1;
882 mapsize += alignmask;
883 mapsize &= ~alignmask;
884 mapsize += shdr[i].sh_size;
890 * We know how much space we need for the text/data/bss/etc.
891 * This stuff needs to be in a single chunk so that profiling etc
892 * can get the bounds and gdb can associate offsets with modules
894 ef->object = vm_pager_allocate(OBJT_PHYS, NULL, round_page(mapsize),
895 VM_PROT_ALL, 0, thread0.td_ucred);
896 if (ef->object == NULL) {
902 * In order to satisfy amd64's architectural requirements on the
903 * location of code and data in the kernel's address space, request a
904 * mapping that is above the kernel.
906 * Protections will be restricted once relocations are applied.
911 mapbase = VM_MIN_KERNEL_ADDRESS;
913 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
914 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
916 if (error != KERN_SUCCESS) {
917 vm_object_deallocate(ef->object);
924 error = vm_map_wire(kernel_map, mapbase,
925 mapbase + round_page(mapsize),
926 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
927 if (error != KERN_SUCCESS) {
932 /* Inform the kld system about the situation */
933 lf->address = ef->address = (caddr_t)mapbase;
937 * Now load code/data(progbits), zero bss(nobits), allocate space for
944 for (i = 0; i < hdr->e_shnum; i++) {
945 if (shdr[i].sh_size == 0)
947 switch (shdr[i].sh_type) {
951 case SHT_X86_64_UNWIND:
953 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
955 alignmask = shdr[i].sh_addralign - 1;
956 mapbase += alignmask;
957 mapbase &= ~alignmask;
958 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
959 ef->progtab[pb].name =
960 ef->shstrtab + shdr[i].sh_name;
961 if (!strcmp(ef->progtab[pb].name, ".ctors")) {
962 lf->ctors_addr = (caddr_t)mapbase;
963 lf->ctors_size = shdr[i].sh_size;
965 } else if (shdr[i].sh_type == SHT_PROGBITS)
966 ef->progtab[pb].name = "<<PROGBITS>>";
968 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
969 ef->progtab[pb].name = "<<UNWIND>>";
972 ef->progtab[pb].name = "<<NOBITS>>";
973 if (ef->progtab[pb].name != NULL &&
974 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
975 ef->progtab[pb].addr =
976 dpcpu_alloc(shdr[i].sh_size);
977 if (ef->progtab[pb].addr == NULL) {
978 printf("%s: pcpu module space is out "
979 "of space; cannot allocate %#jx "
980 "for %s\n", __func__,
981 (uintmax_t)shdr[i].sh_size,
986 else if (ef->progtab[pb].name != NULL &&
987 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
988 ef->progtab[pb].addr =
989 vnet_data_alloc(shdr[i].sh_size);
990 if (ef->progtab[pb].addr == NULL) {
991 printf("%s: vnet module space is out "
992 "of space; cannot allocate %#jx "
993 "for %s\n", __func__,
994 (uintmax_t)shdr[i].sh_size,
1000 ef->progtab[pb].addr =
1001 (void *)(uintptr_t)mapbase;
1002 if (ef->progtab[pb].addr == NULL) {
1006 ef->progtab[pb].size = shdr[i].sh_size;
1007 ef->progtab[pb].flags = shdr[i].sh_flags;
1008 ef->progtab[pb].sec = i;
1009 if (shdr[i].sh_type == SHT_PROGBITS
1011 || shdr[i].sh_type == SHT_X86_64_UNWIND
1014 error = vn_rdwr(UIO_READ, nd->ni_vp,
1015 ef->progtab[pb].addr,
1016 shdr[i].sh_size, shdr[i].sh_offset,
1017 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1018 NOCRED, &resid, td);
1025 /* Initialize the per-cpu or vnet area. */
1026 if (ef->progtab[pb].addr != (void *)mapbase &&
1027 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1028 dpcpu_copy(ef->progtab[pb].addr,
1031 else if (ef->progtab[pb].addr !=
1033 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
1034 vnet_data_copy(ef->progtab[pb].addr,
1038 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1040 /* Update all symbol values with the offset. */
1041 for (j = 0; j < ef->ddbsymcnt; j++) {
1042 es = &ef->ddbsymtab[j];
1043 if (es->st_shndx != i)
1045 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1047 mapbase += shdr[i].sh_size;
1051 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1053 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1055 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1056 ef->reltab[rl].sec = shdr[i].sh_info;
1057 error = vn_rdwr(UIO_READ, nd->ni_vp,
1058 (void *)ef->reltab[rl].rel,
1059 shdr[i].sh_size, shdr[i].sh_offset,
1060 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1071 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1073 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1075 ef->relatab[ra].nrela =
1076 shdr[i].sh_size / sizeof(Elf_Rela);
1077 ef->relatab[ra].sec = shdr[i].sh_info;
1078 error = vn_rdwr(UIO_READ, nd->ni_vp,
1079 (void *)ef->relatab[ra].rela,
1080 shdr[i].sh_size, shdr[i].sh_offset,
1081 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1093 if (pb != ef->nprogtab) {
1094 link_elf_error(filename, "lost progbits");
1098 if (rl != ef->nreltab) {
1099 link_elf_error(filename, "lost reltab");
1103 if (ra != ef->nrelatab) {
1104 link_elf_error(filename, "lost relatab");
1108 if (mapbase != (vm_offset_t)ef->address + mapsize) {
1110 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1111 filename != NULL ? filename : "<none>",
1112 (u_long)mapbase, ef->address, (u_long)mapsize,
1113 (u_long)(vm_offset_t)ef->address + mapsize);
1118 /* Local intra-module relocations */
1119 error = link_elf_reloc_local(lf, false);
1123 /* Pull in dependencies */
1124 VOP_UNLOCK(nd->ni_vp, 0);
1125 error = linker_load_dependencies(lf);
1126 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1130 /* External relocations */
1131 error = relocate_file(ef);
1135 /* Notify MD code that a module is being loaded. */
1136 error = elf_cpu_load_file(lf);
1140 #if defined(__i386__) || defined(__amd64__)
1142 error = link_elf_reloc_local(lf, true);
1147 link_elf_protect(ef);
1148 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
1152 VOP_UNLOCK(nd->ni_vp, 0);
1153 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1156 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1157 free(hdr, M_LINKER);
1163 link_elf_unload_file(linker_file_t file)
1165 elf_file_t ef = (elf_file_t) file;
1168 /* Notify MD code that a module is being unloaded. */
1169 elf_cpu_unload_file(file);
1172 for (i = 0; i < ef->nprogtab; i++) {
1173 if (ef->progtab[i].size == 0)
1175 if (ef->progtab[i].name == NULL)
1177 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1178 dpcpu_free(ef->progtab[i].addr,
1179 ef->progtab[i].size);
1181 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1182 vnet_data_free(ef->progtab[i].addr,
1183 ef->progtab[i].size);
1187 if (ef->preloaded) {
1188 free(ef->reltab, M_LINKER);
1189 free(ef->relatab, M_LINKER);
1190 free(ef->progtab, M_LINKER);
1191 free(ef->ctftab, M_LINKER);
1192 free(ef->ctfoff, M_LINKER);
1193 free(ef->typoff, M_LINKER);
1194 if (file->pathname != NULL)
1195 preload_delete_name(file->pathname);
1199 for (i = 0; i < ef->nreltab; i++)
1200 free(ef->reltab[i].rel, M_LINKER);
1201 for (i = 0; i < ef->nrelatab; i++)
1202 free(ef->relatab[i].rela, M_LINKER);
1203 free(ef->reltab, M_LINKER);
1204 free(ef->relatab, M_LINKER);
1205 free(ef->progtab, M_LINKER);
1207 if (ef->object != NULL)
1208 vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1209 (vm_offset_t)ef->address + ptoa(ef->object->size));
1210 free(ef->e_shdr, M_LINKER);
1211 free(ef->ddbsymtab, M_LINKER);
1212 free(ef->ddbstrtab, M_LINKER);
1213 free(ef->shstrtab, M_LINKER);
1214 free(ef->ctftab, M_LINKER);
1215 free(ef->ctfoff, M_LINKER);
1216 free(ef->typoff, M_LINKER);
1220 symbol_name(elf_file_t ef, Elf_Size r_info)
1224 if (ELF_R_SYM(r_info)) {
1225 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1226 return ef->ddbstrtab + ref->st_name;
1232 findbase(elf_file_t ef, int sec)
1237 for (i = 0; i < ef->nprogtab; i++) {
1238 if (sec == ef->progtab[i].sec) {
1239 base = (Elf_Addr)ef->progtab[i].addr;
1247 relocate_file(elf_file_t ef)
1249 const Elf_Rel *rellim;
1251 const Elf_Rela *relalim;
1252 const Elf_Rela *rela;
1253 const char *symname;
1260 /* Perform relocations without addend if there are any: */
1261 for (i = 0; i < ef->nreltab; i++) {
1262 rel = ef->reltab[i].rel;
1264 link_elf_error(ef->lf.filename, "lost a reltab!");
1267 rellim = rel + ef->reltab[i].nrel;
1268 base = findbase(ef, ef->reltab[i].sec);
1270 link_elf_error(ef->lf.filename, "lost base for reltab");
1273 for ( ; rel < rellim; rel++) {
1274 symidx = ELF_R_SYM(rel->r_info);
1275 if (symidx >= ef->ddbsymcnt)
1277 sym = ef->ddbsymtab + symidx;
1278 /* Local relocs are already done */
1279 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1281 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1283 symname = symbol_name(ef, rel->r_info);
1284 printf("link_elf_obj: symbol %s undefined\n",
1291 /* Perform relocations with addend if there are any: */
1292 for (i = 0; i < ef->nrelatab; i++) {
1293 rela = ef->relatab[i].rela;
1295 link_elf_error(ef->lf.filename, "lost a relatab!");
1298 relalim = rela + ef->relatab[i].nrela;
1299 base = findbase(ef, ef->relatab[i].sec);
1301 link_elf_error(ef->lf.filename,
1302 "lost base for relatab");
1305 for ( ; rela < relalim; rela++) {
1306 symidx = ELF_R_SYM(rela->r_info);
1307 if (symidx >= ef->ddbsymcnt)
1309 sym = ef->ddbsymtab + symidx;
1310 /* Local relocs are already done */
1311 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1313 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1315 symname = symbol_name(ef, rela->r_info);
1316 printf("link_elf_obj: symbol %s undefined\n",
1324 * Only clean SHN_FBSD_CACHED for successful return. If we
1325 * modified symbol table for the object but found an
1326 * unresolved symbol, there is no reason to roll back.
1328 elf_obj_cleanup_globals_cache(ef);
1334 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1336 elf_file_t ef = (elf_file_t) lf;
1337 const Elf_Sym *symp;
1341 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1342 strp = ef->ddbstrtab + symp->st_name;
1343 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1344 *sym = (c_linker_sym_t) symp;
1352 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1353 linker_symval_t *symval)
1359 ef = (elf_file_t) lf;
1360 es = (const Elf_Sym*) sym;
1361 val = (caddr_t)es->st_value;
1362 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1363 symval->name = ef->ddbstrtab + es->st_name;
1364 val = (caddr_t)es->st_value;
1365 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1366 val = ((caddr_t (*)(void))val)();
1367 symval->value = val;
1368 symval->size = es->st_size;
1375 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1376 c_linker_sym_t *sym, long *diffp)
1378 elf_file_t ef = (elf_file_t) lf;
1379 u_long off = (uintptr_t) (void *) value;
1383 const Elf_Sym *best = NULL;
1386 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1387 if (es->st_name == 0)
1389 st_value = es->st_value;
1390 if (off >= st_value) {
1391 if (off - st_value < diff) {
1392 diff = off - st_value;
1396 } else if (off - st_value == diff) {
1405 *sym = (c_linker_sym_t) best;
1411 * Look up a linker set on an ELF system.
1414 link_elf_lookup_set(linker_file_t lf, const char *name,
1415 void ***startp, void ***stopp, int *countp)
1417 elf_file_t ef = (elf_file_t)lf;
1418 void **start, **stop;
1421 /* Relative to section number */
1422 for (i = 0; i < ef->nprogtab; i++) {
1423 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1424 strcmp(ef->progtab[i].name + 4, name) == 0) {
1425 start = (void **)ef->progtab[i].addr;
1426 stop = (void **)((char *)ef->progtab[i].addr +
1427 ef->progtab[i].size);
1428 count = stop - start;
1442 link_elf_each_function_name(linker_file_t file,
1443 int (*callback)(const char *, void *), void *opaque)
1445 elf_file_t ef = (elf_file_t)file;
1446 const Elf_Sym *symp;
1449 /* Exhaustive search */
1450 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1451 if (symp->st_value != 0 &&
1452 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1453 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1454 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1463 link_elf_each_function_nameval(linker_file_t file,
1464 linker_function_nameval_callback_t callback, void *opaque)
1466 linker_symval_t symval;
1467 elf_file_t ef = (elf_file_t)file;
1468 const Elf_Sym* symp;
1471 /* Exhaustive search */
1472 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1473 if (symp->st_value != 0 &&
1474 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1475 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1476 error = link_elf_symbol_values(file,
1477 (c_linker_sym_t)symp, &symval);
1480 error = callback(file, i, &symval, opaque);
1489 elf_obj_cleanup_globals_cache(elf_file_t ef)
1494 for (i = 0; i < ef->ddbsymcnt; i++) {
1495 sym = ef->ddbsymtab + i;
1496 if (sym->st_shndx == SHN_FBSD_CACHED) {
1497 sym->st_shndx = SHN_UNDEF;
1504 * Symbol lookup function that can be used when the symbol index is known (ie
1505 * in relocations). It uses the symbol index instead of doing a fully fledged
1506 * hash table based lookup when such is valid. For example for local symbols.
1507 * This is not only more efficient, it's also more correct. It's not always
1508 * the case that the symbol can be found through the hash table.
1511 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1513 elf_file_t ef = (elf_file_t)lf;
1518 /* Don't even try to lookup the symbol if the index is bogus. */
1519 if (symidx >= ef->ddbsymcnt) {
1524 sym = ef->ddbsymtab + symidx;
1526 /* Quick answer if there is a definition included. */
1527 if (sym->st_shndx != SHN_UNDEF) {
1528 res1 = (Elf_Addr)sym->st_value;
1529 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1530 res1 = ((Elf_Addr (*)(void))res1)();
1535 /* If we get here, then it is undefined and needs a lookup. */
1536 switch (ELF_ST_BIND(sym->st_info)) {
1538 /* Local, but undefined? huh? */
1544 /* Relative to Data or Function name */
1545 symbol = ef->ddbstrtab + sym->st_name;
1547 /* Force a lookup failure if the symbol name is bogus. */
1552 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1555 * Cache global lookups during module relocation. The failure
1556 * case is particularly expensive for callers, who must scan
1557 * through the entire globals table doing strcmp(). Cache to
1558 * avoid doing such work repeatedly.
1560 * After relocation is complete, undefined globals will be
1561 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1565 sym->st_shndx = SHN_FBSD_CACHED;
1566 sym->st_value = res1;
1569 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1582 link_elf_fix_link_set(elf_file_t ef)
1584 static const char startn[] = "__start_";
1585 static const char stopn[] = "__stop_";
1587 const char *sym_name, *linkset_name;
1588 Elf_Addr startp, stopp;
1593 for (symidx = 1 /* zero entry is special */;
1594 symidx < ef->ddbsymcnt; symidx++) {
1595 sym = ef->ddbsymtab + symidx;
1596 if (sym->st_shndx != SHN_UNDEF)
1599 sym_name = ef->ddbstrtab + sym->st_name;
1600 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1602 linkset_name = sym_name + sizeof(startn) - 1;
1604 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1606 linkset_name = sym_name + sizeof(stopn) - 1;
1611 for (i = 0; i < ef->nprogtab; i++) {
1612 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1613 startp = (Elf_Addr)ef->progtab[i].addr;
1614 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1618 if (i == ef->nprogtab)
1621 sym->st_value = start ? startp : stopp;
1627 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1629 elf_file_t ef = (elf_file_t)lf;
1630 const Elf_Rel *rellim;
1632 const Elf_Rela *relalim;
1633 const Elf_Rela *rela;
1639 link_elf_fix_link_set(ef);
1641 /* Perform relocations without addend if there are any: */
1642 for (i = 0; i < ef->nreltab; i++) {
1643 rel = ef->reltab[i].rel;
1645 link_elf_error(ef->lf.filename, "lost a reltab");
1648 rellim = rel + ef->reltab[i].nrel;
1649 base = findbase(ef, ef->reltab[i].sec);
1651 link_elf_error(ef->lf.filename, "lost base for reltab");
1654 for ( ; rel < rellim; rel++) {
1655 symidx = ELF_R_SYM(rel->r_info);
1656 if (symidx >= ef->ddbsymcnt)
1658 sym = ef->ddbsymtab + symidx;
1659 /* Only do local relocs */
1660 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1662 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1663 elf_is_ifunc_reloc(rel->r_info)) != ifuncs)
1665 if (elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1666 elf_obj_lookup) != 0)
1671 /* Perform relocations with addend if there are any: */
1672 for (i = 0; i < ef->nrelatab; i++) {
1673 rela = ef->relatab[i].rela;
1675 link_elf_error(ef->lf.filename, "lost a relatab!");
1678 relalim = rela + ef->relatab[i].nrela;
1679 base = findbase(ef, ef->relatab[i].sec);
1681 link_elf_error(ef->lf.filename, "lost base for reltab");
1684 for ( ; rela < relalim; rela++) {
1685 symidx = ELF_R_SYM(rela->r_info);
1686 if (symidx >= ef->ddbsymcnt)
1688 sym = ef->ddbsymtab + symidx;
1689 /* Only do local relocs */
1690 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1692 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1693 elf_is_ifunc_reloc(rela->r_info)) != ifuncs)
1695 if (elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1696 elf_obj_lookup) != 0)
1704 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1706 elf_file_t ef = (elf_file_t)lf;
1708 *symtab = ef->ddbsymtab;
1710 if (*symtab == NULL)
1713 return (ef->ddbsymcnt);
1717 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1719 elf_file_t ef = (elf_file_t)lf;
1721 *strtab = ef->ddbstrtab;
1723 if (*strtab == NULL)
1726 return (ef->ddbstrcnt);