xref: /openbsd-src/sys/kern/exec_elf.c (revision d874cce4b1d9fe6b41c9e4f2117a77d8a4a37b92)
1 /*	$OpenBSD: exec_elf.c,v 1.65 2008/06/12 17:02:04 miod Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Per Fogelstrom
5  * All rights reserved.
6  *
7  * Copyright (c) 1994 Christos Zoulas
8  * All rights reserved.
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. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/malloc.h>
39 #include <sys/pool.h>
40 #include <sys/mount.h>
41 #include <sys/namei.h>
42 #include <sys/vnode.h>
43 #include <sys/exec.h>
44 #include <sys/exec_elf.h>
45 #include <sys/exec_olf.h>
46 #include <sys/file.h>
47 #include <sys/syscall.h>
48 #include <sys/signalvar.h>
49 #include <sys/stat.h>
50 
51 #include <sys/mman.h>
52 #include <uvm/uvm_extern.h>
53 
54 #include <machine/cpu.h>
55 #include <machine/reg.h>
56 #include <machine/exec.h>
57 
58 #ifdef COMPAT_LINUX
59 #include <compat/linux/linux_exec.h>
60 #endif
61 
62 #ifdef COMPAT_SVR4
63 #include <compat/svr4/svr4_exec.h>
64 #endif
65 
66 #ifdef COMPAT_FREEBSD
67 #include <compat/freebsd/freebsd_exec.h>
68 #endif
69 
70 struct ELFNAME(probe_entry) {
71 	int (*func)(struct proc *, struct exec_package *, char *,
72 	    u_long *, u_int8_t *);
73 } ELFNAME(probes)[] = {
74 	/* XXX - bogus, shouldn't be size independent.. */
75 #ifdef COMPAT_FREEBSD
76 	{ freebsd_elf_probe },
77 #endif
78 #ifdef COMPAT_LINUX
79 	{ linux_elf_probe },
80 #endif
81 #ifdef COMPAT_SVR4
82 	{ svr4_elf_probe },
83 #endif
84 	{ NULL }
85 };
86 
87 int ELFNAME(load_file)(struct proc *, char *, struct exec_package *,
88 	struct elf_args *, Elf_Addr *);
89 int ELFNAME(check_header)(Elf_Ehdr *, int);
90 int ELFNAME(read_from)(struct proc *, struct vnode *, u_long, caddr_t, int);
91 void ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *,
92 	Elf_Phdr *, Elf_Addr *, Elf_Addr *, int *, int);
93 
94 extern char sigcode[], esigcode[];
95 #ifdef SYSCALL_DEBUG
96 extern char *syscallnames[];
97 #endif
98 
99 /* round up and down to page boundaries. */
100 #define ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
101 #define ELF_TRUNC(a, b)		((a) & ~((b) - 1))
102 
103 /*
104  * We limit the number of program headers to 32, this should
105  * be a reasonable limit for ELF, the most we have seen so far is 12
106  */
107 #define ELF_MAX_VALID_PHDR 32
108 
109 /*
110  * This is the basic elf emul. elf_probe_funcs may change to other emuls.
111  */
112 struct emul ELFNAMEEND(emul) = {
113 	"native",
114 	NULL,
115 	sendsig,
116 	SYS_syscall,
117 	SYS_MAXSYSCALL,
118 	sysent,
119 #ifdef SYSCALL_DEBUG
120 	syscallnames,
121 #else
122 	NULL,
123 #endif
124 	sizeof (AuxInfo) * ELF_AUX_ENTRIES,
125 	ELFNAME(copyargs),
126 	setregs,
127 	ELFNAME2(exec,fixup),
128 	sigcode,
129 	esigcode,
130 	EMUL_ENABLED | EMUL_NATIVE,
131 };
132 
133 /*
134  * Copy arguments onto the stack in the normal way, but add some
135  * space for extra information in case of dynamic binding.
136  */
137 void *
138 ELFNAME(copyargs)(struct exec_package *pack, struct ps_strings *arginfo,
139 		void *stack, void *argp)
140 {
141 	stack = copyargs(pack, arginfo, stack, argp);
142 	if (!stack)
143 		return (NULL);
144 
145 	/*
146 	 * Push space for extra arguments on the stack needed by
147 	 * dynamically linked binaries.
148 	 */
149 	if (pack->ep_interp != NULL) {
150 		pack->ep_emul_argp = stack;
151 		stack = (char *)stack + ELF_AUX_ENTRIES * sizeof (AuxInfo);
152 	}
153 	return (stack);
154 }
155 
156 /*
157  * Check header for validity; return 0 for ok, ENOEXEC if error
158  */
159 int
160 ELFNAME(check_header)(Elf_Ehdr *ehdr, int type)
161 {
162 	/*
163 	 * We need to check magic, class size, endianess, and version before
164 	 * we look at the rest of the Elf_Ehdr structure. These few elements
165 	 * are represented in a machine independant fashion.
166 	 */
167 	if (!IS_ELF(*ehdr) ||
168 	    ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
169 	    ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
170 	    ehdr->e_ident[EI_VERSION] != ELF_TARG_VER)
171 		return (ENOEXEC);
172 
173 	/* Now check the machine dependant header */
174 	if (ehdr->e_machine != ELF_TARG_MACH ||
175 	    ehdr->e_version != ELF_TARG_VER)
176 		return (ENOEXEC);
177 
178 	/* Check the type */
179 	if (ehdr->e_type != type)
180 		return (ENOEXEC);
181 
182 	/* Don't allow an insane amount of sections. */
183 	if (ehdr->e_phnum > ELF_MAX_VALID_PHDR)
184 		return (ENOEXEC);
185 
186 	return (0);
187 }
188 
189 /*
190  * Load a psection at the appropriate address
191  */
192 void
193 ELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp,
194 	Elf_Phdr *ph, Elf_Addr *addr, Elf_Addr *size, int *prot, int flags)
195 {
196 	u_long uaddr, msize, lsize, psize, rm, rf;
197 	long diff, offset, bdiff;
198 	Elf_Addr base;
199 
200 	/*
201 	 * If the user specified an address, then we load there.
202 	 */
203 	if (*addr != ELFDEFNNAME(NO_ADDR)) {
204 		if (ph->p_align > 1) {
205 			*addr = ELF_TRUNC(*addr, ph->p_align);
206 			diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align);
207 			/* page align vaddr */
208 			base = *addr + trunc_page(ph->p_vaddr)
209 			    - ELF_TRUNC(ph->p_vaddr, ph->p_align);
210 
211 			bdiff = ph->p_vaddr - trunc_page(ph->p_vaddr);
212 
213 		} else
214 			diff = 0;
215 	} else {
216 		*addr = uaddr = ph->p_vaddr;
217 		if (ph->p_align > 1)
218 			*addr = ELF_TRUNC(uaddr, ph->p_align);
219 		base = trunc_page(uaddr);
220 		bdiff = uaddr - base;
221 		diff = uaddr - *addr;
222 	}
223 
224 	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
225 	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
226 	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
227 
228 	msize = ph->p_memsz + diff;
229 	offset = ph->p_offset - bdiff;
230 	lsize = ph->p_filesz + bdiff;
231 	psize = round_page(lsize);
232 
233 	/*
234 	 * Because the pagedvn pager can't handle zero fill of the last
235 	 * data page if it's not page aligned we map the last page readvn.
236 	 */
237 	if (ph->p_flags & PF_W) {
238 		psize = trunc_page(lsize);
239 		if (psize > 0)
240 			NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, base, vp,
241 			    offset, *prot, flags);
242 		if (psize != lsize) {
243 			NEW_VMCMD2(vcset, vmcmd_map_readvn, lsize - psize,
244 			    base + psize, vp, offset + psize, *prot, flags);
245 		}
246 	} else {
247 		NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, base, vp, offset,
248 		    *prot, flags);
249 	}
250 
251 	/*
252 	 * Check if we need to extend the size of the segment
253 	 */
254 	rm = round_page(*addr + ph->p_memsz + diff);
255 	rf = round_page(*addr + ph->p_filesz + diff);
256 
257 	if (rm != rf) {
258 		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0,
259 		    *prot, flags);
260 	}
261 	*size = msize;
262 }
263 
264 /*
265  * Read from vnode into buffer at offset.
266  */
267 int
268 ELFNAME(read_from)(struct proc *p, struct vnode *vp, u_long off, caddr_t buf,
269 	int size)
270 {
271 	int error;
272 	size_t resid;
273 
274 	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
275 	    0, p->p_ucred, &resid, p)) != 0)
276 		return error;
277 	/*
278 	 * See if we got all of it
279 	 */
280 	if (resid != 0)
281 		return (ENOEXEC);
282 	return (0);
283 }
284 
285 /*
286  * Load a file (interpreter/library) pointed to by path [stolen from
287  * coff_load_shlib()]. Made slightly generic so it might be used externally.
288  */
289 int
290 ELFNAME(load_file)(struct proc *p, char *path, struct exec_package *epp,
291 	struct elf_args *ap, Elf_Addr *last)
292 {
293 	int error, i;
294 	struct nameidata nd;
295 	Elf_Ehdr eh;
296 	Elf_Phdr *ph = NULL;
297 	u_long phsize;
298 	Elf_Addr addr;
299 	struct vnode *vp;
300 	Elf_Phdr *base_ph = NULL;
301 	struct interp_ld_sec {
302 		Elf_Addr vaddr;
303 		u_long memsz;
304 	} loadmap[ELF_MAX_VALID_PHDR];
305 	int nload, idx = 0;
306 	Elf_Addr pos = *last;
307 	int file_align;
308 
309 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
310 	if ((error = namei(&nd)) != 0) {
311 		return (error);
312 	}
313 	vp = nd.ni_vp;
314 	if (vp->v_type != VREG) {
315 		error = EACCES;
316 		goto bad;
317 	}
318 	if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
319 		goto bad;
320 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
321 		error = EACCES;
322 		goto bad;
323 	}
324 	if ((error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) != 0)
325 		goto bad1;
326 	if ((error = ELFNAME(read_from)(p, nd.ni_vp, 0,
327 				    (caddr_t)&eh, sizeof(eh))) != 0)
328 		goto bad1;
329 
330 	if (ELFNAME(check_header)(&eh, ET_DYN)) {
331 		error = ENOEXEC;
332 		goto bad1;
333 	}
334 
335 	phsize = eh.e_phnum * sizeof(Elf_Phdr);
336 	ph = malloc(phsize, M_TEMP, M_WAITOK);
337 
338 	if ((error = ELFNAME(read_from)(p, nd.ni_vp, eh.e_phoff, (caddr_t)ph,
339 	    phsize)) != 0)
340 		goto bad1;
341 
342 	for (i = 0; i < eh.e_phnum; i++) {
343 		if (ph[i].p_type == PT_LOAD) {
344 			loadmap[idx].vaddr = trunc_page(ph[i].p_vaddr);
345 			loadmap[idx].memsz = round_page (ph[i].p_vaddr +
346 			    ph[i].p_memsz - loadmap[idx].vaddr);
347 			file_align = ph[i].p_align;
348 			idx++;
349 		}
350 	}
351 	nload = idx;
352 
353 	/*
354 	 * If no position to load the interpreter was set by a probe
355 	 * function, pick the same address that a non-fixed mmap(0, ..)
356 	 * would (i.e. something safely out of the way).
357 	 */
358 	if (pos == ELFDEFNNAME(NO_ADDR)) {
359 		pos = uvm_map_hint(p, VM_PROT_EXECUTE);
360 	}
361 
362 	pos = ELF_ROUND(pos, file_align);
363 	*last = epp->ep_interp_pos = pos;
364 	for (i = 0; i < nload;/**/) {
365 		vaddr_t	addr;
366 		struct	uvm_object *uobj;
367 		off_t	uoff;
368 		size_t	size;
369 
370 #ifdef this_needs_fixing
371 		if (i == 0) {
372 			uobj = &vp->v_uvm.u_obj;
373 			/* need to fix uoff */
374 		} else {
375 #endif
376 			uobj = NULL;
377 			uoff = 0;
378 #ifdef this_needs_fixing
379 		}
380 #endif
381 
382 		addr = trunc_page(pos + loadmap[i].vaddr);
383 		size =  round_page(addr + loadmap[i].memsz) - addr;
384 
385 		/* CRAP - map_findspace does not avoid daddr+MAXDSIZ */
386 		if ((addr + size > (vaddr_t)p->p_vmspace->vm_daddr) &&
387 		    (addr < (vaddr_t)p->p_vmspace->vm_daddr + MAXDSIZ))
388 			addr = round_page((vaddr_t)p->p_vmspace->vm_daddr +
389 			    MAXDSIZ);
390 
391 		if (uvm_map_findspace(&p->p_vmspace->vm_map, addr, size,
392 		    &addr, uobj, uoff, 0, UVM_FLAG_FIXED) == NULL) {
393 			if (uvm_map_findspace(&p->p_vmspace->vm_map, addr, size,
394 			    &addr, uobj, uoff, 0, 0) == NULL) {
395 				error = ENOMEM; /* XXX */
396 				goto bad1;
397 			}
398 		}
399 		if (addr != pos + loadmap[i].vaddr) {
400 			/* base changed. */
401 			pos = addr - trunc_page(loadmap[i].vaddr);
402 			pos = ELF_ROUND(pos,file_align);
403 			epp->ep_interp_pos = *last = pos;
404 			i = 0;
405 			continue;
406 		}
407 
408 		i++;
409 	}
410 
411 	/*
412 	 * Load all the necessary sections
413 	 */
414 	for (i = 0; i < eh.e_phnum; i++) {
415 		Elf_Addr size = 0;
416 		int prot = 0;
417 		int flags;
418 
419 		switch (ph[i].p_type) {
420 		case PT_LOAD:
421 			if (base_ph == NULL) {
422 				flags = VMCMD_BASE;
423 				addr = *last;
424 				base_ph = &ph[i];
425 			} else {
426 				flags = VMCMD_RELATIVE;
427 				addr = ph[i].p_vaddr - base_ph->p_vaddr;
428 			}
429 			ELFNAME(load_psection)(&epp->ep_vmcmds, nd.ni_vp,
430 			    &ph[i], &addr, &size, &prot, flags);
431 			/* If entry is within this section it must be text */
432 			if (eh.e_entry >= ph[i].p_vaddr &&
433 			    eh.e_entry < (ph[i].p_vaddr + size)) {
434  				epp->ep_entry = addr + eh.e_entry -
435 				    ELF_TRUNC(ph[i].p_vaddr,ph[i].p_align);
436 				ap->arg_interp = addr;
437 			}
438 			addr += size;
439 			break;
440 
441 		case PT_DYNAMIC:
442 		case PT_PHDR:
443 		case PT_NOTE:
444 			break;
445 
446 		default:
447 			break;
448 		}
449 	}
450 
451 	vn_marktext(nd.ni_vp);
452 
453 bad1:
454 	VOP_CLOSE(nd.ni_vp, FREAD, p->p_ucred, p);
455 bad:
456 	if (ph != NULL)
457 		free(ph, M_TEMP);
458 
459 	*last = addr;
460 	vput(nd.ni_vp);
461 	return (error);
462 }
463 
464 /*
465  * Prepare an Elf binary's exec package
466  *
467  * First, set of the various offsets/lengths in the exec package.
468  *
469  * Then, mark the text image busy (so it can be demand paged) or error out if
470  * this is not possible.  Finally, set up vmcmds for the text, data, bss, and
471  * stack segments.
472  */
473 int
474 ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp)
475 {
476 	Elf_Ehdr *eh = epp->ep_hdr;
477 	Elf_Phdr *ph, *pp;
478 	Elf_Addr phdr = 0;
479 	int error, i;
480 	char *interp = NULL;
481 	u_long pos = 0, phsize;
482 	u_int8_t os = OOS_NULL;
483 
484 	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
485 		return (ENOEXEC);
486 
487 	if (ELFNAME(check_header)(eh, ET_EXEC))
488 		return (ENOEXEC);
489 
490 	/*
491 	 * check if vnode is in open for writing, because we want to demand-
492 	 * page out of it.  if it is, don't do it, for various reasons.
493 	 */
494 	if (epp->ep_vp->v_writecount != 0) {
495 #ifdef DIAGNOSTIC
496 		if (epp->ep_vp->v_flag & VTEXT)
497 			panic("exec: a VTEXT vnode has writecount != 0");
498 #endif
499 		return (ETXTBSY);
500 	}
501 	/*
502 	 * Allocate space to hold all the program headers, and read them
503 	 * from the file
504 	 */
505 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
506 	ph = malloc(phsize, M_TEMP, M_WAITOK);
507 
508 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, (caddr_t)ph,
509 	    phsize)) != 0)
510 		goto bad;
511 
512 	epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
513 	epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
514 
515 	for (i = 0; i < eh->e_phnum; i++) {
516 		pp = &ph[i];
517 		if (pp->p_type == PT_INTERP) {
518 			if (pp->p_filesz >= MAXPATHLEN)
519 				goto bad;
520 			interp = pool_get(&namei_pool, PR_WAITOK);
521 			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
522 			    pp->p_offset, interp, pp->p_filesz)) != 0) {
523 				goto bad;
524 			}
525 			break;
526 		}
527 	}
528 
529 	/*
530 	 * OK, we want a slightly different twist of the
531 	 * standard emulation package for "real" elf.
532 	 */
533 	epp->ep_emul = &ELFNAMEEND(emul);
534 	pos = ELFDEFNNAME(NO_ADDR);
535 
536 	/*
537 	 * On the same architecture, we may be emulating different systems.
538 	 * See which one will accept this executable.
539 	 *
540 	 * Probe functions would normally see if the interpreter (if any)
541 	 * exists. Emulation packages may possibly replace the interpreter in
542 	 * *interp with a changed path (/emul/xxx/<path>), and also
543 	 * set the ep_emul field in the exec package structure.
544 	 */
545 	error = ENOEXEC;
546 	p->p_os = OOS_OPENBSD;
547 #ifdef NATIVE_EXEC_ELF
548 	if (ELFNAME(os_pt_note)(p, epp, epp->ep_hdr, "OpenBSD", 8, 4) == 0) {
549 		goto native;
550 	}
551 #endif
552 	for (i = 0; ELFNAME(probes)[i].func != NULL && error; i++) {
553 		error = (*ELFNAME(probes)[i].func)(p, epp, interp, &pos, &os);
554 	}
555 	if (!error)
556 		p->p_os = os;
557 #ifndef NATIVE_EXEC_ELF
558 	else
559 		goto bad;
560 #else
561 native:
562 #endif /* NATIVE_EXEC_ELF */
563 	/*
564 	 * Load all the necessary sections
565 	 */
566 	for (i = 0; i < eh->e_phnum; i++) {
567 		Elf_Addr addr = ELFDEFNNAME(NO_ADDR), size = 0;
568 		int prot = 0;
569 
570 		pp = &ph[i];
571 
572 		switch (ph[i].p_type) {
573 		case PT_LOAD:
574 			/*
575 			 * Calculates size of text and data segments
576 			 * by starting at first and going to end of last.
577 			 * 'rwx' sections are treated as data.
578 			 * this is correct for BSS_PLT, but may not be
579 			 * for DATA_PLT, is fine for TEXT_PLT.
580 			 */
581 			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
582 			    &ph[i], &addr, &size, &prot, 0);
583 			/*
584 			 * Decide whether it's text or data by looking
585 			 * at the protection of the section
586 			 */
587 			if (prot & VM_PROT_WRITE) {
588 				/* data section */
589 				if (epp->ep_dsize == ELFDEFNNAME(NO_ADDR)) {
590 					epp->ep_daddr = addr;
591 					epp->ep_dsize = size;
592 				} else {
593 					if (addr < epp->ep_daddr) {
594 						epp->ep_dsize =
595 						    epp->ep_dsize +
596 						    epp->ep_daddr -
597 						    addr;
598 						epp->ep_daddr = addr;
599 					} else
600 						epp->ep_dsize = addr+size -
601 						    epp->ep_daddr;
602 				}
603 			} else if (prot & VM_PROT_EXECUTE) {
604 				/* text section */
605 				if (epp->ep_tsize == ELFDEFNNAME(NO_ADDR)) {
606 					epp->ep_taddr = addr;
607 					epp->ep_tsize = size;
608 				} else {
609 					if (addr < epp->ep_taddr) {
610 						epp->ep_tsize =
611 						    epp->ep_tsize +
612 						    epp->ep_taddr -
613 						    addr;
614 						epp->ep_taddr = addr;
615 					} else
616 						epp->ep_tsize = addr+size -
617 						    epp->ep_taddr;
618 				}
619 			}
620 			break;
621 
622 		case PT_SHLIB:
623 			error = ENOEXEC;
624 			goto bad;
625 
626 		case PT_INTERP:
627 			/* Already did this one */
628 		case PT_DYNAMIC:
629 		case PT_NOTE:
630 			break;
631 
632 		case PT_PHDR:
633 			/* Note address of program headers (in text segment) */
634 			phdr = pp->p_vaddr;
635 			break;
636 
637 		default:
638 			/*
639 			 * Not fatal, we don't need to understand everything
640 			 * :-)
641 			 */
642 			break;
643 		}
644 	}
645 
646 	/*
647 	 * Strangely some linux programs may have all load sections marked
648 	 * writeable, in this case, textsize is not -1, but rather 0;
649 	 */
650 	if (epp->ep_tsize == ELFDEFNNAME(NO_ADDR))
651 		epp->ep_tsize = 0;
652 	/*
653 	 * Another possibility is that it has all load sections marked
654 	 * read-only.  Fake a zero-sized data segment right after the
655 	 * text segment.
656 	 */
657 	if (epp->ep_dsize == ELFDEFNNAME(NO_ADDR)) {
658 		epp->ep_daddr = round_page(epp->ep_taddr + epp->ep_tsize);
659 		epp->ep_dsize = 0;
660 	}
661 
662 	epp->ep_interp = interp;
663 	epp->ep_entry = eh->e_entry;
664 
665 	/*
666 	 * Check if we found a dynamically linked binary and arrange to load
667 	 * its interpreter when the exec file is released.
668 	 */
669 	if (interp) {
670 		struct elf_args *ap;
671 
672 		ap = malloc(sizeof(struct elf_args), M_TEMP, M_WAITOK);
673 
674 		ap->arg_phaddr = phdr;
675 		ap->arg_phentsize = eh->e_phentsize;
676 		ap->arg_phnum = eh->e_phnum;
677 		ap->arg_entry = eh->e_entry;
678 		ap->arg_os = os;
679 
680 		epp->ep_emul_arg = ap;
681 		epp->ep_interp_pos = pos;
682 	}
683 
684 #if defined(COMPAT_SVR4) && defined(i386) && 0	/* nothing sets OOS_DELL... */
685 #ifndef ELF_MAP_PAGE_ZERO
686 	/* Dell SVR4 maps page zero, yeuch! */
687 	if (p->p_os == OOS_DELL)
688 #endif
689 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
690 		    epp->ep_vp, 0, VM_PROT_READ);
691 #endif
692 
693 	free(ph, M_TEMP);
694 	vn_marktext(epp->ep_vp);
695 	return (exec_setup_stack(p, epp));
696 
697 bad:
698 	if (interp)
699 		pool_put(&namei_pool, interp);
700 	free(ph, M_TEMP);
701 	kill_vmcmds(&epp->ep_vmcmds);
702 	return (ENOEXEC);
703 }
704 
705 /*
706  * Phase II of load. It is now safe to load the interpreter. Info collected
707  * when loading the program is available for setup of the interpreter.
708  */
709 int
710 ELFNAME2(exec,fixup)(struct proc *p, struct exec_package *epp)
711 {
712 	char	*interp;
713 	int	error;
714 	struct	elf_args *ap;
715 	AuxInfo ai[ELF_AUX_ENTRIES], *a;
716 	Elf_Addr	pos = epp->ep_interp_pos;
717 
718 	if (epp->ep_interp == NULL) {
719 		return (0);
720 	}
721 
722 	interp = epp->ep_interp;
723 	ap = epp->ep_emul_arg;
724 
725 	if ((error = ELFNAME(load_file)(p, interp, epp, ap, &pos)) != 0) {
726 		free(ap, M_TEMP);
727 		pool_put(&namei_pool, interp);
728 		kill_vmcmds(&epp->ep_vmcmds);
729 		return (error);
730 	}
731 	/*
732 	 * We have to do this ourselves...
733 	 */
734 	error = exec_process_vmcmds(p, epp);
735 
736 	/*
737 	 * Push extra arguments on the stack needed by dynamically
738 	 * linked binaries
739 	 */
740 	if (error == 0) {
741 		a = ai;
742 
743 		a->au_id = AUX_phdr;
744 		a->au_v = ap->arg_phaddr;
745 		a++;
746 
747 		a->au_id = AUX_phent;
748 		a->au_v = ap->arg_phentsize;
749 		a++;
750 
751 		a->au_id = AUX_phnum;
752 		a->au_v = ap->arg_phnum;
753 		a++;
754 
755 		a->au_id = AUX_pagesz;
756 		a->au_v = PAGE_SIZE;
757 		a++;
758 
759 		a->au_id = AUX_base;
760 		a->au_v = ap->arg_interp;
761 		a++;
762 
763 		a->au_id = AUX_flags;
764 		a->au_v = 0;
765 		a++;
766 
767 		a->au_id = AUX_entry;
768 		a->au_v = ap->arg_entry;
769 		a++;
770 
771 		a->au_id = AUX_null;
772 		a->au_v = 0;
773 		a++;
774 
775 		error = copyout(ai, epp->ep_emul_argp, sizeof ai);
776 	}
777 	free(ap, M_TEMP);
778 	pool_put(&namei_pool, interp);
779 	return (error);
780 }
781 
782 /*
783  * Older ELF binaries use EI_ABIVERSION (formerly EI_BRAND) to brand
784  * executables.  Newer ELF binaries use EI_OSABI instead.
785  */
786 char *
787 ELFNAME(check_brand)(Elf_Ehdr *eh)
788 {
789 	if (eh->e_ident[EI_ABIVERSION] == '\0')
790 		return (NULL);
791 	return (&eh->e_ident[EI_ABIVERSION]);
792 }
793 
794 int
795 ELFNAME(os_pt_note)(struct proc *p, struct exec_package *epp, Elf_Ehdr *eh,
796 	char *os_name, size_t name_size, size_t desc_size)
797 {
798 	Elf_Phdr *hph, *ph;
799 	Elf_Note *np = NULL;
800 	size_t phsize;
801 	int error;
802 
803 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
804 	hph = malloc(phsize, M_TEMP, M_WAITOK);
805 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
806 	    (caddr_t)hph, phsize)) != 0)
807 		goto out1;
808 
809 	for (ph = hph;  ph < &hph[eh->e_phnum]; ph++) {
810 		if (ph->p_type != PT_NOTE ||
811 		    ph->p_filesz > 1024 ||
812 		    ph->p_filesz < sizeof(Elf_Note) + name_size)
813 			continue;
814 
815 		np = malloc(ph->p_filesz, M_TEMP, M_WAITOK);
816 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset,
817 		    (caddr_t)np, ph->p_filesz)) != 0)
818 			goto out2;
819 
820 #if 0
821 		if (np->type != ELF_NOTE_TYPE_OSVERSION) {
822 			free(np, M_TEMP);
823 			np = NULL;
824 			continue;
825 		}
826 #endif
827 
828 		/* Check the name and description sizes. */
829 		if (np->namesz != name_size ||
830 		    np->descsz != desc_size)
831 			goto out3;
832 
833 		if (bcmp((np + 1), os_name, name_size))
834 			goto out3;
835 
836 		/* XXX: We could check for the specific emulation here */
837 		/* All checks succeeded. */
838 		error = 0;
839 		goto out2;
840 	}
841 
842 out3:
843 	error = ENOEXEC;
844 out2:
845 	if (np)
846 		free(np, M_TEMP);
847 out1:
848 	free(hph, M_TEMP);
849 	return error;
850 }
851