exec_elf.c revision 1.8 1 1.7 christos /* $NetBSD: exec_elf.c,v 1.8 1996/06/14 18:15:55 christos Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Copyright (c) 1994 Christos Zoulas
5 1.1 fvdl * All rights reserved.
6 1.1 fvdl *
7 1.1 fvdl * Redistribution and use in source and binary forms, with or without
8 1.1 fvdl * modification, are permitted provided that the following conditions
9 1.1 fvdl * are met:
10 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
11 1.1 fvdl * notice, this list of conditions and the following disclaimer.
12 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
14 1.1 fvdl * documentation and/or other materials provided with the distribution.
15 1.1 fvdl * 3. The name of the author may not be used to endorse or promote products
16 1.1 fvdl * derived from this software without specific prior written permission
17 1.1 fvdl *
18 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 fvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 fvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 fvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 fvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 fvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 fvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 fvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 fvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 fvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 fvdl *
29 1.1 fvdl */
30 1.1 fvdl
31 1.1 fvdl #include <sys/param.h>
32 1.1 fvdl #include <sys/systm.h>
33 1.1 fvdl #include <sys/kernel.h>
34 1.1 fvdl #include <sys/proc.h>
35 1.1 fvdl #include <sys/malloc.h>
36 1.1 fvdl #include <sys/namei.h>
37 1.1 fvdl #include <sys/vnode.h>
38 1.1 fvdl #include <sys/exec.h>
39 1.1 fvdl #include <sys/exec_elf.h>
40 1.8 christos #include <sys/syscall.h>
41 1.8 christos #include <sys/signalvar.h>
42 1.1 fvdl
43 1.1 fvdl #include <sys/mman.h>
44 1.1 fvdl #include <vm/vm.h>
45 1.1 fvdl #include <vm/vm_param.h>
46 1.1 fvdl #include <vm/vm_map.h>
47 1.1 fvdl
48 1.1 fvdl #include <machine/cpu.h>
49 1.1 fvdl #include <machine/reg.h>
50 1.1 fvdl #include <machine/exec.h>
51 1.1 fvdl
52 1.1 fvdl #ifdef COMPAT_LINUX
53 1.1 fvdl #include <compat/linux/linux_exec.h>
54 1.1 fvdl #endif
55 1.1 fvdl
56 1.1 fvdl #ifdef COMPAT_SVR4
57 1.1 fvdl #include <compat/svr4/svr4_exec.h>
58 1.1 fvdl #endif
59 1.1 fvdl
60 1.6 christos int (*elf_probe_funcs[]) __P((struct proc *, struct exec_package *,
61 1.7 christos Elf32_Ehdr *, char *, u_long *)) = {
62 1.7 christos #ifdef COMPAT_LINUX
63 1.7 christos linux_elf_probe,
64 1.7 christos #endif
65 1.1 fvdl #ifdef COMPAT_SVR4
66 1.1 fvdl svr4_elf_probe,
67 1.1 fvdl #endif
68 1.1 fvdl };
69 1.6 christos
70 1.6 christos int elf_check_header __P((Elf32_Ehdr *, int));
71 1.6 christos int elf_load_file __P((struct proc *, char *, struct exec_vmcmd_set *,
72 1.6 christos u_long *, struct elf_args *, u_long *));
73 1.1 fvdl
74 1.1 fvdl static void elf_load_psection __P((struct exec_vmcmd_set *,
75 1.1 fvdl struct vnode *, Elf32_Phdr *, u_long *, u_long *, int *));
76 1.1 fvdl
77 1.1 fvdl #define ELF_ALIGN(a, b) ((a) & ~((b) - 1))
78 1.1 fvdl
79 1.1 fvdl /*
80 1.8 christos * This is the basic elf emul. elf_probe_funcs may change to other emuls.
81 1.8 christos */
82 1.8 christos extern char sigcode[], esigcode[];
83 1.8 christos #ifdef SYSCALL_DEBUG
84 1.8 christos extern char *syscallnames[];
85 1.8 christos #endif
86 1.8 christos
87 1.8 christos struct emul emul_elf = {
88 1.8 christos "netbsd",
89 1.8 christos NULL,
90 1.8 christos sendsig,
91 1.8 christos SYS_syscall,
92 1.8 christos SYS_MAXSYSCALL,
93 1.8 christos sysent,
94 1.8 christos #ifdef SYSCALL_DEBUG
95 1.8 christos syscallnames,
96 1.8 christos #else
97 1.8 christos NULL,
98 1.8 christos #endif
99 1.8 christos sizeof(AuxInfo) * ELF_AUX_ENTRIES,
100 1.8 christos elf_copyargs,
101 1.8 christos setregs,
102 1.8 christos sigcode,
103 1.8 christos esigcode,
104 1.8 christos };
105 1.8 christos
106 1.8 christos
107 1.8 christos /*
108 1.1 fvdl * Copy arguments onto the stack in the normal way, but add some
109 1.1 fvdl * extra information in case of dynamic binding.
110 1.1 fvdl */
111 1.1 fvdl void *
112 1.1 fvdl elf_copyargs(pack, arginfo, stack, argp)
113 1.1 fvdl struct exec_package *pack;
114 1.1 fvdl struct ps_strings *arginfo;
115 1.1 fvdl void *stack;
116 1.1 fvdl void *argp;
117 1.1 fvdl {
118 1.1 fvdl size_t len;
119 1.4 fvdl AuxInfo ai[ELF_AUX_ENTRIES], *a;
120 1.1 fvdl struct elf_args *ap;
121 1.1 fvdl
122 1.4 fvdl stack = copyargs(pack, arginfo, stack, argp);
123 1.4 fvdl if (!stack)
124 1.1 fvdl return NULL;
125 1.1 fvdl
126 1.1 fvdl /*
127 1.1 fvdl * Push extra arguments on the stack needed by dynamically
128 1.1 fvdl * linked binaries
129 1.1 fvdl */
130 1.1 fvdl if ((ap = (struct elf_args *) pack->ep_emul_arg)) {
131 1.4 fvdl a = ai;
132 1.1 fvdl
133 1.1 fvdl a->au_id = AUX_phdr;
134 1.1 fvdl a->au_v = ap->arg_phaddr;
135 1.1 fvdl a++;
136 1.1 fvdl
137 1.1 fvdl a->au_id = AUX_phent;
138 1.1 fvdl a->au_v = ap->arg_phentsize;
139 1.1 fvdl a++;
140 1.1 fvdl
141 1.1 fvdl a->au_id = AUX_phnum;
142 1.1 fvdl a->au_v = ap->arg_phnum;
143 1.1 fvdl a++;
144 1.1 fvdl
145 1.1 fvdl a->au_id = AUX_pagesz;
146 1.1 fvdl a->au_v = NBPG;
147 1.1 fvdl a++;
148 1.1 fvdl
149 1.1 fvdl a->au_id = AUX_base;
150 1.1 fvdl a->au_v = ap->arg_interp;
151 1.1 fvdl a++;
152 1.1 fvdl
153 1.1 fvdl a->au_id = AUX_flags;
154 1.1 fvdl a->au_v = 0;
155 1.1 fvdl a++;
156 1.1 fvdl
157 1.1 fvdl a->au_id = AUX_entry;
158 1.1 fvdl a->au_v = ap->arg_entry;
159 1.1 fvdl a++;
160 1.1 fvdl
161 1.1 fvdl a->au_id = AUX_null;
162 1.1 fvdl a->au_v = 0;
163 1.1 fvdl a++;
164 1.1 fvdl
165 1.1 fvdl free((char *) ap, M_TEMP);
166 1.4 fvdl len = ELF_AUX_ENTRIES * sizeof (AuxInfo);
167 1.4 fvdl if (copyout(ai, stack, len))
168 1.4 fvdl return NULL;
169 1.4 fvdl stack += len;
170 1.1 fvdl }
171 1.4 fvdl return stack;
172 1.1 fvdl }
173 1.1 fvdl
174 1.1 fvdl /*
175 1.1 fvdl * elf_check_header():
176 1.1 fvdl *
177 1.1 fvdl * Check header for validity; return 0 of ok ENOEXEC if error
178 1.1 fvdl *
179 1.1 fvdl * XXX machine type needs to be moved to <machine/param.h> so
180 1.1 fvdl * just one comparison can be done. Unfortunately, there is both
181 1.1 fvdl * em_486 and em_386, so this would not work on the i386.
182 1.1 fvdl */
183 1.1 fvdl int
184 1.1 fvdl elf_check_header(eh, type)
185 1.1 fvdl Elf32_Ehdr *eh;
186 1.1 fvdl int type;
187 1.1 fvdl {
188 1.3 thorpej
189 1.3 thorpej if (bcmp(eh->e_ident, Elf32_e_ident, Elf32_e_siz) != 0)
190 1.1 fvdl return ENOEXEC;
191 1.1 fvdl
192 1.1 fvdl switch (eh->e_machine) {
193 1.1 fvdl /* XXX */
194 1.1 fvdl #ifdef i386
195 1.1 fvdl case Elf32_em_386:
196 1.1 fvdl case Elf32_em_486:
197 1.1 fvdl #endif
198 1.1 fvdl #ifdef sparc
199 1.1 fvdl case Elf32_em_sparc:
200 1.1 fvdl #endif
201 1.8 christos #ifdef mips
202 1.8 christos case Elf32_em_mips:
203 1.8 christos #endif
204 1.1 fvdl break;
205 1.1 fvdl
206 1.1 fvdl default:
207 1.1 fvdl return ENOEXEC;
208 1.1 fvdl }
209 1.1 fvdl
210 1.1 fvdl if (eh->e_type != type)
211 1.1 fvdl return ENOEXEC;
212 1.1 fvdl
213 1.1 fvdl return 0;
214 1.1 fvdl }
215 1.1 fvdl
216 1.1 fvdl /*
217 1.1 fvdl * elf_load_psection():
218 1.1 fvdl *
219 1.1 fvdl * Load a psection at the appropriate address
220 1.1 fvdl */
221 1.1 fvdl static void
222 1.1 fvdl elf_load_psection(vcset, vp, ph, addr, size, prot)
223 1.1 fvdl struct exec_vmcmd_set *vcset;
224 1.1 fvdl struct vnode *vp;
225 1.1 fvdl Elf32_Phdr *ph;
226 1.1 fvdl u_long *addr;
227 1.1 fvdl u_long *size;
228 1.1 fvdl int *prot;
229 1.1 fvdl {
230 1.8 christos u_long uaddr, msize, psize, rm, rf;
231 1.1 fvdl long diff, offset;
232 1.1 fvdl
233 1.1 fvdl /*
234 1.1 fvdl * If the user specified an address, then we load there.
235 1.1 fvdl */
236 1.1 fvdl if (*addr != ELF32_NO_ADDR) {
237 1.1 fvdl if (ph->p_align > 1) {
238 1.1 fvdl *addr = ELF_ALIGN(*addr + ph->p_align, ph->p_align);
239 1.1 fvdl uaddr = ELF_ALIGN(ph->p_vaddr, ph->p_align);
240 1.1 fvdl } else
241 1.1 fvdl uaddr = ph->p_vaddr;
242 1.1 fvdl diff = ph->p_vaddr - uaddr;
243 1.1 fvdl } else {
244 1.1 fvdl *addr = uaddr = ph->p_vaddr;
245 1.1 fvdl if (ph->p_align > 1)
246 1.1 fvdl *addr = ELF_ALIGN(uaddr, ph->p_align);
247 1.1 fvdl diff = uaddr - *addr;
248 1.1 fvdl }
249 1.1 fvdl
250 1.1 fvdl *prot |= (ph->p_flags & Elf32_pf_r) ? VM_PROT_READ : 0;
251 1.1 fvdl *prot |= (ph->p_flags & Elf32_pf_w) ? VM_PROT_WRITE : 0;
252 1.1 fvdl *prot |= (ph->p_flags & Elf32_pf_x) ? VM_PROT_EXECUTE : 0;
253 1.1 fvdl
254 1.1 fvdl offset = ph->p_offset - diff;
255 1.1 fvdl *size = ph->p_filesz + diff;
256 1.1 fvdl msize = ph->p_memsz + diff;
257 1.8 christos psize = round_page(*size);
258 1.1 fvdl
259 1.8 christos if ((ph->p_flags & Elf32_pf_w) != 0) {
260 1.8 christos /*
261 1.8 christos * Because the pagedvn pager can't handle zero fill of the last
262 1.8 christos * data page if it's not page aligned we map the last page
263 1.8 christos * readvn.
264 1.8 christos */
265 1.8 christos psize = trunc_page(*size);
266 1.8 christos NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
267 1.8 christos offset, *prot);
268 1.8 christos if(psize != *size)
269 1.8 christos NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
270 1.8 christos *addr + psize, vp, offset + psize, *prot);
271 1.8 christos }
272 1.8 christos else
273 1.8 christos NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
274 1.8 christos offset, *prot);
275 1.1 fvdl
276 1.1 fvdl /*
277 1.1 fvdl * Check if we need to extend the size of the segment
278 1.1 fvdl */
279 1.1 fvdl rm = round_page(*addr + msize);
280 1.1 fvdl rf = round_page(*addr + *size);
281 1.1 fvdl
282 1.1 fvdl if (rm != rf) {
283 1.1 fvdl NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0, *prot);
284 1.1 fvdl *size = msize;
285 1.1 fvdl }
286 1.1 fvdl }
287 1.1 fvdl
288 1.1 fvdl /*
289 1.1 fvdl * elf_read_from():
290 1.1 fvdl *
291 1.1 fvdl * Read from vnode into buffer at offset.
292 1.1 fvdl */
293 1.7 christos int
294 1.1 fvdl elf_read_from(p, vp, off, buf, size)
295 1.1 fvdl struct vnode *vp;
296 1.1 fvdl u_long off;
297 1.1 fvdl struct proc *p;
298 1.1 fvdl caddr_t buf;
299 1.1 fvdl int size;
300 1.1 fvdl {
301 1.1 fvdl int error;
302 1.1 fvdl int resid;
303 1.1 fvdl
304 1.1 fvdl if ((error = vn_rdwr(UIO_READ, vp, buf, size,
305 1.1 fvdl off, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
306 1.1 fvdl &resid, p)) != 0)
307 1.1 fvdl return error;
308 1.1 fvdl /*
309 1.1 fvdl * See if we got all of it
310 1.1 fvdl */
311 1.1 fvdl if (resid != 0)
312 1.4 fvdl return ENOEXEC;
313 1.1 fvdl return 0;
314 1.1 fvdl }
315 1.1 fvdl
316 1.1 fvdl /*
317 1.1 fvdl * elf_load_file():
318 1.1 fvdl *
319 1.1 fvdl * Load a file (interpreter/library) pointed to by path
320 1.1 fvdl * [stolen from coff_load_shlib()]. Made slightly generic
321 1.1 fvdl * so it might be used externally.
322 1.1 fvdl */
323 1.1 fvdl int
324 1.1 fvdl elf_load_file(p, path, vcset, entry, ap, last)
325 1.1 fvdl struct proc *p;
326 1.1 fvdl char *path;
327 1.1 fvdl struct exec_vmcmd_set *vcset;
328 1.1 fvdl u_long *entry;
329 1.1 fvdl struct elf_args *ap;
330 1.1 fvdl u_long *last;
331 1.1 fvdl {
332 1.1 fvdl int error, i;
333 1.1 fvdl struct nameidata nd;
334 1.1 fvdl Elf32_Ehdr eh;
335 1.1 fvdl Elf32_Phdr *ph = NULL;
336 1.1 fvdl u_long phsize;
337 1.1 fvdl char *bp = NULL;
338 1.1 fvdl u_long addr = *last;
339 1.1 fvdl
340 1.1 fvdl bp = path;
341 1.1 fvdl /*
342 1.1 fvdl * 1. open file
343 1.1 fvdl * 2. read filehdr
344 1.1 fvdl * 3. map text, data, and bss out of it using VM_*
345 1.1 fvdl */
346 1.1 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
347 1.1 fvdl if ((error = namei(&nd)) != 0) {
348 1.1 fvdl return error;
349 1.1 fvdl }
350 1.1 fvdl if ((error = elf_read_from(p, nd.ni_vp, 0, (caddr_t) &eh,
351 1.1 fvdl sizeof(eh))) != 0)
352 1.1 fvdl goto bad;
353 1.1 fvdl
354 1.1 fvdl if ((error = elf_check_header(&eh, Elf32_et_dyn)) != 0)
355 1.1 fvdl goto bad;
356 1.1 fvdl
357 1.1 fvdl phsize = eh.e_phnum * sizeof(Elf32_Phdr);
358 1.1 fvdl ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
359 1.1 fvdl
360 1.1 fvdl if ((error = elf_read_from(p, nd.ni_vp, eh.e_phoff,
361 1.1 fvdl (caddr_t) ph, phsize)) != 0)
362 1.1 fvdl goto bad;
363 1.1 fvdl
364 1.1 fvdl /*
365 1.1 fvdl * Load all the necessary sections
366 1.1 fvdl */
367 1.1 fvdl for (i = 0; i < eh.e_phnum; i++) {
368 1.1 fvdl u_long size = 0;
369 1.1 fvdl int prot = 0;
370 1.1 fvdl
371 1.1 fvdl switch (ph[i].p_type) {
372 1.1 fvdl case Elf32_pt_load:
373 1.1 fvdl elf_load_psection(vcset, nd.ni_vp, &ph[i], &addr,
374 1.1 fvdl &size, &prot);
375 1.4 fvdl /* If entry is within this section it must be text */
376 1.4 fvdl if (eh.e_entry >= ph[i].p_vaddr &&
377 1.4 fvdl eh.e_entry < (ph[i].p_vaddr + size)) {
378 1.1 fvdl *entry = addr + eh.e_entry;
379 1.1 fvdl ap->arg_interp = addr;
380 1.1 fvdl }
381 1.1 fvdl addr += size;
382 1.1 fvdl break;
383 1.1 fvdl
384 1.1 fvdl case Elf32_pt_dynamic:
385 1.1 fvdl case Elf32_pt_phdr:
386 1.1 fvdl case Elf32_pt_note:
387 1.1 fvdl break;
388 1.1 fvdl
389 1.1 fvdl default:
390 1.1 fvdl break;
391 1.1 fvdl }
392 1.1 fvdl }
393 1.1 fvdl
394 1.1 fvdl bad:
395 1.1 fvdl if (ph != NULL)
396 1.1 fvdl free((char *) ph, M_TEMP);
397 1.1 fvdl
398 1.1 fvdl *last = addr;
399 1.1 fvdl vrele(nd.ni_vp);
400 1.1 fvdl return error;
401 1.1 fvdl }
402 1.1 fvdl
403 1.1 fvdl /*
404 1.1 fvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
405 1.1 fvdl *
406 1.1 fvdl * First, set of the various offsets/lengths in the exec package.
407 1.1 fvdl *
408 1.1 fvdl * Then, mark the text image busy (so it can be demand paged) or error
409 1.1 fvdl * out if this is not possible. Finally, set up vmcmds for the
410 1.1 fvdl * text, data, bss, and stack segments.
411 1.1 fvdl */
412 1.1 fvdl int
413 1.1 fvdl exec_elf_makecmds(p, epp)
414 1.1 fvdl struct proc *p;
415 1.1 fvdl struct exec_package *epp;
416 1.1 fvdl {
417 1.1 fvdl Elf32_Ehdr *eh = epp->ep_hdr;
418 1.1 fvdl Elf32_Phdr *ph, *pp;
419 1.4 fvdl Elf32_Addr phdr = 0;
420 1.4 fvdl int error, i, n, nload;
421 1.1 fvdl char interp[MAXPATHLEN];
422 1.1 fvdl u_long pos = 0, phsize;
423 1.1 fvdl
424 1.1 fvdl if (epp->ep_hdrvalid < sizeof(Elf32_Ehdr))
425 1.1 fvdl return ENOEXEC;
426 1.1 fvdl
427 1.1 fvdl if (elf_check_header(eh, Elf32_et_exec))
428 1.1 fvdl return ENOEXEC;
429 1.1 fvdl
430 1.1 fvdl /*
431 1.1 fvdl * check if vnode is in open for writing, because we want to
432 1.1 fvdl * demand-page out of it. if it is, don't do it, for various
433 1.1 fvdl * reasons
434 1.1 fvdl */
435 1.1 fvdl if (epp->ep_vp->v_writecount != 0) {
436 1.1 fvdl #ifdef DIAGNOSTIC
437 1.1 fvdl if (epp->ep_vp->v_flag & VTEXT)
438 1.1 fvdl panic("exec: a VTEXT vnode has writecount != 0\n");
439 1.1 fvdl #endif
440 1.1 fvdl return ETXTBSY;
441 1.1 fvdl }
442 1.1 fvdl /*
443 1.1 fvdl * Allocate space to hold all the program headers, and read them
444 1.1 fvdl * from the file
445 1.1 fvdl */
446 1.1 fvdl phsize = eh->e_phnum * sizeof(Elf32_Phdr);
447 1.1 fvdl ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
448 1.1 fvdl
449 1.1 fvdl if ((error = elf_read_from(p, epp->ep_vp, eh->e_phoff,
450 1.8 christos (caddr_t) ph, phsize)) != 0)
451 1.1 fvdl goto bad;
452 1.1 fvdl
453 1.1 fvdl epp->ep_tsize = ELF32_NO_ADDR;
454 1.1 fvdl epp->ep_dsize = ELF32_NO_ADDR;
455 1.1 fvdl
456 1.1 fvdl interp[0] = '\0';
457 1.1 fvdl
458 1.1 fvdl for (i = 0; i < eh->e_phnum; i++) {
459 1.1 fvdl pp = &ph[i];
460 1.1 fvdl if (pp->p_type == Elf32_pt_interp) {
461 1.1 fvdl if (pp->p_filesz >= sizeof(interp))
462 1.1 fvdl goto bad;
463 1.1 fvdl if ((error = elf_read_from(p, epp->ep_vp, pp->p_offset,
464 1.1 fvdl (caddr_t) interp, pp->p_filesz)) != 0)
465 1.1 fvdl goto bad;
466 1.1 fvdl break;
467 1.1 fvdl }
468 1.1 fvdl }
469 1.1 fvdl
470 1.8 christos /*
471 1.8 christos * Setup things for native emulation.
472 1.8 christos */
473 1.8 christos epp->ep_emul = &emul_elf;
474 1.8 christos pos = ELF32_NO_ADDR;
475 1.8 christos
476 1.1 fvdl /*
477 1.1 fvdl * On the same architecture, we may be emulating different systems.
478 1.1 fvdl * See which one will accept this executable. This currently only
479 1.1 fvdl * applies to Linux and SVR4 on the i386.
480 1.1 fvdl *
481 1.1 fvdl * Probe functions would normally see if the interpreter (if any)
482 1.1 fvdl * exists. Emulation packages may possibly replace the interpreter in
483 1.1 fvdl * interp[] with a changed path (/emul/xxx/<path>), and also
484 1.1 fvdl * set the ep_emul field in the exec package structure.
485 1.1 fvdl */
486 1.1 fvdl if ((n = sizeof elf_probe_funcs / sizeof elf_probe_funcs[0])) {
487 1.1 fvdl error = ENOEXEC;
488 1.1 fvdl for (i = 0; i < n && error; i++)
489 1.7 christos error = elf_probe_funcs[i](p, epp, eh, interp, &pos);
490 1.1 fvdl
491 1.8 christos #ifdef notyet
492 1.8 christos /*
493 1.8 christos * We should really use a signature in our native binaries
494 1.8 christos * and have our own probe function for matching binaries,
495 1.8 christos * before trying the emulations. For now, if the emulation
496 1.8 christos * probes failed we default to native.
497 1.8 christos */
498 1.1 fvdl if (error)
499 1.1 fvdl goto bad;
500 1.8 christos #endif
501 1.1 fvdl }
502 1.1 fvdl
503 1.1 fvdl /*
504 1.1 fvdl * Load all the necessary sections
505 1.1 fvdl */
506 1.4 fvdl for (i = nload = 0; i < eh->e_phnum; i++) {
507 1.1 fvdl u_long addr = ELF32_NO_ADDR, size = 0;
508 1.1 fvdl int prot = 0;
509 1.1 fvdl
510 1.1 fvdl pp = &ph[i];
511 1.1 fvdl
512 1.1 fvdl switch (ph[i].p_type) {
513 1.1 fvdl case Elf32_pt_load:
514 1.4 fvdl /*
515 1.4 fvdl * XXX
516 1.4 fvdl * Can handle only 2 sections: text and data
517 1.4 fvdl */
518 1.4 fvdl if (nload++ == 2)
519 1.4 fvdl goto bad;
520 1.1 fvdl elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
521 1.1 fvdl &ph[i], &addr, &size, &prot);
522 1.4 fvdl /*
523 1.4 fvdl * Decide whether it's text or data by looking
524 1.4 fvdl * at the entry point.
525 1.4 fvdl */
526 1.4 fvdl if (eh->e_entry >= addr && eh->e_entry < (addr + size)){
527 1.4 fvdl epp->ep_taddr = addr;
528 1.4 fvdl epp->ep_tsize = size;
529 1.4 fvdl } else {
530 1.4 fvdl epp->ep_daddr = addr;
531 1.4 fvdl epp->ep_dsize = size;
532 1.4 fvdl }
533 1.1 fvdl break;
534 1.1 fvdl
535 1.1 fvdl case Elf32_pt_shlib:
536 1.1 fvdl error = ENOEXEC;
537 1.1 fvdl goto bad;
538 1.1 fvdl
539 1.1 fvdl case Elf32_pt_interp:
540 1.1 fvdl /* Already did this one */
541 1.1 fvdl case Elf32_pt_dynamic:
542 1.1 fvdl case Elf32_pt_note:
543 1.1 fvdl break;
544 1.1 fvdl
545 1.4 fvdl case Elf32_pt_phdr:
546 1.4 fvdl /* Note address of program headers (in text segment) */
547 1.4 fvdl phdr = pp->p_vaddr;
548 1.7 christos break;
549 1.4 fvdl
550 1.1 fvdl default:
551 1.1 fvdl /*
552 1.1 fvdl * Not fatal, we don't need to understand everything
553 1.1 fvdl * :-)
554 1.1 fvdl */
555 1.1 fvdl break;
556 1.1 fvdl }
557 1.1 fvdl }
558 1.5 fvdl
559 1.5 fvdl /*
560 1.5 fvdl * If no position to load the interpreter was set by a probe
561 1.5 fvdl * function, pick the same address that a non-fixed mmap(0, ..)
562 1.5 fvdl * would (i.e. something safely out of the way).
563 1.5 fvdl */
564 1.8 christos if (pos == ELF32_NO_ADDR && epp->ep_emul == &emul_elf)
565 1.5 fvdl pos = round_page(epp->ep_daddr + MAXDSIZ);
566 1.1 fvdl
567 1.1 fvdl /*
568 1.1 fvdl * Check if we found a dynamically linked binary and arrange to load
569 1.1 fvdl * it's interpreter
570 1.1 fvdl */
571 1.1 fvdl if (interp[0]) {
572 1.1 fvdl struct elf_args *ap;
573 1.1 fvdl
574 1.1 fvdl ap = (struct elf_args *) malloc(sizeof(struct elf_args),
575 1.1 fvdl M_TEMP, M_WAITOK);
576 1.1 fvdl if ((error = elf_load_file(p, interp, &epp->ep_vmcmds,
577 1.1 fvdl &epp->ep_entry, ap, &pos)) != 0) {
578 1.1 fvdl free((char *) ap, M_TEMP);
579 1.1 fvdl goto bad;
580 1.1 fvdl }
581 1.1 fvdl pos += phsize;
582 1.4 fvdl ap->arg_phaddr = phdr;
583 1.1 fvdl
584 1.1 fvdl ap->arg_phentsize = eh->e_phentsize;
585 1.1 fvdl ap->arg_phnum = eh->e_phnum;
586 1.1 fvdl ap->arg_entry = eh->e_entry;
587 1.1 fvdl
588 1.1 fvdl epp->ep_emul_arg = ap;
589 1.1 fvdl } else
590 1.1 fvdl epp->ep_entry = eh->e_entry;
591 1.1 fvdl
592 1.8 christos #ifdef ELF_MAP_PAGE_ZERO
593 1.8 christos /* Dell SVR4 maps page zero, yeuch! */
594 1.8 christos NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
595 1.8 christos VM_PROT_READ);
596 1.8 christos #endif
597 1.1 fvdl free((char *) ph, M_TEMP);
598 1.1 fvdl epp->ep_vp->v_flag |= VTEXT;
599 1.1 fvdl return exec_aout_setup_stack(p, epp);
600 1.1 fvdl
601 1.1 fvdl bad:
602 1.1 fvdl free((char *) ph, M_TEMP);
603 1.1 fvdl kill_vmcmds(&epp->ep_vmcmds);
604 1.1 fvdl return ENOEXEC;
605 1.1 fvdl }
606