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