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