linux_exec_elf32.c revision 1.7 1 1.7 fvdl /* $NetBSD: linux_exec_elf32.c,v 1.7 1995/06/11 15:15:09 fvdl Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Copyright (c) 1995 Frank van der Linden
5 1.7 fvdl * Copyright (c) 1994 Christos Zoulas
6 1.1 fvdl * All rights reserved.
7 1.1 fvdl *
8 1.1 fvdl * Redistribution and use in source and binary forms, with or without
9 1.1 fvdl * modification, are permitted provided that the following conditions
10 1.1 fvdl * are met:
11 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
12 1.1 fvdl * notice, this list of conditions and the following disclaimer.
13 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
15 1.1 fvdl * documentation and/or other materials provided with the distribution.
16 1.7 fvdl * 3. The name of the author may not be used to endorse or promote products
17 1.1 fvdl * derived from this software without specific prior written permission
18 1.1 fvdl *
19 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 fvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 fvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 fvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 fvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 fvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 fvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 fvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 fvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 fvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 fvdl *
30 1.7 fvdl * based on exec_aout.c, sunos_exec.c and svr4_exec.c
31 1.1 fvdl */
32 1.1 fvdl
33 1.1 fvdl #include <sys/param.h>
34 1.1 fvdl #include <sys/systm.h>
35 1.1 fvdl #include <sys/filedesc.h>
36 1.1 fvdl #include <sys/kernel.h>
37 1.1 fvdl #include <sys/proc.h>
38 1.1 fvdl #include <sys/mount.h>
39 1.1 fvdl #include <sys/malloc.h>
40 1.1 fvdl #include <sys/namei.h>
41 1.1 fvdl #include <sys/vnode.h>
42 1.1 fvdl #include <sys/file.h>
43 1.1 fvdl #include <sys/resourcevar.h>
44 1.1 fvdl #include <sys/wait.h>
45 1.1 fvdl
46 1.1 fvdl #include <sys/mman.h>
47 1.1 fvdl #include <vm/vm.h>
48 1.1 fvdl #include <vm/vm_param.h>
49 1.1 fvdl #include <vm/vm_map.h>
50 1.1 fvdl #include <vm/vm_kern.h>
51 1.1 fvdl #include <vm/vm_pager.h>
52 1.1 fvdl
53 1.1 fvdl #include <machine/cpu.h>
54 1.1 fvdl #include <machine/reg.h>
55 1.1 fvdl #include <machine/exec.h>
56 1.4 christos #include <machine/linux_machdep.h>
57 1.1 fvdl
58 1.1 fvdl #include <compat/linux/linux_types.h>
59 1.4 christos #include <compat/linux/linux_syscall.h>
60 1.1 fvdl #include <compat/linux/linux_syscallargs.h>
61 1.1 fvdl #include <compat/linux/linux_util.h>
62 1.1 fvdl #include <compat/linux/linux_exec.h>
63 1.1 fvdl
64 1.6 fvdl struct elf_args {
65 1.6 fvdl u_long arg_entry; /* progran entry point */
66 1.6 fvdl u_long arg_interp; /* Interpreter load address */
67 1.6 fvdl u_long arg_phaddr; /* program header address */
68 1.6 fvdl u_long arg_phentsize; /* Size of program header */
69 1.6 fvdl u_long arg_phnum; /* Number of program headers */
70 1.6 fvdl };
71 1.6 fvdl
72 1.6 fvdl static void *linux_aout_copyargs __P((struct exec_package *,
73 1.6 fvdl struct ps_strings *, void *, void *));
74 1.6 fvdl static void *linux_elf_copyargs __P((struct exec_package *, struct ps_strings *,
75 1.6 fvdl void *, void *));
76 1.6 fvdl static int linux_elf_check_header __P((Elf32_Ehdr *, int));
77 1.6 fvdl static void linux_elf_load_psection __P((struct exec_vmcmd_set *,
78 1.6 fvdl struct vnode *, Elf32_Phdr *, u_long *, u_long *, int *));
79 1.6 fvdl static int linux_elf_set_segment __P((struct exec_package *, u_long, u_long,
80 1.6 fvdl int));
81 1.6 fvdl static int linux_elf_read_from __P((struct vnode *, u_long, struct proc *,
82 1.6 fvdl caddr_t, int));
83 1.6 fvdl static int linux_elf_load_file __P((struct proc *, char *,
84 1.6 fvdl struct exec_vmcmd_set *, u_long *, struct elf_args *, u_long *));
85 1.6 fvdl
86 1.6 fvdl #ifdef DEBUG_EXEC_LINUX_ELF
87 1.6 fvdl #define DPRINTF(x) printf x
88 1.6 fvdl #else
89 1.6 fvdl #define DPRINTF(x)
90 1.6 fvdl #endif
91 1.4 christos
92 1.6 fvdl #define LINUX_ELF_ALIGN(a, b) ((a) & ~((b) - 1))
93 1.6 fvdl #define LINUX_ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
94 1.6 fvdl #define LINUX_AOUT_AUX_ARGSIZ 2
95 1.4 christos
96 1.4 christos extern int linux_error[];
97 1.4 christos extern struct sysent linux_sysent[];
98 1.4 christos extern char *linux_syscallnames[];
99 1.4 christos
100 1.6 fvdl struct emul emul_linux_aout = {
101 1.4 christos "linux",
102 1.4 christos linux_error,
103 1.4 christos linux_sendsig,
104 1.4 christos LINUX_SYS_syscall,
105 1.4 christos LINUX_SYS_MAXSYSCALL,
106 1.4 christos linux_sysent,
107 1.4 christos linux_syscallnames,
108 1.6 fvdl LINUX_AOUT_AUX_ARGSIZ,
109 1.6 fvdl linux_aout_copyargs,
110 1.6 fvdl setregs,
111 1.6 fvdl linux_sigcode,
112 1.6 fvdl linux_esigcode,
113 1.6 fvdl };
114 1.6 fvdl
115 1.6 fvdl struct emul emul_linux_elf = {
116 1.6 fvdl "linux",
117 1.6 fvdl linux_error,
118 1.6 fvdl linux_sendsig,
119 1.6 fvdl LINUX_SYS_syscall,
120 1.6 fvdl LINUX_SYS_MAXSYSCALL,
121 1.6 fvdl linux_sysent,
122 1.6 fvdl linux_syscallnames,
123 1.6 fvdl LINUX_ELF_AUX_ARGSIZ,
124 1.6 fvdl linux_elf_copyargs,
125 1.4 christos setregs,
126 1.4 christos linux_sigcode,
127 1.4 christos linux_esigcode,
128 1.4 christos };
129 1.4 christos
130 1.4 christos
131 1.4 christos static void *
132 1.6 fvdl linux_aout_copyargs(pack, arginfo, stack, argp)
133 1.4 christos struct exec_package *pack;
134 1.4 christos struct ps_strings *arginfo;
135 1.4 christos void *stack;
136 1.4 christos void *argp;
137 1.4 christos {
138 1.4 christos char **cpp = stack;
139 1.4 christos char **stk = stack;
140 1.4 christos char *dp, *sp;
141 1.4 christos size_t len;
142 1.4 christos void *nullp = NULL;
143 1.4 christos int argc = arginfo->ps_nargvstr;
144 1.4 christos int envc = arginfo->ps_nenvstr;
145 1.4 christos
146 1.4 christos if (copyout(&argc, cpp++, sizeof(argc)))
147 1.4 christos return NULL;
148 1.4 christos
149 1.4 christos /* leave room for envp and argv */
150 1.4 christos cpp += 2;
151 1.4 christos if (copyout(&cpp, &stk[1], sizeof (cpp)))
152 1.4 christos return NULL;
153 1.4 christos
154 1.4 christos dp = (char *) (cpp + argc + envc + 2);
155 1.4 christos sp = argp;
156 1.4 christos
157 1.4 christos /* XXX don't copy them out, remap them! */
158 1.5 mycroft arginfo->ps_argvstr = cpp; /* remember location of argv for later */
159 1.4 christos
160 1.4 christos for (; --argc >= 0; sp += len, dp += len)
161 1.4 christos if (copyout(&dp, cpp++, sizeof(dp)) ||
162 1.4 christos copyoutstr(sp, dp, ARG_MAX, &len))
163 1.4 christos return NULL;
164 1.4 christos
165 1.4 christos if (copyout(&nullp, cpp++, sizeof(nullp)))
166 1.4 christos return NULL;
167 1.4 christos
168 1.4 christos if (copyout(&cpp, &stk[2], sizeof (cpp)))
169 1.4 christos return NULL;
170 1.4 christos
171 1.5 mycroft arginfo->ps_envstr = cpp; /* remember location of envp for later */
172 1.4 christos
173 1.4 christos for (; --envc >= 0; sp += len, dp += len)
174 1.4 christos if (copyout(&dp, cpp++, sizeof(dp)) ||
175 1.4 christos copyoutstr(sp, dp, ARG_MAX, &len))
176 1.4 christos return NULL;
177 1.4 christos
178 1.4 christos if (copyout(&nullp, cpp++, sizeof(nullp)))
179 1.4 christos return NULL;
180 1.4 christos
181 1.4 christos return cpp;
182 1.4 christos }
183 1.4 christos
184 1.6 fvdl static void *
185 1.6 fvdl linux_elf_copyargs(pack, arginfo, stack, argp)
186 1.6 fvdl struct exec_package *pack;
187 1.6 fvdl struct ps_strings *arginfo;
188 1.6 fvdl void *stack;
189 1.6 fvdl void *argp;
190 1.6 fvdl {
191 1.6 fvdl char **cpp = stack;
192 1.6 fvdl char *dp, *sp;
193 1.6 fvdl size_t len;
194 1.6 fvdl void *nullp = NULL;
195 1.6 fvdl int argc = arginfo->ps_nargvstr;
196 1.6 fvdl int envc = arginfo->ps_nenvstr;
197 1.6 fvdl AuxInfo *a;
198 1.6 fvdl struct elf_args *ap;
199 1.6 fvdl
200 1.6 fvdl if (copyout(&argc, cpp++, sizeof(argc)))
201 1.6 fvdl return NULL;
202 1.6 fvdl
203 1.6 fvdl dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
204 1.6 fvdl sp = argp;
205 1.6 fvdl
206 1.6 fvdl /* XXX don't copy them out, remap them! */
207 1.6 fvdl arginfo->ps_argvstr = cpp; /* remember location of argv for later */
208 1.6 fvdl
209 1.6 fvdl for (; --argc >= 0; sp += len, dp += len)
210 1.6 fvdl if (copyout(&dp, cpp++, sizeof(dp)) ||
211 1.6 fvdl copyoutstr(sp, dp, ARG_MAX, &len))
212 1.6 fvdl return NULL;
213 1.6 fvdl
214 1.6 fvdl if (copyout(&nullp, cpp++, sizeof(nullp)))
215 1.6 fvdl return NULL;
216 1.6 fvdl
217 1.6 fvdl arginfo->ps_envstr = cpp; /* remember location of envp for later */
218 1.6 fvdl
219 1.6 fvdl for (; --envc >= 0; sp += len, dp += len)
220 1.6 fvdl if (copyout(&dp, cpp++, sizeof(dp)) ||
221 1.6 fvdl copyoutstr(sp, dp, ARG_MAX, &len))
222 1.6 fvdl return NULL;
223 1.6 fvdl
224 1.6 fvdl if (copyout(&nullp, cpp++, sizeof(nullp)))
225 1.6 fvdl return NULL;
226 1.6 fvdl
227 1.6 fvdl /*
228 1.6 fvdl * Push extra arguments on the stack needed by dynamically
229 1.6 fvdl * linked binaries
230 1.6 fvdl */
231 1.6 fvdl a = (AuxInfo *) cpp;
232 1.6 fvdl if ((ap = (struct elf_args *) pack->ep_emul_arg)) {
233 1.6 fvdl
234 1.6 fvdl DPRINTF(("phaddr=0x%x, phsize=%d, phnum=%d, interp=0x%x, ",
235 1.6 fvdl ap->arg_phaddr, ap->arg_phentsize, ap->arg_phnum,
236 1.6 fvdl ap->arg_interp));
237 1.6 fvdl DPRINTF((" entry=0x%x\n", ap->arg_entry));
238 1.6 fvdl
239 1.6 fvdl a->au_id = AUX_phdr;
240 1.6 fvdl a->au_v = ap->arg_phaddr;
241 1.6 fvdl a++;
242 1.6 fvdl
243 1.6 fvdl a->au_id = AUX_phent;
244 1.6 fvdl a->au_v = ap->arg_phentsize;
245 1.6 fvdl a++;
246 1.6 fvdl
247 1.6 fvdl a->au_id = AUX_phnum;
248 1.6 fvdl a->au_v = ap->arg_phnum;
249 1.6 fvdl a++;
250 1.6 fvdl
251 1.6 fvdl a->au_id = AUX_pagesz;
252 1.6 fvdl a->au_v = NBPG;
253 1.6 fvdl a++;
254 1.6 fvdl
255 1.6 fvdl a->au_id = AUX_base;
256 1.6 fvdl a->au_v = ap->arg_interp;
257 1.6 fvdl a++;
258 1.6 fvdl
259 1.6 fvdl a->au_id = AUX_flags;
260 1.6 fvdl a->au_v = 0;
261 1.6 fvdl a++;
262 1.6 fvdl
263 1.6 fvdl a->au_id = AUX_entry;
264 1.6 fvdl a->au_v = ap->arg_entry;
265 1.6 fvdl a++;
266 1.6 fvdl
267 1.6 fvdl a->au_id = AUX_null;
268 1.6 fvdl a->au_v = 0;
269 1.6 fvdl a++;
270 1.6 fvdl
271 1.6 fvdl free((char *) ap, M_TEMP);
272 1.6 fvdl }
273 1.6 fvdl return a;
274 1.6 fvdl }
275 1.6 fvdl
276 1.6 fvdl #ifdef DEBUG_EXEC_LINUX_ELF
277 1.6 fvdl static void
278 1.6 fvdl print_Ehdr(e)
279 1.6 fvdl Elf32_Ehdr *e;
280 1.6 fvdl {
281 1.6 fvdl printf("e_ident %s, ", e->e_ident);
282 1.6 fvdl printf("e_type %d, ", e->e_type);
283 1.6 fvdl printf("e_machine %d, ", e->e_machine);
284 1.6 fvdl printf("e_version %ld, ", e->e_version);
285 1.6 fvdl printf("e_entry %lx, ", e->e_entry);
286 1.6 fvdl printf("e_phoff %lx, ", e->e_phoff);
287 1.6 fvdl printf("e_shoff %lx, ", e->e_shoff);
288 1.6 fvdl printf("e_flags %lx, ", e->e_flags);
289 1.6 fvdl printf("e_ehsize %d, ", e->e_ehsize);
290 1.6 fvdl printf("e_phentsize %d, ", e->e_phentsize);
291 1.6 fvdl printf("e_phnum %d, ", e->e_phnum);
292 1.6 fvdl printf("e_shentsize %d, ", e->e_shentsize);
293 1.6 fvdl printf("e_shnum %d, ", e->e_shnum);
294 1.6 fvdl printf("e_shstrndx %d\n", e->e_shstrndx);
295 1.6 fvdl }
296 1.6 fvdl
297 1.6 fvdl
298 1.6 fvdl static void
299 1.6 fvdl print_Phdr(p)
300 1.6 fvdl Elf32_Phdr *p;
301 1.6 fvdl {
302 1.6 fvdl static char *types[] =
303 1.6 fvdl {
304 1.6 fvdl "null", "load", "dynamic", "interp",
305 1.6 fvdl "note", "shlib", "phdr", "entry7"
306 1.6 fvdl };
307 1.6 fvdl
308 1.6 fvdl printf("p_type %ld [%s], ", p->p_type, types[p->p_type & 7]);
309 1.6 fvdl printf("p_offset %lx, ", p->p_offset);
310 1.6 fvdl printf("p_vaddr %lx, ", p->p_vaddr);
311 1.6 fvdl printf("p_paddr %lx, ", p->p_paddr);
312 1.6 fvdl printf("p_filesz %ld, ", p->p_filesz);
313 1.6 fvdl printf("p_memsz %ld, ", p->p_memsz);
314 1.6 fvdl printf("p_flags %lx, ", p->p_flags);
315 1.6 fvdl printf("p_align %ld\n", p->p_align);
316 1.6 fvdl }
317 1.6 fvdl #endif
318 1.4 christos
319 1.1 fvdl int
320 1.1 fvdl exec_linux_aout_makecmds(p, epp)
321 1.1 fvdl struct proc *p;
322 1.1 fvdl struct exec_package *epp;
323 1.1 fvdl {
324 1.1 fvdl struct exec *linux_ep = epp->ep_hdr;
325 1.1 fvdl int machtype, magic;
326 1.1 fvdl int error = ENOEXEC;
327 1.1 fvdl
328 1.1 fvdl magic = LINUX_N_MAGIC(linux_ep);
329 1.1 fvdl machtype = LINUX_N_MACHTYPE(linux_ep);
330 1.1 fvdl
331 1.1 fvdl
332 1.1 fvdl if (machtype != LINUX_MID_MACHINE)
333 1.1 fvdl return (ENOEXEC);
334 1.1 fvdl
335 1.1 fvdl switch (magic) {
336 1.1 fvdl case QMAGIC:
337 1.1 fvdl error = exec_linux_aout_prep_qmagic(p, epp);
338 1.1 fvdl break;
339 1.1 fvdl case ZMAGIC:
340 1.1 fvdl error = exec_linux_aout_prep_zmagic(p, epp);
341 1.1 fvdl break;
342 1.1 fvdl case NMAGIC:
343 1.1 fvdl error = exec_linux_aout_prep_nmagic(p, epp);
344 1.1 fvdl break;
345 1.1 fvdl case OMAGIC:
346 1.1 fvdl error = exec_linux_aout_prep_omagic(p, epp);
347 1.1 fvdl break;
348 1.1 fvdl }
349 1.4 christos if (error == 0)
350 1.6 fvdl epp->ep_emul = &emul_linux_aout;
351 1.1 fvdl return error;
352 1.1 fvdl }
353 1.1 fvdl
354 1.1 fvdl /*
355 1.1 fvdl * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
356 1.1 fvdl * is very likely not page aligned on most architectures, it is treated
357 1.1 fvdl * as an NMAGIC here. XXX
358 1.1 fvdl */
359 1.1 fvdl
360 1.1 fvdl int
361 1.1 fvdl exec_linux_aout_prep_zmagic(p, epp)
362 1.1 fvdl struct proc *p;
363 1.1 fvdl struct exec_package *epp;
364 1.1 fvdl {
365 1.1 fvdl struct exec *execp = epp->ep_hdr;
366 1.1 fvdl
367 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
368 1.1 fvdl epp->ep_tsize = execp->a_text;
369 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
370 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
371 1.1 fvdl epp->ep_entry = execp->a_entry;
372 1.1 fvdl
373 1.1 fvdl /* set up command for text segment */
374 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
375 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
376 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
377 1.1 fvdl
378 1.1 fvdl /* set up command for data segment */
379 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
380 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
381 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
382 1.1 fvdl
383 1.1 fvdl /* set up command for bss segment */
384 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
385 1.1 fvdl epp->ep_daddr + execp->a_data, NULLVP, 0,
386 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
387 1.1 fvdl
388 1.1 fvdl return exec_aout_setup_stack(p, epp);
389 1.1 fvdl }
390 1.1 fvdl
391 1.1 fvdl /*
392 1.1 fvdl * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
393 1.1 fvdl * Not different from the normal stuff.
394 1.1 fvdl */
395 1.1 fvdl
396 1.1 fvdl int
397 1.1 fvdl exec_linux_aout_prep_nmagic(p, epp)
398 1.1 fvdl struct proc *p;
399 1.1 fvdl struct exec_package *epp;
400 1.1 fvdl {
401 1.1 fvdl struct exec *execp = epp->ep_hdr;
402 1.1 fvdl long bsize, baddr;
403 1.1 fvdl
404 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
405 1.1 fvdl epp->ep_tsize = execp->a_text;
406 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
407 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
408 1.1 fvdl epp->ep_entry = execp->a_entry;
409 1.1 fvdl
410 1.1 fvdl /* set up command for text segment */
411 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
412 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
413 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
414 1.1 fvdl
415 1.1 fvdl /* set up command for data segment */
416 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
417 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
418 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
419 1.1 fvdl
420 1.1 fvdl /* set up command for bss segment */
421 1.1 fvdl baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
422 1.1 fvdl bsize = epp->ep_daddr + epp->ep_dsize - baddr;
423 1.1 fvdl if (bsize > 0)
424 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
425 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
426 1.1 fvdl
427 1.1 fvdl return exec_aout_setup_stack(p, epp);
428 1.1 fvdl }
429 1.1 fvdl
430 1.1 fvdl /*
431 1.1 fvdl * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
432 1.1 fvdl * Business as usual.
433 1.1 fvdl */
434 1.1 fvdl
435 1.1 fvdl int
436 1.1 fvdl exec_linux_aout_prep_omagic(p, epp)
437 1.1 fvdl struct proc *p;
438 1.1 fvdl struct exec_package *epp;
439 1.1 fvdl {
440 1.1 fvdl struct exec *execp = epp->ep_hdr;
441 1.1 fvdl long dsize, bsize, baddr;
442 1.1 fvdl
443 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
444 1.1 fvdl epp->ep_tsize = execp->a_text;
445 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
446 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
447 1.1 fvdl epp->ep_entry = execp->a_entry;
448 1.1 fvdl
449 1.1 fvdl /* set up command for text and data segments */
450 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
451 1.1 fvdl execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
452 1.1 fvdl LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
453 1.1 fvdl
454 1.1 fvdl /* set up command for bss segment */
455 1.1 fvdl baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
456 1.1 fvdl bsize = epp->ep_daddr + epp->ep_dsize - baddr;
457 1.1 fvdl if (bsize > 0)
458 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
459 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
460 1.1 fvdl
461 1.1 fvdl /*
462 1.1 fvdl * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
463 1.1 fvdl * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
464 1.1 fvdl * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
465 1.1 fvdl * respectively to page boundaries.
466 1.1 fvdl * Compensate `ep_dsize' for the amount of data covered by the last
467 1.1 fvdl * text page.
468 1.1 fvdl */
469 1.1 fvdl dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
470 1.1 fvdl epp->ep_dsize = (dsize > 0) ? dsize : 0;
471 1.1 fvdl return exec_aout_setup_stack(p, epp);
472 1.1 fvdl }
473 1.1 fvdl
474 1.1 fvdl int
475 1.1 fvdl exec_linux_aout_prep_qmagic(p, epp)
476 1.1 fvdl struct proc *p;
477 1.1 fvdl struct exec_package *epp;
478 1.1 fvdl {
479 1.1 fvdl struct exec *execp = epp->ep_hdr;
480 1.1 fvdl
481 1.1 fvdl epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
482 1.1 fvdl epp->ep_tsize = execp->a_text;
483 1.1 fvdl epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
484 1.1 fvdl epp->ep_dsize = execp->a_data + execp->a_bss;
485 1.1 fvdl epp->ep_entry = execp->a_entry;
486 1.1 fvdl
487 1.1 fvdl /*
488 1.1 fvdl * check if vnode is in open for writing, because we want to
489 1.1 fvdl * demand-page out of it. if it is, don't do it, for various
490 1.1 fvdl * reasons
491 1.1 fvdl */
492 1.1 fvdl if ((execp->a_text != 0 || execp->a_data != 0) &&
493 1.1 fvdl epp->ep_vp->v_writecount != 0) {
494 1.1 fvdl #ifdef DIAGNOSTIC
495 1.1 fvdl if (epp->ep_vp->v_flag & VTEXT)
496 1.1 fvdl panic("exec: a VTEXT vnode has writecount != 0\n");
497 1.1 fvdl #endif
498 1.1 fvdl return ETXTBSY;
499 1.1 fvdl }
500 1.1 fvdl epp->ep_vp->v_flag |= VTEXT;
501 1.1 fvdl
502 1.1 fvdl /* set up command for text segment */
503 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
504 1.1 fvdl epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
505 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE);
506 1.1 fvdl
507 1.1 fvdl /* set up command for data segment */
508 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
509 1.1 fvdl epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
510 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
511 1.1 fvdl
512 1.1 fvdl /* set up command for bss segment */
513 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
514 1.1 fvdl epp->ep_daddr + execp->a_data, NULLVP, 0,
515 1.1 fvdl VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
516 1.1 fvdl
517 1.1 fvdl return exec_aout_setup_stack(p, epp);
518 1.1 fvdl }
519 1.1 fvdl
520 1.1 fvdl /*
521 1.6 fvdl * linux_elf_check_header():
522 1.6 fvdl *
523 1.6 fvdl * Check header for validity; return 0 of ok ENOEXEC if error
524 1.6 fvdl */
525 1.6 fvdl static int
526 1.6 fvdl linux_elf_check_header(eh, type)
527 1.6 fvdl Elf32_Ehdr *eh;
528 1.6 fvdl int type;
529 1.6 fvdl {
530 1.6 fvdl #ifdef sparc
531 1.6 fvdl /* #$%@#$%@#$%! */
532 1.6 fvdl # define memcmp bcmp
533 1.6 fvdl #endif
534 1.6 fvdl if (memcmp(eh->e_ident, Elf32_e_ident, Elf32_e_siz) != 0) {
535 1.6 fvdl DPRINTF(("Not an elf file\n"));
536 1.6 fvdl return ENOEXEC;
537 1.6 fvdl }
538 1.6 fvdl
539 1.6 fvdl switch (eh->e_machine) {
540 1.6 fvdl #ifdef i386
541 1.6 fvdl case Elf32_em_386:
542 1.6 fvdl case Elf32_em_486:
543 1.6 fvdl #endif
544 1.6 fvdl #ifdef sparc
545 1.6 fvdl case Elf32_em_sparc:
546 1.6 fvdl #endif
547 1.6 fvdl break;
548 1.6 fvdl
549 1.6 fvdl default:
550 1.6 fvdl DPRINTF(("Unsupported elf machine type %d\n", eh->e_machine));
551 1.6 fvdl return ENOEXEC;
552 1.6 fvdl }
553 1.6 fvdl
554 1.6 fvdl if (eh->e_type != type) {
555 1.6 fvdl DPRINTF(("Not an elf executable\n"));
556 1.6 fvdl return ENOEXEC;
557 1.6 fvdl }
558 1.6 fvdl
559 1.6 fvdl return 0;
560 1.6 fvdl }
561 1.6 fvdl
562 1.6 fvdl
563 1.6 fvdl /*
564 1.6 fvdl * linux_elf_load_psection():
565 1.6 fvdl *
566 1.6 fvdl * Load a psection at the appropriate address
567 1.6 fvdl */
568 1.6 fvdl static void
569 1.6 fvdl linux_elf_load_psection(vcset, vp, ph, addr, size, prot)
570 1.6 fvdl struct exec_vmcmd_set *vcset;
571 1.6 fvdl struct vnode *vp;
572 1.6 fvdl Elf32_Phdr *ph;
573 1.6 fvdl u_long *addr;
574 1.6 fvdl u_long *size;
575 1.6 fvdl int *prot;
576 1.6 fvdl {
577 1.6 fvdl u_long uaddr;
578 1.6 fvdl long diff;
579 1.6 fvdl long offset;
580 1.6 fvdl u_long msize;
581 1.6 fvdl
582 1.6 fvdl /*
583 1.6 fvdl * If the user specified an address, then we load there.
584 1.6 fvdl */
585 1.6 fvdl if (*addr != ~0) {
586 1.6 fvdl uaddr = *addr + ph->p_align;
587 1.6 fvdl *addr = LINUX_ELF_ALIGN(uaddr, ph->p_align);
588 1.6 fvdl uaddr = LINUX_ELF_ALIGN(ph->p_vaddr, ph->p_align);
589 1.6 fvdl diff = ph->p_vaddr - uaddr;
590 1.6 fvdl } else {
591 1.6 fvdl uaddr = ph->p_vaddr;
592 1.6 fvdl *addr = LINUX_ELF_ALIGN(uaddr, ph->p_align);
593 1.6 fvdl diff = uaddr - *addr;
594 1.6 fvdl }
595 1.6 fvdl
596 1.6 fvdl *prot |= (ph->p_flags & Elf32_pf_r) ? VM_PROT_READ : 0;
597 1.6 fvdl *prot |= (ph->p_flags & Elf32_pf_w) ? VM_PROT_WRITE : 0;
598 1.6 fvdl *prot |= (ph->p_flags & Elf32_pf_x) ? VM_PROT_EXECUTE : 0;
599 1.6 fvdl
600 1.6 fvdl offset = ph->p_offset - diff;
601 1.6 fvdl *size = ph->p_filesz + diff;
602 1.6 fvdl msize = ph->p_memsz + diff;
603 1.6 fvdl
604 1.6 fvdl DPRINTF(("Elf Seg@ 0x%x/0x%x sz %d/%d off 0x%x/0x%x[%d] algn 0x%x\n",
605 1.6 fvdl ph->p_vaddr, *addr, *size, msize, ph->p_offset, offset,
606 1.6 fvdl diff, ph->p_align));
607 1.6 fvdl
608 1.6 fvdl NEW_VMCMD(vcset, vmcmd_map_readvn, *size,
609 1.6 fvdl *addr, vp, offset, *prot);
610 1.6 fvdl
611 1.6 fvdl /*
612 1.6 fvdl * Check if we need to extend the size of the segment
613 1.6 fvdl */
614 1.6 fvdl {
615 1.6 fvdl u_long rm = round_page(*addr + msize);
616 1.6 fvdl u_long rf = round_page(*addr + *size);
617 1.6 fvdl if (rm != rf) {
618 1.6 fvdl DPRINTF(("zeropad 0x%x-0x%x\n", rf, rm));
619 1.6 fvdl NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf,
620 1.6 fvdl rf, NULLVP, 0, *prot);
621 1.6 fvdl *size = msize;
622 1.6 fvdl }
623 1.6 fvdl }
624 1.6 fvdl }
625 1.6 fvdl
626 1.6 fvdl
627 1.6 fvdl /*
628 1.6 fvdl * linux_elf_set_segment():
629 1.6 fvdl *
630 1.6 fvdl * Decide if the segment is text or data, depending on the protection
631 1.6 fvdl * and set it appropriately
632 1.6 fvdl */
633 1.6 fvdl static int
634 1.6 fvdl linux_elf_set_segment(epp, vaddr, size, prot)
635 1.6 fvdl struct exec_package *epp;
636 1.6 fvdl u_long vaddr;
637 1.6 fvdl u_long size;
638 1.6 fvdl int prot;
639 1.6 fvdl {
640 1.6 fvdl /*
641 1.6 fvdl * Kludge: Unfortunately the current implementation of
642 1.6 fvdl * exec package assumes a single text and data segment.
643 1.6 fvdl * In Elf we can have more, but here we limit ourselves
644 1.6 fvdl * to two and hope :-(
645 1.6 fvdl * We also assume that the text is r-x, and data is rwx.
646 1.6 fvdl */
647 1.6 fvdl switch (prot) {
648 1.6 fvdl case (VM_PROT_READ | VM_PROT_EXECUTE):
649 1.6 fvdl if (epp->ep_tsize != ~0) {
650 1.6 fvdl DPRINTF(("More than one text segment\n"));
651 1.6 fvdl return ENOEXEC;
652 1.6 fvdl }
653 1.6 fvdl epp->ep_taddr = vaddr;
654 1.6 fvdl epp->ep_tsize = size;
655 1.6 fvdl DPRINTF(("Elf Text@ 0x%x, size %d\n", vaddr, size));
656 1.6 fvdl break;
657 1.6 fvdl
658 1.6 fvdl case (VM_PROT_READ | VM_PROT_WRITE):
659 1.6 fvdl case (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE):
660 1.6 fvdl if (epp->ep_dsize != ~0) {
661 1.6 fvdl DPRINTF(("More than one data segment\n"));
662 1.6 fvdl return ENOEXEC;
663 1.6 fvdl }
664 1.6 fvdl epp->ep_daddr = vaddr;
665 1.6 fvdl epp->ep_dsize = size;
666 1.6 fvdl
667 1.6 fvdl DPRINTF(("Elf Data@ 0x%x, size %d\n", vaddr, size));
668 1.6 fvdl break;
669 1.6 fvdl
670 1.6 fvdl default:
671 1.6 fvdl DPRINTF(("Bad protection 0%o\n", prot));
672 1.6 fvdl return ENOEXEC;
673 1.6 fvdl }
674 1.6 fvdl return 0;
675 1.6 fvdl }
676 1.6 fvdl
677 1.6 fvdl
678 1.6 fvdl /*
679 1.6 fvdl * linux_elf_read_from():
680 1.6 fvdl *
681 1.6 fvdl * Read from vnode into buffer at offset.
682 1.6 fvdl */
683 1.6 fvdl static int
684 1.6 fvdl linux_elf_read_from(vp, off, p, buf, size)
685 1.6 fvdl struct vnode *vp;
686 1.6 fvdl u_long off;
687 1.6 fvdl struct proc *p;
688 1.6 fvdl caddr_t buf;
689 1.6 fvdl int size;
690 1.6 fvdl {
691 1.6 fvdl int error;
692 1.6 fvdl int resid;
693 1.6 fvdl
694 1.6 fvdl DPRINTF(("read from 0x%x to 0x%x size %d\n",
695 1.6 fvdl off, buf, size));
696 1.6 fvdl if ((error = vn_rdwr(UIO_READ, vp, buf, size,
697 1.6 fvdl off, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
698 1.6 fvdl &resid, p)) != 0) {
699 1.6 fvdl DPRINTF(("Bad read error %d\n", error));
700 1.6 fvdl return error;
701 1.6 fvdl }
702 1.6 fvdl /*
703 1.6 fvdl * See if we got all of it
704 1.6 fvdl */
705 1.6 fvdl if (resid != 0) {
706 1.6 fvdl DPRINTF(("Incomplete read for header ask=%d, rem=%d\n",
707 1.6 fvdl size, resid));
708 1.6 fvdl return error;
709 1.6 fvdl }
710 1.6 fvdl return 0;
711 1.6 fvdl }
712 1.6 fvdl
713 1.6 fvdl
714 1.6 fvdl /*
715 1.6 fvdl * linux_elf_load_file():
716 1.6 fvdl *
717 1.6 fvdl * Load a file (interpreter/library) pointed to by path
718 1.6 fvdl * [stolen from coff_load_shlib()]. Made slightly more generic than
719 1.6 fvdl * the svr4 version, for possible later use in linux_uselib().
720 1.6 fvdl */
721 1.6 fvdl static int
722 1.6 fvdl linux_elf_load_file(p, path, vcset, entry, ap, last)
723 1.6 fvdl struct proc *p;
724 1.6 fvdl char *path;
725 1.6 fvdl struct exec_vmcmd_set *vcset;
726 1.6 fvdl u_long *entry;
727 1.6 fvdl struct elf_args *ap;
728 1.6 fvdl u_long *last;
729 1.6 fvdl {
730 1.6 fvdl int error, i;
731 1.6 fvdl struct nameidata nd;
732 1.6 fvdl Elf32_Ehdr eh;
733 1.6 fvdl Elf32_Phdr *ph = NULL;
734 1.6 fvdl u_long phsize;
735 1.6 fvdl char *bp = NULL;
736 1.6 fvdl u_long addr = *last;
737 1.6 fvdl
738 1.6 fvdl DPRINTF(("Loading file %s @ %x\n", path, addr));
739 1.6 fvdl
740 1.6 fvdl if ((error = linux_emul_find(p, NULL, linux_emul_path, path, &bp, 0)) != 0)
741 1.6 fvdl bp = NULL;
742 1.6 fvdl else
743 1.6 fvdl path = bp;
744 1.6 fvdl /*
745 1.6 fvdl * 1. open file
746 1.6 fvdl * 2. read filehdr
747 1.6 fvdl * 3. map text, data, and bss out of it using VM_*
748 1.6 fvdl */
749 1.6 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
750 1.6 fvdl /* first get the vnode */
751 1.6 fvdl if ((error = namei(&nd)) != 0) {
752 1.6 fvdl if (bp != NULL)
753 1.6 fvdl free((char *) bp, M_TEMP);
754 1.6 fvdl return error;
755 1.6 fvdl }
756 1.6 fvdl if ((error = linux_elf_read_from(nd.ni_vp, 0, p, (caddr_t) &eh,
757 1.6 fvdl sizeof(eh))) != 0)
758 1.6 fvdl goto bad;
759 1.6 fvdl
760 1.6 fvdl #ifdef DEBUG_EXEC_LINUX_ELF
761 1.6 fvdl print_Ehdr(&eh);
762 1.6 fvdl #endif
763 1.6 fvdl
764 1.6 fvdl if ((error = linux_elf_check_header(&eh, Elf32_et_dyn)) != 0)
765 1.6 fvdl goto bad;
766 1.6 fvdl
767 1.6 fvdl phsize = eh.e_phnum * sizeof(Elf32_Phdr);
768 1.6 fvdl ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
769 1.6 fvdl
770 1.6 fvdl if ((error = linux_elf_read_from(nd.ni_vp, eh.e_phoff, p,
771 1.6 fvdl (caddr_t) ph, phsize)) != 0)
772 1.6 fvdl goto bad;
773 1.6 fvdl
774 1.6 fvdl /*
775 1.6 fvdl * Load all the necessary sections
776 1.6 fvdl */
777 1.6 fvdl for (i = 0; i < eh.e_phnum; i++) {
778 1.6 fvdl u_long size = 0;
779 1.6 fvdl int prot = 0;
780 1.6 fvdl #ifdef DEBUG_EXEC_LINUX_ELF
781 1.6 fvdl print_Phdr(&ph[i]);
782 1.6 fvdl #endif
783 1.6 fvdl
784 1.6 fvdl switch (ph[i].p_type) {
785 1.6 fvdl case Elf32_pt_load:
786 1.6 fvdl linux_elf_load_psection(vcset, nd.ni_vp, &ph[i], &addr,
787 1.6 fvdl &size, &prot);
788 1.6 fvdl /* Assume that the text segment is r-x only */
789 1.6 fvdl if ((prot & PROT_WRITE) == 0) {
790 1.6 fvdl *entry = addr + eh.e_entry;
791 1.6 fvdl ap->arg_interp = addr;
792 1.6 fvdl DPRINTF(("Interpreter@ 0x%x\n", addr));
793 1.6 fvdl }
794 1.6 fvdl addr += size;
795 1.6 fvdl break;
796 1.6 fvdl
797 1.6 fvdl case Elf32_pt_dynamic:
798 1.6 fvdl case Elf32_pt_phdr:
799 1.6 fvdl case Elf32_pt_note:
800 1.6 fvdl break;
801 1.6 fvdl
802 1.6 fvdl default:
803 1.6 fvdl DPRINTF(("interp: Unexpected program header type %d\n",
804 1.6 fvdl ph[i].p_type));
805 1.6 fvdl break;
806 1.6 fvdl }
807 1.6 fvdl }
808 1.6 fvdl
809 1.6 fvdl bad:
810 1.6 fvdl if (ph != NULL)
811 1.6 fvdl free((char *) ph, M_TEMP);
812 1.6 fvdl if (bp != NULL)
813 1.6 fvdl free((char *) bp, M_TEMP);
814 1.6 fvdl
815 1.6 fvdl *last = addr;
816 1.6 fvdl vrele(nd.ni_vp);
817 1.6 fvdl return error;
818 1.6 fvdl }
819 1.6 fvdl
820 1.6 fvdl
821 1.6 fvdl /*
822 1.6 fvdl * exec_linux_elf_makecmds(): Prepare an Elf binary's exec package
823 1.6 fvdl *
824 1.6 fvdl * First, set of the various offsets/lengths in the exec package.
825 1.6 fvdl *
826 1.6 fvdl * Then, mark the text image busy (so it can be demand paged) or error
827 1.6 fvdl * out if this is not possible. Finally, set up vmcmds for the
828 1.6 fvdl * text, data, bss, and stack segments.
829 1.6 fvdl */
830 1.6 fvdl int
831 1.6 fvdl exec_linux_elf_makecmds(p, epp)
832 1.6 fvdl struct proc *p;
833 1.6 fvdl struct exec_package *epp;
834 1.6 fvdl {
835 1.6 fvdl Elf32_Ehdr *eh = epp->ep_hdr;
836 1.6 fvdl Elf32_Phdr *ph, *pp;
837 1.6 fvdl int error;
838 1.6 fvdl int i;
839 1.6 fvdl char interp[MAXPATHLEN];
840 1.6 fvdl u_long pos = 0;
841 1.6 fvdl u_long phsize;
842 1.6 fvdl
843 1.6 fvdl #ifdef DEBUG_EXEC_LINUX_ELF
844 1.6 fvdl print_Ehdr(eh);
845 1.6 fvdl #endif
846 1.6 fvdl if (epp->ep_hdrvalid < sizeof(Elf32_Ehdr))
847 1.6 fvdl return ENOEXEC;
848 1.6 fvdl
849 1.6 fvdl if (linux_elf_check_header(eh, Elf32_et_exec))
850 1.6 fvdl return ENOEXEC;
851 1.6 fvdl
852 1.6 fvdl /*
853 1.6 fvdl * check if vnode is in open for writing, because we want to
854 1.6 fvdl * demand-page out of it. if it is, don't do it, for various
855 1.6 fvdl * reasons
856 1.6 fvdl */
857 1.6 fvdl if (epp->ep_vp->v_writecount != 0) {
858 1.6 fvdl #ifdef DIAGNOSTIC
859 1.6 fvdl if (epp->ep_vp->v_flag & VTEXT)
860 1.6 fvdl panic("exec: a VTEXT vnode has writecount != 0\n");
861 1.6 fvdl #endif
862 1.6 fvdl return ETXTBSY;
863 1.6 fvdl }
864 1.6 fvdl /*
865 1.6 fvdl * Allocate space to hold all the program headers, and read them
866 1.6 fvdl * from the file
867 1.6 fvdl */
868 1.6 fvdl phsize = eh->e_phnum * sizeof(Elf32_Phdr);
869 1.6 fvdl ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
870 1.6 fvdl
871 1.6 fvdl if ((error = linux_elf_read_from(epp->ep_vp, eh->e_phoff, p,
872 1.6 fvdl (caddr_t) ph, phsize)) != 0)
873 1.6 fvdl goto bad;
874 1.6 fvdl
875 1.6 fvdl epp->ep_tsize = ~0;
876 1.6 fvdl epp->ep_dsize = ~0;
877 1.6 fvdl
878 1.6 fvdl interp[0] = '\0';
879 1.6 fvdl
880 1.6 fvdl /*
881 1.6 fvdl * Load all the necessary sections
882 1.6 fvdl */
883 1.6 fvdl for (i = 0; i < eh->e_phnum; i++) {
884 1.6 fvdl u_long addr = ~0, size = 0;
885 1.6 fvdl int prot = 0;
886 1.6 fvdl
887 1.6 fvdl pp = &ph[i];
888 1.6 fvdl #ifdef DEBUG_EXEC_LINUX_ELF
889 1.6 fvdl print_Phdr(pp);
890 1.6 fvdl #endif
891 1.6 fvdl
892 1.6 fvdl switch (ph[i].p_type) {
893 1.6 fvdl case Elf32_pt_load:
894 1.6 fvdl linux_elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
895 1.6 fvdl &ph[i], &addr, &size, &prot);
896 1.6 fvdl if ((error = linux_elf_set_segment(epp, addr, size,
897 1.6 fvdl prot)) != 0)
898 1.6 fvdl goto bad;
899 1.6 fvdl break;
900 1.6 fvdl
901 1.6 fvdl case Elf32_pt_shlib:
902 1.6 fvdl DPRINTF(("No support for COFF libraries (yet)\n"));
903 1.6 fvdl error = ENOEXEC;
904 1.6 fvdl goto bad;
905 1.6 fvdl
906 1.6 fvdl case Elf32_pt_interp:
907 1.6 fvdl if (pp->p_filesz >= sizeof(interp)) {
908 1.6 fvdl DPRINTF(("Interpreter path too long %d\n",
909 1.6 fvdl pp->p_filesz));
910 1.6 fvdl goto bad;
911 1.6 fvdl }
912 1.6 fvdl if ((error = linux_elf_read_from(epp->ep_vp, pp->p_offset, p,
913 1.6 fvdl (caddr_t) interp, pp->p_filesz)) != 0)
914 1.6 fvdl goto bad;
915 1.6 fvdl break;
916 1.6 fvdl
917 1.6 fvdl case Elf32_pt_dynamic:
918 1.6 fvdl case Elf32_pt_phdr:
919 1.6 fvdl case Elf32_pt_note:
920 1.6 fvdl break;
921 1.6 fvdl
922 1.6 fvdl default:
923 1.6 fvdl /*
924 1.6 fvdl * Not fatal, we don't need to understand everything
925 1.6 fvdl * :-)
926 1.6 fvdl */
927 1.6 fvdl DPRINTF(("Unsupported program header type %d\n",
928 1.6 fvdl pp->p_type));
929 1.6 fvdl break;
930 1.6 fvdl }
931 1.6 fvdl }
932 1.6 fvdl
933 1.6 fvdl /*
934 1.6 fvdl * Check if we found a dynamically linked binary and arrange to load
935 1.6 fvdl * it's interpreter
936 1.6 fvdl */
937 1.6 fvdl if (interp[0]) {
938 1.6 fvdl struct elf_args *ap;
939 1.6 fvdl pos = ~0;
940 1.6 fvdl
941 1.6 fvdl ap = (struct elf_args *) malloc(sizeof(struct elf_args),
942 1.6 fvdl M_TEMP, M_WAITOK);
943 1.6 fvdl if ((error = linux_elf_load_file(p, interp, &epp->ep_vmcmds,
944 1.6 fvdl &epp->ep_entry, ap, &pos)) != 0) {
945 1.6 fvdl free((char *) ap, M_TEMP);
946 1.6 fvdl goto bad;
947 1.6 fvdl }
948 1.6 fvdl /* Arrange to load the program headers. */
949 1.6 fvdl pos = LINUX_ELF_ALIGN(pos + NBPG, NBPG);
950 1.6 fvdl DPRINTF(("Program header @0x%x\n", pos));
951 1.6 fvdl ap->arg_phaddr = pos;
952 1.6 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, phsize,
953 1.6 fvdl pos, epp->ep_vp, eh->e_phoff,
954 1.6 fvdl VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
955 1.6 fvdl pos += phsize;
956 1.6 fvdl
957 1.6 fvdl ap->arg_phentsize = eh->e_phentsize;
958 1.6 fvdl ap->arg_phnum = eh->e_phnum;
959 1.6 fvdl ap->arg_entry = eh->e_entry;
960 1.6 fvdl
961 1.6 fvdl epp->ep_emul_arg = ap;
962 1.6 fvdl } else
963 1.6 fvdl epp->ep_entry = eh->e_entry;
964 1.6 fvdl
965 1.6 fvdl DPRINTF(("taddr 0x%x tsize 0x%x daddr 0x%x dsize 0x%x\n",
966 1.6 fvdl epp->ep_taddr, epp->ep_tsize, epp->ep_daddr, epp->ep_dsize));
967 1.6 fvdl
968 1.6 fvdl free((char *) ph, M_TEMP);
969 1.6 fvdl
970 1.6 fvdl DPRINTF(("Elf entry@ 0x%x\n", epp->ep_entry));
971 1.6 fvdl epp->ep_vp->v_flag |= VTEXT;
972 1.6 fvdl
973 1.6 fvdl epp->ep_emul = &emul_linux_elf;
974 1.6 fvdl
975 1.6 fvdl return exec_aout_setup_stack(p, epp);
976 1.6 fvdl
977 1.6 fvdl bad:
978 1.6 fvdl free((char *) ph, M_TEMP);
979 1.6 fvdl kill_vmcmds(&epp->ep_vmcmds);
980 1.6 fvdl return ENOEXEC;
981 1.6 fvdl }
982 1.6 fvdl /*
983 1.6 fvdl * The Linux system call to load shared libraries, a.out version. The
984 1.6 fvdl * a.out shared libs are just files that are mapped onto a fixed
985 1.6 fvdl * address in the process' address space. The address is given in
986 1.1 fvdl * a_entry. Read in the header, set up some VM commands and run them.
987 1.1 fvdl *
988 1.1 fvdl * Yes, both text and data are mapped at once, so we're left with
989 1.1 fvdl * writeable text for the shared libs. The Linux crt0 seemed to break
990 1.1 fvdl * sometimes when data was mapped seperately. It munmapped a uselib()
991 1.1 fvdl * of ld.so by hand, which failed with shared text and data for ld.so
992 1.1 fvdl * Yuck.
993 1.1 fvdl *
994 1.1 fvdl * Because of the problem with ZMAGIC executables (text starts
995 1.6 fvdl * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
996 1.1 fvdl * shared libs are not handled very efficiently :-(
997 1.1 fvdl */
998 1.1 fvdl
999 1.1 fvdl int
1000 1.1 fvdl linux_uselib(p, uap, retval)
1001 1.1 fvdl struct proc *p;
1002 1.1 fvdl struct linux_uselib_args /* {
1003 1.1 fvdl syscallarg(char *) path;
1004 1.1 fvdl } */ *uap;
1005 1.1 fvdl register_t *retval;
1006 1.1 fvdl {
1007 1.1 fvdl caddr_t sg;
1008 1.1 fvdl long bsize, dsize, tsize, taddr, baddr, daddr;
1009 1.1 fvdl struct nameidata ni;
1010 1.1 fvdl struct vnode *vp;
1011 1.1 fvdl struct exec hdr;
1012 1.1 fvdl struct exec_vmcmd_set vcset;
1013 1.1 fvdl int rem, i, magic, error;
1014 1.1 fvdl
1015 1.1 fvdl sg = stackgap_init();
1016 1.2 fvdl CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
1017 1.1 fvdl
1018 1.1 fvdl NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1019 1.1 fvdl
1020 1.1 fvdl if ((error = namei(&ni)))
1021 1.1 fvdl return error;
1022 1.1 fvdl
1023 1.1 fvdl vp = ni.ni_vp;
1024 1.1 fvdl
1025 1.1 fvdl if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
1026 1.1 fvdl 0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
1027 1.1 fvdl &rem, p))) {
1028 1.1 fvdl vrele(vp);
1029 1.1 fvdl return error;
1030 1.1 fvdl }
1031 1.1 fvdl
1032 1.1 fvdl if (rem != 0) {
1033 1.1 fvdl vrele(vp);
1034 1.1 fvdl return ENOEXEC;
1035 1.1 fvdl }
1036 1.6 fvdl
1037 1.6 fvdl if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
1038 1.6 fvdl return ENOEXEC;
1039 1.1 fvdl
1040 1.1 fvdl magic = LINUX_N_MAGIC(&hdr);
1041 1.1 fvdl taddr = hdr.a_entry & (~(NBPG - 1));
1042 1.1 fvdl tsize = hdr.a_text;
1043 1.1 fvdl daddr = taddr + tsize;
1044 1.1 fvdl dsize = hdr.a_data + hdr.a_bss;
1045 1.1 fvdl
1046 1.1 fvdl if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
1047 1.1 fvdl vrele(vp);
1048 1.1 fvdl return ETXTBSY;
1049 1.1 fvdl }
1050 1.1 fvdl vp->v_flag |= VTEXT;
1051 1.1 fvdl
1052 1.1 fvdl vcset.evs_cnt = 0;
1053 1.1 fvdl vcset.evs_used = 0;
1054 1.1 fvdl
1055 1.1 fvdl NEW_VMCMD(&vcset,
1056 1.1 fvdl magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
1057 1.1 fvdl hdr.a_text + hdr.a_data, taddr,
1058 1.1 fvdl vp, LINUX_N_TXTOFF(hdr, magic),
1059 1.1 fvdl VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
1060 1.1 fvdl
1061 1.1 fvdl baddr = roundup(daddr + hdr.a_data, NBPG);
1062 1.1 fvdl bsize = daddr + dsize - baddr;
1063 1.1 fvdl if (bsize > 0) {
1064 1.1 fvdl NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
1065 1.1 fvdl NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
1066 1.1 fvdl }
1067 1.1 fvdl
1068 1.1 fvdl for (i = 0; i < vcset.evs_used && !error; i++) {
1069 1.1 fvdl struct exec_vmcmd *vcp;
1070 1.1 fvdl
1071 1.1 fvdl vcp = &vcset.evs_cmds[i];
1072 1.1 fvdl error = (*vcp->ev_proc)(p, vcp);
1073 1.1 fvdl }
1074 1.1 fvdl
1075 1.1 fvdl kill_vmcmds(&vcset);
1076 1.1 fvdl
1077 1.1 fvdl vrele(vp);
1078 1.1 fvdl
1079 1.1 fvdl return error;
1080 1.1 fvdl }
1081 1.1 fvdl
1082 1.1 fvdl /*
1083 1.1 fvdl * Execve(2). Just check the alternate emulation path, and pass it on
1084 1.1 fvdl * to the NetBSD execve().
1085 1.1 fvdl */
1086 1.1 fvdl int
1087 1.1 fvdl linux_execve(p, uap, retval)
1088 1.1 fvdl struct proc *p;
1089 1.1 fvdl struct linux_execve_args /* {
1090 1.1 fvdl syscallarg(char *) path;
1091 1.1 fvdl syscallarg(char **) argv;
1092 1.1 fvdl syscallarg(char **) envp;
1093 1.1 fvdl } */ *uap;
1094 1.1 fvdl register_t *retval;
1095 1.1 fvdl {
1096 1.1 fvdl caddr_t sg;
1097 1.1 fvdl
1098 1.1 fvdl sg = stackgap_init();
1099 1.2 fvdl CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
1100 1.1 fvdl
1101 1.1 fvdl return execve(p, uap, retval);
1102 1.1 fvdl }
1103