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