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