xref: /freebsd/sys/kern/imgact_elf.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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  * $FreeBSD$
30  */
31 
32 #include "opt_rlimit.h"
33 
34 #include <sys/param.h>
35 #include <sys/exec.h>
36 #include <sys/fcntl.h>
37 #include <sys/imgact.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/mman.h>
42 #include <sys/namei.h>
43 #include <sys/pioctl.h>
44 #include <sys/proc.h>
45 #include <sys/procfs.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/stat.h>
49 #include <sys/syscall.h>
50 #include <sys/sysctl.h>
51 #include <sys/sysent.h>
52 #include <sys/systm.h>
53 #include <sys/vnode.h>
54 
55 #include <vm/vm.h>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_param.h>
58 #include <vm/pmap.h>
59 #include <sys/lock.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_extern.h>
63 
64 #include <machine/elf.h>
65 #include <machine/md_var.h>
66 
67 #define OLD_EI_BRAND	8
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((register_t **stack_base,
74     struct image_params *imgp));
75 static int elf_load_file __P((struct proc *p, const 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 static struct sysentvec elf_freebsd_sysvec = {
87         SYS_MAXSYSCALL,
88         sysent,
89         0,
90         0,
91         0,
92         0,
93         0,
94         0,
95         elf_freebsd_fixup,
96         sendsig,
97         sigcode,
98         &szsigcode,
99         0,
100 	"FreeBSD ELF",
101 	elf_coredump
102 };
103 
104 static Elf_Brandinfo freebsd_brand_info = {
105 						ELFOSABI_FREEBSD,
106 						"",
107 						"/usr/libexec/ld-elf.so.1",
108 						&elf_freebsd_sysvec
109 					  };
110 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS] = {
111 							&freebsd_brand_info,
112 							NULL, NULL, NULL,
113 							NULL, NULL, NULL, NULL
114 						    };
115 
116 int
117 elf_insert_brand_entry(Elf_Brandinfo *entry)
118 {
119 	int i;
120 
121 	for (i=1; i<MAX_BRANDS; i++) {
122 		if (elf_brand_list[i] == NULL) {
123 			elf_brand_list[i] = entry;
124 			break;
125 		}
126 	}
127 	if (i == MAX_BRANDS)
128 		return -1;
129 	return 0;
130 }
131 
132 int
133 elf_remove_brand_entry(Elf_Brandinfo *entry)
134 {
135 	int i;
136 
137 	for (i=1; i<MAX_BRANDS; i++) {
138 		if (elf_brand_list[i] == entry) {
139 			elf_brand_list[i] = NULL;
140 			break;
141 		}
142 	}
143 	if (i == MAX_BRANDS)
144 		return -1;
145 	return 0;
146 }
147 
148 int
149 elf_brand_inuse(Elf_Brandinfo *entry)
150 {
151 	struct proc *p;
152 
153 	LIST_FOREACH(p, &allproc, p_list) {
154 		if (p->p_sysent == entry->sysvec)
155 			return TRUE;
156 	}
157 
158 	return FALSE;
159 }
160 
161 static int
162 elf_check_header(const Elf_Ehdr *hdr)
163 {
164 	if (!IS_ELF(*hdr) ||
165 	    hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
166 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
167 	    hdr->e_ident[EI_VERSION] != EV_CURRENT)
168 		return ENOEXEC;
169 
170 	if (!ELF_MACHINE_OK(hdr->e_machine))
171 		return ENOEXEC;
172 
173 	if (hdr->e_version != ELF_TARG_VER)
174 		return ENOEXEC;
175 
176 	return 0;
177 }
178 
179 static int
180 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)
181 {
182 	size_t map_len;
183 	vm_offset_t map_addr;
184 	int error, rv;
185 	size_t copy_len;
186 	vm_object_t object;
187 	vm_offset_t file_addr;
188 	vm_offset_t data_buf = 0;
189 
190 	object = vp->v_object;
191 	error = 0;
192 
193 	map_addr = trunc_page((vm_offset_t)vmaddr);
194 	file_addr = trunc_page(offset);
195 
196 	/*
197 	 * We have two choices.  We can either clear the data in the last page
198 	 * of an oversized mapping, or we can start the anon mapping a page
199 	 * early and copy the initialized data into that first page.  We
200 	 * choose the second..
201 	 */
202 	if (memsz > filsz)
203 		map_len = trunc_page(offset+filsz) - file_addr;
204 	else
205 		map_len = round_page(offset+filsz) - file_addr;
206 
207 	if (map_len != 0) {
208 		vm_object_reference(object);
209 		vm_map_lock(&vmspace->vm_map);
210 		rv = vm_map_insert(&vmspace->vm_map,
211 				      object,
212 				      file_addr,	/* file offset */
213 				      map_addr,		/* virtual start */
214 				      map_addr + map_len,/* virtual end */
215 				      prot,
216 				      VM_PROT_ALL,
217 				      MAP_COPY_ON_WRITE | MAP_PREFAULT);
218 		vm_map_unlock(&vmspace->vm_map);
219 		if (rv != KERN_SUCCESS) {
220 			vm_object_deallocate(object);
221 			return EINVAL;
222 		}
223 
224 		/* we can stop now if we've covered it all */
225 		if (memsz == filsz)
226 			return 0;
227 	}
228 
229 
230 	/*
231 	 * We have to get the remaining bit of the file into the first part
232 	 * of the oversized map segment.  This is normally because the .data
233 	 * segment in the file is extended to provide bss.  It's a neat idea
234 	 * to try and save a page, but it's a pain in the behind to implement.
235 	 */
236 	copy_len = (offset + filsz) - trunc_page(offset + filsz);
237 	map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
238 	map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
239 
240 	/* This had damn well better be true! */
241         if (map_len != 0) {
242 		vm_map_lock(&vmspace->vm_map);
243 		rv = vm_map_insert(&vmspace->vm_map, NULL, 0,
244 					map_addr, map_addr + map_len,
245 					VM_PROT_ALL, VM_PROT_ALL, 0);
246 		vm_map_unlock(&vmspace->vm_map);
247 		if (rv != KERN_SUCCESS)
248 			return EINVAL;
249 	}
250 
251 	if (copy_len != 0) {
252 		vm_object_reference(object);
253 		rv = vm_map_find(exec_map,
254 				 object,
255 				 trunc_page(offset + filsz),
256 				 &data_buf,
257 				 PAGE_SIZE,
258 				 TRUE,
259 				 VM_PROT_READ,
260 				 VM_PROT_ALL,
261 				 MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL);
262 		if (rv != KERN_SUCCESS) {
263 			vm_object_deallocate(object);
264 			return EINVAL;
265 		}
266 
267 		/* send the page fragment to user space */
268 		error = copyout((caddr_t)data_buf, (caddr_t)map_addr, copy_len);
269 		vm_map_remove(exec_map, data_buf, data_buf + PAGE_SIZE);
270 		if (error)
271 			return (error);
272 	}
273 
274 	/*
275 	 * set it to the specified protection
276 	 */
277 	vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len,  prot,
278 		       FALSE);
279 
280 	return error;
281 }
282 
283 /*
284  * Load the file "file" into memory.  It may be either a shared object
285  * or an executable.
286  *
287  * The "addr" reference parameter is in/out.  On entry, it specifies
288  * the address where a shared object should be loaded.  If the file is
289  * an executable, this value is ignored.  On exit, "addr" specifies
290  * where the file was actually loaded.
291  *
292  * The "entry" reference parameter is out only.  On exit, it specifies
293  * the entry point for the loaded file.
294  */
295 static int
296 elf_load_file(struct proc *p, const char *file, u_long *addr, u_long *entry)
297 {
298 	const Elf_Ehdr *hdr = NULL;
299 	const Elf_Phdr *phdr = NULL;
300 	struct nameidata nd;
301 	struct vmspace *vmspace = p->p_vmspace;
302 	struct vattr attr;
303 	struct image_params image_params, *imgp;
304 	vm_prot_t prot;
305 	u_long rbase;
306 	u_long base_addr = 0;
307 	int error, i, numsegs;
308 
309 	imgp = &image_params;
310 	/*
311 	 * Initialize part of the common data
312 	 */
313 	imgp->proc = p;
314 	imgp->uap = NULL;
315 	imgp->attr = &attr;
316 	imgp->firstpage = NULL;
317 	imgp->image_header = (char *)kmem_alloc_wait(exec_map, PAGE_SIZE);
318 
319 	if (imgp->image_header == NULL) {
320 		nd.ni_vp = NULL;
321 		error = ENOMEM;
322 		goto fail;
323 	}
324 
325         NDINIT(&nd, LOOKUP, LOCKLEAF|FOLLOW, UIO_SYSSPACE, file, p);
326 
327 	if ((error = namei(&nd)) != 0) {
328 		nd.ni_vp = NULL;
329 		goto fail;
330 	}
331 	NDFREE(&nd, NDF_ONLY_PNBUF);
332 	imgp->vp = nd.ni_vp;
333 
334 	/*
335 	 * Check permissions, modes, uid, etc on the file, and "open" it.
336 	 */
337 	error = exec_check_permissions(imgp);
338 	if (error) {
339 		VOP_UNLOCK(nd.ni_vp, 0, p);
340 		goto fail;
341 	}
342 
343 	error = exec_map_first_page(imgp);
344 	VOP_UNLOCK(nd.ni_vp, 0, p);
345 	if (error)
346                 goto fail;
347 
348 	hdr = (const Elf_Ehdr *)imgp->image_header;
349 	if ((error = elf_check_header(hdr)) != 0)
350 		goto fail;
351 	if (hdr->e_type == ET_DYN)
352 		rbase = *addr;
353 	else if (hdr->e_type == ET_EXEC)
354 		rbase = 0;
355 	else {
356 		error = ENOEXEC;
357 		goto fail;
358 	}
359 
360 	/* Only support headers that fit within first page for now */
361 	if ((hdr->e_phoff > PAGE_SIZE) ||
362 	    (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
363 		error = ENOEXEC;
364 		goto fail;
365 	}
366 
367 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
368 
369 	for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
370 		if (phdr[i].p_type == PT_LOAD) {	/* Loadable segment */
371 			prot = 0;
372 			if (phdr[i].p_flags & PF_X)
373   				prot |= VM_PROT_EXECUTE;
374 			if (phdr[i].p_flags & PF_W)
375   				prot |= VM_PROT_WRITE;
376 			if (phdr[i].p_flags & PF_R)
377   				prot |= VM_PROT_READ;
378 
379 			if ((error = elf_load_section(p, vmspace, nd.ni_vp,
380   						     phdr[i].p_offset,
381   						     (caddr_t)phdr[i].p_vaddr +
382 							rbase,
383   						     phdr[i].p_memsz,
384   						     phdr[i].p_filesz, prot)) != 0)
385 				goto fail;
386 			/*
387 			 * Establish the base address if this is the
388 			 * first segment.
389 			 */
390 			if (numsegs == 0)
391   				base_addr = trunc_page(phdr[i].p_vaddr + rbase);
392 			numsegs++;
393 		}
394 	}
395 	*addr = base_addr;
396 	*entry=(unsigned long)hdr->e_entry + rbase;
397 
398 fail:
399 	if (imgp->firstpage)
400 		exec_unmap_first_page(imgp);
401 	if (imgp->image_header)
402 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->image_header,
403 			PAGE_SIZE);
404 	if (nd.ni_vp)
405 		vrele(nd.ni_vp);
406 
407 	return error;
408 }
409 
410 static int fallback_elf_brand = ELFOSABI_FREEBSD;
411 SYSCTL_INT(_kern, OID_AUTO, fallback_elf_brand, CTLFLAG_RW,
412 		&fallback_elf_brand, ELFOSABI_FREEBSD,
413 		"ELF brand of last resort");
414 
415 static int
416 exec_elf_imgact(struct image_params *imgp)
417 {
418 	const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
419 	const Elf_Phdr *phdr;
420 	Elf_Auxargs *elf_auxargs = NULL;
421 	struct vmspace *vmspace;
422 	vm_prot_t prot;
423 	u_long text_size = 0, data_size = 0;
424 	u_long text_addr = 0, data_addr = 0;
425 	u_long addr, entry = 0, proghdr = 0;
426 	int error, i;
427 	const char *interp = NULL;
428 	Elf_Brandinfo *brand_info;
429 	char path[MAXPATHLEN];
430 
431 	/*
432 	 * Do we have a valid ELF header ?
433 	 */
434 	if (elf_check_header(hdr) != 0 || hdr->e_type != ET_EXEC)
435 		return -1;
436 
437 	/*
438 	 * From here on down, we return an errno, not -1, as we've
439 	 * detected an ELF file.
440 	 */
441 
442 	if ((hdr->e_phoff > PAGE_SIZE) ||
443 	    (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
444 		/* Only support headers in first page for now */
445 		return ENOEXEC;
446 	}
447 	phdr = (const Elf_Phdr*)(imgp->image_header + hdr->e_phoff);
448 
449 	/*
450 	 * From this point on, we may have resources that need to be freed.
451 	 */
452 	if ((error = exec_extract_strings(imgp)) != 0)
453 		goto fail;
454 
455 	exec_new_vmspace(imgp);
456 
457 	vmspace = imgp->proc->p_vmspace;
458 
459 	for (i = 0; i < hdr->e_phnum; i++) {
460 		switch(phdr[i].p_type) {
461 
462 		case PT_LOAD:	/* Loadable segment */
463 			prot = 0;
464 			if (phdr[i].p_flags & PF_X)
465   				prot |= VM_PROT_EXECUTE;
466 			if (phdr[i].p_flags & PF_W)
467   				prot |= VM_PROT_WRITE;
468 			if (phdr[i].p_flags & PF_R)
469   				prot |= VM_PROT_READ;
470 
471 			if ((error = elf_load_section(imgp->proc,
472 						     vmspace, imgp->vp,
473   						     phdr[i].p_offset,
474   						     (caddr_t)phdr[i].p_vaddr,
475   						     phdr[i].p_memsz,
476   						     phdr[i].p_filesz, prot)) != 0)
477   				goto fail;
478 
479 			/*
480 			 * Is this .text or .data ??
481 			 *
482 			 * We only handle one each of those yet XXX
483 			 */
484 			if (hdr->e_entry >= phdr[i].p_vaddr &&
485 			hdr->e_entry <(phdr[i].p_vaddr+phdr[i].p_memsz)) {
486   				text_addr = trunc_page(phdr[i].p_vaddr);
487   				text_size = round_page(phdr[i].p_memsz +
488 						       phdr[i].p_vaddr -
489 						       text_addr);
490 				entry = (u_long)hdr->e_entry;
491 			} else {
492   				data_addr = trunc_page(phdr[i].p_vaddr);
493   				data_size = round_page(phdr[i].p_memsz +
494 						       phdr[i].p_vaddr -
495 						       data_addr);
496 			}
497 			break;
498 	  	case PT_INTERP:	/* Path to interpreter */
499 			if (phdr[i].p_filesz > MAXPATHLEN ||
500 			    phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) {
501 				error = ENOEXEC;
502 				goto fail;
503 			}
504 			interp = imgp->image_header + phdr[i].p_offset;
505 			break;
506 		case PT_PHDR: 	/* Program header table info */
507 			proghdr = phdr[i].p_vaddr;
508 			break;
509 		default:
510 			break;
511 		}
512 	}
513 
514 	vmspace->vm_tsize = text_size >> PAGE_SHIFT;
515 	vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
516 	vmspace->vm_dsize = data_size >> PAGE_SHIFT;
517 	vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
518 
519 	addr = ELF_RTLD_ADDR(vmspace);
520 
521 	imgp->entry_addr = entry;
522 
523 	brand_info = NULL;
524 
525 	/* XXX  For now we look for the magic "FreeBSD" that we used to put
526 	 * into the ELF header at the EI_ABIVERSION location.  If found use
527 	 * that information rather than figuring out the ABI from proper
528 	 * branding.  This should be removed for 5.0-RELEASE. The Linux caes
529 	 * can be figured out from the `interp_path' field.
530 	 */
531 	if (strcmp("FreeBSD", (const char *)&hdr->e_ident[OLD_EI_BRAND]) == 0)
532 		brand_info = &freebsd_brand_info;
533 
534 	/* If the executable has a brand, search for it in the brand list. */
535 	if (brand_info == NULL) {
536 		for (i = 0;  i < MAX_BRANDS;  i++) {
537 			Elf_Brandinfo *bi = elf_brand_list[i];
538 
539 			if (bi != NULL && hdr->e_ident[EI_OSABI] == bi->brand) {
540 				brand_info = bi;
541 				break;
542 			}
543 		}
544 	}
545 
546 	/* Lacking a known brand, search for a recognized interpreter. */
547 	if (brand_info == NULL && interp != NULL) {
548 		for (i = 0;  i < MAX_BRANDS;  i++) {
549 			Elf_Brandinfo *bi = elf_brand_list[i];
550 
551 			if (bi != NULL &&
552 			    strcmp(interp, bi->interp_path) == 0) {
553 				brand_info = bi;
554 				break;
555 			}
556 		}
557 	}
558 
559 	/* Lacking a recognized interpreter, try the default brand */
560 	if (brand_info == NULL) {
561 		for (i = 0; i < MAX_BRANDS; i++) {
562 			Elf_Brandinfo *bi = elf_brand_list[i];
563 
564 			if (bi != NULL && fallback_elf_brand == bi->brand) {
565 				brand_info = bi;
566 				break;
567 			}
568 		}
569 	}
570 
571 	/* XXX - Assume FreeBSD after the branding method change. */
572 	if (brand_info == NULL)
573 		brand_info = &freebsd_brand_info;
574 
575 	if (brand_info == NULL) {
576 		uprintf("ELF binary type \"%u\" not known.\n",
577 		    hdr->e_ident[EI_OSABI]);
578 		error = ENOEXEC;
579 		goto fail;
580 	}
581 
582 	imgp->proc->p_sysent = brand_info->sysvec;
583 	if (interp != NULL) {
584 	        snprintf(path, sizeof(path), "%s%s",
585 			 brand_info->emul_path, interp);
586 		if ((error = elf_load_file(imgp->proc, path, &addr,
587 					   &imgp->entry_addr)) != 0) {
588 		        if ((error = elf_load_file(imgp->proc, interp, &addr,
589 						   &imgp->entry_addr)) != 0) {
590 			        uprintf("ELF interpreter %s not found\n", path);
591 				goto fail;
592 			}
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(register_t **stack_base, struct image_params *imgp)
622 {
623 	Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
624 	register_t *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, vp, limit)
684 	register struct proc *p;
685 	register struct vnode *vp;
686 	off_t limit;
687 {
688 	register struct ucred *cred = p->p_ucred;
689 	int error = 0;
690 	struct sseg_closure seginfo;
691 	void *hdr;
692 	size_t hdrsize;
693 
694 	/* Size the program segments. */
695 	seginfo.count = 0;
696 	seginfo.size = 0;
697 	each_writable_segment(p, cb_size_segment, &seginfo);
698 
699 	/*
700 	 * Calculate the size of the core file header area by making
701 	 * a dry run of generating it.  Nothing is written, but the
702 	 * size is calculated.
703 	 */
704 	hdrsize = 0;
705 	elf_puthdr((struct proc *)NULL, (void *)NULL, &hdrsize,
706 	    (const prstatus_t *)NULL, (const prfpregset_t *)NULL,
707 	    (const prpsinfo_t *)NULL, seginfo.count);
708 
709 	if (hdrsize + seginfo.size >= limit)
710 		return (EFAULT);
711 
712 	/*
713 	 * Allocate memory for building the header, fill it up,
714 	 * and write it out.
715 	 */
716 	hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
717 	if (hdr == NULL) {
718 		return EINVAL;
719 	}
720 	error = elf_corehdr(p, vp, cred, seginfo.count, hdr, hdrsize);
721 
722 	/* Write the contents of all of the writable segments. */
723 	if (error == 0) {
724 		Elf_Phdr *php;
725 		off_t offset;
726 		int i;
727 
728 		php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
729 		offset = hdrsize;
730 		for (i = 0;  i < seginfo.count;  i++) {
731 			error = vn_rdwr(UIO_WRITE, vp, (caddr_t)php->p_vaddr,
732 			    php->p_filesz, offset, UIO_USERSPACE,
733 			    IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, p);
734 			if (error != 0)
735 				break;
736 			offset += php->p_filesz;
737 			php++;
738 		}
739 	}
740 	free(hdr, M_TEMP);
741 
742 	return error;
743 }
744 
745 /*
746  * A callback for each_writable_segment() to write out the segment's
747  * program header entry.
748  */
749 static void
750 cb_put_phdr(entry, closure)
751 	vm_map_entry_t entry;
752 	void *closure;
753 {
754 	struct phdr_closure *phc = (struct phdr_closure *)closure;
755 	Elf_Phdr *phdr = phc->phdr;
756 
757 	phc->offset = round_page(phc->offset);
758 
759 	phdr->p_type = PT_LOAD;
760 	phdr->p_offset = phc->offset;
761 	phdr->p_vaddr = entry->start;
762 	phdr->p_paddr = 0;
763 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
764 	phdr->p_align = PAGE_SIZE;
765 	phdr->p_flags = 0;
766 	if (entry->protection & VM_PROT_READ)
767 		phdr->p_flags |= PF_R;
768 	if (entry->protection & VM_PROT_WRITE)
769 		phdr->p_flags |= PF_W;
770 	if (entry->protection & VM_PROT_EXECUTE)
771 		phdr->p_flags |= PF_X;
772 
773 	phc->offset += phdr->p_filesz;
774 	phc->phdr++;
775 }
776 
777 /*
778  * A callback for each_writable_segment() to gather information about
779  * the number of segments and their total size.
780  */
781 static void
782 cb_size_segment(entry, closure)
783 	vm_map_entry_t entry;
784 	void *closure;
785 {
786 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
787 
788 	ssc->count++;
789 	ssc->size += entry->end - entry->start;
790 }
791 
792 /*
793  * For each writable segment in the process's memory map, call the given
794  * function with a pointer to the map entry and some arbitrary
795  * caller-supplied data.
796  */
797 static void
798 each_writable_segment(p, func, closure)
799 	struct proc *p;
800 	segment_callback func;
801 	void *closure;
802 {
803 	vm_map_t map = &p->p_vmspace->vm_map;
804 	vm_map_entry_t entry;
805 
806 	for (entry = map->header.next;  entry != &map->header;
807 	    entry = entry->next) {
808 		vm_object_t obj;
809 
810 		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
811 		    (entry->protection & (VM_PROT_READ|VM_PROT_WRITE)) !=
812 		    (VM_PROT_READ|VM_PROT_WRITE))
813 			continue;
814 
815 		/*
816 		** Dont include memory segment in the coredump if
817 		** MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
818 		** madvise(2).
819 		*/
820 		if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
821 			continue;
822 
823 		if ((obj = entry->object.vm_object) == NULL)
824 			continue;
825 
826 		/* Find the deepest backing object. */
827 		while (obj->backing_object != NULL)
828 			obj = obj->backing_object;
829 
830 		/* Ignore memory-mapped devices and such things. */
831 		if (obj->type != OBJT_DEFAULT &&
832 		    obj->type != OBJT_SWAP &&
833 		    obj->type != OBJT_VNODE)
834 			continue;
835 
836 		(*func)(entry, closure);
837 	}
838 }
839 
840 /*
841  * Write the core file header to the file, including padding up to
842  * the page boundary.
843  */
844 static int
845 elf_corehdr(p, vp, cred, numsegs, hdr, hdrsize)
846 	struct proc *p;
847 	struct vnode *vp;
848 	struct ucred *cred;
849 	int numsegs;
850 	size_t hdrsize;
851 	void *hdr;
852 {
853 	size_t off;
854 	prstatus_t status;
855 	prfpregset_t fpregset;
856 	prpsinfo_t psinfo;
857 
858 	/* Gather the information for the header. */
859 	bzero(&status, sizeof status);
860 	status.pr_version = PRSTATUS_VERSION;
861 	status.pr_statussz = sizeof(prstatus_t);
862 	status.pr_gregsetsz = sizeof(gregset_t);
863 	status.pr_fpregsetsz = sizeof(fpregset_t);
864 	status.pr_osreldate = osreldate;
865 	status.pr_cursig = p->p_sig;
866 	status.pr_pid = p->p_pid;
867 	fill_regs(p, &status.pr_reg);
868 
869 	fill_fpregs(p, &fpregset);
870 
871 	bzero(&psinfo, sizeof psinfo);
872 	psinfo.pr_version = PRPSINFO_VERSION;
873 	psinfo.pr_psinfosz = sizeof(prpsinfo_t);
874 	strncpy(psinfo.pr_fname, p->p_comm, MAXCOMLEN);
875 	/* XXX - We don't fill in the command line arguments properly yet. */
876 	strncpy(psinfo.pr_psargs, p->p_comm, PRARGSZ);
877 
878 	/* Fill in the header. */
879 	bzero(hdr, hdrsize);
880 	off = 0;
881 	elf_puthdr(p, hdr, &off, &status, &fpregset, &psinfo, numsegs);
882 
883 	/* Write it to the core file. */
884 	return vn_rdwr(UIO_WRITE, vp, hdr, hdrsize, (off_t)0,
885 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
886 }
887 
888 static void
889 elf_puthdr(struct proc *p, void *dst, size_t *off, const prstatus_t *status,
890     const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs)
891 {
892 	size_t ehoff;
893 	size_t phoff;
894 	size_t noteoff;
895 	size_t notesz;
896 
897 	ehoff = *off;
898 	*off += sizeof(Elf_Ehdr);
899 
900 	phoff = *off;
901 	*off += (numsegs + 1) * sizeof(Elf_Phdr);
902 
903 	noteoff = *off;
904 	elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status,
905 	    sizeof *status);
906 	elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
907 	    sizeof *fpregset);
908 	elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
909 	    sizeof *psinfo);
910 	notesz = *off - noteoff;
911 
912 	/* Align up to a page boundary for the program segments. */
913 	*off = round_page(*off);
914 
915 	if (dst != NULL) {
916 		Elf_Ehdr *ehdr;
917 		Elf_Phdr *phdr;
918 		struct phdr_closure phc;
919 
920 		/*
921 		 * Fill in the ELF header.
922 		 */
923 		ehdr = (Elf_Ehdr *)((char *)dst + ehoff);
924 		ehdr->e_ident[EI_MAG0] = ELFMAG0;
925 		ehdr->e_ident[EI_MAG1] = ELFMAG1;
926 		ehdr->e_ident[EI_MAG2] = ELFMAG2;
927 		ehdr->e_ident[EI_MAG3] = ELFMAG3;
928 		ehdr->e_ident[EI_CLASS] = ELF_CLASS;
929 		ehdr->e_ident[EI_DATA] = ELF_DATA;
930 		ehdr->e_ident[EI_VERSION] = EV_CURRENT;
931 		ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
932 		ehdr->e_ident[EI_ABIVERSION] = 0;
933 		ehdr->e_ident[EI_PAD] = 0;
934 		ehdr->e_type = ET_CORE;
935 		ehdr->e_machine = ELF_ARCH;
936 		ehdr->e_version = EV_CURRENT;
937 		ehdr->e_entry = 0;
938 		ehdr->e_phoff = phoff;
939 		ehdr->e_flags = 0;
940 		ehdr->e_ehsize = sizeof(Elf_Ehdr);
941 		ehdr->e_phentsize = sizeof(Elf_Phdr);
942 		ehdr->e_phnum = numsegs + 1;
943 		ehdr->e_shentsize = sizeof(Elf_Shdr);
944 		ehdr->e_shnum = 0;
945 		ehdr->e_shstrndx = SHN_UNDEF;
946 
947 		/*
948 		 * Fill in the program header entries.
949 		 */
950 		phdr = (Elf_Phdr *)((char *)dst + phoff);
951 
952 		/* The note segement. */
953 		phdr->p_type = PT_NOTE;
954 		phdr->p_offset = noteoff;
955 		phdr->p_vaddr = 0;
956 		phdr->p_paddr = 0;
957 		phdr->p_filesz = notesz;
958 		phdr->p_memsz = 0;
959 		phdr->p_flags = 0;
960 		phdr->p_align = 0;
961 		phdr++;
962 
963 		/* All the writable segments from the program. */
964 		phc.phdr = phdr;
965 		phc.offset = *off;
966 		each_writable_segment(p, cb_put_phdr, &phc);
967 	}
968 }
969 
970 static void
971 elf_putnote(void *dst, size_t *off, const char *name, int type,
972     const void *desc, size_t descsz)
973 {
974 	Elf_Note note;
975 
976 	note.n_namesz = strlen(name) + 1;
977 	note.n_descsz = descsz;
978 	note.n_type = type;
979 	if (dst != NULL)
980 		bcopy(&note, (char *)dst + *off, sizeof note);
981 	*off += sizeof note;
982 	if (dst != NULL)
983 		bcopy(name, (char *)dst + *off, note.n_namesz);
984 	*off += roundup2(note.n_namesz, sizeof(Elf_Size));
985 	if (dst != NULL)
986 		bcopy(desc, (char *)dst + *off, note.n_descsz);
987 	*off += roundup2(note.n_descsz, sizeof(Elf_Size));
988 }
989 
990 /*
991  * Tell kern_execve.c about it, with a little help from the linker.
992  */
993 static struct execsw elf_execsw = {exec_elf_imgact, "ELF"};
994 EXEC_SET(elf, elf_execsw);
995