2 * Copyright (c) 1998-2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/mount.h>
44 #include <sys/namei.h>
45 #include <sys/fcntl.h>
46 #include <sys/vnode.h>
47 #include <sys/linker.h>
49 #include <machine/elf.h>
51 #include <security/mac/mac_framework.h>
54 #include <vm/vm_param.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_extern.h>
61 #include <vm/vm_map.h>
63 #include <sys/link_elf.h>
69 #include "linker_if.h"
73 typedef struct elf_file {
74 struct linker_file lf; /* Common fields */
75 int preloaded; /* Was file pre-loaded */
76 caddr_t address; /* Relocation address */
78 vm_object_t object; /* VM object to hold file pages */
80 Elf_Dyn* dynamic; /* Symbol table etc. */
81 Elf_Hashelt nbuckets; /* DT_HASH info */
83 const Elf_Hashelt* buckets;
84 const Elf_Hashelt* chains;
86 caddr_t strtab; /* DT_STRTAB */
87 int strsz; /* DT_STRSZ */
88 const Elf_Sym* symtab; /* DT_SYMTAB */
89 Elf_Addr* got; /* DT_PLTGOT */
90 const Elf_Rel* pltrel; /* DT_JMPREL */
91 int pltrelsize; /* DT_PLTRELSZ */
92 const Elf_Rela* pltrela; /* DT_JMPREL */
93 int pltrelasize; /* DT_PLTRELSZ */
94 const Elf_Rel* rel; /* DT_REL */
95 int relsize; /* DT_RELSZ */
96 const Elf_Rela* rela; /* DT_RELA */
97 int relasize; /* DT_RELASZ */
99 const Elf_Sym* ddbsymtab; /* The symbol table we are using */
100 long ddbsymcnt; /* Number of symbols */
101 caddr_t ddbstrtab; /* String table */
102 long ddbstrcnt; /* number of bytes in string table */
103 caddr_t symbase; /* malloc'ed symbold base */
104 caddr_t strbase; /* malloc'ed string base */
105 caddr_t ctftab; /* CTF table */
106 long ctfcnt; /* number of bytes in CTF table */
107 caddr_t ctfoff; /* CTF offset table */
108 caddr_t typoff; /* Type offset table */
109 long typlen; /* Number of type entries. */
111 struct link_map gdb; /* hooks for gdb */
115 #include <kern/kern_ctf.c>
117 static int link_elf_link_common_finish(linker_file_t);
118 static int link_elf_link_preload(linker_class_t cls,
119 const char*, linker_file_t*);
120 static int link_elf_link_preload_finish(linker_file_t);
121 static int link_elf_load_file(linker_class_t, const char*, linker_file_t*);
122 static int link_elf_lookup_symbol(linker_file_t, const char*,
124 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t*);
125 static int link_elf_search_symbol(linker_file_t, caddr_t value,
126 c_linker_sym_t* sym, long* diffp);
128 static void link_elf_unload_file(linker_file_t);
129 static void link_elf_unload_preload(linker_file_t);
130 static int link_elf_lookup_set(linker_file_t, const char *,
131 void ***, void ***, int *);
132 static int link_elf_each_function_name(linker_file_t,
133 int (*)(const char *, void *),
135 static int link_elf_each_function_nameval(linker_file_t,
136 linker_function_nameval_callback_t,
138 static void link_elf_reloc_local(linker_file_t);
139 static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
140 static long link_elf_strtab_get(linker_file_t, caddr_t *);
141 static Elf_Addr elf_lookup(linker_file_t lf, Elf_Size symidx, int deps);
143 static kobj_method_t link_elf_methods[] = {
144 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
145 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
146 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
147 KOBJMETHOD(linker_unload, link_elf_unload_file),
148 KOBJMETHOD(linker_load_file, link_elf_load_file),
149 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
150 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
151 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
152 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
153 KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
154 KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
155 KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
156 KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
160 static struct linker_class link_elf_class = {
161 #if ELF_TARG_CLASS == ELFCLASS32
166 link_elf_methods, sizeof(struct elf_file)
169 static int parse_dynamic(elf_file_t ef);
170 static int relocate_file(elf_file_t ef);
171 static int link_elf_preload_parse_symbols(elf_file_t ef);
174 static void r_debug_state(struct r_debug *dummy_one,
175 struct link_map *dummy_two);
178 * A list of loaded modules for GDB to use for loading symbols.
180 struct r_debug r_debug;
182 #define GDB_STATE(s) r_debug.r_state = s; r_debug_state(NULL, NULL);
185 * Function for the debugger to set a breakpoint on to gain control.
188 r_debug_state(struct r_debug *dummy_one __unused,
189 struct link_map *dummy_two __unused)
194 link_elf_add_gdb(struct link_map *l)
196 struct link_map *prev;
200 if (r_debug.r_map == NULL) {
205 /* Append to list. */
206 for (prev = r_debug.r_map; prev->l_next != NULL; prev = prev->l_next)
214 link_elf_delete_gdb(struct link_map *l)
216 if (l->l_prev == NULL) {
218 if ((r_debug.r_map = l->l_next) != NULL)
219 l->l_next->l_prev = NULL;
221 /* Remove any but first. */
222 if ((l->l_prev->l_next = l->l_next) != NULL)
223 l->l_next->l_prev = l->l_prev;
229 Elf_Addr link_elf_get_gp(linker_file_t);
233 * The kernel symbol table starts here.
235 extern struct _dynamic _DYNAMIC;
238 link_elf_error(const char *filename, const char *s)
240 if (filename == NULL)
241 printf("kldload: %s\n", s);
243 printf("kldload: %s: %s\n", filename, s);
247 * Actions performed after linking/loading both the preloaded kernel and any
248 * modules; whether preloaded or dynamicly loaded.
251 link_elf_link_common_finish(linker_file_t lf)
254 elf_file_t ef = (elf_file_t)lf;
259 /* Notify MD code that a module is being loaded. */
260 error = elf_cpu_load_file(lf);
266 ef->gdb.l_addr = lf->address;
267 newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
268 strcpy(newfilename, lf->filename);
269 ef->gdb.l_name = newfilename;
270 ef->gdb.l_ld = ef->dynamic;
271 link_elf_add_gdb(&ef->gdb);
272 GDB_STATE(RT_CONSISTENT);
279 link_elf_init(void* arg)
282 caddr_t modptr, baseptr, sizeptr;
286 linker_add_class(&link_elf_class);
288 dp = (Elf_Dyn*) &_DYNAMIC;
290 modptr = preload_search_by_type("elf" __XSTRING(__ELF_WORD_SIZE) " kernel");
292 modptr = preload_search_by_type("elf kernel");
294 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
297 linker_kernel_file = linker_make_file(modname, &link_elf_class);
298 if (linker_kernel_file == NULL)
299 panic("link_elf_init: Can't create linker structures for kernel");
301 ef = (elf_file_t) linker_kernel_file;
304 #ifdef SPARSE_MAPPING
311 linker_kernel_file->address = (caddr_t) KERNBASE;
312 linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
316 baseptr = preload_search_info(modptr, MODINFO_ADDR);
318 linker_kernel_file->address = *(caddr_t *)baseptr;
319 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
321 linker_kernel_file->size = *(size_t *)sizeptr;
323 (void)link_elf_preload_parse_symbols(ef);
326 r_debug.r_map = NULL;
327 r_debug.r_brk = r_debug_state;
328 r_debug.r_state = RT_CONSISTENT;
331 (void)link_elf_link_common_finish(linker_kernel_file);
332 linker_kernel_file->flags |= LINKER_FILE_LINKED;
335 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_THIRD, link_elf_init, 0);
338 link_elf_preload_parse_symbols(elf_file_t ef)
341 caddr_t ssym, esym, base;
347 if (ef->modptr == NULL)
349 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM);
352 ssym = *(caddr_t *)pointer;
353 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM);
356 esym = *(caddr_t *)pointer;
360 symcnt = *(long *)base;
361 base += sizeof(long);
362 symtab = (Elf_Sym *)base;
363 base += roundup(symcnt, sizeof(long));
365 if (base > esym || base < ssym) {
366 printf("Symbols are corrupt!\n");
370 strcnt = *(long *)base;
371 base += sizeof(long);
373 base += roundup(strcnt, sizeof(long));
375 if (base > esym || base < ssym) {
376 printf("Symbols are corrupt!\n");
380 ef->ddbsymtab = symtab;
381 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
382 ef->ddbstrtab = strtab;
383 ef->ddbstrcnt = strcnt;
389 parse_dynamic(elf_file_t ef)
392 int plttype = DT_REL;
394 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
398 /* From src/libexec/rtld-elf/rtld.c */
399 const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
400 (ef->address + dp->d_un.d_ptr);
401 ef->nbuckets = hashtab[0];
402 ef->nchains = hashtab[1];
403 ef->buckets = hashtab + 2;
404 ef->chains = ef->buckets + ef->nbuckets;
408 ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
411 ef->strsz = dp->d_un.d_val;
414 ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
417 if (dp->d_un.d_val != sizeof(Elf_Sym))
421 ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
424 ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
427 ef->relsize = dp->d_un.d_val;
430 if (dp->d_un.d_val != sizeof(Elf_Rel))
434 ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
437 ef->pltrelsize = dp->d_un.d_val;
440 ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
443 ef->relasize = dp->d_un.d_val;
446 if (dp->d_un.d_val != sizeof(Elf_Rela))
450 plttype = dp->d_un.d_val;
451 if (plttype != DT_REL && plttype != DT_RELA)
456 dp->d_un.d_ptr = (Elf_Addr) &r_debug;
462 if (plttype == DT_RELA) {
463 ef->pltrela = (const Elf_Rela *) ef->pltrel;
465 ef->pltrelasize = ef->pltrelsize;
469 ef->ddbsymtab = ef->symtab;
470 ef->ddbsymcnt = ef->nchains;
471 ef->ddbstrtab = ef->strtab;
472 ef->ddbstrcnt = ef->strsz;
478 link_elf_link_preload(linker_class_t cls,
479 const char* filename, linker_file_t *result)
481 caddr_t modptr, baseptr, sizeptr, dynptr;
488 /* Look to see if we have the file preloaded */
489 modptr = preload_search_by_name(filename);
493 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
494 baseptr = preload_search_info(modptr, MODINFO_ADDR);
495 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
496 dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC);
498 (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " module") != 0 &&
499 strcmp(type, "elf module") != 0))
501 if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
504 lf = linker_make_file(filename, &link_elf_class);
509 ef = (elf_file_t) lf;
512 ef->address = *(caddr_t *)baseptr;
513 #ifdef SPARSE_MAPPING
516 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
517 ef->dynamic = (Elf_Dyn *)dp;
518 lf->address = ef->address;
519 lf->size = *(size_t *)sizeptr;
521 error = parse_dynamic(ef);
523 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
526 link_elf_reloc_local(lf);
532 link_elf_link_preload_finish(linker_file_t lf)
537 ef = (elf_file_t) lf;
538 #if 0 /* this will be more trouble than it's worth for now */
539 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
540 if (dp->d_tag != DT_NEEDED)
542 modname = ef->strtab + dp->d_un.d_val;
543 error = linker_load_module(modname, lf);
548 error = relocate_file(ef);
551 (void)link_elf_preload_parse_symbols(ef);
553 return (link_elf_link_common_finish(lf));
557 link_elf_load_file(linker_class_t cls, const char* filename,
558 linker_file_t* result)
561 struct thread* td = curthread; /* XXX */
567 Elf_Phdr *segs[MAXSEGS];
575 Elf_Addr base_vlimit;
590 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, filename, td);
592 error = vn_open(&nd, &flags, 0, NULL);
595 vfslocked = NDHASGIANT(&nd);
596 NDFREE(&nd, NDF_ONLY_PNBUF);
597 if (nd.ni_vp->v_type != VREG) {
603 error = mac_kld_check_load(curthread->td_ucred, nd.ni_vp);
611 * Read the elf header from the file.
613 firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
614 if (firstpage == NULL) {
618 hdr = (Elf_Ehdr *)firstpage;
619 error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
620 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
622 nbytes = PAGE_SIZE - resid;
631 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
632 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
633 link_elf_error(filename, "Unsupported file layout");
637 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
638 || hdr->e_version != EV_CURRENT) {
639 link_elf_error(filename, "Unsupported file version");
643 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
647 if (hdr->e_machine != ELF_TARG_MACH) {
648 link_elf_error(filename, "Unsupported machine");
654 * We rely on the program header being in the first page. This is
655 * not strictly required by the ABI specification, but it seems to
656 * always true in practice. And, it simplifies things considerably.
658 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
659 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
660 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
661 link_elf_error(filename, "Unreadable program headers");
664 * Scan the program header entries, and save key information.
666 * We rely on there being exactly two load segments, text and data,
669 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
670 phlimit = phdr + hdr->e_phnum;
674 while (phdr < phlimit) {
675 switch (phdr->p_type) {
678 if (nsegs == MAXSEGS) {
679 link_elf_error(filename, "Too many sections");
684 * XXX: We just trust they come in right order ??
706 link_elf_error(filename, "Object is not dynamically-linked");
711 link_elf_error(filename, "No sections");
717 * Allocate the entire address space of the object, to stake out our
718 * contiguous region, and to establish the base address for relocation.
720 base_offset = trunc_page(segs[0]->p_offset);
721 base_vaddr = trunc_page(segs[0]->p_vaddr);
722 base_vlimit = round_page(segs[nsegs - 1]->p_vaddr +
723 segs[nsegs - 1]->p_memsz);
724 mapsize = base_vlimit - base_vaddr;
726 lf = linker_make_file(filename, &link_elf_class);
732 ef = (elf_file_t) lf;
733 #ifdef SPARSE_MAPPING
734 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
735 if (ef->object == NULL) {
739 ef->address = (caddr_t) vm_map_min(kernel_map);
740 error = vm_map_find(kernel_map, ef->object, 0,
741 (vm_offset_t *) &ef->address,
743 VM_PROT_ALL, VM_PROT_ALL, 0);
745 vm_object_deallocate(ef->object);
750 ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
756 mapbase = ef->address;
759 * Read the text and data sections and zero the bss.
761 for (i = 0; i < nsegs; i++) {
762 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
763 error = vn_rdwr(UIO_READ, nd.ni_vp,
764 segbase, segs[i]->p_filesz, segs[i]->p_offset,
765 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
770 bzero(segbase + segs[i]->p_filesz,
771 segs[i]->p_memsz - segs[i]->p_filesz);
773 #ifdef SPARSE_MAPPING
775 * Wire down the pages
777 error = vm_map_wire(kernel_map,
778 (vm_offset_t) segbase,
779 (vm_offset_t) segbase + segs[i]->p_memsz,
780 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
781 if (error != KERN_SUCCESS) {
789 /* Update profiling information with the new text segment. */
791 kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr +
796 ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
798 lf->address = ef->address;
801 error = parse_dynamic(ef);
804 link_elf_reloc_local(lf);
806 VOP_UNLOCK(nd.ni_vp, 0);
807 error = linker_load_dependencies(lf);
808 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
811 #if 0 /* this will be more trouble than it's worth for now */
812 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
813 if (dp->d_tag != DT_NEEDED)
815 modname = ef->strtab + dp->d_un.d_val;
816 error = linker_load_module(modname, lf);
821 error = relocate_file(ef);
825 /* Try and load the symbol table if it's present. (you can strip it!) */
826 nbytes = hdr->e_shnum * hdr->e_shentsize;
827 if (nbytes == 0 || hdr->e_shoff == 0)
829 shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
834 error = vn_rdwr(UIO_READ, nd.ni_vp,
835 (caddr_t)shdr, nbytes, hdr->e_shoff,
836 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
842 for (i = 0; i < hdr->e_shnum; i++) {
843 if (shdr[i].sh_type == SHT_SYMTAB) {
845 symstrindex = shdr[i].sh_link;
848 if (symtabindex < 0 || symstrindex < 0)
851 symcnt = shdr[symtabindex].sh_size;
852 ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
853 strcnt = shdr[symstrindex].sh_size;
854 ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
856 if (ef->symbase == NULL || ef->strbase == NULL) {
860 error = vn_rdwr(UIO_READ, nd.ni_vp,
861 ef->symbase, symcnt, shdr[symtabindex].sh_offset,
862 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
866 error = vn_rdwr(UIO_READ, nd.ni_vp,
867 ef->strbase, strcnt, shdr[symstrindex].sh_offset,
868 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
873 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
874 ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
875 ef->ddbstrcnt = strcnt;
876 ef->ddbstrtab = ef->strbase;
878 error = link_elf_link_common_finish(lf);
888 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
890 free(shdr, M_LINKER);
892 free(firstpage, M_LINKER);
893 VOP_UNLOCK(nd.ni_vp, 0);
894 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
895 VFS_UNLOCK_GIANT(vfslocked);
901 link_elf_unload_file(linker_file_t file)
903 elf_file_t ef = (elf_file_t) file;
907 GDB_STATE(RT_DELETE);
908 free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
909 link_elf_delete_gdb(&ef->gdb);
910 GDB_STATE(RT_CONSISTENT);
914 /* Notify MD code that a module is being unloaded. */
915 elf_cpu_unload_file(file);
918 link_elf_unload_preload(file);
922 #ifdef SPARSE_MAPPING
924 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
925 (vm_offset_t) ef->address
926 + (ef->object->size << PAGE_SHIFT));
930 free(ef->address, M_LINKER);
933 free(ef->symbase, M_LINKER);
935 free(ef->strbase, M_LINKER);
937 free(ef->ctftab, M_LINKER);
939 free(ef->ctfoff, M_LINKER);
941 free(ef->typoff, M_LINKER);
945 link_elf_unload_preload(linker_file_t file)
948 preload_delete_name(file->filename);
952 symbol_name(elf_file_t ef, Elf_Size r_info)
956 if (ELF_R_SYM(r_info)) {
957 ref = ef->symtab + ELF_R_SYM(r_info);
958 return ef->strtab + ref->st_name;
964 relocate_file(elf_file_t ef)
966 const Elf_Rel *rellim;
968 const Elf_Rela *relalim;
969 const Elf_Rela *rela;
972 /* Perform relocations without addend if there are any: */
975 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
976 while (rel < rellim) {
977 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
979 symname = symbol_name(ef, rel->r_info);
980 printf("link_elf: symbol %s undefined\n", symname);
987 /* Perform relocations with addend if there are any: */
990 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
991 while (rela < relalim) {
992 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
994 symname = symbol_name(ef, rela->r_info);
995 printf("link_elf: symbol %s undefined\n", symname);
1002 /* Perform PLT relocations without addend if there are any: */
1005 rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
1006 while (rel < rellim) {
1007 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
1009 symname = symbol_name(ef, rel->r_info);
1010 printf("link_elf: symbol %s undefined\n", symname);
1017 /* Perform relocations with addend if there are any: */
1020 relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
1021 while (rela < relalim) {
1022 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
1024 symname = symbol_name(ef, rela->r_info);
1025 printf("link_elf: symbol %s undefined\n", symname);
1036 * Hash function for symbol table lookup. Don't even think about changing
1037 * this. It is specified by the System V ABI.
1039 static unsigned long
1040 elf_hash(const char *name)
1042 const unsigned char *p = (const unsigned char *) name;
1043 unsigned long h = 0;
1046 while (*p != '\0') {
1047 h = (h << 4) + *p++;
1048 if ((g = h & 0xf0000000) != 0)
1056 link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
1058 elf_file_t ef = (elf_file_t) lf;
1059 unsigned long symnum;
1060 const Elf_Sym* symp;
1065 /* If we don't have a hash, bail. */
1066 if (ef->buckets == NULL || ef->nbuckets == 0) {
1067 printf("link_elf_lookup_symbol: missing symbol hash table\n");
1071 /* First, search hashed global symbols */
1072 hash = elf_hash(name);
1073 symnum = ef->buckets[hash % ef->nbuckets];
1075 while (symnum != STN_UNDEF) {
1076 if (symnum >= ef->nchains) {
1077 printf("link_elf_lookup_symbol: corrupt symbol table\n");
1081 symp = ef->symtab + symnum;
1082 if (symp->st_name == 0) {
1083 printf("link_elf_lookup_symbol: corrupt symbol table\n");
1087 strp = ef->strtab + symp->st_name;
1089 if (strcmp(name, strp) == 0) {
1090 if (symp->st_shndx != SHN_UNDEF ||
1091 (symp->st_value != 0 &&
1092 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1093 *sym = (c_linker_sym_t) symp;
1099 symnum = ef->chains[symnum];
1102 /* If we have not found it, look at the full table (if loaded) */
1103 if (ef->symtab == ef->ddbsymtab)
1106 /* Exhaustive search */
1107 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1108 strp = ef->ddbstrtab + symp->st_name;
1109 if (strcmp(name, strp) == 0) {
1110 if (symp->st_shndx != SHN_UNDEF ||
1111 (symp->st_value != 0 &&
1112 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1113 *sym = (c_linker_sym_t) symp;
1124 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
1126 elf_file_t ef = (elf_file_t) lf;
1127 const Elf_Sym* es = (const Elf_Sym*) sym;
1129 if (es >= ef->symtab && es < (ef->symtab + ef->nchains)) {
1130 symval->name = ef->strtab + es->st_name;
1131 symval->value = (caddr_t) ef->address + es->st_value;
1132 symval->size = es->st_size;
1135 if (ef->symtab == ef->ddbsymtab)
1137 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1138 symval->name = ef->ddbstrtab + es->st_name;
1139 symval->value = (caddr_t) ef->address + es->st_value;
1140 symval->size = es->st_size;
1147 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1148 c_linker_sym_t* sym, long* diffp)
1150 elf_file_t ef = (elf_file_t) lf;
1151 u_long off = (uintptr_t) (void *) value;
1155 const Elf_Sym* best = 0;
1158 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1159 if (es->st_name == 0)
1161 st_value = es->st_value + (uintptr_t) (void *) ef->address;
1162 if (off >= st_value) {
1163 if (off - st_value < diff) {
1164 diff = off - st_value;
1168 } else if (off - st_value == diff) {
1177 *sym = (c_linker_sym_t) best;
1183 * Look up a linker set on an ELF system.
1186 link_elf_lookup_set(linker_file_t lf, const char *name,
1187 void ***startp, void ***stopp, int *countp)
1190 linker_symval_t symval;
1192 void **start, **stop;
1193 int len, error = 0, count;
1195 len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1196 setsym = malloc(len, M_LINKER, M_WAITOK);
1200 /* get address of first entry */
1201 snprintf(setsym, len, "%s%s", "__start_set_", name);
1202 error = link_elf_lookup_symbol(lf, setsym, &sym);
1205 link_elf_symbol_values(lf, sym, &symval);
1206 if (symval.value == 0) {
1210 start = (void **)symval.value;
1212 /* get address of last entry */
1213 snprintf(setsym, len, "%s%s", "__stop_set_", name);
1214 error = link_elf_lookup_symbol(lf, setsym, &sym);
1217 link_elf_symbol_values(lf, sym, &symval);
1218 if (symval.value == 0) {
1222 stop = (void **)symval.value;
1224 /* and the number of entries */
1225 count = stop - start;
1236 free(setsym, M_LINKER);
1241 link_elf_each_function_name(linker_file_t file,
1242 int (*callback)(const char *, void *), void *opaque) {
1243 elf_file_t ef = (elf_file_t)file;
1244 const Elf_Sym* symp;
1247 /* Exhaustive search */
1248 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1249 if (symp->st_value != 0 &&
1250 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1251 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1260 link_elf_each_function_nameval(linker_file_t file,
1261 linker_function_nameval_callback_t callback, void *opaque)
1263 linker_symval_t symval;
1264 elf_file_t ef = (elf_file_t)file;
1265 const Elf_Sym* symp;
1268 /* Exhaustive search */
1269 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1270 if (symp->st_value != 0 &&
1271 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1272 error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval);
1275 error = callback(file, i, &symval, opaque);
1285 * Each KLD has its own GP. The GP value for each load module is given by
1286 * DT_PLTGOT on ia64. We need GP to construct function descriptors, but
1287 * don't have direct access to the ELF file structure. The link_elf_get_gp()
1288 * function returns the GP given a pointer to a generic linker file struct.
1291 link_elf_get_gp(linker_file_t lf)
1293 elf_file_t ef = (elf_file_t)lf;
1294 return (Elf_Addr)ef->got;
1299 elf_get_sym(linker_file_t lf, Elf_Size symidx)
1301 elf_file_t ef = (elf_file_t)lf;
1303 if (symidx >= ef->nchains)
1305 return (ef->symtab + symidx);
1309 elf_get_symname(linker_file_t lf, Elf_Size symidx)
1311 elf_file_t ef = (elf_file_t)lf;
1314 if (symidx >= ef->nchains)
1316 sym = ef->symtab + symidx;
1317 return (ef->strtab + sym->st_name);
1321 * Symbol lookup function that can be used when the symbol index is known (ie
1322 * in relocations). It uses the symbol index instead of doing a fully fledged
1323 * hash table based lookup when such is valid. For example for local symbols.
1324 * This is not only more efficient, it's also more correct. It's not always
1325 * the case that the symbol can be found through the hash table.
1328 elf_lookup(linker_file_t lf, Elf_Size symidx, int deps)
1330 elf_file_t ef = (elf_file_t)lf;
1334 /* Don't even try to lookup the symbol if the index is bogus. */
1335 if (symidx >= ef->nchains)
1338 sym = ef->symtab + symidx;
1341 * Don't do a full lookup when the symbol is local. It may even
1342 * fail because it may not be found through the hash table.
1344 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1345 /* Force lookup failure when we have an insanity. */
1346 if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0)
1348 return ((Elf_Addr)ef->address + sym->st_value);
1352 * XXX we can avoid doing a hash table based lookup for global
1353 * symbols as well. This however is not always valid, so we'll
1354 * just do it the hard way for now. Performance tweaks can
1358 symbol = ef->strtab + sym->st_name;
1360 /* Force a lookup failure if the symbol name is bogus. */
1364 return ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1368 link_elf_reloc_local(linker_file_t lf)
1370 const Elf_Rel *rellim;
1372 const Elf_Rela *relalim;
1373 const Elf_Rela *rela;
1374 elf_file_t ef = (elf_file_t)lf;
1376 /* Perform relocations without addend if there are any: */
1377 if ((rel = ef->rel) != NULL) {
1378 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
1379 while (rel < rellim) {
1380 elf_reloc_local(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
1386 /* Perform relocations with addend if there are any: */
1387 if ((rela = ef->rela) != NULL) {
1388 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
1389 while (rela < relalim) {
1390 elf_reloc_local(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
1398 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
1400 elf_file_t ef = (elf_file_t)lf;
1402 *symtab = ef->ddbsymtab;
1404 if (*symtab == NULL)
1407 return (ef->ddbsymcnt);
1411 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
1413 elf_file_t ef = (elf_file_t)lf;
1415 *strtab = ef->ddbstrtab;
1417 if (*strtab == NULL)
1420 return (ef->ddbstrcnt);