1 /* $NetBSD: linux_exec_elf32.c,v 1.82 2008/11/20 09:26:06 ad Exp $ */
4 * Copyright (c) 1995, 1998, 2000, 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * based on exec_aout.c, sunos_exec.c and svr4_exec.c
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: linux_exec_elf32.c,v 1.82 2008/11/20 09:26:06 ad Exp $");
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
54 #include <sys/exec_elf.h>
56 #include <sys/kauth.h>
59 #include <sys/syscallargs.h>
62 #include <machine/reg.h>
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_ipc.h>
70 #include <compat/linux/common/linux_sem.h>
72 #include <compat/linux/linux_syscallargs.h>
73 #include <compat/linux/linux_syscall.h>
76 #define DPRINTF(a) uprintf a
81 #ifdef LINUX_ATEXIT_SIGNATURE
83 * On the PowerPC, statically linked Linux binaries are not recognized
84 * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
85 * binaries features a __libc_atexit ELF section. We therefore assume we
86 * have a Linux binary if we find this section.
89 ELFNAME2(linux
,atexit_signature
)(l
, epp
, eh
)
91 struct exec_package
*epp
;
97 static const char signature
[] = "__libc_atexit";
98 char *strtable
= NULL
;
104 * load the section header table
106 shsize
= eh
->e_shnum
* sizeof(Elf_Shdr
);
107 sh
= (Elf_Shdr
*) malloc(shsize
, M_TEMP
, M_WAITOK
);
108 error
= exec_read_from(l
, epp
->ep_vp
, eh
->e_shoff
, sh
, shsize
);
113 * Now let's find the string table. If it does not exists, give up.
115 strndx
= (int)(eh
->e_shstrndx
);
116 if (strndx
== SHN_UNDEF
) {
122 * strndx is the index in section header table of the string table
123 * section get the whole string table in strtable, and then we get access to the names
124 * s->sh_name is the offset of the section name in strtable.
126 strtable
= malloc(sh
[strndx
].sh_size
, M_TEMP
, M_WAITOK
);
127 error
= exec_read_from(l
, epp
->ep_vp
, sh
[strndx
].sh_offset
, strtable
,
132 for (i
= 0; i
< eh
->e_shnum
; i
++) {
133 Elf_Shdr
*s
= &sh
[i
];
134 if (!memcmp((void*)(&(strtable
[s
->sh_name
])), signature
,
135 sizeof(signature
))) {
136 DPRINTF(("linux_atexit_sig=%s\n",
137 &(strtable
[s
->sh_name
])));
147 free(strtable
, M_TEMP
);
152 #ifdef LINUX_GCC_SIGNATURE
154 * Take advantage of the fact that all the linux binaries are compiled
155 * with gcc, and gcc sticks in the comment field a signature. Note that
156 * on SVR4 binaries, the gcc signature will follow the OS name signature,
157 * that will not be a problem. We don't bother to read in the string table,
158 * but we check all the progbits headers.
160 * XXX This only works in the i386. On the alpha (at least)
161 * XXX we have the same gcc signature which incorrectly identifies
162 * XXX NetBSD binaries as Linux.
165 ELFNAME2(linux
,gcc_signature
)(l
, epp
, eh
)
167 struct exec_package
*epp
;
172 static const char signature
[] = "\0GCC: (GNU) ";
173 char tbuf
[sizeof(signature
) - 1];
177 shsize
= eh
->e_shnum
* sizeof(Elf_Shdr
);
178 sh
= (Elf_Shdr
*) malloc(shsize
, M_TEMP
, M_WAITOK
);
179 error
= exec_read_from(l
, epp
->ep_vp
, eh
->e_shoff
, sh
, shsize
);
183 for (i
= 0; i
< eh
->e_shnum
; i
++) {
184 Elf_Shdr
*s
= &sh
[i
];
187 * Identify candidates for the comment header;
188 * Header cannot have a load address, or flags and
189 * it must be large enough.
191 if (s
->sh_type
!= SHT_PROGBITS
||
194 s
->sh_size
< sizeof(signature
) - 1)
197 error
= exec_read_from(l
, epp
->ep_vp
, s
->sh_offset
, tbuf
,
198 sizeof(signature
) - 1);
203 * error is 0, if the signatures match we are done.
205 DPRINTF(("linux_gcc_sig: sig=%s\n", tbuf
));
206 if (!memcmp(tbuf
, signature
, sizeof(signature
) - 1)) {
219 #ifdef LINUX_DEBUGLINK_SIGNATURE
221 * Look for a .gnu_debuglink, specific to x86_64 interpeter
224 ELFNAME2(linux
,debuglink_signature
)(struct lwp
*l
, struct exec_package
*epp
, Elf_Ehdr
*eh
)
229 static const char signature
[] = ".gnu_debuglink";
230 char *strtable
= NULL
;
236 * load the section header table
238 shsize
= eh
->e_shnum
* sizeof(Elf_Shdr
);
239 sh
= (Elf_Shdr
*) malloc(shsize
, M_TEMP
, M_WAITOK
);
240 error
= exec_read_from(l
, epp
->ep_vp
, eh
->e_shoff
, sh
, shsize
);
245 * Now let's find the string table. If it does not exists, give up.
247 strndx
= (int)(eh
->e_shstrndx
);
248 if (strndx
== SHN_UNDEF
) {
254 * strndx is the index in section header table of the string table
255 * section get the whole string table in strtable, and then we get access to the names
256 * s->sh_name is the offset of the section name in strtable.
258 strtable
= malloc(sh
[strndx
].sh_size
, M_TEMP
, M_WAITOK
);
259 error
= exec_read_from(l
, epp
->ep_vp
, sh
[strndx
].sh_offset
, strtable
,
264 for (i
= 0; i
< eh
->e_shnum
; i
++) {
265 Elf_Shdr
*s
= &sh
[i
];
267 if (!memcmp((void*)(&(strtable
[s
->sh_name
])), signature
,
268 sizeof(signature
))) {
269 DPRINTF(("linux_debuglink_sig=%s\n",
270 &(strtable
[s
->sh_name
])));
280 free(strtable
, M_TEMP
);
286 ELFNAME2(linux
,signature
)(struct lwp
*l
, struct exec_package
*epp
, Elf_Ehdr
*eh
, char *itp
)
292 static const char linux
[] = "Linux";
294 if (eh
->e_ident
[EI_OSABI
] == 3 ||
295 memcmp(&eh
->e_ident
[EI_ABIVERSION
], linux
, sizeof(linux
)) == 0)
298 phsize
= eh
->e_phnum
* sizeof(Elf_Phdr
);
299 ph
= (Elf_Phdr
*)malloc(phsize
, M_TEMP
, M_WAITOK
);
300 error
= exec_read_from(l
, epp
->ep_vp
, eh
->e_phoff
, ph
, phsize
);
304 for (i
= 0; i
< eh
->e_phnum
; i
++) {
305 Elf_Phdr
*ephp
= &ph
[i
];
309 if (ephp
->p_type
!= PT_NOTE
||
310 ephp
->p_filesz
> 1024 ||
311 ephp
->p_filesz
< sizeof(Elf_Nhdr
) + 20)
314 np
= (Elf_Nhdr
*)malloc(ephp
->p_filesz
, M_TEMP
, M_WAITOK
);
315 error
= exec_read_from(l
, epp
->ep_vp
, ephp
->p_offset
, np
,
320 if (np
->n_type
!= ELF_NOTE_TYPE_ABI_TAG
||
321 np
->n_namesz
!= ELF_NOTE_ABI_NAMESZ
||
322 np
->n_descsz
!= ELF_NOTE_ABI_DESCSZ
||
323 memcmp((void *)(np
+ 1), ELF_NOTE_ABI_NAME
,
324 ELF_NOTE_ABI_NAMESZ
))
327 /* Make sure the OS is Linux. */
328 abi
= (u_int32_t
*)((char *)np
+ sizeof(Elf_Nhdr
) +
330 if (abi
[0] == ELF_NOTE_ABI_OS_LINUX
)
342 /* Check for certain intepreter names. */
344 if (!strncmp(itp
, "/lib/ld-linux", 13) ||
346 !strncmp(itp
, "/lib64/ld-linux", 15) ||
348 !strncmp(itp
, "/lib/ld.so.", 11))
362 ELFNAME2(linux
,probe
)(struct lwp
*l
, struct exec_package
*epp
, void *eh
,
363 char *itp
, vaddr_t
*pos
)
367 if (((error
= ELFNAME2(linux
,signature
)(l
, epp
, eh
, itp
)) != 0) &&
368 #ifdef LINUX_GCC_SIGNATURE
369 ((error
= ELFNAME2(linux
,gcc_signature
)(l
, epp
, eh
)) != 0) &&
371 #ifdef LINUX_ATEXIT_SIGNATURE
372 ((error
= ELFNAME2(linux
,atexit_signature
)(l
, epp
, eh
)) != 0) &&
374 #ifdef LINUX_DEBUGLINK_SIGNATURE
375 ((error
= ELFNAME2(linux
,debuglink_signature
)(l
, epp
, eh
)) != 0) &&
378 DPRINTF(("linux_probe: returning %d\n", error
));
383 if ((error
= emul_find_interp(l
, epp
, itp
)))
386 DPRINTF(("linux_probe: returning 0\n"));
390 #ifndef LINUX_MACHDEP_ELF_COPYARGS
392 * Copy arguments onto the stack in the normal way, but add some
393 * extra information in case of dynamic binding.
396 ELFNAME2(linux
,copyargs
)(struct lwp
*l
, struct exec_package
*pack
,
397 struct ps_strings
*arginfo
, char **stackp
, void *argp
)
400 AuxInfo ai
[LINUX_ELF_AUX_ENTRIES
], *a
;
405 if ((error
= copyargs(l
, pack
, arginfo
, stackp
, argp
)) != 0)
411 * Push extra arguments used by glibc on the stack.
414 a
->a_type
= AT_PAGESZ
;
418 if ((ap
= (struct elf_args
*)pack
->ep_emul_arg
)) {
421 a
->a_v
= ap
->arg_phaddr
;
424 a
->a_type
= AT_PHENT
;
425 a
->a_v
= ap
->arg_phentsize
;
428 a
->a_type
= AT_PHNUM
;
429 a
->a_v
= ap
->arg_phnum
;
433 a
->a_v
= ap
->arg_interp
;
436 a
->a_type
= AT_FLAGS
;
440 a
->a_type
= AT_ENTRY
;
441 a
->a_v
= ap
->arg_entry
;
444 free(pack
->ep_emul_arg
, M_TEMP
);
445 pack
->ep_emul_arg
= NULL
;
448 /* Linux-specific items */
449 a
->a_type
= LINUX_AT_CLKTCK
;
455 a
->a_type
= LINUX_AT_UID
;
456 a
->a_v
= kauth_cred_getuid(l
->l_cred
);
459 a
->a_type
= LINUX_AT_EUID
;
460 if (vap
->va_mode
& S_ISUID
)
461 a
->a_v
= vap
->va_uid
;
463 a
->a_v
= kauth_cred_geteuid(l
->l_cred
);
466 a
->a_type
= LINUX_AT_GID
;
467 a
->a_v
= kauth_cred_getgid(l
->l_cred
);
470 a
->a_type
= LINUX_AT_EGID
;
471 if (vap
->va_mode
& S_ISGID
)
472 a
->a_v
= vap
->va_gid
;
474 a
->a_v
= kauth_cred_getegid(l
->l_cred
);
481 len
= (a
- ai
) * sizeof(AuxInfo
);
482 if ((error
= copyout(ai
, *stackp
, len
)) != 0)
488 #endif /* !LINUX_MACHDEP_ELF_COPYARGS */