xref: /netbsd-src/sys/kern/exec_elf32.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: exec_elf32.c,v 1.61 2000/12/15 06:14:21 mycroft 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 
86 extern const struct emul emul_netbsd;
87 
88 int	ELFNAME(check_header)(Elf_Ehdr *, int);
89 int	ELFNAME(load_file)(struct proc *, struct exec_package *, char *,
90 	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
91 void	ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *,
92 	    const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
93 
94 int ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *,
95     Elf_Ehdr *);
96 int 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  */
107 void *
108 ELFNAME(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  */
176 int
177 ELFNAME(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  */
207 void
208 ELFNAME(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  */
276 int
277 ELFNAME(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  */
301 int
302 ELFNAME(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 
423 badunlock:
424 	VOP_UNLOCK(vp, 0);
425 
426 bad:
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  */
445 int
446 ELFNAME2(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 		error = (*epp->ep_esch->u.elf_probe_func)(p, epp, eh, interp,
522 				(vaddr_t *)&pos);
523 		if (error)
524 			goto bad;
525 	}
526 
527 	/*
528 	 * Load all the necessary sections
529 	 */
530 	for (i = nload = 0; i < eh->e_phnum; i++) {
531 		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
532 		u_long size = 0;
533 		int prot = 0;
534 
535 		pp = &ph[i];
536 
537 		switch (ph[i].p_type) {
538 		case PT_LOAD:
539 			/*
540 			 * XXX
541 			 * Can handle only 2 sections: text and data
542 			 */
543 			if (nload++ == 2)
544 				goto bad;
545 			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
546 			    &ph[i], &addr, &size, &prot, 0);
547 
548 			/*
549 			 * Decide whether it's text or data by looking
550 			 * at the entry point.
551 			 */
552 			if (eh->e_entry >= addr &&
553 			    eh->e_entry < (addr + size)) {
554 				epp->ep_taddr = addr;
555 				epp->ep_tsize = size;
556 				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
557 					epp->ep_daddr = addr;
558 					epp->ep_dsize = size;
559 				}
560 			} else {
561 				epp->ep_daddr = addr;
562 				epp->ep_dsize = size;
563 			}
564 			break;
565 
566 		case PT_SHLIB:
567 			/* SCO has these sections. */
568 		case PT_INTERP:
569 			/* Already did this one. */
570 		case PT_DYNAMIC:
571 		case PT_NOTE:
572 			break;
573 
574 		case PT_PHDR:
575 			/* Note address of program headers (in text segment) */
576 			phdr = pp->p_vaddr;
577 			break;
578 
579 		default:
580 			/*
581 			 * Not fatal; we don't need to understand everything.
582 			 */
583 			break;
584 		}
585 	}
586 
587 	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
588 #ifndef ELF_INTERP_NON_RELOCATABLE
589 	/*
590 	 * If no position to load the interpreter was set by a probe
591 	 * function, pick the same address that a non-fixed mmap(0, ..)
592 	 * would (i.e. something safely out of the way).
593 	 */
594 	if (pos == ELFDEFNNAME(NO_ADDR))
595 		pos = round_page(epp->ep_daddr + MAXDSIZ);
596 #endif	/* !ELF_INTERP_NON_RELOCATABLE */
597 
598 	/*
599 	 * Check if we found a dynamically linked binary and arrange to load
600 	 * it's interpreter
601 	 */
602 	if (interp[0]) {
603 		struct elf_args *ap;
604 
605 		MALLOC(ap, struct elf_args *, sizeof(struct elf_args),
606 		    M_TEMP, M_WAITOK);
607 		if ((error = ELFNAME(load_file)(p, epp, interp,
608 		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
609 			FREE((char *)ap, M_TEMP);
610 			goto bad;
611 		}
612 		pos += phsize;
613 		ap->arg_phaddr = phdr;
614 
615 		ap->arg_phentsize = eh->e_phentsize;
616 		ap->arg_phnum = eh->e_phnum;
617 		ap->arg_entry = eh->e_entry;
618 
619 		epp->ep_emul_arg = ap;
620 	} else
621 		epp->ep_entry = eh->e_entry;
622 
623 #ifdef ELF_MAP_PAGE_ZERO
624 	/* Dell SVR4 maps page zero, yeuch! */
625 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
626 	    epp->ep_vp, 0, VM_PROT_READ);
627 #endif
628 	FREE(interp, M_TEMP);
629 	free((char *)ph, M_TEMP);
630 	vn_marktext(epp->ep_vp);
631 	return exec_elf_setup_stack(p, epp);
632 
633 bad:
634 	if (interp)
635 		FREE(interp, M_TEMP);
636 	free((char *)ph, M_TEMP);
637 	kill_vmcmds(&epp->ep_vmcmds);
638 	return ENOEXEC;
639 }
640 
641 int
642 ELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp,
643     Elf_Ehdr *eh)
644 {
645 	size_t i;
646 	Elf_Phdr *ph;
647 	size_t phsize;
648 	int error;
649 
650 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
651 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
652 	error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, (caddr_t)ph,
653 	    phsize);
654 	if (error)
655 		goto out;
656 
657 	for (i = 0; i < eh->e_phnum; i++) {
658 		Elf_Phdr *ephp = &ph[i];
659 		Elf_Nhdr *np;
660 
661 		if (ephp->p_type != PT_NOTE ||
662 		    ephp->p_filesz > 1024 ||
663 		    ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
664 			continue;
665 
666 		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
667 		error = ELFNAME(read_from)(p, epp->ep_vp, ephp->p_offset,
668 		    (caddr_t)np, ephp->p_filesz);
669 		if (error)
670 			goto next;
671 
672 		if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG ||
673 		    np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
674 		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ ||
675 		    memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME,
676 		    ELF_NOTE_NETBSD_NAMESZ))
677 			goto next;
678 
679 		error = 0;
680 		free(np, M_TEMP);
681 		goto out;
682 
683 	next:
684 		free(np, M_TEMP);
685 		continue;
686 	}
687 
688 	error = ENOEXEC;
689 out:
690 	free(ph, M_TEMP);
691 	return (error);
692 }
693 
694 int
695 ELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp,
696     void *eh, char *itp, vaddr_t *pos)
697 {
698 	int error;
699 
700 	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
701 		return error;
702 	*pos = ELFDEFNNAME(NO_ADDR);
703 	return 0;
704 }
705