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 Elf_Addr elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps);
149 static kobj_method_t link_elf_methods[] = {
150 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
151 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
152 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
153 KOBJMETHOD(linker_unload, link_elf_unload_file),
154 KOBJMETHOD(linker_load_file, link_elf_load_file),
155 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
156 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
157 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
158 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
159 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
160 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
161 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
162 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
166 static struct linker_class link_elf_class = {
167 #if ELF_TARG_CLASS == ELFCLASS32
172 link_elf_methods, sizeof(struct elf_file)
175 static int relocate_file(elf_file_t ef);
178 link_elf_error(const char *filename, const char *s)
180 if (filename == NULL)
181 printf("kldload: %s\n", s);
183 printf("kldload: %s: %s\n", filename, s);
187 link_elf_init(void *arg)
190 linker_add_class(&link_elf_class);
193 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
196 link_elf_link_preload(linker_class_t cls, const char *filename,
197 linker_file_t *result)
202 void *modptr, *baseptr, *sizeptr;
207 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
209 /* Look to see if we have the file preloaded */
210 modptr = preload_search_by_name(filename);
214 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
215 baseptr = preload_search_info(modptr, MODINFO_ADDR);
216 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
217 hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
219 shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
221 if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
222 " obj module") != 0 &&
223 strcmp(type, "elf obj module") != 0)) {
226 if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
230 lf = linker_make_file(filename, &link_elf_class);
236 ef->address = *(caddr_t *)baseptr;
237 lf->address = *(caddr_t *)baseptr;
238 lf->size = *(size_t *)sizeptr;
240 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
241 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
242 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
243 hdr->e_version != EV_CURRENT ||
244 hdr->e_type != ET_REL ||
245 hdr->e_machine != ELF_TARG_MACH) {
251 /* Scan the section header for information and table sizing. */
254 for (i = 0; i < hdr->e_shnum; i++) {
255 switch (shdr[i].sh_type) {
262 symstrindex = shdr[i].sh_link;
273 shstrindex = hdr->e_shstrndx;
274 if (ef->nprogtab == 0 || symstrindex < 0 ||
275 symstrindex >= hdr->e_shnum ||
276 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
277 shstrindex >= hdr->e_shnum ||
278 shdr[shstrindex].sh_type != SHT_STRTAB) {
279 printf("%s: bad/missing section headers\n", filename);
284 /* Allocate space for tracking the load chunks */
285 if (ef->nprogtab != 0)
286 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
287 M_LINKER, M_WAITOK | M_ZERO);
288 if (ef->nreltab != 0)
289 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
290 M_LINKER, M_WAITOK | M_ZERO);
291 if (ef->nrelatab != 0)
292 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
293 M_LINKER, M_WAITOK | M_ZERO);
294 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
295 (ef->nreltab != 0 && ef->reltab == NULL) ||
296 (ef->nrelatab != 0 && ef->relatab == NULL)) {
301 /* XXX, relocate the sh_addr fields saved by the loader. */
303 for (i = 0; i < hdr->e_shnum; i++) {
304 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
305 off = shdr[i].sh_addr;
307 for (i = 0; i < hdr->e_shnum; i++) {
308 if (shdr[i].sh_addr != 0)
309 shdr[i].sh_addr = shdr[i].sh_addr - off +
310 (Elf_Addr)ef->address;
313 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
314 ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
315 ef->ddbstrcnt = shdr[symstrindex].sh_size;
316 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
317 ef->shstrcnt = shdr[shstrindex].sh_size;
318 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
320 /* Now fill out progtab and the relocation tables. */
324 for (i = 0; i < hdr->e_shnum; i++) {
325 switch (shdr[i].sh_type) {
328 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
329 if (shdr[i].sh_type == SHT_PROGBITS)
330 ef->progtab[pb].name = "<<PROGBITS>>";
332 ef->progtab[pb].name = "<<NOBITS>>";
333 ef->progtab[pb].size = shdr[i].sh_size;
334 ef->progtab[pb].sec = i;
335 if (ef->shstrtab && shdr[i].sh_name != 0)
336 ef->progtab[pb].name =
337 ef->shstrtab + shdr[i].sh_name;
338 if (ef->progtab[pb].name != NULL &&
339 !strcmp(ef->progtab[pb].name, "set_pcpu")) {
342 dpcpu = dpcpu_alloc(shdr[i].sh_size);
347 memcpy(dpcpu, ef->progtab[pb].addr,
348 ef->progtab[pb].size);
349 dpcpu_copy(dpcpu, shdr[i].sh_size);
350 ef->progtab[pb].addr = dpcpu;
352 } else if (ef->progtab[pb].name != NULL &&
353 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
356 vnet_data = vnet_data_alloc(shdr[i].sh_size);
357 if (vnet_data == NULL) {
361 memcpy(vnet_data, ef->progtab[pb].addr,
362 ef->progtab[pb].size);
363 vnet_data_copy(vnet_data, shdr[i].sh_size);
364 ef->progtab[pb].addr = vnet_data;
368 /* Update all symbol values with the offset. */
369 for (j = 0; j < ef->ddbsymcnt; j++) {
370 es = &ef->ddbsymtab[j];
371 if (es->st_shndx != i)
373 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
378 ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
379 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
380 ef->reltab[rl].sec = shdr[i].sh_info;
384 ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
385 ef->relatab[ra].nrela =
386 shdr[i].sh_size / sizeof(Elf_Rela);
387 ef->relatab[ra].sec = shdr[i].sh_info;
392 if (pb != ef->nprogtab)
393 panic("lost progbits");
394 if (rl != ef->nreltab)
395 panic("lost reltab");
396 if (ra != ef->nrelatab)
397 panic("lost relatab");
399 /* Local intra-module relocations */
400 link_elf_reloc_local(lf);
406 /* preload not done this way */
407 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
412 link_elf_link_preload_finish(linker_file_t lf)
418 error = relocate_file(ef);
422 /* Notify MD code that a module is being loaded. */
423 error = elf_cpu_load_file(lf);
431 link_elf_load_file(linker_class_t cls, const char *filename,
432 linker_file_t *result)
435 struct thread *td = curthread; /* XXX */
459 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, filename, td);
461 error = vn_open(&nd, &flags, 0, NULL);
464 vfslocked = NDHASGIANT(&nd);
465 NDFREE(&nd, NDF_ONLY_PNBUF);
466 if (nd.ni_vp->v_type != VREG) {
471 error = mac_kld_check_load(td->td_ucred, nd.ni_vp);
477 /* Read the elf header from the file. */
478 hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
483 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)hdr, sizeof(*hdr), 0,
484 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
498 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
499 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
500 link_elf_error(filename, "Unsupported file layout");
504 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
505 || hdr->e_version != EV_CURRENT) {
506 link_elf_error(filename, "Unsupported file version");
510 if (hdr->e_type != ET_REL) {
514 if (hdr->e_machine != ELF_TARG_MACH) {
515 link_elf_error(filename, "Unsupported machine");
520 lf = linker_make_file(filename, &link_elf_class);
525 ef = (elf_file_t) lf;
531 /* Allocate and read in the section header */
532 nbytes = hdr->e_shnum * hdr->e_shentsize;
533 if (nbytes == 0 || hdr->e_shoff == 0 ||
534 hdr->e_shentsize != sizeof(Elf_Shdr)) {
538 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
544 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes, hdr->e_shoff,
545 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
553 /* Scan the section header for information and table sizing. */
557 for (i = 0; i < hdr->e_shnum; i++) {
558 if (shdr[i].sh_size == 0)
560 switch (shdr[i].sh_type) {
568 symstrindex = shdr[i].sh_link;
580 if (ef->nprogtab == 0) {
581 link_elf_error(filename, "file has no contents");
586 /* Only allow one symbol table for now */
587 link_elf_error(filename, "file has no valid symbol table");
591 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
592 shdr[symstrindex].sh_type != SHT_STRTAB) {
593 link_elf_error(filename, "file has invalid symbol strings");
598 /* Allocate space for tracking the load chunks */
599 if (ef->nprogtab != 0)
600 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
601 M_LINKER, M_WAITOK | M_ZERO);
602 if (ef->nreltab != 0)
603 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
604 M_LINKER, M_WAITOK | M_ZERO);
605 if (ef->nrelatab != 0)
606 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
607 M_LINKER, M_WAITOK | M_ZERO);
608 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
609 (ef->nreltab != 0 && ef->reltab == NULL) ||
610 (ef->nrelatab != 0 && ef->relatab == NULL)) {
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 if (ef->ddbsymtab == NULL) {
624 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab,
625 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
626 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
635 if (symstrindex == -1)
636 panic("lost symbol string index");
637 /* Allocate space for and load the symbol strings */
638 ef->ddbstrcnt = shdr[symstrindex].sh_size;
639 ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
640 if (ef->ddbstrtab == NULL) {
644 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab,
645 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
646 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
655 /* Do we have a string table for the section names? */
657 if (hdr->e_shstrndx != 0 &&
658 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
659 shstrindex = hdr->e_shstrndx;
660 ef->shstrcnt = shdr[shstrindex].sh_size;
661 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
663 if (ef->shstrtab == NULL) {
667 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab,
668 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
669 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
679 /* Size up code/data(progbits) and bss(nobits). */
681 for (i = 0; i < hdr->e_shnum; i++) {
682 if (shdr[i].sh_size == 0)
684 switch (shdr[i].sh_type) {
687 alignmask = shdr[i].sh_addralign - 1;
688 mapsize += alignmask;
689 mapsize &= ~alignmask;
690 mapsize += shdr[i].sh_size;
696 * We know how much space we need for the text/data/bss/etc.
697 * This stuff needs to be in a single chunk so that profiling etc
698 * can get the bounds and gdb can associate offsets with modules
700 ef->object = vm_object_allocate(OBJT_DEFAULT,
701 round_page(mapsize) >> PAGE_SHIFT);
702 if (ef->object == NULL) {
706 ef->address = (caddr_t) vm_map_min(kernel_map);
709 * In order to satisfy amd64's architectural requirements on the
710 * location of code and data in the kernel's address space, request a
711 * mapping that is above the kernel.
714 error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
715 round_page(mapsize), TRUE, VM_PROT_ALL, VM_PROT_ALL, FALSE);
717 vm_object_deallocate(ef->object);
723 error = vm_map_wire(kernel_map, mapbase,
724 mapbase + round_page(mapsize),
725 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
726 if (error != KERN_SUCCESS) {
731 /* Inform the kld system about the situation */
732 lf->address = ef->address = (caddr_t)mapbase;
736 * Now load code/data(progbits), zero bss(nobits), allocate space for
743 for (i = 0; i < hdr->e_shnum; i++) {
744 if (shdr[i].sh_size == 0)
746 switch (shdr[i].sh_type) {
749 alignmask = shdr[i].sh_addralign - 1;
750 mapbase += alignmask;
751 mapbase &= ~alignmask;
752 if (ef->shstrtab && shdr[i].sh_name != 0)
753 ef->progtab[pb].name =
754 ef->shstrtab + shdr[i].sh_name;
755 else if (shdr[i].sh_type == SHT_PROGBITS)
756 ef->progtab[pb].name = "<<PROGBITS>>";
758 ef->progtab[pb].name = "<<NOBITS>>";
759 if (ef->progtab[pb].name != NULL &&
760 !strcmp(ef->progtab[pb].name, "set_pcpu"))
761 ef->progtab[pb].addr =
762 dpcpu_alloc(shdr[i].sh_size);
764 else if (ef->progtab[pb].name != NULL &&
765 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
766 ef->progtab[pb].addr =
767 vnet_data_alloc(shdr[i].sh_size);
770 ef->progtab[pb].addr =
771 (void *)(uintptr_t)mapbase;
772 if (ef->progtab[pb].addr == NULL) {
776 ef->progtab[pb].size = shdr[i].sh_size;
777 ef->progtab[pb].sec = i;
778 if (shdr[i].sh_type == SHT_PROGBITS) {
779 error = vn_rdwr(UIO_READ, nd.ni_vp,
780 ef->progtab[pb].addr,
781 shdr[i].sh_size, shdr[i].sh_offset,
782 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
790 /* Initialize the per-cpu or vnet area. */
791 if (ef->progtab[pb].addr != (void *)mapbase &&
792 !strcmp(ef->progtab[pb].name, "set_pcpu"))
793 dpcpu_copy(ef->progtab[pb].addr,
796 else if (ef->progtab[pb].addr !=
798 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
799 vnet_data_copy(ef->progtab[pb].addr,
803 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
805 /* Update all symbol values with the offset. */
806 for (j = 0; j < ef->ddbsymcnt; j++) {
807 es = &ef->ddbsymtab[j];
808 if (es->st_shndx != i)
810 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
812 mapbase += shdr[i].sh_size;
816 ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
818 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
819 ef->reltab[rl].sec = shdr[i].sh_info;
820 error = vn_rdwr(UIO_READ, nd.ni_vp,
821 (void *)ef->reltab[rl].rel,
822 shdr[i].sh_size, shdr[i].sh_offset,
823 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
834 ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
836 ef->relatab[ra].nrela =
837 shdr[i].sh_size / sizeof(Elf_Rela);
838 ef->relatab[ra].sec = shdr[i].sh_info;
839 error = vn_rdwr(UIO_READ, nd.ni_vp,
840 (void *)ef->relatab[ra].rela,
841 shdr[i].sh_size, shdr[i].sh_offset,
842 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
854 if (pb != ef->nprogtab)
855 panic("lost progbits");
856 if (rl != ef->nreltab)
857 panic("lost reltab");
858 if (ra != ef->nrelatab)
859 panic("lost relatab");
860 if (mapbase != (vm_offset_t)ef->address + mapsize)
861 panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
862 (u_long)mapbase, ef->address, (u_long)mapsize,
863 (u_long)(vm_offset_t)ef->address + mapsize);
865 /* Local intra-module relocations */
866 link_elf_reloc_local(lf);
868 /* Pull in dependencies */
869 VOP_UNLOCK(nd.ni_vp, 0);
870 error = linker_load_dependencies(lf);
871 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
875 /* External relocations */
876 error = relocate_file(ef);
880 /* Notify MD code that a module is being loaded. */
881 error = elf_cpu_load_file(lf);
888 VOP_UNLOCK(nd.ni_vp, 0);
889 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
890 VFS_UNLOCK_GIANT(vfslocked);
892 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
900 link_elf_unload_file(linker_file_t file)
902 elf_file_t ef = (elf_file_t) file;
905 /* Notify MD code that a module is being unloaded. */
906 elf_cpu_unload_file(file);
909 for (i = 0; i < ef->nprogtab; i++) {
910 if (ef->progtab[i].size == 0)
912 if (ef->progtab[i].name == NULL)
914 if (!strcmp(ef->progtab[i].name, "set_pcpu"))
915 dpcpu_free(ef->progtab[i].addr,
916 ef->progtab[i].size);
918 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
919 vnet_data_free(ef->progtab[i].addr,
920 ef->progtab[i].size);
926 free(ef->reltab, M_LINKER);
928 free(ef->relatab, M_LINKER);
930 free(ef->progtab, M_LINKER);
932 free(ef->ctftab, M_LINKER);
934 free(ef->ctfoff, M_LINKER);
936 free(ef->typoff, M_LINKER);
937 if (file->filename != NULL)
938 preload_delete_name(file->filename);
939 /* XXX reclaim module memory? */
943 for (i = 0; i < ef->nreltab; i++)
944 if (ef->reltab[i].rel)
945 free(ef->reltab[i].rel, M_LINKER);
946 for (i = 0; i < ef->nrelatab; i++)
947 if (ef->relatab[i].rela)
948 free(ef->relatab[i].rela, M_LINKER);
950 free(ef->reltab, M_LINKER);
952 free(ef->relatab, M_LINKER);
954 free(ef->progtab, M_LINKER);
957 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
958 (vm_offset_t) ef->address +
959 (ef->object->size << PAGE_SHIFT));
962 free(ef->e_shdr, M_LINKER);
964 free(ef->ddbsymtab, M_LINKER);
966 free(ef->ddbstrtab, M_LINKER);
968 free(ef->shstrtab, M_LINKER);
970 free(ef->ctftab, M_LINKER);
972 free(ef->ctfoff, M_LINKER);
974 free(ef->typoff, M_LINKER);
978 symbol_name(elf_file_t ef, Elf_Size r_info)
982 if (ELF_R_SYM(r_info)) {
983 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
984 return ef->ddbstrtab + ref->st_name;
990 findbase(elf_file_t ef, int sec)
995 for (i = 0; i < ef->nprogtab; i++) {
996 if (sec == ef->progtab[i].sec) {
997 base = (Elf_Addr)ef->progtab[i].addr;
1005 relocate_file(elf_file_t ef)
1007 const Elf_Rel *rellim;
1009 const Elf_Rela *relalim;
1010 const Elf_Rela *rela;
1011 const char *symname;
1018 /* Perform relocations without addend if there are any: */
1019 for (i = 0; i < ef->nreltab; i++) {
1020 rel = ef->reltab[i].rel;
1022 panic("lost a reltab!");
1023 rellim = rel + ef->reltab[i].nrel;
1024 base = findbase(ef, ef->reltab[i].sec);
1026 panic("lost base for reltab");
1027 for ( ; rel < rellim; rel++) {
1028 symidx = ELF_R_SYM(rel->r_info);
1029 if (symidx >= ef->ddbsymcnt)
1031 sym = ef->ddbsymtab + symidx;
1032 /* Local relocs are already done */
1033 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1035 if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
1037 symname = symbol_name(ef, rel->r_info);
1038 printf("link_elf_obj: symbol %s undefined\n",
1045 /* Perform relocations with addend if there are any: */
1046 for (i = 0; i < ef->nrelatab; i++) {
1047 rela = ef->relatab[i].rela;
1049 panic("lost a relatab!");
1050 relalim = rela + ef->relatab[i].nrela;
1051 base = findbase(ef, ef->relatab[i].sec);
1053 panic("lost base for relatab");
1054 for ( ; rela < relalim; rela++) {
1055 symidx = ELF_R_SYM(rela->r_info);
1056 if (symidx >= ef->ddbsymcnt)
1058 sym = ef->ddbsymtab + symidx;
1059 /* Local relocs are already done */
1060 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1062 if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
1064 symname = symbol_name(ef, rela->r_info);
1065 printf("link_elf_obj: symbol %s undefined\n",
1076 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1078 elf_file_t ef = (elf_file_t) lf;
1079 const Elf_Sym *symp;
1083 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1084 strp = ef->ddbstrtab + symp->st_name;
1085 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1086 *sym = (c_linker_sym_t) symp;
1094 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1095 linker_symval_t *symval)
1097 elf_file_t ef = (elf_file_t) lf;
1098 const Elf_Sym *es = (const Elf_Sym*) sym;
1100 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1101 symval->name = ef->ddbstrtab + es->st_name;
1102 symval->value = (caddr_t)es->st_value;
1103 symval->size = es->st_size;
1110 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1111 c_linker_sym_t *sym, long *diffp)
1113 elf_file_t ef = (elf_file_t) lf;
1114 u_long off = (uintptr_t) (void *) value;
1118 const Elf_Sym *best = 0;
1121 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1122 if (es->st_name == 0)
1124 st_value = es->st_value;
1125 if (off >= st_value) {
1126 if (off - st_value < diff) {
1127 diff = off - st_value;
1131 } else if (off - st_value == diff) {
1140 *sym = (c_linker_sym_t) best;
1146 * Look up a linker set on an ELF system.
1149 link_elf_lookup_set(linker_file_t lf, const char *name,
1150 void ***startp, void ***stopp, int *countp)
1152 elf_file_t ef = (elf_file_t)lf;
1153 void **start, **stop;
1156 /* Relative to section number */
1157 for (i = 0; i < ef->nprogtab; i++) {
1158 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1159 strcmp(ef->progtab[i].name + 4, name) == 0) {
1160 start = (void **)ef->progtab[i].addr;
1161 stop = (void **)((char *)ef->progtab[i].addr +
1162 ef->progtab[i].size);
1163 count = stop - start;
1177 link_elf_each_function_name(linker_file_t file,
1178 int (*callback)(const char *, void *), void *opaque)
1180 elf_file_t ef = (elf_file_t)file;
1181 const Elf_Sym *symp;
1184 /* Exhaustive search */
1185 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1186 if (symp->st_value != 0 &&
1187 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1188 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1197 link_elf_each_function_nameval(linker_file_t file,
1198 linker_function_nameval_callback_t callback, void *opaque)
1200 linker_symval_t symval;
1201 elf_file_t ef = (elf_file_t)file;
1202 const Elf_Sym* symp;
1205 /* Exhaustive search */
1206 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1207 if (symp->st_value != 0 &&
1208 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1209 error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval);
1212 error = callback(file, i, &symval, opaque);
1221 * Symbol lookup function that can be used when the symbol index is known (ie
1222 * in relocations). It uses the symbol index instead of doing a fully fledged
1223 * hash table based lookup when such is valid. For example for local symbols.
1224 * This is not only more efficient, it's also more correct. It's not always
1225 * the case that the symbol can be found through the hash table.
1228 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps)
1230 elf_file_t ef = (elf_file_t)lf;
1235 /* Don't even try to lookup the symbol if the index is bogus. */
1236 if (symidx >= ef->ddbsymcnt)
1239 sym = ef->ddbsymtab + symidx;
1241 /* Quick answer if there is a definition included. */
1242 if (sym->st_shndx != SHN_UNDEF)
1243 return (sym->st_value);
1245 /* If we get here, then it is undefined and needs a lookup. */
1246 switch (ELF_ST_BIND(sym->st_info)) {
1248 /* Local, but undefined? huh? */
1252 /* Relative to Data or Function name */
1253 symbol = ef->ddbstrtab + sym->st_name;
1255 /* Force a lookup failure if the symbol name is bogus. */
1258 ret = ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1262 printf("link_elf_obj: Weak symbols not supported\n");
1271 link_elf_fix_link_set(elf_file_t ef)
1273 static const char startn[] = "__start_";
1274 static const char stopn[] = "__stop_";
1276 const char *sym_name, *linkset_name;
1277 Elf_Addr startp, stopp;
1282 for (symidx = 1 /* zero entry is special */;
1283 symidx < ef->ddbsymcnt; symidx++) {
1284 sym = ef->ddbsymtab + symidx;
1285 if (sym->st_shndx != SHN_UNDEF)
1288 sym_name = ef->ddbstrtab + sym->st_name;
1289 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1291 linkset_name = sym_name + sizeof(startn) - 1;
1293 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1295 linkset_name = sym_name + sizeof(stopn) - 1;
1300 for (i = 0; i < ef->nprogtab; i++) {
1301 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1302 startp = (Elf_Addr)ef->progtab[i].addr;
1303 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1307 if (i == ef->nprogtab)
1310 sym->st_value = start ? startp : stopp;
1316 link_elf_reloc_local(linker_file_t lf)
1318 elf_file_t ef = (elf_file_t)lf;
1319 const Elf_Rel *rellim;
1321 const Elf_Rela *relalim;
1322 const Elf_Rela *rela;
1328 link_elf_fix_link_set(ef);
1330 /* Perform relocations without addend if there are any: */
1331 for (i = 0; i < ef->nreltab; i++) {
1332 rel = ef->reltab[i].rel;
1334 panic("lost a reltab!");
1335 rellim = rel + ef->reltab[i].nrel;
1336 base = findbase(ef, ef->reltab[i].sec);
1338 panic("lost base for reltab");
1339 for ( ; rel < rellim; rel++) {
1340 symidx = ELF_R_SYM(rel->r_info);
1341 if (symidx >= ef->ddbsymcnt)
1343 sym = ef->ddbsymtab + symidx;
1344 /* Only do local relocs */
1345 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1347 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1352 /* Perform relocations with addend if there are any: */
1353 for (i = 0; i < ef->nrelatab; i++) {
1354 rela = ef->relatab[i].rela;
1356 panic("lost a relatab!");
1357 relalim = rela + ef->relatab[i].nrela;
1358 base = findbase(ef, ef->relatab[i].sec);
1360 panic("lost base for relatab");
1361 for ( ; rela < relalim; rela++) {
1362 symidx = ELF_R_SYM(rela->r_info);
1363 if (symidx >= ef->ddbsymcnt)
1365 sym = ef->ddbsymtab + symidx;
1366 /* Only do local relocs */
1367 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1369 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1376 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1378 elf_file_t ef = (elf_file_t)lf;
1380 *symtab = ef->ddbsymtab;
1382 if (*symtab == NULL)
1385 return (ef->ddbsymcnt);
1389 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1391 elf_file_t ef = (elf_file_t)lf;
1393 *strtab = ef->ddbstrtab;
1395 if (*strtab == NULL)
1398 return (ef->ddbstrcnt);