Fully synchronize sys/boot from FreeBSD-5.x, but add / to the module path
[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 $
cde87949 27 * $DragonFly: src/sys/kern/link_elf.c,v 1.7 2003/10/02 21:00:20 hmp 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;
dadab5e9
MD
395 struct thread *td = curthread; /* XXX */
396 struct proc *p = td->td_proc;
984263bc
MD
397 Elf_Ehdr *hdr;
398 caddr_t firstpage;
399 int nbytes, i;
400 Elf_Phdr *phdr;
401 Elf_Phdr *phlimit;
402 Elf_Phdr *segs[2];
403 int nsegs;
404 Elf_Phdr *phdyn;
405 Elf_Phdr *phphdr;
406 caddr_t mapbase;
407 size_t mapsize;
408 Elf_Off base_offset;
409 Elf_Addr base_vaddr;
410 Elf_Addr base_vlimit;
411 int error = 0;
412 int resid;
413 elf_file_t ef;
414 linker_file_t lf;
415 char *pathname;
416 Elf_Shdr *shdr;
417 int symtabindex;
418 int symstrindex;
419 int symcnt;
420 int strcnt;
421
dadab5e9 422 KKASSERT(p != NULL);
984263bc
MD
423 shdr = NULL;
424 lf = NULL;
984263bc
MD
425 pathname = linker_search_path(filename);
426 if (pathname == NULL)
427 return ENOENT;
428
2b69e610 429 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_SYSSPACE, pathname, td);
984263bc
MD
430 error = vn_open(&nd, FREAD, 0);
431 free(pathname, M_LINKER);
432 if (error)
433 return error;
434 NDFREE(&nd, NDF_ONLY_PNBUF);
435
436 /*
437 * Read the elf header from the file.
438 */
439 firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
440 if (firstpage == NULL) {
441 error = ENOMEM;
442 goto out;
443 }
444 hdr = (Elf_Ehdr *)firstpage;
445 error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
dadab5e9 446 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, td);
984263bc
MD
447 nbytes = PAGE_SIZE - resid;
448 if (error)
449 goto out;
450
451 if (!IS_ELF(*hdr)) {
452 error = ENOEXEC;
453 goto out;
454 }
455
456 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
457 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
458 link_elf_error("Unsupported file layout");
459 error = ENOEXEC;
460 goto out;
461 }
462 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
463 || hdr->e_version != EV_CURRENT) {
464 link_elf_error("Unsupported file version");
465 error = ENOEXEC;
466 goto out;
467 }
468 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
469 link_elf_error("Unsupported file type");
470 error = ENOEXEC;
471 goto out;
472 }
473 if (hdr->e_machine != ELF_TARG_MACH) {
474 link_elf_error("Unsupported machine");
475 error = ENOEXEC;
476 goto out;
477 }
478
479 /*
480 * We rely on the program header being in the first page. This is
481 * not strictly required by the ABI specification, but it seems to
482 * always true in practice. And, it simplifies things considerably.
483 */
484 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
485 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
486 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
487 link_elf_error("Unreadable program headers");
488
489 /*
490 * Scan the program header entries, and save key information.
491 *
492 * We rely on there being exactly two load segments, text and data,
493 * in that order.
494 */
495 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
496 phlimit = phdr + hdr->e_phnum;
497 nsegs = 0;
498 phdyn = NULL;
499 phphdr = NULL;
500 while (phdr < phlimit) {
501 switch (phdr->p_type) {
502
503 case PT_LOAD:
504 if (nsegs == 2) {
505 link_elf_error("Too many sections");
506 error = ENOEXEC;
507 goto out;
508 }
509 segs[nsegs] = phdr;
510 ++nsegs;
511 break;
512
513 case PT_PHDR:
514 phphdr = phdr;
515 break;
516
517 case PT_DYNAMIC:
518 phdyn = phdr;
519 break;
520 }
521
522 ++phdr;
523 }
524 if (phdyn == NULL) {
525 link_elf_error("Object is not dynamically-linked");
526 error = ENOEXEC;
527 goto out;
528 }
529
530 /*
531 * Allocate the entire address space of the object, to stake out our
532 * contiguous region, and to establish the base address for relocation.
533 */
534 base_offset = trunc_page(segs[0]->p_offset);
535 base_vaddr = trunc_page(segs[0]->p_vaddr);
536 base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
537 mapsize = base_vlimit - base_vaddr;
538
539 ef = malloc(sizeof(struct elf_file), M_LINKER, M_WAITOK);
540 bzero(ef, sizeof(*ef));
541#ifdef SPARSE_MAPPING
542 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
543 if (ef->object == NULL) {
544 free(ef, M_LINKER);
545 error = ENOMEM;
546 goto out;
547 }
548 vm_object_reference(ef->object);
549 ef->address = (caddr_t) vm_map_min(kernel_map);
550 error = vm_map_find(kernel_map, ef->object, 0,
551 (vm_offset_t *) &ef->address,
552 mapsize, 1,
553 VM_PROT_ALL, VM_PROT_ALL, 0);
554 if (error) {
555 vm_object_deallocate(ef->object);
556 free(ef, M_LINKER);
557 goto out;
558 }
559#else
560 ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
561#endif
562 mapbase = ef->address;
563
564 /*
565 * Read the text and data sections and zero the bss.
566 */
567 for (i = 0; i < 2; i++) {
568 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
569 error = vn_rdwr(UIO_READ, nd.ni_vp,
570 segbase, segs[i]->p_filesz, segs[i]->p_offset,
dadab5e9 571 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, td);
984263bc
MD
572 if (error) {
573#ifdef SPARSE_MAPPING
574 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
575 (vm_offset_t) ef->address
576 + (ef->object->size << PAGE_SHIFT));
577 vm_object_deallocate(ef->object);
578#else
579 free(ef->address, M_LINKER);
580#endif
581 free(ef, M_LINKER);
582 goto out;
583 }
584 bzero(segbase + segs[i]->p_filesz,
585 segs[i]->p_memsz - segs[i]->p_filesz);
586
587#ifdef SPARSE_MAPPING
588 /*
589 * Wire down the pages
590 */
cde87949 591 vm_map_wire(kernel_map,
984263bc
MD
592 (vm_offset_t) segbase,
593 (vm_offset_t) segbase + segs[i]->p_memsz,
594 FALSE);
595#endif
596 }
597
598 ef->dynamic = (const Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
599
600 lf = linker_make_file(filename, ef, &link_elf_file_ops);
601 if (lf == NULL) {
602#ifdef SPARSE_MAPPING
603 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
604 (vm_offset_t) ef->address
605 + (ef->object->size << PAGE_SHIFT));
606 vm_object_deallocate(ef->object);
607#else
608 free(ef->address, M_LINKER);
609#endif
610 free(ef, M_LINKER);
611 error = ENOMEM;
612 goto out;
613 }
614 lf->address = ef->address;
615 lf->size = mapsize;
616
617 error = parse_dynamic(lf);
618 if (error)
619 goto out;
620 error = load_dependancies(lf);
621 if (error)
622 goto out;
623 error = relocate_file(lf);
624 if (error)
625 goto out;
626
627 /* Try and load the symbol table if it's present. (you can strip it!) */
628 nbytes = hdr->e_shnum * hdr->e_shentsize;
629 if (nbytes == 0 || hdr->e_shoff == 0)
630 goto nosyms;
631 shdr = malloc(nbytes, M_LINKER, M_WAITOK);
632 if (shdr == NULL) {
633 error = ENOMEM;
634 goto out;
635 }
636 bzero(shdr, nbytes);
637 error = vn_rdwr(UIO_READ, nd.ni_vp,
638 (caddr_t)shdr, nbytes, hdr->e_shoff,
dadab5e9 639 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, td);
984263bc
MD
640 if (error)
641 goto out;
642 symtabindex = -1;
643 symstrindex = -1;
644 for (i = 0; i < hdr->e_shnum; i++) {
645 if (shdr[i].sh_type == SHT_SYMTAB) {
646 symtabindex = i;
647 symstrindex = shdr[i].sh_link;
648 }
649 }
650 if (symtabindex < 0 || symstrindex < 0)
651 goto nosyms;
652
653 symcnt = shdr[symtabindex].sh_size;
654 ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
655 strcnt = shdr[symstrindex].sh_size;
656 ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
657
658 if (ef->symbase == NULL || ef->strbase == NULL) {
659 error = ENOMEM;
660 goto out;
661 }
662 error = vn_rdwr(UIO_READ, nd.ni_vp,
663 ef->symbase, symcnt, shdr[symtabindex].sh_offset,
dadab5e9 664 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, td);
984263bc
MD
665 if (error)
666 goto out;
667 error = vn_rdwr(UIO_READ, nd.ni_vp,
668 ef->strbase, strcnt, shdr[symstrindex].sh_offset,
dadab5e9 669 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, td);
984263bc
MD
670 if (error)
671 goto out;
672
673 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
674 ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
675 ef->ddbstrcnt = strcnt;
676 ef->ddbstrtab = ef->strbase;
677
678 lf->flags |= LINKER_FILE_LINKED;
679
680nosyms:
681
682 *result = lf;
683
684out:
685 if (error && lf)
686 linker_file_unload(lf);
687 if (shdr)
688 free(shdr, M_LINKER);
689 if (firstpage)
690 free(firstpage, M_LINKER);
dadab5e9 691 VOP_UNLOCK(nd.ni_vp, 0, td);
3b568787 692 vn_close(nd.ni_vp, FREAD, td);
984263bc
MD
693
694 return error;
695}
696
697static void
698link_elf_unload_file(linker_file_t file)
699{
700 elf_file_t ef = file->priv;
701
702 if (ef) {
703#ifdef SPARSE_MAPPING
704 if (ef->object) {
705 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
706 (vm_offset_t) ef->address
707 + (ef->object->size << PAGE_SHIFT));
708 vm_object_deallocate(ef->object);
709 }
710#else
711 if (ef->address)
712 free(ef->address, M_LINKER);
713#endif
714 if (ef->symbase)
715 free(ef->symbase, M_LINKER);
716 if (ef->strbase)
717 free(ef->strbase, M_LINKER);
718 free(ef, M_LINKER);
719 }
720}
721
722static void
723link_elf_unload_module(linker_file_t file)
724{
725 elf_file_t ef = file->priv;
726
727 if (ef)
728 free(ef, M_LINKER);
729 if (file->filename)
730 preload_delete_name(file->filename);
731}
732
733static int
734load_dependancies(linker_file_t lf)
735{
736 elf_file_t ef = lf->priv;
737 linker_file_t lfdep;
738 char* name;
739 const Elf_Dyn *dp;
740 int error = 0;
741
742 /*
743 * All files are dependant on /kernel.
744 */
745 if (linker_kernel_file) {
746 linker_kernel_file->refs++;
747 linker_file_add_dependancy(lf, linker_kernel_file);
748 }
749
750 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
751 if (dp->d_tag == DT_NEEDED) {
752 name = ef->strtab + dp->d_un.d_val;
753
754 error = linker_load_file(name, &lfdep);
755 if (error)
756 goto out;
757 error = linker_file_add_dependancy(lf, lfdep);
758 if (error)
759 goto out;
760 }
761 }
762
763out:
764 return error;
765}
766
767static const char *
768symbol_name(elf_file_t ef, Elf_Word r_info)
769{
770 const Elf_Sym *ref;
771
772 if (ELF_R_SYM(r_info)) {
773 ref = ef->symtab + ELF_R_SYM(r_info);
774 return ef->strtab + ref->st_name;
775 } else
776 return NULL;
777}
778
779static int
780relocate_file(linker_file_t lf)
781{
782 elf_file_t ef = lf->priv;
783 const Elf_Rel *rellim;
784 const Elf_Rel *rel;
785 const Elf_Rela *relalim;
786 const Elf_Rela *rela;
787 const char *symname;
788
789 /* Perform relocations without addend if there are any: */
790 rel = ef->rel;
791 if (rel) {
792 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
793 while (rel < rellim) {
794 symname = symbol_name(ef, rel->r_info);
795 if (elf_reloc(lf, rel, ELF_RELOC_REL, symname)) {
796 printf("link_elf: symbol %s undefined\n", symname);
797 return ENOENT;
798 }
799 rel++;
800 }
801 }
802
803 /* Perform relocations with addend if there are any: */
804 rela = ef->rela;
805 if (rela) {
806 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
807 while (rela < relalim) {
808 symname = symbol_name(ef, rela->r_info);
809 if (elf_reloc(lf, rela, ELF_RELOC_RELA, symname)) {
810 printf("link_elf: symbol %s undefined\n", symname);
811 return ENOENT;
812 }
813 rela++;
814 }
815 }
816
817 /* Perform PLT relocations without addend if there are any: */
818 rel = ef->pltrel;
819 if (rel) {
820 rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
821 while (rel < rellim) {
822 symname = symbol_name(ef, rel->r_info);
823 if (elf_reloc(lf, rel, ELF_RELOC_REL, symname)) {
824 printf("link_elf: symbol %s undefined\n", symname);
825 return ENOENT;
826 }
827 rel++;
828 }
829 }
830
831 /* Perform relocations with addend if there are any: */
832 rela = ef->pltrela;
833 if (rela) {
834 relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
835 while (rela < relalim) {
836 symname = symbol_name(ef, rela->r_info);
837 if (elf_reloc(lf, rela, ELF_RELOC_RELA, symname)) {
838 printf("link_elf: symbol %s undefined\n", symname);
839 return ENOENT;
840 }
841 rela++;
842 }
843 }
844
845 return 0;
846}
847
848/*
849 * Hash function for symbol table lookup. Don't even think about changing
850 * this. It is specified by the System V ABI.
851 */
852static unsigned long
853elf_hash(const char *name)
854{
855 const unsigned char *p = (const unsigned char *) name;
856 unsigned long h = 0;
857 unsigned long g;
858
859 while (*p != '\0') {
860 h = (h << 4) + *p++;
861 if ((g = h & 0xf0000000) != 0)
862 h ^= g >> 24;
863 h &= ~g;
864 }
865 return h;
866}
867
868int
869link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
870{
871 elf_file_t ef = lf->priv;
872 unsigned long symnum;
873 const Elf_Sym* symp;
874 const char *strp;
875 unsigned long hash;
876 int i;
877
878 /* First, search hashed global symbols */
879 hash = elf_hash(name);
880 symnum = ef->buckets[hash % ef->nbuckets];
881
882 while (symnum != STN_UNDEF) {
883 if (symnum >= ef->nchains) {
884 printf("link_elf_lookup_symbol: corrupt symbol table\n");
885 return ENOENT;
886 }
887
888 symp = ef->symtab + symnum;
889 if (symp->st_name == 0) {
890 printf("link_elf_lookup_symbol: corrupt symbol table\n");
891 return ENOENT;
892 }
893
894 strp = ef->strtab + symp->st_name;
895
896 if (strcmp(name, strp) == 0) {
897 if (symp->st_shndx != SHN_UNDEF ||
898 (symp->st_value != 0 &&
d1786c1b
MD
899 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
900 ) {
984263bc
MD
901 *sym = (c_linker_sym_t) symp;
902 return 0;
d1786c1b 903 } else {
984263bc 904 return ENOENT;
d1786c1b 905 }
984263bc
MD
906 }
907
908 symnum = ef->chains[symnum];
909 }
910
911 /* If we have not found it, look at the full table (if loaded) */
912 if (ef->symtab == ef->ddbsymtab)
913 return ENOENT;
914
915 /* Exhaustive search */
916 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
917 strp = ef->ddbstrtab + symp->st_name;
918 if (strcmp(name, strp) == 0) {
919 if (symp->st_shndx != SHN_UNDEF ||
920 (symp->st_value != 0 &&
921 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
922 *sym = (c_linker_sym_t) symp;
923 return 0;
d1786c1b 924 } else {
984263bc 925 return ENOENT;
d1786c1b 926 }
984263bc
MD
927 }
928 }
984263bc
MD
929 return ENOENT;
930}
931
932static int
933link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
934{
935 elf_file_t ef = lf->priv;
936 const Elf_Sym* es = (const Elf_Sym*) sym;
937
938 if (es >= ef->symtab && ((es - ef->symtab) < ef->nchains)) {
939 symval->name = ef->strtab + es->st_name;
940 symval->value = (caddr_t) ef->address + es->st_value;
941 symval->size = es->st_size;
942 return 0;
943 }
944 if (ef->symtab == ef->ddbsymtab)
945 return ENOENT;
946 if (es >= ef->ddbsymtab && ((es - ef->ddbsymtab) < ef->ddbsymcnt)) {
947 symval->name = ef->ddbstrtab + es->st_name;
948 symval->value = (caddr_t) ef->address + es->st_value;
949 symval->size = es->st_size;
950 return 0;
951 }
952 return ENOENT;
953}
954
955static int
956link_elf_search_symbol(linker_file_t lf, caddr_t value,
957 c_linker_sym_t* sym, long* diffp)
958{
959 elf_file_t ef = lf->priv;
960 u_long off = (uintptr_t) (void *) value;
961 u_long diff = off;
962 u_long st_value;
963 const Elf_Sym* es;
964 const Elf_Sym* best = 0;
965 int i;
966
967 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
968 if (es->st_name == 0)
969 continue;
970 st_value = es->st_value + (uintptr_t) (void *) ef->address;
971 if (off >= st_value) {
972 if (off - st_value < diff) {
973 diff = off - st_value;
974 best = es;
975 if (diff == 0)
976 break;
977 } else if (off - st_value == diff) {
978 best = es;
979 }
980 }
981 }
982 if (best == 0)
983 *diffp = off;
984 else
985 *diffp = diff;
986 *sym = (c_linker_sym_t) best;
987
988 return 0;
989}