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 $
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
34 #include <sys/nlookup.h>
35 #include <sys/fcntl.h>
36 #include <sys/vnode.h>
37 #include <sys/linker.h>
38 #include <machine/elf.h>
41 #include <vm/vm_param.h>
42 #include <vm/vm_zone.h>
45 #include <vm/vm_map.h>
47 #if defined(__x86_64__) && defined(_KERNEL_VIRTUAL)
51 static int link_elf_preload_file(const char *, linker_file_t
*);
52 static int link_elf_preload_finish(linker_file_t
);
53 static int link_elf_load_file(const char*, linker_file_t
*);
54 static int link_elf_lookup_symbol(linker_file_t
, const char*,
56 static int link_elf_symbol_values(linker_file_t
, c_linker_sym_t
, linker_symval_t
*);
57 static int link_elf_search_symbol(linker_file_t
, caddr_t value
,
58 c_linker_sym_t
* sym
, long* diffp
);
60 static void link_elf_unload_file(linker_file_t
);
61 static void link_elf_unload_module(linker_file_t
);
62 static int link_elf_lookup_set(linker_file_t
, const char *,
63 void ***, void ***, int *);
64 static int elf_lookup(linker_file_t lf
, Elf_Size symidx
, int deps
, Elf_Addr
*);
65 static void link_elf_reloc_local(linker_file_t lf
);
67 static struct linker_class_ops link_elf_class_ops
= {
69 link_elf_preload_file
,
72 static struct linker_file_ops link_elf_file_ops
= {
73 .lookup_symbol
= link_elf_lookup_symbol
,
74 .symbol_values
= link_elf_symbol_values
,
75 .search_symbol
= link_elf_search_symbol
,
76 .unload
= link_elf_unload_file
,
77 .lookup_set
= link_elf_lookup_set
80 static struct linker_file_ops link_elf_module_ops
= {
81 .lookup_symbol
= link_elf_lookup_symbol
,
82 .symbol_values
= link_elf_symbol_values
,
83 .search_symbol
= link_elf_search_symbol
,
84 .preload_finish
= link_elf_preload_finish
,
85 .unload
= link_elf_unload_module
,
86 .lookup_set
= link_elf_lookup_set
,
89 typedef struct elf_file
{
90 caddr_t address
; /* Relocation address */
91 const Elf_Dyn
* dynamic
; /* Symbol table etc. */
92 Elf_Hashelt nbuckets
; /* DT_HASH info */
94 const Elf_Hashelt
* buckets
;
95 const Elf_Hashelt
* 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 relocate_file(linker_file_t lf
);
120 static int parse_module_symbols(linker_file_t lf
);
123 * The kernel symbol table starts here.
125 extern struct _dynamic _DYNAMIC
;
128 link_elf_init(void* arg
)
131 caddr_t modptr
, baseptr
, sizeptr
;
135 #if ELF_TARG_CLASS == ELFCLASS32
136 linker_add_class("elf32", NULL
, &link_elf_class_ops
);
138 linker_add_class("elf64", NULL
, &link_elf_class_ops
);
141 dp
= (Elf_Dyn
*) &_DYNAMIC
;
143 ef
= kmalloc(sizeof(struct elf_file
), M_LINKER
, M_INTWAIT
| M_ZERO
);
147 modptr
= preload_search_by_type("elf kernel");
149 modname
= (char *)preload_search_info(modptr
, MODINFO_NAME
);
152 linker_kernel_file
= linker_make_file(modname
, ef
, &link_elf_file_ops
);
153 if (linker_kernel_file
== NULL
)
154 panic("link_elf_init: Can't create linker structures for kernel");
155 parse_dynamic(linker_kernel_file
);
156 #if defined(__x86_64__) && defined(_KERNEL_VIRTUAL)
157 fprintf(stderr
, "WARNING: KERNBASE being used\n");
159 linker_kernel_file
->address
= (caddr_t
) KERNBASE
;
160 linker_kernel_file
->size
= -(intptr_t)linker_kernel_file
->address
;
164 baseptr
= preload_search_info(modptr
, MODINFO_ADDR
);
166 linker_kernel_file
->address
= *(caddr_t
*)baseptr
;
167 sizeptr
= preload_search_info(modptr
, MODINFO_SIZE
);
169 linker_kernel_file
->size
= *(size_t *)sizeptr
;
171 parse_module_symbols(linker_kernel_file
);
172 linker_current_file
= linker_kernel_file
;
173 linker_kernel_file
->flags
|= LINKER_FILE_LINKED
;
177 SYSINIT(link_elf
, SI_BOOT2_KLD
, SI_ORDER_SECOND
, link_elf_init
, 0);
180 parse_module_symbols(linker_file_t lf
)
182 elf_file_t ef
= lf
->priv
;
184 caddr_t ssym
, esym
, base
;
190 if (ef
->modptr
== NULL
)
192 pointer
= preload_search_info(ef
->modptr
, MODINFO_METADATA
|MODINFOMD_SSYM
);
195 ssym
= *(caddr_t
*)pointer
;
196 pointer
= preload_search_info(ef
->modptr
, MODINFO_METADATA
|MODINFOMD_ESYM
);
199 esym
= *(caddr_t
*)pointer
;
203 symcnt
= *(long *)base
;
204 base
+= sizeof(long);
205 symtab
= (Elf_Sym
*)base
;
206 base
+= roundup(symcnt
, sizeof(long));
208 if (base
> esym
|| base
< ssym
) {
209 kprintf("Symbols are corrupt!\n");
213 strcnt
= *(long *)base
;
214 base
+= sizeof(long);
216 base
+= roundup(strcnt
, sizeof(long));
218 if (base
> esym
|| base
< ssym
) {
219 kprintf("Symbols are corrupt!\n");
223 ef
->ddbsymtab
= symtab
;
224 ef
->ddbsymcnt
= symcnt
/ sizeof(Elf_Sym
);
225 ef
->ddbstrtab
= strtab
;
226 ef
->ddbstrcnt
= strcnt
;
232 parse_dynamic(linker_file_t lf
)
234 elf_file_t ef
= lf
->priv
;
236 int plttype
= DT_REL
;
238 for (dp
= ef
->dynamic
; dp
->d_tag
!= DT_NULL
; dp
++) {
242 /* From src/libexec/rtld-elf/rtld.c */
243 const Elf_Hashelt
*hashtab
= (const Elf_Hashelt
*)
244 (ef
->address
+ dp
->d_un
.d_ptr
);
245 ef
->nbuckets
= hashtab
[0];
246 ef
->nchains
= hashtab
[1];
247 ef
->buckets
= hashtab
+ 2;
248 ef
->chains
= ef
->buckets
+ ef
->nbuckets
;
252 ef
->strtab
= (caddr_t
) (ef
->address
+ dp
->d_un
.d_ptr
);
255 ef
->strsz
= dp
->d_un
.d_val
;
258 ef
->symtab
= (Elf_Sym
*) (ef
->address
+ dp
->d_un
.d_ptr
);
261 if (dp
->d_un
.d_val
!= sizeof(Elf_Sym
))
265 ef
->got
= (Elf_Addr
*) (ef
->address
+ dp
->d_un
.d_ptr
);
268 ef
->rel
= (const Elf_Rel
*) (ef
->address
+ dp
->d_un
.d_ptr
);
271 ef
->relsize
= dp
->d_un
.d_val
;
274 if (dp
->d_un
.d_val
!= sizeof(Elf_Rel
))
278 ef
->pltrel
= (const Elf_Rel
*) (ef
->address
+ dp
->d_un
.d_ptr
);
281 ef
->pltrelsize
= dp
->d_un
.d_val
;
284 ef
->rela
= (const Elf_Rela
*) (ef
->address
+ dp
->d_un
.d_ptr
);
287 ef
->relasize
= dp
->d_un
.d_val
;
290 if (dp
->d_un
.d_val
!= sizeof(Elf_Rela
))
294 plttype
= dp
->d_un
.d_val
;
295 if (plttype
!= DT_REL
&& plttype
!= DT_RELA
)
301 if (plttype
== DT_RELA
) {
302 ef
->pltrela
= (const Elf_Rela
*) ef
->pltrel
;
304 ef
->pltrelasize
= ef
->pltrelsize
;
308 ef
->ddbsymtab
= ef
->symtab
;
309 ef
->ddbsymcnt
= ef
->nchains
;
310 ef
->ddbstrtab
= ef
->strtab
;
311 ef
->ddbstrcnt
= ef
->strsz
;
317 link_elf_error(const char *s
)
319 kprintf("kldload: %s\n", s
);
323 link_elf_preload_file(const char *filename
, linker_file_t
*result
)
325 caddr_t modptr
, baseptr
, sizeptr
, dynptr
;
333 * Look to see if we have the module preloaded.
335 modptr
= preload_search_by_name(filename
);
339 /* It's preloaded, check we can handle it and collect information */
340 type
= (char *)preload_search_info(modptr
, MODINFO_TYPE
);
341 baseptr
= preload_search_info(modptr
, MODINFO_ADDR
);
342 sizeptr
= preload_search_info(modptr
, MODINFO_SIZE
);
343 dynptr
= preload_search_info(modptr
, MODINFO_METADATA
|MODINFOMD_DYNAMIC
);
345 (strcmp(type
, "elf" __XSTRING(__ELF_WORD_SIZE
) " module") != 0 &&
346 strcmp(type
, "elf module") != 0))
348 if (baseptr
== NULL
|| sizeptr
== NULL
|| dynptr
== NULL
)
351 ef
= kmalloc(sizeof(struct elf_file
), M_LINKER
, M_WAITOK
| M_ZERO
);
353 ef
->address
= *(caddr_t
*)baseptr
;
354 dp
= (vm_offset_t
)ef
->address
+ *(vm_offset_t
*)dynptr
;
355 ef
->dynamic
= (Elf_Dyn
*)dp
;
356 lf
= linker_make_file(filename
, ef
, &link_elf_module_ops
);
361 lf
->address
= ef
->address
;
362 lf
->size
= *(size_t *)sizeptr
;
364 error
= parse_dynamic(lf
);
366 linker_file_unload(lf
);
369 link_elf_reloc_local(lf
);
375 link_elf_preload_finish(linker_file_t lf
)
379 error
= relocate_file(lf
);
382 parse_module_symbols(lf
);
388 link_elf_load_file(const char* filename
, linker_file_t
* result
)
390 struct nlookupdata nd
;
391 struct thread
*td
= curthread
; /* XXX */
392 struct proc
*p
= td
->td_proc
;
405 Elf_Addr base_vlimit
;
417 /* XXX Hack for firmware loading where p == NULL */
423 if (p
->p_ucred
== NULL
) {
424 kprintf("link_elf_load_file: cannot load '%s' from filesystem"
425 " this early\n", filename
);
430 pathname
= linker_search_path(filename
);
431 if (pathname
== NULL
)
434 error
= nlookup_init(&nd
, pathname
, UIO_SYSSPACE
, NLC_FOLLOW
|NLC_LOCKVP
);
436 error
= vn_open(&nd
, NULL
, FREAD
, 0);
437 kfree(pathname
, M_LINKER
);
443 nd
.nl_open_vp
= NULL
;
447 * Read the elf header from the file.
449 firstpage
= kmalloc(PAGE_SIZE
, M_LINKER
, M_WAITOK
);
450 hdr
= (Elf_Ehdr
*)firstpage
;
451 error
= vn_rdwr(UIO_READ
, vp
, firstpage
, PAGE_SIZE
, 0,
452 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
);
453 nbytes
= PAGE_SIZE
- resid
;
462 if (hdr
->e_ident
[EI_CLASS
] != ELF_TARG_CLASS
463 || hdr
->e_ident
[EI_DATA
] != ELF_TARG_DATA
) {
464 link_elf_error("Unsupported file layout");
468 if (hdr
->e_ident
[EI_VERSION
] != EV_CURRENT
469 || hdr
->e_version
!= EV_CURRENT
) {
470 link_elf_error("Unsupported file version");
474 if (hdr
->e_type
!= ET_EXEC
&& hdr
->e_type
!= ET_DYN
) {
478 if (hdr
->e_machine
!= ELF_TARG_MACH
) {
479 link_elf_error("Unsupported machine");
485 * We rely on the program header being in the first page. This is
486 * not strictly required by the ABI specification, but it seems to
487 * always true in practice. And, it simplifies things considerably.
489 if (!((hdr
->e_phentsize
== sizeof(Elf_Phdr
)) &&
490 (hdr
->e_phoff
+ hdr
->e_phnum
*sizeof(Elf_Phdr
) <= PAGE_SIZE
) &&
491 (hdr
->e_phoff
+ hdr
->e_phnum
*sizeof(Elf_Phdr
) <= nbytes
)))
492 link_elf_error("Unreadable program headers");
495 * Scan the program header entries, and save key information.
497 * We rely on there being exactly two load segments, text and data,
500 phdr
= (Elf_Phdr
*) (firstpage
+ hdr
->e_phoff
);
501 phlimit
= phdr
+ hdr
->e_phnum
;
504 while (phdr
< phlimit
) {
505 switch (phdr
->p_type
) {
509 link_elf_error("Too many sections");
532 link_elf_error("Object is not dynamically-linked");
538 * Allocate the entire address space of the object, to stake out our
539 * contiguous region, and to establish the base address for relocation.
541 base_vaddr
= trunc_page(segs
[0]->p_vaddr
);
542 base_vlimit
= round_page(segs
[1]->p_vaddr
+ segs
[1]->p_memsz
);
543 mapsize
= base_vlimit
- base_vaddr
;
545 ef
= kmalloc(sizeof(struct elf_file
), M_LINKER
, M_WAITOK
| M_ZERO
);
546 ef
->address
= kmalloc(mapsize
, M_LINKER
, M_WAITOK
);
547 mapbase
= ef
->address
;
550 * Read the text and data sections and zero the bss.
552 for (i
= 0; i
< 2; i
++) {
553 caddr_t segbase
= mapbase
+ segs
[i
]->p_vaddr
- base_vaddr
;
554 error
= vn_rdwr(UIO_READ
, vp
,
555 segbase
, segs
[i
]->p_filesz
, segs
[i
]->p_offset
,
556 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
);
558 kfree(ef
->address
, M_LINKER
);
562 bzero(segbase
+ segs
[i
]->p_filesz
,
563 segs
[i
]->p_memsz
- segs
[i
]->p_filesz
);
566 ef
->dynamic
= (const Elf_Dyn
*) (mapbase
+ phdyn
->p_vaddr
- base_vaddr
);
568 lf
= linker_make_file(filename
, ef
, &link_elf_file_ops
);
570 kfree(ef
->address
, M_LINKER
);
575 lf
->address
= ef
->address
;
578 error
= parse_dynamic(lf
);
581 link_elf_reloc_local(lf
);
582 error
= linker_load_dependencies(lf
);
585 error
= relocate_file(lf
);
589 /* Try and load the symbol table if it's present. (you can strip it!) */
590 nbytes
= hdr
->e_shnum
* hdr
->e_shentsize
;
591 if (nbytes
== 0 || hdr
->e_shoff
== 0)
593 shdr
= kmalloc(nbytes
, M_LINKER
, M_WAITOK
| M_ZERO
);
594 error
= vn_rdwr(UIO_READ
, vp
,
595 (caddr_t
)shdr
, nbytes
, hdr
->e_shoff
,
596 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
);
601 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
602 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
604 symstrindex
= shdr
[i
].sh_link
;
607 if (symtabindex
< 0 || symstrindex
< 0)
610 symcnt
= shdr
[symtabindex
].sh_size
;
611 ef
->symbase
= kmalloc(symcnt
, M_LINKER
, M_WAITOK
);
612 strcnt
= shdr
[symstrindex
].sh_size
;
613 ef
->strbase
= kmalloc(strcnt
, M_LINKER
, M_WAITOK
);
614 error
= vn_rdwr(UIO_READ
, vp
,
615 ef
->symbase
, symcnt
, shdr
[symtabindex
].sh_offset
,
616 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
);
619 error
= vn_rdwr(UIO_READ
, vp
,
620 ef
->strbase
, strcnt
, shdr
[symstrindex
].sh_offset
,
621 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
);
625 ef
->ddbsymcnt
= symcnt
/ sizeof(Elf_Sym
);
626 ef
->ddbsymtab
= (const Elf_Sym
*)ef
->symbase
;
627 ef
->ddbstrcnt
= strcnt
;
628 ef
->ddbstrtab
= ef
->strbase
;
636 linker_file_unload(lf
);
638 kfree(shdr
, M_LINKER
);
640 kfree(firstpage
, M_LINKER
);
642 vn_close(vp
, FREAD
, NULL
);
648 elf_relocaddr(linker_file_t lf
, Elf_Addr x
)
654 if (x
>= ef
->pcpu_start
&& x
< ef
->pcpu_stop
)
655 return ((x
- ef
->pcpu_start
) + ef
->pcpu_base
);
657 if (x
>= ef
->vnet_start
&& x
< ef
->vnet_stop
)
658 return ((x
- ef
->vnet_start
) + ef
->vnet_base
);
665 link_elf_unload_file(linker_file_t file
)
667 elf_file_t ef
= file
->priv
;
671 kfree(ef
->address
, M_LINKER
);
673 kfree(ef
->symbase
, M_LINKER
);
675 kfree(ef
->strbase
, M_LINKER
);
681 link_elf_unload_module(linker_file_t file
)
683 elf_file_t ef
= file
->priv
;
688 preload_delete_name(file
->filename
);
692 symbol_name(elf_file_t ef
, Elf_Size r_info
)
696 if (ELF_R_SYM(r_info
)) {
697 ref
= ef
->symtab
+ ELF_R_SYM(r_info
);
698 return ef
->strtab
+ ref
->st_name
;
704 relocate_file(linker_file_t lf
)
706 elf_file_t ef
= lf
->priv
;
707 const Elf_Rel
*rellim
;
709 const Elf_Rela
*relalim
;
710 const Elf_Rela
*rela
;
713 /* Perform relocations without addend if there are any: */
716 rellim
= (const Elf_Rel
*)((const char *)ef
->rel
+ ef
->relsize
);
717 while (rel
< rellim
) {
718 if (elf_reloc(lf
, (Elf_Addr
)ef
->address
, rel
, ELF_RELOC_REL
, elf_lookup
)) {
719 symname
= symbol_name(ef
, rel
->r_info
);
720 kprintf("link_elf: symbol %s undefined\n", symname
);
727 /* Perform relocations with addend if there are any: */
730 relalim
= (const Elf_Rela
*)((const char *)ef
->rela
+ ef
->relasize
);
731 while (rela
< relalim
) {
732 if (elf_reloc(lf
, (Elf_Addr
)ef
->address
, rela
, ELF_RELOC_RELA
, elf_lookup
)) {
733 symname
= symbol_name(ef
, rela
->r_info
);
734 kprintf("link_elf: symbol %s undefined\n", symname
);
741 /* Perform PLT relocations without addend if there are any: */
744 rellim
= (const Elf_Rel
*)((const char *)ef
->pltrel
+ ef
->pltrelsize
);
745 while (rel
< rellim
) {
746 if (elf_reloc(lf
, (Elf_Addr
)ef
->address
, rel
, ELF_RELOC_REL
, elf_lookup
)) {
747 symname
= symbol_name(ef
, rel
->r_info
);
748 kprintf("link_elf: symbol %s undefined\n", symname
);
755 /* Perform relocations with addend if there are any: */
758 relalim
= (const Elf_Rela
*)((const char *)ef
->pltrela
+ ef
->pltrelasize
);
759 while (rela
< relalim
) {
760 symname
= symbol_name(ef
, rela
->r_info
);
761 if (elf_reloc(lf
, (Elf_Addr
)ef
->address
, rela
, ELF_RELOC_RELA
, elf_lookup
)) {
762 kprintf("link_elf: symbol %s undefined\n", symname
);
773 * Hash function for symbol table lookup. Don't even think about changing
774 * this. It is specified by the System V ABI.
777 elf_hash(const char *name
)
779 const unsigned char *p
= (const unsigned char *) name
;
785 if ((g
= h
& 0xf0000000) != 0)
793 link_elf_lookup_symbol(linker_file_t lf
, const char* name
, c_linker_sym_t
* sym
)
795 elf_file_t ef
= lf
->priv
;
796 unsigned long symnum
;
802 /* If we don't have a hash, bail. */
803 if (ef
->buckets
== NULL
|| ef
->nbuckets
== 0) {
804 kprintf("link_elf_lookup_symbol: missing symbol hash table\n");
808 /* First, search hashed global symbols */
809 hash
= elf_hash(name
);
810 symnum
= ef
->buckets
[hash
% ef
->nbuckets
];
812 while (symnum
!= STN_UNDEF
) {
813 if (symnum
>= ef
->nchains
) {
814 kprintf("link_elf_lookup_symbol: corrupt symbol table\n");
818 symp
= ef
->symtab
+ symnum
;
819 if (symp
->st_name
== 0) {
820 kprintf("link_elf_lookup_symbol: corrupt symbol table\n");
824 strp
= ef
->strtab
+ symp
->st_name
;
826 if (strcmp(name
, strp
) == 0) {
827 if (symp
->st_shndx
!= SHN_UNDEF
||
828 (symp
->st_value
!= 0 &&
829 ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
)
831 *sym
= (c_linker_sym_t
) symp
;
838 symnum
= ef
->chains
[symnum
];
841 /* If we have not found it, look at the full table (if loaded) */
842 if (ef
->symtab
== ef
->ddbsymtab
)
845 /* Exhaustive search */
846 for (i
= 0, symp
= ef
->ddbsymtab
; i
< ef
->ddbsymcnt
; i
++, symp
++) {
847 strp
= ef
->ddbstrtab
+ symp
->st_name
;
848 if (strcmp(name
, strp
) == 0) {
849 if (symp
->st_shndx
!= SHN_UNDEF
||
850 (symp
->st_value
!= 0 &&
851 ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
)) {
852 *sym
= (c_linker_sym_t
) symp
;
863 link_elf_symbol_values(linker_file_t lf
, c_linker_sym_t sym
, linker_symval_t
*symval
)
865 elf_file_t ef
= lf
->priv
;
866 const Elf_Sym
*es
= (const Elf_Sym
*)sym
;
868 if (es
>= ef
->symtab
&& es
< (ef
->symtab
+ ef
->nchains
)) {
869 symval
->name
= ef
->strtab
+ es
->st_name
;
870 symval
->value
= ef
->address
+ es
->st_value
;
871 symval
->size
= es
->st_size
;
874 if (ef
->symtab
== ef
->ddbsymtab
)
876 if (es
>= ef
->ddbsymtab
&& es
< (ef
->ddbsymtab
+ ef
->ddbsymcnt
)) {
877 symval
->name
= ef
->ddbstrtab
+ es
->st_name
;
878 symval
->value
= ef
->address
+ es
->st_value
;
879 symval
->size
= es
->st_size
;
886 link_elf_search_symbol(linker_file_t lf
, caddr_t value
,
887 c_linker_sym_t
*sym
, long *diffp
)
889 elf_file_t ef
= lf
->priv
;
890 u_long off
= (uintptr_t)(void *)value
;
894 const Elf_Sym
*best
= NULL
;
897 for (i
= 0, es
= ef
->ddbsymtab
; i
< ef
->ddbsymcnt
; i
++, es
++) {
898 if (es
->st_name
== 0)
900 st_value
= es
->st_value
+ (uintptr_t)(void *)ef
->address
;
901 if (off
>= st_value
) {
902 if (off
- st_value
< diff
) {
903 diff
= off
- st_value
;
907 } else if (off
- st_value
== diff
) {
916 *sym
= (c_linker_sym_t
) best
;
922 * Look up a linker set on an ELF system.
925 link_elf_lookup_set(linker_file_t lf
, const char *name
,
926 void ***startp
, void ***stopp
, int *countp
)
929 linker_symval_t symval
;
931 void **start
, **stop
;
932 int len
, error
= 0, count
;
934 len
= strlen(name
) + sizeof("__start_set_"); /* sizeof includes \0 */
935 setsym
= kmalloc(len
, M_LINKER
, M_WAITOK
);
937 /* get address of first entry */
938 ksnprintf(setsym
, len
, "%s%s", "__start_set_", name
);
939 error
= link_elf_lookup_symbol(lf
, setsym
, &sym
);
942 link_elf_symbol_values(lf
, sym
, &symval
);
943 if (symval
.value
== NULL
) {
947 start
= (void **)symval
.value
;
949 /* get address of last entry */
950 ksnprintf(setsym
, len
, "%s%s", "__stop_set_", name
);
951 error
= link_elf_lookup_symbol(lf
, setsym
, &sym
);
954 link_elf_symbol_values(lf
, sym
, &symval
);
955 if (symval
.value
== NULL
) {
959 stop
= (void **)symval
.value
;
961 /* and the number of entries */
962 count
= stop
- start
;
973 kfree(setsym
, M_LINKER
);
978 * Symbol lookup function that can be used when the symbol index is known (ie
979 * in relocations). It uses the symbol index instead of doing a fully fledged
980 * hash table based lookup when such is valid. For example for local symbols.
981 * This is not only more efficient, it's also more correct. It's not always
982 * the case that the symbol can be found through the hash table.
985 elf_lookup(linker_file_t lf
, Elf_Size symidx
, int deps
, Elf_Addr
*result
)
987 elf_file_t ef
= lf
->priv
;
991 /* Don't even try to lookup the symbol if the index is bogus. */
992 if (symidx
>= ef
->nchains
)
995 sym
= ef
->symtab
+ symidx
;
998 * Don't do a full lookup when the symbol is local. It may even
999 * fail because it may not be found through the hash table.
1001 if (ELF_ST_BIND(sym
->st_info
) == STB_LOCAL
) {
1002 /* Force lookup failure when we have an insanity. */
1003 if (sym
->st_shndx
== SHN_UNDEF
|| sym
->st_value
== 0)
1005 return ((Elf_Addr
) ef
->address
+ sym
->st_value
);
1008 * XXX we can avoid doing a hash table based lookup for global
1009 * symbols as well. This however is not always valid, so we'll
1010 * just do it the hard way for now. Performance tweaks can
1014 symbol
= ef
->strtab
+ sym
->st_name
;
1016 /* Force a lookup failure if the symbol name is bogus. */
1020 return (linker_file_lookup_symbol(lf
, symbol
, deps
, (caddr_t
*)result
));
1023 link_elf_reloc_local(linker_file_t lf
)
1025 elf_file_t ef
= lf
->priv
;
1026 const Elf_Rel
*rellim
;
1028 const Elf_Rela
*relalim
;
1029 const Elf_Rela
*rela
;
1031 /* Perform relocations without addend if there are any: */
1032 if ((rel
= ef
->rel
) != NULL
) {
1033 rellim
= (const Elf_Rel
*)((const char *)ef
->rel
+ ef
->relsize
);
1034 while (rel
< rellim
) {
1035 elf_reloc_local(lf
, (Elf_Addr
)ef
->address
, rel
, ELF_RELOC_REL
,
1041 /* Perform relocations with addend if there are any: */
1042 if ((rela
= ef
->rela
) != NULL
) {
1043 relalim
= (const Elf_Rela
*)((const char *)ef
->rela
+ ef
->relasize
);
1044 while (rela
< relalim
) {
1045 elf_reloc_local(lf
, (Elf_Addr
)ef
->address
, rela
, ELF_RELOC_RELA
,