Home | History | Annotate | Line # | Download | only in freebsd
freebsd_exec.c revision 1.4.8.1
      1  1.4.8.1    bouyer /*	$NetBSD: freebsd_exec.c,v 1.4.8.1 2000/11/20 18:08:10 bouyer 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 
     49      1.1   mycroft #include <machine/freebsd_machdep.h>
     50      1.1   mycroft 
     51      1.4  christos #include <compat/freebsd/freebsd_exec.h>
     52      1.4  christos #include <compat/freebsd/freebsd_util.h>
     53      1.4  christos 
     54      1.1   mycroft #include <compat/freebsd/freebsd_syscall.h>
     55      1.1   mycroft 
     56      1.1   mycroft extern struct sysent freebsd_sysent[];
     57      1.1   mycroft extern char *freebsd_syscallnames[];
     58      1.1   mycroft 
     59      1.4  christos #ifdef EXEC_AOUT
     60      1.4  christos struct emul emul_freebsd_aout = {
     61      1.1   mycroft 	"freebsd",
     62      1.1   mycroft 	NULL,
     63      1.1   mycroft 	freebsd_sendsig,
     64      1.1   mycroft 	FREEBSD_SYS_syscall,
     65      1.1   mycroft 	FREEBSD_SYS_MAXSYSCALL,
     66      1.1   mycroft 	freebsd_sysent,
     67      1.1   mycroft 	freebsd_syscallnames,
     68      1.1   mycroft 	0,
     69      1.1   mycroft 	copyargs,
     70      1.3   mycroft 	freebsd_setregs,
     71      1.1   mycroft 	freebsd_sigcode,
     72      1.1   mycroft 	freebsd_esigcode,
     73      1.1   mycroft };
     74      1.4  christos #endif /* EXEC_AOUT */
     75      1.1   mycroft 
     76      1.4  christos #ifdef EXEC_ELF32
     77      1.4  christos 
     78      1.4  christos struct emul ELFNAMEEND(emul_freebsd) = {
     79      1.4  christos 	"freebsd",
     80      1.4  christos 	NULL,
     81      1.4  christos 	freebsd_sendsig,
     82      1.4  christos 	FREEBSD_SYS_syscall,
     83      1.4  christos 	FREEBSD_SYS_MAXSYSCALL,
     84      1.4  christos 	freebsd_sysent,
     85      1.4  christos 	freebsd_syscallnames,
     86      1.4  christos 	FREEBSD_ELF_AUX_ARGSIZ,
     87      1.4  christos 	ELFNAME(copyargs),
     88      1.4  christos 	freebsd_setregs,
     89      1.4  christos 	freebsd_sigcode,
     90      1.4  christos 	freebsd_esigcode,
     91      1.4  christos };
     92      1.4  christos 
     93      1.4  christos int
     94      1.4  christos ELFNAME2(freebsd,probe)(p, epp, eh, itp, pos)
     95      1.4  christos 	struct proc *p;
     96      1.4  christos 	struct exec_package *epp;
     97      1.4  christos 	Elf_Ehdr *eh;
     98      1.4  christos 	char *itp;
     99      1.4  christos 	Elf_Addr *pos;
    100      1.4  christos {
    101      1.4  christos 	int error;
    102      1.4  christos 	size_t i;
    103      1.4  christos 	size_t phsize;
    104      1.4  christos 	Elf_Phdr *ph;
    105      1.4  christos 	Elf_Phdr *ephp;
    106  1.4.8.1    bouyer 	Elf_Nhdr *np;
    107      1.4  christos 	const char *bp;
    108      1.4  christos 
    109      1.4  christos         static const char wantBrand[] = FREEBSD_ELF_BRAND_STRING;
    110      1.4  christos         static const char wantInterp[] = FREEBSD_ELF_INTERP_PREFIX_STRING;
    111      1.4  christos 
    112      1.4  christos         /* Insist that the executable have a brand, and that it be "FreeBSD" */
    113      1.4  christos #ifndef EI_BRAND
    114      1.4  christos #define EI_BRAND 8
    115      1.4  christos #endif
    116      1.4  christos         if (eh->e_ident[EI_BRAND] == '\0'
    117      1.4  christos 	  || strcmp(&eh->e_ident[EI_BRAND], wantBrand))
    118      1.4  christos 		return ENOEXEC;
    119      1.4  christos 
    120      1.4  christos 	i = eh->e_phnum;
    121      1.4  christos 	if (i != 0) {
    122      1.4  christos 		phsize = i * sizeof(Elf_Phdr);
    123      1.4  christos 		ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
    124      1.4  christos 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
    125      1.4  christos 		    (caddr_t) ph, phsize)) != 0)
    126      1.4  christos 			goto bad1;
    127      1.4  christos 
    128      1.4  christos 		for (ephp = ph; i--; ephp++) {
    129  1.4.8.1    bouyer 			if (ephp->p_type != PT_INTERP)
    130      1.4  christos 				continue;
    131      1.4  christos 
    132      1.4  christos 			/* Check for "legal" intepreter name. */
    133      1.4  christos 			if (ephp->p_filesz < sizeof wantInterp)
    134      1.4  christos 				goto bad1;
    135      1.4  christos 
    136  1.4.8.1    bouyer 			np = (Elf_Nhdr *) malloc(ephp->p_filesz+1,
    137      1.4  christos 			    M_TEMP, M_WAITOK);
    138      1.4  christos 
    139      1.4  christos 			if (((error = ELFNAME(read_from)(p, epp->ep_vp,
    140      1.4  christos 			    ephp->p_offset, (caddr_t)np, ephp->p_filesz)) != 0))
    141      1.4  christos 				goto bad2;
    142      1.4  christos 
    143      1.4  christos 			if (strncmp((char *)np, wantInterp,
    144      1.4  christos 			    sizeof wantInterp - 1))
    145      1.4  christos 				goto bad2;
    146      1.4  christos 
    147      1.4  christos 			free(np, M_TEMP);
    148      1.4  christos 			break;
    149      1.4  christos 		}
    150      1.4  christos 		free(ph, M_TEMP);
    151      1.4  christos 	}
    152      1.4  christos 
    153      1.4  christos 	if (itp[0]) {
    154      1.4  christos 		if ((error = emul_find(p, NULL, freebsd_emul_path,
    155      1.4  christos 		    itp, &bp, 0)))
    156      1.4  christos 			return error;
    157      1.4  christos 		if ((error = copystr(bp, itp, MAXPATHLEN, &i)) != 0)
    158      1.4  christos 			return error;
    159      1.4  christos 		free((void *)bp, M_TEMP);
    160      1.4  christos 	}
    161      1.4  christos 	epp->ep_emul = &ELFNAMEEND(emul_freebsd);
    162      1.4  christos 	*pos = ELF_NO_ADDR;
    163      1.4  christos #ifdef DEBUG_FREEBSD_ELF
    164      1.4  christos 	printf("freebsd_elf32_probe: returning 0\n");
    165      1.4  christos #endif
    166      1.4  christos 	return 0;
    167      1.4  christos 
    168      1.4  christos bad2:
    169      1.4  christos 	free(np, M_TEMP);
    170      1.4  christos bad1:
    171      1.4  christos 	free(ph, M_TEMP);
    172      1.4  christos 	return ENOEXEC;
    173      1.4  christos }
    174      1.4  christos #endif /* EXEC_ELF32 */
    175      1.4  christos 
    176      1.4  christos 
    177      1.4  christos #ifdef EXEC_AOUT
    178      1.1   mycroft /*
    179      1.4  christos * exec_aout_makecmds(): Check if it's an a.out-format executable.
    180      1.4  christos *
    181      1.4  christos * Given a proc pointer and an exec package pointer, see if the referent
    182      1.4  christos * of the epp is in a.out format.  First check 'standard' magic numbers for
    183      1.4  christos * this architecture.  If that fails, try a cpu-dependent hook.
    184      1.1   mycroft  *
    185      1.1   mycroft  * This function, in the former case, or the hook, in the latter, is
    186      1.1   mycroft  * responsible for creating a set of vmcmds which can be used to build
    187      1.1   mycroft  * the process's vm space and inserting them into the exec package.
    188      1.1   mycroft  */
    189      1.1   mycroft 
    190      1.1   mycroft int
    191      1.1   mycroft exec_freebsd_aout_makecmds(p, epp)
    192      1.1   mycroft 	struct proc *p;
    193      1.1   mycroft 	struct exec_package *epp;
    194      1.1   mycroft {
    195      1.1   mycroft 	u_long midmag;
    196      1.1   mycroft 	int error = ENOEXEC;
    197      1.1   mycroft 	struct exec *execp = epp->ep_hdr;
    198      1.1   mycroft 
    199      1.1   mycroft 	if (epp->ep_hdrvalid < sizeof(struct exec))
    200      1.1   mycroft 		return ENOEXEC;
    201      1.1   mycroft 
    202      1.1   mycroft 	midmag = FREEBSD_N_GETMID(*execp) << 16 | FREEBSD_N_GETMAGIC(*execp);
    203      1.1   mycroft 
    204      1.1   mycroft 	/* assume FreeBSD's MID_MACHINE and [ZQNO]MAGIC is same as NetBSD's */
    205      1.1   mycroft 	switch (midmag) {
    206      1.1   mycroft 	case (MID_MACHINE << 16) | ZMAGIC:
    207      1.2  christos 		error = exec_aout_prep_oldzmagic(p, epp);
    208      1.1   mycroft 		break;
    209      1.1   mycroft 	case (MID_MACHINE << 16) | QMAGIC:
    210      1.1   mycroft 		error = exec_aout_prep_zmagic(p, epp);
    211      1.1   mycroft 		break;
    212      1.1   mycroft 	case (MID_MACHINE << 16) | NMAGIC:
    213      1.1   mycroft 		error = exec_aout_prep_nmagic(p, epp);
    214      1.1   mycroft 		break;
    215      1.1   mycroft 	case (MID_MACHINE << 16) | OMAGIC:
    216      1.1   mycroft 		error = exec_aout_prep_omagic(p, epp);
    217      1.1   mycroft 		break;
    218      1.1   mycroft 	}
    219      1.1   mycroft 	if (error == 0)
    220      1.4  christos 		epp->ep_emul = &emul_freebsd_aout;
    221      1.1   mycroft 	else
    222      1.1   mycroft 		kill_vmcmds(&epp->ep_vmcmds);
    223      1.1   mycroft 
    224      1.1   mycroft 	return error;
    225      1.1   mycroft }
    226      1.4  christos #endif /* EXEC_AOUT */
    227