xref: /netbsd-src/sys/compat/linux32/common/linux32_exec_elf32.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: linux32_exec_elf32.c,v 1.13 2012/02/03 20:11:54 matt Exp $ */
2 
3 /*-
4  * Copyright (c) 1995, 1998, 2000, 2001,2006 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, Eric Haszlakiewicz and
9  * Emmanuel Dreyfus.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: linux32_exec_elf32.c,v 1.13 2012/02/03 20:11:54 matt Exp $");
35 
36 #define	ELFSIZE		32
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/vnode.h>
42 #include <sys/exec.h>
43 #include <sys/exec_elf.h>
44 #include <sys/kauth.h>
45 #include <sys/kernel.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signal.h>
48 #include <sys/signalvar.h>
49 
50 #include <compat/linux/common/linux_exec.h>
51 #include <compat/netbsd32/netbsd32.h>
52 #include <compat/netbsd32/netbsd32_exec.h>
53 #include <compat/linux32/common/linux32_exec.h>
54 
55 #include <machine/cpuvar.h>
56 #include <machine/frame.h>
57 
58 #ifdef DEBUG_LINUX
59 #define DPRINTF(a)      uprintf a
60 #else
61 #define DPRINTF(a)
62 #endif
63 
64 int linux32_copyinargs(struct exec_package *, struct ps_strings *,
65 			void *, size_t, const void *, const void *);
66 
67 int
68 ELFNAME2(linux32,probe)(struct lwp *l, struct exec_package *epp,
69 			void *eh, char *itp, vaddr_t *pos)
70 {
71 	int error;
72 
73 	if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
74 #ifdef LINUX32_GCC_SIGNATURE
75 	    ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
76 #endif
77 #ifdef LINUX32_ATEXIT_SIGNATURE
78 	    ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
79 #endif
80 #ifdef LINUX32_DEBUGLINK_SIGNATURE
81 	    ((error = ELFNAME2(linux,debuglink_signature)(l, epp, eh)) != 0) &&
82 #endif
83 	    1)
84 			return error;
85 
86 	if (itp) {
87 		if ((error = emul_find_interp(l, epp, itp)))
88 			return (error);
89 	}
90 #if 0
91 	DPRINTF(("linux32_probe: returning 0\n"));
92 #endif
93 
94 	epp->ep_flags |= EXEC_32 | EXEC_FORCEAUX;
95 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
96 	epp->ep_vm_maxaddr = USRSTACK32;
97 
98 	return 0;
99 }
100 
101 /*
102  * Copy arguments onto the stack in the normal way, but add some
103  * extra information in case of dynamic binding.
104  */
105 int
106 linux32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
107     struct ps_strings *arginfo, char **stackp, void *argp)
108 {
109 	Aux32Info ai[LINUX32_ELF_AUX_ENTRIES], *a;
110 	struct elf_args *ap;
111 	struct vattr *vap;
112 	size_t len;
113 	int error;
114 
115 	if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
116 		return error;
117 
118 	a = ai;
119 
120 	/*
121 	 * Push extra arguments on the stack needed by dynamically
122 	 * linked binaries and static binaries as well.
123 	 */
124 
125 	a->a_type = AT_PAGESZ;
126 	a->a_v = PAGE_SIZE;
127 	a++;
128 
129 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
130 
131 		a->a_type = AT_PHDR;
132 		a->a_v = ap->arg_phaddr;
133 		a++;
134 
135 		a->a_type = AT_PHENT;
136 		a->a_v = ap->arg_phentsize;
137 		a++;
138 
139 		a->a_type = AT_PHNUM;
140 		a->a_v = ap->arg_phnum;
141 		a++;
142 
143 		a->a_type = AT_BASE;
144 		a->a_v = ap->arg_interp;
145 		a++;
146 
147 		a->a_type = AT_FLAGS;
148 		a->a_v = 0;
149 		a++;
150 
151 		a->a_type = AT_ENTRY;
152 		a->a_v = ap->arg_entry;
153 		a++;
154 
155 		exec_free_emul_arg(pack);
156 	}
157 
158 	/* Linux-specific items */
159 	a->a_type = LINUX_AT_CLKTCK;
160 	a->a_v = hz;
161 	a++;
162 
163 	vap = pack->ep_vap;
164 
165 	a->a_type = LINUX_AT_UID;
166 	a->a_v = kauth_cred_getuid(l->l_cred);
167 	a++;
168 
169 	a->a_type = LINUX_AT_EUID;
170 	a->a_v = ((vap->va_mode & S_ISUID) ?
171 	    vap->va_uid : kauth_cred_geteuid(l->l_cred));
172 	a++;
173 
174 	a->a_type = LINUX_AT_GID;
175 	a->a_v = kauth_cred_getgid(l->l_cred);
176 	a++;
177 
178 	a->a_type = LINUX_AT_EGID;
179 	a->a_v = ((vap->va_mode & S_ISGID) ?
180 	    vap->va_gid : kauth_cred_getegid(l->l_cred));
181 	a++;
182 
183 	a->a_type = LINUX_AT_SECURE;
184 	a->a_v = 0;
185 	a++;
186 
187 #if 0
188 	a->a_type = LINUX_AT_SYSINFO;
189 	a->a_v = NETBSD32PTR32I(&esdp->kernel_vsyscall[0]);
190 	a++;
191 
192 	a->a_type = LINUX_AT_SYSINFO_EHDR;
193 	a->a_v = NETBSD32PTR32I(&esdp->elfhdr);
194 	a++;
195 
196 	a->a_type = LINUX_AT_HWCAP;
197 	a->a_v = LINUX32_CPUCAP;
198 	a++;
199 
200 	a->a_type = LINUX_AT_PLATFORM;
201 	a->a_v = NETBSD32PTR32I(&esdp->hw_platform[0]);
202 	a++;
203 #endif
204 
205 	a->a_type = AT_NULL;
206 	a->a_v = 0;
207 	a++;
208 
209 #if 0
210 	memcpy(esd.kernel_vsyscall, linux32_kernel_vsyscall,
211 	    sizeof(linux32_kernel_vsyscall));
212 
213 	memcpy(&esd.elfhdr, eh, sizeof(*eh));
214 
215 	strcpy(esd.hw_platform, LINUX32_PLATFORM);
216 
217 	/*
218 	 * Copy out the ELF auxiliary table and hw platform name
219 	 */
220 	if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
221 		return error;
222 	*stackp += sizeof(esd);
223 #endif
224 
225 	len = (a - ai) * sizeof(AuxInfo);
226 	if ((error = copyout(ai, *stackp, len)) != 0)
227 		return error;
228 	*stackp += len;
229 
230 	return 0;
231 }
232