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>
65 #include "linker_if.h"
69 typedef struct elf_file {
70 struct linker_file lf; /* Common fields */
71 int preloaded; /* Was file pre-loaded */
72 caddr_t address; /* Relocation address */
74 vm_object_t object; /* VM object to hold file pages */
76 Elf_Dyn* dynamic; /* Symbol table etc. */
77 Elf_Hashelt nbuckets; /* DT_HASH info */
79 const Elf_Hashelt* buckets;
80 const Elf_Hashelt* chains;
82 caddr_t strtab; /* DT_STRTAB */
83 int strsz; /* DT_STRSZ */
84 const Elf_Sym* symtab; /* DT_SYMTAB */
85 Elf_Addr* got; /* DT_PLTGOT */
86 const Elf_Rel* pltrel; /* DT_JMPREL */
87 int pltrelsize; /* DT_PLTRELSZ */
88 const Elf_Rela* pltrela; /* DT_JMPREL */
89 int pltrelasize; /* DT_PLTRELSZ */
90 const Elf_Rel* rel; /* DT_REL */
91 int relsize; /* DT_RELSZ */
92 const Elf_Rela* rela; /* DT_RELA */
93 int relasize; /* DT_RELASZ */
95 const Elf_Sym* ddbsymtab; /* The symbol table we are using */
96 long ddbsymcnt; /* Number of symbols */
97 caddr_t ddbstrtab; /* String table */
98 long ddbstrcnt; /* number of bytes in string table */
99 caddr_t symbase; /* malloc'ed symbold base */
100 caddr_t strbase; /* malloc'ed string base */
102 struct link_map gdb; /* hooks for gdb */
106 static int link_elf_link_common_finish(linker_file_t);
107 static int link_elf_link_preload(linker_class_t cls,
108 const char*, linker_file_t*);
109 static int link_elf_link_preload_finish(linker_file_t);
110 static int link_elf_load_file(linker_class_t, const char*, linker_file_t*);
111 static int link_elf_lookup_symbol(linker_file_t, const char*,
113 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t*);
114 static int link_elf_search_symbol(linker_file_t, caddr_t value,
115 c_linker_sym_t* sym, long* diffp);
117 static void link_elf_unload_file(linker_file_t);
118 static void link_elf_unload_preload(linker_file_t);
119 static int link_elf_lookup_set(linker_file_t, const char *,
120 void ***, void ***, int *);
121 static int link_elf_each_function_name(linker_file_t,
122 int (*)(const char *, void *),
124 static void link_elf_reloc_local(linker_file_t);
125 static Elf_Addr elf_lookup(linker_file_t lf, Elf_Size symidx, int deps);
127 static kobj_method_t link_elf_methods[] = {
128 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
129 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
130 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
131 KOBJMETHOD(linker_unload, link_elf_unload_file),
132 KOBJMETHOD(linker_load_file, link_elf_load_file),
133 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
134 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
135 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
136 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
140 static struct linker_class link_elf_class = {
141 #if ELF_TARG_CLASS == ELFCLASS32
146 link_elf_methods, sizeof(struct elf_file)
149 static int parse_dynamic(elf_file_t ef);
150 static int relocate_file(elf_file_t ef);
151 static int link_elf_preload_parse_symbols(elf_file_t ef);
154 static void r_debug_state(struct r_debug *dummy_one,
155 struct link_map *dummy_two);
158 * A list of loaded modules for GDB to use for loading symbols.
160 struct r_debug r_debug;
162 #define GDB_STATE(s) r_debug.r_state = s; r_debug_state(NULL, NULL);
165 * Function for the debugger to set a breakpoint on to gain control.
168 r_debug_state(struct r_debug *dummy_one __unused,
169 struct link_map *dummy_two __unused)
174 link_elf_add_gdb(struct link_map *l)
176 struct link_map *prev;
180 if (r_debug.r_map == NULL) {
185 /* Append to list. */
186 for (prev = r_debug.r_map; prev->l_next != NULL; prev = prev->l_next)
194 link_elf_delete_gdb(struct link_map *l)
196 if (l->l_prev == NULL) {
198 if ((r_debug.r_map = l->l_next) != NULL)
199 l->l_next->l_prev = NULL;
201 /* Remove any but first. */
202 if ((l->l_prev->l_next = l->l_next) != NULL)
203 l->l_next->l_prev = l->l_prev;
209 Elf_Addr link_elf_get_gp(linker_file_t);
213 * The kernel symbol table starts here.
215 extern struct _dynamic _DYNAMIC;
218 link_elf_error(const char *s)
220 printf("kldload: %s\n", s);
224 * Actions performed after linking/loading both the preloaded kernel and any
225 * modules; whether preloaded or dynamicly loaded.
228 link_elf_link_common_finish(linker_file_t lf)
231 elf_file_t ef = (elf_file_t)lf;
236 /* Notify MD code that a module is being loaded. */
237 error = elf_cpu_load_file(lf);
243 ef->gdb.l_addr = lf->address;
244 newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
245 strcpy(newfilename, lf->filename);
246 ef->gdb.l_name = newfilename;
247 ef->gdb.l_ld = ef->dynamic;
248 link_elf_add_gdb(&ef->gdb);
249 GDB_STATE(RT_CONSISTENT);
256 link_elf_init(void* arg)
259 caddr_t modptr, baseptr, sizeptr;
263 linker_add_class(&link_elf_class);
265 dp = (Elf_Dyn*) &_DYNAMIC;
267 modptr = preload_search_by_type("elf" __XSTRING(__ELF_WORD_SIZE) " kernel");
269 modptr = preload_search_by_type("elf kernel");
271 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
274 linker_kernel_file = linker_make_file(modname, &link_elf_class);
275 if (linker_kernel_file == NULL)
276 panic("link_elf_init: Can't create linker structures for kernel");
278 ef = (elf_file_t) linker_kernel_file;
281 #ifdef SPARSE_MAPPING
288 linker_kernel_file->address = (caddr_t) KERNBASE;
289 linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
293 baseptr = preload_search_info(modptr, MODINFO_ADDR);
295 linker_kernel_file->address = *(caddr_t *)baseptr;
296 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
298 linker_kernel_file->size = *(size_t *)sizeptr;
300 (void)link_elf_preload_parse_symbols(ef);
303 r_debug.r_map = NULL;
304 r_debug.r_brk = r_debug_state;
305 r_debug.r_state = RT_CONSISTENT;
308 (void)link_elf_link_common_finish(linker_kernel_file);
311 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_THIRD, link_elf_init, 0);
314 link_elf_preload_parse_symbols(elf_file_t ef)
317 caddr_t ssym, esym, base;
323 if (ef->modptr == NULL)
325 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM);
328 ssym = *(caddr_t *)pointer;
329 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM);
332 esym = *(caddr_t *)pointer;
336 symcnt = *(long *)base;
337 base += sizeof(long);
338 symtab = (Elf_Sym *)base;
339 base += roundup(symcnt, sizeof(long));
341 if (base > esym || base < ssym) {
342 printf("Symbols are corrupt!\n");
346 strcnt = *(long *)base;
347 base += sizeof(long);
349 base += roundup(strcnt, sizeof(long));
351 if (base > esym || base < ssym) {
352 printf("Symbols are corrupt!\n");
356 ef->ddbsymtab = symtab;
357 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
358 ef->ddbstrtab = strtab;
359 ef->ddbstrcnt = strcnt;
365 parse_dynamic(elf_file_t ef)
368 int plttype = DT_REL;
370 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
374 /* From src/libexec/rtld-elf/rtld.c */
375 const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
376 (ef->address + dp->d_un.d_ptr);
377 ef->nbuckets = hashtab[0];
378 ef->nchains = hashtab[1];
379 ef->buckets = hashtab + 2;
380 ef->chains = ef->buckets + ef->nbuckets;
384 ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
387 ef->strsz = dp->d_un.d_val;
390 ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
393 if (dp->d_un.d_val != sizeof(Elf_Sym))
397 ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
400 ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
403 ef->relsize = dp->d_un.d_val;
406 if (dp->d_un.d_val != sizeof(Elf_Rel))
410 ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
413 ef->pltrelsize = dp->d_un.d_val;
416 ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
419 ef->relasize = dp->d_un.d_val;
422 if (dp->d_un.d_val != sizeof(Elf_Rela))
426 plttype = dp->d_un.d_val;
427 if (plttype != DT_REL && plttype != DT_RELA)
432 dp->d_un.d_ptr = (Elf_Addr) &r_debug;
438 if (plttype == DT_RELA) {
439 ef->pltrela = (const Elf_Rela *) ef->pltrel;
441 ef->pltrelasize = ef->pltrelsize;
445 ef->ddbsymtab = ef->symtab;
446 ef->ddbsymcnt = ef->nchains;
447 ef->ddbstrtab = ef->strtab;
448 ef->ddbstrcnt = ef->strsz;
454 link_elf_link_preload(linker_class_t cls,
455 const char* filename, linker_file_t *result)
457 caddr_t modptr, baseptr, sizeptr, dynptr;
464 /* Look to see if we have the file preloaded */
465 modptr = preload_search_by_name(filename);
469 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
470 baseptr = preload_search_info(modptr, MODINFO_ADDR);
471 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
472 dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC);
474 (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " module") != 0 &&
475 strcmp(type, "elf module") != 0))
477 if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
480 lf = linker_make_file(filename, &link_elf_class);
485 ef = (elf_file_t) lf;
488 ef->address = *(caddr_t *)baseptr;
489 #ifdef SPARSE_MAPPING
492 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
493 ef->dynamic = (Elf_Dyn *)dp;
494 lf->address = ef->address;
495 lf->size = *(size_t *)sizeptr;
497 error = parse_dynamic(ef);
499 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
502 link_elf_reloc_local(lf);
508 link_elf_link_preload_finish(linker_file_t lf)
513 ef = (elf_file_t) lf;
514 #if 0 /* this will be more trouble than it's worth for now */
515 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
516 if (dp->d_tag != DT_NEEDED)
518 modname = ef->strtab + dp->d_un.d_val;
519 error = linker_load_module(modname, lf);
524 error = relocate_file(ef);
527 (void)link_elf_preload_parse_symbols(ef);
529 return (link_elf_link_common_finish(lf));
533 link_elf_load_file(linker_class_t cls, const char* filename,
534 linker_file_t* result)
537 struct thread* td = curthread; /* XXX */
543 Elf_Phdr *segs[MAXSEGS];
551 Elf_Addr base_vlimit;
566 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, filename, td);
568 error = vn_open(&nd, &flags, 0, -1);
571 vfslocked = NDHASGIANT(&nd);
572 NDFREE(&nd, NDF_ONLY_PNBUF);
574 error = mac_check_kld_load(curthread->td_ucred, nd.ni_vp);
582 * Read the elf header from the file.
584 firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
585 if (firstpage == NULL) {
589 hdr = (Elf_Ehdr *)firstpage;
590 error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
591 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
593 nbytes = PAGE_SIZE - resid;
602 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
603 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
604 link_elf_error("Unsupported file layout");
608 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
609 || hdr->e_version != EV_CURRENT) {
610 link_elf_error("Unsupported file version");
614 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
615 link_elf_error("Unsupported file type");
619 if (hdr->e_machine != ELF_TARG_MACH) {
620 link_elf_error("Unsupported machine");
626 * We rely on the program header being in the first page. This is
627 * not strictly required by the ABI specification, but it seems to
628 * always true in practice. And, it simplifies things considerably.
630 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
631 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
632 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
633 link_elf_error("Unreadable program headers");
636 * Scan the program header entries, and save key information.
638 * We rely on there being exactly two load segments, text and data,
641 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
642 phlimit = phdr + hdr->e_phnum;
646 while (phdr < phlimit) {
647 switch (phdr->p_type) {
650 if (nsegs == MAXSEGS) {
651 link_elf_error("Too many sections");
656 * XXX: We just trust they come in right order ??
671 link_elf_error("Unsupported file type");
679 link_elf_error("Object is not dynamically-linked");
684 link_elf_error("No sections");
690 * Allocate the entire address space of the object, to stake out our
691 * contiguous region, and to establish the base address for relocation.
693 base_offset = trunc_page(segs[0]->p_offset);
694 base_vaddr = trunc_page(segs[0]->p_vaddr);
695 base_vlimit = round_page(segs[nsegs - 1]->p_vaddr +
696 segs[nsegs - 1]->p_memsz);
697 mapsize = base_vlimit - base_vaddr;
699 lf = linker_make_file(filename, &link_elf_class);
705 ef = (elf_file_t) lf;
706 #ifdef SPARSE_MAPPING
707 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
708 if (ef->object == NULL) {
712 ef->address = (caddr_t) vm_map_min(kernel_map);
713 error = vm_map_find(kernel_map, ef->object, 0,
714 (vm_offset_t *) &ef->address,
716 VM_PROT_ALL, VM_PROT_ALL, 0);
718 vm_object_deallocate(ef->object);
723 ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
729 mapbase = ef->address;
732 * Read the text and data sections and zero the bss.
734 for (i = 0; i < nsegs; i++) {
735 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
736 error = vn_rdwr(UIO_READ, nd.ni_vp,
737 segbase, segs[i]->p_filesz, segs[i]->p_offset,
738 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
743 bzero(segbase + segs[i]->p_filesz,
744 segs[i]->p_memsz - segs[i]->p_filesz);
746 #ifdef SPARSE_MAPPING
748 * Wire down the pages
750 error = vm_map_wire(kernel_map,
751 (vm_offset_t) segbase,
752 (vm_offset_t) segbase + segs[i]->p_memsz,
753 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
754 if (error != KERN_SUCCESS) {
762 /* Update profiling information with the new text segment. */
764 kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr +
769 ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
771 lf->address = ef->address;
774 error = parse_dynamic(ef);
777 link_elf_reloc_local(lf);
779 error = linker_load_dependencies(lf);
782 #if 0 /* this will be more trouble than it's worth for now */
783 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
784 if (dp->d_tag != DT_NEEDED)
786 modname = ef->strtab + dp->d_un.d_val;
787 error = linker_load_module(modname, lf);
792 error = relocate_file(ef);
796 /* Try and load the symbol table if it's present. (you can strip it!) */
797 nbytes = hdr->e_shnum * hdr->e_shentsize;
798 if (nbytes == 0 || hdr->e_shoff == 0)
800 shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
805 error = vn_rdwr(UIO_READ, nd.ni_vp,
806 (caddr_t)shdr, nbytes, hdr->e_shoff,
807 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
813 for (i = 0; i < hdr->e_shnum; i++) {
814 if (shdr[i].sh_type == SHT_SYMTAB) {
816 symstrindex = shdr[i].sh_link;
819 if (symtabindex < 0 || symstrindex < 0)
822 symcnt = shdr[symtabindex].sh_size;
823 ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
824 strcnt = shdr[symstrindex].sh_size;
825 ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
827 if (ef->symbase == NULL || ef->strbase == NULL) {
831 error = vn_rdwr(UIO_READ, nd.ni_vp,
832 ef->symbase, symcnt, shdr[symtabindex].sh_offset,
833 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
837 error = vn_rdwr(UIO_READ, nd.ni_vp,
838 ef->strbase, strcnt, shdr[symstrindex].sh_offset,
839 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
844 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
845 ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
846 ef->ddbstrcnt = strcnt;
847 ef->ddbstrtab = ef->strbase;
849 error = link_elf_link_common_finish(lf);
859 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
861 free(shdr, M_LINKER);
863 free(firstpage, M_LINKER);
864 VOP_UNLOCK(nd.ni_vp, 0, td);
865 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
866 VFS_UNLOCK_GIANT(vfslocked);
872 link_elf_unload_file(linker_file_t file)
874 elf_file_t ef = (elf_file_t) file;
878 GDB_STATE(RT_DELETE);
879 free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
880 link_elf_delete_gdb(&ef->gdb);
881 GDB_STATE(RT_CONSISTENT);
885 /* Notify MD code that a module is being unloaded. */
886 elf_cpu_unload_file(file);
889 link_elf_unload_preload(file);
893 #ifdef SPARSE_MAPPING
895 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
896 (vm_offset_t) ef->address
897 + (ef->object->size << PAGE_SHIFT));
901 free(ef->address, M_LINKER);
904 free(ef->symbase, M_LINKER);
906 free(ef->strbase, M_LINKER);
910 link_elf_unload_preload(linker_file_t file)
913 preload_delete_name(file->filename);
917 symbol_name(elf_file_t ef, Elf_Size r_info)
921 if (ELF_R_SYM(r_info)) {
922 ref = ef->symtab + ELF_R_SYM(r_info);
923 return ef->strtab + ref->st_name;
929 relocate_file(elf_file_t ef)
931 const Elf_Rel *rellim;
933 const Elf_Rela *relalim;
934 const Elf_Rela *rela;
937 /* Perform relocations without addend if there are any: */
940 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
941 while (rel < rellim) {
942 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
944 symname = symbol_name(ef, rel->r_info);
945 printf("link_elf: symbol %s undefined\n", symname);
952 /* Perform relocations with addend if there are any: */
955 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
956 while (rela < relalim) {
957 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
959 symname = symbol_name(ef, rela->r_info);
960 printf("link_elf: symbol %s undefined\n", symname);
967 /* Perform PLT relocations without addend if there are any: */
970 rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
971 while (rel < rellim) {
972 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
974 symname = symbol_name(ef, rel->r_info);
975 printf("link_elf: symbol %s undefined\n", symname);
982 /* Perform relocations with addend if there are any: */
985 relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
986 while (rela < relalim) {
987 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
989 symname = symbol_name(ef, rela->r_info);
990 printf("link_elf: symbol %s undefined\n", symname);
1001 * Hash function for symbol table lookup. Don't even think about changing
1002 * this. It is specified by the System V ABI.
1004 static unsigned long
1005 elf_hash(const char *name)
1007 const unsigned char *p = (const unsigned char *) name;
1008 unsigned long h = 0;
1011 while (*p != '\0') {
1012 h = (h << 4) + *p++;
1013 if ((g = h & 0xf0000000) != 0)
1021 link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
1023 elf_file_t ef = (elf_file_t) lf;
1024 unsigned long symnum;
1025 const Elf_Sym* symp;
1030 /* If we don't have a hash, bail. */
1031 if (ef->buckets == NULL || ef->nbuckets == 0) {
1032 printf("link_elf_lookup_symbol: missing symbol hash table\n");
1036 /* First, search hashed global symbols */
1037 hash = elf_hash(name);
1038 symnum = ef->buckets[hash % ef->nbuckets];
1040 while (symnum != STN_UNDEF) {
1041 if (symnum >= ef->nchains) {
1042 printf("link_elf_lookup_symbol: corrupt symbol table\n");
1046 symp = ef->symtab + symnum;
1047 if (symp->st_name == 0) {
1048 printf("link_elf_lookup_symbol: corrupt symbol table\n");
1052 strp = ef->strtab + symp->st_name;
1054 if (strcmp(name, strp) == 0) {
1055 if (symp->st_shndx != SHN_UNDEF ||
1056 (symp->st_value != 0 &&
1057 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1058 *sym = (c_linker_sym_t) symp;
1064 symnum = ef->chains[symnum];
1067 /* If we have not found it, look at the full table (if loaded) */
1068 if (ef->symtab == ef->ddbsymtab)
1071 /* Exhaustive search */
1072 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1073 strp = ef->ddbstrtab + symp->st_name;
1074 if (strcmp(name, strp) == 0) {
1075 if (symp->st_shndx != SHN_UNDEF ||
1076 (symp->st_value != 0 &&
1077 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1078 *sym = (c_linker_sym_t) symp;
1089 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
1091 elf_file_t ef = (elf_file_t) lf;
1092 const Elf_Sym* es = (const Elf_Sym*) sym;
1094 if (es >= ef->symtab && es < (ef->symtab + ef->nchains)) {
1095 symval->name = ef->strtab + es->st_name;
1096 symval->value = (caddr_t) ef->address + es->st_value;
1097 symval->size = es->st_size;
1100 if (ef->symtab == ef->ddbsymtab)
1102 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1103 symval->name = ef->ddbstrtab + es->st_name;
1104 symval->value = (caddr_t) ef->address + es->st_value;
1105 symval->size = es->st_size;
1112 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1113 c_linker_sym_t* sym, long* diffp)
1115 elf_file_t ef = (elf_file_t) lf;
1116 u_long off = (uintptr_t) (void *) value;
1120 const Elf_Sym* best = 0;
1123 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1124 if (es->st_name == 0)
1126 st_value = es->st_value + (uintptr_t) (void *) ef->address;
1127 if (off >= st_value) {
1128 if (off - st_value < diff) {
1129 diff = off - st_value;
1133 } else if (off - st_value == diff) {
1142 *sym = (c_linker_sym_t) best;
1148 * Look up a linker set on an ELF system.
1151 link_elf_lookup_set(linker_file_t lf, const char *name,
1152 void ***startp, void ***stopp, int *countp)
1155 linker_symval_t symval;
1157 void **start, **stop;
1158 int len, error = 0, count;
1160 len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1161 setsym = malloc(len, M_LINKER, M_WAITOK);
1165 /* get address of first entry */
1166 snprintf(setsym, len, "%s%s", "__start_set_", name);
1167 error = link_elf_lookup_symbol(lf, setsym, &sym);
1170 link_elf_symbol_values(lf, sym, &symval);
1171 if (symval.value == 0) {
1175 start = (void **)symval.value;
1177 /* get address of last entry */
1178 snprintf(setsym, len, "%s%s", "__stop_set_", name);
1179 error = link_elf_lookup_symbol(lf, setsym, &sym);
1182 link_elf_symbol_values(lf, sym, &symval);
1183 if (symval.value == 0) {
1187 stop = (void **)symval.value;
1189 /* and the number of entries */
1190 count = stop - start;
1201 free(setsym, M_LINKER);
1206 link_elf_each_function_name(linker_file_t file,
1207 int (*callback)(const char *, void *), void *opaque) {
1208 elf_file_t ef = (elf_file_t)file;
1209 const Elf_Sym* symp;
1212 /* Exhaustive search */
1213 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1214 if (symp->st_value != 0 &&
1215 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1216 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1226 * Each KLD has its own GP. The GP value for each load module is given by
1227 * DT_PLTGOT on ia64. We need GP to construct function descriptors, but
1228 * don't have direct access to the ELF file structure. The link_elf_get_gp()
1229 * function returns the GP given a pointer to a generic linker file struct.
1232 link_elf_get_gp(linker_file_t lf)
1234 elf_file_t ef = (elf_file_t)lf;
1235 return (Elf_Addr)ef->got;
1240 elf_get_sym(linker_file_t lf, Elf_Size symidx)
1242 elf_file_t ef = (elf_file_t)lf;
1244 if (symidx >= ef->nchains)
1246 return (ef->symtab + symidx);
1250 elf_get_symname(linker_file_t lf, Elf_Size symidx)
1252 elf_file_t ef = (elf_file_t)lf;
1255 if (symidx >= ef->nchains)
1257 sym = ef->symtab + symidx;
1258 return (ef->strtab + sym->st_name);
1262 * Symbol lookup function that can be used when the symbol index is known (ie
1263 * in relocations). It uses the symbol index instead of doing a fully fledged
1264 * hash table based lookup when such is valid. For example for local symbols.
1265 * This is not only more efficient, it's also more correct. It's not always
1266 * the case that the symbol can be found through the hash table.
1269 elf_lookup(linker_file_t lf, Elf_Size symidx, int deps)
1271 elf_file_t ef = (elf_file_t)lf;
1275 /* Don't even try to lookup the symbol if the index is bogus. */
1276 if (symidx >= ef->nchains)
1279 sym = ef->symtab + symidx;
1282 * Don't do a full lookup when the symbol is local. It may even
1283 * fail because it may not be found through the hash table.
1285 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1286 /* Force lookup failure when we have an insanity. */
1287 if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0)
1289 return ((Elf_Addr)ef->address + sym->st_value);
1293 * XXX we can avoid doing a hash table based lookup for global
1294 * symbols as well. This however is not always valid, so we'll
1295 * just do it the hard way for now. Performance tweaks can
1299 symbol = ef->strtab + sym->st_name;
1301 /* Force a lookup failure if the symbol name is bogus. */
1305 return ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1309 link_elf_reloc_local(linker_file_t lf)
1311 const Elf_Rel *rellim;
1313 const Elf_Rela *relalim;
1314 const Elf_Rela *rela;
1315 elf_file_t ef = (elf_file_t)lf;
1317 /* Perform relocations without addend if there are any: */
1318 if ((rel = ef->rel) != NULL) {
1319 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
1320 while (rel < rellim) {
1321 elf_reloc_local(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
1327 /* Perform relocations with addend if there are any: */
1328 if ((rela = ef->rela) != NULL) {
1329 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
1330 while (rela < relalim) {
1331 elf_reloc_local(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,