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