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"
74 int sec; /* Original section */
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);
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, 0);
200 link_elf_link_preload(linker_class_t cls, const char *filename,
201 linker_file_t *result)
206 void *modptr, *baseptr, *sizeptr;
211 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
213 /* Look to see if we have the file preloaded */
214 modptr = preload_search_by_name(filename);
218 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
219 baseptr = preload_search_info(modptr, MODINFO_ADDR);
220 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
221 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
223 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
225 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
226 " obj module") != 0 &&
227 strcmp(type, "elf obj module") != 0)) {
230 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
234 lf = linker_make_file(filename, &link_elf_class);
240 ef->address = *(caddr_t *)baseptr;
241 lf->address = *(caddr_t *)baseptr;
242 lf->size = *(size_t *)sizeptr;
244 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
245 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
246 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
247 hdr->e_version != EV_CURRENT ||
248 hdr->e_type != ET_REL ||
249 hdr->e_machine != ELF_TARG_MACH) {
255 /* Scan the section header for information and table sizing. */
258 for (i = 0; i < hdr->e_shnum; i++) {
259 switch (shdr[i].sh_type) {
263 case SHT_X86_64_UNWIND:
265 /* Ignore sections not loaded by the loader. */
266 if (shdr[i].sh_addr == 0)
272 symstrindex = shdr[i].sh_link;
276 * Ignore relocation tables for sections not
277 * loaded by the loader.
279 if (shdr[shdr[i].sh_info].sh_addr == 0)
284 if (shdr[shdr[i].sh_info].sh_addr == 0)
291 shstrindex = hdr->e_shstrndx;
292 if (ef->nprogtab == 0 || symstrindex < 0 ||
293 symstrindex >= hdr->e_shnum ||
294 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
295 shstrindex >= hdr->e_shnum ||
296 shdr[shstrindex].sh_type != SHT_STRTAB) {
297 printf("%s: bad/missing section headers\n", filename);
302 /* Allocate space for tracking the load chunks */
303 if (ef->nprogtab != 0)
304 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
305 M_LINKER, M_WAITOK | M_ZERO);
306 if (ef->nreltab != 0)
307 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
308 M_LINKER, M_WAITOK | M_ZERO);
309 if (ef->nrelatab != 0)
310 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
311 M_LINKER, M_WAITOK | M_ZERO);
312 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
313 (ef->nreltab != 0 && ef->reltab == NULL) ||
314 (ef->nrelatab != 0 && ef->relatab == NULL)) {
319 /* XXX, relocate the sh_addr fields saved by the loader. */
321 for (i = 0; i < hdr->e_shnum; i++) {
322 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
323 off = shdr[i].sh_addr;
325 for (i = 0; i < hdr->e_shnum; i++) {
326 if (shdr[i].sh_addr != 0)
327 shdr[i].sh_addr = shdr[i].sh_addr - off +
328 (Elf_Addr)ef->address;
331 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
332 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
333 ef->ddbstrcnt = shdr[symstrindex].sh_size;
334 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
335 ef->shstrcnt = shdr[shstrindex].sh_size;
336 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
338 /* Now fill out progtab and the relocation tables. */
342 for (i = 0; i < hdr->e_shnum; i++) {
343 switch (shdr[i].sh_type) {
347 case SHT_X86_64_UNWIND:
349 if (shdr[i].sh_addr == 0)
351 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
352 if (shdr[i].sh_type == SHT_PROGBITS)
353 ef->progtab[pb].name = "<<PROGBITS>>";
355 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
356 ef->progtab[pb].name = "<<UNWIND>>";
359 ef->progtab[pb].name = "<<NOBITS>>";
360 ef->progtab[pb].size = shdr[i].sh_size;
361 ef->progtab[pb].sec = i;
362 if (ef->shstrtab && shdr[i].sh_name != 0)
363 ef->progtab[pb].name =
364 ef->shstrtab + shdr[i].sh_name;
365 if (ef->progtab[pb].name != NULL &&
366 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
369 dpcpu = dpcpu_alloc(shdr[i].sh_size);
374 memcpy(dpcpu, ef->progtab[pb].addr,
375 ef->progtab[pb].size);
376 dpcpu_copy(dpcpu, shdr[i].sh_size);
377 ef->progtab[pb].addr = dpcpu;
379 } else if (ef->progtab[pb].name != NULL &&
380 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
383 vnet_data = vnet_data_alloc(shdr[i].sh_size);
384 if (vnet_data == NULL) {
388 memcpy(vnet_data, ef->progtab[pb].addr,
389 ef->progtab[pb].size);
390 vnet_data_copy(vnet_data, shdr[i].sh_size);
391 ef->progtab[pb].addr = vnet_data;
393 } else if (ef->progtab[pb].name != NULL &&
394 !strcmp(ef->progtab[pb].name, ".ctors")) {
395 lf->ctors_addr = ef->progtab[pb].addr;
396 lf->ctors_size = shdr[i].sh_size;
399 /* Update all symbol values with the offset. */
400 for (j = 0; j < ef->ddbsymcnt; j++) {
401 es = &ef->ddbsymtab[j];
402 if (es->st_shndx != i)
404 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
409 if (shdr[shdr[i].sh_info].sh_addr == 0)
411 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
412 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
413 ef->reltab[rl].sec = shdr[i].sh_info;
417 if (shdr[shdr[i].sh_info].sh_addr == 0)
419 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
420 ef->relatab[ra].nrela =
421 shdr[i].sh_size / sizeof(Elf_Rela);
422 ef->relatab[ra].sec = shdr[i].sh_info;
427 if (pb != ef->nprogtab) {
428 printf("%s: lost progbits\n", filename);
432 if (rl != ef->nreltab) {
433 printf("%s: lost reltab\n", filename);
437 if (ra != ef->nrelatab) {
438 printf("%s: lost relatab\n", filename);
443 /* Local intra-module relocations */
444 error = link_elf_reloc_local(lf);
452 /* preload not done this way */
453 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
458 link_elf_invoke_ctors(caddr_t addr, size_t size)
463 if (addr == NULL || size == 0)
465 cnt = size / sizeof(*ctor);
467 for (i = 0; i < cnt; i++) {
474 link_elf_link_preload_finish(linker_file_t lf)
480 error = relocate_file(ef);
484 /* Notify MD code that a module is being loaded. */
485 error = elf_cpu_load_file(lf);
490 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
495 link_elf_load_file(linker_class_t cls, const char *filename,
496 linker_file_t *result)
498 struct nameidata *nd;
499 struct thread *td = curthread; /* XXX */
523 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
524 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
526 error = vn_open(nd, &flags, 0, NULL);
531 NDFREE(nd, NDF_ONLY_PNBUF);
532 if (nd->ni_vp->v_type != VREG) {
537 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
543 /* Read the elf header from the file. */
544 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
545 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
546 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
560 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
561 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
562 link_elf_error(filename, "Unsupported file layout");
566 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
567 || hdr->e_version != EV_CURRENT) {
568 link_elf_error(filename, "Unsupported file version");
572 if (hdr->e_type != ET_REL) {
576 if (hdr->e_machine != ELF_TARG_MACH) {
577 link_elf_error(filename, "Unsupported machine");
582 lf = linker_make_file(filename, &link_elf_class);
587 ef = (elf_file_t) lf;
593 /* Allocate and read in the section header */
594 nbytes = hdr->e_shnum * hdr->e_shentsize;
595 if (nbytes == 0 || hdr->e_shoff == 0 ||
596 hdr->e_shentsize != sizeof(Elf_Shdr)) {
600 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
602 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
603 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
612 /* Scan the section header for information and table sizing. */
616 for (i = 0; i < hdr->e_shnum; i++) {
617 if (shdr[i].sh_size == 0)
619 switch (shdr[i].sh_type) {
623 case SHT_X86_64_UNWIND:
625 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
632 symstrindex = shdr[i].sh_link;
636 * Ignore relocation tables for unallocated
639 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
644 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
652 if (ef->nprogtab == 0) {
653 link_elf_error(filename, "file has no contents");
658 /* Only allow one symbol table for now */
659 link_elf_error(filename, "file has no valid symbol table");
663 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
664 shdr[symstrindex].sh_type != SHT_STRTAB) {
665 link_elf_error(filename, "file has invalid symbol strings");
670 /* Allocate space for tracking the load chunks */
671 if (ef->nprogtab != 0)
672 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
673 M_LINKER, M_WAITOK | M_ZERO);
674 if (ef->nreltab != 0)
675 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
676 M_LINKER, M_WAITOK | M_ZERO);
677 if (ef->nrelatab != 0)
678 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
679 M_LINKER, M_WAITOK | M_ZERO);
681 if (symtabindex == -1) {
682 link_elf_error(filename, "lost symbol table index");
686 /* Allocate space for and load the symbol table */
687 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
688 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
689 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
690 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
691 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
700 if (symstrindex == -1) {
701 link_elf_error(filename, "lost symbol string index");
705 /* Allocate space for and load the symbol strings */
706 ef->ddbstrcnt = shdr[symstrindex].sh_size;
707 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
708 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
709 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
710 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
719 /* Do we have a string table for the section names? */
721 if (hdr->e_shstrndx != 0 &&
722 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
723 shstrindex = hdr->e_shstrndx;
724 ef->shstrcnt = shdr[shstrindex].sh_size;
725 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
727 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
728 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
729 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
739 /* Size up code/data(progbits) and bss(nobits). */
741 for (i = 0; i < hdr->e_shnum; i++) {
742 if (shdr[i].sh_size == 0)
744 switch (shdr[i].sh_type) {
748 case SHT_X86_64_UNWIND:
750 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
752 alignmask = shdr[i].sh_addralign - 1;
753 mapsize += alignmask;
754 mapsize &= ~alignmask;
755 mapsize += shdr[i].sh_size;
761 * We know how much space we need for the text/data/bss/etc.
762 * This stuff needs to be in a single chunk so that profiling etc
763 * can get the bounds and gdb can associate offsets with modules
765 ef->object = vm_object_allocate(OBJT_DEFAULT,
766 round_page(mapsize) >> PAGE_SHIFT);
767 if (ef->object == NULL) {
771 ef->address = (caddr_t) vm_map_min(kernel_map);
774 * In order to satisfy amd64's architectural requirements on the
775 * location of code and data in the kernel's address space, request a
776 * mapping that is above the kernel.
781 mapbase = VM_MIN_KERNEL_ADDRESS;
783 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
784 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
787 vm_object_deallocate(ef->object);
793 error = vm_map_wire(kernel_map, mapbase,
794 mapbase + round_page(mapsize),
795 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
796 if (error != KERN_SUCCESS) {
801 /* Inform the kld system about the situation */
802 lf->address = ef->address = (caddr_t)mapbase;
806 * Now load code/data(progbits), zero bss(nobits), allocate space for
813 for (i = 0; i < hdr->e_shnum; i++) {
814 if (shdr[i].sh_size == 0)
816 switch (shdr[i].sh_type) {
820 case SHT_X86_64_UNWIND:
822 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
824 alignmask = shdr[i].sh_addralign - 1;
825 mapbase += alignmask;
826 mapbase &= ~alignmask;
827 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
828 ef->progtab[pb].name =
829 ef->shstrtab + shdr[i].sh_name;
830 if (!strcmp(ef->progtab[pb].name, ".ctors")) {
831 lf->ctors_addr = (caddr_t)mapbase;
832 lf->ctors_size = shdr[i].sh_size;
834 } else if (shdr[i].sh_type == SHT_PROGBITS)
835 ef->progtab[pb].name = "<<PROGBITS>>";
837 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
838 ef->progtab[pb].name = "<<UNWIND>>";
841 ef->progtab[pb].name = "<<NOBITS>>";
842 if (ef->progtab[pb].name != NULL &&
843 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
844 ef->progtab[pb].addr =
845 dpcpu_alloc(shdr[i].sh_size);
847 else if (ef->progtab[pb].name != NULL &&
848 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
849 ef->progtab[pb].addr =
850 vnet_data_alloc(shdr[i].sh_size);
853 ef->progtab[pb].addr =
854 (void *)(uintptr_t)mapbase;
855 if (ef->progtab[pb].addr == NULL) {
859 ef->progtab[pb].size = shdr[i].sh_size;
860 ef->progtab[pb].sec = i;
861 if (shdr[i].sh_type == SHT_PROGBITS
863 || shdr[i].sh_type == SHT_X86_64_UNWIND
866 error = vn_rdwr(UIO_READ, nd->ni_vp,
867 ef->progtab[pb].addr,
868 shdr[i].sh_size, shdr[i].sh_offset,
869 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
877 /* Initialize the per-cpu or vnet area. */
878 if (ef->progtab[pb].addr != (void *)mapbase &&
879 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
880 dpcpu_copy(ef->progtab[pb].addr,
883 else if (ef->progtab[pb].addr !=
885 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
886 vnet_data_copy(ef->progtab[pb].addr,
890 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
892 /* Update all symbol values with the offset. */
893 for (j = 0; j < ef->ddbsymcnt; j++) {
894 es = &ef->ddbsymtab[j];
895 if (es->st_shndx != i)
897 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
899 mapbase += shdr[i].sh_size;
903 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
905 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
907 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
908 ef->reltab[rl].sec = shdr[i].sh_info;
909 error = vn_rdwr(UIO_READ, nd->ni_vp,
910 (void *)ef->reltab[rl].rel,
911 shdr[i].sh_size, shdr[i].sh_offset,
912 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
923 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
925 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
927 ef->relatab[ra].nrela =
928 shdr[i].sh_size / sizeof(Elf_Rela);
929 ef->relatab[ra].sec = shdr[i].sh_info;
930 error = vn_rdwr(UIO_READ, nd->ni_vp,
931 (void *)ef->relatab[ra].rela,
932 shdr[i].sh_size, shdr[i].sh_offset,
933 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
945 if (pb != ef->nprogtab) {
946 link_elf_error(filename, "lost progbits");
950 if (rl != ef->nreltab) {
951 link_elf_error(filename, "lost reltab");
955 if (ra != ef->nrelatab) {
956 link_elf_error(filename, "lost relatab");
960 if (mapbase != (vm_offset_t)ef->address + mapsize) {
962 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
963 filename != NULL ? filename : "<none>",
964 (u_long)mapbase, ef->address, (u_long)mapsize,
965 (u_long)(vm_offset_t)ef->address + mapsize);
970 /* Local intra-module relocations */
971 error = link_elf_reloc_local(lf);
975 /* Pull in dependencies */
976 VOP_UNLOCK(nd->ni_vp, 0);
977 error = linker_load_dependencies(lf);
978 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
982 /* External relocations */
983 error = relocate_file(ef);
987 /* Notify MD code that a module is being loaded. */
988 error = elf_cpu_load_file(lf);
993 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
998 VOP_UNLOCK(nd->ni_vp, 0);
999 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
1002 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1003 free(hdr, M_LINKER);
1009 link_elf_unload_file(linker_file_t file)
1011 elf_file_t ef = (elf_file_t) file;
1014 /* Notify MD code that a module is being unloaded. */
1015 elf_cpu_unload_file(file);
1018 for (i = 0; i < ef->nprogtab; i++) {
1019 if (ef->progtab[i].size == 0)
1021 if (ef->progtab[i].name == NULL)
1023 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1024 dpcpu_free(ef->progtab[i].addr,
1025 ef->progtab[i].size);
1027 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1028 vnet_data_free(ef->progtab[i].addr,
1029 ef->progtab[i].size);
1033 if (ef->preloaded) {
1034 free(ef->reltab, M_LINKER);
1035 free(ef->relatab, M_LINKER);
1036 free(ef->progtab, M_LINKER);
1037 free(ef->ctftab, M_LINKER);
1038 free(ef->ctfoff, M_LINKER);
1039 free(ef->typoff, M_LINKER);
1040 if (file->filename != NULL)
1041 preload_delete_name(file->filename);
1042 /* XXX reclaim module memory? */
1046 for (i = 0; i < ef->nreltab; i++)
1047 free(ef->reltab[i].rel, M_LINKER);
1048 for (i = 0; i < ef->nrelatab; i++)
1049 free(ef->relatab[i].rela, M_LINKER);
1050 free(ef->reltab, M_LINKER);
1051 free(ef->relatab, M_LINKER);
1052 free(ef->progtab, M_LINKER);
1055 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
1056 (vm_offset_t) ef->address +
1057 (ef->object->size << PAGE_SHIFT));
1059 free(ef->e_shdr, M_LINKER);
1060 free(ef->ddbsymtab, M_LINKER);
1061 free(ef->ddbstrtab, M_LINKER);
1062 free(ef->shstrtab, M_LINKER);
1063 free(ef->ctftab, M_LINKER);
1064 free(ef->ctfoff, M_LINKER);
1065 free(ef->typoff, M_LINKER);
1069 symbol_name(elf_file_t ef, Elf_Size r_info)
1073 if (ELF_R_SYM(r_info)) {
1074 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1075 return ef->ddbstrtab + ref->st_name;
1081 findbase(elf_file_t ef, int sec)
1086 for (i = 0; i < ef->nprogtab; i++) {
1087 if (sec == ef->progtab[i].sec) {
1088 base = (Elf_Addr)ef->progtab[i].addr;
1096 relocate_file(elf_file_t ef)
1098 const Elf_Rel *rellim;
1100 const Elf_Rela *relalim;
1101 const Elf_Rela *rela;
1102 const char *symname;
1109 /* Perform relocations without addend if there are any: */
1110 for (i = 0; i < ef->nreltab; i++) {
1111 rel = ef->reltab[i].rel;
1113 link_elf_error(ef->lf.filename, "lost a reltab!");
1116 rellim = rel + ef->reltab[i].nrel;
1117 base = findbase(ef, ef->reltab[i].sec);
1119 link_elf_error(ef->lf.filename, "lost base for reltab");
1122 for ( ; rel < rellim; rel++) {
1123 symidx = ELF_R_SYM(rel->r_info);
1124 if (symidx >= ef->ddbsymcnt)
1126 sym = ef->ddbsymtab + symidx;
1127 /* Local relocs are already done */
1128 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1130 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1132 symname = symbol_name(ef, rel->r_info);
1133 printf("link_elf_obj: symbol %s undefined\n",
1140 /* Perform relocations with addend if there are any: */
1141 for (i = 0; i < ef->nrelatab; i++) {
1142 rela = ef->relatab[i].rela;
1144 link_elf_error(ef->lf.filename, "lost a relatab!");
1147 relalim = rela + ef->relatab[i].nrela;
1148 base = findbase(ef, ef->relatab[i].sec);
1150 link_elf_error(ef->lf.filename,
1151 "lost base for relatab");
1154 for ( ; rela < relalim; rela++) {
1155 symidx = ELF_R_SYM(rela->r_info);
1156 if (symidx >= ef->ddbsymcnt)
1158 sym = ef->ddbsymtab + symidx;
1159 /* Local relocs are already done */
1160 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1162 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1164 symname = symbol_name(ef, rela->r_info);
1165 printf("link_elf_obj: symbol %s undefined\n",
1173 * Only clean SHN_FBSD_CACHED for successful return. If we
1174 * modified symbol table for the object but found an
1175 * unresolved symbol, there is no reason to roll back.
1177 elf_obj_cleanup_globals_cache(ef);
1183 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1185 elf_file_t ef = (elf_file_t) lf;
1186 const Elf_Sym *symp;
1190 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1191 strp = ef->ddbstrtab + symp->st_name;
1192 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1193 *sym = (c_linker_sym_t) symp;
1201 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1202 linker_symval_t *symval)
1204 elf_file_t ef = (elf_file_t) lf;
1205 const Elf_Sym *es = (const Elf_Sym*) sym;
1207 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1208 symval->name = ef->ddbstrtab + es->st_name;
1209 symval->value = (caddr_t)es->st_value;
1210 symval->size = es->st_size;
1217 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1218 c_linker_sym_t *sym, long *diffp)
1220 elf_file_t ef = (elf_file_t) lf;
1221 u_long off = (uintptr_t) (void *) value;
1225 const Elf_Sym *best = NULL;
1228 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1229 if (es->st_name == 0)
1231 st_value = es->st_value;
1232 if (off >= st_value) {
1233 if (off - st_value < diff) {
1234 diff = off - st_value;
1238 } else if (off - st_value == diff) {
1247 *sym = (c_linker_sym_t) best;
1253 * Look up a linker set on an ELF system.
1256 link_elf_lookup_set(linker_file_t lf, const char *name,
1257 void ***startp, void ***stopp, int *countp)
1259 elf_file_t ef = (elf_file_t)lf;
1260 void **start, **stop;
1263 /* Relative to section number */
1264 for (i = 0; i < ef->nprogtab; i++) {
1265 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1266 strcmp(ef->progtab[i].name + 4, name) == 0) {
1267 start = (void **)ef->progtab[i].addr;
1268 stop = (void **)((char *)ef->progtab[i].addr +
1269 ef->progtab[i].size);
1270 count = stop - start;
1284 link_elf_each_function_name(linker_file_t file,
1285 int (*callback)(const char *, void *), void *opaque)
1287 elf_file_t ef = (elf_file_t)file;
1288 const Elf_Sym *symp;
1291 /* Exhaustive search */
1292 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1293 if (symp->st_value != 0 &&
1294 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1295 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1304 link_elf_each_function_nameval(linker_file_t file,
1305 linker_function_nameval_callback_t callback, void *opaque)
1307 linker_symval_t symval;
1308 elf_file_t ef = (elf_file_t)file;
1309 const Elf_Sym* symp;
1312 /* Exhaustive search */
1313 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1314 if (symp->st_value != 0 &&
1315 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1316 error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval);
1319 error = callback(file, i, &symval, opaque);
1328 elf_obj_cleanup_globals_cache(elf_file_t ef)
1333 for (i = 0; i < ef->ddbsymcnt; i++) {
1334 sym = ef->ddbsymtab + i;
1335 if (sym->st_shndx == SHN_FBSD_CACHED) {
1336 sym->st_shndx = SHN_UNDEF;
1343 * Symbol lookup function that can be used when the symbol index is known (ie
1344 * in relocations). It uses the symbol index instead of doing a fully fledged
1345 * hash table based lookup when such is valid. For example for local symbols.
1346 * This is not only more efficient, it's also more correct. It's not always
1347 * the case that the symbol can be found through the hash table.
1350 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1352 elf_file_t ef = (elf_file_t)lf;
1357 /* Don't even try to lookup the symbol if the index is bogus. */
1358 if (symidx >= ef->ddbsymcnt) {
1363 sym = ef->ddbsymtab + symidx;
1365 /* Quick answer if there is a definition included. */
1366 if (sym->st_shndx != SHN_UNDEF) {
1367 *res = sym->st_value;
1371 /* If we get here, then it is undefined and needs a lookup. */
1372 switch (ELF_ST_BIND(sym->st_info)) {
1374 /* Local, but undefined? huh? */
1380 /* Relative to Data or Function name */
1381 symbol = ef->ddbstrtab + sym->st_name;
1383 /* Force a lookup failure if the symbol name is bogus. */
1388 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1391 * Cache global lookups during module relocation. The failure
1392 * case is particularly expensive for callers, who must scan
1393 * through the entire globals table doing strcmp(). Cache to
1394 * avoid doing such work repeatedly.
1396 * After relocation is complete, undefined globals will be
1397 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1401 sym->st_shndx = SHN_FBSD_CACHED;
1402 sym->st_value = res1;
1405 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1418 link_elf_fix_link_set(elf_file_t ef)
1420 static const char startn[] = "__start_";
1421 static const char stopn[] = "__stop_";
1423 const char *sym_name, *linkset_name;
1424 Elf_Addr startp, stopp;
1429 for (symidx = 1 /* zero entry is special */;
1430 symidx < ef->ddbsymcnt; symidx++) {
1431 sym = ef->ddbsymtab + symidx;
1432 if (sym->st_shndx != SHN_UNDEF)
1435 sym_name = ef->ddbstrtab + sym->st_name;
1436 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1438 linkset_name = sym_name + sizeof(startn) - 1;
1440 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1442 linkset_name = sym_name + sizeof(stopn) - 1;
1447 for (i = 0; i < ef->nprogtab; i++) {
1448 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1449 startp = (Elf_Addr)ef->progtab[i].addr;
1450 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1454 if (i == ef->nprogtab)
1457 sym->st_value = start ? startp : stopp;
1463 link_elf_reloc_local(linker_file_t lf)
1465 elf_file_t ef = (elf_file_t)lf;
1466 const Elf_Rel *rellim;
1468 const Elf_Rela *relalim;
1469 const Elf_Rela *rela;
1475 link_elf_fix_link_set(ef);
1477 /* Perform relocations without addend if there are any: */
1478 for (i = 0; i < ef->nreltab; i++) {
1479 rel = ef->reltab[i].rel;
1481 link_elf_error(ef->lf.filename, "lost a reltab");
1484 rellim = rel + ef->reltab[i].nrel;
1485 base = findbase(ef, ef->reltab[i].sec);
1487 link_elf_error(ef->lf.filename, "lost base for reltab");
1490 for ( ; rel < rellim; rel++) {
1491 symidx = ELF_R_SYM(rel->r_info);
1492 if (symidx >= ef->ddbsymcnt)
1494 sym = ef->ddbsymtab + symidx;
1495 /* Only do local relocs */
1496 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1498 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1503 /* Perform relocations with addend if there are any: */
1504 for (i = 0; i < ef->nrelatab; i++) {
1505 rela = ef->relatab[i].rela;
1507 link_elf_error(ef->lf.filename, "lost a relatab!");
1510 relalim = rela + ef->relatab[i].nrela;
1511 base = findbase(ef, ef->relatab[i].sec);
1513 link_elf_error(ef->lf.filename, "lost base for reltab");
1516 for ( ; rela < relalim; rela++) {
1517 symidx = ELF_R_SYM(rela->r_info);
1518 if (symidx >= ef->ddbsymcnt)
1520 sym = ef->ddbsymtab + symidx;
1521 /* Only do local relocs */
1522 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1524 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1532 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1534 elf_file_t ef = (elf_file_t)lf;
1536 *symtab = ef->ddbsymtab;
1538 if (*symtab == NULL)
1541 return (ef->ddbsymcnt);
1545 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1547 elf_file_t ef = (elf_file_t)lf;
1549 *strtab = ef->ddbstrtab;
1551 if (*strtab == NULL)
1554 return (ef->ddbstrcnt);