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