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