Home | History | Annotate | Line # | Download | only in common
linux32_exec_elf32.c revision 1.9.4.2
      1  1.9.4.2   rmind /*	$NetBSD: linux32_exec_elf32.c,v 1.9.4.2 2011/03/05 20:52:53 rmind Exp $ */
      2      1.1    manu 
      3      1.1    manu /*-
      4      1.1    manu  * Copyright (c) 1995, 1998, 2000, 2001,2006 The NetBSD Foundation, Inc.
      5      1.1    manu  * All rights reserved.
      6      1.1    manu  *
      7      1.1    manu  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1    manu  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
      9      1.1    manu  * Emmanuel Dreyfus.
     10      1.1    manu  *
     11      1.1    manu  * Redistribution and use in source and binary forms, with or without
     12      1.1    manu  * modification, are permitted provided that the following conditions
     13      1.1    manu  * are met:
     14      1.1    manu  * 1. Redistributions of source code must retain the above copyright
     15      1.1    manu  *    notice, this list of conditions and the following disclaimer.
     16      1.1    manu  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.1    manu  *    notice, this list of conditions and the following disclaimer in the
     18      1.1    manu  *    documentation and/or other materials provided with the distribution.
     19      1.1    manu  *
     20      1.1    manu  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21      1.1    manu  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22      1.1    manu  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23      1.1    manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24      1.1    manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25      1.1    manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26      1.1    manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27      1.1    manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28      1.1    manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29      1.1    manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30      1.1    manu  * POSSIBILITY OF SUCH DAMAGE.
     31      1.1    manu  */
     32      1.1    manu 
     33      1.1    manu #include <sys/cdefs.h>
     34  1.9.4.2   rmind __KERNEL_RCSID(0, "$NetBSD: linux32_exec_elf32.c,v 1.9.4.2 2011/03/05 20:52:53 rmind Exp $");
     35      1.1    manu 
     36      1.1    manu #define	ELFSIZE		32
     37      1.1    manu 
     38      1.1    manu #include <sys/param.h>
     39      1.1    manu #include <sys/systm.h>
     40      1.1    manu #include <sys/proc.h>
     41      1.1    manu #include <sys/malloc.h>
     42      1.1    manu #include <sys/vnode.h>
     43      1.1    manu #include <sys/exec.h>
     44      1.1    manu #include <sys/exec_elf.h>
     45      1.3     skd #include <sys/kauth.h>
     46      1.1    manu #include <sys/kernel.h>
     47      1.1    manu #include <sys/resourcevar.h>
     48      1.1    manu #include <sys/signal.h>
     49      1.1    manu #include <sys/signalvar.h>
     50      1.1    manu 
     51      1.1    manu #include <compat/linux/common/linux_exec.h>
     52      1.1    manu #include <compat/netbsd32/netbsd32.h>
     53      1.1    manu #include <compat/netbsd32/netbsd32_exec.h>
     54      1.1    manu #include <compat/linux32/common/linux32_exec.h>
     55      1.1    manu 
     56  1.9.4.1   rmind #include <machine/cpuvar.h>
     57      1.1    manu #include <machine/frame.h>
     58      1.1    manu 
     59      1.1    manu #ifdef DEBUG_LINUX
     60      1.1    manu #define DPRINTF(a)      uprintf a
     61      1.1    manu #else
     62      1.1    manu #define DPRINTF(a)
     63      1.1    manu #endif
     64      1.1    manu 
     65      1.1    manu int linux32_copyinargs(struct exec_package *, struct ps_strings *,
     66      1.1    manu 			void *, size_t, const void *, const void *);
     67      1.1    manu 
     68      1.1    manu int
     69      1.9  cegger ELFNAME2(linux32,probe)(struct lwp *l, struct exec_package *epp,
     70      1.9  cegger 			void *eh, char *itp, vaddr_t *pos)
     71      1.1    manu {
     72      1.1    manu 	int error;
     73      1.1    manu 
     74      1.1    manu 	if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
     75      1.5    manu #ifdef LINUX32_GCC_SIGNATURE
     76      1.1    manu 	    ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
     77      1.1    manu #endif
     78      1.5    manu #ifdef LINUX32_ATEXIT_SIGNATURE
     79      1.1    manu 	    ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
     80      1.1    manu #endif
     81      1.5    manu #ifdef LINUX32_DEBUGLINK_SIGNATURE
     82      1.5    manu 	    ((error = ELFNAME2(linux,debuglink_signature)(l, epp, eh)) != 0) &&
     83      1.5    manu #endif
     84      1.1    manu 	    1)
     85      1.1    manu 			return error;
     86      1.1    manu 
     87      1.1    manu 	if (itp) {
     88      1.7     dsl 		if ((error = emul_find_interp(l, epp, itp)))
     89      1.1    manu 			return (error);
     90      1.1    manu 	}
     91      1.1    manu #if 0
     92      1.1    manu 	DPRINTF(("linux32_probe: returning 0\n"));
     93      1.1    manu #endif
     94      1.1    manu 
     95  1.9.4.2   rmind 	epp->ep_flags |= EXEC_32 | EXEC_FORCEAUX;
     96      1.1    manu 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
     97      1.1    manu 	epp->ep_vm_maxaddr = USRSTACK32;
     98      1.1    manu 
     99      1.1    manu 	return 0;
    100      1.1    manu }
    101      1.1    manu 
    102      1.1    manu /*
    103      1.1    manu  * Copy arguments onto the stack in the normal way, but add some
    104      1.1    manu  * extra information in case of dynamic binding.
    105      1.1    manu  */
    106      1.1    manu int
    107      1.1    manu linux32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
    108      1.1    manu     struct ps_strings *arginfo, char **stackp, void *argp)
    109      1.1    manu {
    110  1.9.4.2   rmind 	Aux32Info ai[LINUX32_ELF_AUX_ENTRIES], *a;
    111      1.1    manu 	struct elf_args *ap;
    112      1.1    manu 	struct vattr *vap;
    113  1.9.4.2   rmind 	size_t len;
    114      1.1    manu 	int error;
    115      1.1    manu 
    116      1.1    manu 	if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
    117      1.1    manu 		return error;
    118      1.1    manu 
    119  1.9.4.2   rmind 	a = ai;
    120  1.9.4.2   rmind 
    121      1.1    manu 	/*
    122      1.1    manu 	 * Push extra arguments on the stack needed by dynamically
    123      1.1    manu 	 * linked binaries and static binaries as well.
    124      1.1    manu 	 */
    125      1.1    manu 
    126  1.9.4.2   rmind 	a->a_type = AT_PAGESZ;
    127  1.9.4.2   rmind 	a->a_v = PAGE_SIZE;
    128  1.9.4.2   rmind 	a++;
    129      1.1    manu 
    130  1.9.4.2   rmind 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
    131      1.1    manu 
    132  1.9.4.2   rmind 		a->a_type = AT_PHDR;
    133  1.9.4.2   rmind 		a->a_v = ap->arg_phaddr;
    134  1.9.4.2   rmind 		a++;
    135      1.1    manu 
    136  1.9.4.2   rmind 		a->a_type = AT_PHENT;
    137  1.9.4.2   rmind 		a->a_v = ap->arg_phentsize;
    138  1.9.4.2   rmind 		a++;
    139      1.1    manu 
    140  1.9.4.2   rmind 		a->a_type = AT_PHNUM;
    141  1.9.4.2   rmind 		a->a_v = ap->arg_phnum;
    142  1.9.4.2   rmind 		a++;
    143      1.1    manu 
    144  1.9.4.2   rmind 		a->a_type = AT_BASE;
    145  1.9.4.2   rmind 		a->a_v = ap->arg_interp;
    146  1.9.4.2   rmind 		a++;
    147      1.1    manu 
    148  1.9.4.2   rmind 		a->a_type = AT_FLAGS;
    149  1.9.4.2   rmind 		a->a_v = 0;
    150  1.9.4.2   rmind 		a++;
    151      1.1    manu 
    152  1.9.4.2   rmind 		a->a_type = AT_ENTRY;
    153  1.9.4.2   rmind 		a->a_v = ap->arg_entry;
    154  1.9.4.2   rmind 		a++;
    155      1.1    manu 
    156  1.9.4.2   rmind 		free(ap, M_TEMP);
    157  1.9.4.2   rmind 		pack->ep_emul_arg = NULL;
    158  1.9.4.2   rmind 	}
    159      1.1    manu 
    160  1.9.4.2   rmind 	/* Linux-specific items */
    161  1.9.4.2   rmind 	a->a_type = LINUX_AT_CLKTCK;
    162  1.9.4.2   rmind 	a->a_v = hz;
    163  1.9.4.2   rmind 	a++;
    164      1.1    manu 
    165  1.9.4.2   rmind 	vap = pack->ep_vap;
    166      1.1    manu 
    167  1.9.4.2   rmind 	a->a_type = LINUX_AT_UID;
    168  1.9.4.2   rmind 	a->a_v = kauth_cred_getuid(l->l_cred);
    169  1.9.4.2   rmind 	a++;
    170      1.1    manu 
    171  1.9.4.2   rmind 	a->a_type = LINUX_AT_EUID;
    172  1.9.4.2   rmind 	a->a_v = ((vap->va_mode & S_ISUID) ?
    173      1.4      ad 	    vap->va_uid : kauth_cred_geteuid(l->l_cred));
    174  1.9.4.2   rmind 	a++;
    175      1.1    manu 
    176  1.9.4.2   rmind 	a->a_type = LINUX_AT_GID;
    177  1.9.4.2   rmind 	a->a_v = kauth_cred_getgid(l->l_cred);
    178  1.9.4.2   rmind 	a++;
    179      1.1    manu 
    180  1.9.4.2   rmind 	a->a_type = LINUX_AT_EGID;
    181  1.9.4.2   rmind 	a->a_v = ((vap->va_mode & S_ISGID) ?
    182  1.9.4.2   rmind 	    vap->va_gid : kauth_cred_getegid(l->l_cred));
    183  1.9.4.2   rmind 	a++;
    184      1.1    manu 
    185  1.9.4.2   rmind 	a->a_type = LINUX_AT_SECURE;
    186  1.9.4.2   rmind 	a->a_v = 0;
    187  1.9.4.2   rmind 	a++;
    188      1.1    manu 
    189  1.9.4.2   rmind #if 0
    190  1.9.4.2   rmind 	a->a_type = LINUX_AT_SYSINFO;
    191  1.9.4.2   rmind 	a->a_v = NETBSD32PTR32I(&esdp->kernel_vsyscall[0]);
    192  1.9.4.2   rmind 	a++;
    193  1.9.4.2   rmind 
    194  1.9.4.2   rmind 	a->a_type = LINUX_AT_SYSINFO_EHDR;
    195  1.9.4.2   rmind 	a->a_v = NETBSD32PTR32I(&esdp->elfhdr);
    196  1.9.4.2   rmind 	a++;
    197  1.9.4.2   rmind 
    198  1.9.4.2   rmind 	a->a_type = LINUX_AT_HWCAP;
    199  1.9.4.2   rmind 	a->a_v = LINUX32_CPUCAP;
    200  1.9.4.2   rmind 	a++;
    201  1.9.4.2   rmind 
    202  1.9.4.2   rmind 	a->a_type = LINUX_AT_PLATFORM;
    203  1.9.4.2   rmind 	a->a_v = NETBSD32PTR32I(&esdp->hw_platform[0]);
    204  1.9.4.2   rmind 	a++;
    205  1.9.4.2   rmind #endif
    206  1.9.4.2   rmind 
    207  1.9.4.2   rmind 	a->a_type = AT_NULL;
    208  1.9.4.2   rmind 	a->a_v = 0;
    209  1.9.4.2   rmind 	a++;
    210      1.1    manu 
    211  1.9.4.2   rmind #if 0
    212  1.9.4.2   rmind 	memcpy(esd.kernel_vsyscall, linux32_kernel_vsyscall,
    213      1.1    manu 	    sizeof(linux32_kernel_vsyscall));
    214      1.1    manu 
    215      1.1    manu 	memcpy(&esd.elfhdr, eh, sizeof(*eh));
    216      1.1    manu 
    217  1.9.4.2   rmind 	strcpy(esd.hw_platform, LINUX32_PLATFORM);
    218      1.1    manu 
    219      1.1    manu 	/*
    220      1.1    manu 	 * Copy out the ELF auxiliary table and hw platform name
    221      1.1    manu 	 */
    222      1.1    manu 	if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
    223      1.1    manu 		return error;
    224      1.1    manu 	*stackp += sizeof(esd);
    225  1.9.4.2   rmind #endif
    226  1.9.4.2   rmind 
    227  1.9.4.2   rmind 	len = (a - ai) * sizeof(AuxInfo);
    228  1.9.4.2   rmind 	if ((error = copyout(ai, *stackp, len)) != 0)
    229  1.9.4.2   rmind 		return error;
    230  1.9.4.2   rmind 	*stackp += len;
    231  1.9.4.2   rmind 
    232      1.1    manu 	return 0;
    233      1.1    manu }
    234