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