Home | History | Annotate | Line # | Download | only in common
linux32_exec_elf32.c revision 1.1
      1 /*	$NetBSD: linux32_exec_elf32.c,v 1.1 2006/02/09 19:18:57 manu Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998, 2000, 2001,2006 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 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: linux32_exec_elf32.c,v 1.1 2006/02/09 19:18:57 manu Exp $");
     42 
     43 #define	ELFSIZE		32
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/proc.h>
     48 #include <sys/malloc.h>
     49 #include <sys/vnode.h>
     50 #include <sys/exec.h>
     51 #include <sys/exec_elf.h>
     52 #include <sys/kernel.h>
     53 #include <sys/resourcevar.h>
     54 #include <sys/signal.h>
     55 #include <sys/signalvar.h>
     56 
     57 #include <compat/linux/common/linux_exec.h>
     58 #include <compat/netbsd32/netbsd32.h>
     59 #include <compat/netbsd32/netbsd32_exec.h>
     60 #include <compat/linux32/common/linux32_exec.h>
     61 
     62 #include <machine/frame.h>
     63 
     64 #ifdef DEBUG_LINUX
     65 #define DPRINTF(a)      uprintf a
     66 #else
     67 #define DPRINTF(a)
     68 #endif
     69 
     70 #if 0
     71 static int ELFNAME2(linux32,signature) __P((struct lwp *, struct exec_package *,
     72 	Elf_Ehdr *, char *));
     73 #ifdef LINUX_GCC_SIGNATURE
     74 static int ELFNAME2(linux32,gcc_signature) __P((struct lwp *l,
     75 	struct exec_package *, Elf_Ehdr *));
     76 #endif
     77 #ifdef LINUX_ATEXIT_SIGNATURE
     78 static int ELFNAME2(linux32,atexit_signature) __P((struct lwp *l,
     79 	struct exec_package *, Elf_Ehdr *));
     80 #endif
     81 #endif
     82 int linux32_copyinargs(struct exec_package *, struct ps_strings *,
     83 			void *, size_t, const void *, const void *);
     84 
     85 int
     86 ELFNAME2(linux32,probe)(l, epp, eh, itp, pos)
     87 	struct lwp *l;
     88 	struct exec_package *epp;
     89 	void *eh;
     90 	char *itp;
     91 	vaddr_t *pos;
     92 {
     93 	int error;
     94 
     95 	if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
     96 #ifdef LINUX_GCC_SIGNATURE
     97 	    ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
     98 #endif
     99 #ifdef LINUX_ATEXIT_SIGNATURE
    100 	    ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
    101 #endif
    102 	    1)
    103 			return error;
    104 
    105 	if (itp) {
    106 		if ((error = emul_find_interp(l, epp->ep_esch->es_emul->e_path,
    107 		    itp)))
    108 			return (error);
    109 	}
    110 #if 0
    111 	DPRINTF(("linux32_probe: returning 0\n"));
    112 #endif
    113 
    114 	epp->ep_flags |= EXEC_32;
    115 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
    116 	epp->ep_vm_maxaddr = USRSTACK32;
    117 
    118 	return 0;
    119 }
    120 
    121 /* round up and down to page boundaries. */
    122 #define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
    123 #define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
    124 
    125 /*
    126  * Copy arguments onto the stack in the normal way, but add some
    127  * extra information in case of dynamic binding.
    128  */
    129 int
    130 linux32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
    131     struct ps_strings *arginfo, char **stackp, void *argp)
    132 {
    133 	struct linux32_extra_stack_data *esdp, esd;
    134 	struct elf_args *ap;
    135 	struct vattr *vap;
    136 	struct proc *p;
    137 	Elf_Ehdr *eh;
    138 	Elf_Phdr *ph;
    139 	Elf_Addr phdr = 0;
    140 	u_long phsize;
    141 	int error;
    142 	int i;
    143 
    144 	p = l->l_proc;
    145 
    146 	if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
    147 		return error;
    148 
    149 	/*
    150 	 * Push extra arguments on the stack needed by dynamically
    151 	 * linked binaries and static binaries as well.
    152 	 */
    153 	memset(&esd, 0, sizeof(esd));
    154 	esdp = (struct linux32_extra_stack_data *)(*stackp);
    155 	ap = (struct elf_args *)pack->ep_emul_arg;
    156 	vap = pack->ep_vap;
    157 	eh = (Elf_Ehdr *)pack->ep_hdr;
    158 
    159 	/*
    160 	 * We forgot this, so we ned to reload it now. XXX keep track of it?
    161 	 */
    162 	if (ap == NULL) {
    163 		phsize = eh->e_phnum * sizeof(Elf_Phdr);
    164 		ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
    165 		error = exec_read_from(l, pack->ep_vp, eh->e_phoff, ph, phsize);
    166 		if (error != 0) {
    167 			for (i = 0; i < eh->e_phnum; i++) {
    168 				if (ph[i].p_type == PT_PHDR) {
    169 					phdr = ph[i].p_vaddr;
    170 					break;
    171 				}
    172 			}
    173 		}
    174 		free(ph, M_TEMP);
    175 	}
    176 
    177 
    178 	/*
    179 	 * The exec_package doesn't have a proc pointer and it's not
    180 	 * exactly trivial to add one since the credentials are
    181 	 * changing. XXX Linux uses curlwp's credentials.
    182 	 * Why can't we use them too?
    183 	 */
    184 
    185 	i = 0;
    186 	esd.ai[i].a_type = LINUX_AT_SYSINFO;
    187 	esd.ai[i++].a_v = (netbsd32_pointer_t)(long)&esdp->kernel_vsyscall[0];
    188 
    189 	esd.ai[i].a_type = LINUX_AT_SYSINFO_EHDR;
    190 	esd.ai[i++].a_v = (netbsd32_pointer_t)(long)&esdp->elfhdr;
    191 
    192 	esd.ai[i].a_type = LINUX_AT_HWCAP;
    193 	esd.ai[i++].a_v = LINUX32_CPUCAP;
    194 
    195 	esd.ai[i].a_type = AT_PAGESZ;
    196 	esd.ai[i++].a_v = PAGE_SIZE;
    197 
    198 	esd.ai[i].a_type = LINUX_AT_CLKTCK;
    199 	esd.ai[i++].a_v = hz;
    200 
    201 	esd.ai[i].a_type = AT_PHDR;
    202 	esd.ai[i++].a_v = (ap ? ap->arg_phaddr: phdr);
    203 
    204 	esd.ai[i].a_type = AT_PHENT;
    205 	esd.ai[i++].a_v = (ap ? ap->arg_phentsize : eh->e_phentsize);
    206 
    207 	esd.ai[i].a_type = AT_PHNUM;
    208 	esd.ai[i++].a_v = (ap ? ap->arg_phnum : eh->e_phnum);
    209 
    210 	esd.ai[i].a_type = AT_BASE;
    211 	esd.ai[i++].a_v = (ap ? ap->arg_interp : 0);
    212 
    213 	esd.ai[i].a_type = AT_FLAGS;
    214 	esd.ai[i++].a_v = 0;
    215 
    216 	esd.ai[i].a_type = AT_ENTRY;
    217 	esd.ai[i++].a_v = (ap ? ap->arg_entry : eh->e_entry);
    218 
    219 	esd.ai[i].a_type = LINUX_AT_EGID;
    220 	esd.ai[i++].a_v =
    221 	    ((vap->va_mode & S_ISGID) ? vap->va_gid : p->p_ucred->cr_gid);
    222 
    223 	esd.ai[i].a_type = LINUX_AT_GID;
    224 	esd.ai[i++].a_v = p->p_cred->p_rgid;
    225 
    226 	esd.ai[i].a_type = LINUX_AT_EUID;
    227 	esd.ai[i++].a_v =
    228 	    ((vap->va_mode & S_ISUID) ? vap->va_uid : p->p_ucred->cr_uid);
    229 
    230 	esd.ai[i].a_type = LINUX_AT_UID;
    231 	esd.ai[i++].a_v = p->p_cred->p_ruid;
    232 
    233 	esd.ai[i].a_type = LINUX_AT_SECURE;
    234 	esd.ai[i++].a_v = 0;
    235 
    236 	esd.ai[i].a_type = LINUX_AT_PLATFORM;
    237 	esd.ai[i++].a_v = (netbsd32_pointer_t)(long)&esdp->hw_platform[0];
    238 
    239 	esd.ai[i].a_type = AT_NULL;
    240 	esd.ai[i++].a_v = 0;
    241 
    242 #ifdef DEBUG_LINUX
    243 	if (i != LINUX32_ELF_AUX_ENTRIES) {
    244 		printf("linux32_elf32_copyargs: %d Aux entries\n", i);
    245 		return EINVAL;
    246 	}
    247 #endif
    248 
    249 	memcpy(esd.kernel_vsyscall, linux32_kernel_vsyscall,
    250 	    sizeof(linux32_kernel_vsyscall));
    251 
    252 	memcpy(&esd.elfhdr, eh, sizeof(*eh));
    253 
    254 	strcpy(esd.hw_platform, LINUX32_PLATFORM);
    255 
    256 	if (ap) {
    257 		free((char *)ap, M_TEMP);
    258 		pack->ep_emul_arg = NULL;
    259 	}
    260 
    261 	/*
    262 	 * Copy out the ELF auxiliary table and hw platform name
    263 	 */
    264 	if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
    265 		return error;
    266 	*stackp += sizeof(esd);
    267 	return 0;
    268 }
    269 
    270