2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/endian.h>
34 #include <sys/linker.h>
35 #include <sys/module.h>
36 #include <sys/stdint.h>
38 #include <machine/elf.h>
41 #include <sys/link_elf.h>
43 #include "bootstrap.h"
45 #define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
47 #if defined(__i386__) && __ELF_WORD_SIZE == 64
50 #define ELF_TARG_CLASS ELFCLASS64
51 #define ELF_TARG_MACH EM_X86_64
54 typedef struct elf_file {
74 #ifdef LOADER_VERIEXEC_VECTX
79 #ifdef LOADER_VERIEXEC_VECTX
80 #define VECTX_HANDLE(ef) (ef)->vctx
82 #define VECTX_HANDLE(ef) (ef)->fd
85 static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef,
87 static int __elfN(lookup_symbol)(elf_file_t ef, const char* name,
88 Elf_Sym *sym, unsigned char type);
89 static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
90 Elf_Addr p, void *val, size_t len);
91 static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef,
92 Elf_Addr p_start, Elf_Addr p_end);
93 static symaddr_fn __elfN(symaddr);
94 static char *fake_modname(const char *name);
96 const char *__elfN(kerneltype) = "elf kernel";
97 const char *__elfN(moduletype) = "elf module";
99 uint64_t __elfN(relocation_offset) = 0;
101 extern void elf_wrong_field_size(void);
102 #define CONVERT_FIELD(b, f, e) \
103 switch (sizeof((b)->f)) { \
105 (b)->f = e ## 16toh((b)->f); \
108 (b)->f = e ## 32toh((b)->f); \
111 (b)->f = e ## 64toh((b)->f); \
114 /* Force a link time error. */ \
115 elf_wrong_field_size(); \
119 #define CONVERT_SWITCH(h, d, f) \
120 switch ((h)->e_ident[EI_DATA]) { \
132 static int elf_header_convert(Elf_Ehdr *ehdr)
135 * Fixup ELF header endianness.
137 * The Xhdr structure was loaded using block read call to optimize file
138 * accesses. It might happen, that the endianness of the system memory
139 * is different that endianness of the ELF header. Swap fields here to
140 * guarantee that Xhdr always contain valid data regardless of
143 #define HEADER_FIELDS(b, e) \
144 CONVERT_FIELD(b, e_type, e); \
145 CONVERT_FIELD(b, e_machine, e); \
146 CONVERT_FIELD(b, e_version, e); \
147 CONVERT_FIELD(b, e_entry, e); \
148 CONVERT_FIELD(b, e_phoff, e); \
149 CONVERT_FIELD(b, e_shoff, e); \
150 CONVERT_FIELD(b, e_flags, e); \
151 CONVERT_FIELD(b, e_ehsize, e); \
152 CONVERT_FIELD(b, e_phentsize, e); \
153 CONVERT_FIELD(b, e_phnum, e); \
154 CONVERT_FIELD(b, e_shentsize, e); \
155 CONVERT_FIELD(b, e_shnum, e); \
156 CONVERT_FIELD(b, e_shstrndx, e)
158 CONVERT_SWITCH(ehdr, ehdr, HEADER_FIELDS);
165 static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr)
167 #define PROGRAM_HEADER_FIELDS(b, e) \
168 CONVERT_FIELD(b, p_type, e); \
169 CONVERT_FIELD(b, p_flags, e); \
170 CONVERT_FIELD(b, p_offset, e); \
171 CONVERT_FIELD(b, p_vaddr, e); \
172 CONVERT_FIELD(b, p_paddr, e); \
173 CONVERT_FIELD(b, p_filesz, e); \
174 CONVERT_FIELD(b, p_memsz, e); \
175 CONVERT_FIELD(b, p_align, e)
177 CONVERT_SWITCH(ehdr, phdr, PROGRAM_HEADER_FIELDS);
179 #undef PROGRAM_HEADER_FIELDS
184 static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr)
186 #define SECTION_HEADER_FIELDS(b, e) \
187 CONVERT_FIELD(b, sh_name, e); \
188 CONVERT_FIELD(b, sh_type, e); \
189 CONVERT_FIELD(b, sh_link, e); \
190 CONVERT_FIELD(b, sh_info, e); \
191 CONVERT_FIELD(b, sh_flags, e); \
192 CONVERT_FIELD(b, sh_addr, e); \
193 CONVERT_FIELD(b, sh_offset, e); \
194 CONVERT_FIELD(b, sh_size, e); \
195 CONVERT_FIELD(b, sh_addralign, e); \
196 CONVERT_FIELD(b, sh_entsize, e)
198 CONVERT_SWITCH(ehdr, shdr, SECTION_HEADER_FIELDS);
200 #undef SECTION_HEADER_FIELDS
204 #undef CONVERT_SWITCH
210 is_kernphys_relocatable(elf_file_t ef)
214 return (__elfN(lookup_symbol)(ef, "kernphys", &sym, STT_OBJECT) == 0 &&
221 is_tg_kernel_support(struct preloaded_file *fp, elf_file_t ef)
224 Elf_Addr p_start, p_end, v, p;
228 if (__elfN(lookup_symbol)(ef, "__start_set_vt_drv_set", &sym, STT_NOTYPE) != 0)
230 p_start = sym.st_value + ef->off;
231 if (__elfN(lookup_symbol)(ef, "__stop_set_vt_drv_set", &sym, STT_NOTYPE) != 0)
233 p_end = sym.st_value + ef->off;
236 * Walk through vt_drv_set, each vt driver structure starts with
237 * static 16 chars for driver name. If we have "vbefb", return true.
239 for (p = p_start; p < p_end; p += sizeof(Elf_Addr)) {
240 COPYOUT(p, &v, sizeof(v));
242 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
243 if (error == EOPNOTSUPP)
247 COPYOUT(v, &vd_name, sizeof(vd_name));
248 if (strncmp(vd_name, "vbefb", sizeof(vd_name)) == 0)
257 __elfN(load_elf_header)(char *filename, elf_file_t ef)
264 * Open the image, read and validate the ELF header
266 if (filename == NULL) /* can't handle nameless */
268 if ((ef->fd = open(filename, O_RDONLY)) == -1)
270 ef->firstpage = malloc(PAGE_SIZE);
271 if (ef->firstpage == NULL) {
275 #ifdef LOADER_VERIEXEC_VECTX
279 ef->vctx = vectx_open(ef->fd, filename, 0L, NULL, &verror, __func__);
281 printf("Unverified %s: %s\n", filename, ve_error_get());
288 bytes_read = VECTX_READ(VECTX_HANDLE(ef), ef->firstpage, PAGE_SIZE);
289 ef->firstlen = (size_t)bytes_read;
290 if (bytes_read < 0 || ef->firstlen <= sizeof(Elf_Ehdr)) {
291 err = EFTYPE; /* could be EIO, but may be small file */
294 ehdr = ef->ehdr = (Elf_Ehdr *)ef->firstpage;
297 if (!IS_ELF(*ehdr)) {
302 if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
303 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
304 ehdr->e_ident[EI_VERSION] != EV_CURRENT) /* Version ? */ {
309 err = elf_header_convert(ehdr);
313 if (ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) {
319 #if defined(LOADER_VERIEXEC) && !defined(LOADER_VERIEXEC_VECTX)
320 if (verify_file(ef->fd, filename, bytes_read, VE_MUST, __func__) < 0) {
328 if (ef->firstpage != NULL) {
330 ef->firstpage = NULL;
333 #ifdef LOADER_VERIEXEC_VECTX
343 * Attempt to load the file (file) as an ELF module. It will be stored at
344 * (dest), and a pointer to a module structure describing the loaded object
345 * will be saved in (result).
348 __elfN(loadfile)(char *filename, uint64_t dest, struct preloaded_file **result)
350 return (__elfN(loadfile_raw)(filename, dest, result, 0));
354 __elfN(loadfile_raw)(char *filename, uint64_t dest,
355 struct preloaded_file **result, int multiboot)
357 struct preloaded_file *fp, *kfp;
363 bzero(&ef, sizeof(struct elf_file));
366 err = __elfN(load_elf_header)(filename, &ef);
373 * Check to see what sort of module we are.
375 kfp = file_findfile(NULL, __elfN(kerneltype));
378 * Kernels can be ET_DYN, so just assume the first loaded object is the
379 * kernel. This assumption will be checked later.
384 if (ef.kernel || ehdr->e_type == ET_EXEC) {
385 /* Looks like a kernel */
387 printf("elf" __XSTRING(__ELF_WORD_SIZE)
388 "_loadfile: kernel already loaded\n");
393 * Calculate destination address based on kernel entrypoint.
395 * For ARM, the destination address is independent of any values
396 * in the elf header (an ARM kernel can be loaded at any 2MB
397 * boundary), so we leave dest set to the value calculated by
398 * archsw.arch_loadaddr() and passed in to this function.
401 if (ehdr->e_type == ET_EXEC)
402 dest = (ehdr->e_entry & ~PAGE_MASK);
404 if ((ehdr->e_entry & ~PAGE_MASK) == 0) {
405 printf("elf" __XSTRING(__ELF_WORD_SIZE)
406 "_loadfile: not a kernel (maybe static binary?)\n");
412 } else if (ehdr->e_type == ET_DYN) {
413 /* Looks like a kld module */
414 if (multiboot != 0) {
415 printf("elf" __XSTRING(__ELF_WORD_SIZE)
416 "_loadfile: can't load module as multiboot\n");
421 printf("elf" __XSTRING(__ELF_WORD_SIZE)
422 "_loadfile: can't load module before kernel\n");
426 if (strcmp(__elfN(kerneltype), kfp->f_type)) {
427 printf("elf" __XSTRING(__ELF_WORD_SIZE)
428 "_loadfile: can't load module with kernel type '%s'\n",
433 /* Looks OK, got ahead */
441 if (archsw.arch_loadaddr != NULL)
442 dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest);
444 dest = roundup(dest, PAGE_SIZE);
447 * Ok, we think we should handle this.
451 printf("elf" __XSTRING(__ELF_WORD_SIZE)
452 "_loadfile: cannot allocate module info\n");
456 if (ef.kernel == 1 && multiboot == 0)
457 setenv("kernelname", filename, 1);
458 fp->f_name = strdup(filename);
460 fp->f_type = strdup(ef.kernel ?
461 __elfN(kerneltype) : __elfN(moduletype));
463 fp->f_type = strdup("elf multiboot kernel");
467 printf("%s entry at 0x%jx\n", filename,
468 (uintmax_t)ehdr->e_entry);
470 printf("%s ", filename);
473 fp->f_size = __elfN(loadimage)(fp, &ef, dest);
474 if (fp->f_size == 0 || fp->f_addr == 0)
477 /* save exec header as metadata */
478 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
480 /* Load OK, return module pointer */
481 *result = (struct preloaded_file *)fp;
484 fp->f_kernphys_relocatable = multiboot || is_kernphys_relocatable(&ef);
487 fp->f_tg_kernel_support = is_tg_kernel_support(fp, &ef);
499 #ifdef LOADER_VERIEXEC_VECTX
500 if (!err && ef.vctx) {
503 verror = vectx_close(ef.vctx, VE_MUST, __func__);
516 * With the file (fd) open on the image, and (ehdr) containing
517 * the Elf header, load the image at (off)
520 __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, uint64_t off)
525 Elf_Phdr *phdr, *php;
529 vm_offset_t firstaddr;
530 vm_offset_t lastaddr;
543 Elf_Addr p_start, p_end;
548 firstaddr = lastaddr = 0;
553 if (ehdr->e_type == ET_EXEC) {
555 #if defined(__i386__) || defined(__amd64__)
556 #if __ELF_WORD_SIZE == 64
557 /* x86_64 relocates after locore */
558 off = - (off & 0xffffffffff000000ull);
560 /* i386 relocates after locore */
561 off = - (off & 0xff000000u);
563 #elif defined(__powerpc__)
565 * On the purely virtual memory machines like e500, the kernel
566 * is linked against its final VA range, which is most often
567 * not available at the loader stage, but only after kernel
568 * initializes and completes its VM settings. In such cases we
569 * cannot use p_vaddr field directly to load ELF segments, but
570 * put them at some 'load-time' locations.
572 if (off & 0xf0000000u) {
573 off = -(off & 0xf0000000u);
575 * XXX the physical load address should not be
576 * hardcoded. Note that the Book-E kernel assumes that
577 * it's loaded at a 16MB boundary for now...
581 ehdr->e_entry += off;
583 printf("Converted entry 0x%jx\n", (uintmax_t)ehdr->e_entry);
585 #elif defined(__arm__) && !defined(EFI)
587 * The elf headers in arm kernels specify virtual addresses in
588 * all header fields, even the ones that should be physical
589 * addresses. We assume the entry point is in the first page,
590 * and masking the page offset will leave us with the virtual
591 * address the kernel was linked at. We subtract that from the
592 * load offset, making 'off' into the value which, when added
593 * to a virtual address in an elf header, translates it to a
594 * physical address. We do the va->pa conversion on the entry
595 * point address in the header now, so that later we can launch
596 * the kernel by just jumping to that address.
598 * When booting from UEFI the copyin and copyout functions
599 * handle adjusting the location relative to the first virtual
600 * address. Because of this there is no need to adjust the
601 * offset or entry point address as these will both be handled
604 off -= ehdr->e_entry & ~PAGE_MASK;
605 ehdr->e_entry += off;
607 printf("ehdr->e_entry 0x%jx, va<->pa off %llx\n",
608 (uintmax_t)ehdr->e_entry, off);
611 off = 0; /* other archs use direct mapped kernels */
617 __elfN(relocation_offset) = off;
619 if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
620 printf("elf" __XSTRING(__ELF_WORD_SIZE)
621 "_loadimage: program header not within first page\n");
624 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
626 for (i = 0; i < ehdr->e_phnum; i++) {
627 if (elf_program_header_convert(ehdr, phdr))
630 /* We want to load PT_LOAD segments only.. */
631 if (phdr[i].p_type != PT_LOAD)
635 printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
636 (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
637 (long)(phdr[i].p_vaddr + off),
638 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
640 if ((phdr[i].p_flags & PF_W) == 0) {
641 printf("text=0x%lx ", (long)phdr[i].p_filesz);
643 printf("data=0x%lx", (long)phdr[i].p_filesz);
644 if (phdr[i].p_filesz < phdr[i].p_memsz)
645 printf("+0x%lx", (long)(phdr[i].p_memsz -
651 if (ef->firstlen > phdr[i].p_offset) {
652 fpcopy = ef->firstlen - phdr[i].p_offset;
653 archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
654 phdr[i].p_vaddr + off, fpcopy);
656 if (phdr[i].p_filesz > fpcopy) {
657 if (kern_pread(VECTX_HANDLE(ef),
658 phdr[i].p_vaddr + off + fpcopy,
659 phdr[i].p_filesz - fpcopy,
660 phdr[i].p_offset + fpcopy) != 0) {
661 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
662 "_loadimage: read failed\n");
666 /* clear space from oversized segments; eg: bss */
667 if (phdr[i].p_filesz < phdr[i].p_memsz) {
669 printf(" (bss: 0x%lx-0x%lx)",
670 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
671 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz -1));
674 kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
675 phdr[i].p_memsz - phdr[i].p_filesz);
681 if (archsw.arch_loadseg != NULL)
682 archsw.arch_loadseg(ehdr, phdr + i, off);
684 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
685 firstaddr = phdr[i].p_vaddr + off;
686 if (lastaddr == 0 || lastaddr <
687 (phdr[i].p_vaddr + off + phdr[i].p_memsz))
688 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
690 lastaddr = roundup(lastaddr, sizeof(long));
693 * Get the section headers. We need this for finding the .ctors
694 * section as well as for loading any symbols. Both may be hard
695 * to do if reading from a .gz file as it involves seeking. I
696 * think the rule is going to have to be that you must strip a
697 * file to remove symbols before gzipping it.
699 chunk = (size_t)ehdr->e_shnum * (size_t)ehdr->e_shentsize;
700 if (chunk == 0 || ehdr->e_shoff == 0)
702 shdr = alloc_pread(VECTX_HANDLE(ef), ehdr->e_shoff, chunk);
704 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
705 "_loadimage: failed to read section headers");
709 for (i = 0; i < ehdr->e_shnum; i++)
710 elf_section_header_convert(ehdr, &shdr[i]);
712 file_addmetadata(fp, MODINFOMD_SHDR, chunk, shdr);
715 * Read the section string table and look for the .ctors section.
716 * We need to tell the kernel where it is so that it can call the
719 chunk = shdr[ehdr->e_shstrndx].sh_size;
721 shstr = alloc_pread(VECTX_HANDLE(ef),
722 shdr[ehdr->e_shstrndx].sh_offset, chunk);
724 for (i = 0; i < ehdr->e_shnum; i++) {
725 if (strcmp(shstr + shdr[i].sh_name,
728 ctors = shdr[i].sh_addr;
729 file_addmetadata(fp, MODINFOMD_CTORS_ADDR,
730 sizeof(ctors), &ctors);
731 size = shdr[i].sh_size;
732 file_addmetadata(fp, MODINFOMD_CTORS_SIZE,
733 sizeof(size), &size);
741 * Now load any symbols.
745 for (i = 0; i < ehdr->e_shnum; i++) {
746 if (shdr[i].sh_type != SHT_SYMTAB)
748 for (j = 0; j < ehdr->e_phnum; j++) {
749 if (phdr[j].p_type != PT_LOAD)
751 if (shdr[i].sh_offset >= phdr[j].p_offset &&
752 (shdr[i].sh_offset + shdr[i].sh_size <=
753 phdr[j].p_offset + phdr[j].p_filesz)) {
754 shdr[i].sh_offset = 0;
759 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
760 continue; /* alread loaded in a PT_LOAD above */
761 /* Save it for loading below */
763 symstrindex = shdr[i].sh_link;
765 if (symtabindex < 0 || symstrindex < 0)
768 /* Ok, committed to a load. */
773 for (i = symtabindex; i >= 0; i = symstrindex) {
777 switch(shdr[i].sh_type) {
778 case SHT_SYMTAB: /* Symbol table */
781 case SHT_STRTAB: /* String table */
789 size = shdr[i].sh_size;
791 archsw.arch_copyin(&size, lastaddr, sizeof(size));
792 lastaddr += sizeof(size);
795 printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
796 (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
798 (uintmax_t)(lastaddr + shdr[i].sh_size));
800 if (i == symstrindex)
802 printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
805 if (VECTX_LSEEK(VECTX_HANDLE(ef), (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
806 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
807 "_loadimage: could not seek for symbols - skipped!");
812 result = archsw.arch_readin(VECTX_HANDLE(ef), lastaddr, shdr[i].sh_size);
813 if (result < 0 || (size_t)result != shdr[i].sh_size) {
814 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
815 "_loadimage: could not read symbols - skipped! "
816 "(%ju != %ju)", (uintmax_t)result,
817 (uintmax_t)shdr[i].sh_size);
822 /* Reset offsets relative to ssym */
823 lastaddr += shdr[i].sh_size;
824 lastaddr = roundup(lastaddr, sizeof(size));
825 if (i == symtabindex)
827 else if (i == symstrindex)
835 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
836 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
841 ret = lastaddr - firstaddr;
842 fp->f_addr = firstaddr;
845 for (i = 0; i < ehdr->e_phnum; i++) {
846 if (phdr[i].p_type == PT_DYNAMIC) {
849 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp),
855 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
858 ndp = php->p_filesz / sizeof(Elf_Dyn);
861 dp = malloc(php->p_filesz);
864 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
867 for (i = 0; i < ndp; i++) {
868 if (dp[i].d_tag == 0)
870 switch (dp[i].d_tag) {
873 (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
877 (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
880 ef->strsz = dp[i].d_un.d_val;
884 (Elf_Sym *)(uintptr_t)(dp[i].d_un.d_ptr + off);
888 (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
891 ef->relsz = dp[i].d_un.d_val;
895 (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
898 ef->relasz = dp[i].d_un.d_val;
904 if (ef->hashtab == NULL || ef->symtab == NULL ||
905 ef->strtab == NULL || ef->strsz == 0)
907 COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
908 COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
909 ef->buckets = ef->hashtab + 2;
910 ef->chains = ef->buckets + ef->nbuckets;
912 if (__elfN(lookup_symbol)(ef, "__start_set_modmetadata_set", &sym,
915 p_start = sym.st_value + ef->off;
916 if (__elfN(lookup_symbol)(ef, "__stop_set_modmetadata_set", &sym,
919 p_end = sym.st_value + ef->off;
921 if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0)
924 if (ef->kernel) /* kernel must not depend on anything */
935 static char invalid_name[] = "bad";
938 fake_modname(const char *name)
944 sp = strrchr(name, '/');
950 ep = strrchr(sp, '.');
952 ep = sp + strlen(sp);
956 ep = invalid_name + sizeof(invalid_name) - 1;
960 fp = malloc(len + 1);
968 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
969 struct mod_metadata64 {
970 int md_version; /* structure version MDTV_* */
971 int md_type; /* type of entry MDT_* */
972 uint64_t md_data; /* specific data */
973 uint64_t md_cval; /* common string label */
976 #if defined(__amd64__) && __ELF_WORD_SIZE == 32
977 struct mod_metadata32 {
978 int md_version; /* structure version MDTV_* */
979 int md_type; /* type of entry MDT_* */
980 uint32_t md_data; /* specific data */
981 uint32_t md_cval; /* common string label */
986 __elfN(load_modmetadata)(struct preloaded_file *fp, uint64_t dest)
990 Elf_Shdr *sh_meta, *shdr = NULL;
991 Elf_Shdr *sh_data[2];
992 char *shstrtab = NULL;
994 Elf_Addr p_start, p_end;
996 bzero(&ef, sizeof(struct elf_file));
999 err = __elfN(load_elf_header)(fp->f_name, &ef);
1003 if (ef.kernel == 1 || ef.ehdr->e_type == ET_EXEC) {
1005 } else if (ef.ehdr->e_type != ET_DYN) {
1010 size = (size_t)ef.ehdr->e_shnum * (size_t)ef.ehdr->e_shentsize;
1011 shdr = alloc_pread(VECTX_HANDLE(&ef), ef.ehdr->e_shoff, size);
1017 /* Load shstrtab. */
1018 shstrtab = alloc_pread(VECTX_HANDLE(&ef), shdr[ef.ehdr->e_shstrndx].sh_offset,
1019 shdr[ef.ehdr->e_shstrndx].sh_size);
1020 if (shstrtab == NULL) {
1021 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1022 "load_modmetadata: unable to load shstrtab\n");
1027 /* Find set_modmetadata_set and data sections. */
1028 sh_data[0] = sh_data[1] = sh_meta = NULL;
1029 for (i = 0, j = 0; i < ef.ehdr->e_shnum; i++) {
1030 if (strcmp(&shstrtab[shdr[i].sh_name],
1031 "set_modmetadata_set") == 0) {
1034 if ((strcmp(&shstrtab[shdr[i].sh_name], ".data") == 0) ||
1035 (strcmp(&shstrtab[shdr[i].sh_name], ".rodata") == 0)) {
1036 sh_data[j++] = &shdr[i];
1039 if (sh_meta == NULL || sh_data[0] == NULL || sh_data[1] == NULL) {
1040 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1041 "load_modmetadata: unable to find set_modmetadata_set or data sections\n");
1046 /* Load set_modmetadata_set into memory */
1047 err = kern_pread(VECTX_HANDLE(&ef), dest, sh_meta->sh_size, sh_meta->sh_offset);
1049 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1050 "load_modmetadata: unable to load set_modmetadata_set: %d\n", err);
1054 p_end = dest + sh_meta->sh_size;
1055 dest += sh_meta->sh_size;
1057 /* Load data sections into memory. */
1058 err = kern_pread(VECTX_HANDLE(&ef), dest, sh_data[0]->sh_size,
1059 sh_data[0]->sh_offset);
1061 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1062 "load_modmetadata: unable to load data: %d\n", err);
1067 * We have to increment the dest, so that the offset is the same into
1068 * both the .rodata and .data sections.
1070 ef.off = -(sh_data[0]->sh_addr - dest);
1071 dest += (sh_data[1]->sh_addr - sh_data[0]->sh_addr);
1073 err = kern_pread(VECTX_HANDLE(&ef), dest, sh_data[1]->sh_size,
1074 sh_data[1]->sh_offset);
1076 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1077 "load_modmetadata: unable to load data: %d\n", err);
1081 err = __elfN(parse_modmetadata)(fp, &ef, p_start, p_end);
1083 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1084 "load_modmetadata: unable to parse metadata: %d\n", err);
1089 if (shstrtab != NULL)
1093 if (ef.firstpage != NULL)
1096 #ifdef LOADER_VERIEXEC_VECTX
1097 if (!err && ef.vctx) {
1100 verror = vectx_close(ef.vctx, VE_MUST, __func__);
1113 __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
1114 Elf_Addr p_start, Elf_Addr p_end)
1116 struct mod_metadata md;
1117 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
1118 struct mod_metadata64 md64;
1119 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
1120 struct mod_metadata32 md32;
1122 struct mod_depend *mdepend;
1123 struct mod_version mver;
1125 int error, modcnt, minfolen;
1131 COPYOUT(p, &v, sizeof(v));
1132 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
1133 if (error == EOPNOTSUPP)
1135 else if (error != 0)
1137 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
1138 COPYOUT(v, &md64, sizeof(md64));
1139 error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
1140 if (error == EOPNOTSUPP) {
1141 md64.md_cval += ef->off;
1142 md64.md_data += ef->off;
1143 } else if (error != 0)
1145 md.md_version = md64.md_version;
1146 md.md_type = md64.md_type;
1147 md.md_cval = (const char *)(uintptr_t)md64.md_cval;
1148 md.md_data = (void *)(uintptr_t)md64.md_data;
1149 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
1150 COPYOUT(v, &md32, sizeof(md32));
1151 error = __elfN(reloc_ptr)(fp, ef, v, &md32, sizeof(md32));
1152 if (error == EOPNOTSUPP) {
1153 md32.md_cval += ef->off;
1154 md32.md_data += ef->off;
1155 } else if (error != 0)
1157 md.md_version = md32.md_version;
1158 md.md_type = md32.md_type;
1159 md.md_cval = (const char *)(uintptr_t)md32.md_cval;
1160 md.md_data = (void *)(uintptr_t)md32.md_data;
1162 COPYOUT(v, &md, sizeof(md));
1163 error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
1164 if (error == EOPNOTSUPP) {
1165 md.md_cval += ef->off;
1166 md.md_data = (void *)((uintptr_t)md.md_data +
1167 (uintptr_t)ef->off);
1168 } else if (error != 0)
1171 p += sizeof(Elf_Addr);
1172 switch(md.md_type) {
1174 if (ef->kernel) /* kernel must not depend on anything */
1176 s = strdupout((vm_offset_t)md.md_cval);
1177 minfolen = sizeof(*mdepend) + strlen(s) + 1;
1178 mdepend = malloc(minfolen);
1179 if (mdepend == NULL)
1181 COPYOUT((vm_offset_t)md.md_data, mdepend,
1183 strcpy((char*)(mdepend + 1), s);
1185 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
1190 s = strdupout((vm_offset_t)md.md_cval);
1191 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
1192 file_addmodule(fp, s, mver.mv_version, NULL);
1199 s = fake_modname(fp->f_name);
1200 file_addmodule(fp, s, 1, NULL);
1206 static unsigned long
1207 elf_hash(const char *name)
1209 const unsigned char *p = (const unsigned char *) name;
1210 unsigned long h = 0;
1213 while (*p != '\0') {
1214 h = (h << 4) + *p++;
1215 if ((g = h & 0xf0000000) != 0)
1222 static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE)
1223 "_lookup_symbol: corrupt symbol table\n";
1225 __elfN(lookup_symbol)(elf_file_t ef, const char* name, Elf_Sym *symp,
1233 if (ef->nbuckets == 0) {
1234 printf(__elfN(bad_symtable));
1238 hash = elf_hash(name);
1239 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
1241 while (symnum != STN_UNDEF) {
1242 if (symnum >= ef->nchains) {
1243 printf(__elfN(bad_symtable));
1247 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
1248 if (sym.st_name == 0) {
1249 printf(__elfN(bad_symtable));
1253 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
1254 if (strcmp(name, strp) == 0) {
1256 if (sym.st_shndx != SHN_UNDEF ||
1257 (sym.st_value != 0 &&
1258 ELF_ST_TYPE(sym.st_info) == type)) {
1265 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
1271 * Apply any intra-module relocations to the value. p is the load address
1272 * of the value and val/len is the value to be modified. This does NOT modify
1273 * the image in-place, because this is done by kern_linker later on.
1275 * Returns EOPNOTSUPP if no relocation method is supplied.
1278 __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
1279 Elf_Addr p, void *val, size_t len)
1287 * The kernel is already relocated, but we still want to apply
1288 * offset adjustments.
1291 return (EOPNOTSUPP);
1293 for (n = 0; n < ef->relsz / sizeof(r); n++) {
1294 COPYOUT(ef->rel + n, &r, sizeof(r));
1296 error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
1297 ef->off, p, val, len);
1301 for (n = 0; n < ef->relasz / sizeof(a); n++) {
1302 COPYOUT(ef->rela + n, &a, sizeof(a));
1304 error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
1305 ef->off, p, val, len);
1314 __elfN(symaddr)(struct elf_file *ef, Elf_Size symidx)
1317 /* Symbol lookup by index not required here. */