Reduce warnings as part of WARNS=6 cleaning, stage 1/6:
[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 $
761c7789 27 * $DragonFly: src/sys/kern/link_elf.c,v 1.16 2005/03/03 19:50:33 hsu 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
66static struct linker_class_ops link_elf_class_ops = {
67 link_elf_load_module,
68};
69
70static struct linker_file_ops link_elf_file_ops = {
71 link_elf_lookup_symbol,
72 link_elf_symbol_values,
73 link_elf_search_symbol,
74 link_elf_unload_file,
dc62b251 75 link_elf_lookup_set
984263bc
MD
76};
77
78static struct linker_file_ops link_elf_module_ops = {
79 link_elf_lookup_symbol,
80 link_elf_symbol_values,
81 link_elf_search_symbol,
82 link_elf_unload_module,
dc62b251 83 link_elf_lookup_set
984263bc 84};
761c7789 85
984263bc
MD
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) {
761c7789 144 ef = malloc(sizeof(struct elf_file), M_LINKER, M_NOWAIT | M_ZERO);
984263bc
MD
145 if (ef == NULL)
146 panic("link_elf_init: Can't create linker structures for kernel");
984263bc
MD
147
148 ef->address = 0;
149#ifdef SPARSE_MAPPING
150 ef->object = 0;
151#endif
152 ef->dynamic = dp;
153 modname = NULL;
154 modptr = preload_search_by_type("elf kernel");
155 if (modptr)
156 modname = (char *)preload_search_info(modptr, MODINFO_NAME);
157 if (modname == NULL)
158 modname = "kernel";
159 linker_kernel_file = linker_make_file(modname, ef, &link_elf_file_ops);
160 if (linker_kernel_file == NULL)
161 panic("link_elf_init: Can't create linker structures for kernel");
162 parse_dynamic(linker_kernel_file);
163 linker_kernel_file->address = (caddr_t) KERNBASE;
164 linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
165
166 if (modptr) {
167 ef->modptr = modptr;
168 baseptr = preload_search_info(modptr, MODINFO_ADDR);
169 if (baseptr)
170 linker_kernel_file->address = *(caddr_t *)baseptr;
171 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
172 if (sizeptr)
173 linker_kernel_file->size = *(size_t *)sizeptr;
174 }
761c7789 175 parse_module_symbols(linker_kernel_file);
984263bc
MD
176 linker_current_file = linker_kernel_file;
177 linker_kernel_file->flags |= LINKER_FILE_LINKED;
178 }
984263bc
MD
179}
180
181SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
182
183static int
184parse_module_symbols(linker_file_t lf)
185{
186 elf_file_t ef = lf->priv;
187 caddr_t pointer;
188 caddr_t ssym, esym, base;
189 caddr_t strtab;
190 int strcnt;
191 Elf_Sym* symtab;
192 int symcnt;
193
194 if (ef->modptr == NULL)
195 return 0;
196 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM);
197 if (pointer == NULL)
198 return 0;
199 ssym = *(caddr_t *)pointer;
200 pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM);
201 if (pointer == NULL)
202 return 0;
203 esym = *(caddr_t *)pointer;
204
205 base = ssym;
206
207 symcnt = *(long *)base;
208 base += sizeof(long);
209 symtab = (Elf_Sym *)base;
210 base += roundup(symcnt, sizeof(long));
211
212 if (base > esym || base < ssym) {
213 printf("Symbols are corrupt!\n");
214 return EINVAL;
215 }
216
217 strcnt = *(long *)base;
218 base += sizeof(long);
219 strtab = base;
220 base += roundup(strcnt, sizeof(long));
221
222 if (base > esym || base < ssym) {
223 printf("Symbols are corrupt!\n");
224 return EINVAL;
225 }
226
227 ef->ddbsymtab = symtab;
228 ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
229 ef->ddbstrtab = strtab;
230 ef->ddbstrcnt = strcnt;
231
232 return 0;
233}
234
235static int
236parse_dynamic(linker_file_t lf)
237{
238 elf_file_t ef = lf->priv;
239 const Elf_Dyn *dp;
240 int plttype = DT_REL;
241
242 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
243 switch (dp->d_tag) {
244 case DT_HASH:
245 {
246 /* From src/libexec/rtld-elf/rtld.c */
247 const Elf_Off *hashtab = (const Elf_Off *)
248 (ef->address + dp->d_un.d_ptr);
249 ef->nbuckets = hashtab[0];
250 ef->nchains = hashtab[1];
251 ef->buckets = hashtab + 2;
252 ef->chains = ef->buckets + ef->nbuckets;
253 break;
254 }
255 case DT_STRTAB:
256 ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
257 break;
258 case DT_STRSZ:
259 ef->strsz = dp->d_un.d_val;
260 break;
261 case DT_SYMTAB:
262 ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
263 break;
264 case DT_SYMENT:
265 if (dp->d_un.d_val != sizeof(Elf_Sym))
266 return ENOEXEC;
267 break;
268 case DT_PLTGOT:
269 ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
270 break;
271 case DT_REL:
272 ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
273 break;
274 case DT_RELSZ:
275 ef->relsize = dp->d_un.d_val;
276 break;
277 case DT_RELENT:
278 if (dp->d_un.d_val != sizeof(Elf_Rel))
279 return ENOEXEC;
280 break;
281 case DT_JMPREL:
282 ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
283 break;
284 case DT_PLTRELSZ:
285 ef->pltrelsize = dp->d_un.d_val;
286 break;
287 case DT_RELA:
288 ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
289 break;
290 case DT_RELASZ:
291 ef->relasize = dp->d_un.d_val;
292 break;
293 case DT_RELAENT:
294 if (dp->d_un.d_val != sizeof(Elf_Rela))
295 return ENOEXEC;
296 break;
297 case DT_PLTREL:
298 plttype = dp->d_un.d_val;
299 if (plttype != DT_REL && plttype != DT_RELA)
300 return ENOEXEC;
301 break;
302 }
303 }
304
305 if (plttype == DT_RELA) {
306 ef->pltrela = (const Elf_Rela *) ef->pltrel;
307 ef->pltrel = NULL;
308 ef->pltrelasize = ef->pltrelsize;
309 ef->pltrelsize = 0;
310 }
311
312 ef->ddbsymtab = ef->symtab;
313 ef->ddbsymcnt = ef->nchains;
314 ef->ddbstrtab = ef->strtab;
315 ef->ddbstrcnt = ef->strsz;
316
317 return 0;
318}
319
320static void
321link_elf_error(const char *s)
322{
323 printf("kldload: %s\n", s);
324}
325
326static int
327link_elf_load_module(const char *filename, linker_file_t *result)
328{
329 caddr_t modptr, baseptr, sizeptr, dynptr;
330 char *type;
331 elf_file_t ef;
332 linker_file_t lf;
333 int error;
334 vm_offset_t dp;
335
32832096
MD
336 /*
337 * Look to see if we have the module preloaded.
338 */
984263bc
MD
339 modptr = preload_search_by_name(filename);
340 if (modptr == NULL)
341 return (link_elf_load_file(filename, result));
342
343 /* It's preloaded, check we can handle it and collect information */
344 type = (char *)preload_search_info(modptr, MODINFO_TYPE);
345 baseptr = preload_search_info(modptr, MODINFO_ADDR);
346 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
347 dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC);
348 if (type == NULL || strcmp(type, "elf module") != 0)
349 return (EFTYPE);
350 if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
351 return (EINVAL);
352
761c7789 353 ef = malloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
354 if (ef == NULL)
355 return (ENOMEM);
984263bc
MD
356 ef->modptr = modptr;
357 ef->address = *(caddr_t *)baseptr;
358#ifdef SPARSE_MAPPING
359 ef->object = 0;
360#endif
361 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
362 ef->dynamic = (Elf_Dyn *)dp;
363 lf = linker_make_file(filename, ef, &link_elf_module_ops);
364 if (lf == NULL) {
365 free(ef, M_LINKER);
366 return ENOMEM;
367 }
368 lf->address = ef->address;
369 lf->size = *(size_t *)sizeptr;
370
371 error = parse_dynamic(lf);
372 if (error) {
373 linker_file_unload(lf);
374 return error;
375 }
376 error = load_dependancies(lf);
377 if (error) {
378 linker_file_unload(lf);
379 return error;
380 }
381 error = relocate_file(lf);
382 if (error) {
383 linker_file_unload(lf);
384 return error;
385 }
761c7789 386 parse_module_symbols(lf);
984263bc
MD
387 lf->flags |= LINKER_FILE_LINKED;
388 *result = lf;
389 return (0);
390}
391
392static int
393link_elf_load_file(const char* filename, linker_file_t* result)
394{
fad57d0e 395 struct nlookupdata nd;
dadab5e9
MD
396 struct thread *td = curthread; /* XXX */
397 struct proc *p = td->td_proc;
fad57d0e 398 struct vnode *vp;
984263bc
MD
399 Elf_Ehdr *hdr;
400 caddr_t firstpage;
401 int nbytes, i;
402 Elf_Phdr *phdr;
403 Elf_Phdr *phlimit;
404 Elf_Phdr *segs[2];
405 int nsegs;
406 Elf_Phdr *phdyn;
407 Elf_Phdr *phphdr;
408 caddr_t mapbase;
409 size_t mapsize;
410 Elf_Off base_offset;
411 Elf_Addr base_vaddr;
412 Elf_Addr base_vlimit;
413 int error = 0;
414 int resid;
415 elf_file_t ef;
416 linker_file_t lf;
417 char *pathname;
418 Elf_Shdr *shdr;
419 int symtabindex;
420 int symstrindex;
421 int symcnt;
422 int strcnt;
423
dadab5e9 424 KKASSERT(p != NULL);
a3d82f10
MD
425 if (p->p_ucred == NULL) {
426 printf("link_elf_load_file: cannot load '%s' from filesystem"
427 " this early\n", filename);
428 return ENOENT;
429 }
984263bc
MD
430 shdr = NULL;
431 lf = NULL;
984263bc
MD
432 pathname = linker_search_path(filename);
433 if (pathname == NULL)
434 return ENOENT;
435
fad57d0e
MD
436 error = nlookup_init(&nd, pathname, UIO_SYSSPACE, NLC_FOLLOW|NLC_LOCKVP);
437 if (error == 0)
438 error = vn_open(&nd, NULL, FREAD, 0);
984263bc 439 free(pathname, M_LINKER);
fad57d0e
MD
440 if (error) {
441 nlookup_done(&nd);
984263bc 442 return error;
fad57d0e
MD
443 }
444 vp = nd.nl_open_vp;
445 nd.nl_open_vp = NULL;
446 nlookup_done(&nd);
984263bc
MD
447
448 /*
449 * Read the elf header from the file.
450 */
451 firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
452 if (firstpage == NULL) {
453 error = ENOMEM;
454 goto out;
455 }
456 hdr = (Elf_Ehdr *)firstpage;
fad57d0e 457 error = vn_rdwr(UIO_READ, vp, firstpage, PAGE_SIZE, 0,
dadab5e9 458 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, td);
984263bc
MD
459 nbytes = PAGE_SIZE - resid;
460 if (error)
461 goto out;
462
463 if (!IS_ELF(*hdr)) {
464 error = ENOEXEC;
465 goto out;
466 }
467
468 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
469 || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
470 link_elf_error("Unsupported file layout");
471 error = ENOEXEC;
472 goto out;
473 }
474 if (hdr->e_ident[EI_VERSION] != EV_CURRENT
475 || hdr->e_version != EV_CURRENT) {
476 link_elf_error("Unsupported file version");
477 error = ENOEXEC;
478 goto out;
479 }
480 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
481 link_elf_error("Unsupported file type");
482 error = ENOEXEC;
483 goto out;
484 }
485 if (hdr->e_machine != ELF_TARG_MACH) {
486 link_elf_error("Unsupported machine");
487 error = ENOEXEC;
488 goto out;
489 }
490
491 /*
492 * We rely on the program header being in the first page. This is
493 * not strictly required by the ABI specification, but it seems to
494 * always true in practice. And, it simplifies things considerably.
495 */
496 if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
497 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
498 (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
499 link_elf_error("Unreadable program headers");
500
501 /*
502 * Scan the program header entries, and save key information.
503 *
504 * We rely on there being exactly two load segments, text and data,
505 * in that order.
506 */
507 phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
508 phlimit = phdr + hdr->e_phnum;
509 nsegs = 0;
510 phdyn = NULL;
511 phphdr = NULL;
512 while (phdr < phlimit) {
513 switch (phdr->p_type) {
514
515 case PT_LOAD:
516 if (nsegs == 2) {
517 link_elf_error("Too many sections");
518 error = ENOEXEC;
519 goto out;
520 }
521 segs[nsegs] = phdr;
522 ++nsegs;
523 break;
524
525 case PT_PHDR:
526 phphdr = phdr;
527 break;
528
529 case PT_DYNAMIC:
530 phdyn = phdr;
531 break;
532 }
533
534 ++phdr;
535 }
536 if (phdyn == NULL) {
537 link_elf_error("Object is not dynamically-linked");
538 error = ENOEXEC;
539 goto out;
540 }
541
542 /*
543 * Allocate the entire address space of the object, to stake out our
544 * contiguous region, and to establish the base address for relocation.
545 */
546 base_offset = trunc_page(segs[0]->p_offset);
547 base_vaddr = trunc_page(segs[0]->p_vaddr);
548 base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
549 mapsize = base_vlimit - base_vaddr;
550
761c7789 551 ef = malloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
552#ifdef SPARSE_MAPPING
553 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
554 if (ef->object == NULL) {
555 free(ef, M_LINKER);
556 error = ENOMEM;
557 goto out;
558 }
559 vm_object_reference(ef->object);
560 ef->address = (caddr_t) vm_map_min(kernel_map);
561 error = vm_map_find(kernel_map, ef->object, 0,
562 (vm_offset_t *) &ef->address,
563 mapsize, 1,
564 VM_PROT_ALL, VM_PROT_ALL, 0);
565 if (error) {
566 vm_object_deallocate(ef->object);
567 free(ef, M_LINKER);
568 goto out;
569 }
570#else
571 ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
572#endif
573 mapbase = ef->address;
574
575 /*
576 * Read the text and data sections and zero the bss.
577 */
578 for (i = 0; i < 2; i++) {
579 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
fad57d0e 580 error = vn_rdwr(UIO_READ, vp,
984263bc 581 segbase, segs[i]->p_filesz, segs[i]->p_offset,
dadab5e9 582 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, td);
984263bc
MD
583 if (error) {
584#ifdef SPARSE_MAPPING
585 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
586 (vm_offset_t) ef->address
587 + (ef->object->size << PAGE_SHIFT));
588 vm_object_deallocate(ef->object);
589#else
590 free(ef->address, M_LINKER);
591#endif
592 free(ef, M_LINKER);
593 goto out;
594 }
595 bzero(segbase + segs[i]->p_filesz,
596 segs[i]->p_memsz - segs[i]->p_filesz);
597
598#ifdef SPARSE_MAPPING
599 /*
600 * Wire down the pages
601 */
cde87949 602 vm_map_wire(kernel_map,
984263bc
MD
603 (vm_offset_t) segbase,
604 (vm_offset_t) segbase + segs[i]->p_memsz,
8ee99890 605 0);
984263bc
MD
606#endif
607 }
608
609 ef->dynamic = (const Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
610
611 lf = linker_make_file(filename, ef, &link_elf_file_ops);
612 if (lf == NULL) {
613#ifdef SPARSE_MAPPING
614 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
615 (vm_offset_t) ef->address
616 + (ef->object->size << PAGE_SHIFT));
617 vm_object_deallocate(ef->object);
618#else
619 free(ef->address, M_LINKER);
620#endif
621 free(ef, M_LINKER);
622 error = ENOMEM;
623 goto out;
624 }
625 lf->address = ef->address;
626 lf->size = mapsize;
627
628 error = parse_dynamic(lf);
629 if (error)
630 goto out;
631 error = load_dependancies(lf);
632 if (error)
633 goto out;
634 error = relocate_file(lf);
635 if (error)
636 goto out;
637
638 /* Try and load the symbol table if it's present. (you can strip it!) */
639 nbytes = hdr->e_shnum * hdr->e_shentsize;
640 if (nbytes == 0 || hdr->e_shoff == 0)
641 goto nosyms;
761c7789 642 shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
984263bc
MD
643 if (shdr == NULL) {
644 error = ENOMEM;
645 goto out;
646 }
fad57d0e 647 error = vn_rdwr(UIO_READ, vp,
984263bc 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 }
fad57d0e 672 error = vn_rdwr(UIO_READ, vp,
984263bc 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;
fad57d0e 677 error = vn_rdwr(UIO_READ, vp,
984263bc 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);
fad57d0e
MD
701 VOP_UNLOCK(vp, 0, td);
702 vn_close(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