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