Home | History | Annotate | Line # | Download | only in common
linux_exec_elf32.c revision 1.40
      1  1.40       erh /*	$NetBSD: linux_exec_elf32.c,v 1.40 1998/11/05 22:19:25 erh Exp $	*/
      2  1.29  christos 
      3  1.29  christos /*-
      4  1.34      fvdl  * Copyright (c) 1995, 1998 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.34      fvdl  * by Christos Zoulas, Frank van der Linden and Eric Haszlakiewicz.
      9  1.29  christos  *
     10  1.29  christos  * Redistribution and use in source and binary forms, with or without
     11  1.29  christos  * modification, are permitted provided that the following conditions
     12  1.29  christos  * are met:
     13  1.29  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.29  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.29  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.29  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.29  christos  *    documentation and/or other materials provided with the distribution.
     18  1.29  christos  * 3. All advertising materials mentioning features or use of this software
     19  1.29  christos  *    must display the following acknowledgement:
     20  1.31       erh  *	This product includes software developed by the NetBSD
     21  1.31       erh  *	Foundation, Inc. and its contributors.
     22  1.29  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.29  christos  *    contributors may be used to endorse or promote products derived
     24  1.29  christos  *    from this software without specific prior written permission.
     25  1.29  christos  *
     26  1.29  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.29  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.29  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.29  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.29  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.29  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.29  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.29  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.29  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.29  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.29  christos  * POSSIBILITY OF SUCH DAMAGE.
     37  1.29  christos  */
     38   1.1      fvdl 
     39   1.1      fvdl /*
     40   1.7      fvdl  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
     41   1.1      fvdl  */
     42   1.1      fvdl 
     43  1.31       erh #ifndef ELFSIZE
     44  1.18       cgd #define	ELFSIZE		32				/* XXX should die */
     45  1.31       erh #endif
     46  1.17       cgd 
     47   1.1      fvdl #include <sys/param.h>
     48   1.1      fvdl #include <sys/systm.h>
     49   1.1      fvdl #include <sys/kernel.h>
     50   1.1      fvdl #include <sys/proc.h>
     51   1.1      fvdl #include <sys/malloc.h>
     52   1.1      fvdl #include <sys/namei.h>
     53   1.1      fvdl #include <sys/vnode.h>
     54  1.13  christos #include <sys/mount.h>
     55  1.25  christos #include <sys/exec.h>
     56   1.8      fvdl #include <sys/exec_elf.h>
     57   1.1      fvdl 
     58   1.1      fvdl #include <sys/mman.h>
     59  1.13  christos #include <sys/syscallargs.h>
     60  1.13  christos 
     61   1.1      fvdl #include <vm/vm.h>
     62   1.1      fvdl #include <vm/vm_param.h>
     63   1.1      fvdl #include <vm/vm_map.h>
     64   1.1      fvdl 
     65   1.1      fvdl #include <machine/cpu.h>
     66   1.1      fvdl #include <machine/reg.h>
     67   1.1      fvdl 
     68  1.32  christos #include <compat/linux/common/linux_types.h>
     69  1.32  christos #include <compat/linux/common/linux_signal.h>
     70  1.32  christos #include <compat/linux/common/linux_util.h>
     71  1.32  christos #include <compat/linux/common/linux_exec.h>
     72  1.32  christos #include <compat/linux/common/linux_machdep.h>
     73  1.39     veego #include <compat/linux/common/linux_errno.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.31       erh 	Elf_Ehdr *));
     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.4  christos 
     85   1.6      fvdl #define LINUX_ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
     86   1.8      fvdl 
     87   1.4  christos 
     88  1.12   mycroft extern char linux_sigcode[], linux_esigcode[];
     89   1.4  christos extern struct sysent linux_sysent[];
     90   1.4  christos extern char *linux_syscallnames[];
     91  1.13  christos 
     92  1.31       erh struct emul ELFNAMEEND(emul_linux) = {
     93   1.6      fvdl 	"linux",
     94  1.38       erh 	native_to_linux_errno,
     95   1.6      fvdl 	linux_sendsig,
     96   1.6      fvdl 	LINUX_SYS_syscall,
     97   1.6      fvdl 	LINUX_SYS_MAXSYSCALL,
     98   1.6      fvdl 	linux_sysent,
     99   1.6      fvdl 	linux_syscallnames,
    100   1.6      fvdl 	LINUX_ELF_AUX_ARGSIZ,
    101  1.31       erh 	ELFNAME(copyargs),
    102  1.26   mycroft 	linux_setregs,
    103   1.4  christos 	linux_sigcode,
    104   1.4  christos 	linux_esigcode,
    105   1.4  christos };
    106   1.4  christos 
    107   1.4  christos 
    108  1.31       erh #ifdef LINUX_GCC_SIGNATURE
    109  1.14  christos /*
    110  1.14  christos  * Take advantage of the fact that all the linux binaries are compiled
    111  1.14  christos  * with gcc, and gcc sticks in the comment field a signature. Note that
    112  1.14  christos  * on SVR4 binaries, the gcc signature will follow the OS name signature,
    113  1.14  christos  * that will not be a problem. We don't bother to read in the string table,
    114  1.14  christos  * but we check all the progbits headers.
    115  1.31       erh  *
    116  1.31       erh  * XXX This only works in the i386.  On the alpha (at least)
    117  1.31       erh  * XXX we have the same gcc signature which incorrectly identifies
    118  1.31       erh  * XXX NetBSD binaries as Linux.
    119  1.14  christos  */
    120  1.14  christos static int
    121  1.31       erh ELFNAME2(linux,gcc_signature)(p, epp, eh)
    122  1.14  christos 	struct proc *p;
    123  1.14  christos 	struct exec_package *epp;
    124  1.31       erh 	Elf_Ehdr *eh;
    125  1.14  christos {
    126  1.31       erh 	size_t shsize = sizeof(Elf_Shdr) * eh->e_shnum;
    127  1.14  christos 	size_t i;
    128  1.14  christos 	static const char signature[] = "\0GCC: (GNU) ";
    129  1.14  christos 	char buf[sizeof(signature) - 1];
    130  1.31       erh 	Elf_Shdr *sh;
    131  1.14  christos 	int error;
    132  1.14  christos 
    133  1.31       erh 	error = ENOEXEC;
    134  1.31       erh 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
    135  1.14  christos 
    136  1.31       erh 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_shoff,
    137  1.14  christos 	    (caddr_t) sh, shsize)) != 0)
    138  1.14  christos 		goto out;
    139  1.14  christos 
    140  1.14  christos 	for (i = 0; i < eh->e_shnum; i++) {
    141  1.31       erh 		Elf_Shdr *s = &sh[i];
    142  1.14  christos 
    143  1.14  christos 		/*
    144  1.14  christos 		 * Identify candidates for the comment header;
    145  1.15  christos 		 * Header cannot have a load address, or flags and
    146  1.14  christos 		 * it must be large enough.
    147  1.14  christos 		 */
    148  1.22       jtk 		if (s->sh_type != Elf_sht_progbits ||
    149  1.14  christos 		    s->sh_addr != 0 ||
    150  1.14  christos 		    s->sh_flags != 0 ||
    151  1.14  christos 		    s->sh_size < sizeof(signature) - 1)
    152  1.14  christos 			continue;
    153  1.14  christos 
    154  1.31       erh 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, s->sh_offset,
    155  1.15  christos 		    (caddr_t) buf, sizeof(signature) - 1)) != 0)
    156  1.14  christos 			goto out;
    157  1.14  christos 
    158  1.15  christos 		/*
    159  1.15  christos 		 * error is 0, if the signatures match we are done.
    160  1.15  christos 		 */
    161  1.38       erh #ifdef DEBUG_LINUX
    162  1.38       erh 		printf("linux_gcc_sig: sig=%s\n", buf);
    163  1.38       erh #endif
    164  1.28     perry 		if (memcmp(buf, signature, sizeof(signature) - 1) == 0)
    165  1.14  christos 			goto out;
    166  1.14  christos 	}
    167  1.14  christos 
    168  1.14  christos out:
    169  1.14  christos 	free(sh, M_TEMP);
    170  1.38       erh #ifdef DEBUG_LINUX
    171  1.38       erh 	printf("linux_gcc_sig: returning %d\n", error);
    172  1.38       erh #endif
    173  1.14  christos 	return error;
    174  1.14  christos }
    175  1.31       erh #endif
    176  1.31       erh 
    177  1.31       erh static int
    178  1.31       erh ELFNAME2(linux,signature)(p, epp, eh)
    179  1.31       erh 	struct proc *p;
    180  1.31       erh 	struct exec_package *epp;
    181  1.31       erh 	Elf_Ehdr *eh;
    182  1.31       erh {
    183  1.31       erh 	size_t i;
    184  1.31       erh 	Elf_Phdr *ph;
    185  1.31       erh 	Elf_Note *notep;
    186  1.31       erh 	size_t phsize;
    187  1.31       erh 	int error = ENOEXEC;
    188  1.31       erh 
    189  1.31       erh 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    190  1.31       erh 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
    191  1.31       erh 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
    192  1.31       erh 					(caddr_t) ph, phsize)) != 0)
    193  1.31       erh 		goto out1;
    194  1.31       erh 
    195  1.31       erh 	for (i = 0; i < eh->e_phnum; i++) {
    196  1.31       erh 		Elf_Phdr *ephp = &ph[i];
    197  1.31       erh 		u_int32_t ostype;
    198  1.31       erh 
    199  1.31       erh 		if (ephp->p_type != Elf_pt_interp /* XAX pt_note */
    200  1.33  christos #if 0
    201  1.33  christos 		    || ephp->p_flags != 0
    202  1.33  christos 		    || ephp->p_filesz < sizeof(Elf_Note))
    203  1.33  christos #endif
    204  1.33  christos 		    )
    205  1.31       erh 			continue;
    206  1.31       erh 
    207  1.40       erh 		notep = (Elf_Note *)malloc(ephp->p_filesz+1, M_TEMP, M_WAITOK);
    208  1.31       erh 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ephp->p_offset,
    209  1.31       erh 					(caddr_t)notep, ephp->p_filesz)) != 0)
    210  1.31       erh 			goto out3;
    211  1.31       erh 
    212  1.40       erh 		/* Check for "linux" in the intepreter name. */
    213  1.40       erh 		if (ephp->p_filesz < 8 + 5) {
    214  1.40       erh 			error = ENOEXEC;
    215  1.40       erh 			goto out3;
    216  1.40       erh 		}
    217  1.38       erh #ifdef DEBUG_LINUX
    218  1.40       erh 		printf("linux_signature: interp=%s\n", notep);
    219  1.38       erh #endif
    220  1.40       erh 		if (strncmp(&((char *)notep)[8], "linux", 5) == 0 ||
    221  1.40       erh 		    strncmp((char *)notep, "/lib/ld.so.", 11) == 0) {
    222  1.31       erh 			error = 0;
    223  1.31       erh 			goto out3;
    224  1.31       erh 		}
    225  1.31       erh 
    226  1.31       erh 		goto out2;
    227  1.31       erh 
    228  1.31       erh 		/* XXX XAX Should handle NETBSD_TYPE_EMULNAME */
    229  1.31       erh 		if (notep->type != ELF_NOTE_TYPE_OSVERSION) {
    230  1.31       erh 			free(notep, M_TEMP);
    231  1.31       erh 			continue;
    232  1.31       erh 		}
    233  1.31       erh 
    234  1.31       erh 		/* Check the name and description sizes. */
    235  1.31       erh 		if (notep->namesz != ELF_NOTE_GNU_NAMESZ ||
    236  1.31       erh 		    notep->descsz != ELF_NOTE_GNU_DESCSZ)
    237  1.31       erh 			goto out2;
    238  1.31       erh 
    239  1.31       erh 		/* Is the name "GNU\0"? */
    240  1.31       erh 		if (memcmp((notep + sizeof(Elf_Note)),
    241  1.31       erh 			   ELF_NOTE_GNU_NAME, ELF_NOTE_GNU_NAMESZ))
    242  1.31       erh 			goto out2;
    243  1.31       erh 
    244  1.31       erh 		/* Make sure the OS is Linux */
    245  1.33  christos 		ostype = (u_int32_t)(*((u_int32_t *)notep + sizeof(Elf_Note) +
    246  1.33  christos 		    notep->namesz)) & ELF_NOTE_GNU_OSMASK;
    247  1.31       erh 		if (ostype != ELF_NOTE_GNU_OSLINUX)
    248  1.31       erh 			goto out2;
    249  1.31       erh 
    250  1.31       erh 		/* All checks succeeded. */
    251  1.31       erh 		error = 0;
    252  1.31       erh 		goto out3;
    253  1.31       erh 	}
    254  1.31       erh 
    255  1.31       erh 	error = ENOEXEC;
    256  1.31       erh 
    257  1.31       erh out1:
    258  1.31       erh 	free(ph, M_TEMP);
    259  1.38       erh #ifdef DEBUG_LINUX
    260  1.38       erh 	printf("linux_signature: out1=%d\n", error);
    261  1.38       erh #endif
    262  1.31       erh 	return error;
    263  1.31       erh 
    264  1.31       erh out2:
    265  1.31       erh 	error = ENOEXEC;
    266  1.31       erh out3:
    267  1.31       erh 	free(notep, M_TEMP);
    268  1.31       erh 	free(ph, M_TEMP);
    269  1.38       erh #ifdef DEBUG_LINUX
    270  1.38       erh 	printf("linux_signature: out2,3=%d\n", error);
    271  1.38       erh #endif
    272  1.31       erh 	return error;
    273  1.31       erh }
    274  1.14  christos 
    275   1.8      fvdl int
    276  1.31       erh ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
    277   1.8      fvdl 	struct proc *p;
    278   1.8      fvdl 	struct exec_package *epp;
    279  1.31       erh 	Elf_Ehdr *eh;
    280   1.8      fvdl 	char *itp;
    281  1.31       erh 	Elf_Addr *pos;
    282   1.6      fvdl {
    283   1.8      fvdl 	char *bp;
    284   1.8      fvdl 	int error;
    285   1.8      fvdl 	size_t len;
    286  1.14  christos 
    287  1.31       erh 	if ((error = ELFNAME2(linux,signature)(p, epp, eh)) != 0)
    288  1.31       erh #ifdef LINUX_GCC_SIGNATURE
    289  1.31       erh 		if ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0)
    290  1.31       erh 			return error;
    291  1.31       erh #else
    292  1.14  christos 		return error;
    293  1.31       erh #endif
    294   1.6      fvdl 
    295   1.8      fvdl 	if (itp[0]) {
    296   1.9  christos 		if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
    297   1.8      fvdl 			return error;
    298   1.8      fvdl 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
    299   1.8      fvdl 			return error;
    300   1.8      fvdl 		free(bp, M_TEMP);
    301   1.6      fvdl 	}
    302  1.31       erh 	epp->ep_emul = &ELFNAMEEND(emul_linux);
    303  1.31       erh 	*pos = ELF_NO_ADDR;
    304  1.38       erh #ifdef DEBUG_LINUX
    305  1.38       erh 	printf("linux_probe: returning 0\n");
    306  1.38       erh #endif
    307   1.6      fvdl 	return 0;
    308   1.6      fvdl }
    309   1.6      fvdl 
    310