loader: update elf module code from FreeBSD
[dragonfly.git] / sys / boot / common / load_elf.c
blobaa6307a2bd3dce9d4ffd914824206077dad6dc80
1 /*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
27 * $FreeBSD: src/sys/boot/common/load_elf.c,v 1.39 2008/10/14 10:11:14 raj Exp $
30 #include <sys/param.h>
31 #include <sys/exec.h>
32 #include <sys/linker.h>
33 #include <sys/module.h>
34 #include <sys/stdint.h>
35 #include <string.h>
36 #include <machine/elf.h>
37 #include <stand.h>
38 #define FREEBSD_ELF
39 #include <link.h>
41 #include "bootstrap.h"
43 #define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
45 #if defined(__i386__) && __ELF_WORD_SIZE == 64
46 #undef ELF_TARG_CLASS
47 #undef ELF_TARG_MACH
48 #define ELF_TARG_CLASS ELFCLASS64
49 #define ELF_TARG_MACH EM_X86_64
50 #endif
52 typedef struct elf_file {
53 Elf_Phdr *ph;
54 Elf_Ehdr *ehdr;
55 Elf_Sym *symtab;
56 Elf_Hashelt *hashtab;
57 Elf_Hashelt nbuckets;
58 Elf_Hashelt nchains;
59 Elf_Hashelt *buckets;
60 Elf_Hashelt *chains;
61 Elf_Rel *rel;
62 size_t relsz;
63 Elf_Rela *rela;
64 size_t relasz;
65 char *strtab;
66 size_t strsz;
67 int fd;
68 caddr_t firstpage;
69 size_t firstlen;
70 int kernel;
71 u_int64_t off;
72 } *elf_file_t;
74 static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef, u_int64_t loadaddr);
75 static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym);
76 static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
77 Elf_Addr p, void *val, size_t len);
78 static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef);
79 static symaddr_fn __elfN(symaddr);
80 static char *fake_modname(const char *name);
82 const char *__elfN(kerneltype) = "elf kernel";
83 const char *__elfN(moduletype) = "elf module";
85 u_int64_t __elfN(relocation_offset) = 0;
88 * Attempt to load the file (file) as an ELF module. It will be stored at
89 * (dest), and a pointer to a module structure describing the loaded object
90 * will be saved in (result).
92 int
93 __elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result)
95 struct preloaded_file *fp, *kfp;
96 struct elf_file ef;
97 Elf_Ehdr *ehdr;
98 int err;
99 u_int pad;
100 ssize_t bytes_read;
102 fp = NULL;
103 bzero(&ef, sizeof(struct elf_file));
106 * Open the image, read and validate the ELF header
108 if (filename == NULL) /* can't handle nameless */
109 return(EFTYPE);
110 if ((ef.fd = rel_open(filename, O_RDONLY)) == -1)
111 return(errno);
112 ef.firstpage = malloc(PAGE_SIZE);
113 if (ef.firstpage == NULL) {
114 close(ef.fd);
115 return(ENOMEM);
117 bytes_read = read(ef.fd, ef.firstpage, PAGE_SIZE);
118 ef.firstlen = (size_t)bytes_read;
119 if (bytes_read < 0 || ef.firstlen <= sizeof(Elf_Ehdr)) {
120 err = EFTYPE; /* could be EIO, but may be small file */
121 goto oerr;
123 ehdr = ef.ehdr = (Elf_Ehdr *)ef.firstpage;
125 /* Is it ELF? */
126 if (!IS_ELF(*ehdr)) {
127 err = EFTYPE;
128 goto oerr;
130 if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
131 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
132 ehdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */
133 ehdr->e_version != EV_CURRENT ||
134 ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */
135 err = EFTYPE;
136 goto oerr;
141 * Check to see what sort of module we are.
143 kfp = file_findfile(NULL, NULL);
144 if (ehdr->e_type == ET_DYN) {
145 /* Looks like a kld module */
146 if (kfp == NULL) {
147 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n");
148 err = EPERM;
149 goto oerr;
151 if (strcmp(__elfN(kerneltype), kfp->f_type)) {
152 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
153 err = EPERM;
154 goto oerr;
156 /* Looks OK, got ahead */
157 ef.kernel = 0;
159 /* Page-align the load address */
160 pad = (u_int)dest & PAGE_MASK;
161 if (pad != 0) {
162 pad = PAGE_SIZE - pad;
163 dest += pad;
165 } else if (ehdr->e_type == ET_EXEC) {
166 /* Looks like a kernel */
167 if (kfp != NULL) {
168 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n");
169 err = EPERM;
170 goto oerr;
173 * Calculate destination address based on kernel entrypoint
175 dest = ehdr->e_entry;
176 if (dest == 0) {
177 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n");
178 err = EPERM;
179 goto oerr;
181 ef.kernel = 1;
183 } else {
184 err = EFTYPE;
185 goto oerr;
189 * Ok, we think we should handle this.
191 fp = file_alloc();
192 if (fp == NULL) {
193 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n");
194 err = EPERM;
195 goto out;
197 if (ef.kernel)
198 setenv("kernelname", filename, 1);
199 fp->f_name = strdup(filename);
200 fp->f_type = strdup(ef.kernel ? __elfN(kerneltype) : __elfN(moduletype));
202 #ifdef ELF_VERBOSE
203 if (ef.kernel)
204 printf("%s entry at 0x%jx\n", filename, (uintmax_t)dest);
205 #else
206 printf("%s ", filename);
207 #endif
209 fp->f_size = __elfN(loadimage)(fp, &ef, dest);
210 if (fp->f_size == 0 || fp->f_addr == 0)
211 goto ioerr;
213 /* save exec header as metadata */
214 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
216 /* Load OK, return module pointer */
217 *result = (struct preloaded_file *)fp;
218 err = 0;
219 goto out;
221 ioerr:
222 err = EIO;
223 oerr:
224 file_discard(fp);
225 out:
226 if (ef.firstpage)
227 free(ef.firstpage);
228 close(ef.fd);
229 return(err);
233 * With the file (fd) open on the image, and (ehdr) containing
234 * the Elf header, load the image at (off)
236 static int
237 __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
239 int i;
240 u_int j;
241 Elf_Ehdr *ehdr;
242 Elf_Phdr *phdr, *php;
243 Elf_Shdr *shdr;
244 int ret;
245 vm_offset_t firstaddr;
246 vm_offset_t lastaddr;
247 size_t chunk;
248 ssize_t result;
249 Elf_Addr ssym, esym;
250 Elf_Dyn *dp;
251 Elf_Addr adp;
252 int ndp;
253 int symstrindex;
254 int symtabindex;
255 Elf_Size size;
256 u_int fpcopy;
258 dp = NULL;
259 shdr = NULL;
260 ret = 0;
261 firstaddr = lastaddr = 0;
262 ehdr = ef->ehdr;
263 if (ef->kernel) {
264 #ifdef __i386__
265 #if __ELF_WORD_SIZE == 64
266 off = - (off & 0xffffffffff000000ull);/* x86_64 relocates after locore */
267 #else
268 off = - (off & 0xff000000u); /* i386 relocates after locore */
269 #endif
270 #elif defined(__powerpc__)
272 * On the purely virtual memory machines like e500, the kernel is
273 * linked against its final VA range, which is most often not
274 * available at the loader stage, but only after kernel initializes
275 * and completes its VM settings. In such cases we cannot use p_vaddr
276 * field directly to load ELF segments, but put them at some
277 * 'load-time' locations.
279 if (off & 0xf0000000u) {
280 off = -(off & 0xf0000000u);
282 * XXX the physical load address should not be hardcoded. Note
283 * that the Book-E kernel assumes that it's loaded at a 16MB
284 * boundary for now...
286 off += 0x01000000;
287 ehdr->e_entry += off;
288 #ifdef ELF_VERBOSE
289 printf("Converted entry 0x%08x\n", ehdr->e_entry);
290 #endif
291 } else
292 off = 0;
293 #elif defined(__arm__)
294 if (off & 0xf0000000u) {
295 off = -(off & 0xf0000000u);
296 ehdr->e_entry += off;
297 #ifdef ELF_VERBOSE
298 printf("Converted entry 0x%08x\n", ehdr->e_entry);
299 #endif
300 } else
301 off = 0;
302 #else
303 off = 0; /* other archs use direct mapped kernels */
304 #endif
305 __elfN(relocation_offset) = off;
307 ef->off = off;
309 if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
310 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: program header not within first page\n");
311 goto out;
313 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
315 for (i = 0; i < ehdr->e_phnum; i++) {
316 /* We want to load PT_LOAD segments only.. */
317 if (phdr[i].p_type != PT_LOAD)
318 continue;
320 #ifdef ELF_VERBOSE
321 printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
322 (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
323 (long)(phdr[i].p_vaddr + off),
324 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
325 #else
326 if ((phdr[i].p_flags & PF_W) == 0) {
327 printf("text=0x%lx ", (long)phdr[i].p_filesz);
328 } else {
329 printf("data=0x%lx", (long)phdr[i].p_filesz);
330 if (phdr[i].p_filesz < phdr[i].p_memsz)
331 printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz));
332 printf(" ");
334 #endif
335 fpcopy = 0;
336 if (ef->firstlen > phdr[i].p_offset) {
337 fpcopy = ef->firstlen - phdr[i].p_offset;
338 archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
339 phdr[i].p_vaddr + off, fpcopy);
341 if (phdr[i].p_filesz > fpcopy) {
342 if (kern_pread(ef->fd, phdr[i].p_vaddr + off + fpcopy,
343 phdr[i].p_filesz - fpcopy, phdr[i].p_offset + fpcopy) != 0) {
344 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
345 "_loadimage: read failed\n");
346 goto out;
349 /* clear space from oversized segments; eg: bss */
350 if (phdr[i].p_filesz < phdr[i].p_memsz) {
351 #ifdef ELF_VERBOSE
352 printf(" (bss: 0x%lx-0x%lx)",
353 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
354 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
355 #endif
357 kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
358 phdr[i].p_memsz - phdr[i].p_filesz);
360 #ifdef ELF_VERBOSE
361 printf("\n");
362 #endif
364 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
365 firstaddr = phdr[i].p_vaddr + off;
366 if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz))
367 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
369 lastaddr = roundup(lastaddr, sizeof(long));
372 * Now grab the symbol tables. This isn't easy if we're reading a
373 * .gz file. I think the rule is going to have to be that you must
374 * strip a file to remove symbols before gzipping it so that we do not
375 * try to lseek() on it.
377 chunk = ehdr->e_shnum * ehdr->e_shentsize;
378 if (chunk == 0 || ehdr->e_shoff == 0)
379 goto nosyms;
380 shdr = alloc_pread(ef->fd, ehdr->e_shoff, chunk);
381 if (shdr == NULL) {
382 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
383 "_loadimage: failed to read section headers");
384 goto nosyms;
386 symtabindex = -1;
387 symstrindex = -1;
388 for (i = 0; i < ehdr->e_shnum; i++) {
389 if (shdr[i].sh_type != SHT_SYMTAB)
390 continue;
391 for (j = 0; j < ehdr->e_phnum; j++) {
392 if (phdr[j].p_type != PT_LOAD)
393 continue;
394 if (shdr[i].sh_offset >= phdr[j].p_offset &&
395 (shdr[i].sh_offset + shdr[i].sh_size <=
396 phdr[j].p_offset + phdr[j].p_filesz)) {
397 shdr[i].sh_offset = 0;
398 shdr[i].sh_size = 0;
399 break;
402 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
403 continue; /* alread loaded in a PT_LOAD above */
404 /* Save it for loading below */
405 symtabindex = i;
406 symstrindex = shdr[i].sh_link;
408 if (symtabindex < 0 || symstrindex < 0)
409 goto nosyms;
411 /* Ok, committed to a load. */
412 #ifndef ELF_VERBOSE
413 printf("syms=[");
414 #endif
415 ssym = lastaddr;
416 for (i = symtabindex; i >= 0; i = symstrindex) {
417 #ifdef ELF_VERBOSE
418 char *secname;
420 switch(shdr[i].sh_type) {
421 case SHT_SYMTAB: /* Symbol table */
422 secname = "symtab";
423 break;
424 case SHT_STRTAB: /* String table */
425 secname = "strtab";
426 break;
427 default:
428 secname = "WHOA!!";
429 break;
431 #endif
433 size = shdr[i].sh_size;
434 archsw.arch_copyin(&size, lastaddr, sizeof(size));
435 lastaddr += sizeof(size);
437 #ifdef ELF_VERBOSE
438 printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
439 (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
440 (uintmax_t)lastaddr, (uintmax_t)(lastaddr + shdr[i].sh_size));
441 #else
442 if (i == symstrindex)
443 printf("+");
444 printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
445 #endif
447 if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
448 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not seek for symbols - skipped!");
449 lastaddr = ssym;
450 ssym = 0;
451 goto nosyms;
453 result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
454 if (result < 0 || (size_t)result != shdr[i].sh_size) {
455 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not read symbols - skipped!");
456 lastaddr = ssym;
457 ssym = 0;
458 goto nosyms;
460 /* Reset offsets relative to ssym */
461 lastaddr += shdr[i].sh_size;
462 lastaddr = roundup(lastaddr, sizeof(size));
463 if (i == symtabindex)
464 symtabindex = -1;
465 else if (i == symstrindex)
466 symstrindex = -1;
468 esym = lastaddr;
469 #ifndef ELF_VERBOSE
470 printf("]");
471 #endif
473 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
474 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
476 nosyms:
477 printf("\n");
479 ret = lastaddr - firstaddr;
480 fp->f_addr = firstaddr;
482 php = NULL;
483 for (i = 0; i < ehdr->e_phnum; i++) {
484 if (phdr[i].p_type == PT_DYNAMIC) {
485 php = phdr + i;
486 adp = php->p_vaddr;
487 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp);
488 break;
492 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
493 goto out;
495 ndp = php->p_filesz / sizeof(Elf_Dyn);
496 if (ndp == 0)
497 goto out;
498 dp = malloc(php->p_filesz);
499 if (dp == NULL)
500 goto out;
501 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
503 ef->strsz = 0;
504 for (i = 0; i < ndp; i++) {
505 if (dp[i].d_tag == 0)
506 break;
507 switch (dp[i].d_tag) {
508 case DT_HASH:
509 ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
510 break;
511 case DT_STRTAB:
512 ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
513 break;
514 case DT_STRSZ:
515 ef->strsz = dp[i].d_un.d_val;
516 break;
517 case DT_SYMTAB:
518 ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off);
519 break;
520 case DT_REL:
521 ef->rel = (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
522 break;
523 case DT_RELSZ:
524 ef->relsz = dp[i].d_un.d_val;
525 break;
526 case DT_RELA:
527 ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
528 break;
529 case DT_RELASZ:
530 ef->relasz = dp[i].d_un.d_val;
531 break;
532 default:
533 break;
536 if (ef->hashtab == NULL || ef->symtab == NULL ||
537 ef->strtab == NULL || ef->strsz == 0)
538 goto out;
539 COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
540 COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
541 ef->buckets = ef->hashtab + 2;
542 ef->chains = ef->buckets + ef->nbuckets;
543 if (__elfN(parse_modmetadata)(fp, ef) == 0)
544 goto out;
546 if (ef->kernel) /* kernel must not depend on anything */
547 goto out;
549 out:
550 if (dp)
551 free(dp);
552 if (shdr)
553 free(shdr);
554 return ret;
557 static char invalid_name[] = "bad";
559 char *
560 fake_modname(const char *name)
562 const char *sp, *ep;
563 char *fp;
564 size_t len;
566 sp = strrchr(name, '/');
567 if (sp)
568 sp++;
569 else
570 sp = name;
571 ep = strrchr(name, '.');
572 if (ep) {
573 if (ep == name) {
574 sp = invalid_name;
575 ep = invalid_name + sizeof(invalid_name) - 1;
577 } else
578 ep = name + strlen(name);
579 len = ep - sp;
580 fp = malloc(len + 1);
581 if (fp == NULL)
582 return NULL;
583 memcpy(fp, sp, len);
584 fp[len] = '\0';
585 return fp;
588 #if defined(__i386__) && __ELF_WORD_SIZE == 64
589 struct mod_metadata64 {
590 int md_version; /* structure version MDTV_* */
591 int md_type; /* type of entry MDT_* */
592 u_int64_t md_data; /* specific data */
593 u_int64_t md_cval; /* common string label */
595 #endif
598 __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef)
600 struct mod_metadata md;
601 #if defined(__i386__) && __ELF_WORD_SIZE == 64
602 struct mod_metadata64 md64;
603 #endif
604 struct mod_depend *mdepend;
605 struct mod_version mver;
606 Elf_Sym sym;
607 char *s;
608 int error, modcnt, minfolen;
609 Elf_Addr v, p, p_stop;
611 if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
612 return ENOENT;
613 p = sym.st_value + ef->off;
614 if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
615 return ENOENT;
616 p_stop = sym.st_value + ef->off;
618 modcnt = 0;
619 while (p < p_stop) {
620 COPYOUT(p, &v, sizeof(v));
621 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
622 if (error == EOPNOTSUPP)
623 v += ef->off;
624 else if (error != 0)
625 return (error);
626 #if defined(__i386__) && __ELF_WORD_SIZE == 64
627 COPYOUT(v, &md64, sizeof(md64));
628 error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
629 if (error == EOPNOTSUPP) {
630 md64.md_cval += ef->off;
631 md64.md_data += ef->off;
632 } else if (error != 0)
633 return (error);
634 md.md_version = md64.md_version;
635 md.md_type = md64.md_type;
636 md.md_cval = (const char *)(uintptr_t)md64.md_cval;
637 md.md_data = (void *)(uintptr_t)md64.md_data;
638 #else
639 COPYOUT(v, &md, sizeof(md));
640 error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
641 if (error == EOPNOTSUPP) {
642 md.md_cval += ef->off;
643 md.md_data += ef->off;
644 } else if (error != 0)
645 return (error);
646 #endif
647 p += sizeof(Elf_Addr);
648 switch(md.md_type) {
649 case MDT_DEPEND:
650 if (ef->kernel) /* kernel must not depend on anything */
651 break;
652 s = strdupout((vm_offset_t)md.md_cval);
653 minfolen = sizeof(*mdepend) + strlen(s) + 1;
654 mdepend = malloc(minfolen);
655 if (mdepend == NULL)
656 return ENOMEM;
657 COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend));
658 strcpy((char*)(mdepend + 1), s);
659 free(s);
660 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
661 free(mdepend);
662 break;
663 case MDT_VERSION:
664 s = strdupout((vm_offset_t)md.md_cval);
665 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
666 file_addmodule(fp, s, mver.mv_version, NULL);
667 free(s);
668 modcnt++;
669 break;
672 if (modcnt == 0) {
673 s = fake_modname(fp->f_name);
674 file_addmodule(fp, s, 1, NULL);
675 free(s);
677 return 0;
680 static unsigned long
681 elf_hash(const char *name)
683 const unsigned char *p = (const unsigned char *) name;
684 unsigned long h = 0;
685 unsigned long g;
687 while (*p != '\0') {
688 h = (h << 4) + *p++;
689 if ((g = h & 0xf0000000) != 0)
690 h ^= g >> 24;
691 h &= ~g;
693 return h;
696 static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n";
698 __elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef, const char* name,
699 Elf_Sym *symp)
701 Elf_Hashelt symnum;
702 Elf_Sym sym;
703 char *strp;
704 unsigned long hash;
706 hash = elf_hash(name);
707 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
709 while (symnum != STN_UNDEF) {
710 if (symnum >= ef->nchains) {
711 printf(__elfN(bad_symtable));
712 return ENOENT;
715 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
716 if (sym.st_name == 0) {
717 printf(__elfN(bad_symtable));
718 return ENOENT;
721 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
722 if (strcmp(name, strp) == 0) {
723 free(strp);
724 if (sym.st_shndx != SHN_UNDEF ||
725 (sym.st_value != 0 &&
726 ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
727 *symp = sym;
728 return 0;
730 return ENOENT;
732 free(strp);
733 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
735 return ENOENT;
739 * Apply any intra-module relocations to the value. p is the load address
740 * of the value and val/len is the value to be modified. This does NOT modify
741 * the image in-place, because this is done by kern_linker later on.
743 * Returns EOPNOTSUPP if no relocation method is supplied.
745 static int
746 __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
747 Elf_Addr p, void *val, size_t len)
749 size_t n;
750 Elf_Rela a;
751 Elf_Rel r;
752 int error;
755 * The kernel is already relocated, but we still want to apply
756 * offset adjustments.
758 if (ef->kernel)
759 return (EOPNOTSUPP);
761 for (n = 0; n < ef->relsz / sizeof(r); n++) {
762 COPYOUT(ef->rel + n, &r, sizeof(r));
764 error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
765 ef->off, p, val, len);
766 if (error != 0)
767 return (error);
769 for (n = 0; n < ef->relasz / sizeof(a); n++) {
770 COPYOUT(ef->rela + n, &a, sizeof(a));
772 error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
773 ef->off, p, val, len);
774 if (error != 0)
775 return (error);
778 return (0);
781 static Elf_Addr
782 __elfN(symaddr)(struct elf_file *ef, Elf_Size symidx)
785 /* Symbol lookup by index not required here. */
786 return (0);