linux_exec_elf32.c revision 1.34 1 /* $NetBSD: linux_exec_elf32.c,v 1.34 1998/10/04 00:02:32 fvdl 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_siginfo.h>
71 #include <compat/linux/common/linux_util.h>
72 #include <compat/linux/common/linux_exec.h>
73 #include <compat/linux/common/linux_machdep.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 int linux_error[];
89 extern char linux_sigcode[], linux_esigcode[];
90 extern struct sysent linux_sysent[];
91 extern char *linux_syscallnames[];
92
93 struct emul ELFNAMEEND(emul_linux) = {
94 "linux",
95 linux_error,
96 linux_sendsig,
97 LINUX_SYS_syscall,
98 LINUX_SYS_MAXSYSCALL,
99 linux_sysent,
100 linux_syscallnames,
101 LINUX_ELF_AUX_ARGSIZ,
102 ELFNAME(copyargs),
103 linux_setregs,
104 linux_sigcode,
105 linux_esigcode,
106 };
107
108
109 #ifdef LINUX_GCC_SIGNATURE
110 /*
111 * Take advantage of the fact that all the linux binaries are compiled
112 * with gcc, and gcc sticks in the comment field a signature. Note that
113 * on SVR4 binaries, the gcc signature will follow the OS name signature,
114 * that will not be a problem. We don't bother to read in the string table,
115 * but we check all the progbits headers.
116 *
117 * XXX This only works in the i386. On the alpha (at least)
118 * XXX we have the same gcc signature which incorrectly identifies
119 * XXX NetBSD binaries as Linux.
120 */
121 static int
122 ELFNAME2(linux,gcc_signature)(p, epp, eh)
123 struct proc *p;
124 struct exec_package *epp;
125 Elf_Ehdr *eh;
126 {
127 size_t shsize = sizeof(Elf_Shdr) * eh->e_shnum;
128 size_t i;
129 static const char signature[] = "\0GCC: (GNU) ";
130 char buf[sizeof(signature) - 1];
131 Elf_Shdr *sh;
132 int error;
133
134 error = ENOEXEC;
135 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
136
137 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_shoff,
138 (caddr_t) sh, shsize)) != 0)
139 goto out;
140
141 for (i = 0; i < eh->e_shnum; i++) {
142 Elf_Shdr *s = &sh[i];
143
144 /*
145 * Identify candidates for the comment header;
146 * Header cannot have a load address, or flags and
147 * it must be large enough.
148 */
149 if (s->sh_type != Elf_sht_progbits ||
150 s->sh_addr != 0 ||
151 s->sh_flags != 0 ||
152 s->sh_size < sizeof(signature) - 1)
153 continue;
154
155 if ((error = ELFNAME(read_from)(p, epp->ep_vp, s->sh_offset,
156 (caddr_t) buf, sizeof(signature) - 1)) != 0)
157 goto out;
158
159 /*
160 * error is 0, if the signatures match we are done.
161 */
162 if (memcmp(buf, signature, sizeof(signature) - 1) == 0)
163 goto out;
164 }
165
166 out:
167 free(sh, M_TEMP);
168 return error;
169 }
170 #endif
171
172 static int
173 ELFNAME2(linux,signature)(p, epp, eh)
174 struct proc *p;
175 struct exec_package *epp;
176 Elf_Ehdr *eh;
177 {
178 size_t i;
179 Elf_Phdr *ph;
180 Elf_Note *notep;
181 char *testp;
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 != Elf_pt_interp /* XAX pt_note */
196 #if 0
197 || ephp->p_flags != 0
198 || ephp->p_filesz < sizeof(Elf_Note))
199 #endif
200 )
201 continue;
202
203 notep = (Elf_Note *)malloc(ephp->p_filesz, 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 testp = (char *)notep;
209 testp[16] = '\0';
210 if (strncmp(&testp[8], "linux", 5) != 0) {
211 error = 0;
212 goto out3;
213 }
214
215 goto out2;
216
217 /* XXX XAX Should handle NETBSD_TYPE_EMULNAME */
218 if (notep->type != ELF_NOTE_TYPE_OSVERSION) {
219 free(notep, M_TEMP);
220 continue;
221 }
222
223 /* Check the name and description sizes. */
224 if (notep->namesz != ELF_NOTE_GNU_NAMESZ ||
225 notep->descsz != ELF_NOTE_GNU_DESCSZ)
226 goto out2;
227
228 /* Is the name "GNU\0"? */
229 if (memcmp((notep + sizeof(Elf_Note)),
230 ELF_NOTE_GNU_NAME, ELF_NOTE_GNU_NAMESZ))
231 goto out2;
232
233 /* Make sure the OS is Linux */
234 ostype = (u_int32_t)(*((u_int32_t *)notep + sizeof(Elf_Note) +
235 notep->namesz)) & ELF_NOTE_GNU_OSMASK;
236 if (ostype != ELF_NOTE_GNU_OSLINUX)
237 goto out2;
238
239 /* All checks succeeded. */
240 error = 0;
241 goto out3;
242 }
243
244 error = ENOEXEC;
245
246 out1:
247 free(ph, M_TEMP);
248 return error;
249
250 out2:
251 error = ENOEXEC;
252 out3:
253 free(notep, M_TEMP);
254 free(ph, M_TEMP);
255 return error;
256 }
257
258 int
259 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
260 struct proc *p;
261 struct exec_package *epp;
262 Elf_Ehdr *eh;
263 char *itp;
264 Elf_Addr *pos;
265 {
266 char *bp;
267 int error;
268 size_t len;
269
270 if ((error = ELFNAME2(linux,signature)(p, epp, eh)) != 0)
271 #ifdef LINUX_GCC_SIGNATURE
272 if ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0)
273 return error;
274 #else
275 return error;
276 #endif
277
278 if (itp[0]) {
279 if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
280 return error;
281 if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
282 return error;
283 free(bp, M_TEMP);
284 }
285 epp->ep_emul = &ELFNAMEEND(emul_linux);
286 *pos = ELF_NO_ADDR;
287 return 0;
288 }
289
290