2 * Copyright (c) 1998 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
27 * $FreeBSD: src/sys/kern/link_elf.c,v 1.24 1999/12/24 15:33:36 bde Exp $
28 * $DragonFly: src/sys/kern/link_elf.c,v 1.29 2008/08/01 23:11:16 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
36 #include <sys/nlookup.h>
37 #include <sys/fcntl.h>
38 #include <sys/vnode.h>
39 #include <sys/linker.h>
40 #include <machine/elf.h>
43 #include <vm/vm_param.h>
44 #include <vm/vm_zone.h>
45 #include <vm/vm_object.h>
46 #include <vm/vm_kern.h>
47 #include <vm/vm_extern.h>
50 #include <vm/vm_map.h>
52 static int link_elf_obj_preload_file(const char *, linker_file_t *);
53 static int link_elf_obj_preload_finish(linker_file_t);
54 static int link_elf_obj_load_file(const char *, linker_file_t *);
56 link_elf_obj_lookup_symbol(linker_file_t, const char *,
58 static int link_elf_obj_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t *);
60 link_elf_obj_search_symbol(linker_file_t, caddr_t value,
61 c_linker_sym_t * sym, long *diffp);
63 static void link_elf_obj_unload_file(linker_file_t);
65 link_elf_obj_lookup_set(linker_file_t, const char *,
66 void ***, void ***, int *);
67 static void link_elf_obj_reloc_local(linker_file_t lf);
68 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *);
70 static struct linker_class_ops link_elf_obj_class_ops = {
71 link_elf_obj_load_file,
72 link_elf_obj_preload_file,
75 static struct linker_file_ops link_elf_obj_file_ops = {
76 .lookup_symbol = link_elf_obj_lookup_symbol,
77 .symbol_values = link_elf_obj_symbol_values,
78 .search_symbol = link_elf_obj_search_symbol,
79 .preload_finish = link_elf_obj_preload_finish,
80 .unload = link_elf_obj_unload_file,
81 .lookup_set = link_elf_obj_lookup_set,
88 int sec; /* Original section */
105 typedef struct elf_file {
108 caddr_t address; /* Relocation address */
109 size_t bytes; /* Chunk size in bytes */
110 vm_object_t object; /* VM object to hold file pages */
113 Elf_progent *progtab;
116 Elf_relaent *relatab;
122 Elf_Sym *ddbsymtab; /* The symbol table we are using */
123 long ddbsymcnt; /* Number of symbols */
124 caddr_t ddbstrtab; /* String table */
125 long ddbstrcnt; /* number of bytes in string table */
127 caddr_t shstrtab; /* Section name string table */
128 long shstrcnt; /* number of bytes in string table */
130 caddr_t ctftab; /* CTF table */
131 long ctfcnt; /* number of bytes in CTF table */
132 caddr_t ctfoff; /* CTF offset table */
133 caddr_t typoff; /* Type offset table */
134 long typlen; /* Number of type entries. */
138 static int relocate_file(linker_file_t lf);
141 * The kernel symbol table starts here.
143 extern struct _dynamic _DYNAMIC;
146 link_elf_obj_init(void *arg)
148 #if ELF_TARG_CLASS == ELFCLASS32
149 linker_add_class("elf32", NULL, &link_elf_obj_class_ops);
151 linker_add_class("elf64", NULL, &link_elf_obj_class_ops);
155 SYSINIT(link_elf, SI_BOOT2_KLD, SI_ORDER_SECOND, link_elf_obj_init, 0);
158 link_elf_obj_error(const char *file, const char *s)
160 kprintf("kldload: %s: %s\n", file, s);
164 link_elf_obj_preload_file(const char *filename, linker_file_t *result)
169 caddr_t modptr, baseptr, sizeptr;
174 int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
177 * Look to see if we have the module preloaded.
179 modptr = preload_search_by_name(filename);
183 /* It's preloaded, check we can handle it and collect information */
184 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
185 baseptr = preload_search_info(modptr, MODINFO_ADDR);
186 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
187 hdr = (Elf_Ehdr *) preload_search_info(modptr, MODINFO_METADATA |
189 shdr = (Elf_Shdr *) preload_search_info(modptr, MODINFO_METADATA |
192 (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " obj module") != 0 &&
193 strcmp(type, "elf obj module") != 0)) {
196 if (baseptr == NULL || sizeptr == NULL || hdr == NULL || shdr == NULL)
199 ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
201 ef->address = *(caddr_t *) baseptr;
203 lf = linker_make_file(filename, ef, &link_elf_obj_file_ops);
208 lf->address = ef->address;
209 lf->size = *(size_t *) sizeptr;
211 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
212 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
213 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
214 hdr->e_version != EV_CURRENT ||
215 hdr->e_type != ET_REL ||
216 hdr->e_machine != ELF_TARG_MACH) {
222 /* Scan the section header for information and table sizing. */
225 for (i = 0; i < hdr->e_shnum; i++) {
226 switch (shdr[i].sh_type) {
233 symstrindex = shdr[i].sh_link;
244 shstrindex = hdr->e_shstrndx;
245 if (ef->nprogtab == 0 || symstrindex < 0 ||
246 symstrindex >= hdr->e_shnum ||
247 shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
248 shstrindex >= hdr->e_shnum ||
249 shdr[shstrindex].sh_type != SHT_STRTAB) {
253 /* Allocate space for tracking the load chunks */
254 if (ef->nprogtab != 0)
255 ef->progtab = kmalloc(ef->nprogtab * sizeof(*ef->progtab),
256 M_LINKER, M_WAITOK | M_ZERO);
257 if (ef->nreltab != 0)
258 ef->reltab = kmalloc(ef->nreltab * sizeof(*ef->reltab),
259 M_LINKER, M_WAITOK | M_ZERO);
260 if (ef->nrelatab != 0)
261 ef->relatab = kmalloc(ef->nrelatab * sizeof(*ef->relatab),
262 M_LINKER, M_WAITOK | M_ZERO);
263 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
264 (ef->nreltab != 0 && ef->reltab == NULL) ||
265 (ef->nrelatab != 0 && ef->relatab == NULL)) {
269 /* XXX, relocate the sh_addr fields saved by the loader. */
271 for (i = 0; i < hdr->e_shnum; i++) {
272 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
273 off = shdr[i].sh_addr;
275 for (i = 0; i < hdr->e_shnum; i++) {
276 if (shdr[i].sh_addr != 0)
277 shdr[i].sh_addr = shdr[i].sh_addr - off +
278 (Elf_Addr) ef->address;
281 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
282 ef->ddbsymtab = (Elf_Sym *) shdr[symtabindex].sh_addr;
283 ef->ddbstrcnt = shdr[symstrindex].sh_size;
284 ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
285 ef->shstrcnt = shdr[shstrindex].sh_size;
286 ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
288 /* Now fill out progtab and the relocation tables. */
292 for (i = 0; i < hdr->e_shnum; i++) {
293 switch (shdr[i].sh_type) {
296 ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
297 if (shdr[i].sh_type == SHT_PROGBITS)
298 ef->progtab[pb].name = "<<PROGBITS>>";
300 ef->progtab[pb].name = "<<NOBITS>>";
301 ef->progtab[pb].size = shdr[i].sh_size;
302 ef->progtab[pb].sec = i;
303 if (ef->shstrtab && shdr[i].sh_name != 0)
304 ef->progtab[pb].name =
305 ef->shstrtab + shdr[i].sh_name;
307 if (ef->progtab[pb].name != NULL &&
308 !strcmp(ef->progtab[pb].name, "set_pcpu")) {
311 dpcpu = dpcpu_alloc(shdr[i].sh_size);
316 memcpy(dpcpu, ef->progtab[pb].addr,
317 ef->progtab[pb].size);
318 dpcpu_copy(dpcpu, shdr[i].sh_size);
319 ef->progtab[pb].addr = dpcpu;
321 } else if (ef->progtab[pb].name != NULL &&
322 !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
325 vnet_data = vnet_data_alloc(shdr[i].sh_size);
326 if (vnet_data == NULL) {
330 memcpy(vnet_data, ef->progtab[pb].addr,
331 ef->progtab[pb].size);
332 vnet_data_copy(vnet_data, shdr[i].sh_size);
333 ef->progtab[pb].addr = vnet_data;
337 /* Update all symbol values with the offset. */
338 for (j = 0; j < ef->ddbsymcnt; j++) {
339 es = &ef->ddbsymtab[j];
340 if (es->st_shndx != i)
342 es->st_value += (Elf_Addr) ef->progtab[pb].addr;
347 ef->reltab[rl].rel = (Elf_Rel *) shdr[i].sh_addr;
348 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
349 ef->reltab[rl].sec = shdr[i].sh_info;
353 ef->relatab[ra].rela = (Elf_Rela *) shdr[i].sh_addr;
354 ef->relatab[ra].nrela =
355 shdr[i].sh_size / sizeof(Elf_Rela);
356 ef->relatab[ra].sec = shdr[i].sh_info;
361 if (pb != ef->nprogtab)
362 panic("lost progbits");
363 if (rl != ef->nreltab)
364 panic("lost reltab");
365 if (ra != ef->nrelatab)
366 panic("lost relatab");
368 /* Local intra-module relocations */
369 link_elf_obj_reloc_local(lf);
375 /* preload not done this way */
376 linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ );
381 link_elf_obj_preload_finish(linker_file_t lf)
385 error = relocate_file(lf);
391 link_elf_obj_load_file(const char *filename, linker_file_t * result)
393 struct nlookupdata nd;
394 struct thread *td = curthread; /* XXX */
395 struct proc *p = td->td_proc;
415 /* XXX Hack for firmware loading where p == NULL */
421 if (p->p_ucred == NULL) {
422 kprintf("link_elf_obj_load_file: cannot load '%s' from filesystem"
423 " this early\n", filename);
430 pathname = linker_search_path(filename);
431 if (pathname == NULL)
434 error = nlookup_init(&nd, pathname, UIO_SYSSPACE, NLC_FOLLOW | NLC_LOCKVP);
436 error = vn_open(&nd, NULL, FREAD, 0);
437 kfree(pathname, M_LINKER);
443 nd.nl_open_vp = NULL;
447 * Read the elf header from the file.
449 hdr = kmalloc(sizeof(*hdr), M_LINKER, M_WAITOK);
454 error = vn_rdwr(UIO_READ, vp, (void *)hdr, sizeof(*hdr), 0,
455 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
467 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
468 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
469 link_elf_obj_error(filename, "Unsupported file layout");
473 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
474 || hdr->e_version != EV_CURRENT) {
475 link_elf_obj_error(filename, "Unsupported file version");
479 if (hdr->e_type != ET_REL) {
483 if (hdr->e_machine != ELF_TARG_MACH) {
484 link_elf_obj_error(filename, "Unsupported machine");
489 ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
490 lf = linker_make_file(filename, ef, &link_elf_obj_file_ops);
501 /* Allocate and read in the section header */
502 nbytes = hdr->e_shnum * hdr->e_shentsize;
503 if (nbytes == 0 || hdr->e_shoff == 0 ||
504 hdr->e_shentsize != sizeof(Elf_Shdr)) {
508 shdr = kmalloc(nbytes, M_LINKER, M_WAITOK);
514 error = vn_rdwr(UIO_READ, vp, (caddr_t) shdr, nbytes, hdr->e_shoff,
515 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
522 /* Scan the section header for information and table sizing. */
526 for (i = 0; i < hdr->e_shnum; i++) {
527 switch (shdr[i].sh_type) {
535 symstrindex = shdr[i].sh_link;
547 if (ef->nprogtab == 0) {
548 link_elf_obj_error(filename, "file has no contents");
553 /* Only allow one symbol table for now */
554 link_elf_obj_error(filename, "file has no valid symbol table");
558 if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
559 shdr[symstrindex].sh_type != SHT_STRTAB) {
560 link_elf_obj_error(filename, "file has invalid symbol strings");
564 /* Allocate space for tracking the load chunks */
565 if (ef->nprogtab != 0)
566 ef->progtab = kmalloc(ef->nprogtab * sizeof(*ef->progtab),
567 M_LINKER, M_WAITOK | M_ZERO);
568 if (ef->nreltab != 0)
569 ef->reltab = kmalloc(ef->nreltab * sizeof(*ef->reltab),
570 M_LINKER, M_WAITOK | M_ZERO);
571 if (ef->nrelatab != 0)
572 ef->relatab = kmalloc(ef->nrelatab * sizeof(*ef->relatab),
573 M_LINKER, M_WAITOK | M_ZERO);
574 if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
575 (ef->nreltab != 0 && ef->reltab == NULL) ||
576 (ef->nrelatab != 0 && ef->relatab == NULL)) {
580 if (symtabindex == -1)
581 panic("lost symbol table index");
582 /* Allocate space for and load the symbol table */
583 ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
584 ef->ddbsymtab = kmalloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
585 if (ef->ddbsymtab == NULL) {
589 error = vn_rdwr(UIO_READ, vp, (void *)ef->ddbsymtab,
590 shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
591 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
598 if (symstrindex == -1)
599 panic("lost symbol string index");
600 /* Allocate space for and load the symbol strings */
601 ef->ddbstrcnt = shdr[symstrindex].sh_size;
602 ef->ddbstrtab = kmalloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
603 if (ef->ddbstrtab == NULL) {
607 error = vn_rdwr(UIO_READ, vp, ef->ddbstrtab,
608 shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
609 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
616 /* Do we have a string table for the section names? */
618 if (hdr->e_shstrndx != 0 &&
619 shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
620 shstrindex = hdr->e_shstrndx;
621 ef->shstrcnt = shdr[shstrindex].sh_size;
622 ef->shstrtab = kmalloc(shdr[shstrindex].sh_size, M_LINKER,
624 if (ef->shstrtab == NULL) {
628 error = vn_rdwr(UIO_READ, vp, ef->shstrtab,
629 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
630 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
638 /* Size up code/data(progbits) and bss(nobits). */
640 for (i = 0; i < hdr->e_shnum; i++) {
641 switch (shdr[i].sh_type) {
644 alignmask = shdr[i].sh_addralign - 1;
645 mapsize += alignmask;
646 mapsize &= ~alignmask;
647 mapsize += shdr[i].sh_size;
653 * We know how much space we need for the text/data/bss/etc. This
654 * stuff needs to be in a single chunk so that profiling etc can get
655 * the bounds and gdb can associate offsets with modules
657 ef->object = vm_object_allocate(OBJT_DEFAULT,
658 round_page(mapsize) >> PAGE_SHIFT);
659 if (ef->object == NULL) {
663 vm_object_reference(ef->object);
664 ef->address = (caddr_t) vm_map_min(&kernel_map);
668 * In order to satisfy x86_64's architectural requirements on the
669 * location of code and data in the kernel's address space, request a
670 * mapping that is above the kernel.
672 * vkernel64's text+data is outside the managed VM space entirely.
674 #if defined(__amd64__) && defined(_KERNEL_VIRTUAL)
675 error = vkernel_module_memory_alloc(&mapbase, round_page(mapsize));
678 error = vm_map_find(&kernel_map, ef->object, 0, &mapbase,
679 round_page(mapsize), PAGE_SIZE,
680 TRUE, VM_MAPTYPE_NORMAL,
681 VM_PROT_ALL, VM_PROT_ALL, FALSE);
683 vm_object_deallocate(ef->object);
688 error = vm_map_wire(&kernel_map, mapbase,
689 mapbase + round_page(mapsize), 0);
691 if (error != KERN_SUCCESS) {
695 /* Inform the kld system about the situation */
696 lf->address = ef->address = (caddr_t) mapbase;
697 lf->size = round_page(mapsize);
701 * Now load code/data(progbits), zero bss(nobits), allocate space for
708 for (i = 0; i < hdr->e_shnum; i++) {
709 switch (shdr[i].sh_type) {
712 alignmask = shdr[i].sh_addralign - 1;
713 mapbase += alignmask;
714 mapbase &= ~alignmask;
715 if (ef->shstrtab && shdr[i].sh_name != 0)
716 ef->progtab[pb].name =
717 ef->shstrtab + shdr[i].sh_name;
718 else if (shdr[i].sh_type == SHT_PROGBITS)
719 ef->progtab[pb].name = "<<PROGBITS>>";
721 ef->progtab[pb].name = "<<NOBITS>>";
723 if (ef->progtab[pb].name != NULL &&
724 !strcmp(ef->progtab[pb].name, "set_pcpu"))
725 ef->progtab[pb].addr =
726 dpcpu_alloc(shdr[i].sh_size);
728 else if (ef->progtab[pb].name != NULL &&
729 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
730 ef->progtab[pb].addr =
731 vnet_data_alloc(shdr[i].sh_size);
735 ef->progtab[pb].addr =
736 (void *)(uintptr_t) mapbase;
737 if (ef->progtab[pb].addr == NULL) {
741 ef->progtab[pb].size = shdr[i].sh_size;
742 ef->progtab[pb].sec = i;
743 if (shdr[i].sh_type == SHT_PROGBITS) {
744 error = vn_rdwr(UIO_READ, vp,
745 ef->progtab[pb].addr,
746 shdr[i].sh_size, shdr[i].sh_offset,
747 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
756 /* Initialize the per-cpu or vnet area. */
757 if (ef->progtab[pb].addr != (void *)mapbase &&
758 !strcmp(ef->progtab[pb].name, "set_pcpu"))
759 dpcpu_copy(ef->progtab[pb].addr,
762 else if (ef->progtab[pb].addr !=
764 !strcmp(ef->progtab[pb].name, VNET_SETNAME))
765 vnet_data_copy(ef->progtab[pb].addr,
770 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
772 /* Update all symbol values with the offset. */
773 for (j = 0; j < ef->ddbsymcnt; j++) {
774 es = &ef->ddbsymtab[j];
775 if (es->st_shndx != i)
777 es->st_value += (Elf_Addr) ef->progtab[pb].addr;
779 mapbase += shdr[i].sh_size;
783 ef->reltab[rl].rel = kmalloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
784 ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
785 ef->reltab[rl].sec = shdr[i].sh_info;
786 error = vn_rdwr(UIO_READ, vp,
787 (void *)ef->reltab[rl].rel,
788 shdr[i].sh_size, shdr[i].sh_offset,
789 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
799 ef->relatab[ra].rela = kmalloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
800 ef->relatab[ra].nrela = shdr[i].sh_size / sizeof(Elf_Rela);
801 ef->relatab[ra].sec = shdr[i].sh_info;
802 error = vn_rdwr(UIO_READ, vp,
803 (void *)ef->relatab[ra].rela,
804 shdr[i].sh_size, shdr[i].sh_offset,
805 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
816 if (pb != ef->nprogtab)
817 panic("lost progbits");
818 if (rl != ef->nreltab)
819 panic("lost reltab");
820 if (ra != ef->nrelatab)
821 panic("lost relatab");
822 if (mapbase != (vm_offset_t) ef->address + mapsize)
823 panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
824 mapbase, ef->address, mapsize,
825 (vm_offset_t) ef->address + mapsize);
827 /* Local intra-module relocations */
828 link_elf_obj_reloc_local(lf);
830 /* Pull in dependencies */
831 error = linker_load_dependencies(lf);
835 /* External relocations */
836 error = relocate_file(lf);
844 linker_file_unload(lf /*, LINKER_UNLOAD_FORCE */);
846 kfree(hdr, M_LINKER);
854 link_elf_obj_unload_file(linker_file_t file)
856 elf_file_t ef = file->priv;
860 for (i = 0; i < ef->nprogtab; i++) {
861 if (ef->progtab[i].size == 0)
863 if (ef->progtab[i].name == NULL)
866 if (!strcmp(ef->progtab[i].name, "set_pcpu"))
867 dpcpu_free(ef->progtab[i].addr,
868 ef->progtab[i].size);
870 else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
871 vnet_data_free(ef->progtab[i].addr,
872 ef->progtab[i].size);
879 kfree(ef->reltab, M_LINKER);
881 kfree(ef->relatab, M_LINKER);
883 kfree(ef->progtab, M_LINKER);
885 kfree(ef->ctftab, M_LINKER);
887 kfree(ef->ctfoff, M_LINKER);
889 kfree(ef->typoff, M_LINKER);
890 if (file->filename != NULL)
891 preload_delete_name(file->filename);
893 /* XXX reclaim module memory? */
897 for (i = 0; i < ef->nreltab; i++)
898 if (ef->reltab[i].rel)
899 kfree(ef->reltab[i].rel, M_LINKER);
900 for (i = 0; i < ef->nrelatab; i++)
901 if (ef->relatab[i].rela)
902 kfree(ef->relatab[i].rela, M_LINKER);
904 kfree(ef->reltab, M_LINKER);
906 kfree(ef->relatab, M_LINKER);
908 kfree(ef->progtab, M_LINKER);
911 #if defined(__amd64__) && defined(_KERNEL_VIRTUAL)
912 vkernel_module_memory_free((vm_offset_t)ef->address, ef->bytes);
914 vm_map_remove(&kernel_map, (vm_offset_t) ef->address,
915 (vm_offset_t) ef->address +
916 (ef->object->size << PAGE_SHIFT));
918 vm_object_deallocate(ef->object);
922 kfree(ef->e_shdr, M_LINKER);
924 kfree(ef->ddbsymtab, M_LINKER);
926 kfree(ef->ddbstrtab, M_LINKER);
928 kfree(ef->shstrtab, M_LINKER);
930 kfree(ef->ctftab, M_LINKER);
932 kfree(ef->ctfoff, M_LINKER);
934 kfree(ef->typoff, M_LINKER);
939 symbol_name(elf_file_t ef, Elf_Size r_info)
943 if (ELF_R_SYM(r_info)) {
944 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
945 return ef->ddbstrtab + ref->st_name;
951 findbase(elf_file_t ef, int sec)
956 for (i = 0; i < ef->nprogtab; i++) {
957 if (sec == ef->progtab[i].sec) {
958 base = (Elf_Addr)ef->progtab[i].addr;
966 relocate_file(linker_file_t lf)
968 elf_file_t ef = lf->priv;
969 const Elf_Rel *rellim;
971 const Elf_Rela *relalim;
972 const Elf_Rela *rela;
979 /* Perform relocations without addend if there are any: */
980 for (i = 0; i < ef->nreltab; i++) {
981 rel = ef->reltab[i].rel;
983 panic("lost a reltab!");
984 rellim = rel + ef->reltab[i].nrel;
985 base = findbase(ef, ef->reltab[i].sec);
987 panic("lost base for reltab");
988 for ( ; rel < rellim; rel++) {
989 symidx = ELF_R_SYM(rel->r_info);
990 if (symidx >= ef->ddbsymcnt)
992 sym = ef->ddbsymtab + symidx;
993 /* Local relocs are already done */
994 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
996 if (elf_reloc(lf, base, rel, ELF_RELOC_REL,
998 symname = symbol_name(ef, rel->r_info);
999 kprintf("link_elf_obj_obj: symbol %s undefined\n",
1006 /* Perform relocations with addend if there are any: */
1007 for (i = 0; i < ef->nrelatab; i++) {
1008 rela = ef->relatab[i].rela;
1010 panic("lost a relatab!");
1011 relalim = rela + ef->relatab[i].nrela;
1012 base = findbase(ef, ef->relatab[i].sec);
1014 panic("lost base for relatab");
1015 for ( ; rela < relalim; rela++) {
1016 symidx = ELF_R_SYM(rela->r_info);
1017 if (symidx >= ef->ddbsymcnt)
1019 sym = ef->ddbsymtab + symidx;
1020 /* Local relocs are already done */
1021 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1023 if (elf_reloc(lf, base, rela, ELF_RELOC_RELA,
1025 symname = symbol_name(ef, rela->r_info);
1026 kprintf("link_elf_obj_obj: symbol %s undefined\n",
1037 link_elf_obj_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1039 elf_file_t ef = lf->priv;
1040 const Elf_Sym *symp;
1044 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1045 strp = ef->ddbstrtab + symp->st_name;
1046 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1047 *sym = (c_linker_sym_t) symp;
1055 link_elf_obj_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1056 linker_symval_t *symval)
1058 elf_file_t ef = lf->priv;
1059 const Elf_Sym *es = (const Elf_Sym*) sym;
1061 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1062 symval->name = ef->ddbstrtab + es->st_name;
1063 symval->value = (caddr_t)es->st_value;
1064 symval->size = es->st_size;
1071 link_elf_obj_search_symbol(linker_file_t lf, caddr_t value,
1072 c_linker_sym_t *sym, long *diffp)
1074 elf_file_t ef = lf->priv;
1075 u_long off = (uintptr_t) (void *) value;
1079 const Elf_Sym *best = 0;
1082 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1083 if (es->st_name == 0)
1085 st_value = es->st_value;
1086 if (off >= st_value) {
1087 if (off - st_value < diff) {
1088 diff = off - st_value;
1092 } else if (off - st_value == diff) {
1101 *sym = (c_linker_sym_t) best;
1107 * Look up a linker set on an ELF system.
1110 link_elf_obj_lookup_set(linker_file_t lf, const char *name,
1111 void ***startp, void ***stopp, int *countp)
1113 elf_file_t ef = lf->priv;
1114 void **start, **stop;
1117 /* Relative to section number */
1118 for (i = 0; i < ef->nprogtab; i++) {
1119 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1120 strcmp(ef->progtab[i].name + 4, name) == 0) {
1121 start = (void **)ef->progtab[i].addr;
1122 stop = (void **)((char *)ef->progtab[i].addr +
1123 ef->progtab[i].size);
1124 count = stop - start;
1138 * Symbol lookup function that can be used when the symbol index is known (ie
1139 * in relocations). It uses the symbol index instead of doing a fully fledged
1140 * hash table based lookup when such is valid. For example for local symbols.
1141 * This is not only more efficient, it's also more correct. It's not always
1142 * the case that the symbol can be found through the hash table.
1145 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *result)
1147 elf_file_t ef = lf->priv;
1151 /* Don't even try to lookup the symbol if the index is bogus. */
1152 if (symidx >= ef->ddbsymcnt)
1155 sym = ef->ddbsymtab + symidx;
1157 /* Quick answer if there is a definition included. */
1158 if (sym->st_shndx != SHN_UNDEF) {
1159 *result = sym->st_value;
1163 /* If we get here, then it is undefined and needs a lookup. */
1164 switch (ELF_ST_BIND(sym->st_info)) {
1166 /* Local, but undefined? huh? */
1170 /* Relative to Data or Function name */
1171 symbol = ef->ddbstrtab + sym->st_name;
1173 /* Force a lookup failure if the symbol name is bogus. */
1176 return (linker_file_lookup_symbol(lf, symbol, deps, (caddr_t *)result));
1179 kprintf("link_elf_obj_obj: Weak symbols not supported\n");
1188 link_elf_obj_fix_link_set(elf_file_t ef)
1190 static const char startn[] = "__start_";
1191 static const char stopn[] = "__stop_";
1193 const char *sym_name, *linkset_name;
1194 Elf_Addr startp, stopp;
1199 for (symidx = 1 /* zero entry is special */;
1200 symidx < ef->ddbsymcnt; symidx++) {
1201 sym = ef->ddbsymtab + symidx;
1202 if (sym->st_shndx != SHN_UNDEF)
1205 sym_name = ef->ddbstrtab + sym->st_name;
1206 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1208 linkset_name = sym_name + sizeof(startn) - 1;
1210 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1212 linkset_name = sym_name + sizeof(stopn) - 1;
1217 for (i = 0; i < ef->nprogtab; i++) {
1218 if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1219 startp = (Elf_Addr)ef->progtab[i].addr;
1220 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1224 if (i == ef->nprogtab)
1227 sym->st_value = start ? startp : stopp;
1233 link_elf_obj_reloc_local(linker_file_t lf)
1235 elf_file_t ef = lf->priv;
1236 const Elf_Rel *rellim;
1238 const Elf_Rela *relalim;
1239 const Elf_Rela *rela;
1245 link_elf_obj_fix_link_set(ef);
1247 /* Perform relocations without addend if there are any: */
1248 for (i = 0; i < ef->nreltab; i++) {
1249 rel = ef->reltab[i].rel;
1251 panic("lost a reltab!");
1252 rellim = rel + ef->reltab[i].nrel;
1253 base = findbase(ef, ef->reltab[i].sec);
1255 panic("lost base for reltab");
1256 for ( ; rel < rellim; rel++) {
1257 symidx = ELF_R_SYM(rel->r_info);
1258 if (symidx >= ef->ddbsymcnt)
1260 sym = ef->ddbsymtab + symidx;
1261 /* Only do local relocs */
1262 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1264 elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1269 /* Perform relocations with addend if there are any: */
1270 for (i = 0; i < ef->nrelatab; i++) {
1271 rela = ef->relatab[i].rela;
1273 panic("lost a relatab!");
1274 relalim = rela + ef->relatab[i].nrela;
1275 base = findbase(ef, ef->relatab[i].sec);
1277 panic("lost base for relatab");
1278 for ( ; rela < relalim; rela++) {
1279 symidx = ELF_R_SYM(rela->r_info);
1280 if (symidx >= ef->ddbsymcnt)
1282 sym = ef->ddbsymtab + symidx;
1283 /* Only do local relocs */
1284 if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1286 elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,