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 {
76 static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef,
78 static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef,
79 const char* name, Elf_Sym* sym);
80 static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
81 Elf_Addr p, void *val, size_t len);
82 static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef,
83 Elf_Addr p_start, Elf_Addr p_end);
84 static symaddr_fn __elfN(symaddr);
85 static char *fake_modname(const char *name);
87 const char *__elfN(kerneltype) = "elf kernel";
88 const char *__elfN(moduletype) = "elf module";
90 uint64_t __elfN(relocation_offset) = 0;
92 extern void elf_wrong_field_size(void);
93 #define CONVERT_FIELD(b, f, e) \
94 switch (sizeof((b)->f)) { \
96 (b)->f = e ## 16toh((b)->f); \
99 (b)->f = e ## 32toh((b)->f); \
102 (b)->f = e ## 64toh((b)->f); \
105 /* Force a link time error. */ \
106 elf_wrong_field_size(); \
110 #define CONVERT_SWITCH(h, d, f) \
111 switch ((h)->e_ident[EI_DATA]) { \
123 static int elf_header_convert(Elf_Ehdr *ehdr)
126 * Fixup ELF header endianness.
128 * The Xhdr structure was loaded using block read call to optimize file
129 * accesses. It might happen, that the endianness of the system memory
130 * is different that endianness of the ELF header. Swap fields here to
131 * guarantee that Xhdr always contain valid data regardless of
134 #define HEADER_FIELDS(b, e) \
135 CONVERT_FIELD(b, e_type, e); \
136 CONVERT_FIELD(b, e_machine, e); \
137 CONVERT_FIELD(b, e_version, e); \
138 CONVERT_FIELD(b, e_entry, e); \
139 CONVERT_FIELD(b, e_phoff, e); \
140 CONVERT_FIELD(b, e_shoff, e); \
141 CONVERT_FIELD(b, e_flags, e); \
142 CONVERT_FIELD(b, e_ehsize, e); \
143 CONVERT_FIELD(b, e_phentsize, e); \
144 CONVERT_FIELD(b, e_phnum, e); \
145 CONVERT_FIELD(b, e_shentsize, e); \
146 CONVERT_FIELD(b, e_shnum, e); \
147 CONVERT_FIELD(b, e_shstrndx, e)
149 CONVERT_SWITCH(ehdr, ehdr, HEADER_FIELDS);
156 static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr)
158 #define PROGRAM_HEADER_FIELDS(b, e) \
159 CONVERT_FIELD(b, p_type, e); \
160 CONVERT_FIELD(b, p_flags, e); \
161 CONVERT_FIELD(b, p_offset, e); \
162 CONVERT_FIELD(b, p_vaddr, e); \
163 CONVERT_FIELD(b, p_paddr, e); \
164 CONVERT_FIELD(b, p_filesz, e); \
165 CONVERT_FIELD(b, p_memsz, e); \
166 CONVERT_FIELD(b, p_align, e)
168 CONVERT_SWITCH(ehdr, phdr, PROGRAM_HEADER_FIELDS);
170 #undef PROGRAM_HEADER_FIELDS
175 static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr)
177 #define SECTION_HEADER_FIELDS(b, e) \
178 CONVERT_FIELD(b, sh_name, e); \
179 CONVERT_FIELD(b, sh_type, e); \
180 CONVERT_FIELD(b, sh_link, e); \
181 CONVERT_FIELD(b, sh_info, e); \
182 CONVERT_FIELD(b, sh_flags, e); \
183 CONVERT_FIELD(b, sh_addr, e); \
184 CONVERT_FIELD(b, sh_offset, e); \
185 CONVERT_FIELD(b, sh_size, e); \
186 CONVERT_FIELD(b, sh_addralign, e); \
187 CONVERT_FIELD(b, sh_entsize, e)
189 CONVERT_SWITCH(ehdr, shdr, SECTION_HEADER_FIELDS);
191 #undef SECTION_HEADER_FIELDS
195 #undef CONVERT_SWITCH
199 __elfN(load_elf_header)(char *filename, elf_file_t ef)
206 * Open the image, read and validate the ELF header
208 if (filename == NULL) /* can't handle nameless */
210 if ((ef->fd = open(filename, O_RDONLY)) == -1)
212 ef->firstpage = malloc(PAGE_SIZE);
213 if (ef->firstpage == NULL) {
217 bytes_read = read(ef->fd, ef->firstpage, PAGE_SIZE);
218 ef->firstlen = (size_t)bytes_read;
219 if (bytes_read < 0 || ef->firstlen <= sizeof(Elf_Ehdr)) {
220 err = EFTYPE; /* could be EIO, but may be small file */
223 ehdr = ef->ehdr = (Elf_Ehdr *)ef->firstpage;
226 if (!IS_ELF(*ehdr)) {
231 if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
232 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
233 ehdr->e_ident[EI_VERSION] != EV_CURRENT) /* Version ? */ {
238 err = elf_header_convert(ehdr);
242 if (ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) {
248 #ifdef LOADER_VERIEXEC
249 if (verify_file(ef->fd, filename, bytes_read, VE_MUST) < 0) {
257 if (ef->firstpage != NULL) {
259 ef->firstpage = NULL;
269 * Attempt to load the file (file) as an ELF module. It will be stored at
270 * (dest), and a pointer to a module structure describing the loaded object
271 * will be saved in (result).
274 __elfN(loadfile)(char *filename, uint64_t dest, struct preloaded_file **result)
276 return (__elfN(loadfile_raw)(filename, dest, result, 0));
280 __elfN(loadfile_raw)(char *filename, uint64_t dest,
281 struct preloaded_file **result, int multiboot)
283 struct preloaded_file *fp, *kfp;
289 bzero(&ef, sizeof(struct elf_file));
292 err = __elfN(load_elf_header)(filename, &ef);
299 * Check to see what sort of module we are.
301 kfp = file_findfile(NULL, __elfN(kerneltype));
304 * Kernels can be ET_DYN, so just assume the first loaded object is the
305 * kernel. This assumption will be checked later.
310 if (ef.kernel || ehdr->e_type == ET_EXEC) {
311 /* Looks like a kernel */
313 printf("elf" __XSTRING(__ELF_WORD_SIZE)
314 "_loadfile: kernel already loaded\n");
319 * Calculate destination address based on kernel entrypoint.
321 * For ARM, the destination address is independent of any values
322 * in the elf header (an ARM kernel can be loaded at any 2MB
323 * boundary), so we leave dest set to the value calculated by
324 * archsw.arch_loadaddr() and passed in to this function.
327 if (ehdr->e_type == ET_EXEC)
328 dest = (ehdr->e_entry & ~PAGE_MASK);
330 if ((ehdr->e_entry & ~PAGE_MASK) == 0) {
331 printf("elf" __XSTRING(__ELF_WORD_SIZE)
332 "_loadfile: not a kernel (maybe static binary?)\n");
338 } else if (ehdr->e_type == ET_DYN) {
339 /* Looks like a kld module */
340 if (multiboot != 0) {
341 printf("elf" __XSTRING(__ELF_WORD_SIZE)
342 "_loadfile: can't load module as multiboot\n");
347 printf("elf" __XSTRING(__ELF_WORD_SIZE)
348 "_loadfile: can't load module before kernel\n");
352 if (strcmp(__elfN(kerneltype), kfp->f_type)) {
353 printf("elf" __XSTRING(__ELF_WORD_SIZE)
354 "_loadfile: can't load module with kernel type '%s'\n",
359 /* Looks OK, got ahead */
367 if (archsw.arch_loadaddr != NULL)
368 dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest);
370 dest = roundup(dest, PAGE_SIZE);
373 * Ok, we think we should handle this.
377 printf("elf" __XSTRING(__ELF_WORD_SIZE)
378 "_loadfile: cannot allocate module info\n");
382 if (ef.kernel == 1 && multiboot == 0)
383 setenv("kernelname", filename, 1);
384 fp->f_name = strdup(filename);
386 fp->f_type = strdup(ef.kernel ?
387 __elfN(kerneltype) : __elfN(moduletype));
389 fp->f_type = strdup("elf multiboot kernel");
393 printf("%s entry at 0x%jx\n", filename,
394 (uintmax_t)ehdr->e_entry);
396 printf("%s ", filename);
399 fp->f_size = __elfN(loadimage)(fp, &ef, dest);
400 if (fp->f_size == 0 || fp->f_addr == 0)
403 /* save exec header as metadata */
404 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
406 /* Load OK, return module pointer */
407 *result = (struct preloaded_file *)fp;
424 * With the file (fd) open on the image, and (ehdr) containing
425 * the Elf header, load the image at (off)
428 __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, uint64_t off)
433 Elf_Phdr *phdr, *php;
437 vm_offset_t firstaddr;
438 vm_offset_t lastaddr;
451 Elf_Addr p_start, p_end;
456 firstaddr = lastaddr = 0;
458 if (ehdr->e_type == ET_EXEC) {
459 #if defined(__i386__) || defined(__amd64__)
460 #if __ELF_WORD_SIZE == 64
461 /* x86_64 relocates after locore */
462 off = - (off & 0xffffffffff000000ull);
464 /* i386 relocates after locore */
465 off = - (off & 0xff000000u);
467 #elif defined(__powerpc__)
469 * On the purely virtual memory machines like e500, the kernel
470 * is linked against its final VA range, which is most often
471 * not available at the loader stage, but only after kernel
472 * initializes and completes its VM settings. In such cases we
473 * cannot use p_vaddr field directly to load ELF segments, but
474 * put them at some 'load-time' locations.
476 if (off & 0xf0000000u) {
477 off = -(off & 0xf0000000u);
479 * XXX the physical load address should not be
480 * hardcoded. Note that the Book-E kernel assumes that
481 * it's loaded at a 16MB boundary for now...
484 ehdr->e_entry += off;
486 printf("Converted entry 0x%jx\n", (uintmax_t)ehdr->e_entry);
490 #elif defined(__arm__) && !defined(EFI)
492 * The elf headers in arm kernels specify virtual addresses in
493 * all header fields, even the ones that should be physical
494 * addresses. We assume the entry point is in the first page,
495 * and masking the page offset will leave us with the virtual
496 * address the kernel was linked at. We subtract that from the
497 * load offset, making 'off' into the value which, when added
498 * to a virtual address in an elf header, translates it to a
499 * physical address. We do the va->pa conversion on the entry
500 * point address in the header now, so that later we can launch
501 * the kernel by just jumping to that address.
503 * When booting from UEFI the copyin and copyout functions
504 * handle adjusting the location relative to the first virtual
505 * address. Because of this there is no need to adjust the
506 * offset or entry point address as these will both be handled
509 off -= ehdr->e_entry & ~PAGE_MASK;
510 ehdr->e_entry += off;
512 printf("ehdr->e_entry 0x%jx, va<->pa off %llx\n",
513 (uintmax_t)ehdr->e_entry, off);
516 off = 0; /* other archs use direct mapped kernels */
522 __elfN(relocation_offset) = off;
524 if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
525 printf("elf" __XSTRING(__ELF_WORD_SIZE)
526 "_loadimage: program header not within first page\n");
529 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
531 for (i = 0; i < ehdr->e_phnum; i++) {
532 if (elf_program_header_convert(ehdr, phdr))
535 /* We want to load PT_LOAD segments only.. */
536 if (phdr[i].p_type != PT_LOAD)
540 printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
541 (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
542 (long)(phdr[i].p_vaddr + off),
543 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
545 if ((phdr[i].p_flags & PF_W) == 0) {
546 printf("text=0x%lx ", (long)phdr[i].p_filesz);
548 printf("data=0x%lx", (long)phdr[i].p_filesz);
549 if (phdr[i].p_filesz < phdr[i].p_memsz)
550 printf("+0x%lx", (long)(phdr[i].p_memsz -
556 if (ef->firstlen > phdr[i].p_offset) {
557 fpcopy = ef->firstlen - phdr[i].p_offset;
558 archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
559 phdr[i].p_vaddr + off, fpcopy);
561 if (phdr[i].p_filesz > fpcopy) {
562 if (kern_pread(ef->fd, phdr[i].p_vaddr + off + fpcopy,
563 phdr[i].p_filesz - fpcopy,
564 phdr[i].p_offset + fpcopy) != 0) {
565 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
566 "_loadimage: read failed\n");
570 /* clear space from oversized segments; eg: bss */
571 if (phdr[i].p_filesz < phdr[i].p_memsz) {
573 printf(" (bss: 0x%lx-0x%lx)",
574 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
575 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz -1));
578 kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
579 phdr[i].p_memsz - phdr[i].p_filesz);
585 if (archsw.arch_loadseg != NULL)
586 archsw.arch_loadseg(ehdr, phdr + i, off);
588 if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
589 firstaddr = phdr[i].p_vaddr + off;
590 if (lastaddr == 0 || lastaddr <
591 (phdr[i].p_vaddr + off + phdr[i].p_memsz))
592 lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
594 lastaddr = roundup(lastaddr, sizeof(long));
597 * Get the section headers. We need this for finding the .ctors
598 * section as well as for loading any symbols. Both may be hard
599 * to do if reading from a .gz file as it involves seeking. I
600 * think the rule is going to have to be that you must strip a
601 * file to remove symbols before gzipping it.
603 chunk = (size_t)ehdr->e_shnum * (size_t)ehdr->e_shentsize;
604 if (chunk == 0 || ehdr->e_shoff == 0)
606 shdr = alloc_pread(ef->fd, ehdr->e_shoff, chunk);
608 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
609 "_loadimage: failed to read section headers");
613 for (i = 0; i < ehdr->e_shnum; i++)
614 elf_section_header_convert(ehdr, &shdr[i]);
616 file_addmetadata(fp, MODINFOMD_SHDR, chunk, shdr);
619 * Read the section string table and look for the .ctors section.
620 * We need to tell the kernel where it is so that it can call the
623 chunk = shdr[ehdr->e_shstrndx].sh_size;
625 shstr = alloc_pread(ef->fd, shdr[ehdr->e_shstrndx].sh_offset,
628 for (i = 0; i < ehdr->e_shnum; i++) {
629 if (strcmp(shstr + shdr[i].sh_name,
632 ctors = shdr[i].sh_addr;
633 file_addmetadata(fp, MODINFOMD_CTORS_ADDR,
634 sizeof(ctors), &ctors);
635 size = shdr[i].sh_size;
636 file_addmetadata(fp, MODINFOMD_CTORS_SIZE,
637 sizeof(size), &size);
645 * Now load any symbols.
649 for (i = 0; i < ehdr->e_shnum; i++) {
650 if (shdr[i].sh_type != SHT_SYMTAB)
652 for (j = 0; j < ehdr->e_phnum; j++) {
653 if (phdr[j].p_type != PT_LOAD)
655 if (shdr[i].sh_offset >= phdr[j].p_offset &&
656 (shdr[i].sh_offset + shdr[i].sh_size <=
657 phdr[j].p_offset + phdr[j].p_filesz)) {
658 shdr[i].sh_offset = 0;
663 if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
664 continue; /* alread loaded in a PT_LOAD above */
665 /* Save it for loading below */
667 symstrindex = shdr[i].sh_link;
669 if (symtabindex < 0 || symstrindex < 0)
672 /* Ok, committed to a load. */
677 for (i = symtabindex; i >= 0; i = symstrindex) {
681 switch(shdr[i].sh_type) {
682 case SHT_SYMTAB: /* Symbol table */
685 case SHT_STRTAB: /* String table */
693 size = shdr[i].sh_size;
694 #if defined(__powerpc__)
695 #if __ELF_WORD_SIZE == 64
696 size = htobe64(size);
698 size = htobe32(size);
702 archsw.arch_copyin(&size, lastaddr, sizeof(size));
703 lastaddr += sizeof(size);
706 printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
707 (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
709 (uintmax_t)(lastaddr + shdr[i].sh_size));
711 if (i == symstrindex)
713 printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
716 if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
717 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
718 "_loadimage: could not seek for symbols - skipped!");
723 result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
724 if (result < 0 || (size_t)result != shdr[i].sh_size) {
725 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
726 "_loadimage: could not read symbols - skipped! "
727 "(%ju != %ju)", (uintmax_t)result,
728 (uintmax_t)shdr[i].sh_size);
733 /* Reset offsets relative to ssym */
734 lastaddr += shdr[i].sh_size;
735 lastaddr = roundup(lastaddr, sizeof(size));
736 if (i == symtabindex)
738 else if (i == symstrindex)
746 #if defined(__powerpc__)
747 /* On PowerPC we always need to provide BE data to the kernel */
748 #if __ELF_WORD_SIZE == 64
749 ssym = htobe64((uint64_t)ssym);
750 esym = htobe64((uint64_t)esym);
752 ssym = htobe32((uint32_t)ssym);
753 esym = htobe32((uint32_t)esym);
757 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
758 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
763 ret = lastaddr - firstaddr;
764 fp->f_addr = firstaddr;
767 for (i = 0; i < ehdr->e_phnum; i++) {
768 if (phdr[i].p_type == PT_DYNAMIC) {
771 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp),
777 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
780 ndp = php->p_filesz / sizeof(Elf_Dyn);
783 dp = malloc(php->p_filesz);
786 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
789 for (i = 0; i < ndp; i++) {
790 if (dp[i].d_tag == 0)
792 switch (dp[i].d_tag) {
795 (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
799 (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
802 ef->strsz = dp[i].d_un.d_val;
806 (Elf_Sym *)(uintptr_t)(dp[i].d_un.d_ptr + off);
810 (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
813 ef->relsz = dp[i].d_un.d_val;
817 (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
820 ef->relasz = dp[i].d_un.d_val;
826 if (ef->hashtab == NULL || ef->symtab == NULL ||
827 ef->strtab == NULL || ef->strsz == 0)
829 COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
830 COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
831 ef->buckets = ef->hashtab + 2;
832 ef->chains = ef->buckets + ef->nbuckets;
834 if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set",
837 p_start = sym.st_value + ef->off;
838 if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set",
841 p_end = sym.st_value + ef->off;
843 if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0)
846 if (ef->kernel) /* kernel must not depend on anything */
857 static char invalid_name[] = "bad";
860 fake_modname(const char *name)
866 sp = strrchr(name, '/');
871 ep = strrchr(name, '.');
875 ep = invalid_name + sizeof(invalid_name) - 1;
878 ep = name + strlen(name);
880 fp = malloc(len + 1);
888 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
889 struct mod_metadata64 {
890 int md_version; /* structure version MDTV_* */
891 int md_type; /* type of entry MDT_* */
892 uint64_t md_data; /* specific data */
893 uint64_t md_cval; /* common string label */
896 #if defined(__amd64__) && __ELF_WORD_SIZE == 32
897 struct mod_metadata32 {
898 int md_version; /* structure version MDTV_* */
899 int md_type; /* type of entry MDT_* */
900 uint32_t md_data; /* specific data */
901 uint32_t md_cval; /* common string label */
906 __elfN(load_modmetadata)(struct preloaded_file *fp, uint64_t dest)
910 Elf_Shdr *sh_meta, *shdr = NULL;
911 Elf_Shdr *sh_data[2];
912 char *shstrtab = NULL;
914 Elf_Addr p_start, p_end;
916 bzero(&ef, sizeof(struct elf_file));
919 err = __elfN(load_elf_header)(fp->f_name, &ef);
923 if (ef.kernel == 1 || ef.ehdr->e_type == ET_EXEC) {
925 } else if (ef.ehdr->e_type != ET_DYN) {
930 size = (size_t)ef.ehdr->e_shnum * (size_t)ef.ehdr->e_shentsize;
931 shdr = alloc_pread(ef.fd, ef.ehdr->e_shoff, size);
938 shstrtab = alloc_pread(ef.fd, shdr[ef.ehdr->e_shstrndx].sh_offset,
939 shdr[ef.ehdr->e_shstrndx].sh_size);
940 if (shstrtab == NULL) {
941 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
942 "load_modmetadata: unable to load shstrtab\n");
947 /* Find set_modmetadata_set and data sections. */
948 sh_data[0] = sh_data[1] = sh_meta = NULL;
949 for (i = 0, j = 0; i < ef.ehdr->e_shnum; i++) {
950 if (strcmp(&shstrtab[shdr[i].sh_name],
951 "set_modmetadata_set") == 0) {
954 if ((strcmp(&shstrtab[shdr[i].sh_name], ".data") == 0) ||
955 (strcmp(&shstrtab[shdr[i].sh_name], ".rodata") == 0)) {
956 sh_data[j++] = &shdr[i];
959 if (sh_meta == NULL || sh_data[0] == NULL || sh_data[1] == NULL) {
960 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
961 "load_modmetadata: unable to find set_modmetadata_set or data sections\n");
966 /* Load set_modmetadata_set into memory */
967 err = kern_pread(ef.fd, dest, sh_meta->sh_size, sh_meta->sh_offset);
969 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
970 "load_modmetadata: unable to load set_modmetadata_set: %d\n", err);
974 p_end = dest + sh_meta->sh_size;
975 dest += sh_meta->sh_size;
977 /* Load data sections into memory. */
978 err = kern_pread(ef.fd, dest, sh_data[0]->sh_size,
979 sh_data[0]->sh_offset);
981 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
982 "load_modmetadata: unable to load data: %d\n", err);
987 * We have to increment the dest, so that the offset is the same into
988 * both the .rodata and .data sections.
990 ef.off = -(sh_data[0]->sh_addr - dest);
991 dest += (sh_data[1]->sh_addr - sh_data[0]->sh_addr);
993 err = kern_pread(ef.fd, dest, sh_data[1]->sh_size,
994 sh_data[1]->sh_offset);
996 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
997 "load_modmetadata: unable to load data: %d\n", err);
1001 err = __elfN(parse_modmetadata)(fp, &ef, p_start, p_end);
1003 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
1004 "load_modmetadata: unable to parse metadata: %d\n", err);
1009 if (shstrtab != NULL)
1013 if (ef.firstpage != NULL)
1021 __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
1022 Elf_Addr p_start, Elf_Addr p_end)
1024 struct mod_metadata md;
1025 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
1026 struct mod_metadata64 md64;
1027 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
1028 struct mod_metadata32 md32;
1030 struct mod_depend *mdepend;
1031 struct mod_version mver;
1033 int error, modcnt, minfolen;
1039 COPYOUT(p, &v, sizeof(v));
1040 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
1041 if (error == EOPNOTSUPP)
1043 else if (error != 0)
1045 #if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
1046 COPYOUT(v, &md64, sizeof(md64));
1047 error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
1048 if (error == EOPNOTSUPP) {
1049 md64.md_cval += ef->off;
1050 md64.md_data += ef->off;
1051 } else if (error != 0)
1053 md.md_version = md64.md_version;
1054 md.md_type = md64.md_type;
1055 md.md_cval = (const char *)(uintptr_t)md64.md_cval;
1056 md.md_data = (void *)(uintptr_t)md64.md_data;
1057 #elif defined(__amd64__) && __ELF_WORD_SIZE == 32
1058 COPYOUT(v, &md32, sizeof(md32));
1059 error = __elfN(reloc_ptr)(fp, ef, v, &md32, sizeof(md32));
1060 if (error == EOPNOTSUPP) {
1061 md32.md_cval += ef->off;
1062 md32.md_data += ef->off;
1063 } else if (error != 0)
1065 md.md_version = md32.md_version;
1066 md.md_type = md32.md_type;
1067 md.md_cval = (const char *)(uintptr_t)md32.md_cval;
1068 md.md_data = (void *)(uintptr_t)md32.md_data;
1070 COPYOUT(v, &md, sizeof(md));
1071 error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
1072 if (error == EOPNOTSUPP) {
1073 md.md_cval += ef->off;
1074 md.md_data = (void *)((uintptr_t)md.md_data +
1075 (uintptr_t)ef->off);
1076 } else if (error != 0)
1079 p += sizeof(Elf_Addr);
1080 switch(md.md_type) {
1082 if (ef->kernel) /* kernel must not depend on anything */
1084 s = strdupout((vm_offset_t)md.md_cval);
1085 minfolen = sizeof(*mdepend) + strlen(s) + 1;
1086 mdepend = malloc(minfolen);
1087 if (mdepend == NULL)
1089 COPYOUT((vm_offset_t)md.md_data, mdepend,
1091 strcpy((char*)(mdepend + 1), s);
1093 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
1098 s = strdupout((vm_offset_t)md.md_cval);
1099 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
1100 file_addmodule(fp, s, mver.mv_version, NULL);
1107 s = fake_modname(fp->f_name);
1108 file_addmodule(fp, s, 1, NULL);
1114 static unsigned long
1115 elf_hash(const char *name)
1117 const unsigned char *p = (const unsigned char *) name;
1118 unsigned long h = 0;
1121 while (*p != '\0') {
1122 h = (h << 4) + *p++;
1123 if ((g = h & 0xf0000000) != 0)
1130 static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE)
1131 "_lookup_symbol: corrupt symbol table\n";
1133 __elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef,
1134 const char* name, Elf_Sym *symp)
1141 hash = elf_hash(name);
1142 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
1144 while (symnum != STN_UNDEF) {
1145 if (symnum >= ef->nchains) {
1146 printf(__elfN(bad_symtable));
1150 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
1151 if (sym.st_name == 0) {
1152 printf(__elfN(bad_symtable));
1156 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
1157 if (strcmp(name, strp) == 0) {
1159 if (sym.st_shndx != SHN_UNDEF ||
1160 (sym.st_value != 0 &&
1161 ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
1168 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
1174 * Apply any intra-module relocations to the value. p is the load address
1175 * of the value and val/len is the value to be modified. This does NOT modify
1176 * the image in-place, because this is done by kern_linker later on.
1178 * Returns EOPNOTSUPP if no relocation method is supplied.
1181 __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
1182 Elf_Addr p, void *val, size_t len)
1190 * The kernel is already relocated, but we still want to apply
1191 * offset adjustments.
1194 return (EOPNOTSUPP);
1196 for (n = 0; n < ef->relsz / sizeof(r); n++) {
1197 COPYOUT(ef->rel + n, &r, sizeof(r));
1199 error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
1200 ef->off, p, val, len);
1204 for (n = 0; n < ef->relasz / sizeof(a); n++) {
1205 COPYOUT(ef->rela + n, &a, sizeof(a));
1207 error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
1208 ef->off, p, val, len);
1217 __elfN(symaddr)(struct elf_file *ef, Elf_Size symidx)
1220 /* Symbol lookup by index not required here. */