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