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