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