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