199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2004 Ian Dowse <iedowse@freebsd.org>
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 Soome int shstrindex; /* Index of section name string table */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int __elfN(obj_loadimage)(struct preloaded_file *mp, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int __elfN(obj_lookup_set)(struct preloaded_file *mp, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *name, Elf_Addr *startp, Elf_Addr *stopp, int *countp);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int __elfN(obj_parse_modmetadata)(struct preloaded_file *mp,
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic Elf_Addr __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeconst char *__elfN(obj_kerneltype) = "elf kernel";
199767f8919635c4928607450d9e0abb932109ceToomas Soomeconst char *__elfN(obj_moduletype) = "elf obj module";
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(obj_loadfile)(char *filename, u_int64_t dest,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Open the image, read and validate the ELF header
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (filename == NULL) /* can't handle nameless */
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EFTYPE; /* could be EIO, but may be small file */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Is it ELF? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome hdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome hdr->e_machine != ELF_TARG_MACH || /* Machine ? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (hdr->e_shnum * hdr->e_shentsize == 0 || hdr->e_shoff == 0 ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome kfp = file_findfile(NULL, __elfN(obj_kerneltype));
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_obj_loadfile: can't load module before kernel\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome dest = archsw.arch_loadaddr(LOAD_ELF, hdr, dest);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Ok, we think we should handle this.
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_obj_loadfile: cannot allocate module info\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_size = __elfN(obj_loadimage)(fp, &ef, dest);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* save exec header as metadata */
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*hdr), hdr);
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(obj_loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Read in the section headers. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr = alloc_pread(ef->fd, (off_t)hdr->e_shoff, shdrbytes);
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_obj_loadimage: read section headers failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Decide where to load everything, but don't read it yet.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We store the load address as a non-zero sh_addr value.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Start with the code/data and bss.
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr = roundup(lastaddr, shdr[i].sh_addralign);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Symbols. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_obj_loadimage: file has no valid symbol table\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr = roundup(lastaddr, shdr[ef->symtabindex].sh_addralign);
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr[ef->symtabindex].sh_addr = (Elf_Addr)lastaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (symstrindex < 0 || symstrindex >= hdr->e_shnum ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_obj_loadimage: file has invalid symbol strings\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr = roundup(lastaddr, shdr[symstrindex].sh_addralign);
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr[symstrindex].sh_addr = (Elf_Addr)lastaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Section names. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (hdr->e_shstrndx == 0 || hdr->e_shstrndx >= hdr->e_shnum ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_obj_loadimage: file has no section names\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr = roundup(lastaddr, shdr[ef->shstrindex].sh_addralign);
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr[ef->shstrindex].sh_addr = (Elf_Addr)lastaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Relocation tables. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr = roundup(lastaddr, shdr[i].sh_addralign);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Clear the whole area, including bss regions. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Figure section with the lowest file offset we haven't loaded yet. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Find next section to load. The complexity of this loop is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * O(n^2), but with the number of sections being typically
199767f8919635c4928607450d9e0abb932109ceToomas Soome * small, we do not care.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Skip sections that were loaded already. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find section with smallest offset. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kern_pread(ef->fd, (vm_offset_t)cshdr->sh_addr,
199767f8919635c4928607450d9e0abb932109ceToomas Soome cshdr->sh_size, (off_t)cshdr->sh_offset) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_obj_loadimage: read failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_SHDR, shdrbytes, shdr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("size 0x%lx at 0x%lx", (u_long)ret, (u_long)firstaddr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(obj_parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (__elfN(obj_lookup_set)(fp, ef, "modmetadata_set", &p, &p_stop,
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (p < p_stop) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(p, &v, sizeof(v));
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(obj_reloc_ptr)(fp, ef, p, &v, sizeof(v));
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(obj_reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_cval = (const char *)(uintptr_t)md64.md_cval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(obj_reloc_ptr)(fp, ef, v, &md, sizeof(md));
199767f8919635c4928607450d9e0abb932109ceToomas Soome p += sizeof(Elf_Addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(obj_lookup_set)(struct preloaded_file *fp __unused, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char* name, Elf_Addr *startp, Elf_Addr *stopp, int *countp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strncmp(p, "set_", 4) == 0 && strcmp(p + 4, name) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *countp = (*stopp - *startp) / sizeof(Elf_Addr);
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__elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, Elf_Addr p,
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (shdr[i].sh_type != SHT_RELA && shdr[i].sh_type != SHT_REL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; j < nrela; j++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; j < nrel; j++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Look up the address of a specified symbol. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (symidx >= ef->e_shdr[ef->symtabindex].sh_size / sizeof(Elf_Sym))
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(ef->e_shdr[ef->symtabindex].sh_addr + symidx * sizeof(Elf_Sym),