xref: /netbsd-src/sys/compat/linux32/common/linux32_exec_elf32.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: linux32_exec_elf32.c,v 1.3 2006/06/13 16:23:57 skd 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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the NetBSD
22  *      Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: linux32_exec_elf32.c,v 1.3 2006/06/13 16:23:57 skd Exp $");
42 
43 #define	ELFSIZE		32
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/malloc.h>
49 #include <sys/vnode.h>
50 #include <sys/exec.h>
51 #include <sys/exec_elf.h>
52 #include <sys/kauth.h>
53 #include <sys/kernel.h>
54 #include <sys/resourcevar.h>
55 #include <sys/signal.h>
56 #include <sys/signalvar.h>
57 
58 #include <compat/linux/common/linux_exec.h>
59 #include <compat/netbsd32/netbsd32.h>
60 #include <compat/netbsd32/netbsd32_exec.h>
61 #include <compat/linux32/common/linux32_exec.h>
62 
63 #include <machine/frame.h>
64 
65 #ifdef DEBUG_LINUX
66 #define DPRINTF(a)      uprintf a
67 #else
68 #define DPRINTF(a)
69 #endif
70 
71 #if 0
72 static int ELFNAME2(linux32,signature) __P((struct lwp *, struct exec_package *,
73 	Elf_Ehdr *, char *));
74 #ifdef LINUX_GCC_SIGNATURE
75 static int ELFNAME2(linux32,gcc_signature) __P((struct lwp *l,
76 	struct exec_package *, Elf_Ehdr *));
77 #endif
78 #ifdef LINUX_ATEXIT_SIGNATURE
79 static int ELFNAME2(linux32,atexit_signature) __P((struct lwp *l,
80 	struct exec_package *, Elf_Ehdr *));
81 #endif
82 #endif
83 int linux32_copyinargs(struct exec_package *, struct ps_strings *,
84 			void *, size_t, const void *, const void *);
85 
86 int
87 ELFNAME2(linux32,probe)(l, epp, eh, itp, pos)
88 	struct lwp *l;
89 	struct exec_package *epp;
90 	void *eh;
91 	char *itp;
92 	vaddr_t *pos;
93 {
94 	int error;
95 
96 	if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
97 #ifdef LINUX_GCC_SIGNATURE
98 	    ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
99 #endif
100 #ifdef LINUX_ATEXIT_SIGNATURE
101 	    ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
102 #endif
103 	    1)
104 			return error;
105 
106 	if (itp) {
107 		if ((error = emul_find_interp(l, epp->ep_esch->es_emul->e_path,
108 		    itp)))
109 			return (error);
110 	}
111 #if 0
112 	DPRINTF(("linux32_probe: returning 0\n"));
113 #endif
114 
115 	epp->ep_flags |= EXEC_32;
116 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
117 	epp->ep_vm_maxaddr = USRSTACK32;
118 
119 	return 0;
120 }
121 
122 /* round up and down to page boundaries. */
123 #define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
124 #define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
125 
126 /*
127  * Copy arguments onto the stack in the normal way, but add some
128  * extra information in case of dynamic binding.
129  */
130 int
131 linux32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
132     struct ps_strings *arginfo, char **stackp, void *argp)
133 {
134 	struct linux32_extra_stack_data *esdp, esd;
135 	struct elf_args *ap;
136 	struct vattr *vap;
137 	struct proc *p;
138 	Elf_Ehdr *eh;
139 	Elf_Phdr *ph;
140 	Elf_Addr phdr = 0;
141 	u_long phsize;
142 	int error;
143 	int i;
144 
145 	p = l->l_proc;
146 
147 	if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
148 		return error;
149 
150 	/*
151 	 * Push extra arguments on the stack needed by dynamically
152 	 * linked binaries and static binaries as well.
153 	 */
154 	memset(&esd, 0, sizeof(esd));
155 	esdp = (struct linux32_extra_stack_data *)(*stackp);
156 	ap = (struct elf_args *)pack->ep_emul_arg;
157 	vap = pack->ep_vap;
158 	eh = (Elf_Ehdr *)pack->ep_hdr;
159 
160 	/*
161 	 * We forgot this, so we ned to reload it now. XXX keep track of it?
162 	 */
163 	if (ap == NULL) {
164 		phsize = eh->e_phnum * sizeof(Elf_Phdr);
165 		ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
166 		error = exec_read_from(l, pack->ep_vp, eh->e_phoff, ph, phsize);
167 		if (error != 0) {
168 			for (i = 0; i < eh->e_phnum; i++) {
169 				if (ph[i].p_type == PT_PHDR) {
170 					phdr = ph[i].p_vaddr;
171 					break;
172 				}
173 			}
174 		}
175 		free(ph, M_TEMP);
176 	}
177 
178 
179 	/*
180 	 * The exec_package doesn't have a proc pointer and it's not
181 	 * exactly trivial to add one since the credentials are
182 	 * changing. XXX Linux uses curlwp's credentials.
183 	 * Why can't we use them too?
184 	 */
185 
186 	i = 0;
187 	esd.ai[i].a_type = LINUX_AT_SYSINFO;
188 	esd.ai[i++].a_v = (netbsd32_pointer_t)(long)&esdp->kernel_vsyscall[0];
189 
190 	esd.ai[i].a_type = LINUX_AT_SYSINFO_EHDR;
191 	esd.ai[i++].a_v = (netbsd32_pointer_t)(long)&esdp->elfhdr;
192 
193 	esd.ai[i].a_type = LINUX_AT_HWCAP;
194 	esd.ai[i++].a_v = LINUX32_CPUCAP;
195 
196 	esd.ai[i].a_type = AT_PAGESZ;
197 	esd.ai[i++].a_v = PAGE_SIZE;
198 
199 	esd.ai[i].a_type = LINUX_AT_CLKTCK;
200 	esd.ai[i++].a_v = hz;
201 
202 	esd.ai[i].a_type = AT_PHDR;
203 	esd.ai[i++].a_v = (ap ? ap->arg_phaddr: phdr);
204 
205 	esd.ai[i].a_type = AT_PHENT;
206 	esd.ai[i++].a_v = (ap ? ap->arg_phentsize : eh->e_phentsize);
207 
208 	esd.ai[i].a_type = AT_PHNUM;
209 	esd.ai[i++].a_v = (ap ? ap->arg_phnum : eh->e_phnum);
210 
211 	esd.ai[i].a_type = AT_BASE;
212 	esd.ai[i++].a_v = (ap ? ap->arg_interp : 0);
213 
214 	esd.ai[i].a_type = AT_FLAGS;
215 	esd.ai[i++].a_v = 0;
216 
217 	esd.ai[i].a_type = AT_ENTRY;
218 	esd.ai[i++].a_v = (ap ? ap->arg_entry : eh->e_entry);
219 
220 	esd.ai[i].a_type = LINUX_AT_EGID;
221 	esd.ai[i++].a_v =
222 	    ((vap->va_mode & S_ISGID) ? vap->va_gid : kauth_cred_getegid(p->p_cred));
223 
224 	esd.ai[i].a_type = LINUX_AT_GID;
225 	esd.ai[i++].a_v = kauth_cred_getgid(p->p_cred);
226 
227 	esd.ai[i].a_type = LINUX_AT_EUID;
228 	esd.ai[i++].a_v =
229 	    ((vap->va_mode & S_ISUID) ? vap->va_uid : kauth_cred_geteuid(p->p_cred));
230 
231 	esd.ai[i].a_type = LINUX_AT_UID;
232 	esd.ai[i++].a_v = kauth_cred_getuid(p->p_cred);
233 
234 	esd.ai[i].a_type = LINUX_AT_SECURE;
235 	esd.ai[i++].a_v = 0;
236 
237 	esd.ai[i].a_type = LINUX_AT_PLATFORM;
238 	esd.ai[i++].a_v = (netbsd32_pointer_t)(long)&esdp->hw_platform[0];
239 
240 	esd.ai[i].a_type = AT_NULL;
241 	esd.ai[i++].a_v = 0;
242 
243 #ifdef DEBUG_LINUX
244 	if (i != LINUX32_ELF_AUX_ENTRIES) {
245 		printf("linux32_elf32_copyargs: %d Aux entries\n", i);
246 		return EINVAL;
247 	}
248 #endif
249 
250 	memcpy(esd.kernel_vsyscall, linux32_kernel_vsyscall,
251 	    sizeof(linux32_kernel_vsyscall));
252 
253 	memcpy(&esd.elfhdr, eh, sizeof(*eh));
254 
255 	strcpy(esd.hw_platform, LINUX32_PLATFORM);
256 
257 	if (ap) {
258 		free((char *)ap, M_TEMP);
259 		pack->ep_emul_arg = NULL;
260 	}
261 
262 	/*
263 	 * Copy out the ELF auxiliary table and hw platform name
264 	 */
265 	if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
266 		return error;
267 	*stackp += sizeof(esd);
268 	return 0;
269 }
270 
271