linux_exec_elf32.c revision 1.39 1 /* $NetBSD: linux_exec_elf32.c,v 1.39 1998/10/23 10:54:58 veego Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas, Frank van der Linden and Eric Haszlakiewicz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * based on exec_aout.c, sunos_exec.c and svr4_exec.c
41 */
42
43 #ifndef ELFSIZE
44 #define ELFSIZE 32 /* XXX should die */
45 #endif
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/malloc.h>
52 #include <sys/namei.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/exec.h>
56 #include <sys/exec_elf.h>
57
58 #include <sys/mman.h>
59 #include <sys/syscallargs.h>
60
61 #include <vm/vm.h>
62 #include <vm/vm_param.h>
63 #include <vm/vm_map.h>
64
65 #include <machine/cpu.h>
66 #include <machine/reg.h>
67
68 #include <compat/linux/common/linux_types.h>
69 #include <compat/linux/common/linux_signal.h>
70 #include <compat/linux/common/linux_util.h>
71 #include <compat/linux/common/linux_exec.h>
72 #include <compat/linux/common/linux_machdep.h>
73 #include <compat/linux/common/linux_errno.h>
74
75 #include <compat/linux/linux_syscallargs.h>
76 #include <compat/linux/linux_syscall.h>
77
78 static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *,
79 Elf_Ehdr *));
80 #ifdef LINUX_GCC_SIGNATURE
81 static int ELFNAME2(linux,gcc_signature) __P((struct proc *p,
82 struct exec_package *, Elf_Ehdr *));
83 #endif
84
85 #define LINUX_ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
86
87
88 extern char linux_sigcode[], linux_esigcode[];
89 extern struct sysent linux_sysent[];
90 extern char *linux_syscallnames[];
91
92 struct emul ELFNAMEEND(emul_linux) = {
93 "linux",
94 native_to_linux_errno,
95 linux_sendsig,
96 LINUX_SYS_syscall,
97 LINUX_SYS_MAXSYSCALL,
98 linux_sysent,
99 linux_syscallnames,
100 LINUX_ELF_AUX_ARGSIZ,
101 ELFNAME(copyargs),
102 linux_setregs,
103 linux_sigcode,
104 linux_esigcode,
105 };
106
107
108 #ifdef LINUX_GCC_SIGNATURE
109 /*
110 * Take advantage of the fact that all the linux binaries are compiled
111 * with gcc, and gcc sticks in the comment field a signature. Note that
112 * on SVR4 binaries, the gcc signature will follow the OS name signature,
113 * that will not be a problem. We don't bother to read in the string table,
114 * but we check all the progbits headers.
115 *
116 * XXX This only works in the i386. On the alpha (at least)
117 * XXX we have the same gcc signature which incorrectly identifies
118 * XXX NetBSD binaries as Linux.
119 */
120 static int
121 ELFNAME2(linux,gcc_signature)(p, epp, eh)
122 struct proc *p;
123 struct exec_package *epp;
124 Elf_Ehdr *eh;
125 {
126 size_t shsize = sizeof(Elf_Shdr) * eh->e_shnum;
127 size_t i;
128 static const char signature[] = "\0GCC: (GNU) ";
129 char buf[sizeof(signature) - 1];
130 Elf_Shdr *sh;
131 int error;
132
133 error = ENOEXEC;
134 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
135
136 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_shoff,
137 (caddr_t) sh, shsize)) != 0)
138 goto out;
139
140 for (i = 0; i < eh->e_shnum; i++) {
141 Elf_Shdr *s = &sh[i];
142
143 /*
144 * Identify candidates for the comment header;
145 * Header cannot have a load address, or flags and
146 * it must be large enough.
147 */
148 if (s->sh_type != Elf_sht_progbits ||
149 s->sh_addr != 0 ||
150 s->sh_flags != 0 ||
151 s->sh_size < sizeof(signature) - 1)
152 continue;
153
154 if ((error = ELFNAME(read_from)(p, epp->ep_vp, s->sh_offset,
155 (caddr_t) buf, sizeof(signature) - 1)) != 0)
156 goto out;
157
158 /*
159 * error is 0, if the signatures match we are done.
160 */
161 #ifdef DEBUG_LINUX
162 printf("linux_gcc_sig: sig=%s\n", buf);
163 #endif
164 if (memcmp(buf, signature, sizeof(signature) - 1) == 0)
165 goto out;
166 }
167
168 out:
169 free(sh, M_TEMP);
170 #ifdef DEBUG_LINUX
171 printf("linux_gcc_sig: returning %d\n", error);
172 #endif
173 return error;
174 }
175 #endif
176
177 static int
178 ELFNAME2(linux,signature)(p, epp, eh)
179 struct proc *p;
180 struct exec_package *epp;
181 Elf_Ehdr *eh;
182 {
183 size_t i;
184 Elf_Phdr *ph;
185 Elf_Note *notep;
186 char *testp;
187 size_t phsize;
188 int error = ENOEXEC;
189
190 phsize = eh->e_phnum * sizeof(Elf_Phdr);
191 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
192 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
193 (caddr_t) ph, phsize)) != 0)
194 goto out1;
195
196 for (i = 0; i < eh->e_phnum; i++) {
197 Elf_Phdr *ephp = &ph[i];
198 u_int32_t ostype;
199
200 if (ephp->p_type != Elf_pt_interp /* XAX pt_note */
201 #if 0
202 || ephp->p_flags != 0
203 || ephp->p_filesz < sizeof(Elf_Note))
204 #endif
205 )
206 continue;
207
208 notep = (Elf_Note *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
209 if ((error = ELFNAME(read_from)(p, epp->ep_vp, ephp->p_offset,
210 (caddr_t)notep, ephp->p_filesz)) != 0)
211 goto out3;
212
213 testp = (char *)notep;
214 testp[16] = '\0';
215 #ifdef DEBUG_LINUX
216 printf("linux_signature: interp=%s\n", testp);
217 #endif
218 if (strncmp(&testp[8], "linux", 5) == 0) {
219 error = 0;
220 goto out3;
221 }
222
223 goto out2;
224
225 /* XXX XAX Should handle NETBSD_TYPE_EMULNAME */
226 if (notep->type != ELF_NOTE_TYPE_OSVERSION) {
227 free(notep, M_TEMP);
228 continue;
229 }
230
231 /* Check the name and description sizes. */
232 if (notep->namesz != ELF_NOTE_GNU_NAMESZ ||
233 notep->descsz != ELF_NOTE_GNU_DESCSZ)
234 goto out2;
235
236 /* Is the name "GNU\0"? */
237 if (memcmp((notep + sizeof(Elf_Note)),
238 ELF_NOTE_GNU_NAME, ELF_NOTE_GNU_NAMESZ))
239 goto out2;
240
241 /* Make sure the OS is Linux */
242 ostype = (u_int32_t)(*((u_int32_t *)notep + sizeof(Elf_Note) +
243 notep->namesz)) & ELF_NOTE_GNU_OSMASK;
244 if (ostype != ELF_NOTE_GNU_OSLINUX)
245 goto out2;
246
247 /* All checks succeeded. */
248 error = 0;
249 goto out3;
250 }
251
252 error = ENOEXEC;
253
254 out1:
255 free(ph, M_TEMP);
256 #ifdef DEBUG_LINUX
257 printf("linux_signature: out1=%d\n", error);
258 #endif
259 return error;
260
261 out2:
262 error = ENOEXEC;
263 out3:
264 free(notep, M_TEMP);
265 free(ph, M_TEMP);
266 #ifdef DEBUG_LINUX
267 printf("linux_signature: out2,3=%d\n", error);
268 #endif
269 return error;
270 }
271
272 int
273 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
274 struct proc *p;
275 struct exec_package *epp;
276 Elf_Ehdr *eh;
277 char *itp;
278 Elf_Addr *pos;
279 {
280 char *bp;
281 int error;
282 size_t len;
283
284 if ((error = ELFNAME2(linux,signature)(p, epp, eh)) != 0)
285 #ifdef LINUX_GCC_SIGNATURE
286 if ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0)
287 return error;
288 #else
289 return error;
290 #endif
291
292 if (itp[0]) {
293 if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
294 return error;
295 if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
296 return error;
297 free(bp, M_TEMP);
298 }
299 epp->ep_emul = &ELFNAMEEND(emul_linux);
300 *pos = ELF_NO_ADDR;
301 #ifdef DEBUG_LINUX
302 printf("linux_probe: returning 0\n");
303 #endif
304 return 0;
305 }
306
307