]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/common/load_elf.c
MFV: r329072
[FreeBSD/FreeBSD.git] / stand / 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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/exec.h>
34 #include <sys/linker.h>
35 #include <sys/module.h>
36 #include <sys/stdint.h>
37 #include <string.h>
38 #include <machine/elf.h>
39 #include <stand.h>
40 #define FREEBSD_ELF
41 #include <sys/link_elf.h>
42
43 #include "bootstrap.h"
44
45 #define COPYOUT(s,d,l)  archsw.arch_copyout((vm_offset_t)(s), d, l)
46
47 #if defined(__i386__) && __ELF_WORD_SIZE == 64
48 #undef ELF_TARG_CLASS
49 #undef ELF_TARG_MACH
50 #define ELF_TARG_CLASS  ELFCLASS64
51 #define ELF_TARG_MACH   EM_X86_64
52 #endif
53
54 typedef struct elf_file {
55     Elf_Phdr    *ph;
56     Elf_Ehdr    *ehdr;
57     Elf_Sym     *symtab;
58     Elf_Hashelt *hashtab;
59     Elf_Hashelt nbuckets;
60     Elf_Hashelt nchains;
61     Elf_Hashelt *buckets;
62     Elf_Hashelt *chains;
63     Elf_Rel     *rel;
64     size_t      relsz;
65     Elf_Rela    *rela;
66     size_t      relasz;
67     char        *strtab;
68     size_t      strsz;
69     int         fd;
70     caddr_t     firstpage;
71     size_t      firstlen;
72     int         kernel;
73     u_int64_t   off;
74 } *elf_file_t;
75
76 static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef, u_int64_t loadaddr);
77 static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym);
78 static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
79     Elf_Addr p, void *val, size_t len);
80 static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef,
81     Elf_Addr p_start, Elf_Addr p_end);
82 static symaddr_fn __elfN(symaddr);
83 static char     *fake_modname(const char *name);
84
85 const char      *__elfN(kerneltype) = "elf kernel";
86 const char      *__elfN(moduletype) = "elf module";
87
88 u_int64_t       __elfN(relocation_offset) = 0;
89
90 extern void elf_wrong_field_size(void);
91 #define CONVERT_FIELD(b, f, e)                  \
92         switch (sizeof((b)->f)) {               \
93         case 2:                                 \
94                 (b)->f = e ## 16toh((b)->f);    \
95                 break;                          \
96         case 4:                                 \
97                 (b)->f = e ## 32toh((b)->f);    \
98                 break;                          \
99         case 8:                                 \
100                 (b)->f = e ## 64toh((b)->f);    \
101                 break;                          \
102         default:                                \
103                 /* Force a link time error. */  \
104                 elf_wrong_field_size();         \
105                 break;                          \
106         }
107
108 #define CONVERT_SWITCH(h, d, f)                 \
109         switch ((h)->e_ident[EI_DATA]) {        \
110         case ELFDATA2MSB:                       \
111                 f(d, be);                       \
112                 break;                          \
113         case ELFDATA2LSB:                       \
114                 f(d, le);                       \
115                 break;                          \
116         default:                                \
117                 return (EINVAL);                \
118         }
119
120
121 static int elf_header_convert(Elf_Ehdr *ehdr)
122 {
123         /*
124          * Fixup ELF header endianness.
125          *
126          * The Xhdr structure was loaded using block read call to optimize file
127          * accesses. It might happen, that the endianness of the system memory
128          * is different that endianness of the ELF header.  Swap fields here to
129          * guarantee that Xhdr always contain valid data regardless of
130          * architecture.
131          */
132 #define HEADER_FIELDS(b, e)                     \
133         CONVERT_FIELD(b, e_type, e);            \
134         CONVERT_FIELD(b, e_machine, e);         \
135         CONVERT_FIELD(b, e_version, e);         \
136         CONVERT_FIELD(b, e_entry, e);           \
137         CONVERT_FIELD(b, e_phoff, e);           \
138         CONVERT_FIELD(b, e_shoff, e);           \
139         CONVERT_FIELD(b, e_flags, e);           \
140         CONVERT_FIELD(b, e_ehsize, e);          \
141         CONVERT_FIELD(b, e_phentsize, e);       \
142         CONVERT_FIELD(b, e_phnum, e);           \
143         CONVERT_FIELD(b, e_shentsize, e);       \
144         CONVERT_FIELD(b, e_shnum, e);           \
145         CONVERT_FIELD(b, e_shstrndx, e)
146
147         CONVERT_SWITCH(ehdr, ehdr, HEADER_FIELDS);
148
149 #undef HEADER_FIELDS
150
151         return (0);
152 }
153
154 static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr)
155 {
156 #define PROGRAM_HEADER_FIELDS(b, e)             \
157         CONVERT_FIELD(b, p_type, e);            \
158         CONVERT_FIELD(b, p_flags, e);           \
159         CONVERT_FIELD(b, p_offset, e);          \
160         CONVERT_FIELD(b, p_vaddr, e);           \
161         CONVERT_FIELD(b, p_paddr, e);           \
162         CONVERT_FIELD(b, p_filesz, e);          \
163         CONVERT_FIELD(b, p_memsz, e);           \
164         CONVERT_FIELD(b, p_align, e)
165
166         CONVERT_SWITCH(ehdr, phdr, PROGRAM_HEADER_FIELDS);
167
168 #undef PROGRAM_HEADER_FIELDS
169
170         return (0);
171 }
172
173 static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr)
174 {
175 #define SECTION_HEADER_FIELDS(b, e)             \
176         CONVERT_FIELD(b, sh_name, e);           \
177         CONVERT_FIELD(b, sh_type, e);           \
178         CONVERT_FIELD(b, sh_link, e);           \
179         CONVERT_FIELD(b, sh_info, e);           \
180         CONVERT_FIELD(b, sh_flags, e);          \
181         CONVERT_FIELD(b, sh_addr, e);           \
182         CONVERT_FIELD(b, sh_offset, e);         \
183         CONVERT_FIELD(b, sh_size, e);           \
184         CONVERT_FIELD(b, sh_addralign, e);      \
185         CONVERT_FIELD(b, sh_entsize, e)
186
187         CONVERT_SWITCH(ehdr, shdr, SECTION_HEADER_FIELDS);
188
189 #undef SECTION_HEADER_FIELDS
190
191         return (0);
192 }
193 #undef CONVERT_SWITCH
194 #undef CONVERT_FIELD
195
196 static int
197 __elfN(load_elf_header)(char *filename, elf_file_t ef)
198 {
199         ssize_t                  bytes_read;
200         Elf_Ehdr                *ehdr;
201         int                      err;
202
203         /*
204         * Open the image, read and validate the ELF header 
205         */
206         if (filename == NULL)   /* can't handle nameless */
207                 return (EFTYPE);
208         if ((ef->fd = open(filename, O_RDONLY)) == -1)
209                 return (errno);
210         ef->firstpage = malloc(PAGE_SIZE);
211         if (ef->firstpage == NULL) {
212                 close(ef->fd);
213                 return (ENOMEM);
214         }
215         bytes_read = read(ef->fd, ef->firstpage, PAGE_SIZE);
216         ef->firstlen = (size_t)bytes_read;
217         if (bytes_read < 0 || ef->firstlen <= sizeof(Elf_Ehdr)) {
218                 err = EFTYPE; /* could be EIO, but may be small file */
219                 goto error;
220         }
221         ehdr = ef->ehdr = (Elf_Ehdr *)ef->firstpage;
222
223         /* Is it ELF? */
224         if (!IS_ELF(*ehdr)) {
225                 err = EFTYPE;
226                 goto error;
227         }
228
229         if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
230             ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
231             ehdr->e_ident[EI_VERSION] != EV_CURRENT) /* Version ? */ {
232                 err = EFTYPE;
233                 goto error;
234         }
235
236         err = elf_header_convert(ehdr);
237         if (err)
238                 goto error;
239
240         if (ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */
241                 err = EFTYPE;
242                 goto error;
243         }
244
245         return (0);
246
247 error:
248         if (ef->firstpage != NULL) {
249                 free(ef->firstpage);
250                 ef->firstpage = NULL;
251         }
252         if (ef->fd != -1) {
253                 close(ef->fd);
254                 ef->fd = -1;
255         }
256         return (err);
257 }
258
259 /*
260  * Attempt to load the file (file) as an ELF module.  It will be stored at
261  * (dest), and a pointer to a module structure describing the loaded object
262  * will be saved in (result).
263  */
264 int
265 __elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result)
266 {
267         return (__elfN(loadfile_raw)(filename, dest, result, 0));
268 }
269
270 int
271 __elfN(loadfile_raw)(char *filename, u_int64_t dest,
272     struct preloaded_file **result, int multiboot)
273 {
274     struct preloaded_file       *fp, *kfp;
275     struct elf_file             ef;
276     Elf_Ehdr                    *ehdr;
277     int                         err;
278
279     fp = NULL;
280     bzero(&ef, sizeof(struct elf_file));
281     ef.fd = -1;
282
283     err = __elfN(load_elf_header)(filename, &ef);
284     if (err != 0)
285         return (err);
286
287     ehdr = ef.ehdr;
288
289     /*
290      * Check to see what sort of module we are.
291      */
292     kfp = file_findfile(NULL, __elfN(kerneltype));
293 #ifdef __powerpc__
294     /*
295      * Kernels can be ET_DYN, so just assume the first loaded object is the
296      * kernel. This assumption will be checked later.
297      */
298     if (kfp == NULL)
299         ef.kernel = 1;
300 #endif
301     if (ef.kernel || ehdr->e_type == ET_EXEC) {
302         /* Looks like a kernel */
303         if (kfp != NULL) {
304             printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n");
305             err = EPERM;
306             goto oerr;
307         }
308         /* 
309          * Calculate destination address based on kernel entrypoint.
310          *
311          * For ARM, the destination address is independent of any values in the
312          * elf header (an ARM kernel can be loaded at any 2MB boundary), so we
313          * leave dest set to the value calculated by archsw.arch_loadaddr() and
314          * passed in to this function.
315          */
316 #ifndef __arm__
317         if (ehdr->e_type == ET_EXEC)
318             dest = (ehdr->e_entry & ~PAGE_MASK);
319 #endif
320         if ((ehdr->e_entry & ~PAGE_MASK) == 0) {
321             printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n");
322             err = EPERM;
323             goto oerr;
324         }
325         ef.kernel = 1;
326
327     } else if (ehdr->e_type == ET_DYN) {
328         /* Looks like a kld module */
329         if (multiboot != 0) {
330                 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module as multiboot\n");
331                 err = EPERM;
332                 goto oerr;
333         }
334         if (kfp == NULL) {
335             printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n");
336             err = EPERM;
337             goto oerr;
338         }
339         if (strcmp(__elfN(kerneltype), kfp->f_type)) {
340             printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
341             err = EPERM;
342             goto oerr;
343         }
344         /* Looks OK, got ahead */
345         ef.kernel = 0;
346
347     } else {
348         err = EFTYPE;
349         goto oerr;
350     }
351
352     if (archsw.arch_loadaddr != NULL)
353         dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest);
354     else
355         dest = roundup(dest, PAGE_SIZE);
356
357     /* 
358      * Ok, we think we should handle this.
359      */
360     fp = file_alloc();
361     if (fp == NULL) {
362             printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n");
363             err = EPERM;
364             goto out;
365     }
366     if (ef.kernel == 1 && multiboot == 0)
367         setenv("kernelname", filename, 1);
368     fp->f_name = strdup(filename);
369     if (multiboot == 0)
370         fp->f_type = strdup(ef.kernel ?
371             __elfN(kerneltype) : __elfN(moduletype));
372     else
373         fp->f_type = strdup("elf multiboot kernel");
374
375 #ifdef ELF_VERBOSE
376     if (ef.kernel)
377         printf("%s entry at 0x%jx\n", filename, (uintmax_t)ehdr->e_entry);
378 #else
379     printf("%s ", filename);
380 #endif
381
382     fp->f_size = __elfN(loadimage)(fp, &ef, dest);
383     if (fp->f_size == 0 || fp->f_addr == 0)
384         goto ioerr;
385
386     /* save exec header as metadata */
387     file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
388
389     /* Load OK, return module pointer */
390     *result = (struct preloaded_file *)fp;
391     err = 0;
392     goto out;
393     
394  ioerr:
395     err = EIO;
396  oerr:
397     file_discard(fp);
398  out:
399     if (ef.firstpage)
400         free(ef.firstpage);
401     if (ef.fd != -1)
402         close(ef.fd);
403     return(err);
404 }
405
406 /*
407  * With the file (fd) open on the image, and (ehdr) containing
408  * the Elf header, load the image at (off)
409  */
410 static int
411 __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
412 {
413     int         i;
414     u_int       j;
415     Elf_Ehdr    *ehdr;
416     Elf_Phdr    *phdr, *php;
417     Elf_Shdr    *shdr;
418     char        *shstr;
419     int         ret;
420     vm_offset_t firstaddr;
421     vm_offset_t lastaddr;
422     size_t      chunk;
423     ssize_t     result;
424     Elf_Addr    ssym, esym;
425     Elf_Dyn     *dp;
426     Elf_Addr    adp;
427     Elf_Addr    ctors;
428     int         ndp;
429     int         symstrindex;
430     int         symtabindex;
431     Elf_Size    size;
432     u_int       fpcopy;
433     Elf_Sym     sym;
434     Elf_Addr    p_start, p_end;
435
436     dp = NULL;
437     shdr = NULL;
438     ret = 0;
439     firstaddr = lastaddr = 0;
440     ehdr = ef->ehdr;
441     if (ehdr->e_type == ET_EXEC) {
442 #if defined(__i386__) || defined(__amd64__)
443 #if __ELF_WORD_SIZE == 64
444         off = - (off & 0xffffffffff000000ull);/* x86_64 relocates after locore */
445 #else
446         off = - (off & 0xff000000u);    /* i386 relocates after locore */
447 #endif
448 #elif defined(__powerpc__)
449         /*
450          * On the purely virtual memory machines like e500, the kernel is
451          * linked against its final VA range, which is most often not
452          * available at the loader stage, but only after kernel initializes
453          * and completes its VM settings. In such cases we cannot use p_vaddr
454          * field directly to load ELF segments, but put them at some
455          * 'load-time' locations.
456          */
457         if (off & 0xf0000000u) {
458             off = -(off & 0xf0000000u);
459             /*
460              * XXX the physical load address should not be hardcoded. Note
461              * that the Book-E kernel assumes that it's loaded at a 16MB
462              * boundary for now...
463              */
464             off += 0x01000000;
465             ehdr->e_entry += off;
466 #ifdef ELF_VERBOSE
467             printf("Converted entry 0x%08x\n", ehdr->e_entry);
468 #endif
469         } else
470             off = 0;
471 #elif defined(__arm__) && !defined(EFI)
472         /*
473          * The elf headers in arm kernels specify virtual addresses in all
474          * header fields, even the ones that should be physical addresses.
475          * We assume the entry point is in the first page, and masking the page
476          * offset will leave us with the virtual address the kernel was linked
477          * at.  We subtract that from the load offset, making 'off' into the
478          * value which, when added to a virtual address in an elf header,
479          * translates it to a physical address.  We do the va->pa conversion on
480          * the entry point address in the header now, so that later we can
481          * launch the kernel by just jumping to that address.
482          *
483          * When booting from UEFI the copyin and copyout functions handle
484          * adjusting the location relative to the first virtual address.
485          * Because of this there is no need to adjust the offset or entry
486          * point address as these will both be handled by the efi code.
487          */
488         off -= ehdr->e_entry & ~PAGE_MASK;
489         ehdr->e_entry += off;
490 #ifdef ELF_VERBOSE
491         printf("ehdr->e_entry 0x%08x, va<->pa off %llx\n", ehdr->e_entry, off);
492 #endif
493 #else
494         off = 0;                /* other archs use direct mapped kernels */
495 #endif
496     }
497     ef->off = off;
498
499     if (ef->kernel)
500         __elfN(relocation_offset) = off;
501
502     if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
503         printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: program header not within first page\n");
504         goto out;
505     }
506     phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
507
508     for (i = 0; i < ehdr->e_phnum; i++) {
509         if (elf_program_header_convert(ehdr, phdr))
510             continue;
511
512         /* We want to load PT_LOAD segments only.. */
513         if (phdr[i].p_type != PT_LOAD)
514             continue;
515
516 #ifdef ELF_VERBOSE
517         printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
518             (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
519             (long)(phdr[i].p_vaddr + off),
520             (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
521 #else
522         if ((phdr[i].p_flags & PF_W) == 0) {
523             printf("text=0x%lx ", (long)phdr[i].p_filesz);
524         } else {
525             printf("data=0x%lx", (long)phdr[i].p_filesz);
526             if (phdr[i].p_filesz < phdr[i].p_memsz)
527                 printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz));
528             printf(" ");
529         }
530 #endif
531         fpcopy = 0;
532         if (ef->firstlen > phdr[i].p_offset) {
533             fpcopy = ef->firstlen - phdr[i].p_offset;
534             archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
535                                phdr[i].p_vaddr + off, fpcopy);
536         }
537         if (phdr[i].p_filesz > fpcopy) {
538             if (kern_pread(ef->fd, phdr[i].p_vaddr + off + fpcopy,
539                 phdr[i].p_filesz - fpcopy, phdr[i].p_offset + fpcopy) != 0) {
540                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
541                     "_loadimage: read failed\n");
542                 goto out;
543             }
544         }
545         /* clear space from oversized segments; eg: bss */
546         if (phdr[i].p_filesz < phdr[i].p_memsz) {
547 #ifdef ELF_VERBOSE
548             printf(" (bss: 0x%lx-0x%lx)",
549                 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
550                 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
551 #endif
552
553             kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
554                 phdr[i].p_memsz - phdr[i].p_filesz);
555         }
556 #ifdef ELF_VERBOSE
557         printf("\n");
558 #endif
559
560         if (archsw.arch_loadseg != NULL)
561             archsw.arch_loadseg(ehdr, phdr + i, off);
562
563         if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
564             firstaddr = phdr[i].p_vaddr + off;
565         if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz))
566             lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
567     }
568     lastaddr = roundup(lastaddr, sizeof(long));
569
570     /*
571      * Get the section headers.  We need this for finding the .ctors
572      * section as well as for loading any symbols.  Both may be hard
573      * to do if reading from a .gz file as it involves seeking.  I
574      * think the rule is going to have to be that you must strip a
575      * file to remove symbols before gzipping it.
576      */
577     chunk = (size_t)ehdr->e_shnum * (size_t)ehdr->e_shentsize;
578     if (chunk == 0 || ehdr->e_shoff == 0)
579         goto nosyms;
580     shdr = alloc_pread(ef->fd, ehdr->e_shoff, chunk);
581     if (shdr == NULL) {
582         printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
583             "_loadimage: failed to read section headers");
584         goto nosyms;
585     }
586
587     for (i = 0; i < ehdr->e_shnum; i++)
588         elf_section_header_convert(ehdr, &shdr[i]);
589
590     file_addmetadata(fp, MODINFOMD_SHDR, chunk, shdr);
591
592     /*
593      * Read the section string table and look for the .ctors section.
594      * We need to tell the kernel where it is so that it can call the
595      * ctors.
596      */
597     chunk = shdr[ehdr->e_shstrndx].sh_size;
598     if (chunk) {
599         shstr = alloc_pread(ef->fd, shdr[ehdr->e_shstrndx].sh_offset, chunk);
600         if (shstr) {
601             for (i = 0; i < ehdr->e_shnum; i++) {
602                 if (strcmp(shstr + shdr[i].sh_name, ".ctors") != 0)
603                     continue;
604                 ctors = shdr[i].sh_addr;
605                 file_addmetadata(fp, MODINFOMD_CTORS_ADDR, sizeof(ctors),
606                     &ctors);
607                 size = shdr[i].sh_size;
608                 file_addmetadata(fp, MODINFOMD_CTORS_SIZE, sizeof(size),
609                     &size);
610                 break;
611             }
612             free(shstr);
613         }
614     }
615
616     /*
617      * Now load any symbols.
618      */
619     symtabindex = -1;
620     symstrindex = -1;
621     for (i = 0; i < ehdr->e_shnum; i++) {
622         if (shdr[i].sh_type != SHT_SYMTAB)
623             continue;
624         for (j = 0; j < ehdr->e_phnum; j++) {
625             if (phdr[j].p_type != PT_LOAD)
626                 continue;
627             if (shdr[i].sh_offset >= phdr[j].p_offset &&
628                 (shdr[i].sh_offset + shdr[i].sh_size <=
629                  phdr[j].p_offset + phdr[j].p_filesz)) {
630                 shdr[i].sh_offset = 0;
631                 shdr[i].sh_size = 0;
632                 break;
633             }
634         }
635         if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
636             continue;           /* alread loaded in a PT_LOAD above */
637         /* Save it for loading below */
638         symtabindex = i;
639         symstrindex = shdr[i].sh_link;
640     }
641     if (symtabindex < 0 || symstrindex < 0)
642         goto nosyms;
643
644     /* Ok, committed to a load. */
645 #ifndef ELF_VERBOSE
646     printf("syms=[");
647 #endif
648     ssym = lastaddr;
649     for (i = symtabindex; i >= 0; i = symstrindex) {
650 #ifdef ELF_VERBOSE
651         char    *secname;
652
653         switch(shdr[i].sh_type) {
654             case SHT_SYMTAB:            /* Symbol table */
655                 secname = "symtab";
656                 break;
657             case SHT_STRTAB:            /* String table */
658                 secname = "strtab";
659                 break;
660             default:
661                 secname = "WHOA!!";
662                 break;
663         }
664 #endif
665         size = shdr[i].sh_size;
666 #if defined(__powerpc__)
667   #if __ELF_WORD_SIZE == 64
668         size = htobe64(size);
669   #else
670         size = htobe32(size);
671   #endif
672 #endif
673
674         archsw.arch_copyin(&size, lastaddr, sizeof(size));
675         lastaddr += sizeof(size);
676
677 #ifdef ELF_VERBOSE
678         printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
679             (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
680             (uintmax_t)lastaddr, (uintmax_t)(lastaddr + shdr[i].sh_size));
681 #else
682         if (i == symstrindex)
683             printf("+");
684         printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
685 #endif
686
687         if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
688             printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not seek for symbols - skipped!");
689             lastaddr = ssym;
690             ssym = 0;
691             goto nosyms;
692         }
693         result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
694         if (result < 0 || (size_t)result != shdr[i].sh_size) {
695             printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not read symbols - skipped! (%ju != %ju)", (uintmax_t)result,
696                 (uintmax_t)shdr[i].sh_size);
697             lastaddr = ssym;
698             ssym = 0;
699             goto nosyms;
700         }
701         /* Reset offsets relative to ssym */
702         lastaddr += shdr[i].sh_size;
703         lastaddr = roundup(lastaddr, sizeof(size));
704         if (i == symtabindex)
705             symtabindex = -1;
706         else if (i == symstrindex)
707             symstrindex = -1;
708     }
709     esym = lastaddr;
710 #ifndef ELF_VERBOSE
711     printf("]");
712 #endif
713
714 #if defined(__powerpc__)
715   /* On PowerPC we always need to provide BE data to the kernel */
716   #if __ELF_WORD_SIZE == 64
717     ssym = htobe64((uint64_t)ssym);
718     esym = htobe64((uint64_t)esym);
719   #else
720     ssym = htobe32((uint32_t)ssym);
721     esym = htobe32((uint32_t)esym);
722   #endif
723 #endif
724
725     file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
726     file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
727
728 nosyms:
729     printf("\n");
730
731     ret = lastaddr - firstaddr;
732     fp->f_addr = firstaddr;
733
734     php = NULL;
735     for (i = 0; i < ehdr->e_phnum; i++) {
736         if (phdr[i].p_type == PT_DYNAMIC) {
737             php = phdr + i;
738             adp = php->p_vaddr;
739             file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp);
740             break;
741         }
742     }
743
744     if (php == NULL)    /* this is bad, we cannot get to symbols or _DYNAMIC */
745         goto out;
746
747     ndp = php->p_filesz / sizeof(Elf_Dyn);
748     if (ndp == 0)
749         goto out;
750     dp = malloc(php->p_filesz);
751     if (dp == NULL)
752         goto out;
753     archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
754
755     ef->strsz = 0;
756     for (i = 0; i < ndp; i++) {
757         if (dp[i].d_tag == 0)
758             break;
759         switch (dp[i].d_tag) {
760         case DT_HASH:
761             ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
762             break;
763         case DT_STRTAB:
764             ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
765             break;
766         case DT_STRSZ:
767             ef->strsz = dp[i].d_un.d_val;
768             break;
769         case DT_SYMTAB:
770             ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off);
771             break;
772         case DT_REL:
773             ef->rel = (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
774             break;
775         case DT_RELSZ:
776             ef->relsz = dp[i].d_un.d_val;
777             break;
778         case DT_RELA:
779             ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
780             break;
781         case DT_RELASZ:
782             ef->relasz = dp[i].d_un.d_val;
783             break;
784         default:
785             break;
786         }
787     }
788     if (ef->hashtab == NULL || ef->symtab == NULL ||
789         ef->strtab == NULL || ef->strsz == 0)
790         goto out;
791     COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
792     COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
793     ef->buckets = ef->hashtab + 2;
794     ef->chains = ef->buckets + ef->nbuckets;
795
796     if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
797         return 0;
798     p_start = sym.st_value + ef->off;
799     if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
800         return ENOENT;
801     p_end = sym.st_value + ef->off;
802
803     if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0)
804         goto out;
805
806     if (ef->kernel)                     /* kernel must not depend on anything */
807         goto out;
808
809 out:
810     if (dp)
811         free(dp);
812     if (shdr)
813         free(shdr);
814     return ret;
815 }
816
817 static char invalid_name[] = "bad";
818
819 char *
820 fake_modname(const char *name)
821 {
822     const char *sp, *ep;
823     char *fp;
824     size_t len;
825
826     sp = strrchr(name, '/');
827     if (sp)
828         sp++;
829     else
830         sp = name;
831     ep = strrchr(name, '.');
832     if (ep) {
833             if (ep == name) {
834                 sp = invalid_name;
835                 ep = invalid_name + sizeof(invalid_name) - 1;
836             } 
837     } else
838         ep = name + strlen(name);
839     len = ep - sp;
840     fp = malloc(len + 1);
841     if (fp == NULL)
842         return NULL;
843     memcpy(fp, sp, len);
844     fp[len] = '\0';
845     return fp;
846 }
847
848 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
849 struct mod_metadata64 {
850         int             md_version;     /* structure version MDTV_* */  
851         int             md_type;        /* type of entry MDT_* */
852         u_int64_t       md_data;        /* specific data */
853         u_int64_t       md_cval;        /* common string label */
854 };
855 #endif
856 #if defined(__amd64__) && __ELF_WORD_SIZE == 32
857 struct mod_metadata32 {
858         int             md_version;     /* structure version MDTV_* */  
859         int             md_type;        /* type of entry MDT_* */
860         u_int32_t       md_data;        /* specific data */
861         u_int32_t       md_cval;        /* common string label */
862 };
863 #endif
864
865 int
866 __elfN(load_modmetadata)(struct preloaded_file *fp, u_int64_t dest)
867 {
868         struct elf_file          ef;
869         int                      err, i, j;
870         Elf_Shdr                *sh_meta, *shdr = NULL;
871         Elf_Shdr                *sh_data[2];
872         char                    *shstrtab = NULL;
873         size_t                   size;
874         Elf_Addr                 p_start, p_end;
875
876         bzero(&ef, sizeof(struct elf_file));
877         ef.fd = -1;
878
879         err = __elfN(load_elf_header)(fp->f_name, &ef);
880         if (err != 0)
881                 goto out;
882
883         if (ef.kernel == 1 || ef.ehdr->e_type == ET_EXEC) {
884                 ef.kernel = 1;
885         } else if (ef.ehdr->e_type != ET_DYN) {
886                 err = EFTYPE;
887                 goto out;
888         }
889
890         size = (size_t)ef.ehdr->e_shnum * (size_t)ef.ehdr->e_shentsize;
891         shdr = alloc_pread(ef.fd, ef.ehdr->e_shoff, size);
892         if (shdr == NULL) {
893                 err = ENOMEM;
894                 goto out;
895         }
896
897         /* Load shstrtab. */
898         shstrtab = alloc_pread(ef.fd, shdr[ef.ehdr->e_shstrndx].sh_offset,
899             shdr[ef.ehdr->e_shstrndx].sh_size);
900         if (shstrtab == NULL) {
901                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
902                     "load_modmetadata: unable to load shstrtab\n");
903                 err = EFTYPE;
904                 goto out;
905         }
906
907         /* Find set_modmetadata_set and data sections. */
908         sh_data[0] = sh_data[1] = sh_meta = NULL;
909         for (i = 0, j = 0; i < ef.ehdr->e_shnum; i++) {
910                 if (strcmp(&shstrtab[shdr[i].sh_name],
911                     "set_modmetadata_set") == 0) {
912                         sh_meta = &shdr[i];
913                 }
914                 if ((strcmp(&shstrtab[shdr[i].sh_name], ".data") == 0) ||
915                     (strcmp(&shstrtab[shdr[i].sh_name], ".rodata") == 0)) {
916                         sh_data[j++] = &shdr[i];
917                 }
918         }
919         if (sh_meta == NULL || sh_data[0] == NULL || sh_data[1] == NULL) {
920                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
921     "load_modmetadata: unable to find set_modmetadata_set or data sections\n");
922                 err = EFTYPE;
923                 goto out;
924         }
925
926         /* Load set_modmetadata_set into memory */
927         err = kern_pread(ef.fd, dest, sh_meta->sh_size, sh_meta->sh_offset);
928         if (err != 0) {
929                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
930     "load_modmetadata: unable to load set_modmetadata_set: %d\n", err);
931                 goto out;
932         }
933         p_start = dest;
934         p_end = dest + sh_meta->sh_size;
935         dest += sh_meta->sh_size;
936
937         /* Load data sections into memory. */
938         err = kern_pread(ef.fd, dest, sh_data[0]->sh_size,
939             sh_data[0]->sh_offset);
940         if (err != 0) {
941                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
942                     "load_modmetadata: unable to load data: %d\n", err);
943                 goto out;
944         }
945
946         /*
947          * We have to increment the dest, so that the offset is the same into
948          * both the .rodata and .data sections.
949          */
950         ef.off = -(sh_data[0]->sh_addr - dest);
951         dest += (sh_data[1]->sh_addr - sh_data[0]->sh_addr);
952
953         err = kern_pread(ef.fd, dest, sh_data[1]->sh_size,
954             sh_data[1]->sh_offset);
955         if (err != 0) {
956                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
957                     "load_modmetadata: unable to load data: %d\n", err);
958                 goto out;
959         }
960
961         err = __elfN(parse_modmetadata)(fp, &ef, p_start, p_end);
962         if (err != 0) {
963                 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
964                     "load_modmetadata: unable to parse metadata: %d\n", err);
965                 goto out;
966         }
967
968 out:
969         if (shstrtab != NULL)
970                 free(shstrtab);
971         if (shdr != NULL)
972                 free(shdr);
973         if (ef.firstpage != NULL)
974                 free(ef.firstpage);
975         if (ef.fd != -1)
976                 close(ef.fd);
977         return (err);
978 }
979
980 int
981 __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
982     Elf_Addr p_start, Elf_Addr p_end)
983 {
984     struct mod_metadata md;
985 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
986     struct mod_metadata64 md64;
987 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
988     struct mod_metadata32 md32;
989 #endif
990     struct mod_depend *mdepend;
991     struct mod_version mver;
992     char *s;
993     int error, modcnt, minfolen;
994     Elf_Addr v, p;
995
996     modcnt = 0;
997     p = p_start;
998     while (p < p_end) {
999         COPYOUT(p, &v, sizeof(v));
1000         error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
1001         if (error == EOPNOTSUPP)
1002             v += ef->off;
1003         else if (error != 0)
1004             return (error);
1005 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
1006         COPYOUT(v, &md64, sizeof(md64));
1007         error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
1008         if (error == EOPNOTSUPP) {
1009             md64.md_cval += ef->off;
1010             md64.md_data += ef->off;
1011         } else if (error != 0)
1012             return (error);
1013         md.md_version = md64.md_version;
1014         md.md_type = md64.md_type;
1015         md.md_cval = (const char *)(uintptr_t)md64.md_cval;
1016         md.md_data = (void *)(uintptr_t)md64.md_data;
1017 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
1018         COPYOUT(v, &md32, sizeof(md32));
1019         error = __elfN(reloc_ptr)(fp, ef, v, &md32, sizeof(md32));
1020         if (error == EOPNOTSUPP) {
1021             md32.md_cval += ef->off;
1022             md32.md_data += ef->off;
1023         } else if (error != 0)
1024             return (error);
1025         md.md_version = md32.md_version;
1026         md.md_type = md32.md_type;
1027         md.md_cval = (const char *)(uintptr_t)md32.md_cval;
1028         md.md_data = (void *)(uintptr_t)md32.md_data;
1029 #else
1030         COPYOUT(v, &md, sizeof(md));
1031         error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
1032         if (error == EOPNOTSUPP) {
1033             md.md_cval += ef->off;
1034             md.md_data = (void *)((uintptr_t)md.md_data + (uintptr_t)ef->off);
1035         } else if (error != 0)
1036             return (error);
1037 #endif
1038         p += sizeof(Elf_Addr);
1039         switch(md.md_type) {
1040           case MDT_DEPEND:
1041             if (ef->kernel)             /* kernel must not depend on anything */
1042               break;
1043             s = strdupout((vm_offset_t)md.md_cval);
1044             minfolen = sizeof(*mdepend) + strlen(s) + 1;
1045             mdepend = malloc(minfolen);
1046             if (mdepend == NULL)
1047                 return ENOMEM;
1048             COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend));
1049             strcpy((char*)(mdepend + 1), s);
1050             free(s);
1051             file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
1052             free(mdepend);
1053             break;
1054           case MDT_VERSION:
1055             s = strdupout((vm_offset_t)md.md_cval);
1056             COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
1057             file_addmodule(fp, s, mver.mv_version, NULL);
1058             free(s);
1059             modcnt++;
1060             break;
1061         }
1062     }
1063     if (modcnt == 0) {
1064         s = fake_modname(fp->f_name);
1065         file_addmodule(fp, s, 1, NULL);
1066         free(s);
1067     }
1068     return 0;
1069 }
1070
1071 static unsigned long
1072 elf_hash(const char *name)
1073 {
1074     const unsigned char *p = (const unsigned char *) name;
1075     unsigned long h = 0;
1076     unsigned long g;
1077
1078     while (*p != '\0') {
1079         h = (h << 4) + *p++;
1080         if ((g = h & 0xf0000000) != 0)
1081             h ^= g >> 24;
1082         h &= ~g;
1083     }
1084     return h;
1085 }
1086
1087 static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n";
1088 int
1089 __elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef, const char* name,
1090                   Elf_Sym *symp)
1091 {
1092     Elf_Hashelt symnum;
1093     Elf_Sym sym;
1094     char *strp;
1095     unsigned long hash;
1096
1097     hash = elf_hash(name);
1098     COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
1099
1100     while (symnum != STN_UNDEF) {
1101         if (symnum >= ef->nchains) {
1102             printf(__elfN(bad_symtable));
1103             return ENOENT;
1104         }
1105
1106         COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
1107         if (sym.st_name == 0) {
1108             printf(__elfN(bad_symtable));
1109             return ENOENT;
1110         }
1111
1112         strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
1113         if (strcmp(name, strp) == 0) {
1114             free(strp);
1115             if (sym.st_shndx != SHN_UNDEF ||
1116                 (sym.st_value != 0 &&
1117                  ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
1118                 *symp = sym;
1119                 return 0;
1120             }
1121             return ENOENT;
1122         }
1123         free(strp);
1124         COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
1125     }
1126     return ENOENT;
1127 }
1128
1129 /*
1130  * Apply any intra-module relocations to the value. p is the load address
1131  * of the value and val/len is the value to be modified. This does NOT modify
1132  * the image in-place, because this is done by kern_linker later on.
1133  *
1134  * Returns EOPNOTSUPP if no relocation method is supplied.
1135  */
1136 static int
1137 __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
1138     Elf_Addr p, void *val, size_t len)
1139 {
1140         size_t n;
1141         Elf_Rela a;
1142         Elf_Rel r;
1143         int error;
1144
1145         /*
1146          * The kernel is already relocated, but we still want to apply
1147          * offset adjustments.
1148          */
1149         if (ef->kernel)
1150                 return (EOPNOTSUPP);
1151
1152         for (n = 0; n < ef->relsz / sizeof(r); n++) {
1153                 COPYOUT(ef->rel + n, &r, sizeof(r));
1154
1155                 error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
1156                     ef->off, p, val, len);
1157                 if (error != 0)
1158                         return (error);
1159         }
1160         for (n = 0; n < ef->relasz / sizeof(a); n++) {
1161                 COPYOUT(ef->rela + n, &a, sizeof(a));
1162
1163                 error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
1164                     ef->off, p, val, len);
1165                 if (error != 0)
1166                         return (error);
1167         }
1168
1169         return (0);
1170 }
1171
1172 static Elf_Addr
1173 __elfN(symaddr)(struct elf_file *ef, Elf_Size symidx)
1174 {
1175
1176         /* Symbol lookup by index not required here. */
1177         return (0);
1178 }