VNode sequencing and locking - part 2/4.
[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 $
87de5057 27 * $DragonFly: src/sys/kern/link_elf.c,v 1.19 2006/05/06 02:43:12 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>
fad57d0e 35#include <sys/nlookup.h>
984263bc
MD
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
66static struct linker_class_ops link_elf_class_ops = {
67 link_elf_load_module,
68};
69
70static 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,
dc62b251 75 link_elf_lookup_set
984263bc
MD
76};
77
78static 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,
dc62b251 83 link_elf_lookup_set
984263bc 84};
761c7789 85
984263bc
MD
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) {
311e51b4 144 ef = malloc(sizeof(struct elf_file), M_LINKER, M_INTWAIT | M_ZERO);
984263bc
MD
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;
162
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;
171 }
761c7789 172 parse_module_symbols(linker_kernel_file);
984263bc
MD
173 linker_current_file = linker_kernel_file;
174 linker_kernel_file->flags |= LINKER_FILE_LINKED;
175 }
984263bc
MD
176}
177
178SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
179
180static int
181parse_module_symbols(linker_file_t lf)
182{
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;
190
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;
201
202 base = ssym;
203
204 symcnt = *(long *)base;
205 base += sizeof(long);
206 symtab = (Elf_Sym *)base;
207 base += roundup(symcnt, sizeof(long));
208
209 if (base > esym || base < ssym) {
210 printf("Symbols are corrupt!\n");
211 return EINVAL;
212 }
213
214 strcnt = *(long *)base;
215 base += sizeof(long);
216 strtab = base;
217 base += roundup(strcnt, sizeof(long));
218
219 if (base > esym || base < ssym) {
220 printf("Symbols are corrupt!\n");
221 return EINVAL;
222 }
223
224 ef->ddbsymtab = symtab;
225 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
226 ef->ddbstrtab = strtab;
227 ef->ddbstrcnt = strcnt;
228
229 return 0;
230}
231
232static int
233parse_dynamic(linker_file_t lf)
234{
235 elf_file_t ef = lf->priv;
236 const Elf_Dyn *dp;
237 int plttype = DT_REL;
238
239 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
240 switch (dp->d_tag) {
241 case DT_HASH:
242 {
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;
251 }
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;
299 }
300 }
301
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;
307 }
308
309 ef->ddbsymtab = ef->symtab;
310 ef->ddbsymcnt = ef->nchains;
311 ef->ddbstrtab = ef->strtab;
312 ef->ddbstrcnt = ef->strsz;
313
314 return 0;
315}
316
317static void
318link_elf_error(const char *s)
319{
320 printf("kldload: %s\n", s);
321}
322
323static int
324link_elf_load_module(const char *filename, linker_file_t *result)
325{
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;
332
32832096
MD
333 /*
334 * Look to see if we have the module preloaded.
335 */
984263bc
MD
336 modptr = preload_search_by_name(filename);
337 if (modptr == NULL)
338 return (link_elf_load_file(filename, result));
339
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);
349
761c7789 350 ef = malloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
351 if (ef == NULL)
352 return (ENOMEM);
984263bc
MD
353 ef->modptr = modptr;
354 ef->address = *(caddr_t *)baseptr;
355#ifdef SPARSE_MAPPING
356 ef->object = 0;
357#endif
358 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
359 ef->dynamic = (Elf_Dyn *)dp;
360 lf = linker_make_file(filename, ef, &link_elf_module_ops);
361 if (lf == NULL) {
362 free(ef, M_LINKER);
363 return ENOMEM;
364 }
365 lf->address = ef->address;
366 lf->size = *(size_t *)sizeptr;
367
368 error = parse_dynamic(lf);
369 if (error) {
370 linker_file_unload(lf);
371 return error;
372 }
373 error = load_dependancies(lf);
374 if (error) {
375 linker_file_unload(lf);
376 return error;
377 }
378 error = relocate_file(lf);
379 if (error) {
380 linker_file_unload(lf);
381 return error;
382 }
761c7789 383 parse_module_symbols(lf);
984263bc
MD
384 lf->flags |= LINKER_FILE_LINKED;
385 *result = lf;
386 return (0);
387}
388
389static int
390link_elf_load_file(const char* filename, linker_file_t* result)
391{
fad57d0e 392 struct nlookupdata nd;
dadab5e9
MD
393 struct thread *td = curthread; /* XXX */
394 struct proc *p = td->td_proc;
fad57d0e 395 struct vnode *vp;
984263bc
MD
396 Elf_Ehdr *hdr;
397 caddr_t firstpage;
398 int nbytes, i;
399 Elf_Phdr *phdr;
400 Elf_Phdr *phlimit;
401 Elf_Phdr *segs[2];
402 int nsegs;
403 Elf_Phdr *phdyn;
404 Elf_Phdr *phphdr;
405 caddr_t mapbase;
406 size_t mapsize;
407 Elf_Off base_offset;
408 Elf_Addr base_vaddr;
409 Elf_Addr base_vlimit;
410 int error = 0;
411 int resid;
412 elf_file_t ef;
413 linker_file_t lf;
414 char *pathname;
415 Elf_Shdr *shdr;
416 int symtabindex;
417 int symstrindex;
418 int symcnt;
419 int strcnt;
420
dadab5e9 421 KKASSERT(p != NULL);
a3d82f10
MD
422 if (p->p_ucred == NULL) {
423 printf("link_elf_load_file: cannot load '%s' from filesystem"
424 " this early\n", filename);
425 return ENOENT;
426 }
984263bc
MD
427 shdr = NULL;
428 lf = NULL;
984263bc
MD
429 pathname = linker_search_path(filename);
430 if (pathname == NULL)
431 return ENOENT;
432
fad57d0e
MD
433 error = nlookup_init(&nd, pathname, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
434 if (error == 0)
435 error = vn_open(&nd, NULL, FREAD, 0);
984263bc 436 free(pathname, M_LINKER);
fad57d0e
MD
437 if (error) {
438 nlookup_done(&nd);
984263bc 439 return error;
fad57d0e
MD
440 }
441 vp = nd.nl_open_vp;
442 nd.nl_open_vp = NULL;
443 nlookup_done(&nd);
984263bc
MD
444
445 /*
446 * Read the elf header from the file.
447 */
448 firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
449 if (firstpage == NULL) {
450 error = ENOMEM;
451 goto out;
452 }
453 hdr = (Elf_Ehdr *)firstpage;
fad57d0e 454 error = vn_rdwr(UIO_READ, vp, firstpage, PAGE_SIZE, 0,
87de5057 455 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
456 nbytes = PAGE_SIZE - resid;
457 if (error)
458 goto out;
459
460 if (!IS_ELF(*hdr)) {
461 error = ENOEXEC;
462 goto out;
463 }
464
465 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
466 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
467 link_elf_error("Unsupported file layout");
468 error = ENOEXEC;
469 goto out;
470 }
471 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
472 || hdr->e_version != EV_CURRENT) {
473 link_elf_error("Unsupported file version");
474 error = ENOEXEC;
475 goto out;
476 }
477 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
478 link_elf_error("Unsupported file type");
479 error = ENOEXEC;
480 goto out;
481 }
482 if (hdr->e_machine != ELF_TARG_MACH) {
483 link_elf_error("Unsupported machine");
484 error = ENOEXEC;
485 goto out;
486 }
487
488 /*
489 * We rely on the program header being in the first page. This is
490 * not strictly required by the ABI specification, but it seems to
491 * always true in practice. And, it simplifies things considerably.
492 */
493 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
494 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
495 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
496 link_elf_error("Unreadable program headers");
497
498 /*
499 * Scan the program header entries, and save key information.
500 *
501 * We rely on there being exactly two load segments, text and data,
502 * in that order.
503 */
504 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
505 phlimit = phdr + hdr->e_phnum;
506 nsegs = 0;
507 phdyn = NULL;
508 phphdr = NULL;
509 while (phdr < phlimit) {
510 switch (phdr->p_type) {
511
512 case PT_LOAD:
513 if (nsegs == 2) {
514 link_elf_error("Too many sections");
515 error = ENOEXEC;
516 goto out;
517 }
518 segs[nsegs] = phdr;
519 ++nsegs;
520 break;
521
522 case PT_PHDR:
523 phphdr = phdr;
524 break;
525
526 case PT_DYNAMIC:
527 phdyn = phdr;
528 break;
529 }
530
531 ++phdr;
532 }
533 if (phdyn == NULL) {
534 link_elf_error("Object is not dynamically-linked");
535 error = ENOEXEC;
536 goto out;
537 }
538
539 /*
540 * Allocate the entire address space of the object, to stake out our
541 * contiguous region, and to establish the base address for relocation.
542 */
543 base_offset = trunc_page(segs[0]->p_offset);
544 base_vaddr = trunc_page(segs[0]->p_vaddr);
545 base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
546 mapsize = base_vlimit - base_vaddr;
547
761c7789 548 ef = malloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
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;
fad57d0e 577 error = vn_rdwr(UIO_READ, vp,
984263bc 578 segbase, segs[i]->p_filesz, segs[i]->p_offset,
87de5057 579 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
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;
761c7789 639 shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
640 if (shdr == NULL) {
641 error = ENOMEM;
642 goto out;
643 }
fad57d0e 644 error = vn_rdwr(UIO_READ, vp,
984263bc 645 (caddr_t)shdr, nbytes, hdr->e_shoff,
87de5057 646 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
647 if (error)
648 goto out;
649 symtabindex = -1;
650 symstrindex = -1;
651 for (i = 0; i < hdr->e_shnum; i++) {
652 if (shdr[i].sh_type == SHT_SYMTAB) {
653 symtabindex = i;
654 symstrindex = shdr[i].sh_link;
655 }
656 }
657 if (symtabindex < 0 || symstrindex < 0)
658 goto nosyms;
659
660 symcnt = shdr[symtabindex].sh_size;
661 ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
662 strcnt = shdr[symstrindex].sh_size;
663 ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
664
665 if (ef->symbase == NULL || ef->strbase == NULL) {
666 error = ENOMEM;
667 goto out;
668 }
fad57d0e 669 error = vn_rdwr(UIO_READ, vp,
984263bc 670 ef->symbase, symcnt, shdr[symtabindex].sh_offset,
87de5057 671 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
672 if (error)
673 goto out;
fad57d0e 674 error = vn_rdwr(UIO_READ, vp,
984263bc 675 ef->strbase, strcnt, shdr[symstrindex].sh_offset,
87de5057 676 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
677 if (error)
678 goto out;
679
680 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
681 ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
682 ef->ddbstrcnt = strcnt;
683 ef->ddbstrtab = ef->strbase;
684
685 lf->flags |= LINKER_FILE_LINKED;
686
687nosyms:
688
689 *result = lf;
690
691out:
692 if (error && lf)
693 linker_file_unload(lf);
694 if (shdr)
695 free(shdr, M_LINKER);
696 if (firstpage)
697 free(firstpage, M_LINKER);
ca466bae 698 VOP_UNLOCK(vp, 0);
87de5057 699 vn_close(vp, FREAD);
984263bc
MD
700
701 return error;
702}
703
704static void
705link_elf_unload_file(linker_file_t file)
706{
707 elf_file_t ef = file->priv;
708
709 if (ef) {
710#ifdef SPARSE_MAPPING
711 if (ef->object) {
712 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
713 (vm_offset_t) ef->address
714 + (ef->object->size << PAGE_SHIFT));
715 vm_object_deallocate(ef->object);
716 }
717#else
718 if (ef->address)
719 free(ef->address, M_LINKER);
720#endif
721 if (ef->symbase)
722 free(ef->symbase, M_LINKER);
723 if (ef->strbase)
724 free(ef->strbase, M_LINKER);
725 free(ef, M_LINKER);
726 }
727}
728
729static void
730link_elf_unload_module(linker_file_t file)
731{
732 elf_file_t ef = file->priv;
733
734 if (ef)
735 free(ef, M_LINKER);
736 if (file->filename)
737 preload_delete_name(file->filename);
738}
739
740static int
741load_dependancies(linker_file_t lf)
742{
743 elf_file_t ef = lf->priv;
744 linker_file_t lfdep;
745 char* name;
746 const Elf_Dyn *dp;
747 int error = 0;
748
749 /*
750 * All files are dependant on /kernel.
751 */
752 if (linker_kernel_file) {
753 linker_kernel_file->refs++;
754 linker_file_add_dependancy(lf, linker_kernel_file);
755 }
756
757 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
758 if (dp->d_tag == DT_NEEDED) {
759 name = ef->strtab + dp->d_un.d_val;
760
761 error = linker_load_file(name, &lfdep);
762 if (error)
763 goto out;
764 error = linker_file_add_dependancy(lf, lfdep);
765 if (error)
766 goto out;
767 }
768 }
769
770out:
771 return error;
772}
773
774static const char *
775symbol_name(elf_file_t ef, Elf_Word r_info)
776{
777 const Elf_Sym *ref;
778
779 if (ELF_R_SYM(r_info)) {
780 ref = ef->symtab + ELF_R_SYM(r_info);
781 return ef->strtab + ref->st_name;
782 } else
783 return NULL;
784}
785
786static int
787relocate_file(linker_file_t lf)
788{
789 elf_file_t ef = lf->priv;
790 const Elf_Rel *rellim;
791 const Elf_Rel *rel;
792 const Elf_Rela *relalim;
793 const Elf_Rela *rela;
794 const char *symname;
795
796 /* Perform relocations without addend if there are any: */
797 rel = ef->rel;
798 if (rel) {
799 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
800 while (rel < rellim) {
801 symname = symbol_name(ef, rel->r_info);
802 if (elf_reloc(lf, rel, ELF_RELOC_REL, symname)) {
803 printf("link_elf: symbol %s undefined\n", symname);
804 return ENOENT;
805 }
806 rel++;
807 }
808 }
809
810 /* Perform relocations with addend if there are any: */
811 rela = ef->rela;
812 if (rela) {
813 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
814 while (rela < relalim) {
815 symname = symbol_name(ef, rela->r_info);
816 if (elf_reloc(lf, rela, ELF_RELOC_RELA, symname)) {
817 printf("link_elf: symbol %s undefined\n", symname);
818 return ENOENT;
819 }
820 rela++;
821 }
822 }
823
824 /* Perform PLT relocations without addend if there are any: */
825 rel = ef->pltrel;
826 if (rel) {
827 rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
828 while (rel < rellim) {
829 symname = symbol_name(ef, rel->r_info);
830 if (elf_reloc(lf, rel, ELF_RELOC_REL, symname)) {
831 printf("link_elf: symbol %s undefined\n", symname);
832 return ENOENT;
833 }
834 rel++;
835 }
836 }
837
838 /* Perform relocations with addend if there are any: */
839 rela = ef->pltrela;
840 if (rela) {
841 relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
842 while (rela < relalim) {
843 symname = symbol_name(ef, rela->r_info);
844 if (elf_reloc(lf, rela, ELF_RELOC_RELA, symname)) {
845 printf("link_elf: symbol %s undefined\n", symname);
846 return ENOENT;
847 }
848 rela++;
849 }
850 }
851
852 return 0;
853}
854
855/*
856 * Hash function for symbol table lookup. Don't even think about changing
857 * this. It is specified by the System V ABI.
858 */
859static unsigned long
860elf_hash(const char *name)
861{
862 const unsigned char *p = (const unsigned char *) name;
863 unsigned long h = 0;
864 unsigned long g;
865
866 while (*p != '\0') {
867 h = (h << 4) + *p++;
868 if ((g = h & 0xf0000000) != 0)
869 h ^= g >> 24;
870 h &= ~g;
871 }
872 return h;
873}
874
875int
876link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
877{
878 elf_file_t ef = lf->priv;
879 unsigned long symnum;
880 const Elf_Sym* symp;
881 const char *strp;
882 unsigned long hash;
883 int i;
884
885 /* First, search hashed global symbols */
886 hash = elf_hash(name);
887 symnum = ef->buckets[hash % ef->nbuckets];
888
889 while (symnum != STN_UNDEF) {
890 if (symnum >= ef->nchains) {
891 printf("link_elf_lookup_symbol: corrupt symbol table\n");
892 return ENOENT;
893 }
894
895 symp = ef->symtab + symnum;
896 if (symp->st_name == 0) {
897 printf("link_elf_lookup_symbol: corrupt symbol table\n");
898 return ENOENT;
899 }
900
901 strp = ef->strtab + symp->st_name;
902
903 if (strcmp(name, strp) == 0) {
904 if (symp->st_shndx != SHN_UNDEF ||
905 (symp->st_value != 0 &&
d1786c1b
MD
906 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
907 ) {
984263bc
MD
908 *sym = (c_linker_sym_t) symp;
909 return 0;
d1786c1b 910 } else {
984263bc 911 return ENOENT;
d1786c1b 912 }
984263bc
MD
913 }
914
915 symnum = ef->chains[symnum];
916 }
917
918 /* If we have not found it, look at the full table (if loaded) */
919 if (ef->symtab == ef->ddbsymtab)
920 return ENOENT;
921
922 /* Exhaustive search */
923 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
924 strp = ef->ddbstrtab + symp->st_name;
925 if (strcmp(name, strp) == 0) {
926 if (symp->st_shndx != SHN_UNDEF ||
927 (symp->st_value != 0 &&
928 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
929 *sym = (c_linker_sym_t) symp;
930 return 0;
d1786c1b 931 } else {
984263bc 932 return ENOENT;
d1786c1b 933 }
984263bc
MD
934 }
935 }
984263bc
MD
936 return ENOENT;
937}
938
939static int
940link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
941{
942 elf_file_t ef = lf->priv;
943 const Elf_Sym* es = (const Elf_Sym*) sym;
944
945 if (es >= ef->symtab && ((es - ef->symtab) < ef->nchains)) {
946 symval->name = ef->strtab + es->st_name;
947 symval->value = (caddr_t) ef->address + es->st_value;
948 symval->size = es->st_size;
949 return 0;
950 }
951 if (ef->symtab == ef->ddbsymtab)
952 return ENOENT;
953 if (es >= ef->ddbsymtab && ((es - ef->ddbsymtab) < ef->ddbsymcnt)) {
954 symval->name = ef->ddbstrtab + es->st_name;
955 symval->value = (caddr_t) ef->address + es->st_value;
956 symval->size = es->st_size;
957 return 0;
958 }
959 return ENOENT;
960}
961
962static int
963link_elf_search_symbol(linker_file_t lf, caddr_t value,
964 c_linker_sym_t* sym, long* diffp)
965{
966 elf_file_t ef = lf->priv;
967 u_long off = (uintptr_t) (void *) value;
968 u_long diff = off;
969 u_long st_value;
970 const Elf_Sym* es;
971 const Elf_Sym* best = 0;
972 int i;
973
974 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
975 if (es->st_name == 0)
976 continue;
977 st_value = es->st_value + (uintptr_t) (void *) ef->address;
978 if (off >= st_value) {
979 if (off - st_value < diff) {
980 diff = off - st_value;
981 best = es;
982 if (diff == 0)
983 break;
984 } else if (off - st_value == diff) {
985 best = es;
986 }
987 }
988 }
989 if (best == 0)
990 *diffp = off;
991 else
992 *diffp = diff;
993 *sym = (c_linker_sym_t) best;
994
995 return 0;
996}
dc62b251
MD
997
998/*
999 * Look up a linker set on an ELF system.
1000 */
1001static int
1002link_elf_lookup_set(linker_file_t lf, const char *name,
1003 void ***startp, void ***stopp, int *countp)
1004{
1005 c_linker_sym_t sym;
1006 linker_symval_t symval;
1007 char *setsym;
1008 void **start, **stop;
1009 int len, error = 0, count;
1010
1011 len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1012 setsym = malloc(len, M_LINKER, M_WAITOK);
1013 if (setsym == NULL)
1014 return ENOMEM;
1015
1016 /* get address of first entry */
1017 snprintf(setsym, len, "%s%s", "__start_set_", name);
1018 error = link_elf_lookup_symbol(lf, setsym, &sym);
1019 if (error)
1020 goto out;
1021 link_elf_symbol_values(lf, sym, &symval);
1022 if (symval.value == 0) {
1023 error = ESRCH;
1024 goto out;
1025 }
1026 start = (void **)symval.value;
1027
1028 /* get address of last entry */
1029 snprintf(setsym, len, "%s%s", "__stop_set_", name);
1030 error = link_elf_lookup_symbol(lf, setsym, &sym);
1031 if (error)
1032 goto out;
1033 link_elf_symbol_values(lf, sym, &symval);
1034 if (symval.value == 0) {
1035 error = ESRCH;
1036 goto out;
1037 }
1038 stop = (void **)symval.value;
1039
1040 /* and the number of entries */
1041 count = stop - start;
1042
1043 /* and copy out */
1044 if (startp)
1045 *startp = start;
1046 if (stopp)
1047 *stopp = stop;
1048 if (countp)
1049 *countp = count;
1050
1051 out:
1052 free(setsym, M_LINKER);
1053 return error;
1054}
1055