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