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 void 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_AMD64_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_AMD64_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_AMD64_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;
380 /* Update all symbol values with the offset. */
381 for (j = 0; j < ef->ddbsymcnt; j++) {
382 es = &ef->ddbsymtab[j];
383 if (es->st_shndx != i)
385 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
390 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
391 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
392 ef->reltab[rl].sec = shdr[i].sh_info;
396 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
397 ef->relatab[ra].nrela =
398 shdr[i].sh_size / sizeof(Elf_Rela);
399 ef->relatab[ra].sec = shdr[i].sh_info;
404 if (pb != ef->nprogtab)
405 panic("lost progbits");
406 if (rl != ef->nreltab)
407 panic("lost reltab");
408 if (ra != ef->nrelatab)
409 panic("lost relatab");
411 /* Local intra-module relocations */
412 link_elf_reloc_local(lf);
418 /* preload not done this way */
419 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
424 link_elf_link_preload_finish(linker_file_t lf)
430 error = relocate_file(ef);
434 /* Notify MD code that a module is being loaded. */
435 error = elf_cpu_load_file(lf);
443 link_elf_load_file(linker_class_t cls, const char *filename,
444 linker_file_t *result)
447 struct thread *td = curthread; /* XXX */
471 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
473 error = vn_open(&nd, &flags, 0, NULL);
476 NDFREE(&nd, NDF_ONLY_PNBUF);
477 if (nd.ni_vp->v_type != VREG) {
482 error = mac_kld_check_load(td->td_ucred, nd.ni_vp);
488 /* Read the elf header from the file. */
489 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
490 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)hdr, sizeof(*hdr), 0,
491 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
505 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
506 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
507 link_elf_error(filename, "Unsupported file layout");
511 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
512 || hdr->e_version != EV_CURRENT) {
513 link_elf_error(filename, "Unsupported file version");
517 if (hdr->e_type != ET_REL) {
521 if (hdr->e_machine != ELF_TARG_MACH) {
522 link_elf_error(filename, "Unsupported machine");
527 lf = linker_make_file(filename, &link_elf_class);
532 ef = (elf_file_t) lf;
538 /* Allocate and read in the section header */
539 nbytes = hdr->e_shnum * hdr->e_shentsize;
540 if (nbytes == 0 || hdr->e_shoff == 0 ||
541 hdr->e_shentsize != sizeof(Elf_Shdr)) {
545 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
547 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes, hdr->e_shoff,
548 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
556 /* Scan the section header for information and table sizing. */
560 for (i = 0; i < hdr->e_shnum; i++) {
561 if (shdr[i].sh_size == 0)
563 switch (shdr[i].sh_type) {
567 case SHT_AMD64_UNWIND:
574 symstrindex = shdr[i].sh_link;
586 if (ef->nprogtab == 0) {
587 link_elf_error(filename, "file has no contents");
592 /* Only allow one symbol table for now */
593 link_elf_error(filename, "file has no valid symbol table");
597 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
598 shdr[symstrindex].sh_type != SHT_STRTAB) {
599 link_elf_error(filename, "file has invalid symbol strings");
604 /* Allocate space for tracking the load chunks */
605 if (ef->nprogtab != 0)
606 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
607 M_LINKER, M_WAITOK | M_ZERO);
608 if (ef->nreltab != 0)
609 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
610 M_LINKER, M_WAITOK | M_ZERO);
611 if (ef->nrelatab != 0)
612 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
613 M_LINKER, M_WAITOK | M_ZERO);
615 if (symtabindex == -1)
616 panic("lost symbol table index");
617 /* Allocate space for and load the symbol table */
618 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
619 ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
620 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab,
621 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
622 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
631 if (symstrindex == -1)
632 panic("lost symbol string index");
633 /* Allocate space for and load the symbol strings */
634 ef->ddbstrcnt = shdr[symstrindex].sh_size;
635 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
636 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab,
637 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
638 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
647 /* Do we have a string table for the section names? */
649 if (hdr->e_shstrndx != 0 &&
650 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
651 shstrindex = hdr->e_shstrndx;
652 ef->shstrcnt = shdr[shstrindex].sh_size;
653 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
655 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab,
656 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
657 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
667 /* Size up code/data(progbits) and bss(nobits). */
669 for (i = 0; i < hdr->e_shnum; i++) {
670 if (shdr[i].sh_size == 0)
672 switch (shdr[i].sh_type) {
676 case SHT_AMD64_UNWIND:
678 alignmask = shdr[i].sh_addralign - 1;
679 mapsize += alignmask;
680 mapsize &= ~alignmask;
681 mapsize += shdr[i].sh_size;
687 * We know how much space we need for the text/data/bss/etc.
688 * This stuff needs to be in a single chunk so that profiling etc
689 * can get the bounds and gdb can associate offsets with modules
691 ef->object = vm_object_allocate(OBJT_DEFAULT,
692 round_page(mapsize) >> PAGE_SHIFT);
693 if (ef->object == NULL) {
697 ef->address = (caddr_t) vm_map_min(kernel_map);
700 * In order to satisfy amd64's architectural requirements on the
701 * location of code and data in the kernel's address space, request a
702 * mapping that is above the kernel.
707 mapbase = VM_MIN_KERNEL_ADDRESS;
709 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
710 round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
713 vm_object_deallocate(ef->object);
719 error = vm_map_wire(kernel_map, mapbase,
720 mapbase + round_page(mapsize),
721 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
722 if (error != KERN_SUCCESS) {
727 /* Inform the kld system about the situation */
728 lf->address = ef->address = (caddr_t)mapbase;
732 * Now load code/data(progbits), zero bss(nobits), allocate space for
739 for (i = 0; i < hdr->e_shnum; i++) {
740 if (shdr[i].sh_size == 0)
742 switch (shdr[i].sh_type) {
746 case SHT_AMD64_UNWIND:
748 alignmask = shdr[i].sh_addralign - 1;
749 mapbase += alignmask;
750 mapbase &= ~alignmask;
751 if (ef->shstrtab && shdr[i].sh_name != 0)
752 ef->progtab[pb].name =
753 ef->shstrtab + shdr[i].sh_name;
754 else if (shdr[i].sh_type == SHT_PROGBITS)
755 ef->progtab[pb].name = "<<PROGBITS>>";
757 else if (shdr[i].sh_type == SHT_AMD64_UNWIND)
758 ef->progtab[pb].name = "<<UNWIND>>";
761 ef->progtab[pb].name = "<<NOBITS>>";
762 if (ef->progtab[pb].name != NULL &&
763 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
764 ef->progtab[pb].addr =
765 dpcpu_alloc(shdr[i].sh_size);
767 else if (ef->progtab[pb].name != NULL &&
768 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
769 ef->progtab[pb].addr =
770 vnet_data_alloc(shdr[i].sh_size);
773 ef->progtab[pb].addr =
774 (void *)(uintptr_t)mapbase;
775 if (ef->progtab[pb].addr == NULL) {
779 ef->progtab[pb].size = shdr[i].sh_size;
780 ef->progtab[pb].sec = i;
781 if (shdr[i].sh_type == SHT_PROGBITS
783 || shdr[i].sh_type == SHT_AMD64_UNWIND
786 error = vn_rdwr(UIO_READ, nd.ni_vp,
787 ef->progtab[pb].addr,
788 shdr[i].sh_size, shdr[i].sh_offset,
789 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
797 /* Initialize the per-cpu or vnet area. */
798 if (ef->progtab[pb].addr != (void *)mapbase &&
799 !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
800 dpcpu_copy(ef->progtab[pb].addr,
803 else if (ef->progtab[pb].addr !=
805 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
806 vnet_data_copy(ef->progtab[pb].addr,
810 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
812 /* Update all symbol values with the offset. */
813 for (j = 0; j < ef->ddbsymcnt; j++) {
814 es = &ef->ddbsymtab[j];
815 if (es->st_shndx != i)
817 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
819 mapbase += shdr[i].sh_size;
823 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
825 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
826 ef->reltab[rl].sec = shdr[i].sh_info;
827 error = vn_rdwr(UIO_READ, nd.ni_vp,
828 (void *)ef->reltab[rl].rel,
829 shdr[i].sh_size, shdr[i].sh_offset,
830 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
841 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
843 ef->relatab[ra].nrela =
844 shdr[i].sh_size / sizeof(Elf_Rela);
845 ef->relatab[ra].sec = shdr[i].sh_info;
846 error = vn_rdwr(UIO_READ, nd.ni_vp,
847 (void *)ef->relatab[ra].rela,
848 shdr[i].sh_size, shdr[i].sh_offset,
849 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
861 if (pb != ef->nprogtab)
862 panic("lost progbits");
863 if (rl != ef->nreltab)
864 panic("lost reltab");
865 if (ra != ef->nrelatab)
866 panic("lost relatab");
867 if (mapbase != (vm_offset_t)ef->address + mapsize)
868 panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
869 (u_long)mapbase, ef->address, (u_long)mapsize,
870 (u_long)(vm_offset_t)ef->address + mapsize);
872 /* Local intra-module relocations */
873 link_elf_reloc_local(lf);
875 /* Pull in dependencies */
876 VOP_UNLOCK(nd.ni_vp, 0);
877 error = linker_load_dependencies(lf);
878 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
882 /* External relocations */
883 error = relocate_file(ef);
887 /* Notify MD code that a module is being loaded. */
888 error = elf_cpu_load_file(lf);
895 VOP_UNLOCK(nd.ni_vp, 0);
896 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
898 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
906 link_elf_unload_file(linker_file_t file)
908 elf_file_t ef = (elf_file_t) file;
911 /* Notify MD code that a module is being unloaded. */
912 elf_cpu_unload_file(file);
915 for (i = 0; i < ef->nprogtab; i++) {
916 if (ef->progtab[i].size == 0)
918 if (ef->progtab[i].name == NULL)
920 if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
921 dpcpu_free(ef->progtab[i].addr,
922 ef->progtab[i].size);
924 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
925 vnet_data_free(ef->progtab[i].addr,
926 ef->progtab[i].size);
932 free(ef->reltab, M_LINKER);
934 free(ef->relatab, M_LINKER);
936 free(ef->progtab, M_LINKER);
938 free(ef->ctftab, M_LINKER);
940 free(ef->ctfoff, M_LINKER);
942 free(ef->typoff, M_LINKER);
943 if (file->filename != NULL)
944 preload_delete_name(file->filename);
945 /* XXX reclaim module memory? */
949 for (i = 0; i < ef->nreltab; i++)
950 if (ef->reltab[i].rel)
951 free(ef->reltab[i].rel, M_LINKER);
952 for (i = 0; i < ef->nrelatab; i++)
953 if (ef->relatab[i].rela)
954 free(ef->relatab[i].rela, M_LINKER);
956 free(ef->reltab, M_LINKER);
958 free(ef->relatab, M_LINKER);
960 free(ef->progtab, M_LINKER);
963 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
964 (vm_offset_t) ef->address +
965 (ef->object->size << PAGE_SHIFT));
968 free(ef->e_shdr, M_LINKER);
970 free(ef->ddbsymtab, M_LINKER);
972 free(ef->ddbstrtab, M_LINKER);
974 free(ef->shstrtab, M_LINKER);
976 free(ef->ctftab, M_LINKER);
978 free(ef->ctfoff, M_LINKER);
980 free(ef->typoff, M_LINKER);
984 symbol_name(elf_file_t ef, Elf_Size r_info)
988 if (ELF_R_SYM(r_info)) {
989 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
990 return ef->ddbstrtab + ref->st_name;
996 findbase(elf_file_t ef, int sec)
1001 for (i = 0; i < ef->nprogtab; i++) {
1002 if (sec == ef->progtab[i].sec) {
1003 base = (Elf_Addr)ef->progtab[i].addr;
1011 relocate_file(elf_file_t ef)
1013 const Elf_Rel *rellim;
1015 const Elf_Rela *relalim;
1016 const Elf_Rela *rela;
1017 const char *symname;
1024 /* Perform relocations without addend if there are any: */
1025 for (i = 0; i < ef->nreltab; i++) {
1026 rel = ef->reltab[i].rel;
1028 panic("lost a reltab!");
1029 rellim = rel + ef->reltab[i].nrel;
1030 base = findbase(ef, ef->reltab[i].sec);
1032 panic("lost base for reltab");
1033 for ( ; rel < rellim; rel++) {
1034 symidx = ELF_R_SYM(rel->r_info);
1035 if (symidx >= ef->ddbsymcnt)
1037 sym = ef->ddbsymtab + symidx;
1038 /* Local relocs are already done */
1039 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1041 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1043 symname = symbol_name(ef, rel->r_info);
1044 printf("link_elf_obj: symbol %s undefined\n",
1051 /* Perform relocations with addend if there are any: */
1052 for (i = 0; i < ef->nrelatab; i++) {
1053 rela = ef->relatab[i].rela;
1055 panic("lost a relatab!");
1056 relalim = rela + ef->relatab[i].nrela;
1057 base = findbase(ef, ef->relatab[i].sec);
1059 panic("lost base for relatab");
1060 for ( ; rela < relalim; rela++) {
1061 symidx = ELF_R_SYM(rela->r_info);
1062 if (symidx >= ef->ddbsymcnt)
1064 sym = ef->ddbsymtab + symidx;
1065 /* Local relocs are already done */
1066 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1068 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1070 symname = symbol_name(ef, rela->r_info);
1071 printf("link_elf_obj: symbol %s undefined\n",
1079 * Only clean SHN_FBSD_CACHED for successfull return. If we
1080 * modified symbol table for the object but found an
1081 * unresolved symbol, there is no reason to roll back.
1083 elf_obj_cleanup_globals_cache(ef);
1089 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1091 elf_file_t ef = (elf_file_t) lf;
1092 const Elf_Sym *symp;
1096 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1097 strp = ef->ddbstrtab + symp->st_name;
1098 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1099 *sym = (c_linker_sym_t) symp;
1107 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1108 linker_symval_t *symval)
1110 elf_file_t ef = (elf_file_t) lf;
1111 const Elf_Sym *es = (const Elf_Sym*) sym;
1113 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1114 symval->name = ef->ddbstrtab + es->st_name;
1115 symval->value = (caddr_t)es->st_value;
1116 symval->size = es->st_size;
1123 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1124 c_linker_sym_t *sym, long *diffp)
1126 elf_file_t ef = (elf_file_t) lf;
1127 u_long off = (uintptr_t) (void *) value;
1131 const Elf_Sym *best = 0;
1134 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1135 if (es->st_name == 0)
1137 st_value = es->st_value;
1138 if (off >= st_value) {
1139 if (off - st_value < diff) {
1140 diff = off - st_value;
1144 } else if (off - st_value == diff) {
1153 *sym = (c_linker_sym_t) best;
1159 * Look up a linker set on an ELF system.
1162 link_elf_lookup_set(linker_file_t lf, const char *name,
1163 void ***startp, void ***stopp, int *countp)
1165 elf_file_t ef = (elf_file_t)lf;
1166 void **start, **stop;
1169 /* Relative to section number */
1170 for (i = 0; i < ef->nprogtab; i++) {
1171 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1172 strcmp(ef->progtab[i].name + 4, name) == 0) {
1173 start = (void **)ef->progtab[i].addr;
1174 stop = (void **)((char *)ef->progtab[i].addr +
1175 ef->progtab[i].size);
1176 count = stop - start;
1190 link_elf_each_function_name(linker_file_t file,
1191 int (*callback)(const char *, void *), void *opaque)
1193 elf_file_t ef = (elf_file_t)file;
1194 const Elf_Sym *symp;
1197 /* Exhaustive search */
1198 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1199 if (symp->st_value != 0 &&
1200 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1201 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1210 link_elf_each_function_nameval(linker_file_t file,
1211 linker_function_nameval_callback_t callback, void *opaque)
1213 linker_symval_t symval;
1214 elf_file_t ef = (elf_file_t)file;
1215 const Elf_Sym* symp;
1218 /* Exhaustive search */
1219 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1220 if (symp->st_value != 0 &&
1221 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1222 error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval);
1225 error = callback(file, i, &symval, opaque);
1234 elf_obj_cleanup_globals_cache(elf_file_t ef)
1239 for (i = 0; i < ef->ddbsymcnt; i++) {
1240 sym = ef->ddbsymtab + i;
1241 if (sym->st_shndx == SHN_FBSD_CACHED) {
1242 sym->st_shndx = SHN_UNDEF;
1249 * Symbol lookup function that can be used when the symbol index is known (ie
1250 * in relocations). It uses the symbol index instead of doing a fully fledged
1251 * hash table based lookup when such is valid. For example for local symbols.
1252 * This is not only more efficient, it's also more correct. It's not always
1253 * the case that the symbol can be found through the hash table.
1256 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1258 elf_file_t ef = (elf_file_t)lf;
1263 /* Don't even try to lookup the symbol if the index is bogus. */
1264 if (symidx >= ef->ddbsymcnt) {
1269 sym = ef->ddbsymtab + symidx;
1271 /* Quick answer if there is a definition included. */
1272 if (sym->st_shndx != SHN_UNDEF) {
1273 *res = sym->st_value;
1277 /* If we get here, then it is undefined and needs a lookup. */
1278 switch (ELF_ST_BIND(sym->st_info)) {
1280 /* Local, but undefined? huh? */
1286 /* Relative to Data or Function name */
1287 symbol = ef->ddbstrtab + sym->st_name;
1289 /* Force a lookup failure if the symbol name is bogus. */
1294 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
1297 * Cache global lookups during module relocation. The failure
1298 * case is particularly expensive for callers, who must scan
1299 * through the entire globals table doing strcmp(). Cache to
1300 * avoid doing such work repeatedly.
1302 * After relocation is complete, undefined globals will be
1303 * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
1307 sym->st_shndx = SHN_FBSD_CACHED;
1308 sym->st_value = res1;
1311 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1324 link_elf_fix_link_set(elf_file_t ef)
1326 static const char startn[] = "__start_";
1327 static const char stopn[] = "__stop_";
1329 const char *sym_name, *linkset_name;
1330 Elf_Addr startp, stopp;
1335 for (symidx = 1 /* zero entry is special */;
1336 symidx < ef->ddbsymcnt; symidx++) {
1337 sym = ef->ddbsymtab + symidx;
1338 if (sym->st_shndx != SHN_UNDEF)
1341 sym_name = ef->ddbstrtab + sym->st_name;
1342 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1344 linkset_name = sym_name + sizeof(startn) - 1;
1346 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1348 linkset_name = sym_name + sizeof(stopn) - 1;
1353 for (i = 0; i < ef->nprogtab; i++) {
1354 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1355 startp = (Elf_Addr)ef->progtab[i].addr;
1356 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1360 if (i == ef->nprogtab)
1363 sym->st_value = start ? startp : stopp;
1369 link_elf_reloc_local(linker_file_t lf)
1371 elf_file_t ef = (elf_file_t)lf;
1372 const Elf_Rel *rellim;
1374 const Elf_Rela *relalim;
1375 const Elf_Rela *rela;
1381 link_elf_fix_link_set(ef);
1383 /* Perform relocations without addend if there are any: */
1384 for (i = 0; i < ef->nreltab; i++) {
1385 rel = ef->reltab[i].rel;
1387 panic("lost a reltab!");
1388 rellim = rel + ef->reltab[i].nrel;
1389 base = findbase(ef, ef->reltab[i].sec);
1391 panic("lost base for reltab");
1392 for ( ; rel < rellim; rel++) {
1393 symidx = ELF_R_SYM(rel->r_info);
1394 if (symidx >= ef->ddbsymcnt)
1396 sym = ef->ddbsymtab + symidx;
1397 /* Only do local relocs */
1398 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1400 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1405 /* Perform relocations with addend if there are any: */
1406 for (i = 0; i < ef->nrelatab; i++) {
1407 rela = ef->relatab[i].rela;
1409 panic("lost a relatab!");
1410 relalim = rela + ef->relatab[i].nrela;
1411 base = findbase(ef, ef->relatab[i].sec);
1413 panic("lost base for relatab");
1414 for ( ; rela < relalim; rela++) {
1415 symidx = ELF_R_SYM(rela->r_info);
1416 if (symidx >= ef->ddbsymcnt)
1418 sym = ef->ddbsymtab + symidx;
1419 /* Only do local relocs */
1420 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1422 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1429 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1431 elf_file_t ef = (elf_file_t)lf;
1433 *symtab = ef->ddbsymtab;
1435 if (*symtab == NULL)
1438 return (ef->ddbsymcnt);
1442 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1444 elf_file_t ef = (elf_file_t)lf;
1446 *strtab = ef->ddbstrtab;
1448 if (*strtab == NULL)
1451 return (ef->ddbstrcnt);