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