2 * Copyright (c) 1998 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
26 * $FreeBSD: src/sys/kern/link_elf.c,v 1.24 1999/12/24 15:33:36 bde Exp $
27 * $DragonFly: src/sys/kern/link_elf.c,v 1.26 2007/04/30 07:18:54 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
35 #include <sys/nlookup.h>
36 #include <sys/fcntl.h>
37 #include <sys/vnode.h>
38 #include <sys/linker.h>
39 #include <machine/elf.h>
42 #include <vm/vm_param.h>
43 #include <vm/vm_zone.h>
46 #include <vm/vm_object.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_extern.h>
51 #include <vm/vm_map.h>
53 static int link_elf_load_module(const char*, linker_file_t
*);
54 static int link_elf_load_file(const char*, linker_file_t
*);
55 static int link_elf_lookup_symbol(linker_file_t
, const char*,
57 static int link_elf_symbol_values(linker_file_t
, c_linker_sym_t
, linker_symval_t
*);
58 static int link_elf_search_symbol(linker_file_t
, caddr_t value
,
59 c_linker_sym_t
* sym
, long* diffp
);
61 static void link_elf_unload_file(linker_file_t
);
62 static void link_elf_unload_module(linker_file_t
);
63 static int link_elf_lookup_set(linker_file_t
, const char *,
64 void ***, void ***, int *);
66 static struct linker_class_ops link_elf_class_ops
= {
70 static struct linker_file_ops link_elf_file_ops
= {
71 link_elf_lookup_symbol
,
72 link_elf_symbol_values
,
73 link_elf_search_symbol
,
78 static struct linker_file_ops link_elf_module_ops
= {
79 link_elf_lookup_symbol
,
80 link_elf_symbol_values
,
81 link_elf_search_symbol
,
82 link_elf_unload_module
,
86 typedef struct elf_file
{
87 caddr_t address
; /* Relocation address */
89 vm_object_t object
; /* VM object to hold file pages */
91 const Elf_Dyn
* dynamic
; /* Symbol table etc. */
92 Elf_Off nbuckets
; /* DT_HASH info */
94 const Elf_Off
* buckets
;
95 const Elf_Off
* chains
;
97 caddr_t strtab
; /* DT_STRTAB */
98 int strsz
; /* DT_STRSZ */
99 const Elf_Sym
* symtab
; /* DT_SYMTAB */
100 Elf_Addr
* got
; /* DT_PLTGOT */
101 const Elf_Rel
* pltrel
; /* DT_JMPREL */
102 int pltrelsize
; /* DT_PLTRELSZ */
103 const Elf_Rela
* pltrela
; /* DT_JMPREL */
104 int pltrelasize
; /* DT_PLTRELSZ */
105 const Elf_Rel
* rel
; /* DT_REL */
106 int relsize
; /* DT_RELSZ */
107 const Elf_Rela
* rela
; /* DT_RELA */
108 int relasize
; /* DT_RELASZ */
110 const Elf_Sym
* ddbsymtab
; /* The symbol table we are using */
111 long ddbsymcnt
; /* Number of symbols */
112 caddr_t ddbstrtab
; /* String table */
113 long ddbstrcnt
; /* number of bytes in string table */
114 caddr_t symbase
; /* malloc'ed symbold base */
115 caddr_t strbase
; /* malloc'ed string base */
118 static int parse_dynamic(linker_file_t lf
);
119 static int load_dependancies(linker_file_t lf
);
120 static int relocate_file(linker_file_t lf
);
121 static int parse_module_symbols(linker_file_t lf
);
124 * The kernel symbol table starts here.
126 extern struct _dynamic _DYNAMIC
;
129 link_elf_init(void* arg
)
132 caddr_t modptr
, baseptr
, sizeptr
;
136 #if ELF_TARG_CLASS == ELFCLASS32
137 linker_add_class("elf32", NULL
, &link_elf_class_ops
);
139 linker_add_class("elf64", NULL
, &link_elf_class_ops
);
142 dp
= (Elf_Dyn
*) &_DYNAMIC
;
144 ef
= kmalloc(sizeof(struct elf_file
), M_LINKER
, M_INTWAIT
| M_ZERO
);
146 #ifdef SPARSE_MAPPING
151 modptr
= preload_search_by_type("elf kernel");
153 modname
= (char *)preload_search_info(modptr
, MODINFO_NAME
);
156 linker_kernel_file
= linker_make_file(modname
, ef
, &link_elf_file_ops
);
157 if (linker_kernel_file
== NULL
)
158 panic("link_elf_init: Can't create linker structures for kernel");
159 parse_dynamic(linker_kernel_file
);
160 linker_kernel_file
->address
= (caddr_t
) KERNBASE
;
161 linker_kernel_file
->size
= -(intptr_t)linker_kernel_file
->address
;
165 baseptr
= preload_search_info(modptr
, MODINFO_ADDR
);
167 linker_kernel_file
->address
= *(caddr_t
*)baseptr
;
168 sizeptr
= preload_search_info(modptr
, MODINFO_SIZE
);
170 linker_kernel_file
->size
= *(size_t *)sizeptr
;
172 parse_module_symbols(linker_kernel_file
);
173 linker_current_file
= linker_kernel_file
;
174 linker_kernel_file
->flags
|= LINKER_FILE_LINKED
;
178 SYSINIT(link_elf
, SI_BOOT2_KLD
, SI_ORDER_SECOND
, link_elf_init
, 0);
181 parse_module_symbols(linker_file_t lf
)
183 elf_file_t ef
= lf
->priv
;
185 caddr_t ssym
, esym
, base
;
191 if (ef
->modptr
== NULL
)
193 pointer
= preload_search_info(ef
->modptr
, MODINFO_METADATA
|MODINFOMD_SSYM
);
196 ssym
= *(caddr_t
*)pointer
;
197 pointer
= preload_search_info(ef
->modptr
, MODINFO_METADATA
|MODINFOMD_ESYM
);
200 esym
= *(caddr_t
*)pointer
;
204 symcnt
= *(long *)base
;
205 base
+= sizeof(long);
206 symtab
= (Elf_Sym
*)base
;
207 base
+= roundup(symcnt
, sizeof(long));
209 if (base
> esym
|| base
< ssym
) {
210 kprintf("Symbols are corrupt!\n");
214 strcnt
= *(long *)base
;
215 base
+= sizeof(long);
217 base
+= roundup(strcnt
, sizeof(long));
219 if (base
> esym
|| base
< ssym
) {
220 kprintf("Symbols are corrupt!\n");
224 ef
->ddbsymtab
= symtab
;
225 ef
->ddbsymcnt
= symcnt
/ sizeof(Elf_Sym
);
226 ef
->ddbstrtab
= strtab
;
227 ef
->ddbstrcnt
= strcnt
;
233 parse_dynamic(linker_file_t lf
)
235 elf_file_t ef
= lf
->priv
;
237 int plttype
= DT_REL
;
239 for (dp
= ef
->dynamic
; dp
->d_tag
!= DT_NULL
; dp
++) {
243 /* From src/libexec/rtld-elf/rtld.c */
244 const Elf_Off
*hashtab
= (const Elf_Off
*)
245 (ef
->address
+ dp
->d_un
.d_ptr
);
246 ef
->nbuckets
= hashtab
[0];
247 ef
->nchains
= hashtab
[1];
248 ef
->buckets
= hashtab
+ 2;
249 ef
->chains
= ef
->buckets
+ ef
->nbuckets
;
253 ef
->strtab
= (caddr_t
) (ef
->address
+ dp
->d_un
.d_ptr
);
256 ef
->strsz
= dp
->d_un
.d_val
;
259 ef
->symtab
= (Elf_Sym
*) (ef
->address
+ dp
->d_un
.d_ptr
);
262 if (dp
->d_un
.d_val
!= sizeof(Elf_Sym
))
266 ef
->got
= (Elf_Addr
*) (ef
->address
+ dp
->d_un
.d_ptr
);
269 ef
->rel
= (const Elf_Rel
*) (ef
->address
+ dp
->d_un
.d_ptr
);
272 ef
->relsize
= dp
->d_un
.d_val
;
275 if (dp
->d_un
.d_val
!= sizeof(Elf_Rel
))
279 ef
->pltrel
= (const Elf_Rel
*) (ef
->address
+ dp
->d_un
.d_ptr
);
282 ef
->pltrelsize
= dp
->d_un
.d_val
;
285 ef
->rela
= (const Elf_Rela
*) (ef
->address
+ dp
->d_un
.d_ptr
);
288 ef
->relasize
= dp
->d_un
.d_val
;
291 if (dp
->d_un
.d_val
!= sizeof(Elf_Rela
))
295 plttype
= dp
->d_un
.d_val
;
296 if (plttype
!= DT_REL
&& plttype
!= DT_RELA
)
302 if (plttype
== DT_RELA
) {
303 ef
->pltrela
= (const Elf_Rela
*) ef
->pltrel
;
305 ef
->pltrelasize
= ef
->pltrelsize
;
309 ef
->ddbsymtab
= ef
->symtab
;
310 ef
->ddbsymcnt
= ef
->nchains
;
311 ef
->ddbstrtab
= ef
->strtab
;
312 ef
->ddbstrcnt
= ef
->strsz
;
318 link_elf_error(const char *s
)
320 kprintf("kldload: %s\n", s
);
324 link_elf_load_module(const char *filename
, linker_file_t
*result
)
326 caddr_t modptr
, baseptr
, sizeptr
, dynptr
;
334 * Look to see if we have the module preloaded.
336 modptr
= preload_search_by_name(filename
);
338 return (link_elf_load_file(filename
, result
));
340 /* It's preloaded, check we can handle it and collect information */
341 type
= (char *)preload_search_info(modptr
, MODINFO_TYPE
);
342 baseptr
= preload_search_info(modptr
, MODINFO_ADDR
);
343 sizeptr
= preload_search_info(modptr
, MODINFO_SIZE
);
344 dynptr
= preload_search_info(modptr
, MODINFO_METADATA
|MODINFOMD_DYNAMIC
);
345 if (type
== NULL
|| strcmp(type
, "elf module") != 0)
347 if (baseptr
== NULL
|| sizeptr
== NULL
|| dynptr
== NULL
)
350 ef
= kmalloc(sizeof(struct elf_file
), M_LINKER
, M_WAITOK
| M_ZERO
);
354 ef
->address
= *(caddr_t
*)baseptr
;
355 #ifdef SPARSE_MAPPING
358 dp
= (vm_offset_t
)ef
->address
+ *(vm_offset_t
*)dynptr
;
359 ef
->dynamic
= (Elf_Dyn
*)dp
;
360 lf
= linker_make_file(filename
, ef
, &link_elf_module_ops
);
365 lf
->address
= ef
->address
;
366 lf
->size
= *(size_t *)sizeptr
;
368 error
= parse_dynamic(lf
);
370 linker_file_unload(lf
);
373 error
= load_dependancies(lf
);
375 linker_file_unload(lf
);
378 error
= relocate_file(lf
);
380 linker_file_unload(lf
);
383 parse_module_symbols(lf
);
384 lf
->flags
|= LINKER_FILE_LINKED
;
390 link_elf_load_file(const char* filename
, linker_file_t
* result
)
392 struct nlookupdata nd
;
393 struct thread
*td
= curthread
; /* XXX */
394 struct proc
*p
= td
->td_proc
;
409 Elf_Addr base_vlimit
;
422 if (p
->p_ucred
== NULL
) {
423 kprintf("link_elf_load_file: cannot load '%s' from filesystem"
424 " this early\n", filename
);
429 pathname
= linker_search_path(filename
);
430 if (pathname
== NULL
)
433 error
= nlookup_init(&nd
, pathname
, UIO_SYSSPACE
, NLC_FOLLOW
|NLC_LOCKVP
);
435 error
= vn_open(&nd
, NULL
, FREAD
, 0);
436 kfree(pathname
, M_LINKER
);
442 nd
.nl_open_vp
= NULL
;
446 * Read the elf header from the file.
448 firstpage
= kmalloc(PAGE_SIZE
, M_LINKER
, M_WAITOK
);
449 if (firstpage
== NULL
) {
453 hdr
= (Elf_Ehdr
*)firstpage
;
454 error
= vn_rdwr(UIO_READ
, vp
, firstpage
, PAGE_SIZE
, 0,
455 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
);
456 nbytes
= PAGE_SIZE
- resid
;
465 if (hdr
->e_ident
[EI_CLASS
] != ELF_TARG_CLASS
466 || hdr
->e_ident
[EI_DATA
] != ELF_TARG_DATA
) {
467 link_elf_error("Unsupported file layout");
471 if (hdr
->e_ident
[EI_VERSION
] != EV_CURRENT
472 || hdr
->e_version
!= EV_CURRENT
) {
473 link_elf_error("Unsupported file version");
477 if (hdr
->e_type
!= ET_EXEC
&& hdr
->e_type
!= ET_DYN
) {
478 link_elf_error("Unsupported file type");
482 if (hdr
->e_machine
!= ELF_TARG_MACH
) {
483 link_elf_error("Unsupported machine");
489 * We rely on the program header being in the first page. This is
490 * not strictly required by the ABI specification, but it seems to
491 * always true in practice. And, it simplifies things considerably.
493 if (!((hdr
->e_phentsize
== sizeof(Elf_Phdr
)) &&
494 (hdr
->e_phoff
+ hdr
->e_phnum
*sizeof(Elf_Phdr
) <= PAGE_SIZE
) &&
495 (hdr
->e_phoff
+ hdr
->e_phnum
*sizeof(Elf_Phdr
) <= nbytes
)))
496 link_elf_error("Unreadable program headers");
499 * Scan the program header entries, and save key information.
501 * We rely on there being exactly two load segments, text and data,
504 phdr
= (Elf_Phdr
*) (firstpage
+ hdr
->e_phoff
);
505 phlimit
= phdr
+ hdr
->e_phnum
;
509 while (phdr
< phlimit
) {
510 switch (phdr
->p_type
) {
514 link_elf_error("Too many sections");
534 link_elf_error("Object is not dynamically-linked");
540 * Allocate the entire address space of the object, to stake out our
541 * contiguous region, and to establish the base address for relocation.
543 base_offset
= trunc_page(segs
[0]->p_offset
);
544 base_vaddr
= trunc_page(segs
[0]->p_vaddr
);
545 base_vlimit
= round_page(segs
[1]->p_vaddr
+ segs
[1]->p_memsz
);
546 mapsize
= base_vlimit
- base_vaddr
;
548 ef
= kmalloc(sizeof(struct elf_file
), M_LINKER
, M_WAITOK
| M_ZERO
);
549 #ifdef SPARSE_MAPPING
550 ef
->object
= vm_object_allocate(OBJT_DEFAULT
, mapsize
>> PAGE_SHIFT
);
551 if (ef
->object
== NULL
) {
556 vm_object_reference(ef
->object
);
557 ef
->address
= (caddr_t
)vm_map_min(&kernel_map
);
558 error
= vm_map_find(&kernel_map
, ef
->object
, 0,
559 (vm_offset_t
*)&ef
->address
, mapsize
,
562 VM_PROT_ALL
, VM_PROT_ALL
,
565 vm_object_deallocate(ef
->object
);
570 ef
->address
= kmalloc(mapsize
, M_LINKER
, M_WAITOK
);
572 mapbase
= ef
->address
;
575 * Read the text and data sections and zero the bss.
577 for (i
= 0; i
< 2; i
++) {
578 caddr_t segbase
= mapbase
+ segs
[i
]->p_vaddr
- base_vaddr
;
579 error
= vn_rdwr(UIO_READ
, vp
,
580 segbase
, segs
[i
]->p_filesz
, segs
[i
]->p_offset
,
581 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
);
583 #ifdef SPARSE_MAPPING
584 vm_map_remove(&kernel_map
, (vm_offset_t
) ef
->address
,
585 (vm_offset_t
) ef
->address
586 + (ef
->object
->size
<< PAGE_SHIFT
));
587 vm_object_deallocate(ef
->object
);
589 kfree(ef
->address
, M_LINKER
);
594 bzero(segbase
+ segs
[i
]->p_filesz
,
595 segs
[i
]->p_memsz
- segs
[i
]->p_filesz
);
597 #ifdef SPARSE_MAPPING
599 * Wire down the pages
601 vm_map_wire(&kernel_map
,
602 (vm_offset_t
) segbase
,
603 (vm_offset_t
) segbase
+ segs
[i
]->p_memsz
,
608 ef
->dynamic
= (const Elf_Dyn
*) (mapbase
+ phdyn
->p_vaddr
- base_vaddr
);
610 lf
= linker_make_file(filename
, ef
, &link_elf_file_ops
);
612 #ifdef SPARSE_MAPPING
613 vm_map_remove(&kernel_map
, (vm_offset_t
) ef
->address
,
614 (vm_offset_t
) ef
->address
615 + (ef
->object
->size
<< PAGE_SHIFT
));
616 vm_object_deallocate(ef
->object
);
618 kfree(ef
->address
, M_LINKER
);
624 lf
->address
= ef
->address
;
627 error
= parse_dynamic(lf
);
630 error
= load_dependancies(lf
);
633 error
= relocate_file(lf
);
637 /* Try and load the symbol table if it's present. (you can strip it!) */
638 nbytes
= hdr
->e_shnum
* hdr
->e_shentsize
;
639 if (nbytes
== 0 || hdr
->e_shoff
== 0)
641 shdr
= kmalloc(nbytes
, M_LINKER
, M_WAITOK
| M_ZERO
);
646 error
= vn_rdwr(UIO_READ
, vp
,
647 (caddr_t
)shdr
, nbytes
, hdr
->e_shoff
,
648 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
);
653 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
654 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
656 symstrindex
= shdr
[i
].sh_link
;
659 if (symtabindex
< 0 || symstrindex
< 0)
662 symcnt
= shdr
[symtabindex
].sh_size
;
663 ef
->symbase
= kmalloc(symcnt
, M_LINKER
, M_WAITOK
);
664 strcnt
= shdr
[symstrindex
].sh_size
;
665 ef
->strbase
= kmalloc(strcnt
, M_LINKER
, M_WAITOK
);
667 if (ef
->symbase
== NULL
|| ef
->strbase
== NULL
) {
671 error
= vn_rdwr(UIO_READ
, vp
,
672 ef
->symbase
, symcnt
, shdr
[symtabindex
].sh_offset
,
673 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
);
676 error
= vn_rdwr(UIO_READ
, vp
,
677 ef
->strbase
, strcnt
, shdr
[symstrindex
].sh_offset
,
678 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
);
682 ef
->ddbsymcnt
= symcnt
/ sizeof(Elf_Sym
);
683 ef
->ddbsymtab
= (const Elf_Sym
*)ef
->symbase
;
684 ef
->ddbstrcnt
= strcnt
;
685 ef
->ddbstrtab
= ef
->strbase
;
687 lf
->flags
|= LINKER_FILE_LINKED
;
695 linker_file_unload(lf
);
697 kfree(shdr
, M_LINKER
);
699 kfree(firstpage
, M_LINKER
);
707 link_elf_unload_file(linker_file_t file
)
709 elf_file_t ef
= file
->priv
;
712 #ifdef SPARSE_MAPPING
714 vm_map_remove(&kernel_map
, (vm_offset_t
) ef
->address
,
715 (vm_offset_t
) ef
->address
716 + (ef
->object
->size
<< PAGE_SHIFT
));
717 vm_object_deallocate(ef
->object
);
721 kfree(ef
->address
, M_LINKER
);
724 kfree(ef
->symbase
, M_LINKER
);
726 kfree(ef
->strbase
, M_LINKER
);
732 link_elf_unload_module(linker_file_t file
)
734 elf_file_t ef
= file
->priv
;
739 preload_delete_name(file
->filename
);
743 load_dependancies(linker_file_t lf
)
745 elf_file_t ef
= lf
->priv
;
752 * All files are dependant on /kernel.
754 if (linker_kernel_file
) {
755 linker_kernel_file
->refs
++;
756 linker_file_add_dependancy(lf
, linker_kernel_file
);
759 for (dp
= ef
->dynamic
; dp
->d_tag
!= DT_NULL
; dp
++) {
760 if (dp
->d_tag
== DT_NEEDED
) {
761 name
= ef
->strtab
+ dp
->d_un
.d_val
;
763 error
= linker_load_file(name
, &lfdep
);
766 error
= linker_file_add_dependancy(lf
, lfdep
);
777 symbol_name(elf_file_t ef
, Elf_Word r_info
)
781 if (ELF_R_SYM(r_info
)) {
782 ref
= ef
->symtab
+ ELF_R_SYM(r_info
);
783 return ef
->strtab
+ ref
->st_name
;
789 relocate_file(linker_file_t lf
)
791 elf_file_t ef
= lf
->priv
;
792 const Elf_Rel
*rellim
;
794 const Elf_Rela
*relalim
;
795 const Elf_Rela
*rela
;
798 /* Perform relocations without addend if there are any: */
801 rellim
= (const Elf_Rel
*)((const char *)ef
->rel
+ ef
->relsize
);
802 while (rel
< rellim
) {
803 symname
= symbol_name(ef
, rel
->r_info
);
804 if (elf_reloc(lf
, rel
, ELF_RELOC_REL
, symname
)) {
805 kprintf("link_elf: symbol %s undefined\n", symname
);
812 /* Perform relocations with addend if there are any: */
815 relalim
= (const Elf_Rela
*)((const char *)ef
->rela
+ ef
->relasize
);
816 while (rela
< relalim
) {
817 symname
= symbol_name(ef
, rela
->r_info
);
818 if (elf_reloc(lf
, rela
, ELF_RELOC_RELA
, symname
)) {
819 kprintf("link_elf: symbol %s undefined\n", symname
);
826 /* Perform PLT relocations without addend if there are any: */
829 rellim
= (const Elf_Rel
*)((const char *)ef
->pltrel
+ ef
->pltrelsize
);
830 while (rel
< rellim
) {
831 symname
= symbol_name(ef
, rel
->r_info
);
832 if (elf_reloc(lf
, rel
, ELF_RELOC_REL
, symname
)) {
833 kprintf("link_elf: symbol %s undefined\n", symname
);
840 /* Perform relocations with addend if there are any: */
843 relalim
= (const Elf_Rela
*)((const char *)ef
->pltrela
+ ef
->pltrelasize
);
844 while (rela
< relalim
) {
845 symname
= symbol_name(ef
, rela
->r_info
);
846 if (elf_reloc(lf
, rela
, ELF_RELOC_RELA
, symname
)) {
847 kprintf("link_elf: symbol %s undefined\n", symname
);
858 * Hash function for symbol table lookup. Don't even think about changing
859 * this. It is specified by the System V ABI.
862 elf_hash(const char *name
)
864 const unsigned char *p
= (const unsigned char *) name
;
870 if ((g
= h
& 0xf0000000) != 0)
878 link_elf_lookup_symbol(linker_file_t lf
, const char* name
, c_linker_sym_t
* sym
)
880 elf_file_t ef
= lf
->priv
;
881 unsigned long symnum
;
887 /* First, search hashed global symbols */
888 hash
= elf_hash(name
);
889 symnum
= ef
->buckets
[hash
% ef
->nbuckets
];
891 while (symnum
!= STN_UNDEF
) {
892 if (symnum
>= ef
->nchains
) {
893 kprintf("link_elf_lookup_symbol: corrupt symbol table\n");
897 symp
= ef
->symtab
+ symnum
;
898 if (symp
->st_name
== 0) {
899 kprintf("link_elf_lookup_symbol: corrupt symbol table\n");
903 strp
= ef
->strtab
+ symp
->st_name
;
905 if (strcmp(name
, strp
) == 0) {
906 if (symp
->st_shndx
!= SHN_UNDEF
||
907 (symp
->st_value
!= 0 &&
908 ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
)
910 *sym
= (c_linker_sym_t
) symp
;
917 symnum
= ef
->chains
[symnum
];
920 /* If we have not found it, look at the full table (if loaded) */
921 if (ef
->symtab
== ef
->ddbsymtab
)
924 /* Exhaustive search */
925 for (i
= 0, symp
= ef
->ddbsymtab
; i
< ef
->ddbsymcnt
; i
++, symp
++) {
926 strp
= ef
->ddbstrtab
+ symp
->st_name
;
927 if (strcmp(name
, strp
) == 0) {
928 if (symp
->st_shndx
!= SHN_UNDEF
||
929 (symp
->st_value
!= 0 &&
930 ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
)) {
931 *sym
= (c_linker_sym_t
) symp
;
942 link_elf_symbol_values(linker_file_t lf
, c_linker_sym_t sym
, linker_symval_t
* symval
)
944 elf_file_t ef
= lf
->priv
;
945 const Elf_Sym
* es
= (const Elf_Sym
*) sym
;
947 if (es
>= ef
->symtab
&& ((es
- ef
->symtab
) < ef
->nchains
)) {
948 symval
->name
= ef
->strtab
+ es
->st_name
;
949 symval
->value
= (caddr_t
) ef
->address
+ es
->st_value
;
950 symval
->size
= es
->st_size
;
953 if (ef
->symtab
== ef
->ddbsymtab
)
955 if (es
>= ef
->ddbsymtab
&& ((es
- ef
->ddbsymtab
) < ef
->ddbsymcnt
)) {
956 symval
->name
= ef
->ddbstrtab
+ es
->st_name
;
957 symval
->value
= (caddr_t
) ef
->address
+ es
->st_value
;
958 symval
->size
= es
->st_size
;
965 link_elf_search_symbol(linker_file_t lf
, caddr_t value
,
966 c_linker_sym_t
* sym
, long* diffp
)
968 elf_file_t ef
= lf
->priv
;
969 u_long off
= (uintptr_t) (void *) value
;
973 const Elf_Sym
* best
= 0;
976 for (i
= 0, es
= ef
->ddbsymtab
; i
< ef
->ddbsymcnt
; i
++, es
++) {
977 if (es
->st_name
== 0)
979 st_value
= es
->st_value
+ (uintptr_t) (void *) ef
->address
;
980 if (off
>= st_value
) {
981 if (off
- st_value
< diff
) {
982 diff
= off
- st_value
;
986 } else if (off
- st_value
== diff
) {
995 *sym
= (c_linker_sym_t
) best
;
1001 * Look up a linker set on an ELF system.
1004 link_elf_lookup_set(linker_file_t lf
, const char *name
,
1005 void ***startp
, void ***stopp
, int *countp
)
1008 linker_symval_t symval
;
1010 void **start
, **stop
;
1011 int len
, error
= 0, count
;
1013 len
= strlen(name
) + sizeof("__start_set_"); /* sizeof includes \0 */
1014 setsym
= kmalloc(len
, M_LINKER
, M_WAITOK
);
1018 /* get address of first entry */
1019 ksnprintf(setsym
, len
, "%s%s", "__start_set_", name
);
1020 error
= link_elf_lookup_symbol(lf
, setsym
, &sym
);
1023 link_elf_symbol_values(lf
, sym
, &symval
);
1024 if (symval
.value
== 0) {
1028 start
= (void **)symval
.value
;
1030 /* get address of last entry */
1031 ksnprintf(setsym
, len
, "%s%s", "__stop_set_", name
);
1032 error
= link_elf_lookup_symbol(lf
, setsym
, &sym
);
1035 link_elf_symbol_values(lf
, sym
, &symval
);
1036 if (symval
.value
== 0) {
1040 stop
= (void **)symval
.value
;
1042 /* and the number of entries */
1043 count
= stop
- start
;
1054 kfree(setsym
, M_LINKER
);