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