199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
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 *
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 * are met:
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 *
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/exec.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/linker.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/module.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/stdint.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/elf.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define FREEBSD_ELF
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <link.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(__i386__) && __ELF_WORD_SIZE == 64
199767f8919635c4928607450d9e0abb932109ceToomas Soome#undef ELF_TARG_CLASS
199767f8919635c4928607450d9e0abb932109ceToomas Soome#undef ELF_TARG_MACH
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ELF_TARG_CLASS ELFCLASS64
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ELF_TARG_MACH EM_X86_64
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef struct elf_file {
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Phdr *ph;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Ehdr *ehdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Sym *symtab;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Hashelt *hashtab;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Hashelt nbuckets;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Hashelt nchains;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Hashelt *buckets;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Hashelt *chains;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Rel *rel;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t relsz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Rela *rela;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t relasz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *strtab;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t strsz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome caddr_t firstpage;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t firstlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int kernel;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int64_t off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome} *elf_file_t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 Soome Elf_Addr p, void *val, size_t len);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Addr p_start, Elf_Addr p_end);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic symaddr_fn __elfN(symaddr);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *fake_modname(const char *name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeconst char *__elfN(kerneltype) = "elf kernel";
199767f8919635c4928607450d9e0abb932109ceToomas Soomeconst char *__elfN(moduletype) = "elf module";
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeu_int64_t __elfN(relocation_offset) = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(load_elf_header)(char *filename, elf_file_t ef)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t bytes_read;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Ehdr *ehdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Open the image, read and validate the ELF header
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (filename == NULL) /* can't handle nameless */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EFTYPE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((ef->fd = open(filename, O_RDONLY)) == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->firstpage = malloc(PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->firstpage == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(ef->fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome bytes_read = read(ef->fd, ef->firstpage, PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->firstlen = (size_t)bytes_read;
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 goto error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr = ef->ehdr = (Elf_Ehdr *)ef->firstpage;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Is it ELF? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!IS_ELF(*ehdr)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EFTYPE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr->e_version != EV_CURRENT ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EFTYPE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeerror:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->firstpage != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(ef->firstpage);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->firstpage = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->fd != -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(ef->fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->fd = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (__elfN(loadfile_raw)(filename, dest, result, 0));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(loadfile_raw)(char *filename, u_int64_t dest,
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct preloaded_file **result, int multiboot)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct preloaded_file *fp, *kfp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct elf_file ef;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Ehdr *ehdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(&ef, sizeof(struct elf_file));
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef.fd = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = __elfN(load_elf_header)(filename, &ef);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr = ef.ehdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check to see what sort of module we are.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome kfp = file_findfile(NULL, __elfN(kerneltype));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef __powerpc__
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kfp == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef.kernel = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef.kernel || ehdr->e_type == ET_EXEC) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Looks like a kernel */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kfp != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EPERM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto oerr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Calculate destination address based on kernel entrypoint.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef __arm__
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_type == ET_EXEC)
199767f8919635c4928607450d9e0abb932109ceToomas Soome dest = (ehdr->e_entry & ~PAGE_MASK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((ehdr->e_entry & ~PAGE_MASK) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EPERM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto oerr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef.kernel = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (ehdr->e_type == ET_DYN) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Looks like a kld module */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (multiboot != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module as multiboot\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EPERM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto oerr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kfp == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EPERM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto oerr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(__elfN(kerneltype), kfp->f_type)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EPERM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto oerr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Looks OK, got ahead */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef.kernel = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EFTYPE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto oerr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (archsw.arch_loadaddr != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome dest = roundup(dest, PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Ok, we think we should handle this.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp = file_alloc();
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EPERM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef.kernel == 1 && multiboot == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("kernelname", filename, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_name = strdup(filename);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (multiboot == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_type = strdup(ef.kernel ?
199767f8919635c4928607450d9e0abb932109ceToomas Soome __elfN(kerneltype) : __elfN(moduletype));
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_type = strdup("elf multiboot kernel");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef ELF_VERBOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef.kernel)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s entry at 0x%jx\n", filename, (uintmax_t)ehdr->e_entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s ", filename);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_size = __elfN(loadimage)(fp, &ef, dest);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_size == 0 || fp->f_addr == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto ioerr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* save exec header as metadata */
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Load OK, return module pointer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *result = (struct preloaded_file *)fp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ioerr:
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EIO;
199767f8919635c4928607450d9e0abb932109ceToomas Soome oerr:
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_discard(fp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome out:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef.firstpage)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(ef.firstpage);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef.fd != -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(ef.fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int j;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Ehdr *ehdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Phdr *phdr, *php;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Shdr *shdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *shstr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ret;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t firstaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t lastaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t chunk;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t result;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Addr ssym, esym;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Dyn *dp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Addr adp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Addr ctors;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ndp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int symstrindex;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int symtabindex;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Size size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int fpcopy;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Sym sym;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Addr p_start, p_end;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dp = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome firstaddr = lastaddr = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr = ef->ehdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_type == ET_EXEC) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(__i386__) || defined(__amd64__)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if __ELF_WORD_SIZE == 64
199767f8919635c4928607450d9e0abb932109ceToomas Soome off = - (off & 0xffffffffff000000ull);/* x86_64 relocates after locore */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome off = - (off & 0xff000000u); /* i386 relocates after locore */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#elif defined(__powerpc__)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (off & 0xf0000000u) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome off = -(off & 0xf0000000u);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome off += 0x01000000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr->e_entry += off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef ELF_VERBOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Converted entry 0x%08x\n", ehdr->e_entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome off = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#elif defined(__arm__) && !defined(EFI)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 *
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome off -= ehdr->e_entry & ~PAGE_MASK;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr->e_entry += off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef ELF_VERBOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ehdr->e_entry 0x%08x, va<->pa off %llx\n", ehdr->e_entry, off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome off = 0; /* other archs use direct mapped kernels */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->off = off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* use entry address from header */
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_addr = ehdr->e_entry;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->kernel)
199767f8919635c4928607450d9e0abb932109ceToomas Soome __elfN(relocation_offset) = off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < ehdr->e_phnum; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We want to load PT_LOAD segments only.. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (phdr[i].p_type != PT_LOAD)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef ELF_VERBOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)(phdr[i].p_paddr + off),
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)(phdr[i].p_paddr + off + phdr[i].p_memsz - 1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)(phdr[i].p_vaddr + off),
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((phdr[i].p_flags & PF_W) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("text=0x%lx ", (long)phdr[i].p_filesz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("data=0x%lx", (long)phdr[i].p_filesz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (phdr[i].p_filesz < phdr[i].p_memsz)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" ");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome fpcopy = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->firstlen > phdr[i].p_offset) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fpcopy = ef->firstlen - phdr[i].p_offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome phdr[i].p_paddr + off, fpcopy);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome phdr[i].p_vaddr + off, fpcopy);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (phdr[i].p_filesz > fpcopy) {
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 phdr[i].p_filesz - fpcopy,
199767f8919635c4928607450d9e0abb932109ceToomas Soome phdr[i].p_offset + fpcopy) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_loadimage: read failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kern_pread(ef->fd, phdr[i].p_vaddr + off + fpcopy,
199767f8919635c4928607450d9e0abb932109ceToomas Soome phdr[i].p_filesz - fpcopy,
199767f8919635c4928607450d9e0abb932109ceToomas Soome phdr[i].p_offset + fpcopy) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_loadimage: read failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* clear space from oversized segments; eg: bss */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (phdr[i].p_filesz < phdr[i].p_memsz) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef ELF_VERBOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" (bss: 0x%lx-0x%lx)",
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 } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" (bss: 0x%lx-0x%lx)",
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 }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 phdr[i].p_memsz - phdr[i].p_filesz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
199767f8919635c4928607450d9e0abb932109ceToomas Soome phdr[i].p_memsz - phdr[i].p_filesz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef ELF_VERBOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (archsw.arch_loadseg != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_loadseg(ehdr, phdr + i, off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] == ELFOSABI_SOLARIS) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (firstaddr == 0 || firstaddr > (phdr[i].p_paddr + off))
199767f8919635c4928607450d9e0abb932109ceToomas Soome firstaddr = phdr[i].p_paddr + off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lastaddr == 0 ||
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 } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
199767f8919635c4928607450d9e0abb932109ceToomas Soome firstaddr = phdr[i].p_vaddr + off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lastaddr == 0 ||
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 }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr = roundup(lastaddr, sizeof(long));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome chunk = ehdr->e_shnum * ehdr->e_shentsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (chunk == 0 || ehdr->e_shoff == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto nosyms;
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr = alloc_pread(ef->fd, ehdr->e_shoff, chunk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (shdr == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome "_loadimage: failed to read section headers");
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto nosyms;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_SHDR, chunk, shdr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 * ctors.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome chunk = shdr[ehdr->e_shstrndx].sh_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (chunk) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome shstr = alloc_pread(ef->fd, shdr[ehdr->e_shstrndx].sh_offset, chunk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (shstr) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < ehdr->e_shnum; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(shstr + shdr[i].sh_name, ".ctors") != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ctors = shdr[i].sh_addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_CTORS_ADDR, sizeof(ctors),
199767f8919635c4928607450d9e0abb932109ceToomas Soome &ctors);
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = shdr[i].sh_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_CTORS_SIZE, sizeof(size),
199767f8919635c4928607450d9e0abb932109ceToomas Soome &size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(shstr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Now load any symbols.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome symtabindex = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome symstrindex = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < ehdr->e_shnum; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (shdr[i].sh_type != SHT_SYMTAB)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; j < ehdr->e_phnum; j++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (phdr[j].p_type != PT_LOAD)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (shdr[i].sh_offset >= phdr[j].p_offset &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (shdr[i].sh_offset + shdr[i].sh_size <=
199767f8919635c4928607450d9e0abb932109ceToomas Soome phdr[j].p_offset + phdr[j].p_filesz)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr[i].sh_offset = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr[i].sh_size = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
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 symtabindex = i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome symstrindex = shdr[i].sh_link;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (symtabindex < 0 || symstrindex < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto nosyms;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Ok, committed to a load. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef ELF_VERBOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("syms=[");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssym = lastaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = symtabindex; i >= 0; i = symstrindex) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef ELF_VERBOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *secname;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch(shdr[i].sh_type) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SHT_SYMTAB: /* Symbol table */
199767f8919635c4928607450d9e0abb932109ceToomas Soome secname = "symtab";
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SHT_STRTAB: /* String table */
199767f8919635c4928607450d9e0abb932109ceToomas Soome secname = "strtab";
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome secname = "WHOA!!";
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = shdr[i].sh_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(&size, lastaddr, sizeof(size));
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr += sizeof(size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef ELF_VERBOSE
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#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i == symstrindex)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("+");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 lastaddr = ssym;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssym = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto nosyms;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
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 (uintmax_t)shdr[i].sh_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr = ssym;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssym = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto nosyms;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Reset offsets relative to ssym */
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr += shdr[i].sh_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome lastaddr = roundup(lastaddr, sizeof(size));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i == symtabindex)
199767f8919635c4928607450d9e0abb932109ceToomas Soome symtabindex = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (i == symstrindex)
199767f8919635c4928607450d9e0abb932109ceToomas Soome symstrindex = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome esym = lastaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef ELF_VERBOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("]");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomenosyms:
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ret = lastaddr - firstaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ehdr->e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp->f_addr = firstaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome php = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < ehdr->e_phnum; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (phdr[i].p_type == PT_DYNAMIC) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome php = phdr + i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome adp = php->p_vaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ndp = php->p_filesz / sizeof(Elf_Dyn);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ndp == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dp = malloc(php->p_filesz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dp == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
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 else
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->strsz = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < ndp; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dp[i].d_tag == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (dp[i].d_tag) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DT_HASH:
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DT_STRTAB:
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DT_STRSZ:
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->strsz = dp[i].d_un.d_val;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DT_SYMTAB:
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DT_REL:
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->rel = (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DT_RELSZ:
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->relsz = dp[i].d_un.d_val;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DT_RELA:
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DT_RELASZ:
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->relasz = dp[i].d_un.d_val;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->hashtab == NULL || ef->symtab == NULL ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->strtab == NULL || ef->strsz == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->buckets = ef->hashtab + 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->chains = ef->buckets + ef->nbuckets;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome p_start = sym.st_value + ef->off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome p_end = sym.st_value + ef->off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->kernel) /* kernel must not depend on anything */
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(dp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (shdr)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(shdr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ret;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char invalid_name[] = "bad";
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar *
199767f8919635c4928607450d9e0abb932109ceToomas Soomefake_modname(const char *name)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *sp, *ep;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *fp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sp = strrchr(name, '/');
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome sp = name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ep = strrchr(name, '.');
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ep) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ep == name) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sp = invalid_name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ep = invalid_name + sizeof(invalid_name) - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome ep = name + strlen(name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = ep - sp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp = malloc(len + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(fp, sp, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fp[len] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome return fp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct mod_metadata64 {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int md_version; /* structure version MDTV_* */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int md_type; /* type of entry MDT_* */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int64_t md_data; /* specific data */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int64_t md_cval; /* common string label */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(__amd64__) && __ELF_WORD_SIZE == 32
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct mod_metadata32 {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int md_version; /* structure version MDTV_* */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int md_type; /* type of entry MDT_* */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t md_data; /* specific data */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t md_cval; /* common string label */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(load_modmetadata)(struct preloaded_file *fp, u_int64_t dest)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct elf_file ef;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err, i, j;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Shdr *sh_meta, *shdr = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Shdr *sh_data[2];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *shstrtab = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Addr p_start, p_end;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(&ef, sizeof(struct elf_file));
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef.fd = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = __elfN(load_elf_header)(fp->f_name, &ef);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef.kernel == 1 || ef.ehdr->e_type == ET_EXEC) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef.kernel = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (ef.ehdr->e_type != ET_DYN) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EFTYPE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 if (shdr == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = ENOMEM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Load shstrtab. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome shstrtab = alloc_pread(ef.fd, shdr[ef.ehdr->e_shstrndx].sh_offset,
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr[ef.ehdr->e_shstrndx].sh_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (shstrtab == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to load shstrtab\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EFTYPE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find set_modmetadata_set and data sections. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sh_data[0] = sh_data[1] = sh_meta = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0, j = 0; i < ef.ehdr->e_shnum; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(&shstrtab[shdr[i].sh_name],
199767f8919635c4928607450d9e0abb932109ceToomas Soome "set_modmetadata_set") == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sh_meta = &shdr[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((strcmp(&shstrtab[shdr[i].sh_name], ".data") == 0) ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome (strcmp(&shstrtab[shdr[i].sh_name], ".rodata") == 0)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sh_data[j++] = &shdr[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sh_meta == NULL || sh_data[0] == NULL || sh_data[1] == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to find set_modmetadata_set or data sections\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = EFTYPE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 if (err != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to load set_modmetadata_set: %d\n", err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome p_start = dest;
199767f8919635c4928607450d9e0abb932109ceToomas Soome p_end = dest + sh_meta->sh_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dest += sh_meta->sh_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Load data sections into memory. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = kern_pread(ef.fd, dest, sh_data[0]->sh_size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sh_data[0]->sh_offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to load data: %d\n", err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef.off = -(sh_data[0]->sh_addr - dest);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dest += (sh_data[1]->sh_addr - sh_data[0]->sh_addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = kern_pread(ef.fd, dest, sh_data[1]->sh_size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sh_data[1]->sh_offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to load data: %d\n", err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = __elfN(parse_modmetadata)(fp, &ef, p_start, p_end);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome "load_modmetadata: unable to parse metadata: %d\n", err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (shstrtab != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(shstrtab);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (shdr != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(shdr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef.firstpage != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(ef.firstpage);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef.fd != -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(ef.fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Addr p_start, Elf_Addr p_end)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct mod_metadata md;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct mod_metadata64 md64;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#elif defined(__amd64__) && __ELF_WORD_SIZE == 32
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct mod_metadata32 md32;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct mod_depend *mdepend;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct mod_version mver;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error, modcnt, minfolen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Addr v, p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome modcnt = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = p_start;
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 if (error == EOPNOTSUPP)
199767f8919635c4928607450d9e0abb932109ceToomas Soome v += ef->off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(v, &md64, sizeof(md64));
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error == EOPNOTSUPP) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome md64.md_cval += ef->off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome md64.md_data += ef->off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_version = md64.md_version;
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_type = md64.md_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_cval = (const char *)(uintptr_t)md64.md_cval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_data = (void *)(uintptr_t)md64.md_data;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#elif defined(__amd64__) && __ELF_WORD_SIZE == 32
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(v, &md32, sizeof(md32));
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(reloc_ptr)(fp, ef, v, &md32, sizeof(md32));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error == EOPNOTSUPP) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome md32.md_cval += ef->off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome md32.md_data += ef->off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_version = md32.md_version;
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_type = md32.md_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_cval = (const char *)(uintptr_t)md32.md_cval;
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_data = (void *)(uintptr_t)md32.md_data;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(v, &md, sizeof(md));
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error == EOPNOTSUPP) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_cval += ef->off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome md.md_data = (void *)((uintptr_t)md.md_data + (uintptr_t)ef->off);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome p += sizeof(Elf_Addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch(md.md_type) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case MDT_DEPEND:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->kernel) /* kernel must not depend on anything */
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome s = strdupout((vm_offset_t)md.md_cval);
199767f8919635c4928607450d9e0abb932109ceToomas Soome minfolen = sizeof(*mdepend) + strlen(s) + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome mdepend = malloc(minfolen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (mdepend == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ENOMEM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend));
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy((char*)(mdepend + 1), s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(mdepend);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case MDT_VERSION:
199767f8919635c4928607450d9e0abb932109ceToomas Soome s = strdupout((vm_offset_t)md.md_cval);
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmodule(fp, s, mver.mv_version, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome modcnt++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (modcnt == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome s = fake_modname(fp->f_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmodule(fp, s, 1, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic unsigned long
199767f8919635c4928607450d9e0abb932109ceToomas Soomeelf_hash(const char *name)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned char *p = (const unsigned char *) name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long h = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long g;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p != '\0') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome h = (h << 4) + *p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((g = h & 0xf0000000) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome h ^= g >> 24;
199767f8919635c4928607450d9e0abb932109ceToomas Soome h &= ~g;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return h;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n";
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(lookup_symbol)(struct preloaded_file *fp __unused, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char* name, Elf_Sym *symp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Hashelt symnum;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Sym sym;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *strp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned long hash;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome hash = elf_hash(name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (symnum != STN_UNDEF) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (symnum >= ef->nchains) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(__elfN(bad_symtable));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sym.st_name == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(__elfN(bad_symtable));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(name, strp) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(strp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sym.st_shndx != SHN_UNDEF ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome (sym.st_value != 0 &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *symp = sym;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(strp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ENOENT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Returns EOPNOTSUPP if no relocation method is supplied.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Addr p, void *val, size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Rela a;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Rel r;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)mp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The kernel is already relocated, but we still want to apply
199767f8919635c4928607450d9e0abb932109ceToomas Soome * offset adjustments.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ef->kernel)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EOPNOTSUPP);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < ef->relsz / sizeof(r); n++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(ef->rel + n, &r, sizeof(r));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->off, p, val, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 0; n < ef->relasz / sizeof(a); n++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(ef->rela + n, &a, sizeof(a));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
199767f8919635c4928607450d9e0abb932109ceToomas Soome ef->off, p, val, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic Elf_Addr
199767f8919635c4928607450d9e0abb932109ceToomas Soome__elfN(symaddr)(struct elf_file *ef __unused, Elf_Size symidx __unused)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Symbol lookup by index not required here. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}