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