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