xref: /freebsd/sys/kern/imgact_elf.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
1 /*-
2  * Copyright (c) 1995-1996 S�ren Schmidt
3  * Copyright (c) 1996 Peter Wemm
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software withough specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *	$Id: imgact_elf.c,v 1.57 1999/05/14 23:09:00 alc Exp $
30  */
31 
32 #include "opt_rlimit.h"
33 
34 #include <sys/param.h>
35 #include <sys/acct.h>
36 #include <sys/exec.h>
37 #include <sys/fcntl.h>
38 #include <sys/imgact.h>
39 #include <sys/imgact_elf.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mman.h>
43 #include <sys/namei.h>
44 #include <sys/pioctl.h>
45 #include <sys/proc.h>
46 #include <sys/procfs.h>
47 #include <sys/resourcevar.h>
48 #include <sys/signalvar.h>
49 #include <sys/stat.h>
50 #include <sys/syscall.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysent.h>
53 #include <sys/systm.h>
54 #include <sys/vnode.h>
55 
56 #include <vm/vm.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_param.h>
59 #include <vm/pmap.h>
60 #include <sys/lock.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_prot.h>
64 #include <vm/vm_extern.h>
65 
66 #include <machine/elf.h>
67 #include <machine/md_var.h>
68 
69 __ElfType(Brandinfo);
70 __ElfType(Auxargs);
71 
72 static int elf_check_header __P((const Elf_Ehdr *hdr));
73 static int elf_freebsd_fixup __P((long **stack_base,
74     struct image_params *imgp));
75 static int elf_load_file __P((struct proc *p, char *file, u_long *addr,
76     u_long *entry));
77 static int elf_load_section __P((struct proc *p,
78     struct vmspace *vmspace, struct vnode *vp,
79     vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
80     vm_prot_t prot));
81 static int exec_elf_imgact __P((struct image_params *imgp));
82 
83 static int elf_trace = 0;
84 SYSCTL_INT(_debug, OID_AUTO, elf_trace, CTLFLAG_RW, &elf_trace, 0, "");
85 
86 /*
87  * XXX Maximum length of an ELF brand (sysctl wants a statically-allocated
88  * buffer).
89  */
90 #define	MAXBRANDLEN	16
91 
92 static struct sysentvec elf_freebsd_sysvec = {
93         SYS_MAXSYSCALL,
94         sysent,
95         0,
96         0,
97         0,
98         0,
99         0,
100         0,
101         elf_freebsd_fixup,
102         sendsig,
103         sigcode,
104         &szsigcode,
105         0,
106 	"FreeBSD ELF",
107 	elf_coredump
108 };
109 
110 static Elf_Brandinfo freebsd_brand_info = {
111 						"FreeBSD",
112 						"",
113 						"/usr/libexec/ld-elf.so.1",
114 						&elf_freebsd_sysvec
115 					  };
116 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS] = {
117 							&freebsd_brand_info,
118 							NULL, NULL, NULL,
119 							NULL, NULL, NULL, NULL
120 						    };
121 
122 int
123 elf_insert_brand_entry(Elf_Brandinfo *entry)
124 {
125 	int i;
126 
127 	for (i=1; i<MAX_BRANDS; i++) {
128 		if (elf_brand_list[i] == NULL) {
129 			elf_brand_list[i] = entry;
130 			break;
131 		}
132 	}
133 	if (i == MAX_BRANDS)
134 		return -1;
135 	return 0;
136 }
137 
138 int
139 elf_remove_brand_entry(Elf_Brandinfo *entry)
140 {
141 	int i;
142 
143 	for (i=1; i<MAX_BRANDS; i++) {
144 		if (elf_brand_list[i] == entry) {
145 			elf_brand_list[i] = NULL;
146 			break;
147 		}
148 	}
149 	if (i == MAX_BRANDS)
150 		return -1;
151 	return 0;
152 }
153 
154 int
155 elf_brand_inuse(Elf_Brandinfo *entry)
156 {
157 	struct proc *p;
158 
159 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
160 		if (p->p_sysent == entry->sysvec)
161 			return TRUE;
162 	}
163 
164 	return FALSE;
165 }
166 
167 static int
168 elf_check_header(const Elf_Ehdr *hdr)
169 {
170 	if (!IS_ELF(*hdr) ||
171 	    hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
172 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
173 	    hdr->e_ident[EI_VERSION] != EV_CURRENT)
174 		return ENOEXEC;
175 
176 	if (!ELF_MACHINE_OK(hdr->e_machine))
177 		return ENOEXEC;
178 
179 	if (hdr->e_version != ELF_TARG_VER)
180 		return ENOEXEC;
181 
182 	return 0;
183 }
184 
185 static int
186 elf_load_section(struct proc *p, struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
187 {
188 	size_t map_len;
189 	vm_offset_t map_addr;
190 	int error, rv;
191 	size_t copy_len;
192 	vm_object_t object;
193 	vm_offset_t file_addr;
194 	vm_offset_t data_buf = 0;
195 
196 	object = vp->v_object;
197 	error = 0;
198 
199 	map_addr = trunc_page((vm_offset_t)vmaddr);
200 	file_addr = trunc_page(offset);
201 
202 	/*
203 	 * We have two choices.  We can either clear the data in the last page
204 	 * of an oversized mapping, or we can start the anon mapping a page
205 	 * early and copy the initialized data into that first page.  We
206 	 * choose the second..
207 	 */
208 	if (memsz > filsz)
209 		map_len = trunc_page(offset+filsz) - file_addr;
210 	else
211 		map_len = round_page(offset+filsz) - file_addr;
212 
213 	if (map_len != 0) {
214 		vm_object_reference(object);
215 		vm_map_lock(&vmspace->vm_map);
216 		rv = vm_map_insert(&vmspace->vm_map,
217 				      object,
218 				      file_addr,	/* file offset */
219 				      map_addr,		/* virtual start */
220 				      map_addr + map_len,/* virtual end */
221 				      prot,
222 				      VM_PROT_ALL,
223 				      MAP_COPY_ON_WRITE | MAP_PREFAULT);
224 		vm_map_unlock(&vmspace->vm_map);
225 		if (rv != KERN_SUCCESS)
226 			return EINVAL;
227 
228 		/* we can stop now if we've covered it all */
229 		if (memsz == filsz)
230 			return 0;
231 	}
232 
233 
234 	/*
235 	 * We have to get the remaining bit of the file into the first part
236 	 * of the oversized map segment.  This is normally because the .data
237 	 * segment in the file is extended to provide bss.  It's a neat idea
238 	 * to try and save a page, but it's a pain in the behind to implement.
239 	 */
240 	copy_len = (offset + filsz) - trunc_page(offset + filsz);
241 	map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
242 	map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
243 
244 	/* This had damn well better be true! */
245         if (map_len != 0) {
246 		vm_map_lock(&vmspace->vm_map);
247 		rv = vm_map_insert(&vmspace->vm_map, NULL, 0,
248 					map_addr, map_addr + map_len,
249 					VM_PROT_ALL, VM_PROT_ALL, 0);
250 		vm_map_unlock(&vmspace->vm_map);
251 		if (rv != KERN_SUCCESS)
252 			return EINVAL;
253 	}
254 
255 	if (copy_len != 0) {
256 		vm_object_reference(object);
257 		rv = vm_map_find(exec_map,
258 				 object,
259 				 trunc_page(offset + filsz),
260 				 &data_buf,
261 				 PAGE_SIZE,
262 				 TRUE,
263 				 VM_PROT_READ,
264 				 VM_PROT_ALL,
265 				 MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL);
266 		if (rv != KERN_SUCCESS) {
267 			vm_object_deallocate(object);
268 			return EINVAL;
269 		}
270 
271 		/* send the page fragment to user space */
272 		error = copyout((caddr_t)data_buf, (caddr_t)map_addr, copy_len);
273 		vm_map_remove(exec_map, data_buf, data_buf + PAGE_SIZE);
274 		if (error)
275 			return (error);
276 	}
277 
278 	/*
279 	 * set it to the specified protection
280 	 */
281 	vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len,  prot,
282 		       FALSE);
283 
284 	return error;
285 }
286 
287 /*
288  * Load the file "file" into memory.  It may be either a shared object
289  * or an executable.
290  *
291  * The "addr" reference parameter is in/out.  On entry, it specifies
292  * the address where a shared object should be loaded.  If the file is
293  * an executable, this value is ignored.  On exit, "addr" specifies
294  * where the file was actually loaded.
295  *
296  * The "entry" reference parameter is out only.  On exit, it specifies
297  * the entry point for the loaded file.
298  */
299 static int
300 elf_load_file(struct proc *p, char *file, u_long *addr, u_long *entry)
301 {
302 	const Elf_Ehdr *hdr = NULL;
303 	const Elf_Phdr *phdr = NULL;
304 	struct nameidata nd;
305 	struct vmspace *vmspace = p->p_vmspace;
306 	struct vattr attr;
307 	struct image_params image_params, *imgp;
308 	vm_prot_t prot;
309 	u_long rbase;
310 	u_long base_addr = 0;
311 	int error, i, numsegs;
312 
313 	imgp = &image_params;
314 	/*
315 	 * Initialize part of the common data
316 	 */
317 	imgp->proc = p;
318 	imgp->uap = NULL;
319 	imgp->attr = &attr;
320 	imgp->firstpage = NULL;
321 	imgp->image_header = (char *)kmem_alloc_wait(exec_map, PAGE_SIZE);
322 
323 	if (imgp->image_header == NULL) {
324 		nd.ni_vp = NULL;
325 		error = ENOMEM;
326 		goto fail;
327 	}
328 
329         NDINIT(&nd, LOOKUP, LOCKLEAF|FOLLOW, UIO_SYSSPACE, file, p);
330 
331 	if ((error = namei(&nd)) != 0) {
332 		nd.ni_vp = NULL;
333 		goto fail;
334 	}
335 
336 	imgp->vp = nd.ni_vp;
337 
338 	/*
339 	 * Check permissions, modes, uid, etc on the file, and "open" it.
340 	 */
341 	error = exec_check_permissions(imgp);
342 	if (error) {
343 		VOP_UNLOCK(nd.ni_vp, 0, p);
344 		goto fail;
345 	}
346 
347 	error = exec_map_first_page(imgp);
348 	VOP_UNLOCK(nd.ni_vp, 0, p);
349 	if (error)
350                 goto fail;
351 
352 	hdr = (const Elf_Ehdr *)imgp->image_header;
353 	if ((error = elf_check_header(hdr)) != 0)
354 		goto fail;
355 	if (hdr->e_type == ET_DYN)
356 		rbase = *addr;
357 	else if (hdr->e_type == ET_EXEC)
358 		rbase = 0;
359 	else {
360 		error = ENOEXEC;
361 		goto fail;
362 	}
363 
364 	/* Only support headers that fit within first page for now */
365 	if ((hdr->e_phoff > PAGE_SIZE) ||
366 	    (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
367 		error = ENOEXEC;
368 		goto fail;
369 	}
370 
371 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
372 
373 	for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
374 		if (phdr[i].p_type == PT_LOAD) {	/* Loadable segment */
375 			prot = 0;
376 			if (phdr[i].p_flags & PF_X)
377   				prot |= VM_PROT_EXECUTE;
378 			if (phdr[i].p_flags & PF_W)
379   				prot |= VM_PROT_WRITE;
380 			if (phdr[i].p_flags & PF_R)
381   				prot |= VM_PROT_READ;
382 
383 			if ((error = elf_load_section(p, vmspace, nd.ni_vp,
384   						     phdr[i].p_offset,
385   						     (caddr_t)phdr[i].p_vaddr +
386 							rbase,
387   						     phdr[i].p_memsz,
388   						     phdr[i].p_filesz, prot)) != 0)
389 				goto fail;
390 			/*
391 			 * Establish the base address if this is the
392 			 * first segment.
393 			 */
394 			if (numsegs == 0)
395   				base_addr = trunc_page(phdr[i].p_vaddr + rbase);
396 			numsegs++;
397 		}
398 	}
399 	*addr = base_addr;
400 	*entry=(unsigned long)hdr->e_entry + rbase;
401 
402 fail:
403 	if (imgp->firstpage)
404 		exec_unmap_first_page(imgp);
405 	if (imgp->image_header)
406 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->image_header,
407 			PAGE_SIZE);
408 	if (nd.ni_vp)
409 		vrele(nd.ni_vp);
410 
411 	return error;
412 }
413 
414 static char fallback_elf_brand[MAXBRANDLEN+1] = { "none" };
415 SYSCTL_STRING(_kern, OID_AUTO, fallback_elf_brand, CTLFLAG_RW,
416 		fallback_elf_brand, sizeof(fallback_elf_brand),
417 		"ELF brand of last resort");
418 
419 static int
420 exec_elf_imgact(struct image_params *imgp)
421 {
422 	const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
423 	const Elf_Phdr *phdr;
424 	Elf_Auxargs *elf_auxargs = NULL;
425 	struct vmspace *vmspace;
426 	vm_prot_t prot;
427 	u_long text_size = 0, data_size = 0;
428 	u_long text_addr = 0, data_addr = 0;
429 	u_long addr, entry = 0, proghdr = 0;
430 	int error, i;
431 	const char *interp = NULL;
432 	Elf_Brandinfo *brand_info;
433 	const char *brand;
434 	char path[MAXPATHLEN];
435 
436 	/*
437 	 * Do we have a valid ELF header ?
438 	 */
439 	if (elf_check_header(hdr) != 0 || hdr->e_type != ET_EXEC)
440 		return -1;
441 
442 	/*
443 	 * From here on down, we return an errno, not -1, as we've
444 	 * detected an ELF file.
445 	 */
446 
447 	if ((hdr->e_phoff > PAGE_SIZE) ||
448 	    (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
449 		/* Only support headers in first page for now */
450 		return ENOEXEC;
451 	}
452 	phdr = (const Elf_Phdr*)(imgp->image_header + hdr->e_phoff);
453 
454 	/*
455 	 * From this point on, we may have resources that need to be freed.
456 	 */
457 	if ((error = exec_extract_strings(imgp)) != 0)
458 		goto fail;
459 
460 	exec_new_vmspace(imgp);
461 
462 	vmspace = imgp->proc->p_vmspace;
463 
464 	for (i = 0; i < hdr->e_phnum; i++) {
465 		switch(phdr[i].p_type) {
466 
467 		case PT_LOAD:	/* Loadable segment */
468 			prot = 0;
469 			if (phdr[i].p_flags & PF_X)
470   				prot |= VM_PROT_EXECUTE;
471 			if (phdr[i].p_flags & PF_W)
472   				prot |= VM_PROT_WRITE;
473 			if (phdr[i].p_flags & PF_R)
474   				prot |= VM_PROT_READ;
475 
476 			if ((error = elf_load_section(imgp->proc,
477 						     vmspace, imgp->vp,
478   						     phdr[i].p_offset,
479   						     (caddr_t)phdr[i].p_vaddr,
480   						     phdr[i].p_memsz,
481   						     phdr[i].p_filesz, prot)) != 0)
482   				goto fail;
483 
484 			/*
485 			 * Is this .text or .data ??
486 			 *
487 			 * We only handle one each of those yet XXX
488 			 */
489 			if (hdr->e_entry >= phdr[i].p_vaddr &&
490 			hdr->e_entry <(phdr[i].p_vaddr+phdr[i].p_memsz)) {
491   				text_addr = trunc_page(phdr[i].p_vaddr);
492   				text_size = round_page(phdr[i].p_memsz +
493 						       phdr[i].p_vaddr -
494 						       text_addr);
495 				entry = (u_long)hdr->e_entry;
496 			} else {
497   				data_addr = trunc_page(phdr[i].p_vaddr);
498   				data_size = round_page(phdr[i].p_memsz +
499 						       phdr[i].p_vaddr -
500 						       data_addr);
501 			}
502 			break;
503 	  	case PT_INTERP:	/* Path to interpreter */
504 			if (phdr[i].p_filesz > MAXPATHLEN ||
505 			    phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) {
506 				error = ENOEXEC;
507 				goto fail;
508 			}
509 			interp = imgp->image_header + phdr[i].p_offset;
510 			break;
511 		case PT_PHDR: 	/* Program header table info */
512 			proghdr = phdr[i].p_vaddr;
513 			break;
514 		default:
515 			break;
516 		}
517 	}
518 
519 	vmspace->vm_tsize = text_size >> PAGE_SHIFT;
520 	vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
521 	vmspace->vm_dsize = data_size >> PAGE_SHIFT;
522 	vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
523 
524 	addr = ELF_RTLD_ADDR(vmspace);
525 
526 	imgp->entry_addr = entry;
527 
528 	/* If the executable has a brand, search for it in the brand list. */
529 	brand_info = NULL;
530 	brand = (const char *)&hdr->e_ident[EI_BRAND];
531 	if (brand[0] != '\0') {
532 		for (i = 0;  i < MAX_BRANDS;  i++) {
533 			Elf_Brandinfo *bi = elf_brand_list[i];
534 
535 			if (bi != NULL && strcmp(brand, bi->brand) == 0) {
536 				brand_info = bi;
537 				break;
538 			}
539 		}
540 	}
541 
542 	/* Lacking a known brand, search for a recognized interpreter. */
543 	if (brand_info == NULL && interp != NULL) {
544 		for (i = 0;  i < MAX_BRANDS;  i++) {
545 			Elf_Brandinfo *bi = elf_brand_list[i];
546 
547 			if (bi != NULL &&
548 			    strcmp(interp, bi->interp_path) == 0) {
549 				brand_info = bi;
550 				break;
551 			}
552 		}
553 	}
554 
555 	/* Lacking a recognized interpreter, try the default brand */
556 	if (brand_info == NULL && fallback_elf_brand[0] != '\0') {
557 		for (i = 0; i < MAX_BRANDS; i++) {
558 			Elf_Brandinfo *bi = elf_brand_list[i];
559 
560 			if (bi != NULL
561 			    && strcmp(fallback_elf_brand, bi->brand) == 0) {
562 				brand_info = bi;
563 				break;
564 			}
565 		}
566 	}
567 
568 #ifdef __alpha__
569 	/* XXX - Assume FreeBSD on the alpha. */
570 	if (brand_info == NULL)
571 		brand_info = &freebsd_brand_info;
572 #endif
573 
574 	if (brand_info == NULL) {
575 		if (brand[0] == 0)
576 			uprintf("ELF binary type not known."
577 			    "  Use \"brandelf\" to brand it.\n");
578 		else
579 			uprintf("ELF binary type \"%.*s\" not known.\n",
580 			    EI_NIDENT - EI_BRAND, brand);
581 		error = ENOEXEC;
582 		goto fail;
583 	}
584 
585 	imgp->proc->p_sysent = brand_info->sysvec;
586 	if (interp != NULL) {
587 		snprintf(path, sizeof(path), "%s%s",
588 		    brand_info->emul_path, interp);
589                 if ((error = elf_load_file(imgp->proc, path, &addr,
590 		    &imgp->entry_addr)) != 0) {
591                         uprintf("ELF interpreter %s not found\n", path);
592                         goto fail;
593                 }
594 	}
595 
596 	/*
597 	 * Construct auxargs table (used by the fixup routine)
598 	 */
599 	elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
600 	elf_auxargs->execfd = -1;
601 	elf_auxargs->phdr = proghdr;
602 	elf_auxargs->phent = hdr->e_phentsize;
603 	elf_auxargs->phnum = hdr->e_phnum;
604 	elf_auxargs->pagesz = PAGE_SIZE;
605 	elf_auxargs->base = addr;
606 	elf_auxargs->flags = 0;
607 	elf_auxargs->entry = entry;
608 	elf_auxargs->trace = elf_trace;
609 
610 	imgp->auxargs = elf_auxargs;
611 	imgp->interpreted = 0;
612 
613 	/* don't allow modifying the file while we run it */
614 	imgp->vp->v_flag |= VTEXT;
615 
616 fail:
617 	return error;
618 }
619 
620 static int
621 elf_freebsd_fixup(long **stack_base, struct image_params *imgp)
622 {
623 	Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
624 	long *pos;
625 
626 	pos = *stack_base + (imgp->argc + imgp->envc + 2);
627 
628 	if (args->trace) {
629 		AUXARGS_ENTRY(pos, AT_DEBUG, 1);
630 	}
631 	if (args->execfd != -1) {
632 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
633 	}
634 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
635 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
636 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
637 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
638 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
639 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
640 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
641 	AUXARGS_ENTRY(pos, AT_NULL, 0);
642 
643 	free(imgp->auxargs, M_TEMP);
644 	imgp->auxargs = NULL;
645 
646 	(*stack_base)--;
647 	suword(*stack_base, (long) imgp->argc);
648 	return 0;
649 }
650 
651 /*
652  * Code for generating ELF core dumps.
653  */
654 
655 typedef void (*segment_callback) __P((vm_map_entry_t, void *));
656 
657 /* Closure for cb_put_phdr(). */
658 struct phdr_closure {
659 	Elf_Phdr *phdr;		/* Program header to fill in */
660 	Elf_Off offset;		/* Offset of segment in core file */
661 };
662 
663 /* Closure for cb_size_segment(). */
664 struct sseg_closure {
665 	int count;		/* Count of writable segments. */
666 	size_t size;		/* Total size of all writable segments. */
667 };
668 
669 static void cb_put_phdr __P((vm_map_entry_t, void *));
670 static void cb_size_segment __P((vm_map_entry_t, void *));
671 static void each_writable_segment __P((struct proc *, segment_callback,
672     void *));
673 static int elf_corehdr __P((struct proc *, struct vnode *, struct ucred *,
674     int, void *, size_t));
675 static void elf_puthdr __P((struct proc *, void *, size_t *,
676     const prstatus_t *, const prfpregset_t *, const prpsinfo_t *, int));
677 static void elf_putnote __P((void *, size_t *, const char *, int,
678     const void *, size_t));
679 
680 extern int osreldate;
681 
682 int
683 elf_coredump(p)
684 	register struct proc *p;
685 {
686 	register struct vnode *vp;
687 	register struct ucred *cred = p->p_cred->pc_ucred;
688 	struct nameidata nd;
689 	struct vattr vattr;
690 	int error, error1;
691 	char *name;			/* name of corefile */
692 	struct sseg_closure seginfo;
693 	void *hdr;
694 	size_t hdrsize;
695 
696 	STOPEVENT(p, S_CORE, 0);
697 
698 	if (sugid_coredump == 0 && p->p_flag & P_SUGID)
699 		return (EFAULT);
700 
701 	/* Size the program segments. */
702 	seginfo.count = 0;
703 	seginfo.size = 0;
704 	each_writable_segment(p, cb_size_segment, &seginfo);
705 
706 	/*
707 	 * Calculate the size of the core file header area by making
708 	 * a dry run of generating it.  Nothing is written, but the
709 	 * size is calculated.
710 	 */
711 	hdrsize = 0;
712 	elf_puthdr((struct proc *)NULL, (void *)NULL, &hdrsize,
713 	    (const prstatus_t *)NULL, (const prfpregset_t *)NULL,
714 	    (const prpsinfo_t *)NULL, seginfo.count);
715 
716 	if (hdrsize + seginfo.size >= p->p_rlimit[RLIMIT_CORE].rlim_cur)
717 		return (EFAULT);
718 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
719 	if (name == NULL)
720 		return (EFAULT);	/* XXX -- not the best error */
721 
722 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
723 	error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR | S_IWUSR);
724 	free(name, M_TEMP);
725 	if (error)
726 		return (error);
727 	vp = nd.ni_vp;
728 
729 	/* Don't dump to non-regular files or files with links. */
730 	if (vp->v_type != VREG ||
731 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
732 		error = EFAULT;
733 		goto out;
734 	}
735 	VATTR_NULL(&vattr);
736 	vattr.va_size = 0;
737 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
738 	VOP_SETATTR(vp, &vattr, cred, p);
739 	p->p_acflag |= ACORE;
740 
741 
742 	/*
743 	 * Allocate memory for building the header, fill it up,
744 	 * and write it out.
745 	 */
746 	hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
747 	if (hdr == NULL) {
748 		error = EINVAL;
749 		goto out;
750 	}
751 	error = elf_corehdr(p, vp, cred, seginfo.count, hdr, hdrsize);
752 
753 	/* Write the contents of all of the writable segments. */
754 	if (error == 0) {
755 		Elf_Phdr *php;
756 		off_t offset;
757 		int i;
758 
759 		php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
760 		offset = hdrsize;
761 		for (i = 0;  i < seginfo.count;  i++) {
762 			error = vn_rdwr(UIO_WRITE, vp, (caddr_t)php->p_vaddr,
763 			    php->p_filesz, offset, UIO_USERSPACE,
764 			    IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, p);
765 			if (error != 0)
766 				break;
767 			offset += php->p_filesz;
768 			php++;
769 		}
770 	}
771 	free(hdr, M_TEMP);
772 
773 out:
774 	VOP_UNLOCK(vp, 0, p);
775 	error1 = vn_close(vp, FWRITE, cred, p);
776 	if (error == 0)
777 		error = error1;
778 	return (error);
779 }
780 
781 /*
782  * A callback for each_writable_segment() to write out the segment's
783  * program header entry.
784  */
785 static void
786 cb_put_phdr(entry, closure)
787 	vm_map_entry_t entry;
788 	void *closure;
789 {
790 	struct phdr_closure *phc = (struct phdr_closure *)closure;
791 	Elf_Phdr *phdr = phc->phdr;
792 
793 	phc->offset = round_page(phc->offset);
794 
795 	phdr->p_type = PT_LOAD;
796 	phdr->p_offset = phc->offset;
797 	phdr->p_vaddr = entry->start;
798 	phdr->p_paddr = 0;
799 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
800 	phdr->p_align = PAGE_SIZE;
801 	phdr->p_flags = 0;
802 	if (entry->protection & VM_PROT_READ)
803 		phdr->p_flags |= PF_R;
804 	if (entry->protection & VM_PROT_WRITE)
805 		phdr->p_flags |= PF_W;
806 	if (entry->protection & VM_PROT_EXECUTE)
807 		phdr->p_flags |= PF_X;
808 
809 	phc->offset += phdr->p_filesz;
810 	phc->phdr++;
811 }
812 
813 /*
814  * A callback for each_writable_segment() to gather information about
815  * the number of segments and their total size.
816  */
817 static void
818 cb_size_segment(entry, closure)
819 	vm_map_entry_t entry;
820 	void *closure;
821 {
822 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
823 
824 	ssc->count++;
825 	ssc->size += entry->end - entry->start;
826 }
827 
828 /*
829  * For each writable segment in the process's memory map, call the given
830  * function with a pointer to the map entry and some arbitrary
831  * caller-supplied data.
832  */
833 static void
834 each_writable_segment(p, func, closure)
835 	struct proc *p;
836 	segment_callback func;
837 	void *closure;
838 {
839 	vm_map_t map = &p->p_vmspace->vm_map;
840 	vm_map_entry_t entry;
841 
842 	for (entry = map->header.next;  entry != &map->header;
843 	    entry = entry->next) {
844 		vm_object_t obj;
845 
846 		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
847 		    (entry->protection & (VM_PROT_READ|VM_PROT_WRITE)) !=
848 		    (VM_PROT_READ|VM_PROT_WRITE))
849 			continue;
850 
851 		if ((obj = entry->object.vm_object) == NULL)
852 			continue;
853 
854 		/* Find the deepest backing object. */
855 		while (obj->backing_object != NULL)
856 			obj = obj->backing_object;
857 
858 		/* Ignore memory-mapped devices and such things. */
859 		if (obj->type != OBJT_DEFAULT &&
860 		    obj->type != OBJT_SWAP &&
861 		    obj->type != OBJT_VNODE)
862 			continue;
863 
864 		(*func)(entry, closure);
865 	}
866 }
867 
868 /*
869  * Write the core file header to the file, including padding up to
870  * the page boundary.
871  */
872 static int
873 elf_corehdr(p, vp, cred, numsegs, hdr, hdrsize)
874 	struct proc *p;
875 	struct vnode *vp;
876 	struct ucred *cred;
877 	int numsegs;
878 	size_t hdrsize;
879 	void *hdr;
880 {
881 	size_t off;
882 	prstatus_t status;
883 	prfpregset_t fpregset;
884 	prpsinfo_t psinfo;
885 
886 	/* Gather the information for the header. */
887 	bzero(&status, sizeof status);
888 	status.pr_version = PRSTATUS_VERSION;
889 	status.pr_statussz = sizeof(prstatus_t);
890 	status.pr_gregsetsz = sizeof(gregset_t);
891 	status.pr_fpregsetsz = sizeof(fpregset_t);
892 	status.pr_osreldate = osreldate;
893 	status.pr_cursig = p->p_sig;
894 	status.pr_pid = p->p_pid;
895 	fill_regs(p, &status.pr_reg);
896 
897 	fill_fpregs(p, &fpregset);
898 
899 	bzero(&psinfo, sizeof psinfo);
900 	psinfo.pr_version = PRPSINFO_VERSION;
901 	psinfo.pr_psinfosz = sizeof(prpsinfo_t);
902 	strncpy(psinfo.pr_fname, p->p_comm, MAXCOMLEN);
903 	/* XXX - We don't fill in the command line arguments properly yet. */
904 	strncpy(psinfo.pr_psargs, p->p_comm, PRARGSZ);
905 
906 	/* Fill in the header. */
907 	bzero(hdr, hdrsize);
908 	off = 0;
909 	elf_puthdr(p, hdr, &off, &status, &fpregset, &psinfo, numsegs);
910 
911 	/* Write it to the core file. */
912 	return vn_rdwr(UIO_WRITE, vp, hdr, hdrsize, (off_t)0,
913 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
914 }
915 
916 static void
917 elf_puthdr(struct proc *p, void *dst, size_t *off, const prstatus_t *status,
918     const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs)
919 {
920 	size_t ehoff;
921 	size_t phoff;
922 	size_t noteoff;
923 	size_t notesz;
924 
925 	ehoff = *off;
926 	*off += sizeof(Elf_Ehdr);
927 
928 	phoff = *off;
929 	*off += (numsegs + 1) * sizeof(Elf_Phdr);
930 
931 	noteoff = *off;
932 	elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status,
933 	    sizeof *status);
934 	elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
935 	    sizeof *fpregset);
936 	elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
937 	    sizeof *psinfo);
938 	notesz = *off - noteoff;
939 
940 	/* Align up to a page boundary for the program segments. */
941 	*off = round_page(*off);
942 
943 	if (dst != NULL) {
944 		Elf_Ehdr *ehdr;
945 		Elf_Phdr *phdr;
946 		struct phdr_closure phc;
947 
948 		/*
949 		 * Fill in the ELF header.
950 		 */
951 		ehdr = (Elf_Ehdr *)((char *)dst + ehoff);
952 		ehdr->e_ident[EI_MAG0] = ELFMAG0;
953 		ehdr->e_ident[EI_MAG1] = ELFMAG1;
954 		ehdr->e_ident[EI_MAG2] = ELFMAG2;
955 		ehdr->e_ident[EI_MAG3] = ELFMAG3;
956 		ehdr->e_ident[EI_CLASS] = ELF_CLASS;
957 		ehdr->e_ident[EI_DATA] = ELF_DATA;
958 		ehdr->e_ident[EI_VERSION] = EV_CURRENT;
959 		ehdr->e_ident[EI_PAD] = 0;
960 		strncpy(ehdr->e_ident + EI_BRAND, "FreeBSD",
961 		    EI_NIDENT - EI_BRAND);
962 		ehdr->e_type = ET_CORE;
963 		ehdr->e_machine = ELF_ARCH;
964 		ehdr->e_version = EV_CURRENT;
965 		ehdr->e_entry = 0;
966 		ehdr->e_phoff = phoff;
967 		ehdr->e_flags = 0;
968 		ehdr->e_ehsize = sizeof(Elf_Ehdr);
969 		ehdr->e_phentsize = sizeof(Elf_Phdr);
970 		ehdr->e_phnum = numsegs + 1;
971 		ehdr->e_shentsize = sizeof(Elf_Shdr);
972 		ehdr->e_shnum = 0;
973 		ehdr->e_shstrndx = SHN_UNDEF;
974 
975 		/*
976 		 * Fill in the program header entries.
977 		 */
978 		phdr = (Elf_Phdr *)((char *)dst + phoff);
979 
980 		/* The note segement. */
981 		phdr->p_type = PT_NOTE;
982 		phdr->p_offset = noteoff;
983 		phdr->p_vaddr = 0;
984 		phdr->p_paddr = 0;
985 		phdr->p_filesz = notesz;
986 		phdr->p_memsz = 0;
987 		phdr->p_flags = 0;
988 		phdr->p_align = 0;
989 		phdr++;
990 
991 		/* All the writable segments from the program. */
992 		phc.phdr = phdr;
993 		phc.offset = *off;
994 		each_writable_segment(p, cb_put_phdr, &phc);
995 	}
996 }
997 
998 static void
999 elf_putnote(void *dst, size_t *off, const char *name, int type,
1000     const void *desc, size_t descsz)
1001 {
1002 	Elf_Note note;
1003 
1004 	note.n_namesz = strlen(name) + 1;
1005 	note.n_descsz = descsz;
1006 	note.n_type = type;
1007 	if (dst != NULL)
1008 		bcopy(&note, (char *)dst + *off, sizeof note);
1009 	*off += sizeof note;
1010 	if (dst != NULL)
1011 		bcopy(name, (char *)dst + *off, note.n_namesz);
1012 	*off += roundup2(note.n_namesz, sizeof(Elf_Size));
1013 	if (dst != NULL)
1014 		bcopy(desc, (char *)dst + *off, note.n_descsz);
1015 	*off += roundup2(note.n_descsz, sizeof(Elf_Size));
1016 }
1017 
1018 /*
1019  * Tell kern_execve.c about it, with a little help from the linker.
1020  */
1021 static struct execsw elf_execsw = {exec_elf_imgact, "ELF"};
1022 EXEC_SET(elf, elf_execsw);
1023