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