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