xref: /netbsd-src/sys/compat/freebsd/freebsd_exec.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: freebsd_exec.c,v 1.5 1999/10/25 13:55:07 kleink 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         /* Insist that the executable have a brand, and that it be "FreeBSD" */
114 #ifndef EI_BRAND
115 #define EI_BRAND 8
116 #endif
117         if (eh->e_ident[EI_BRAND] == '\0'
118 	  || strcmp(&eh->e_ident[EI_BRAND], wantBrand))
119 		return ENOEXEC;
120 
121 	i = eh->e_phnum;
122 	if (i != 0) {
123 		phsize = i * sizeof(Elf_Phdr);
124 		ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
125 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
126 		    (caddr_t) ph, phsize)) != 0)
127 			goto bad1;
128 
129 		for (ephp = ph; i--; ephp++) {
130 			if (ephp->p_type != PT_INTERP)
131 				continue;
132 
133 			/* Check for "legal" intepreter name. */
134 			if (ephp->p_filesz < sizeof wantInterp)
135 				goto bad1;
136 
137 			np = (Elf_Nhdr *) malloc(ephp->p_filesz+1,
138 			    M_TEMP, M_WAITOK);
139 
140 			if (((error = ELFNAME(read_from)(p, epp->ep_vp,
141 			    ephp->p_offset, (caddr_t)np, ephp->p_filesz)) != 0))
142 				goto bad2;
143 
144 			if (strncmp((char *)np, wantInterp,
145 			    sizeof wantInterp - 1))
146 				goto bad2;
147 
148 			free(np, M_TEMP);
149 			break;
150 		}
151 		free(ph, M_TEMP);
152 	}
153 
154 	if (itp[0]) {
155 		if ((error = emul_find(p, NULL, freebsd_emul_path,
156 		    itp, &bp, 0)))
157 			return error;
158 		if ((error = copystr(bp, itp, MAXPATHLEN, &i)) != 0)
159 			return error;
160 		free((void *)bp, M_TEMP);
161 	}
162 	epp->ep_emul = &ELFNAMEEND(emul_freebsd);
163 	*pos = ELF_NO_ADDR;
164 #ifdef DEBUG_FREEBSD_ELF
165 	printf("freebsd_elf32_probe: returning 0\n");
166 #endif
167 	return 0;
168 
169 bad2:
170 	free(np, M_TEMP);
171 bad1:
172 	free(ph, M_TEMP);
173 	return ENOEXEC;
174 }
175 #endif /* EXEC_ELF32 */
176 
177 
178 #ifdef EXEC_AOUT
179 /*
180 * exec_aout_makecmds(): Check if it's an a.out-format executable.
181 *
182 * Given a proc pointer and an exec package pointer, see if the referent
183 * of the epp is in a.out format.  First check 'standard' magic numbers for
184 * this architecture.  If that fails, try a cpu-dependent hook.
185  *
186  * This function, in the former case, or the hook, in the latter, is
187  * responsible for creating a set of vmcmds which can be used to build
188  * the process's vm space and inserting them into the exec package.
189  */
190 
191 int
192 exec_freebsd_aout_makecmds(p, epp)
193 	struct proc *p;
194 	struct exec_package *epp;
195 {
196 	u_long midmag;
197 	int error = ENOEXEC;
198 	struct exec *execp = epp->ep_hdr;
199 
200 	if (epp->ep_hdrvalid < sizeof(struct exec))
201 		return ENOEXEC;
202 
203 	midmag = FREEBSD_N_GETMID(*execp) << 16 | FREEBSD_N_GETMAGIC(*execp);
204 
205 	/* assume FreeBSD's MID_MACHINE and [ZQNO]MAGIC is same as NetBSD's */
206 	switch (midmag) {
207 	case (MID_MACHINE << 16) | ZMAGIC:
208 		error = exec_aout_prep_oldzmagic(p, epp);
209 		break;
210 	case (MID_MACHINE << 16) | QMAGIC:
211 		error = exec_aout_prep_zmagic(p, epp);
212 		break;
213 	case (MID_MACHINE << 16) | NMAGIC:
214 		error = exec_aout_prep_nmagic(p, epp);
215 		break;
216 	case (MID_MACHINE << 16) | OMAGIC:
217 		error = exec_aout_prep_omagic(p, epp);
218 		break;
219 	}
220 	if (error == 0)
221 		epp->ep_emul = &emul_freebsd_aout;
222 	else
223 		kill_vmcmds(&epp->ep_vmcmds);
224 
225 	return error;
226 }
227 #endif /* EXEC_AOUT */
228