Merge from vendor branch BSDTAR:
[dragonfly.git] / sys / boot / common / load_elf.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
4  * All rights reserved.
5  *
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.
14  *
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.
26  *
27  * $FreeBSD: src/sys/boot/common/load_elf.c,v 1.29 2003/08/25 23:30:41 obrien Exp $
28  * $DragonFly: src/sys/boot/common/load_elf.c,v 1.4 2004/09/09 03:47:08 joerg Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/exec.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
35 #include <string.h>
36 #include <machine/elf.h>
37 #include <stand.h>
38 #define FREEBSD_ELF
39 #include <link.h>
40
41 #include "bootstrap.h"
42
43 #define COPYOUT(s,d,l)  archsw.arch_copyout((vm_offset_t)(s), d, l)
44
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
51
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_Rela    *rela;
62     size_t      relasz;
63     char        *strtab;
64     size_t      strsz;
65     int         fd;
66     caddr_t     firstpage;
67     size_t      firstlen;
68     int         kernel;
69     u_int64_t   off;
70 } *elf_file_t;
71
72 static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef, u_int64_t loadaddr);
73 static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym);
74 #ifdef __sparc__
75 static void __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
76     void *p, void *val, size_t len);
77 #endif
78 static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef);
79 static char     *fake_modname(const char *name);
80
81 const char      *__elfN(kerneltype) = "elf kernel";
82 const char      *__elfN(moduletype) = "elf module";
83
84 /*
85  * Attempt to load the file (file) as an ELF module.  It will be stored at
86  * (dest), and a pointer to a module structure describing the loaded object
87  * will be saved in (result).
88  */
89 int
90 __elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result)
91 {
92     struct preloaded_file       *fp, *kfp;
93     struct elf_file             ef;
94     Elf_Ehdr                    *ehdr;
95     int                         err;
96     u_int                       pad;
97     ssize_t                     bytes_read;
98
99     fp = NULL;
100     bzero(&ef, sizeof(struct elf_file));
101     
102     /*
103      * Open the image, read and validate the ELF header 
104      */
105     if (filename == NULL)       /* can't handle nameless */
106         return(EFTYPE);
107     if ((ef.fd = open(filename, O_RDONLY)) == -1)
108         return(errno);
109     ef.firstpage = malloc(PAGE_SIZE);
110     if (ef.firstpage == NULL) {
111         close(ef.fd);
112         return(ENOMEM);
113     }
114     bytes_read = read(ef.fd, ef.firstpage, PAGE_SIZE);
115     ef.firstlen = (size_t)bytes_read;
116     if (bytes_read < 0 || ef.firstlen <= sizeof(Elf_Ehdr)) {
117         err = EFTYPE;           /* could be EIO, but may be small file */
118         goto oerr;
119     }
120     ehdr = ef.ehdr = (Elf_Ehdr *)ef.firstpage;
121
122     /* Is it ELF? */
123     if (!IS_ELF(*ehdr)) {
124         err = EFTYPE;
125         goto oerr;
126     }
127     if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||    /* Layout ? */
128         ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
129         ehdr->e_ident[EI_VERSION] != EV_CURRENT ||      /* Version ? */
130         ehdr->e_version != EV_CURRENT ||
131         ehdr->e_machine != ELF_TARG_MACH) {             /* Machine ? */
132         err = EFTYPE;
133         goto oerr;
134     }
135
136
137     /*
138      * Check to see what sort of module we are.
139      */
140     kfp = file_findfile(NULL, NULL);
141     if (ehdr->e_type == ET_DYN) {
142         /* Looks like a kld module */
143         if (kfp == NULL) {
144             printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n");
145             err = EPERM;
146             goto oerr;
147         }
148         if (strcmp(__elfN(kerneltype), kfp->f_type)) {
149             printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
150             err = EPERM;
151             goto oerr;
152         }
153         /* Looks OK, got ahead */
154         ef.kernel = 0;
155
156         /* Page-align the load address */
157         pad = (u_int)dest & PAGE_MASK;
158         if (pad != 0) {
159             pad = PAGE_SIZE - pad;
160             dest += pad;
161         }
162     } else if (ehdr->e_type == ET_EXEC) {
163         /* Looks like a kernel */
164         if (kfp != NULL) {
165             printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n");
166             err = EPERM;
167             goto oerr;
168         }
169         /* 
170          * Calculate destination address based on kernel entrypoint     
171          */
172         dest = ehdr->e_entry;
173         if (dest == 0) {
174             printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n");
175             err = EPERM;
176             goto oerr;
177         }
178         ef.kernel = 1;
179
180     } else {
181         err = EFTYPE;
182         goto oerr;
183     }
184
185     /* 
186      * Ok, we think we should handle this.
187      */
188     fp = file_alloc();
189     if (fp == NULL) {
190             printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n");
191             err = EPERM;
192             goto out;
193     }
194     if (ef.kernel)
195         setenv("kernelname", filename, 1);
196     fp->f_name = strdup(filename);
197     fp->f_type = strdup(ef.kernel ? __elfN(kerneltype) : __elfN(moduletype));
198
199 #ifdef ELF_VERBOSE
200     if (ef.kernel)
201         printf("%s entry at 0x%jx\n", filename, (uintmax_t)dest);
202 #else
203     printf("%s ", filename);
204 #endif
205
206     fp->f_size = __elfN(loadimage)(fp, &ef, dest);
207     if (fp->f_size == 0 || fp->f_addr == 0)
208         goto ioerr;
209
210     /* save exec header as metadata */
211     file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
212
213     /* Load OK, return module pointer */
214     *result = (struct preloaded_file *)fp;
215     err = 0;
216     goto out;
217     
218  ioerr:
219     err = EIO;
220  oerr:
221     file_discard(fp);
222  out:
223     if (ef.firstpage)
224         free(ef.firstpage);
225     close(ef.fd);
226     return(err);
227 }
228
229 /*
230  * With the file (fd) open on the image, and (ehdr) containing
231  * the Elf header, load the image at (off)
232  */
233 static int
234 __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
235 {
236     int         i;
237     u_int       j;
238     Elf_Ehdr    *ehdr;
239     Elf_Phdr    *phdr, *php;
240     Elf_Shdr    *shdr;
241     int         ret;
242     vm_offset_t firstaddr;
243     vm_offset_t lastaddr;
244     void        *buf;
245     size_t      resid, chunk;
246     ssize_t     result;
247     vm_offset_t dest;
248     Elf_Addr    ssym, esym;
249     Elf_Dyn     *dp;
250     Elf_Addr    adp;
251     int         ndp;
252     int         symstrindex;
253     int         symtabindex;
254     Elf_Size    size;
255     u_int       fpcopy;
256
257     dp = NULL;
258     shdr = NULL;
259     ret = 0;
260     firstaddr = lastaddr = 0;
261     ehdr = ef->ehdr;
262     if (ef->kernel) {
263 #ifdef __i386__
264 #if __ELF_WORD_SIZE == 64
265         off = - (off & 0xffffffffff000000ull);/* x86_64 relocates after locore */
266 #else
267         off = - (off & 0xff000000u);    /* i386 relocates after locore */
268 #endif
269 #else
270         off = 0;                /* alpha is direct mapped for kernels */
271 #endif
272     }
273     ef->off = off;
274
275     if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
276         printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: program header not within first page\n");
277         goto out;
278     }
279     phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
280
281     for (i = 0; i < ehdr->e_phnum; i++) {
282         /* We want to load PT_LOAD segments only.. */
283         if (phdr[i].p_type != PT_LOAD)
284             continue;
285
286 #ifdef ELF_VERBOSE
287         printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
288             (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
289             (long)(phdr[i].p_vaddr + off),
290             (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
291 #else
292         if ((phdr[i].p_flags & PF_W) == 0) {
293             printf("text=0x%lx ", (long)phdr[i].p_filesz);
294         } else {
295             printf("data=0x%lx", (long)phdr[i].p_filesz);
296             if (phdr[i].p_filesz < phdr[i].p_memsz)
297                 printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz));
298             printf(" ");
299         }
300 #endif
301         fpcopy = 0;
302         if (ef->firstlen > phdr[i].p_offset) {
303             fpcopy = ef->firstlen - phdr[i].p_offset;
304             archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
305                                phdr[i].p_vaddr + off, fpcopy);
306         }
307         if (phdr[i].p_filesz > fpcopy) {
308             if (lseek(ef->fd, (off_t)(phdr[i].p_offset + fpcopy),
309                       SEEK_SET) == -1) {
310                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadexec: cannot seek\n");
311                 goto out;
312             }
313             if (archsw.arch_readin(ef->fd, phdr[i].p_vaddr + off + fpcopy,
314                 phdr[i].p_filesz - fpcopy) != (ssize_t)(phdr[i].p_filesz - fpcopy)) {
315                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadexec: archsw.readin failed\n");
316                 goto out;
317             }
318         }
319         /* clear space from oversized segments; eg: bss */
320         if (phdr[i].p_filesz < phdr[i].p_memsz) {
321 #ifdef ELF_VERBOSE
322             printf(" (bss: 0x%lx-0x%lx)",
323                 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
324                 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
325 #endif
326
327             /* no archsw.arch_bzero */
328             buf = malloc(PAGE_SIZE);
329             if (buf == NULL) {
330                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: malloc() failed\n");
331                 goto out;
332             }
333             bzero(buf, PAGE_SIZE);
334             resid = phdr[i].p_memsz - phdr[i].p_filesz;
335             dest = phdr[i].p_vaddr + off + phdr[i].p_filesz;
336             while (resid > 0) {
337                 chunk = min(PAGE_SIZE, resid);
338                 archsw.arch_copyin(buf, dest, chunk);
339                 resid -= chunk;
340                 dest += chunk;
341             }
342             free(buf);
343         }
344 #ifdef ELF_VERBOSE
345         printf("\n");
346 #endif
347
348         if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
349             firstaddr = phdr[i].p_vaddr + off;
350         if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz))
351             lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
352     }
353     lastaddr = roundup(lastaddr, sizeof(long));
354
355     /*
356      * Now grab the symbol tables.  This isn't easy if we're reading a
357      * .gz file.  I think the rule is going to have to be that you must
358      * strip a file to remove symbols before gzipping it so that we do not
359      * try to lseek() on it.
360      */
361     chunk = ehdr->e_shnum * ehdr->e_shentsize;
362     if (chunk == 0 || ehdr->e_shoff == 0)
363         goto nosyms;
364     shdr = malloc(chunk);
365     if (shdr == NULL)
366         goto nosyms;
367     if (lseek(ef->fd, (off_t)ehdr->e_shoff, SEEK_SET) == -1) {
368         printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: cannot lseek() to section headers");
369         goto nosyms;
370     }
371     result = read(ef->fd, shdr, chunk);
372     if (result < 0 || (size_t)result != chunk) {
373         printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: read section headers failed");
374         goto nosyms;
375     }
376     symtabindex = -1;
377     symstrindex = -1;
378     for (i = 0; i < ehdr->e_shnum; i++) {
379         if (shdr[i].sh_type != SHT_SYMTAB)
380             continue;
381         for (j = 0; j < ehdr->e_phnum; j++) {
382             if (phdr[j].p_type != PT_LOAD)
383                 continue;
384             if (shdr[i].sh_offset >= phdr[j].p_offset &&
385                 (shdr[i].sh_offset + shdr[i].sh_size <=
386                  phdr[j].p_offset + phdr[j].p_filesz)) {
387                 shdr[i].sh_offset = 0;
388                 shdr[i].sh_size = 0;
389                 break;
390             }
391         }
392         if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
393             continue;           /* alread loaded in a PT_LOAD above */
394         /* Save it for loading below */
395         symtabindex = i;
396         symstrindex = shdr[i].sh_link;
397     }
398     if (symtabindex < 0 || symstrindex < 0)
399         goto nosyms;
400
401     /* Ok, committed to a load. */
402 #ifndef ELF_VERBOSE
403     printf("syms=[");
404 #endif
405     ssym = lastaddr;
406     for (i = symtabindex; i >= 0; i = symstrindex) {
407 #ifdef ELF_VERBOSE
408         char    *secname;
409
410         switch(shdr[i].sh_type) {
411             case SHT_SYMTAB:            /* Symbol table */
412                 secname = "symtab";
413                 break;
414             case SHT_STRTAB:            /* String table */
415                 secname = "strtab";
416                 break;
417             default:
418                 secname = "WHOA!!";
419                 break;
420         }
421 #endif
422
423         size = shdr[i].sh_size;
424         archsw.arch_copyin(&size, lastaddr, sizeof(size));
425         lastaddr += sizeof(size);
426
427 #ifdef ELF_VERBOSE
428         printf("\n%s: 0x%lx@0x%lx -> 0x%lx-0x%lx", secname,
429             shdr[i].sh_size, shdr[i].sh_offset,
430             lastaddr, lastaddr + shdr[i].sh_size);
431 #else
432         if (i == symstrindex)
433             printf("+");
434         printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
435 #endif
436
437         if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
438             printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not seek for symbols - skipped!");
439             lastaddr = ssym;
440             ssym = 0;
441             goto nosyms;
442         }
443         result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
444         if (result < 0 || (size_t)result != shdr[i].sh_size) {
445             printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not read symbols - skipped!");
446             lastaddr = ssym;
447             ssym = 0;
448             goto nosyms;
449         }
450         /* Reset offsets relative to ssym */
451         lastaddr += shdr[i].sh_size;
452         lastaddr = roundup(lastaddr, sizeof(size));
453         if (i == symtabindex)
454             symtabindex = -1;
455         else if (i == symstrindex)
456             symstrindex = -1;
457     }
458     esym = lastaddr;
459 #ifndef ELF_VERBOSE
460     printf("]");
461 #endif
462
463     file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
464     file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
465
466 nosyms:
467     printf("\n");
468
469     ret = lastaddr - firstaddr;
470     fp->f_addr = firstaddr;
471
472     php = NULL;
473     for (i = 0; i < ehdr->e_phnum; i++) {
474         if (phdr[i].p_type == PT_DYNAMIC) {
475             php = phdr + i;
476             adp = php->p_vaddr;
477             file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp);
478             break;
479         }
480     }
481
482     if (php == NULL)    /* this is bad, we cannot get to symbols or _DYNAMIC */
483         goto out;
484
485     ndp = php->p_filesz / sizeof(Elf_Dyn);
486     if (ndp == 0)
487         goto out;
488     dp = malloc(php->p_filesz);
489     if (dp == NULL)
490         goto out;
491     archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
492
493     ef->strsz = 0;
494     for (i = 0; i < ndp; i++) {
495         if (dp[i].d_tag == NULL)
496             break;
497         switch (dp[i].d_tag) {
498         case DT_HASH:
499             ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
500             break;
501         case DT_STRTAB:
502             ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
503             break;
504         case DT_STRSZ:
505             ef->strsz = dp[i].d_un.d_val;
506             break;
507         case DT_SYMTAB:
508             ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off);
509             break;
510         case DT_RELA:
511             ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
512             break;
513         case DT_RELASZ:
514             ef->relasz = dp[i].d_un.d_val;
515             break;
516         default:
517             break;
518         }
519     }
520     if (ef->hashtab == NULL || ef->symtab == NULL ||
521         ef->strtab == NULL || ef->strsz == 0)
522         goto out;
523     COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
524     COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
525     ef->buckets = ef->hashtab + 2;
526     ef->chains = ef->buckets + ef->nbuckets;
527     if (__elfN(parse_modmetadata)(fp, ef) == 0)
528         goto out;
529
530     if (ef->kernel)                     /* kernel must not depend on anything */
531         goto out;
532
533 out:
534     if (dp)
535         free(dp);
536     if (shdr)
537         free(shdr);
538     return ret;
539 }
540
541 static char invalid_name[] = "bad";
542
543 char *
544 fake_modname(const char *name)
545 {
546     const char *sp, *ep;
547     char *fp;
548     size_t len;
549
550     sp = strrchr(name, '/');
551     if (sp)
552         sp++;
553     else
554         sp = name;
555     ep = strrchr(name, '.');
556     if (ep) {
557             if (ep == name) {
558                 sp = invalid_name;
559                 ep = invalid_name + sizeof(invalid_name) - 1;
560             } 
561     } else
562         ep = name + strlen(name);
563     len = ep - sp;
564     fp = malloc(len + 1);
565     if (fp == NULL)
566         return NULL;
567     memcpy(fp, sp, len);
568     fp[len] = '\0';
569     return fp;
570 }
571
572 #if defined(__i386__) && __ELF_WORD_SIZE == 64
573 struct mod_metadata64 {
574         int             md_version;     /* structure version MDTV_* */  
575         int             md_type;        /* type of entry MDT_* */
576         u_int64_t       md_data;        /* specific data */
577         u_int64_t       md_cval;        /* common string label */
578 };
579 #endif
580
581 int
582 __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef)
583 {
584     struct mod_metadata md;
585 #if defined(__i386__) && __ELF_WORD_SIZE == 64
586     struct mod_metadata64 md64;
587 #endif
588     struct mod_depend *mdepend;
589     struct mod_version mver;
590     Elf_Sym sym;
591     char *s;
592     int modcnt, minfolen;
593     Elf_Addr v, p, p_stop;
594
595     if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
596         return ENOENT;
597     p = sym.st_value + ef->off;
598     if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
599         return ENOENT;
600     p_stop = sym.st_value + ef->off;
601
602     modcnt = 0;
603     while (p < p_stop) {
604         COPYOUT(p, &v, sizeof(v));
605 #ifdef __sparc64__
606         __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
607 #else
608         v += ef->off;
609 #endif
610 #if defined(__i386__) && __ELF_WORD_SIZE == 64
611         COPYOUT(v, &md64, sizeof(md64));
612         md.md_version = md64.md_version;
613         md.md_type = md64.md_type;
614         md.md_cval = (const char *)(uintptr_t)(md64.md_cval + ef->off);
615         md.md_data = (void *)(uintptr_t)(md64.md_data + ef->off);
616 #else
617         COPYOUT(v, &md, sizeof(md));
618 #ifdef __sparc64__
619         __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
620 #else
621         md.md_cval += ef->off;
622         md.md_data = (uint8_t *)md.md_data + ef->off;
623 #endif
624 #endif
625         p += sizeof(Elf_Addr);
626         switch(md.md_type) {
627           case MDT_DEPEND:
628             if (ef->kernel)             /* kernel must not depend on anything */
629               break;
630             s = strdupout((vm_offset_t)md.md_cval);
631             minfolen = sizeof(*mdepend) + strlen(s) + 1;
632             mdepend = malloc(minfolen);
633             if (mdepend == NULL)
634                 return ENOMEM;
635             COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend));
636             strcpy((char*)(mdepend + 1), s);
637             free(s);
638             file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
639             free(mdepend);
640             break;
641           case MDT_VERSION:
642             s = strdupout((vm_offset_t)md.md_cval);
643             COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
644             file_addmodule(fp, s, mver.mv_version, NULL);
645             free(s);
646             modcnt++;
647             break;
648         }
649     }
650     if (modcnt == 0) {
651         s = fake_modname(fp->f_name);
652         file_addmodule(fp, s, 1, NULL);
653         free(s);
654     }
655     return 0;
656 }
657
658 static unsigned long
659 elf_hash(const char *name)
660 {
661     const unsigned char *p = (const unsigned char *) name;
662     unsigned long h = 0;
663     unsigned long g;
664
665     while (*p != '\0') {
666         h = (h << 4) + *p++;
667         if ((g = h & 0xf0000000) != 0)
668             h ^= g >> 24;
669         h &= ~g;
670     }
671     return h;
672 }
673
674 static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n";
675 int
676 __elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef, const char* name,
677                   Elf_Sym *symp)
678 {
679     Elf_Hashelt symnum;
680     Elf_Sym sym;
681     char *strp;
682     unsigned long hash;
683
684     hash = elf_hash(name);
685     COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
686
687     while (symnum != STN_UNDEF) {
688         if (symnum >= ef->nchains) {
689             printf(__elfN(bad_symtable));
690             return ENOENT;
691         }
692
693         COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
694         if (sym.st_name == 0) {
695             printf(__elfN(bad_symtable));
696             return ENOENT;
697         }
698
699         strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
700         if (strcmp(name, strp) == 0) {
701             free(strp);
702             if (sym.st_shndx != SHN_UNDEF ||
703                 (sym.st_value != 0 &&
704                  ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
705                 *symp = sym;
706                 return 0;
707             }
708             return ENOENT;
709         }
710         free(strp);
711         COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
712     }
713     return ENOENT;
714 }
715
716 #ifdef __sparc__
717 /*
718  * Apply any intra-module relocations to the value. *p is the load address
719  * of the value and val/len is the value to be modified. This does NOT modify
720  * the image in-place, because this is done by kern_linker later on.
721  */
722 static void
723 __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
724     void *p, void *val, size_t len)
725 {
726         Elf_Addr off = (Elf_Addr)p - ef->off, word;
727         size_t n;
728         Elf_Rela r;
729
730         for (n = 0; n < ef->relasz / sizeof(r); n++) {
731                 COPYOUT(ef->rela + n, &r, sizeof(r));
732
733                 if (r.r_offset >= off && r.r_offset < off + len &&
734                     ELF_R_TYPE(r.r_info) == R_SPARC_RELATIVE) {
735                         word = ef->off + r.r_addend;
736                         bcopy(&word, (char *)val + (r.r_offset - off),
737                             sizeof(word));
738                 }
739         }
740 }
741 #endif