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>
41 #include <sys/malloc.h>
42 #include <sys/mutex.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>
52 #include <vm/vm_param.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_kern.h>
56 #include <vm/vm_extern.h>
59 #include <vm/vm_map.h>
61 #include <sys/link_elf.h>
63 #include "linker_if.h"
67 typedef struct elf_file {
68 struct linker_file lf; /* Common fields */
69 int preloaded; /* Was file pre-loaded */
70 caddr_t address; /* Relocation address */
72 vm_object_t object; /* VM object to hold file pages */
74 Elf_Dyn* dynamic; /* Symbol table etc. */
75 Elf_Hashelt nbuckets; /* DT_HASH info */
77 const Elf_Hashelt* buckets;
78 const Elf_Hashelt* chains;
80 caddr_t strtab; /* DT_STRTAB */
81 int strsz; /* DT_STRSZ */
82 const Elf_Sym* symtab; /* DT_SYMTAB */
83 Elf_Addr* got; /* DT_PLTGOT */
84 const Elf_Rel* pltrel; /* DT_JMPREL */
85 int pltrelsize; /* DT_PLTRELSZ */
86 const Elf_Rela* pltrela; /* DT_JMPREL */
87 int pltrelasize; /* DT_PLTRELSZ */
88 const Elf_Rel* rel; /* DT_REL */
89 int relsize; /* DT_RELSZ */
90 const Elf_Rela* rela; /* DT_RELA */
91 int relasize; /* DT_RELASZ */
93 const Elf_Sym* ddbsymtab; /* The symbol table we are using */
94 long ddbsymcnt; /* Number of symbols */
95 caddr_t ddbstrtab; /* String table */
96 long ddbstrcnt; /* number of bytes in string table */
97 caddr_t symbase; /* malloc'ed symbold base */
98 caddr_t strbase; /* malloc'ed string base */
100 struct link_map gdb; /* hooks for gdb */
104 static int link_elf_link_common_finish(linker_file_t);
105 static int link_elf_link_preload(linker_class_t cls,
106 const char*, linker_file_t*);
107 static int link_elf_link_preload_finish(linker_file_t);
108 static int link_elf_load_file(linker_class_t, const char*, linker_file_t*);
109 static int link_elf_lookup_symbol(linker_file_t, const char*,
111 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t*);
112 static int link_elf_search_symbol(linker_file_t, caddr_t value,
113 c_linker_sym_t* sym, long* diffp);
115 static void link_elf_unload_file(linker_file_t);
116 static void link_elf_unload_preload(linker_file_t);
117 static int link_elf_lookup_set(linker_file_t, const char *,
118 void ***, void ***, int *);
119 static int link_elf_each_function_name(linker_file_t,
120 int (*)(const char *, void *),
122 static void link_elf_reloc_local(linker_file_t);
123 static Elf_Addr elf_lookup(linker_file_t lf, Elf_Word symidx, int deps);
125 static kobj_method_t link_elf_methods[] = {
126 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
127 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
128 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
129 KOBJMETHOD(linker_unload, link_elf_unload_file),
130 KOBJMETHOD(linker_load_file, link_elf_load_file),
131 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
132 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
133 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
134 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
138 static struct linker_class link_elf_class = {
139 #if ELF_TARG_CLASS == ELFCLASS32
144 link_elf_methods, sizeof(struct elf_file)
147 static int parse_dynamic(elf_file_t ef);
148 static int relocate_file(elf_file_t ef);
149 static int link_elf_preload_parse_symbols(elf_file_t ef);
152 static void r_debug_state(struct r_debug *dummy_one,
153 struct link_map *dummy_two);
156 * A list of loaded modules for GDB to use for loading symbols.
158 struct r_debug r_debug;
160 #define GDB_STATE(s) r_debug.r_state = s; r_debug_state(NULL, NULL);
163 * Function for the debugger to set a breakpoint on to gain control.
166 r_debug_state(struct r_debug *dummy_one __unused,
167 struct link_map *dummy_two __unused)
172 link_elf_add_gdb(struct link_map *l)
174 struct link_map *prev;
178 if (r_debug.r_map == NULL) {
183 /* Append to list. */
184 for (prev = r_debug.r_map; prev->l_next != NULL; prev = prev->l_next)
192 link_elf_delete_gdb(struct link_map *l)
194 if (l->l_prev == NULL) {
196 if ((r_debug.r_map = l->l_next) != NULL)
197 l->l_next->l_prev = NULL;
199 /* Remove any but first. */
200 if ((l->l_prev->l_next = l->l_next) != NULL)
201 l->l_next->l_prev = l->l_prev;
207 Elf_Addr link_elf_get_gp(linker_file_t);
211 * The kernel symbol table starts here.
213 extern struct _dynamic _DYNAMIC;
216 link_elf_error(const char *s)
218 printf("kldload: %s\n", s);
222 * Actions performed after linking/loading both the preloaded kernel and any
223 * modules; whether preloaded or dynamicly loaded.
226 link_elf_link_common_finish(linker_file_t lf)
229 elf_file_t ef = (elf_file_t)lf;
234 /* Notify MD code that a module is being loaded. */
235 error = elf_cpu_load_file(lf);
241 ef->gdb.l_addr = lf->address;
242 newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
243 strcpy(newfilename, lf->filename);
244 ef->gdb.l_name = newfilename;
245 ef->gdb.l_ld = ef->dynamic;
246 link_elf_add_gdb(&ef->gdb);
247 GDB_STATE(RT_CONSISTENT);
254 link_elf_init(void* arg)
257 caddr_t modptr, baseptr, sizeptr;
261 linker_add_class(&link_elf_class);
263 dp = (Elf_Dyn*) &_DYNAMIC;
265 modptr = preload_search_by_type("elf" __XSTRING(__ELF_WORD_SIZE) " kernel");
267 modptr = preload_search_by_type("elf kernel");
269 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
272 linker_kernel_file = linker_make_file(modname, &link_elf_class);
273 if (linker_kernel_file == NULL)
274 panic("link_elf_init: Can't create linker structures for kernel");
276 ef = (elf_file_t) linker_kernel_file;
279 #ifdef SPARSE_MAPPING
286 linker_kernel_file->address = (caddr_t) KERNBASE;
287 linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
291 baseptr = preload_search_info(modptr, MODINFO_ADDR);
293 linker_kernel_file->address = *(caddr_t *)baseptr;
294 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
296 linker_kernel_file->size = *(size_t *)sizeptr;
298 (void)link_elf_preload_parse_symbols(ef);
301 r_debug.r_map = NULL;
302 r_debug.r_brk = r_debug_state;
303 r_debug.r_state = RT_CONSISTENT;
306 (void)link_elf_link_common_finish(linker_kernel_file);
309 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
312 link_elf_preload_parse_symbols(elf_file_t ef)
315 caddr_t ssym, esym, base;
321 if (ef->modptr == NULL)
323 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM);
326 ssym = *(caddr_t *)pointer;
327 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM);
330 esym = *(caddr_t *)pointer;
334 symcnt = *(long *)base;
335 base += sizeof(long);
336 symtab = (Elf_Sym *)base;
337 base += roundup(symcnt, sizeof(long));
339 if (base > esym || base < ssym) {
340 printf("Symbols are corrupt!\n");
344 strcnt = *(long *)base;
345 base += sizeof(long);
347 base += roundup(strcnt, sizeof(long));
349 if (base > esym || base < ssym) {
350 printf("Symbols are corrupt!\n");
354 ef->ddbsymtab = symtab;
355 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
356 ef->ddbstrtab = strtab;
357 ef->ddbstrcnt = strcnt;
363 parse_dynamic(elf_file_t ef)
366 int plttype = DT_REL;
368 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
372 /* From src/libexec/rtld-elf/rtld.c */
373 const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
374 (ef->address + dp->d_un.d_ptr);
375 ef->nbuckets = hashtab[0];
376 ef->nchains = hashtab[1];
377 ef->buckets = hashtab + 2;
378 ef->chains = ef->buckets + ef->nbuckets;
382 ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
385 ef->strsz = dp->d_un.d_val;
388 ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
391 if (dp->d_un.d_val != sizeof(Elf_Sym))
395 ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
398 ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
401 ef->relsize = dp->d_un.d_val;
404 if (dp->d_un.d_val != sizeof(Elf_Rel))
408 ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
411 ef->pltrelsize = dp->d_un.d_val;
414 ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
417 ef->relasize = dp->d_un.d_val;
420 if (dp->d_un.d_val != sizeof(Elf_Rela))
424 plttype = dp->d_un.d_val;
425 if (plttype != DT_REL && plttype != DT_RELA)
430 dp->d_un.d_ptr = (Elf_Addr) &r_debug;
436 if (plttype == DT_RELA) {
437 ef->pltrela = (const Elf_Rela *) ef->pltrel;
439 ef->pltrelasize = ef->pltrelsize;
443 ef->ddbsymtab = ef->symtab;
444 ef->ddbsymcnt = ef->nchains;
445 ef->ddbstrtab = ef->strtab;
446 ef->ddbstrcnt = ef->strsz;
452 link_elf_link_preload(linker_class_t cls,
453 const char* filename, linker_file_t *result)
455 caddr_t modptr, baseptr, sizeptr, dynptr;
462 /* Look to see if we have the file preloaded */
463 modptr = preload_search_by_name(filename);
467 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
468 baseptr = preload_search_info(modptr, MODINFO_ADDR);
469 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
470 dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC);
472 (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " module") != 0 &&
473 strcmp(type, "elf module") != 0))
475 if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
478 lf = linker_make_file(filename, &link_elf_class);
483 ef = (elf_file_t) lf;
486 ef->address = *(caddr_t *)baseptr;
487 #ifdef SPARSE_MAPPING
490 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
491 ef->dynamic = (Elf_Dyn *)dp;
492 lf->address = ef->address;
493 lf->size = *(size_t *)sizeptr;
495 error = parse_dynamic(ef);
497 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
500 link_elf_reloc_local(lf);
506 link_elf_link_preload_finish(linker_file_t lf)
511 ef = (elf_file_t) lf;
512 #if 0 /* this will be more trouble than it's worth for now */
513 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
514 if (dp->d_tag != DT_NEEDED)
516 modname = ef->strtab + dp->d_un.d_val;
517 error = linker_load_module(modname, lf);
522 error = relocate_file(ef);
525 (void)link_elf_preload_parse_symbols(ef);
527 return (link_elf_link_common_finish(lf));
531 link_elf_load_file(linker_class_t cls, const char* filename,
532 linker_file_t* result)
535 struct thread* td = curthread; /* XXX */
541 Elf_Phdr *segs[MAXSEGS];
549 Elf_Addr base_vlimit;
565 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
567 error = vn_open(&nd, &flags, 0, -1);
570 NDFREE(&nd, NDF_ONLY_PNBUF);
572 error = mac_check_kld_load(curthread->td_ucred, nd.ni_vp);
580 * Read the elf header from the file.
582 firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
583 if (firstpage == NULL) {
587 hdr = (Elf_Ehdr *)firstpage;
588 error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
589 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
591 nbytes = PAGE_SIZE - resid;
600 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
601 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
602 link_elf_error("Unsupported file layout");
606 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
607 || hdr->e_version != EV_CURRENT) {
608 link_elf_error("Unsupported file version");
612 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
613 link_elf_error("Unsupported file type");
617 if (hdr->e_machine != ELF_TARG_MACH) {
618 link_elf_error("Unsupported machine");
624 * We rely on the program header being in the first page. This is
625 * not strictly required by the ABI specification, but it seems to
626 * always true in practice. And, it simplifies things considerably.
628 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
629 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
630 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
631 link_elf_error("Unreadable program headers");
634 * Scan the program header entries, and save key information.
636 * We rely on there being exactly two load segments, text and data,
639 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
640 phlimit = phdr + hdr->e_phnum;
644 while (phdr < phlimit) {
645 switch (phdr->p_type) {
648 if (nsegs == MAXSEGS) {
649 link_elf_error("Too many sections");
654 * XXX: We just trust they come in right order ??
669 link_elf_error("Unsupported file type");
677 link_elf_error("Object is not dynamically-linked");
682 link_elf_error("No sections");
688 * Allocate the entire address space of the object, to stake out our
689 * contiguous region, and to establish the base address for relocation.
691 base_offset = trunc_page(segs[0]->p_offset);
692 base_vaddr = trunc_page(segs[0]->p_vaddr);
693 base_vlimit = round_page(segs[nsegs - 1]->p_vaddr +
694 segs[nsegs - 1]->p_memsz);
695 mapsize = base_vlimit - base_vaddr;
697 lf = linker_make_file(filename, &link_elf_class);
703 ef = (elf_file_t) lf;
704 #ifdef SPARSE_MAPPING
705 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
706 if (ef->object == NULL) {
710 ef->address = (caddr_t) vm_map_min(kernel_map);
711 error = vm_map_find(kernel_map, ef->object, 0,
712 (vm_offset_t *) &ef->address,
714 VM_PROT_ALL, VM_PROT_ALL, 0);
716 vm_object_deallocate(ef->object);
721 ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
727 mapbase = ef->address;
730 * Read the text and data sections and zero the bss.
732 for (i = 0; i < nsegs; i++) {
733 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
734 error = vn_rdwr(UIO_READ, nd.ni_vp,
735 segbase, segs[i]->p_filesz, segs[i]->p_offset,
736 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
741 bzero(segbase + segs[i]->p_filesz,
742 segs[i]->p_memsz - segs[i]->p_filesz);
744 #ifdef SPARSE_MAPPING
746 * Wire down the pages
748 error = vm_map_wire(kernel_map,
749 (vm_offset_t) segbase,
750 (vm_offset_t) segbase + segs[i]->p_memsz,
751 VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
752 if (error != KERN_SUCCESS) {
760 /* Update profiling information with the new text segment. */
761 kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr +
765 ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
767 lf->address = ef->address;
770 error = parse_dynamic(ef);
773 link_elf_reloc_local(lf);
775 error = linker_load_dependencies(lf);
778 #if 0 /* this will be more trouble than it's worth for now */
779 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
780 if (dp->d_tag != DT_NEEDED)
782 modname = ef->strtab + dp->d_un.d_val;
783 error = linker_load_module(modname, lf);
788 error = relocate_file(ef);
792 /* Try and load the symbol table if it's present. (you can strip it!) */
793 nbytes = hdr->e_shnum * hdr->e_shentsize;
794 if (nbytes == 0 || hdr->e_shoff == 0)
796 shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
801 error = vn_rdwr(UIO_READ, nd.ni_vp,
802 (caddr_t)shdr, nbytes, hdr->e_shoff,
803 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
809 for (i = 0; i < hdr->e_shnum; i++) {
810 if (shdr[i].sh_type == SHT_SYMTAB) {
812 symstrindex = shdr[i].sh_link;
815 if (symtabindex < 0 || symstrindex < 0)
818 symcnt = shdr[symtabindex].sh_size;
819 ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
820 strcnt = shdr[symstrindex].sh_size;
821 ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
823 if (ef->symbase == NULL || ef->strbase == NULL) {
827 error = vn_rdwr(UIO_READ, nd.ni_vp,
828 ef->symbase, symcnt, shdr[symtabindex].sh_offset,
829 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
833 error = vn_rdwr(UIO_READ, nd.ni_vp,
834 ef->strbase, strcnt, shdr[symstrindex].sh_offset,
835 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
840 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
841 ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
842 ef->ddbstrcnt = strcnt;
843 ef->ddbstrtab = ef->strbase;
845 error = link_elf_link_common_finish(lf);
855 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
857 free(shdr, M_LINKER);
859 free(firstpage, M_LINKER);
860 VOP_UNLOCK(nd.ni_vp, 0, td);
861 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
867 link_elf_unload_file(linker_file_t file)
869 elf_file_t ef = (elf_file_t) file;
873 GDB_STATE(RT_DELETE);
874 free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
875 link_elf_delete_gdb(&ef->gdb);
876 GDB_STATE(RT_CONSISTENT);
880 /* Notify MD code that a module is being unloaded. */
881 elf_cpu_unload_file(file);
884 link_elf_unload_preload(file);
888 #ifdef SPARSE_MAPPING
890 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
891 (vm_offset_t) ef->address
892 + (ef->object->size << PAGE_SHIFT));
896 free(ef->address, M_LINKER);
899 free(ef->symbase, M_LINKER);
901 free(ef->strbase, M_LINKER);
905 link_elf_unload_preload(linker_file_t file)
908 preload_delete_name(file->filename);
912 symbol_name(elf_file_t ef, Elf_Word r_info)
916 if (ELF_R_SYM(r_info)) {
917 ref = ef->symtab + ELF_R_SYM(r_info);
918 return ef->strtab + ref->st_name;
924 relocate_file(elf_file_t ef)
926 const Elf_Rel *rellim;
928 const Elf_Rela *relalim;
929 const Elf_Rela *rela;
932 /* Perform relocations without addend if there are any: */
935 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
936 while (rel < rellim) {
937 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
939 symname = symbol_name(ef, rel->r_info);
940 printf("link_elf: symbol %s undefined\n", symname);
947 /* Perform relocations with addend if there are any: */
950 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
951 while (rela < relalim) {
952 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
954 symname = symbol_name(ef, rela->r_info);
955 printf("link_elf: symbol %s undefined\n", symname);
962 /* Perform PLT relocations without addend if there are any: */
965 rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
966 while (rel < rellim) {
967 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
969 symname = symbol_name(ef, rel->r_info);
970 printf("link_elf: symbol %s undefined\n", symname);
977 /* Perform relocations with addend if there are any: */
980 relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
981 while (rela < relalim) {
982 if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
984 symname = symbol_name(ef, rela->r_info);
985 printf("link_elf: symbol %s undefined\n", symname);
996 * Hash function for symbol table lookup. Don't even think about changing
997 * this. It is specified by the System V ABI.
1000 elf_hash(const char *name)
1002 const unsigned char *p = (const unsigned char *) name;
1003 unsigned long h = 0;
1006 while (*p != '\0') {
1007 h = (h << 4) + *p++;
1008 if ((g = h & 0xf0000000) != 0)
1016 link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
1018 elf_file_t ef = (elf_file_t) lf;
1019 unsigned long symnum;
1020 const Elf_Sym* symp;
1025 /* First, search hashed global symbols */
1026 hash = elf_hash(name);
1027 symnum = ef->buckets[hash % ef->nbuckets];
1029 while (symnum != STN_UNDEF) {
1030 if (symnum >= ef->nchains) {
1031 printf("link_elf_lookup_symbol: corrupt symbol table\n");
1035 symp = ef->symtab + symnum;
1036 if (symp->st_name == 0) {
1037 printf("link_elf_lookup_symbol: corrupt symbol table\n");
1041 strp = ef->strtab + symp->st_name;
1043 if (strcmp(name, strp) == 0) {
1044 if (symp->st_shndx != SHN_UNDEF ||
1045 (symp->st_value != 0 &&
1046 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1047 *sym = (c_linker_sym_t) symp;
1053 symnum = ef->chains[symnum];
1056 /* If we have not found it, look at the full table (if loaded) */
1057 if (ef->symtab == ef->ddbsymtab)
1060 /* Exhaustive search */
1061 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1062 strp = ef->ddbstrtab + symp->st_name;
1063 if (strcmp(name, strp) == 0) {
1064 if (symp->st_shndx != SHN_UNDEF ||
1065 (symp->st_value != 0 &&
1066 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1067 *sym = (c_linker_sym_t) symp;
1078 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
1080 elf_file_t ef = (elf_file_t) lf;
1081 const Elf_Sym* es = (const Elf_Sym*) sym;
1083 if (es >= ef->symtab && es < (ef->symtab + ef->nchains)) {
1084 symval->name = ef->strtab + es->st_name;
1085 symval->value = (caddr_t) ef->address + es->st_value;
1086 symval->size = es->st_size;
1089 if (ef->symtab == ef->ddbsymtab)
1091 if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1092 symval->name = ef->ddbstrtab + es->st_name;
1093 symval->value = (caddr_t) ef->address + es->st_value;
1094 symval->size = es->st_size;
1101 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1102 c_linker_sym_t* sym, long* diffp)
1104 elf_file_t ef = (elf_file_t) lf;
1105 u_long off = (uintptr_t) (void *) value;
1109 const Elf_Sym* best = 0;
1112 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1113 if (es->st_name == 0)
1115 st_value = es->st_value + (uintptr_t) (void *) ef->address;
1116 if (off >= st_value) {
1117 if (off - st_value < diff) {
1118 diff = off - st_value;
1122 } else if (off - st_value == diff) {
1131 *sym = (c_linker_sym_t) best;
1137 * Look up a linker set on an ELF system.
1140 link_elf_lookup_set(linker_file_t lf, const char *name,
1141 void ***startp, void ***stopp, int *countp)
1144 linker_symval_t symval;
1146 void **start, **stop;
1147 int len, error = 0, count;
1149 len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1150 setsym = malloc(len, M_LINKER, M_WAITOK);
1154 /* get address of first entry */
1155 snprintf(setsym, len, "%s%s", "__start_set_", name);
1156 error = link_elf_lookup_symbol(lf, setsym, &sym);
1159 link_elf_symbol_values(lf, sym, &symval);
1160 if (symval.value == 0) {
1164 start = (void **)symval.value;
1166 /* get address of last entry */
1167 snprintf(setsym, len, "%s%s", "__stop_set_", name);
1168 error = link_elf_lookup_symbol(lf, setsym, &sym);
1171 link_elf_symbol_values(lf, sym, &symval);
1172 if (symval.value == 0) {
1176 stop = (void **)symval.value;
1178 /* and the number of entries */
1179 count = stop - start;
1190 free(setsym, M_LINKER);
1195 link_elf_each_function_name(linker_file_t file,
1196 int (*callback)(const char *, void *), void *opaque) {
1197 elf_file_t ef = (elf_file_t)file;
1198 const Elf_Sym* symp;
1201 /* Exhaustive search */
1202 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1203 if (symp->st_value != 0 &&
1204 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1205 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1215 * Each KLD has its own GP. The GP value for each load module is given by
1216 * DT_PLTGOT on ia64. We need GP to construct function descriptors, but
1217 * don't have direct access to the ELF file structure. The link_elf_get_gp()
1218 * function returns the GP given a pointer to a generic linker file struct.
1221 link_elf_get_gp(linker_file_t lf)
1223 elf_file_t ef = (elf_file_t)lf;
1224 return (Elf_Addr)ef->got;
1229 elf_get_sym(linker_file_t lf, Elf_Word symidx)
1231 elf_file_t ef = (elf_file_t)lf;
1233 if (symidx >= ef->nchains)
1235 return (ef->symtab + symidx);
1239 elf_get_symname(linker_file_t lf, Elf_Word symidx)
1241 elf_file_t ef = (elf_file_t)lf;
1244 if (symidx >= ef->nchains)
1246 sym = ef->symtab + symidx;
1247 return (ef->strtab + sym->st_name);
1251 * Symbol lookup function that can be used when the symbol index is known (ie
1252 * in relocations). It uses the symbol index instead of doing a fully fledged
1253 * hash table based lookup when such is valid. For example for local symbols.
1254 * This is not only more efficient, it's also more correct. It's not always
1255 * the case that the symbol can be found through the hash table.
1258 elf_lookup(linker_file_t lf, Elf_Word symidx, int deps)
1260 elf_file_t ef = (elf_file_t)lf;
1264 /* Don't even try to lookup the symbol if the index is bogus. */
1265 if (symidx >= ef->nchains)
1268 sym = ef->symtab + symidx;
1271 * Don't do a full lookup when the symbol is local. It may even
1272 * fail because it may not be found through the hash table.
1274 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1275 /* Force lookup failure when we have an insanity. */
1276 if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0)
1278 return ((Elf_Addr)ef->address + sym->st_value);
1282 * XXX we can avoid doing a hash table based lookup for global
1283 * symbols as well. This however is not always valid, so we'll
1284 * just do it the hard way for now. Performance tweaks can
1288 symbol = ef->strtab + sym->st_name;
1290 /* Force a lookup failure if the symbol name is bogus. */
1294 return ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1298 link_elf_reloc_local(linker_file_t lf)
1300 const Elf_Rel *rellim;
1302 const Elf_Rela *relalim;
1303 const Elf_Rela *rela;
1304 elf_file_t ef = (elf_file_t)lf;
1306 /* Perform relocations without addend if there are any: */
1307 if ((rel = ef->rel) != NULL) {
1308 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
1309 while (rel < rellim) {
1310 elf_reloc_local(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
1316 /* Perform relocations with addend if there are any: */
1317 if ((rela = ef->rela) != NULL) {
1318 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
1319 while (rela < relalim) {
1320 elf_reloc_local(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,