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