hammer - Fix core dump during remote termination of mirror-stream
[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);
984263bc
MD
152 ef->address = 0;
153#ifdef SPARSE_MAPPING
154 ef->object = 0;
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
366 ef->object = 0;
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;
416 Elf_Phdr *phphdr;
417 caddr_t mapbase;
418 size_t mapsize;
419 Elf_Off base_offset;
420 Elf_Addr base_vaddr;
421 Elf_Addr base_vlimit;
422 int error = 0;
423 int resid;
424 elf_file_t ef;
425 linker_file_t lf;
426 char *pathname;
427 Elf_Shdr *shdr;
428 int symtabindex;
429 int symstrindex;
430 int symcnt;
431 int strcnt;
432
6d6c8d9d
JT
433 /* XXX Hack for firmware loading where p == NULL */
434 if (p == NULL) {
435 p = &proc0;
436 }
437
dadab5e9 438 KKASSERT(p != NULL);
a3d82f10 439 if (p->p_ucred == NULL) {
6ea70f76 440 kprintf("link_elf_load_file: cannot load '%s' from filesystem"
a3d82f10
MD
441 " this early\n", filename);
442 return ENOENT;
443 }
984263bc
MD
444 shdr = NULL;
445 lf = NULL;
984263bc
MD
446 pathname = linker_search_path(filename);
447 if (pathname == NULL)
448 return ENOENT;
449
fad57d0e
MD
450 error = nlookup_init(&nd, pathname, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
451 if (error == 0)
452 error = vn_open(&nd, NULL, FREAD, 0);
efda3bd0 453 kfree(pathname, M_LINKER);
fad57d0e
MD
454 if (error) {
455 nlookup_done(&nd);
984263bc 456 return error;
fad57d0e
MD
457 }
458 vp = nd.nl_open_vp;
459 nd.nl_open_vp = NULL;
460 nlookup_done(&nd);
984263bc
MD
461
462 /*
463 * Read the elf header from the file.
464 */
efda3bd0 465 firstpage = kmalloc(PAGE_SIZE, M_LINKER, M_WAITOK);
984263bc 466 hdr = (Elf_Ehdr *)firstpage;
fad57d0e 467 error = vn_rdwr(UIO_READ, vp, firstpage, PAGE_SIZE, 0,
87de5057 468 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
469 nbytes = PAGE_SIZE - resid;
470 if (error)
471 goto out;
472
473 if (!IS_ELF(*hdr)) {
474 error = ENOEXEC;
475 goto out;
476 }
477
478 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
479 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
480 link_elf_error("Unsupported file layout");
481 error = ENOEXEC;
482 goto out;
483 }
484 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
485 || hdr->e_version != EV_CURRENT) {
486 link_elf_error("Unsupported file version");
487 error = ENOEXEC;
488 goto out;
489 }
490 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
1c0e3286 491 error = ENOSYS;
984263bc
MD
492 goto out;
493 }
494 if (hdr->e_machine != ELF_TARG_MACH) {
495 link_elf_error("Unsupported machine");
496 error = ENOEXEC;
497 goto out;
498 }
499
500 /*
501 * We rely on the program header being in the first page. This is
502 * not strictly required by the ABI specification, but it seems to
503 * always true in practice. And, it simplifies things considerably.
504 */
505 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
506 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
507 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
508 link_elf_error("Unreadable program headers");
509
510 /*
511 * Scan the program header entries, and save key information.
512 *
513 * We rely on there being exactly two load segments, text and data,
514 * in that order.
515 */
516 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
517 phlimit = phdr + hdr->e_phnum;
518 nsegs = 0;
519 phdyn = NULL;
520 phphdr = NULL;
521 while (phdr < phlimit) {
522 switch (phdr->p_type) {
523
524 case PT_LOAD:
525 if (nsegs == 2) {
526 link_elf_error("Too many sections");
527 error = ENOEXEC;
528 goto out;
529 }
530 segs[nsegs] = phdr;
531 ++nsegs;
532 break;
533
534 case PT_PHDR:
535 phphdr = phdr;
536 break;
537
538 case PT_DYNAMIC:
539 phdyn = phdr;
540 break;
1c0e3286
SS
541
542 case PT_INTERP:
543 error = ENOSYS;
544 goto out;
984263bc
MD
545 }
546
547 ++phdr;
548 }
549 if (phdyn == NULL) {
550 link_elf_error("Object is not dynamically-linked");
551 error = ENOEXEC;
552 goto out;
553 }
554
555 /*
556 * Allocate the entire address space of the object, to stake out our
557 * contiguous region, and to establish the base address for relocation.
558 */
559 base_offset = trunc_page(segs[0]->p_offset);
560 base_vaddr = trunc_page(segs[0]->p_vaddr);
561 base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
562 mapsize = base_vlimit - base_vaddr;
563
efda3bd0 564 ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
565#ifdef SPARSE_MAPPING
566 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
567 if (ef->object == NULL) {
efda3bd0 568 kfree(ef, M_LINKER);
984263bc
MD
569 error = ENOMEM;
570 goto out;
571 }
b12defdc
MD
572 vm_object_hold(ef->object);
573 vm_object_reference_locked(ef->object);
e4846942
MD
574 ef->address = (caddr_t)vm_map_min(&kernel_map);
575 error = vm_map_find(&kernel_map, ef->object, 0,
9388fcaa
MD
576 (vm_offset_t *)&ef->address,
577 mapsize, PAGE_SIZE,
578 1, VM_MAPTYPE_NORMAL,
1b874851
MD
579 VM_PROT_ALL, VM_PROT_ALL,
580 0);
b12defdc 581 vm_object_drop(ef->object);
984263bc
MD
582 if (error) {
583 vm_object_deallocate(ef->object);
efda3bd0 584 kfree(ef, M_LINKER);
984263bc
MD
585 goto out;
586 }
587#else
efda3bd0 588 ef->address = kmalloc(mapsize, M_LINKER, M_WAITOK);
984263bc
MD
589#endif
590 mapbase = ef->address;
591
592 /*
593 * Read the text and data sections and zero the bss.
594 */
595 for (i = 0; i < 2; i++) {
596 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
fad57d0e 597 error = vn_rdwr(UIO_READ, vp,
984263bc 598 segbase, segs[i]->p_filesz, segs[i]->p_offset,
87de5057 599 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
600 if (error) {
601#ifdef SPARSE_MAPPING
e4846942 602 vm_map_remove(&kernel_map, (vm_offset_t) ef->address,
984263bc
MD
603 (vm_offset_t) ef->address
604 + (ef->object->size << PAGE_SHIFT));
605 vm_object_deallocate(ef->object);
606#else
efda3bd0 607 kfree(ef->address, M_LINKER);
984263bc 608#endif
efda3bd0 609 kfree(ef, M_LINKER);
984263bc
MD
610 goto out;
611 }
612 bzero(segbase + segs[i]->p_filesz,
613 segs[i]->p_memsz - segs[i]->p_filesz);
614
615#ifdef SPARSE_MAPPING
616 /*
617 * Wire down the pages
618 */
e4846942
MD
619 vm_map_wire(&kernel_map,
620 (vm_offset_t) segbase,
621 (vm_offset_t) segbase + segs[i]->p_memsz,
622 0);
984263bc
MD
623#endif
624 }
625
626 ef->dynamic = (const Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
627
628 lf = linker_make_file(filename, ef, &link_elf_file_ops);
629 if (lf == NULL) {
630#ifdef SPARSE_MAPPING
e4846942 631 vm_map_remove(&kernel_map, (vm_offset_t) ef->address,
984263bc
MD
632 (vm_offset_t) ef->address
633 + (ef->object->size << PAGE_SHIFT));
634 vm_object_deallocate(ef->object);
635#else
efda3bd0 636 kfree(ef->address, M_LINKER);
984263bc 637#endif
efda3bd0 638 kfree(ef, M_LINKER);
984263bc
MD
639 error = ENOMEM;
640 goto out;
641 }
642 lf->address = ef->address;
643 lf->size = mapsize;
644
645 error = parse_dynamic(lf);
646 if (error)
647 goto out;
1c0e3286
SS
648 link_elf_reloc_local(lf);
649 error = linker_load_dependencies(lf);
984263bc
MD
650 if (error)
651 goto out;
652 error = relocate_file(lf);
653 if (error)
654 goto out;
655
656 /* Try and load the symbol table if it's present. (you can strip it!) */
657 nbytes = hdr->e_shnum * hdr->e_shentsize;
658 if (nbytes == 0 || hdr->e_shoff == 0)
659 goto nosyms;
efda3bd0 660 shdr = kmalloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
fad57d0e 661 error = vn_rdwr(UIO_READ, vp,
984263bc 662 (caddr_t)shdr, nbytes, hdr->e_shoff,
87de5057 663 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
664 if (error)
665 goto out;
666 symtabindex = -1;
667 symstrindex = -1;
668 for (i = 0; i < hdr->e_shnum; i++) {
669 if (shdr[i].sh_type == SHT_SYMTAB) {
670 symtabindex = i;
671 symstrindex = shdr[i].sh_link;
672 }
673 }
674 if (symtabindex < 0 || symstrindex < 0)
675 goto nosyms;
676
677 symcnt = shdr[symtabindex].sh_size;
efda3bd0 678 ef->symbase = kmalloc(symcnt, M_LINKER, M_WAITOK);
984263bc 679 strcnt = shdr[symstrindex].sh_size;
efda3bd0 680 ef->strbase = kmalloc(strcnt, M_LINKER, M_WAITOK);
fad57d0e 681 error = vn_rdwr(UIO_READ, vp,
984263bc 682 ef->symbase, symcnt, shdr[symtabindex].sh_offset,
87de5057 683 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
684 if (error)
685 goto out;
fad57d0e 686 error = vn_rdwr(UIO_READ, vp,
984263bc 687 ef->strbase, strcnt, shdr[symstrindex].sh_offset,
87de5057 688 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
984263bc
MD
689 if (error)
690 goto out;
691
692 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
693 ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
694 ef->ddbstrcnt = strcnt;
695 ef->ddbstrtab = ef->strbase;
696
984263bc
MD
697nosyms:
698
699 *result = lf;
700
701out:
702 if (error && lf)
703 linker_file_unload(lf);
704 if (shdr)
efda3bd0 705 kfree(shdr, M_LINKER);
984263bc 706 if (firstpage)
efda3bd0 707 kfree(firstpage, M_LINKER);
a11aaa81 708 vn_unlock(vp);
87de5057 709 vn_close(vp, FREAD);
984263bc
MD
710
711 return error;
712}
713
1c0e3286
SS
714Elf_Addr
715elf_relocaddr(linker_file_t lf, Elf_Addr x)
716{
717 elf_file_t ef;
718
719 ef = lf->priv;
720#if 0
721 if (x >= ef->pcpu_start && x < ef->pcpu_stop)
722 return ((x - ef->pcpu_start) + ef->pcpu_base);
723#ifdef VIMAGE
724 if (x >= ef->vnet_start && x < ef->vnet_stop)
725 return ((x - ef->vnet_start) + ef->vnet_base);
726#endif
727#endif
728 return (x);
729}
730
984263bc
MD
731static void
732link_elf_unload_file(linker_file_t file)
733{
734 elf_file_t ef = file->priv;
735
736 if (ef) {
737#ifdef SPARSE_MAPPING
738 if (ef->object) {
e4846942 739 vm_map_remove(&kernel_map, (vm_offset_t) ef->address,
984263bc
MD
740 (vm_offset_t) ef->address
741 + (ef->object->size << PAGE_SHIFT));
742 vm_object_deallocate(ef->object);
743 }
744#else
745 if (ef->address)
efda3bd0 746 kfree(ef->address, M_LINKER);
984263bc
MD
747#endif
748 if (ef->symbase)
efda3bd0 749 kfree(ef->symbase, M_LINKER);
984263bc 750 if (ef->strbase)
efda3bd0
MD
751 kfree(ef->strbase, M_LINKER);
752 kfree(ef, M_LINKER);
984263bc
MD
753 }
754}
755
756static void
757link_elf_unload_module(linker_file_t file)
758{
759 elf_file_t ef = file->priv;
760
761 if (ef)
efda3bd0 762 kfree(ef, M_LINKER);
984263bc
MD
763 if (file->filename)
764 preload_delete_name(file->filename);
765}
766
984263bc 767static const char *
d5fc85d0 768symbol_name(elf_file_t ef, Elf_Size r_info)
984263bc
MD
769{
770 const Elf_Sym *ref;
771
772 if (ELF_R_SYM(r_info)) {
773 ref = ef->symtab + ELF_R_SYM(r_info);
774 return ef->strtab + ref->st_name;
775 } else
776 return NULL;
777}
778
779static int
780relocate_file(linker_file_t lf)
781{
782 elf_file_t ef = lf->priv;
783 const Elf_Rel *rellim;
784 const Elf_Rel *rel;
785 const Elf_Rela *relalim;
786 const Elf_Rela *rela;
787 const char *symname;
788
789 /* Perform relocations without addend if there are any: */
790 rel = ef->rel;
791 if (rel) {
792 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
793 while (rel < rellim) {
1c0e3286
SS
794 if (elf_reloc(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL, elf_lookup)) {
795 symname = symbol_name(ef, rel->r_info);
6ea70f76 796 kprintf("link_elf: symbol %s undefined\n", symname);
984263bc
MD
797 return ENOENT;
798 }
799 rel++;
800 }
801 }
802
803 /* Perform relocations with addend if there are any: */
804 rela = ef->rela;
805 if (rela) {
806 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
807 while (rela < relalim) {
1c0e3286
SS
808 if (elf_reloc(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA, elf_lookup)) {
809 symname = symbol_name(ef, rela->r_info);
6ea70f76 810 kprintf("link_elf: symbol %s undefined\n", symname);
984263bc
MD
811 return ENOENT;
812 }
813 rela++;
814 }
815 }
816
817 /* Perform PLT relocations without addend if there are any: */
818 rel = ef->pltrel;
819 if (rel) {
820 rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
821 while (rel < rellim) {
1c0e3286
SS
822 if (elf_reloc(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL, elf_lookup)) {
823 symname = symbol_name(ef, rel->r_info);
6ea70f76 824 kprintf("link_elf: symbol %s undefined\n", symname);
984263bc
MD
825 return ENOENT;
826 }
827 rel++;
828 }
829 }
830
831 /* Perform relocations with addend if there are any: */
832 rela = ef->pltrela;
833 if (rela) {
834 relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
835 while (rela < relalim) {
836 symname = symbol_name(ef, rela->r_info);
1c0e3286 837 if (elf_reloc(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA, elf_lookup)) {
6ea70f76 838 kprintf("link_elf: symbol %s undefined\n", symname);
984263bc
MD
839 return ENOENT;
840 }
841 rela++;
842 }
843 }
844
845 return 0;
846}
847
848/*
849 * Hash function for symbol table lookup. Don't even think about changing
850 * this. It is specified by the System V ABI.
851 */
852static unsigned long
853elf_hash(const char *name)
854{
855 const unsigned char *p = (const unsigned char *) name;
856 unsigned long h = 0;
857 unsigned long g;
858
859 while (*p != '\0') {
860 h = (h << 4) + *p++;
861 if ((g = h & 0xf0000000) != 0)
862 h ^= g >> 24;
863 h &= ~g;
864 }
865 return h;
866}
867
868int
869link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
870{
871 elf_file_t ef = lf->priv;
872 unsigned long symnum;
873 const Elf_Sym* symp;
874 const char *strp;
875 unsigned long hash;
876 int i;
877
1c0e3286
SS
878 /* If we don't have a hash, bail. */
879 if (ef->buckets == NULL || ef->nbuckets == 0) {
880 kprintf("link_elf_lookup_symbol: missing symbol hash table\n");
881 return ENOENT;
882 }
883
984263bc
MD
884 /* First, search hashed global symbols */
885 hash = elf_hash(name);
886 symnum = ef->buckets[hash % ef->nbuckets];
887
888 while (symnum != STN_UNDEF) {
889 if (symnum >= ef->nchains) {
6ea70f76 890 kprintf("link_elf_lookup_symbol: corrupt symbol table\n");
984263bc
MD
891 return ENOENT;
892 }
893
894 symp = ef->symtab + symnum;
895 if (symp->st_name == 0) {
6ea70f76 896 kprintf("link_elf_lookup_symbol: corrupt symbol table\n");
984263bc
MD
897 return ENOENT;
898 }
899
900 strp = ef->strtab + symp->st_name;
901
902 if (strcmp(name, strp) == 0) {
903 if (symp->st_shndx != SHN_UNDEF ||
904 (symp->st_value != 0 &&
d1786c1b
MD
905 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
906 ) {
984263bc
MD
907 *sym = (c_linker_sym_t) symp;
908 return 0;
d1786c1b 909 } else {
984263bc 910 return ENOENT;
d1786c1b 911 }
984263bc
MD
912 }
913
914 symnum = ef->chains[symnum];
915 }
916
917 /* If we have not found it, look at the full table (if loaded) */
918 if (ef->symtab == ef->ddbsymtab)
919 return ENOENT;
920
921 /* Exhaustive search */
922 for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
923 strp = ef->ddbstrtab + symp->st_name;
924 if (strcmp(name, strp) == 0) {
925 if (symp->st_shndx != SHN_UNDEF ||
926 (symp->st_value != 0 &&
927 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
928 *sym = (c_linker_sym_t) symp;
929 return 0;
d1786c1b 930 } else {
984263bc 931 return ENOENT;
d1786c1b 932 }
984263bc
MD
933 }
934 }
984263bc
MD
935 return ENOENT;
936}
937
938static int
1c0e3286 939link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t *symval)
984263bc 940{
1c0e3286
SS
941 elf_file_t ef = lf->priv;
942 const Elf_Sym *es = (const Elf_Sym *)sym;
943
944 if (es >= ef->symtab && ((es - ef->symtab) < ef->nchains)) {
945 symval->name = ef->strtab + es->st_name;
51295aee 946 symval->value = ef->address + es->st_value;
1c0e3286
SS
947 symval->size = es->st_size;
948 return 0;
949 }
950 if (ef->symtab == ef->ddbsymtab)
984263bc 951 return ENOENT;
1c0e3286
SS
952 if (es >= ef->ddbsymtab && ((es - ef->ddbsymtab) < ef->ddbsymcnt)) {
953 symval->name = ef->ddbstrtab + es->st_name;
51295aee 954 symval->value = ef->address + es->st_value;
1c0e3286
SS
955 symval->size = es->st_size;
956 return 0;
957 }
958 return ENOENT;
984263bc
MD
959}
960
961static int
962link_elf_search_symbol(linker_file_t lf, caddr_t value,
1c0e3286 963 c_linker_sym_t *sym, long *diffp)
984263bc 964{
1c0e3286
SS
965 elf_file_t ef = lf->priv;
966 u_long off = (uintptr_t)(void *)value;
967 u_long diff = off;
968 u_long st_value;
969 const Elf_Sym *es;
4090d6ff 970 const Elf_Sym *best = NULL;
1c0e3286
SS
971 int i;
972
973 for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
974 if (es->st_name == 0)
975 continue;
976 st_value = es->st_value + (uintptr_t)(void *)ef->address;
977 if (off >= st_value) {
978 if (off - st_value < diff) {
979 diff = off - st_value;
980 best = es;
981 if (diff == 0)
982 break;
983 } else if (off - st_value == diff) {
984 best = es;
985 }
984263bc 986 }
1c0e3286 987 }
4090d6ff 988 if (best == NULL)
1c0e3286
SS
989 *diffp = off;
990 else
991 *diffp = diff;
992 *sym = (c_linker_sym_t) best;
984263bc 993
1c0e3286 994 return 0;
984263bc 995}
dc62b251
MD
996
997/*
998 * Look up a linker set on an ELF system.
999 */
1000static int
1001link_elf_lookup_set(linker_file_t lf, const char *name,
1c0e3286 1002 void ***startp, void ***stopp, int *countp)
dc62b251 1003{
1c0e3286
SS
1004 c_linker_sym_t sym;
1005 linker_symval_t symval;
1006 char *setsym;
1007 void **start, **stop;
1008 int len, error = 0, count;
1009
1010 len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1011 setsym = kmalloc(len, M_LINKER, M_WAITOK);
1012
1013 /* get address of first entry */
1014 ksnprintf(setsym, len, "%s%s", "__start_set_", name);
1015 error = link_elf_lookup_symbol(lf, setsym, &sym);
1016 if (error)
1017 goto out;
1018 link_elf_symbol_values(lf, sym, &symval);
1019 if (symval.value == 0) {
1020 error = ESRCH;
1021 goto out;
1022 }
1023 start = (void **)symval.value;
1024
1025 /* get address of last entry */
1026 ksnprintf(setsym, len, "%s%s", "__stop_set_", name);
1027 error = link_elf_lookup_symbol(lf, setsym, &sym);
1028 if (error)
1029 goto out;
1030 link_elf_symbol_values(lf, sym, &symval);
1031 if (symval.value == 0) {
1032 error = ESRCH;
1033 goto out;
1034 }
1035 stop = (void **)symval.value;
dc62b251 1036
1c0e3286
SS
1037 /* and the number of entries */
1038 count = stop - start;
dc62b251 1039
1c0e3286
SS
1040 /* and copy out */
1041 if (startp)
1042 *startp = start;
1043 if (stopp)
1044 *stopp = stop;
1045 if (countp)
1046 *countp = count;
dc62b251 1047
1c0e3286
SS
1048out:
1049 kfree(setsym, M_LINKER);
1050 return error;
1051}
1052
1053/*
1054 * Symbol lookup function that can be used when the symbol index is known (ie
1055 * in relocations). It uses the symbol index instead of doing a fully fledged
1056 * hash table based lookup when such is valid. For example for local symbols.
1057 * This is not only more efficient, it's also more correct. It's not always
1058 * the case that the symbol can be found through the hash table.
1059 */
1060static int
1061elf_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *result)
1062{
1063 elf_file_t ef = lf->priv;
1064 const Elf_Sym *sym;
1065 const char *symbol;
1066
1067 /* Don't even try to lookup the symbol if the index is bogus. */
1068 if (symidx >= ef->nchains)
1069 return (ENOENT);
1070
1071 sym = ef->symtab + symidx;
1072
1073 /*
1074 * Don't do a full lookup when the symbol is local. It may even
1075 * fail because it may not be found through the hash table.
1076 */
1077 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1078 /* Force lookup failure when we have an insanity. */
1079 if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0)
1080 return (ENOENT);
1081 return ((Elf_Addr) ef->address + sym->st_value);
1082 }
1083 /*
1084 * XXX we can avoid doing a hash table based lookup for global
1085 * symbols as well. This however is not always valid, so we'll
1086 * just do it the hard way for now. Performance tweaks can
1087 * always be added.
1088 */
1089
1090 symbol = ef->strtab + sym->st_name;
1091
1092 /* Force a lookup failure if the symbol name is bogus. */
1093 if (*symbol == 0)
1094 return (ENOENT);
1095
1096 return (linker_file_lookup_symbol(lf, symbol, deps, (caddr_t *)result));
dc62b251 1097}
1c0e3286
SS
1098static void
1099link_elf_reloc_local(linker_file_t lf)
1100{
1101 elf_file_t ef = lf->priv;
1102 const Elf_Rel *rellim;
1103 const Elf_Rel *rel;
1104 const Elf_Rela *relalim;
1105 const Elf_Rela *rela;
1106
1107 /* Perform relocations without addend if there are any: */
1108 if ((rel = ef->rel) != NULL) {
1109 rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
1110 while (rel < rellim) {
1111 elf_reloc_local(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
1112 elf_lookup);
1113 rel++;
1114 }
1115 }
dc62b251 1116
1c0e3286
SS
1117 /* Perform relocations with addend if there are any: */
1118 if ((rela = ef->rela) != NULL) {
1119 relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
1120 while (rela < relalim) {
1121 elf_reloc_local(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
1122 elf_lookup);
1123 rela++;
1124 }
1125 }
1126}