exec_elf.c revision 1.3 1 1.3 thorpej /* $NetBSD: exec_elf.c,v 1.3 1995/09/16 00:28:08 thorpej 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.1 fvdl
41 1.1 fvdl #include <sys/mman.h>
42 1.1 fvdl #include <vm/vm.h>
43 1.1 fvdl #include <vm/vm_param.h>
44 1.1 fvdl #include <vm/vm_map.h>
45 1.1 fvdl
46 1.1 fvdl #include <machine/cpu.h>
47 1.1 fvdl #include <machine/reg.h>
48 1.1 fvdl #include <machine/exec.h>
49 1.1 fvdl
50 1.1 fvdl #ifdef COMPAT_LINUX
51 1.1 fvdl #include <compat/linux/linux_exec.h>
52 1.1 fvdl #endif
53 1.1 fvdl
54 1.1 fvdl #ifdef COMPAT_SVR4
55 1.1 fvdl #include <compat/svr4/svr4_exec.h>
56 1.1 fvdl #endif
57 1.1 fvdl
58 1.1 fvdl int (*elf_probe_funcs[])() = {
59 1.1 fvdl #ifdef COMPAT_SVR4
60 1.1 fvdl svr4_elf_probe,
61 1.1 fvdl #endif
62 1.1 fvdl #ifdef COMPAT_LINUX
63 1.1 fvdl linux_elf_probe
64 1.1 fvdl #endif
65 1.1 fvdl };
66 1.1 fvdl
67 1.1 fvdl static int elf_set_segment __P((struct exec_package *, u_long, u_long,
68 1.1 fvdl int));
69 1.1 fvdl static int elf_read_from __P((struct proc *, struct vnode *, u_long,
70 1.1 fvdl caddr_t, int));
71 1.1 fvdl static void elf_load_psection __P((struct exec_vmcmd_set *,
72 1.1 fvdl struct vnode *, Elf32_Phdr *, u_long *, u_long *, int *));
73 1.1 fvdl
74 1.1 fvdl #define ELF_ALIGN(a, b) ((a) & ~((b) - 1))
75 1.1 fvdl
76 1.1 fvdl /*
77 1.1 fvdl * Copy arguments onto the stack in the normal way, but add some
78 1.1 fvdl * extra information in case of dynamic binding.
79 1.1 fvdl */
80 1.1 fvdl void *
81 1.1 fvdl elf_copyargs(pack, arginfo, stack, argp)
82 1.1 fvdl struct exec_package *pack;
83 1.1 fvdl struct ps_strings *arginfo;
84 1.1 fvdl void *stack;
85 1.1 fvdl void *argp;
86 1.1 fvdl {
87 1.1 fvdl char **cpp = stack;
88 1.1 fvdl char *dp, *sp;
89 1.1 fvdl size_t len;
90 1.1 fvdl void *nullp = NULL;
91 1.1 fvdl int argc = arginfo->ps_nargvstr;
92 1.1 fvdl int envc = arginfo->ps_nenvstr;
93 1.1 fvdl AuxInfo *a;
94 1.1 fvdl struct elf_args *ap;
95 1.1 fvdl
96 1.1 fvdl if (copyout(&argc, cpp++, sizeof(argc)))
97 1.1 fvdl return NULL;
98 1.1 fvdl
99 1.1 fvdl dp = (char *) (cpp + argc + envc + 2 + pack->ep_emul->e_arglen);
100 1.1 fvdl sp = argp;
101 1.1 fvdl
102 1.1 fvdl /* XXX don't copy them out, remap them! */
103 1.1 fvdl arginfo->ps_argvstr = cpp; /* remember location of argv for later */
104 1.1 fvdl
105 1.1 fvdl for (; --argc >= 0; sp += len, dp += len)
106 1.1 fvdl if (copyout(&dp, cpp++, sizeof(dp)) ||
107 1.1 fvdl copyoutstr(sp, dp, ARG_MAX, &len))
108 1.1 fvdl return NULL;
109 1.1 fvdl
110 1.1 fvdl if (copyout(&nullp, cpp++, sizeof(nullp)))
111 1.1 fvdl return NULL;
112 1.1 fvdl
113 1.1 fvdl arginfo->ps_envstr = cpp; /* remember location of envp for later */
114 1.1 fvdl
115 1.1 fvdl for (; --envc >= 0; sp += len, dp += len)
116 1.1 fvdl if (copyout(&dp, cpp++, sizeof(dp)) ||
117 1.1 fvdl copyoutstr(sp, dp, ARG_MAX, &len))
118 1.1 fvdl return NULL;
119 1.1 fvdl
120 1.1 fvdl if (copyout(&nullp, cpp++, sizeof(nullp)))
121 1.1 fvdl return NULL;
122 1.1 fvdl
123 1.1 fvdl /*
124 1.1 fvdl * Push extra arguments on the stack needed by dynamically
125 1.1 fvdl * linked binaries
126 1.1 fvdl */
127 1.1 fvdl a = (AuxInfo *) cpp;
128 1.1 fvdl if ((ap = (struct elf_args *) pack->ep_emul_arg)) {
129 1.1 fvdl
130 1.1 fvdl a->au_id = AUX_phdr;
131 1.1 fvdl a->au_v = ap->arg_phaddr;
132 1.1 fvdl a++;
133 1.1 fvdl
134 1.1 fvdl a->au_id = AUX_phent;
135 1.1 fvdl a->au_v = ap->arg_phentsize;
136 1.1 fvdl a++;
137 1.1 fvdl
138 1.1 fvdl a->au_id = AUX_phnum;
139 1.1 fvdl a->au_v = ap->arg_phnum;
140 1.1 fvdl a++;
141 1.1 fvdl
142 1.1 fvdl a->au_id = AUX_pagesz;
143 1.1 fvdl a->au_v = NBPG;
144 1.1 fvdl a++;
145 1.1 fvdl
146 1.1 fvdl a->au_id = AUX_base;
147 1.1 fvdl a->au_v = ap->arg_interp;
148 1.1 fvdl a++;
149 1.1 fvdl
150 1.1 fvdl a->au_id = AUX_flags;
151 1.1 fvdl a->au_v = 0;
152 1.1 fvdl a++;
153 1.1 fvdl
154 1.1 fvdl a->au_id = AUX_entry;
155 1.1 fvdl a->au_v = ap->arg_entry;
156 1.1 fvdl a++;
157 1.1 fvdl
158 1.1 fvdl a->au_id = AUX_null;
159 1.1 fvdl a->au_v = 0;
160 1.1 fvdl a++;
161 1.1 fvdl
162 1.1 fvdl free((char *) ap, M_TEMP);
163 1.1 fvdl }
164 1.1 fvdl return a;
165 1.1 fvdl }
166 1.1 fvdl
167 1.1 fvdl /*
168 1.1 fvdl * elf_check_header():
169 1.1 fvdl *
170 1.1 fvdl * Check header for validity; return 0 of ok ENOEXEC if error
171 1.1 fvdl *
172 1.1 fvdl * XXX machine type needs to be moved to <machine/param.h> so
173 1.1 fvdl * just one comparison can be done. Unfortunately, there is both
174 1.1 fvdl * em_486 and em_386, so this would not work on the i386.
175 1.1 fvdl */
176 1.1 fvdl int
177 1.1 fvdl elf_check_header(eh, type)
178 1.1 fvdl Elf32_Ehdr *eh;
179 1.1 fvdl int type;
180 1.1 fvdl {
181 1.3 thorpej
182 1.3 thorpej if (bcmp(eh->e_ident, Elf32_e_ident, Elf32_e_siz) != 0)
183 1.1 fvdl return ENOEXEC;
184 1.1 fvdl
185 1.1 fvdl switch (eh->e_machine) {
186 1.1 fvdl /* XXX */
187 1.1 fvdl #ifdef i386
188 1.1 fvdl case Elf32_em_386:
189 1.1 fvdl case Elf32_em_486:
190 1.1 fvdl #endif
191 1.1 fvdl #ifdef sparc
192 1.1 fvdl case Elf32_em_sparc:
193 1.1 fvdl #endif
194 1.1 fvdl break;
195 1.1 fvdl
196 1.1 fvdl default:
197 1.1 fvdl return ENOEXEC;
198 1.1 fvdl }
199 1.1 fvdl
200 1.1 fvdl if (eh->e_type != type)
201 1.1 fvdl return ENOEXEC;
202 1.1 fvdl
203 1.1 fvdl return 0;
204 1.1 fvdl }
205 1.1 fvdl
206 1.1 fvdl /*
207 1.1 fvdl * elf_load_psection():
208 1.1 fvdl *
209 1.1 fvdl * Load a psection at the appropriate address
210 1.1 fvdl */
211 1.1 fvdl static void
212 1.1 fvdl elf_load_psection(vcset, vp, ph, addr, size, prot)
213 1.1 fvdl struct exec_vmcmd_set *vcset;
214 1.1 fvdl struct vnode *vp;
215 1.1 fvdl Elf32_Phdr *ph;
216 1.1 fvdl u_long *addr;
217 1.1 fvdl u_long *size;
218 1.1 fvdl int *prot;
219 1.1 fvdl {
220 1.1 fvdl u_long uaddr, msize, rm, rf;
221 1.1 fvdl long diff, offset;
222 1.1 fvdl
223 1.1 fvdl /*
224 1.1 fvdl * If the user specified an address, then we load there.
225 1.1 fvdl */
226 1.1 fvdl if (*addr != ELF32_NO_ADDR) {
227 1.1 fvdl if (ph->p_align > 1) {
228 1.1 fvdl *addr = ELF_ALIGN(*addr + ph->p_align, ph->p_align);
229 1.1 fvdl uaddr = ELF_ALIGN(ph->p_vaddr, ph->p_align);
230 1.1 fvdl } else
231 1.1 fvdl uaddr = ph->p_vaddr;
232 1.1 fvdl diff = ph->p_vaddr - uaddr;
233 1.1 fvdl } else {
234 1.1 fvdl *addr = uaddr = ph->p_vaddr;
235 1.1 fvdl if (ph->p_align > 1)
236 1.1 fvdl *addr = ELF_ALIGN(uaddr, ph->p_align);
237 1.1 fvdl diff = uaddr - *addr;
238 1.1 fvdl }
239 1.1 fvdl
240 1.1 fvdl *prot |= (ph->p_flags & Elf32_pf_r) ? VM_PROT_READ : 0;
241 1.1 fvdl *prot |= (ph->p_flags & Elf32_pf_w) ? VM_PROT_WRITE : 0;
242 1.1 fvdl *prot |= (ph->p_flags & Elf32_pf_x) ? VM_PROT_EXECUTE : 0;
243 1.1 fvdl
244 1.1 fvdl offset = ph->p_offset - diff;
245 1.1 fvdl *size = ph->p_filesz + diff;
246 1.1 fvdl msize = ph->p_memsz + diff;
247 1.1 fvdl
248 1.1 fvdl NEW_VMCMD(vcset, vmcmd_map_readvn, *size, *addr, vp, offset, *prot);
249 1.1 fvdl
250 1.1 fvdl /*
251 1.1 fvdl * Check if we need to extend the size of the segment
252 1.1 fvdl */
253 1.1 fvdl rm = round_page(*addr + msize);
254 1.1 fvdl rf = round_page(*addr + *size);
255 1.1 fvdl
256 1.1 fvdl if (rm != rf) {
257 1.1 fvdl NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0, *prot);
258 1.1 fvdl *size = msize;
259 1.1 fvdl }
260 1.1 fvdl }
261 1.1 fvdl
262 1.1 fvdl /*
263 1.1 fvdl * elf_set_segment():
264 1.1 fvdl *
265 1.1 fvdl * Decide if the segment is text or data, depending on the protection
266 1.1 fvdl * and set it appropriately
267 1.1 fvdl */
268 1.1 fvdl static int
269 1.1 fvdl elf_set_segment(epp, vaddr, size, prot)
270 1.1 fvdl struct exec_package *epp;
271 1.1 fvdl u_long vaddr;
272 1.1 fvdl u_long size;
273 1.1 fvdl int prot;
274 1.1 fvdl {
275 1.1 fvdl /*
276 1.1 fvdl * Kludge: Unfortunately the current implementation of
277 1.1 fvdl * exec package assumes a single text and data segment.
278 1.1 fvdl * In Elf we can have more, but here we limit ourselves
279 1.1 fvdl * to two and hope :-(
280 1.1 fvdl * We also assume that the text is r-x, and data is rwx or rw-.
281 1.1 fvdl */
282 1.1 fvdl switch (prot) {
283 1.1 fvdl case (VM_PROT_READ | VM_PROT_EXECUTE):
284 1.1 fvdl if (epp->ep_tsize != ELF32_NO_ADDR)
285 1.1 fvdl return ENOEXEC;
286 1.1 fvdl epp->ep_taddr = vaddr;
287 1.1 fvdl epp->ep_tsize = size;
288 1.1 fvdl break;
289 1.1 fvdl
290 1.1 fvdl case (VM_PROT_READ | VM_PROT_WRITE):
291 1.1 fvdl case (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE):
292 1.1 fvdl if (epp->ep_dsize != ELF32_NO_ADDR)
293 1.1 fvdl return ENOEXEC;
294 1.1 fvdl epp->ep_daddr = vaddr;
295 1.1 fvdl epp->ep_dsize = size;
296 1.1 fvdl break;
297 1.1 fvdl
298 1.1 fvdl default:
299 1.1 fvdl return ENOEXEC;
300 1.1 fvdl }
301 1.1 fvdl return 0;
302 1.1 fvdl }
303 1.1 fvdl
304 1.1 fvdl /*
305 1.1 fvdl * elf_read_from():
306 1.1 fvdl *
307 1.1 fvdl * Read from vnode into buffer at offset.
308 1.1 fvdl */
309 1.1 fvdl static int
310 1.1 fvdl elf_read_from(p, vp, off, buf, size)
311 1.1 fvdl struct vnode *vp;
312 1.1 fvdl u_long off;
313 1.1 fvdl struct proc *p;
314 1.1 fvdl caddr_t buf;
315 1.1 fvdl int size;
316 1.1 fvdl {
317 1.1 fvdl int error;
318 1.1 fvdl int resid;
319 1.1 fvdl
320 1.1 fvdl if ((error = vn_rdwr(UIO_READ, vp, buf, size,
321 1.1 fvdl off, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
322 1.1 fvdl &resid, p)) != 0)
323 1.1 fvdl return error;
324 1.1 fvdl /*
325 1.1 fvdl * See if we got all of it
326 1.1 fvdl */
327 1.1 fvdl if (resid != 0)
328 1.1 fvdl return error;
329 1.1 fvdl return 0;
330 1.1 fvdl }
331 1.1 fvdl
332 1.1 fvdl /*
333 1.1 fvdl * elf_load_file():
334 1.1 fvdl *
335 1.1 fvdl * Load a file (interpreter/library) pointed to by path
336 1.1 fvdl * [stolen from coff_load_shlib()]. Made slightly generic
337 1.1 fvdl * so it might be used externally.
338 1.1 fvdl */
339 1.1 fvdl int
340 1.1 fvdl elf_load_file(p, path, vcset, entry, ap, last)
341 1.1 fvdl struct proc *p;
342 1.1 fvdl char *path;
343 1.1 fvdl struct exec_vmcmd_set *vcset;
344 1.1 fvdl u_long *entry;
345 1.1 fvdl struct elf_args *ap;
346 1.1 fvdl u_long *last;
347 1.1 fvdl {
348 1.1 fvdl int error, i;
349 1.1 fvdl struct nameidata nd;
350 1.1 fvdl Elf32_Ehdr eh;
351 1.1 fvdl Elf32_Phdr *ph = NULL;
352 1.1 fvdl u_long phsize;
353 1.1 fvdl char *bp = NULL;
354 1.1 fvdl u_long addr = *last;
355 1.1 fvdl
356 1.1 fvdl bp = path;
357 1.1 fvdl /*
358 1.1 fvdl * 1. open file
359 1.1 fvdl * 2. read filehdr
360 1.1 fvdl * 3. map text, data, and bss out of it using VM_*
361 1.1 fvdl */
362 1.1 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
363 1.1 fvdl if ((error = namei(&nd)) != 0) {
364 1.1 fvdl return error;
365 1.1 fvdl }
366 1.1 fvdl if ((error = elf_read_from(p, nd.ni_vp, 0, (caddr_t) &eh,
367 1.1 fvdl sizeof(eh))) != 0)
368 1.1 fvdl goto bad;
369 1.1 fvdl
370 1.1 fvdl if ((error = elf_check_header(&eh, Elf32_et_dyn)) != 0)
371 1.1 fvdl goto bad;
372 1.1 fvdl
373 1.1 fvdl phsize = eh.e_phnum * sizeof(Elf32_Phdr);
374 1.1 fvdl ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
375 1.1 fvdl
376 1.1 fvdl if ((error = elf_read_from(p, nd.ni_vp, eh.e_phoff,
377 1.1 fvdl (caddr_t) ph, phsize)) != 0)
378 1.1 fvdl goto bad;
379 1.1 fvdl
380 1.1 fvdl /*
381 1.1 fvdl * Load all the necessary sections
382 1.1 fvdl */
383 1.1 fvdl for (i = 0; i < eh.e_phnum; i++) {
384 1.1 fvdl u_long size = 0;
385 1.1 fvdl int prot = 0;
386 1.1 fvdl
387 1.1 fvdl switch (ph[i].p_type) {
388 1.1 fvdl case Elf32_pt_load:
389 1.1 fvdl elf_load_psection(vcset, nd.ni_vp, &ph[i], &addr,
390 1.1 fvdl &size, &prot);
391 1.1 fvdl /* Assume that the text segment is r-x only */
392 1.1 fvdl if ((prot & PROT_WRITE) == 0) {
393 1.1 fvdl *entry = addr + eh.e_entry;
394 1.1 fvdl ap->arg_interp = addr;
395 1.1 fvdl }
396 1.1 fvdl addr += size;
397 1.1 fvdl break;
398 1.1 fvdl
399 1.1 fvdl case Elf32_pt_dynamic:
400 1.1 fvdl case Elf32_pt_phdr:
401 1.1 fvdl case Elf32_pt_note:
402 1.1 fvdl break;
403 1.1 fvdl
404 1.1 fvdl default:
405 1.1 fvdl break;
406 1.1 fvdl }
407 1.1 fvdl }
408 1.1 fvdl
409 1.1 fvdl bad:
410 1.1 fvdl if (ph != NULL)
411 1.1 fvdl free((char *) ph, M_TEMP);
412 1.1 fvdl
413 1.1 fvdl *last = addr;
414 1.1 fvdl vrele(nd.ni_vp);
415 1.1 fvdl return error;
416 1.1 fvdl }
417 1.1 fvdl
418 1.1 fvdl /*
419 1.1 fvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package
420 1.1 fvdl *
421 1.1 fvdl * First, set of the various offsets/lengths in the exec package.
422 1.1 fvdl *
423 1.1 fvdl * Then, mark the text image busy (so it can be demand paged) or error
424 1.1 fvdl * out if this is not possible. Finally, set up vmcmds for the
425 1.1 fvdl * text, data, bss, and stack segments.
426 1.1 fvdl *
427 1.1 fvdl * XXX no demand paging (yet?)
428 1.1 fvdl */
429 1.1 fvdl int
430 1.1 fvdl exec_elf_makecmds(p, epp)
431 1.1 fvdl struct proc *p;
432 1.1 fvdl struct exec_package *epp;
433 1.1 fvdl {
434 1.1 fvdl Elf32_Ehdr *eh = epp->ep_hdr;
435 1.1 fvdl Elf32_Phdr *ph, *pp;
436 1.1 fvdl int error, i, n;
437 1.1 fvdl char interp[MAXPATHLEN];
438 1.1 fvdl u_long pos = 0, phsize;
439 1.1 fvdl
440 1.1 fvdl if (epp->ep_hdrvalid < sizeof(Elf32_Ehdr))
441 1.1 fvdl return ENOEXEC;
442 1.1 fvdl
443 1.1 fvdl if (elf_check_header(eh, Elf32_et_exec))
444 1.1 fvdl return ENOEXEC;
445 1.1 fvdl
446 1.1 fvdl /*
447 1.1 fvdl * check if vnode is in open for writing, because we want to
448 1.1 fvdl * demand-page out of it. if it is, don't do it, for various
449 1.1 fvdl * reasons
450 1.1 fvdl */
451 1.1 fvdl if (epp->ep_vp->v_writecount != 0) {
452 1.1 fvdl #ifdef DIAGNOSTIC
453 1.1 fvdl if (epp->ep_vp->v_flag & VTEXT)
454 1.1 fvdl panic("exec: a VTEXT vnode has writecount != 0\n");
455 1.1 fvdl #endif
456 1.1 fvdl return ETXTBSY;
457 1.1 fvdl }
458 1.1 fvdl /*
459 1.1 fvdl * Allocate space to hold all the program headers, and read them
460 1.1 fvdl * from the file
461 1.1 fvdl */
462 1.1 fvdl phsize = eh->e_phnum * sizeof(Elf32_Phdr);
463 1.1 fvdl ph = (Elf32_Phdr *) malloc(phsize, M_TEMP, M_WAITOK);
464 1.1 fvdl
465 1.1 fvdl if ((error = elf_read_from(p, epp->ep_vp, eh->e_phoff,
466 1.1 fvdl (caddr_t) ph, phsize)) != 0)
467 1.1 fvdl goto bad;
468 1.1 fvdl
469 1.1 fvdl epp->ep_tsize = ELF32_NO_ADDR;
470 1.1 fvdl epp->ep_dsize = ELF32_NO_ADDR;
471 1.1 fvdl
472 1.1 fvdl interp[0] = '\0';
473 1.1 fvdl
474 1.1 fvdl for (i = 0; i < eh->e_phnum; i++) {
475 1.1 fvdl pp = &ph[i];
476 1.1 fvdl if (pp->p_type == Elf32_pt_interp) {
477 1.1 fvdl if (pp->p_filesz >= sizeof(interp))
478 1.1 fvdl goto bad;
479 1.1 fvdl if ((error = elf_read_from(p, epp->ep_vp, pp->p_offset,
480 1.1 fvdl (caddr_t) interp, pp->p_filesz)) != 0)
481 1.1 fvdl goto bad;
482 1.1 fvdl break;
483 1.1 fvdl }
484 1.1 fvdl }
485 1.1 fvdl
486 1.1 fvdl /*
487 1.1 fvdl * On the same architecture, we may be emulating different systems.
488 1.1 fvdl * See which one will accept this executable. This currently only
489 1.1 fvdl * applies to Linux and SVR4 on the i386.
490 1.1 fvdl *
491 1.1 fvdl * Probe functions would normally see if the interpreter (if any)
492 1.1 fvdl * exists. Emulation packages may possibly replace the interpreter in
493 1.1 fvdl * interp[] with a changed path (/emul/xxx/<path>), and also
494 1.1 fvdl * set the ep_emul field in the exec package structure.
495 1.1 fvdl */
496 1.1 fvdl if ((n = sizeof elf_probe_funcs / sizeof elf_probe_funcs[0])) {
497 1.1 fvdl error = ENOEXEC;
498 1.1 fvdl for (i = 0; i < n && error; i++)
499 1.1 fvdl error = elf_probe_funcs[i](p, epp, interp, &pos);
500 1.1 fvdl
501 1.1 fvdl if (error)
502 1.1 fvdl goto bad;
503 1.1 fvdl }
504 1.1 fvdl
505 1.1 fvdl /*
506 1.1 fvdl * Load all the necessary sections
507 1.1 fvdl */
508 1.1 fvdl for (i = 0; i < eh->e_phnum; i++) {
509 1.1 fvdl u_long addr = ELF32_NO_ADDR, size = 0;
510 1.1 fvdl int prot = 0;
511 1.1 fvdl
512 1.1 fvdl pp = &ph[i];
513 1.1 fvdl
514 1.1 fvdl switch (ph[i].p_type) {
515 1.1 fvdl case Elf32_pt_load:
516 1.1 fvdl elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
517 1.1 fvdl &ph[i], &addr, &size, &prot);
518 1.1 fvdl if ((error = elf_set_segment(epp, addr, size,
519 1.1 fvdl prot)) != 0)
520 1.1 fvdl goto bad;
521 1.1 fvdl break;
522 1.1 fvdl
523 1.1 fvdl case Elf32_pt_shlib:
524 1.1 fvdl error = ENOEXEC;
525 1.1 fvdl goto bad;
526 1.1 fvdl
527 1.1 fvdl case Elf32_pt_interp:
528 1.1 fvdl /* Already did this one */
529 1.1 fvdl case Elf32_pt_dynamic:
530 1.1 fvdl case Elf32_pt_phdr:
531 1.1 fvdl case Elf32_pt_note:
532 1.1 fvdl break;
533 1.1 fvdl
534 1.1 fvdl default:
535 1.1 fvdl /*
536 1.1 fvdl * Not fatal, we don't need to understand everything
537 1.1 fvdl * :-)
538 1.1 fvdl */
539 1.1 fvdl break;
540 1.1 fvdl }
541 1.1 fvdl }
542 1.1 fvdl
543 1.1 fvdl /*
544 1.1 fvdl * Check if we found a dynamically linked binary and arrange to load
545 1.1 fvdl * it's interpreter
546 1.1 fvdl */
547 1.1 fvdl if (interp[0]) {
548 1.1 fvdl struct elf_args *ap;
549 1.1 fvdl
550 1.1 fvdl ap = (struct elf_args *) malloc(sizeof(struct elf_args),
551 1.1 fvdl M_TEMP, M_WAITOK);
552 1.1 fvdl if ((error = elf_load_file(p, interp, &epp->ep_vmcmds,
553 1.1 fvdl &epp->ep_entry, ap, &pos)) != 0) {
554 1.1 fvdl free((char *) ap, M_TEMP);
555 1.1 fvdl goto bad;
556 1.1 fvdl }
557 1.1 fvdl /* Arrange to load the program headers. */
558 1.1 fvdl pos = ELF_ALIGN(pos + NBPG, NBPG);
559 1.1 fvdl ap->arg_phaddr = pos;
560 1.1 fvdl NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, phsize,
561 1.1 fvdl pos, epp->ep_vp, eh->e_phoff,
562 1.1 fvdl VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
563 1.1 fvdl pos += phsize;
564 1.1 fvdl
565 1.1 fvdl ap->arg_phentsize = eh->e_phentsize;
566 1.1 fvdl ap->arg_phnum = eh->e_phnum;
567 1.1 fvdl ap->arg_entry = eh->e_entry;
568 1.1 fvdl
569 1.1 fvdl epp->ep_emul_arg = ap;
570 1.1 fvdl } else
571 1.1 fvdl epp->ep_entry = eh->e_entry;
572 1.1 fvdl
573 1.1 fvdl free((char *) ph, M_TEMP);
574 1.1 fvdl epp->ep_vp->v_flag |= VTEXT;
575 1.1 fvdl return exec_aout_setup_stack(p, epp);
576 1.1 fvdl
577 1.1 fvdl bad:
578 1.1 fvdl free((char *) ph, M_TEMP);
579 1.1 fvdl kill_vmcmds(&epp->ep_vmcmds);
580 1.1 fvdl return ENOEXEC;
581 1.1 fvdl }
582