Home | History | Annotate | Line # | Download | only in common
linux_exec_elf32.c revision 1.51.2.7
      1  1.51.2.7   nathanw /*	$NetBSD: linux_exec_elf32.c,v 1.51.2.7 2002/09/17 21:19:01 nathanw Exp $	*/
      2      1.29  christos 
      3      1.29  christos /*-
      4      1.51      manu  * Copyright (c) 1995, 1998, 2000, 2001 The NetBSD Foundation, Inc.
      5      1.29  christos  * All rights reserved.
      6      1.29  christos  *
      7      1.29  christos  * This code is derived from software contributed to The NetBSD Foundation
      8      1.51      manu  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
      9      1.51      manu  * Emmanuel Dreyfus.
     10      1.29  christos  *
     11      1.29  christos  * Redistribution and use in source and binary forms, with or without
     12      1.29  christos  * modification, are permitted provided that the following conditions
     13      1.29  christos  * are met:
     14      1.29  christos  * 1. Redistributions of source code must retain the above copyright
     15      1.29  christos  *    notice, this list of conditions and the following disclaimer.
     16      1.29  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.29  christos  *    notice, this list of conditions and the following disclaimer in the
     18      1.29  christos  *    documentation and/or other materials provided with the distribution.
     19      1.29  christos  * 3. All advertising materials mentioning features or use of this software
     20      1.29  christos  *    must display the following acknowledgement:
     21      1.31       erh  *	This product includes software developed by the NetBSD
     22      1.31       erh  *	Foundation, Inc. and its contributors.
     23      1.29  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24      1.29  christos  *    contributors may be used to endorse or promote products derived
     25      1.29  christos  *    from this software without specific prior written permission.
     26      1.29  christos  *
     27      1.29  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28      1.29  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29      1.29  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30      1.29  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31      1.29  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32      1.29  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.29  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.29  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35      1.29  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.29  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37      1.29  christos  * POSSIBILITY OF SUCH DAMAGE.
     38      1.29  christos  */
     39       1.1      fvdl 
     40       1.1      fvdl /*
     41       1.7      fvdl  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
     42       1.1      fvdl  */
     43  1.51.2.3   nathanw 
     44  1.51.2.3   nathanw #include <sys/cdefs.h>
     45  1.51.2.7   nathanw __KERNEL_RCSID(0, "$NetBSD: linux_exec_elf32.c,v 1.51.2.7 2002/09/17 21:19:01 nathanw Exp $");
     46       1.1      fvdl 
     47      1.31       erh #ifndef ELFSIZE
     48      1.18       cgd #define	ELFSIZE		32				/* XXX should die */
     49      1.31       erh #endif
     50      1.17       cgd 
     51       1.1      fvdl #include <sys/param.h>
     52       1.1      fvdl #include <sys/systm.h>
     53       1.1      fvdl #include <sys/kernel.h>
     54       1.1      fvdl #include <sys/proc.h>
     55       1.1      fvdl #include <sys/malloc.h>
     56       1.1      fvdl #include <sys/namei.h>
     57       1.1      fvdl #include <sys/vnode.h>
     58      1.13  christos #include <sys/mount.h>
     59      1.25  christos #include <sys/exec.h>
     60       1.8      fvdl #include <sys/exec_elf.h>
     61       1.1      fvdl 
     62       1.1      fvdl #include <sys/mman.h>
     63  1.51.2.6   nathanw #include <sys/sa.h>
     64      1.13  christos #include <sys/syscallargs.h>
     65       1.1      fvdl 
     66       1.1      fvdl #include <machine/cpu.h>
     67       1.1      fvdl #include <machine/reg.h>
     68       1.1      fvdl 
     69      1.32  christos #include <compat/linux/common/linux_types.h>
     70      1.32  christos #include <compat/linux/common/linux_signal.h>
     71      1.32  christos #include <compat/linux/common/linux_util.h>
     72      1.32  christos #include <compat/linux/common/linux_exec.h>
     73      1.32  christos #include <compat/linux/common/linux_machdep.h>
     74      1.32  christos 
     75      1.32  christos #include <compat/linux/linux_syscallargs.h>
     76       1.4  christos #include <compat/linux/linux_syscall.h>
     77      1.31       erh 
     78      1.31       erh static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *,
     79      1.50   mycroft 	Elf_Ehdr *, char *));
     80      1.31       erh #ifdef LINUX_GCC_SIGNATURE
     81      1.31       erh static int ELFNAME2(linux,gcc_signature) __P((struct proc *p,
     82      1.31       erh 	struct exec_package *, Elf_Ehdr *));
     83      1.31       erh #endif
     84      1.51      manu #ifdef LINUX_ATEXIT_SIGNATURE
     85      1.51      manu static int ELFNAME2(linux,atexit_signature) __P((struct proc *p,
     86      1.51      manu 	struct exec_package *, Elf_Ehdr *));
     87      1.51      manu #endif
     88      1.51      manu 
     89  1.51.2.5   nathanw #ifdef DEBUG_LINUX
     90  1.51.2.5   nathanw #define DPRINTF(a)	uprintf a
     91  1.51.2.5   nathanw #else
     92  1.51.2.5   nathanw #define DPRINTF(a)
     93  1.51.2.5   nathanw #endif
     94  1.51.2.5   nathanw 
     95      1.51      manu #ifdef LINUX_ATEXIT_SIGNATURE
     96      1.51      manu /*
     97      1.51      manu  * On the PowerPC, statically linked Linux binaries are not recognized
     98      1.51      manu  * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
     99      1.51      manu  * binaries features a __libc_atexit ELF section. We therefore assume we
    100      1.51      manu  * have a Linux binary if we find this section.
    101      1.51      manu  */
    102      1.51      manu static int
    103      1.51      manu ELFNAME2(linux,atexit_signature)(p, epp, eh)
    104      1.51      manu 	struct proc *p;
    105      1.51      manu 	struct exec_package *epp;
    106      1.51      manu 	Elf_Ehdr *eh;
    107      1.51      manu {
    108      1.51      manu 	size_t shsize;
    109      1.51      manu 	int	strndx;
    110      1.51      manu 	size_t i;
    111      1.51      manu 	static const char signature[] = "__libc_atexit";
    112      1.51      manu 	char* strtable;
    113      1.51      manu 	Elf_Shdr *sh;
    114      1.51      manu 
    115      1.51      manu 	int error;
    116      1.51      manu 
    117  1.51.2.1   nathanw 	/*
    118  1.51.2.1   nathanw 	 * load the section header table
    119  1.51.2.1   nathanw 	 */
    120      1.51      manu 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
    121      1.51      manu 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
    122  1.51.2.1   nathanw 	error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
    123      1.51      manu 	if (error)
    124      1.51      manu 		goto out;
    125      1.51      manu 
    126  1.51.2.1   nathanw 	/*
    127  1.51.2.1   nathanw 	 * Now let's find the string table. If it does not exists, give up.
    128  1.51.2.1   nathanw 	 */
    129      1.51      manu 	strndx = (int)(eh->e_shstrndx);
    130  1.51.2.1   nathanw 	if (strndx == SHN_UNDEF) {
    131      1.51      manu 		error = ENOEXEC;
    132      1.51      manu 		goto out;
    133      1.51      manu 	}
    134      1.51      manu 
    135  1.51.2.1   nathanw 	/*
    136  1.51.2.1   nathanw 	 * strndx is the index in section header table of the string table
    137  1.51.2.1   nathanw 	 * section get the whole string table in strtable, and then we get access to the names
    138  1.51.2.1   nathanw 	 * s->sh_name is the offset of the section name in strtable.
    139  1.51.2.1   nathanw 	 */
    140      1.51      manu 	strtable = malloc(sh[strndx].sh_size, M_TEMP, M_WAITOK);
    141  1.51.2.1   nathanw 	error = exec_read_from(p, epp->ep_vp, sh[strndx].sh_offset, strtable,
    142  1.51.2.1   nathanw 	    sh[strndx].sh_size);
    143      1.51      manu 	if (error)
    144      1.51      manu 		goto out;
    145      1.51      manu 
    146      1.51      manu 	for (i = 0; i < eh->e_shnum; i++) {
    147      1.51      manu 		Elf_Shdr *s = &sh[i];
    148      1.51      manu 		if (!memcmp((void*)(&(strtable[s->sh_name])), signature,
    149      1.51      manu 				sizeof(signature))) {
    150  1.51.2.5   nathanw 			DPRINTF(("linux_atexit_sig=%s\n",
    151  1.51.2.5   nathanw 			    &(strtable[s->sh_name])));
    152      1.51      manu 			error = 0;
    153      1.51      manu 			goto out;
    154      1.51      manu 		}
    155      1.51      manu 	}
    156      1.51      manu 	error = ENOEXEC;
    157      1.51      manu 
    158      1.51      manu out:
    159      1.51      manu 	free(sh, M_TEMP);
    160      1.51      manu 	free(strtable, M_TEMP);
    161      1.51      manu 	return (error);
    162      1.51      manu }
    163      1.51      manu #endif
    164       1.4  christos 
    165      1.31       erh #ifdef LINUX_GCC_SIGNATURE
    166      1.14  christos /*
    167      1.14  christos  * Take advantage of the fact that all the linux binaries are compiled
    168      1.14  christos  * with gcc, and gcc sticks in the comment field a signature. Note that
    169      1.14  christos  * on SVR4 binaries, the gcc signature will follow the OS name signature,
    170      1.14  christos  * that will not be a problem. We don't bother to read in the string table,
    171      1.14  christos  * but we check all the progbits headers.
    172      1.31       erh  *
    173      1.31       erh  * XXX This only works in the i386.  On the alpha (at least)
    174      1.31       erh  * XXX we have the same gcc signature which incorrectly identifies
    175      1.31       erh  * XXX NetBSD binaries as Linux.
    176      1.14  christos  */
    177      1.14  christos static int
    178      1.31       erh ELFNAME2(linux,gcc_signature)(p, epp, eh)
    179      1.14  christos 	struct proc *p;
    180      1.14  christos 	struct exec_package *epp;
    181      1.31       erh 	Elf_Ehdr *eh;
    182      1.14  christos {
    183      1.50   mycroft 	size_t shsize;
    184      1.14  christos 	size_t i;
    185      1.14  christos 	static const char signature[] = "\0GCC: (GNU) ";
    186      1.14  christos 	char buf[sizeof(signature) - 1];
    187      1.31       erh 	Elf_Shdr *sh;
    188      1.14  christos 	int error;
    189      1.14  christos 
    190      1.50   mycroft 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
    191      1.31       erh 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
    192  1.51.2.1   nathanw 	error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
    193      1.50   mycroft 	if (error)
    194      1.14  christos 		goto out;
    195      1.14  christos 
    196      1.14  christos 	for (i = 0; i < eh->e_shnum; i++) {
    197      1.31       erh 		Elf_Shdr *s = &sh[i];
    198      1.14  christos 
    199      1.14  christos 		/*
    200      1.14  christos 		 * Identify candidates for the comment header;
    201      1.15  christos 		 * Header cannot have a load address, or flags and
    202      1.14  christos 		 * it must be large enough.
    203      1.14  christos 		 */
    204      1.43    kleink 		if (s->sh_type != SHT_PROGBITS ||
    205      1.14  christos 		    s->sh_addr != 0 ||
    206      1.14  christos 		    s->sh_flags != 0 ||
    207      1.14  christos 		    s->sh_size < sizeof(signature) - 1)
    208      1.14  christos 			continue;
    209      1.14  christos 
    210  1.51.2.1   nathanw 		error = exec_read_from(p, epp->ep_vp, s->sh_offset, buf,
    211  1.51.2.1   nathanw 		    sizeof(signature) - 1);
    212      1.50   mycroft 		if (error)
    213      1.50   mycroft 			continue;
    214      1.14  christos 
    215      1.15  christos 		/*
    216      1.15  christos 		 * error is 0, if the signatures match we are done.
    217      1.15  christos 		 */
    218  1.51.2.5   nathanw 		DPRINTF(("linux_gcc_sig: sig=%s\n", buf));
    219      1.50   mycroft 		if (!memcmp(buf, signature, sizeof(signature) - 1)) {
    220      1.50   mycroft 			error = 0;
    221      1.14  christos 			goto out;
    222      1.50   mycroft 		}
    223      1.14  christos 	}
    224      1.50   mycroft 	error = ENOEXEC;
    225      1.14  christos 
    226      1.14  christos out:
    227      1.14  christos 	free(sh, M_TEMP);
    228      1.50   mycroft 	return (error);
    229      1.14  christos }
    230      1.31       erh #endif
    231      1.31       erh 
    232      1.31       erh static int
    233      1.50   mycroft ELFNAME2(linux,signature)(p, epp, eh, itp)
    234      1.31       erh 	struct proc *p;
    235      1.31       erh 	struct exec_package *epp;
    236      1.31       erh 	Elf_Ehdr *eh;
    237      1.50   mycroft 	char *itp;
    238      1.31       erh {
    239      1.31       erh 	size_t i;
    240      1.31       erh 	Elf_Phdr *ph;
    241      1.31       erh 	size_t phsize;
    242      1.50   mycroft 	int error;
    243  1.51.2.7   nathanw 	static const char linux[] = "Linux";
    244  1.51.2.7   nathanw 
    245  1.51.2.7   nathanw 	if (eh->e_ident[EI_OSABI] == 3 ||
    246  1.51.2.7   nathanw 	    memcmp(&eh->e_ident[EI_ABIVERSION], linux, sizeof(linux)) == 0)
    247  1.51.2.7   nathanw 		return 0;
    248      1.31       erh 
    249      1.31       erh 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    250      1.31       erh 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
    251  1.51.2.1   nathanw 	error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
    252      1.50   mycroft 	if (error)
    253      1.50   mycroft 		goto out;
    254      1.31       erh 
    255      1.31       erh 	for (i = 0; i < eh->e_phnum; i++) {
    256      1.31       erh 		Elf_Phdr *ephp = &ph[i];
    257      1.50   mycroft 		Elf_Nhdr *np;
    258      1.50   mycroft 		u_int32_t *abi;
    259      1.31       erh 
    260      1.50   mycroft 		if (ephp->p_type != PT_NOTE ||
    261      1.50   mycroft 		    ephp->p_filesz > 1024 ||
    262      1.50   mycroft 		    ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
    263      1.31       erh 			continue;
    264      1.31       erh 
    265      1.50   mycroft 		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
    266  1.51.2.1   nathanw 		error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
    267  1.51.2.1   nathanw 		    ephp->p_filesz);
    268      1.50   mycroft 		if (error)
    269      1.50   mycroft 			goto next;
    270      1.50   mycroft 
    271      1.50   mycroft 		if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
    272      1.50   mycroft 		    np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
    273      1.50   mycroft 		    np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
    274      1.50   mycroft 		    memcmp((caddr_t)(np + 1), ELF_NOTE_ABI_NAME,
    275      1.50   mycroft 		    ELF_NOTE_ABI_NAMESZ))
    276      1.50   mycroft 			goto next;
    277      1.50   mycroft 
    278      1.50   mycroft 		/* Make sure the OS is Linux. */
    279      1.50   mycroft 		abi = (u_int32_t *)((caddr_t)np + sizeof(Elf_Nhdr) +
    280      1.50   mycroft 		    np->n_namesz);
    281      1.50   mycroft 		if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
    282      1.50   mycroft 			error = 0;
    283      1.50   mycroft 		else
    284      1.40       erh 			error = ENOEXEC;
    285      1.50   mycroft 		free(np, M_TEMP);
    286      1.50   mycroft 		goto out;
    287      1.31       erh 
    288      1.50   mycroft 	next:
    289      1.50   mycroft 		free(np, M_TEMP);
    290      1.50   mycroft 		continue;
    291      1.50   mycroft 	}
    292      1.31       erh 
    293      1.50   mycroft 	/* Check for certain intepreter names. */
    294      1.50   mycroft 	if (itp[0]) {
    295      1.50   mycroft 		if (!strncmp(itp, "/lib/ld-linux", 13) ||
    296      1.50   mycroft 		    !strncmp(itp, "/lib/ld.so.", 11))
    297      1.50   mycroft 			error = 0;
    298      1.50   mycroft 		else
    299      1.50   mycroft 			error = ENOEXEC;
    300      1.50   mycroft 		goto out;
    301      1.31       erh 	}
    302      1.31       erh 
    303      1.31       erh 	error = ENOEXEC;
    304      1.50   mycroft out:
    305      1.31       erh 	free(ph, M_TEMP);
    306      1.50   mycroft 	return (error);
    307      1.31       erh }
    308      1.14  christos 
    309       1.8      fvdl int
    310      1.31       erh ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
    311       1.8      fvdl 	struct proc *p;
    312       1.8      fvdl 	struct exec_package *epp;
    313      1.48  jdolecek 	void *eh;
    314       1.8      fvdl 	char *itp;
    315      1.48  jdolecek 	vaddr_t *pos;
    316       1.6      fvdl {
    317      1.41  christos 	const char *bp;
    318       1.8      fvdl 	int error;
    319       1.8      fvdl 	size_t len;
    320      1.14  christos 
    321  1.51.2.2   nathanw 	if (((error = ELFNAME2(linux,signature)(p, epp, eh, itp)) != 0) &&
    322      1.51      manu #ifdef LINUX_GCC_SIGNATURE
    323  1.51.2.2   nathanw 	    ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0) &&
    324      1.51      manu #endif
    325  1.51.2.2   nathanw #ifdef LINUX_ATEXIT_SIGNATURE
    326  1.51.2.2   nathanw 	    ((error = ELFNAME2(linux,atexit_signature)(p, epp, eh)) != 0) &&
    327      1.31       erh #endif
    328  1.51.2.2   nathanw 	    1)
    329  1.51.2.2   nathanw 			return error;
    330       1.6      fvdl 
    331       1.8      fvdl 	if (itp[0]) {
    332      1.49  jdolecek 		if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path,
    333      1.49  jdolecek 		    itp, &bp, 0)))
    334       1.8      fvdl 			return error;
    335       1.8      fvdl 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
    336       1.8      fvdl 			return error;
    337      1.41  christos 		free((void *)bp, M_TEMP);
    338       1.6      fvdl 	}
    339      1.31       erh 	*pos = ELF_NO_ADDR;
    340  1.51.2.5   nathanw 	DPRINTF(("linux_probe: returning 0\n"));
    341       1.6      fvdl 	return 0;
    342       1.6      fvdl }
    343       1.6      fvdl 
    344