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/param.h>
29 #include <sys/endian.h>
31 #include <sys/linker.h>
32 #include <sys/module.h>
33 #include <sys/stdint.h>
35 #include <machine/elf.h>
38 #include <sys/link_elf.h>
40 #include "bootstrap.h"
42 #define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
44 #if defined(__i386__) && __ELF_WORD_SIZE == 64
47 #define ELF_TARG_CLASS ELFCLASS64
48 #define ELF_TARG_MACH EM_X86_64
51 typedef struct elf_file {
71 #ifdef LOADER_VERIEXEC_VECTX
76 #ifdef LOADER_VERIEXEC_VECTX
77 #define VECTX_HANDLE(ef) (ef)->vctx
79 #define VECTX_HANDLE(ef) (ef)->fd
82 static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef,
84 static int __elfN(lookup_symbol)(elf_file_t ef, const char* name,
85 Elf_Sym *sym, unsigned char type);
86 static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
87 Elf_Addr p, void *val, size_t len);
88 static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef,
89 Elf_Addr p_start, Elf_Addr p_end);
90 static symaddr_fn __elfN(symaddr);
91 static char *fake_modname(const char *name);
93 const char *__elfN(kerneltype) = "elf kernel";
94 const char *__elfN(moduletype) = "elf module";
96 uint64_t __elfN(relocation_offset) = 0;
99 extern void elf_wrong_field_size(void);
100 #define CONVERT_FIELD(b, f, e) \
101 switch (sizeof((b)->f)) { \
103 (b)->f = e ## 16toh((b)->f); \
106 (b)->f = e ## 32toh((b)->f); \
109 (b)->f = e ## 64toh((b)->f); \
112 /* Force a link time error. */ \
113 elf_wrong_field_size(); \
117 #define CONVERT_SWITCH(h, d, f) \
118 switch ((h)->e_ident[EI_DATA]) { \
130 static int elf_header_convert(Elf_Ehdr *ehdr)
133 * Fixup ELF header endianness.
135 * The Xhdr structure was loaded using block read call to optimize file
136 * accesses. It might happen, that the endianness of the system memory
137 * is different that endianness of the ELF header. Swap fields here to
138 * guarantee that Xhdr always contain valid data regardless of
141 #define HEADER_FIELDS(b, e) \
142 CONVERT_FIELD(b, e_type, e); \
143 CONVERT_FIELD(b, e_machine, e); \
144 CONVERT_FIELD(b, e_version, e); \
145 CONVERT_FIELD(b, e_entry, e); \
146 CONVERT_FIELD(b, e_phoff, e); \
147 CONVERT_FIELD(b, e_shoff, e); \
148 CONVERT_FIELD(b, e_flags, e); \
149 CONVERT_FIELD(b, e_ehsize, e); \
150 CONVERT_FIELD(b, e_phentsize, e); \
151 CONVERT_FIELD(b, e_phnum, e); \
152 CONVERT_FIELD(b, e_shentsize, e); \
153 CONVERT_FIELD(b, e_shnum, e); \
154 CONVERT_FIELD(b, e_shstrndx, e)
156 CONVERT_SWITCH(ehdr, ehdr, HEADER_FIELDS);
163 static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr)
165 #define PROGRAM_HEADER_FIELDS(b, e) \
166 CONVERT_FIELD(b, p_type, e); \
167 CONVERT_FIELD(b, p_flags, e); \
168 CONVERT_FIELD(b, p_offset, e); \
169 CONVERT_FIELD(b, p_vaddr, e); \
170 CONVERT_FIELD(b, p_paddr, e); \
171 CONVERT_FIELD(b, p_filesz, e); \
172 CONVERT_FIELD(b, p_memsz, e); \
173 CONVERT_FIELD(b, p_align, e)
175 CONVERT_SWITCH(ehdr, phdr, PROGRAM_HEADER_FIELDS);
177 #undef PROGRAM_HEADER_FIELDS
182 static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr)
184 #define SECTION_HEADER_FIELDS(b, e) \
185 CONVERT_FIELD(b, sh_name, e); \
186 CONVERT_FIELD(b, sh_type, e); \
187 CONVERT_FIELD(b, sh_link, e); \
188 CONVERT_FIELD(b, sh_info, e); \
189 CONVERT_FIELD(b, sh_flags, e); \
190 CONVERT_FIELD(b, sh_addr, e); \
191 CONVERT_FIELD(b, sh_offset, e); \
192 CONVERT_FIELD(b, sh_size, e); \
193 CONVERT_FIELD(b, sh_addralign, e); \
194 CONVERT_FIELD(b, sh_entsize, e)
196 CONVERT_SWITCH(ehdr, shdr, SECTION_HEADER_FIELDS);
198 #undef SECTION_HEADER_FIELDS
202 #undef CONVERT_SWITCH
205 static int elf_header_convert(Elf_Ehdr *ehdr)
210 static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr)
215 static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr)
223 is_kernphys_relocatable(elf_file_t ef)
227 return (__elfN(lookup_symbol)(ef, "kernphys", &sym, STT_OBJECT) == 0);
233 is_tg_kernel_support(struct preloaded_file *fp, elf_file_t ef)
236 Elf_Addr p_start, p_end, v, p;
240 if (__elfN(lookup_symbol)(ef, "__start_set_vt_drv_set", &sym, STT_NOTYPE) != 0)
242 p_start = sym.st_value + ef->off;
243 if (__elfN(lookup_symbol)(ef, "__stop_set_vt_drv_set", &sym, STT_NOTYPE) != 0)
245 p_end = sym.st_value + ef->off;
248 * Walk through vt_drv_set, each vt driver structure starts with
249 * static 16 chars for driver name. If we have "vbefb", return true.
251 for (p = p_start; p < p_end; p += sizeof(Elf_Addr)) {
252 COPYOUT(p, &v, sizeof(v));
254 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
255 if (error == EOPNOTSUPP)
259 COPYOUT(v, &vd_name, sizeof(vd_name));
260 if (strncmp(vd_name, "vbefb", sizeof(vd_name)) == 0)
269 __elfN(load_elf_header)(char *filename, elf_file_t ef)
276 * Open the image, read and validate the ELF header
278 if (filename == NULL) /* can't handle nameless */
280 if ((ef->fd = open(filename, O_RDONLY)) == -1)
282 ef->firstpage = malloc(PAGE_SIZE);
283 if (ef->firstpage == NULL) {
288 #ifdef LOADER_VERIEXEC_VECTX
292 ef->vctx = vectx_open(ef->fd, filename, 0L, NULL, &verror, __func__);
294 printf("Unverified %s: %s\n", filename, ve_error_get());
301 bytes_read = VECTX_READ(VECTX_HANDLE(ef), ef->firstpage, PAGE_SIZE);
302 ef->firstlen = (size_t)bytes_read;
303 if (bytes_read < 0 || ef->firstlen <= sizeof(Elf_Ehdr)) {
304 err = EFTYPE; /* could be EIO, but may be small file */
307 ehdr = ef->ehdr = (Elf_Ehdr *)ef->firstpage;
310 if (!IS_ELF(*ehdr)) {
315 if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
316 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
317 ehdr->e_ident[EI_VERSION] != EV_CURRENT) /* Version ? */ {
322 err = elf_header_convert(ehdr);
326 if (ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) {
332 #if defined(LOADER_VERIEXEC) && !defined(LOADER_VERIEXEC_VECTX)
333 if (verify_file(ef->fd, filename, bytes_read, VE_MUST, __func__) < 0) {
341 if (ef->firstpage != NULL) {
343 ef->firstpage = NULL;
346 #ifdef LOADER_VERIEXEC_VECTX
356 * Attempt to load the file (file) as an ELF module. It will be stored at
357 * (dest), and a pointer to a module structure describing the loaded object
358 * will be saved in (result).
361 __elfN(loadfile)(char *filename, uint64_t dest, struct preloaded_file **result)
363 return (__elfN(loadfile_raw)(filename, dest, result, 0));
367 __elfN(loadfile_raw)(char *filename, uint64_t dest,
368 struct preloaded_file **result, int multiboot)
370 struct preloaded_file *fp, *kfp;
376 bzero(&ef, sizeof(struct elf_file));
379 err = __elfN(load_elf_header)(filename, &ef);
386 * Check to see what sort of module we are.
388 kfp = file_findfile(NULL, __elfN(kerneltype));
391 * Kernels can be ET_DYN, so just assume the first loaded object is the
392 * kernel. This assumption will be checked later.
397 if (ef.kernel || ehdr->e_type == ET_EXEC) {
398 /* Looks like a kernel */
400 printf("elf" __XSTRING(__ELF_WORD_SIZE)
401 "_loadfile: kernel already loaded\n");
406 * Calculate destination address based on kernel entrypoint.
408 * For ARM, the destination address is independent of any values
409 * in the elf header (an ARM kernel can be loaded at any 2MB
410 * boundary), so we leave dest set to the value calculated by
411 * archsw.arch_loadaddr() and passed in to this function.
414 if (ehdr->e_type == ET_EXEC)
415 dest = (ehdr->e_entry & ~PAGE_MASK);
417 if ((ehdr->e_entry & ~PAGE_MASK) == 0) {
418 printf("elf" __XSTRING(__ELF_WORD_SIZE)
419 "_loadfile: not a kernel (maybe static binary?)\n");
425 } else if (ehdr->e_type == ET_DYN) {
426 /* Looks like a kld module */
427 if (multiboot != 0) {
428 printf("elf" __XSTRING(__ELF_WORD_SIZE)
429 "_loadfile: can't load module as multiboot\n");
434 printf("elf" __XSTRING(__ELF_WORD_SIZE)
435 "_loadfile: can't load module before kernel\n");
439 if (strcmp(__elfN(kerneltype), kfp->f_type)) {
440 printf("elf" __XSTRING(__ELF_WORD_SIZE)
441 "_loadfile: can't load module with kernel type '%s'\n",
446 /* Looks OK, got ahead */
454 if (archsw.arch_loadaddr != NULL)
455 dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest);
457 dest = roundup(dest, PAGE_SIZE);
460 * Ok, we think we should handle this.
464 printf("elf" __XSTRING(__ELF_WORD_SIZE)
465 "_loadfile: cannot allocate module info\n");
469 if (ef.kernel == 1 && multiboot == 0)
470 setenv("kernelname", filename, 1);
471 fp->f_name = strdup(filename);
473 fp->f_type = strdup(ef.kernel ?
474 __elfN(kerneltype) : __elfN(moduletype));
476 fp->f_type = strdup("elf multiboot kernel");
478 if (module_verbose >= MODULE_VERBOSE_FULL) {
480 printf("%s entry at 0x%jx\n", filename,
481 (uintmax_t)ehdr->e_entry);
482 } else if (module_verbose > MODULE_VERBOSE_SILENT)
483 printf("%s ", filename);
485 fp->f_size = __elfN(loadimage)(fp, &ef, dest);
486 if (fp->f_size == 0 || fp->f_addr == 0)
489 /* save exec header as metadata */
490 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
492 /* Load OK, return module pointer */
493 *result = (struct preloaded_file *)fp;
496 fp->f_kernphys_relocatable = multiboot || is_kernphys_relocatable(&ef);
499 fp->f_tg_kernel_support = is_tg_kernel_support(fp, &ef);
511 #ifdef LOADER_VERIEXEC_VECTX
512 if (!err && ef.vctx) {
515 verror = vectx_close(ef.vctx, VE_MUST, __func__);
528 * With the file (fd) open on the image, and (ehdr) containing
529 * the Elf header, load the image at (off)
532 __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, uint64_t off)
537 Elf_Phdr *phdr, *php;
541 vm_offset_t firstaddr;
542 vm_offset_t lastaddr;
555 Elf_Addr p_start, p_end;
560 firstaddr = lastaddr = 0;
565 if (ehdr->e_type == ET_EXEC) {
567 #if defined(__i386__) || defined(__amd64__)
568 #if __ELF_WORD_SIZE == 64
569 /* x86_64 relocates after locore */
570 off = - (off & 0xffffffffff000000ull);
572 /* i386 relocates after locore */
573 off = - (off & 0xff000000u);
575 #elif defined(__powerpc__)
577 * On the purely virtual memory machines like e500, the kernel
578 * is linked against its final VA range, which is most often
579 * not available at the loader stage, but only after kernel
580 * initializes and completes its VM settings. In such cases we
581 * cannot use p_vaddr field directly to load ELF segments, but
582 * put them at some 'load-time' locations.
584 if (off & 0xf0000000u) {
585 off = -(off & 0xf0000000u);
587 * XXX the physical load address should not be
588 * hardcoded. Note that the Book-E kernel assumes that
589 * it's loaded at a 16MB boundary for now...
593 ehdr->e_entry += off;
594 if (module_verbose >= MODULE_VERBOSE_FULL)
595 printf("Converted entry 0x%jx\n",
596 (uintmax_t)ehdr->e_entry);
598 #elif defined(__arm__) && !defined(EFI)
600 * The elf headers in arm kernels specify virtual addresses in
601 * all header fields, even the ones that should be physical
602 * addresses. We assume the entry point is in the first page,
603 * and masking the page offset will leave us with the virtual
604 * address the kernel was linked at. We subtract that from the
605 * load offset, making 'off' into the value which, when added
606 * to a virtual address in an elf header, translates it to a
607 * physical address. We do the va->pa conversion on the entry
608 * point address in the header now, so that later we can launch
609 * the kernel by just jumping to that address.
611 * When booting from UEFI the copyin and copyout functions
612 * handle adjusting the location relative to the first virtual
613 * address. Because of this there is no need to adjust the
614 * offset or entry point address as these will both be handled
617 off -= ehdr->e_entry & ~PAGE_MASK;
618 ehdr->e_entry += off;
619 if (module_verbose >= MODULE_VERBOSE_FULL)
620 printf("ehdr->e_entry 0x%jx, va<->pa off %llx\n",
621 (uintmax_t)ehdr->e_entry, off);
623 off = 0; /* other archs use direct mapped kernels */
629 __elfN(relocation_offset) = off;
631 if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
632 printf("elf" __XSTRING(__ELF_WORD_SIZE)
633 "_loadimage: program header not within first page\n");
636 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
638 for (i = 0; i < ehdr->e_phnum; i++) {
639 if (elf_program_header_convert(ehdr, phdr))
642 /* We want to load PT_LOAD segments only.. */
643 if (phdr[i].p_type != PT_LOAD)
646 if (module_verbose >= MODULE_VERBOSE_FULL) {
647 printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
648 (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
649 (long)(phdr[i].p_vaddr + off),
650 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
651 } else if (module_verbose > MODULE_VERBOSE_SILENT) {
652 if ((phdr[i].p_flags & PF_W) == 0) {
653 printf("text=0x%lx ", (long)phdr[i].p_filesz);
655 printf("data=0x%lx", (long)phdr[i].p_filesz);
656 if (phdr[i].p_filesz < phdr[i].p_memsz)
657 printf("+0x%lx", (long)(phdr[i].p_memsz -
663 if (ef->firstlen > phdr[i].p_offset) {
664 fpcopy = ef->firstlen - phdr[i].p_offset;
665 archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
666 phdr[i].p_vaddr + off, fpcopy);
668 if (phdr[i].p_filesz > fpcopy) {
669 if (kern_pread(VECTX_HANDLE(ef),
670 phdr[i].p_vaddr + off + fpcopy,
671 phdr[i].p_filesz - fpcopy,
672 phdr[i].p_offset + fpcopy) != 0) {
673 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
674 "_loadimage: read failed\n");
678 /* clear space from oversized segments; eg: bss */
679 if (phdr[i].p_filesz < phdr[i].p_memsz) {
680 if (module_verbose >= MODULE_VERBOSE_FULL) {
681 printf(" (bss: 0x%lx-0x%lx)",
682 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
683 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz -1));
685 kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
686 phdr[i].p_memsz - phdr[i].p_filesz);
688 if (module_verbose >= MODULE_VERBOSE_FULL)
691 if (archsw.arch_loadseg != NULL)
692 archsw.arch_loadseg(ehdr, phdr + i, off);
694 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
695 firstaddr = phdr[i].p_vaddr + off;
696 if (lastaddr == 0 || lastaddr <
697 (phdr[i].p_vaddr + off + phdr[i].p_memsz))
698 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
700 lastaddr = roundup(lastaddr, sizeof(long));
703 * Get the section headers. We need this for finding the .ctors
704 * section as well as for loading any symbols. Both may be hard
705 * to do if reading from a .gz file as it involves seeking. I
706 * think the rule is going to have to be that you must strip a
707 * file to remove symbols before gzipping it.
709 chunk = (size_t)ehdr->e_shnum * (size_t)ehdr->e_shentsize;
710 if (chunk == 0 || ehdr->e_shoff == 0)
712 shdr = alloc_pread(VECTX_HANDLE(ef), ehdr->e_shoff, chunk);
714 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
715 "_loadimage: failed to read section headers");
719 for (i = 0; i < ehdr->e_shnum; i++)
720 elf_section_header_convert(ehdr, &shdr[i]);
722 file_addmetadata(fp, MODINFOMD_SHDR, chunk, shdr);
725 * Read the section string table and look for the .ctors section.
726 * We need to tell the kernel where it is so that it can call the
729 chunk = shdr[ehdr->e_shstrndx].sh_size;
731 shstr = alloc_pread(VECTX_HANDLE(ef),
732 shdr[ehdr->e_shstrndx].sh_offset, chunk);
734 for (i = 0; i < ehdr->e_shnum; i++) {
735 if (strcmp(shstr + shdr[i].sh_name,
738 ctors = shdr[i].sh_addr;
739 file_addmetadata(fp, MODINFOMD_CTORS_ADDR,
740 sizeof(ctors), &ctors);
741 size = shdr[i].sh_size;
742 file_addmetadata(fp, MODINFOMD_CTORS_SIZE,
743 sizeof(size), &size);
751 * Now load any symbols.
755 for (i = 0; i < ehdr->e_shnum; i++) {
756 if (shdr[i].sh_type != SHT_SYMTAB)
758 for (j = 0; j < ehdr->e_phnum; j++) {
759 if (phdr[j].p_type != PT_LOAD)
761 if (shdr[i].sh_offset >= phdr[j].p_offset &&
762 (shdr[i].sh_offset + shdr[i].sh_size <=
763 phdr[j].p_offset + phdr[j].p_filesz)) {
764 shdr[i].sh_offset = 0;
769 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
770 continue; /* alread loaded in a PT_LOAD above */
771 /* Save it for loading below */
773 symstrindex = shdr[i].sh_link;
775 if (symtabindex < 0 || symstrindex < 0)
778 /* Ok, committed to a load. */
779 if (module_verbose >= MODULE_VERBOSE_FULL)
782 for (i = symtabindex; i >= 0; i = symstrindex) {
785 switch(shdr[i].sh_type) {
786 case SHT_SYMTAB: /* Symbol table */
789 case SHT_STRTAB: /* String table */
796 size = shdr[i].sh_size;
798 archsw.arch_copyin(&size, lastaddr, sizeof(size));
799 lastaddr += sizeof(size);
801 if (module_verbose >= MODULE_VERBOSE_FULL) {
802 printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
803 (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
805 (uintmax_t)(lastaddr + shdr[i].sh_size));
806 } else if (module_verbose > MODULE_VERBOSE_SILENT) {
807 if (i == symstrindex)
809 printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
811 if (VECTX_LSEEK(VECTX_HANDLE(ef), (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
812 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
813 "_loadimage: could not seek for symbols - skipped!");
818 result = archsw.arch_readin(VECTX_HANDLE(ef), lastaddr, shdr[i].sh_size);
819 if (result < 0 || (size_t)result != shdr[i].sh_size) {
820 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
821 "_loadimage: could not read symbols - skipped! "
822 "(%ju != %ju)", (uintmax_t)result,
823 (uintmax_t)shdr[i].sh_size);
828 /* Reset offsets relative to ssym */
829 lastaddr += shdr[i].sh_size;
830 lastaddr = roundup(lastaddr, sizeof(size));
831 if (i == symtabindex)
833 else if (i == symstrindex)
837 if (module_verbose >= MODULE_VERBOSE_FULL)
840 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
841 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
844 if (module_verbose > MODULE_VERBOSE_SILENT)
847 ret = lastaddr - firstaddr;
848 fp->f_addr = firstaddr;
851 for (i = 0; i < ehdr->e_phnum; i++) {
852 if (phdr[i].p_type == PT_DYNAMIC) {
855 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp),
861 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
864 ndp = php->p_filesz / sizeof(Elf_Dyn);
867 dp = malloc(php->p_filesz);
870 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
873 for (i = 0; i < ndp; i++) {
874 if (dp[i].d_tag == 0)
876 switch (dp[i].d_tag) {
879 (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
883 (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
886 ef->strsz = dp[i].d_un.d_val;
890 (Elf_Sym *)(uintptr_t)(dp[i].d_un.d_ptr + off);
894 (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
897 ef->relsz = dp[i].d_un.d_val;
901 (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
904 ef->relasz = dp[i].d_un.d_val;
910 if (ef->hashtab == NULL || ef->symtab == NULL ||
911 ef->strtab == NULL || ef->strsz == 0)
913 COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
914 COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
915 ef->buckets = ef->hashtab + 2;
916 ef->chains = ef->buckets + ef->nbuckets;
918 if (__elfN(lookup_symbol)(ef, "__start_set_modmetadata_set", &sym,
921 p_start = sym.st_value + ef->off;
922 if (__elfN(lookup_symbol)(ef, "__stop_set_modmetadata_set", &sym,
925 p_end = sym.st_value + ef->off;
927 if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0)
930 if (ef->kernel) /* kernel must not depend on anything */
941 static char invalid_name[] = "bad";
944 fake_modname(const char *name)
950 sp = strrchr(name, '/');
956 ep = strrchr(sp, '.');
958 ep = sp + strlen(sp);
962 ep = invalid_name + sizeof(invalid_name) - 1;
966 fp = malloc(len + 1);
974 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
975 struct mod_metadata64 {
976 int md_version; /* structure version MDTV_* */
977 int md_type; /* type of entry MDT_* */
978 uint64_t md_data; /* specific data */
979 uint64_t md_cval; /* common string label */
982 #if defined(__amd64__) && __ELF_WORD_SIZE == 32
983 struct mod_metadata32 {
984 int md_version; /* structure version MDTV_* */
985 int md_type; /* type of entry MDT_* */
986 uint32_t md_data; /* specific data */
987 uint32_t md_cval; /* common string label */
992 __elfN(load_modmetadata)(struct preloaded_file *fp, uint64_t dest)
996 Elf_Shdr *sh_meta, *shdr = NULL;
997 Elf_Shdr *sh_data[2];
998 char *shstrtab = NULL;
1000 Elf_Addr p_start, p_end;
1002 bzero(&ef, sizeof(struct elf_file));
1005 err = __elfN(load_elf_header)(fp->f_name, &ef);
1009 if (ef.kernel == 1 || ef.ehdr->e_type == ET_EXEC) {
1011 } else if (ef.ehdr->e_type != ET_DYN) {
1016 size = (size_t)ef.ehdr->e_shnum * (size_t)ef.ehdr->e_shentsize;
1017 shdr = alloc_pread(VECTX_HANDLE(&ef), ef.ehdr->e_shoff, size);
1023 /* Load shstrtab. */
1024 shstrtab = alloc_pread(VECTX_HANDLE(&ef), shdr[ef.ehdr->e_shstrndx].sh_offset,
1025 shdr[ef.ehdr->e_shstrndx].sh_size);
1026 if (shstrtab == NULL) {
1027 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1028 "load_modmetadata: unable to load shstrtab\n");
1033 /* Find set_modmetadata_set and data sections. */
1034 sh_data[0] = sh_data[1] = sh_meta = NULL;
1035 for (i = 0, j = 0; i < ef.ehdr->e_shnum; i++) {
1036 if (strcmp(&shstrtab[shdr[i].sh_name],
1037 "set_modmetadata_set") == 0) {
1040 if ((strcmp(&shstrtab[shdr[i].sh_name], ".data") == 0) ||
1041 (strcmp(&shstrtab[shdr[i].sh_name], ".rodata") == 0)) {
1042 sh_data[j++] = &shdr[i];
1045 if (sh_meta == NULL || sh_data[0] == NULL || sh_data[1] == NULL) {
1046 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1047 "load_modmetadata: unable to find set_modmetadata_set or data sections\n");
1052 /* Load set_modmetadata_set into memory */
1053 err = kern_pread(VECTX_HANDLE(&ef), dest, sh_meta->sh_size, sh_meta->sh_offset);
1055 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1056 "load_modmetadata: unable to load set_modmetadata_set: %d\n", err);
1060 p_end = dest + sh_meta->sh_size;
1061 dest += sh_meta->sh_size;
1063 /* Load data sections into memory. */
1064 err = kern_pread(VECTX_HANDLE(&ef), dest, sh_data[0]->sh_size,
1065 sh_data[0]->sh_offset);
1067 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1068 "load_modmetadata: unable to load data: %d\n", err);
1073 * We have to increment the dest, so that the offset is the same into
1074 * both the .rodata and .data sections.
1076 ef.off = -(sh_data[0]->sh_addr - dest);
1077 dest += (sh_data[1]->sh_addr - sh_data[0]->sh_addr);
1079 err = kern_pread(VECTX_HANDLE(&ef), dest, sh_data[1]->sh_size,
1080 sh_data[1]->sh_offset);
1082 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1083 "load_modmetadata: unable to load data: %d\n", err);
1087 err = __elfN(parse_modmetadata)(fp, &ef, p_start, p_end);
1089 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1090 "load_modmetadata: unable to parse metadata: %d\n", err);
1095 if (shstrtab != NULL)
1099 if (ef.firstpage != NULL)
1102 #ifdef LOADER_VERIEXEC_VECTX
1103 if (!err && ef.vctx) {
1106 verror = vectx_close(ef.vctx, VE_MUST, __func__);
1119 __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
1120 Elf_Addr p_start, Elf_Addr p_end)
1122 struct mod_metadata md;
1123 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
1124 struct mod_metadata64 md64;
1125 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
1126 struct mod_metadata32 md32;
1128 struct mod_depend *mdepend;
1129 struct mod_version mver;
1131 int error, modcnt, minfolen;
1137 COPYOUT(p, &v, sizeof(v));
1138 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
1139 if (error == EOPNOTSUPP)
1141 else if (error != 0)
1143 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
1144 COPYOUT(v, &md64, sizeof(md64));
1145 error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
1146 if (error == EOPNOTSUPP) {
1147 md64.md_cval += ef->off;
1148 md64.md_data += ef->off;
1149 } else if (error != 0)
1151 md.md_version = md64.md_version;
1152 md.md_type = md64.md_type;
1153 md.md_cval = (const char *)(uintptr_t)md64.md_cval;
1154 md.md_data = (void *)(uintptr_t)md64.md_data;
1155 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
1156 COPYOUT(v, &md32, sizeof(md32));
1157 error = __elfN(reloc_ptr)(fp, ef, v, &md32, sizeof(md32));
1158 if (error == EOPNOTSUPP) {
1159 md32.md_cval += ef->off;
1160 md32.md_data += ef->off;
1161 } else if (error != 0)
1163 md.md_version = md32.md_version;
1164 md.md_type = md32.md_type;
1165 md.md_cval = (const char *)(uintptr_t)md32.md_cval;
1166 md.md_data = (void *)(uintptr_t)md32.md_data;
1168 COPYOUT(v, &md, sizeof(md));
1169 error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
1170 if (error == EOPNOTSUPP) {
1171 md.md_cval += ef->off;
1172 md.md_data = (void *)((uintptr_t)md.md_data +
1173 (uintptr_t)ef->off);
1174 } else if (error != 0)
1177 p += sizeof(Elf_Addr);
1178 switch(md.md_type) {
1180 if (ef->kernel) /* kernel must not depend on anything */
1182 s = strdupout((vm_offset_t)md.md_cval);
1183 minfolen = sizeof(*mdepend) + strlen(s) + 1;
1184 mdepend = malloc(minfolen);
1185 if (mdepend == NULL)
1187 COPYOUT((vm_offset_t)md.md_data, mdepend,
1189 strcpy((char*)(mdepend + 1), s);
1191 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
1196 s = strdupout((vm_offset_t)md.md_cval);
1197 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
1198 file_addmodule(fp, s, mver.mv_version, NULL);
1205 s = fake_modname(fp->f_name);
1206 file_addmodule(fp, s, 1, NULL);
1212 static unsigned long
1213 elf_hash(const char *name)
1215 const unsigned char *p = (const unsigned char *) name;
1216 unsigned long h = 0;
1219 while (*p != '\0') {
1220 h = (h << 4) + *p++;
1221 if ((g = h & 0xf0000000) != 0)
1228 static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE)
1229 "_lookup_symbol: corrupt symbol table\n";
1231 __elfN(lookup_symbol)(elf_file_t ef, const char* name, Elf_Sym *symp,
1239 if (ef->nbuckets == 0) {
1240 printf(__elfN(bad_symtable));
1244 hash = elf_hash(name);
1245 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
1247 while (symnum != STN_UNDEF) {
1248 if (symnum >= ef->nchains) {
1249 printf(__elfN(bad_symtable));
1253 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
1254 if (sym.st_name == 0) {
1255 printf(__elfN(bad_symtable));
1259 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
1260 if (strcmp(name, strp) == 0) {
1262 if (sym.st_shndx != SHN_UNDEF && sym.st_value != 0 &&
1263 ELF_ST_TYPE(sym.st_info) == type) {
1270 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
1276 * Apply any intra-module relocations to the value. p is the load address
1277 * of the value and val/len is the value to be modified. This does NOT modify
1278 * the image in-place, because this is done by kern_linker later on.
1280 * Returns EOPNOTSUPP if no relocation method is supplied.
1283 __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
1284 Elf_Addr p, void *val, size_t len)
1292 * The kernel is already relocated, but we still want to apply
1293 * offset adjustments.
1296 return (EOPNOTSUPP);
1298 for (n = 0; n < ef->relsz / sizeof(r); n++) {
1299 COPYOUT(ef->rel + n, &r, sizeof(r));
1301 error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
1302 ef->off, p, val, len);
1306 for (n = 0; n < ef->relasz / sizeof(a); n++) {
1307 COPYOUT(ef->rela + n, &a, sizeof(a));
1309 error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
1310 ef->off, p, val, len);
1319 __elfN(symaddr)(struct elf_file *ef, Elf_Size symidx)
1322 /* Symbol lookup by index not required here. */