exec_elf32.c revision 1.110
1/*	$NetBSD: exec_elf32.c,v 1.110 2006/03/17 08:51:35 erh 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.110 2006/03/17 08:51:35 erh 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		if (base_ph == NULL)
448		{
449			error = ENOEXEC;
450			goto bad;
451		}
452
453		/*
454		 * Now compute the size and load address.
455		 */
456		addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p,
457		    epp->ep_daddr,
458		    round_page(limit) - trunc_page(base_ph->p_vaddr));
459	} else
460		addr = *last; /* may be ELF_LINK_ADDR */
461
462	/*
463	 * Load all the necessary sections
464	 */
465	for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL;
466	     i < eh.e_phnum; i++, ph0++) {
467		switch (ph0->p_type) {
468		case PT_LOAD: {
469			u_long size;
470			int prot = 0;
471			int flags;
472
473			if (base_ph == NULL) {
474				/*
475				 * First encountered psection is always the
476				 * base psection.  Make sure it's aligned
477				 * properly (align down for topdown and align
478				 * upwards for not topdown).
479				 */
480				base_ph = ph0;
481				flags = VMCMD_BASE;
482				if (addr == ELF_LINK_ADDR)
483					addr = ph0->p_vaddr;
484				if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)
485					addr = ELF_TRUNC(addr, ph0->p_align);
486				else
487					addr = ELF_ROUND(addr, ph0->p_align);
488			} else {
489				u_long limit = round_page(last_ph->p_vaddr
490				    + last_ph->p_memsz);
491				u_long base = trunc_page(ph0->p_vaddr);
492
493				/*
494				 * If there is a gap in between the psections,
495				 * map it as inaccessible so nothing else
496				 * mmap'ed will be placed there.
497				 */
498				if (limit != base) {
499					NEW_VMCMD2(vcset, vmcmd_map_zero,
500					    base - limit,
501					    limit - base_ph->p_vaddr, NULLVP,
502					    0, VM_PROT_NONE, VMCMD_RELATIVE);
503				}
504
505				addr = ph0->p_vaddr - base_ph->p_vaddr;
506				flags = VMCMD_RELATIVE;
507			}
508			last_ph = ph0;
509			elf_load_psection(vcset, vp, &ph[i], &addr,
510			    &size, &prot, flags);
511			/*
512			 * If entry is within this psection then this
513			 * must contain the .text section.  *entryoff is
514			 * relative to the base psection.
515			 */
516			if (eh.e_entry >= ph0->p_vaddr &&
517			    eh.e_entry < (ph0->p_vaddr + size)) {
518				*entryoff = eh.e_entry - base_ph->p_vaddr;
519			}
520			addr += size;
521			break;
522		}
523
524		case PT_DYNAMIC:
525		case PT_PHDR:
526		case PT_NOTE:
527			break;
528
529		default:
530			break;
531		}
532	}
533
534	free(ph, M_TEMP);
535	/*
536	 * This value is ignored if TOPDOWN.
537	 */
538	*last = addr;
539	vrele(vp);
540	return 0;
541
542badunlock:
543	VOP_UNLOCK(vp, 0);
544
545bad:
546	if (ph != NULL)
547		free(ph, M_TEMP);
548#ifdef notyet /* XXX cgd 960926 */
549	(maybe) VOP_CLOSE it
550#endif
551	vrele(vp);
552	return error;
553}
554
555/*
556 * exec_elf_makecmds(): Prepare an Elf binary's exec package
557 *
558 * First, set of the various offsets/lengths in the exec package.
559 *
560 * Then, mark the text image busy (so it can be demand paged) or error
561 * out if this is not possible.  Finally, set up vmcmds for the
562 * text, data, bss, and stack segments.
563 */
564int
565exec_elf_makecmds(struct lwp *l, struct exec_package *epp)
566{
567	Elf_Ehdr *eh = epp->ep_hdr;
568	Elf_Phdr *ph, *pp;
569	Elf_Addr phdr = 0, pos = 0;
570	int error, i, nload;
571	char *interp = NULL;
572	u_long phsize;
573	struct proc *p;
574
575	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
576		return ENOEXEC;
577
578	/*
579	 * XXX allow for executing shared objects. It seems silly
580	 * but other ELF-based systems allow it as well.
581	 */
582	if (elf_check_header(eh, ET_EXEC) != 0 &&
583	    elf_check_header(eh, ET_DYN) != 0)
584		return ENOEXEC;
585
586	if (eh->e_phnum > MAXPHNUM)
587		return ENOEXEC;
588
589	error = vn_marktext(epp->ep_vp);
590	if (error)
591		return error;
592
593	/*
594	 * Allocate space to hold all the program headers, and read them
595	 * from the file
596	 */
597	p = l->l_proc;
598	phsize = eh->e_phnum * sizeof(Elf_Phdr);
599	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
600
601	if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
602	    0)
603		goto bad;
604
605	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
606	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
607
608	for (i = 0; i < eh->e_phnum; i++) {
609		pp = &ph[i];
610		if (pp->p_type == PT_INTERP) {
611			if (pp->p_filesz >= MAXPATHLEN)
612				goto bad;
613			interp = PNBUF_GET();
614			interp[0] = '\0';
615			if ((error = exec_read_from(l, epp->ep_vp,
616			    pp->p_offset, interp, pp->p_filesz)) != 0)
617				goto bad;
618			break;
619		}
620	}
621
622	/*
623	 * On the same architecture, we may be emulating different systems.
624	 * See which one will accept this executable.
625	 *
626	 * Probe functions would normally see if the interpreter (if any)
627	 * exists. Emulation packages may possibly replace the interpreter in
628	 * interp[] with a changed path (/emul/xxx/<path>).
629	 */
630	pos = ELFDEFNNAME(NO_ADDR);
631	if (epp->ep_esch->u.elf_probe_func) {
632		vaddr_t startp = (vaddr_t)pos;
633
634		error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp,
635							  &startp);
636		if (error)
637			goto bad;
638		pos = (Elf_Addr)startp;
639	}
640
641	/*
642	 * Load all the necessary sections
643	 */
644	for (i = nload = 0; i < eh->e_phnum; i++) {
645		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
646		u_long size = 0;
647		int prot = 0;
648
649		pp = &ph[i];
650
651		switch (ph[i].p_type) {
652		case PT_LOAD:
653			/*
654			 * XXX
655			 * Can handle only 2 sections: text and data
656			 */
657			if (nload++ == 2)
658				goto bad;
659			elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
660			    &ph[i], &addr, &size, &prot, VMCMD_FIXED);
661
662			/*
663			 * Decide whether it's text or data by looking
664			 * at the entry point.
665			 */
666			if (eh->e_entry >= addr &&
667			    eh->e_entry < (addr + size)) {
668				epp->ep_taddr = addr;
669				epp->ep_tsize = size;
670				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
671					epp->ep_daddr = addr;
672					epp->ep_dsize = size;
673				}
674			} else {
675				epp->ep_daddr = addr;
676				epp->ep_dsize = size;
677			}
678			break;
679
680		case PT_SHLIB:
681			/* SCO has these sections. */
682		case PT_INTERP:
683			/* Already did this one. */
684		case PT_DYNAMIC:
685		case PT_NOTE:
686			break;
687
688		case PT_PHDR:
689			/* Note address of program headers (in text segment) */
690			phdr = pp->p_vaddr;
691			break;
692
693		default:
694			/*
695			 * Not fatal; we don't need to understand everything.
696			 */
697			break;
698		}
699	}
700
701	/*
702	 * Check if we found a dynamically linked binary and arrange to load
703	 * its interpreter
704	 */
705	if (interp) {
706		struct elf_args *ap;
707		int j = epp->ep_vmcmds.evs_used;
708		u_long interp_offset;
709
710		MALLOC(ap, struct elf_args *, sizeof(struct elf_args),
711		    M_TEMP, M_WAITOK);
712		if ((error = elf_load_file(l, epp, interp,
713		    &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) {
714			FREE(ap, M_TEMP);
715			goto bad;
716		}
717		ap->arg_interp = epp->ep_vmcmds.evs_cmds[j].ev_addr;
718		epp->ep_entry = ap->arg_interp + interp_offset;
719		ap->arg_phaddr = phdr;
720
721		ap->arg_phentsize = eh->e_phentsize;
722		ap->arg_phnum = eh->e_phnum;
723		ap->arg_entry = eh->e_entry;
724
725		epp->ep_emul_arg = ap;
726
727		PNBUF_PUT(interp);
728	} else
729		epp->ep_entry = eh->e_entry;
730
731#ifdef ELF_MAP_PAGE_ZERO
732	/* Dell SVR4 maps page zero, yeuch! */
733	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
734	    epp->ep_vp, 0, VM_PROT_READ);
735#endif
736	free(ph, M_TEMP);
737	return (*epp->ep_esch->es_setup_stack)(l, epp);
738
739bad:
740	if (interp)
741		PNBUF_PUT(interp);
742	free(ph, M_TEMP);
743	kill_vmcmds(&epp->ep_vmcmds);
744	return ENOEXEC;
745}
746
747int
748netbsd_elf_signature(struct lwp *l, struct exec_package *epp,
749    Elf_Ehdr *eh)
750{
751	size_t i;
752	Elf_Phdr *ph;
753	size_t phsize;
754	int error;
755
756	if (eh->e_phnum > MAXPHNUM)
757		return ENOEXEC;
758
759	phsize = eh->e_phnum * sizeof(Elf_Phdr);
760	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
761	error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize);
762	if (error)
763		goto out;
764
765	for (i = 0; i < eh->e_phnum; i++) {
766		Elf_Phdr *ephp = &ph[i];
767		Elf_Nhdr *np;
768
769		if (ephp->p_type != PT_NOTE ||
770		    ephp->p_filesz > 1024 ||
771		    ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
772			continue;
773
774		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
775		error = exec_read_from(l, epp->ep_vp, ephp->p_offset, np,
776		    ephp->p_filesz);
777		if (error)
778			goto next;
779
780		if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG ||
781		    np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
782		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ ||
783		    memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME,
784		    ELF_NOTE_NETBSD_NAMESZ))
785			goto next;
786
787		error = 0;
788		free(np, M_TEMP);
789		goto out;
790
791	next:
792		free(np, M_TEMP);
793		continue;
794	}
795
796	error = ENOEXEC;
797out:
798	free(ph, M_TEMP);
799	return error;
800}
801
802int
803netbsd_elf_probe(struct lwp *l, struct exec_package *epp,
804    void *eh, char *itp, vaddr_t *pos)
805{
806	int error;
807
808	if ((error = netbsd_elf_signature(l, epp, eh)) != 0)
809		return error;
810#ifdef ELF_INTERP_NON_RELOCATABLE
811	*pos = ELF_LINK_ADDR;
812#endif
813	return 0;
814}
815