linux_exec_elf32.c revision 1.63 1 /* $NetBSD: linux_exec_elf32.c,v 1.63 2003/06/29 11:02:25 darrenr Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998, 2000, 2001 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, Eric Haszlakiewicz and
9 * Emmanuel Dreyfus.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * based on exec_aout.c, sunos_exec.c and svr4_exec.c
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: linux_exec_elf32.c,v 1.63 2003/06/29 11:02:25 darrenr Exp $");
46
47 #ifndef ELFSIZE
48 /* XXX should die */
49 #define ELFSIZE 32
50 #endif
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/proc.h>
56 #include <sys/malloc.h>
57 #include <sys/namei.h>
58 #include <sys/vnode.h>
59 #include <sys/mount.h>
60 #include <sys/exec.h>
61 #include <sys/exec_elf.h>
62 #include <sys/stat.h>
63
64 #include <sys/mman.h>
65 #include <sys/sa.h>
66 #include <sys/syscallargs.h>
67
68 #include <machine/cpu.h>
69 #include <machine/reg.h>
70
71 #include <compat/linux/common/linux_types.h>
72 #include <compat/linux/common/linux_signal.h>
73 #include <compat/linux/common/linux_util.h>
74 #include <compat/linux/common/linux_exec.h>
75 #include <compat/linux/common/linux_machdep.h>
76
77 #include <compat/linux/linux_syscallargs.h>
78 #include <compat/linux/linux_syscall.h>
79
80 static int ELFNAME2(linux,signature) __P((struct lwp *, struct exec_package *,
81 Elf_Ehdr *, char *));
82 #ifdef LINUX_GCC_SIGNATURE
83 static int ELFNAME2(linux,gcc_signature) __P((struct lwp *l,
84 struct exec_package *, Elf_Ehdr *));
85 #endif
86 #ifdef LINUX_ATEXIT_SIGNATURE
87 static int ELFNAME2(linux,atexit_signature) __P((struct lwp *l,
88 struct exec_package *, Elf_Ehdr *));
89 #endif
90
91 #ifdef DEBUG_LINUX
92 #define DPRINTF(a) uprintf a
93 #else
94 #define DPRINTF(a)
95 #endif
96
97 #ifdef LINUX_ATEXIT_SIGNATURE
98 /*
99 * On the PowerPC, statically linked Linux binaries are not recognized
100 * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
101 * binaries features a __libc_atexit ELF section. We therefore assume we
102 * have a Linux binary if we find this section.
103 */
104 static int
105 ELFNAME2(linux,atexit_signature)(l, epp, eh)
106 struct lwp *l;
107 struct exec_package *epp;
108 Elf_Ehdr *eh;
109 {
110 size_t shsize;
111 int strndx;
112 size_t i;
113 static const char signature[] = "__libc_atexit";
114 char* strtable;
115 Elf_Shdr *sh;
116
117 int error;
118
119 /*
120 * load the section header table
121 */
122 shsize = eh->e_shnum * sizeof(Elf_Shdr);
123 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
124 error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
125 if (error)
126 goto out;
127
128 /*
129 * Now let's find the string table. If it does not exists, give up.
130 */
131 strndx = (int)(eh->e_shstrndx);
132 if (strndx == SHN_UNDEF) {
133 error = ENOEXEC;
134 goto out;
135 }
136
137 /*
138 * strndx is the index in section header table of the string table
139 * section get the whole string table in strtable, and then we get access to the names
140 * s->sh_name is the offset of the section name in strtable.
141 */
142 strtable = malloc(sh[strndx].sh_size, M_TEMP, M_WAITOK);
143 error = exec_read_from(l, epp->ep_vp, sh[strndx].sh_offset, strtable,
144 sh[strndx].sh_size);
145 if (error)
146 goto out;
147
148 for (i = 0; i < eh->e_shnum; i++) {
149 Elf_Shdr *s = &sh[i];
150 if (!memcmp((void*)(&(strtable[s->sh_name])), signature,
151 sizeof(signature))) {
152 DPRINTF(("linux_atexit_sig=%s\n",
153 &(strtable[s->sh_name])));
154 error = 0;
155 goto out;
156 }
157 }
158 error = ENOEXEC;
159
160 out:
161 free(sh, M_TEMP);
162 free(strtable, M_TEMP);
163 return (error);
164 }
165 #endif
166
167 #ifdef LINUX_GCC_SIGNATURE
168 /*
169 * Take advantage of the fact that all the linux binaries are compiled
170 * with gcc, and gcc sticks in the comment field a signature. Note that
171 * on SVR4 binaries, the gcc signature will follow the OS name signature,
172 * that will not be a problem. We don't bother to read in the string table,
173 * but we check all the progbits headers.
174 *
175 * XXX This only works in the i386. On the alpha (at least)
176 * XXX we have the same gcc signature which incorrectly identifies
177 * XXX NetBSD binaries as Linux.
178 */
179 static int
180 ELFNAME2(linux,gcc_signature)(l, epp, eh)
181 struct lwp *l;
182 struct exec_package *epp;
183 Elf_Ehdr *eh;
184 {
185 size_t shsize;
186 size_t i;
187 static const char signature[] = "\0GCC: (GNU) ";
188 char buf[sizeof(signature) - 1];
189 Elf_Shdr *sh;
190 int error;
191
192 shsize = eh->e_shnum * sizeof(Elf_Shdr);
193 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
194 error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
195 if (error)
196 goto out;
197
198 for (i = 0; i < eh->e_shnum; i++) {
199 Elf_Shdr *s = &sh[i];
200
201 /*
202 * Identify candidates for the comment header;
203 * Header cannot have a load address, or flags and
204 * it must be large enough.
205 */
206 if (s->sh_type != SHT_PROGBITS ||
207 s->sh_addr != 0 ||
208 s->sh_flags != 0 ||
209 s->sh_size < sizeof(signature) - 1)
210 continue;
211
212 error = exec_read_from(l, epp->ep_vp, s->sh_offset, buf,
213 sizeof(signature) - 1);
214 if (error)
215 continue;
216
217 /*
218 * error is 0, if the signatures match we are done.
219 */
220 DPRINTF(("linux_gcc_sig: sig=%s\n", buf));
221 if (!memcmp(buf, signature, sizeof(signature) - 1)) {
222 error = 0;
223 goto out;
224 }
225 }
226 error = ENOEXEC;
227
228 out:
229 free(sh, M_TEMP);
230 return (error);
231 }
232 #endif
233
234 static int
235 ELFNAME2(linux,signature)(l, epp, eh, itp)
236 struct lwp *l;
237 struct exec_package *epp;
238 Elf_Ehdr *eh;
239 char *itp;
240 {
241 size_t i;
242 Elf_Phdr *ph;
243 size_t phsize;
244 int error;
245 static const char linux[] = "Linux";
246
247 if (eh->e_ident[EI_OSABI] == 3 ||
248 memcmp(&eh->e_ident[EI_ABIVERSION], linux, sizeof(linux)) == 0)
249 return 0;
250
251 phsize = eh->e_phnum * sizeof(Elf_Phdr);
252 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
253 error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize);
254 if (error)
255 goto out;
256
257 for (i = 0; i < eh->e_phnum; i++) {
258 Elf_Phdr *ephp = &ph[i];
259 Elf_Nhdr *np;
260 u_int32_t *abi;
261
262 if (ephp->p_type != PT_NOTE ||
263 ephp->p_filesz > 1024 ||
264 ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
265 continue;
266
267 np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
268 error = exec_read_from(l, epp->ep_vp, ephp->p_offset, np,
269 ephp->p_filesz);
270 if (error)
271 goto next;
272
273 if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
274 np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
275 np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
276 memcmp((caddr_t)(np + 1), ELF_NOTE_ABI_NAME,
277 ELF_NOTE_ABI_NAMESZ))
278 goto next;
279
280 /* Make sure the OS is Linux. */
281 abi = (u_int32_t *)((caddr_t)np + sizeof(Elf_Nhdr) +
282 np->n_namesz);
283 if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
284 error = 0;
285 else
286 error = ENOEXEC;
287 free(np, M_TEMP);
288 goto out;
289
290 next:
291 free(np, M_TEMP);
292 continue;
293 }
294
295 /* Check for certain intepreter names. */
296 if (itp[0]) {
297 if (!strncmp(itp, "/lib/ld-linux", 13) ||
298 !strncmp(itp, "/lib/ld.so.", 11))
299 error = 0;
300 else
301 error = ENOEXEC;
302 goto out;
303 }
304
305 error = ENOEXEC;
306 out:
307 free(ph, M_TEMP);
308 return (error);
309 }
310
311 int
312 ELFNAME2(linux,probe)(l, epp, eh, itp, pos)
313 struct lwp *l;
314 struct exec_package *epp;
315 void *eh;
316 char *itp;
317 vaddr_t *pos;
318 {
319 int error;
320
321 if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
322 #ifdef LINUX_GCC_SIGNATURE
323 ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
324 #endif
325 #ifdef LINUX_ATEXIT_SIGNATURE
326 ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
327 #endif
328 1)
329 return error;
330
331 if (itp[0]) {
332 if ((error = emul_find_interp(l, epp->ep_esch->es_emul->e_path,
333 itp)))
334 return (error);
335 }
336 *pos = ELF_NO_ADDR;
337 DPRINTF(("linux_probe: returning 0\n"));
338 return 0;
339 }
340
341 #ifndef LINUX_MACHDEP_ELF_COPYARGS
342 /*
343 * Copy arguments onto the stack in the normal way, but add some
344 * extra information in case of dynamic binding.
345 */
346 int
347 ELFNAME2(linux,copyargs)(struct lwp *l, struct exec_package *pack,
348 struct ps_strings *arginfo, char **stackp, void *argp)
349 {
350 struct proc *p = l->l_proc;
351 size_t len;
352 AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
353 struct elf_args *ap;
354 int error;
355 struct vattr *vap;
356
357 if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
358 return error;
359
360 a = ai;
361
362 /*
363 * Push extra arguments used by glibc on the stack.
364 */
365
366 a->a_type = AT_PAGESZ;
367 a->a_v = PAGE_SIZE;
368 a++;
369
370 if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
371
372 a->a_type = AT_PHDR;
373 a->a_v = ap->arg_phaddr;
374 a++;
375
376 a->a_type = AT_PHENT;
377 a->a_v = ap->arg_phentsize;
378 a++;
379
380 a->a_type = AT_PHNUM;
381 a->a_v = ap->arg_phnum;
382 a++;
383
384 a->a_type = AT_BASE;
385 a->a_v = ap->arg_interp;
386 a++;
387
388 a->a_type = AT_FLAGS;
389 a->a_v = 0;
390 a++;
391
392 a->a_type = AT_ENTRY;
393 a->a_v = ap->arg_entry;
394 a++;
395
396 free(pack->ep_emul_arg, M_TEMP);
397 pack->ep_emul_arg = NULL;
398 }
399
400 /* Linux-specific items */
401 a->a_type = LINUX_AT_CLKTCK;
402 a->a_v = hz;
403 a++;
404
405 vap = pack->ep_vap;
406
407 a->a_type = LINUX_AT_UID;
408 a->a_v = p->p_cred->p_ruid;
409 a++;
410
411 a->a_type = LINUX_AT_EUID;
412 if (vap->va_mode & S_ISUID)
413 a->a_v = vap->va_uid;
414 else
415 a->a_v = p->p_ucred->cr_uid;
416 a++;
417
418 a->a_type = LINUX_AT_GID;
419 a->a_v = p->p_cred->p_rgid;
420 a++;
421
422 a->a_type = LINUX_AT_EGID;
423 if (vap->va_mode & S_ISGID)
424 a->a_v = vap->va_gid;
425 else
426 a->a_v = p->p_ucred->cr_gid;
427 a++;
428
429 a->a_type = AT_NULL;
430 a->a_v = 0;
431 a++;
432
433 len = (a - ai) * sizeof(AuxInfo);
434 if ((error = copyout(ai, *stackp, len)) != 0)
435 return error;
436 *stackp += len;
437
438 return 0;
439 }
440 #endif /* !LINUX_MACHDEP_ELF_COPYARGS */
441