Home | History | Annotate | Line # | Download | only in common
linux_exec_elf32.c revision 1.37
      1 /*	$NetBSD: linux_exec_elf32.c,v 1.37 1998/10/07 22:17:57 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 int linux_error[];
     88 extern char linux_sigcode[], linux_esigcode[];
     89 extern struct sysent linux_sysent[];
     90 extern char *linux_syscallnames[];
     91 
     92 struct emul ELFNAMEEND(emul_linux) = {
     93 	"linux",
     94 	linux_error,
     95 	linux_sendsig,
     96 	LINUX_SYS_syscall,
     97 	LINUX_SYS_MAXSYSCALL,
     98 	linux_sysent,
     99 	linux_syscallnames,
    100 	LINUX_ELF_AUX_ARGSIZ,
    101 	ELFNAME(copyargs),
    102 	linux_setregs,
    103 	linux_sigcode,
    104 	linux_esigcode,
    105 };
    106 
    107 
    108 #ifdef LINUX_GCC_SIGNATURE
    109 /*
    110  * Take advantage of the fact that all the linux binaries are compiled
    111  * with gcc, and gcc sticks in the comment field a signature. Note that
    112  * on SVR4 binaries, the gcc signature will follow the OS name signature,
    113  * that will not be a problem. We don't bother to read in the string table,
    114  * but we check all the progbits headers.
    115  *
    116  * XXX This only works in the i386.  On the alpha (at least)
    117  * XXX we have the same gcc signature which incorrectly identifies
    118  * XXX NetBSD binaries as Linux.
    119  */
    120 static int
    121 ELFNAME2(linux,gcc_signature)(p, epp, eh)
    122 	struct proc *p;
    123 	struct exec_package *epp;
    124 	Elf_Ehdr *eh;
    125 {
    126 	size_t shsize = sizeof(Elf_Shdr) * eh->e_shnum;
    127 	size_t i;
    128 	static const char signature[] = "\0GCC: (GNU) ";
    129 	char buf[sizeof(signature) - 1];
    130 	Elf_Shdr *sh;
    131 	int error;
    132 
    133 	error = ENOEXEC;
    134 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
    135 
    136 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_shoff,
    137 	    (caddr_t) sh, shsize)) != 0)
    138 		goto out;
    139 
    140 	for (i = 0; i < eh->e_shnum; i++) {
    141 		Elf_Shdr *s = &sh[i];
    142 
    143 		/*
    144 		 * Identify candidates for the comment header;
    145 		 * Header cannot have a load address, or flags and
    146 		 * it must be large enough.
    147 		 */
    148 		if (s->sh_type != Elf_sht_progbits ||
    149 		    s->sh_addr != 0 ||
    150 		    s->sh_flags != 0 ||
    151 		    s->sh_size < sizeof(signature) - 1)
    152 			continue;
    153 
    154 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, s->sh_offset,
    155 		    (caddr_t) buf, sizeof(signature) - 1)) != 0)
    156 			goto out;
    157 
    158 		/*
    159 		 * error is 0, if the signatures match we are done.
    160 		 */
    161 		if (memcmp(buf, signature, sizeof(signature) - 1) == 0)
    162 			goto out;
    163 	}
    164 
    165 out:
    166 	free(sh, M_TEMP);
    167 	return error;
    168 }
    169 #endif
    170 
    171 static int
    172 ELFNAME2(linux,signature)(p, epp, eh)
    173 	struct proc *p;
    174 	struct exec_package *epp;
    175 	Elf_Ehdr *eh;
    176 {
    177 	size_t i;
    178 	Elf_Phdr *ph;
    179 	Elf_Note *notep;
    180 	char *testp;
    181 	size_t phsize;
    182 	int error = ENOEXEC;
    183 
    184 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    185 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
    186 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
    187 					(caddr_t) ph, phsize)) != 0)
    188 		goto out1;
    189 
    190 	for (i = 0; i < eh->e_phnum; i++) {
    191 		Elf_Phdr *ephp = &ph[i];
    192 		u_int32_t ostype;
    193 
    194 		if (ephp->p_type != Elf_pt_interp /* XAX pt_note */
    195 #if 0
    196 		    || ephp->p_flags != 0
    197 		    || ephp->p_filesz < sizeof(Elf_Note))
    198 #endif
    199 		    )
    200 			continue;
    201 
    202 		notep = (Elf_Note *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
    203 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ephp->p_offset,
    204 					(caddr_t)notep, ephp->p_filesz)) != 0)
    205 			goto out3;
    206 
    207 		testp = (char *)notep;
    208 		testp[16] = '\0';
    209 		if (strncmp(&testp[8], "linux", 5) == 0)  {
    210 			error = 0;
    211 			goto out3;
    212 		}
    213 
    214 		goto out2;
    215 
    216 		/* XXX XAX Should handle NETBSD_TYPE_EMULNAME */
    217 		if (notep->type != ELF_NOTE_TYPE_OSVERSION) {
    218 			free(notep, M_TEMP);
    219 			continue;
    220 		}
    221 
    222 		/* Check the name and description sizes. */
    223 		if (notep->namesz != ELF_NOTE_GNU_NAMESZ ||
    224 		    notep->descsz != ELF_NOTE_GNU_DESCSZ)
    225 			goto out2;
    226 
    227 		/* Is the name "GNU\0"? */
    228 		if (memcmp((notep + sizeof(Elf_Note)),
    229 			   ELF_NOTE_GNU_NAME, ELF_NOTE_GNU_NAMESZ))
    230 			goto out2;
    231 
    232 		/* Make sure the OS is Linux */
    233 		ostype = (u_int32_t)(*((u_int32_t *)notep + sizeof(Elf_Note) +
    234 		    notep->namesz)) & ELF_NOTE_GNU_OSMASK;
    235 		if (ostype != ELF_NOTE_GNU_OSLINUX)
    236 			goto out2;
    237 
    238 		/* All checks succeeded. */
    239 		error = 0;
    240 		goto out3;
    241 	}
    242 
    243 	error = ENOEXEC;
    244 
    245 out1:
    246 	free(ph, M_TEMP);
    247 	return error;
    248 
    249 out2:
    250 	error = ENOEXEC;
    251 out3:
    252 	free(notep, M_TEMP);
    253 	free(ph, M_TEMP);
    254 	return error;
    255 }
    256 
    257 int
    258 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
    259 	struct proc *p;
    260 	struct exec_package *epp;
    261 	Elf_Ehdr *eh;
    262 	char *itp;
    263 	Elf_Addr *pos;
    264 {
    265 	char *bp;
    266 	int error;
    267 	size_t len;
    268 
    269 	if ((error = ELFNAME2(linux,signature)(p, epp, eh)) != 0)
    270 #ifdef LINUX_GCC_SIGNATURE
    271 		if ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0)
    272 			return error;
    273 #else
    274 		return error;
    275 #endif
    276 
    277 	if (itp[0]) {
    278 		if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
    279 			return error;
    280 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
    281 			return error;
    282 		free(bp, M_TEMP);
    283 	}
    284 	epp->ep_emul = &ELFNAMEEND(emul_linux);
    285 	*pos = ELF_NO_ADDR;
    286 	return 0;
    287 }
    288 
    289