Home | History | Annotate | Line # | Download | only in common
linux32_exec_elf32.c revision 1.5.12.1
      1  1.5.12.1  yamt /*	$NetBSD: linux32_exec_elf32.c,v 1.5.12.1 2007/03/24 14:55:10 yamt Exp $ */
      2       1.1  manu 
      3       1.1  manu /*-
      4       1.1  manu  * Copyright (c) 1995, 1998, 2000, 2001,2006 The NetBSD Foundation, Inc.
      5       1.1  manu  * All rights reserved.
      6       1.1  manu  *
      7       1.1  manu  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  manu  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
      9       1.1  manu  * Emmanuel Dreyfus.
     10       1.1  manu  *
     11       1.1  manu  * Redistribution and use in source and binary forms, with or without
     12       1.1  manu  * modification, are permitted provided that the following conditions
     13       1.1  manu  * are met:
     14       1.1  manu  * 1. Redistributions of source code must retain the above copyright
     15       1.1  manu  *    notice, this list of conditions and the following disclaimer.
     16       1.1  manu  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1  manu  *    notice, this list of conditions and the following disclaimer in the
     18       1.1  manu  *    documentation and/or other materials provided with the distribution.
     19       1.1  manu  * 3. All advertising materials mentioning features or use of this software
     20       1.1  manu  *    must display the following acknowledgement:
     21       1.1  manu  *      This product includes software developed by the NetBSD
     22       1.1  manu  *      Foundation, Inc. and its contributors.
     23       1.1  manu  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1  manu  *    contributors may be used to endorse or promote products derived
     25       1.1  manu  *    from this software without specific prior written permission.
     26       1.1  manu  *
     27       1.1  manu  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1  manu  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1  manu  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1  manu  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1  manu  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1  manu  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1  manu  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1  manu  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1  manu  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1  manu  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1  manu  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1  manu  */
     39       1.1  manu 
     40       1.1  manu #include <sys/cdefs.h>
     41  1.5.12.1  yamt __KERNEL_RCSID(0, "$NetBSD: linux32_exec_elf32.c,v 1.5.12.1 2007/03/24 14:55:10 yamt Exp $");
     42       1.1  manu 
     43       1.1  manu #define	ELFSIZE		32
     44       1.1  manu 
     45       1.1  manu #include <sys/param.h>
     46       1.1  manu #include <sys/systm.h>
     47       1.1  manu #include <sys/proc.h>
     48       1.1  manu #include <sys/malloc.h>
     49       1.1  manu #include <sys/vnode.h>
     50       1.1  manu #include <sys/exec.h>
     51       1.1  manu #include <sys/exec_elf.h>
     52       1.3   skd #include <sys/kauth.h>
     53       1.1  manu #include <sys/kernel.h>
     54       1.1  manu #include <sys/resourcevar.h>
     55       1.1  manu #include <sys/signal.h>
     56       1.1  manu #include <sys/signalvar.h>
     57       1.1  manu 
     58       1.1  manu #include <compat/linux/common/linux_exec.h>
     59       1.1  manu #include <compat/netbsd32/netbsd32.h>
     60       1.1  manu #include <compat/netbsd32/netbsd32_exec.h>
     61       1.1  manu #include <compat/linux32/common/linux32_exec.h>
     62       1.1  manu 
     63       1.1  manu #include <machine/frame.h>
     64       1.1  manu 
     65       1.1  manu #ifdef DEBUG_LINUX
     66       1.1  manu #define DPRINTF(a)      uprintf a
     67       1.1  manu #else
     68       1.1  manu #define DPRINTF(a)
     69       1.1  manu #endif
     70       1.1  manu 
     71       1.1  manu int linux32_copyinargs(struct exec_package *, struct ps_strings *,
     72       1.1  manu 			void *, size_t, const void *, const void *);
     73       1.1  manu 
     74       1.1  manu int
     75       1.1  manu ELFNAME2(linux32,probe)(l, epp, eh, itp, pos)
     76       1.1  manu 	struct lwp *l;
     77       1.1  manu 	struct exec_package *epp;
     78       1.1  manu 	void *eh;
     79       1.1  manu 	char *itp;
     80       1.1  manu 	vaddr_t *pos;
     81       1.1  manu {
     82       1.1  manu 	int error;
     83       1.1  manu 
     84       1.1  manu 	if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
     85       1.5  manu #ifdef LINUX32_GCC_SIGNATURE
     86       1.1  manu 	    ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
     87       1.1  manu #endif
     88       1.5  manu #ifdef LINUX32_ATEXIT_SIGNATURE
     89       1.1  manu 	    ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
     90       1.1  manu #endif
     91       1.5  manu #ifdef LINUX32_DEBUGLINK_SIGNATURE
     92       1.5  manu 	    ((error = ELFNAME2(linux,debuglink_signature)(l, epp, eh)) != 0) &&
     93       1.5  manu #endif
     94       1.1  manu 	    1)
     95       1.1  manu 			return error;
     96       1.1  manu 
     97       1.1  manu 	if (itp) {
     98       1.1  manu 		if ((error = emul_find_interp(l, epp->ep_esch->es_emul->e_path,
     99       1.1  manu 		    itp)))
    100       1.1  manu 			return (error);
    101       1.1  manu 	}
    102       1.1  manu #if 0
    103       1.1  manu 	DPRINTF(("linux32_probe: returning 0\n"));
    104       1.1  manu #endif
    105       1.1  manu 
    106       1.1  manu 	epp->ep_flags |= EXEC_32;
    107       1.1  manu 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
    108       1.1  manu 	epp->ep_vm_maxaddr = USRSTACK32;
    109       1.1  manu 
    110       1.1  manu 	return 0;
    111       1.1  manu }
    112       1.1  manu 
    113       1.1  manu /* round up and down to page boundaries. */
    114       1.1  manu #define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
    115       1.1  manu #define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
    116       1.1  manu 
    117       1.1  manu /*
    118       1.1  manu  * Copy arguments onto the stack in the normal way, but add some
    119       1.1  manu  * extra information in case of dynamic binding.
    120       1.1  manu  */
    121       1.1  manu int
    122       1.1  manu linux32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
    123       1.1  manu     struct ps_strings *arginfo, char **stackp, void *argp)
    124       1.1  manu {
    125       1.1  manu 	struct linux32_extra_stack_data *esdp, esd;
    126       1.1  manu 	struct elf_args *ap;
    127       1.1  manu 	struct vattr *vap;
    128       1.1  manu 	Elf_Ehdr *eh;
    129       1.1  manu 	Elf_Phdr *ph;
    130       1.1  manu 	Elf_Addr phdr = 0;
    131       1.1  manu 	u_long phsize;
    132       1.1  manu 	int error;
    133       1.1  manu 	int i;
    134       1.1  manu 
    135       1.1  manu 	if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
    136       1.1  manu 		return error;
    137       1.1  manu 
    138       1.1  manu 	/*
    139       1.1  manu 	 * Push extra arguments on the stack needed by dynamically
    140       1.1  manu 	 * linked binaries and static binaries as well.
    141       1.1  manu 	 */
    142       1.1  manu 	memset(&esd, 0, sizeof(esd));
    143       1.1  manu 	esdp = (struct linux32_extra_stack_data *)(*stackp);
    144       1.1  manu 	ap = (struct elf_args *)pack->ep_emul_arg;
    145       1.1  manu 	vap = pack->ep_vap;
    146       1.1  manu 	eh = (Elf_Ehdr *)pack->ep_hdr;
    147       1.1  manu 
    148       1.1  manu 	/*
    149       1.1  manu 	 * We forgot this, so we ned to reload it now. XXX keep track of it?
    150       1.1  manu 	 */
    151       1.1  manu 	if (ap == NULL) {
    152       1.1  manu 		phsize = eh->e_phnum * sizeof(Elf_Phdr);
    153       1.1  manu 		ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
    154       1.1  manu 		error = exec_read_from(l, pack->ep_vp, eh->e_phoff, ph, phsize);
    155       1.1  manu 		if (error != 0) {
    156       1.1  manu 			for (i = 0; i < eh->e_phnum; i++) {
    157       1.1  manu 				if (ph[i].p_type == PT_PHDR) {
    158       1.1  manu 					phdr = ph[i].p_vaddr;
    159       1.1  manu 					break;
    160       1.1  manu 				}
    161       1.1  manu 			}
    162       1.1  manu 		}
    163       1.1  manu 		free(ph, M_TEMP);
    164       1.1  manu 	}
    165       1.1  manu 
    166       1.1  manu 
    167       1.1  manu 	/*
    168       1.1  manu 	 * The exec_package doesn't have a proc pointer and it's not
    169       1.1  manu 	 * exactly trivial to add one since the credentials are
    170       1.1  manu 	 * changing. XXX Linux uses curlwp's credentials.
    171       1.4    ad 	 * Why can't we use them too? XXXad we do now; what's different
    172       1.4    ad 	 * about Linux's LWP creds?
    173       1.1  manu 	 */
    174       1.1  manu 
    175       1.1  manu 	i = 0;
    176       1.1  manu 	esd.ai[i].a_type = LINUX_AT_SYSINFO;
    177  1.5.12.1  yamt 	esd.ai[i++].a_v = NETBSD32PTR32I(&esdp->kernel_vsyscall[0]);
    178       1.1  manu 
    179       1.1  manu 	esd.ai[i].a_type = LINUX_AT_SYSINFO_EHDR;
    180  1.5.12.1  yamt 	esd.ai[i++].a_v = NETBSD32PTR32I(&esdp->elfhdr);
    181       1.1  manu 
    182       1.1  manu 	esd.ai[i].a_type = LINUX_AT_HWCAP;
    183       1.1  manu 	esd.ai[i++].a_v = LINUX32_CPUCAP;
    184       1.1  manu 
    185       1.1  manu 	esd.ai[i].a_type = AT_PAGESZ;
    186       1.1  manu 	esd.ai[i++].a_v = PAGE_SIZE;
    187       1.1  manu 
    188       1.1  manu 	esd.ai[i].a_type = LINUX_AT_CLKTCK;
    189       1.1  manu 	esd.ai[i++].a_v = hz;
    190       1.1  manu 
    191       1.1  manu 	esd.ai[i].a_type = AT_PHDR;
    192       1.1  manu 	esd.ai[i++].a_v = (ap ? ap->arg_phaddr: phdr);
    193       1.1  manu 
    194       1.1  manu 	esd.ai[i].a_type = AT_PHENT;
    195       1.1  manu 	esd.ai[i++].a_v = (ap ? ap->arg_phentsize : eh->e_phentsize);
    196       1.1  manu 
    197       1.1  manu 	esd.ai[i].a_type = AT_PHNUM;
    198       1.1  manu 	esd.ai[i++].a_v = (ap ? ap->arg_phnum : eh->e_phnum);
    199       1.1  manu 
    200       1.1  manu 	esd.ai[i].a_type = AT_BASE;
    201       1.1  manu 	esd.ai[i++].a_v = (ap ? ap->arg_interp : 0);
    202       1.1  manu 
    203       1.1  manu 	esd.ai[i].a_type = AT_FLAGS;
    204       1.1  manu 	esd.ai[i++].a_v = 0;
    205       1.1  manu 
    206       1.1  manu 	esd.ai[i].a_type = AT_ENTRY;
    207       1.1  manu 	esd.ai[i++].a_v = (ap ? ap->arg_entry : eh->e_entry);
    208       1.1  manu 
    209       1.1  manu 	esd.ai[i].a_type = LINUX_AT_EGID;
    210       1.4    ad 	esd.ai[i++].a_v = ((vap->va_mode & S_ISGID) ?
    211       1.4    ad 	    vap->va_gid : kauth_cred_getegid(l->l_cred));
    212       1.1  manu 
    213       1.1  manu 	esd.ai[i].a_type = LINUX_AT_GID;
    214       1.4    ad 	esd.ai[i++].a_v = kauth_cred_getgid(l->l_cred);
    215       1.1  manu 
    216       1.1  manu 	esd.ai[i].a_type = LINUX_AT_EUID;
    217       1.4    ad 	esd.ai[i++].a_v = ((vap->va_mode & S_ISUID) ?
    218       1.4    ad 	    vap->va_uid : kauth_cred_geteuid(l->l_cred));
    219       1.1  manu 
    220       1.1  manu 	esd.ai[i].a_type = LINUX_AT_UID;
    221       1.4    ad 	esd.ai[i++].a_v = kauth_cred_getuid(l->l_cred);
    222       1.1  manu 
    223       1.1  manu 	esd.ai[i].a_type = LINUX_AT_SECURE;
    224       1.1  manu 	esd.ai[i++].a_v = 0;
    225       1.1  manu 
    226       1.1  manu 	esd.ai[i].a_type = LINUX_AT_PLATFORM;
    227  1.5.12.1  yamt 	esd.ai[i++].a_v = NETBSD32PTR32I(&esdp->hw_platform[0]);
    228       1.1  manu 
    229       1.1  manu 	esd.ai[i].a_type = AT_NULL;
    230       1.1  manu 	esd.ai[i++].a_v = 0;
    231       1.1  manu 
    232       1.1  manu #ifdef DEBUG_LINUX
    233       1.1  manu 	if (i != LINUX32_ELF_AUX_ENTRIES) {
    234       1.1  manu 		printf("linux32_elf32_copyargs: %d Aux entries\n", i);
    235       1.1  manu 		return EINVAL;
    236       1.1  manu 	}
    237       1.1  manu #endif
    238       1.1  manu 
    239       1.1  manu 	memcpy(esd.kernel_vsyscall, linux32_kernel_vsyscall,
    240       1.1  manu 	    sizeof(linux32_kernel_vsyscall));
    241       1.1  manu 
    242       1.1  manu 	memcpy(&esd.elfhdr, eh, sizeof(*eh));
    243       1.1  manu 
    244       1.1  manu 	strcpy(esd.hw_platform, LINUX32_PLATFORM);
    245       1.1  manu 
    246       1.1  manu 	if (ap) {
    247       1.1  manu 		free((char *)ap, M_TEMP);
    248       1.1  manu 		pack->ep_emul_arg = NULL;
    249       1.1  manu 	}
    250       1.1  manu 
    251       1.1  manu 	/*
    252       1.1  manu 	 * Copy out the ELF auxiliary table and hw platform name
    253       1.1  manu 	 */
    254       1.1  manu 	if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
    255       1.1  manu 		return error;
    256       1.1  manu 	*stackp += sizeof(esd);
    257       1.1  manu 	return 0;
    258       1.1  manu }
    259       1.1  manu 
    260