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