Home | History | Annotate | Line # | Download | only in common
linux_exec_elf32.c revision 1.44
      1 /*	$NetBSD: linux_exec_elf32.c,v 1.44 2000/06/26 14:38:57 mrg 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 
     63 #include <machine/cpu.h>
     64 #include <machine/reg.h>
     65 
     66 #include <compat/linux/common/linux_types.h>
     67 #include <compat/linux/common/linux_signal.h>
     68 #include <compat/linux/common/linux_util.h>
     69 #include <compat/linux/common/linux_exec.h>
     70 #include <compat/linux/common/linux_machdep.h>
     71 #include <compat/linux/common/linux_errno.h>
     72 
     73 #include <compat/linux/linux_syscallargs.h>
     74 #include <compat/linux/linux_syscall.h>
     75 
     76 static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *,
     77 	Elf_Ehdr *));
     78 #ifdef LINUX_GCC_SIGNATURE
     79 static int ELFNAME2(linux,gcc_signature) __P((struct proc *p,
     80 	struct exec_package *, Elf_Ehdr *));
     81 #endif
     82 
     83 #define LINUX_ELF_AUX_ARGSIZ howmany(sizeof(AuxInfo) * 8, sizeof(char *))
     84 
     85 
     86 extern char linux_sigcode[], linux_esigcode[];
     87 extern struct sysent linux_sysent[];
     88 extern char *linux_syscallnames[];
     89 
     90 struct emul ELFNAMEEND(emul_linux) = {
     91 	"linux",
     92 	native_to_linux_errno,
     93 	linux_sendsig,
     94 	LINUX_SYS_syscall,
     95 	LINUX_SYS_MAXSYSCALL,
     96 	linux_sysent,
     97 	linux_syscallnames,
     98 	LINUX_ELF_AUX_ARGSIZ,
     99 	ELFNAME(copyargs),
    100 	linux_setregs,
    101 	linux_sigcode,
    102 	linux_esigcode,
    103 };
    104 
    105 
    106 #ifdef LINUX_GCC_SIGNATURE
    107 /*
    108  * Take advantage of the fact that all the linux binaries are compiled
    109  * with gcc, and gcc sticks in the comment field a signature. Note that
    110  * on SVR4 binaries, the gcc signature will follow the OS name signature,
    111  * that will not be a problem. We don't bother to read in the string table,
    112  * but we check all the progbits headers.
    113  *
    114  * XXX This only works in the i386.  On the alpha (at least)
    115  * XXX we have the same gcc signature which incorrectly identifies
    116  * XXX NetBSD binaries as Linux.
    117  */
    118 static int
    119 ELFNAME2(linux,gcc_signature)(p, epp, eh)
    120 	struct proc *p;
    121 	struct exec_package *epp;
    122 	Elf_Ehdr *eh;
    123 {
    124 	size_t shsize = sizeof(Elf_Shdr) * eh->e_shnum;
    125 	size_t i;
    126 	static const char signature[] = "\0GCC: (GNU) ";
    127 	char buf[sizeof(signature) - 1];
    128 	Elf_Shdr *sh;
    129 	int error;
    130 
    131 	error = ENOEXEC;
    132 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
    133 
    134 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_shoff,
    135 	    (caddr_t) sh, shsize)) != 0)
    136 		goto out;
    137 
    138 	for (i = 0; i < eh->e_shnum; i++) {
    139 		Elf_Shdr *s = &sh[i];
    140 
    141 		/*
    142 		 * Identify candidates for the comment header;
    143 		 * Header cannot have a load address, or flags and
    144 		 * it must be large enough.
    145 		 */
    146 		if (s->sh_type != SHT_PROGBITS ||
    147 		    s->sh_addr != 0 ||
    148 		    s->sh_flags != 0 ||
    149 		    s->sh_size < sizeof(signature) - 1)
    150 			continue;
    151 
    152 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, s->sh_offset,
    153 		    (caddr_t) buf, sizeof(signature) - 1)) != 0)
    154 			goto out;
    155 
    156 		/*
    157 		 * error is 0, if the signatures match we are done.
    158 		 */
    159 #ifdef DEBUG_LINUX
    160 		printf("linux_gcc_sig: sig=%s\n", buf);
    161 #endif
    162 		if (memcmp(buf, signature, sizeof(signature) - 1) == 0)
    163 			goto out;
    164 	}
    165 
    166 out:
    167 	free(sh, M_TEMP);
    168 #ifdef DEBUG_LINUX
    169 	printf("linux_gcc_sig: returning %d\n", error);
    170 #endif
    171 	return error;
    172 }
    173 #endif
    174 
    175 static int
    176 ELFNAME2(linux,signature)(p, epp, eh)
    177 	struct proc *p;
    178 	struct exec_package *epp;
    179 	Elf_Ehdr *eh;
    180 {
    181 	size_t i;
    182 	Elf_Phdr *ph;
    183 	Elf_Nhdr *notep;
    184 	size_t phsize;
    185 	int error = ENOEXEC;
    186 
    187 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    188 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
    189 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
    190 					(caddr_t) ph, phsize)) != 0)
    191 		goto out1;
    192 
    193 	for (i = 0; i < eh->e_phnum; i++) {
    194 		Elf_Phdr *ephp = &ph[i];
    195 		u_int32_t ostype;
    196 
    197 		if (ephp->p_type != PT_INTERP /* XAX PT_NOTE */
    198 #if 0
    199 		    || ephp->p_flags != 0
    200 		    || ephp->p_filesz < sizeof(Elf_Nhdr))
    201 #endif
    202 		    )
    203 			continue;
    204 
    205 		notep = (Elf_Nhdr *)malloc(ephp->p_filesz+1, M_TEMP, M_WAITOK);
    206 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ephp->p_offset,
    207 					(caddr_t)notep, ephp->p_filesz)) != 0)
    208 			goto out3;
    209 
    210 		/* Check for "linux" in the intepreter name. */
    211 		if (ephp->p_filesz < 8 + 5) {
    212 			error = ENOEXEC;
    213 			goto out3;
    214 		}
    215 #ifdef DEBUG_LINUX
    216 		printf("linux_signature: interp=%s\n", notep);
    217 #endif
    218 		if (strncmp(&((char *)notep)[8], "linux", 5) == 0 ||
    219 		    strncmp((char *)notep, "/lib/ld.so.", 11) == 0) {
    220 			error = 0;
    221 			goto out3;
    222 		}
    223 
    224 		goto out2;
    225 
    226 		/* XXX XAX Should handle NETBSD_TYPE_EMULNAME */
    227 		if (notep->n_type != ELF_NOTE_TYPE_OSVERSION) {
    228 			free(notep, M_TEMP);
    229 			continue;
    230 		}
    231 
    232 		/* Check the name and description sizes. */
    233 		if (notep->n_namesz != ELF_NOTE_GNU_NAMESZ ||
    234 		    notep->n_descsz != ELF_NOTE_GNU_DESCSZ)
    235 			goto out2;
    236 
    237 		/* Is the name "GNU\0"? */
    238 		if (memcmp((notep + sizeof(Elf_Nhdr)),
    239 			   ELF_NOTE_GNU_NAME, ELF_NOTE_GNU_NAMESZ))
    240 			goto out2;
    241 
    242 		/* Make sure the OS is Linux */
    243 		ostype = (u_int32_t)(*((u_int32_t *)notep + sizeof(Elf_Nhdr) +
    244 		    notep->n_namesz)) & ELF_NOTE_GNU_OSMASK;
    245 		if (ostype != ELF_NOTE_GNU_OSLINUX)
    246 			goto out2;
    247 
    248 		/* All checks succeeded. */
    249 		error = 0;
    250 		goto out3;
    251 	}
    252 
    253 	error = ENOEXEC;
    254 
    255 out1:
    256 	free(ph, M_TEMP);
    257 #ifdef DEBUG_LINUX
    258 	printf("linux_signature: out1=%d\n", error);
    259 #endif
    260 	return error;
    261 
    262 out2:
    263 	error = ENOEXEC;
    264 out3:
    265 	free(notep, M_TEMP);
    266 	free(ph, M_TEMP);
    267 #ifdef DEBUG_LINUX
    268 	printf("linux_signature: out2,3=%d\n", error);
    269 #endif
    270 	return error;
    271 }
    272 
    273 int
    274 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
    275 	struct proc *p;
    276 	struct exec_package *epp;
    277 	Elf_Ehdr *eh;
    278 	char *itp;
    279 	Elf_Addr *pos;
    280 {
    281 	const char *bp;
    282 	int error;
    283 	size_t len;
    284 
    285 	if ((error = ELFNAME2(linux,signature)(p, epp, eh)) != 0)
    286 #ifdef LINUX_GCC_SIGNATURE
    287 		if ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0)
    288 			return error;
    289 #else
    290 		return error;
    291 #endif
    292 
    293 	if (itp[0]) {
    294 		if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
    295 			return error;
    296 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
    297 			return error;
    298 		free((void *)bp, M_TEMP);
    299 	}
    300 	epp->ep_emul = &ELFNAMEEND(emul_linux);
    301 	*pos = ELF_NO_ADDR;
    302 #ifdef DEBUG_LINUX
    303 	printf("linux_probe: returning 0\n");
    304 #endif
    305 	return 0;
    306 }
    307 
    308