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