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