linux32_exec_elf32.c revision 1.6 1 /* $NetBSD: linux32_exec_elf32.c,v 1.6 2007/03/16 22:21:40 dsl 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.6 2007/03/16 22:21:40 dsl 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 int linux32_copyinargs(struct exec_package *, struct ps_strings *,
72 void *, size_t, const void *, const void *);
73
74 int
75 ELFNAME2(linux32,probe)(l, epp, eh, itp, pos)
76 struct lwp *l;
77 struct exec_package *epp;
78 void *eh;
79 char *itp;
80 vaddr_t *pos;
81 {
82 int error;
83
84 if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
85 #ifdef LINUX32_GCC_SIGNATURE
86 ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
87 #endif
88 #ifdef LINUX32_ATEXIT_SIGNATURE
89 ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
90 #endif
91 #ifdef LINUX32_DEBUGLINK_SIGNATURE
92 ((error = ELFNAME2(linux,debuglink_signature)(l, epp, eh)) != 0) &&
93 #endif
94 1)
95 return error;
96
97 if (itp) {
98 if ((error = emul_find_interp(l, epp->ep_esch->es_emul->e_path,
99 itp)))
100 return (error);
101 }
102 #if 0
103 DPRINTF(("linux32_probe: returning 0\n"));
104 #endif
105
106 epp->ep_flags |= EXEC_32;
107 epp->ep_vm_minaddr = VM_MIN_ADDRESS;
108 epp->ep_vm_maxaddr = USRSTACK32;
109
110 return 0;
111 }
112
113 /* round up and down to page boundaries. */
114 #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
115 #define ELF_TRUNC(a, b) ((a) & ~((b) - 1))
116
117 /*
118 * Copy arguments onto the stack in the normal way, but add some
119 * extra information in case of dynamic binding.
120 */
121 int
122 linux32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
123 struct ps_strings *arginfo, char **stackp, void *argp)
124 {
125 struct linux32_extra_stack_data *esdp, esd;
126 struct elf_args *ap;
127 struct vattr *vap;
128 Elf_Ehdr *eh;
129 Elf_Phdr *ph;
130 Elf_Addr phdr = 0;
131 u_long phsize;
132 int error;
133 int i;
134
135 if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
136 return error;
137
138 /*
139 * Push extra arguments on the stack needed by dynamically
140 * linked binaries and static binaries as well.
141 */
142 memset(&esd, 0, sizeof(esd));
143 esdp = (struct linux32_extra_stack_data *)(*stackp);
144 ap = (struct elf_args *)pack->ep_emul_arg;
145 vap = pack->ep_vap;
146 eh = (Elf_Ehdr *)pack->ep_hdr;
147
148 /*
149 * We forgot this, so we ned to reload it now. XXX keep track of it?
150 */
151 if (ap == NULL) {
152 phsize = eh->e_phnum * sizeof(Elf_Phdr);
153 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
154 error = exec_read_from(l, pack->ep_vp, eh->e_phoff, ph, phsize);
155 if (error != 0) {
156 for (i = 0; i < eh->e_phnum; i++) {
157 if (ph[i].p_type == PT_PHDR) {
158 phdr = ph[i].p_vaddr;
159 break;
160 }
161 }
162 }
163 free(ph, M_TEMP);
164 }
165
166
167 /*
168 * The exec_package doesn't have a proc pointer and it's not
169 * exactly trivial to add one since the credentials are
170 * changing. XXX Linux uses curlwp's credentials.
171 * Why can't we use them too? XXXad we do now; what's different
172 * about Linux's LWP creds?
173 */
174
175 i = 0;
176 esd.ai[i].a_type = LINUX_AT_SYSINFO;
177 esd.ai[i++].a_v = NETBSD32PTR32I(&esdp->kernel_vsyscall[0]);
178
179 esd.ai[i].a_type = LINUX_AT_SYSINFO_EHDR;
180 esd.ai[i++].a_v = NETBSD32PTR32I(&esdp->elfhdr);
181
182 esd.ai[i].a_type = LINUX_AT_HWCAP;
183 esd.ai[i++].a_v = LINUX32_CPUCAP;
184
185 esd.ai[i].a_type = AT_PAGESZ;
186 esd.ai[i++].a_v = PAGE_SIZE;
187
188 esd.ai[i].a_type = LINUX_AT_CLKTCK;
189 esd.ai[i++].a_v = hz;
190
191 esd.ai[i].a_type = AT_PHDR;
192 esd.ai[i++].a_v = (ap ? ap->arg_phaddr: phdr);
193
194 esd.ai[i].a_type = AT_PHENT;
195 esd.ai[i++].a_v = (ap ? ap->arg_phentsize : eh->e_phentsize);
196
197 esd.ai[i].a_type = AT_PHNUM;
198 esd.ai[i++].a_v = (ap ? ap->arg_phnum : eh->e_phnum);
199
200 esd.ai[i].a_type = AT_BASE;
201 esd.ai[i++].a_v = (ap ? ap->arg_interp : 0);
202
203 esd.ai[i].a_type = AT_FLAGS;
204 esd.ai[i++].a_v = 0;
205
206 esd.ai[i].a_type = AT_ENTRY;
207 esd.ai[i++].a_v = (ap ? ap->arg_entry : eh->e_entry);
208
209 esd.ai[i].a_type = LINUX_AT_EGID;
210 esd.ai[i++].a_v = ((vap->va_mode & S_ISGID) ?
211 vap->va_gid : kauth_cred_getegid(l->l_cred));
212
213 esd.ai[i].a_type = LINUX_AT_GID;
214 esd.ai[i++].a_v = kauth_cred_getgid(l->l_cred);
215
216 esd.ai[i].a_type = LINUX_AT_EUID;
217 esd.ai[i++].a_v = ((vap->va_mode & S_ISUID) ?
218 vap->va_uid : kauth_cred_geteuid(l->l_cred));
219
220 esd.ai[i].a_type = LINUX_AT_UID;
221 esd.ai[i++].a_v = kauth_cred_getuid(l->l_cred);
222
223 esd.ai[i].a_type = LINUX_AT_SECURE;
224 esd.ai[i++].a_v = 0;
225
226 esd.ai[i].a_type = LINUX_AT_PLATFORM;
227 esd.ai[i++].a_v = NETBSD32PTR32I(&esdp->hw_platform[0]);
228
229 esd.ai[i].a_type = AT_NULL;
230 esd.ai[i++].a_v = 0;
231
232 #ifdef DEBUG_LINUX
233 if (i != LINUX32_ELF_AUX_ENTRIES) {
234 printf("linux32_elf32_copyargs: %d Aux entries\n", i);
235 return EINVAL;
236 }
237 #endif
238
239 memcpy(esd.kernel_vsyscall, linux32_kernel_vsyscall,
240 sizeof(linux32_kernel_vsyscall));
241
242 memcpy(&esd.elfhdr, eh, sizeof(*eh));
243
244 strcpy(esd.hw_platform, LINUX32_PLATFORM);
245
246 if (ap) {
247 free((char *)ap, M_TEMP);
248 pack->ep_emul_arg = NULL;
249 }
250
251 /*
252 * Copy out the ELF auxiliary table and hw platform name
253 */
254 if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
255 return error;
256 *stackp += sizeof(esd);
257 return 0;
258 }
259
260