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);
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:
267 symstrindex = shdr[i].sh_link;
278 shstrindex = hdr->e_shstrndx;
279 if (ef->nprogtab == 0 || symstrindex < 0 ||
280 symstrindex >= hdr->e_shnum ||
281 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
282 shstrindex >= hdr->e_shnum ||
283 shdr[shstrindex].sh_type != SHT_STRTAB) {
284 printf("%s: bad/missing section headers\n", filename);
289 /* Allocate space for tracking the load chunks */
290 if (ef->nprogtab != 0)
291 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
292 M_LINKER, M_WAITOK | M_ZERO);
293 if (ef->nreltab != 0)
294 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
295 M_LINKER, M_WAITOK | M_ZERO);
296 if (ef->nrelatab != 0)
297 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
298 M_LINKER, M_WAITOK | M_ZERO);
299 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
300 (ef->nreltab != 0 && ef->reltab == NULL) ||
301 (ef->nrelatab != 0 && ef->relatab == NULL)) {
306 /* XXX, relocate the sh_addr fields saved by the loader. */
308 for (i = 0; i < hdr->e_shnum; i++) {
309 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
310 off = shdr[i].sh_addr;
312 for (i = 0; i < hdr->e_shnum; i++) {
313 if (shdr[i].sh_addr != 0)
314 shdr[i].sh_addr = shdr[i].sh_addr - off +
315 (Elf_Addr)ef->address;
318 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
319 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
320 ef->ddbstrcnt = shdr[symstrindex].sh_size;
321 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
322 ef->shstrcnt = shdr[shstrindex].sh_size;
323 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
325 /* Now fill out progtab and the relocation tables. */
329 for (i = 0; i < hdr->e_shnum; i++) {
330 switch (shdr[i].sh_type) {
334 case SHT_X86_64_UNWIND:
336 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
337 if (shdr[i].sh_type == SHT_PROGBITS)
338 ef->progtab[pb].name = "<<PROGBITS>>";
340 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
341 ef->progtab[pb].name = "<<UNWIND>>";
344 ef->progtab[pb].name = "<<NOBITS>>";
345 ef->progtab[pb].size = shdr[i].sh_size;
346 ef->progtab[pb].sec = i;
347 if (ef->shstrtab && shdr[i].sh_name != 0)
348 ef->progtab[pb].name =
349 ef->shstrtab + shdr[i].sh_name;
350 if (ef->progtab[pb].name != NULL &&
351 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
354 dpcpu = dpcpu_alloc(shdr[i].sh_size);
359 memcpy(dpcpu, ef->progtab[pb].addr,
360 ef->progtab[pb].size);
361 dpcpu_copy(dpcpu, shdr[i].sh_size);
362 ef->progtab[pb].addr = dpcpu;
364 } else if (ef->progtab[pb].name != NULL &&
365 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
368 vnet_data = vnet_data_alloc(shdr[i].sh_size);
369 if (vnet_data == NULL) {
373 memcpy(vnet_data, ef->progtab[pb].addr,
374 ef->progtab[pb].size);
375 vnet_data_copy(vnet_data, shdr[i].sh_size);
376 ef->progtab[pb].addr = vnet_data;
378 } else if (ef->progtab[pb].name != NULL &&
379 !strcmp(ef->progtab[pb].name, ".ctors")) {
380 lf->ctors_addr = ef->progtab[pb].addr;
381 lf->ctors_size = shdr[i].sh_size;
384 /* Update all symbol values with the offset. */
385 for (j = 0; j < ef->ddbsymcnt; j++) {
386 es = &ef->ddbsymtab[j];
387 if (es->st_shndx != i)
389 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
394 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
395 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
396 ef->reltab[rl].sec = shdr[i].sh_info;
400 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
401 ef->relatab[ra].nrela =
402 shdr[i].sh_size / sizeof(Elf_Rela);
403 ef->relatab[ra].sec = shdr[i].sh_info;
408 if (pb != ef->nprogtab) {
409 printf("%s: lost progbits\n", filename);
413 if (rl != ef->nreltab) {
414 printf("%s: lost reltab\n", filename);
418 if (ra != ef->nrelatab) {
419 printf("%s: lost relatab\n", filename);
424 /* Local intra-module relocations */
425 error = link_elf_reloc_local(lf);
433 /* preload not done this way */
434 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
439 link_elf_invoke_ctors(caddr_t addr, size_t size)
444 if (addr == NULL || size == 0)
446 cnt = size / sizeof(*ctor);
448 for (i = 0; i < cnt; i++) {
455 link_elf_link_preload_finish(linker_file_t lf)
461 error = relocate_file(ef);
465 /* Notify MD code that a module is being loaded. */
466 error = elf_cpu_load_file(lf);
471 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
476 link_elf_load_file(linker_class_t cls, const char *filename,
477 linker_file_t *result)
479 struct nameidata *nd;
480 struct thread *td = curthread; /* XXX */
504 nd = malloc(sizeof(struct nameidata), M_TEMP, M_WAITOK);
505 NDINIT(nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
507 error = vn_open(nd, &flags, 0, NULL);
512 NDFREE(nd, NDF_ONLY_PNBUF);
513 if (nd->ni_vp->v_type != VREG) {
518 error = mac_kld_check_load(td->td_ucred, nd->ni_vp);
524 /* Read the elf header from the file. */
525 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
526 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)hdr, sizeof(*hdr), 0,
527 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
541 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
542 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
543 link_elf_error(filename, "Unsupported file layout");
547 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
548 || hdr->e_version != EV_CURRENT) {
549 link_elf_error(filename, "Unsupported file version");
553 if (hdr->e_type != ET_REL) {
557 if (hdr->e_machine != ELF_TARG_MACH) {
558 link_elf_error(filename, "Unsupported machine");
563 lf = linker_make_file(filename, &link_elf_class);
568 ef = (elf_file_t) lf;
574 /* Allocate and read in the section header */
575 nbytes = hdr->e_shnum * hdr->e_shentsize;
576 if (nbytes == 0 || hdr->e_shoff == 0 ||
577 hdr->e_shentsize != sizeof(Elf_Shdr)) {
581 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
583 error = vn_rdwr(UIO_READ, nd->ni_vp, (caddr_t)shdr, nbytes,
584 hdr->e_shoff, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
593 /* Scan the section header for information and table sizing. */
597 for (i = 0; i < hdr->e_shnum; i++) {
598 if (shdr[i].sh_size == 0)
600 switch (shdr[i].sh_type) {
604 case SHT_X86_64_UNWIND:
611 symstrindex = shdr[i].sh_link;
623 if (ef->nprogtab == 0) {
624 link_elf_error(filename, "file has no contents");
629 /* Only allow one symbol table for now */
630 link_elf_error(filename, "file has no valid symbol table");
634 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
635 shdr[symstrindex].sh_type != SHT_STRTAB) {
636 link_elf_error(filename, "file has invalid symbol strings");
641 /* Allocate space for tracking the load chunks */
642 if (ef->nprogtab != 0)
643 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
644 M_LINKER, M_WAITOK | M_ZERO);
645 if (ef->nreltab != 0)
646 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
647 M_LINKER, M_WAITOK | M_ZERO);
648 if (ef->nrelatab != 0)
649 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
650 M_LINKER, M_WAITOK | M_ZERO);
652 if (symtabindex == -1) {
653 link_elf_error(filename, "lost symbol table index");
657 /* Allocate space for and load the symbol table */
658 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
659 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
660 error = vn_rdwr(UIO_READ, nd->ni_vp, (void *)ef->ddbsymtab,
661 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
662 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
671 if (symstrindex == -1) {
672 link_elf_error(filename, "lost symbol string index");
676 /* Allocate space for and load the symbol strings */
677 ef->ddbstrcnt = shdr[symstrindex].sh_size;
678 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
679 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->ddbstrtab,
680 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
681 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
690 /* Do we have a string table for the section names? */
692 if (hdr->e_shstrndx != 0 &&
693 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
694 shstrindex = hdr->e_shstrndx;
695 ef->shstrcnt = shdr[shstrindex].sh_size;
696 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
698 error = vn_rdwr(UIO_READ, nd->ni_vp, ef->shstrtab,
699 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
700 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
710 /* Size up code/data(progbits) and bss(nobits). */
712 for (i = 0; i < hdr->e_shnum; i++) {
713 if (shdr[i].sh_size == 0)
715 switch (shdr[i].sh_type) {
719 case SHT_X86_64_UNWIND:
721 alignmask = shdr[i].sh_addralign - 1;
722 mapsize += alignmask;
723 mapsize &= ~alignmask;
724 mapsize += shdr[i].sh_size;
730 * We know how much space we need for the text/data/bss/etc.
731 * This stuff needs to be in a single chunk so that profiling etc
732 * can get the bounds and gdb can associate offsets with modules
734 ef->object = vm_object_allocate(OBJT_DEFAULT,
735 round_page(mapsize) >> PAGE_SHIFT);
736 if (ef->object == NULL) {
740 ef->address = (caddr_t) vm_map_min(kernel_map);
743 * In order to satisfy amd64's architectural requirements on the
744 * location of code and data in the kernel's address space, request a
745 * mapping that is above the kernel.
750 mapbase = VM_MIN_KERNEL_ADDRESS;
752 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
753 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
756 vm_object_deallocate(ef->object);
762 error = vm_map_wire(kernel_map, mapbase,
763 mapbase + round_page(mapsize),
764 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
765 if (error != KERN_SUCCESS) {
770 /* Inform the kld system about the situation */
771 lf->address = ef->address = (caddr_t)mapbase;
775 * Now load code/data(progbits), zero bss(nobits), allocate space for
782 for (i = 0; i < hdr->e_shnum; i++) {
783 if (shdr[i].sh_size == 0)
785 switch (shdr[i].sh_type) {
789 case SHT_X86_64_UNWIND:
791 alignmask = shdr[i].sh_addralign - 1;
792 mapbase += alignmask;
793 mapbase &= ~alignmask;
794 if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
795 ef->progtab[pb].name =
796 ef->shstrtab + shdr[i].sh_name;
797 if (!strcmp(ef->progtab[pb].name, ".ctors")) {
798 lf->ctors_addr = (caddr_t)mapbase;
799 lf->ctors_size = shdr[i].sh_size;
801 } else if (shdr[i].sh_type == SHT_PROGBITS)
802 ef->progtab[pb].name = "<<PROGBITS>>";
804 else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
805 ef->progtab[pb].name = "<<UNWIND>>";
808 ef->progtab[pb].name = "<<NOBITS>>";
809 if (ef->progtab[pb].name != NULL &&
810 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
811 ef->progtab[pb].addr =
812 dpcpu_alloc(shdr[i].sh_size);
814 else if (ef->progtab[pb].name != NULL &&
815 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
816 ef->progtab[pb].addr =
817 vnet_data_alloc(shdr[i].sh_size);
820 ef->progtab[pb].addr =
821 (void *)(uintptr_t)mapbase;
822 if (ef->progtab[pb].addr == NULL) {
826 ef->progtab[pb].size = shdr[i].sh_size;
827 ef->progtab[pb].sec = i;
828 if (shdr[i].sh_type == SHT_PROGBITS
830 || shdr[i].sh_type == SHT_X86_64_UNWIND
833 error = vn_rdwr(UIO_READ, nd->ni_vp,
834 ef->progtab[pb].addr,
835 shdr[i].sh_size, shdr[i].sh_offset,
836 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
844 /* Initialize the per-cpu or vnet area. */
845 if (ef->progtab[pb].addr != (void *)mapbase &&
846 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
847 dpcpu_copy(ef->progtab[pb].addr,
850 else if (ef->progtab[pb].addr !=
852 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
853 vnet_data_copy(ef->progtab[pb].addr,
857 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
859 /* Update all symbol values with the offset. */
860 for (j = 0; j < ef->ddbsymcnt; j++) {
861 es = &ef->ddbsymtab[j];
862 if (es->st_shndx != i)
864 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
866 mapbase += shdr[i].sh_size;
870 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
872 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
873 ef->reltab[rl].sec = shdr[i].sh_info;
874 error = vn_rdwr(UIO_READ, nd->ni_vp,
875 (void *)ef->reltab[rl].rel,
876 shdr[i].sh_size, shdr[i].sh_offset,
877 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
888 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
890 ef->relatab[ra].nrela =
891 shdr[i].sh_size / sizeof(Elf_Rela);
892 ef->relatab[ra].sec = shdr[i].sh_info;
893 error = vn_rdwr(UIO_READ, nd->ni_vp,
894 (void *)ef->relatab[ra].rela,
895 shdr[i].sh_size, shdr[i].sh_offset,
896 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
908 if (pb != ef->nprogtab) {
909 link_elf_error(filename, "lost progbits");
913 if (rl != ef->nreltab) {
914 link_elf_error(filename, "lost reltab");
918 if (ra != ef->nrelatab) {
919 link_elf_error(filename, "lost relatab");
923 if (mapbase != (vm_offset_t)ef->address + mapsize) {
925 "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
926 filename != NULL ? filename : "<none>",
927 (u_long)mapbase, ef->address, (u_long)mapsize,
928 (u_long)(vm_offset_t)ef->address + mapsize);
933 /* Local intra-module relocations */
934 error = link_elf_reloc_local(lf);
938 /* Pull in dependencies */
939 VOP_UNLOCK(nd->ni_vp, 0);
940 error = linker_load_dependencies(lf);
941 vn_lock(nd->ni_vp, LK_EXCLUSIVE | LK_RETRY);
945 /* External relocations */
946 error = relocate_file(ef);
950 /* Notify MD code that a module is being loaded. */
951 error = elf_cpu_load_file(lf);
956 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
961 VOP_UNLOCK(nd->ni_vp, 0);
962 vn_close(nd->ni_vp, FREAD, td->td_ucred, td);
965 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
972 link_elf_unload_file(linker_file_t file)
974 elf_file_t ef = (elf_file_t) file;
977 /* Notify MD code that a module is being unloaded. */
978 elf_cpu_unload_file(file);
981 for (i = 0; i < ef->nprogtab; i++) {
982 if (ef->progtab[i].size == 0)
984 if (ef->progtab[i].name == NULL)
986 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
987 dpcpu_free(ef->progtab[i].addr,
988 ef->progtab[i].size);
990 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
991 vnet_data_free(ef->progtab[i].addr,
992 ef->progtab[i].size);
997 free(ef->reltab, M_LINKER);
998 free(ef->relatab, M_LINKER);
999 free(ef->progtab, M_LINKER);
1000 free(ef->ctftab, M_LINKER);
1001 free(ef->ctfoff, M_LINKER);
1002 free(ef->typoff, M_LINKER);
1003 if (file->filename != NULL)
1004 preload_delete_name(file->filename);
1005 /* XXX reclaim module memory? */
1009 for (i = 0; i < ef->nreltab; i++)
1010 free(ef->reltab[i].rel, M_LINKER);
1011 for (i = 0; i < ef->nrelatab; i++)
1012 free(ef->relatab[i].rela, M_LINKER);
1013 free(ef->reltab, M_LINKER);
1014 free(ef->relatab, M_LINKER);
1015 free(ef->progtab, M_LINKER);
1018 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
1019 (vm_offset_t) ef->address +
1020 (ef->object->size << PAGE_SHIFT));
1022 free(ef->e_shdr, M_LINKER);
1023 free(ef->ddbsymtab, M_LINKER);
1024 free(ef->ddbstrtab, M_LINKER);
1025 free(ef->shstrtab, M_LINKER);
1026 free(ef->ctftab, M_LINKER);
1027 free(ef->ctfoff, M_LINKER);
1028 free(ef->typoff, M_LINKER);
1032 symbol_name(elf_file_t ef, Elf_Size r_info)
1036 if (ELF_R_SYM(r_info)) {
1037 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
1038 return ef->ddbstrtab + ref->st_name;
1044 findbase(elf_file_t ef, int sec)
1049 for (i = 0; i < ef->nprogtab; i++) {
1050 if (sec == ef->progtab[i].sec) {
1051 base = (Elf_Addr)ef->progtab[i].addr;
1059 relocate_file(elf_file_t ef)
1061 const Elf_Rel *rellim;
1063 const Elf_Rela *relalim;
1064 const Elf_Rela *rela;
1065 const char *symname;
1072 /* Perform relocations without addend if there are any: */
1073 for (i = 0; i < ef->nreltab; i++) {
1074 rel = ef->reltab[i].rel;
1076 link_elf_error(ef->lf.filename, "lost a reltab!");
1079 rellim = rel + ef->reltab[i].nrel;
1080 base = findbase(ef, ef->reltab[i].sec);
1082 link_elf_error(ef->lf.filename, "lost base for reltab");
1085 for ( ; rel < rellim; rel++) {
1086 symidx = ELF_R_SYM(rel->r_info);
1087 if (symidx >= ef->ddbsymcnt)
1089 sym = ef->ddbsymtab + symidx;
1090 /* Local relocs are already done */
1091 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1093 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1095 symname = symbol_name(ef, rel->r_info);
1096 printf("link_elf_obj: symbol %s undefined\n",
1103 /* Perform relocations with addend if there are any: */
1104 for (i = 0; i < ef->nrelatab; i++) {
1105 rela = ef->relatab[i].rela;
1107 link_elf_error(ef->lf.filename, "lost a relatab!");
1110 relalim = rela + ef->relatab[i].nrela;
1111 base = findbase(ef, ef->relatab[i].sec);
1113 link_elf_error(ef->lf.filename,
1114 "lost base for relatab");
1117 for ( ; rela < relalim; rela++) {
1118 symidx = ELF_R_SYM(rela->r_info);
1119 if (symidx >= ef->ddbsymcnt)
1121 sym = ef->ddbsymtab + symidx;
1122 /* Local relocs are already done */
1123 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1125 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1127 symname = symbol_name(ef, rela->r_info);
1128 printf("link_elf_obj: symbol %s undefined\n",
1136 * Only clean SHN_FBSD_CACHED for successful return. If we
1137 * modified symbol table for the object but found an
1138 * unresolved symbol, there is no reason to roll back.
1140 elf_obj_cleanup_globals_cache(ef);
1146 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1148 elf_file_t ef = (elf_file_t) lf;
1149 const Elf_Sym *symp;
1153 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1154 strp = ef->ddbstrtab + symp->st_name;
1155 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1156 *sym = (c_linker_sym_t) symp;
1164 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1165 linker_symval_t *symval)
1167 elf_file_t ef = (elf_file_t) lf;
1168 const Elf_Sym *es = (const Elf_Sym*) sym;
1170 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1171 symval->name = ef->ddbstrtab + es->st_name;
1172 symval->value = (caddr_t)es->st_value;
1173 symval->size = es->st_size;
1180 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1181 c_linker_sym_t *sym, long *diffp)
1183 elf_file_t ef = (elf_file_t) lf;
1184 u_long off = (uintptr_t) (void *) value;
1188 const Elf_Sym *best = NULL;
1191 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1192 if (es->st_name == 0)
1194 st_value = es->st_value;
1195 if (off >= st_value) {
1196 if (off - st_value < diff) {
1197 diff = off - st_value;
1201 } else if (off - st_value == diff) {
1210 *sym = (c_linker_sym_t) best;
1216 * Look up a linker set on an ELF system.
1219 link_elf_lookup_set(linker_file_t lf, const char *name,
1220 void ***startp, void ***stopp, int *countp)
1222 elf_file_t ef = (elf_file_t)lf;
1223 void **start, **stop;
1226 /* Relative to section number */
1227 for (i = 0; i < ef->nprogtab; i++) {
1228 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1229 strcmp(ef->progtab[i].name + 4, name) == 0) {
1230 start = (void **)ef->progtab[i].addr;
1231 stop = (void **)((char *)ef->progtab[i].addr +
1232 ef->progtab[i].size);
1233 count = stop - start;
1247 link_elf_each_function_name(linker_file_t file,
1248 int (*callback)(const char *, void *), void *opaque)
1250 elf_file_t ef = (elf_file_t)file;
1251 const Elf_Sym *symp;
1254 /* Exhaustive search */
1255 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1256 if (symp->st_value != 0 &&
1257 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1258 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1267 link_elf_each_function_nameval(linker_file_t file,
1268 linker_function_nameval_callback_t callback, void *opaque)
1270 linker_symval_t symval;
1271 elf_file_t ef = (elf_file_t)file;
1272 const Elf_Sym* symp;
1275 /* Exhaustive search */
1276 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1277 if (symp->st_value != 0 &&
1278 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1279 error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval);
1282 error = callback(file, i, &symval, opaque);
1291 elf_obj_cleanup_globals_cache(elf_file_t ef)
1296 for (i = 0; i < ef->ddbsymcnt; i++) {
1297 sym = ef->ddbsymtab + i;
1298 if (sym->st_shndx == SHN_FBSD_CACHED) {
1299 sym->st_shndx = SHN_UNDEF;
1306 * Symbol lookup function that can be used when the symbol index is known (ie
1307 * in relocations). It uses the symbol index instead of doing a fully fledged
1308 * hash table based lookup when such is valid. For example for local symbols.
1309 * This is not only more efficient, it's also more correct. It's not always
1310 * the case that the symbol can be found through the hash table.
1313 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1315 elf_file_t ef = (elf_file_t)lf;
1320 /* Don't even try to lookup the symbol if the index is bogus. */
1321 if (symidx >= ef->ddbsymcnt) {
1326 sym = ef->ddbsymtab + symidx;
1328 /* Quick answer if there is a definition included. */
1329 if (sym->st_shndx != SHN_UNDEF) {
1330 *res = sym->st_value;
1334 /* If we get here, then it is undefined and needs a lookup. */
1335 switch (ELF_ST_BIND(sym->st_info)) {
1337 /* Local, but undefined? huh? */
1343 /* Relative to Data or Function name */
1344 symbol = ef->ddbstrtab + sym->st_name;
1346 /* Force a lookup failure if the symbol name is bogus. */
1351 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1354 * Cache global lookups during module relocation. The failure
1355 * case is particularly expensive for callers, who must scan
1356 * through the entire globals table doing strcmp(). Cache to
1357 * avoid doing such work repeatedly.
1359 * After relocation is complete, undefined globals will be
1360 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1364 sym->st_shndx = SHN_FBSD_CACHED;
1365 sym->st_value = res1;
1368 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1381 link_elf_fix_link_set(elf_file_t ef)
1383 static const char startn[] = "__start_";
1384 static const char stopn[] = "__stop_";
1386 const char *sym_name, *linkset_name;
1387 Elf_Addr startp, stopp;
1392 for (symidx = 1 /* zero entry is special */;
1393 symidx < ef->ddbsymcnt; symidx++) {
1394 sym = ef->ddbsymtab + symidx;
1395 if (sym->st_shndx != SHN_UNDEF)
1398 sym_name = ef->ddbstrtab + sym->st_name;
1399 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1401 linkset_name = sym_name + sizeof(startn) - 1;
1403 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1405 linkset_name = sym_name + sizeof(stopn) - 1;
1410 for (i = 0; i < ef->nprogtab; i++) {
1411 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1412 startp = (Elf_Addr)ef->progtab[i].addr;
1413 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1417 if (i == ef->nprogtab)
1420 sym->st_value = start ? startp : stopp;
1426 link_elf_reloc_local(linker_file_t lf)
1428 elf_file_t ef = (elf_file_t)lf;
1429 const Elf_Rel *rellim;
1431 const Elf_Rela *relalim;
1432 const Elf_Rela *rela;
1438 link_elf_fix_link_set(ef);
1440 /* Perform relocations without addend if there are any: */
1441 for (i = 0; i < ef->nreltab; i++) {
1442 rel = ef->reltab[i].rel;
1444 link_elf_error(ef->lf.filename, "lost a reltab");
1447 rellim = rel + ef->reltab[i].nrel;
1448 base = findbase(ef, ef->reltab[i].sec);
1450 link_elf_error(ef->lf.filename, "lost base for reltab");
1453 for ( ; rel < rellim; rel++) {
1454 symidx = ELF_R_SYM(rel->r_info);
1455 if (symidx >= ef->ddbsymcnt)
1457 sym = ef->ddbsymtab + symidx;
1458 /* Only do local relocs */
1459 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1461 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1466 /* Perform relocations with addend if there are any: */
1467 for (i = 0; i < ef->nrelatab; i++) {
1468 rela = ef->relatab[i].rela;
1470 link_elf_error(ef->lf.filename, "lost a relatab!");
1473 relalim = rela + ef->relatab[i].nrela;
1474 base = findbase(ef, ef->relatab[i].sec);
1476 link_elf_error(ef->lf.filename, "lost base for reltab");
1479 for ( ; rela < relalim; rela++) {
1480 symidx = ELF_R_SYM(rela->r_info);
1481 if (symidx >= ef->ddbsymcnt)
1483 sym = ef->ddbsymtab + symidx;
1484 /* Only do local relocs */
1485 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1487 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1495 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1497 elf_file_t ef = (elf_file_t)lf;
1499 *symtab = ef->ddbsymtab;
1501 if (*symtab == NULL)
1504 return (ef->ddbsymcnt);
1508 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1510 elf_file_t ef = (elf_file_t)lf;
1512 *strtab = ef->ddbstrtab;
1514 if (*strtab == NULL)
1517 return (ef->ddbstrcnt);