Home | History | Annotate | Line # | Download | only in common
linux_exec_elf32.c revision 1.51.2.6
      1 /*	$NetBSD: linux_exec_elf32.c,v 1.51.2.6 2002/05/29 21:32:37 nathanw 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.2.6 2002/05/29 21:32:37 nathanw 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/sa.h>
     64 #include <sys/syscallargs.h>
     65 
     66 #include <machine/cpu.h>
     67 #include <machine/reg.h>
     68 
     69 #include <compat/linux/common/linux_types.h>
     70 #include <compat/linux/common/linux_signal.h>
     71 #include <compat/linux/common/linux_util.h>
     72 #include <compat/linux/common/linux_exec.h>
     73 #include <compat/linux/common/linux_machdep.h>
     74 
     75 #include <compat/linux/linux_syscallargs.h>
     76 #include <compat/linux/linux_syscall.h>
     77 
     78 static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *,
     79 	Elf_Ehdr *, char *));
     80 #ifdef LINUX_GCC_SIGNATURE
     81 static int ELFNAME2(linux,gcc_signature) __P((struct proc *p,
     82 	struct exec_package *, Elf_Ehdr *));
     83 #endif
     84 #ifdef LINUX_ATEXIT_SIGNATURE
     85 static int ELFNAME2(linux,atexit_signature) __P((struct proc *p,
     86 	struct exec_package *, Elf_Ehdr *));
     87 #endif
     88 
     89 #ifdef DEBUG_LINUX
     90 #define DPRINTF(a)	uprintf a
     91 #else
     92 #define DPRINTF(a)
     93 #endif
     94 
     95 #ifdef LINUX_ATEXIT_SIGNATURE
     96 /*
     97  * On the PowerPC, statically linked Linux binaries are not recognized
     98  * by linux_signature nor by linux_gcc_signature. Fortunately, thoses
     99  * binaries features a __libc_atexit ELF section. We therefore assume we
    100  * have a Linux binary if we find this section.
    101  */
    102 static int
    103 ELFNAME2(linux,atexit_signature)(p, epp, eh)
    104 	struct proc *p;
    105 	struct exec_package *epp;
    106 	Elf_Ehdr *eh;
    107 {
    108 	size_t shsize;
    109 	int	strndx;
    110 	size_t i;
    111 	static const char signature[] = "__libc_atexit";
    112 	char* strtable;
    113 	Elf_Shdr *sh;
    114 
    115 	int error;
    116 
    117 	/*
    118 	 * load the section header table
    119 	 */
    120 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
    121 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
    122 	error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
    123 	if (error)
    124 		goto out;
    125 
    126 	/*
    127 	 * Now let's find the string table. If it does not exists, give up.
    128 	 */
    129 	strndx = (int)(eh->e_shstrndx);
    130 	if (strndx == SHN_UNDEF) {
    131 		error = ENOEXEC;
    132 		goto out;
    133 	}
    134 
    135 	/*
    136 	 * strndx is the index in section header table of the string table
    137 	 * section get the whole string table in strtable, and then we get access to the names
    138 	 * s->sh_name is the offset of the section name in strtable.
    139 	 */
    140 	strtable = malloc(sh[strndx].sh_size, M_TEMP, M_WAITOK);
    141 	error = exec_read_from(p, epp->ep_vp, sh[strndx].sh_offset, strtable,
    142 	    sh[strndx].sh_size);
    143 	if (error)
    144 		goto out;
    145 
    146 	for (i = 0; i < eh->e_shnum; i++) {
    147 		Elf_Shdr *s = &sh[i];
    148 		if (!memcmp((void*)(&(strtable[s->sh_name])), signature,
    149 				sizeof(signature))) {
    150 			DPRINTF(("linux_atexit_sig=%s\n",
    151 			    &(strtable[s->sh_name])));
    152 			error = 0;
    153 			goto out;
    154 		}
    155 	}
    156 	error = ENOEXEC;
    157 
    158 out:
    159 	free(sh, M_TEMP);
    160 	free(strtable, M_TEMP);
    161 	return (error);
    162 }
    163 #endif
    164 
    165 #ifdef LINUX_GCC_SIGNATURE
    166 /*
    167  * Take advantage of the fact that all the linux binaries are compiled
    168  * with gcc, and gcc sticks in the comment field a signature. Note that
    169  * on SVR4 binaries, the gcc signature will follow the OS name signature,
    170  * that will not be a problem. We don't bother to read in the string table,
    171  * but we check all the progbits headers.
    172  *
    173  * XXX This only works in the i386.  On the alpha (at least)
    174  * XXX we have the same gcc signature which incorrectly identifies
    175  * XXX NetBSD binaries as Linux.
    176  */
    177 static int
    178 ELFNAME2(linux,gcc_signature)(p, epp, eh)
    179 	struct proc *p;
    180 	struct exec_package *epp;
    181 	Elf_Ehdr *eh;
    182 {
    183 	size_t shsize;
    184 	size_t i;
    185 	static const char signature[] = "\0GCC: (GNU) ";
    186 	char buf[sizeof(signature) - 1];
    187 	Elf_Shdr *sh;
    188 	int error;
    189 
    190 	shsize = eh->e_shnum * sizeof(Elf_Shdr);
    191 	sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
    192 	error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize);
    193 	if (error)
    194 		goto out;
    195 
    196 	for (i = 0; i < eh->e_shnum; i++) {
    197 		Elf_Shdr *s = &sh[i];
    198 
    199 		/*
    200 		 * Identify candidates for the comment header;
    201 		 * Header cannot have a load address, or flags and
    202 		 * it must be large enough.
    203 		 */
    204 		if (s->sh_type != SHT_PROGBITS ||
    205 		    s->sh_addr != 0 ||
    206 		    s->sh_flags != 0 ||
    207 		    s->sh_size < sizeof(signature) - 1)
    208 			continue;
    209 
    210 		error = exec_read_from(p, epp->ep_vp, s->sh_offset, buf,
    211 		    sizeof(signature) - 1);
    212 		if (error)
    213 			continue;
    214 
    215 		/*
    216 		 * error is 0, if the signatures match we are done.
    217 		 */
    218 		DPRINTF(("linux_gcc_sig: sig=%s\n", buf));
    219 		if (!memcmp(buf, signature, sizeof(signature) - 1)) {
    220 			error = 0;
    221 			goto out;
    222 		}
    223 	}
    224 	error = ENOEXEC;
    225 
    226 out:
    227 	free(sh, M_TEMP);
    228 	return (error);
    229 }
    230 #endif
    231 
    232 static int
    233 ELFNAME2(linux,signature)(p, epp, eh, itp)
    234 	struct proc *p;
    235 	struct exec_package *epp;
    236 	Elf_Ehdr *eh;
    237 	char *itp;
    238 {
    239 	size_t i;
    240 	Elf_Phdr *ph;
    241 	size_t phsize;
    242 	int error;
    243 
    244 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    245 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
    246 	error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
    247 	if (error)
    248 		goto out;
    249 
    250 	for (i = 0; i < eh->e_phnum; i++) {
    251 		Elf_Phdr *ephp = &ph[i];
    252 		Elf_Nhdr *np;
    253 		u_int32_t *abi;
    254 
    255 		if (ephp->p_type != PT_NOTE ||
    256 		    ephp->p_filesz > 1024 ||
    257 		    ephp->p_filesz < sizeof(Elf_Nhdr) + 20)
    258 			continue;
    259 
    260 		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
    261 		error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
    262 		    ephp->p_filesz);
    263 		if (error)
    264 			goto next;
    265 
    266 		if (np->n_type != ELF_NOTE_TYPE_ABI_TAG ||
    267 		    np->n_namesz != ELF_NOTE_ABI_NAMESZ ||
    268 		    np->n_descsz != ELF_NOTE_ABI_DESCSZ ||
    269 		    memcmp((caddr_t)(np + 1), ELF_NOTE_ABI_NAME,
    270 		    ELF_NOTE_ABI_NAMESZ))
    271 			goto next;
    272 
    273 		/* Make sure the OS is Linux. */
    274 		abi = (u_int32_t *)((caddr_t)np + sizeof(Elf_Nhdr) +
    275 		    np->n_namesz);
    276 		if (abi[0] == ELF_NOTE_ABI_OS_LINUX)
    277 			error = 0;
    278 		else
    279 			error = ENOEXEC;
    280 		free(np, M_TEMP);
    281 		goto out;
    282 
    283 	next:
    284 		free(np, M_TEMP);
    285 		continue;
    286 	}
    287 
    288 	/* Check for certain intepreter names. */
    289 	if (itp[0]) {
    290 		if (!strncmp(itp, "/lib/ld-linux", 13) ||
    291 		    !strncmp(itp, "/lib/ld.so.", 11))
    292 			error = 0;
    293 		else
    294 			error = ENOEXEC;
    295 		goto out;
    296 	}
    297 
    298 	error = ENOEXEC;
    299 out:
    300 	free(ph, M_TEMP);
    301 	return (error);
    302 }
    303 
    304 int
    305 ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
    306 	struct proc *p;
    307 	struct exec_package *epp;
    308 	void *eh;
    309 	char *itp;
    310 	vaddr_t *pos;
    311 {
    312 	const char *bp;
    313 	int error;
    314 	size_t len;
    315 
    316 	if (((error = ELFNAME2(linux,signature)(p, epp, eh, itp)) != 0) &&
    317 #ifdef LINUX_GCC_SIGNATURE
    318 	    ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0) &&
    319 #endif
    320 #ifdef LINUX_ATEXIT_SIGNATURE
    321 	    ((error = ELFNAME2(linux,atexit_signature)(p, epp, eh)) != 0) &&
    322 #endif
    323 	    1)
    324 			return error;
    325 
    326 	if (itp[0]) {
    327 		if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path,
    328 		    itp, &bp, 0)))
    329 			return error;
    330 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
    331 			return error;
    332 		free((void *)bp, M_TEMP);
    333 	}
    334 	*pos = ELF_NO_ADDR;
    335 	DPRINTF(("linux_probe: returning 0\n"));
    336 	return 0;
    337 }
    338 
    339