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