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