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