Make sure lo0 is brought up before any other interfaces to avoid problems
[dragonfly.git] / sys / kern / link_elf.c
blob3775b6aad37b11a37500254b5530ef5d63458a76
1 /*-
2 * Copyright (c) 1998 Doug Rabson
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/sys/kern/link_elf.c,v 1.24 1999/12/24 15:33:36 bde Exp $
27 * $DragonFly: src/sys/kern/link_elf.c,v 1.28 2008/02/06 22:37:46 nth Exp $
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/nlookup.h>
36 #include <sys/fcntl.h>
37 #include <sys/vnode.h>
38 #include <sys/linker.h>
39 #include <machine/elf.h>
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>
53 static int link_elf_load_module(const char*, linker_file_t*);
54 static int link_elf_load_file(const char*, linker_file_t*);
55 static int link_elf_lookup_symbol(linker_file_t, const char*,
56 c_linker_sym_t*);
57 static int link_elf_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t*);
58 static int link_elf_search_symbol(linker_file_t, caddr_t value,
59 c_linker_sym_t* sym, long* diffp);
61 static void link_elf_unload_file(linker_file_t);
62 static void link_elf_unload_module(linker_file_t);
63 static int link_elf_lookup_set(linker_file_t, const char *,
64 void ***, void ***, int *);
66 static struct linker_class_ops link_elf_class_ops = {
67 link_elf_load_module,
70 static struct linker_file_ops link_elf_file_ops = {
71 link_elf_lookup_symbol,
72 link_elf_symbol_values,
73 link_elf_search_symbol,
74 link_elf_unload_file,
75 link_elf_lookup_set
78 static struct linker_file_ops link_elf_module_ops = {
79 link_elf_lookup_symbol,
80 link_elf_symbol_values,
81 link_elf_search_symbol,
82 link_elf_unload_module,
83 link_elf_lookup_set
86 typedef 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;
118 static int parse_dynamic(linker_file_t lf);
119 static int load_dependancies(linker_file_t lf);
120 static int relocate_file(linker_file_t lf);
121 static int parse_module_symbols(linker_file_t lf);
124 * The kernel symbol table starts here.
126 extern struct _dynamic _DYNAMIC;
128 static void
129 link_elf_init(void* arg)
131 Elf_Dyn *dp;
132 caddr_t modptr, baseptr, sizeptr;
133 elf_file_t ef;
134 char *modname;
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
142 dp = (Elf_Dyn*) &_DYNAMIC;
143 if (dp) {
144 ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_INTWAIT | M_ZERO);
145 ef->address = 0;
146 #ifdef SPARSE_MAPPING
147 ef->object = 0;
148 #endif
149 ef->dynamic = dp;
150 modname = NULL;
151 modptr = preload_search_by_type("elf kernel");
152 if (modptr)
153 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
154 if (modname == NULL)
155 modname = "kernel";
156 linker_kernel_file = linker_make_file(modname, ef, &link_elf_file_ops);
157 if (linker_kernel_file == NULL)
158 panic("link_elf_init: Can't create linker structures for kernel");
159 parse_dynamic(linker_kernel_file);
160 linker_kernel_file->address = (caddr_t) KERNBASE;
161 linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
163 if (modptr) {
164 ef->modptr = modptr;
165 baseptr = preload_search_info(modptr, MODINFO_ADDR);
166 if (baseptr)
167 linker_kernel_file->address = *(caddr_t *)baseptr;
168 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
169 if (sizeptr)
170 linker_kernel_file->size = *(size_t *)sizeptr;
172 parse_module_symbols(linker_kernel_file);
173 linker_current_file = linker_kernel_file;
174 linker_kernel_file->flags |= LINKER_FILE_LINKED;
178 SYSINIT(link_elf, SI_BOOT2_KLD, SI_ORDER_SECOND, link_elf_init, 0);
180 static int
181 parse_module_symbols(linker_file_t lf)
183 elf_file_t ef = lf->priv;
184 caddr_t pointer;
185 caddr_t ssym, esym, base;
186 caddr_t strtab;
187 int strcnt;
188 Elf_Sym* symtab;
189 int symcnt;
191 if (ef->modptr == NULL)
192 return 0;
193 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM);
194 if (pointer == NULL)
195 return 0;
196 ssym = *(caddr_t *)pointer;
197 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM);
198 if (pointer == NULL)
199 return 0;
200 esym = *(caddr_t *)pointer;
202 base = ssym;
204 symcnt = *(long *)base;
205 base += sizeof(long);
206 symtab = (Elf_Sym *)base;
207 base += roundup(symcnt, sizeof(long));
209 if (base > esym || base < ssym) {
210 kprintf("Symbols are corrupt!\n");
211 return EINVAL;
214 strcnt = *(long *)base;
215 base += sizeof(long);
216 strtab = base;
217 base += roundup(strcnt, sizeof(long));
219 if (base > esym || base < ssym) {
220 kprintf("Symbols are corrupt!\n");
221 return EINVAL;
224 ef->ddbsymtab = symtab;
225 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
226 ef->ddbstrtab = strtab;
227 ef->ddbstrcnt = strcnt;
229 return 0;
232 static int
233 parse_dynamic(linker_file_t lf)
235 elf_file_t ef = lf->priv;
236 const Elf_Dyn *dp;
237 int plttype = DT_REL;
239 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
240 switch (dp->d_tag) {
241 case DT_HASH:
243 /* From src/libexec/rtld-elf/rtld.c */
244 const Elf_Off *hashtab = (const Elf_Off *)
245 (ef->address + dp->d_un.d_ptr);
246 ef->nbuckets = hashtab[0];
247 ef->nchains = hashtab[1];
248 ef->buckets = hashtab + 2;
249 ef->chains = ef->buckets + ef->nbuckets;
250 break;
252 case DT_STRTAB:
253 ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
254 break;
255 case DT_STRSZ:
256 ef->strsz = dp->d_un.d_val;
257 break;
258 case DT_SYMTAB:
259 ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
260 break;
261 case DT_SYMENT:
262 if (dp->d_un.d_val != sizeof(Elf_Sym))
263 return ENOEXEC;
264 break;
265 case DT_PLTGOT:
266 ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
267 break;
268 case DT_REL:
269 ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
270 break;
271 case DT_RELSZ:
272 ef->relsize = dp->d_un.d_val;
273 break;
274 case DT_RELENT:
275 if (dp->d_un.d_val != sizeof(Elf_Rel))
276 return ENOEXEC;
277 break;
278 case DT_JMPREL:
279 ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
280 break;
281 case DT_PLTRELSZ:
282 ef->pltrelsize = dp->d_un.d_val;
283 break;
284 case DT_RELA:
285 ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
286 break;
287 case DT_RELASZ:
288 ef->relasize = dp->d_un.d_val;
289 break;
290 case DT_RELAENT:
291 if (dp->d_un.d_val != sizeof(Elf_Rela))
292 return ENOEXEC;
293 break;
294 case DT_PLTREL:
295 plttype = dp->d_un.d_val;
296 if (plttype != DT_REL && plttype != DT_RELA)
297 return ENOEXEC;
298 break;
302 if (plttype == DT_RELA) {
303 ef->pltrela = (const Elf_Rela *) ef->pltrel;
304 ef->pltrel = NULL;
305 ef->pltrelasize = ef->pltrelsize;
306 ef->pltrelsize = 0;
309 ef->ddbsymtab = ef->symtab;
310 ef->ddbsymcnt = ef->nchains;
311 ef->ddbstrtab = ef->strtab;
312 ef->ddbstrcnt = ef->strsz;
314 return 0;
317 static void
318 link_elf_error(const char *s)
320 kprintf("kldload: %s\n", s);
323 static int
324 link_elf_load_module(const char *filename, linker_file_t *result)
326 caddr_t modptr, baseptr, sizeptr, dynptr;
327 char *type;
328 elf_file_t ef;
329 linker_file_t lf;
330 int error;
331 vm_offset_t dp;
334 * Look to see if we have the module preloaded.
336 modptr = preload_search_by_name(filename);
337 if (modptr == NULL)
338 return (link_elf_load_file(filename, result));
340 /* It's preloaded, check we can handle it and collect information */
341 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
342 baseptr = preload_search_info(modptr, MODINFO_ADDR);
343 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
344 dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC);
345 if (type == NULL || strcmp(type, "elf module") != 0)
346 return (EFTYPE);
347 if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
348 return (EINVAL);
350 ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
351 ef->modptr = modptr;
352 ef->address = *(caddr_t *)baseptr;
353 #ifdef SPARSE_MAPPING
354 ef->object = 0;
355 #endif
356 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
357 ef->dynamic = (Elf_Dyn *)dp;
358 lf = linker_make_file(filename, ef, &link_elf_module_ops);
359 if (lf == NULL) {
360 kfree(ef, M_LINKER);
361 return ENOMEM;
363 lf->address = ef->address;
364 lf->size = *(size_t *)sizeptr;
366 error = parse_dynamic(lf);
367 if (error) {
368 linker_file_unload(lf);
369 return error;
371 error = load_dependancies(lf);
372 if (error) {
373 linker_file_unload(lf);
374 return error;
376 error = relocate_file(lf);
377 if (error) {
378 linker_file_unload(lf);
379 return error;
381 parse_module_symbols(lf);
382 lf->flags |= LINKER_FILE_LINKED;
383 *result = lf;
384 return (0);
387 static int
388 link_elf_load_file(const char* filename, linker_file_t* result)
390 struct nlookupdata nd;
391 struct thread *td = curthread; /* XXX */
392 struct proc *p = td->td_proc;
393 struct vnode *vp;
394 Elf_Ehdr *hdr;
395 caddr_t firstpage;
396 int nbytes, i;
397 Elf_Phdr *phdr;
398 Elf_Phdr *phlimit;
399 Elf_Phdr *segs[2];
400 int nsegs;
401 Elf_Phdr *phdyn;
402 Elf_Phdr *phphdr;
403 caddr_t mapbase;
404 size_t mapsize;
405 Elf_Off base_offset;
406 Elf_Addr base_vaddr;
407 Elf_Addr base_vlimit;
408 int error = 0;
409 int resid;
410 elf_file_t ef;
411 linker_file_t lf;
412 char *pathname;
413 Elf_Shdr *shdr;
414 int symtabindex;
415 int symstrindex;
416 int symcnt;
417 int strcnt;
419 KKASSERT(p != NULL);
420 if (p->p_ucred == NULL) {
421 kprintf("link_elf_load_file: cannot load '%s' from filesystem"
422 " this early\n", filename);
423 return ENOENT;
425 shdr = NULL;
426 lf = NULL;
427 pathname = linker_search_path(filename);
428 if (pathname == NULL)
429 return ENOENT;
431 error = nlookup_init(&nd, pathname, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
432 if (error == 0)
433 error = vn_open(&nd, NULL, FREAD, 0);
434 kfree(pathname, M_LINKER);
435 if (error) {
436 nlookup_done(&nd);
437 return error;
439 vp = nd.nl_open_vp;
440 nd.nl_open_vp = NULL;
441 nlookup_done(&nd);
444 * Read the elf header from the file.
446 firstpage = kmalloc(PAGE_SIZE, M_LINKER, M_WAITOK);
447 hdr = (Elf_Ehdr *)firstpage;
448 error = vn_rdwr(UIO_READ, vp, firstpage, PAGE_SIZE, 0,
449 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
450 nbytes = PAGE_SIZE - resid;
451 if (error)
452 goto out;
454 if (!IS_ELF(*hdr)) {
455 error = ENOEXEC;
456 goto out;
459 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
460 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
461 link_elf_error("Unsupported file layout");
462 error = ENOEXEC;
463 goto out;
465 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
466 || hdr->e_version != EV_CURRENT) {
467 link_elf_error("Unsupported file version");
468 error = ENOEXEC;
469 goto out;
471 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
472 link_elf_error("Unsupported file type");
473 error = ENOEXEC;
474 goto out;
476 if (hdr->e_machine != ELF_TARG_MACH) {
477 link_elf_error("Unsupported machine");
478 error = ENOEXEC;
479 goto out;
483 * We rely on the program header being in the first page. This is
484 * not strictly required by the ABI specification, but it seems to
485 * always true in practice. And, it simplifies things considerably.
487 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
488 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
489 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
490 link_elf_error("Unreadable program headers");
493 * Scan the program header entries, and save key information.
495 * We rely on there being exactly two load segments, text and data,
496 * in that order.
498 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
499 phlimit = phdr + hdr->e_phnum;
500 nsegs = 0;
501 phdyn = NULL;
502 phphdr = NULL;
503 while (phdr < phlimit) {
504 switch (phdr->p_type) {
506 case PT_LOAD:
507 if (nsegs == 2) {
508 link_elf_error("Too many sections");
509 error = ENOEXEC;
510 goto out;
512 segs[nsegs] = phdr;
513 ++nsegs;
514 break;
516 case PT_PHDR:
517 phphdr = phdr;
518 break;
520 case PT_DYNAMIC:
521 phdyn = phdr;
522 break;
525 ++phdr;
527 if (phdyn == NULL) {
528 link_elf_error("Object is not dynamically-linked");
529 error = ENOEXEC;
530 goto out;
534 * Allocate the entire address space of the object, to stake out our
535 * contiguous region, and to establish the base address for relocation.
537 base_offset = trunc_page(segs[0]->p_offset);
538 base_vaddr = trunc_page(segs[0]->p_vaddr);
539 base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
540 mapsize = base_vlimit - base_vaddr;
542 ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
543 #ifdef SPARSE_MAPPING
544 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
545 if (ef->object == NULL) {
546 kfree(ef, M_LINKER);
547 error = ENOMEM;
548 goto out;
550 vm_object_reference(ef->object);
551 ef->address = (caddr_t)vm_map_min(&kernel_map);
552 error = vm_map_find(&kernel_map, ef->object, 0,
553 (vm_offset_t *)&ef->address, mapsize,
555 VM_MAPTYPE_NORMAL,
556 VM_PROT_ALL, VM_PROT_ALL,
558 if (error) {
559 vm_object_deallocate(ef->object);
560 kfree(ef, M_LINKER);
561 goto out;
563 #else
564 ef->address = kmalloc(mapsize, M_LINKER, M_WAITOK);
565 #endif
566 mapbase = ef->address;
569 * Read the text and data sections and zero the bss.
571 for (i = 0; i < 2; i++) {
572 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
573 error = vn_rdwr(UIO_READ, vp,
574 segbase, segs[i]->p_filesz, segs[i]->p_offset,
575 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
576 if (error) {
577 #ifdef SPARSE_MAPPING
578 vm_map_remove(&kernel_map, (vm_offset_t) ef->address,
579 (vm_offset_t) ef->address
580 + (ef->object->size << PAGE_SHIFT));
581 vm_object_deallocate(ef->object);
582 #else
583 kfree(ef->address, M_LINKER);
584 #endif
585 kfree(ef, M_LINKER);
586 goto out;
588 bzero(segbase + segs[i]->p_filesz,
589 segs[i]->p_memsz - segs[i]->p_filesz);
591 #ifdef SPARSE_MAPPING
593 * Wire down the pages
595 vm_map_wire(&kernel_map,
596 (vm_offset_t) segbase,
597 (vm_offset_t) segbase + segs[i]->p_memsz,
599 #endif
602 ef->dynamic = (const Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
604 lf = linker_make_file(filename, ef, &link_elf_file_ops);
605 if (lf == NULL) {
606 #ifdef SPARSE_MAPPING
607 vm_map_remove(&kernel_map, (vm_offset_t) ef->address,
608 (vm_offset_t) ef->address
609 + (ef->object->size << PAGE_SHIFT));
610 vm_object_deallocate(ef->object);
611 #else
612 kfree(ef->address, M_LINKER);
613 #endif
614 kfree(ef, M_LINKER);
615 error = ENOMEM;
616 goto out;
618 lf->address = ef->address;
619 lf->size = mapsize;
621 error = parse_dynamic(lf);
622 if (error)
623 goto out;
624 error = load_dependancies(lf);
625 if (error)
626 goto out;
627 error = relocate_file(lf);
628 if (error)
629 goto out;
631 /* Try and load the symbol table if it's present. (you can strip it!) */
632 nbytes = hdr->e_shnum * hdr->e_shentsize;
633 if (nbytes == 0 || hdr->e_shoff == 0)
634 goto nosyms;
635 shdr = kmalloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
636 error = vn_rdwr(UIO_READ, vp,
637 (caddr_t)shdr, nbytes, hdr->e_shoff,
638 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
639 if (error)
640 goto out;
641 symtabindex = -1;
642 symstrindex = -1;
643 for (i = 0; i < hdr->e_shnum; i++) {
644 if (shdr[i].sh_type == SHT_SYMTAB) {
645 symtabindex = i;
646 symstrindex = shdr[i].sh_link;
649 if (symtabindex < 0 || symstrindex < 0)
650 goto nosyms;
652 symcnt = shdr[symtabindex].sh_size;
653 ef->symbase = kmalloc(symcnt, M_LINKER, M_WAITOK);
654 strcnt = shdr[symstrindex].sh_size;
655 ef->strbase = kmalloc(strcnt, M_LINKER, M_WAITOK);
656 error = vn_rdwr(UIO_READ, vp,
657 ef->symbase, symcnt, shdr[symtabindex].sh_offset,
658 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
659 if (error)
660 goto out;
661 error = vn_rdwr(UIO_READ, vp,
662 ef->strbase, strcnt, shdr[symstrindex].sh_offset,
663 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
664 if (error)
665 goto out;
667 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
668 ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
669 ef->ddbstrcnt = strcnt;
670 ef->ddbstrtab = ef->strbase;
672 lf->flags |= LINKER_FILE_LINKED;
674 nosyms:
676 *result = lf;
678 out:
679 if (error && lf)
680 linker_file_unload(lf);
681 if (shdr)
682 kfree(shdr, M_LINKER);
683 if (firstpage)
684 kfree(firstpage, M_LINKER);
685 vn_unlock(vp);
686 vn_close(vp, FREAD);
688 return error;
691 static void
692 link_elf_unload_file(linker_file_t file)
694 elf_file_t ef = file->priv;
696 if (ef) {
697 #ifdef SPARSE_MAPPING
698 if (ef->object) {
699 vm_map_remove(&kernel_map, (vm_offset_t) ef->address,
700 (vm_offset_t) ef->address
701 + (ef->object->size << PAGE_SHIFT));
702 vm_object_deallocate(ef->object);
704 #else
705 if (ef->address)
706 kfree(ef->address, M_LINKER);
707 #endif
708 if (ef->symbase)
709 kfree(ef->symbase, M_LINKER);
710 if (ef->strbase)
711 kfree(ef->strbase, M_LINKER);
712 kfree(ef, M_LINKER);
716 static void
717 link_elf_unload_module(linker_file_t file)
719 elf_file_t ef = file->priv;
721 if (ef)
722 kfree(ef, M_LINKER);
723 if (file->filename)
724 preload_delete_name(file->filename);
727 static int
728 load_dependancies(linker_file_t lf)
730 elf_file_t ef = lf->priv;
731 linker_file_t lfdep;
732 char* name;
733 const Elf_Dyn *dp;
734 int error = 0;
737 * All files are dependant on /kernel.
739 if (linker_kernel_file) {
740 linker_kernel_file->refs++;
741 linker_file_add_dependancy(lf, linker_kernel_file);
744 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
745 if (dp->d_tag == DT_NEEDED) {
746 name = ef->strtab + dp->d_un.d_val;
748 error = linker_load_file(name, &lfdep);
749 if (error)
750 goto out;
751 linker_file_add_dependancy(lf, lfdep);
755 out:
756 return error;
759 static const char *
760 symbol_name(elf_file_t ef, Elf_Word r_info)
762 const Elf_Sym *ref;
764 if (ELF_R_SYM(r_info)) {
765 ref = ef->symtab + ELF_R_SYM(r_info);
766 return ef->strtab + ref->st_name;
767 } else
768 return NULL;
771 static int
772 relocate_file(linker_file_t lf)
774 elf_file_t ef = lf->priv;
775 const Elf_Rel *rellim;
776 const Elf_Rel *rel;
777 const Elf_Rela *relalim;
778 const Elf_Rela *rela;
779 const char *symname;
781 /* Perform relocations without addend if there are any: */
782 rel = ef->rel;
783 if (rel) {
784 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
785 while (rel < rellim) {
786 symname = symbol_name(ef, rel->r_info);
787 if (elf_reloc(lf, rel, ELF_RELOC_REL, symname)) {
788 kprintf("link_elf: symbol %s undefined\n", symname);
789 return ENOENT;
791 rel++;
795 /* Perform relocations with addend if there are any: */
796 rela = ef->rela;
797 if (rela) {
798 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
799 while (rela < relalim) {
800 symname = symbol_name(ef, rela->r_info);
801 if (elf_reloc(lf, rela, ELF_RELOC_RELA, symname)) {
802 kprintf("link_elf: symbol %s undefined\n", symname);
803 return ENOENT;
805 rela++;
809 /* Perform PLT relocations without addend if there are any: */
810 rel = ef->pltrel;
811 if (rel) {
812 rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
813 while (rel < rellim) {
814 symname = symbol_name(ef, rel->r_info);
815 if (elf_reloc(lf, rel, ELF_RELOC_REL, symname)) {
816 kprintf("link_elf: symbol %s undefined\n", symname);
817 return ENOENT;
819 rel++;
823 /* Perform relocations with addend if there are any: */
824 rela = ef->pltrela;
825 if (rela) {
826 relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
827 while (rela < relalim) {
828 symname = symbol_name(ef, rela->r_info);
829 if (elf_reloc(lf, rela, ELF_RELOC_RELA, symname)) {
830 kprintf("link_elf: symbol %s undefined\n", symname);
831 return ENOENT;
833 rela++;
837 return 0;
841 * Hash function for symbol table lookup. Don't even think about changing
842 * this. It is specified by the System V ABI.
844 static unsigned long
845 elf_hash(const char *name)
847 const unsigned char *p = (const unsigned char *) name;
848 unsigned long h = 0;
849 unsigned long g;
851 while (*p != '\0') {
852 h = (h << 4) + *p++;
853 if ((g = h & 0xf0000000) != 0)
854 h ^= g >> 24;
855 h &= ~g;
857 return h;
861 link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
863 elf_file_t ef = lf->priv;
864 unsigned long symnum;
865 const Elf_Sym* symp;
866 const char *strp;
867 unsigned long hash;
868 int i;
870 /* First, search hashed global symbols */
871 hash = elf_hash(name);
872 symnum = ef->buckets[hash % ef->nbuckets];
874 while (symnum != STN_UNDEF) {
875 if (symnum >= ef->nchains) {
876 kprintf("link_elf_lookup_symbol: corrupt symbol table\n");
877 return ENOENT;
880 symp = ef->symtab + symnum;
881 if (symp->st_name == 0) {
882 kprintf("link_elf_lookup_symbol: corrupt symbol table\n");
883 return ENOENT;
886 strp = ef->strtab + symp->st_name;
888 if (strcmp(name, strp) == 0) {
889 if (symp->st_shndx != SHN_UNDEF ||
890 (symp->st_value != 0 &&
891 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
893 *sym = (c_linker_sym_t) symp;
894 return 0;
895 } else {
896 return ENOENT;
900 symnum = ef->chains[symnum];
903 /* If we have not found it, look at the full table (if loaded) */
904 if (ef->symtab == ef->ddbsymtab)
905 return ENOENT;
907 /* Exhaustive search */
908 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
909 strp = ef->ddbstrtab + symp->st_name;
910 if (strcmp(name, strp) == 0) {
911 if (symp->st_shndx != SHN_UNDEF ||
912 (symp->st_value != 0 &&
913 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
914 *sym = (c_linker_sym_t) symp;
915 return 0;
916 } else {
917 return ENOENT;
921 return ENOENT;
924 static int
925 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
927 elf_file_t ef = lf->priv;
928 const Elf_Sym* es = (const Elf_Sym*) sym;
930 if (es >= ef->symtab && ((es - ef->symtab) < ef->nchains)) {
931 symval->name = ef->strtab + es->st_name;
932 symval->value = (caddr_t) ef->address + es->st_value;
933 symval->size = es->st_size;
934 return 0;
936 if (ef->symtab == ef->ddbsymtab)
937 return ENOENT;
938 if (es >= ef->ddbsymtab && ((es - ef->ddbsymtab) < ef->ddbsymcnt)) {
939 symval->name = ef->ddbstrtab + es->st_name;
940 symval->value = (caddr_t) ef->address + es->st_value;
941 symval->size = es->st_size;
942 return 0;
944 return ENOENT;
947 static int
948 link_elf_search_symbol(linker_file_t lf, caddr_t value,
949 c_linker_sym_t* sym, long* diffp)
951 elf_file_t ef = lf->priv;
952 u_long off = (uintptr_t) (void *) value;
953 u_long diff = off;
954 u_long st_value;
955 const Elf_Sym* es;
956 const Elf_Sym* best = 0;
957 int i;
959 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
960 if (es->st_name == 0)
961 continue;
962 st_value = es->st_value + (uintptr_t) (void *) ef->address;
963 if (off >= st_value) {
964 if (off - st_value < diff) {
965 diff = off - st_value;
966 best = es;
967 if (diff == 0)
968 break;
969 } else if (off - st_value == diff) {
970 best = es;
974 if (best == 0)
975 *diffp = off;
976 else
977 *diffp = diff;
978 *sym = (c_linker_sym_t) best;
980 return 0;
984 * Look up a linker set on an ELF system.
986 static int
987 link_elf_lookup_set(linker_file_t lf, const char *name,
988 void ***startp, void ***stopp, int *countp)
990 c_linker_sym_t sym;
991 linker_symval_t symval;
992 char *setsym;
993 void **start, **stop;
994 int len, error = 0, count;
996 len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
997 setsym = kmalloc(len, M_LINKER, M_WAITOK);
999 /* get address of first entry */
1000 ksnprintf(setsym, len, "%s%s", "__start_set_", name);
1001 error = link_elf_lookup_symbol(lf, setsym, &sym);
1002 if (error)
1003 goto out;
1004 link_elf_symbol_values(lf, sym, &symval);
1005 if (symval.value == 0) {
1006 error = ESRCH;
1007 goto out;
1009 start = (void **)symval.value;
1011 /* get address of last entry */
1012 ksnprintf(setsym, len, "%s%s", "__stop_set_", name);
1013 error = link_elf_lookup_symbol(lf, setsym, &sym);
1014 if (error)
1015 goto out;
1016 link_elf_symbol_values(lf, sym, &symval);
1017 if (symval.value == 0) {
1018 error = ESRCH;
1019 goto out;
1021 stop = (void **)symval.value;
1023 /* and the number of entries */
1024 count = stop - start;
1026 /* and copy out */
1027 if (startp)
1028 *startp = start;
1029 if (stopp)
1030 *stopp = stop;
1031 if (countp)
1032 *countp = count;
1034 out:
1035 kfree(setsym, M_LINKER);
1036 return error;