Home | History | Annotate | Line # | Download | only in common
linux_exec_elf32.c revision 1.51.4.6
      1 /*	$NetBSD: linux_exec_elf32.c,v 1.51.4.6 2002/10/10 18:38:02 jdolecek Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998, 2000, 2001 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, Eric Haszlakiewicz and
      9  * Emmanuel Dreyfus.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: linux_exec_elf32.c,v 1.51.4.6 2002/10/10 18:38:02 jdolecek Exp $");
     46 
     47 #ifndef ELFSIZE
     48 #define	ELFSIZE		32				/* XXX should die */
     49 #endif
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/kernel.h>
     54 #include <sys/proc.h>
     55 #include <sys/malloc.h>
     56 #include <sys/namei.h>
     57 #include <sys/vnode.h>
     58 #include <sys/mount.h>
     59 #include <sys/exec.h>
     60 #include <sys/exec_elf.h>
     61 
     62 #include <sys/mman.h>
     63 #include <sys/syscallargs.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 *, char *));
     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 #ifdef LINUX_ATEXIT_SIGNATURE
     84 static int ELFNAME2(linux,atexit_signature) __P((struct proc *p,
     85 	struct exec_package *, Elf_Ehdr *));
     86 #endif
     87 
     88 #ifdef DEBUG_LINUX
     89 #define DPRINTF(a)	uprintf a
     90 #else
     91 #define DPRINTF(a)
     92 #endif
     93 
     94 #ifdef LINUX_ATEXIT_SIGNATURE
     95 /*
     96  * On the PowerPC, statically linked Linux binaries are not recognized
     97  * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
     98  * binaries features a __libc_atexit ELF section. We therefore assume we
     99  * have a Linux binary if we find this section.
    100  */
    101 static int
    102 ELFNAME2(linux,atexit_signature)(p, epp, eh)
    103 	struct proc *p;
    104 	struct exec_package *epp;
    105 	Elf_Ehdr *eh;
    106 {
    107 	size_t shsize;
    108 	int	strndx;
    109 	size_t i;
    110 	static const char signature[] = "__libc_atexit";
    111 	char* strtable;
    112 	Elf_Shdr *sh;
    113 
    114 	int error;
    115 
    116 	/*
    117 	 * load the section header table
    118 	 */
    119 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
    120 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
    121 	error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
    122 	if (error)
    123 		goto out;
    124 
    125 	/*
    126 	 * Now let's find the string table. If it does not exists, give up.
    127 	 */
    128 	strndx = (int)(eh->e_shstrndx);
    129 	if (strndx == SHN_UNDEF) {
    130 		error = ENOEXEC;
    131 		goto out;
    132 	}
    133 
    134 	/*
    135 	 * strndx is the index in section header table of the string table
    136 	 * section get the whole string table in strtable, and then we get access to the names
    137 	 * s->sh_name is the offset of the section name in strtable.
    138 	 */
    139 	strtable = malloc(sh[strndx].sh_size, M_TEMP, M_WAITOK);
    140 	error = exec_read_from(p, epp->ep_vp, sh[strndx].sh_offset, strtable,
    141 	    sh[strndx].sh_size);
    142 	if (error)
    143 		goto out;
    144 
    145 	for (i = 0; i < eh->e_shnum; i++) {
    146 		Elf_Shdr *s = &sh[i];
    147 		if (!memcmp((void*)(&(strtable[s->sh_name])), signature,
    148 				sizeof(signature))) {
    149 			DPRINTF(("linux_atexit_sig=%s\n",
    150 			    &(strtable[s->sh_name])));
    151 			error = 0;
    152 			goto out;
    153 		}
    154 	}
    155 	error = ENOEXEC;
    156 
    157 out:
    158 	free(sh, M_TEMP);
    159 	free(strtable, M_TEMP);
    160 	return (error);
    161 }
    162 #endif
    163 
    164 #ifdef LINUX_GCC_SIGNATURE
    165 /*
    166  * Take advantage of the fact that all the linux binaries are compiled
    167  * with gcc, and gcc sticks in the comment field a signature. Note that
    168  * on SVR4 binaries, the gcc signature will follow the OS name signature,
    169  * that will not be a problem. We don't bother to read in the string table,
    170  * but we check all the progbits headers.
    171  *
    172  * XXX This only works in the i386.  On the alpha (at least)
    173  * XXX we have the same gcc signature which incorrectly identifies
    174  * XXX NetBSD binaries as Linux.
    175  */
    176 static int
    177 ELFNAME2(linux,gcc_signature)(p, epp, eh)
    178 	struct proc *p;
    179 	struct exec_package *epp;
    180 	Elf_Ehdr *eh;
    181 {
    182 	size_t shsize;
    183 	size_t i;
    184 	static const char signature[] = "\0GCC: (GNU) ";
    185 	char buf[sizeof(signature) - 1];
    186 	Elf_Shdr *sh;
    187 	int error;
    188 
    189 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
    190 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
    191 	error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
    192 	if (error)
    193 		goto out;
    194 
    195 	for (i = 0; i < eh->e_shnum; i++) {
    196 		Elf_Shdr *s = &sh[i];
    197 
    198 		/*
    199 		 * Identify candidates for the comment header;
    200 		 * Header cannot have a load address, or flags and
    201 		 * it must be large enough.
    202 		 */
    203 		if (s->sh_type != SHT_PROGBITS ||
    204 		    s->sh_addr != 0 ||
    205 		    s->sh_flags != 0 ||
    206 		    s->sh_size < sizeof(signature) - 1)
    207 			continue;
    208 
    209 		error = exec_read_from(p, epp->ep_vp, s->sh_offset, buf,
    210 		    sizeof(signature) - 1);
    211 		if (error)
    212 			continue;
    213 
    214 		/*
    215 		 * error is 0, if the signatures match we are done.
    216 		 */
    217 		DPRINTF(("linux_gcc_sig: sig=%s\n", buf));
    218 		if (!memcmp(buf, signature, sizeof(signature) - 1)) {
    219 			error = 0;
    220 			goto out;
    221 		}
    222 	}
    223 	error = ENOEXEC;
    224 
    225 out:
    226 	free(sh, M_TEMP);
    227 	return (error);
    228 }
    229 #endif
    230 
    231 static int
    232 ELFNAME2(linux,signature)(p, epp, eh, itp)
    233 	struct proc *p;
    234 	struct exec_package *epp;
    235 	Elf_Ehdr *eh;
    236 	char *itp;
    237 {
    238 	size_t i;
    239 	Elf_Phdr *ph;
    240 	size_t phsize;
    241 	int error;
    242 	static const char linux[] = "Linux";
    243 
    244 	if (eh->e_ident[EI_OSABI] == 3 ||
    245 	    memcmp(&eh->e_ident[EI_ABIVERSION], linux, sizeof(linux)) == 0)
    246 		return 0;
    247 
    248 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    249 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
    250 	error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
    251 	if (error)
    252 		goto out;
    253 
    254 	for (i = 0; i < eh->e_phnum; i++) {
    255 		Elf_Phdr *ephp = &ph[i];
    256 		Elf_Nhdr *np;
    257 		u_int32_t *abi;
    258 
    259 		if (ephp->p_type != PT_NOTE ||
    260 		    ephp->p_filesz > 1024 ||
    261 		    ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
    262 			continue;
    263 
    264 		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
    265 		error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
    266 		    ephp->p_filesz);
    267 		if (error)
    268 			goto next;
    269 
    270 		if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
    271 		    np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
    272 		    np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
    273 		    memcmp((caddr_t)(np + 1), ELF_NOTE_ABI_NAME,
    274 		    ELF_NOTE_ABI_NAMESZ))
    275 			goto next;
    276 
    277 		/* Make sure the OS is Linux. */
    278 		abi = (u_int32_t *)((caddr_t)np + sizeof(Elf_Nhdr) +
    279 		    np->n_namesz);
    280 		if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
    281 			error = 0;
    282 		else
    283 			error = ENOEXEC;
    284 		free(np, M_TEMP);
    285 		goto out;
    286 
    287 	next:
    288 		free(np, M_TEMP);
    289 		continue;
    290 	}
    291 
    292 	/* Check for certain intepreter names. */
    293 	if (itp[0]) {
    294 		if (!strncmp(itp, "/lib/ld-linux", 13) ||
    295 		    !strncmp(itp, "/lib/ld.so.", 11))
    296 			error = 0;
    297 		else
    298 			error = ENOEXEC;
    299 		goto out;
    300 	}
    301 
    302 	error = ENOEXEC;
    303 out:
    304 	free(ph, M_TEMP);
    305 	return (error);
    306 }
    307 
    308 int
    309 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
    310 	struct proc *p;
    311 	struct exec_package *epp;
    312 	void *eh;
    313 	char *itp;
    314 	vaddr_t *pos;
    315 {
    316 	const char *bp;
    317 	int error;
    318 	size_t len;
    319 
    320 	if (((error = ELFNAME2(linux,signature)(p, epp, eh, itp)) != 0) &&
    321 #ifdef LINUX_GCC_SIGNATURE
    322 	    ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0) &&
    323 #endif
    324 #ifdef LINUX_ATEXIT_SIGNATURE
    325 	    ((error = ELFNAME2(linux,atexit_signature)(p, epp, eh)) != 0) &&
    326 #endif
    327 	    1)
    328 			return error;
    329 
    330 	if (itp[0]) {
    331 		if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path,
    332 		    itp, &bp, 0)))
    333 			return error;
    334 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
    335 			return error;
    336 		free((void *)bp, M_TEMP);
    337 	}
    338 	*pos = ELF_NO_ADDR;
    339 	DPRINTF(("linux_probe: returning 0\n"));
    340 	return 0;
    341 }
    342 
    343