Kernel Elf: Expand ABI.note-tag search to cover entire file
[dragonfly.git] / sys / kern / imgact_elf.c
1 /*-
2  * Copyright (c) 2000 David O'Brien
3  * Copyright (c) 1995-1996 Søren Schmidt
4  * Copyright (c) 1996 Peter Wemm
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD: src/sys/kern/imgact_elf.c,v 1.73.2.13 2002/12/28 19:49:41 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/nlookup.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 #include <sys/eventhandler.h>
55
56 #include <cpu/lwbuf.h>
57
58 #include <vm/vm.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_param.h>
61 #include <vm/pmap.h>
62 #include <sys/lock.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_extern.h>
66
67 #include <machine/elf.h>
68 #include <machine/md_var.h>
69 #include <sys/mount.h>
70 #include <sys/ckpt.h>
71
72 #define OLD_EI_BRAND    8
73 #define truncps(va,ps)  ((va) & ~(ps - 1))
74 #define aligned(a,t)    (truncps((u_long)(a), sizeof(t)) == (u_long)(a))
75
76 static int __elfN(check_header)(const Elf_Ehdr *hdr);
77 static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
78     const char *interp, int32_t *osrel);
79 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
80     u_long *entry);
81 static int __elfN(load_section)(struct proc *p,
82     struct vmspace *vmspace, struct vnode *vp,
83     vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
84     vm_prot_t prot);
85 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
86 static boolean_t __elfN(check_note)(struct image_params *imgp,
87     Elf_Brandnote *checknote, int32_t *osrel);
88 static boolean_t check_PT_NOTE(struct image_params *imgp,
89     Elf_Brandnote *checknote, int32_t *osrel, const Elf_Phdr * pnote);
90
91 static int elf_legacy_coredump = 0;
92 static int __elfN(fallback_brand) = -1;
93 #if defined(__x86_64__)
94 SYSCTL_NODE(_kern, OID_AUTO, elf64, CTLFLAG_RW, 0, "");
95 SYSCTL_INT(_debug, OID_AUTO, elf64_legacy_coredump, CTLFLAG_RW,
96     &elf_legacy_coredump, 0, "legacy coredump mode");
97 SYSCTL_INT(_kern_elf64, OID_AUTO, fallback_brand, CTLFLAG_RW,
98     &elf64_fallback_brand, 0, "ELF64 brand of last resort");
99 TUNABLE_INT("kern.elf64.fallback_brand", &elf64_fallback_brand);
100 #else /* i386 assumed */
101 SYSCTL_NODE(_kern, OID_AUTO, elf32, CTLFLAG_RW, 0, "");
102 SYSCTL_INT(_debug, OID_AUTO, elf32_legacy_coredump, CTLFLAG_RW,
103     &elf_legacy_coredump, 0, "legacy coredump mode");
104 SYSCTL_INT(_kern_elf32, OID_AUTO, fallback_brand, CTLFLAG_RW,
105     &elf32_fallback_brand, 0, "ELF32 brand of last resort");
106 TUNABLE_INT("kern.elf32.fallback_brand", &elf32_fallback_brand);
107 #endif
108
109 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
110
111 static const char DRAGONFLY_ABI_VENDOR[] = "DragonFly";
112 static const char FREEBSD_ABI_VENDOR[]   = "FreeBSD";
113
114 Elf_Brandnote __elfN(dragonfly_brandnote) = {
115         .hdr.n_namesz   = sizeof(DRAGONFLY_ABI_VENDOR),
116         .hdr.n_descsz   = sizeof(int32_t),
117         .hdr.n_type     = 1,
118         .vendor         = DRAGONFLY_ABI_VENDOR,
119         .flags          = BN_CAN_FETCH_OSREL,
120 };
121
122 Elf_Brandnote __elfN(freebsd_brandnote) = {
123         .hdr.n_namesz   = sizeof(FREEBSD_ABI_VENDOR),
124         .hdr.n_descsz   = sizeof(int32_t),
125         .hdr.n_type     = 1,
126         .vendor         = FREEBSD_ABI_VENDOR,
127         .flags          = BN_CAN_FETCH_OSREL,
128 };
129
130 int
131 __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
132 {
133         int i;
134
135         for (i = 0; i < MAX_BRANDS; i++) {
136                 if (elf_brand_list[i] == NULL) {
137                         elf_brand_list[i] = entry;
138                         break;
139                 }
140         }
141         if (i == MAX_BRANDS) {
142                 uprintf("WARNING: %s: could not insert brandinfo entry: %p\n",
143                         __func__, entry);
144                 return (-1);
145         }
146         return (0);
147 }
148
149 int
150 __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
151 {
152         int i;
153
154         for (i = 0; i < MAX_BRANDS; i++) {
155                 if (elf_brand_list[i] == entry) {
156                         elf_brand_list[i] = NULL;
157                         break;
158                 }
159         }
160         if (i == MAX_BRANDS)
161                 return (-1);
162         return (0);
163 }
164
165 /*
166  * Check if an elf brand is being used anywhere in the system.
167  *
168  * Used by the linux emulation module unloader.  This isn't safe from
169  * races.
170  */
171 struct elf_brand_inuse_info {
172         int rval;
173         Elf_Brandinfo *entry;
174 };
175
176 static int elf_brand_inuse_callback(struct proc *p, void *data);
177
178 int
179 __elfN(brand_inuse)(Elf_Brandinfo *entry)
180 {
181         struct elf_brand_inuse_info info;
182
183         info.rval = FALSE;
184         info.entry = entry;
185         allproc_scan(elf_brand_inuse_callback, entry);
186         return (info.rval);
187 }
188
189 static
190 int
191 elf_brand_inuse_callback(struct proc *p, void *data)
192 {
193         struct elf_brand_inuse_info *info = data;
194
195         if (p->p_sysent == info->entry->sysvec) {
196                 info->rval = TRUE;
197                 return (-1);
198         }
199         return (0);
200 }
201
202 static int
203 __elfN(check_header)(const Elf_Ehdr *hdr)
204 {
205         Elf_Brandinfo *bi;
206         int i;
207
208         if (!IS_ELF(*hdr) ||
209             hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
210             hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
211             hdr->e_ident[EI_VERSION] != EV_CURRENT ||
212             hdr->e_phentsize != sizeof(Elf_Phdr) ||
213             hdr->e_ehsize != sizeof(Elf_Ehdr) ||
214             hdr->e_version != ELF_TARG_VER)
215                 return (ENOEXEC);
216
217         /*
218          * Make sure we have at least one brand for this machine.
219          */
220
221         for (i = 0; i < MAX_BRANDS; i++) {
222                 bi = elf_brand_list[i];
223                 if (bi != NULL && bi->machine == hdr->e_machine)
224                         break;
225         }
226         if (i == MAX_BRANDS)
227                 return (ENOEXEC);
228
229         return (0);
230 }
231
232 static int
233 __elfN(load_section)(struct proc *p, struct vmspace *vmspace, struct vnode *vp,
234                  vm_offset_t offset, caddr_t vmaddr, size_t memsz,
235                  size_t filsz, vm_prot_t prot)
236 {
237         size_t map_len;
238         vm_offset_t map_addr;
239         int error, rv, cow;
240         int count;
241         size_t copy_len;
242         vm_object_t object;
243         vm_offset_t file_addr;
244
245         object = vp->v_object;
246         error = 0;
247
248         /*
249          * It's necessary to fail if the filsz + offset taken from the
250          * header is greater than the actual file pager object's size.
251          * If we were to allow this, then the vm_map_find() below would
252          * walk right off the end of the file object and into the ether.
253          *
254          * While I'm here, might as well check for something else that
255          * is invalid: filsz cannot be greater than memsz.
256          */
257         if ((off_t)filsz + offset > vp->v_filesize || filsz > memsz) {
258                 uprintf("elf_load_section: truncated ELF file\n");
259                 return (ENOEXEC);
260         }
261
262         map_addr = trunc_page((vm_offset_t)vmaddr);
263         file_addr = trunc_page(offset);
264
265         /*
266          * We have two choices.  We can either clear the data in the last page
267          * of an oversized mapping, or we can start the anon mapping a page
268          * early and copy the initialized data into that first page.  We
269          * choose the second..
270          */
271         if (memsz > filsz)
272                 map_len = trunc_page(offset+filsz) - file_addr;
273         else
274                 map_len = round_page(offset+filsz) - file_addr;
275
276         if (map_len != 0) {
277                 vm_object_reference(object);
278
279                 /* cow flags: don't dump readonly sections in core */
280                 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
281                     (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
282
283                 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
284                 vm_map_lock(&vmspace->vm_map);
285                 rv = vm_map_insert(&vmspace->vm_map, &count,
286                                       object,
287                                       file_addr,        /* file offset */
288                                       map_addr,         /* virtual start */
289                                       map_addr + map_len,/* virtual end */
290                                       VM_MAPTYPE_NORMAL,
291                                       prot, VM_PROT_ALL,
292                                       cow);
293                 vm_map_unlock(&vmspace->vm_map);
294                 vm_map_entry_release(count);
295                 if (rv != KERN_SUCCESS) {
296                         vm_object_deallocate(object);
297                         return (EINVAL);
298                 }
299
300                 /* we can stop now if we've covered it all */
301                 if (memsz == filsz) {
302                         return (0);
303                 }
304         }
305
306
307         /*
308          * We have to get the remaining bit of the file into the first part
309          * of the oversized map segment.  This is normally because the .data
310          * segment in the file is extended to provide bss.  It's a neat idea
311          * to try and save a page, but it's a pain in the behind to implement.
312          */
313         copy_len = (offset + filsz) - trunc_page(offset + filsz);
314         map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
315         map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
316
317         /* This had damn well better be true! */
318         if (map_len != 0) {
319                 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
320                 vm_map_lock(&vmspace->vm_map);
321                 rv = vm_map_insert(&vmspace->vm_map, &count,
322                                         NULL, 0,
323                                         map_addr, map_addr + map_len,
324                                         VM_MAPTYPE_NORMAL,
325                                         VM_PROT_ALL, VM_PROT_ALL,
326                                         0);
327                 vm_map_unlock(&vmspace->vm_map);
328                 vm_map_entry_release(count);
329                 if (rv != KERN_SUCCESS) {
330                         return (EINVAL);
331                 }
332         }
333
334         if (copy_len != 0) {
335                 vm_page_t m;
336                 struct lwbuf *lwb;
337                 struct lwbuf lwb_cache;
338
339                 m = vm_fault_object_page(object, trunc_page(offset + filsz),
340                                          VM_PROT_READ, 0, &error);
341                 if (m) {
342                         lwb = lwbuf_alloc(m, &lwb_cache);
343                         error = copyout((caddr_t)lwbuf_kva(lwb),
344                                         (caddr_t)map_addr, copy_len);
345                         lwbuf_free(lwb);
346                         vm_page_unhold(m);
347                 }
348                 if (error) {
349                         return (error);
350                 }
351         }
352
353         /*
354          * set it to the specified protection
355          */
356         vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len,  prot,
357                        FALSE);
358
359         return (error);
360 }
361
362 /*
363  * Load the file "file" into memory.  It may be either a shared object
364  * or an executable.
365  *
366  * The "addr" reference parameter is in/out.  On entry, it specifies
367  * the address where a shared object should be loaded.  If the file is
368  * an executable, this value is ignored.  On exit, "addr" specifies
369  * where the file was actually loaded.
370  *
371  * The "entry" reference parameter is out only.  On exit, it specifies
372  * the entry point for the loaded file.
373  */
374 static int
375 __elfN(load_file)(struct proc *p, const char *file, u_long *addr, u_long *entry)
376 {
377         struct {
378                 struct nlookupdata nd;
379                 struct vattr attr;
380                 struct image_params image_params;
381         } *tempdata;
382         const Elf_Ehdr *hdr = NULL;
383         const Elf_Phdr *phdr = NULL;
384         struct nlookupdata *nd;
385         struct vmspace *vmspace = p->p_vmspace;
386         struct vattr *attr;
387         struct image_params *imgp;
388         struct mount *topmnt;
389         vm_prot_t prot;
390         u_long rbase;
391         u_long base_addr = 0;
392         int error, i, numsegs;
393
394         tempdata = kmalloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
395         nd = &tempdata->nd;
396         attr = &tempdata->attr;
397         imgp = &tempdata->image_params;
398
399         /*
400          * Initialize part of the common data
401          */
402         imgp->proc = p;
403         imgp->attr = attr;
404         imgp->firstpage = NULL;
405         imgp->image_header = NULL;
406         imgp->vp = NULL;
407
408         error = nlookup_init(nd, file, UIO_SYSSPACE, NLC_FOLLOW);
409         if (error == 0)
410                 error = nlookup(nd);
411         if (error == 0)
412                 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &imgp->vp);
413         topmnt = nd->nl_nch.mount;
414         nlookup_done(nd);
415         if (error)
416                 goto fail;
417
418         /*
419          * Check permissions, modes, uid, etc on the file, and "open" it.
420          */
421         error = exec_check_permissions(imgp, topmnt);
422         if (error) {
423                 vn_unlock(imgp->vp);
424                 goto fail;
425         }
426
427         error = exec_map_first_page(imgp);
428         /*
429          * Also make certain that the interpreter stays the same, so set
430          * its VTEXT flag, too.
431          */
432         if (error == 0)
433                 vsetflags(imgp->vp, VTEXT);
434         vn_unlock(imgp->vp);
435         if (error)
436                 goto fail;
437
438         hdr = (const Elf_Ehdr *)imgp->image_header;
439         if ((error = __elfN(check_header)(hdr)) != 0)
440                 goto fail;
441         if (hdr->e_type == ET_DYN)
442                 rbase = *addr;
443         else if (hdr->e_type == ET_EXEC)
444                 rbase = 0;
445         else {
446                 error = ENOEXEC;
447                 goto fail;
448         }
449
450         /* Only support headers that fit within first page for now      */
451         /*    (multiplication of two Elf_Half fields will not overflow) */
452         if ((hdr->e_phoff > PAGE_SIZE) ||
453             (hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE - hdr->e_phoff) {
454                 error = ENOEXEC;
455                 goto fail;
456         }
457
458         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
459         if (!aligned(phdr, Elf_Addr)) {
460                 error = ENOEXEC;
461                 goto fail;
462         }
463
464         for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
465                 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
466                         /* Loadable segment */
467                         prot = 0;
468                         if (phdr[i].p_flags & PF_X)
469                                 prot |= VM_PROT_EXECUTE;
470                         if (phdr[i].p_flags & PF_W)
471                                 prot |= VM_PROT_WRITE;
472                         if (phdr[i].p_flags & PF_R)
473                                 prot |= VM_PROT_READ;
474
475                         error = __elfN(load_section)(
476                                     p, vmspace, imgp->vp,
477                                     phdr[i].p_offset,
478                                     (caddr_t)phdr[i].p_vaddr +
479                                     rbase,
480                                     phdr[i].p_memsz,
481                                     phdr[i].p_filesz, prot);
482                         if (error != 0)
483                                 goto fail;
484                         /*
485                          * Establish the base address if this is the
486                          * first segment.
487                          */
488                         if (numsegs == 0)
489                                 base_addr = trunc_page(phdr[i].p_vaddr + rbase);
490                         numsegs++;
491                 }
492         }
493         *addr = base_addr;
494         *entry = (unsigned long)hdr->e_entry + rbase;
495
496 fail:
497         if (imgp->firstpage)
498                 exec_unmap_first_page(imgp);
499         if (imgp->vp) {
500                 vrele(imgp->vp);
501                 imgp->vp = NULL;
502         }
503         kfree(tempdata, M_TEMP);
504
505         return (error);
506 }
507
508 static Elf_Brandinfo *
509 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
510     int32_t *osrel)
511 {
512         const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
513         Elf_Brandinfo *bi;
514         boolean_t ret;
515         int i;
516
517         /* We support four types of branding -- (1) the ELF EI_OSABI field
518          * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
519          * branding within the ELF header, (3) path of the `interp_path' field,
520          * and (4) the ".note.ABI-tag" ELF section.
521          */
522
523         /* Look for an ".note.ABI-tag" ELF section */
524         for (i = 0; i < MAX_BRANDS; i++) {
525                 bi = elf_brand_list[i];
526
527                 if (bi == NULL)
528                         continue;
529                 if (hdr->e_machine == bi->machine && (bi->flags &
530                     (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
531                         ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
532                         if (ret)
533                                 return (bi);
534                 }
535         }
536
537         /* If the executable has a brand, search for it in the brand list. */
538         for (i = 0;  i < MAX_BRANDS;  i++) {
539                 bi = elf_brand_list[i];
540
541                 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
542                         continue;
543                 if (hdr->e_machine == bi->machine &&
544                     (hdr->e_ident[EI_OSABI] == bi->brand ||
545                     strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
546                     bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
547                         return (bi);
548         }
549
550         /* Lacking a known brand, search for a recognized interpreter. */
551         if (interp != NULL) {
552                 for (i = 0;  i < MAX_BRANDS;  i++) {
553                         bi = elf_brand_list[i];
554
555                         if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
556                                 continue;
557                         if (hdr->e_machine == bi->machine &&
558                             strcmp(interp, bi->interp_path) == 0)
559                                 return (bi);
560                 }
561         }
562
563         /* Lacking a recognized interpreter, try the default brand */
564         for (i = 0; i < MAX_BRANDS; i++) {
565                 bi = elf_brand_list[i];
566
567                 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
568                         continue;
569                 if (hdr->e_machine == bi->machine &&
570                     __elfN(fallback_brand) == bi->brand)
571                         return (bi);
572         }
573         return (NULL);
574 }
575
576 static int
577 __CONCAT(exec_,__elfN(imgact))(struct image_params *imgp)
578 {
579         const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
580         const Elf_Phdr *phdr;
581         Elf_Auxargs *elf_auxargs;
582         struct vmspace *vmspace;
583         vm_prot_t prot;
584         u_long text_size = 0, data_size = 0, total_size = 0;
585         u_long text_addr = 0, data_addr = 0;
586         u_long seg_size, seg_addr;
587         u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0;
588         int32_t osrel = 0;
589         int error = 0, i, n;
590         const char *interp = NULL, *newinterp = NULL;
591         Elf_Brandinfo *brand_info;
592         char *path;
593
594         /*
595          * Do we have a valid ELF header ?
596          *
597          * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later if a particular
598          * brand doesn't support it.  Both DragonFly platforms do by default.
599          */
600         if (__elfN(check_header)(hdr) != 0 ||
601             (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
602                 return (-1);
603
604         /*
605          * From here on down, we return an errno, not -1, as we've
606          * detected an ELF file.
607          */
608
609         if ((hdr->e_phoff > PAGE_SIZE) ||
610             (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
611                 /* Only support headers in first page for now */
612                 return (ENOEXEC);
613         }
614         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
615         if (!aligned(phdr, Elf_Addr))
616                 return (ENOEXEC);
617         n = 0;
618         baddr = 0;
619         for (i = 0; i < hdr->e_phnum; i++) {
620                 if (phdr[i].p_type == PT_LOAD) {
621                         if (n == 0)
622                                 baddr = phdr[i].p_vaddr;
623                         n++;
624                         continue;
625                 }
626                 if (phdr[i].p_type == PT_INTERP) {
627                         /* Path to interpreter */
628                         if (phdr[i].p_filesz > MAXPATHLEN ||
629                             phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE)
630                                 return (ENOEXEC);
631                         interp = imgp->image_header + phdr[i].p_offset;
632                         continue;
633                 }
634         }
635         
636         brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel);
637         if (brand_info == NULL) {
638                 uprintf("ELF binary type \"%u\" not known.\n",
639                     hdr->e_ident[EI_OSABI]);
640                 return (ENOEXEC);
641         }
642         if (hdr->e_type == ET_DYN) {
643                 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0)
644                         return (ENOEXEC);
645                 /*
646                  * Honour the base load address from the dso if it is
647                  * non-zero for some reason.
648                  */
649                 if (baddr == 0)
650                         et_dyn_addr = ET_DYN_LOAD_ADDR;
651                 else
652                         et_dyn_addr = 0;
653         } else
654                 et_dyn_addr = 0;
655
656         if (interp != NULL && brand_info->interp_newpath != NULL)
657                 newinterp = brand_info->interp_newpath;
658
659         exec_new_vmspace(imgp, NULL);
660
661         /*
662          * Yeah, I'm paranoid.  There is every reason in the world to get
663          * VTEXT now since from here on out, there are places we can have
664          * a context switch.  Better safe than sorry; I really don't want
665          * the file to change while it's being loaded.
666          */
667         vsetflags(imgp->vp, VTEXT);
668
669         vmspace = imgp->proc->p_vmspace;
670
671         for (i = 0; i < hdr->e_phnum; i++) {
672                 switch (phdr[i].p_type) {
673
674                 case PT_LOAD:   /* Loadable segment */
675                         if (phdr[i].p_memsz == 0)
676                                 break;
677                         prot = 0;
678                         if (phdr[i].p_flags & PF_X)
679                                 prot |= VM_PROT_EXECUTE;
680                         if (phdr[i].p_flags & PF_W)
681                                 prot |= VM_PROT_WRITE;
682                         if (phdr[i].p_flags & PF_R)
683                                 prot |= VM_PROT_READ;
684
685                         if ((error = __elfN(load_section)(
686                                         imgp->proc,
687                                         vmspace,
688                                         imgp->vp,
689                                         phdr[i].p_offset,
690                                         (caddr_t)phdr[i].p_vaddr + et_dyn_addr,
691                                         phdr[i].p_memsz,
692                                         phdr[i].p_filesz,
693                                         prot)) != 0)
694                                 return (error);
695
696                         /*
697                          * If this segment contains the program headers,
698                          * remember their virtual address for the AT_PHDR
699                          * aux entry. Static binaries don't usually include
700                          * a PT_PHDR entry.
701                          */
702                         if (phdr[i].p_offset == 0 &&
703                             hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
704                                 <= phdr[i].p_filesz)
705                                 proghdr = phdr[i].p_vaddr + hdr->e_phoff +
706                                     et_dyn_addr;
707
708                         seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
709                         seg_size = round_page(phdr[i].p_memsz +
710                             phdr[i].p_vaddr + et_dyn_addr - seg_addr);
711
712                         /*
713                          * Is this .text or .data?  We can't use
714                          * VM_PROT_WRITE or VM_PROT_EXEC, it breaks the
715                          * alpha terribly and possibly does other bad
716                          * things so we stick to the old way of figuring
717                          * it out:  If the segment contains the program
718                          * entry point, it's a text segment, otherwise it
719                          * is a data segment.
720                          *
721                          * Note that obreak() assumes that data_addr + 
722                          * data_size == end of data load area, and the ELF
723                          * file format expects segments to be sorted by
724                          * address.  If multiple data segments exist, the
725                          * last one will be used.
726                          */
727                         if (hdr->e_entry >= phdr[i].p_vaddr &&
728                             hdr->e_entry < (phdr[i].p_vaddr +
729                             phdr[i].p_memsz)) {
730                                 text_size = seg_size;
731                                 text_addr = seg_addr;
732                                 entry = (u_long)hdr->e_entry + et_dyn_addr;
733                         } else {
734                                 data_size = seg_size;
735                                 data_addr = seg_addr;
736                         }
737                         total_size += seg_size;
738
739                         /*
740                          * Check limits.  It should be safe to check the
741                          * limits after loading the segment since we do
742                          * not actually fault in all the segment's pages.
743                          */
744                         if (data_size >
745                             imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur ||
746                             text_size > maxtsiz ||
747                             total_size >
748                             imgp->proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
749                                 error = ENOMEM;
750                                 return (error);
751                         }
752                         break;
753                 case PT_PHDR:   /* Program header table info */
754                         proghdr = phdr[i].p_vaddr + et_dyn_addr;
755                         break;
756                 default:
757                         break;
758                 }
759         }
760
761         vmspace->vm_tsize = text_size >> PAGE_SHIFT;
762         vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
763         vmspace->vm_dsize = data_size >> PAGE_SHIFT;
764         vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
765
766         addr = ELF_RTLD_ADDR(vmspace);
767
768         imgp->entry_addr = entry;
769
770         imgp->proc->p_sysent = brand_info->sysvec;
771         EVENTHANDLER_INVOKE(process_exec, imgp);
772
773         if (interp != NULL) {
774                 int have_interp = FALSE;
775                 if (brand_info->emul_path != NULL &&
776                     brand_info->emul_path[0] != '\0') {
777                         path = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
778                         ksnprintf(path, MAXPATHLEN, "%s%s",
779                             brand_info->emul_path, interp);
780                         error = __elfN(load_file)(imgp->proc, path, &addr,
781                             &imgp->entry_addr);
782                         kfree(path, M_TEMP);
783                         if (error == 0)
784                                 have_interp = TRUE;
785                 }
786                 if (!have_interp && newinterp != NULL) {
787                         error = __elfN(load_file)(imgp->proc, newinterp,
788                             &addr, &imgp->entry_addr);
789                         if (error == 0)
790                                 have_interp = TRUE;
791                 }
792                 if (!have_interp) {
793                         error = __elfN(load_file)(imgp->proc, interp, &addr,
794                             &imgp->entry_addr);
795                 }
796                 if (error != 0) {
797                         uprintf("ELF interpreter %s not found\n", interp);
798                         return (error);
799                 }
800         } else
801                 addr = et_dyn_addr;
802
803         /*
804          * Construct auxargs table (used by the fixup routine)
805          */
806         elf_auxargs = kmalloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
807         elf_auxargs->execfd = -1;
808         elf_auxargs->phdr = proghdr;
809         elf_auxargs->phent = hdr->e_phentsize;
810         elf_auxargs->phnum = hdr->e_phnum;
811         elf_auxargs->pagesz = PAGE_SIZE;
812         elf_auxargs->base = addr;
813         elf_auxargs->flags = 0;
814         elf_auxargs->entry = entry;
815
816         imgp->auxargs = elf_auxargs;
817         imgp->interpreted = 0;
818         imgp->proc->p_osrel = osrel;
819
820         return (error);
821 }
822
823 int
824 __elfN(dragonfly_fixup)(register_t **stack_base, struct image_params *imgp)
825 {
826         Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
827         Elf_Addr *base;
828         Elf_Addr *pos;
829
830         base = (Elf_Addr *)*stack_base;
831         pos = base + (imgp->args->argc + imgp->args->envc + 2);
832
833         if (args->execfd != -1)
834                 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
835         AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
836         AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
837         AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
838         AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
839         AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
840         AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
841         AUXARGS_ENTRY(pos, AT_BASE, args->base);
842         if (imgp->execpathp != 0)
843                 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
844         AUXARGS_ENTRY(pos, AT_NULL, 0);
845
846         kfree(imgp->auxargs, M_TEMP);
847         imgp->auxargs = NULL;
848
849         base--;
850         suword(base, (long)imgp->args->argc);
851         *stack_base = (register_t *)base;
852         return (0);
853 }
854
855 /*
856  * Code for generating ELF core dumps.
857  */
858
859 typedef int (*segment_callback)(vm_map_entry_t, void *);
860
861 /* Closure for cb_put_phdr(). */
862 struct phdr_closure {
863         Elf_Phdr *phdr;         /* Program header to fill in (incremented) */
864         Elf_Phdr *phdr_max;     /* Pointer bound for error check */
865         Elf_Off offset;         /* Offset of segment in core file */
866 };
867
868 /* Closure for cb_size_segment(). */
869 struct sseg_closure {
870         int count;              /* Count of writable segments. */
871         size_t vsize;           /* Total size of all writable segments. */
872 };
873
874 /* Closure for cb_put_fp(). */
875 struct fp_closure {
876         struct vn_hdr *vnh;
877         struct vn_hdr *vnh_max;
878         int count;
879         struct stat *sb;
880 };
881
882 typedef struct elf_buf {
883         char    *buf;
884         size_t  off;
885         size_t  off_max;
886 } *elf_buf_t;
887
888 static void *target_reserve(elf_buf_t target, size_t bytes, int *error);
889
890 static int cb_put_phdr (vm_map_entry_t, void *);
891 static int cb_size_segment (vm_map_entry_t, void *);
892 static int cb_fpcount_segment(vm_map_entry_t, void *);
893 static int cb_put_fp(vm_map_entry_t, void *);
894
895
896 static int each_segment (struct proc *, segment_callback, void *, int);
897 static int __elfN(corehdr)(struct lwp *, int, struct file *, struct ucred *,
898                         int, elf_buf_t);
899 enum putmode { WRITE, DRYRUN };
900 static int __elfN(puthdr)(struct lwp *, elf_buf_t, int sig, enum putmode,
901                         int, struct file *);
902 static int elf_putallnotes(struct lwp *, elf_buf_t, int, enum putmode);
903 static int __elfN(putnote)(elf_buf_t, const char *, int, const void *, size_t);
904
905 static int elf_putsigs(struct lwp *, elf_buf_t);
906 static int elf_puttextvp(struct proc *, elf_buf_t);
907 static int elf_putfiles(struct proc *, elf_buf_t, struct file *);
908
909 int
910 __elfN(coredump)(struct lwp *lp, int sig, struct vnode *vp, off_t limit)
911 {
912         struct file *fp; 
913         int error;
914
915         if ((error = falloc(NULL, &fp, NULL)) != 0)
916                 return (error);
917         fsetcred(fp, lp->lwp_proc->p_ucred);
918
919         /*
920          * XXX fixme.
921          */
922         fp->f_type = DTYPE_VNODE;
923         fp->f_flag = O_CREAT|O_WRONLY|O_NOFOLLOW;
924         fp->f_ops = &vnode_fileops;
925         fp->f_data = vp;
926         vn_unlock(vp);
927         
928         error = generic_elf_coredump(lp, sig, fp, limit);
929
930         fp->f_type = 0;
931         fp->f_flag = 0;
932         fp->f_ops = &badfileops;
933         fp->f_data = NULL;
934         fdrop(fp);
935         return (error);
936 }
937
938 int
939 generic_elf_coredump(struct lwp *lp, int sig, struct file *fp, off_t limit)
940 {
941         struct proc *p = lp->lwp_proc;
942         struct ucred *cred = p->p_ucred;
943         int error = 0;
944         struct sseg_closure seginfo;
945         struct elf_buf target;
946
947         if (!fp)
948                 kprintf("can't dump core - null fp\n");
949
950         /*
951          * Size the program segments
952          */
953         seginfo.count = 0;
954         seginfo.vsize = 0;
955         each_segment(p, cb_size_segment, &seginfo, 1);
956
957         /*
958          * Calculate the size of the core file header area by making
959          * a dry run of generating it.  Nothing is written, but the
960          * size is calculated.
961          */
962         bzero(&target, sizeof(target));
963         __elfN(puthdr)(lp, &target, sig, DRYRUN, seginfo.count, fp);
964
965         if (target.off + seginfo.vsize >= limit)
966                 return (EFAULT);
967
968         /*
969          * Allocate memory for building the header, fill it up,
970          * and write it out.
971          */
972         target.off_max = target.off;
973         target.off = 0;
974         target.buf = kmalloc(target.off_max, M_TEMP, M_WAITOK|M_ZERO);
975
976         error = __elfN(corehdr)(lp, sig, fp, cred, seginfo.count, &target);
977
978         /* Write the contents of all of the writable segments. */
979         if (error == 0) {
980                 Elf_Phdr *php;
981                 int i;
982                 ssize_t nbytes;
983
984                 php = (Elf_Phdr *)(target.buf + sizeof(Elf_Ehdr)) + 1;
985                 for (i = 0; i < seginfo.count; i++) {
986                         error = fp_write(fp, (caddr_t)php->p_vaddr,
987                                         php->p_filesz, &nbytes, UIO_USERSPACE);
988                         if (error != 0)
989                                 break;
990                         php++;
991                 }
992         }
993         kfree(target.buf, M_TEMP);
994         
995         return (error);
996 }
997
998 /*
999  * A callback for each_segment() to write out the segment's
1000  * program header entry.
1001  */
1002 static int
1003 cb_put_phdr(vm_map_entry_t entry, void *closure)
1004 {
1005         struct phdr_closure *phc = closure;
1006         Elf_Phdr *phdr = phc->phdr;
1007
1008         if (phc->phdr == phc->phdr_max)
1009                 return (EINVAL);
1010
1011         phc->offset = round_page(phc->offset);
1012
1013         phdr->p_type = PT_LOAD;
1014         phdr->p_offset = phc->offset;
1015         phdr->p_vaddr = entry->start;
1016         phdr->p_paddr = 0;
1017         phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1018         phdr->p_align = PAGE_SIZE;
1019         phdr->p_flags = 0;
1020         if (entry->protection & VM_PROT_READ)
1021                 phdr->p_flags |= PF_R;
1022         if (entry->protection & VM_PROT_WRITE)
1023                 phdr->p_flags |= PF_W;
1024         if (entry->protection & VM_PROT_EXECUTE)
1025                 phdr->p_flags |= PF_X;
1026
1027         phc->offset += phdr->p_filesz;
1028         ++phc->phdr;
1029         return (0);
1030 }
1031
1032 /*
1033  * A callback for each_writable_segment() to gather information about
1034  * the number of segments and their total size.
1035  */
1036 static int
1037 cb_size_segment(vm_map_entry_t entry, void *closure)
1038 {
1039         struct sseg_closure *ssc = closure;
1040
1041         ++ssc->count;
1042         ssc->vsize += entry->end - entry->start;
1043         return (0);
1044 }
1045
1046 /*
1047  * A callback for each_segment() to gather information about
1048  * the number of text segments.
1049  */
1050 static int
1051 cb_fpcount_segment(vm_map_entry_t entry, void *closure)
1052 {
1053         int *count = closure;
1054         struct vnode *vp;
1055
1056         if (entry->object.vm_object->type == OBJT_VNODE) {
1057                 vp = (struct vnode *)entry->object.vm_object->handle;
1058                 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
1059                         return (0);
1060                 ++*count;
1061         }
1062         return (0);
1063 }
1064
1065 static int
1066 cb_put_fp(vm_map_entry_t entry, void *closure) 
1067 {
1068         struct fp_closure *fpc = closure;
1069         struct vn_hdr *vnh = fpc->vnh;
1070         Elf_Phdr *phdr = &vnh->vnh_phdr;
1071         struct vnode *vp;
1072         int error;
1073
1074         /*
1075          * If an entry represents a vnode then write out a file handle.
1076          *
1077          * If we are checkpointing a checkpoint-restored program we do
1078          * NOT record the filehandle for the old checkpoint vnode (which
1079          * is mapped all over the place).  Instead we rely on the fact
1080          * that a checkpoint-restored program does not mmap() the checkpt
1081          * vnode NOCORE, so its contents will be written out to the
1082          * new checkpoint file.  This is necessary because the 'old'
1083          * checkpoint file is typically destroyed when a new one is created
1084          * and thus cannot be used to restore the new checkpoint.
1085          *
1086          * Theoretically we could create a chain of checkpoint files and
1087          * operate the checkpointing operation kinda like an incremental
1088          * checkpoint, but a checkpoint restore would then likely wind up
1089          * referencing many prior checkpoint files and that is a bit over
1090          * the top for the purpose of the checkpoint API.
1091          */
1092         if (entry->object.vm_object->type == OBJT_VNODE) {
1093                 vp = (struct vnode *)entry->object.vm_object->handle;
1094                 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
1095                         return (0);
1096                 if (vnh == fpc->vnh_max)
1097                         return (EINVAL);
1098
1099                 if (vp->v_mount)
1100                         vnh->vnh_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1101                 error = VFS_VPTOFH(vp, &vnh->vnh_fh.fh_fid);
1102                 if (error) {
1103                         char *freepath, *fullpath;
1104
1105                         if (vn_fullpath(curproc, vp, &fullpath, &freepath, 0)) {
1106                                 kprintf("Warning: coredump, error %d: cannot store file handle for vnode %p\n", error, vp);
1107                         } else {
1108                                 kprintf("Warning: coredump, error %d: cannot store file handle for %s\n", error, fullpath);
1109                                 kfree(freepath, M_TEMP);
1110                         }
1111                         error = 0;
1112                 }
1113
1114                 phdr->p_type = PT_LOAD;
1115                 phdr->p_offset = 0;        /* not written to core */
1116                 phdr->p_vaddr = entry->start;
1117                 phdr->p_paddr = 0;
1118                 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1119                 phdr->p_align = PAGE_SIZE;
1120                 phdr->p_flags = 0;
1121                 if (entry->protection & VM_PROT_READ)
1122                         phdr->p_flags |= PF_R;
1123                 if (entry->protection & VM_PROT_WRITE)
1124                         phdr->p_flags |= PF_W;
1125                 if (entry->protection & VM_PROT_EXECUTE)
1126                         phdr->p_flags |= PF_X;
1127                 ++fpc->vnh;
1128                 ++fpc->count;
1129         }
1130         return (0);
1131 }
1132
1133 /*
1134  * For each writable segment in the process's memory map, call the given
1135  * function with a pointer to the map entry and some arbitrary
1136  * caller-supplied data.
1137  */
1138 static int
1139 each_segment(struct proc *p, segment_callback func, void *closure, int writable)
1140 {
1141         int error = 0;
1142         vm_map_t map = &p->p_vmspace->vm_map;
1143         vm_map_entry_t entry;
1144
1145         for (entry = map->header.next; error == 0 && entry != &map->header;
1146             entry = entry->next) {
1147                 vm_object_t obj;
1148
1149                 /*
1150                  * Don't dump inaccessible mappings, deal with legacy
1151                  * coredump mode.
1152                  *
1153                  * Note that read-only segments related to the elf binary
1154                  * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1155                  * need to arbitrarily ignore such segments.
1156                  */
1157                 if (elf_legacy_coredump) {
1158                         if (writable && (entry->protection & VM_PROT_RW) != VM_PROT_RW)
1159                                 continue;
1160                 } else {
1161                         if (writable && (entry->protection & VM_PROT_ALL) == 0)
1162                                 continue;
1163                 }
1164
1165                 /*
1166                  * Dont include memory segment in the coredump if
1167                  * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1168                  * madvise(2).
1169                  *
1170                  * Currently we only dump normal VM object maps.  We do
1171                  * not dump submaps or virtual page tables.
1172                  */
1173                 if (writable && (entry->eflags & MAP_ENTRY_NOCOREDUMP))
1174                         continue;
1175                 if (entry->maptype != VM_MAPTYPE_NORMAL)
1176                         continue;
1177                 if ((obj = entry->object.vm_object) == NULL)
1178                         continue;
1179
1180                 /* Find the deepest backing object. */
1181                 while (obj->backing_object != NULL)
1182                         obj = obj->backing_object;
1183
1184                 /* Ignore memory-mapped devices and such things. */
1185                 if (obj->type != OBJT_DEFAULT &&
1186                     obj->type != OBJT_SWAP &&
1187                     obj->type != OBJT_VNODE)
1188                         continue;
1189
1190                 error = (*func)(entry, closure);
1191         }
1192         return (error);
1193 }
1194
1195 static
1196 void *
1197 target_reserve(elf_buf_t target, size_t bytes, int *error)
1198 {
1199     void *res = NULL;
1200
1201     if (target->buf) {
1202             if (target->off + bytes > target->off_max)
1203                     *error = EINVAL;
1204             else
1205                     res = target->buf + target->off;
1206     }
1207     target->off += bytes;
1208     return (res);
1209 }
1210
1211 /*
1212  * Write the core file header to the file, including padding up to
1213  * the page boundary.
1214  */
1215 static int
1216 __elfN(corehdr)(struct lwp *lp, int sig, struct file *fp, struct ucred *cred,
1217             int numsegs, elf_buf_t target)
1218 {
1219         int error;
1220         ssize_t nbytes;
1221
1222         /*
1223          * Fill in the header.  The fp is passed so we can detect and flag
1224          * a checkpoint file pointer within the core file itself, because
1225          * it may not be restored from the same file handle.
1226          */
1227         error = __elfN(puthdr)(lp, target, sig, WRITE, numsegs, fp);
1228
1229         /* Write it to the core file. */
1230         if (error == 0) {
1231                 error = fp_write(fp, target->buf, target->off, &nbytes,
1232                                  UIO_SYSSPACE);
1233         }
1234         return (error);
1235 }
1236
1237 static int
1238 __elfN(puthdr)(struct lwp *lp, elf_buf_t target, int sig, enum putmode mode,
1239     int numsegs, struct file *fp)
1240 {
1241         struct proc *p = lp->lwp_proc;
1242         int error = 0;
1243         size_t phoff;
1244         size_t noteoff;
1245         size_t notesz;
1246         Elf_Ehdr *ehdr;
1247         Elf_Phdr *phdr;
1248
1249         ehdr = target_reserve(target, sizeof(Elf_Ehdr), &error);
1250
1251         phoff = target->off;
1252         phdr = target_reserve(target, (numsegs + 1) * sizeof(Elf_Phdr), &error);
1253
1254         noteoff = target->off;
1255         if (error == 0)
1256                 elf_putallnotes(lp, target, sig, mode);
1257         notesz = target->off - noteoff;
1258
1259         /*
1260          * put extra cruft for dumping process state here 
1261          *  - we really want it be before all the program 
1262          *    mappings
1263          *  - we just need to update the offset accordingly
1264          *    and GDB will be none the wiser.
1265          */
1266         if (error == 0)
1267                 error = elf_puttextvp(p, target);
1268         if (error == 0)
1269                 error = elf_putsigs(lp, target);
1270         if (error == 0)
1271                 error = elf_putfiles(p, target, fp);
1272
1273         /*
1274          * Align up to a page boundary for the program segments.  The
1275          * actual data will be written to the outptu file, not to elf_buf_t,
1276          * so we do not have to do any further bounds checking.
1277          */
1278         target->off = round_page(target->off);
1279         if (error == 0 && ehdr != NULL) {
1280                 /*
1281                  * Fill in the ELF header.
1282                  */
1283                 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1284                 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1285                 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1286                 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1287                 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1288                 ehdr->e_ident[EI_DATA] = ELF_DATA;
1289                 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1290                 ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
1291                 ehdr->e_ident[EI_ABIVERSION] = 0;
1292                 ehdr->e_ident[EI_PAD] = 0;
1293                 ehdr->e_type = ET_CORE;
1294                 ehdr->e_machine = ELF_ARCH;
1295                 ehdr->e_version = EV_CURRENT;
1296                 ehdr->e_entry = 0;
1297                 ehdr->e_phoff = phoff;
1298                 ehdr->e_flags = 0;
1299                 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1300                 ehdr->e_phentsize = sizeof(Elf_Phdr);
1301                 ehdr->e_phnum = numsegs + 1;
1302                 ehdr->e_shentsize = sizeof(Elf_Shdr);
1303                 ehdr->e_shnum = 0;
1304                 ehdr->e_shstrndx = SHN_UNDEF;
1305         }
1306         if (error == 0 && phdr != NULL) {
1307                 /*
1308                  * Fill in the program header entries.
1309                  */
1310                 struct phdr_closure phc;
1311
1312                 /* The note segement. */
1313                 phdr->p_type = PT_NOTE;
1314                 phdr->p_offset = noteoff;
1315                 phdr->p_vaddr = 0;
1316                 phdr->p_paddr = 0;
1317                 phdr->p_filesz = notesz;
1318                 phdr->p_memsz = 0;
1319                 phdr->p_flags = 0;
1320                 phdr->p_align = 0;
1321                 ++phdr;
1322
1323                 /* All the writable segments from the program. */
1324                 phc.phdr = phdr;
1325                 phc.phdr_max = phdr + numsegs;
1326                 phc.offset = target->off;
1327                 each_segment(p, cb_put_phdr, &phc, 1);
1328         }
1329         return (error);
1330 }
1331
1332 /*
1333  * Append core dump notes to target ELF buffer or simply update target size
1334  * if dryrun selected.
1335  */
1336 static int
1337 elf_putallnotes(struct lwp *corelp, elf_buf_t target, int sig,
1338     enum putmode mode)
1339 {
1340         struct proc *p = corelp->lwp_proc;
1341         int error;
1342         struct {
1343                 prstatus_t status;
1344                 prfpregset_t fpregs;
1345                 prpsinfo_t psinfo;
1346         } *tmpdata;
1347         prstatus_t *status;
1348         prfpregset_t *fpregs;
1349         prpsinfo_t *psinfo;
1350         struct lwp *lp;
1351
1352         /*
1353          * Allocate temporary storage for notes on heap to avoid stack overflow.
1354          */
1355         if (mode != DRYRUN) {
1356                 tmpdata = kmalloc(sizeof(*tmpdata), M_TEMP, M_ZERO | M_WAITOK);
1357                 status = &tmpdata->status;
1358                 fpregs = &tmpdata->fpregs;
1359                 psinfo = &tmpdata->psinfo;
1360         } else {
1361                 tmpdata = NULL;
1362                 status = NULL;
1363                 fpregs = NULL;
1364                 psinfo = NULL;
1365         }
1366
1367         /*
1368          * Append LWP-agnostic note.
1369          */
1370         if (mode != DRYRUN) {
1371                 psinfo->pr_version = PRPSINFO_VERSION;
1372                 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
1373                 strlcpy(psinfo->pr_fname, p->p_comm,
1374                         sizeof(psinfo->pr_fname));
1375                 /*
1376                  * XXX - We don't fill in the command line arguments
1377                  * properly yet.
1378                  */
1379                 strlcpy(psinfo->pr_psargs, p->p_comm,
1380                         sizeof(psinfo->pr_psargs));
1381         }
1382         error =
1383             __elfN(putnote)(target, "CORE", NT_PRPSINFO, psinfo, sizeof *psinfo);
1384         if (error)
1385                 goto exit;
1386
1387         /*
1388          * Append first note for LWP that triggered core so that it is
1389          * the selected one when the debugger starts.
1390          */
1391         if (mode != DRYRUN) {
1392                 status->pr_version = PRSTATUS_VERSION;
1393                 status->pr_statussz = sizeof(prstatus_t);
1394                 status->pr_gregsetsz = sizeof(gregset_t);
1395                 status->pr_fpregsetsz = sizeof(fpregset_t);
1396                 status->pr_osreldate = osreldate;
1397                 status->pr_cursig = sig;
1398                 /*
1399                  * XXX GDB needs unique pr_pid for each LWP and does not
1400                  * not support pr_pid==0 but lwp_tid can be 0, so hack unique
1401                  * value.
1402                  */
1403                 status->pr_pid = corelp->lwp_tid;
1404                 fill_regs(corelp, &status->pr_reg);
1405                 fill_fpregs(corelp, fpregs);
1406         }
1407         error =
1408             __elfN(putnote)(target, "CORE", NT_PRSTATUS, status, sizeof *status);
1409         if (error)
1410                 goto exit;
1411         error =
1412             __elfN(putnote)(target, "CORE", NT_FPREGSET, fpregs, sizeof *fpregs);
1413         if (error)
1414                 goto exit;
1415
1416         /*
1417          * Then append notes for other LWPs.
1418          */
1419         FOREACH_LWP_IN_PROC(lp, p) {
1420                 if (lp == corelp)
1421                         continue;
1422                 /* skip lwps being created */
1423                 if (lp->lwp_thread == NULL)
1424                         continue;
1425                 if (mode != DRYRUN) {
1426                         status->pr_pid = lp->lwp_tid;
1427                         fill_regs(lp, &status->pr_reg);
1428                         fill_fpregs(lp, fpregs);
1429                 }
1430                 error = __elfN(putnote)(target, "CORE", NT_PRSTATUS,
1431                                         status, sizeof *status);
1432                 if (error)
1433                         goto exit;
1434                 error = __elfN(putnote)(target, "CORE", NT_FPREGSET,
1435                                         fpregs, sizeof *fpregs);
1436                 if (error)
1437                         goto exit;
1438         }
1439
1440 exit:
1441         if (tmpdata != NULL)
1442                 kfree(tmpdata, M_TEMP);
1443         return (error);
1444 }
1445
1446 /*
1447  * Generate a note sub-structure.
1448  *
1449  * NOTE: 4-byte alignment.
1450  */
1451 static int
1452 __elfN(putnote)(elf_buf_t target, const char *name, int type,
1453             const void *desc, size_t descsz)
1454 {
1455         int error = 0;
1456         char *dst;
1457         Elf_Note note;
1458
1459         note.n_namesz = strlen(name) + 1;
1460         note.n_descsz = descsz;
1461         note.n_type = type;
1462         dst = target_reserve(target, sizeof(note), &error);
1463         if (dst != NULL)
1464                 bcopy(&note, dst, sizeof note);
1465         dst = target_reserve(target, note.n_namesz, &error);
1466         if (dst != NULL)
1467                 bcopy(name, dst, note.n_namesz);
1468         target->off = roundup2(target->off, sizeof(Elf_Word));
1469         dst = target_reserve(target, note.n_descsz, &error);
1470         if (dst != NULL)
1471                 bcopy(desc, dst, note.n_descsz);
1472         target->off = roundup2(target->off, sizeof(Elf_Word));
1473         return (error);
1474 }
1475
1476
1477 static int
1478 elf_putsigs(struct lwp *lp, elf_buf_t target)
1479 {
1480         /* XXX lwp handle more than one lwp */
1481         struct proc *p = lp->lwp_proc;
1482         int error = 0;
1483         struct ckpt_siginfo *csi;
1484
1485         csi = target_reserve(target, sizeof(struct ckpt_siginfo), &error);
1486         if (csi) {
1487                 csi->csi_ckptpisz = sizeof(struct ckpt_siginfo);
1488                 bcopy(p->p_sigacts, &csi->csi_sigacts, sizeof(*p->p_sigacts));
1489                 bcopy(&p->p_realtimer, &csi->csi_itimerval, sizeof(struct itimerval));
1490                 bcopy(&lp->lwp_sigmask, &csi->csi_sigmask,
1491                         sizeof(sigset_t));
1492                 csi->csi_sigparent = p->p_sigparent;
1493         }
1494         return (error);
1495 }
1496
1497 static int
1498 elf_putfiles(struct proc *p, elf_buf_t target, struct file *ckfp)
1499 {
1500         int error = 0;
1501         int i;
1502         struct ckpt_filehdr *cfh = NULL;
1503         struct ckpt_fileinfo *cfi;
1504         struct file *fp;        
1505         struct vnode *vp;
1506         /*
1507          * the duplicated loop is gross, but it was the only way
1508          * to eliminate uninitialized variable warnings 
1509          */
1510         cfh = target_reserve(target, sizeof(struct ckpt_filehdr), &error);
1511         if (cfh) {
1512                 cfh->cfh_nfiles = 0;            
1513         }
1514
1515         /*
1516          * ignore STDIN/STDERR/STDOUT.
1517          */
1518         for (i = 3; error == 0 && i < p->p_fd->fd_nfiles; i++) {
1519                 fp = holdfp(p->p_fd, i, -1);
1520                 if (fp == NULL)
1521                         continue;
1522                 /* 
1523                  * XXX Only checkpoint vnodes for now.
1524                  */
1525                 if (fp->f_type != DTYPE_VNODE) {
1526                         fdrop(fp);
1527                         continue;
1528                 }
1529                 cfi = target_reserve(target, sizeof(struct ckpt_fileinfo),
1530                                         &error);
1531                 if (cfi == NULL) {
1532                         fdrop(fp);
1533                         continue;
1534                 }
1535                 cfi->cfi_index = -1;
1536                 cfi->cfi_type = fp->f_type;
1537                 cfi->cfi_flags = fp->f_flag;
1538                 cfi->cfi_offset = fp->f_offset;
1539                 cfi->cfi_ckflags = 0;
1540
1541                 if (fp == ckfp)
1542                         cfi->cfi_ckflags |= CKFIF_ISCKPTFD;
1543                 /* f_count and f_msgcount should not be saved/restored */
1544                 /* XXX save cred info */
1545
1546                 switch(fp->f_type) {
1547                 case DTYPE_VNODE:
1548                         vp = (struct vnode *)fp->f_data;
1549                         /*
1550                          * it looks like a bug in ptrace is marking 
1551                          * a non-vnode as a vnode - until we find the 
1552                          * root cause this will at least prevent
1553                          * further panics from truss
1554                          */
1555                         if (vp == NULL || vp->v_mount == NULL)
1556                                 break;
1557                         cfh->cfh_nfiles++;
1558                         cfi->cfi_index = i;
1559                         cfi->cfi_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1560                         error = VFS_VPTOFH(vp, &cfi->cfi_fh.fh_fid);
1561                         break;
1562                 default:
1563                         break;
1564                 }
1565                 fdrop(fp);
1566         }
1567         return (error);
1568 }
1569
1570 static int
1571 elf_puttextvp(struct proc *p, elf_buf_t target)
1572 {
1573         int error = 0;
1574         int *vn_count;
1575         struct fp_closure fpc;
1576         struct ckpt_vminfo *vminfo;
1577
1578         vminfo = target_reserve(target, sizeof(struct ckpt_vminfo), &error);
1579         if (vminfo != NULL) {
1580                 vminfo->cvm_dsize = p->p_vmspace->vm_dsize;
1581                 vminfo->cvm_tsize = p->p_vmspace->vm_tsize;
1582                 vminfo->cvm_daddr = p->p_vmspace->vm_daddr;
1583                 vminfo->cvm_taddr = p->p_vmspace->vm_taddr;
1584         }
1585
1586         fpc.count = 0;
1587         vn_count = target_reserve(target, sizeof(int), &error);
1588         if (target->buf != NULL) {
1589                 fpc.vnh = (struct vn_hdr *)(target->buf + target->off);
1590                 fpc.vnh_max = fpc.vnh + 
1591                         (target->off_max - target->off) / sizeof(struct vn_hdr);
1592                 error = each_segment(p, cb_put_fp, &fpc, 0);
1593                 if (vn_count)
1594                         *vn_count = fpc.count;
1595         } else {
1596                 error = each_segment(p, cb_fpcount_segment, &fpc.count, 0);
1597         }
1598         target->off += fpc.count * sizeof(struct vn_hdr);
1599         return (error);
1600 }
1601
1602 /*
1603  * Try to find the appropriate ABI-note section for checknote,
1604  * The entire image is searched if necessary, not only the first page.
1605  */
1606 static boolean_t
1607 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
1608     int32_t *osrel)
1609 {
1610         boolean_t valid_note_found;
1611         const Elf_Phdr *phdr, *pnote;
1612         const Elf_Ehdr *hdr;
1613         int i;
1614
1615         valid_note_found = FALSE;
1616         hdr = (const Elf_Ehdr *)imgp->image_header;
1617         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
1618
1619         for (i = 0; i < hdr->e_phnum; i++) {
1620                 if (phdr[i].p_type == PT_NOTE) {
1621                         pnote = &phdr[i];
1622                         valid_note_found = check_PT_NOTE (imgp, checknote,
1623                                 osrel, pnote);
1624                         if (valid_note_found)
1625                                 break;
1626                 }
1627         }
1628         return valid_note_found;
1629 }
1630
1631 static boolean_t
1632 check_PT_NOTE(struct image_params *imgp, Elf_Brandnote *checknote,
1633     int32_t *osrel, const Elf_Phdr * pnote)
1634 {
1635         boolean_t limited_to_first_page;
1636         boolean_t found = FALSE;
1637         const Elf_Note *note, *note0, *note_end;
1638         const char *note_name;
1639         __ElfN(Off) noteloc, firstloc;
1640         __ElfN(Size) notesz, firstlen, endbyte;
1641         struct lwbuf *lwb;
1642         struct lwbuf lwb_cache;
1643         const char *page;
1644         char *data = NULL;
1645         int n;
1646
1647         notesz = pnote->p_filesz;
1648         noteloc = pnote->p_offset;
1649         endbyte = noteloc + notesz;
1650         limited_to_first_page = noteloc < PAGE_SIZE && endbyte < PAGE_SIZE;
1651
1652         if (limited_to_first_page) {
1653                 note = (const Elf_Note *)(imgp->image_header + noteloc);
1654                 note_end = (const Elf_Note *)(imgp->image_header + endbyte);
1655                 note0 = note;
1656         } else {
1657                 firstloc = noteloc & PAGE_MASK;
1658                 firstlen = PAGE_SIZE - firstloc;
1659                 if (notesz < sizeof(Elf_Note) || notesz > PAGE_SIZE)
1660                         return (FALSE);
1661
1662                 lwb = &lwb_cache;
1663                 if (exec_map_page(imgp, noteloc >> PAGE_SHIFT, &lwb, &page))
1664                         return (FALSE);
1665                 if (firstlen < notesz) {         /* crosses page boundary */
1666                         data = kmalloc(notesz, M_TEMP, M_WAITOK);
1667                         bcopy(page + firstloc, data, firstlen);
1668
1669                         exec_unmap_page(lwb);
1670                         lwb = &lwb_cache;
1671                         if (exec_map_page(imgp, (noteloc >> PAGE_SHIFT) + 1,
1672                                 &lwb, &page)) {
1673                                 kfree(data, M_TEMP);
1674                         }
1675                         bcopy(page, data + firstlen, notesz - firstlen);
1676                         note = note0 = (const Elf_Note *)(data);
1677                         note_end = (const Elf_Note *)(data + notesz);
1678                 } else {
1679                         note = note0 = (const Elf_Note *)(page + firstloc);
1680                         note_end = (const Elf_Note *)(page + firstloc +
1681                                 firstlen);
1682                 }
1683         }
1684
1685         for (n = 0; n < 100 && note >= note0 && note < note_end; n++) {
1686                 if (!aligned(note, Elf32_Addr))
1687                         break;
1688                 note_name = (const char *)(note + 1);
1689
1690                 if (note->n_namesz == checknote->hdr.n_namesz
1691                     && note->n_descsz == checknote->hdr.n_descsz
1692                     && note->n_type == checknote->hdr.n_type
1693                     && (strncmp(checknote->vendor, note_name,
1694                         checknote->hdr.n_namesz) == 0)) {
1695                         /* Fetch osreldata from ABI.note-tag */
1696                         if ((checknote->flags & BN_CAN_FETCH_OSREL) != 0 &&
1697                                 osrel != NULL)
1698                                 *osrel = *(const int32_t *) (note_name +
1699                                         roundup2(checknote->hdr.n_namesz,
1700                                         sizeof(Elf32_Addr)));
1701                         found = TRUE;
1702                         break;
1703                 }
1704                 note = (const Elf_Note *)((const char *)(note + 1) +
1705                     roundup2(note->n_namesz, sizeof(Elf32_Addr)) +
1706                     roundup2(note->n_descsz, sizeof(Elf32_Addr)));
1707         }
1708
1709         if (!limited_to_first_page) {
1710                 if (data != NULL)
1711                         kfree(data, M_TEMP);
1712                 exec_unmap_page(lwb);
1713         }
1714         return (found);
1715 }
1716
1717
1718 /*
1719  * Tell kern_execve.c about it, with a little help from the linker.
1720  */
1721 #if defined(__x86_64__)
1722 static struct execsw elf_execsw = {exec_elf64_imgact, "ELF64"};
1723 EXEC_SET_ORDERED(elf64, elf_execsw, SI_ORDER_FIRST);
1724 #else /* i386 assumed */
1725 static struct execsw elf_execsw = {exec_elf32_imgact, "ELF32"};
1726 EXEC_SET_ORDERED(elf32, elf_execsw, SI_ORDER_FIRST);
1727 #endif