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$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <sys/fcntl.h>
47 #include <sys/vnode.h>
48 #include <sys/linker.h>
50 #include <machine/elf.h>
52 #include <security/mac/mac_framework.h>
55 #include <vm/vm_param.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_extern.h>
62 #include <vm/vm_map.h>
64 #include <sys/link_elf.h>
70 #include "linker_if.h"
74 typedef struct elf_file
{
75 struct linker_file lf
; /* Common fields */
76 int preloaded
; /* Was file pre-loaded */
77 caddr_t address
; /* Relocation address */
79 vm_object_t object
; /* VM object to hold file pages */
81 Elf_Dyn
* dynamic
; /* Symbol table etc. */
82 Elf_Hashelt nbuckets
; /* DT_HASH info */
84 const Elf_Hashelt
* buckets
;
85 const Elf_Hashelt
* chains
;
87 caddr_t strtab
; /* DT_STRTAB */
88 int strsz
; /* DT_STRSZ */
89 const Elf_Sym
* symtab
; /* DT_SYMTAB */
90 Elf_Addr
* got
; /* DT_PLTGOT */
91 const Elf_Rel
* pltrel
; /* DT_JMPREL */
92 int pltrelsize
; /* DT_PLTRELSZ */
93 const Elf_Rela
* pltrela
; /* DT_JMPREL */
94 int pltrelasize
; /* DT_PLTRELSZ */
95 const Elf_Rel
* rel
; /* DT_REL */
96 int relsize
; /* DT_RELSZ */
97 const Elf_Rela
* rela
; /* DT_RELA */
98 int relasize
; /* DT_RELASZ */
100 const Elf_Sym
* ddbsymtab
; /* The symbol table we are using */
101 long ddbsymcnt
; /* Number of symbols */
102 caddr_t ddbstrtab
; /* String table */
103 long ddbstrcnt
; /* number of bytes in string table */
104 caddr_t symbase
; /* malloc'ed symbold base */
105 caddr_t strbase
; /* malloc'ed string base */
106 caddr_t ctftab
; /* CTF table */
107 long ctfcnt
; /* number of bytes in CTF table */
108 caddr_t ctfoff
; /* CTF offset table */
109 caddr_t typoff
; /* Type offset table */
110 long typlen
; /* Number of type entries. */
112 struct link_map gdb
; /* hooks for gdb */
116 #include <kern/kern_ctf.c>
118 static int link_elf_link_common_finish(linker_file_t
);
119 static int link_elf_link_preload(linker_class_t cls
,
120 const char*, linker_file_t
*);
121 static int link_elf_link_preload_finish(linker_file_t
);
122 static int link_elf_load_file(linker_class_t
, const char*, linker_file_t
*);
123 static int link_elf_lookup_symbol(linker_file_t
, const char*,
125 static int link_elf_symbol_values(linker_file_t
, c_linker_sym_t
, linker_symval_t
*);
126 static int link_elf_search_symbol(linker_file_t
, caddr_t value
,
127 c_linker_sym_t
* sym
, long* diffp
);
129 static void link_elf_unload_file(linker_file_t
);
130 static void link_elf_unload_preload(linker_file_t
);
131 static int link_elf_lookup_set(linker_file_t
, const char *,
132 void ***, void ***, int *);
133 static int link_elf_each_function_name(linker_file_t
,
134 int (*)(const char *, void *),
136 static int link_elf_each_function_nameval(linker_file_t
,
137 linker_function_nameval_callback_t
,
139 static void link_elf_reloc_local(linker_file_t
);
140 static Elf_Addr
elf_lookup(linker_file_t lf
, Elf_Size symidx
, int deps
);
142 static kobj_method_t link_elf_methods
[] = {
143 KOBJMETHOD(linker_lookup_symbol
, link_elf_lookup_symbol
),
144 KOBJMETHOD(linker_symbol_values
, link_elf_symbol_values
),
145 KOBJMETHOD(linker_search_symbol
, link_elf_search_symbol
),
146 KOBJMETHOD(linker_unload
, link_elf_unload_file
),
147 KOBJMETHOD(linker_load_file
, link_elf_load_file
),
148 KOBJMETHOD(linker_link_preload
, link_elf_link_preload
),
149 KOBJMETHOD(linker_link_preload_finish
, link_elf_link_preload_finish
),
150 KOBJMETHOD(linker_lookup_set
, link_elf_lookup_set
),
151 KOBJMETHOD(linker_each_function_name
, link_elf_each_function_name
),
152 KOBJMETHOD(linker_each_function_nameval
, link_elf_each_function_nameval
),
153 KOBJMETHOD(linker_ctf_get
, link_elf_ctf_get
),
157 static struct linker_class link_elf_class
= {
158 #if ELF_TARG_CLASS == ELFCLASS32
163 link_elf_methods
, sizeof(struct elf_file
)
166 static int parse_dynamic(elf_file_t ef
);
167 static int relocate_file(elf_file_t ef
);
168 static int link_elf_preload_parse_symbols(elf_file_t ef
);
171 static void r_debug_state(struct r_debug
*dummy_one
,
172 struct link_map
*dummy_two
);
175 * A list of loaded modules for GDB to use for loading symbols.
177 struct r_debug r_debug
;
179 #define GDB_STATE(s) r_debug.r_state = s; r_debug_state(NULL, NULL);
182 * Function for the debugger to set a breakpoint on to gain control.
185 r_debug_state(struct r_debug
*dummy_one __unused
,
186 struct link_map
*dummy_two __unused
)
191 link_elf_add_gdb(struct link_map
*l
)
193 struct link_map
*prev
;
197 if (r_debug
.r_map
== NULL
) {
202 /* Append to list. */
203 for (prev
= r_debug
.r_map
; prev
->l_next
!= NULL
; prev
= prev
->l_next
)
211 link_elf_delete_gdb(struct link_map
*l
)
213 if (l
->l_prev
== NULL
) {
215 if ((r_debug
.r_map
= l
->l_next
) != NULL
)
216 l
->l_next
->l_prev
= NULL
;
218 /* Remove any but first. */
219 if ((l
->l_prev
->l_next
= l
->l_next
) != NULL
)
220 l
->l_next
->l_prev
= l
->l_prev
;
226 Elf_Addr
link_elf_get_gp(linker_file_t
);
230 * The kernel symbol table starts here.
232 extern struct _dynamic _DYNAMIC
;
235 link_elf_error(const char *filename
, const char *s
)
237 if (filename
== NULL
)
238 printf("kldload: %s\n", s
);
240 printf("kldload: %s: %s\n", filename
, s
);
244 * Actions performed after linking/loading both the preloaded kernel and any
245 * modules; whether preloaded or dynamicly loaded.
248 link_elf_link_common_finish(linker_file_t lf
)
251 elf_file_t ef
= (elf_file_t
)lf
;
256 /* Notify MD code that a module is being loaded. */
257 error
= elf_cpu_load_file(lf
);
263 ef
->gdb
.l_addr
= lf
->address
;
264 newfilename
= malloc(strlen(lf
->filename
) + 1, M_LINKER
, M_WAITOK
);
265 strcpy(newfilename
, lf
->filename
);
266 ef
->gdb
.l_name
= newfilename
;
267 ef
->gdb
.l_ld
= ef
->dynamic
;
268 link_elf_add_gdb(&ef
->gdb
);
269 GDB_STATE(RT_CONSISTENT
);
276 link_elf_init(void* arg
)
279 caddr_t modptr
, baseptr
, sizeptr
;
283 linker_add_class(&link_elf_class
);
285 dp
= (Elf_Dyn
*) &_DYNAMIC
;
287 modptr
= preload_search_by_type("elf" __XSTRING(__ELF_WORD_SIZE
) " kernel");
289 modptr
= preload_search_by_type("elf kernel");
291 modname
= (char *)preload_search_info(modptr
, MODINFO_NAME
);
294 linker_kernel_file
= linker_make_file(modname
, &link_elf_class
);
295 if (linker_kernel_file
== NULL
)
296 panic("link_elf_init: Can't create linker structures for kernel");
298 ef
= (elf_file_t
) linker_kernel_file
;
301 #ifdef SPARSE_MAPPING
308 linker_kernel_file
->address
= (caddr_t
) KERNBASE
;
309 linker_kernel_file
->size
= -(intptr_t)linker_kernel_file
->address
;
313 baseptr
= preload_search_info(modptr
, MODINFO_ADDR
);
315 linker_kernel_file
->address
= *(caddr_t
*)baseptr
;
316 sizeptr
= preload_search_info(modptr
, MODINFO_SIZE
);
318 linker_kernel_file
->size
= *(size_t *)sizeptr
;
320 (void)link_elf_preload_parse_symbols(ef
);
323 r_debug
.r_map
= NULL
;
324 r_debug
.r_brk
= r_debug_state
;
325 r_debug
.r_state
= RT_CONSISTENT
;
328 (void)link_elf_link_common_finish(linker_kernel_file
);
329 linker_kernel_file
->flags
|= LINKER_FILE_LINKED
;
332 SYSINIT(link_elf
, SI_SUB_KLD
, SI_ORDER_THIRD
, link_elf_init
, 0);
335 link_elf_preload_parse_symbols(elf_file_t ef
)
338 caddr_t ssym
, esym
, base
;
344 if (ef
->modptr
== NULL
)
346 pointer
= preload_search_info(ef
->modptr
, MODINFO_METADATA
|MODINFOMD_SSYM
);
349 ssym
= *(caddr_t
*)pointer
;
350 pointer
= preload_search_info(ef
->modptr
, MODINFO_METADATA
|MODINFOMD_ESYM
);
353 esym
= *(caddr_t
*)pointer
;
357 symcnt
= *(long *)base
;
358 base
+= sizeof(long);
359 symtab
= (Elf_Sym
*)base
;
360 base
+= roundup(symcnt
, sizeof(long));
362 if (base
> esym
|| base
< ssym
) {
363 printf("Symbols are corrupt!\n");
367 strcnt
= *(long *)base
;
368 base
+= sizeof(long);
370 base
+= roundup(strcnt
, sizeof(long));
372 if (base
> esym
|| base
< ssym
) {
373 printf("Symbols are corrupt!\n");
377 ef
->ddbsymtab
= symtab
;
378 ef
->ddbsymcnt
= symcnt
/ sizeof(Elf_Sym
);
379 ef
->ddbstrtab
= strtab
;
380 ef
->ddbstrcnt
= strcnt
;
386 parse_dynamic(elf_file_t ef
)
389 int plttype
= DT_REL
;
391 for (dp
= ef
->dynamic
; dp
->d_tag
!= DT_NULL
; dp
++) {
395 /* From src/libexec/rtld-elf/rtld.c */
396 const Elf_Hashelt
*hashtab
= (const Elf_Hashelt
*)
397 (ef
->address
+ dp
->d_un
.d_ptr
);
398 ef
->nbuckets
= hashtab
[0];
399 ef
->nchains
= hashtab
[1];
400 ef
->buckets
= hashtab
+ 2;
401 ef
->chains
= ef
->buckets
+ ef
->nbuckets
;
405 ef
->strtab
= (caddr_t
) (ef
->address
+ dp
->d_un
.d_ptr
);
408 ef
->strsz
= dp
->d_un
.d_val
;
411 ef
->symtab
= (Elf_Sym
*) (ef
->address
+ dp
->d_un
.d_ptr
);
414 if (dp
->d_un
.d_val
!= sizeof(Elf_Sym
))
418 ef
->got
= (Elf_Addr
*) (ef
->address
+ dp
->d_un
.d_ptr
);
421 ef
->rel
= (const Elf_Rel
*) (ef
->address
+ dp
->d_un
.d_ptr
);
424 ef
->relsize
= dp
->d_un
.d_val
;
427 if (dp
->d_un
.d_val
!= sizeof(Elf_Rel
))
431 ef
->pltrel
= (const Elf_Rel
*) (ef
->address
+ dp
->d_un
.d_ptr
);
434 ef
->pltrelsize
= dp
->d_un
.d_val
;
437 ef
->rela
= (const Elf_Rela
*) (ef
->address
+ dp
->d_un
.d_ptr
);
440 ef
->relasize
= dp
->d_un
.d_val
;
443 if (dp
->d_un
.d_val
!= sizeof(Elf_Rela
))
447 plttype
= dp
->d_un
.d_val
;
448 if (plttype
!= DT_REL
&& plttype
!= DT_RELA
)
453 dp
->d_un
.d_ptr
= (Elf_Addr
) &r_debug
;
459 if (plttype
== DT_RELA
) {
460 ef
->pltrela
= (const Elf_Rela
*) ef
->pltrel
;
462 ef
->pltrelasize
= ef
->pltrelsize
;
466 ef
->ddbsymtab
= ef
->symtab
;
467 ef
->ddbsymcnt
= ef
->nchains
;
468 ef
->ddbstrtab
= ef
->strtab
;
469 ef
->ddbstrcnt
= ef
->strsz
;
475 link_elf_link_preload(linker_class_t cls
,
476 const char* filename
, linker_file_t
*result
)
478 caddr_t modptr
, baseptr
, sizeptr
, dynptr
;
485 /* Look to see if we have the file preloaded */
486 modptr
= preload_search_by_name(filename
);
490 type
= (char *)preload_search_info(modptr
, MODINFO_TYPE
);
491 baseptr
= preload_search_info(modptr
, MODINFO_ADDR
);
492 sizeptr
= preload_search_info(modptr
, MODINFO_SIZE
);
493 dynptr
= preload_search_info(modptr
, MODINFO_METADATA
|MODINFOMD_DYNAMIC
);
495 (strcmp(type
, "elf" __XSTRING(__ELF_WORD_SIZE
) " module") != 0 &&
496 strcmp(type
, "elf module") != 0))
498 if (baseptr
== NULL
|| sizeptr
== NULL
|| dynptr
== NULL
)
501 lf
= linker_make_file(filename
, &link_elf_class
);
506 ef
= (elf_file_t
) lf
;
509 ef
->address
= *(caddr_t
*)baseptr
;
510 #ifdef SPARSE_MAPPING
513 dp
= (vm_offset_t
)ef
->address
+ *(vm_offset_t
*)dynptr
;
514 ef
->dynamic
= (Elf_Dyn
*)dp
;
515 lf
->address
= ef
->address
;
516 lf
->size
= *(size_t *)sizeptr
;
518 error
= parse_dynamic(ef
);
520 linker_file_unload(lf
, LINKER_UNLOAD_FORCE
);
523 link_elf_reloc_local(lf
);
529 link_elf_link_preload_finish(linker_file_t lf
)
534 ef
= (elf_file_t
) lf
;
535 #if 0 /* this will be more trouble than it's worth for now */
536 for (dp
= ef
->dynamic
; dp
->d_tag
!= DT_NULL
; dp
++) {
537 if (dp
->d_tag
!= DT_NEEDED
)
539 modname
= ef
->strtab
+ dp
->d_un
.d_val
;
540 error
= linker_load_module(modname
, lf
);
545 error
= relocate_file(ef
);
548 (void)link_elf_preload_parse_symbols(ef
);
550 return (link_elf_link_common_finish(lf
));
554 link_elf_load_file(linker_class_t cls
, const char* filename
,
555 linker_file_t
* result
)
558 struct thread
* td
= curthread
; /* XXX */
564 Elf_Phdr
*segs
[MAXSEGS
];
572 Elf_Addr base_vlimit
;
587 NDINIT(&nd
, LOOKUP
, FOLLOW
| MPSAFE
, UIO_SYSSPACE
, filename
, td
);
589 error
= vn_open(&nd
, &flags
, 0, NULL
);
592 vfslocked
= NDHASGIANT(&nd
);
593 NDFREE(&nd
, NDF_ONLY_PNBUF
);
594 if (nd
.ni_vp
->v_type
!= VREG
) {
600 error
= mac_kld_check_load(curthread
->td_ucred
, nd
.ni_vp
);
608 * Read the elf header from the file.
610 firstpage
= malloc(PAGE_SIZE
, M_LINKER
, M_WAITOK
);
611 if (firstpage
== NULL
) {
615 hdr
= (Elf_Ehdr
*)firstpage
;
616 error
= vn_rdwr(UIO_READ
, nd
.ni_vp
, firstpage
, PAGE_SIZE
, 0,
617 UIO_SYSSPACE
, IO_NODELOCKED
, td
->td_ucred
, NOCRED
,
619 nbytes
= PAGE_SIZE
- resid
;
628 if (hdr
->e_ident
[EI_CLASS
] != ELF_TARG_CLASS
629 || hdr
->e_ident
[EI_DATA
] != ELF_TARG_DATA
) {
630 link_elf_error(filename
, "Unsupported file layout");
634 if (hdr
->e_ident
[EI_VERSION
] != EV_CURRENT
635 || hdr
->e_version
!= EV_CURRENT
) {
636 link_elf_error(filename
, "Unsupported file version");
640 if (hdr
->e_type
!= ET_EXEC
&& hdr
->e_type
!= ET_DYN
) {
641 link_elf_error(filename
, "Unsupported file type");
645 if (hdr
->e_machine
!= ELF_TARG_MACH
) {
646 link_elf_error(filename
, "Unsupported machine");
652 * We rely on the program header being in the first page. This is
653 * not strictly required by the ABI specification, but it seems to
654 * always true in practice. And, it simplifies things considerably.
656 if (!((hdr
->e_phentsize
== sizeof(Elf_Phdr
)) &&
657 (hdr
->e_phoff
+ hdr
->e_phnum
*sizeof(Elf_Phdr
) <= PAGE_SIZE
) &&
658 (hdr
->e_phoff
+ hdr
->e_phnum
*sizeof(Elf_Phdr
) <= nbytes
)))
659 link_elf_error(filename
, "Unreadable program headers");
662 * Scan the program header entries, and save key information.
664 * We rely on there being exactly two load segments, text and data,
667 phdr
= (Elf_Phdr
*) (firstpage
+ hdr
->e_phoff
);
668 phlimit
= phdr
+ hdr
->e_phnum
;
672 while (phdr
< phlimit
) {
673 switch (phdr
->p_type
) {
676 if (nsegs
== MAXSEGS
) {
677 link_elf_error(filename
, "Too many sections");
682 * XXX: We just trust they come in right order ??
697 link_elf_error(filename
, "Unsupported file type");
705 link_elf_error(filename
, "Object is not dynamically-linked");
710 link_elf_error(filename
, "No sections");
716 * Allocate the entire address space of the object, to stake out our
717 * contiguous region, and to establish the base address for relocation.
719 base_offset
= trunc_page(segs
[0]->p_offset
);
720 base_vaddr
= trunc_page(segs
[0]->p_vaddr
);
721 base_vlimit
= round_page(segs
[nsegs
- 1]->p_vaddr
+
722 segs
[nsegs
- 1]->p_memsz
);
723 mapsize
= base_vlimit
- base_vaddr
;
725 lf
= linker_make_file(filename
, &link_elf_class
);
731 ef
= (elf_file_t
) lf
;
732 #ifdef SPARSE_MAPPING
733 ef
->object
= vm_object_allocate(OBJT_DEFAULT
, mapsize
>> PAGE_SHIFT
);
734 if (ef
->object
== NULL
) {
738 ef
->address
= (caddr_t
) vm_map_min(kernel_map
);
739 error
= vm_map_find(kernel_map
, ef
->object
, 0,
740 (vm_offset_t
*) &ef
->address
,
742 VM_PROT_ALL
, VM_PROT_ALL
, 0);
744 vm_object_deallocate(ef
->object
);
749 ef
->address
= malloc(mapsize
, M_LINKER
, M_WAITOK
);
755 mapbase
= ef
->address
;
758 * Read the text and data sections and zero the bss.
760 for (i
= 0; i
< nsegs
; i
++) {
761 caddr_t segbase
= mapbase
+ segs
[i
]->p_vaddr
- base_vaddr
;
762 error
= vn_rdwr(UIO_READ
, nd
.ni_vp
,
763 segbase
, segs
[i
]->p_filesz
, segs
[i
]->p_offset
,
764 UIO_SYSSPACE
, IO_NODELOCKED
, td
->td_ucred
, NOCRED
,
769 bzero(segbase
+ segs
[i
]->p_filesz
,
770 segs
[i
]->p_memsz
- segs
[i
]->p_filesz
);
772 #ifdef SPARSE_MAPPING
774 * Wire down the pages
776 error
= vm_map_wire(kernel_map
,
777 (vm_offset_t
) segbase
,
778 (vm_offset_t
) segbase
+ segs
[i
]->p_memsz
,
779 VM_MAP_WIRE_SYSTEM
|VM_MAP_WIRE_NOHOLES
);
780 if (error
!= KERN_SUCCESS
) {
788 /* Update profiling information with the new text segment. */
790 kmupetext((uintfptr_t
)(mapbase
+ segs
[0]->p_vaddr
- base_vaddr
+
795 ef
->dynamic
= (Elf_Dyn
*) (mapbase
+ phdyn
->p_vaddr
- base_vaddr
);
797 lf
->address
= ef
->address
;
800 error
= parse_dynamic(ef
);
803 link_elf_reloc_local(lf
);
805 VOP_UNLOCK(nd
.ni_vp
, 0);
806 error
= linker_load_dependencies(lf
);
807 vn_lock(nd
.ni_vp
, LK_EXCLUSIVE
| LK_RETRY
);
810 #if 0 /* this will be more trouble than it's worth for now */
811 for (dp
= ef
->dynamic
; dp
->d_tag
!= DT_NULL
; dp
++) {
812 if (dp
->d_tag
!= DT_NEEDED
)
814 modname
= ef
->strtab
+ dp
->d_un
.d_val
;
815 error
= linker_load_module(modname
, lf
);
820 error
= relocate_file(ef
);
824 /* Try and load the symbol table if it's present. (you can strip it!) */
825 nbytes
= hdr
->e_shnum
* hdr
->e_shentsize
;
826 if (nbytes
== 0 || hdr
->e_shoff
== 0)
828 shdr
= malloc(nbytes
, M_LINKER
, M_WAITOK
| M_ZERO
);
833 error
= vn_rdwr(UIO_READ
, nd
.ni_vp
,
834 (caddr_t
)shdr
, nbytes
, hdr
->e_shoff
,
835 UIO_SYSSPACE
, IO_NODELOCKED
, td
->td_ucred
, NOCRED
,
841 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
842 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
844 symstrindex
= shdr
[i
].sh_link
;
847 if (symtabindex
< 0 || symstrindex
< 0)
850 symcnt
= shdr
[symtabindex
].sh_size
;
851 ef
->symbase
= malloc(symcnt
, M_LINKER
, M_WAITOK
);
852 strcnt
= shdr
[symstrindex
].sh_size
;
853 ef
->strbase
= malloc(strcnt
, M_LINKER
, M_WAITOK
);
855 if (ef
->symbase
== NULL
|| ef
->strbase
== NULL
) {
859 error
= vn_rdwr(UIO_READ
, nd
.ni_vp
,
860 ef
->symbase
, symcnt
, shdr
[symtabindex
].sh_offset
,
861 UIO_SYSSPACE
, IO_NODELOCKED
, td
->td_ucred
, NOCRED
,
865 error
= vn_rdwr(UIO_READ
, nd
.ni_vp
,
866 ef
->strbase
, strcnt
, shdr
[symstrindex
].sh_offset
,
867 UIO_SYSSPACE
, IO_NODELOCKED
, td
->td_ucred
, NOCRED
,
872 ef
->ddbsymcnt
= symcnt
/ sizeof(Elf_Sym
);
873 ef
->ddbsymtab
= (const Elf_Sym
*)ef
->symbase
;
874 ef
->ddbstrcnt
= strcnt
;
875 ef
->ddbstrtab
= ef
->strbase
;
877 error
= link_elf_link_common_finish(lf
);
887 linker_file_unload(lf
, LINKER_UNLOAD_FORCE
);
889 free(shdr
, M_LINKER
);
891 free(firstpage
, M_LINKER
);
892 VOP_UNLOCK(nd
.ni_vp
, 0);
893 vn_close(nd
.ni_vp
, FREAD
, td
->td_ucred
, td
);
894 VFS_UNLOCK_GIANT(vfslocked
);
900 link_elf_unload_file(linker_file_t file
)
902 elf_file_t ef
= (elf_file_t
) file
;
906 GDB_STATE(RT_DELETE
);
907 free((void *)(uintptr_t)ef
->gdb
.l_name
, M_LINKER
);
908 link_elf_delete_gdb(&ef
->gdb
);
909 GDB_STATE(RT_CONSISTENT
);
913 /* Notify MD code that a module is being unloaded. */
914 elf_cpu_unload_file(file
);
917 link_elf_unload_preload(file
);
921 #ifdef SPARSE_MAPPING
923 vm_map_remove(kernel_map
, (vm_offset_t
) ef
->address
,
924 (vm_offset_t
) ef
->address
925 + (ef
->object
->size
<< PAGE_SHIFT
));
929 free(ef
->address
, M_LINKER
);
932 free(ef
->symbase
, M_LINKER
);
934 free(ef
->strbase
, M_LINKER
);
936 free(ef
->ctftab
, M_LINKER
);
938 free(ef
->ctfoff
, M_LINKER
);
940 free(ef
->typoff
, M_LINKER
);
944 link_elf_unload_preload(linker_file_t file
)
947 preload_delete_name(file
->filename
);
951 symbol_name(elf_file_t ef
, Elf_Size r_info
)
955 if (ELF_R_SYM(r_info
)) {
956 ref
= ef
->symtab
+ ELF_R_SYM(r_info
);
957 return ef
->strtab
+ ref
->st_name
;
963 relocate_file(elf_file_t ef
)
965 const Elf_Rel
*rellim
;
967 const Elf_Rela
*relalim
;
968 const Elf_Rela
*rela
;
971 /* Perform relocations without addend if there are any: */
974 rellim
= (const Elf_Rel
*)((const char *)ef
->rel
+ ef
->relsize
);
975 while (rel
< rellim
) {
976 if (elf_reloc(&ef
->lf
, (Elf_Addr
)ef
->address
, rel
, ELF_RELOC_REL
,
978 symname
= symbol_name(ef
, rel
->r_info
);
979 printf("link_elf: symbol %s undefined\n", symname
);
986 /* Perform relocations with addend if there are any: */
989 relalim
= (const Elf_Rela
*)((const char *)ef
->rela
+ ef
->relasize
);
990 while (rela
< relalim
) {
991 if (elf_reloc(&ef
->lf
, (Elf_Addr
)ef
->address
, rela
, ELF_RELOC_RELA
,
993 symname
= symbol_name(ef
, rela
->r_info
);
994 printf("link_elf: symbol %s undefined\n", symname
);
1001 /* Perform PLT relocations without addend if there are any: */
1004 rellim
= (const Elf_Rel
*)((const char *)ef
->pltrel
+ ef
->pltrelsize
);
1005 while (rel
< rellim
) {
1006 if (elf_reloc(&ef
->lf
, (Elf_Addr
)ef
->address
, rel
, ELF_RELOC_REL
,
1008 symname
= symbol_name(ef
, rel
->r_info
);
1009 printf("link_elf: symbol %s undefined\n", symname
);
1016 /* Perform relocations with addend if there are any: */
1019 relalim
= (const Elf_Rela
*)((const char *)ef
->pltrela
+ ef
->pltrelasize
);
1020 while (rela
< relalim
) {
1021 if (elf_reloc(&ef
->lf
, (Elf_Addr
)ef
->address
, rela
, ELF_RELOC_RELA
,
1023 symname
= symbol_name(ef
, rela
->r_info
);
1024 printf("link_elf: symbol %s undefined\n", symname
);
1035 * Hash function for symbol table lookup. Don't even think about changing
1036 * this. It is specified by the System V ABI.
1038 static unsigned long
1039 elf_hash(const char *name
)
1041 const unsigned char *p
= (const unsigned char *) name
;
1042 unsigned long h
= 0;
1045 while (*p
!= '\0') {
1046 h
= (h
<< 4) + *p
++;
1047 if ((g
= h
& 0xf0000000) != 0)
1055 link_elf_lookup_symbol(linker_file_t lf
, const char* name
, c_linker_sym_t
* sym
)
1057 elf_file_t ef
= (elf_file_t
) lf
;
1058 unsigned long symnum
;
1059 const Elf_Sym
* symp
;
1064 /* If we don't have a hash, bail. */
1065 if (ef
->buckets
== NULL
|| ef
->nbuckets
== 0) {
1066 printf("link_elf_lookup_symbol: missing symbol hash table\n");
1070 /* First, search hashed global symbols */
1071 hash
= elf_hash(name
);
1072 symnum
= ef
->buckets
[hash
% ef
->nbuckets
];
1074 while (symnum
!= STN_UNDEF
) {
1075 if (symnum
>= ef
->nchains
) {
1076 printf("link_elf_lookup_symbol: corrupt symbol table\n");
1080 symp
= ef
->symtab
+ symnum
;
1081 if (symp
->st_name
== 0) {
1082 printf("link_elf_lookup_symbol: corrupt symbol table\n");
1086 strp
= ef
->strtab
+ symp
->st_name
;
1088 if (strcmp(name
, strp
) == 0) {
1089 if (symp
->st_shndx
!= SHN_UNDEF
||
1090 (symp
->st_value
!= 0 &&
1091 ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
)) {
1092 *sym
= (c_linker_sym_t
) symp
;
1098 symnum
= ef
->chains
[symnum
];
1101 /* If we have not found it, look at the full table (if loaded) */
1102 if (ef
->symtab
== ef
->ddbsymtab
)
1105 /* Exhaustive search */
1106 for (i
= 0, symp
= ef
->ddbsymtab
; i
< ef
->ddbsymcnt
; i
++, symp
++) {
1107 strp
= ef
->ddbstrtab
+ symp
->st_name
;
1108 if (strcmp(name
, strp
) == 0) {
1109 if (symp
->st_shndx
!= SHN_UNDEF
||
1110 (symp
->st_value
!= 0 &&
1111 ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
)) {
1112 *sym
= (c_linker_sym_t
) symp
;
1123 link_elf_symbol_values(linker_file_t lf
, c_linker_sym_t sym
, linker_symval_t
* symval
)
1125 elf_file_t ef
= (elf_file_t
) lf
;
1126 const Elf_Sym
* es
= (const Elf_Sym
*) sym
;
1128 if (es
>= ef
->symtab
&& es
< (ef
->symtab
+ ef
->nchains
)) {
1129 symval
->name
= ef
->strtab
+ es
->st_name
;
1130 symval
->value
= (caddr_t
) ef
->address
+ es
->st_value
;
1131 symval
->size
= es
->st_size
;
1134 if (ef
->symtab
== ef
->ddbsymtab
)
1136 if (es
>= ef
->ddbsymtab
&& es
< (ef
->ddbsymtab
+ ef
->ddbsymcnt
)) {
1137 symval
->name
= ef
->ddbstrtab
+ es
->st_name
;
1138 symval
->value
= (caddr_t
) ef
->address
+ es
->st_value
;
1139 symval
->size
= es
->st_size
;
1146 link_elf_search_symbol(linker_file_t lf
, caddr_t value
,
1147 c_linker_sym_t
* sym
, long* diffp
)
1149 elf_file_t ef
= (elf_file_t
) lf
;
1150 u_long off
= (uintptr_t) (void *) value
;
1154 const Elf_Sym
* best
= 0;
1157 for (i
= 0, es
= ef
->ddbsymtab
; i
< ef
->ddbsymcnt
; i
++, es
++) {
1158 if (es
->st_name
== 0)
1160 st_value
= es
->st_value
+ (uintptr_t) (void *) ef
->address
;
1161 if (off
>= st_value
) {
1162 if (off
- st_value
< diff
) {
1163 diff
= off
- st_value
;
1167 } else if (off
- st_value
== diff
) {
1176 *sym
= (c_linker_sym_t
) best
;
1182 * Look up a linker set on an ELF system.
1185 link_elf_lookup_set(linker_file_t lf
, const char *name
,
1186 void ***startp
, void ***stopp
, int *countp
)
1189 linker_symval_t symval
;
1191 void **start
, **stop
;
1192 int len
, error
= 0, count
;
1194 len
= strlen(name
) + sizeof("__start_set_"); /* sizeof includes \0 */
1195 setsym
= malloc(len
, M_LINKER
, M_WAITOK
);
1199 /* get address of first entry */
1200 snprintf(setsym
, len
, "%s%s", "__start_set_", name
);
1201 error
= link_elf_lookup_symbol(lf
, setsym
, &sym
);
1204 link_elf_symbol_values(lf
, sym
, &symval
);
1205 if (symval
.value
== 0) {
1209 start
= (void **)symval
.value
;
1211 /* get address of last entry */
1212 snprintf(setsym
, len
, "%s%s", "__stop_set_", name
);
1213 error
= link_elf_lookup_symbol(lf
, setsym
, &sym
);
1216 link_elf_symbol_values(lf
, sym
, &symval
);
1217 if (symval
.value
== 0) {
1221 stop
= (void **)symval
.value
;
1223 /* and the number of entries */
1224 count
= stop
- start
;
1235 free(setsym
, M_LINKER
);
1240 link_elf_each_function_name(linker_file_t file
,
1241 int (*callback
)(const char *, void *), void *opaque
) {
1242 elf_file_t ef
= (elf_file_t
)file
;
1243 const Elf_Sym
* symp
;
1246 /* Exhaustive search */
1247 for (i
= 0, symp
= ef
->ddbsymtab
; i
< ef
->ddbsymcnt
; i
++, symp
++) {
1248 if (symp
->st_value
!= 0 &&
1249 ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
) {
1250 error
= callback(ef
->ddbstrtab
+ symp
->st_name
, opaque
);
1259 link_elf_each_function_nameval(linker_file_t file
,
1260 linker_function_nameval_callback_t callback
, void *opaque
)
1262 linker_symval_t symval
;
1263 elf_file_t ef
= (elf_file_t
)file
;
1264 const Elf_Sym
* symp
;
1267 /* Exhaustive search */
1268 for (i
= 0, symp
= ef
->ddbsymtab
; i
< ef
->ddbsymcnt
; i
++, symp
++) {
1269 if (symp
->st_value
!= 0 &&
1270 ELF_ST_TYPE(symp
->st_info
) == STT_FUNC
) {
1271 error
= link_elf_symbol_values(file
, (c_linker_sym_t
) symp
, &symval
);
1274 error
= callback(file
, i
, &symval
, opaque
);
1284 * Each KLD has its own GP. The GP value for each load module is given by
1285 * DT_PLTGOT on ia64. We need GP to construct function descriptors, but
1286 * don't have direct access to the ELF file structure. The link_elf_get_gp()
1287 * function returns the GP given a pointer to a generic linker file struct.
1290 link_elf_get_gp(linker_file_t lf
)
1292 elf_file_t ef
= (elf_file_t
)lf
;
1293 return (Elf_Addr
)ef
->got
;
1298 elf_get_sym(linker_file_t lf
, Elf_Size symidx
)
1300 elf_file_t ef
= (elf_file_t
)lf
;
1302 if (symidx
>= ef
->nchains
)
1304 return (ef
->symtab
+ symidx
);
1308 elf_get_symname(linker_file_t lf
, Elf_Size symidx
)
1310 elf_file_t ef
= (elf_file_t
)lf
;
1313 if (symidx
>= ef
->nchains
)
1315 sym
= ef
->symtab
+ symidx
;
1316 return (ef
->strtab
+ sym
->st_name
);
1320 * Symbol lookup function that can be used when the symbol index is known (ie
1321 * in relocations). It uses the symbol index instead of doing a fully fledged
1322 * hash table based lookup when such is valid. For example for local symbols.
1323 * This is not only more efficient, it's also more correct. It's not always
1324 * the case that the symbol can be found through the hash table.
1327 elf_lookup(linker_file_t lf
, Elf_Size symidx
, int deps
)
1329 elf_file_t ef
= (elf_file_t
)lf
;
1333 /* Don't even try to lookup the symbol if the index is bogus. */
1334 if (symidx
>= ef
->nchains
)
1337 sym
= ef
->symtab
+ symidx
;
1340 * Don't do a full lookup when the symbol is local. It may even
1341 * fail because it may not be found through the hash table.
1343 if (ELF_ST_BIND(sym
->st_info
) == STB_LOCAL
) {
1344 /* Force lookup failure when we have an insanity. */
1345 if (sym
->st_shndx
== SHN_UNDEF
|| sym
->st_value
== 0)
1347 return ((Elf_Addr
)ef
->address
+ sym
->st_value
);
1351 * XXX we can avoid doing a hash table based lookup for global
1352 * symbols as well. This however is not always valid, so we'll
1353 * just do it the hard way for now. Performance tweaks can
1357 symbol
= ef
->strtab
+ sym
->st_name
;
1359 /* Force a lookup failure if the symbol name is bogus. */
1363 return ((Elf_Addr
)linker_file_lookup_symbol(lf
, symbol
, deps
));
1367 link_elf_reloc_local(linker_file_t lf
)
1369 const Elf_Rel
*rellim
;
1371 const Elf_Rela
*relalim
;
1372 const Elf_Rela
*rela
;
1373 elf_file_t ef
= (elf_file_t
)lf
;
1375 /* Perform relocations without addend if there are any: */
1376 if ((rel
= ef
->rel
) != NULL
) {
1377 rellim
= (const Elf_Rel
*)((const char *)ef
->rel
+ ef
->relsize
);
1378 while (rel
< rellim
) {
1379 elf_reloc_local(lf
, (Elf_Addr
)ef
->address
, rel
, ELF_RELOC_REL
,
1385 /* Perform relocations with addend if there are any: */
1386 if ((rela
= ef
->rela
) != NULL
) {
1387 relalim
= (const Elf_Rela
*)((const char *)ef
->rela
+ ef
->relasize
);
1388 while (rela
< relalim
) {
1389 elf_reloc_local(lf
, (Elf_Addr
)ef
->address
, rela
, ELF_RELOC_RELA
,