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