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, 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);
85 const char *__elfN(kerneltype) = "elf kernel";
86 const char *__elfN(moduletype) = "elf module";
88 u_int64_t __elfN(relocation_offset) = 0;
90 extern void elf_wrong_field_size(void);
91 #define CONVERT_FIELD(b, f, e) \
92 switch (sizeof((b)->f)) { \
94 (b)->f = e ## 16toh((b)->f); \
97 (b)->f = e ## 32toh((b)->f); \
100 (b)->f = e ## 64toh((b)->f); \
103 /* Force a link time error. */ \
104 elf_wrong_field_size(); \
108 #define CONVERT_SWITCH(h, d, f) \
109 switch ((h)->e_ident[EI_DATA]) { \
121 static int elf_header_convert(Elf_Ehdr *ehdr)
124 * Fixup ELF header endianness.
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
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)
147 CONVERT_SWITCH(ehdr, ehdr, HEADER_FIELDS);
154 static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr)
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)
166 CONVERT_SWITCH(ehdr, phdr, PROGRAM_HEADER_FIELDS);
168 #undef PROGRAM_HEADER_FIELDS
173 static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr)
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)
187 CONVERT_SWITCH(ehdr, shdr, SECTION_HEADER_FIELDS);
189 #undef SECTION_HEADER_FIELDS
193 #undef CONVERT_SWITCH
197 __elfN(load_elf_header)(char *filename, elf_file_t ef)
204 * Open the image, read and validate the ELF header
206 if (filename == NULL) /* can't handle nameless */
208 if ((ef->fd = open(filename, O_RDONLY)) == -1)
210 ef->firstpage = malloc(PAGE_SIZE);
211 if (ef->firstpage == NULL) {
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 */
221 ehdr = ef->ehdr = (Elf_Ehdr *)ef->firstpage;
224 if (!IS_ELF(*ehdr)) {
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 ? */ {
236 err = elf_header_convert(ehdr);
240 if (ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */
248 if (ef->firstpage != NULL) {
250 ef->firstpage = NULL;
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).
265 __elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result)
267 return (__elfN(loadfile_raw)(filename, dest, result, 0));
271 __elfN(loadfile_raw)(char *filename, u_int64_t dest,
272 struct preloaded_file **result, int multiboot)
274 struct preloaded_file *fp, *kfp;
280 bzero(&ef, sizeof(struct elf_file));
283 err = __elfN(load_elf_header)(filename, &ef);
290 * Check to see what sort of module we are.
292 kfp = file_findfile(NULL, __elfN(kerneltype));
295 * Kernels can be ET_DYN, so just assume the first loaded object is the
296 * kernel. This assumption will be checked later.
301 if (ef.kernel || ehdr->e_type == ET_EXEC) {
302 /* Looks like a kernel */
304 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n");
309 * Calculate destination address based on kernel entrypoint.
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.
317 if (ehdr->e_type == ET_EXEC)
318 dest = (ehdr->e_entry & ~PAGE_MASK);
320 if ((ehdr->e_entry & ~PAGE_MASK) == 0) {
321 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n");
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");
335 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n");
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);
344 /* Looks OK, got ahead */
352 if (archsw.arch_loadaddr != NULL)
353 dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest);
355 dest = roundup(dest, PAGE_SIZE);
358 * Ok, we think we should handle this.
362 printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n");
366 if (ef.kernel == 1 && multiboot == 0)
367 setenv("kernelname", filename, 1);
368 fp->f_name = strdup(filename);
370 fp->f_type = strdup(ef.kernel ?
371 __elfN(kerneltype) : __elfN(moduletype));
373 fp->f_type = strdup("elf multiboot kernel");
377 printf("%s entry at 0x%jx\n", filename, (uintmax_t)ehdr->e_entry);
379 printf("%s ", filename);
382 fp->f_size = __elfN(loadimage)(fp, &ef, dest);
383 if (fp->f_size == 0 || fp->f_addr == 0)
386 /* save exec header as metadata */
387 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
389 /* Load OK, return module pointer */
390 *result = (struct preloaded_file *)fp;
407 * With the file (fd) open on the image, and (ehdr) containing
408 * the Elf header, load the image at (off)
411 __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
416 Elf_Phdr *phdr, *php;
420 vm_offset_t firstaddr;
421 vm_offset_t lastaddr;
434 Elf_Addr p_start, p_end;
439 firstaddr = lastaddr = 0;
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 */
446 off = - (off & 0xff000000u); /* i386 relocates after locore */
448 #elif defined(__powerpc__)
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.
457 if (off & 0xf0000000u) {
458 off = -(off & 0xf0000000u);
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...
465 ehdr->e_entry += off;
467 printf("Converted entry 0x%08x\n", ehdr->e_entry);
471 #elif defined(__arm__) && !defined(EFI)
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.
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.
488 off -= ehdr->e_entry & ~PAGE_MASK;
489 ehdr->e_entry += off;
491 printf("ehdr->e_entry 0x%08x, va<->pa off %llx\n", ehdr->e_entry, off);
494 off = 0; /* other archs use direct mapped kernels */
500 __elfN(relocation_offset) = off;
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");
506 phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
508 for (i = 0; i < ehdr->e_phnum; i++) {
509 if (elf_program_header_convert(ehdr, phdr))
512 /* We want to load PT_LOAD segments only.. */
513 if (phdr[i].p_type != PT_LOAD)
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));
522 if ((phdr[i].p_flags & PF_W) == 0) {
523 printf("text=0x%lx ", (long)phdr[i].p_filesz);
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));
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);
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");
545 /* clear space from oversized segments; eg: bss */
546 if (phdr[i].p_filesz < phdr[i].p_memsz) {
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));
553 kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
554 phdr[i].p_memsz - phdr[i].p_filesz);
560 if (archsw.arch_loadseg != NULL)
561 archsw.arch_loadseg(ehdr, phdr + i, off);
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;
568 lastaddr = roundup(lastaddr, sizeof(long));
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.
577 chunk = (size_t)ehdr->e_shnum * (size_t)ehdr->e_shentsize;
578 if (chunk == 0 || ehdr->e_shoff == 0)
580 shdr = alloc_pread(ef->fd, ehdr->e_shoff, chunk);
582 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
583 "_loadimage: failed to read section headers");
587 for (i = 0; i < ehdr->e_shnum; i++)
588 elf_section_header_convert(ehdr, &shdr[i]);
590 file_addmetadata(fp, MODINFOMD_SHDR, chunk, shdr);
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
597 chunk = shdr[ehdr->e_shstrndx].sh_size;
599 shstr = alloc_pread(ef->fd, shdr[ehdr->e_shstrndx].sh_offset, chunk);
601 for (i = 0; i < ehdr->e_shnum; i++) {
602 if (strcmp(shstr + shdr[i].sh_name, ".ctors") != 0)
604 ctors = shdr[i].sh_addr;
605 file_addmetadata(fp, MODINFOMD_CTORS_ADDR, sizeof(ctors),
607 size = shdr[i].sh_size;
608 file_addmetadata(fp, MODINFOMD_CTORS_SIZE, sizeof(size),
617 * Now load any symbols.
621 for (i = 0; i < ehdr->e_shnum; i++) {
622 if (shdr[i].sh_type != SHT_SYMTAB)
624 for (j = 0; j < ehdr->e_phnum; j++) {
625 if (phdr[j].p_type != PT_LOAD)
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;
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 */
639 symstrindex = shdr[i].sh_link;
641 if (symtabindex < 0 || symstrindex < 0)
644 /* Ok, committed to a load. */
649 for (i = symtabindex; i >= 0; i = symstrindex) {
653 switch(shdr[i].sh_type) {
654 case SHT_SYMTAB: /* Symbol table */
657 case SHT_STRTAB: /* String table */
665 size = shdr[i].sh_size;
666 #if defined(__powerpc__)
667 #if __ELF_WORD_SIZE == 64
668 size = htobe64(size);
670 size = htobe32(size);
674 archsw.arch_copyin(&size, lastaddr, sizeof(size));
675 lastaddr += sizeof(size);
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));
682 if (i == symstrindex)
684 printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
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!");
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);
701 /* Reset offsets relative to ssym */
702 lastaddr += shdr[i].sh_size;
703 lastaddr = roundup(lastaddr, sizeof(size));
704 if (i == symtabindex)
706 else if (i == symstrindex)
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);
720 ssym = htobe32((uint32_t)ssym);
721 esym = htobe32((uint32_t)esym);
725 file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
726 file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
731 ret = lastaddr - firstaddr;
732 fp->f_addr = firstaddr;
735 for (i = 0; i < ehdr->e_phnum; i++) {
736 if (phdr[i].p_type == PT_DYNAMIC) {
739 file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp);
744 if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
747 ndp = php->p_filesz / sizeof(Elf_Dyn);
750 dp = malloc(php->p_filesz);
753 archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
756 for (i = 0; i < ndp; i++) {
757 if (dp[i].d_tag == 0)
759 switch (dp[i].d_tag) {
761 ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
764 ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
767 ef->strsz = dp[i].d_un.d_val;
770 ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off);
773 ef->rel = (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
776 ef->relsz = dp[i].d_un.d_val;
779 ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
782 ef->relasz = dp[i].d_un.d_val;
788 if (ef->hashtab == NULL || ef->symtab == NULL ||
789 ef->strtab == NULL || ef->strsz == 0)
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;
796 if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
798 p_start = sym.st_value + ef->off;
799 if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
801 p_end = sym.st_value + ef->off;
803 if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0)
806 if (ef->kernel) /* kernel must not depend on anything */
817 static char invalid_name[] = "bad";
820 fake_modname(const char *name)
826 sp = strrchr(name, '/');
831 ep = strrchr(name, '.');
835 ep = invalid_name + sizeof(invalid_name) - 1;
838 ep = name + strlen(name);
840 fp = malloc(len + 1);
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 */
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 */
866 __elfN(load_modmetadata)(struct preloaded_file *fp, u_int64_t dest)
870 Elf_Shdr *sh_meta, *shdr = NULL;
871 Elf_Shdr *sh_data[2];
872 char *shstrtab = NULL;
874 Elf_Addr p_start, p_end;
876 bzero(&ef, sizeof(struct elf_file));
879 err = __elfN(load_elf_header)(fp->f_name, &ef);
883 if (ef.kernel == 1 || ef.ehdr->e_type == ET_EXEC) {
885 } else if (ef.ehdr->e_type != ET_DYN) {
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);
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");
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) {
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];
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");
926 /* Load set_modmetadata_set into memory */
927 err = kern_pread(ef.fd, dest, sh_meta->sh_size, sh_meta->sh_offset);
929 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
930 "load_modmetadata: unable to load set_modmetadata_set: %d\n", err);
934 p_end = dest + sh_meta->sh_size;
935 dest += sh_meta->sh_size;
937 /* Load data sections into memory. */
938 err = kern_pread(ef.fd, dest, sh_data[0]->sh_size,
939 sh_data[0]->sh_offset);
941 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
942 "load_modmetadata: unable to load data: %d\n", err);
947 * We have to increment the dest, so that the offset is the same into
948 * both the .rodata and .data sections.
950 ef.off = -(sh_data[0]->sh_addr - dest);
951 dest += (sh_data[1]->sh_addr - sh_data[0]->sh_addr);
953 err = kern_pread(ef.fd, dest, sh_data[1]->sh_size,
954 sh_data[1]->sh_offset);
956 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
957 "load_modmetadata: unable to load data: %d\n", err);
961 err = __elfN(parse_modmetadata)(fp, &ef, p_start, p_end);
963 printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
964 "load_modmetadata: unable to parse metadata: %d\n", err);
969 if (shstrtab != NULL)
973 if (ef.firstpage != NULL)
981 __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
982 Elf_Addr p_start, Elf_Addr p_end)
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;
990 struct mod_depend *mdepend;
991 struct mod_version mver;
993 int error, modcnt, minfolen;
999 COPYOUT(p, &v, sizeof(v));
1000 error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
1001 if (error == EOPNOTSUPP)
1003 else if (error != 0)
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)
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)
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;
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)
1038 p += sizeof(Elf_Addr);
1039 switch(md.md_type) {
1041 if (ef->kernel) /* kernel must not depend on anything */
1043 s = strdupout((vm_offset_t)md.md_cval);
1044 minfolen = sizeof(*mdepend) + strlen(s) + 1;
1045 mdepend = malloc(minfolen);
1046 if (mdepend == NULL)
1048 COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend));
1049 strcpy((char*)(mdepend + 1), s);
1051 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
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);
1064 s = fake_modname(fp->f_name);
1065 file_addmodule(fp, s, 1, NULL);
1071 static unsigned long
1072 elf_hash(const char *name)
1074 const unsigned char *p = (const unsigned char *) name;
1075 unsigned long h = 0;
1078 while (*p != '\0') {
1079 h = (h << 4) + *p++;
1080 if ((g = h & 0xf0000000) != 0)
1087 static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n";
1089 __elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef, const char* name,
1097 hash = elf_hash(name);
1098 COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
1100 while (symnum != STN_UNDEF) {
1101 if (symnum >= ef->nchains) {
1102 printf(__elfN(bad_symtable));
1106 COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
1107 if (sym.st_name == 0) {
1108 printf(__elfN(bad_symtable));
1112 strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
1113 if (strcmp(name, strp) == 0) {
1115 if (sym.st_shndx != SHN_UNDEF ||
1116 (sym.st_value != 0 &&
1117 ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
1124 COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
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.
1134 * Returns EOPNOTSUPP if no relocation method is supplied.
1137 __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
1138 Elf_Addr p, void *val, size_t len)
1146 * The kernel is already relocated, but we still want to apply
1147 * offset adjustments.
1150 return (EOPNOTSUPP);
1152 for (n = 0; n < ef->relsz / sizeof(r); n++) {
1153 COPYOUT(ef->rel + n, &r, sizeof(r));
1155 error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
1156 ef->off, p, val, len);
1160 for (n = 0; n < ef->relasz / sizeof(a); n++) {
1161 COPYOUT(ef->rela + n, &a, sizeof(a));
1163 error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
1164 ef->off, p, val, len);
1173 __elfN(symaddr)(struct elf_file *ef, Elf_Size symidx)
1176 /* Symbol lookup by index not required here. */