Home | History | Annotate | Line # | Download | only in common
linux_uselib.c revision 1.10
      1 /*	$NetBSD: linux_uselib.c,v 1.10 2003/04/01 15:05:12 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998 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 and Eric Haszlakiewicz.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: linux_uselib.c,v 1.10 2003/04/01 15:05:12 thorpej Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/proc.h>
     46 #include <sys/malloc.h>
     47 #include <sys/namei.h>
     48 #include <sys/vnode.h>
     49 #include <sys/mount.h>
     50 #include <sys/exec.h>
     51 #include <sys/exec_elf.h>
     52 
     53 #include <sys/mman.h>
     54 #include <sys/sa.h>
     55 #include <sys/syscallargs.h>
     56 
     57 #include <machine/cpu.h>
     58 #include <machine/reg.h>
     59 
     60 #include <compat/linux/common/linux_types.h>
     61 #include <compat/linux/common/linux_signal.h>
     62 #include <compat/linux/common/linux_util.h>
     63 #include <compat/linux/common/linux_exec.h>
     64 #include <compat/linux/common/linux_machdep.h>
     65 
     66 #ifndef EXEC_AOUT
     67 /* define EXEC_AOUT to get prototype from linux_syscall.h */
     68 #define EXEC_AOUT
     69 #endif
     70 
     71 #include <compat/linux/linux_syscallargs.h>
     72 #include <compat/linux/linux_syscall.h>
     73 
     74 /*
     75  * The Linux system call to load shared libraries, a.out version. The
     76  * a.out shared libs are just files that are mapped onto a fixed
     77  * address in the process' address space. The address is given in
     78  * a_entry. Read in the header, set up some VM commands and run them.
     79  *
     80  * Yes, both text and data are mapped at once, so we're left with
     81  * writable text for the shared libs. The Linux crt0 seemed to break
     82  * sometimes when data was mapped separately. It munmapped a uselib()
     83  * of ld.so by hand, which failed with shared text and data for ld.so
     84  * Yuck.
     85  *
     86  * Because of the problem with ZMAGIC executables (text starts
     87  * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
     88  * shared libs are not handled very efficiently :-(
     89  */
     90 
     91 int
     92 linux_sys_uselib(l, v, retval)
     93 	struct lwp *l;
     94 	void *v;
     95 	register_t *retval;
     96 {
     97 	struct linux_sys_uselib_args /* {
     98 		syscallarg(const char *) path;
     99 	} */ *uap = v;
    100 	struct proc *p = l->l_proc;
    101 	caddr_t sg;
    102 	long bsize, dsize, tsize, taddr, baddr, daddr;
    103 	struct nameidata ni;
    104 	struct vnode *vp;
    105 	struct exec hdr;
    106 	struct exec_vmcmd_set vcset;
    107 	int i, magic, error;
    108 	size_t rem;
    109 
    110 	sg = stackgap_init(p, 0);
    111 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    112 
    113 	NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    114 
    115 	if ((error = namei(&ni)))
    116 		return error;
    117 
    118 	vp = ni.ni_vp;
    119 
    120 	if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
    121 			     0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
    122 			     &rem, p))) {
    123 		vrele(vp);
    124 		return error;
    125 	}
    126 
    127 	if (rem != 0) {
    128 		vrele(vp);
    129 		return ENOEXEC;
    130 	}
    131 
    132 	if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
    133 		return ENOEXEC;
    134 
    135 	magic = LINUX_N_MAGIC(&hdr);
    136 	taddr = hdr.a_entry & (~(PAGE_SIZE - 1));
    137 	tsize = hdr.a_text;
    138 	daddr = taddr + tsize;
    139 	dsize = hdr.a_data + hdr.a_bss;
    140 
    141 	error = vn_marktext(vp);
    142 	if (error)
    143 		return (error);
    144 
    145 	vcset.evs_cnt = 0;
    146 	vcset.evs_used = 0;
    147 
    148 	NEW_VMCMD(&vcset,
    149 		  magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
    150 		  hdr.a_text + hdr.a_data, taddr,
    151 		  vp, LINUX_N_TXTOFF(hdr, magic),
    152 		  VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
    153 
    154 	baddr = roundup(daddr + hdr.a_data, PAGE_SIZE);
    155 	bsize = daddr + dsize - baddr;
    156         if (bsize > 0) {
    157                 NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
    158                     NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    159 	}
    160 
    161 	for (i = 0; i < vcset.evs_used && !error; i++) {
    162 		struct exec_vmcmd *vcp;
    163 
    164 		vcp = &vcset.evs_cmds[i];
    165 		error = (*vcp->ev_proc)(p, vcp);
    166 	}
    167 
    168 	kill_vmcmds(&vcset);
    169 
    170 	vrele(vp);
    171 
    172 	return error;
    173 }
    174