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>
63 #include "linker_if.h"
65 typedef struct elf_file {
66 struct linker_file lf; /* Common fields */
67 int preloaded; /* Was file pre-loaded */
68 caddr_t address; /* Relocation address */
70 vm_object_t object; /* VM object to hold file pages */
72 Elf_Dyn* dynamic; /* Symbol table etc. */
73 Elf_Hashelt nbuckets; /* DT_HASH info */
75 const Elf_Hashelt* buckets;
76 const Elf_Hashelt* chains;
78 caddr_t strtab; /* DT_STRTAB */
79 int strsz; /* DT_STRSZ */
80 const Elf_Sym* symtab; /* DT_SYMTAB */
81 Elf_Addr* got; /* DT_PLTGOT */
82 const Elf_Rel* pltrel; /* DT_JMPREL */
83 int pltrelsize; /* DT_PLTRELSZ */
84 const Elf_Rela* pltrela; /* DT_JMPREL */
85 int pltrelasize; /* DT_PLTRELSZ */
86 const Elf_Rel* rel; /* DT_REL */
87 int relsize; /* DT_RELSZ */
88 const Elf_Rela* rela; /* DT_RELA */
89 int relasize; /* DT_RELASZ */
91 const Elf_Sym* ddbsymtab; /* The symbol table we are using */
92 long ddbsymcnt; /* Number of symbols */
93 caddr_t ddbstrtab; /* String table */
94 long ddbstrcnt; /* number of bytes in string table */
95 caddr_t symbase; /* malloc'ed symbold base */
96 caddr_t strbase; /* malloc'ed string base */
98 struct link_map gdb; /* hooks for gdb */
102 static int link_elf_link_preload(linker_class_t cls,
103 const char*, linker_file_t*);
104 static int link_elf_link_preload_finish(linker_file_t);
105 static int link_elf_load_file(linker_class_t, const char*, linker_file_t*);
106 static int link_elf_lookup_symbol(linker_file_t, const char*,
108 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t*);
109 static int link_elf_search_symbol(linker_file_t, caddr_t value,
110 c_linker_sym_t* sym, long* diffp);
112 static void link_elf_unload_file(linker_file_t);
113 static void link_elf_unload_preload(linker_file_t);
114 static int link_elf_lookup_set(linker_file_t, const char *,
115 void ***, void ***, int *);
116 static int link_elf_each_function_name(linker_file_t,
117 int (*)(const char *, void *),
120 static kobj_method_t link_elf_methods[] = {
121 KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
122 KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
123 KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
124 KOBJMETHOD(linker_unload, link_elf_unload_file),
125 KOBJMETHOD(linker_load_file, link_elf_load_file),
126 KOBJMETHOD(linker_link_preload, link_elf_link_preload),
127 KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
128 KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
129 KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
133 static struct linker_class link_elf_class = {
134 #if ELF_TARG_CLASS == ELFCLASS32
139 link_elf_methods, sizeof(struct elf_file)
142 static int parse_dynamic(elf_file_t ef);
143 static int relocate_file(elf_file_t ef);
144 static int link_elf_preload_parse_symbols(elf_file_t ef);
147 static void r_debug_state(struct r_debug *dummy_one,
148 struct link_map *dummy_two);
151 * A list of loaded modules for GDB to use for loading symbols.
153 struct r_debug r_debug;
155 #define GDB_STATE(s) r_debug.r_state = s; r_debug_state(NULL, NULL);
158 * Function for the debugger to set a breakpoint on to gain control.
161 r_debug_state(struct r_debug *dummy_one __unused,
162 struct link_map *dummy_two __unused)
169 * The kernel symbol table starts here.
171 extern struct _dynamic _DYNAMIC;
174 link_elf_init(void* arg)
178 caddr_t modptr, baseptr, sizeptr;
186 linker_add_class(&link_elf_class);
189 dp = (Elf_Dyn*) &_DYNAMIC;
191 modptr = preload_search_by_type("elf kernel");
193 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
196 linker_kernel_file = linker_make_file(modname, &link_elf_class);
197 if (linker_kernel_file == NULL)
198 panic("link_elf_init: Can't create linker structures for kernel");
200 ef = (elf_file_t) linker_kernel_file;
203 #ifdef SPARSE_MAPPING
210 linker_kernel_file->address = (caddr_t) KERNBASE;
211 linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
215 baseptr = preload_search_info(modptr, MODINFO_ADDR);
217 linker_kernel_file->address = *(caddr_t *)baseptr;
218 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
220 linker_kernel_file->size = *(size_t *)sizeptr;
222 (void)link_elf_preload_parse_symbols(ef);
225 ef->gdb.l_addr = linker_kernel_file->address;
226 newfilename = malloc(strlen(modname) + 1, M_LINKER, M_WAITOK);
227 strcpy(newfilename, modname);
228 ef->gdb.l_name = newfilename;
233 r_debug.r_map = &ef->gdb;
234 r_debug.r_brk = r_debug_state;
235 r_debug.r_state = RT_CONSISTENT;
237 r_debug_state(NULL, NULL); /* say hello to gdb! */
243 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
246 link_elf_preload_parse_symbols(elf_file_t ef)
249 caddr_t ssym, esym, base;
255 if (ef->modptr == NULL)
257 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM);
260 ssym = *(caddr_t *)pointer;
261 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM);
264 esym = *(caddr_t *)pointer;
268 symcnt = *(long *)base;
269 base += sizeof(long);
270 symtab = (Elf_Sym *)base;
271 base += roundup(symcnt, sizeof(long));
273 if (base > esym || base < ssym) {
274 printf("Symbols are corrupt!\n");
278 strcnt = *(long *)base;
279 base += sizeof(long);
281 base += roundup(strcnt, sizeof(long));
283 if (base > esym || base < ssym) {
284 printf("Symbols are corrupt!\n");
288 ef->ddbsymtab = symtab;
289 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
290 ef->ddbstrtab = strtab;
291 ef->ddbstrcnt = strcnt;
297 parse_dynamic(elf_file_t ef)
300 int plttype = DT_REL;
302 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
306 /* From src/libexec/rtld-elf/rtld.c */
307 const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
308 (ef->address + dp->d_un.d_ptr);
309 ef->nbuckets = hashtab[0];
310 ef->nchains = hashtab[1];
311 ef->buckets = hashtab + 2;
312 ef->chains = ef->buckets + ef->nbuckets;
316 ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
319 ef->strsz = dp->d_un.d_val;
322 ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
325 if (dp->d_un.d_val != sizeof(Elf_Sym))
329 ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
332 ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
335 ef->relsize = dp->d_un.d_val;
338 if (dp->d_un.d_val != sizeof(Elf_Rel))
342 ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
345 ef->pltrelsize = dp->d_un.d_val;
348 ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
351 ef->relasize = dp->d_un.d_val;
354 if (dp->d_un.d_val != sizeof(Elf_Rela))
358 plttype = dp->d_un.d_val;
359 if (plttype != DT_REL && plttype != DT_RELA)
364 dp->d_un.d_ptr = (Elf_Addr) &r_debug;
370 if (plttype == DT_RELA) {
371 ef->pltrela = (const Elf_Rela *) ef->pltrel;
373 ef->pltrelasize = ef->pltrelsize;
377 ef->ddbsymtab = ef->symtab;
378 ef->ddbsymcnt = ef->nchains;
379 ef->ddbstrtab = ef->strtab;
380 ef->ddbstrcnt = ef->strsz;
386 link_elf_error(const char *s)
388 printf("kldload: %s\n", s);
394 link_elf_add_gdb(struct link_map *l)
396 struct link_map *prev;
399 * Scan to the end of the list.
401 for (prev = r_debug.r_map; prev->l_next != NULL; prev = prev->l_next)
404 /* Link in the new entry. */
406 l->l_next = prev->l_next;
411 link_elf_delete_gdb(struct link_map *l)
413 if (l->l_prev == NULL) {
414 if ((r_debug.r_map = l->l_next) != NULL)
415 l->l_next->l_prev = NULL;
419 if ((l->l_prev->l_next = l->l_next) != NULL)
420 l->l_next->l_prev = l->l_prev;
426 link_elf_link_preload(linker_class_t cls,
427 const char* filename, linker_file_t *result)
429 caddr_t modptr, baseptr, sizeptr, dynptr;
436 /* Look to see if we have the file preloaded */
437 modptr = preload_search_by_name(filename);
441 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
442 baseptr = preload_search_info(modptr, MODINFO_ADDR);
443 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
444 dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC);
445 if (type == NULL || strcmp(type, "elf module") != 0)
447 if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
450 lf = linker_make_file(filename, &link_elf_class);
455 ef = (elf_file_t) lf;
458 ef->address = *(caddr_t *)baseptr;
459 #ifdef SPARSE_MAPPING
462 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
463 ef->dynamic = (Elf_Dyn *)dp;
464 lf->address = ef->address;
465 lf->size = *(size_t *)sizeptr;
467 error = parse_dynamic(ef);
469 linker_file_unload(lf);
477 link_elf_link_preload_finish(linker_file_t lf)
485 ef = (elf_file_t) lf;
486 #if 0 /* this will be more trouble than it's worth for now */
487 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
488 if (dp->d_tag != DT_NEEDED)
490 modname = ef->strtab + dp->d_un.d_val;
491 error = linker_load_module(modname, lf);
496 error = relocate_file(ef);
499 (void)link_elf_preload_parse_symbols(ef);
503 ef->gdb.l_addr = lf->address;
504 newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
505 strcpy(newfilename, lf->filename);
506 ef->gdb.l_name = newfilename;
507 ef->gdb.l_ld = ef->dynamic;
508 link_elf_add_gdb(&ef->gdb);
509 GDB_STATE(RT_CONSISTENT);
516 link_elf_load_file(linker_class_t cls, const char* filename, linker_file_t* result)
519 struct thread* td = curthread; /* XXX */
533 Elf_Addr base_vlimit;
552 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
554 error = vn_open(&nd, &flags, 0);
557 NDFREE(&nd, NDF_ONLY_PNBUF);
560 * Read the elf header from the file.
562 firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
563 if (firstpage == NULL) {
567 hdr = (Elf_Ehdr *)firstpage;
568 error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
569 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, &resid, td);
570 nbytes = PAGE_SIZE - resid;
579 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
580 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
581 link_elf_error("Unsupported file layout");
585 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
586 || hdr->e_version != EV_CURRENT) {
587 link_elf_error("Unsupported file version");
591 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
592 link_elf_error("Unsupported file type");
596 if (hdr->e_machine != ELF_TARG_MACH) {
597 link_elf_error("Unsupported machine");
603 * We rely on the program header being in the first page. This is
604 * not strictly required by the ABI specification, but it seems to
605 * always true in practice. And, it simplifies things considerably.
607 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
608 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
609 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
610 link_elf_error("Unreadable program headers");
613 * Scan the program header entries, and save key information.
615 * We rely on there being exactly two load segments, text and data,
618 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
619 phlimit = phdr + hdr->e_phnum;
623 while (phdr < phlimit) {
624 switch (phdr->p_type) {
628 link_elf_error("Too many sections");
645 link_elf_error("Unsupported file type");
653 link_elf_error("Object is not dynamically-linked");
659 * Allocate the entire address space of the object, to stake out our
660 * contiguous region, and to establish the base address for relocation.
662 base_offset = trunc_page(segs[0]->p_offset);
663 base_vaddr = trunc_page(segs[0]->p_vaddr);
664 base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
665 mapsize = base_vlimit - base_vaddr;
667 lf = linker_make_file(filename, &link_elf_class);
673 ef = (elf_file_t) lf;
674 #ifdef SPARSE_MAPPING
675 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
676 if (ef->object == NULL) {
681 vm_object_reference(ef->object);
682 ef->address = (caddr_t) vm_map_min(kernel_map);
683 error = vm_map_find(kernel_map, ef->object, 0,
684 (vm_offset_t *) &ef->address,
686 VM_PROT_ALL, VM_PROT_ALL, 0);
688 vm_object_deallocate(ef->object);
693 ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
699 mapbase = ef->address;
702 * Read the text and data sections and zero the bss.
704 for (i = 0; i < 2; i++) {
705 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
706 error = vn_rdwr(UIO_READ, nd.ni_vp,
707 segbase, segs[i]->p_filesz, segs[i]->p_offset,
708 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, &resid, td);
712 bzero(segbase + segs[i]->p_filesz,
713 segs[i]->p_memsz - segs[i]->p_filesz);
715 #ifdef SPARSE_MAPPING
717 * Wire down the pages
719 vm_map_pageable(kernel_map,
720 (vm_offset_t) segbase,
721 (vm_offset_t) segbase + segs[i]->p_memsz,
727 /* Update profiling information with the new text segment. */
728 kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr +
732 ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
734 lf->address = ef->address;
737 error = parse_dynamic(ef);
740 error = linker_load_dependencies(lf);
743 #if 0 /* this will be more trouble than it's worth for now */
744 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
745 if (dp->d_tag != DT_NEEDED)
747 modname = ef->strtab + dp->d_un.d_val;
748 error = linker_load_module(modname, lf);
753 error = relocate_file(ef);
757 /* Try and load the symbol table if it's present. (you can strip it!) */
758 nbytes = hdr->e_shnum * hdr->e_shentsize;
759 if (nbytes == 0 || hdr->e_shoff == 0)
761 shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
766 error = vn_rdwr(UIO_READ, nd.ni_vp,
767 (caddr_t)shdr, nbytes, hdr->e_shoff,
768 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, &resid, td);
773 for (i = 0; i < hdr->e_shnum; i++) {
774 if (shdr[i].sh_type == SHT_SYMTAB) {
776 symstrindex = shdr[i].sh_link;
779 if (symtabindex < 0 || symstrindex < 0)
782 symcnt = shdr[symtabindex].sh_size;
783 ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
784 strcnt = shdr[symstrindex].sh_size;
785 ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
787 if (ef->symbase == NULL || ef->strbase == NULL) {
791 error = vn_rdwr(UIO_READ, nd.ni_vp,
792 ef->symbase, symcnt, shdr[symtabindex].sh_offset,
793 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, &resid, td);
796 error = vn_rdwr(UIO_READ, nd.ni_vp,
797 ef->strbase, strcnt, shdr[symstrindex].sh_offset,
798 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, &resid, td);
802 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
803 ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
804 ef->ddbstrcnt = strcnt;
805 ef->ddbstrtab = ef->strbase;
809 ef->gdb.l_addr = lf->address;
810 newfilename = malloc(strlen(filename) + 1, M_LINKER, M_WAITOK);
811 strcpy(newfilename, filename);
812 ef->gdb.l_name = (const char *)newfilename;
813 ef->gdb.l_ld = ef->dynamic;
814 link_elf_add_gdb(&ef->gdb);
815 GDB_STATE(RT_CONSISTENT);
824 linker_file_unload(lf);
826 free(shdr, M_LINKER);
828 free(firstpage, M_LINKER);
829 VOP_UNLOCK(nd.ni_vp, 0, td);
830 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
836 link_elf_unload_file(linker_file_t file)
838 elf_file_t ef = (elf_file_t) file;
842 GDB_STATE(RT_DELETE);
843 free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
844 link_elf_delete_gdb(&ef->gdb);
845 GDB_STATE(RT_CONSISTENT);
850 link_elf_unload_preload(file);
853 #ifdef SPARSE_MAPPING
855 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
856 (vm_offset_t) ef->address
857 + (ef->object->size << PAGE_SHIFT));
858 vm_object_deallocate(ef->object);
862 free(ef->address, M_LINKER);
865 free(ef->symbase, M_LINKER);
867 free(ef->strbase, M_LINKER);
871 link_elf_unload_preload(linker_file_t file)
874 preload_delete_name(file->filename);
878 symbol_name(elf_file_t ef, Elf_Word r_info)
882 if (ELF_R_SYM(r_info)) {
883 ref = ef->symtab + ELF_R_SYM(r_info);
884 return ef->strtab + ref->st_name;
890 relocate_file(elf_file_t ef)
892 const Elf_Rel *rellim;
894 const Elf_Rela *relalim;
895 const Elf_Rela *rela;
898 /* Perform relocations without addend if there are any: */
901 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
902 while (rel < rellim) {
903 symname = symbol_name(ef, rel->r_info);
904 if (elf_reloc(&ef->lf, rel, ELF_RELOC_REL, symname)) {
905 printf("link_elf: symbol %s undefined\n", symname);
912 /* Perform relocations with addend if there are any: */
915 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
916 while (rela < relalim) {
917 symname = symbol_name(ef, rela->r_info);
918 if (elf_reloc(&ef->lf, rela, ELF_RELOC_RELA, symname)) {
919 printf("link_elf: symbol %s undefined\n", symname);
926 /* Perform PLT relocations without addend if there are any: */
929 rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
930 while (rel < rellim) {
931 symname = symbol_name(ef, rel->r_info);
932 if (elf_reloc(&ef->lf, rel, ELF_RELOC_REL, symname)) {
933 printf("link_elf: symbol %s undefined\n", symname);
940 /* Perform relocations with addend if there are any: */
943 relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
944 while (rela < relalim) {
945 symname = symbol_name(ef, rela->r_info);
946 if (elf_reloc(&ef->lf, rela, ELF_RELOC_RELA, symname)) {
947 printf("link_elf: symbol %s undefined\n", symname);
958 * Hash function for symbol table lookup. Don't even think about changing
959 * this. It is specified by the System V ABI.
962 elf_hash(const char *name)
964 const unsigned char *p = (const unsigned char *) name;
970 if ((g = h & 0xf0000000) != 0)
978 link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
980 elf_file_t ef = (elf_file_t) lf;
981 unsigned long symnum;
987 /* First, search hashed global symbols */
988 hash = elf_hash(name);
989 symnum = ef->buckets[hash % ef->nbuckets];
991 while (symnum != STN_UNDEF) {
992 if (symnum >= ef->nchains) {
993 printf("link_elf_lookup_symbol: corrupt symbol table\n");
997 symp = ef->symtab + symnum;
998 if (symp->st_name == 0) {
999 printf("link_elf_lookup_symbol: corrupt symbol table\n");
1003 strp = ef->strtab + symp->st_name;
1005 if (strcmp(name, strp) == 0) {
1006 if (symp->st_shndx != SHN_UNDEF ||
1007 (symp->st_value != 0 &&
1008 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1009 *sym = (c_linker_sym_t) symp;
1015 symnum = ef->chains[symnum];
1018 /* If we have not found it, look at the full table (if loaded) */
1019 if (ef->symtab == ef->ddbsymtab)
1022 /* Exhaustive search */
1023 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1024 strp = ef->ddbstrtab + symp->st_name;
1025 if (strcmp(name, strp) == 0) {
1026 if (symp->st_shndx != SHN_UNDEF ||
1027 (symp->st_value != 0 &&
1028 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1029 *sym = (c_linker_sym_t) symp;
1040 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
1042 elf_file_t ef = (elf_file_t) lf;
1043 const Elf_Sym* es = (const Elf_Sym*) sym;
1045 if (es >= ef->symtab && ((es - ef->symtab) < ef->nchains)) {
1046 symval->name = ef->strtab + es->st_name;
1047 symval->value = (caddr_t) ef->address + es->st_value;
1048 symval->size = es->st_size;
1051 if (ef->symtab == ef->ddbsymtab)
1053 if (es >= ef->ddbsymtab && ((es - ef->ddbsymtab) < ef->ddbsymcnt)) {
1054 symval->name = ef->ddbstrtab + es->st_name;
1055 symval->value = (caddr_t) ef->address + es->st_value;
1056 symval->size = es->st_size;
1063 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1064 c_linker_sym_t* sym, long* diffp)
1066 elf_file_t ef = (elf_file_t) lf;
1067 u_long off = (uintptr_t) (void *) value;
1071 const Elf_Sym* best = 0;
1074 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1075 if (es->st_name == 0)
1077 st_value = es->st_value + (uintptr_t) (void *) ef->address;
1078 if (off >= st_value) {
1079 if (off - st_value < diff) {
1080 diff = off - st_value;
1084 } else if (off - st_value == diff) {
1093 *sym = (c_linker_sym_t) best;
1099 * Look up a linker set on an ELF system.
1102 link_elf_lookup_set(linker_file_t lf, const char *name,
1103 void ***startp, void ***stopp, int *countp)
1106 linker_symval_t symval;
1108 void **start, **stop;
1109 int len, error = 0, count;
1111 len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1112 setsym = malloc(len, M_LINKER, M_WAITOK);
1116 /* get address of first entry */
1117 snprintf(setsym, len, "%s%s", "__start_set_", name);
1118 error = link_elf_lookup_symbol(lf, setsym, &sym);
1121 link_elf_symbol_values(lf, sym, &symval);
1122 if (symval.value == 0) {
1126 start = (void **)symval.value;
1128 /* get address of last entry */
1129 snprintf(setsym, len, "%s%s", "__stop_set_", name);
1130 error = link_elf_lookup_symbol(lf, setsym, &sym);
1133 link_elf_symbol_values(lf, sym, &symval);
1134 if (symval.value == 0) {
1138 stop = (void **)symval.value;
1140 /* and the number of entries */
1141 count = stop - start;
1152 free(setsym, M_LINKER);
1157 link_elf_each_function_name(linker_file_t file,
1158 int (*callback)(const char *, void *), void *opaque) {
1159 elf_file_t ef = (elf_file_t)file;
1160 const Elf_Sym* symp;
1163 /* Exhaustive search */
1164 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1165 if (symp->st_value != 0 &&
1166 ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1167 error = callback(ef->ddbstrtab + symp->st_name, opaque);