exec_elf32.c revision 1.51
1/*	$NetBSD: exec_elf32.c,v 1.51 2000/06/27 17:41:09 mrg 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	    Elf_Phdr *, Elf_Addr *, u_long *, 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
263	switch (eh->e_machine) {
264
265	ELFDEFNNAME(MACHDEP_ID_CASES)
266
267	default:
268		return ENOEXEC;
269	}
270
271	if (eh->e_type != type)
272		return ENOEXEC;
273
274	return 0;
275}
276
277/*
278 * elf_load_psection():
279 *
280 * Load a psection at the appropriate address
281 */
282void
283ELFNAME(load_psection)(vcset, vp, ph, addr, size, prot)
284	struct exec_vmcmd_set *vcset;
285	struct vnode *vp;
286	Elf_Phdr *ph;
287	Elf_Addr *addr;
288	u_long *size;
289	int *prot;
290{
291	u_long uaddr, msize, psize, rm, rf;
292	long diff, offset;
293
294	/*
295	 * If the user specified an address, then we load there.
296	 */
297	if (*addr != ELFDEFNNAME(NO_ADDR)) {
298		if (ph->p_align > 1) {
299			*addr = ELF_ROUND(*addr, ph->p_align);
300			uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
301		} else
302			uaddr = ph->p_vaddr;
303		diff = ph->p_vaddr - uaddr;
304	} else {
305		*addr = uaddr = ph->p_vaddr;
306		if (ph->p_align > 1)
307			*addr = ELF_TRUNC(uaddr, ph->p_align);
308		diff = uaddr - *addr;
309	}
310
311	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
312	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
313	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
314
315	offset = ph->p_offset - diff;
316	*size = ph->p_filesz + diff;
317	msize = ph->p_memsz + diff;
318	psize = round_page(*size);
319
320	if ((ph->p_flags & PF_W) != 0) {
321		/*
322		 * Because the pagedvn pager can't handle zero fill of the last
323		 * data page if it's not page aligned we map the last page
324		 * readvn.
325		 */
326		psize = trunc_page(*size);
327		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
328		    offset, *prot);
329		if(psize != *size)
330			NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
331			    *addr + psize, vp, offset + psize, *prot);
332	} else
333		NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
334		    offset, *prot);
335
336	/*
337	 * Check if we need to extend the size of the segment
338	 */
339	rm = round_page(*addr + msize);
340	rf = round_page(*addr + *size);
341
342	if (rm != rf) {
343		NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
344		    0, *prot);
345		*size = msize;
346	}
347}
348
349/*
350 * elf_read_from():
351 *
352 *	Read from vnode into buffer at offset.
353 */
354int
355ELFNAME(read_from)(p, vp, off, buf, size)
356	struct vnode *vp;
357	u_long off;
358	struct proc *p;
359	caddr_t buf;
360	int size;
361{
362	int error;
363	size_t resid;
364
365	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
366	    0, p->p_ucred, &resid, p)) != 0)
367		return error;
368	/*
369	 * See if we got all of it
370	 */
371	if (resid != 0)
372		return ENOEXEC;
373	return 0;
374}
375
376/*
377 * elf_load_file():
378 *
379 * Load a file (interpreter/library) pointed to by path
380 * [stolen from coff_load_shlib()]. Made slightly generic
381 * so it might be used externally.
382 */
383int
384ELFNAME(load_file)(p, epp, path, vcset, entry, ap, last)
385	struct proc *p;
386	struct exec_package *epp;
387	char *path;
388	struct exec_vmcmd_set *vcset;
389	u_long *entry;
390	struct elf_args	*ap;
391	Elf_Addr *last;
392{
393	int error, i;
394	struct nameidata nd;
395	struct vnode *vp;
396	struct vattr attr;
397	Elf_Ehdr eh;
398	Elf_Phdr *ph = NULL;
399	u_long phsize;
400	char *bp = NULL;
401	Elf_Addr addr = *last;
402
403	bp = path;
404	/*
405	 * 1. open file
406	 * 2. read filehdr
407	 * 3. map text, data, and bss out of it using VM_*
408	 */
409	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
410	if ((error = namei(&nd)) != 0)
411		return error;
412	vp = nd.ni_vp;
413
414	/*
415	 * Similarly, if it's not marked as executable, or it's not a regular
416	 * file, we don't allow it to be used.
417	 */
418	if (vp->v_type != VREG) {
419		error = EACCES;
420		goto badunlock;
421	}
422	if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
423		goto badunlock;
424
425	/* get attributes */
426	if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
427		goto badunlock;
428
429	/*
430	 * Check mount point.  Though we're not trying to exec this binary,
431	 * we will be executing code from it, so if the mount point
432	 * disallows execution or set-id-ness, we punt or kill the set-id.
433	 */
434	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
435		error = EACCES;
436		goto badunlock;
437	}
438	if (vp->v_mount->mnt_flag & MNT_NOSUID)
439		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
440
441#ifdef notyet /* XXX cgd 960926 */
442	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
443#endif
444	VOP_UNLOCK(vp, 0);
445
446	if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
447	    sizeof(eh))) != 0)
448		goto bad;
449
450	if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
451		goto bad;
452
453	phsize = eh.e_phnum * sizeof(Elf_Phdr);
454	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
455
456	if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
457	    (caddr_t) ph, phsize)) != 0)
458		goto bad;
459
460	/*
461	 * Load all the necessary sections
462	 */
463	for (i = 0; i < eh.e_phnum; i++) {
464		u_long size = 0;
465		int prot = 0;
466
467		switch (ph[i].p_type) {
468		case PT_LOAD:
469			ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
470			    &size, &prot);
471			/* If entry is within this section it must be text */
472			if (eh.e_entry >= ph[i].p_vaddr &&
473			    eh.e_entry < (ph[i].p_vaddr + size)) {
474				/* XXX */
475				*entry = addr + eh.e_entry;
476#ifdef mips
477				*entry -= ph[i].p_vaddr;
478#endif
479				ap->arg_interp = addr;
480			}
481			addr += size;
482			break;
483
484		case PT_DYNAMIC:
485		case PT_PHDR:
486		case PT_NOTE:
487			break;
488
489		default:
490			break;
491		}
492	}
493
494	free((char *)ph, M_TEMP);
495	*last = addr;
496	vrele(vp);
497	return 0;
498
499badunlock:
500	VOP_UNLOCK(vp, 0);
501
502bad:
503	if (ph != NULL)
504		free((char *)ph, M_TEMP);
505#ifdef notyet /* XXX cgd 960926 */
506	(maybe) VOP_CLOSE it
507#endif
508	vrele(vp);
509	return error;
510}
511
512/*
513 * exec_elf_makecmds(): Prepare an Elf binary's exec package
514 *
515 * First, set of the various offsets/lengths in the exec package.
516 *
517 * Then, mark the text image busy (so it can be demand paged) or error
518 * out if this is not possible.  Finally, set up vmcmds for the
519 * text, data, bss, and stack segments.
520 */
521int
522ELFNAME2(exec,makecmds)(p, epp)
523	struct proc *p;
524	struct exec_package *epp;
525{
526	Elf_Ehdr *eh = epp->ep_hdr;
527	Elf_Phdr *ph, *pp;
528	Elf_Addr phdr = 0, pos = 0;
529	int error, i, n, nload;
530	char interp[MAXPATHLEN];
531	u_long phsize;
532
533	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
534		return ENOEXEC;
535
536	/*
537	 * XXX allow for executing shared objects. It seems silly
538	 * but other ELF-based systems allow it as well.
539	 */
540	if (ELFNAME(check_header)(eh, ET_EXEC) != 0 &&
541	    ELFNAME(check_header)(eh, ET_DYN) != 0)
542		return ENOEXEC;
543
544	/*
545	 * check if vnode is in open for writing, because we want to
546	 * demand-page out of it.  if it is, don't do it, for various
547	 * reasons
548	 */
549	if (epp->ep_vp->v_writecount != 0) {
550#ifdef DIAGNOSTIC
551		if (epp->ep_vp->v_flag & VTEXT)
552			panic("exec: a VTEXT vnode has writecount != 0\n");
553#endif
554		return ETXTBSY;
555	}
556	/*
557	 * Allocate space to hold all the program headers, and read them
558	 * from the file
559	 */
560	phsize = eh->e_phnum * sizeof(Elf_Phdr);
561	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
562
563	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
564	    (caddr_t) ph, phsize)) != 0)
565		goto bad;
566
567	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
568	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
569
570	interp[0] = '\0';
571
572	for (i = 0; i < eh->e_phnum; i++) {
573		pp = &ph[i];
574		if (pp->p_type == PT_INTERP) {
575			if (pp->p_filesz >= sizeof(interp))
576				goto bad;
577			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
578			    pp->p_offset, (caddr_t) interp,
579			    pp->p_filesz)) != 0)
580				goto bad;
581			break;
582		}
583	}
584
585	/*
586	 * Setup things for native emulation.
587	 */
588	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
589	pos = ELFDEFNNAME(NO_ADDR);
590
591	/*
592	 * On the same architecture, we may be emulating different systems.
593	 * See which one will accept this executable. This currently only
594	 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386
595	 * and the Alpha.
596	 *
597	 * Probe functions would normally see if the interpreter (if any)
598	 * exists. Emulation packages may possibly replace the interpreter in
599	 * interp[] with a changed path (/emul/xxx/<path>), and also
600	 * set the ep_emul field in the exec package structure.
601	 */
602	n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]);
603	if (n != 0) {
604		error = ENOEXEC;
605		for (i = 0; i < n && error; i++)
606			error = ELFNAME(probe_funcs)[i](p, epp, eh,
607			    interp, &pos);
608#ifdef notyet
609		/*
610		 * We should really use a signature in our native binaries
611		 * and have our own probe function for matching binaries,
612		 * before trying the emulations. For now, if the emulation
613		 * probes failed we default to native.
614		 */
615		if (error)
616			goto bad;
617#endif
618	}
619
620	/*
621	 * Load all the necessary sections
622	 */
623	for (i = nload = 0; i < eh->e_phnum; i++) {
624		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
625		u_long size = 0;
626		int prot = 0;
627
628		pp = &ph[i];
629
630		switch (ph[i].p_type) {
631		case PT_LOAD:
632			/*
633			 * XXX
634			 * Can handle only 2 sections: text and data
635			 */
636			if (nload++ == 2)
637				goto bad;
638			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
639			    &ph[i], &addr, &size, &prot);
640
641			/*
642			 * Decide whether it's text or data by looking
643			 * at the entry point.
644			 */
645			if (eh->e_entry >= addr &&
646			    eh->e_entry < (addr + size)) {
647				epp->ep_taddr = addr;
648				epp->ep_tsize = size;
649				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
650					epp->ep_daddr = addr;
651					epp->ep_dsize = size;
652				}
653			} else {
654				epp->ep_daddr = addr;
655				epp->ep_dsize = size;
656			}
657			break;
658
659		case PT_SHLIB:
660#ifndef COMPAT_IBCS2			/* SCO has these sections */
661			error = ENOEXEC;
662			goto bad;
663#endif
664
665		case PT_INTERP:
666			/* Already did this one */
667		case PT_DYNAMIC:
668		case PT_NOTE:
669			break;
670
671		case PT_PHDR:
672			/* Note address of program headers (in text segment) */
673			phdr = pp->p_vaddr;
674			break;
675
676		default:
677			/*
678			 * Not fatal; we don't need to understand everything.
679			 */
680			break;
681		}
682	}
683
684	/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
685#ifndef ELF_INTERP_NON_RELOCATABLE
686	/*
687	 * If no position to load the interpreter was set by a probe
688	 * function, pick the same address that a non-fixed mmap(0, ..)
689	 * would (i.e. something safely out of the way).
690	 */
691	if (pos == ELFDEFNNAME(NO_ADDR))
692		pos = round_page(epp->ep_daddr + MAXDSIZ);
693#endif	/* !ELF_INTERP_NON_RELOCATABLE */
694
695	/*
696	 * Check if we found a dynamically linked binary and arrange to load
697	 * it's interpreter
698	 */
699	if (interp[0]) {
700		struct elf_args *ap;
701
702		ap = (struct elf_args *)malloc(sizeof(struct elf_args),
703		    M_TEMP, M_WAITOK);
704		if ((error = ELFNAME(load_file)(p, epp, interp,
705		    &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
706			free((char *)ap, M_TEMP);
707			goto bad;
708		}
709		pos += phsize;
710		ap->arg_phaddr = phdr;
711
712		ap->arg_phentsize = eh->e_phentsize;
713		ap->arg_phnum = eh->e_phnum;
714		ap->arg_entry = eh->e_entry;
715
716		epp->ep_emul_arg = ap;
717	} else
718		epp->ep_entry = eh->e_entry;
719
720#ifdef ELF_MAP_PAGE_ZERO
721	/* Dell SVR4 maps page zero, yeuch! */
722	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
723	    VM_PROT_READ);
724#endif
725	free((char *)ph, M_TEMP);
726	vn_marktext(epp->ep_vp);
727	return exec_elf_setup_stack(p, epp);
728
729bad:
730	free((char *)ph, M_TEMP);
731	kill_vmcmds(&epp->ep_vmcmds);
732	return ENOEXEC;
733}
734
735int
736ELFNAME2(netbsd,signature)(p, epp, eh)
737	struct proc *p;
738	struct exec_package *epp;
739	Elf_Ehdr *eh;
740{
741	Elf_Phdr *hph, *ph;
742	Elf_Nhdr *np = NULL;
743	size_t phsize;
744	int error;
745
746	phsize = eh->e_phnum * sizeof(Elf_Phdr);
747	hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
748	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
749	    (caddr_t)hph, phsize)) != 0)
750		goto out1;
751
752	for (ph = hph;  ph < &hph[eh->e_phnum]; ph++) {
753		if (ph->p_type != PT_NOTE ||
754		    ph->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
755			continue;
756
757		np = (Elf_Nhdr *)malloc(ph->p_filesz, M_TEMP, M_WAITOK);
758		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset,
759		    (caddr_t)np, ph->p_filesz)) != 0)
760			goto out2;
761
762		if (np->n_type != ELF_NOTE_TYPE_OSVERSION) {
763			free(np, M_TEMP);
764			np = NULL;
765			continue;
766		}
767
768		/* Check the name and description sizes. */
769		if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
770		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ)
771			goto out3;
772
773		/* Is the name "NetBSD\0\0"? */
774		if (memcmp((np + 1), ELF_NOTE_NETBSD_NAME,
775		    ELF_NOTE_NETBSD_NAMESZ))
776			goto out3;
777
778		/* XXX: We could check for the specific emulation here */
779		/* All checks succeeded. */
780		error = 0;
781		goto out2;
782	}
783
784out3:
785	error = ENOEXEC;
786out2:
787	if (np)
788		free(np, M_TEMP);
789out1:
790	free(hph, M_TEMP);
791	return error;
792}
793
794static int
795ELFNAME2(netbsd,probe)(p, epp, eh, itp, pos)
796	struct proc *p;
797	struct exec_package *epp;
798	Elf_Ehdr *eh;
799	char *itp;
800	Elf_Addr *pos;
801{
802	int error;
803
804	if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
805		return error;
806	epp->ep_emul = &ELFNAMEEND(emul_netbsd);
807	*pos = ELFDEFNNAME(NO_ADDR);
808	return 0;
809}
810