linux_exec_elf32.c revision 1.70 1 /* $NetBSD: linux_exec_elf32.c,v 1.70 2005/12/11 12:20:19 christos 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.70 2005/12/11 12:20:19 christos 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 = NULL;
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 if (strtable)
163 free(strtable, M_TEMP);
164 return (error);
165 }
166 #endif
167
168 #ifdef LINUX_GCC_SIGNATURE
169 /*
170 * Take advantage of the fact that all the linux binaries are compiled
171 * with gcc, and gcc sticks in the comment field a signature. Note that
172 * on SVR4 binaries, the gcc signature will follow the OS name signature,
173 * that will not be a problem. We don't bother to read in the string table,
174 * but we check all the progbits headers.
175 *
176 * XXX This only works in the i386. On the alpha (at least)
177 * XXX we have the same gcc signature which incorrectly identifies
178 * XXX NetBSD binaries as Linux.
179 */
180 static int
181 ELFNAME2(linux,gcc_signature)(l, epp, eh)
182 struct lwp *l;
183 struct exec_package *epp;
184 Elf_Ehdr *eh;
185 {
186 size_t shsize;
187 size_t i;
188 static const char signature[] = "\0GCC: (GNU) ";
189 char tbuf[sizeof(signature) - 1];
190 Elf_Shdr *sh;
191 int error;
192
193 shsize = eh->e_shnum * sizeof(Elf_Shdr);
194 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
195 error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, shsize);
196 if (error)
197 goto out;
198
199 for (i = 0; i < eh->e_shnum; i++) {
200 Elf_Shdr *s = &sh[i];
201
202 /*
203 * Identify candidates for the comment header;
204 * Header cannot have a load address, or flags and
205 * it must be large enough.
206 */
207 if (s->sh_type != SHT_PROGBITS ||
208 s->sh_addr != 0 ||
209 s->sh_flags != 0 ||
210 s->sh_size < sizeof(signature) - 1)
211 continue;
212
213 error = exec_read_from(l, epp->ep_vp, s->sh_offset, tbuf,
214 sizeof(signature) - 1);
215 if (error)
216 continue;
217
218 /*
219 * error is 0, if the signatures match we are done.
220 */
221 DPRINTF(("linux_gcc_sig: sig=%s\n", tbuf));
222 if (!memcmp(tbuf, signature, sizeof(signature) - 1)) {
223 error = 0;
224 goto out;
225 }
226 }
227 error = ENOEXEC;
228
229 out:
230 free(sh, M_TEMP);
231 return (error);
232 }
233 #endif
234
235 static int
236 ELFNAME2(linux,signature)(l, epp, eh, itp)
237 struct lwp *l;
238 struct exec_package *epp;
239 Elf_Ehdr *eh;
240 char *itp;
241 {
242 size_t i;
243 Elf_Phdr *ph;
244 size_t phsize;
245 int error;
246 static const char linux[] = "Linux";
247
248 if (eh->e_ident[EI_OSABI] == 3 ||
249 memcmp(&eh->e_ident[EI_ABIVERSION], linux, sizeof(linux)) == 0)
250 return 0;
251
252 phsize = eh->e_phnum * sizeof(Elf_Phdr);
253 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
254 error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize);
255 if (error)
256 goto out;
257
258 for (i = 0; i < eh->e_phnum; i++) {
259 Elf_Phdr *ephp = &ph[i];
260 Elf_Nhdr *np;
261 u_int32_t *abi;
262
263 if (ephp->p_type != PT_NOTE ||
264 ephp->p_filesz > 1024 ||
265 ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
266 continue;
267
268 np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
269 error = exec_read_from(l, epp->ep_vp, ephp->p_offset, np,
270 ephp->p_filesz);
271 if (error)
272 goto next;
273
274 if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
275 np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
276 np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
277 memcmp((caddr_t)(np + 1), ELF_NOTE_ABI_NAME,
278 ELF_NOTE_ABI_NAMESZ))
279 goto next;
280
281 /* Make sure the OS is Linux. */
282 abi = (u_int32_t *)((caddr_t)np + sizeof(Elf_Nhdr) +
283 np->n_namesz);
284 if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
285 error = 0;
286 else
287 error = ENOEXEC;
288 free(np, M_TEMP);
289 goto out;
290
291 next:
292 free(np, M_TEMP);
293 continue;
294 }
295
296 /* Check for certain intepreter names. */
297 if (itp) {
298 if (!strncmp(itp, "/lib/ld-linux", 13) ||
299 #if (ELFSIZE == 64)
300 !strncmp(itp, "/lib64/ld-linux", 15) ||
301 #endif
302 !strncmp(itp, "/lib/ld.so.", 11))
303 error = 0;
304 else
305 error = ENOEXEC;
306 goto out;
307 }
308
309 error = ENOEXEC;
310 out:
311 free(ph, M_TEMP);
312 return (error);
313 }
314
315 int
316 ELFNAME2(linux,probe)(l, epp, eh, itp, pos)
317 struct lwp *l;
318 struct exec_package *epp;
319 void *eh;
320 char *itp;
321 vaddr_t *pos;
322 {
323 int error;
324
325 if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
326 #ifdef LINUX_GCC_SIGNATURE
327 ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
328 #endif
329 #ifdef LINUX_ATEXIT_SIGNATURE
330 ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
331 #endif
332 1)
333 return error;
334
335 if (itp) {
336 if ((error = emul_find_interp(l, epp->ep_esch->es_emul->e_path,
337 itp)))
338 return (error);
339 }
340 DPRINTF(("linux_probe: returning 0\n"));
341 return 0;
342 }
343
344 #ifndef LINUX_MACHDEP_ELF_COPYARGS
345 /*
346 * Copy arguments onto the stack in the normal way, but add some
347 * extra information in case of dynamic binding.
348 */
349 int
350 ELFNAME2(linux,copyargs)(struct lwp *l, struct exec_package *pack,
351 struct ps_strings *arginfo, char **stackp, void *argp)
352 {
353 struct proc *p = l->l_proc;
354 size_t len;
355 AuxInfo ai[LINUX_ELF_AUX_ENTRIES], *a;
356 struct elf_args *ap;
357 int error;
358 struct vattr *vap;
359
360 if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
361 return error;
362
363 a = ai;
364
365 /*
366 * Push extra arguments used by glibc on the stack.
367 */
368
369 a->a_type = AT_PAGESZ;
370 a->a_v = PAGE_SIZE;
371 a++;
372
373 if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
374
375 a->a_type = AT_PHDR;
376 a->a_v = ap->arg_phaddr;
377 a++;
378
379 a->a_type = AT_PHENT;
380 a->a_v = ap->arg_phentsize;
381 a++;
382
383 a->a_type = AT_PHNUM;
384 a->a_v = ap->arg_phnum;
385 a++;
386
387 a->a_type = AT_BASE;
388 a->a_v = ap->arg_interp;
389 a++;
390
391 a->a_type = AT_FLAGS;
392 a->a_v = 0;
393 a++;
394
395 a->a_type = AT_ENTRY;
396 a->a_v = ap->arg_entry;
397 a++;
398
399 free(pack->ep_emul_arg, M_TEMP);
400 pack->ep_emul_arg = NULL;
401 }
402
403 /* Linux-specific items */
404 a->a_type = LINUX_AT_CLKTCK;
405 a->a_v = hz;
406 a++;
407
408 vap = pack->ep_vap;
409
410 a->a_type = LINUX_AT_UID;
411 a->a_v = p->p_cred->p_ruid;
412 a++;
413
414 a->a_type = LINUX_AT_EUID;
415 if (vap->va_mode & S_ISUID)
416 a->a_v = vap->va_uid;
417 else
418 a->a_v = p->p_ucred->cr_uid;
419 a++;
420
421 a->a_type = LINUX_AT_GID;
422 a->a_v = p->p_cred->p_rgid;
423 a++;
424
425 a->a_type = LINUX_AT_EGID;
426 if (vap->va_mode & S_ISGID)
427 a->a_v = vap->va_gid;
428 else
429 a->a_v = p->p_ucred->cr_gid;
430 a++;
431
432 a->a_type = AT_NULL;
433 a->a_v = 0;
434 a++;
435
436 len = (a - ai) * sizeof(AuxInfo);
437 if ((error = copyout(ai, *stackp, len)) != 0)
438 return error;
439 *stackp += len;
440
441 return 0;
442 }
443 #endif /* !LINUX_MACHDEP_ELF_COPYARGS */
444