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