xref: /freebsd/sys/kern/link_elf.c (revision 77b7cdf1999ee965ad494fddd184b18f532ac91a)
1 /*-
2  * Copyright (c) 1998-2000 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include "opt_ddb.h"
30 #include "opt_mac.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/mac.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 #include <sys/namei.h>
41 #include <sys/fcntl.h>
42 #include <sys/vnode.h>
43 #include <sys/linker.h>
44 
45 #include <machine/elf.h>
46 #ifdef GPROF
47 #include <machine/profile.h>
48 #endif
49 
50 #include <vm/vm.h>
51 #include <vm/vm_param.h>
52 #ifdef SPARSE_MAPPING
53 #include <vm/vm_object.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_extern.h>
56 #endif
57 #include <vm/pmap.h>
58 #include <vm/vm_map.h>
59 
60 #include <sys/link_elf.h>
61 
62 #include "linker_if.h"
63 
64 typedef struct elf_file {
65     struct linker_file	lf;		/* Common fields */
66     int			preloaded;	/* Was file pre-loaded */
67     caddr_t		address;	/* Relocation address */
68 #ifdef SPARSE_MAPPING
69     vm_object_t		object;		/* VM object to hold file pages */
70 #endif
71     Elf_Dyn*		dynamic;	/* Symbol table etc. */
72     Elf_Hashelt		nbuckets;	/* DT_HASH info */
73     Elf_Hashelt		nchains;
74     const Elf_Hashelt*	buckets;
75     const Elf_Hashelt*	chains;
76     caddr_t		hash;
77     caddr_t		strtab;		/* DT_STRTAB */
78     int			strsz;		/* DT_STRSZ */
79     const Elf_Sym*	symtab;		/* DT_SYMTAB */
80     Elf_Addr*		got;		/* DT_PLTGOT */
81     const Elf_Rel*	pltrel;		/* DT_JMPREL */
82     int			pltrelsize;	/* DT_PLTRELSZ */
83     const Elf_Rela*	pltrela;	/* DT_JMPREL */
84     int			pltrelasize;	/* DT_PLTRELSZ */
85     const Elf_Rel*	rel;		/* DT_REL */
86     int			relsize;	/* DT_RELSZ */
87     const Elf_Rela*	rela;		/* DT_RELA */
88     int			relasize;	/* DT_RELASZ */
89     caddr_t		modptr;
90     const Elf_Sym*	ddbsymtab;	/* The symbol table we are using */
91     long		ddbsymcnt;	/* Number of symbols */
92     caddr_t		ddbstrtab;	/* String table */
93     long		ddbstrcnt;	/* number of bytes in string table */
94     caddr_t		symbase;	/* malloc'ed symbold base */
95     caddr_t		strbase;	/* malloc'ed string base */
96 #ifdef DDB
97     struct link_map	gdb;		/* hooks for gdb */
98 #endif
99 } *elf_file_t;
100 
101 static int	link_elf_link_common_finish(linker_file_t);
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*,
107 				       c_linker_sym_t*);
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);
111 
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 *),
118 				void *);
119 static void	link_elf_reloc_local(linker_file_t);
120 
121 static kobj_method_t link_elf_methods[] = {
122     KOBJMETHOD(linker_lookup_symbol,	link_elf_lookup_symbol),
123     KOBJMETHOD(linker_symbol_values,	link_elf_symbol_values),
124     KOBJMETHOD(linker_search_symbol,	link_elf_search_symbol),
125     KOBJMETHOD(linker_unload,		link_elf_unload_file),
126     KOBJMETHOD(linker_load_file,	link_elf_load_file),
127     KOBJMETHOD(linker_link_preload,	link_elf_link_preload),
128     KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
129     KOBJMETHOD(linker_lookup_set,	link_elf_lookup_set),
130     KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
131     { 0, 0 }
132 };
133 
134 static struct linker_class link_elf_class = {
135 #if ELF_TARG_CLASS == ELFCLASS32
136     "elf32",
137 #else
138     "elf64",
139 #endif
140     link_elf_methods, sizeof(struct elf_file)
141 };
142 
143 static int		parse_dynamic(elf_file_t ef);
144 static int		relocate_file(elf_file_t ef);
145 static int		link_elf_preload_parse_symbols(elf_file_t ef);
146 
147 #ifdef DDB
148 static void		r_debug_state(struct r_debug *dummy_one,
149 				      struct link_map *dummy_two);
150 
151 /*
152  * A list of loaded modules for GDB to use for loading symbols.
153  */
154 struct r_debug r_debug;
155 
156 #define GDB_STATE(s)	r_debug.r_state = s; r_debug_state(NULL, NULL);
157 
158 /*
159  * Function for the debugger to set a breakpoint on to gain control.
160  */
161 static void
162 r_debug_state(struct r_debug *dummy_one __unused,
163 	      struct link_map *dummy_two __unused)
164 {
165 }
166 
167 static void
168 link_elf_add_gdb(struct link_map *l)
169 {
170     struct link_map *prev;
171 
172     l->l_next = NULL;
173 
174     if (r_debug.r_map == NULL) {
175 	/* Add first. */
176 	l->l_prev = NULL;
177 	r_debug.r_map = l;
178     } else {
179 	/* Append to list. */
180 	for (prev = r_debug.r_map; prev->l_next != NULL; prev = prev->l_next)
181 	    ;
182 	l->l_prev = prev;
183 	prev->l_next = l;
184     }
185 }
186 
187 static void
188 link_elf_delete_gdb(struct link_map *l)
189 {
190     if (l->l_prev == NULL) {
191 	/* Remove first. */
192 	if ((r_debug.r_map = l->l_next) != NULL)
193 	    l->l_next->l_prev = NULL;
194     } else {
195 	/* Remove any but first. */
196 	if ((l->l_prev->l_next = l->l_next) != NULL)
197 	    l->l_next->l_prev = l->l_prev;
198     }
199 }
200 #endif /* DDB */
201 
202 #ifdef __ia64__
203 Elf_Addr link_elf_get_gp(linker_file_t);
204 #endif
205 
206 /*
207  * The kernel symbol table starts here.
208  */
209 extern struct _dynamic _DYNAMIC;
210 
211 static void
212 link_elf_error(const char *s)
213 {
214     printf("kldload: %s\n", s);
215 }
216 
217 /*
218  * Actions performed after linking/loading both the preloaded kernel and any
219  * modules; whether preloaded or dynamicly loaded.
220  */
221 static int
222 link_elf_link_common_finish(linker_file_t lf)
223 {
224 #ifdef DDB
225     elf_file_t ef = (elf_file_t)lf;
226     char *newfilename;
227 #endif
228     int error;
229 
230     /* Notify MD code that a module is being loaded. */
231     error = elf_cpu_load_file(lf);
232     if (error)
233 	return (error);
234 
235 #ifdef DDB
236     GDB_STATE(RT_ADD);
237     ef->gdb.l_addr = lf->address;
238     newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
239     strcpy(newfilename, lf->filename);
240     ef->gdb.l_name = newfilename;
241     ef->gdb.l_ld = ef->dynamic;
242     link_elf_add_gdb(&ef->gdb);
243     GDB_STATE(RT_CONSISTENT);
244 #endif
245 
246     return (0);
247 }
248 
249 static void
250 link_elf_init(void* arg)
251 {
252     Elf_Dyn	*dp;
253     caddr_t	modptr, baseptr, sizeptr;
254     elf_file_t	ef;
255     char	*modname;
256 
257     linker_add_class(&link_elf_class);
258 
259     dp = (Elf_Dyn*) &_DYNAMIC;
260     modname = NULL;
261     modptr = preload_search_by_type("elf" __XSTRING(__ELF_WORD_SIZE) " kernel");
262     if (modptr == NULL)
263 	modptr = preload_search_by_type("elf kernel");
264     if (modptr)
265 	modname = (char *)preload_search_info(modptr, MODINFO_NAME);
266     if (modname == NULL)
267 	modname = "kernel";
268     linker_kernel_file = linker_make_file(modname, &link_elf_class);
269     if (linker_kernel_file == NULL)
270 	panic("link_elf_init: Can't create linker structures for kernel");
271 
272     ef = (elf_file_t) linker_kernel_file;
273     ef->preloaded = 1;
274     ef->address = 0;
275 #ifdef SPARSE_MAPPING
276     ef->object = 0;
277 #endif
278     ef->dynamic = dp;
279 
280     if (dp)
281 	parse_dynamic(ef);
282     linker_kernel_file->address = (caddr_t) KERNBASE;
283     linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
284 
285     if (modptr) {
286 	ef->modptr = modptr;
287 	baseptr = preload_search_info(modptr, MODINFO_ADDR);
288 	if (baseptr)
289 	    linker_kernel_file->address = *(caddr_t *)baseptr;
290 	sizeptr = preload_search_info(modptr, MODINFO_SIZE);
291 	if (sizeptr)
292 	    linker_kernel_file->size = *(size_t *)sizeptr;
293     }
294     (void)link_elf_preload_parse_symbols(ef);
295 
296 #ifdef DDB
297     r_debug.r_map = NULL;
298     r_debug.r_brk = r_debug_state;
299     r_debug.r_state = RT_CONSISTENT;
300 #endif
301 
302     (void)link_elf_link_common_finish(linker_kernel_file);
303 }
304 
305 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
306 
307 static int
308 link_elf_preload_parse_symbols(elf_file_t ef)
309 {
310     caddr_t	pointer;
311     caddr_t	ssym, esym, base;
312     caddr_t	strtab;
313     int		strcnt;
314     Elf_Sym*	symtab;
315     int		symcnt;
316 
317     if (ef->modptr == NULL)
318 	return 0;
319     pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM);
320     if (pointer == NULL)
321 	return 0;
322     ssym = *(caddr_t *)pointer;
323     pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM);
324     if (pointer == NULL)
325 	return 0;
326     esym = *(caddr_t *)pointer;
327 
328     base = ssym;
329 
330     symcnt = *(long *)base;
331     base += sizeof(long);
332     symtab = (Elf_Sym *)base;
333     base += roundup(symcnt, sizeof(long));
334 
335     if (base > esym || base < ssym) {
336 	printf("Symbols are corrupt!\n");
337 	return EINVAL;
338     }
339 
340     strcnt = *(long *)base;
341     base += sizeof(long);
342     strtab = base;
343     base += roundup(strcnt, sizeof(long));
344 
345     if (base > esym || base < ssym) {
346 	printf("Symbols are corrupt!\n");
347 	return EINVAL;
348     }
349 
350     ef->ddbsymtab = symtab;
351     ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
352     ef->ddbstrtab = strtab;
353     ef->ddbstrcnt = strcnt;
354 
355     return 0;
356 }
357 
358 static int
359 parse_dynamic(elf_file_t ef)
360 {
361     Elf_Dyn *dp;
362     int plttype = DT_REL;
363 
364     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
365 	switch (dp->d_tag) {
366 	case DT_HASH:
367 	{
368 	    /* From src/libexec/rtld-elf/rtld.c */
369 	    const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
370 		(ef->address + dp->d_un.d_ptr);
371 	    ef->nbuckets = hashtab[0];
372 	    ef->nchains = hashtab[1];
373 	    ef->buckets = hashtab + 2;
374 	    ef->chains = ef->buckets + ef->nbuckets;
375 	    break;
376 	}
377 	case DT_STRTAB:
378 	    ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
379 	    break;
380 	case DT_STRSZ:
381 	    ef->strsz = dp->d_un.d_val;
382 	    break;
383 	case DT_SYMTAB:
384 	    ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
385 	    break;
386 	case DT_SYMENT:
387 	    if (dp->d_un.d_val != sizeof(Elf_Sym))
388 		return ENOEXEC;
389 	    break;
390 	case DT_PLTGOT:
391 	    ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
392 	    break;
393 	case DT_REL:
394 	    ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
395 	    break;
396 	case DT_RELSZ:
397 	    ef->relsize = dp->d_un.d_val;
398 	    break;
399 	case DT_RELENT:
400 	    if (dp->d_un.d_val != sizeof(Elf_Rel))
401 		return ENOEXEC;
402 	    break;
403 	case DT_JMPREL:
404 	    ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
405 	    break;
406 	case DT_PLTRELSZ:
407 	    ef->pltrelsize = dp->d_un.d_val;
408 	    break;
409 	case DT_RELA:
410 	    ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
411 	    break;
412 	case DT_RELASZ:
413 	    ef->relasize = dp->d_un.d_val;
414 	    break;
415 	case DT_RELAENT:
416 	    if (dp->d_un.d_val != sizeof(Elf_Rela))
417 		return ENOEXEC;
418 	    break;
419 	case DT_PLTREL:
420 	    plttype = dp->d_un.d_val;
421 	    if (plttype != DT_REL && plttype != DT_RELA)
422 		return ENOEXEC;
423 	    break;
424 #ifdef DDB
425 	case DT_DEBUG:
426 	    dp->d_un.d_ptr = (Elf_Addr) &r_debug;
427 	    break;
428 #endif
429 	}
430     }
431 
432     if (plttype == DT_RELA) {
433 	ef->pltrela = (const Elf_Rela *) ef->pltrel;
434 	ef->pltrel = NULL;
435 	ef->pltrelasize = ef->pltrelsize;
436 	ef->pltrelsize = 0;
437     }
438 
439     ef->ddbsymtab = ef->symtab;
440     ef->ddbsymcnt = ef->nchains;
441     ef->ddbstrtab = ef->strtab;
442     ef->ddbstrcnt = ef->strsz;
443 
444     return 0;
445 }
446 
447 static int
448 link_elf_link_preload(linker_class_t cls,
449 		      const char* filename, linker_file_t *result)
450 {
451     caddr_t		modptr, baseptr, sizeptr, dynptr;
452     char		*type;
453     elf_file_t		ef;
454     linker_file_t	lf;
455     int			error;
456     vm_offset_t		dp;
457 
458     /* Look to see if we have the file preloaded */
459     modptr = preload_search_by_name(filename);
460     if (modptr == NULL)
461 	return ENOENT;
462 
463     type = (char *)preload_search_info(modptr, MODINFO_TYPE);
464     baseptr = preload_search_info(modptr, MODINFO_ADDR);
465     sizeptr = preload_search_info(modptr, MODINFO_SIZE);
466     dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC);
467     if (type == NULL ||
468 	(strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " module") != 0 &&
469 	 strcmp(type, "elf module") != 0))
470 	return (EFTYPE);
471     if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
472 	return (EINVAL);
473 
474     lf = linker_make_file(filename, &link_elf_class);
475     if (lf == NULL) {
476 	return ENOMEM;
477     }
478 
479     ef = (elf_file_t) lf;
480     ef->preloaded = 1;
481     ef->modptr = modptr;
482     ef->address = *(caddr_t *)baseptr;
483 #ifdef SPARSE_MAPPING
484     ef->object = 0;
485 #endif
486     dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
487     ef->dynamic = (Elf_Dyn *)dp;
488     lf->address = ef->address;
489     lf->size = *(size_t *)sizeptr;
490 
491     error = parse_dynamic(ef);
492     if (error) {
493 	linker_file_unload(lf);
494 	return error;
495     }
496     link_elf_reloc_local(lf);
497     *result = lf;
498     return (0);
499 }
500 
501 static int
502 link_elf_link_preload_finish(linker_file_t lf)
503 {
504     elf_file_t		ef;
505     int error;
506 
507     ef = (elf_file_t) lf;
508 #if 0	/* this will be more trouble than it's worth for now */
509     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
510 	if (dp->d_tag != DT_NEEDED)
511 	    continue;
512 	modname = ef->strtab + dp->d_un.d_val;
513 	error = linker_load_module(modname, lf);
514 	if (error)
515 	    goto out;
516     }
517 #endif
518     error = relocate_file(ef);
519     if (error)
520 	return error;
521     (void)link_elf_preload_parse_symbols(ef);
522 
523     return (link_elf_link_common_finish(lf));
524 }
525 
526 static int
527 link_elf_load_file(linker_class_t cls, const char* filename,
528 	linker_file_t* result)
529 {
530     struct nameidata nd;
531     struct thread* td = curthread;	/* XXX */
532     Elf_Ehdr *hdr;
533     caddr_t firstpage;
534     int nbytes, i;
535     Elf_Phdr *phdr;
536     Elf_Phdr *phlimit;
537     Elf_Phdr *segs[2];
538     int nsegs;
539     Elf_Phdr *phdyn;
540     Elf_Phdr *phphdr;
541     caddr_t mapbase;
542     size_t mapsize;
543     Elf_Off base_offset;
544     Elf_Addr base_vaddr;
545     Elf_Addr base_vlimit;
546     int error = 0;
547     int resid, flags;
548     elf_file_t ef;
549     linker_file_t lf;
550     Elf_Shdr *shdr;
551     int symtabindex;
552     int symstrindex;
553     int symcnt;
554     int strcnt;
555 
556     GIANT_REQUIRED;
557 
558     shdr = NULL;
559     lf = NULL;
560 
561     NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
562     flags = FREAD;
563     error = vn_open(&nd, &flags, 0);
564     if (error)
565 	return error;
566     NDFREE(&nd, NDF_ONLY_PNBUF);
567 #ifdef MAC
568     error = mac_check_kld_load(curthread->td_ucred, nd.ni_vp);
569     if (error) {
570 	firstpage = NULL;
571 	goto out;
572     }
573 #endif
574 
575     /*
576      * Read the elf header from the file.
577      */
578     firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
579     if (firstpage == NULL) {
580 	error = ENOMEM;
581 	goto out;
582     }
583     hdr = (Elf_Ehdr *)firstpage;
584     error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
585 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
586 		    &resid, td);
587     nbytes = PAGE_SIZE - resid;
588     if (error)
589 	goto out;
590 
591     if (!IS_ELF(*hdr)) {
592 	error = ENOEXEC;
593 	goto out;
594     }
595 
596     if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
597       || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
598 	link_elf_error("Unsupported file layout");
599 	error = ENOEXEC;
600 	goto out;
601     }
602     if (hdr->e_ident[EI_VERSION] != EV_CURRENT
603       || hdr->e_version != EV_CURRENT) {
604 	link_elf_error("Unsupported file version");
605 	error = ENOEXEC;
606 	goto out;
607     }
608     if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
609 	link_elf_error("Unsupported file type");
610 	error = ENOEXEC;
611 	goto out;
612     }
613     if (hdr->e_machine != ELF_TARG_MACH) {
614 	link_elf_error("Unsupported machine");
615 	error = ENOEXEC;
616 	goto out;
617     }
618 
619     /*
620      * We rely on the program header being in the first page.  This is
621      * not strictly required by the ABI specification, but it seems to
622      * always true in practice.  And, it simplifies things considerably.
623      */
624     if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
625 	  (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
626 	  (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
627 	link_elf_error("Unreadable program headers");
628 
629     /*
630      * Scan the program header entries, and save key information.
631      *
632      * We rely on there being exactly two load segments, text and data,
633      * in that order.
634      */
635     phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
636     phlimit = phdr + hdr->e_phnum;
637     nsegs = 0;
638     phdyn = NULL;
639     phphdr = NULL;
640     while (phdr < phlimit) {
641 	switch (phdr->p_type) {
642 
643 	case PT_LOAD:
644 	    if (nsegs == 2) {
645 		link_elf_error("Too many sections");
646 		error = ENOEXEC;
647 		goto out;
648 	    }
649 	    segs[nsegs] = phdr;
650 	    ++nsegs;
651 	    break;
652 
653 	case PT_PHDR:
654 	    phphdr = phdr;
655 	    break;
656 
657 	case PT_DYNAMIC:
658 	    phdyn = phdr;
659 	    break;
660 
661 	case PT_INTERP:
662 	    link_elf_error("Unsupported file type");
663 	    error = ENOEXEC;
664 	    goto out;
665 	}
666 
667 	++phdr;
668     }
669     if (phdyn == NULL) {
670 	link_elf_error("Object is not dynamically-linked");
671 	error = ENOEXEC;
672 	goto out;
673     }
674 
675     /*
676      * Allocate the entire address space of the object, to stake out our
677      * contiguous region, and to establish the base address for relocation.
678      */
679     base_offset = trunc_page(segs[0]->p_offset);
680     base_vaddr = trunc_page(segs[0]->p_vaddr);
681     base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
682     mapsize = base_vlimit - base_vaddr;
683 
684     lf = linker_make_file(filename, &link_elf_class);
685     if (!lf) {
686 	error = ENOMEM;
687 	goto out;
688     }
689 
690     ef = (elf_file_t) lf;
691 #ifdef SPARSE_MAPPING
692     ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
693     if (ef->object == NULL) {
694 	error = ENOMEM;
695 	goto out;
696     }
697     vm_object_reference(ef->object);
698     ef->address = (caddr_t) vm_map_min(kernel_map);
699     error = vm_map_find(kernel_map, ef->object, 0,
700 			(vm_offset_t *) &ef->address,
701 			mapsize, 1,
702 			VM_PROT_ALL, VM_PROT_ALL, 0);
703     if (error) {
704 	vm_object_deallocate(ef->object);
705 	ef->object = 0;
706 	goto out;
707     }
708 #else
709     ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
710     if (!ef->address) {
711 	error = ENOMEM;
712 	goto out;
713     }
714 #endif
715     mapbase = ef->address;
716 
717     /*
718      * Read the text and data sections and zero the bss.
719      */
720     for (i = 0; i < 2; i++) {
721 	caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
722 	error = vn_rdwr(UIO_READ, nd.ni_vp,
723 			segbase, segs[i]->p_filesz, segs[i]->p_offset,
724 			UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
725 			&resid, td);
726 	if (error) {
727 	    goto out;
728 	}
729 	bzero(segbase + segs[i]->p_filesz,
730 	      segs[i]->p_memsz - segs[i]->p_filesz);
731 
732 #ifdef SPARSE_MAPPING
733 	/*
734 	 * Wire down the pages
735 	 */
736 	vm_map_wire(kernel_map,
737 		    (vm_offset_t) segbase,
738 		    (vm_offset_t) segbase + segs[i]->p_memsz,
739 		    FALSE);
740 #endif
741     }
742 
743 #ifdef GPROF
744     /* Update profiling information with the new text segment. */
745     kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr +
746 	segs[0]->p_memsz));
747 #endif
748 
749     ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
750 
751     lf->address = ef->address;
752     lf->size = mapsize;
753 
754     error = parse_dynamic(ef);
755     if (error)
756 	goto out;
757     link_elf_reloc_local(lf);
758 
759     error = linker_load_dependencies(lf);
760     if (error)
761 	goto out;
762 #if 0	/* this will be more trouble than it's worth for now */
763     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
764 	if (dp->d_tag != DT_NEEDED)
765 	    continue;
766 	modname = ef->strtab + dp->d_un.d_val;
767 	error = linker_load_module(modname, lf);
768 	if (error)
769 	    goto out;
770     }
771 #endif
772     error = relocate_file(ef);
773     if (error)
774 	goto out;
775 
776     /* Try and load the symbol table if it's present.  (you can strip it!) */
777     nbytes = hdr->e_shnum * hdr->e_shentsize;
778     if (nbytes == 0 || hdr->e_shoff == 0)
779 	goto nosyms;
780     shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
781     if (shdr == NULL) {
782 	error = ENOMEM;
783 	goto out;
784     }
785     error = vn_rdwr(UIO_READ, nd.ni_vp,
786 		    (caddr_t)shdr, nbytes, hdr->e_shoff,
787 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
788 		    &resid, td);
789     if (error)
790 	goto out;
791     symtabindex = -1;
792     symstrindex = -1;
793     for (i = 0; i < hdr->e_shnum; i++) {
794 	if (shdr[i].sh_type == SHT_SYMTAB) {
795 	    symtabindex = i;
796 	    symstrindex = shdr[i].sh_link;
797 	}
798     }
799     if (symtabindex < 0 || symstrindex < 0)
800 	goto nosyms;
801 
802     symcnt = shdr[symtabindex].sh_size;
803     ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
804     strcnt = shdr[symstrindex].sh_size;
805     ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
806 
807     if (ef->symbase == NULL || ef->strbase == NULL) {
808 	error = ENOMEM;
809 	goto out;
810     }
811     error = vn_rdwr(UIO_READ, nd.ni_vp,
812 		    ef->symbase, symcnt, shdr[symtabindex].sh_offset,
813 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
814 		    &resid, td);
815     if (error)
816 	goto out;
817     error = vn_rdwr(UIO_READ, nd.ni_vp,
818 		    ef->strbase, strcnt, shdr[symstrindex].sh_offset,
819 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
820 		    &resid, td);
821     if (error)
822 	goto out;
823 
824     ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
825     ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
826     ef->ddbstrcnt = strcnt;
827     ef->ddbstrtab = ef->strbase;
828 
829     error = link_elf_link_common_finish(lf);
830     if (error)
831 	goto out;
832 
833 nosyms:
834 
835     *result = lf;
836 
837 out:
838     if (error && lf)
839 	linker_file_unload(lf);
840     if (shdr)
841 	free(shdr, M_LINKER);
842     if (firstpage)
843 	free(firstpage, M_LINKER);
844     VOP_UNLOCK(nd.ni_vp, 0, td);
845     vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
846 
847     return error;
848 }
849 
850 static void
851 link_elf_unload_file(linker_file_t file)
852 {
853     elf_file_t ef = (elf_file_t) file;
854 
855 #ifdef DDB
856     if (ef->gdb.l_ld) {
857 	GDB_STATE(RT_DELETE);
858 	free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
859 	link_elf_delete_gdb(&ef->gdb);
860 	GDB_STATE(RT_CONSISTENT);
861     }
862 #endif
863 
864     /* Notify MD code that a module is being unloaded. */
865     elf_cpu_unload_file(file);
866 
867     if (ef->preloaded) {
868 	link_elf_unload_preload(file);
869 	return;
870     }
871 
872 #ifdef SPARSE_MAPPING
873     if (ef->object) {
874 	vm_map_remove(kernel_map, (vm_offset_t) ef->address,
875 		      (vm_offset_t) ef->address
876 		      + (ef->object->size << PAGE_SHIFT));
877 	vm_object_deallocate(ef->object);
878     }
879 #else
880     if (ef->address)
881 	free(ef->address, M_LINKER);
882 #endif
883     if (ef->symbase)
884 	free(ef->symbase, M_LINKER);
885     if (ef->strbase)
886 	free(ef->strbase, M_LINKER);
887 }
888 
889 static void
890 link_elf_unload_preload(linker_file_t file)
891 {
892     if (file->filename)
893 	preload_delete_name(file->filename);
894 }
895 
896 static const char *
897 symbol_name(elf_file_t ef, Elf_Word r_info)
898 {
899     const Elf_Sym *ref;
900 
901     if (ELF_R_SYM(r_info)) {
902 	ref = ef->symtab + ELF_R_SYM(r_info);
903 	return ef->strtab + ref->st_name;
904     } else
905 	return NULL;
906 }
907 
908 static int
909 relocate_file(elf_file_t ef)
910 {
911     const Elf_Rel *rellim;
912     const Elf_Rel *rel;
913     const Elf_Rela *relalim;
914     const Elf_Rela *rela;
915     const char *symname;
916 
917     /* Perform relocations without addend if there are any: */
918     rel = ef->rel;
919     if (rel) {
920 	rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
921 	while (rel < rellim) {
922 	    if (elf_reloc(&ef->lf, rel, ELF_RELOC_REL)) {
923 		symname = symbol_name(ef, rel->r_info);
924 		printf("link_elf: symbol %s undefined\n", symname);
925 		return ENOENT;
926 	    }
927 	    rel++;
928 	}
929     }
930 
931     /* Perform relocations with addend if there are any: */
932     rela = ef->rela;
933     if (rela) {
934 	relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
935 	while (rela < relalim) {
936 	    if (elf_reloc(&ef->lf, rela, ELF_RELOC_RELA)) {
937 		symname = symbol_name(ef, rela->r_info);
938 		printf("link_elf: symbol %s undefined\n", symname);
939 		return ENOENT;
940 	    }
941 	    rela++;
942 	}
943     }
944 
945     /* Perform PLT relocations without addend if there are any: */
946     rel = ef->pltrel;
947     if (rel) {
948 	rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
949 	while (rel < rellim) {
950 	    if (elf_reloc(&ef->lf, rel, ELF_RELOC_REL)) {
951 		symname = symbol_name(ef, rel->r_info);
952 		printf("link_elf: symbol %s undefined\n", symname);
953 		return ENOENT;
954 	    }
955 	    rel++;
956 	}
957     }
958 
959     /* Perform relocations with addend if there are any: */
960     rela = ef->pltrela;
961     if (rela) {
962 	relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
963 	while (rela < relalim) {
964 	    if (elf_reloc(&ef->lf, rela, ELF_RELOC_RELA)) {
965 		symname = symbol_name(ef, rela->r_info);
966 		printf("link_elf: symbol %s undefined\n", symname);
967 		return ENOENT;
968 	    }
969 	    rela++;
970 	}
971     }
972 
973     return 0;
974 }
975 
976 /*
977  * Hash function for symbol table lookup.  Don't even think about changing
978  * this.  It is specified by the System V ABI.
979  */
980 static unsigned long
981 elf_hash(const char *name)
982 {
983     const unsigned char *p = (const unsigned char *) name;
984     unsigned long h = 0;
985     unsigned long g;
986 
987     while (*p != '\0') {
988 	h = (h << 4) + *p++;
989 	if ((g = h & 0xf0000000) != 0)
990 	    h ^= g >> 24;
991 	h &= ~g;
992     }
993     return h;
994 }
995 
996 static int
997 link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
998 {
999     elf_file_t ef = (elf_file_t) lf;
1000     unsigned long symnum;
1001     const Elf_Sym* symp;
1002     const char *strp;
1003     unsigned long hash;
1004     int i;
1005 
1006     /* First, search hashed global symbols */
1007     hash = elf_hash(name);
1008     symnum = ef->buckets[hash % ef->nbuckets];
1009 
1010     while (symnum != STN_UNDEF) {
1011 	if (symnum >= ef->nchains) {
1012 	    printf("link_elf_lookup_symbol: corrupt symbol table\n");
1013 	    return ENOENT;
1014 	}
1015 
1016 	symp = ef->symtab + symnum;
1017 	if (symp->st_name == 0) {
1018 	    printf("link_elf_lookup_symbol: corrupt symbol table\n");
1019 	    return ENOENT;
1020 	}
1021 
1022 	strp = ef->strtab + symp->st_name;
1023 
1024 	if (strcmp(name, strp) == 0) {
1025 	    if (symp->st_shndx != SHN_UNDEF ||
1026 		(symp->st_value != 0 &&
1027 		 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1028 		*sym = (c_linker_sym_t) symp;
1029 		return 0;
1030 	    } else
1031 		return ENOENT;
1032 	}
1033 
1034 	symnum = ef->chains[symnum];
1035     }
1036 
1037     /* If we have not found it, look at the full table (if loaded) */
1038     if (ef->symtab == ef->ddbsymtab)
1039 	return ENOENT;
1040 
1041     /* Exhaustive search */
1042     for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1043 	strp = ef->ddbstrtab + symp->st_name;
1044 	if (strcmp(name, strp) == 0) {
1045 	    if (symp->st_shndx != SHN_UNDEF ||
1046 		(symp->st_value != 0 &&
1047 		 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1048 		*sym = (c_linker_sym_t) symp;
1049 		return 0;
1050 	    } else
1051 		return ENOENT;
1052 	}
1053     }
1054 
1055     return ENOENT;
1056 }
1057 
1058 static int
1059 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
1060 {
1061 	elf_file_t ef = (elf_file_t) lf;
1062 	const Elf_Sym* es = (const Elf_Sym*) sym;
1063 
1064 	if (es >= ef->symtab && es < (ef->symtab + ef->nchains)) {
1065 	    symval->name = ef->strtab + es->st_name;
1066 	    symval->value = (caddr_t) ef->address + es->st_value;
1067 	    symval->size = es->st_size;
1068 	    return 0;
1069 	}
1070 	if (ef->symtab == ef->ddbsymtab)
1071 	    return ENOENT;
1072 	if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1073 	    symval->name = ef->ddbstrtab + es->st_name;
1074 	    symval->value = (caddr_t) ef->address + es->st_value;
1075 	    symval->size = es->st_size;
1076 	    return 0;
1077 	}
1078 	return ENOENT;
1079 }
1080 
1081 static int
1082 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1083 		       c_linker_sym_t* sym, long* diffp)
1084 {
1085 	elf_file_t ef = (elf_file_t) lf;
1086 	u_long off = (uintptr_t) (void *) value;
1087 	u_long diff = off;
1088 	u_long st_value;
1089 	const Elf_Sym* es;
1090 	const Elf_Sym* best = 0;
1091 	int i;
1092 
1093 	for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1094 		if (es->st_name == 0)
1095 			continue;
1096 		st_value = es->st_value + (uintptr_t) (void *) ef->address;
1097 		if (off >= st_value) {
1098 			if (off - st_value < diff) {
1099 				diff = off - st_value;
1100 				best = es;
1101 				if (diff == 0)
1102 					break;
1103 			} else if (off - st_value == diff) {
1104 				best = es;
1105 			}
1106 		}
1107 	}
1108 	if (best == 0)
1109 		*diffp = off;
1110 	else
1111 		*diffp = diff;
1112 	*sym = (c_linker_sym_t) best;
1113 
1114 	return 0;
1115 }
1116 
1117 /*
1118  * Look up a linker set on an ELF system.
1119  */
1120 static int
1121 link_elf_lookup_set(linker_file_t lf, const char *name,
1122 		    void ***startp, void ***stopp, int *countp)
1123 {
1124 	c_linker_sym_t sym;
1125 	linker_symval_t symval;
1126 	char *setsym;
1127 	void **start, **stop;
1128 	int len, error = 0, count;
1129 
1130 	len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1131 	setsym = malloc(len, M_LINKER, M_WAITOK);
1132 	if (setsym == NULL)
1133 		return ENOMEM;
1134 
1135 	/* get address of first entry */
1136 	snprintf(setsym, len, "%s%s", "__start_set_", name);
1137 	error = link_elf_lookup_symbol(lf, setsym, &sym);
1138 	if (error)
1139 		goto out;
1140 	link_elf_symbol_values(lf, sym, &symval);
1141 	if (symval.value == 0) {
1142 		error = ESRCH;
1143 		goto out;
1144 	}
1145 	start = (void **)symval.value;
1146 
1147 	/* get address of last entry */
1148 	snprintf(setsym, len, "%s%s", "__stop_set_", name);
1149 	error = link_elf_lookup_symbol(lf, setsym, &sym);
1150 	if (error)
1151 		goto out;
1152 	link_elf_symbol_values(lf, sym, &symval);
1153 	if (symval.value == 0) {
1154 		error = ESRCH;
1155 		goto out;
1156 	}
1157 	stop = (void **)symval.value;
1158 
1159 	/* and the number of entries */
1160 	count = stop - start;
1161 
1162 	/* and copy out */
1163 	if (startp)
1164 		*startp = start;
1165 	if (stopp)
1166 		*stopp = stop;
1167 	if (countp)
1168 		*countp = count;
1169 
1170 out:
1171 	free(setsym, M_LINKER);
1172 	return error;
1173 }
1174 
1175 static int
1176 link_elf_each_function_name(linker_file_t file,
1177   int (*callback)(const char *, void *), void *opaque) {
1178     elf_file_t ef = (elf_file_t)file;
1179     const Elf_Sym* symp;
1180     int i, error;
1181 
1182     /* Exhaustive search */
1183     for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1184 	if (symp->st_value != 0 &&
1185 	    ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1186 		error = callback(ef->ddbstrtab + symp->st_name, opaque);
1187 		if (error)
1188 		    return (error);
1189 	}
1190     }
1191     return (0);
1192 }
1193 
1194 #ifdef __ia64__
1195 /*
1196  * Each KLD has its own GP. The GP value for each load module is given by
1197  * DT_PLTGOT on ia64. We need GP to construct function descriptors, but
1198  * don't have direct access to the ELF file structure. The link_elf_get_gp()
1199  * function returns the GP given a pointer to a generic linker file struct.
1200  */
1201 Elf_Addr
1202 link_elf_get_gp(linker_file_t lf)
1203 {
1204 	elf_file_t ef = (elf_file_t)lf;
1205 	return (Elf_Addr)ef->got;
1206 }
1207 #endif
1208 
1209 const Elf_Sym *
1210 elf_get_sym(linker_file_t lf, Elf_Word symidx)
1211 {
1212 	elf_file_t ef = (elf_file_t)lf;
1213 
1214 	if (symidx >= ef->nchains)
1215 		return (NULL);
1216 	return (ef->symtab + symidx);
1217 }
1218 
1219 const char *
1220 elf_get_symname(linker_file_t lf, Elf_Word symidx)
1221 {
1222 	elf_file_t ef = (elf_file_t)lf;
1223 	const Elf_Sym *sym;
1224 
1225 	if (symidx >= ef->nchains)
1226 		return (NULL);
1227 	sym = ef->symtab + symidx;
1228 	return (ef->strtab + sym->st_name);
1229 }
1230 
1231 /*
1232  * Symbol lookup function that can be used when the symbol index is known (ie
1233  * in relocations). It uses the symbol index instead of doing a fully fledged
1234  * hash table based lookup when such is valid. For example for local symbols.
1235  * This is not only more efficient, it's also more correct. It's not always
1236  * the case that the symbol can be found through the hash table.
1237  */
1238 Elf_Addr
1239 elf_lookup(linker_file_t lf, Elf_Word symidx, int deps)
1240 {
1241 	elf_file_t ef = (elf_file_t)lf;
1242 	const Elf_Sym *sym;
1243 	const char *symbol;
1244 
1245 	/* Don't even try to lookup the symbol if the index is bogus. */
1246 	if (symidx >= ef->nchains)
1247 		return (0);
1248 
1249 	sym = ef->symtab + symidx;
1250 
1251 	/*
1252 	 * Don't do a full lookup when the symbol is local. It may even
1253 	 * fail because it may not be found through the hash table.
1254 	 */
1255 	if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1256 		/* Force lookup failure when we have an insanity. */
1257 		if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0)
1258 			return (0);
1259 		return ((Elf_Addr)ef->address + sym->st_value);
1260 	}
1261 
1262 	/*
1263 	 * XXX we can avoid doing a hash table based lookup for global
1264 	 * symbols as well. This however is not always valid, so we'll
1265 	 * just do it the hard way for now. Performance tweaks can
1266 	 * always be added.
1267 	 */
1268 
1269 	symbol = ef->strtab + sym->st_name;
1270 
1271 	/* Force a lookup failure if the symbol name is bogus. */
1272 	if (*symbol == 0)
1273 		return (0);
1274 
1275 	return ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1276 }
1277 
1278 static void
1279 link_elf_reloc_local(linker_file_t lf)
1280 {
1281     const Elf_Rel *rellim;
1282     const Elf_Rel *rel;
1283     const Elf_Rela *relalim;
1284     const Elf_Rela *rela;
1285     elf_file_t ef = (elf_file_t)lf;
1286 
1287     /* Perform relocations without addend if there are any: */
1288     if ((rel = ef->rel) != NULL) {
1289 	rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
1290 	while (rel < rellim) {
1291 	    elf_reloc_local(lf, rel, ELF_RELOC_REL);
1292 	    rel++;
1293 	}
1294     }
1295 
1296     /* Perform relocations with addend if there are any: */
1297     if ((rela = ef->rela) != NULL) {
1298 	relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
1299 	while (rela < relalim) {
1300 	    elf_reloc_local(lf, rela, ELF_RELOC_RELA);
1301 	    rela++;
1302 	}
1303     }
1304 }
1305