xref: /netbsd-src/sys/kern/exec_elf32.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: exec_elf32.c,v 1.114 2006/05/18 17:35:49 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.114 2006/05/18 17:35:49 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 
91 #include <machine/cpu.h>
92 #include <machine/reg.h>
93 
94 #ifdef PAX_MPROTECT
95 #include <sys/pax.h>
96 #endif /* PAX_MPROTECT */
97 
98 extern const struct emul emul_netbsd;
99 
100 #define elf_check_header	ELFNAME(check_header)
101 #define elf_copyargs		ELFNAME(copyargs)
102 #define elf_load_file		ELFNAME(load_file)
103 #define elf_load_psection	ELFNAME(load_psection)
104 #define exec_elf_makecmds	ELFNAME2(exec,makecmds)
105 #define netbsd_elf_signature	ELFNAME2(netbsd,signature)
106 #define netbsd_elf_probe	ELFNAME2(netbsd,probe)
107 
108 int	elf_load_file(struct lwp *, struct exec_package *, char *,
109 	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
110 void	elf_load_psection(struct exec_vmcmd_set *, struct vnode *,
111 	    const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
112 
113 int	netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *);
114 int	netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *,
115 	    vaddr_t *);
116 
117 /* round up and down to page boundaries. */
118 #define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
119 #define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
120 
121 #define MAXPHNUM	50
122 
123 /*
124  * Copy arguments onto the stack in the normal way, but add some
125  * extra information in case of dynamic binding.
126  */
127 int
128 elf_copyargs(struct lwp *l, struct exec_package *pack,
129     struct ps_strings *arginfo, char **stackp, void *argp)
130 {
131 	size_t len;
132 	AuxInfo ai[ELF_AUX_ENTRIES], *a;
133 	struct elf_args *ap;
134 	struct proc *p;
135 	int error;
136 
137 	if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
138 		return error;
139 
140 	a = ai;
141 	p = l->l_proc;
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(p->p_cred);
183 		a++;
184 
185 		a->a_type = AT_RUID;
186 		a->a_v = kauth_cred_getuid(p->p_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(p->p_cred);
194 		a++;
195 
196 		a->a_type = AT_RGID;
197 		a->a_v = kauth_cred_getgid(p->p_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  */
221 int
222 elf_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  */
254 void
255 elf_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  */
340 int
341 elf_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 nameidata nd;
347 	struct vnode *vp;
348 	struct vattr attr;
349 	Elf_Ehdr eh;
350 	Elf_Phdr *ph = NULL;
351 	const Elf_Phdr *ph0;
352 	const Elf_Phdr *base_ph;
353 	const Elf_Phdr *last_ph;
354 	u_long phsize;
355 	Elf_Addr addr = *last;
356 	struct proc *p;
357 
358 	p = l->l_proc;
359 
360 	/*
361 	 * 1. open file
362 	 * 2. read filehdr
363 	 * 3. map text, data, and bss out of it using VM_*
364 	 */
365 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, l);
366 	if ((error = namei(&nd)) != 0)
367 		return error;
368 	vp = nd.ni_vp;
369 
370 	/*
371 	 * Similarly, if it's not marked as executable, or it's not a regular
372 	 * file, we don't allow it to be used.
373 	 */
374 	if (vp->v_type != VREG) {
375 		error = EACCES;
376 		goto badunlock;
377 	}
378 	if ((error = VOP_ACCESS(vp, VEXEC, l->l_proc->p_cred, l)) != 0)
379 		goto badunlock;
380 
381 	/* get attributes */
382 	if ((error = VOP_GETATTR(vp, &attr, l->l_proc->p_cred, l)) != 0)
383 		goto badunlock;
384 
385 	/*
386 	 * Check mount point.  Though we're not trying to exec this binary,
387 	 * we will be executing code from it, so if the mount point
388 	 * disallows execution or set-id-ness, we punt or kill the set-id.
389 	 */
390 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
391 		error = EACCES;
392 		goto badunlock;
393 	}
394 	if (vp->v_mount->mnt_flag & MNT_NOSUID)
395 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
396 
397 #ifdef notyet /* XXX cgd 960926 */
398 	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
399 #endif
400 
401 	error = vn_marktext(vp);
402 	if (error)
403 		goto badunlock;
404 
405 	VOP_UNLOCK(vp, 0);
406 
407 	if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0)
408 		goto bad;
409 
410 	if ((error = elf_check_header(&eh, ET_DYN)) != 0)
411 		goto bad;
412 
413 	if (eh.e_phnum > MAXPHNUM)
414 		goto bad;
415 
416 	phsize = eh.e_phnum * sizeof(Elf_Phdr);
417 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
418 
419 	if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0)
420 		goto bad;
421 
422 #ifdef ELF_INTERP_NON_RELOCATABLE
423 	/*
424 	 * Evil hack:  Only MIPS should be non-relocatable, and the
425 	 * psections should have a high address (typically 0x5ffe0000).
426 	 * If it's now relocatable, it should be linked at 0 and the
427 	 * psections should have zeros in the upper part of the address.
428 	 * Otherwise, force the load at the linked address.
429 	 */
430 	if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0)
431 		*last = ELFDEFNNAME(NO_ADDR);
432 #endif
433 
434 	/*
435 	 * If no position to load the interpreter was set by a probe
436 	 * function, pick the same address that a non-fixed mmap(0, ..)
437 	 * would (i.e. something safely out of the way).
438 	 */
439 	if (*last == ELFDEFNNAME(NO_ADDR)) {
440 		u_long limit = 0;
441 		/*
442 		 * Find the start and ending addresses of the psections to
443 		 * be loaded.  This will give us the size.
444 		 */
445 		for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum;
446 		     i++, ph0++) {
447 			if (ph0->p_type == PT_LOAD) {
448 				u_long psize = ph0->p_vaddr + ph0->p_memsz;
449 				if (base_ph == NULL)
450 					base_ph = ph0;
451 				if (psize > limit)
452 					limit = psize;
453 			}
454 		}
455 
456 		if (base_ph == NULL) {
457 			error = ENOEXEC;
458 			goto bad;
459 		}
460 
461 		/*
462 		 * Now compute the size and load address.
463 		 */
464 		addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p,
465 		    epp->ep_daddr,
466 		    round_page(limit) - trunc_page(base_ph->p_vaddr));
467 	} else
468 		addr = *last; /* may be ELF_LINK_ADDR */
469 
470 	/*
471 	 * Load all the necessary sections
472 	 */
473 	for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL;
474 	     i < eh.e_phnum; i++, ph0++) {
475 		switch (ph0->p_type) {
476 		case PT_LOAD: {
477 			u_long size;
478 			int prot = 0;
479 			int flags;
480 
481 			if (base_ph == NULL) {
482 				/*
483 				 * First encountered psection is always the
484 				 * base psection.  Make sure it's aligned
485 				 * properly (align down for topdown and align
486 				 * upwards for not topdown).
487 				 */
488 				base_ph = ph0;
489 				flags = VMCMD_BASE;
490 				if (addr == ELF_LINK_ADDR)
491 					addr = ph0->p_vaddr;
492 				if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)
493 					addr = ELF_TRUNC(addr, ph0->p_align);
494 				else
495 					addr = ELF_ROUND(addr, ph0->p_align);
496 			} else {
497 				u_long limit = round_page(last_ph->p_vaddr
498 				    + last_ph->p_memsz);
499 				u_long base = trunc_page(ph0->p_vaddr);
500 
501 				/*
502 				 * If there is a gap in between the psections,
503 				 * map it as inaccessible so nothing else
504 				 * mmap'ed will be placed there.
505 				 */
506 				if (limit != base) {
507 					NEW_VMCMD2(vcset, vmcmd_map_zero,
508 					    base - limit,
509 					    limit - base_ph->p_vaddr, NULLVP,
510 					    0, VM_PROT_NONE, VMCMD_RELATIVE);
511 				}
512 
513 				addr = ph0->p_vaddr - base_ph->p_vaddr;
514 				flags = VMCMD_RELATIVE;
515 			}
516 			last_ph = ph0;
517 			elf_load_psection(vcset, vp, &ph[i], &addr,
518 			    &size, &prot, flags);
519 			/*
520 			 * If entry is within this psection then this
521 			 * must contain the .text section.  *entryoff is
522 			 * relative to the base psection.
523 			 */
524 			if (eh.e_entry >= ph0->p_vaddr &&
525 			    eh.e_entry < (ph0->p_vaddr + size)) {
526 				*entryoff = eh.e_entry - base_ph->p_vaddr;
527 			}
528 			addr += size;
529 			break;
530 		}
531 
532 		case PT_DYNAMIC:
533 		case PT_PHDR:
534 			break;
535 
536 		case PT_NOTE:
537 #ifdef PAX_MPROTECT
538 			pax_mprotect_adjust(l, ph[i].p_flags);
539 			break;
540 #endif /* PAX_MPROTECT */
541 
542 		default:
543 			break;
544 		}
545 	}
546 
547 	free(ph, M_TEMP);
548 	/*
549 	 * This value is ignored if TOPDOWN.
550 	 */
551 	*last = addr;
552 	vrele(vp);
553 	return 0;
554 
555 badunlock:
556 	VOP_UNLOCK(vp, 0);
557 
558 bad:
559 	if (ph != NULL)
560 		free(ph, M_TEMP);
561 #ifdef notyet /* XXX cgd 960926 */
562 	(maybe) VOP_CLOSE it
563 #endif
564 	vrele(vp);
565 	return error;
566 }
567 
568 /*
569  * exec_elf_makecmds(): Prepare an Elf binary's exec package
570  *
571  * First, set of the various offsets/lengths in the exec package.
572  *
573  * Then, mark the text image busy (so it can be demand paged) or error
574  * out if this is not possible.  Finally, set up vmcmds for the
575  * text, data, bss, and stack segments.
576  */
577 int
578 exec_elf_makecmds(struct lwp *l, struct exec_package *epp)
579 {
580 	Elf_Ehdr *eh = epp->ep_hdr;
581 	Elf_Phdr *ph, *pp;
582 	Elf_Addr phdr = 0, pos = 0;
583 	int error, i, nload;
584 	char *interp = NULL;
585 	u_long phsize;
586 	struct proc *p;
587 
588 	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
589 		return ENOEXEC;
590 
591 	/*
592 	 * XXX allow for executing shared objects. It seems silly
593 	 * but other ELF-based systems allow it as well.
594 	 */
595 	if (elf_check_header(eh, ET_EXEC) != 0 &&
596 	    elf_check_header(eh, ET_DYN) != 0)
597 		return ENOEXEC;
598 
599 	if (eh->e_phnum > MAXPHNUM)
600 		return ENOEXEC;
601 
602 	error = vn_marktext(epp->ep_vp);
603 	if (error)
604 		return error;
605 
606 	/*
607 	 * Allocate space to hold all the program headers, and read them
608 	 * from the file
609 	 */
610 	p = l->l_proc;
611 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
612 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
613 
614 	if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
615 	    0)
616 		goto bad;
617 
618 	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
619 	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
620 
621 	for (i = 0; i < eh->e_phnum; i++) {
622 		pp = &ph[i];
623 		if (pp->p_type == PT_INTERP) {
624 			if (pp->p_filesz >= MAXPATHLEN)
625 				goto bad;
626 			interp = PNBUF_GET();
627 			interp[0] = '\0';
628 			if ((error = exec_read_from(l, epp->ep_vp,
629 			    pp->p_offset, interp, pp->p_filesz)) != 0)
630 				goto bad;
631 			break;
632 		}
633 	}
634 
635 	/*
636 	 * On the same architecture, we may be emulating different systems.
637 	 * See which one will accept this executable.
638 	 *
639 	 * Probe functions would normally see if the interpreter (if any)
640 	 * exists. Emulation packages may possibly replace the interpreter in
641 	 * interp[] with a changed path (/emul/xxx/<path>).
642 	 */
643 	pos = ELFDEFNNAME(NO_ADDR);
644 	if (epp->ep_esch->u.elf_probe_func) {
645 		vaddr_t startp = (vaddr_t)pos;
646 
647 		error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp,
648 							  &startp);
649 		if (error)
650 			goto bad;
651 		pos = (Elf_Addr)startp;
652 	}
653 
654 	/*
655 	 * Load all the necessary sections
656 	 */
657 	for (i = nload = 0; i < eh->e_phnum; i++) {
658 		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
659 		u_long size = 0;
660 		int prot = 0;
661 
662 		pp = &ph[i];
663 
664 		switch (ph[i].p_type) {
665 		case PT_LOAD:
666 			/*
667 			 * XXX
668 			 * Can handle only 2 sections: text and data
669 			 */
670 			if (nload++ == 2)
671 				goto bad;
672 			elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
673 			    &ph[i], &addr, &size, &prot, VMCMD_FIXED);
674 
675 			/*
676 			 * Decide whether it's text or data by looking
677 			 * at the entry point.
678 			 */
679 			if (eh->e_entry >= addr &&
680 			    eh->e_entry < (addr + size)) {
681 				epp->ep_taddr = addr;
682 				epp->ep_tsize = size;
683 				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
684 					epp->ep_daddr = addr;
685 					epp->ep_dsize = size;
686 				}
687 			} else {
688 				epp->ep_daddr = addr;
689 				epp->ep_dsize = size;
690 			}
691 			break;
692 
693 		case PT_SHLIB:
694 			/* SCO has these sections. */
695 		case PT_INTERP:
696 			/* Already did this one. */
697 		case PT_DYNAMIC:
698 		case PT_NOTE:
699 			break;
700 
701 		case PT_PHDR:
702 			/* Note address of program headers (in text segment) */
703 			phdr = pp->p_vaddr;
704 			break;
705 
706 		default:
707 			/*
708 			 * Not fatal; we don't need to understand everything.
709 			 */
710 			break;
711 		}
712 	}
713 
714 	/*
715 	 * Check if we found a dynamically linked binary and arrange to load
716 	 * its interpreter
717 	 */
718 	if (interp) {
719 		struct elf_args *ap;
720 		int j = epp->ep_vmcmds.evs_used;
721 		u_long interp_offset;
722 
723 		MALLOC(ap, struct elf_args *, sizeof(struct elf_args),
724 		    M_TEMP, M_WAITOK);
725 		if ((error = elf_load_file(l, epp, interp,
726 		    &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) {
727 			FREE(ap, M_TEMP);
728 			goto bad;
729 		}
730 		ap->arg_interp = epp->ep_vmcmds.evs_cmds[j].ev_addr;
731 		epp->ep_entry = ap->arg_interp + interp_offset;
732 		ap->arg_phaddr = phdr;
733 
734 		ap->arg_phentsize = eh->e_phentsize;
735 		ap->arg_phnum = eh->e_phnum;
736 		ap->arg_entry = eh->e_entry;
737 
738 		epp->ep_emul_arg = ap;
739 
740 		PNBUF_PUT(interp);
741 	} else
742 		epp->ep_entry = eh->e_entry;
743 
744 #ifdef ELF_MAP_PAGE_ZERO
745 	/* Dell SVR4 maps page zero, yeuch! */
746 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
747 	    epp->ep_vp, 0, VM_PROT_READ);
748 #endif
749 	free(ph, M_TEMP);
750 	return (*epp->ep_esch->es_setup_stack)(l, epp);
751 
752 bad:
753 	if (interp)
754 		PNBUF_PUT(interp);
755 	free(ph, M_TEMP);
756 	kill_vmcmds(&epp->ep_vmcmds);
757 	return ENOEXEC;
758 }
759 
760 int
761 netbsd_elf_signature(struct lwp *l, struct exec_package *epp,
762     Elf_Ehdr *eh)
763 {
764 	size_t i;
765 	Elf_Phdr *ph;
766 	size_t phsize;
767 	int error;
768 
769 	if (eh->e_phnum > MAXPHNUM)
770 		return ENOEXEC;
771 
772 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
773 	ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
774 	error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize);
775 	if (error)
776 		goto out;
777 
778 	for (i = 0; i < eh->e_phnum; i++) {
779 		Elf_Phdr *ephp = &ph[i];
780 		Elf_Nhdr *np;
781 
782 		if (ephp->p_type != PT_NOTE ||
783 		    ephp->p_filesz > 1024 ||
784 		    ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
785 			continue;
786 
787 		np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
788 		error = exec_read_from(l, epp->ep_vp, ephp->p_offset, np,
789 		    ephp->p_filesz);
790 		if (error)
791 			goto next;
792 
793 		if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG ||
794 		    np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
795 		    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ ||
796 		    memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME,
797 		    ELF_NOTE_NETBSD_NAMESZ))
798 			goto next;
799 
800 		error = 0;
801 		free(np, M_TEMP);
802 		goto out;
803 
804 	next:
805 		free(np, M_TEMP);
806 		continue;
807 	}
808 
809 	error = ENOEXEC;
810 out:
811 	free(ph, M_TEMP);
812 	return error;
813 }
814 
815 int
816 netbsd_elf_probe(struct lwp *l, struct exec_package *epp,
817     void *eh, char *itp, vaddr_t *pos)
818 {
819 	int error;
820 
821 	if ((error = netbsd_elf_signature(l, epp, eh)) != 0)
822 		return error;
823 #ifdef ELF_INTERP_NON_RELOCATABLE
824 	*pos = ELF_LINK_ADDR;
825 #endif
826 	return 0;
827 }
828