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