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