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>
65 #include <contrib/zlib/zlib.h>
68 #include "linker_if.h"
73 int flags; /* Section flags. */
74 int sec; /* Original section number. */
90 typedef struct elf_file {
91 struct linker_file lf; /* Common fields */
94 caddr_t address; /* Relocation address */
95 vm_object_t object; /* VM object to hold file pages */
101 Elf_relaent *relatab;
107 Elf_Sym *ddbsymtab; /* The symbol table we are using */
108 long ddbsymcnt; /* Number of symbols */
109 caddr_t ddbstrtab; /* String table */
110 long ddbstrcnt; /* number of bytes in string table */
112 caddr_t shstrtab; /* Section name string table */
113 long shstrcnt; /* number of bytes in string table */
115 caddr_t ctftab; /* CTF table */
116 long ctfcnt; /* number of bytes in CTF table */
117 caddr_t ctfoff; /* CTF offset table */
118 caddr_t typoff; /* Type offset table */
119 long typlen; /* Number of type entries. */
123 #include <kern/kern_ctf.c>
125 static int link_elf_link_preload(linker_class_t cls,
126 const char *, linker_file_t *);
127 static int link_elf_link_preload_finish(linker_file_t);
128 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *);
129 static int link_elf_lookup_symbol(linker_file_t, const char *,
131 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t,
133 static int link_elf_search_symbol(linker_file_t, caddr_t value,
134 c_linker_sym_t *sym, long *diffp);
136 static void link_elf_unload_file(linker_file_t);
137 static int link_elf_lookup_set(linker_file_t, const char *,
138 void ***, void ***, int *);
139 static int link_elf_each_function_name(linker_file_t,
140 int (*)(const char *, void *), void *);
141 static int link_elf_each_function_nameval(linker_file_t,
142 linker_function_nameval_callback_t,
144 static int link_elf_reloc_local(linker_file_t, bool);
145 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
146 static long link_elf_strtab_get(linker_file_t, caddr_t *);
148 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps,
151 static kobj_method_t link_elf_methods[] = {
152 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
153 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
154 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
155 KOBJMETHOD(linker_unload, link_elf_unload_file),
156 KOBJMETHOD(linker_load_file, link_elf_load_file),
157 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
158 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
159 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
160 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
161 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
162 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
163 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
164 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
168 static struct linker_class link_elf_class = {
169 #if ELF_TARG_CLASS == ELFCLASS32
174 link_elf_methods, sizeof(struct elf_file)
177 static int relocate_file(elf_file_t ef);
178 static void elf_obj_cleanup_globals_cache(elf_file_t);
181 link_elf_error(const char *filename, const char *s)
183 if (filename == NULL)
184 printf("kldload: %s\n", s);
186 printf("kldload: %s: %s\n", filename, s);
190 link_elf_init(void *arg)
193 linker_add_class(&link_elf_class);
195 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, NULL);
198 link_elf_protect_range(elf_file_t ef, vm_offset_t start, vm_offset_t end,
203 KASSERT(start <= end && start >= (vm_offset_t)ef->address &&
204 end <= round_page((vm_offset_t)ef->address + ef->lf.size),
205 ("link_elf_protect_range: invalid range %#jx-%#jx",
206 (uintmax_t)start, (uintmax_t)end));
212 error = pmap_change_prot(start, end - start, prot);
214 ("link_elf_protect_range: pmap_change_prot() returned %d",
219 error = vm_map_protect(kernel_map, start, end, prot, FALSE);
220 KASSERT(error == KERN_SUCCESS,
221 ("link_elf_protect_range: vm_map_protect() returned %d", error));
225 * Restrict permissions on linker file memory based on section flags.
226 * Sections need not be page-aligned, so overlap within a page is possible.
229 link_elf_protect(elf_file_t ef)
231 vm_offset_t end, segend, segstart, start;
232 vm_prot_t gapprot, prot, segprot;
236 * If the file was preloaded, the last page may contain other preloaded
237 * data which may need to be writeable. ELF files are always
238 * page-aligned, but other preloaded data, such as entropy or CPU
239 * microcode may be loaded with a smaller alignment.
241 gapprot = ef->preloaded ? VM_PROT_RW : VM_PROT_READ;
243 start = end = (vm_offset_t)ef->address;
245 for (i = 0; i < ef->nprogtab; i++) {
247 * VNET and DPCPU sections have their memory allocated by their
248 * respective subsystems.
250 if (ef->progtab[i].name != NULL && (
252 strcmp(ef->progtab[i].name, VNET_SETNAME) == 0 ||
254 strcmp(ef->progtab[i].name, DPCPU_SETNAME) == 0))
257 segstart = trunc_page((vm_offset_t)ef->progtab[i].addr);
258 segend = round_page((vm_offset_t)ef->progtab[i].addr +
259 ef->progtab[i].size);
260 segprot = VM_PROT_READ;
261 if ((ef->progtab[i].flags & SHF_WRITE) != 0)
262 segprot |= VM_PROT_WRITE;
263 if ((ef->progtab[i].flags & SHF_EXECINSTR) != 0)
264 segprot |= VM_PROT_EXECUTE;
266 if (end <= segstart) {
268 * Case 1: there is no overlap between the previous
269 * segment and this one. Apply protections to the
270 * previous segment, and protect the gap between the
271 * previous and current segments, if any.
273 link_elf_protect_range(ef, start, end, prot);
274 link_elf_protect_range(ef, end, segstart, gapprot);
279 } else if (start < segstart && end == segend) {
281 * Case 2: the current segment is a subrange of the
282 * previous segment. Apply protections to the
283 * non-overlapping portion of the previous segment.
285 link_elf_protect_range(ef, start, segstart, prot);
289 } else if (end < segend) {
291 * Case 3: there is partial overlap between the previous
292 * and current segments. Apply protections to the
293 * non-overlapping portion of the previous segment, and
294 * then the overlap, which must use the union of the two
295 * segments' protections.
297 link_elf_protect_range(ef, start, segstart, prot);
298 link_elf_protect_range(ef, segstart, end,
305 * Case 4: the two segments reside in the same page.
312 * Fix up the last unprotected segment and trailing data.
314 link_elf_protect_range(ef, start, end, prot);
315 link_elf_protect_range(ef, end,
316 round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
320 link_elf_link_preload(linker_class_t cls, const char *filename,
321 linker_file_t *result)
326 void *modptr, *baseptr, *sizeptr;
331 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
333 /* Look to see if we have the file preloaded */
334 modptr = preload_search_by_name(filename);
338 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
339 baseptr = preload_search_info(modptr, MODINFO_ADDR);
340 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
341 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
343 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
345 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
346 " obj module") != 0 &&
347 strcmp(type, "elf obj module") != 0)) {
350 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
354 lf = linker_make_file(filename, &link_elf_class);
360 ef->address = *(caddr_t *)baseptr;
361 lf->address = *(caddr_t *)baseptr;
362 lf->size = *(size_t *)sizeptr;
364 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
365 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
366 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
367 hdr->e_version != EV_CURRENT ||
368 hdr->e_type != ET_REL ||
369 hdr->e_machine != ELF_TARG_MACH) {
375 /* Scan the section header for information and table sizing. */
378 for (i = 0; i < hdr->e_shnum; i++) {
379 switch (shdr[i].sh_type) {
383 case SHT_X86_64_UNWIND:
385 /* Ignore sections not loaded by the loader. */
386 if (shdr[i].sh_addr == 0)
392 symstrindex = shdr[i].sh_link;
396 * Ignore relocation tables for sections not
397 * loaded by the loader.
399 if (shdr[shdr[i].sh_info].sh_addr == 0)
404 if (shdr[shdr[i].sh_info].sh_addr == 0)
411 shstrindex = hdr->e_shstrndx;
412 if (ef->nprogtab == 0 || symstrindex < 0 ||
413 symstrindex >= hdr->e_shnum ||
414 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
415 shstrindex >= hdr->e_shnum ||
416 shdr[shstrindex].sh_type != SHT_STRTAB) {
417 printf("%s: bad/missing section headers\n", filename);
422 /* Allocate space for tracking the load chunks */
423 if (ef->nprogtab != 0)
424 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
425 M_LINKER, M_WAITOK | M_ZERO);
426 if (ef->nreltab != 0)
427 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
428 M_LINKER, M_WAITOK | M_ZERO);
429 if (ef->nrelatab != 0)
430 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
431 M_LINKER, M_WAITOK | M_ZERO);
432 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
433 (ef->nreltab != 0 && ef->reltab == NULL) ||
434 (ef->nrelatab != 0 && ef->relatab == NULL)) {
439 /* XXX, relocate the sh_addr fields saved by the loader. */
441 for (i = 0; i < hdr->e_shnum; i++) {
442 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
443 off = shdr[i].sh_addr;
445 for (i = 0; i < hdr->e_shnum; i++) {
446 if (shdr[i].sh_addr != 0)
447 shdr[i].sh_addr = shdr[i].sh_addr - off +
448 (Elf_Addr)ef->address;
451 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
452 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
453 ef->ddbstrcnt = shdr[symstrindex].sh_size;
454 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
455 ef->shstrcnt = shdr[shstrindex].sh_size;
456 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
458 /* Now fill out progtab and the relocation tables. */
462 for (i = 0; i < hdr->e_shnum; i++) {
463 switch (shdr[i].sh_type) {
467 case SHT_X86_64_UNWIND:
469 if (shdr[i].sh_addr == 0)
471 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
472 if (shdr[i].sh_type == SHT_PROGBITS)
473 ef->progtab[pb].name = "<<PROGBITS>>";
475 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
476 ef->progtab[pb].name = "<<UNWIND>>";
479 ef->progtab[pb].name = "<<NOBITS>>";
480 ef->progtab[pb].size = shdr[i].sh_size;
481 ef->progtab[pb].flags = shdr[i].sh_flags;
482 ef->progtab[pb].sec = i;
483 if (ef->shstrtab && shdr[i].sh_name != 0)
484 ef->progtab[pb].name =
485 ef->shstrtab + shdr[i].sh_name;
486 if (ef->progtab[pb].name != NULL &&
487 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
490 dpcpu = dpcpu_alloc(shdr[i].sh_size);
492 printf("%s: pcpu module space is out "
493 "of space; cannot allocate %#jx "
494 "for %s\n", __func__,
495 (uintmax_t)shdr[i].sh_size,
500 memcpy(dpcpu, ef->progtab[pb].addr,
501 ef->progtab[pb].size);
502 dpcpu_copy(dpcpu, shdr[i].sh_size);
503 ef->progtab[pb].addr = dpcpu;
505 } else if (ef->progtab[pb].name != NULL &&
506 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
509 vnet_data = vnet_data_alloc(shdr[i].sh_size);
510 if (vnet_data == NULL) {
511 printf("%s: vnet module space is out "
512 "of space; cannot allocate %#jx "
513 "for %s\n", __func__,
514 (uintmax_t)shdr[i].sh_size,
519 memcpy(vnet_data, ef->progtab[pb].addr,
520 ef->progtab[pb].size);
521 vnet_data_copy(vnet_data, shdr[i].sh_size);
522 ef->progtab[pb].addr = vnet_data;
524 } else if (ef->progtab[pb].name != NULL &&
525 !strcmp(ef->progtab[pb].name, ".ctors")) {
526 lf->ctors_addr = ef->progtab[pb].addr;
527 lf->ctors_size = shdr[i].sh_size;
530 /* Update all symbol values with the offset. */
531 for (j = 0; j < ef->ddbsymcnt; j++) {
532 es = &ef->ddbsymtab[j];
533 if (es->st_shndx != i)
535 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
540 if (shdr[shdr[i].sh_info].sh_addr == 0)
542 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
543 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
544 ef->reltab[rl].sec = shdr[i].sh_info;
548 if (shdr[shdr[i].sh_info].sh_addr == 0)
550 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
551 ef->relatab[ra].nrela =
552 shdr[i].sh_size / sizeof(Elf_Rela);
553 ef->relatab[ra].sec = shdr[i].sh_info;
558 if (pb != ef->nprogtab) {
559 printf("%s: lost progbits\n", filename);
563 if (rl != ef->nreltab) {
564 printf("%s: lost reltab\n", filename);
568 if (ra != ef->nrelatab) {
569 printf("%s: lost relatab\n", filename);
575 * The file needs to be writeable and executable while applying
576 * relocations. Mapping protections are applied once relocation
577 * processing is complete.
579 link_elf_protect_range(ef, (vm_offset_t)ef->address,
580 round_page((vm_offset_t)ef->address + ef->lf.size), VM_PROT_ALL);
582 /* Local intra-module relocations */
583 error = link_elf_reloc_local(lf, false);
590 /* preload not done this way */
591 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
596 link_elf_invoke_ctors(caddr_t addr, size_t size)
601 if (addr == NULL || size == 0)
603 cnt = size / sizeof(*ctor);
605 for (i = 0; i < cnt; i++) {
612 link_elf_link_preload_finish(linker_file_t lf)
618 error = relocate_file(ef);
622 /* Notify MD code that a module is being loaded. */
623 error = elf_cpu_load_file(lf);
627 #if defined(__i386__) || defined(__amd64__)
629 error = link_elf_reloc_local(lf, true);
634 /* Apply protections now that relocation processing is complete. */
635 link_elf_protect(ef);
637 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
642 link_elf_load_file(linker_class_t cls, const char *filename,
643 linker_file_t *result)
645 struct nameidata *nd;
646 struct thread *td = curthread; /* XXX */
670 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
671 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
673 error = vn_open(nd, &flags, 0, NULL);
678 NDFREE(nd, NDF_ONLY_PNBUF);
679 if (nd->ni_vp->v_type != VREG) {
684 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
690 /* Read the elf header from the file. */
691 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
692 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
693 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
707 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
708 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
709 link_elf_error(filename, "Unsupported file layout");
713 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
714 || hdr->e_version != EV_CURRENT) {
715 link_elf_error(filename, "Unsupported file version");
719 if (hdr->e_type != ET_REL) {
723 if (hdr->e_machine != ELF_TARG_MACH) {
724 link_elf_error(filename, "Unsupported machine");
729 lf = linker_make_file(filename, &link_elf_class);
734 ef = (elf_file_t) lf;
740 /* Allocate and read in the section header */
741 nbytes = hdr->e_shnum * hdr->e_shentsize;
742 if (nbytes == 0 || hdr->e_shoff == 0 ||
743 hdr->e_shentsize != sizeof(Elf_Shdr)) {
747 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
749 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
750 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
759 /* Scan the section header for information and table sizing. */
763 for (i = 0; i < hdr->e_shnum; i++) {
764 if (shdr[i].sh_size == 0)
766 switch (shdr[i].sh_type) {
770 case SHT_X86_64_UNWIND:
772 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
779 symstrindex = shdr[i].sh_link;
783 * Ignore relocation tables for unallocated
786 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
791 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
799 if (ef->nprogtab == 0) {
800 link_elf_error(filename, "file has no contents");
805 /* Only allow one symbol table for now */
806 link_elf_error(filename,
807 "file must have exactly one symbol table");
811 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
812 shdr[symstrindex].sh_type != SHT_STRTAB) {
813 link_elf_error(filename, "file has invalid symbol strings");
818 /* Allocate space for tracking the load chunks */
819 if (ef->nprogtab != 0)
820 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
821 M_LINKER, M_WAITOK | M_ZERO);
822 if (ef->nreltab != 0)
823 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
824 M_LINKER, M_WAITOK | M_ZERO);
825 if (ef->nrelatab != 0)
826 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
827 M_LINKER, M_WAITOK | M_ZERO);
829 if (symtabindex == -1) {
830 link_elf_error(filename, "lost symbol table index");
834 /* Allocate space for and load the symbol table */
835 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
836 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
837 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
838 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
839 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
848 /* Allocate space for and load the symbol strings */
849 ef->ddbstrcnt = shdr[symstrindex].sh_size;
850 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
851 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
852 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
853 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
862 /* Do we have a string table for the section names? */
864 if (hdr->e_shstrndx != 0 &&
865 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
866 shstrindex = hdr->e_shstrndx;
867 ef->shstrcnt = shdr[shstrindex].sh_size;
868 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
870 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
871 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
872 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
882 /* Size up code/data(progbits) and bss(nobits). */
884 for (i = 0; i < hdr->e_shnum; i++) {
885 if (shdr[i].sh_size == 0)
887 switch (shdr[i].sh_type) {
891 case SHT_X86_64_UNWIND:
893 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
895 alignmask = shdr[i].sh_addralign - 1;
896 mapsize += alignmask;
897 mapsize &= ~alignmask;
898 mapsize += shdr[i].sh_size;
904 * We know how much space we need for the text/data/bss/etc.
905 * This stuff needs to be in a single chunk so that profiling etc
906 * can get the bounds and gdb can associate offsets with modules
908 ef->object = vm_object_allocate(OBJT_PHYS, atop(round_page(mapsize)));
909 if (ef->object == NULL) {
915 * In order to satisfy amd64's architectural requirements on the
916 * location of code and data in the kernel's address space, request a
917 * mapping that is above the kernel.
919 * Protections will be restricted once relocations are applied.
924 mapbase = VM_MIN_KERNEL_ADDRESS;
926 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
927 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
929 if (error != KERN_SUCCESS) {
930 vm_object_deallocate(ef->object);
937 error = vm_map_wire(kernel_map, mapbase,
938 mapbase + round_page(mapsize),
939 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
940 if (error != KERN_SUCCESS) {
945 /* Inform the kld system about the situation */
946 lf->address = ef->address = (caddr_t)mapbase;
950 * Now load code/data(progbits), zero bss(nobits), allocate space for
957 for (i = 0; i < hdr->e_shnum; i++) {
958 if (shdr[i].sh_size == 0)
960 switch (shdr[i].sh_type) {
964 case SHT_X86_64_UNWIND:
966 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
968 alignmask = shdr[i].sh_addralign - 1;
969 mapbase += alignmask;
970 mapbase &= ~alignmask;
971 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
972 ef->progtab[pb].name =
973 ef->shstrtab + shdr[i].sh_name;
974 if (!strcmp(ef->progtab[pb].name, ".ctors")) {
975 lf->ctors_addr = (caddr_t)mapbase;
976 lf->ctors_size = shdr[i].sh_size;
978 } else if (shdr[i].sh_type == SHT_PROGBITS)
979 ef->progtab[pb].name = "<<PROGBITS>>";
981 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
982 ef->progtab[pb].name = "<<UNWIND>>";
985 ef->progtab[pb].name = "<<NOBITS>>";
986 if (ef->progtab[pb].name != NULL &&
987 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
988 ef->progtab[pb].addr =
989 dpcpu_alloc(shdr[i].sh_size);
990 if (ef->progtab[pb].addr == NULL) {
991 printf("%s: pcpu module space is out "
992 "of space; cannot allocate %#jx "
993 "for %s\n", __func__,
994 (uintmax_t)shdr[i].sh_size,
999 else if (ef->progtab[pb].name != NULL &&
1000 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
1001 ef->progtab[pb].addr =
1002 vnet_data_alloc(shdr[i].sh_size);
1003 if (ef->progtab[pb].addr == NULL) {
1004 printf("%s: vnet module space is out "
1005 "of space; cannot allocate %#jx "
1006 "for %s\n", __func__,
1007 (uintmax_t)shdr[i].sh_size,
1013 ef->progtab[pb].addr =
1014 (void *)(uintptr_t)mapbase;
1015 if (ef->progtab[pb].addr == NULL) {
1019 ef->progtab[pb].size = shdr[i].sh_size;
1020 ef->progtab[pb].flags = shdr[i].sh_flags;
1021 ef->progtab[pb].sec = i;
1022 if (shdr[i].sh_type == SHT_PROGBITS
1024 || shdr[i].sh_type == SHT_X86_64_UNWIND
1027 error = vn_rdwr(UIO_READ, nd->ni_vp,
1028 ef->progtab[pb].addr,
1029 shdr[i].sh_size, shdr[i].sh_offset,
1030 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
1031 NOCRED, &resid, td);
1038 /* Initialize the per-cpu or vnet area. */
1039 if (ef->progtab[pb].addr != (void *)mapbase &&
1040 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
1041 dpcpu_copy(ef->progtab[pb].addr,
1044 else if (ef->progtab[pb].addr !=
1046 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
1047 vnet_data_copy(ef->progtab[pb].addr,
1051 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
1053 /* Update all symbol values with the offset. */
1054 for (j = 0; j < ef->ddbsymcnt; j++) {
1055 es = &ef->ddbsymtab[j];
1056 if (es->st_shndx != i)
1058 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
1060 mapbase += shdr[i].sh_size;
1064 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1066 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
1068 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
1069 ef->reltab[rl].sec = shdr[i].sh_info;
1070 error = vn_rdwr(UIO_READ, nd->ni_vp,
1071 (void *)ef->reltab[rl].rel,
1072 shdr[i].sh_size, shdr[i].sh_offset,
1073 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1084 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
1086 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
1088 ef->relatab[ra].nrela =
1089 shdr[i].sh_size / sizeof(Elf_Rela);
1090 ef->relatab[ra].sec = shdr[i].sh_info;
1091 error = vn_rdwr(UIO_READ, nd->ni_vp,
1092 (void *)ef->relatab[ra].rela,
1093 shdr[i].sh_size, shdr[i].sh_offset,
1094 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1106 if (pb != ef->nprogtab) {
1107 link_elf_error(filename, "lost progbits");
1111 if (rl != ef->nreltab) {
1112 link_elf_error(filename, "lost reltab");
1116 if (ra != ef->nrelatab) {
1117 link_elf_error(filename, "lost relatab");
1121 if (mapbase != (vm_offset_t)ef->address + mapsize) {
1123 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
1124 filename != NULL ? filename : "<none>",
1125 (u_long)mapbase, ef->address, (u_long)mapsize,
1126 (u_long)(vm_offset_t)ef->address + mapsize);
1131 /* Local intra-module relocations */
1132 error = link_elf_reloc_local(lf, false);
1136 /* Pull in dependencies */
1137 VOP_UNLOCK(nd->ni_vp);
1138 error = linker_load_dependencies(lf);
1139 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
1143 /* External relocations */
1144 error = relocate_file(ef);
1148 /* Notify MD code that a module is being loaded. */
1149 error = elf_cpu_load_file(lf);
1153 #if defined(__i386__) || defined(__amd64__)
1155 error = link_elf_reloc_local(lf, true);
1160 link_elf_protect(ef);
1161 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
1165 VOP_UNLOCK(nd->ni_vp);
1166 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1169 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1170 free(hdr, M_LINKER);
1176 link_elf_unload_file(linker_file_t file)
1178 elf_file_t ef = (elf_file_t) file;
1181 /* Notify MD code that a module is being unloaded. */
1182 elf_cpu_unload_file(file);
1185 for (i = 0; i < ef->nprogtab; i++) {
1186 if (ef->progtab[i].size == 0)
1188 if (ef->progtab[i].name == NULL)
1190 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1191 dpcpu_free(ef->progtab[i].addr,
1192 ef->progtab[i].size);
1194 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1195 vnet_data_free(ef->progtab[i].addr,
1196 ef->progtab[i].size);
1200 if (ef->preloaded) {
1201 free(ef->reltab, M_LINKER);
1202 free(ef->relatab, M_LINKER);
1203 free(ef->progtab, M_LINKER);
1204 free(ef->ctftab, M_LINKER);
1205 free(ef->ctfoff, M_LINKER);
1206 free(ef->typoff, M_LINKER);
1207 if (file->pathname != NULL)
1208 preload_delete_name(file->pathname);
1212 for (i = 0; i < ef->nreltab; i++)
1213 free(ef->reltab[i].rel, M_LINKER);
1214 for (i = 0; i < ef->nrelatab; i++)
1215 free(ef->relatab[i].rela, M_LINKER);
1216 free(ef->reltab, M_LINKER);
1217 free(ef->relatab, M_LINKER);
1218 free(ef->progtab, M_LINKER);
1220 if (ef->object != NULL)
1221 vm_map_remove(kernel_map, (vm_offset_t)ef->address,
1222 (vm_offset_t)ef->address + ptoa(ef->object->size));
1223 free(ef->e_shdr, M_LINKER);
1224 free(ef->ddbsymtab, M_LINKER);
1225 free(ef->ddbstrtab, M_LINKER);
1226 free(ef->shstrtab, M_LINKER);
1227 free(ef->ctftab, M_LINKER);
1228 free(ef->ctfoff, M_LINKER);
1229 free(ef->typoff, M_LINKER);
1233 symbol_name(elf_file_t ef, Elf_Size r_info)
1237 if (ELF_R_SYM(r_info)) {
1238 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1239 return ef->ddbstrtab + ref->st_name;
1245 findbase(elf_file_t ef, int sec)
1250 for (i = 0; i < ef->nprogtab; i++) {
1251 if (sec == ef->progtab[i].sec) {
1252 base = (Elf_Addr)ef->progtab[i].addr;
1260 relocate_file(elf_file_t ef)
1262 const Elf_Rel *rellim;
1264 const Elf_Rela *relalim;
1265 const Elf_Rela *rela;
1266 const char *symname;
1272 /* Perform relocations without addend if there are any: */
1273 for (i = 0; i < ef->nreltab; i++) {
1274 rel = ef->reltab[i].rel;
1276 link_elf_error(ef->lf.filename, "lost a reltab!");
1279 rellim = rel + ef->reltab[i].nrel;
1280 base = findbase(ef, ef->reltab[i].sec);
1282 link_elf_error(ef->lf.filename, "lost base for reltab");
1285 for ( ; rel < rellim; rel++) {
1286 symidx = ELF_R_SYM(rel->r_info);
1287 if (symidx >= ef->ddbsymcnt)
1289 sym = ef->ddbsymtab + symidx;
1290 /* Local relocs are already done */
1291 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1293 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1295 symname = symbol_name(ef, rel->r_info);
1296 printf("link_elf_obj: symbol %s undefined\n",
1303 /* Perform relocations with addend if there are any: */
1304 for (i = 0; i < ef->nrelatab; i++) {
1305 rela = ef->relatab[i].rela;
1307 link_elf_error(ef->lf.filename, "lost a relatab!");
1310 relalim = rela + ef->relatab[i].nrela;
1311 base = findbase(ef, ef->relatab[i].sec);
1313 link_elf_error(ef->lf.filename,
1314 "lost base for relatab");
1317 for ( ; rela < relalim; rela++) {
1318 symidx = ELF_R_SYM(rela->r_info);
1319 if (symidx >= ef->ddbsymcnt)
1321 sym = ef->ddbsymtab + symidx;
1322 /* Local relocs are already done */
1323 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1325 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1327 symname = symbol_name(ef, rela->r_info);
1328 printf("link_elf_obj: symbol %s undefined\n",
1336 * Only clean SHN_FBSD_CACHED for successful return. If we
1337 * modified symbol table for the object but found an
1338 * unresolved symbol, there is no reason to roll back.
1340 elf_obj_cleanup_globals_cache(ef);
1346 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1348 elf_file_t ef = (elf_file_t) lf;
1349 const Elf_Sym *symp;
1353 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1354 strp = ef->ddbstrtab + symp->st_name;
1355 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1356 *sym = (c_linker_sym_t) symp;
1364 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1365 linker_symval_t *symval)
1371 ef = (elf_file_t) lf;
1372 es = (const Elf_Sym*) sym;
1373 val = (caddr_t)es->st_value;
1374 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1375 symval->name = ef->ddbstrtab + es->st_name;
1376 val = (caddr_t)es->st_value;
1377 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1378 val = ((caddr_t (*)(void))val)();
1379 symval->value = val;
1380 symval->size = es->st_size;
1387 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1388 c_linker_sym_t *sym, long *diffp)
1390 elf_file_t ef = (elf_file_t) lf;
1391 u_long off = (uintptr_t) (void *) value;
1395 const Elf_Sym *best = NULL;
1398 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1399 if (es->st_name == 0)
1401 st_value = es->st_value;
1402 if (off >= st_value) {
1403 if (off - st_value < diff) {
1404 diff = off - st_value;
1408 } else if (off - st_value == diff) {
1417 *sym = (c_linker_sym_t) best;
1423 * Look up a linker set on an ELF system.
1426 link_elf_lookup_set(linker_file_t lf, const char *name,
1427 void ***startp, void ***stopp, int *countp)
1429 elf_file_t ef = (elf_file_t)lf;
1430 void **start, **stop;
1433 /* Relative to section number */
1434 for (i = 0; i < ef->nprogtab; i++) {
1435 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1436 strcmp(ef->progtab[i].name + 4, name) == 0) {
1437 start = (void **)ef->progtab[i].addr;
1438 stop = (void **)((char *)ef->progtab[i].addr +
1439 ef->progtab[i].size);
1440 count = stop - start;
1454 link_elf_each_function_name(linker_file_t file,
1455 int (*callback)(const char *, void *), void *opaque)
1457 elf_file_t ef = (elf_file_t)file;
1458 const Elf_Sym *symp;
1461 /* Exhaustive search */
1462 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1463 if (symp->st_value != 0 &&
1464 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1465 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1466 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1475 link_elf_each_function_nameval(linker_file_t file,
1476 linker_function_nameval_callback_t callback, void *opaque)
1478 linker_symval_t symval;
1479 elf_file_t ef = (elf_file_t)file;
1480 const Elf_Sym* symp;
1483 /* Exhaustive search */
1484 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1485 if (symp->st_value != 0 &&
1486 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1487 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1488 error = link_elf_symbol_values(file,
1489 (c_linker_sym_t)symp, &symval);
1492 error = callback(file, i, &symval, opaque);
1501 elf_obj_cleanup_globals_cache(elf_file_t ef)
1506 for (i = 0; i < ef->ddbsymcnt; i++) {
1507 sym = ef->ddbsymtab + i;
1508 if (sym->st_shndx == SHN_FBSD_CACHED) {
1509 sym->st_shndx = SHN_UNDEF;
1516 * Symbol lookup function that can be used when the symbol index is known (ie
1517 * in relocations). It uses the symbol index instead of doing a fully fledged
1518 * hash table based lookup when such is valid. For example for local symbols.
1519 * This is not only more efficient, it's also more correct. It's not always
1520 * the case that the symbol can be found through the hash table.
1523 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1525 elf_file_t ef = (elf_file_t)lf;
1530 /* Don't even try to lookup the symbol if the index is bogus. */
1531 if (symidx >= ef->ddbsymcnt) {
1536 sym = ef->ddbsymtab + symidx;
1538 /* Quick answer if there is a definition included. */
1539 if (sym->st_shndx != SHN_UNDEF) {
1540 res1 = (Elf_Addr)sym->st_value;
1541 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1542 res1 = ((Elf_Addr (*)(void))res1)();
1547 /* If we get here, then it is undefined and needs a lookup. */
1548 switch (ELF_ST_BIND(sym->st_info)) {
1550 /* Local, but undefined? huh? */
1556 /* Relative to Data or Function name */
1557 symbol = ef->ddbstrtab + sym->st_name;
1559 /* Force a lookup failure if the symbol name is bogus. */
1564 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1567 * Cache global lookups during module relocation. The failure
1568 * case is particularly expensive for callers, who must scan
1569 * through the entire globals table doing strcmp(). Cache to
1570 * avoid doing such work repeatedly.
1572 * After relocation is complete, undefined globals will be
1573 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1577 sym->st_shndx = SHN_FBSD_CACHED;
1578 sym->st_value = res1;
1581 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1594 link_elf_fix_link_set(elf_file_t ef)
1596 static const char startn[] = "__start_";
1597 static const char stopn[] = "__stop_";
1599 const char *sym_name, *linkset_name;
1600 Elf_Addr startp, stopp;
1605 for (symidx = 1 /* zero entry is special */;
1606 symidx < ef->ddbsymcnt; symidx++) {
1607 sym = ef->ddbsymtab + symidx;
1608 if (sym->st_shndx != SHN_UNDEF)
1611 sym_name = ef->ddbstrtab + sym->st_name;
1612 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1614 linkset_name = sym_name + sizeof(startn) - 1;
1616 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1618 linkset_name = sym_name + sizeof(stopn) - 1;
1623 for (i = 0; i < ef->nprogtab; i++) {
1624 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1625 startp = (Elf_Addr)ef->progtab[i].addr;
1626 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1630 if (i == ef->nprogtab)
1633 sym->st_value = start ? startp : stopp;
1639 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1641 elf_file_t ef = (elf_file_t)lf;
1642 const Elf_Rel *rellim;
1644 const Elf_Rela *relalim;
1645 const Elf_Rela *rela;
1651 link_elf_fix_link_set(ef);
1653 /* Perform relocations without addend if there are any: */
1654 for (i = 0; i < ef->nreltab; i++) {
1655 rel = ef->reltab[i].rel;
1657 link_elf_error(ef->lf.filename, "lost a reltab");
1660 rellim = rel + ef->reltab[i].nrel;
1661 base = findbase(ef, ef->reltab[i].sec);
1663 link_elf_error(ef->lf.filename, "lost base for reltab");
1666 for ( ; rel < rellim; rel++) {
1667 symidx = ELF_R_SYM(rel->r_info);
1668 if (symidx >= ef->ddbsymcnt)
1670 sym = ef->ddbsymtab + symidx;
1671 /* Only do local relocs */
1672 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1674 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1675 elf_is_ifunc_reloc(rel->r_info)) == ifuncs)
1676 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1681 /* Perform relocations with addend if there are any: */
1682 for (i = 0; i < ef->nrelatab; i++) {
1683 rela = ef->relatab[i].rela;
1685 link_elf_error(ef->lf.filename, "lost a relatab!");
1688 relalim = rela + ef->relatab[i].nrela;
1689 base = findbase(ef, ef->relatab[i].sec);
1691 link_elf_error(ef->lf.filename, "lost base for reltab");
1694 for ( ; rela < relalim; rela++) {
1695 symidx = ELF_R_SYM(rela->r_info);
1696 if (symidx >= ef->ddbsymcnt)
1698 sym = ef->ddbsymtab + symidx;
1699 /* Only do local relocs */
1700 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1702 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1703 elf_is_ifunc_reloc(rela->r_info)) == ifuncs)
1704 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1712 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1714 elf_file_t ef = (elf_file_t)lf;
1716 *symtab = ef->ddbsymtab;
1718 if (*symtab == NULL)
1721 return (ef->ddbsymcnt);
1725 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1727 elf_file_t ef = (elf_file_t)lf;
1729 *strtab = ef->ddbstrtab;
1731 if (*strtab == NULL)
1734 return (ef->ddbstrcnt);