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