Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_exec_elf32.c revision 1.9
      1 /*	$NetBSD: netbsd32_exec_elf32.c,v 1.9 2002/05/31 00:49:42 eeh Exp $	*/
      2 /*	from: NetBSD: exec_aout.c,v 1.15 1996/09/26 23:34:46 cgd Exp */
      3 
      4 /*
      5  * Copyright (c) 1998, 2001 Matthew R. Green.
      6  * Copyright (c) 1993, 1994 Christopher G. Demetriou
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed by Christopher G. Demetriou.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: netbsd32_exec_elf32.c,v 1.9 2002/05/31 00:49:42 eeh Exp $");
     37 
     38 #define	ELFSIZE		32
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/proc.h>
     43 #include <sys/malloc.h>
     44 #include <sys/vnode.h>
     45 #include <sys/exec.h>
     46 #include <sys/exec_elf.h>
     47 #include <sys/resourcevar.h>
     48 #include <sys/signal.h>
     49 #include <sys/signalvar.h>
     50 
     51 #include <compat/netbsd32/netbsd32.h>
     52 #include <compat/netbsd32/netbsd32_exec.h>
     53 
     54 #include <machine/frame.h>
     55 #include <machine/netbsd32_machdep.h>
     56 
     57 int netbsd32_copyinargs __P((struct exec_package *, struct ps_strings *,
     58 			     void *, size_t, const void *, const void *));
     59 
     60 extern int ELFNAME2(netbsd,signature) __P((struct proc *, struct exec_package *,
     61     Elf_Ehdr *));
     62 
     63 int
     64 ELFNAME2(netbsd32,probe)(p, epp, eh, itp, pos)
     65 	struct proc *p;
     66 	struct exec_package *epp;
     67 	void *eh;
     68 	char *itp;
     69 	vaddr_t *pos;
     70 {
     71 	int error;
     72 
     73 	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
     74 		return error;
     75 
     76 	return ELFNAME2(netbsd32,probe_noteless)(p, epp, eh, itp, pos);
     77 }
     78 
     79 int
     80 ELFNAME2(netbsd32,probe_noteless)(p, epp, eh, itp, pos)
     81 	struct proc *p;
     82 	struct exec_package *epp;
     83 	void *eh;
     84 	char *itp;
     85 	vaddr_t *pos;
     86 {
     87 	int error;
     88 	size_t i;
     89 	const char *bp;
     90 
     91 	if (itp[0]) {
     92 		/* Translate interpreter name if needed */
     93 		if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path,
     94 			itp, &bp, 0)) != 0) {
     95 			/*
     96 			 * See if we're chroot-ed.  Since it's the same OS,
     97 			 * there really shouldn't be any device node issues
     98 			 * so we might as well support chroot-ing to a 32-bit
     99 			 * installation.
    100 			 */
    101 			if ((error = emul_find(p, NULL, "/", itp, &bp, 0))
    102 				!= 0) {
    103 				return error;
    104 			}
    105 		}
    106 		if ((error = copystr(bp, itp, MAXPATHLEN, &i)) != 0)
    107 			return error;
    108 		free((void *)bp, M_TEMP);
    109 	}
    110 	epp->ep_flags |= EXEC_32;
    111 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
    112 	epp->ep_vm_maxaddr = USRSTACK32;
    113 	*pos = ELFDEFNNAME(NO_ADDR);
    114 	return 0;
    115 }
    116 
    117 /* round up and down to page boundaries. */
    118 #define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
    119 #define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
    120 
    121 /*
    122  * Copy arguments onto the stack in the normal way, but add some
    123  * extra information in case of dynamic binding.
    124  */
    125 int
    126 netbsd32_elf32_copyargs(pack, arginfo, stackp, argp)
    127 	struct exec_package *pack;
    128 	struct ps_strings *arginfo;
    129 	char **stackp;
    130 	void *argp;
    131 {
    132 	size_t len;
    133 	AuxInfo ai[ELF_AUX_ENTRIES], *a;
    134 	struct elf_args *ap;
    135 	int error;
    136 
    137 	if ((error = netbsd32_copyargs(pack, arginfo, stackp, argp)) != 0)
    138 		return error;
    139 
    140 	a = ai;
    141 
    142 	/*
    143 	 * Push extra arguments on the stack needed by dynamically
    144 	 * linked binaries
    145 	 */
    146 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
    147 
    148 		a->a_type = AT_PHDR;
    149 		a->a_v = ap->arg_phaddr;
    150 		a++;
    151 
    152 		a->a_type = AT_PHENT;
    153 		a->a_v = ap->arg_phentsize;
    154 		a++;
    155 
    156 		a->a_type = AT_PHNUM;
    157 		a->a_v = ap->arg_phnum;
    158 		a++;
    159 
    160 		a->a_type = AT_PAGESZ;
    161 		a->a_v = NBPG;
    162 		a++;
    163 
    164 		a->a_type = AT_BASE;
    165 		a->a_v = ap->arg_interp;
    166 		a++;
    167 
    168 		a->a_type = AT_FLAGS;
    169 		a->a_v = 0;
    170 		a++;
    171 
    172 		a->a_type = AT_ENTRY;
    173 		a->a_v = ap->arg_entry;
    174 		a++;
    175 
    176 		free((char *)ap, M_TEMP);
    177 		pack->ep_emul_arg = NULL;
    178 	}
    179 
    180 	a->a_type = AT_NULL;
    181 	a->a_v = 0;
    182 	a++;
    183 
    184 	len = (a - ai) * sizeof(AuxInfo);
    185 	if ((error = copyout(ai, *stackp, len)) != 0)
    186 		return error;
    187 	*stackp += len;
    188 
    189 	return 0;
    190 }
    191