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