load_elf.c revision 199767f8919635c4928607450d9e0abb932109ce
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef struct elf_file {
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef, u_int64_t loadaddr);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(load_elf_header)(char *filename, elf_file_t ef)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Open the image, read and validate the ELF header
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (filename == NULL) /* can't handle nameless */
199767f8919635c4928607450d9e0abb932109ceToomas Soome bytes_read = read(ef->fd, ef->firstpage, PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bytes_read < 0 || ef->firstlen <= sizeof(Elf_Ehdr)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EFTYPE; /* could be EIO, but may be small file */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Is it ELF? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Attempt to load the file (file) as an ELF module. It will be stored at
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (dest), and a pointer to a module structure describing the loaded object
199767f8919635c4928607450d9e0abb932109ceToomas Soome * will be saved in (result).
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (__elfN(loadfile_raw)(filename, dest, result, 0));
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(loadfile_raw)(char *filename, u_int64_t dest,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check to see what sort of module we are.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Kernels can be ET_DYN, so just assume the first loaded object is the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * kernel. This assumption will be checked later.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Looks like a kernel */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Calculate destination address based on kernel entrypoint.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * For ARM, the destination address is independent of any values in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * elf header (an ARM kernel can be loaded at any 2MB boundary), so we
199767f8919635c4928607450d9e0abb932109ceToomas Soome * leave dest set to the value calculated by archsw.arch_loadaddr() and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * passed in to this function.
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Looks like a kld module */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module as multiboot\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Looks OK, got ahead */
199767f8919635c4928607450d9e0abb932109ceToomas Soome dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Ok, we think we should handle this.
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s entry at 0x%jx\n", filename, (uintmax_t)ehdr->e_entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* save exec header as metadata */
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Load OK, return module pointer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * With the file (fd) open on the image, and (ehdr) containing
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the Elf header, load the image at (off)
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
199767f8919635c4928607450d9e0abb932109ceToomas Soome off = - (off & 0xffffffffff000000ull);/* x86_64 relocates after locore */
199767f8919635c4928607450d9e0abb932109ceToomas Soome off = - (off & 0xff000000u); /* i386 relocates after locore */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * On the purely virtual memory machines like e500, the kernel is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * linked against its final VA range, which is most often not
199767f8919635c4928607450d9e0abb932109ceToomas Soome * available at the loader stage, but only after kernel initializes
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and completes its VM settings. In such cases we cannot use p_vaddr
199767f8919635c4928607450d9e0abb932109ceToomas Soome * field directly to load ELF segments, but put them at some
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 'load-time' locations.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * XXX the physical load address should not be hardcoded. Note
199767f8919635c4928607450d9e0abb932109ceToomas Soome * that the Book-E kernel assumes that it's loaded at a 16MB
199767f8919635c4928607450d9e0abb932109ceToomas Soome * boundary for now...
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Converted entry 0x%08x\n", ehdr->e_entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The elf headers in arm kernels specify virtual addresses in all
199767f8919635c4928607450d9e0abb932109ceToomas Soome * header fields, even the ones that should be physical addresses.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We assume the entry point is in the first page, and masking the page
199767f8919635c4928607450d9e0abb932109ceToomas Soome * offset will leave us with the virtual address the kernel was linked
199767f8919635c4928607450d9e0abb932109ceToomas Soome * at. We subtract that from the load offset, making 'off' into the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * value which, when added to a virtual address in an elf header,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * translates it to a physical address. We do the va->pa conversion on
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the entry point address in the header now, so that later we can
199767f8919635c4928607450d9e0abb932109ceToomas Soome * launch the kernel by just jumping to that address.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * When booting from UEFI the copyin and copyout functions handle
199767f8919635c4928607450d9e0abb932109ceToomas Soome * adjusting the location relative to the first virtual address.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Because of this there is no need to adjust the offset or entry
199767f8919635c4928607450d9e0abb932109ceToomas Soome * point address as these will both be handled by the efi code.
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ehdr->e_entry 0x%08x, va<->pa off %llx\n", ehdr->e_entry, off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome off = 0; /* other archs use direct mapped kernels */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* use entry address from header */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: program header not within first page\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We want to load PT_LOAD segments only.. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)(phdr[i].p_paddr + off + phdr[i].p_memsz - 1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kern_pread(ef->fd, phdr[i].p_paddr + off + fpcopy,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_loadimage: read failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kern_pread(ef->fd, phdr[i].p_vaddr + off + fpcopy,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_loadimage: read failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* clear space from oversized segments; eg: bss */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)(phdr[i].p_paddr + off + phdr[i].p_filesz),
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)(phdr[i].p_paddr + off + phdr[i].p_memsz - 1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome kern_bzero(phdr[i].p_paddr + off + phdr[i].p_filesz,
199767f8919635c4928607450d9e0abb932109ceToomas Soome kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (firstaddr == 0 || firstaddr > (phdr[i].p_paddr + off))
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr < (phdr[i].p_paddr + off + phdr[i].p_memsz))
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr = phdr[i].p_paddr + off + phdr[i].p_memsz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz))
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get the section headers. We need this for finding the .ctors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * section as well as for loading any symbols. Both may be hard
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to do if reading from a .gz file as it involves seeking. I
199767f8919635c4928607450d9e0abb932109ceToomas Soome * think the rule is going to have to be that you must strip a
199767f8919635c4928607450d9e0abb932109ceToomas Soome * file to remove symbols before gzipping it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr = alloc_pread(ef->fd, ehdr->e_shoff, chunk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_loadimage: failed to read section headers");
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_SHDR, chunk, shdr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read the section string table and look for the .ctors section.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We need to tell the kernel where it is so that it can call the
199767f8919635c4928607450d9e0abb932109ceToomas Soome shstr = alloc_pread(ef->fd, shdr[ehdr->e_shstrndx].sh_offset, chunk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(shstr + shdr[i].sh_name, ".ctors") != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_CTORS_ADDR, sizeof(ctors),
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_CTORS_SIZE, sizeof(size),
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Now load any symbols.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue; /* alread loaded in a PT_LOAD above */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Save it for loading below */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Ok, committed to a load. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = symtabindex; i >= 0; i = symstrindex) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(&size, lastaddr, sizeof(size));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (uintmax_t)lastaddr, (uintmax_t)(lastaddr + shdr[i].sh_size));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not seek for symbols - skipped!");
199767f8919635c4928607450d9e0abb932109ceToomas Soome result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (result < 0 || (size_t)result != shdr[i].sh_size) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not read symbols - skipped! (%ju != %ju)", (uintmax_t)result,
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Reset offsets relative to ssym */
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (i == symstrindex)
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyout(php->p_paddr + off, dp, php->p_filesz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < ndp; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->rel = (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->hashtab == NULL || ef->symtab == NULL ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->kernel) /* kernel must not depend on anything */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(load_modmetadata)(struct preloaded_file *fp, u_int64_t dest)
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = __elfN(load_elf_header)(fp->f_name, &ef);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef.kernel == 1 || ef.ehdr->e_type == ET_EXEC) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = ef.ehdr->e_shnum * ef.ehdr->e_shentsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr = alloc_pread(ef.fd, ef.ehdr->e_shoff, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Load shstrtab. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome shstrtab = alloc_pread(ef.fd, shdr[ef.ehdr->e_shstrndx].sh_offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to load shstrtab\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find set_modmetadata_set and data sections. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0, j = 0; i < ef.ehdr->e_shnum; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "set_modmetadata_set") == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((strcmp(&shstrtab[shdr[i].sh_name], ".data") == 0) ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome (strcmp(&shstrtab[shdr[i].sh_name], ".rodata") == 0)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sh_meta == NULL || sh_data[0] == NULL || sh_data[1] == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to find set_modmetadata_set or data sections\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Load set_modmetadata_set into memory */
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = kern_pread(ef.fd, dest, sh_meta->sh_size, sh_meta->sh_offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to load set_modmetadata_set: %d\n", err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Load data sections into memory. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = kern_pread(ef.fd, dest, sh_data[0]->sh_size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to load data: %d\n", err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We have to increment the dest, so that the offset is the same into
199767f8919635c4928607450d9e0abb932109ceToomas Soome * both the .rodata and .data sections.
199767f8919635c4928607450d9e0abb932109ceToomas Soome dest += (sh_data[1]->sh_addr - sh_data[0]->sh_addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = kern_pread(ef.fd, dest, sh_data[1]->sh_size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to load data: %d\n", err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = __elfN(parse_modmetadata)(fp, &ef, p_start, p_end);
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to parse metadata: %d\n", err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
199767f8919635c4928607450d9e0abb932109ceToomas Soome#elif defined(__amd64__) && __ELF_WORD_SIZE == 32
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (p < p_end) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(p, &v, sizeof(v));
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_cval = (const char *)(uintptr_t)md64.md_cval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#elif defined(__amd64__) && __ELF_WORD_SIZE == 32
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(reloc_ptr)(fp, ef, v, &md32, sizeof(md32));
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_cval = (const char *)(uintptr_t)md32.md_cval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_data = (void *)((uintptr_t)md.md_data + (uintptr_t)ef->off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome p += sizeof(Elf_Addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->kernel) /* kernel must not depend on anything */
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend));
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic unsigned long
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned char *p = (const unsigned char *) name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long h = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long g;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p != '\0') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome h = (h << 4) + *p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((g = h & 0xf0000000) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome h ^= g >> 24;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n";
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(lookup_symbol)(struct preloaded_file *fp __unused, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long hash;
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
199767f8919635c4928607450d9e0abb932109ceToomas Soome strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Apply any intra-module relocations to the value. p is the load address
199767f8919635c4928607450d9e0abb932109ceToomas Soome * of the value and val/len is the value to be modified. This does NOT modify
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the image in-place, because this is done by kern_linker later on.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Returns EOPNOTSUPP if no relocation method is supplied.
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The kernel is already relocated, but we still want to apply
199767f8919635c4928607450d9e0abb932109ceToomas Soome * offset adjustments.
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(symaddr)(struct elf_file *ef __unused, Elf_Size symidx __unused)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Symbol lookup by index not required here. */