xref: /freebsd/sys/kern/link_elf_obj.c (revision 06a31d6a6779b74405b41c8ad4579b5d81db4d4c)
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 	    /*
650 	     * XXX: We just trust they come in right order ??
651 	     */
652 	    segs[nsegs] = phdr;
653 	    ++nsegs;
654 	    break;
655 
656 	case PT_PHDR:
657 	    phphdr = phdr;
658 	    break;
659 
660 	case PT_DYNAMIC:
661 	    phdyn = phdr;
662 	    break;
663 
664 	case PT_INTERP:
665 	    link_elf_error("Unsupported file type");
666 	    error = ENOEXEC;
667 	    goto out;
668 	}
669 
670 	++phdr;
671     }
672     if (phdyn == NULL) {
673 	link_elf_error("Object is not dynamically-linked");
674 	error = ENOEXEC;
675 	goto out;
676     }
677     if (nsegs != 2) {
678 	link_elf_error("Too few sections");
679 	error = ENOEXEC;
680 	goto out;
681     }
682 
683     /*
684      * Allocate the entire address space of the object, to stake out our
685      * contiguous region, and to establish the base address for relocation.
686      */
687     base_offset = trunc_page(segs[0]->p_offset);
688     base_vaddr = trunc_page(segs[0]->p_vaddr);
689     base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
690     mapsize = base_vlimit - base_vaddr;
691 
692     lf = linker_make_file(filename, &link_elf_class);
693     if (!lf) {
694 	error = ENOMEM;
695 	goto out;
696     }
697 
698     ef = (elf_file_t) lf;
699 #ifdef SPARSE_MAPPING
700     ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
701     if (ef->object == NULL) {
702 	error = ENOMEM;
703 	goto out;
704     }
705     vm_object_reference(ef->object);
706     ef->address = (caddr_t) vm_map_min(kernel_map);
707     error = vm_map_find(kernel_map, ef->object, 0,
708 			(vm_offset_t *) &ef->address,
709 			mapsize, 1,
710 			VM_PROT_ALL, VM_PROT_ALL, 0);
711     if (error) {
712 	vm_object_deallocate(ef->object);
713 	ef->object = 0;
714 	goto out;
715     }
716 #else
717     ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
718     if (!ef->address) {
719 	error = ENOMEM;
720 	goto out;
721     }
722 #endif
723     mapbase = ef->address;
724 
725     /*
726      * Read the text and data sections and zero the bss.
727      */
728     for (i = 0; i < 2; i++) {
729 	caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
730 	error = vn_rdwr(UIO_READ, nd.ni_vp,
731 			segbase, segs[i]->p_filesz, segs[i]->p_offset,
732 			UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
733 			&resid, td);
734 	if (error) {
735 	    goto out;
736 	}
737 	bzero(segbase + segs[i]->p_filesz,
738 	      segs[i]->p_memsz - segs[i]->p_filesz);
739 
740 #ifdef SPARSE_MAPPING
741 	/*
742 	 * Wire down the pages
743 	 */
744 	vm_map_wire(kernel_map,
745 		    (vm_offset_t) segbase,
746 		    (vm_offset_t) segbase + segs[i]->p_memsz,
747 		    FALSE);
748 #endif
749     }
750 
751 #ifdef GPROF
752     /* Update profiling information with the new text segment. */
753     kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr +
754 	segs[0]->p_memsz));
755 #endif
756 
757     ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
758 
759     lf->address = ef->address;
760     lf->size = mapsize;
761 
762     error = parse_dynamic(ef);
763     if (error)
764 	goto out;
765     link_elf_reloc_local(lf);
766 
767     error = linker_load_dependencies(lf);
768     if (error)
769 	goto out;
770 #if 0	/* this will be more trouble than it's worth for now */
771     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
772 	if (dp->d_tag != DT_NEEDED)
773 	    continue;
774 	modname = ef->strtab + dp->d_un.d_val;
775 	error = linker_load_module(modname, lf);
776 	if (error)
777 	    goto out;
778     }
779 #endif
780     error = relocate_file(ef);
781     if (error)
782 	goto out;
783 
784     /* Try and load the symbol table if it's present.  (you can strip it!) */
785     nbytes = hdr->e_shnum * hdr->e_shentsize;
786     if (nbytes == 0 || hdr->e_shoff == 0)
787 	goto nosyms;
788     shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
789     if (shdr == NULL) {
790 	error = ENOMEM;
791 	goto out;
792     }
793     error = vn_rdwr(UIO_READ, nd.ni_vp,
794 		    (caddr_t)shdr, nbytes, hdr->e_shoff,
795 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
796 		    &resid, td);
797     if (error)
798 	goto out;
799     symtabindex = -1;
800     symstrindex = -1;
801     for (i = 0; i < hdr->e_shnum; i++) {
802 	if (shdr[i].sh_type == SHT_SYMTAB) {
803 	    symtabindex = i;
804 	    symstrindex = shdr[i].sh_link;
805 	}
806     }
807     if (symtabindex < 0 || symstrindex < 0)
808 	goto nosyms;
809 
810     symcnt = shdr[symtabindex].sh_size;
811     ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
812     strcnt = shdr[symstrindex].sh_size;
813     ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
814 
815     if (ef->symbase == NULL || ef->strbase == NULL) {
816 	error = ENOMEM;
817 	goto out;
818     }
819     error = vn_rdwr(UIO_READ, nd.ni_vp,
820 		    ef->symbase, symcnt, shdr[symtabindex].sh_offset,
821 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
822 		    &resid, td);
823     if (error)
824 	goto out;
825     error = vn_rdwr(UIO_READ, nd.ni_vp,
826 		    ef->strbase, strcnt, shdr[symstrindex].sh_offset,
827 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
828 		    &resid, td);
829     if (error)
830 	goto out;
831 
832     ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
833     ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
834     ef->ddbstrcnt = strcnt;
835     ef->ddbstrtab = ef->strbase;
836 
837     error = link_elf_link_common_finish(lf);
838     if (error)
839 	goto out;
840 
841 nosyms:
842 
843     *result = lf;
844 
845 out:
846     if (error && lf)
847 	linker_file_unload(lf);
848     if (shdr)
849 	free(shdr, M_LINKER);
850     if (firstpage)
851 	free(firstpage, M_LINKER);
852     VOP_UNLOCK(nd.ni_vp, 0, td);
853     vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
854 
855     return error;
856 }
857 
858 static void
859 link_elf_unload_file(linker_file_t file)
860 {
861     elf_file_t ef = (elf_file_t) file;
862 
863 #ifdef DDB
864     if (ef->gdb.l_ld) {
865 	GDB_STATE(RT_DELETE);
866 	free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
867 	link_elf_delete_gdb(&ef->gdb);
868 	GDB_STATE(RT_CONSISTENT);
869     }
870 #endif
871 
872     /* Notify MD code that a module is being unloaded. */
873     elf_cpu_unload_file(file);
874 
875     if (ef->preloaded) {
876 	link_elf_unload_preload(file);
877 	return;
878     }
879 
880 #ifdef SPARSE_MAPPING
881     if (ef->object) {
882 	vm_map_remove(kernel_map, (vm_offset_t) ef->address,
883 		      (vm_offset_t) ef->address
884 		      + (ef->object->size << PAGE_SHIFT));
885 	vm_object_deallocate(ef->object);
886     }
887 #else
888     if (ef->address)
889 	free(ef->address, M_LINKER);
890 #endif
891     if (ef->symbase)
892 	free(ef->symbase, M_LINKER);
893     if (ef->strbase)
894 	free(ef->strbase, M_LINKER);
895 }
896 
897 static void
898 link_elf_unload_preload(linker_file_t file)
899 {
900     if (file->filename)
901 	preload_delete_name(file->filename);
902 }
903 
904 static const char *
905 symbol_name(elf_file_t ef, Elf_Word r_info)
906 {
907     const Elf_Sym *ref;
908 
909     if (ELF_R_SYM(r_info)) {
910 	ref = ef->symtab + ELF_R_SYM(r_info);
911 	return ef->strtab + ref->st_name;
912     } else
913 	return NULL;
914 }
915 
916 static int
917 relocate_file(elf_file_t ef)
918 {
919     const Elf_Rel *rellim;
920     const Elf_Rel *rel;
921     const Elf_Rela *relalim;
922     const Elf_Rela *rela;
923     const char *symname;
924 
925     /* Perform relocations without addend if there are any: */
926     rel = ef->rel;
927     if (rel) {
928 	rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
929 	while (rel < rellim) {
930 	    if (elf_reloc(&ef->lf, rel, ELF_RELOC_REL)) {
931 		symname = symbol_name(ef, rel->r_info);
932 		printf("link_elf: symbol %s undefined\n", symname);
933 		return ENOENT;
934 	    }
935 	    rel++;
936 	}
937     }
938 
939     /* Perform relocations with addend if there are any: */
940     rela = ef->rela;
941     if (rela) {
942 	relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
943 	while (rela < relalim) {
944 	    if (elf_reloc(&ef->lf, rela, ELF_RELOC_RELA)) {
945 		symname = symbol_name(ef, rela->r_info);
946 		printf("link_elf: symbol %s undefined\n", symname);
947 		return ENOENT;
948 	    }
949 	    rela++;
950 	}
951     }
952 
953     /* Perform PLT relocations without addend if there are any: */
954     rel = ef->pltrel;
955     if (rel) {
956 	rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
957 	while (rel < rellim) {
958 	    if (elf_reloc(&ef->lf, rel, ELF_RELOC_REL)) {
959 		symname = symbol_name(ef, rel->r_info);
960 		printf("link_elf: symbol %s undefined\n", symname);
961 		return ENOENT;
962 	    }
963 	    rel++;
964 	}
965     }
966 
967     /* Perform relocations with addend if there are any: */
968     rela = ef->pltrela;
969     if (rela) {
970 	relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
971 	while (rela < relalim) {
972 	    if (elf_reloc(&ef->lf, rela, ELF_RELOC_RELA)) {
973 		symname = symbol_name(ef, rela->r_info);
974 		printf("link_elf: symbol %s undefined\n", symname);
975 		return ENOENT;
976 	    }
977 	    rela++;
978 	}
979     }
980 
981     return 0;
982 }
983 
984 /*
985  * Hash function for symbol table lookup.  Don't even think about changing
986  * this.  It is specified by the System V ABI.
987  */
988 static unsigned long
989 elf_hash(const char *name)
990 {
991     const unsigned char *p = (const unsigned char *) name;
992     unsigned long h = 0;
993     unsigned long g;
994 
995     while (*p != '\0') {
996 	h = (h << 4) + *p++;
997 	if ((g = h & 0xf0000000) != 0)
998 	    h ^= g >> 24;
999 	h &= ~g;
1000     }
1001     return h;
1002 }
1003 
1004 static int
1005 link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
1006 {
1007     elf_file_t ef = (elf_file_t) lf;
1008     unsigned long symnum;
1009     const Elf_Sym* symp;
1010     const char *strp;
1011     unsigned long hash;
1012     int i;
1013 
1014     /* First, search hashed global symbols */
1015     hash = elf_hash(name);
1016     symnum = ef->buckets[hash % ef->nbuckets];
1017 
1018     while (symnum != STN_UNDEF) {
1019 	if (symnum >= ef->nchains) {
1020 	    printf("link_elf_lookup_symbol: corrupt symbol table\n");
1021 	    return ENOENT;
1022 	}
1023 
1024 	symp = ef->symtab + symnum;
1025 	if (symp->st_name == 0) {
1026 	    printf("link_elf_lookup_symbol: corrupt symbol table\n");
1027 	    return ENOENT;
1028 	}
1029 
1030 	strp = ef->strtab + symp->st_name;
1031 
1032 	if (strcmp(name, strp) == 0) {
1033 	    if (symp->st_shndx != SHN_UNDEF ||
1034 		(symp->st_value != 0 &&
1035 		 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1036 		*sym = (c_linker_sym_t) symp;
1037 		return 0;
1038 	    } else
1039 		return ENOENT;
1040 	}
1041 
1042 	symnum = ef->chains[symnum];
1043     }
1044 
1045     /* If we have not found it, look at the full table (if loaded) */
1046     if (ef->symtab == ef->ddbsymtab)
1047 	return ENOENT;
1048 
1049     /* Exhaustive search */
1050     for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1051 	strp = ef->ddbstrtab + symp->st_name;
1052 	if (strcmp(name, strp) == 0) {
1053 	    if (symp->st_shndx != SHN_UNDEF ||
1054 		(symp->st_value != 0 &&
1055 		 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1056 		*sym = (c_linker_sym_t) symp;
1057 		return 0;
1058 	    } else
1059 		return ENOENT;
1060 	}
1061     }
1062 
1063     return ENOENT;
1064 }
1065 
1066 static int
1067 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
1068 {
1069 	elf_file_t ef = (elf_file_t) lf;
1070 	const Elf_Sym* es = (const Elf_Sym*) sym;
1071 
1072 	if (es >= ef->symtab && es < (ef->symtab + ef->nchains)) {
1073 	    symval->name = ef->strtab + es->st_name;
1074 	    symval->value = (caddr_t) ef->address + es->st_value;
1075 	    symval->size = es->st_size;
1076 	    return 0;
1077 	}
1078 	if (ef->symtab == ef->ddbsymtab)
1079 	    return ENOENT;
1080 	if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1081 	    symval->name = ef->ddbstrtab + es->st_name;
1082 	    symval->value = (caddr_t) ef->address + es->st_value;
1083 	    symval->size = es->st_size;
1084 	    return 0;
1085 	}
1086 	return ENOENT;
1087 }
1088 
1089 static int
1090 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1091 		       c_linker_sym_t* sym, long* diffp)
1092 {
1093 	elf_file_t ef = (elf_file_t) lf;
1094 	u_long off = (uintptr_t) (void *) value;
1095 	u_long diff = off;
1096 	u_long st_value;
1097 	const Elf_Sym* es;
1098 	const Elf_Sym* best = 0;
1099 	int i;
1100 
1101 	for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1102 		if (es->st_name == 0)
1103 			continue;
1104 		st_value = es->st_value + (uintptr_t) (void *) ef->address;
1105 		if (off >= st_value) {
1106 			if (off - st_value < diff) {
1107 				diff = off - st_value;
1108 				best = es;
1109 				if (diff == 0)
1110 					break;
1111 			} else if (off - st_value == diff) {
1112 				best = es;
1113 			}
1114 		}
1115 	}
1116 	if (best == 0)
1117 		*diffp = off;
1118 	else
1119 		*diffp = diff;
1120 	*sym = (c_linker_sym_t) best;
1121 
1122 	return 0;
1123 }
1124 
1125 /*
1126  * Look up a linker set on an ELF system.
1127  */
1128 static int
1129 link_elf_lookup_set(linker_file_t lf, const char *name,
1130 		    void ***startp, void ***stopp, int *countp)
1131 {
1132 	c_linker_sym_t sym;
1133 	linker_symval_t symval;
1134 	char *setsym;
1135 	void **start, **stop;
1136 	int len, error = 0, count;
1137 
1138 	len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1139 	setsym = malloc(len, M_LINKER, M_WAITOK);
1140 	if (setsym == NULL)
1141 		return ENOMEM;
1142 
1143 	/* get address of first entry */
1144 	snprintf(setsym, len, "%s%s", "__start_set_", name);
1145 	error = link_elf_lookup_symbol(lf, setsym, &sym);
1146 	if (error)
1147 		goto out;
1148 	link_elf_symbol_values(lf, sym, &symval);
1149 	if (symval.value == 0) {
1150 		error = ESRCH;
1151 		goto out;
1152 	}
1153 	start = (void **)symval.value;
1154 
1155 	/* get address of last entry */
1156 	snprintf(setsym, len, "%s%s", "__stop_set_", name);
1157 	error = link_elf_lookup_symbol(lf, setsym, &sym);
1158 	if (error)
1159 		goto out;
1160 	link_elf_symbol_values(lf, sym, &symval);
1161 	if (symval.value == 0) {
1162 		error = ESRCH;
1163 		goto out;
1164 	}
1165 	stop = (void **)symval.value;
1166 
1167 	/* and the number of entries */
1168 	count = stop - start;
1169 
1170 	/* and copy out */
1171 	if (startp)
1172 		*startp = start;
1173 	if (stopp)
1174 		*stopp = stop;
1175 	if (countp)
1176 		*countp = count;
1177 
1178 out:
1179 	free(setsym, M_LINKER);
1180 	return error;
1181 }
1182 
1183 static int
1184 link_elf_each_function_name(linker_file_t file,
1185   int (*callback)(const char *, void *), void *opaque) {
1186     elf_file_t ef = (elf_file_t)file;
1187     const Elf_Sym* symp;
1188     int i, error;
1189 
1190     /* Exhaustive search */
1191     for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1192 	if (symp->st_value != 0 &&
1193 	    ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1194 		error = callback(ef->ddbstrtab + symp->st_name, opaque);
1195 		if (error)
1196 		    return (error);
1197 	}
1198     }
1199     return (0);
1200 }
1201 
1202 #ifdef __ia64__
1203 /*
1204  * Each KLD has its own GP. The GP value for each load module is given by
1205  * DT_PLTGOT on ia64. We need GP to construct function descriptors, but
1206  * don't have direct access to the ELF file structure. The link_elf_get_gp()
1207  * function returns the GP given a pointer to a generic linker file struct.
1208  */
1209 Elf_Addr
1210 link_elf_get_gp(linker_file_t lf)
1211 {
1212 	elf_file_t ef = (elf_file_t)lf;
1213 	return (Elf_Addr)ef->got;
1214 }
1215 #endif
1216 
1217 const Elf_Sym *
1218 elf_get_sym(linker_file_t lf, Elf_Word symidx)
1219 {
1220 	elf_file_t ef = (elf_file_t)lf;
1221 
1222 	if (symidx >= ef->nchains)
1223 		return (NULL);
1224 	return (ef->symtab + symidx);
1225 }
1226 
1227 const char *
1228 elf_get_symname(linker_file_t lf, Elf_Word symidx)
1229 {
1230 	elf_file_t ef = (elf_file_t)lf;
1231 	const Elf_Sym *sym;
1232 
1233 	if (symidx >= ef->nchains)
1234 		return (NULL);
1235 	sym = ef->symtab + symidx;
1236 	return (ef->strtab + sym->st_name);
1237 }
1238 
1239 /*
1240  * Symbol lookup function that can be used when the symbol index is known (ie
1241  * in relocations). It uses the symbol index instead of doing a fully fledged
1242  * hash table based lookup when such is valid. For example for local symbols.
1243  * This is not only more efficient, it's also more correct. It's not always
1244  * the case that the symbol can be found through the hash table.
1245  */
1246 Elf_Addr
1247 elf_lookup(linker_file_t lf, Elf_Word symidx, int deps)
1248 {
1249 	elf_file_t ef = (elf_file_t)lf;
1250 	const Elf_Sym *sym;
1251 	const char *symbol;
1252 
1253 	/* Don't even try to lookup the symbol if the index is bogus. */
1254 	if (symidx >= ef->nchains)
1255 		return (0);
1256 
1257 	sym = ef->symtab + symidx;
1258 
1259 	/*
1260 	 * Don't do a full lookup when the symbol is local. It may even
1261 	 * fail because it may not be found through the hash table.
1262 	 */
1263 	if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1264 		/* Force lookup failure when we have an insanity. */
1265 		if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0)
1266 			return (0);
1267 		return ((Elf_Addr)ef->address + sym->st_value);
1268 	}
1269 
1270 	/*
1271 	 * XXX we can avoid doing a hash table based lookup for global
1272 	 * symbols as well. This however is not always valid, so we'll
1273 	 * just do it the hard way for now. Performance tweaks can
1274 	 * always be added.
1275 	 */
1276 
1277 	symbol = ef->strtab + sym->st_name;
1278 
1279 	/* Force a lookup failure if the symbol name is bogus. */
1280 	if (*symbol == 0)
1281 		return (0);
1282 
1283 	return ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1284 }
1285 
1286 static void
1287 link_elf_reloc_local(linker_file_t lf)
1288 {
1289     const Elf_Rel *rellim;
1290     const Elf_Rel *rel;
1291     const Elf_Rela *relalim;
1292     const Elf_Rela *rela;
1293     elf_file_t ef = (elf_file_t)lf;
1294 
1295     /* Perform relocations without addend if there are any: */
1296     if ((rel = ef->rel) != NULL) {
1297 	rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
1298 	while (rel < rellim) {
1299 	    elf_reloc_local(lf, rel, ELF_RELOC_REL);
1300 	    rel++;
1301 	}
1302     }
1303 
1304     /* Perform relocations with addend if there are any: */
1305     if ((rela = ef->rela) != NULL) {
1306 	relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
1307 	while (rela < relalim) {
1308 	    elf_reloc_local(lf, rela, ELF_RELOC_RELA);
1309 	    rela++;
1310 	}
1311     }
1312 }
1313