vkernel64: Fix the way syscalls are passed to the vkernel.
[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 $
d5fc85d0 27 * $DragonFly: src/sys/kern/link_elf.c,v 1.29 2008/08/01 23:11:16 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
1c0e3286
SS
53static int link_elf_preload_file(const char *, linker_file_t *);
54static int link_elf_preload_finish(linker_file_t);
55static int link_elf_load_file(const char*, linker_file_t*);
984263bc
MD
56static int link_elf_lookup_symbol(linker_file_t, const char*,
57 c_linker_sym_t*);
58static int link_elf_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t*);
59static int link_elf_search_symbol(linker_file_t, caddr_t value,
60 c_linker_sym_t* sym, long* diffp);
61
62static void link_elf_unload_file(linker_file_t);
63static void link_elf_unload_module(linker_file_t);
dc62b251
MD
64static int link_elf_lookup_set(linker_file_t, const char *,
65 void ***, void ***, int *);
1c0e3286
SS
66static int elf_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *);
67static void link_elf_reloc_local(linker_file_t lf);
dc62b251 68
984263bc 69static struct linker_class_ops link_elf_class_ops = {
1c0e3286
SS
70 link_elf_load_file,
71 link_elf_preload_file,
984263bc
MD
72};
73
74static struct linker_file_ops link_elf_file_ops = {
1c0e3286
SS
75 .lookup_symbol = link_elf_lookup_symbol,
76 .symbol_values = link_elf_symbol_values,
77 .search_symbol = link_elf_search_symbol,
78 .unload = link_elf_unload_file,
79 .lookup_set = link_elf_lookup_set
984263bc
MD
80};
81
82static struct linker_file_ops link_elf_module_ops = {
1c0e3286
SS
83 .lookup_symbol = link_elf_lookup_symbol,
84 .symbol_values = link_elf_symbol_values,
85 .search_symbol = link_elf_search_symbol,
86 .preload_finish = link_elf_preload_finish,
87 .unload = link_elf_unload_module,
88 .lookup_set = link_elf_lookup_set,
984263bc 89};
761c7789 90
984263bc
MD
91typedef struct elf_file {
92 caddr_t address; /* Relocation address */
93#ifdef SPARSE_MAPPING
94 vm_object_t object; /* VM object to hold file pages */
95#endif
96 const Elf_Dyn* dynamic; /* Symbol table etc. */
d5fc85d0
MD
97 Elf_Hashelt nbuckets; /* DT_HASH info */
98 Elf_Hashelt nchains;
99 const Elf_Hashelt* buckets;
100 const Elf_Hashelt* chains;
984263bc
MD
101 caddr_t hash;
102 caddr_t strtab; /* DT_STRTAB */
103 int strsz; /* DT_STRSZ */
104 const Elf_Sym* symtab; /* DT_SYMTAB */
105 Elf_Addr* got; /* DT_PLTGOT */
106 const Elf_Rel* pltrel; /* DT_JMPREL */
107 int pltrelsize; /* DT_PLTRELSZ */
108 const Elf_Rela* pltrela; /* DT_JMPREL */
109 int pltrelasize; /* DT_PLTRELSZ */
110 const Elf_Rel* rel; /* DT_REL */
111 int relsize; /* DT_RELSZ */
112 const Elf_Rela* rela; /* DT_RELA */
113 int relasize; /* DT_RELASZ */
114 caddr_t modptr;
115 const Elf_Sym* ddbsymtab; /* The symbol table we are using */
116 long ddbsymcnt; /* Number of symbols */
117 caddr_t ddbstrtab; /* String table */
118 long ddbstrcnt; /* number of bytes in string table */
119 caddr_t symbase; /* malloc'ed symbold base */
120 caddr_t strbase; /* malloc'ed string base */
121} *elf_file_t;
122
123static int parse_dynamic(linker_file_t lf);
984263bc
MD
124static int relocate_file(linker_file_t lf);
125static int parse_module_symbols(linker_file_t lf);
126
127/*
128 * The kernel symbol table starts here.
129 */
130extern struct _dynamic _DYNAMIC;
131
132static void
133link_elf_init(void* arg)
134{
984263bc
MD
135 Elf_Dyn *dp;
136 caddr_t modptr, baseptr, sizeptr;
137 elf_file_t ef;
138 char *modname;
984263bc
MD
139
140#if ELF_TARG_CLASS == ELFCLASS32
141 linker_add_class("elf32", NULL, &link_elf_class_ops);
142#else
143 linker_add_class("elf64", NULL, &link_elf_class_ops);
144#endif
145
984263bc
MD
146 dp = (Elf_Dyn*) &_DYNAMIC;
147 if (dp) {
efda3bd0 148 ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_INTWAIT | M_ZERO);
984263bc
MD
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 }
761c7789 176 parse_module_symbols(linker_kernel_file);
984263bc
MD
177 linker_current_file = linker_kernel_file;
178 linker_kernel_file->flags |= LINKER_FILE_LINKED;
179 }
984263bc
MD
180}
181
ba39e2e0 182SYSINIT(link_elf, SI_BOOT2_KLD, SI_ORDER_SECOND, link_elf_init, 0);
984263bc
MD
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) {
6ea70f76 214 kprintf("Symbols are corrupt!\n");
984263bc
MD
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) {
6ea70f76 224 kprintf("Symbols are corrupt!\n");
984263bc
MD
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 */
d5fc85d0 248 const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
984263bc
MD
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{
6ea70f76 324 kprintf("kldload: %s\n", s);
984263bc
MD
325}
326
327static int
1c0e3286 328link_elf_preload_file(const char *filename, linker_file_t *result)
984263bc
MD
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)
1c0e3286 342 return ENOENT;
984263bc
MD
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);
1c0e3286
SS
349 if (type == NULL ||
350 (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " module") != 0 &&
351 strcmp(type, "elf module") != 0))
984263bc
MD
352 return (EFTYPE);
353 if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
354 return (EINVAL);
355
efda3bd0 356 ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
357 ef->modptr = modptr;
358 ef->address = *(caddr_t *)baseptr;
359#ifdef SPARSE_MAPPING
360 ef->object = 0;
361#endif
362 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
363 ef->dynamic = (Elf_Dyn *)dp;
364 lf = linker_make_file(filename, ef, &link_elf_module_ops);
365 if (lf == NULL) {
efda3bd0 366 kfree(ef, M_LINKER);
984263bc
MD
367 return ENOMEM;
368 }
369 lf->address = ef->address;
370 lf->size = *(size_t *)sizeptr;
371
372 error = parse_dynamic(lf);
373 if (error) {
374 linker_file_unload(lf);
375 return error;
376 }
1c0e3286
SS
377 link_elf_reloc_local(lf);
378 *result = lf;
379 return (0);
380}
381
382static int
383link_elf_preload_finish(linker_file_t lf)
384{
385 int error;
386
984263bc 387 error = relocate_file(lf);
1c0e3286 388 if (error)
984263bc 389 return error;
761c7789 390 parse_module_symbols(lf);
1c0e3286 391
984263bc
MD
392 return (0);
393}
394
395static int
1c0e3286 396link_elf_load_file(const char* filename, linker_file_t* result)
984263bc 397{
fad57d0e 398 struct nlookupdata nd;
dadab5e9
MD
399 struct thread *td = curthread; /* XXX */
400 struct proc *p = td->td_proc;
fad57d0e 401 struct vnode *vp;
984263bc
MD
402 Elf_Ehdr *hdr;
403 caddr_t firstpage;
404 int nbytes, i;
405 Elf_Phdr *phdr;
406 Elf_Phdr *phlimit;
407 Elf_Phdr *segs[2];
408 int nsegs;
409 Elf_Phdr *phdyn;
410 Elf_Phdr *phphdr;
411 caddr_t mapbase;
412 size_t mapsize;
413 Elf_Off base_offset;
414 Elf_Addr base_vaddr;
415 Elf_Addr base_vlimit;
416 int error = 0;
417 int resid;
418 elf_file_t ef;
419 linker_file_t lf;
420 char *pathname;
421 Elf_Shdr *shdr;
422 int symtabindex;
423 int symstrindex;
424 int symcnt;
425 int strcnt;
426
dadab5e9 427 KKASSERT(p != NULL);
a3d82f10 428 if (p->p_ucred == NULL) {
6ea70f76 429 kprintf("link_elf_load_file: cannot load '%s' from filesystem"
a3d82f10
MD
430 " this early\n", filename);
431 return ENOENT;
432 }
984263bc
MD
433 shdr = NULL;
434 lf = NULL;
984263bc
MD
435 pathname = linker_search_path(filename);
436 if (pathname == NULL)
437 return ENOENT;
438
fad57d0e
MD
439 error = nlookup_init(&nd, pathname, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
440 if (error == 0)
441 error = vn_open(&nd, NULL, FREAD, 0);
efda3bd0 442 kfree(pathname, M_LINKER);
fad57d0e
MD
443 if (error) {
444 nlookup_done(&nd);
984263bc 445 return error;
fad57d0e
MD
446 }
447 vp = nd.nl_open_vp;
448 nd.nl_open_vp = NULL;
449 nlookup_done(&nd);
984263bc
MD
450
451 /*
452 * Read the elf header from the file.
453 */
efda3bd0 454 firstpage = kmalloc(PAGE_SIZE, M_LINKER, M_WAITOK);
984263bc 455 hdr = (Elf_Ehdr *)firstpage;
fad57d0e 456 error = vn_rdwr(UIO_READ, vp, firstpage, PAGE_SIZE, 0,
87de5057 457 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
458 nbytes = PAGE_SIZE - resid;
459 if (error)
460 goto out;
461
462 if (!IS_ELF(*hdr)) {
463 error = ENOEXEC;
464 goto out;
465 }
466
467 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
468 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
469 link_elf_error("Unsupported file layout");
470 error = ENOEXEC;
471 goto out;
472 }
473 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
474 || hdr->e_version != EV_CURRENT) {
475 link_elf_error("Unsupported file version");
476 error = ENOEXEC;
477 goto out;
478 }
479 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
1c0e3286 480 error = ENOSYS;
984263bc
MD
481 goto out;
482 }
483 if (hdr->e_machine != ELF_TARG_MACH) {
484 link_elf_error("Unsupported machine");
485 error = ENOEXEC;
486 goto out;
487 }
488
489 /*
490 * We rely on the program header being in the first page. This is
491 * not strictly required by the ABI specification, but it seems to
492 * always true in practice. And, it simplifies things considerably.
493 */
494 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
495 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
496 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
497 link_elf_error("Unreadable program headers");
498
499 /*
500 * Scan the program header entries, and save key information.
501 *
502 * We rely on there being exactly two load segments, text and data,
503 * in that order.
504 */
505 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
506 phlimit = phdr + hdr->e_phnum;
507 nsegs = 0;
508 phdyn = NULL;
509 phphdr = NULL;
510 while (phdr < phlimit) {
511 switch (phdr->p_type) {
512
513 case PT_LOAD:
514 if (nsegs == 2) {
515 link_elf_error("Too many sections");
516 error = ENOEXEC;
517 goto out;
518 }
519 segs[nsegs] = phdr;
520 ++nsegs;
521 break;
522
523 case PT_PHDR:
524 phphdr = phdr;
525 break;
526
527 case PT_DYNAMIC:
528 phdyn = phdr;
529 break;
1c0e3286
SS
530
531 case PT_INTERP:
532 error = ENOSYS;
533 goto out;
984263bc
MD
534 }
535
536 ++phdr;
537 }
538 if (phdyn == NULL) {
539 link_elf_error("Object is not dynamically-linked");
540 error = ENOEXEC;
541 goto out;
542 }
543
544 /*
545 * Allocate the entire address space of the object, to stake out our
546 * contiguous region, and to establish the base address for relocation.
547 */
548 base_offset = trunc_page(segs[0]->p_offset);
549 base_vaddr = trunc_page(segs[0]->p_vaddr);
550 base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
551 mapsize = base_vlimit - base_vaddr;
552
efda3bd0 553 ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
554#ifdef SPARSE_MAPPING
555 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
556 if (ef->object == NULL) {
efda3bd0 557 kfree(ef, M_LINKER);
984263bc
MD
558 error = ENOMEM;
559 goto out;
560 }
561 vm_object_reference(ef->object);
e4846942
MD
562 ef->address = (caddr_t)vm_map_min(&kernel_map);
563 error = vm_map_find(&kernel_map, ef->object, 0,
9388fcaa
MD
564 (vm_offset_t *)&ef->address,
565 mapsize, PAGE_SIZE,
566 1, VM_MAPTYPE_NORMAL,
1b874851
MD
567 VM_PROT_ALL, VM_PROT_ALL,
568 0);
984263bc
MD
569 if (error) {
570 vm_object_deallocate(ef->object);
efda3bd0 571 kfree(ef, M_LINKER);
984263bc
MD
572 goto out;
573 }
574#else
efda3bd0 575 ef->address = kmalloc(mapsize, M_LINKER, M_WAITOK);
984263bc
MD
576#endif
577 mapbase = ef->address;
578
579 /*
580 * Read the text and data sections and zero the bss.
581 */
582 for (i = 0; i < 2; i++) {
583 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
fad57d0e 584 error = vn_rdwr(UIO_READ, vp,
984263bc 585 segbase, segs[i]->p_filesz, segs[i]->p_offset,
87de5057 586 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
587 if (error) {
588#ifdef SPARSE_MAPPING
e4846942 589 vm_map_remove(&kernel_map, (vm_offset_t) ef->address,
984263bc
MD
590 (vm_offset_t) ef->address
591 + (ef->object->size << PAGE_SHIFT));
592 vm_object_deallocate(ef->object);
593#else
efda3bd0 594 kfree(ef->address, M_LINKER);
984263bc 595#endif
efda3bd0 596 kfree(ef, M_LINKER);
984263bc
MD
597 goto out;
598 }
599 bzero(segbase + segs[i]->p_filesz,
600 segs[i]->p_memsz - segs[i]->p_filesz);
601
602#ifdef SPARSE_MAPPING
603 /*
604 * Wire down the pages
605 */
e4846942
MD
606 vm_map_wire(&kernel_map,
607 (vm_offset_t) segbase,
608 (vm_offset_t) segbase + segs[i]->p_memsz,
609 0);
984263bc
MD
610#endif
611 }
612
613 ef->dynamic = (const Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
614
615 lf = linker_make_file(filename, ef, &link_elf_file_ops);
616 if (lf == NULL) {
617#ifdef SPARSE_MAPPING
e4846942 618 vm_map_remove(&kernel_map, (vm_offset_t) ef->address,
984263bc
MD
619 (vm_offset_t) ef->address
620 + (ef->object->size << PAGE_SHIFT));
621 vm_object_deallocate(ef->object);
622#else
efda3bd0 623 kfree(ef->address, M_LINKER);
984263bc 624#endif
efda3bd0 625 kfree(ef, M_LINKER);
984263bc
MD
626 error = ENOMEM;
627 goto out;
628 }
629 lf->address = ef->address;
630 lf->size = mapsize;
631
632 error = parse_dynamic(lf);
633 if (error)
634 goto out;
1c0e3286
SS
635 link_elf_reloc_local(lf);
636 error = linker_load_dependencies(lf);
984263bc
MD
637 if (error)
638 goto out;
639 error = relocate_file(lf);
640 if (error)
641 goto out;
642
643 /* Try and load the symbol table if it's present. (you can strip it!) */
644 nbytes = hdr->e_shnum * hdr->e_shentsize;
645 if (nbytes == 0 || hdr->e_shoff == 0)
646 goto nosyms;
efda3bd0 647 shdr = kmalloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
fad57d0e 648 error = vn_rdwr(UIO_READ, vp,
984263bc 649 (caddr_t)shdr, nbytes, hdr->e_shoff,
87de5057 650 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
651 if (error)
652 goto out;
653 symtabindex = -1;
654 symstrindex = -1;
655 for (i = 0; i < hdr->e_shnum; i++) {
656 if (shdr[i].sh_type == SHT_SYMTAB) {
657 symtabindex = i;
658 symstrindex = shdr[i].sh_link;
659 }
660 }
661 if (symtabindex < 0 || symstrindex < 0)
662 goto nosyms;
663
664 symcnt = shdr[symtabindex].sh_size;
efda3bd0 665 ef->symbase = kmalloc(symcnt, M_LINKER, M_WAITOK);
984263bc 666 strcnt = shdr[symstrindex].sh_size;
efda3bd0 667 ef->strbase = kmalloc(strcnt, M_LINKER, M_WAITOK);
fad57d0e 668 error = vn_rdwr(UIO_READ, vp,
984263bc 669 ef->symbase, symcnt, shdr[symtabindex].sh_offset,
87de5057 670 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
671 if (error)
672 goto out;
fad57d0e 673 error = vn_rdwr(UIO_READ, vp,
984263bc 674 ef->strbase, strcnt, shdr[symstrindex].sh_offset,
87de5057 675 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
676 if (error)
677 goto out;
678
679 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
680 ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
681 ef->ddbstrcnt = strcnt;
682 ef->ddbstrtab = ef->strbase;
683
984263bc
MD
684nosyms:
685
686 *result = lf;
687
688out:
689 if (error && lf)
690 linker_file_unload(lf);
691 if (shdr)
efda3bd0 692 kfree(shdr, M_LINKER);
984263bc 693 if (firstpage)
efda3bd0 694 kfree(firstpage, M_LINKER);
a11aaa81 695 vn_unlock(vp);
87de5057 696 vn_close(vp, FREAD);
984263bc
MD
697
698 return error;
699}
700
1c0e3286
SS
701Elf_Addr
702elf_relocaddr(linker_file_t lf, Elf_Addr x)
703{
704 elf_file_t ef;
705
706 ef = lf->priv;
707#if 0
708 if (x >= ef->pcpu_start && x < ef->pcpu_stop)
709 return ((x - ef->pcpu_start) + ef->pcpu_base);
710#ifdef VIMAGE
711 if (x >= ef->vnet_start && x < ef->vnet_stop)
712 return ((x - ef->vnet_start) + ef->vnet_base);
713#endif
714#endif
715 return (x);
716}
717
984263bc
MD
718static void
719link_elf_unload_file(linker_file_t file)
720{
721 elf_file_t ef = file->priv;
722
723 if (ef) {
724#ifdef SPARSE_MAPPING
725 if (ef->object) {
e4846942 726 vm_map_remove(&kernel_map, (vm_offset_t) ef->address,
984263bc
MD
727 (vm_offset_t) ef->address
728 + (ef->object->size << PAGE_SHIFT));
729 vm_object_deallocate(ef->object);
730 }
731#else
732 if (ef->address)
efda3bd0 733 kfree(ef->address, M_LINKER);
984263bc
MD
734#endif
735 if (ef->symbase)
efda3bd0 736 kfree(ef->symbase, M_LINKER);
984263bc 737 if (ef->strbase)
efda3bd0
MD
738 kfree(ef->strbase, M_LINKER);
739 kfree(ef, M_LINKER);
984263bc
MD
740 }
741}
742
743static void
744link_elf_unload_module(linker_file_t file)
745{
746 elf_file_t ef = file->priv;
747
748 if (ef)
efda3bd0 749 kfree(ef, M_LINKER);
984263bc
MD
750 if (file->filename)
751 preload_delete_name(file->filename);
752}
753
984263bc 754static const char *
d5fc85d0 755symbol_name(elf_file_t ef, Elf_Size r_info)
984263bc
MD
756{
757 const Elf_Sym *ref;
758
759 if (ELF_R_SYM(r_info)) {
760 ref = ef->symtab + ELF_R_SYM(r_info);
761 return ef->strtab + ref->st_name;
762 } else
763 return NULL;
764}
765
766static int
767relocate_file(linker_file_t lf)
768{
769 elf_file_t ef = lf->priv;
770 const Elf_Rel *rellim;
771 const Elf_Rel *rel;
772 const Elf_Rela *relalim;
773 const Elf_Rela *rela;
774 const char *symname;
775
776 /* Perform relocations without addend if there are any: */
777 rel = ef->rel;
778 if (rel) {
779 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
780 while (rel < rellim) {
1c0e3286
SS
781 if (elf_reloc(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL, elf_lookup)) {
782 symname = symbol_name(ef, rel->r_info);
6ea70f76 783 kprintf("link_elf: symbol %s undefined\n", symname);
984263bc
MD
784 return ENOENT;
785 }
786 rel++;
787 }
788 }
789
790 /* Perform relocations with addend if there are any: */
791 rela = ef->rela;
792 if (rela) {
793 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
794 while (rela < relalim) {
1c0e3286
SS
795 if (elf_reloc(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA, elf_lookup)) {
796 symname = symbol_name(ef, rela->r_info);
6ea70f76 797 kprintf("link_elf: symbol %s undefined\n", symname);
984263bc
MD
798 return ENOENT;
799 }
800 rela++;
801 }
802 }
803
804 /* Perform PLT relocations without addend if there are any: */
805 rel = ef->pltrel;
806 if (rel) {
807 rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
808 while (rel < rellim) {
1c0e3286
SS
809 if (elf_reloc(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL, elf_lookup)) {
810 symname = symbol_name(ef, rel->r_info);
6ea70f76 811 kprintf("link_elf: symbol %s undefined\n", symname);
984263bc
MD
812 return ENOENT;
813 }
814 rel++;
815 }
816 }
817
818 /* Perform relocations with addend if there are any: */
819 rela = ef->pltrela;
820 if (rela) {
821 relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
822 while (rela < relalim) {
823 symname = symbol_name(ef, rela->r_info);
1c0e3286 824 if (elf_reloc(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA, elf_lookup)) {
6ea70f76 825 kprintf("link_elf: symbol %s undefined\n", symname);
984263bc
MD
826 return ENOENT;
827 }
828 rela++;
829 }
830 }
831
832 return 0;
833}
834
835/*
836 * Hash function for symbol table lookup. Don't even think about changing
837 * this. It is specified by the System V ABI.
838 */
839static unsigned long
840elf_hash(const char *name)
841{
842 const unsigned char *p = (const unsigned char *) name;
843 unsigned long h = 0;
844 unsigned long g;
845
846 while (*p != '\0') {
847 h = (h << 4) + *p++;
848 if ((g = h & 0xf0000000) != 0)
849 h ^= g >> 24;
850 h &= ~g;
851 }
852 return h;
853}
854
855int
856link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
857{
858 elf_file_t ef = lf->priv;
859 unsigned long symnum;
860 const Elf_Sym* symp;
861 const char *strp;
862 unsigned long hash;
863 int i;
864
1c0e3286
SS
865 /* If we don't have a hash, bail. */
866 if (ef->buckets == NULL || ef->nbuckets == 0) {
867 kprintf("link_elf_lookup_symbol: missing symbol hash table\n");
868 return ENOENT;
869 }
870
984263bc
MD
871 /* First, search hashed global symbols */
872 hash = elf_hash(name);
873 symnum = ef->buckets[hash % ef->nbuckets];
874
875 while (symnum != STN_UNDEF) {
876 if (symnum >= ef->nchains) {
6ea70f76 877 kprintf("link_elf_lookup_symbol: corrupt symbol table\n");
984263bc
MD
878 return ENOENT;
879 }
880
881 symp = ef->symtab + symnum;
882 if (symp->st_name == 0) {
6ea70f76 883 kprintf("link_elf_lookup_symbol: corrupt symbol table\n");
984263bc
MD
884 return ENOENT;
885 }
886
887 strp = ef->strtab + symp->st_name;
888
889 if (strcmp(name, strp) == 0) {
890 if (symp->st_shndx != SHN_UNDEF ||
891 (symp->st_value != 0 &&
d1786c1b
MD
892 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
893 ) {
984263bc
MD
894 *sym = (c_linker_sym_t) symp;
895 return 0;
d1786c1b 896 } else {
984263bc 897 return ENOENT;
d1786c1b 898 }
984263bc
MD
899 }
900
901 symnum = ef->chains[symnum];
902 }
903
904 /* If we have not found it, look at the full table (if loaded) */
905 if (ef->symtab == ef->ddbsymtab)
906 return ENOENT;
907
908 /* Exhaustive search */
909 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
910 strp = ef->ddbstrtab + symp->st_name;
911 if (strcmp(name, strp) == 0) {
912 if (symp->st_shndx != SHN_UNDEF ||
913 (symp->st_value != 0 &&
914 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
915 *sym = (c_linker_sym_t) symp;
916 return 0;
d1786c1b 917 } else {
984263bc 918 return ENOENT;
d1786c1b 919 }
984263bc
MD
920 }
921 }
984263bc
MD
922 return ENOENT;
923}
924
925static int
1c0e3286 926link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t *symval)
984263bc 927{
1c0e3286
SS
928 elf_file_t ef = lf->priv;
929 const Elf_Sym *es = (const Elf_Sym *)sym;
930
931 if (es >= ef->symtab && ((es - ef->symtab) < ef->nchains)) {
932 symval->name = ef->strtab + es->st_name;
933 symval->value = (caddr_t) ef->address + es->st_value;
934 symval->size = es->st_size;
935 return 0;
936 }
937 if (ef->symtab == ef->ddbsymtab)
984263bc 938 return ENOENT;
1c0e3286
SS
939 if (es >= ef->ddbsymtab && ((es - ef->ddbsymtab) < ef->ddbsymcnt)) {
940 symval->name = ef->ddbstrtab + es->st_name;
941 symval->value = (caddr_t) ef->address + es->st_value;
942 symval->size = es->st_size;
943 return 0;
944 }
945 return ENOENT;
984263bc
MD
946}
947
948static int
949link_elf_search_symbol(linker_file_t lf, caddr_t value,
1c0e3286 950 c_linker_sym_t *sym, long *diffp)
984263bc 951{
1c0e3286
SS
952 elf_file_t ef = lf->priv;
953 u_long off = (uintptr_t)(void *)value;
954 u_long diff = off;
955 u_long st_value;
956 const Elf_Sym *es;
957 const Elf_Sym *best = 0;
958 int i;
959
960 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
961 if (es->st_name == 0)
962 continue;
963 st_value = es->st_value + (uintptr_t)(void *)ef->address;
964 if (off >= st_value) {
965 if (off - st_value < diff) {
966 diff = off - st_value;
967 best = es;
968 if (diff == 0)
969 break;
970 } else if (off - st_value == diff) {
971 best = es;
972 }
984263bc 973 }
1c0e3286
SS
974 }
975 if (best == 0)
976 *diffp = off;
977 else
978 *diffp = diff;
979 *sym = (c_linker_sym_t) best;
984263bc 980
1c0e3286 981 return 0;
984263bc 982}
dc62b251
MD
983
984/*
985 * Look up a linker set on an ELF system.
986 */
987static int
988link_elf_lookup_set(linker_file_t lf, const char *name,
1c0e3286 989 void ***startp, void ***stopp, int *countp)
dc62b251 990{
1c0e3286
SS
991 c_linker_sym_t sym;
992 linker_symval_t symval;
993 char *setsym;
994 void **start, **stop;
995 int len, error = 0, count;
996
997 len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
998 setsym = kmalloc(len, M_LINKER, M_WAITOK);
999
1000 /* get address of first entry */
1001 ksnprintf(setsym, len, "%s%s", "__start_set_", name);
1002 error = link_elf_lookup_symbol(lf, setsym, &sym);
1003 if (error)
1004 goto out;
1005 link_elf_symbol_values(lf, sym, &symval);
1006 if (symval.value == 0) {
1007 error = ESRCH;
1008 goto out;
1009 }
1010 start = (void **)symval.value;
1011
1012 /* get address of last entry */
1013 ksnprintf(setsym, len, "%s%s", "__stop_set_", name);
1014 error = link_elf_lookup_symbol(lf, setsym, &sym);
1015 if (error)
1016 goto out;
1017 link_elf_symbol_values(lf, sym, &symval);
1018 if (symval.value == 0) {
1019 error = ESRCH;
1020 goto out;
1021 }
1022 stop = (void **)symval.value;
dc62b251 1023
1c0e3286
SS
1024 /* and the number of entries */
1025 count = stop - start;
dc62b251 1026
1c0e3286
SS
1027 /* and copy out */
1028 if (startp)
1029 *startp = start;
1030 if (stopp)
1031 *stopp = stop;
1032 if (countp)
1033 *countp = count;
dc62b251 1034
1c0e3286
SS
1035out:
1036 kfree(setsym, M_LINKER);
1037 return error;
1038}
1039
1040/*
1041 * Symbol lookup function that can be used when the symbol index is known (ie
1042 * in relocations). It uses the symbol index instead of doing a fully fledged
1043 * hash table based lookup when such is valid. For example for local symbols.
1044 * This is not only more efficient, it's also more correct. It's not always
1045 * the case that the symbol can be found through the hash table.
1046 */
1047static int
1048elf_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *result)
1049{
1050 elf_file_t ef = lf->priv;
1051 const Elf_Sym *sym;
1052 const char *symbol;
1053
1054 /* Don't even try to lookup the symbol if the index is bogus. */
1055 if (symidx >= ef->nchains)
1056 return (ENOENT);
1057
1058 sym = ef->symtab + symidx;
1059
1060 /*
1061 * Don't do a full lookup when the symbol is local. It may even
1062 * fail because it may not be found through the hash table.
1063 */
1064 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1065 /* Force lookup failure when we have an insanity. */
1066 if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0)
1067 return (ENOENT);
1068 return ((Elf_Addr) ef->address + sym->st_value);
1069 }
1070 /*
1071 * XXX we can avoid doing a hash table based lookup for global
1072 * symbols as well. This however is not always valid, so we'll
1073 * just do it the hard way for now. Performance tweaks can
1074 * always be added.
1075 */
1076
1077 symbol = ef->strtab + sym->st_name;
1078
1079 /* Force a lookup failure if the symbol name is bogus. */
1080 if (*symbol == 0)
1081 return (ENOENT);
1082
1083 return (linker_file_lookup_symbol(lf, symbol, deps, (caddr_t *)result));
dc62b251 1084}
1c0e3286
SS
1085static void
1086link_elf_reloc_local(linker_file_t lf)
1087{
1088 elf_file_t ef = lf->priv;
1089 const Elf_Rel *rellim;
1090 const Elf_Rel *rel;
1091 const Elf_Rela *relalim;
1092 const Elf_Rela *rela;
1093
1094 /* Perform relocations without addend if there are any: */
1095 if ((rel = ef->rel) != NULL) {
1096 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
1097 while (rel < rellim) {
1098 elf_reloc_local(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
1099 elf_lookup);
1100 rel++;
1101 }
1102 }
dc62b251 1103
1c0e3286
SS
1104 /* Perform relocations with addend if there are any: */
1105 if ((rela = ef->rela) != NULL) {
1106 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
1107 while (rela < relalim) {
1108 elf_reloc_local(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
1109 elf_lookup);
1110 rela++;
1111 }
1112 }
1113}