2 * Copyright (c) 1998-2000 Doug Rabson
3 * Copyright (c) 2004 Peter Wemm
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/mount.h>
41 #include <sys/namei.h>
42 #include <sys/fcntl.h>
43 #include <sys/vnode.h>
44 #include <sys/linker.h>
46 #include <machine/elf.h>
50 #include <security/mac/mac_framework.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_kern.h>
56 #include <vm/vm_extern.h>
58 #include <vm/vm_map.h>
60 #include <sys/link_elf.h>
66 #include "linker_if.h"
72 int sec; /* Original section */
89 typedef struct elf_file {
90 struct linker_file lf; /* Common fields */
93 caddr_t address; /* Relocation address */
94 vm_object_t object; /* VM object to hold file pages */
100 Elf_relaent *relatab;
106 Elf_Sym *ddbsymtab; /* The symbol table we are using */
107 long ddbsymcnt; /* Number of symbols */
108 caddr_t ddbstrtab; /* String table */
109 long ddbstrcnt; /* number of bytes in string table */
111 caddr_t shstrtab; /* Section name string table */
112 long shstrcnt; /* number of bytes in string table */
114 caddr_t ctftab; /* CTF table */
115 long ctfcnt; /* number of bytes in CTF table */
116 caddr_t ctfoff; /* CTF offset table */
117 caddr_t typoff; /* Type offset table */
118 long typlen; /* Number of type entries. */
122 #include <kern/kern_ctf.c>
124 static int link_elf_link_preload(linker_class_t cls,
125 const char *, linker_file_t *);
126 static int link_elf_link_preload_finish(linker_file_t);
127 static int link_elf_load_file(linker_class_t, const char *, linker_file_t *);
128 static int link_elf_lookup_symbol(linker_file_t, const char *,
130 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t,
132 static int link_elf_search_symbol(linker_file_t, caddr_t value,
133 c_linker_sym_t *sym, long *diffp);
135 static void link_elf_unload_file(linker_file_t);
136 static int link_elf_lookup_set(linker_file_t, const char *,
137 void ***, void ***, int *);
138 static int link_elf_each_function_name(linker_file_t,
139 int (*)(const char *, void *), void *);
140 static int link_elf_each_function_nameval(linker_file_t,
141 linker_function_nameval_callback_t,
143 static int link_elf_reloc_local(linker_file_t, bool);
144 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
145 static long link_elf_strtab_get(linker_file_t, caddr_t *);
147 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps,
150 static kobj_method_t link_elf_methods[] = {
151 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
152 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
153 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
154 KOBJMETHOD(linker_unload, link_elf_unload_file),
155 KOBJMETHOD(linker_load_file, link_elf_load_file),
156 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
157 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
158 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
159 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
160 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
161 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
162 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
163 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
167 static struct linker_class link_elf_class = {
168 #if ELF_TARG_CLASS == ELFCLASS32
173 link_elf_methods, sizeof(struct elf_file)
176 static int relocate_file(elf_file_t ef);
177 static void elf_obj_cleanup_globals_cache(elf_file_t);
180 link_elf_error(const char *filename, const char *s)
182 if (filename == NULL)
183 printf("kldload: %s\n", s);
185 printf("kldload: %s: %s\n", filename, s);
189 link_elf_init(void *arg)
192 linker_add_class(&link_elf_class);
195 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
198 link_elf_link_preload(linker_class_t cls, const char *filename,
199 linker_file_t *result)
204 void *modptr, *baseptr, *sizeptr;
209 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
211 /* Look to see if we have the file preloaded */
212 modptr = preload_search_by_name(filename);
216 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
217 baseptr = preload_search_info(modptr, MODINFO_ADDR);
218 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
219 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
221 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
223 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
224 " obj module") != 0 &&
225 strcmp(type, "elf obj module") != 0)) {
228 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
232 lf = linker_make_file(filename, &link_elf_class);
238 ef->address = *(caddr_t *)baseptr;
239 lf->address = *(caddr_t *)baseptr;
240 lf->size = *(size_t *)sizeptr;
242 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
243 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
244 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
245 hdr->e_version != EV_CURRENT ||
246 hdr->e_type != ET_REL ||
247 hdr->e_machine != ELF_TARG_MACH) {
253 /* Scan the section header for information and table sizing. */
256 for (i = 0; i < hdr->e_shnum; i++) {
257 switch (shdr[i].sh_type) {
261 case SHT_X86_64_UNWIND:
263 /* Ignore sections not loaded by the loader. */
264 if (shdr[i].sh_addr == 0)
270 symstrindex = shdr[i].sh_link;
274 * Ignore relocation tables for sections not
275 * loaded by the loader.
277 if (shdr[shdr[i].sh_info].sh_addr == 0)
282 if (shdr[shdr[i].sh_info].sh_addr == 0)
289 shstrindex = hdr->e_shstrndx;
290 if (ef->nprogtab == 0 || symstrindex < 0 ||
291 symstrindex >= hdr->e_shnum ||
292 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
293 shstrindex >= hdr->e_shnum ||
294 shdr[shstrindex].sh_type != SHT_STRTAB) {
295 printf("%s: bad/missing section headers\n", filename);
300 /* Allocate space for tracking the load chunks */
301 if (ef->nprogtab != 0)
302 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
303 M_LINKER, M_WAITOK | M_ZERO);
304 if (ef->nreltab != 0)
305 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
306 M_LINKER, M_WAITOK | M_ZERO);
307 if (ef->nrelatab != 0)
308 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
309 M_LINKER, M_WAITOK | M_ZERO);
310 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
311 (ef->nreltab != 0 && ef->reltab == NULL) ||
312 (ef->nrelatab != 0 && ef->relatab == NULL)) {
317 /* XXX, relocate the sh_addr fields saved by the loader. */
319 for (i = 0; i < hdr->e_shnum; i++) {
320 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
321 off = shdr[i].sh_addr;
323 for (i = 0; i < hdr->e_shnum; i++) {
324 if (shdr[i].sh_addr != 0)
325 shdr[i].sh_addr = shdr[i].sh_addr - off +
326 (Elf_Addr)ef->address;
329 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
330 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
331 ef->ddbstrcnt = shdr[symstrindex].sh_size;
332 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
333 ef->shstrcnt = shdr[shstrindex].sh_size;
334 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
336 /* Now fill out progtab and the relocation tables. */
340 for (i = 0; i < hdr->e_shnum; i++) {
341 switch (shdr[i].sh_type) {
345 case SHT_X86_64_UNWIND:
347 if (shdr[i].sh_addr == 0)
349 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
350 if (shdr[i].sh_type == SHT_PROGBITS)
351 ef->progtab[pb].name = "<<PROGBITS>>";
353 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
354 ef->progtab[pb].name = "<<UNWIND>>";
357 ef->progtab[pb].name = "<<NOBITS>>";
358 ef->progtab[pb].size = shdr[i].sh_size;
359 ef->progtab[pb].sec = i;
360 if (ef->shstrtab && shdr[i].sh_name != 0)
361 ef->progtab[pb].name =
362 ef->shstrtab + shdr[i].sh_name;
363 if (ef->progtab[pb].name != NULL &&
364 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
367 dpcpu = dpcpu_alloc(shdr[i].sh_size);
369 printf("%s: pcpu module space is out "
370 "of space; cannot allocate %#jx "
371 "for %s\n", __func__,
372 (uintmax_t)shdr[i].sh_size,
377 memcpy(dpcpu, ef->progtab[pb].addr,
378 ef->progtab[pb].size);
379 dpcpu_copy(dpcpu, shdr[i].sh_size);
380 ef->progtab[pb].addr = dpcpu;
382 } else if (ef->progtab[pb].name != NULL &&
383 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
386 vnet_data = vnet_data_alloc(shdr[i].sh_size);
387 if (vnet_data == NULL) {
388 printf("%s: vnet module space is out "
389 "of space; cannot allocate %#jx "
390 "for %s\n", __func__,
391 (uintmax_t)shdr[i].sh_size,
396 memcpy(vnet_data, ef->progtab[pb].addr,
397 ef->progtab[pb].size);
398 vnet_data_copy(vnet_data, shdr[i].sh_size);
399 ef->progtab[pb].addr = vnet_data;
401 } else if (ef->progtab[pb].name != NULL &&
402 !strcmp(ef->progtab[pb].name, ".ctors")) {
403 lf->ctors_addr = ef->progtab[pb].addr;
404 lf->ctors_size = shdr[i].sh_size;
407 /* Update all symbol values with the offset. */
408 for (j = 0; j < ef->ddbsymcnt; j++) {
409 es = &ef->ddbsymtab[j];
410 if (es->st_shndx != i)
412 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
417 if (shdr[shdr[i].sh_info].sh_addr == 0)
419 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
420 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
421 ef->reltab[rl].sec = shdr[i].sh_info;
425 if (shdr[shdr[i].sh_info].sh_addr == 0)
427 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
428 ef->relatab[ra].nrela =
429 shdr[i].sh_size / sizeof(Elf_Rela);
430 ef->relatab[ra].sec = shdr[i].sh_info;
435 if (pb != ef->nprogtab) {
436 printf("%s: lost progbits\n", filename);
440 if (rl != ef->nreltab) {
441 printf("%s: lost reltab\n", filename);
445 if (ra != ef->nrelatab) {
446 printf("%s: lost relatab\n", filename);
451 /* Local intra-module relocations */
452 error = link_elf_reloc_local(lf, false);
459 /* preload not done this way */
460 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
465 link_elf_invoke_ctors(caddr_t addr, size_t size)
470 if (addr == NULL || size == 0)
472 cnt = size / sizeof(*ctor);
474 for (i = 0; i < cnt; i++) {
481 link_elf_link_preload_finish(linker_file_t lf)
487 error = relocate_file(ef);
491 /* Notify MD code that a module is being loaded. */
492 error = elf_cpu_load_file(lf);
496 #if defined(__i386__) || defined(__amd64__)
498 error = link_elf_reloc_local(lf, true);
504 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
509 link_elf_load_file(linker_class_t cls, const char *filename,
510 linker_file_t *result)
513 struct thread *td = curthread; /* XXX */
537 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
539 error = vn_open(&nd, &flags, 0, NULL);
542 NDFREE(&nd, NDF_ONLY_PNBUF);
543 if (nd.ni_vp->v_type != VREG) {
548 error = mac_kld_check_load(td->td_ucred, nd.ni_vp);
554 /* Read the elf header from the file. */
555 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
556 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)hdr, sizeof(*hdr), 0,
557 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
571 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
572 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
573 link_elf_error(filename, "Unsupported file layout");
577 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
578 || hdr->e_version != EV_CURRENT) {
579 link_elf_error(filename, "Unsupported file version");
583 if (hdr->e_type != ET_REL) {
587 if (hdr->e_machine != ELF_TARG_MACH) {
588 link_elf_error(filename, "Unsupported machine");
593 lf = linker_make_file(filename, &link_elf_class);
598 ef = (elf_file_t) lf;
604 /* Allocate and read in the section header */
605 nbytes = hdr->e_shnum * hdr->e_shentsize;
606 if (nbytes == 0 || hdr->e_shoff == 0 ||
607 hdr->e_shentsize != sizeof(Elf_Shdr)) {
611 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
613 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes, hdr->e_shoff,
614 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
622 /* Scan the section header for information and table sizing. */
626 for (i = 0; i < hdr->e_shnum; i++) {
627 if (shdr[i].sh_size == 0)
629 switch (shdr[i].sh_type) {
633 case SHT_X86_64_UNWIND:
635 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
642 symstrindex = shdr[i].sh_link;
646 * Ignore relocation tables for unallocated
649 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
654 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
662 if (ef->nprogtab == 0) {
663 link_elf_error(filename, "file has no contents");
668 /* Only allow one symbol table for now */
669 link_elf_error(filename, "file has no valid symbol table");
673 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
674 shdr[symstrindex].sh_type != SHT_STRTAB) {
675 link_elf_error(filename, "file has invalid symbol strings");
680 /* Allocate space for tracking the load chunks */
681 if (ef->nprogtab != 0)
682 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
683 M_LINKER, M_WAITOK | M_ZERO);
684 if (ef->nreltab != 0)
685 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
686 M_LINKER, M_WAITOK | M_ZERO);
687 if (ef->nrelatab != 0)
688 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
689 M_LINKER, M_WAITOK | M_ZERO);
691 if (symtabindex == -1) {
692 link_elf_error(filename, "lost symbol table index");
696 /* Allocate space for and load the symbol table */
697 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
698 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
699 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab,
700 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
701 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
710 if (symstrindex == -1) {
711 link_elf_error(filename, "lost symbol string index");
715 /* Allocate space for and load the symbol strings */
716 ef->ddbstrcnt = shdr[symstrindex].sh_size;
717 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
718 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab,
719 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
720 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
729 /* Do we have a string table for the section names? */
731 if (hdr->e_shstrndx != 0 &&
732 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
733 shstrindex = hdr->e_shstrndx;
734 ef->shstrcnt = shdr[shstrindex].sh_size;
735 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
737 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab,
738 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
739 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
749 /* Size up code/data(progbits) and bss(nobits). */
751 for (i = 0; i < hdr->e_shnum; i++) {
752 if (shdr[i].sh_size == 0)
754 switch (shdr[i].sh_type) {
758 case SHT_X86_64_UNWIND:
760 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
762 alignmask = shdr[i].sh_addralign - 1;
763 mapsize += alignmask;
764 mapsize &= ~alignmask;
765 mapsize += shdr[i].sh_size;
771 * We know how much space we need for the text/data/bss/etc.
772 * This stuff needs to be in a single chunk so that profiling etc
773 * can get the bounds and gdb can associate offsets with modules
775 ef->object = vm_object_allocate(OBJT_DEFAULT,
776 round_page(mapsize) >> PAGE_SHIFT);
777 if (ef->object == NULL) {
781 ef->address = (caddr_t) vm_map_min(kernel_map);
784 * In order to satisfy amd64's architectural requirements on the
785 * location of code and data in the kernel's address space, request a
786 * mapping that is above the kernel.
791 mapbase = VM_MIN_KERNEL_ADDRESS;
793 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
794 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
797 vm_object_deallocate(ef->object);
803 error = vm_map_wire(kernel_map, mapbase,
804 mapbase + round_page(mapsize),
805 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
806 if (error != KERN_SUCCESS) {
811 /* Inform the kld system about the situation */
812 lf->address = ef->address = (caddr_t)mapbase;
816 * Now load code/data(progbits), zero bss(nobits), allocate space for
823 for (i = 0; i < hdr->e_shnum; i++) {
824 if (shdr[i].sh_size == 0)
826 switch (shdr[i].sh_type) {
830 case SHT_X86_64_UNWIND:
832 if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
834 alignmask = shdr[i].sh_addralign - 1;
835 mapbase += alignmask;
836 mapbase &= ~alignmask;
837 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
838 ef->progtab[pb].name =
839 ef->shstrtab + shdr[i].sh_name;
840 if (!strcmp(ef->progtab[pb].name, ".ctors")) {
841 lf->ctors_addr = (caddr_t)mapbase;
842 lf->ctors_size = shdr[i].sh_size;
844 } else if (shdr[i].sh_type == SHT_PROGBITS)
845 ef->progtab[pb].name = "<<PROGBITS>>";
847 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
848 ef->progtab[pb].name = "<<UNWIND>>";
851 ef->progtab[pb].name = "<<NOBITS>>";
852 if (ef->progtab[pb].name != NULL &&
853 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
854 ef->progtab[pb].addr =
855 dpcpu_alloc(shdr[i].sh_size);
856 if (ef->progtab[pb].addr == NULL) {
857 printf("%s: pcpu module space is out "
858 "of space; cannot allocate %#jx "
859 "for %s\n", __func__,
860 (uintmax_t)shdr[i].sh_size,
865 else if (ef->progtab[pb].name != NULL &&
866 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
867 ef->progtab[pb].addr =
868 vnet_data_alloc(shdr[i].sh_size);
869 if (ef->progtab[pb].addr == NULL) {
870 printf("%s: vnet module space is out "
871 "of space; cannot allocate %#jx "
872 "for %s\n", __func__,
873 (uintmax_t)shdr[i].sh_size,
879 ef->progtab[pb].addr =
880 (void *)(uintptr_t)mapbase;
881 if (ef->progtab[pb].addr == NULL) {
885 ef->progtab[pb].size = shdr[i].sh_size;
886 ef->progtab[pb].sec = i;
887 if (shdr[i].sh_type == SHT_PROGBITS
889 || shdr[i].sh_type == SHT_X86_64_UNWIND
892 error = vn_rdwr(UIO_READ, nd.ni_vp,
893 ef->progtab[pb].addr,
894 shdr[i].sh_size, shdr[i].sh_offset,
895 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
903 /* Initialize the per-cpu or vnet area. */
904 if (ef->progtab[pb].addr != (void *)mapbase &&
905 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
906 dpcpu_copy(ef->progtab[pb].addr,
909 else if (ef->progtab[pb].addr !=
911 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
912 vnet_data_copy(ef->progtab[pb].addr,
916 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
918 /* Update all symbol values with the offset. */
919 for (j = 0; j < ef->ddbsymcnt; j++) {
920 es = &ef->ddbsymtab[j];
921 if (es->st_shndx != i)
923 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
925 mapbase += shdr[i].sh_size;
929 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
931 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
933 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
934 ef->reltab[rl].sec = shdr[i].sh_info;
935 error = vn_rdwr(UIO_READ, nd.ni_vp,
936 (void *)ef->reltab[rl].rel,
937 shdr[i].sh_size, shdr[i].sh_offset,
938 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
949 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
951 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
953 ef->relatab[ra].nrela =
954 shdr[i].sh_size / sizeof(Elf_Rela);
955 ef->relatab[ra].sec = shdr[i].sh_info;
956 error = vn_rdwr(UIO_READ, nd.ni_vp,
957 (void *)ef->relatab[ra].rela,
958 shdr[i].sh_size, shdr[i].sh_offset,
959 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
971 if (pb != ef->nprogtab) {
972 link_elf_error(filename, "lost progbits");
976 if (rl != ef->nreltab) {
977 link_elf_error(filename, "lost reltab");
981 if (ra != ef->nrelatab) {
982 link_elf_error(filename, "lost relatab");
986 if (mapbase != (vm_offset_t)ef->address + mapsize) {
988 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
989 filename != NULL ? filename : "<none>",
990 (u_long)mapbase, ef->address, (u_long)mapsize,
991 (u_long)(vm_offset_t)ef->address + mapsize);
996 /* Local intra-module relocations */
997 error = link_elf_reloc_local(lf, false);
1001 /* Pull in dependencies */
1002 VOP_UNLOCK(nd.ni_vp, 0);
1003 error = linker_load_dependencies(lf);
1004 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1008 /* External relocations */
1009 error = relocate_file(ef);
1013 /* Notify MD code that a module is being loaded. */
1014 error = elf_cpu_load_file(lf);
1018 #if defined(__i386__) || defined(__amd64__)
1020 error = link_elf_reloc_local(lf, true);
1026 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
1031 VOP_UNLOCK(nd.ni_vp, 0);
1032 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1034 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1035 free(hdr, M_LINKER);
1041 link_elf_unload_file(linker_file_t file)
1043 elf_file_t ef = (elf_file_t) file;
1046 /* Notify MD code that a module is being unloaded. */
1047 elf_cpu_unload_file(file);
1050 for (i = 0; i < ef->nprogtab; i++) {
1051 if (ef->progtab[i].size == 0)
1053 if (ef->progtab[i].name == NULL)
1055 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
1056 dpcpu_free(ef->progtab[i].addr,
1057 ef->progtab[i].size);
1059 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
1060 vnet_data_free(ef->progtab[i].addr,
1061 ef->progtab[i].size);
1065 if (ef->preloaded) {
1066 free(ef->reltab, M_LINKER);
1067 free(ef->relatab, M_LINKER);
1068 free(ef->progtab, M_LINKER);
1069 free(ef->ctftab, M_LINKER);
1070 free(ef->ctfoff, M_LINKER);
1071 free(ef->typoff, M_LINKER);
1072 if (file->pathname != NULL)
1073 preload_delete_name(file->pathname);
1077 for (i = 0; i < ef->nreltab; i++)
1078 free(ef->reltab[i].rel, M_LINKER);
1079 for (i = 0; i < ef->nrelatab; i++)
1080 free(ef->relatab[i].rela, M_LINKER);
1081 free(ef->reltab, M_LINKER);
1082 free(ef->relatab, M_LINKER);
1083 free(ef->progtab, M_LINKER);
1086 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
1087 (vm_offset_t) ef->address +
1088 (ef->object->size << PAGE_SHIFT));
1090 free(ef->e_shdr, M_LINKER);
1091 free(ef->ddbsymtab, M_LINKER);
1092 free(ef->ddbstrtab, M_LINKER);
1093 free(ef->shstrtab, M_LINKER);
1094 free(ef->ctftab, M_LINKER);
1095 free(ef->ctfoff, M_LINKER);
1096 free(ef->typoff, M_LINKER);
1100 symbol_name(elf_file_t ef, Elf_Size r_info)
1104 if (ELF_R_SYM(r_info)) {
1105 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1106 return ef->ddbstrtab + ref->st_name;
1112 findbase(elf_file_t ef, int sec)
1117 for (i = 0; i < ef->nprogtab; i++) {
1118 if (sec == ef->progtab[i].sec) {
1119 base = (Elf_Addr)ef->progtab[i].addr;
1127 relocate_file(elf_file_t ef)
1129 const Elf_Rel *rellim;
1131 const Elf_Rela *relalim;
1132 const Elf_Rela *rela;
1133 const char *symname;
1140 /* Perform relocations without addend if there are any: */
1141 for (i = 0; i < ef->nreltab; i++) {
1142 rel = ef->reltab[i].rel;
1144 link_elf_error(ef->lf.filename, "lost a reltab!");
1147 rellim = rel + ef->reltab[i].nrel;
1148 base = findbase(ef, ef->reltab[i].sec);
1150 link_elf_error(ef->lf.filename, "lost base for reltab");
1153 for ( ; rel < rellim; rel++) {
1154 symidx = ELF_R_SYM(rel->r_info);
1155 if (symidx >= ef->ddbsymcnt)
1157 sym = ef->ddbsymtab + symidx;
1158 /* Local relocs are already done */
1159 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1161 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1163 symname = symbol_name(ef, rel->r_info);
1164 printf("link_elf_obj: symbol %s undefined\n",
1171 /* Perform relocations with addend if there are any: */
1172 for (i = 0; i < ef->nrelatab; i++) {
1173 rela = ef->relatab[i].rela;
1175 link_elf_error(ef->lf.filename, "lost a relatab!");
1178 relalim = rela + ef->relatab[i].nrela;
1179 base = findbase(ef, ef->relatab[i].sec);
1181 link_elf_error(ef->lf.filename,
1182 "lost base for relatab");
1185 for ( ; rela < relalim; rela++) {
1186 symidx = ELF_R_SYM(rela->r_info);
1187 if (symidx >= ef->ddbsymcnt)
1189 sym = ef->ddbsymtab + symidx;
1190 /* Local relocs are already done */
1191 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1193 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1195 symname = symbol_name(ef, rela->r_info);
1196 printf("link_elf_obj: symbol %s undefined\n",
1204 * Only clean SHN_FBSD_CACHED for successful return. If we
1205 * modified symbol table for the object but found an
1206 * unresolved symbol, there is no reason to roll back.
1208 elf_obj_cleanup_globals_cache(ef);
1214 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1216 elf_file_t ef = (elf_file_t) lf;
1217 const Elf_Sym *symp;
1221 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1222 strp = ef->ddbstrtab + symp->st_name;
1223 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1224 *sym = (c_linker_sym_t) symp;
1232 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1233 linker_symval_t *symval)
1239 ef = (elf_file_t) lf;
1240 es = (const Elf_Sym*) sym;
1241 val = (caddr_t)es->st_value;
1242 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1243 symval->name = ef->ddbstrtab + es->st_name;
1244 val = (caddr_t)es->st_value;
1245 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
1246 val = ((caddr_t (*)(void))val)();
1247 symval->value = val;
1248 symval->size = es->st_size;
1255 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1256 c_linker_sym_t *sym, long *diffp)
1258 elf_file_t ef = (elf_file_t) lf;
1259 u_long off = (uintptr_t) (void *) value;
1263 const Elf_Sym *best = NULL;
1266 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1267 if (es->st_name == 0)
1269 st_value = es->st_value;
1270 if (off >= st_value) {
1271 if (off - st_value < diff) {
1272 diff = off - st_value;
1276 } else if (off - st_value == diff) {
1285 *sym = (c_linker_sym_t) best;
1291 * Look up a linker set on an ELF system.
1294 link_elf_lookup_set(linker_file_t lf, const char *name,
1295 void ***startp, void ***stopp, int *countp)
1297 elf_file_t ef = (elf_file_t)lf;
1298 void **start, **stop;
1301 /* Relative to section number */
1302 for (i = 0; i < ef->nprogtab; i++) {
1303 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1304 strcmp(ef->progtab[i].name + 4, name) == 0) {
1305 start = (void **)ef->progtab[i].addr;
1306 stop = (void **)((char *)ef->progtab[i].addr +
1307 ef->progtab[i].size);
1308 count = stop - start;
1322 link_elf_each_function_name(linker_file_t file,
1323 int (*callback)(const char *, void *), void *opaque)
1325 elf_file_t ef = (elf_file_t)file;
1326 const Elf_Sym *symp;
1329 /* Exhaustive search */
1330 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1331 if (symp->st_value != 0 &&
1332 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1333 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1334 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1343 link_elf_each_function_nameval(linker_file_t file,
1344 linker_function_nameval_callback_t callback, void *opaque)
1346 linker_symval_t symval;
1347 elf_file_t ef = (elf_file_t)file;
1348 const Elf_Sym* symp;
1351 /* Exhaustive search */
1352 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1353 if (symp->st_value != 0 &&
1354 (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1355 ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1356 error = link_elf_symbol_values(file,
1357 (c_linker_sym_t)symp, &symval);
1360 error = callback(file, i, &symval, opaque);
1369 elf_obj_cleanup_globals_cache(elf_file_t ef)
1374 for (i = 0; i < ef->ddbsymcnt; i++) {
1375 sym = ef->ddbsymtab + i;
1376 if (sym->st_shndx == SHN_FBSD_CACHED) {
1377 sym->st_shndx = SHN_UNDEF;
1384 * Symbol lookup function that can be used when the symbol index is known (ie
1385 * in relocations). It uses the symbol index instead of doing a fully fledged
1386 * hash table based lookup when such is valid. For example for local symbols.
1387 * This is not only more efficient, it's also more correct. It's not always
1388 * the case that the symbol can be found through the hash table.
1391 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1393 elf_file_t ef = (elf_file_t)lf;
1398 /* Don't even try to lookup the symbol if the index is bogus. */
1399 if (symidx >= ef->ddbsymcnt) {
1404 sym = ef->ddbsymtab + symidx;
1406 /* Quick answer if there is a definition included. */
1407 if (sym->st_shndx != SHN_UNDEF) {
1408 res1 = (Elf_Addr)sym->st_value;
1409 if (ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC)
1410 res1 = ((Elf_Addr (*)(void))res1)();
1415 /* If we get here, then it is undefined and needs a lookup. */
1416 switch (ELF_ST_BIND(sym->st_info)) {
1418 /* Local, but undefined? huh? */
1424 /* Relative to Data or Function name */
1425 symbol = ef->ddbstrtab + sym->st_name;
1427 /* Force a lookup failure if the symbol name is bogus. */
1432 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1435 * Cache global lookups during module relocation. The failure
1436 * case is particularly expensive for callers, who must scan
1437 * through the entire globals table doing strcmp(). Cache to
1438 * avoid doing such work repeatedly.
1440 * After relocation is complete, undefined globals will be
1441 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1445 sym->st_shndx = SHN_FBSD_CACHED;
1446 sym->st_value = res1;
1449 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1462 link_elf_fix_link_set(elf_file_t ef)
1464 static const char startn[] = "__start_";
1465 static const char stopn[] = "__stop_";
1467 const char *sym_name, *linkset_name;
1468 Elf_Addr startp, stopp;
1473 for (symidx = 1 /* zero entry is special */;
1474 symidx < ef->ddbsymcnt; symidx++) {
1475 sym = ef->ddbsymtab + symidx;
1476 if (sym->st_shndx != SHN_UNDEF)
1479 sym_name = ef->ddbstrtab + sym->st_name;
1480 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1482 linkset_name = sym_name + sizeof(startn) - 1;
1484 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1486 linkset_name = sym_name + sizeof(stopn) - 1;
1491 for (i = 0; i < ef->nprogtab; i++) {
1492 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1493 startp = (Elf_Addr)ef->progtab[i].addr;
1494 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1498 if (i == ef->nprogtab)
1501 sym->st_value = start ? startp : stopp;
1507 link_elf_reloc_local(linker_file_t lf, bool ifuncs)
1509 elf_file_t ef = (elf_file_t)lf;
1510 const Elf_Rel *rellim;
1512 const Elf_Rela *relalim;
1513 const Elf_Rela *rela;
1519 link_elf_fix_link_set(ef);
1521 /* Perform relocations without addend if there are any: */
1522 for (i = 0; i < ef->nreltab; i++) {
1523 rel = ef->reltab[i].rel;
1525 link_elf_error(ef->lf.filename, "lost a reltab");
1528 rellim = rel + ef->reltab[i].nrel;
1529 base = findbase(ef, ef->reltab[i].sec);
1531 link_elf_error(ef->lf.filename, "lost base for reltab");
1534 for ( ; rel < rellim; rel++) {
1535 symidx = ELF_R_SYM(rel->r_info);
1536 if (symidx >= ef->ddbsymcnt)
1538 sym = ef->ddbsymtab + symidx;
1539 /* Only do local relocs */
1540 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1542 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1543 elf_is_ifunc_reloc(rel->r_info)) == ifuncs)
1544 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1549 /* Perform relocations with addend if there are any: */
1550 for (i = 0; i < ef->nrelatab; i++) {
1551 rela = ef->relatab[i].rela;
1553 link_elf_error(ef->lf.filename, "lost a relatab!");
1556 relalim = rela + ef->relatab[i].nrela;
1557 base = findbase(ef, ef->relatab[i].sec);
1559 link_elf_error(ef->lf.filename, "lost base for reltab");
1562 for ( ; rela < relalim; rela++) {
1563 symidx = ELF_R_SYM(rela->r_info);
1564 if (symidx >= ef->ddbsymcnt)
1566 sym = ef->ddbsymtab + symidx;
1567 /* Only do local relocs */
1568 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1570 if ((ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC ||
1571 elf_is_ifunc_reloc(rela->r_info)) == ifuncs)
1572 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1580 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1582 elf_file_t ef = (elf_file_t)lf;
1584 *symtab = ef->ddbsymtab;
1586 if (*symtab == NULL)
1589 return (ef->ddbsymcnt);
1593 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1595 elf_file_t ef = (elf_file_t)lf;
1597 *strtab = ef->ddbstrtab;
1599 if (*strtab == NULL)
1602 return (ef->ddbstrcnt);