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