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