xref: /netbsd-src/sys/compat/linux/common/linux_uselib.c (revision b5cd2489b2952ee409ede7b61183dba7ab0359ad)
1 /*	$NetBSD: linux_uselib.c,v 1.4 2001/10/30 15:32:02 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/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/malloc.h>
44 #include <sys/namei.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/exec.h>
48 #include <sys/exec_elf.h>
49 
50 #include <sys/mman.h>
51 #include <sys/syscallargs.h>
52 
53 #include <machine/cpu.h>
54 #include <machine/reg.h>
55 
56 #include <compat/linux/common/linux_types.h>
57 #include <compat/linux/common/linux_signal.h>
58 #include <compat/linux/common/linux_util.h>
59 #include <compat/linux/common/linux_exec.h>
60 #include <compat/linux/common/linux_machdep.h>
61 
62 #ifndef EXEC_AOUT
63 /* define EXEC_AOUT to get prototype from linux_syscall.h */
64 #define EXEC_AOUT
65 #endif
66 
67 #include <compat/linux/linux_syscallargs.h>
68 #include <compat/linux/linux_syscall.h>
69 
70 /*
71  * The Linux system call to load shared libraries, a.out version. The
72  * a.out shared libs are just files that are mapped onto a fixed
73  * address in the process' address space. The address is given in
74  * a_entry. Read in the header, set up some VM commands and run them.
75  *
76  * Yes, both text and data are mapped at once, so we're left with
77  * writeable text for the shared libs. The Linux crt0 seemed to break
78  * sometimes when data was mapped separately. It munmapped a uselib()
79  * of ld.so by hand, which failed with shared text and data for ld.so
80  * Yuck.
81  *
82  * Because of the problem with ZMAGIC executables (text starts
83  * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
84  * shared libs are not handled very efficiently :-(
85  */
86 
87 int
88 linux_sys_uselib(p, v, retval)
89 	struct proc *p;
90 	void *v;
91 	register_t *retval;
92 {
93 	struct linux_sys_uselib_args /* {
94 		syscallarg(const char *) path;
95 	} */ *uap = v;
96 	caddr_t sg;
97 	long bsize, dsize, tsize, taddr, baddr, daddr;
98 	struct nameidata ni;
99 	struct vnode *vp;
100 	struct exec hdr;
101 	struct exec_vmcmd_set vcset;
102 	int i, magic, error;
103 	size_t rem;
104 
105 	sg = stackgap_init(p->p_emul);
106 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
107 
108 	NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
109 
110 	if ((error = namei(&ni)))
111 		return error;
112 
113 	vp = ni.ni_vp;
114 
115 	if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
116 			     0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
117 			     &rem, p))) {
118 		vrele(vp);
119 		return error;
120 	}
121 
122 	if (rem != 0) {
123 		vrele(vp);
124 		return ENOEXEC;
125 	}
126 
127 	if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
128 		return ENOEXEC;
129 
130 	magic = LINUX_N_MAGIC(&hdr);
131 	taddr = hdr.a_entry & (~(NBPG - 1));
132 	tsize = hdr.a_text;
133 	daddr = taddr + tsize;
134 	dsize = hdr.a_data + hdr.a_bss;
135 
136 	if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
137 		vrele(vp);
138                 return ETXTBSY;
139         }
140 	vp->v_flag |= VTEXT;
141 
142 	vcset.evs_cnt = 0;
143 	vcset.evs_used = 0;
144 
145 	NEW_VMCMD(&vcset,
146 		  magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
147 		  hdr.a_text + hdr.a_data, taddr,
148 		  vp, LINUX_N_TXTOFF(hdr, magic),
149 		  VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
150 
151 	baddr = roundup(daddr + hdr.a_data, NBPG);
152 	bsize = daddr + dsize - baddr;
153         if (bsize > 0) {
154                 NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
155                     NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
156 	}
157 
158 	for (i = 0; i < vcset.evs_used && !error; i++) {
159 		struct exec_vmcmd *vcp;
160 
161 		vcp = &vcset.evs_cmds[i];
162 		error = (*vcp->ev_proc)(p, vcp);
163 	}
164 
165 	kill_vmcmds(&vcset);
166 
167 	vrele(vp);
168 
169 	return error;
170 }
171