xref: /freebsd/sys/kern/link_elf.c (revision 0640d357f29fb1c0daaaffadd0416c5981413afd)
1 /*-
2  * Copyright (c) 1998 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  *	$Id: link_elf.c,v 1.7 1998/10/16 03:55:00 peter Exp $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/proc.h>
34 #include <sys/namei.h>
35 #include <sys/fcntl.h>
36 #include <sys/vnode.h>
37 #include <sys/linker.h>
38 #include <machine/elf.h>
39 
40 #include <vm/vm.h>
41 #include <vm/vm_prot.h>
42 #include <vm/vm_param.h>
43 #include <sys/lock.h>
44 #include <vm/vm_object.h>
45 #include <vm/vm_kern.h>
46 #include <vm/vm_extern.h>
47 #include <vm/pmap.h>
48 #include <vm/vm_map.h>
49 
50 static int	link_elf_load_module(const char*, linker_file_t*);
51 static int	link_elf_load_file(const char*, linker_file_t*);
52 static int	link_elf_lookup_symbol(linker_file_t, const char*,
53 				       linker_sym_t*);
54 static int	link_elf_symbol_values(linker_file_t, linker_sym_t, linker_symval_t*);
55 static int	link_elf_search_symbol(linker_file_t, caddr_t value,
56 				       linker_sym_t* sym, long* diffp);
57 
58 static void	link_elf_unload_file(linker_file_t);
59 static void	link_elf_unload_module(linker_file_t);
60 
61 /*
62  * The file representing the currently running kernel.  This contains
63  * the global symbol table.
64  */
65 
66 linker_file_t linker_kernel_file;
67 
68 static struct linker_class_ops link_elf_class_ops = {
69     link_elf_load_module,
70 };
71 
72 static struct linker_file_ops link_elf_file_ops = {
73     link_elf_lookup_symbol,
74     link_elf_symbol_values,
75     link_elf_search_symbol,
76     link_elf_unload_file,
77 };
78 
79 static struct linker_file_ops link_elf_module_ops = {
80     link_elf_lookup_symbol,
81     link_elf_symbol_values,
82     link_elf_search_symbol,
83     link_elf_unload_module,
84 };
85 typedef struct elf_file {
86     caddr_t		address;	/* Relocation address */
87 #ifdef SPARSE_MAPPING
88     vm_object_t		object;		/* VM object to hold file pages */
89 #endif
90     const Elf_Dyn*	dynamic;	/* Symbol table etc. */
91     Elf_Off		nbuckets;	/* DT_HASH info */
92     Elf_Off		nchains;
93     const Elf_Off*	buckets;
94     const Elf_Off*	chains;
95     caddr_t		hash;
96     caddr_t		strtab;		/* DT_STRTAB */
97     int			strsz;		/* DT_STRSZ */
98     const Elf_Sym*	symtab;		/* DT_SYMTAB */
99     Elf_Addr*		got;		/* DT_PLTGOT */
100     const Elf_Rel*	pltrel;		/* DT_JMPREL */
101     int			pltrelsize;	/* DT_PLTRELSZ */
102     const Elf_Rela*	pltrela;	/* DT_JMPREL */
103     int			pltrelasize;	/* DT_PLTRELSZ */
104     const Elf_Rel*	rel;		/* DT_REL */
105     int			relsize;	/* DT_RELSZ */
106     const Elf_Rela*	rela;		/* DT_RELA */
107     int			relasize;	/* DT_RELASZ */
108     caddr_t		modptr;
109     const Elf_Sym*	ddbsymtab;	/* The symbol table we are using */
110     long		ddbsymcnt;	/* Number of symbols */
111     caddr_t		ddbstrtab;	/* String table */
112     long		ddbstrcnt;	/* number of bytes in string table */
113     caddr_t		symbase;	/* malloc'ed symbold base */
114     caddr_t		strbase;	/* malloc'ed string base */
115 } *elf_file_t;
116 
117 static int		parse_dynamic(linker_file_t lf);
118 static int		load_dependancies(linker_file_t lf);
119 static int		relocate_file(linker_file_t lf);
120 static int		parse_module_symbols(linker_file_t lf);
121 
122 /*
123  * The kernel symbol table starts here.
124  */
125 extern struct _dynamic _DYNAMIC;
126 
127 static void
128 link_elf_init(void* arg)
129 {
130 #ifdef __ELF__
131     Elf_Dyn	*dp;
132     caddr_t	modptr, baseptr, sizeptr;
133     elf_file_t	ef;
134     char	*modname;
135 #endif
136 
137 #if ELF_TARG_CLASS == ELFCLASS32
138     linker_add_class("elf32", NULL, &link_elf_class_ops);
139 #else
140     linker_add_class("elf64", NULL, &link_elf_class_ops);
141 #endif
142 
143 #ifdef __ELF__
144     dp = (Elf_Dyn*) &_DYNAMIC;
145     if (dp) {
146 	ef = malloc(sizeof(struct elf_file), M_LINKER, M_NOWAIT);
147 	if (ef == NULL)
148 	    panic("link_elf_init: Can't create linker structures for kernel");
149 	bzero(ef, sizeof(*ef));
150 
151 	ef->address = 0;
152 #ifdef SPARSE_MAPPING
153 	ef->object = 0;
154 #endif
155 	ef->dynamic = dp;
156 	modname = NULL;
157 	modptr = preload_search_by_type("elf kernel");
158 	if (modptr)
159 	    modname = (char *)preload_search_info(modptr, MODINFO_NAME);
160 	if (modname == NULL)
161 	    modname = "kernel";
162 	linker_kernel_file = linker_make_file(modname, ef, &link_elf_file_ops);
163 	if (linker_kernel_file == NULL)
164 	    panic("link_elf_init: Can't create linker structures for kernel");
165 	parse_dynamic(linker_kernel_file);
166 	/* Sigh, magic constants. */
167 #ifdef __alpha__
168 	linker_kernel_file->address = (caddr_t) 0xfffffc0000300000;
169 #else
170 	linker_kernel_file->address = (caddr_t) 0xf0100000;
171 #endif
172 	linker_kernel_file->size = -(long)linker_kernel_file->address;
173 
174 	if (modptr) {
175 	    ef->modptr = modptr;
176 	    baseptr = preload_search_info(modptr, MODINFO_ADDR);
177 	    if (baseptr)
178 		linker_kernel_file->address = *(caddr_t *)baseptr;
179 	    sizeptr = preload_search_info(modptr, MODINFO_SIZE);
180 	    if (sizeptr)
181 		linker_kernel_file->size = *(size_t *)sizeptr;
182 	}
183 	(void)parse_module_symbols(linker_kernel_file);
184 	linker_current_file = linker_kernel_file;
185     }
186 #endif
187 }
188 
189 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
190 
191 static int
192 parse_module_symbols(linker_file_t lf)
193 {
194     elf_file_t ef = lf->priv;
195     caddr_t	pointer;
196     caddr_t	ssym, esym, base;
197     caddr_t	strtab;
198     int		strcnt;
199     Elf_Sym*	symtab;
200     int		symcnt;
201 
202     if (ef->modptr == NULL)
203 	return 0;
204     pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM);
205     if (pointer == NULL)
206 	return 0;
207     ssym = *(caddr_t *)pointer;
208     pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM);
209     if (pointer == NULL)
210 	return 0;
211     esym = *(caddr_t *)pointer;
212 
213     base = ssym;
214 
215     symcnt = *(long *)base;
216     base += sizeof(long);
217     symtab = (Elf_Sym *)base;
218     base += roundup(symcnt, sizeof(long));
219 
220     if (base > esym || base < ssym) {
221 	printf("Symbols are corrupt!\n");
222 	return EINVAL;
223     }
224 
225     strcnt = *(long *)base;
226     base += sizeof(long);
227     strtab = base;
228     base += roundup(strcnt, sizeof(long));
229 
230     if (base > esym || base < ssym) {
231 	printf("Symbols are corrupt!\n");
232 	return EINVAL;
233     }
234 
235     ef->ddbsymtab = symtab;
236     ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
237     ef->ddbstrtab = strtab;
238     ef->ddbstrcnt = strcnt;
239 
240     return 0;
241 }
242 
243 static int
244 parse_dynamic(linker_file_t lf)
245 {
246     elf_file_t ef = lf->priv;
247     const Elf_Dyn *dp;
248     int plttype = DT_REL;
249 
250     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
251 	switch (dp->d_tag) {
252 	case DT_HASH:
253 	{
254 	    /* From src/libexec/rtld-elf/rtld.c */
255 	    const Elf_Off *hashtab = (const Elf_Off *)
256 		(ef->address + dp->d_un.d_ptr);
257 	    ef->nbuckets = hashtab[0];
258 	    ef->nchains = hashtab[1];
259 	    ef->buckets = hashtab + 2;
260 	    ef->chains = ef->buckets + ef->nbuckets;
261 	    break;
262 	}
263 	case DT_STRTAB:
264 	    ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
265 	    break;
266 	case DT_STRSZ:
267 	    ef->strsz = dp->d_un.d_val;
268 	    break;
269 	case DT_SYMTAB:
270 	    ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
271 	    break;
272 	case DT_SYMENT:
273 	    if (dp->d_un.d_val != sizeof(Elf_Sym))
274 		return ENOEXEC;
275 	    break;
276 	case DT_PLTGOT:
277 	    ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
278 	    break;
279 	case DT_REL:
280 	    ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
281 	    break;
282 	case DT_RELSZ:
283 	    ef->relsize = dp->d_un.d_val;
284 	    break;
285 	case DT_RELENT:
286 	    if (dp->d_un.d_val != sizeof(Elf_Rel))
287 		return ENOEXEC;
288 	    break;
289 	case DT_JMPREL:
290 	    ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
291 	    break;
292 	case DT_PLTRELSZ:
293 	    ef->pltrelsize = dp->d_un.d_val;
294 	    break;
295 	case DT_RELA:
296 	    ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
297 	    break;
298 	case DT_RELASZ:
299 	    ef->relasize = dp->d_un.d_val;
300 	    break;
301 	case DT_RELAENT:
302 	    if (dp->d_un.d_val != sizeof(Elf_Rela))
303 		return ENOEXEC;
304 	    break;
305 	case DT_PLTREL:
306 	    plttype = dp->d_un.d_val;
307 	    if (plttype != DT_REL && plttype != DT_RELA)
308 		return ENOEXEC;
309 	    break;
310 	}
311     }
312 
313     if (plttype == DT_RELA) {
314 	ef->pltrela = (const Elf_Rela *) ef->pltrel;
315 	ef->pltrel = NULL;
316 	ef->pltrelasize = ef->pltrelsize;
317 	ef->pltrelsize = 0;
318     }
319 
320     ef->ddbsymtab = ef->symtab;
321     ef->ddbsymcnt = ef->nchains;
322     ef->ddbstrtab = ef->strtab;
323     ef->ddbstrcnt = ef->strsz;
324 
325     return 0;
326 }
327 
328 static void
329 link_elf_error(const char *s)
330 {
331     printf("kldload: %s\n", s);
332 }
333 
334 static int
335 link_elf_load_module(const char *filename, linker_file_t *result)
336 {
337     caddr_t		modptr, baseptr, sizeptr, dynptr;
338     char		*type;
339     elf_file_t		ef;
340     linker_file_t	lf;
341     int			error;
342     vm_offset_t		dp;
343 
344     /* Look to see if we have the module preloaded */
345     modptr = preload_search_by_name(filename);
346     if (modptr == NULL)
347 	return (link_elf_load_file(filename, result));
348 
349     /* It's preloaded, check we can handle it and collect information */
350     type = (char *)preload_search_info(modptr, MODINFO_TYPE);
351     baseptr = preload_search_info(modptr, MODINFO_ADDR);
352     sizeptr = preload_search_info(modptr, MODINFO_SIZE);
353     dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC);
354     if (type == NULL || strcmp(type, "elf module") != 0)
355 	return (EFTYPE);
356     if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
357 	return (EINVAL);
358 
359     ef = malloc(sizeof(struct elf_file), M_LINKER, M_WAITOK);
360     if (ef == NULL)
361 	return (ENOMEM);
362     bzero(ef, sizeof(*ef));
363     ef->modptr = modptr;
364     ef->address = *(caddr_t *)baseptr;
365 #ifdef SPARSE_MAPPING
366     ef->object = 0;
367 #endif
368     dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
369     ef->dynamic = (Elf_Dyn *)dp;
370     lf = linker_make_file(filename, ef, &link_elf_module_ops);
371     if (lf == NULL) {
372 	free(ef, M_LINKER);
373 	return ENOMEM;
374     }
375     lf->address = ef->address;
376     lf->size = *(size_t *)sizeptr;
377 
378     error = parse_dynamic(lf);
379     if (error) {
380 	linker_file_unload(lf);
381 	return error;
382     }
383     error = load_dependancies(lf);
384     if (error) {
385 	linker_file_unload(lf);
386 	return error;
387     }
388     error = relocate_file(lf);
389     if (error) {
390 	linker_file_unload(lf);
391 	return error;
392     }
393     (void)parse_module_symbols(lf);
394     *result = lf;
395     return (0);
396 }
397 
398 static int
399 link_elf_load_file(const char* filename, linker_file_t* result)
400 {
401     struct nameidata nd;
402     struct proc* p = curproc;	/* XXX */
403     union {
404 	Elf_Ehdr hdr;
405 	char buf[PAGE_SIZE];
406     } u;
407     int nbytes, i;
408     Elf_Phdr *phdr;
409     Elf_Phdr *phlimit;
410     Elf_Phdr *segs[2];
411     int nsegs;
412     Elf_Phdr *phdyn;
413     Elf_Phdr *phphdr;
414     caddr_t mapbase;
415     size_t mapsize;
416     Elf_Off base_offset;
417     Elf_Addr base_vaddr;
418     Elf_Addr base_vlimit;
419     int error = 0;
420     int resid;
421     elf_file_t ef;
422     linker_file_t lf;
423     char *pathname;
424     Elf_Shdr *shdr;
425     int symtabindex;
426     int symstrindex;
427     int symcnt;
428     int strcnt;
429 
430     shdr = NULL;
431     lf = NULL;
432 
433     pathname = linker_search_path(filename);
434     if (pathname == NULL)
435 	return ENOENT;
436     NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, pathname, p);
437     error = vn_open(&nd, FREAD, 0);
438     free(pathname, M_LINKER);
439     if (error)
440 	return error;
441 
442     /*
443      * Read the elf header from the file.
444      */
445     error = vn_rdwr(UIO_READ, nd.ni_vp, (void*) &u, sizeof u, 0,
446 		    UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p);
447     nbytes = sizeof u - resid;
448     if (error)
449 	goto out;
450 
451     if (!IS_ELF(u.hdr)) {
452 	error = ENOEXEC;
453 	goto out;
454     }
455 
456     if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS
457       || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) {
458 	link_elf_error("Unsupported file layout");
459 	error = ENOEXEC;
460 	goto out;
461     }
462     if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT
463       || u.hdr.e_version != EV_CURRENT) {
464 	link_elf_error("Unsupported file version");
465 	error = ENOEXEC;
466 	goto out;
467     }
468     if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) {
469 	link_elf_error("Unsupported file type");
470 	error = ENOEXEC;
471 	goto out;
472     }
473     if (u.hdr.e_machine != ELF_TARG_MACH) {
474 	link_elf_error("Unsupported machine");
475 	error = ENOEXEC;
476 	goto out;
477     }
478 
479     /*
480      * We rely on the program header being in the first page.  This is
481      * not strictly required by the ABI specification, but it seems to
482      * always true in practice.  And, it simplifies things considerably.
483      */
484     if (!((u.hdr.e_phentsize == sizeof(Elf_Phdr))
485 	  || (u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE)
486 	  || (u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) <= nbytes)))
487 	link_elf_error("Unreadable program headers");
488 
489     /*
490      * Scan the program header entries, and save key information.
491      *
492      * We rely on there being exactly two load segments, text and data,
493      * in that order.
494      */
495     phdr = (Elf_Phdr *) (u.buf + u.hdr.e_phoff);
496     phlimit = phdr + u.hdr.e_phnum;
497     nsegs = 0;
498     phdyn = NULL;
499     phphdr = NULL;
500     while (phdr < phlimit) {
501 	switch (phdr->p_type) {
502 
503 	case PT_LOAD:
504 	    if (nsegs == 2) {
505 		link_elf_error("Too many sections");
506 		error = ENOEXEC;
507 		goto out;
508 	    }
509 	    segs[nsegs] = phdr;
510 	    ++nsegs;
511 	    break;
512 
513 	case PT_PHDR:
514 	    phphdr = phdr;
515 	    break;
516 
517 	case PT_DYNAMIC:
518 	    phdyn = phdr;
519 	    break;
520 	}
521 
522 	++phdr;
523     }
524     if (phdyn == NULL) {
525 	link_elf_error("Object is not dynamically-linked");
526 	error = ENOEXEC;
527 	goto out;
528     }
529 
530     /*
531      * Allocate the entire address space of the object, to stake out our
532      * contiguous region, and to establish the base address for relocation.
533      */
534     base_offset = trunc_page(segs[0]->p_offset);
535     base_vaddr = trunc_page(segs[0]->p_vaddr);
536     base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
537     mapsize = base_vlimit - base_vaddr;
538 
539     ef = malloc(sizeof(struct elf_file), M_LINKER, M_WAITOK);
540     bzero(ef, sizeof(*ef));
541 #ifdef SPARSE_MAPPING
542     ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
543     if (ef->object == NULL) {
544 	free(ef, M_LINKER);
545 	error = ENOMEM;
546 	goto out;
547     }
548     vm_object_reference(ef->object);
549     ef->address = (caddr_t) vm_map_min(kernel_map);
550     error = vm_map_find(kernel_map, ef->object, 0,
551 			(vm_offset_t *) &ef->address,
552 			mapsize, 1,
553 			VM_PROT_ALL, VM_PROT_ALL, 0);
554     if (error) {
555 	vm_object_deallocate(ef->object);
556 	free(ef, M_LINKER);
557 	goto out;
558     }
559 #else
560     ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
561 #endif
562     mapbase = ef->address;
563 
564     /*
565      * Read the text and data sections and zero the bss.
566      */
567     for (i = 0; i < 2; i++) {
568 	caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
569 	error = vn_rdwr(UIO_READ, nd.ni_vp,
570 			segbase, segs[i]->p_filesz, segs[i]->p_offset,
571 			UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p);
572 	if (error) {
573 #ifdef SPARSE_MAPPING
574 	    vm_map_remove(kernel_map, (vm_offset_t) ef->address,
575 			  (vm_offset_t) ef->address
576 			  + (ef->object->size << PAGE_SHIFT));
577 	    vm_object_deallocate(ef->object);
578 #else
579 	    free(ef->address, M_LINKER);
580 #endif
581 	    free(ef, M_LINKER);
582 	    goto out;
583 	}
584 	bzero(segbase + segs[i]->p_filesz,
585 	      segs[i]->p_memsz - segs[i]->p_filesz);
586 
587 #ifdef SPARSE_MAPPING
588 	/*
589 	 * Wire down the pages
590 	 */
591 	vm_map_pageable(kernel_map,
592 			(vm_offset_t) segbase,
593 			(vm_offset_t) segbase + segs[i]->p_memsz,
594 			FALSE);
595 #endif
596     }
597 
598     ef->dynamic = (const Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
599 
600     lf = linker_make_file(filename, ef, &link_elf_file_ops);
601     if (lf == NULL) {
602 #ifdef SPARSE_MAPPING
603 	vm_map_remove(kernel_map, (vm_offset_t) ef->address,
604 		      (vm_offset_t) ef->address
605 		      + (ef->object->size << PAGE_SHIFT));
606 	vm_object_deallocate(ef->object);
607 #else
608 	free(ef->address, M_LINKER);
609 #endif
610 	free(ef, M_LINKER);
611 	error = ENOMEM;
612 	goto out;
613     }
614     lf->address = ef->address;
615     lf->size = mapsize;
616 
617     error = parse_dynamic(lf);
618     if (error)
619 	goto out;
620     error = load_dependancies(lf);
621     if (error)
622 	goto out;
623     error = relocate_file(lf);
624     if (error)
625 	goto out;
626 
627     /* Try and load the symbol table if it's present.  (you can strip it!) */
628     nbytes = u.hdr.e_shnum * u.hdr.e_shentsize;
629     if (nbytes == 0 || u.hdr.e_shoff == 0)
630 	goto nosyms;
631     shdr = malloc(nbytes, M_LINKER, M_WAITOK);
632     if (shdr == NULL) {
633 	error = ENOMEM;
634 	goto out;
635     }
636     bzero(shdr, nbytes);
637     error = vn_rdwr(UIO_READ, nd.ni_vp,
638 		    (caddr_t)shdr, nbytes, u.hdr.e_shoff,
639 		    UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p);
640     if (error)
641 	goto out;
642     symtabindex = -1;
643     symstrindex = -1;
644     for (i = 0; i < u.hdr.e_shnum; i++) {
645 	if (shdr[i].sh_type == SHT_SYMTAB) {
646 	    symtabindex = i;
647 	    symstrindex = shdr[i].sh_link;
648 	}
649     }
650     if (symtabindex < 0 || symstrindex < 0)
651 	goto nosyms;
652 
653     symcnt = shdr[symtabindex].sh_size;
654     ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
655     strcnt = shdr[symstrindex].sh_size;
656     ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
657 
658     if (ef->symbase == NULL || ef->strbase == NULL) {
659 	error = ENOMEM;
660 	goto out;
661     }
662     error = vn_rdwr(UIO_READ, nd.ni_vp,
663 		    ef->symbase, symcnt, shdr[symtabindex].sh_offset,
664 		    UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p);
665     if (error)
666 	goto out;
667     error = vn_rdwr(UIO_READ, nd.ni_vp,
668 		    ef->strbase, strcnt, shdr[symstrindex].sh_offset,
669 		    UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p);
670     if (error)
671 	goto out;
672 
673     ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
674     ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
675     ef->ddbstrcnt = strcnt;
676     ef->ddbstrtab = ef->strbase;
677 
678 nosyms:
679 
680     *result = lf;
681 
682 out:
683     if (error && lf)
684 	linker_file_unload(lf);
685     if (shdr)
686 	free(shdr, M_LINKER);
687     VOP_UNLOCK(nd.ni_vp, 0, p);
688     vn_close(nd.ni_vp, FREAD, p->p_ucred, p);
689 
690     return error;
691 }
692 
693 static void
694 link_elf_unload_file(linker_file_t file)
695 {
696     elf_file_t ef = file->priv;
697 
698     if (ef) {
699 #ifdef SPARSE_MAPPING
700 	if (ef->object) {
701 	    vm_map_remove(kernel_map, (vm_offset_t) ef->address,
702 			  (vm_offset_t) ef->address
703 			  + (ef->object->size << PAGE_SHIFT));
704 	    vm_object_deallocate(ef->object);
705 	}
706 #else
707 	if (ef->address)
708 	    free(ef->address, M_LINKER);
709 #endif
710 	if (ef->symbase)
711 	    free(ef->symbase, M_LINKER);
712 	if (ef->strbase)
713 	    free(ef->strbase, M_LINKER);
714 	free(ef, M_LINKER);
715     }
716 }
717 
718 static void
719 link_elf_unload_module(linker_file_t file)
720 {
721     elf_file_t ef = file->priv;
722 
723     if (ef)
724 	free(ef, M_LINKER);
725     if (file->filename)
726 	preload_delete_name(file->filename);
727 }
728 
729 static int
730 load_dependancies(linker_file_t lf)
731 {
732     elf_file_t ef = lf->priv;
733     linker_file_t lfdep;
734     char* name;
735     const Elf_Dyn *dp;
736     int error = 0;
737 
738     /*
739      * All files are dependant on /kernel.
740      */
741     linker_kernel_file->refs++;
742     linker_file_add_dependancy(lf, linker_kernel_file);
743 
744 
745     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
746 	if (dp->d_tag == DT_NEEDED) {
747 	    name = ef->strtab + dp->d_un.d_val;
748 
749 	    error = linker_load_file(name, &lfdep);
750 	    if (error)
751 		goto out;
752 	    error = linker_file_add_dependancy(lf, lfdep);
753 	    if (error)
754 		goto out;
755 	}
756     }
757 
758 out:
759     return error;
760 }
761 
762 static const char *
763 symbol_name(elf_file_t ef, Elf_Word r_info)
764 {
765     const Elf_Sym *ref;
766 
767     if (ELF_R_SYM(r_info)) {
768 	ref = ef->symtab + ELF_R_SYM(r_info);
769 	return ef->strtab + ref->st_name;
770     } else
771 	return NULL;
772 }
773 
774 static int
775 relocate_file(linker_file_t lf)
776 {
777     elf_file_t ef = lf->priv;
778     const Elf_Rel *rellim;
779     const Elf_Rel *rel;
780     const Elf_Rela *relalim;
781     const Elf_Rela *rela;
782     const char *symname;
783 
784     /* Perform relocations without addend if there are any: */
785     rel = ef->rel;
786     if (rel) {
787 	rellim = (const Elf_Rel *) ((caddr_t) ef->rel + ef->relsize);
788 	while (rel < rellim) {
789 	    symname = symbol_name(ef, rel->r_info);
790 	    if (elf_reloc(lf, rel, ELF_RELOC_REL, symname))
791 		return ENOENT;
792 	    rel++;
793 	}
794     }
795 
796     /* Perform relocations with addend if there are any: */
797     rela = ef->rela;
798     if (rela) {
799 	relalim = (const Elf_Rela *) ((caddr_t) ef->rela + ef->relasize);
800 	while (rela < relalim) {
801 	    symname = symbol_name(ef, rela->r_info);
802 	    if (elf_reloc(lf, rela, ELF_RELOC_RELA, symname))
803 		return ENOENT;
804 	    rela++;
805 	}
806     }
807 
808     /* Perform PLT relocations without addend if there are any: */
809     rel = ef->pltrel;
810     if (rel) {
811 	rellim = (const Elf_Rel *) ((caddr_t) ef->pltrel + ef->pltrelsize);
812 	while (rel < rellim) {
813 	    symname = symbol_name(ef, rel->r_info);
814 	    if (elf_reloc(lf, rel, ELF_RELOC_REL, symname))
815 		return ENOENT;
816 	    rel++;
817 	}
818     }
819 
820     /* Perform relocations with addend if there are any: */
821     rela = ef->pltrela;
822     if (rela) {
823 	relalim = (const Elf_Rela *) ((caddr_t) ef->pltrela + ef->pltrelasize);
824 	while (rela < relalim) {
825 	    symname = symbol_name(ef, rela->r_info);
826 	    if (elf_reloc(lf, rela, ELF_RELOC_RELA, symname))
827 		return ENOENT;
828 	    rela++;
829 	}
830     }
831 
832     return 0;
833 }
834 
835 /*
836  * Hash function for symbol table lookup.  Don't even think about changing
837  * this.  It is specified by the System V ABI.
838  */
839 static unsigned long
840 elf_hash(const char *name)
841 {
842     const unsigned char *p = (const unsigned char *) name;
843     unsigned long h = 0;
844     unsigned long g;
845 
846     while (*p != '\0') {
847 	h = (h << 4) + *p++;
848 	if ((g = h & 0xf0000000) != 0)
849 	    h ^= g >> 24;
850 	h &= ~g;
851     }
852     return h;
853 }
854 
855 int
856 link_elf_lookup_symbol(linker_file_t lf, const char* name, linker_sym_t* sym)
857 {
858     elf_file_t ef = lf->priv;
859     unsigned long symnum;
860     const Elf_Sym* symp;
861     const char *strp;
862     unsigned long hash;
863     int i;
864 
865     /* First, search hashed global symbols */
866     hash = elf_hash(name);
867     symnum = ef->buckets[hash % ef->nbuckets];
868 
869     while (symnum != STN_UNDEF) {
870 	if (symnum >= ef->nchains) {
871 	    printf("link_elf_lookup_symbol: corrupt symbol table\n");
872 	    return ENOENT;
873 	}
874 
875 	symp = ef->symtab + symnum;
876 	if (symp->st_name == 0) {
877 	    printf("link_elf_lookup_symbol: corrupt symbol table\n");
878 	    return ENOENT;
879 	}
880 
881 	strp = ef->strtab + symp->st_name;
882 
883 	if (strcmp(name, strp) == 0) {
884 	    if (symp->st_shndx != SHN_UNDEF ||
885 		(symp->st_value != 0 &&
886 		 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
887 		*sym = (linker_sym_t) symp;
888 		return 0;
889 	    } else
890 		return ENOENT;
891 	}
892 
893 	symnum = ef->chains[symnum];
894     }
895 
896     /* If we have not found it, look at the full table (if loaded) */
897     if (ef->symtab == ef->ddbsymtab)
898 	return ENOENT;
899 
900     /* Exhaustive search */
901     for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
902 	strp = ef->ddbstrtab + symp->st_name;
903 	if (strcmp(name, strp) == 0) {
904 	    if (symp->st_shndx != SHN_UNDEF ||
905 		(symp->st_value != 0 &&
906 		 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
907 		*sym = (linker_sym_t) symp;
908 		return 0;
909 	    } else
910 		return ENOENT;
911 	}
912     }
913 
914     return ENOENT;
915 }
916 
917 static int
918 link_elf_symbol_values(linker_file_t lf, linker_sym_t sym, linker_symval_t* symval)
919 {
920 	elf_file_t ef = lf->priv;
921 	Elf_Sym* es = (Elf_Sym*) sym;
922 
923 	if (es >= ef->symtab && ((es - ef->symtab) < ef->nchains)) {
924 	    symval->name = ef->strtab + es->st_name;
925 	    symval->value = (caddr_t) ef->address + es->st_value;
926 	    symval->size = es->st_size;
927 	    return 0;
928 	}
929 	if (ef->symtab == ef->ddbsymtab)
930 	    return ENOENT;
931 	if (es >= ef->ddbsymtab && ((es - ef->ddbsymtab) < ef->ddbsymcnt)) {
932 	    symval->name = ef->ddbstrtab + es->st_name;
933 	    symval->value = (caddr_t) ef->address + es->st_value;
934 	    symval->size = es->st_size;
935 	    return 0;
936 	}
937 	return ENOENT;
938 }
939 
940 static int
941 link_elf_search_symbol(linker_file_t lf, caddr_t value,
942 		       linker_sym_t* sym, long* diffp)
943 {
944 	elf_file_t ef = lf->priv;
945 	u_long off = (u_long) value;
946 	u_long diff = off;
947 	const Elf_Sym* es;
948 	const Elf_Sym* best = 0;
949 	int i;
950 
951 	for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
952 		if (es->st_name == 0)
953 			continue;
954 		if (off >= es->st_value) {
955 			if (off - es->st_value < diff) {
956 				diff = off - es->st_value;
957 				best = es;
958 				if (diff == 0)
959 					break;
960 			} else if (off - es->st_value == diff) {
961 				best = es;
962 			}
963 		}
964 	}
965 	if (best == 0)
966 		*diffp = off;
967 	else
968 		*diffp = diff;
969 	*sym = (linker_sym_t) best;
970 
971 	return 0;
972 }
973