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