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