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