Once we distribute socket protocol processing requests to different
[dragonfly.git] / sys / kern / imgact_elf.c
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: src/sys/kern/imgact_elf.c,v 1.73.2.13 2002/12/28 19:49:41 dillon Exp $
30  * $DragonFly: src/sys/kern/imgact_elf.c,v 1.17 2004/03/01 06:33:16 dillon Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/exec.h>
35 #include <sys/fcntl.h>
36 #include <sys/file.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/systm.h>
43 #include <sys/proc.h>
44 #include <sys/namei.h>
45 #include <sys/pioctl.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/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 #include <sys/mount.h>
67 #include <sys/ckpt.h>
68 #define OLD_EI_BRAND    8
69
70 __ElfType(Brandinfo);
71 __ElfType(Auxargs);
72
73 static int elf_check_header (const Elf_Ehdr *hdr);
74 static int elf_freebsd_fixup (register_t **stack_base,
75     struct image_params *imgp);
76 static int elf_load_file (struct proc *p, const char *file, u_long *addr,
77     u_long *entry);
78 static int elf_load_section (struct proc *p,
79     struct vmspace *vmspace, struct vnode *vp,
80     vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
81     vm_prot_t prot);
82 static int exec_elf_imgact (struct image_params *imgp);
83
84 static int elf_trace = 0;
85 SYSCTL_INT(_debug, OID_AUTO, elf_trace, CTLFLAG_RW, &elf_trace, 0, "");
86 static int elf_legacy_coredump = 0;
87 SYSCTL_INT(_debug, OID_AUTO, elf_legacy_coredump, CTLFLAG_RW,
88     &elf_legacy_coredump, 0, "");
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         NULL,
107         MINSIGSTKSZ
108 };
109
110 static Elf_Brandinfo freebsd_brand_info = {
111                                                 ELFOSABI_FREEBSD,
112                                                 "FreeBSD",
113                                                 "",
114                                                 "/usr/libexec/ld-elf.so.1",
115                                                 &elf_freebsd_sysvec
116                                           };
117 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS] = {
118                                                         &freebsd_brand_info,
119                                                         NULL, NULL, NULL,
120                                                         NULL, NULL, NULL, NULL
121                                                     };
122
123 int
124 elf_insert_brand_entry(Elf_Brandinfo *entry)
125 {
126         int i;
127
128         for (i=1; i<MAX_BRANDS; i++) {
129                 if (elf_brand_list[i] == NULL) {
130                         elf_brand_list[i] = entry;
131                         break;
132                 }
133         }
134         if (i == MAX_BRANDS)
135                 return -1;
136         return 0;
137 }
138
139 int
140 elf_remove_brand_entry(Elf_Brandinfo *entry)
141 {
142         int i;
143
144         for (i=1; i<MAX_BRANDS; i++) {
145                 if (elf_brand_list[i] == entry) {
146                         elf_brand_list[i] = NULL;
147                         break;
148                 }
149         }
150         if (i == MAX_BRANDS)
151                 return -1;
152         return 0;
153 }
154
155 int
156 elf_brand_inuse(Elf_Brandinfo *entry)
157 {
158         struct proc *p;
159         int rval = FALSE;
160
161         FOREACH_PROC_IN_SYSTEM(p) {
162                 if (p->p_sysent == entry->sysvec) {
163                         rval = TRUE;
164                         break;
165                 }
166         }
167
168         return (rval);
169 }
170
171 static int
172 elf_check_header(const Elf_Ehdr *hdr)
173 {
174         if (!IS_ELF(*hdr) ||
175             hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
176             hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
177             hdr->e_ident[EI_VERSION] != EV_CURRENT)
178                 return ENOEXEC;
179
180         if (!ELF_MACHINE_OK(hdr->e_machine))
181                 return ENOEXEC;
182
183         if (hdr->e_version != ELF_TARG_VER)
184                 return ENOEXEC;
185         
186         return 0;
187 }
188
189 static int
190 elf_load_section(struct proc *p, struct vmspace *vmspace, struct vnode *vp, 
191                  vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, 
192                  vm_prot_t prot)
193 {
194         size_t map_len;
195         vm_offset_t map_addr;
196         int error, rv, cow;
197         int count;
198         size_t copy_len;
199         vm_object_t object;
200         vm_offset_t file_addr;
201         vm_offset_t data_buf = 0;
202
203         VOP_GETVOBJECT(vp, &object);
204         error = 0;
205
206         /*
207          * It's necessary to fail if the filsz + offset taken from the
208          * header is greater than the actual file pager object's size.
209          * If we were to allow this, then the vm_map_find() below would
210          * walk right off the end of the file object and into the ether.
211          *
212          * While I'm here, might as well check for something else that
213          * is invalid: filsz cannot be greater than memsz.
214          */
215         if ((off_t)filsz + offset > object->un_pager.vnp.vnp_size ||
216             filsz > memsz) {
217                 uprintf("elf_load_section: truncated ELF file\n");
218                 return (ENOEXEC);
219         }
220
221         map_addr = trunc_page((vm_offset_t)vmaddr);
222         file_addr = trunc_page(offset);
223
224         /*
225          * We have two choices.  We can either clear the data in the last page
226          * of an oversized mapping, or we can start the anon mapping a page
227          * early and copy the initialized data into that first page.  We
228          * choose the second..
229          */
230         if (memsz > filsz)
231                 map_len = trunc_page(offset+filsz) - file_addr;
232         else
233                 map_len = round_page(offset+filsz) - file_addr;
234
235         if (map_len != 0) {
236                 vm_object_reference(object);
237
238                 /* cow flags: don't dump readonly sections in core */
239                 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
240                     (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
241
242                 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
243                 vm_map_lock(&vmspace->vm_map);
244                 rv = vm_map_insert(&vmspace->vm_map, &count,
245                                       object,
246                                       file_addr,        /* file offset */
247                                       map_addr,         /* virtual start */
248                                       map_addr + map_len,/* virtual end */
249                                       prot,
250                                       VM_PROT_ALL,
251                                       cow);
252                 vm_map_unlock(&vmspace->vm_map);
253                 vm_map_entry_release(count);
254                 if (rv != KERN_SUCCESS) {
255                         vm_object_deallocate(object);
256                         return EINVAL;
257                 }
258
259                 /* we can stop now if we've covered it all */
260                 if (memsz == filsz) {
261                         return 0;
262                 }
263         }
264
265
266         /*
267          * We have to get the remaining bit of the file into the first part
268          * of the oversized map segment.  This is normally because the .data
269          * segment in the file is extended to provide bss.  It's a neat idea
270          * to try and save a page, but it's a pain in the behind to implement.
271          */
272         copy_len = (offset + filsz) - trunc_page(offset + filsz);
273         map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
274         map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
275
276         /* This had damn well better be true! */
277         if (map_len != 0) {
278                 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
279                 vm_map_lock(&vmspace->vm_map);
280                 rv = vm_map_insert(&vmspace->vm_map, &count,
281                                         NULL, 0,
282                                         map_addr, map_addr + map_len,
283                                         VM_PROT_ALL, VM_PROT_ALL, 0);
284                 vm_map_unlock(&vmspace->vm_map);
285                 vm_map_entry_release(count);
286                 if (rv != KERN_SUCCESS) {
287                         return EINVAL; 
288                 }
289         }
290
291         if (copy_len != 0) {
292                 vm_object_reference(object);
293                 rv = vm_map_find(exec_map,
294                                  object, 
295                                  trunc_page(offset + filsz),
296                                  &data_buf,
297                                  PAGE_SIZE,
298                                  TRUE,
299                                  VM_PROT_READ,
300                                  VM_PROT_ALL,
301                                  MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL);
302                 if (rv != KERN_SUCCESS) {
303                         vm_object_deallocate(object);
304                         return EINVAL;
305                 }
306
307                 /* send the page fragment to user space */
308                 error = copyout((caddr_t)data_buf, (caddr_t)map_addr, copy_len);
309                 vm_map_remove(exec_map, data_buf, data_buf + PAGE_SIZE);
310                 if (error) {
311                         return (error);
312                 }
313         }
314
315         /*
316          * set it to the specified protection
317          */
318         vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len,  prot,
319                        FALSE);
320
321         return error;
322 }
323
324 /*
325  * Load the file "file" into memory.  It may be either a shared object
326  * or an executable.
327  *
328  * The "addr" reference parameter is in/out.  On entry, it specifies
329  * the address where a shared object should be loaded.  If the file is
330  * an executable, this value is ignored.  On exit, "addr" specifies
331  * where the file was actually loaded.
332  *
333  * The "entry" reference parameter is out only.  On exit, it specifies
334  * the entry point for the loaded file.
335  */
336 static int
337 elf_load_file(struct proc *p, const char *file, u_long *addr, u_long *entry)
338 {
339         struct {
340                 struct nameidata nd;
341                 struct vattr attr;
342                 struct image_params image_params;
343         } *tempdata;
344         const Elf_Ehdr *hdr = NULL;
345         const Elf_Phdr *phdr = NULL;
346         struct nameidata *nd;
347         struct vmspace *vmspace = p->p_vmspace;
348         struct vattr *attr;
349         struct image_params *imgp;
350         vm_prot_t prot;
351         u_long rbase;
352         u_long base_addr = 0;
353         int error, i, numsegs;
354         struct thread *td = p->p_thread;
355
356         tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
357         nd = &tempdata->nd;
358         attr = &tempdata->attr;
359         imgp = &tempdata->image_params;
360
361         /*
362          * Initialize part of the common data
363          */
364         imgp->proc = p;
365         imgp->attr = attr;
366         imgp->firstpage = NULL;
367         imgp->image_header = (char *)kmem_alloc_wait(exec_map, PAGE_SIZE);
368
369         if (imgp->image_header == NULL) {
370                 nd->ni_vp = NULL;
371                 error = ENOMEM;
372                 goto fail;
373         }
374
375         NDINIT(nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_FOLLOW,
376             UIO_SYSSPACE, file, td);
377                          
378         if ((error = namei(nd)) != 0) {
379                 nd->ni_vp = NULL;
380                 goto fail;
381         }
382         NDFREE(nd, NDF_ONLY_PNBUF);
383         imgp->vp = nd->ni_vp;
384
385         /*
386          * Check permissions, modes, uid, etc on the file, and "open" it.
387          */
388         error = exec_check_permissions(imgp);
389         if (error) {
390                 VOP_UNLOCK(nd->ni_vp, NULL, 0, td);
391                 goto fail;
392         }
393
394         error = exec_map_first_page(imgp);
395         /*
396          * Also make certain that the interpreter stays the same, so set
397          * its VTEXT flag, too.
398          */
399         if (error == 0)
400                 nd->ni_vp->v_flag |= VTEXT;
401         VOP_UNLOCK(nd->ni_vp, NULL, 0, td);
402         if (error)
403                 goto fail;
404
405         hdr = (const Elf_Ehdr *)imgp->image_header;
406         if ((error = elf_check_header(hdr)) != 0)
407                 goto fail;
408         if (hdr->e_type == ET_DYN)
409                 rbase = *addr;
410         else if (hdr->e_type == ET_EXEC)
411                 rbase = 0;
412         else {
413                 error = ENOEXEC;
414                 goto fail;
415         }
416
417         /* Only support headers that fit within first page for now */
418         if ((hdr->e_phoff > PAGE_SIZE) ||
419             (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
420                 error = ENOEXEC;
421                 goto fail;
422         }
423
424         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
425
426         for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
427                 if (phdr[i].p_type == PT_LOAD) {        /* Loadable segment */
428                         prot = 0;
429                         if (phdr[i].p_flags & PF_X)
430                                 prot |= VM_PROT_EXECUTE;
431                         if (phdr[i].p_flags & PF_W)
432                                 prot |= VM_PROT_WRITE;
433                         if (phdr[i].p_flags & PF_R)
434                                 prot |= VM_PROT_READ;
435
436                         error = elf_load_section(
437                                     p, vmspace, nd->ni_vp,
438                                     phdr[i].p_offset,
439                                     (caddr_t)phdr[i].p_vaddr +
440                                     rbase,
441                                     phdr[i].p_memsz,
442                                     phdr[i].p_filesz, prot);
443                         if (error != 0)
444                                 goto fail;
445                         /*
446                          * Establish the base address if this is the
447                          * first segment.
448                          */
449                         if (numsegs == 0)
450                                 base_addr = trunc_page(phdr[i].p_vaddr + rbase);
451                         numsegs++;
452                 }
453         }
454         *addr = base_addr;
455         *entry=(unsigned long)hdr->e_entry + rbase;
456
457 fail:
458         if (imgp->firstpage)
459                 exec_unmap_first_page(imgp);
460         if (imgp->image_header)
461                 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->image_header,
462                         PAGE_SIZE);
463         if (nd->ni_vp)
464                 vrele(nd->ni_vp);
465
466         free(tempdata, M_TEMP);
467
468         return error;
469 }
470
471 /*
472  * non static, as it can be overridden by start_init()
473  */
474 int fallback_elf_brand = -1;
475 SYSCTL_INT(_kern, OID_AUTO, fallback_elf_brand, CTLFLAG_RW,
476                 &fallback_elf_brand, -1,
477                 "ELF brand of last resort");
478
479 static int
480 exec_elf_imgact(struct image_params *imgp)
481 {
482         const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
483         const Elf_Phdr *phdr;
484         Elf_Auxargs *elf_auxargs = NULL;
485         struct vmspace *vmspace;
486         vm_prot_t prot;
487         u_long text_size = 0, data_size = 0, total_size = 0;
488         u_long text_addr = 0, data_addr = 0;
489         u_long seg_size, seg_addr;
490         u_long addr, entry = 0, proghdr = 0;
491         int error, i;
492         const char *interp = NULL;
493         Elf_Brandinfo *brand_info;
494         char *path;
495         lwkt_tokref ilock;
496
497         error = 0;
498
499         /*
500          * Do we have a valid ELF header ?
501          */
502         if (elf_check_header(hdr) != 0 || hdr->e_type != ET_EXEC)
503                 return -1;
504
505         /*
506          * From here on down, we return an errno, not -1, as we've
507          * detected an ELF file.
508          */
509
510         if ((hdr->e_phoff > PAGE_SIZE) ||
511             (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
512                 /* Only support headers in first page for now */
513                 return ENOEXEC;
514         }
515         phdr = (const Elf_Phdr*)(imgp->image_header + hdr->e_phoff);
516         
517         /*
518          * From this point on, we may have resources that need to be freed.
519          */
520
521         exec_new_vmspace(imgp, NULL);
522
523         /*
524          * Yeah, I'm paranoid.  There is every reason in the world to get
525          * VTEXT now since from here on out, there are places we can have
526          * a context switch.  Better safe than sorry; I really don't want
527          * the file to change while it's being loaded.
528          */
529         lwkt_gettoken(&ilock, imgp->vp->v_interlock);
530         imgp->vp->v_flag |= VTEXT;
531         lwkt_reltoken(&ilock);
532
533         vmspace = imgp->proc->p_vmspace;
534
535         for (i = 0; i < hdr->e_phnum; i++) {
536                 switch(phdr[i].p_type) {
537
538                 case PT_LOAD:   /* Loadable segment */
539                         prot = 0;
540                         if (phdr[i].p_flags & PF_X)
541                                 prot |= VM_PROT_EXECUTE;
542                         if (phdr[i].p_flags & PF_W)
543                                 prot |= VM_PROT_WRITE;
544                         if (phdr[i].p_flags & PF_R)
545                                 prot |= VM_PROT_READ;
546
547                         if ((error = elf_load_section(imgp->proc,
548                                                      vmspace, imgp->vp,
549                                                      phdr[i].p_offset,
550                                                      (caddr_t)phdr[i].p_vaddr,
551                                                      phdr[i].p_memsz,
552                                                      phdr[i].p_filesz, prot)) != 0)
553                                 goto fail;
554
555                         seg_addr = trunc_page(phdr[i].p_vaddr);
556                         seg_size = round_page(phdr[i].p_memsz +
557                                 phdr[i].p_vaddr - seg_addr);
558
559                         /*
560                          * Is this .text or .data?  We can't use
561                          * VM_PROT_WRITE or VM_PROT_EXEC, it breaks the
562                          * alpha terribly and possibly does other bad
563                          * things so we stick to the old way of figuring
564                          * it out:  If the segment contains the program
565                          * entry point, it's a text segment, otherwise it
566                          * is a data segment.
567                          *
568                          * Note that obreak() assumes that data_addr + 
569                          * data_size == end of data load area, and the ELF
570                          * file format expects segments to be sorted by
571                          * address.  If multiple data segments exist, the
572                          * last one will be used.
573                          */
574                         if (hdr->e_entry >= phdr[i].p_vaddr &&
575                             hdr->e_entry < (phdr[i].p_vaddr +
576                             phdr[i].p_memsz)) {
577                                 text_size = seg_size;
578                                 text_addr = seg_addr;
579                                 entry = (u_long)hdr->e_entry;
580                         } else {
581                                 data_size = seg_size;
582                                 data_addr = seg_addr;
583                         }
584                         total_size += seg_size;
585
586                         /*
587                          * Check limits.  It should be safe to check the
588                          * limits after loading the segment since we do
589                          * not actually fault in all the segment's pages.
590                          */
591                         if (data_size >
592                             imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur ||
593                             text_size > maxtsiz ||
594                             total_size >
595                             imgp->proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
596                                 error = ENOMEM;
597                                 goto fail;
598                         }
599                         break;
600                 case PT_INTERP: /* Path to interpreter */
601                         if (phdr[i].p_filesz > MAXPATHLEN ||
602                             phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) {
603                                 error = ENOEXEC;
604                                 goto fail;
605                         }
606                         interp = imgp->image_header + phdr[i].p_offset;
607                         break;
608                 case PT_PHDR:   /* Program header table info */
609                         proghdr = phdr[i].p_vaddr;
610                         break;
611                 default:
612                         break;
613                 }
614         }
615
616         vmspace->vm_tsize = text_size >> PAGE_SHIFT;
617         vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
618         vmspace->vm_dsize = data_size >> PAGE_SHIFT;
619         vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
620
621         addr = ELF_RTLD_ADDR(vmspace);
622
623         imgp->entry_addr = entry;
624
625         brand_info = NULL;
626
627         /* We support three types of branding -- (1) the ELF EI_OSABI field
628          * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
629          * branding w/in the ELF header, and (3) path of the `interp_path'
630          * field.  We should also look for an ".note.ABI-tag" ELF section now
631          * in all Linux ELF binaries, FreeBSD 4.1+, and some NetBSD ones.
632          */
633
634         /* If the executable has a brand, search for it in the brand list. */
635         if (brand_info == NULL) {
636                 for (i = 0;  i < MAX_BRANDS;  i++) {
637                         Elf_Brandinfo *bi = elf_brand_list[i];
638
639                         if (bi != NULL && 
640                             (hdr->e_ident[EI_OSABI] == bi->brand
641                             || 0 == 
642                             strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND], 
643                             bi->compat_3_brand, strlen(bi->compat_3_brand)))) {
644                                 brand_info = bi;
645                                 break;
646                         }
647                 }
648         }
649
650         /* Lacking a known brand, search for a recognized interpreter. */
651         if (brand_info == NULL && interp != NULL) {
652                 for (i = 0;  i < MAX_BRANDS;  i++) {
653                         Elf_Brandinfo *bi = elf_brand_list[i];
654
655                         if (bi != NULL &&
656                             strcmp(interp, bi->interp_path) == 0) {
657                                 brand_info = bi;
658                                 break;
659                         }
660                 }
661         }
662
663         /* Lacking a recognized interpreter, try the default brand */
664         if (brand_info == NULL) {
665                 for (i = 0; i < MAX_BRANDS; i++) {
666                         Elf_Brandinfo *bi = elf_brand_list[i];
667
668                         if (bi != NULL && fallback_elf_brand == bi->brand) {
669                                 brand_info = bi;
670                                 break;
671                         }
672                 }
673         }
674
675         if (brand_info == NULL) {
676                 uprintf("ELF binary type \"%u\" not known.\n",
677                     hdr->e_ident[EI_OSABI]);
678                 error = ENOEXEC;
679                 goto fail;
680         }
681
682         imgp->proc->p_sysent = brand_info->sysvec;
683         if (interp != NULL) {
684                 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
685                 snprintf(path, MAXPATHLEN, "%s%s",
686                          brand_info->emul_path, interp);
687                 if ((error = elf_load_file(imgp->proc, path, &addr,
688                                            &imgp->entry_addr)) != 0) {
689                         if ((error = elf_load_file(imgp->proc, interp, &addr,
690                                                    &imgp->entry_addr)) != 0) {
691                                 uprintf("ELF interpreter %s not found\n", path);
692                                 free(path, M_TEMP);
693                                 goto fail;
694                         }
695                 }
696                 free(path, M_TEMP);
697         }
698
699         /*
700          * Construct auxargs table (used by the fixup routine)
701          */
702         elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
703         elf_auxargs->execfd = -1;
704         elf_auxargs->phdr = proghdr;
705         elf_auxargs->phent = hdr->e_phentsize;
706         elf_auxargs->phnum = hdr->e_phnum;
707         elf_auxargs->pagesz = PAGE_SIZE;
708         elf_auxargs->base = addr;
709         elf_auxargs->flags = 0;
710         elf_auxargs->entry = entry;
711         elf_auxargs->trace = elf_trace;
712
713         imgp->auxargs = elf_auxargs;
714         imgp->interpreted = 0;
715
716 fail:
717         return error;
718 }
719
720 static int
721 elf_freebsd_fixup(register_t **stack_base, struct image_params *imgp)
722 {
723         Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
724         register_t *pos;
725
726         pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);
727
728         if (args->trace) {
729                 AUXARGS_ENTRY(pos, AT_DEBUG, 1);
730         }
731         if (args->execfd != -1) {
732                 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
733         }
734         AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
735         AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
736         AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
737         AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
738         AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
739         AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
740         AUXARGS_ENTRY(pos, AT_BASE, args->base);
741         AUXARGS_ENTRY(pos, AT_NULL, 0);
742
743         free(imgp->auxargs, M_TEMP);
744         imgp->auxargs = NULL;
745
746         (*stack_base)--;
747         suword(*stack_base, (long) imgp->args->argc);
748         return 0;
749
750
751 /*
752  * Code for generating ELF core dumps.
753  */
754
755 typedef int (*segment_callback) (vm_map_entry_t, void *);
756
757 /* Closure for cb_put_phdr(). */
758 struct phdr_closure {
759         Elf_Phdr *phdr;         /* Program header to fill in (incremented) */
760         Elf_Phdr *phdr_max;     /* Pointer bound for error check */
761         Elf_Off offset;         /* Offset of segment in core file */
762 };
763
764 /* Closure for cb_size_segment(). */
765 struct sseg_closure {
766         int count;              /* Count of writable segments. */
767         size_t vsize;           /* Total size of all writable segments. */
768 };
769
770 /* Closure for cb_put_fp(). */
771 struct fp_closure {
772         struct vn_hdr *vnh;
773         struct vn_hdr *vnh_max;
774         int count;
775         struct stat *sb;
776 };
777
778 typedef struct elf_buf {
779         char    *buf;
780         size_t  off;
781         size_t  off_max;
782 } *elf_buf_t;
783
784 static void *target_reserve(elf_buf_t target, size_t bytes, int *error);
785
786 static int cb_put_phdr (vm_map_entry_t, void *);
787 static int cb_size_segment (vm_map_entry_t, void *);
788 static int cb_fpcount_segment(vm_map_entry_t, void *);
789 static int cb_put_fp(vm_map_entry_t, void *);
790
791
792 static int each_segment (struct proc *, segment_callback, void *, int);
793 static int elf_corehdr (struct proc *, struct file *, struct ucred *,
794                         int, elf_buf_t);
795 static int elf_puthdr (struct proc *, elf_buf_t, const prstatus_t *,
796                         const prfpregset_t *, const prpsinfo_t *, int);
797 static int elf_putnote (elf_buf_t, const char *, int, const void *, size_t);
798
799 static int elf_putsigs(struct proc *, elf_buf_t);
800 static int elf_puttextvp(struct proc *, elf_buf_t);
801 static int elf_putfiles(struct proc *, elf_buf_t);
802
803 extern int osreldate;
804
805 int
806 elf_coredump(struct proc *p, struct vnode *vp, off_t limit)
807 {
808         struct file *fp; 
809         int error;
810
811         if ((error = falloc(NULL, &fp, NULL)) != 0)
812                 return (error);
813         fsetcred(fp, p->p_ucred);
814
815         fp->f_data = (caddr_t)vp;
816         fp->f_flag = O_CREAT|O_WRONLY|O_NOFOLLOW;
817         fp->f_ops = &vnops;
818         fp->f_type = DTYPE_VNODE;
819         VOP_UNLOCK(vp, NULL, 0, p->p_thread);
820         
821         error = generic_elf_coredump(p, fp, limit);
822
823         fp->f_data = NULL;
824         fp->f_flag = 0;
825         fp->f_ops = &badfileops;
826         fp->f_type = 0;
827         fdrop(fp, p->p_thread);
828         return (error);
829 }
830
831 int
832 generic_elf_coredump(struct proc *p, struct file *fp, off_t limit)
833 {
834         struct ucred *cred = p->p_ucred;
835         int error = 0;
836         struct sseg_closure seginfo;
837         struct elf_buf target;
838
839         if (!fp)
840                 printf("can't dump core - null fp\n");
841
842         /*
843          * Size the program segments
844          */
845         seginfo.count = 0;
846         seginfo.vsize = 0;
847         each_segment(p, cb_size_segment, &seginfo, 1);
848
849         /*
850          * Calculate the size of the core file header area by making
851          * a dry run of generating it.  Nothing is written, but the
852          * size is calculated.
853          */
854         bzero(&target, sizeof(target));
855         elf_puthdr(p, &target, NULL, NULL, NULL, seginfo.count);
856
857         if (target.off + seginfo.vsize >= limit)
858                 return (EFAULT);
859
860         /*
861          * Allocate memory for building the header, fill it up,
862          * and write it out.
863          */
864         target.off_max = target.off;
865         target.off = 0;
866         target.buf = malloc(target.off_max, M_TEMP, M_WAITOK|M_ZERO);
867
868         if (target.buf == NULL)
869                 return EINVAL;
870         error = elf_corehdr(p, fp, cred, seginfo.count, &target);
871
872         /* Write the contents of all of the writable segments. */
873         if (error == 0) {
874                 Elf_Phdr *php;
875                 int i;
876                 int nbytes;
877
878                 php = (Elf_Phdr *)(target.buf + sizeof(Elf_Ehdr)) + 1;
879                 for (i = 0; i < seginfo.count; i++) {
880                         error = fp_write(fp, (caddr_t)php->p_vaddr,
881                                         php->p_filesz, &nbytes);
882                         if (error != 0)
883                                 break;
884                         php++;
885                 }
886         }
887         free(target.buf, M_TEMP);
888         
889         return error;
890 }
891
892 /*
893  * A callback for each_segment() to write out the segment's
894  * program header entry.
895  */
896 static int
897 cb_put_phdr(vm_map_entry_t entry, void *closure)
898 {
899         struct phdr_closure *phc = closure;
900         Elf_Phdr *phdr = phc->phdr;
901
902         if (phc->phdr == phc->phdr_max)
903                 return EINVAL;
904
905         phc->offset = round_page(phc->offset);
906
907         phdr->p_type = PT_LOAD;
908         phdr->p_offset = phc->offset;
909         phdr->p_vaddr = entry->start;
910         phdr->p_paddr = 0;
911         phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
912         phdr->p_align = PAGE_SIZE;
913         phdr->p_flags = 0;
914         if (entry->protection & VM_PROT_READ)
915                 phdr->p_flags |= PF_R;
916         if (entry->protection & VM_PROT_WRITE)
917                 phdr->p_flags |= PF_W;
918         if (entry->protection & VM_PROT_EXECUTE)
919                 phdr->p_flags |= PF_X;
920
921         phc->offset += phdr->p_filesz;
922         ++phc->phdr;
923         return 0;
924 }
925
926 /*
927  * A callback for each_writable_segment() to gather information about
928  * the number of segments and their total size.
929  */
930 static int
931 cb_size_segment(vm_map_entry_t entry, void *closure)
932 {
933         struct sseg_closure *ssc = closure;
934
935         ++ssc->count;
936         ssc->vsize += entry->end - entry->start;
937         return 0;
938 }
939
940 /*
941  * A callback for each_segment() to gather information about
942  * the number of text segments.
943  */
944 static int
945 cb_fpcount_segment(vm_map_entry_t entry, void *closure)
946 {
947         int *count = closure;
948         if (entry->object.vm_object->type == OBJT_VNODE)
949                 ++*count;
950         return 0;
951 }
952
953 static int
954 cb_put_fp(vm_map_entry_t entry, void *closure) 
955 {
956         struct fp_closure *fpc = closure;
957         struct vn_hdr *vnh = fpc->vnh;
958         Elf_Phdr *phdr = &vnh->vnh_phdr;
959         struct vnode *vp;
960         int error;
961
962         if (entry->object.vm_object->type == OBJT_VNODE) {
963                 if (vnh == fpc->vnh_max)
964                         return EINVAL;
965                 vp = (struct vnode *)entry->object.vm_object->handle;
966
967                 if (vp->v_mount)
968                         vnh->vnh_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
969                 error = VFS_VPTOFH(vp, &vnh->vnh_fh.fh_fid);
970                 if (error) 
971                         return error;
972
973                 phdr->p_type = PT_LOAD;
974                 phdr->p_offset = 0;        /* not written to core */
975                 phdr->p_vaddr = entry->start;
976                 phdr->p_paddr = 0;
977                 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
978                 phdr->p_align = PAGE_SIZE;
979                 phdr->p_flags = 0;
980                 if (entry->protection & VM_PROT_READ)
981                         phdr->p_flags |= PF_R;
982                 if (entry->protection & VM_PROT_WRITE)
983                         phdr->p_flags |= PF_W;
984                 if (entry->protection & VM_PROT_EXECUTE)
985                         phdr->p_flags |= PF_X;
986                 ++fpc->vnh;
987                 ++fpc->count;
988         }
989         return 0;
990 }
991
992 /*
993  * For each writable segment in the process's memory map, call the given
994  * function with a pointer to the map entry and some arbitrary
995  * caller-supplied data.
996  */
997 static int
998 each_segment(struct proc *p, segment_callback func, void *closure, int writable)
999 {
1000         int error = 0;
1001         vm_map_t map = &p->p_vmspace->vm_map;
1002         vm_map_entry_t entry;
1003
1004         for (entry = map->header.next; error == 0 && entry != &map->header;
1005             entry = entry->next) {
1006                 vm_object_t obj;
1007
1008                 /*
1009                  * Don't dump inaccessible mappings, deal with legacy
1010                  * coredump mode.
1011                  *
1012                  * Note that read-only segments related to the elf binary
1013                  * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1014                  * need to arbitrarily ignore such segments.
1015                  */
1016                 if (elf_legacy_coredump) {
1017                         if (writable && (entry->protection & VM_PROT_RW) != VM_PROT_RW)
1018                                 continue;
1019                 } else {
1020                         if (writable && (entry->protection & VM_PROT_ALL) == 0)
1021                                 continue;
1022                 }
1023
1024                 /*
1025                  * Dont include memory segment in the coredump if
1026                  * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1027                  * madvise(2).  Do not dump submaps (i.e. parts of the
1028                  * kernel map).
1029                  */
1030                 if (writable && entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
1031                         continue;
1032
1033                 if ((obj = entry->object.vm_object) == NULL)
1034                         continue;
1035
1036                 /* Find the deepest backing object. */
1037                 while (obj->backing_object != NULL)
1038                         obj = obj->backing_object;
1039
1040                 /* Ignore memory-mapped devices and such things. */
1041                 if (obj->type != OBJT_DEFAULT &&
1042                     obj->type != OBJT_SWAP &&
1043                     obj->type != OBJT_VNODE)
1044                         continue;
1045
1046                 error = (*func)(entry, closure);
1047         }
1048         return error;
1049 }
1050
1051 static
1052 void *
1053 target_reserve(elf_buf_t target, size_t bytes, int *error)
1054 {
1055     void *res = NULL;
1056
1057     if (target->buf) {
1058             if (target->off + bytes > target->off_max)
1059                     *error = EINVAL;
1060             else
1061                     res = target->buf + target->off;
1062     }
1063     target->off += bytes;
1064     return (res);
1065 }
1066
1067 /*
1068  * Write the core file header to the file, including padding up to
1069  * the page boundary.
1070  */
1071 static int
1072 elf_corehdr(struct proc *p, struct file *fp, struct ucred *cred, int numsegs, 
1073             elf_buf_t target)
1074 {
1075         struct {
1076                 prstatus_t status;
1077                 prfpregset_t fpregset;
1078                 prpsinfo_t psinfo;
1079         } *tempdata;
1080         int error;
1081         prstatus_t *status;
1082         prfpregset_t *fpregset;
1083         prpsinfo_t *psinfo;
1084         int nbytes;
1085         tempdata = malloc(sizeof(*tempdata), M_TEMP, M_ZERO | M_WAITOK);
1086         status = &tempdata->status;
1087         fpregset = &tempdata->fpregset;
1088         psinfo = &tempdata->psinfo;
1089
1090         /* Gather the information for the header. */
1091         status->pr_version = PRSTATUS_VERSION;
1092         status->pr_statussz = sizeof(prstatus_t);
1093         status->pr_gregsetsz = sizeof(gregset_t);
1094         status->pr_fpregsetsz = sizeof(fpregset_t);
1095         status->pr_osreldate = osreldate;
1096         status->pr_cursig = p->p_sig;
1097         status->pr_pid = p->p_pid;
1098         fill_regs(p, &status->pr_reg);
1099
1100         fill_fpregs(p, fpregset);
1101
1102         psinfo->pr_version = PRPSINFO_VERSION;
1103         psinfo->pr_psinfosz = sizeof(prpsinfo_t);
1104         strncpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname) - 1);
1105
1106         /* XXX - We don't fill in the command line arguments properly yet. */
1107         strncpy(psinfo->pr_psargs, p->p_comm, PRARGSZ);
1108
1109         /* Fill in the header. */
1110         error = elf_puthdr(p, target, status, fpregset, psinfo, numsegs);
1111
1112         free(tempdata, M_TEMP);
1113
1114         /* Write it to the core file. */
1115         if (error == 0)
1116                 error = fp_write(fp, target->buf, target->off, &nbytes);
1117         return error;
1118 }
1119
1120 static int
1121 elf_puthdr(struct proc *p, elf_buf_t target, const prstatus_t *status,
1122         const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs)
1123 {
1124         int error = 0;
1125         size_t phoff;
1126         size_t noteoff;
1127         size_t notesz;
1128         Elf_Ehdr *ehdr;
1129         Elf_Phdr *phdr;
1130
1131         ehdr = target_reserve(target, sizeof(Elf_Ehdr), &error);
1132
1133         phoff = target->off;
1134         phdr = target_reserve(target, (numsegs + 1) * sizeof(Elf_Phdr), &error);
1135
1136         noteoff = target->off;
1137         if (error == 0) {
1138                 error = elf_putnote(target, "FreeBSD", NT_PRSTATUS, 
1139                                         status, sizeof *status);
1140         }
1141         if (error == 0) {
1142                 error = elf_putnote(target, "FreeBSD", NT_FPREGSET,
1143                                         fpregset, sizeof *fpregset);
1144         }
1145         if (error == 0) {
1146                 error = elf_putnote(target, "FreeBSD", NT_PRPSINFO,
1147                                         psinfo, sizeof *psinfo);
1148         }
1149         notesz = target->off - noteoff;
1150
1151         /*
1152          * put extra cruft for dumping process state here 
1153          *  - we really want it be before all the program 
1154          *    mappings
1155          *  - we just need to update the offset accordingly
1156          *    and GDB will be none the wiser.
1157          */
1158         if (error == 0)
1159                 error = elf_puttextvp(p, target);
1160         if (error == 0)
1161                 error = elf_putsigs(p, target);
1162         if (error == 0)
1163                 error = elf_putfiles(p, target);
1164
1165         /*
1166          * Align up to a page boundary for the program segments.  The
1167          * actual data will be written to the outptu file, not to elf_buf_t,
1168          * so we do not have to do any further bounds checking.
1169          */
1170         target->off = round_page(target->off);
1171         if (error == 0 && ehdr != NULL) {
1172                 /*
1173                  * Fill in the ELF header.
1174                  */
1175                 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1176                 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1177                 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1178                 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1179                 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1180                 ehdr->e_ident[EI_DATA] = ELF_DATA;
1181                 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1182                 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1183                 ehdr->e_ident[EI_ABIVERSION] = 0;
1184                 ehdr->e_ident[EI_PAD] = 0;
1185                 ehdr->e_type = ET_CORE;
1186                 ehdr->e_machine = ELF_ARCH;
1187                 ehdr->e_version = EV_CURRENT;
1188                 ehdr->e_entry = 0;
1189                 ehdr->e_phoff = phoff;
1190                 ehdr->e_flags = 0;
1191                 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1192                 ehdr->e_phentsize = sizeof(Elf_Phdr);
1193                 ehdr->e_phnum = numsegs + 1;
1194                 ehdr->e_shentsize = sizeof(Elf_Shdr);
1195                 ehdr->e_shnum = 0;
1196                 ehdr->e_shstrndx = SHN_UNDEF;
1197         }
1198         if (error == 0 && phdr != NULL) {
1199                 /*
1200                  * Fill in the program header entries.
1201                  */
1202                 struct phdr_closure phc;
1203
1204                 /* The note segement. */
1205                 phdr->p_type = PT_NOTE;
1206                 phdr->p_offset = noteoff;
1207                 phdr->p_vaddr = 0;
1208                 phdr->p_paddr = 0;
1209                 phdr->p_filesz = notesz;
1210                 phdr->p_memsz = 0;
1211                 phdr->p_flags = 0;
1212                 phdr->p_align = 0;
1213                 ++phdr;
1214
1215                 /* All the writable segments from the program. */
1216                 phc.phdr = phdr;
1217                 phc.phdr_max = phdr + numsegs;
1218                 phc.offset = target->off;
1219                 each_segment(p, cb_put_phdr, &phc, 1);
1220         }
1221         return (error);
1222 }
1223
1224 static int
1225 elf_putnote(elf_buf_t target, const char *name, int type,
1226             const void *desc, size_t descsz)
1227 {
1228         int error = 0;
1229         char *dst;
1230         Elf_Note note;
1231
1232         note.n_namesz = strlen(name) + 1;
1233         note.n_descsz = descsz;
1234         note.n_type = type;
1235         dst = target_reserve(target, sizeof(note), &error);
1236         if (dst != NULL)
1237                 bcopy(&note, dst, sizeof note);
1238         dst = target_reserve(target, note.n_namesz, &error);
1239         if (dst != NULL)
1240                 bcopy(name, dst, note.n_namesz);
1241         target->off = roundup2(target->off, sizeof(Elf_Size));
1242         dst = target_reserve(target, note.n_descsz, &error);
1243         if (dst != NULL)
1244                 bcopy(desc, dst, note.n_descsz);
1245         target->off = roundup2(target->off, sizeof(Elf_Size));
1246         return(error);
1247 }
1248
1249
1250 static int
1251 elf_putsigs(struct proc *p, elf_buf_t target)
1252 {
1253         int error = 0;
1254         struct ckpt_siginfo *csi;
1255
1256         csi = target_reserve(target, sizeof(struct ckpt_siginfo), &error);
1257         if (csi) {
1258                 csi->csi_ckptpisz = sizeof(struct ckpt_siginfo);
1259                 bcopy(p->p_procsig, &csi->csi_procsig, sizeof(struct procsig));
1260                 bcopy(p->p_procsig->ps_sigacts, &csi->csi_sigacts, sizeof(struct sigacts));
1261                 bcopy(&p->p_realtimer, &csi->csi_itimerval, sizeof(struct itimerval));
1262                 csi->csi_sigparent = p->p_sigparent;
1263         }
1264         return(error);
1265 }
1266
1267 static int
1268 elf_putfiles(struct proc *p, elf_buf_t target)
1269 {
1270         int error = 0;
1271         int i;
1272         struct ckpt_filehdr *cfh = NULL;
1273         struct ckpt_fileinfo *cfi;
1274         struct file *fp;        
1275         struct vnode *vp;
1276         /*
1277          * the duplicated loop is gross, but it was the only way
1278          * to eliminate uninitialized variable warnings 
1279          */
1280         cfh = target_reserve(target, sizeof(struct ckpt_filehdr), &error);
1281         if (cfh) {
1282                 cfh->cfh_nfiles = 0;            
1283         }
1284
1285         /*
1286          * ignore STDIN/STDERR/STDOUT
1287          */
1288         for (i = 3; error == 0 && i < p->p_fd->fd_nfiles; i++) {
1289                 if ((fp = p->p_fd->fd_ofiles[i]) == NULL)
1290                         continue;
1291                 if (fp->f_type != DTYPE_VNODE)
1292                         continue;
1293                 cfi = target_reserve(target, sizeof(struct ckpt_fileinfo), &error);
1294                 if (cfi) {
1295                         cfi->cfi_index = -1;
1296                         vp = (struct vnode *)fp->f_data;
1297                         /*
1298                          * it looks like a bug in ptrace is marking 
1299                          * a non-vnode as a vnode - until we find the 
1300                          * root cause this will at least prevent
1301                          * further panics from truss
1302                          */
1303                         if (vp == NULL || vp->v_mount == NULL)
1304                                 continue;
1305                         cfh->cfh_nfiles++;
1306                         cfi->cfi_index = i;
1307                         cfi->cfi_flags = fp->f_flag;
1308                         cfi->cfi_offset = fp->f_offset;
1309                         cfi->cfi_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1310                         error = VFS_VPTOFH(vp, &cfi->cfi_fh.fh_fid);
1311                 }
1312         }
1313         return(error);
1314 }
1315
1316 static int
1317 elf_puttextvp(struct proc *p, elf_buf_t target)
1318 {
1319         int error = 0;
1320         int *vn_count;
1321         struct fp_closure fpc;
1322         struct ckpt_vminfo *vminfo;
1323
1324         vminfo = target_reserve(target, sizeof(struct ckpt_vminfo), &error);
1325         if (vminfo != NULL) {
1326                 vminfo->cvm_dsize = p->p_vmspace->vm_dsize;
1327                 vminfo->cvm_tsize = p->p_vmspace->vm_tsize;
1328                 vminfo->cvm_daddr = p->p_vmspace->vm_daddr;
1329                 vminfo->cvm_taddr = p->p_vmspace->vm_taddr;
1330         }
1331
1332         fpc.count = 0;
1333         vn_count = target_reserve(target, sizeof(int), &error);
1334         if (target->buf != NULL) {
1335                 fpc.vnh = (struct vn_hdr *)(target->buf + target->off);
1336                 fpc.vnh_max = fpc.vnh + 
1337                         (target->off_max - target->off) / sizeof(struct vn_hdr);
1338                 error = each_segment(p, cb_put_fp, &fpc, 0);
1339                 if (vn_count)
1340                         *vn_count = fpc.count;
1341         } else {
1342                 error = each_segment(p, cb_fpcount_segment, &fpc.count, 0);
1343         }
1344         target->off += fpc.count * sizeof(struct vn_hdr);
1345         return(error);
1346 }
1347
1348
1349 /*
1350  * Tell kern_execve.c about it, with a little help from the linker.
1351  */
1352 static struct execsw elf_execsw = {exec_elf_imgact, "ELF"};
1353 EXEC_SET(elf, elf_execsw);