exec_elf32.c revision 1.83
1/*	$NetBSD: exec_elf32.c,v 1.83 2003/02/27 01:58:56 matt Exp $	*/
2
3/*-
4 * Copyright (c) 1994, 2000 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#include <sys/cdefs.h>
67__KERNEL_RCSID(1, "$NetBSD: exec_elf32.c,v 1.83 2003/02/27 01:58:56 matt Exp $");
68
69/* If not included by exec_elf64.c, ELFSIZE won't be defined. */
70#ifndef ELFSIZE
71#define	ELFSIZE		32
72#endif
73
74#include <sys/param.h>
75#include <sys/proc.h>
76#include <sys/malloc.h>
77#include <sys/namei.h>
78#include <sys/vnode.h>
79#include <sys/exec.h>
80#include <sys/exec_elf.h>
81#include <sys/syscall.h>
82#include <sys/signalvar.h>
83#include <sys/mount.h>
84#include <sys/stat.h>
85
86#include <machine/cpu.h>
87#include <machine/reg.h>
88
89extern const struct emul emul_netbsd;
90
91int	ELFNAME(check_header)(Elf_Ehdr *, int);
92int	ELFNAME(load_file)(struct proc *, struct exec_package *, char *,
93	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
94void	ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *,
95	    const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
96
97int ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *,
98    Elf_Ehdr *);
99int ELFNAME2(netbsd,probe)(struct proc *, struct exec_package *,
100    void *, char *, vaddr_t *);
101
102/* round up and down to page boundaries. */
103#define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
104#define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
105
106/*
107 * Copy arguments onto the stack in the normal way, but add some
108 * extra information in case of dynamic binding.
109 */
110int
111ELFNAME(copyargs)(struct proc *p, struct exec_package *pack,
112    struct ps_strings *arginfo, char **stackp, void *argp)
113{
114	size_t len;
115	AuxInfo ai[ELF_AUX_ENTRIES], *a;
116	struct elf_args *ap;
117	int error;
118
119	if ((error = copyargs(p, pack, arginfo, stackp, argp)) != 0)
120		return error;
121
122	a = ai;
123
124	/*
125	 * Push extra arguments on the stack needed by dynamically
126	 * linked binaries
127	 */
128	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
129		struct vattr *vap = pack->ep_vap;
130
131		a->a_type = AT_PHDR;
132		a->a_v = ap->arg_phaddr;
133		a++;
134
135		a->a_type = AT_PHENT;
136		a->a_v = ap->arg_phentsize;
137		a++;
138
139		a->a_type = AT_PHNUM;
140		a->a_v = ap->arg_phnum;
141		a++;
142
143		a->a_type = AT_PAGESZ;
144		a->a_v = PAGE_SIZE;
145		a++;
146
147		a->a_type = AT_BASE;
148		a->a_v = ap->arg_interp;
149		a++;
150
151		a->a_type = AT_FLAGS;
152		a->a_v = 0;
153		a++;
154
155		a->a_type = AT_ENTRY;
156		a->a_v = ap->arg_entry;
157		a++;
158
159		a->a_type = AT_EUID;
160		if (vap->va_mode & S_ISUID)
161			a->a_v = vap->va_uid;
162		else
163			a->a_v = p->p_ucred->cr_uid;
164		a++;
165
166		a->a_type = AT_RUID;
167		a->a_v = p->p_cred->p_ruid;
168		a++;
169
170		a->a_type = AT_EGID;
171		if (vap->va_mode & S_ISGID)
172			a->a_v = vap->va_gid;
173		else
174			a->a_v = p->p_ucred->cr_gid;
175		a++;
176
177		a->a_type = AT_RGID;
178		a->a_v = p->p_cred->p_rgid;
179		a++;
180
181		free(ap, M_TEMP);
182		pack->ep_emul_arg = NULL;
183	}
184
185	a->a_type = AT_NULL;
186	a->a_v = 0;
187	a++;
188
189	len = (a - ai) * sizeof(AuxInfo);
190	if ((error = copyout(ai, *stackp, len)) != 0)
191		return error;
192	*stackp += len;
193
194	return 0;
195}
196
197/*
198 * elf_check_header():
199 *
200 * Check header for validity; return 0 of ok ENOEXEC if error
201 */
202int
203ELFNAME(check_header)(Elf_Ehdr *eh, int type)
204{
205
206	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
207	    eh->e_ident[EI_CLASS] != ELFCLASS)
208		return (ENOEXEC);
209
210	switch (eh->e_machine) {
211
212	ELFDEFNNAME(MACHDEP_ID_CASES)
213
214	default:
215		return (ENOEXEC);
216	}
217
218	if (ELF_EHDR_FLAGS_OK(eh) == 0)
219		return (ENOEXEC);
220
221	if (eh->e_type != type)
222		return (ENOEXEC);
223
224	if (eh->e_shnum > 512 ||
225	    eh->e_phnum > 128)
226		return (ENOEXEC);
227
228	return (0);
229}
230
231/*
232 * elf_load_psection():
233 *
234 * Load a psection at the appropriate address
235 */
236void
237ELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp,
238    const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags)
239{
240	u_long uaddr, msize, psize, rm, rf;
241	long diff, offset;
242
243	/*
244	 * If the user specified an address, then we load there.
245	 */
246	if (*addr != ELFDEFNNAME(NO_ADDR)) {
247		if (ph->p_align > 1) {
248			*addr = ELF_TRUNC(*addr, ph->p_align);
249			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
250		} else
251			uaddr = ph->p_vaddr;
252		diff = ph->p_vaddr - uaddr;
253	} else {
254		*addr = uaddr = ph->p_vaddr;
255		if (ph->p_align > 1)
256			*addr = ELF_TRUNC(uaddr, ph->p_align);
257		diff = uaddr - *addr;
258	}
259
260	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
261	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
262	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
263
264	offset = ph->p_offset - diff;
265	*size = ph->p_filesz + diff;
266	msize = ph->p_memsz + diff;
267
268	if (ph->p_align >= PAGE_SIZE) {
269		if ((ph->p_flags & PF_W) != 0) {
270			/*
271			 * Because the pagedvn pager can't handle zero fill
272			 * of the last data page if it's not page aligned we
273			 * map the last page readvn.
274			 */
275			psize = trunc_page(*size);
276		} else {
277			psize = round_page(*size);
278		}
279	} else {
280		psize = *size;
281	}
282
283	if (psize > 0) {
284		NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ?
285		    vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp,
286		    offset, *prot, flags);
287	}
288	if (psize < *size) {
289		NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
290		    *addr + psize, vp, offset + psize, *prot,
291		    psize > 0 ? flags & VMCMD_RELATIVE : flags);
292	}
293
294	/*
295	 * Check if we need to extend the size of the segment
296	 */
297	rm = round_page(*addr + msize);
298	rf = round_page(*addr + *size);
299
300	if (rm != rf) {
301		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
302		    0, *prot, flags & VMCMD_RELATIVE);
303		*size = msize;
304	}
305}
306
307/*
308 * elf_load_file():
309 *
310 * Load a file (interpreter/library) pointed to by path
311 * [stolen from coff_load_shlib()]. Made slightly generic
312 * so it might be used externally.
313 */
314int
315ELFNAME(load_file)(struct proc *p, struct exec_package *epp, char *path,
316    struct exec_vmcmd_set *vcset, u_long *entryoff, struct elf_args *ap,
317    Elf_Addr *last)
318{
319	int error, i;
320	struct nameidata nd;
321	struct vnode *vp;
322	struct vattr attr;
323	Elf_Ehdr eh;
324	Elf_Phdr *ph = NULL;
325	const Elf_Phdr *ph0;
326	const Elf_Phdr *base_ph;
327	const Elf_Phdr *last_ph;
328	u_long phsize;
329	Elf_Addr addr = *last;
330
331	/*
332	 * 1. open file
333	 * 2. read filehdr
334	 * 3. map text, data, and bss out of it using VM_*
335	 */
336	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
337	if ((error = namei(&nd)) != 0)
338		return error;
339	vp = nd.ni_vp;
340
341	/*
342	 * Similarly, if it's not marked as executable, or it's not a regular
343	 * file, we don't allow it to be used.
344	 */
345	if (vp->v_type != VREG) {
346		error = EACCES;
347		goto badunlock;
348	}
349	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
350		goto badunlock;
351
352	/* get attributes */
353	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
354		goto badunlock;
355
356	/*
357	 * Check mount point.  Though we're not trying to exec this binary,
358	 * we will be executing code from it, so if the mount point
359	 * disallows execution or set-id-ness, we punt or kill the set-id.
360	 */
361	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
362		error = EACCES;
363		goto badunlock;
364	}
365	if (vp->v_mount->mnt_flag & MNT_NOSUID)
366		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
367
368#ifdef notyet /* XXX cgd 960926 */
369	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
370#endif
371
372	error = vn_marktext(vp);
373	if (error)
374		goto badunlock;
375
376	VOP_UNLOCK(vp, 0);
377
378	if ((error = exec_read_from(p, vp, 0, &eh, sizeof(eh))) != 0)
379		goto bad;
380
381	if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
382		goto bad;
383
384	phsize = eh.e_phnum * sizeof(Elf_Phdr);
385	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
386
387	if ((error = exec_read_from(p, vp, eh.e_phoff, ph, phsize)) != 0)
388		goto bad;
389
390	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
391#ifndef ELF_INTERP_NON_RELOCATABLE
392	/*
393	 * If no position to load the interpreter was set by a probe
394	 * function, pick the same address that a non-fixed mmap(0, ..)
395	 * would (i.e. something safely out of the way).
396	 */
397	if (*last == ELFDEFNNAME(NO_ADDR)) {
398		u_long limit = 0;
399		/*
400		 * Find the start and ending addresses of the psections to
401		 * be loaded.  This will give us the size.
402		 */
403		for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum;
404		     i++, ph0++) {
405			if (ph0->p_type == PT_LOAD) {
406				u_long psize = ph0->p_vaddr + ph0->p_memsz;
407				if (base_ph == NULL)
408					base_ph = ph0;
409				if (psize > limit)
410					limit = psize;
411			}
412		}
413
414		/*
415		 * Now compute the size and load address.
416		 */
417		addr = VM_DEFAULT_ADDRESS(epp->ep_daddr,
418		    round_page(limit) - trunc_page(base_ph->p_vaddr));
419	} else
420#endif	/* !ELF_INTERP_NON_RELOCATABLE */
421		addr = *last;
422
423	/*
424	 * Load all the necessary sections
425	 */
426	for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL;
427	     i < eh.e_phnum; i++, ph0++) {
428		switch (ph0->p_type) {
429		case PT_LOAD: {
430			u_long size;
431			int prot = 0;
432			int flags;
433
434			if (base_ph == NULL) {
435				/*
436				 * First encountered psection is always the
437				 * base psection.
438				 */
439				base_ph = ph0;
440				flags = VMCMD_BASE;
441			} else {
442				u_long limit = round_page(last_ph->p_vaddr
443				    + last_ph->p_memsz);
444
445				/*
446				 * If there is a gap in between the psections,
447				 * map it as inaccessible so nothing else
448				 * mmap'ed will be placed there.
449				 */
450				if (limit != trunc_page(ph0->p_vaddr)) {
451					NEW_VMCMD2(vcset, vmcmd_map_zero,
452					    trunc_page(ph0->p_vaddr) - limit,
453					    limit - base_ph->p_vaddr,
454					    NULLVP, 0, 0, VMCMD_RELATIVE);
455				}
456
457				addr = ph0->p_vaddr - base_ph->p_vaddr;
458				flags = VMCMD_RELATIVE;
459			}
460			last_ph = ph0;
461			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
462			    &size, &prot, flags);
463			/*
464			 * If entry is within this psection then this
465			 * must contain the .text section.  *entryoff is
466			 * relative to the base psection.
467			 */
468			if (eh.e_entry >= ph0->p_vaddr &&
469			    eh.e_entry < (ph0->p_vaddr + size)) {
470				*entryoff = eh.e_entry - base_ph->p_vaddr;
471			}
472			/*
473			 * This value is ignored if TOPDOWN.
474			 */
475			if (ph0->p_vaddr != 0)
476				addr += size;
477			break;
478		}
479
480		case PT_DYNAMIC:
481		case PT_PHDR:
482		case PT_NOTE:
483			break;
484
485		default:
486			break;
487		}
488	}
489
490	free(ph, M_TEMP);
491	/*
492	 * This value is ignored if TOPDOWN.
493	 */
494	*last = addr;
495	vrele(vp);
496	return 0;
497
498badunlock:
499	VOP_UNLOCK(vp, 0);
500
501bad:
502	if (ph != NULL)
503		free(ph, M_TEMP);
504#ifdef notyet /* XXX cgd 960926 */
505	(maybe) VOP_CLOSE it
506#endif
507	vrele(vp);
508	return error;
509}
510
511/*
512 * exec_elf_makecmds(): Prepare an Elf binary's exec package
513 *
514 * First, set of the various offsets/lengths in the exec package.
515 *
516 * Then, mark the text image busy (so it can be demand paged) or error
517 * out if this is not possible.  Finally, set up vmcmds for the
518 * text, data, bss, and stack segments.
519 */
520int
521ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp)
522{
523	Elf_Ehdr *eh = epp->ep_hdr;
524	Elf_Phdr *ph, *pp;
525	Elf_Addr phdr = 0, pos = 0;
526	int error, i, nload;
527	char *interp = NULL;
528	u_long phsize;
529
530	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
531		return ENOEXEC;
532
533	/*
534	 * XXX allow for executing shared objects. It seems silly
535	 * but other ELF-based systems allow it as well.
536	 */
537	if (ELFNAME(check_header)(eh, ET_EXEC) != 0 &&
538	    ELFNAME(check_header)(eh, ET_DYN) != 0)
539		return ENOEXEC;
540
541	error = vn_marktext(epp->ep_vp);
542	if (error)
543		return (error);
544
545	/*
546	 * Allocate space to hold all the program headers, and read them
547	 * from the file
548	 */
549	phsize = eh->e_phnum * sizeof(Elf_Phdr);
550	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
551
552	if ((error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
553	    0)
554		goto bad;
555
556	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
557	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
558
559	MALLOC(interp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
560	interp[0] = '\0';
561
562	for (i = 0; i < eh->e_phnum; i++) {
563		pp = &ph[i];
564		if (pp->p_type == PT_INTERP) {
565			if (pp->p_filesz >= MAXPATHLEN)
566				goto bad;
567			if ((error = exec_read_from(p, epp->ep_vp,
568			    pp->p_offset, interp, pp->p_filesz)) != 0)
569				goto bad;
570			break;
571		}
572	}
573
574	/*
575	 * On the same architecture, we may be emulating different systems.
576	 * See which one will accept this executable. This currently only
577	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
578	 * and the Alpha.
579	 *
580	 * Probe functions would normally see if the interpreter (if any)
581	 * exists. Emulation packages may possibly replace the interpreter in
582	 * interp[] with a changed path (/emul/xxx/<path>).
583	 */
584	if (!epp->ep_esch->u.elf_probe_func) {
585		pos = ELFDEFNNAME(NO_ADDR);
586	} else {
587		vaddr_t startp = 0;
588
589		error = (*epp->ep_esch->u.elf_probe_func)(p, epp, eh, interp,
590							  &startp);
591		pos = (Elf_Addr)startp;
592		if (error)
593			goto bad;
594	}
595
596	/*
597	 * Load all the necessary sections
598	 */
599	for (i = nload = 0; i < eh->e_phnum; i++) {
600		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
601		u_long size = 0;
602		int prot = 0;
603
604		pp = &ph[i];
605
606		switch (ph[i].p_type) {
607		case PT_LOAD:
608			/*
609			 * XXX
610			 * Can handle only 2 sections: text and data
611			 */
612			if (nload++ == 2)
613				goto bad;
614			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
615			    &ph[i], &addr, &size, &prot, VMCMD_FIXED);
616
617			/*
618			 * Decide whether it's text or data by looking
619			 * at the entry point.
620			 */
621			if (eh->e_entry >= addr &&
622			    eh->e_entry < (addr + size)) {
623				epp->ep_taddr = addr;
624				epp->ep_tsize = size;
625				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
626					epp->ep_daddr = addr;
627					epp->ep_dsize = size;
628				}
629			} else {
630				epp->ep_daddr = addr;
631				epp->ep_dsize = size;
632			}
633			break;
634
635		case PT_SHLIB:
636			/* SCO has these sections. */
637		case PT_INTERP:
638			/* Already did this one. */
639		case PT_DYNAMIC:
640		case PT_NOTE:
641			break;
642
643		case PT_PHDR:
644			/* Note address of program headers (in text segment) */
645			phdr = pp->p_vaddr;
646			break;
647
648		default:
649			/*
650			 * Not fatal; we don't need to understand everything.
651			 */
652			break;
653		}
654	}
655
656	/*
657	 * Check if we found a dynamically linked binary and arrange to load
658	 * its interpreter
659	 */
660	if (interp[0]) {
661		struct elf_args *ap;
662		int i = epp->ep_vmcmds.evs_used;
663		u_long interp_offset;
664
665		MALLOC(ap, struct elf_args *, sizeof(struct elf_args),
666		    M_TEMP, M_WAITOK);
667		if ((error = ELFNAME(load_file)(p, epp, interp,
668		    &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) {
669			FREE(ap, M_TEMP);
670			goto bad;
671		}
672		ap->arg_interp = epp->ep_vmcmds.evs_cmds[i].ev_addr;
673		epp->ep_entry = ap->arg_interp + interp_offset;
674		ap->arg_phaddr = phdr;
675
676		ap->arg_phentsize = eh->e_phentsize;
677		ap->arg_phnum = eh->e_phnum;
678		ap->arg_entry = eh->e_entry;
679
680		epp->ep_emul_arg = ap;
681	} else
682		epp->ep_entry = eh->e_entry;
683
684#ifdef ELF_MAP_PAGE_ZERO
685	/* Dell SVR4 maps page zero, yeuch! */
686	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
687	    epp->ep_vp, 0, VM_PROT_READ);
688#endif
689	FREE(interp, M_TEMP);
690	free(ph, M_TEMP);
691	return exec_elf_setup_stack(p, epp);
692
693bad:
694	if (interp)
695		FREE(interp, M_TEMP);
696	free(ph, M_TEMP);
697	kill_vmcmds(&epp->ep_vmcmds);
698	return ENOEXEC;
699}
700
701int
702ELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp,
703    Elf_Ehdr *eh)
704{
705	size_t i;
706	Elf_Phdr *ph;
707	size_t phsize;
708	int error;
709
710	phsize = eh->e_phnum * sizeof(Elf_Phdr);
711	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
712	error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
713	if (error)
714		goto out;
715
716	for (i = 0; i < eh->e_phnum; i++) {
717		Elf_Phdr *ephp = &ph[i];
718		Elf_Nhdr *np;
719
720		if (ephp->p_type != PT_NOTE ||
721		    ephp->p_filesz > 1024 ||
722		    ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
723			continue;
724
725		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
726		error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
727		    ephp->p_filesz);
728		if (error)
729			goto next;
730
731		if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG ||
732		    np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
733		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ ||
734		    memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME,
735		    ELF_NOTE_NETBSD_NAMESZ))
736			goto next;
737
738		error = 0;
739		free(np, M_TEMP);
740		goto out;
741
742	next:
743		free(np, M_TEMP);
744		continue;
745	}
746
747	error = ENOEXEC;
748out:
749	free(ph, M_TEMP);
750	return (error);
751}
752
753int
754ELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp,
755    void *eh, char *itp, vaddr_t *pos)
756{
757	int error;
758
759	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
760		return error;
761	*pos = ELFDEFNNAME(NO_ADDR);
762	return 0;
763}
764