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