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