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