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
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
38 #include <sys/namei.h>
39 #include <sys/fcntl.h>
40 #include <sys/vnode.h>
41 #include <sys/linker.h>
43 #include <machine/elf.h>
45 #include <machine/profile.h>
49 #include <vm/vm_param.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_kern.h>
53 #include <vm/vm_extern.h>
56 #include <vm/vm_map.h>
58 #include <sys/link_elf.h>
60 #include "linker_if.h"
62 typedef struct elf_file {
63 struct linker_file lf; /* Common fields */
64 int preloaded; /* Was file pre-loaded */
65 caddr_t address; /* Relocation address */
67 vm_object_t object; /* VM object to hold file pages */
69 Elf_Dyn* dynamic; /* Symbol table etc. */
70 Elf_Hashelt nbuckets; /* DT_HASH info */
72 const Elf_Hashelt* buckets;
73 const Elf_Hashelt* chains;
75 caddr_t strtab; /* DT_STRTAB */
76 int strsz; /* DT_STRSZ */
77 const Elf_Sym* symtab; /* DT_SYMTAB */
78 Elf_Addr* got; /* DT_PLTGOT */
79 const Elf_Rel* pltrel; /* DT_JMPREL */
80 int pltrelsize; /* DT_PLTRELSZ */
81 const Elf_Rela* pltrela; /* DT_JMPREL */
82 int pltrelasize; /* DT_PLTRELSZ */
83 const Elf_Rel* rel; /* DT_REL */
84 int relsize; /* DT_RELSZ */
85 const Elf_Rela* rela; /* DT_RELA */
86 int relasize; /* DT_RELASZ */
88 const Elf_Sym* ddbsymtab; /* The symbol table we are using */
89 long ddbsymcnt; /* Number of symbols */
90 caddr_t ddbstrtab; /* String table */
91 long ddbstrcnt; /* number of bytes in string table */
92 caddr_t symbase; /* malloc'ed symbold base */
93 caddr_t strbase; /* malloc'ed string base */
95 struct link_map gdb; /* hooks for gdb */
99 static int link_elf_link_common_finish(linker_file_t);
100 static int link_elf_link_preload(linker_class_t cls,
101 const char*, linker_file_t*);
102 static int link_elf_link_preload_finish(linker_file_t);
103 static int link_elf_load_file(linker_class_t, const char*, linker_file_t*);
104 static int link_elf_lookup_symbol(linker_file_t, const char*,
106 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t*);
107 static int link_elf_search_symbol(linker_file_t, caddr_t value,
108 c_linker_sym_t* sym, long* diffp);
110 static void link_elf_unload_file(linker_file_t);
111 static void link_elf_unload_preload(linker_file_t);
112 static int link_elf_lookup_set(linker_file_t, const char *,
113 void ***, void ***, int *);
114 static int link_elf_each_function_name(linker_file_t,
115 int (*)(const char *, void *),
118 static kobj_method_t link_elf_methods[] = {
119 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
120 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
121 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
122 KOBJMETHOD(linker_unload, link_elf_unload_file),
123 KOBJMETHOD(linker_load_file, link_elf_load_file),
124 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
125 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
126 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
127 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
131 static struct linker_class link_elf_class = {
132 #if ELF_TARG_CLASS == ELFCLASS32
137 link_elf_methods, sizeof(struct elf_file)
140 static int parse_dynamic(elf_file_t ef);
141 static int relocate_file(elf_file_t ef);
142 static int link_elf_preload_parse_symbols(elf_file_t ef);
145 static void r_debug_state(struct r_debug *dummy_one,
146 struct link_map *dummy_two);
149 * A list of loaded modules for GDB to use for loading symbols.
151 struct r_debug r_debug;
153 #define GDB_STATE(s) r_debug.r_state = s; r_debug_state(NULL, NULL);
156 * Function for the debugger to set a breakpoint on to gain control.
159 r_debug_state(struct r_debug *dummy_one __unused,
160 struct link_map *dummy_two __unused)
165 link_elf_add_gdb(struct link_map *l)
167 struct link_map *prev;
171 if (r_debug.r_map == NULL) {
176 /* Append to list. */
177 for (prev = r_debug.r_map; prev->l_next != NULL; prev = prev->l_next)
185 link_elf_delete_gdb(struct link_map *l)
187 if (l->l_prev == NULL) {
189 if ((r_debug.r_map = l->l_next) != NULL)
190 l->l_next->l_prev = NULL;
192 /* Remove any but first. */
193 if ((l->l_prev->l_next = l->l_next) != NULL)
194 l->l_next->l_prev = l->l_prev;
200 Elf_Addr link_elf_get_gp(linker_file_t);
204 * The kernel symbol table starts here.
206 extern struct _dynamic _DYNAMIC;
209 link_elf_error(const char *s)
211 printf("kldload: %s\n", s);
215 * Actions performed after linking/loading both the preloaded kernel and any
216 * modules; whether preloaded or dynamicly loaded.
219 link_elf_link_common_finish(linker_file_t lf)
222 elf_file_t ef = (elf_file_t)lf;
227 /* Notify MD code that a module is being loaded. */
228 error = elf_cpu_load_file(lf);
234 ef->gdb.l_addr = lf->address;
235 newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
236 strcpy(newfilename, lf->filename);
237 ef->gdb.l_name = newfilename;
238 ef->gdb.l_ld = ef->dynamic;
239 link_elf_add_gdb(&ef->gdb);
240 GDB_STATE(RT_CONSISTENT);
247 link_elf_init(void* arg)
250 caddr_t modptr, baseptr, sizeptr;
254 linker_add_class(&link_elf_class);
256 dp = (Elf_Dyn*) &_DYNAMIC;
258 modptr = preload_search_by_type("elf kernel");
260 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
263 linker_kernel_file = linker_make_file(modname, &link_elf_class);
264 if (linker_kernel_file == NULL)
265 panic("link_elf_init: Can't create linker structures for kernel");
267 ef = (elf_file_t) linker_kernel_file;
270 #ifdef SPARSE_MAPPING
277 linker_kernel_file->address = (caddr_t) KERNBASE;
278 linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
282 baseptr = preload_search_info(modptr, MODINFO_ADDR);
284 linker_kernel_file->address = *(caddr_t *)baseptr;
285 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
287 linker_kernel_file->size = *(size_t *)sizeptr;
289 (void)link_elf_preload_parse_symbols(ef);
292 r_debug.r_map = NULL;
293 r_debug.r_brk = r_debug_state;
294 r_debug.r_state = RT_CONSISTENT;
297 (void)link_elf_link_common_finish(linker_kernel_file);
300 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
303 link_elf_preload_parse_symbols(elf_file_t ef)
306 caddr_t ssym, esym, base;
312 if (ef->modptr == NULL)
314 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM);
317 ssym = *(caddr_t *)pointer;
318 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM);
321 esym = *(caddr_t *)pointer;
325 symcnt = *(long *)base;
326 base += sizeof(long);
327 symtab = (Elf_Sym *)base;
328 base += roundup(symcnt, sizeof(long));
330 if (base > esym || base < ssym) {
331 printf("Symbols are corrupt!\n");
335 strcnt = *(long *)base;
336 base += sizeof(long);
338 base += roundup(strcnt, sizeof(long));
340 if (base > esym || base < ssym) {
341 printf("Symbols are corrupt!\n");
345 ef->ddbsymtab = symtab;
346 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
347 ef->ddbstrtab = strtab;
348 ef->ddbstrcnt = strcnt;
354 parse_dynamic(elf_file_t ef)
357 int plttype = DT_REL;
359 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
363 /* From src/libexec/rtld-elf/rtld.c */
364 const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
365 (ef->address + dp->d_un.d_ptr);
366 ef->nbuckets = hashtab[0];
367 ef->nchains = hashtab[1];
368 ef->buckets = hashtab + 2;
369 ef->chains = ef->buckets + ef->nbuckets;
373 ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
376 ef->strsz = dp->d_un.d_val;
379 ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
382 if (dp->d_un.d_val != sizeof(Elf_Sym))
386 ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
389 ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
392 ef->relsize = dp->d_un.d_val;
395 if (dp->d_un.d_val != sizeof(Elf_Rel))
399 ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
402 ef->pltrelsize = dp->d_un.d_val;
405 ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
408 ef->relasize = dp->d_un.d_val;
411 if (dp->d_un.d_val != sizeof(Elf_Rela))
415 plttype = dp->d_un.d_val;
416 if (plttype != DT_REL && plttype != DT_RELA)
421 dp->d_un.d_ptr = (Elf_Addr) &r_debug;
427 if (plttype == DT_RELA) {
428 ef->pltrela = (const Elf_Rela *) ef->pltrel;
430 ef->pltrelasize = ef->pltrelsize;
434 ef->ddbsymtab = ef->symtab;
435 ef->ddbsymcnt = ef->nchains;
436 ef->ddbstrtab = ef->strtab;
437 ef->ddbstrcnt = ef->strsz;
443 link_elf_link_preload(linker_class_t cls,
444 const char* filename, linker_file_t *result)
446 caddr_t modptr, baseptr, sizeptr, dynptr;
453 /* Look to see if we have the file preloaded */
454 modptr = preload_search_by_name(filename);
458 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
459 baseptr = preload_search_info(modptr, MODINFO_ADDR);
460 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
461 dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC);
462 if (type == NULL || strcmp(type, "elf module") != 0)
464 if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
467 lf = linker_make_file(filename, &link_elf_class);
472 ef = (elf_file_t) lf;
475 ef->address = *(caddr_t *)baseptr;
476 #ifdef SPARSE_MAPPING
479 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
480 ef->dynamic = (Elf_Dyn *)dp;
481 lf->address = ef->address;
482 lf->size = *(size_t *)sizeptr;
484 error = parse_dynamic(ef);
486 linker_file_unload(lf);
494 link_elf_link_preload_finish(linker_file_t lf)
499 ef = (elf_file_t) lf;
500 #if 0 /* this will be more trouble than it's worth for now */
501 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
502 if (dp->d_tag != DT_NEEDED)
504 modname = ef->strtab + dp->d_un.d_val;
505 error = linker_load_module(modname, lf);
510 error = relocate_file(ef);
513 (void)link_elf_preload_parse_symbols(ef);
515 return (link_elf_link_common_finish(lf));
519 link_elf_load_file(linker_class_t cls, const char* filename,
520 linker_file_t* result)
523 struct thread* td = curthread; /* XXX */
537 Elf_Addr base_vlimit;
553 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
555 error = vn_open(&nd, &flags, 0);
558 NDFREE(&nd, NDF_ONLY_PNBUF);
561 * Read the elf header from the file.
563 firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
564 if (firstpage == NULL) {
568 hdr = (Elf_Ehdr *)firstpage;
569 error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
570 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
572 nbytes = PAGE_SIZE - resid;
581 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
582 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
583 link_elf_error("Unsupported file layout");
587 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
588 || hdr->e_version != EV_CURRENT) {
589 link_elf_error("Unsupported file version");
593 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
594 link_elf_error("Unsupported file type");
598 if (hdr->e_machine != ELF_TARG_MACH) {
599 link_elf_error("Unsupported machine");
605 * We rely on the program header being in the first page. This is
606 * not strictly required by the ABI specification, but it seems to
607 * always true in practice. And, it simplifies things considerably.
609 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
610 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
611 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
612 link_elf_error("Unreadable program headers");
615 * Scan the program header entries, and save key information.
617 * We rely on there being exactly two load segments, text and data,
620 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
621 phlimit = phdr + hdr->e_phnum;
625 while (phdr < phlimit) {
626 switch (phdr->p_type) {
630 link_elf_error("Too many sections");
647 link_elf_error("Unsupported file type");
655 link_elf_error("Object is not dynamically-linked");
661 * Allocate the entire address space of the object, to stake out our
662 * contiguous region, and to establish the base address for relocation.
664 base_offset = trunc_page(segs[0]->p_offset);
665 base_vaddr = trunc_page(segs[0]->p_vaddr);
666 base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
667 mapsize = base_vlimit - base_vaddr;
669 lf = linker_make_file(filename, &link_elf_class);
675 ef = (elf_file_t) lf;
676 #ifdef SPARSE_MAPPING
677 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
678 if (ef->object == NULL) {
682 vm_object_reference(ef->object);
683 ef->address = (caddr_t) vm_map_min(kernel_map);
684 error = vm_map_find(kernel_map, ef->object, 0,
685 (vm_offset_t *) &ef->address,
687 VM_PROT_ALL, VM_PROT_ALL, 0);
689 vm_object_deallocate(ef->object);
694 ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
700 mapbase = ef->address;
703 * Read the text and data sections and zero the bss.
705 for (i = 0; i < 2; i++) {
706 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
707 error = vn_rdwr(UIO_READ, nd.ni_vp,
708 segbase, segs[i]->p_filesz, segs[i]->p_offset,
709 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
714 bzero(segbase + segs[i]->p_filesz,
715 segs[i]->p_memsz - segs[i]->p_filesz);
717 #ifdef SPARSE_MAPPING
719 * Wire down the pages
721 vm_map_wire(kernel_map,
722 (vm_offset_t) segbase,
723 (vm_offset_t) segbase + segs[i]->p_memsz,
729 /* Update profiling information with the new text segment. */
730 kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr +
734 ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
736 lf->address = ef->address;
739 error = parse_dynamic(ef);
742 error = linker_load_dependencies(lf);
745 #if 0 /* this will be more trouble than it's worth for now */
746 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
747 if (dp->d_tag != DT_NEEDED)
749 modname = ef->strtab + dp->d_un.d_val;
750 error = linker_load_module(modname, lf);
755 error = relocate_file(ef);
759 /* Try and load the symbol table if it's present. (you can strip it!) */
760 nbytes = hdr->e_shnum * hdr->e_shentsize;
761 if (nbytes == 0 || hdr->e_shoff == 0)
763 shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
768 error = vn_rdwr(UIO_READ, nd.ni_vp,
769 (caddr_t)shdr, nbytes, hdr->e_shoff,
770 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
776 for (i = 0; i < hdr->e_shnum; i++) {
777 if (shdr[i].sh_type == SHT_SYMTAB) {
779 symstrindex = shdr[i].sh_link;
782 if (symtabindex < 0 || symstrindex < 0)
785 symcnt = shdr[symtabindex].sh_size;
786 ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
787 strcnt = shdr[symstrindex].sh_size;
788 ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
790 if (ef->symbase == NULL || ef->strbase == NULL) {
794 error = vn_rdwr(UIO_READ, nd.ni_vp,
795 ef->symbase, symcnt, shdr[symtabindex].sh_offset,
796 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
800 error = vn_rdwr(UIO_READ, nd.ni_vp,
801 ef->strbase, strcnt, shdr[symstrindex].sh_offset,
802 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
807 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
808 ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
809 ef->ddbstrcnt = strcnt;
810 ef->ddbstrtab = ef->strbase;
812 error = link_elf_link_common_finish(lf);
822 linker_file_unload(lf);
824 free(shdr, M_LINKER);
826 free(firstpage, M_LINKER);
827 VOP_UNLOCK(nd.ni_vp, 0, td);
828 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
834 link_elf_unload_file(linker_file_t file)
836 elf_file_t ef = (elf_file_t) file;
840 GDB_STATE(RT_DELETE);
841 free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
842 link_elf_delete_gdb(&ef->gdb);
843 GDB_STATE(RT_CONSISTENT);
847 /* Notify MD code that a module is being unloaded. */
848 elf_cpu_unload_file(file);
851 link_elf_unload_preload(file);
855 #ifdef SPARSE_MAPPING
857 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
858 (vm_offset_t) ef->address
859 + (ef->object->size << PAGE_SHIFT));
860 vm_object_deallocate(ef->object);
864 free(ef->address, M_LINKER);
867 free(ef->symbase, M_LINKER);
869 free(ef->strbase, M_LINKER);
873 link_elf_unload_preload(linker_file_t file)
876 preload_delete_name(file->filename);
880 symbol_name(elf_file_t ef, Elf_Word r_info)
884 if (ELF_R_SYM(r_info)) {
885 ref = ef->symtab + ELF_R_SYM(r_info);
886 return ef->strtab + ref->st_name;
892 relocate_file(elf_file_t ef)
894 const Elf_Rel *rellim;
896 const Elf_Rela *relalim;
897 const Elf_Rela *rela;
900 /* Perform relocations without addend if there are any: */
903 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
904 while (rel < rellim) {
905 if (elf_reloc(&ef->lf, rel, ELF_RELOC_REL)) {
906 symname = symbol_name(ef, rel->r_info);
907 printf("link_elf: symbol %s undefined\n", symname);
914 /* Perform relocations with addend if there are any: */
917 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
918 while (rela < relalim) {
919 if (elf_reloc(&ef->lf, rela, ELF_RELOC_RELA)) {
920 symname = symbol_name(ef, rela->r_info);
921 printf("link_elf: symbol %s undefined\n", symname);
928 /* Perform PLT relocations without addend if there are any: */
931 rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
932 while (rel < rellim) {
933 if (elf_reloc(&ef->lf, rel, ELF_RELOC_REL)) {
934 symname = symbol_name(ef, rel->r_info);
935 printf("link_elf: symbol %s undefined\n", symname);
942 /* Perform relocations with addend if there are any: */
945 relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
946 while (rela < relalim) {
947 if (elf_reloc(&ef->lf, rela, ELF_RELOC_RELA)) {
948 symname = symbol_name(ef, rela->r_info);
949 printf("link_elf: symbol %s undefined\n", symname);
960 * Hash function for symbol table lookup. Don't even think about changing
961 * this. It is specified by the System V ABI.
964 elf_hash(const char *name)
966 const unsigned char *p = (const unsigned char *) name;
972 if ((g = h & 0xf0000000) != 0)
980 link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
982 elf_file_t ef = (elf_file_t) lf;
983 unsigned long symnum;
989 /* First, search hashed global symbols */
990 hash = elf_hash(name);
991 symnum = ef->buckets[hash % ef->nbuckets];
993 while (symnum != STN_UNDEF) {
994 if (symnum >= ef->nchains) {
995 printf("link_elf_lookup_symbol: corrupt symbol table\n");
999 symp = ef->symtab + symnum;
1000 if (symp->st_name == 0) {
1001 printf("link_elf_lookup_symbol: corrupt symbol table\n");
1005 strp = ef->strtab + symp->st_name;
1007 if (strcmp(name, strp) == 0) {
1008 if (symp->st_shndx != SHN_UNDEF ||
1009 (symp->st_value != 0 &&
1010 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1011 *sym = (c_linker_sym_t) symp;
1017 symnum = ef->chains[symnum];
1020 /* If we have not found it, look at the full table (if loaded) */
1021 if (ef->symtab == ef->ddbsymtab)
1024 /* Exhaustive search */
1025 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1026 strp = ef->ddbstrtab + symp->st_name;
1027 if (strcmp(name, strp) == 0) {
1028 if (symp->st_shndx != SHN_UNDEF ||
1029 (symp->st_value != 0 &&
1030 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1031 *sym = (c_linker_sym_t) symp;
1042 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
1044 elf_file_t ef = (elf_file_t) lf;
1045 const Elf_Sym* es = (const Elf_Sym*) sym;
1047 if (es >= ef->symtab && es < (ef->symtab + ef->nchains)) {
1048 symval->name = ef->strtab + es->st_name;
1049 symval->value = (caddr_t) ef->address + es->st_value;
1050 symval->size = es->st_size;
1053 if (ef->symtab == ef->ddbsymtab)
1055 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1056 symval->name = ef->ddbstrtab + es->st_name;
1057 symval->value = (caddr_t) ef->address + es->st_value;
1058 symval->size = es->st_size;
1065 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1066 c_linker_sym_t* sym, long* diffp)
1068 elf_file_t ef = (elf_file_t) lf;
1069 u_long off = (uintptr_t) (void *) value;
1073 const Elf_Sym* best = 0;
1076 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1077 if (es->st_name == 0)
1079 st_value = es->st_value + (uintptr_t) (void *) ef->address;
1080 if (off >= st_value) {
1081 if (off - st_value < diff) {
1082 diff = off - st_value;
1086 } else if (off - st_value == diff) {
1095 *sym = (c_linker_sym_t) best;
1101 * Look up a linker set on an ELF system.
1104 link_elf_lookup_set(linker_file_t lf, const char *name,
1105 void ***startp, void ***stopp, int *countp)
1108 linker_symval_t symval;
1110 void **start, **stop;
1111 int len, error = 0, count;
1113 len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1114 setsym = malloc(len, M_LINKER, M_WAITOK);
1118 /* get address of first entry */
1119 snprintf(setsym, len, "%s%s", "__start_set_", name);
1120 error = link_elf_lookup_symbol(lf, setsym, &sym);
1123 link_elf_symbol_values(lf, sym, &symval);
1124 if (symval.value == 0) {
1128 start = (void **)symval.value;
1130 /* get address of last entry */
1131 snprintf(setsym, len, "%s%s", "__stop_set_", name);
1132 error = link_elf_lookup_symbol(lf, setsym, &sym);
1135 link_elf_symbol_values(lf, sym, &symval);
1136 if (symval.value == 0) {
1140 stop = (void **)symval.value;
1142 /* and the number of entries */
1143 count = stop - start;
1154 free(setsym, M_LINKER);
1159 link_elf_each_function_name(linker_file_t file,
1160 int (*callback)(const char *, void *), void *opaque) {
1161 elf_file_t ef = (elf_file_t)file;
1162 const Elf_Sym* symp;
1165 /* Exhaustive search */
1166 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1167 if (symp->st_value != 0 &&
1168 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1169 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1179 * Each KLD has its own GP. The GP value for each load module is given by
1180 * DT_PLTGOT on ia64. We need GP to construct function descriptors, but
1181 * don't have direct access to the ELF file structure. The link_elf_get_gp()
1182 * function returns the GP given a pointer to a generic linker file struct.
1185 link_elf_get_gp(linker_file_t lf)
1187 elf_file_t ef = (elf_file_t)lf;
1188 return (Elf_Addr)ef->got;
1193 elf_get_sym(linker_file_t lf, Elf_Word symidx)
1195 elf_file_t ef = (elf_file_t)lf;
1197 if (symidx >= ef->nchains)
1199 return (ef->symtab + symidx);
1203 elf_get_symname(linker_file_t lf, Elf_Word symidx)
1205 elf_file_t ef = (elf_file_t)lf;
1208 if (symidx >= ef->nchains)
1210 sym = ef->symtab + symidx;
1211 return (ef->strtab + sym->st_name);
1215 * Symbol lookup function that can be used when the symbol index is known (ie
1216 * in relocations). It uses the symbol index instead of doing a fully fledged
1217 * hash table based lookup when such is valid. For example for local symbols.
1218 * This is not only more efficient, it's also more correct. It's not always
1219 * the case that the symbol can be found through the hash table.
1222 elf_lookup(linker_file_t lf, Elf_Word symidx, int deps)
1224 elf_file_t ef = (elf_file_t)lf;
1228 /* Don't even try to lookup the symbol if the index is bogus. */
1229 if (symidx >= ef->nchains)
1232 sym = ef->symtab + symidx;
1235 * Don't do a full lookup when the symbol is local. It may even
1236 * fail because it may not be found through the hash table.
1238 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1239 /* Force lookup failure when we have an insanity. */
1240 if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0)
1242 return ((Elf_Addr)ef->address + sym->st_value);
1246 * XXX we can avoid doing a hash table based lookup for global
1247 * symbols as well. This however is not always valid, so we'll
1248 * just do it the hard way for now. Performance tweaks can
1252 symbol = ef->strtab + sym->st_name;
1254 /* Force a lookup failure if the symbol name is bogus. */
1258 return ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));