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