linux_exec_elf32.c revision 1.42.2.1 1 /* $NetBSD: linux_exec_elf32.c,v 1.42.2.1 2000/11/20 18:08:22 bouyer 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 <machine/cpu.h>
62 #include <machine/reg.h>
63
64 #include <compat/linux/common/linux_types.h>
65 #include <compat/linux/common/linux_signal.h>
66 #include <compat/linux/common/linux_util.h>
67 #include <compat/linux/common/linux_exec.h>
68 #include <compat/linux/common/linux_machdep.h>
69 #include <compat/linux/common/linux_errno.h>
70
71 #include <compat/linux/linux_syscallargs.h>
72 #include <compat/linux/linux_syscall.h>
73
74 static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *,
75 Elf_Ehdr *));
76 #ifdef LINUX_GCC_SIGNATURE
77 static int ELFNAME2(linux,gcc_signature) __P((struct proc *p,
78 struct exec_package *, Elf_Ehdr *));
79 #endif
80
81 #define LINUX_ELF_AUX_ARGSIZ howmany(sizeof(AuxInfo) * 8, sizeof(char *))
82
83
84 extern char linux_sigcode[], linux_esigcode[];
85 extern struct sysent linux_sysent[];
86 extern char *linux_syscallnames[];
87
88 struct emul ELFNAMEEND(emul_linux) = {
89 "linux",
90 native_to_linux_errno,
91 linux_sendsig,
92 LINUX_SYS_syscall,
93 LINUX_SYS_MAXSYSCALL,
94 linux_sysent,
95 linux_syscallnames,
96 LINUX_ELF_AUX_ARGSIZ,
97 ELFNAME(copyargs),
98 linux_setregs,
99 linux_sigcode,
100 linux_esigcode,
101 };
102
103
104 #ifdef LINUX_GCC_SIGNATURE
105 /*
106 * Take advantage of the fact that all the linux binaries are compiled
107 * with gcc, and gcc sticks in the comment field a signature. Note that
108 * on SVR4 binaries, the gcc signature will follow the OS name signature,
109 * that will not be a problem. We don't bother to read in the string table,
110 * but we check all the progbits headers.
111 *
112 * XXX This only works in the i386. On the alpha (at least)
113 * XXX we have the same gcc signature which incorrectly identifies
114 * XXX NetBSD binaries as Linux.
115 */
116 static int
117 ELFNAME2(linux,gcc_signature)(p, epp, eh)
118 struct proc *p;
119 struct exec_package *epp;
120 Elf_Ehdr *eh;
121 {
122 size_t shsize = sizeof(Elf_Shdr) * eh->e_shnum;
123 size_t i;
124 static const char signature[] = "\0GCC: (GNU) ";
125 char buf[sizeof(signature) - 1];
126 Elf_Shdr *sh;
127 int error;
128
129 error = ENOEXEC;
130 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
131
132 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_shoff,
133 (caddr_t) sh, shsize)) != 0)
134 goto out;
135
136 for (i = 0; i < eh->e_shnum; i++) {
137 Elf_Shdr *s = &sh[i];
138
139 /*
140 * Identify candidates for the comment header;
141 * Header cannot have a load address, or flags and
142 * it must be large enough.
143 */
144 if (s->sh_type != SHT_PROGBITS ||
145 s->sh_addr != 0 ||
146 s->sh_flags != 0 ||
147 s->sh_size < sizeof(signature) - 1)
148 continue;
149
150 if ((error = ELFNAME(read_from)(p, epp->ep_vp, s->sh_offset,
151 (caddr_t) buf, sizeof(signature) - 1)) != 0)
152 goto out;
153
154 /*
155 * error is 0, if the signatures match we are done.
156 */
157 #ifdef DEBUG_LINUX
158 printf("linux_gcc_sig: sig=%s\n", buf);
159 #endif
160 if (memcmp(buf, signature, sizeof(signature) - 1) == 0)
161 goto out;
162 }
163
164 out:
165 free(sh, M_TEMP);
166 #ifdef DEBUG_LINUX
167 printf("linux_gcc_sig: returning %d\n", error);
168 #endif
169 return error;
170 }
171 #endif
172
173 static int
174 ELFNAME2(linux,signature)(p, epp, eh)
175 struct proc *p;
176 struct exec_package *epp;
177 Elf_Ehdr *eh;
178 {
179 size_t i;
180 Elf_Phdr *ph;
181 Elf_Nhdr *notep;
182 size_t phsize;
183 int error = ENOEXEC;
184
185 phsize = eh->e_phnum * sizeof(Elf_Phdr);
186 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
187 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
188 (caddr_t) ph, phsize)) != 0)
189 goto out1;
190
191 for (i = 0; i < eh->e_phnum; i++) {
192 Elf_Phdr *ephp = &ph[i];
193 u_int32_t ostype;
194
195 if (ephp->p_type != PT_INTERP /* XAX PT_NOTE */
196 #if 0
197 || ephp->p_flags != 0
198 || ephp->p_filesz < sizeof(Elf_Nhdr))
199 #endif
200 )
201 continue;
202
203 notep = (Elf_Nhdr *)malloc(ephp->p_filesz+1, M_TEMP, M_WAITOK);
204 if ((error = ELFNAME(read_from)(p, epp->ep_vp, ephp->p_offset,
205 (caddr_t)notep, ephp->p_filesz)) != 0)
206 goto out3;
207
208 /* Check for "linux" in the intepreter name. */
209 if (ephp->p_filesz < 8 + 5) {
210 error = ENOEXEC;
211 goto out3;
212 }
213 #ifdef DEBUG_LINUX
214 printf("linux_signature: interp=%s\n", notep);
215 #endif
216 if (strncmp(&((char *)notep)[8], "linux", 5) == 0 ||
217 strncmp((char *)notep, "/lib/ld.so.", 11) == 0) {
218 error = 0;
219 goto out3;
220 }
221
222 goto out2;
223
224 /* XXX XAX Should handle NETBSD_TYPE_EMULNAME */
225 if (notep->n_type != ELF_NOTE_TYPE_OSVERSION) {
226 free(notep, M_TEMP);
227 continue;
228 }
229
230 /* Check the name and description sizes. */
231 if (notep->n_namesz != ELF_NOTE_GNU_NAMESZ ||
232 notep->n_descsz != ELF_NOTE_GNU_DESCSZ)
233 goto out2;
234
235 /* Is the name "GNU\0"? */
236 if (memcmp((notep + sizeof(Elf_Nhdr)),
237 ELF_NOTE_GNU_NAME, ELF_NOTE_GNU_NAMESZ))
238 goto out2;
239
240 /* Make sure the OS is Linux */
241 ostype = (u_int32_t)(*((u_int32_t *)notep + sizeof(Elf_Nhdr) +
242 notep->n_namesz)) & ELF_NOTE_GNU_OSMASK;
243 if (ostype != ELF_NOTE_GNU_OSLINUX)
244 goto out2;
245
246 /* All checks succeeded. */
247 error = 0;
248 goto out3;
249 }
250
251 error = ENOEXEC;
252
253 out1:
254 free(ph, M_TEMP);
255 #ifdef DEBUG_LINUX
256 printf("linux_signature: out1=%d\n", error);
257 #endif
258 return error;
259
260 out2:
261 error = ENOEXEC;
262 out3:
263 free(notep, M_TEMP);
264 free(ph, M_TEMP);
265 #ifdef DEBUG_LINUX
266 printf("linux_signature: out2,3=%d\n", error);
267 #endif
268 return error;
269 }
270
271 int
272 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
273 struct proc *p;
274 struct exec_package *epp;
275 Elf_Ehdr *eh;
276 char *itp;
277 Elf_Addr *pos;
278 {
279 const char *bp;
280 int error;
281 size_t len;
282
283 if ((error = ELFNAME2(linux,signature)(p, epp, eh)) != 0)
284 #ifdef LINUX_GCC_SIGNATURE
285 if ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0)
286 return error;
287 #else
288 return error;
289 #endif
290
291 if (itp[0]) {
292 if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
293 return error;
294 if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
295 return error;
296 free((void *)bp, M_TEMP);
297 }
298 epp->ep_emul = &ELFNAMEEND(emul_linux);
299 *pos = ELF_NO_ADDR;
300 #ifdef DEBUG_LINUX
301 printf("linux_probe: returning 0\n");
302 #endif
303 return 0;
304 }
305
306