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