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