xref: /netbsd-src/sys/kern/exec_elf32.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: exec_elf32.c,v 1.73 2002/09/12 16:57:44 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 #include <sys/cdefs.h>
67 __KERNEL_RCSID(1, "$NetBSD: exec_elf32.c,v 1.73 2002/09/12 16:57:44 mycroft 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((char *)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 	VOP_UNLOCK(vp, 0);
365 
366 	if ((error = exec_read_from(p, vp, 0, &eh, sizeof(eh))) != 0)
367 		goto bad;
368 
369 	if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
370 		goto bad;
371 
372 	phsize = eh.e_phnum * sizeof(Elf_Phdr);
373 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
374 
375 	if ((error = exec_read_from(p, vp, eh.e_phoff, ph, phsize)) != 0)
376 		goto bad;
377 
378 	/*
379 	 * Load all the necessary sections
380 	 */
381 	for (i = 0; i < eh.e_phnum; i++) {
382 		u_long size = 0;
383 		int prot = 0;
384 		int flags;
385 
386 		switch (ph[i].p_type) {
387 		case PT_LOAD:
388 			if (base_ph == NULL) {
389 				addr = *last;
390 				flags = VMCMD_BASE;
391 			} else {
392 				addr = ph[i].p_vaddr - base_ph->p_vaddr;
393 				flags = VMCMD_RELATIVE;
394 			}
395 			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
396 			    &size, &prot, flags);
397 			/* If entry is within this section it must be text */
398 			if (eh.e_entry >= ph[i].p_vaddr &&
399 			    eh.e_entry < (ph[i].p_vaddr + size)) {
400 				*entry = addr + eh.e_entry - ph[i].p_vaddr;
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 = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
486 	    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 = exec_read_from(p, epp->ep_vp,
501 			    pp->p_offset, interp, pp->p_filesz)) != 0)
502 				goto bad;
503 			break;
504 		}
505 	}
506 
507 	/*
508 	 * On the same architecture, we may be emulating different systems.
509 	 * See which one will accept this executable. This currently only
510 	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
511 	 * and the Alpha.
512 	 *
513 	 * Probe functions would normally see if the interpreter (if any)
514 	 * exists. Emulation packages may possibly replace the interpreter in
515 	 * interp[] with a changed path (/emul/xxx/<path>).
516 	 */
517 	if (!epp->ep_esch->u.elf_probe_func) {
518 		pos = ELFDEFNNAME(NO_ADDR);
519 	} else {
520 		vaddr_t startp = 0;
521 
522 		error = (*epp->ep_esch->u.elf_probe_func)(p, epp, eh, interp,
523 							  &startp);
524 		pos = (Elf_Addr)startp;
525 		if (error)
526 			goto bad;
527 	}
528 
529 	/*
530 	 * Load all the necessary sections
531 	 */
532 	for (i = nload = 0; i < eh->e_phnum; i++) {
533 		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
534 		u_long size = 0;
535 		int prot = 0;
536 
537 		pp = &ph[i];
538 
539 		switch (ph[i].p_type) {
540 		case PT_LOAD:
541 			/*
542 			 * XXX
543 			 * Can handle only 2 sections: text and data
544 			 */
545 			if (nload++ == 2)
546 				goto bad;
547 			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
548 			    &ph[i], &addr, &size, &prot, 0);
549 
550 			/*
551 			 * Decide whether it's text or data by looking
552 			 * at the entry point.
553 			 */
554 			if (eh->e_entry >= addr &&
555 			    eh->e_entry < (addr + size)) {
556 				epp->ep_taddr = addr;
557 				epp->ep_tsize = size;
558 				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
559 					epp->ep_daddr = addr;
560 					epp->ep_dsize = size;
561 				}
562 			} else {
563 				epp->ep_daddr = addr;
564 				epp->ep_dsize = size;
565 			}
566 			break;
567 
568 		case PT_SHLIB:
569 			/* SCO has these sections. */
570 		case PT_INTERP:
571 			/* Already did this one. */
572 		case PT_DYNAMIC:
573 		case PT_NOTE:
574 			break;
575 
576 		case PT_PHDR:
577 			/* Note address of program headers (in text segment) */
578 			phdr = pp->p_vaddr;
579 			break;
580 
581 		default:
582 			/*
583 			 * Not fatal; we don't need to understand everything.
584 			 */
585 			break;
586 		}
587 	}
588 
589 	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
590 #ifndef ELF_INTERP_NON_RELOCATABLE
591 	/*
592 	 * If no position to load the interpreter was set by a probe
593 	 * function, pick the same address that a non-fixed mmap(0, ..)
594 	 * would (i.e. something safely out of the way).
595 	 */
596 	if (pos == ELFDEFNNAME(NO_ADDR))
597 		pos = round_page(epp->ep_daddr + MAXDSIZ);
598 #endif	/* !ELF_INTERP_NON_RELOCATABLE */
599 
600 	/*
601 	 * Check if we found a dynamically linked binary and arrange to load
602 	 * it's interpreter
603 	 */
604 	if (interp[0]) {
605 		struct elf_args *ap;
606 
607 		MALLOC(ap, struct elf_args *, sizeof(struct elf_args),
608 		    M_TEMP, M_WAITOK);
609 		if ((error = ELFNAME(load_file)(p, epp, interp,
610 		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
611 			FREE((char *)ap, M_TEMP);
612 			goto bad;
613 		}
614 		pos += phsize;
615 		ap->arg_phaddr = phdr;
616 
617 		ap->arg_phentsize = eh->e_phentsize;
618 		ap->arg_phnum = eh->e_phnum;
619 		ap->arg_entry = eh->e_entry;
620 
621 		epp->ep_emul_arg = ap;
622 	} else
623 		epp->ep_entry = eh->e_entry;
624 
625 #ifdef ELF_MAP_PAGE_ZERO
626 	/* Dell SVR4 maps page zero, yeuch! */
627 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
628 	    epp->ep_vp, 0, VM_PROT_READ);
629 #endif
630 	FREE(interp, M_TEMP);
631 	free((char *)ph, M_TEMP);
632 	epp->ep_vp->v_flag |= VTEXT;
633 	return exec_elf_setup_stack(p, epp);
634 
635 bad:
636 	if (interp)
637 		FREE(interp, M_TEMP);
638 	free((char *)ph, M_TEMP);
639 	kill_vmcmds(&epp->ep_vmcmds);
640 	return ENOEXEC;
641 }
642 
643 int
644 ELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp,
645     Elf_Ehdr *eh)
646 {
647 	size_t i;
648 	Elf_Phdr *ph;
649 	size_t phsize;
650 	int error;
651 
652 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
653 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
654 	error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
655 	if (error)
656 		goto out;
657 
658 	for (i = 0; i < eh->e_phnum; i++) {
659 		Elf_Phdr *ephp = &ph[i];
660 		Elf_Nhdr *np;
661 
662 		if (ephp->p_type != PT_NOTE ||
663 		    ephp->p_filesz > 1024 ||
664 		    ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
665 			continue;
666 
667 		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
668 		error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
669 		    ephp->p_filesz);
670 		if (error)
671 			goto next;
672 
673 		if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG ||
674 		    np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
675 		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ ||
676 		    memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME,
677 		    ELF_NOTE_NETBSD_NAMESZ))
678 			goto next;
679 
680 		error = 0;
681 		free(np, M_TEMP);
682 		goto out;
683 
684 	next:
685 		free(np, M_TEMP);
686 		continue;
687 	}
688 
689 	error = ENOEXEC;
690 out:
691 	free(ph, M_TEMP);
692 	return (error);
693 }
694 
695 int
696 ELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp,
697     void *eh, char *itp, vaddr_t *pos)
698 {
699 	int error;
700 
701 	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
702 		return error;
703 	*pos = ELFDEFNNAME(NO_ADDR);
704 	return 0;
705 }
706