Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_exec_elf32.c revision 1.31
      1  1.31      matt /*	$NetBSD: netbsd32_exec_elf32.c,v 1.31 2009/12/14 00:47:11 matt Exp $	*/
      2   1.1  jdolecek /*	from: NetBSD: exec_aout.c,v 1.15 1996/09/26 23:34:46 cgd Exp */
      3   1.1  jdolecek 
      4   1.1  jdolecek /*
      5   1.7       mrg  * Copyright (c) 1998, 2001 Matthew R. Green.
      6  1.20       agc  * All rights reserved.
      7  1.20       agc  *
      8  1.20       agc  * Redistribution and use in source and binary forms, with or without
      9  1.20       agc  * modification, are permitted provided that the following conditions
     10  1.20       agc  * are met:
     11  1.20       agc  * 1. Redistributions of source code must retain the above copyright
     12  1.20       agc  *    notice, this list of conditions and the following disclaimer.
     13  1.20       agc  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.20       agc  *    notice, this list of conditions and the following disclaimer in the
     15  1.20       agc  *    documentation and/or other materials provided with the distribution.
     16  1.20       agc  *
     17  1.20       agc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.20       agc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.20       agc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.20       agc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.20       agc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.20       agc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.20       agc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.20       agc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.20       agc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.20       agc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.20       agc  */
     28  1.20       agc 
     29  1.20       agc /*
     30   1.1  jdolecek  * Copyright (c) 1993, 1994 Christopher G. Demetriou
     31   1.1  jdolecek  * All rights reserved.
     32   1.1  jdolecek  *
     33   1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     34   1.1  jdolecek  * modification, are permitted provided that the following conditions
     35   1.1  jdolecek  * are met:
     36   1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     37   1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     38   1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     39   1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     40   1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     41   1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     42   1.1  jdolecek  *    must display the following acknowledgement:
     43   1.1  jdolecek  *      This product includes software developed by Christopher G. Demetriou.
     44   1.1  jdolecek  * 4. The name of the author may not be used to endorse or promote products
     45   1.1  jdolecek  *    derived from this software without specific prior written permission
     46   1.1  jdolecek  *
     47   1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48   1.1  jdolecek  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49   1.1  jdolecek  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50   1.1  jdolecek  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51   1.1  jdolecek  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52   1.1  jdolecek  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53   1.1  jdolecek  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54   1.1  jdolecek  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55   1.1  jdolecek  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56   1.1  jdolecek  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57   1.1  jdolecek  */
     58   1.8     lukem 
     59   1.8     lukem #include <sys/cdefs.h>
     60  1.31      matt __KERNEL_RCSID(0, "$NetBSD: netbsd32_exec_elf32.c,v 1.31 2009/12/14 00:47:11 matt Exp $");
     61   1.1  jdolecek 
     62   1.1  jdolecek #define	ELFSIZE		32
     63   1.1  jdolecek 
     64   1.1  jdolecek #include <sys/param.h>
     65   1.1  jdolecek #include <sys/systm.h>
     66   1.1  jdolecek #include <sys/proc.h>
     67   1.1  jdolecek #include <sys/malloc.h>
     68   1.1  jdolecek #include <sys/vnode.h>
     69   1.1  jdolecek #include <sys/exec.h>
     70   1.1  jdolecek #include <sys/exec_elf.h>
     71   1.1  jdolecek #include <sys/resourcevar.h>
     72   1.1  jdolecek #include <sys/signal.h>
     73   1.1  jdolecek #include <sys/signalvar.h>
     74  1.25      elad #include <sys/kauth.h>
     75  1.29       mrg #include <sys/namei.h>
     76   1.1  jdolecek 
     77   1.1  jdolecek #include <compat/netbsd32/netbsd32.h>
     78   1.1  jdolecek #include <compat/netbsd32/netbsd32_exec.h>
     79   1.1  jdolecek 
     80   1.1  jdolecek #include <machine/netbsd32_machdep.h>
     81   1.1  jdolecek 
     82  1.22     perry int netbsd32_copyinargs(struct exec_package *, struct ps_strings *,
     83  1.10      fvdl 			void *, size_t, const void *, const void *);
     84  1.23  christos int ELFNAME2(netbsd32,probe_noteless)(struct lwp *, struct exec_package *epp,
     85  1.10      fvdl 				      void *eh, char *itp, vaddr_t *pos);
     86  1.24      cube extern int ELFNAME2(netbsd,signature)(struct lwp *, struct exec_package *,
     87  1.10      fvdl 				      Elf_Ehdr *);
     88   1.1  jdolecek 
     89   1.1  jdolecek int
     90  1.23  christos ELFNAME2(netbsd32,probe)(struct lwp *l, struct exec_package *epp,
     91  1.10      fvdl 			 void *eh, char *itp, vaddr_t *pos)
     92   1.1  jdolecek {
     93   1.1  jdolecek 	int error;
     94   1.1  jdolecek 
     95  1.24      cube 	if ((error = ELFNAME2(netbsd,signature)(l, epp, eh)) != 0)
     96   1.1  jdolecek 		return error;
     97   1.1  jdolecek 
     98  1.31      matt #ifdef ELF_MD_PROBE_FUNC
     99  1.31      matt 	if ((error = ELF_MD_PROBE_FUNC(l, epp, eh, itp, pos)) != 0)
    100  1.31      matt 		return error;
    101  1.31      matt #elif defined(ELF_INTERP_NON_RELOCATABLE)
    102  1.31      matt 	*pos = ELF_LINK_ADDR;
    103  1.31      matt #endif
    104  1.31      matt 
    105  1.23  christos 	return ELFNAME2(netbsd32,probe_noteless)(l, epp, eh, itp, pos);
    106   1.9       eeh }
    107   1.9       eeh 
    108   1.9       eeh int
    109  1.23  christos ELFNAME2(netbsd32,probe_noteless)(struct lwp *l, struct exec_package *epp,
    110  1.10      fvdl 				  void *eh, char *itp, vaddr_t *pos)
    111   1.9       eeh {
    112   1.9       eeh 	int error;
    113   1.9       eeh 
    114  1.21  drochner 	if (itp) {
    115  1.29       mrg 		/*
    116  1.29       mrg 		 * If the path is exactly "/usr/libexec/ld.elf_so", first
    117  1.29       mrg 		 * try to see if "/usr/libexec/ld.elf_so-<arch>" exists
    118  1.29       mrg 		 * and if so, use that instead.
    119  1.29       mrg 		 * XXX maybe move this into compat/common
    120  1.29       mrg 		 */
    121  1.29       mrg 		error = 0;
    122  1.29       mrg 		if (strcmp(itp, "/usr/libexec/ld.elf_so") == 0 ||
    123  1.29       mrg 		    strcmp(itp, "/libexec/ld.elf_so") == 0) {
    124  1.29       mrg 			extern const char machine32[];
    125  1.30  dholland 			struct vnode *vp;
    126  1.29       mrg 			char *path;
    127  1.29       mrg 
    128  1.29       mrg 			if (epp->ep_interp != NULL)
    129  1.29       mrg 				vrele(epp->ep_interp);
    130  1.29       mrg 
    131  1.29       mrg 			path = PNBUF_GET();
    132  1.29       mrg 			snprintf(path, MAXPATHLEN, "%s-%s", itp, machine32);
    133  1.30  dholland 			error = namei_simple_kernel(path,
    134  1.30  dholland 					NSM_FOLLOW_NOEMULROOT, &vp);
    135  1.29       mrg 			/*
    136  1.29       mrg 			 * If that worked, save interpreter in case we
    137  1.29       mrg 			 * actually need to load it
    138  1.29       mrg 			 */
    139  1.29       mrg 			if (error != 0)
    140  1.29       mrg 				epp->ep_interp = NULL;
    141  1.29       mrg 			else
    142  1.30  dholland 				epp->ep_interp = vp;
    143  1.29       mrg 			PNBUF_PUT(path);
    144  1.29       mrg 		}
    145  1.29       mrg 
    146   1.9       eeh 		/* Translate interpreter name if needed */
    147  1.29       mrg 		if (error && (error = emul_find_interp(l, epp, itp)) != 0)
    148   1.1  jdolecek 			return error;
    149   1.1  jdolecek 	}
    150   1.1  jdolecek 	epp->ep_flags |= EXEC_32;
    151   1.4       eeh 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
    152   1.4       eeh 	epp->ep_vm_maxaddr = USRSTACK32;
    153  1.21  drochner #ifdef ELF_INTERP_NON_RELOCATABLE
    154  1.21  drochner 	*pos = ELF_LINK_ADDR;
    155  1.21  drochner #endif
    156   1.1  jdolecek 	return 0;
    157   1.1  jdolecek }
    158   1.1  jdolecek 
    159   1.1  jdolecek /* round up and down to page boundaries. */
    160   1.1  jdolecek #define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
    161   1.1  jdolecek #define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
    162   1.1  jdolecek 
    163   1.1  jdolecek /*
    164   1.1  jdolecek  * Copy arguments onto the stack in the normal way, but add some
    165   1.1  jdolecek  * extra information in case of dynamic binding.
    166   1.1  jdolecek  */
    167   1.5  christos int
    168  1.23  christos netbsd32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
    169  1.11  christos     struct ps_strings *arginfo, char **stackp, void *argp)
    170   1.1  jdolecek {
    171   1.1  jdolecek 	size_t len;
    172   1.1  jdolecek 	AuxInfo ai[ELF_AUX_ENTRIES], *a;
    173   1.1  jdolecek 	struct elf_args *ap;
    174   1.6       eeh 	int error;
    175   1.1  jdolecek 
    176  1.23  christos 	if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
    177   1.5  christos 		return error;
    178   1.1  jdolecek 
    179   1.1  jdolecek 	a = ai;
    180   1.1  jdolecek 
    181   1.1  jdolecek 	/*
    182   1.1  jdolecek 	 * Push extra arguments on the stack needed by dynamically
    183   1.1  jdolecek 	 * linked binaries
    184   1.1  jdolecek 	 */
    185   1.1  jdolecek 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
    186   1.1  jdolecek 
    187   1.1  jdolecek 		a->a_type = AT_PHDR;
    188   1.1  jdolecek 		a->a_v = ap->arg_phaddr;
    189   1.1  jdolecek 		a++;
    190   1.1  jdolecek 
    191   1.1  jdolecek 		a->a_type = AT_PHENT;
    192   1.1  jdolecek 		a->a_v = ap->arg_phentsize;
    193   1.1  jdolecek 		a++;
    194   1.1  jdolecek 
    195   1.1  jdolecek 		a->a_type = AT_PHNUM;
    196   1.1  jdolecek 		a->a_v = ap->arg_phnum;
    197   1.1  jdolecek 		a++;
    198   1.1  jdolecek 
    199   1.1  jdolecek 		a->a_type = AT_PAGESZ;
    200  1.16   thorpej 		a->a_v = PAGE_SIZE;
    201   1.1  jdolecek 		a++;
    202   1.1  jdolecek 
    203   1.1  jdolecek 		a->a_type = AT_BASE;
    204   1.1  jdolecek 		a->a_v = ap->arg_interp;
    205   1.1  jdolecek 		a++;
    206   1.1  jdolecek 
    207   1.1  jdolecek 		a->a_type = AT_FLAGS;
    208   1.1  jdolecek 		a->a_v = 0;
    209   1.1  jdolecek 		a++;
    210   1.1  jdolecek 
    211   1.1  jdolecek 		a->a_type = AT_ENTRY;
    212   1.1  jdolecek 		a->a_v = ap->arg_entry;
    213  1.11  christos 		a++;
    214  1.11  christos 
    215  1.12  christos 		a->a_type = AT_EUID;
    216  1.26        ad 		a->a_v = kauth_cred_geteuid(l->l_cred);
    217  1.11  christos 		a++;
    218  1.11  christos 
    219  1.12  christos 		a->a_type = AT_RUID;
    220  1.26        ad 		a->a_v = kauth_cred_getuid(l->l_cred);
    221  1.11  christos 		a++;
    222  1.11  christos 
    223  1.12  christos 		a->a_type = AT_EGID;
    224  1.26        ad 		a->a_v = kauth_cred_getegid(l->l_cred);
    225  1.11  christos 		a++;
    226  1.11  christos 
    227  1.12  christos 		a->a_type = AT_RGID;
    228  1.26        ad 		a->a_v = kauth_cred_getgid(l->l_cred);
    229   1.1  jdolecek 		a++;
    230   1.1  jdolecek 
    231   1.1  jdolecek 		free((char *)ap, M_TEMP);
    232   1.1  jdolecek 		pack->ep_emul_arg = NULL;
    233   1.1  jdolecek 	}
    234   1.1  jdolecek 
    235   1.1  jdolecek 	a->a_type = AT_NULL;
    236   1.1  jdolecek 	a->a_v = 0;
    237   1.1  jdolecek 	a++;
    238   1.1  jdolecek 
    239   1.1  jdolecek 	len = (a - ai) * sizeof(AuxInfo);
    240   1.5  christos 	if ((error = copyout(ai, *stackp, len)) != 0)
    241   1.5  christos 		return error;
    242   1.5  christos 	*stackp += len;
    243   1.1  jdolecek 
    244   1.5  christos 	return 0;
    245   1.1  jdolecek }
    246