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