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