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