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