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