Home | History | Annotate | Line # | Download | only in freebsd
freebsd_exec.c revision 1.5
      1  1.5    kleink /*	$NetBSD: freebsd_exec.c,v 1.5 1999/10/25 13:55:07 kleink Exp $	*/
      2  1.1   mycroft 
      3  1.1   mycroft /*
      4  1.1   mycroft  * Copyright (c) 1993, 1994 Christopher G. Demetriou
      5  1.1   mycroft  * All rights reserved.
      6  1.1   mycroft  *
      7  1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      8  1.1   mycroft  * modification, are permitted provided that the following conditions
      9  1.1   mycroft  * are met:
     10  1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     11  1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     12  1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     15  1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     16  1.1   mycroft  *    must display the following acknowledgement:
     17  1.1   mycroft  *      This product includes software developed by Christopher G. Demetriou.
     18  1.1   mycroft  * 4. The name of the author may not be used to endorse or promote products
     19  1.1   mycroft  *    derived from this software without specific prior written permission
     20  1.1   mycroft  *
     21  1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1   mycroft  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1   mycroft  */
     32  1.1   mycroft 
     33  1.4  christos #include "opt_execfmt.h"
     34  1.4  christos 
     35  1.1   mycroft #include <sys/param.h>
     36  1.1   mycroft #include <sys/systm.h>
     37  1.1   mycroft #include <sys/proc.h>
     38  1.1   mycroft #include <sys/malloc.h>
     39  1.1   mycroft #include <sys/vnode.h>
     40  1.1   mycroft #include <sys/exec.h>
     41  1.4  christos #ifdef EXEC_ELF32
     42  1.4  christos # ifndef ELFSIZE
     43  1.4  christos #  define ELFSIZE 32
     44  1.4  christos # endif /* !ELFSIZE */
     45  1.4  christos # include <sys/exec_elf.h>
     46  1.4  christos #endif /* EXEC_ELF32 */
     47  1.1   mycroft #include <sys/resourcevar.h>
     48  1.1   mycroft #include <vm/vm.h>
     49  1.1   mycroft 
     50  1.1   mycroft #include <machine/freebsd_machdep.h>
     51  1.1   mycroft 
     52  1.4  christos #include <compat/freebsd/freebsd_exec.h>
     53  1.4  christos #include <compat/freebsd/freebsd_util.h>
     54  1.4  christos 
     55  1.1   mycroft #include <compat/freebsd/freebsd_syscall.h>
     56  1.1   mycroft 
     57  1.1   mycroft extern struct sysent freebsd_sysent[];
     58  1.1   mycroft extern char *freebsd_syscallnames[];
     59  1.1   mycroft 
     60  1.4  christos #ifdef EXEC_AOUT
     61  1.4  christos struct emul emul_freebsd_aout = {
     62  1.1   mycroft 	"freebsd",
     63  1.1   mycroft 	NULL,
     64  1.1   mycroft 	freebsd_sendsig,
     65  1.1   mycroft 	FREEBSD_SYS_syscall,
     66  1.1   mycroft 	FREEBSD_SYS_MAXSYSCALL,
     67  1.1   mycroft 	freebsd_sysent,
     68  1.1   mycroft 	freebsd_syscallnames,
     69  1.1   mycroft 	0,
     70  1.1   mycroft 	copyargs,
     71  1.3   mycroft 	freebsd_setregs,
     72  1.1   mycroft 	freebsd_sigcode,
     73  1.1   mycroft 	freebsd_esigcode,
     74  1.1   mycroft };
     75  1.4  christos #endif /* EXEC_AOUT */
     76  1.1   mycroft 
     77  1.4  christos #ifdef EXEC_ELF32
     78  1.4  christos 
     79  1.4  christos struct emul ELFNAMEEND(emul_freebsd) = {
     80  1.4  christos 	"freebsd",
     81  1.4  christos 	NULL,
     82  1.4  christos 	freebsd_sendsig,
     83  1.4  christos 	FREEBSD_SYS_syscall,
     84  1.4  christos 	FREEBSD_SYS_MAXSYSCALL,
     85  1.4  christos 	freebsd_sysent,
     86  1.4  christos 	freebsd_syscallnames,
     87  1.4  christos 	FREEBSD_ELF_AUX_ARGSIZ,
     88  1.4  christos 	ELFNAME(copyargs),
     89  1.4  christos 	freebsd_setregs,
     90  1.4  christos 	freebsd_sigcode,
     91  1.4  christos 	freebsd_esigcode,
     92  1.4  christos };
     93  1.4  christos 
     94  1.4  christos int
     95  1.4  christos ELFNAME2(freebsd,probe)(p, epp, eh, itp, pos)
     96  1.4  christos 	struct proc *p;
     97  1.4  christos 	struct exec_package *epp;
     98  1.4  christos 	Elf_Ehdr *eh;
     99  1.4  christos 	char *itp;
    100  1.4  christos 	Elf_Addr *pos;
    101  1.4  christos {
    102  1.4  christos 	int error;
    103  1.4  christos 	size_t i;
    104  1.4  christos 	size_t phsize;
    105  1.4  christos 	Elf_Phdr *ph;
    106  1.4  christos 	Elf_Phdr *ephp;
    107  1.5    kleink 	Elf_Nhdr *np;
    108  1.4  christos 	const char *bp;
    109  1.4  christos 
    110  1.4  christos         static const char wantBrand[] = FREEBSD_ELF_BRAND_STRING;
    111  1.4  christos         static const char wantInterp[] = FREEBSD_ELF_INTERP_PREFIX_STRING;
    112  1.4  christos 
    113  1.4  christos         /* Insist that the executable have a brand, and that it be "FreeBSD" */
    114  1.4  christos #ifndef EI_BRAND
    115  1.4  christos #define EI_BRAND 8
    116  1.4  christos #endif
    117  1.4  christos         if (eh->e_ident[EI_BRAND] == '\0'
    118  1.4  christos 	  || strcmp(&eh->e_ident[EI_BRAND], wantBrand))
    119  1.4  christos 		return ENOEXEC;
    120  1.4  christos 
    121  1.4  christos 	i = eh->e_phnum;
    122  1.4  christos 	if (i != 0) {
    123  1.4  christos 		phsize = i * sizeof(Elf_Phdr);
    124  1.4  christos 		ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
    125  1.4  christos 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
    126  1.4  christos 		    (caddr_t) ph, phsize)) != 0)
    127  1.4  christos 			goto bad1;
    128  1.4  christos 
    129  1.4  christos 		for (ephp = ph; i--; ephp++) {
    130  1.5    kleink 			if (ephp->p_type != PT_INTERP)
    131  1.4  christos 				continue;
    132  1.4  christos 
    133  1.4  christos 			/* Check for "legal" intepreter name. */
    134  1.4  christos 			if (ephp->p_filesz < sizeof wantInterp)
    135  1.4  christos 				goto bad1;
    136  1.4  christos 
    137  1.5    kleink 			np = (Elf_Nhdr *) malloc(ephp->p_filesz+1,
    138  1.4  christos 			    M_TEMP, M_WAITOK);
    139  1.4  christos 
    140  1.4  christos 			if (((error = ELFNAME(read_from)(p, epp->ep_vp,
    141  1.4  christos 			    ephp->p_offset, (caddr_t)np, ephp->p_filesz)) != 0))
    142  1.4  christos 				goto bad2;
    143  1.4  christos 
    144  1.4  christos 			if (strncmp((char *)np, wantInterp,
    145  1.4  christos 			    sizeof wantInterp - 1))
    146  1.4  christos 				goto bad2;
    147  1.4  christos 
    148  1.4  christos 			free(np, M_TEMP);
    149  1.4  christos 			break;
    150  1.4  christos 		}
    151  1.4  christos 		free(ph, M_TEMP);
    152  1.4  christos 	}
    153  1.4  christos 
    154  1.4  christos 	if (itp[0]) {
    155  1.4  christos 		if ((error = emul_find(p, NULL, freebsd_emul_path,
    156  1.4  christos 		    itp, &bp, 0)))
    157  1.4  christos 			return error;
    158  1.4  christos 		if ((error = copystr(bp, itp, MAXPATHLEN, &i)) != 0)
    159  1.4  christos 			return error;
    160  1.4  christos 		free((void *)bp, M_TEMP);
    161  1.4  christos 	}
    162  1.4  christos 	epp->ep_emul = &ELFNAMEEND(emul_freebsd);
    163  1.4  christos 	*pos = ELF_NO_ADDR;
    164  1.4  christos #ifdef DEBUG_FREEBSD_ELF
    165  1.4  christos 	printf("freebsd_elf32_probe: returning 0\n");
    166  1.4  christos #endif
    167  1.4  christos 	return 0;
    168  1.4  christos 
    169  1.4  christos bad2:
    170  1.4  christos 	free(np, M_TEMP);
    171  1.4  christos bad1:
    172  1.4  christos 	free(ph, M_TEMP);
    173  1.4  christos 	return ENOEXEC;
    174  1.4  christos }
    175  1.4  christos #endif /* EXEC_ELF32 */
    176  1.4  christos 
    177  1.4  christos 
    178  1.4  christos #ifdef EXEC_AOUT
    179  1.1   mycroft /*
    180  1.4  christos * exec_aout_makecmds(): Check if it's an a.out-format executable.
    181  1.4  christos *
    182  1.4  christos * Given a proc pointer and an exec package pointer, see if the referent
    183  1.4  christos * of the epp is in a.out format.  First check 'standard' magic numbers for
    184  1.4  christos * this architecture.  If that fails, try a cpu-dependent hook.
    185  1.1   mycroft  *
    186  1.1   mycroft  * This function, in the former case, or the hook, in the latter, is
    187  1.1   mycroft  * responsible for creating a set of vmcmds which can be used to build
    188  1.1   mycroft  * the process's vm space and inserting them into the exec package.
    189  1.1   mycroft  */
    190  1.1   mycroft 
    191  1.1   mycroft int
    192  1.1   mycroft exec_freebsd_aout_makecmds(p, epp)
    193  1.1   mycroft 	struct proc *p;
    194  1.1   mycroft 	struct exec_package *epp;
    195  1.1   mycroft {
    196  1.1   mycroft 	u_long midmag;
    197  1.1   mycroft 	int error = ENOEXEC;
    198  1.1   mycroft 	struct exec *execp = epp->ep_hdr;
    199  1.1   mycroft 
    200  1.1   mycroft 	if (epp->ep_hdrvalid < sizeof(struct exec))
    201  1.1   mycroft 		return ENOEXEC;
    202  1.1   mycroft 
    203  1.1   mycroft 	midmag = FREEBSD_N_GETMID(*execp) << 16 | FREEBSD_N_GETMAGIC(*execp);
    204  1.1   mycroft 
    205  1.1   mycroft 	/* assume FreeBSD's MID_MACHINE and [ZQNO]MAGIC is same as NetBSD's */
    206  1.1   mycroft 	switch (midmag) {
    207  1.1   mycroft 	case (MID_MACHINE << 16) | ZMAGIC:
    208  1.2  christos 		error = exec_aout_prep_oldzmagic(p, epp);
    209  1.1   mycroft 		break;
    210  1.1   mycroft 	case (MID_MACHINE << 16) | QMAGIC:
    211  1.1   mycroft 		error = exec_aout_prep_zmagic(p, epp);
    212  1.1   mycroft 		break;
    213  1.1   mycroft 	case (MID_MACHINE << 16) | NMAGIC:
    214  1.1   mycroft 		error = exec_aout_prep_nmagic(p, epp);
    215  1.1   mycroft 		break;
    216  1.1   mycroft 	case (MID_MACHINE << 16) | OMAGIC:
    217  1.1   mycroft 		error = exec_aout_prep_omagic(p, epp);
    218  1.1   mycroft 		break;
    219  1.1   mycroft 	}
    220  1.1   mycroft 	if (error == 0)
    221  1.4  christos 		epp->ep_emul = &emul_freebsd_aout;
    222  1.1   mycroft 	else
    223  1.1   mycroft 		kill_vmcmds(&epp->ep_vmcmds);
    224  1.1   mycroft 
    225  1.1   mycroft 	return error;
    226  1.1   mycroft }
    227  1.4  christos #endif /* EXEC_AOUT */
    228