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