]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/kern/imgact_elf.c
MFC r249486:
[FreeBSD/stable/9.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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_capsicum.h"
35 #include "opt_compat.h"
36 #include "opt_core.h"
37
38 #include <sys/param.h>
39 #include <sys/capability.h>
40 #include <sys/exec.h>
41 #include <sys/fcntl.h>
42 #include <sys/imgact.h>
43 #include <sys/imgact_elf.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/mman.h>
50 #include <sys/namei.h>
51 #include <sys/pioctl.h>
52 #include <sys/proc.h>
53 #include <sys/procfs.h>
54 #include <sys/racct.h>
55 #include <sys/resourcevar.h>
56 #include <sys/sbuf.h>
57 #include <sys/sf_buf.h>
58 #include <sys/smp.h>
59 #include <sys/systm.h>
60 #include <sys/signalvar.h>
61 #include <sys/stat.h>
62 #include <sys/sx.h>
63 #include <sys/syscall.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysent.h>
66 #include <sys/vnode.h>
67 #include <sys/syslog.h>
68 #include <sys/eventhandler.h>
69
70 #include <net/zlib.h>
71
72 #include <vm/vm.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_param.h>
75 #include <vm/pmap.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_extern.h>
79
80 #include <machine/elf.h>
81 #include <machine/md_var.h>
82
83 #define OLD_EI_BRAND    8
84
85 static int __elfN(check_header)(const Elf_Ehdr *hdr);
86 static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
87     const char *interp, int interp_name_len, int32_t *osrel);
88 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
89     u_long *entry, size_t pagesize);
90 static int __elfN(load_section)(struct vmspace *vmspace, vm_object_t object,
91     vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
92     vm_prot_t prot, size_t pagesize);
93 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
94 static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note,
95     int32_t *osrel);
96 static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
97 static boolean_t __elfN(check_note)(struct image_params *imgp,
98     Elf_Brandnote *checknote, int32_t *osrel);
99 static vm_prot_t __elfN(trans_prot)(Elf_Word);
100 static Elf_Word __elfN(untrans_prot)(vm_prot_t);
101
102 SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
103     "");
104
105 #ifdef COMPRESS_USER_CORES
106 static int compress_core(gzFile, char *, char *, unsigned int,
107     struct thread * td);
108 #endif
109 #define CORE_BUF_SIZE   (16 * 1024)
110
111 int __elfN(fallback_brand) = -1;
112 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
113     fallback_brand, CTLFLAG_RW, &__elfN(fallback_brand), 0,
114     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
115 TUNABLE_INT("kern.elf" __XSTRING(__ELF_WORD_SIZE) ".fallback_brand",
116     &__elfN(fallback_brand));
117
118 static int elf_legacy_coredump = 0;
119 SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW, 
120     &elf_legacy_coredump, 0, "");
121
122 int __elfN(nxstack) =
123 #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */
124         1;
125 #else
126         0;
127 #endif
128 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
129     nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
130     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
131
132 #if __ELF_WORD_SIZE == 32
133 #if defined(__amd64__) || defined(__ia64__)
134 int i386_read_exec = 0;
135 SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
136     "enable execution from readable segments");
137 #endif
138 #endif
139
140 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
141
142 #define trunc_page_ps(va, ps)   ((va) & ~(ps - 1))
143 #define round_page_ps(va, ps)   (((va) + (ps - 1)) & ~(ps - 1))
144 #define aligned(a, t)   (trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a))
145
146 static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
147
148 Elf_Brandnote __elfN(freebsd_brandnote) = {
149         .hdr.n_namesz   = sizeof(FREEBSD_ABI_VENDOR),
150         .hdr.n_descsz   = sizeof(int32_t),
151         .hdr.n_type     = 1,
152         .vendor         = FREEBSD_ABI_VENDOR,
153         .flags          = BN_TRANSLATE_OSREL,
154         .trans_osrel    = __elfN(freebsd_trans_osrel)
155 };
156
157 static boolean_t
158 __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
159 {
160         uintptr_t p;
161
162         p = (uintptr_t)(note + 1);
163         p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
164         *osrel = *(const int32_t *)(p);
165
166         return (TRUE);
167 }
168
169 static const char GNU_ABI_VENDOR[] = "GNU";
170 static int GNU_KFREEBSD_ABI_DESC = 3;
171
172 Elf_Brandnote __elfN(kfreebsd_brandnote) = {
173         .hdr.n_namesz   = sizeof(GNU_ABI_VENDOR),
174         .hdr.n_descsz   = 16,   /* XXX at least 16 */
175         .hdr.n_type     = 1,
176         .vendor         = GNU_ABI_VENDOR,
177         .flags          = BN_TRANSLATE_OSREL,
178         .trans_osrel    = kfreebsd_trans_osrel
179 };
180
181 static boolean_t
182 kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
183 {
184         const Elf32_Word *desc;
185         uintptr_t p;
186
187         p = (uintptr_t)(note + 1);
188         p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
189
190         desc = (const Elf32_Word *)p;
191         if (desc[0] != GNU_KFREEBSD_ABI_DESC)
192                 return (FALSE);
193
194         /*
195          * Debian GNU/kFreeBSD embed the earliest compatible kernel version
196          * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
197          */
198         *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
199
200         return (TRUE);
201 }
202
203 int
204 __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
205 {
206         int i;
207
208         for (i = 0; i < MAX_BRANDS; i++) {
209                 if (elf_brand_list[i] == NULL) {
210                         elf_brand_list[i] = entry;
211                         break;
212                 }
213         }
214         if (i == MAX_BRANDS) {
215                 printf("WARNING: %s: could not insert brandinfo entry: %p\n",
216                         __func__, entry);
217                 return (-1);
218         }
219         return (0);
220 }
221
222 int
223 __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
224 {
225         int i;
226
227         for (i = 0; i < MAX_BRANDS; i++) {
228                 if (elf_brand_list[i] == entry) {
229                         elf_brand_list[i] = NULL;
230                         break;
231                 }
232         }
233         if (i == MAX_BRANDS)
234                 return (-1);
235         return (0);
236 }
237
238 int
239 __elfN(brand_inuse)(Elf_Brandinfo *entry)
240 {
241         struct proc *p;
242         int rval = FALSE;
243
244         sx_slock(&allproc_lock);
245         FOREACH_PROC_IN_SYSTEM(p) {
246                 if (p->p_sysent == entry->sysvec) {
247                         rval = TRUE;
248                         break;
249                 }
250         }
251         sx_sunlock(&allproc_lock);
252
253         return (rval);
254 }
255
256 static Elf_Brandinfo *
257 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
258     int interp_name_len, int32_t *osrel)
259 {
260         const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
261         Elf_Brandinfo *bi;
262         boolean_t ret;
263         int i;
264
265         /*
266          * We support four types of branding -- (1) the ELF EI_OSABI field
267          * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
268          * branding w/in the ELF header, (3) path of the `interp_path'
269          * field, and (4) the ".note.ABI-tag" ELF section.
270          */
271
272         /* Look for an ".note.ABI-tag" ELF section */
273         for (i = 0; i < MAX_BRANDS; i++) {
274                 bi = elf_brand_list[i];
275                 if (bi == NULL)
276                         continue;
277                 if (hdr->e_machine == bi->machine && (bi->flags &
278                     (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
279                         ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
280                         if (ret)
281                                 return (bi);
282                 }
283         }
284
285         /* If the executable has a brand, search for it in the brand list. */
286         for (i = 0; i < MAX_BRANDS; i++) {
287                 bi = elf_brand_list[i];
288                 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
289                         continue;
290                 if (hdr->e_machine == bi->machine &&
291                     (hdr->e_ident[EI_OSABI] == bi->brand ||
292                     strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
293                     bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
294                         return (bi);
295         }
296
297         /* Lacking a known brand, search for a recognized interpreter. */
298         if (interp != NULL) {
299                 for (i = 0; i < MAX_BRANDS; i++) {
300                         bi = elf_brand_list[i];
301                         if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
302                                 continue;
303                         if (hdr->e_machine == bi->machine &&
304                             /* ELF image p_filesz includes terminating zero */
305                             strlen(bi->interp_path) + 1 == interp_name_len &&
306                             strncmp(interp, bi->interp_path, interp_name_len)
307                             == 0)
308                                 return (bi);
309                 }
310         }
311
312         /* Lacking a recognized interpreter, try the default brand */
313         for (i = 0; i < MAX_BRANDS; i++) {
314                 bi = elf_brand_list[i];
315                 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
316                         continue;
317                 if (hdr->e_machine == bi->machine &&
318                     __elfN(fallback_brand) == bi->brand)
319                         return (bi);
320         }
321         return (NULL);
322 }
323
324 static int
325 __elfN(check_header)(const Elf_Ehdr *hdr)
326 {
327         Elf_Brandinfo *bi;
328         int i;
329
330         if (!IS_ELF(*hdr) ||
331             hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
332             hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
333             hdr->e_ident[EI_VERSION] != EV_CURRENT ||
334             hdr->e_phentsize != sizeof(Elf_Phdr) ||
335             hdr->e_version != ELF_TARG_VER)
336                 return (ENOEXEC);
337
338         /*
339          * Make sure we have at least one brand for this machine.
340          */
341
342         for (i = 0; i < MAX_BRANDS; i++) {
343                 bi = elf_brand_list[i];
344                 if (bi != NULL && bi->machine == hdr->e_machine)
345                         break;
346         }
347         if (i == MAX_BRANDS)
348                 return (ENOEXEC);
349
350         return (0);
351 }
352
353 static int
354 __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
355     vm_offset_t start, vm_offset_t end, vm_prot_t prot)
356 {
357         struct sf_buf *sf;
358         int error;
359         vm_offset_t off;
360
361         /*
362          * Create the page if it doesn't exist yet. Ignore errors.
363          */
364         vm_map_lock(map);
365         vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end),
366             VM_PROT_ALL, VM_PROT_ALL, 0);
367         vm_map_unlock(map);
368
369         /*
370          * Find the page from the underlying object.
371          */
372         if (object) {
373                 sf = vm_imgact_map_page(object, offset);
374                 if (sf == NULL)
375                         return (KERN_FAILURE);
376                 off = offset - trunc_page(offset);
377                 error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
378                     end - start);
379                 vm_imgact_unmap_page(sf);
380                 if (error) {
381                         return (KERN_FAILURE);
382                 }
383         }
384
385         return (KERN_SUCCESS);
386 }
387
388 static int
389 __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
390     vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow)
391 {
392         struct sf_buf *sf;
393         vm_offset_t off;
394         vm_size_t sz;
395         int error, rv;
396
397         if (start != trunc_page(start)) {
398                 rv = __elfN(map_partial)(map, object, offset, start,
399                     round_page(start), prot);
400                 if (rv)
401                         return (rv);
402                 offset += round_page(start) - start;
403                 start = round_page(start);
404         }
405         if (end != round_page(end)) {
406                 rv = __elfN(map_partial)(map, object, offset +
407                     trunc_page(end) - start, trunc_page(end), end, prot);
408                 if (rv)
409                         return (rv);
410                 end = trunc_page(end);
411         }
412         if (end > start) {
413                 if (offset & PAGE_MASK) {
414                         /*
415                          * The mapping is not page aligned. This means we have
416                          * to copy the data. Sigh.
417                          */
418                         rv = vm_map_find(map, NULL, 0, &start, end - start,
419                             FALSE, prot | VM_PROT_WRITE, VM_PROT_ALL, 0);
420                         if (rv)
421                                 return (rv);
422                         if (object == NULL)
423                                 return (KERN_SUCCESS);
424                         for (; start < end; start += sz) {
425                                 sf = vm_imgact_map_page(object, offset);
426                                 if (sf == NULL)
427                                         return (KERN_FAILURE);
428                                 off = offset - trunc_page(offset);
429                                 sz = end - start;
430                                 if (sz > PAGE_SIZE - off)
431                                         sz = PAGE_SIZE - off;
432                                 error = copyout((caddr_t)sf_buf_kva(sf) + off,
433                                     (caddr_t)start, sz);
434                                 vm_imgact_unmap_page(sf);
435                                 if (error) {
436                                         return (KERN_FAILURE);
437                                 }
438                                 offset += sz;
439                         }
440                         rv = KERN_SUCCESS;
441                 } else {
442                         vm_object_reference(object);
443                         vm_map_lock(map);
444                         rv = vm_map_insert(map, object, offset, start, end,
445                             prot, VM_PROT_ALL, cow);
446                         vm_map_unlock(map);
447                         if (rv != KERN_SUCCESS)
448                                 vm_object_deallocate(object);
449                 }
450                 return (rv);
451         } else {
452                 return (KERN_SUCCESS);
453         }
454 }
455
456 static int
457 __elfN(load_section)(struct vmspace *vmspace,
458         vm_object_t object, vm_offset_t offset,
459         caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
460         size_t pagesize)
461 {
462         struct sf_buf *sf;
463         size_t map_len;
464         vm_offset_t map_addr;
465         int error, rv, cow;
466         size_t copy_len;
467         vm_offset_t file_addr;
468
469         /*
470          * It's necessary to fail if the filsz + offset taken from the
471          * header is greater than the actual file pager object's size.
472          * If we were to allow this, then the vm_map_find() below would
473          * walk right off the end of the file object and into the ether.
474          *
475          * While I'm here, might as well check for something else that
476          * is invalid: filsz cannot be greater than memsz.
477          */
478         if ((off_t)filsz + offset > object->un_pager.vnp.vnp_size ||
479             filsz > memsz) {
480                 uprintf("elf_load_section: truncated ELF file\n");
481                 return (ENOEXEC);
482         }
483
484         map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
485         file_addr = trunc_page_ps(offset, pagesize);
486
487         /*
488          * We have two choices.  We can either clear the data in the last page
489          * of an oversized mapping, or we can start the anon mapping a page
490          * early and copy the initialized data into that first page.  We
491          * choose the second..
492          */
493         if (memsz > filsz)
494                 map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
495         else
496                 map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
497
498         if (map_len != 0) {
499                 /* cow flags: don't dump readonly sections in core */
500                 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
501                     (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
502
503                 rv = __elfN(map_insert)(&vmspace->vm_map,
504                                       object,
505                                       file_addr,        /* file offset */
506                                       map_addr,         /* virtual start */
507                                       map_addr + map_len,/* virtual end */
508                                       prot,
509                                       cow);
510                 if (rv != KERN_SUCCESS)
511                         return (EINVAL);
512
513                 /* we can stop now if we've covered it all */
514                 if (memsz == filsz) {
515                         return (0);
516                 }
517         }
518
519
520         /*
521          * We have to get the remaining bit of the file into the first part
522          * of the oversized map segment.  This is normally because the .data
523          * segment in the file is extended to provide bss.  It's a neat idea
524          * to try and save a page, but it's a pain in the behind to implement.
525          */
526         copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize);
527         map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
528         map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
529             map_addr;
530
531         /* This had damn well better be true! */
532         if (map_len != 0) {
533                 rv = __elfN(map_insert)(&vmspace->vm_map, NULL, 0, map_addr,
534                     map_addr + map_len, VM_PROT_ALL, 0);
535                 if (rv != KERN_SUCCESS) {
536                         return (EINVAL);
537                 }
538         }
539
540         if (copy_len != 0) {
541                 vm_offset_t off;
542
543                 sf = vm_imgact_map_page(object, offset + filsz);
544                 if (sf == NULL)
545                         return (EIO);
546
547                 /* send the page fragment to user space */
548                 off = trunc_page_ps(offset + filsz, pagesize) -
549                     trunc_page(offset + filsz);
550                 error = copyout((caddr_t)sf_buf_kva(sf) + off,
551                     (caddr_t)map_addr, copy_len);
552                 vm_imgact_unmap_page(sf);
553                 if (error) {
554                         return (error);
555                 }
556         }
557
558         /*
559          * set it to the specified protection.
560          * XXX had better undo the damage from pasting over the cracks here!
561          */
562         vm_map_protect(&vmspace->vm_map, trunc_page(map_addr),
563             round_page(map_addr + map_len),  prot, FALSE);
564
565         return (0);
566 }
567
568 /*
569  * Load the file "file" into memory.  It may be either a shared object
570  * or an executable.
571  *
572  * The "addr" reference parameter is in/out.  On entry, it specifies
573  * the address where a shared object should be loaded.  If the file is
574  * an executable, this value is ignored.  On exit, "addr" specifies
575  * where the file was actually loaded.
576  *
577  * The "entry" reference parameter is out only.  On exit, it specifies
578  * the entry point for the loaded file.
579  */
580 static int
581 __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
582         u_long *entry, size_t pagesize)
583 {
584         struct {
585                 struct nameidata nd;
586                 struct vattr attr;
587                 struct image_params image_params;
588         } *tempdata;
589         const Elf_Ehdr *hdr = NULL;
590         const Elf_Phdr *phdr = NULL;
591         struct nameidata *nd;
592         struct vmspace *vmspace = p->p_vmspace;
593         struct vattr *attr;
594         struct image_params *imgp;
595         vm_prot_t prot;
596         u_long rbase;
597         u_long base_addr = 0;
598         int vfslocked, error, i, numsegs;
599
600 #ifdef CAPABILITY_MODE
601         /*
602          * XXXJA: This check can go away once we are sufficiently confident
603          * that the checks in namei() are correct.
604          */
605         if (IN_CAPABILITY_MODE(curthread))
606                 return (ECAPMODE);
607 #endif
608
609         tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
610         nd = &tempdata->nd;
611         attr = &tempdata->attr;
612         imgp = &tempdata->image_params;
613
614         /*
615          * Initialize part of the common data
616          */
617         imgp->proc = p;
618         imgp->attr = attr;
619         imgp->firstpage = NULL;
620         imgp->image_header = NULL;
621         imgp->object = NULL;
622         imgp->execlabel = NULL;
623
624         NDINIT(nd, LOOKUP, MPSAFE|LOCKLEAF|FOLLOW, UIO_SYSSPACE, file,
625             curthread);
626         vfslocked = 0;
627         if ((error = namei(nd)) != 0) {
628                 nd->ni_vp = NULL;
629                 goto fail;
630         }
631         vfslocked = NDHASGIANT(nd);
632         NDFREE(nd, NDF_ONLY_PNBUF);
633         imgp->vp = nd->ni_vp;
634
635         /*
636          * Check permissions, modes, uid, etc on the file, and "open" it.
637          */
638         error = exec_check_permissions(imgp);
639         if (error)
640                 goto fail;
641
642         error = exec_map_first_page(imgp);
643         if (error)
644                 goto fail;
645
646         /*
647          * Also make certain that the interpreter stays the same, so set
648          * its VV_TEXT flag, too.
649          */
650         VOP_SET_TEXT(nd->ni_vp);
651
652         imgp->object = nd->ni_vp->v_object;
653
654         hdr = (const Elf_Ehdr *)imgp->image_header;
655         if ((error = __elfN(check_header)(hdr)) != 0)
656                 goto fail;
657         if (hdr->e_type == ET_DYN)
658                 rbase = *addr;
659         else if (hdr->e_type == ET_EXEC)
660                 rbase = 0;
661         else {
662                 error = ENOEXEC;
663                 goto fail;
664         }
665
666         /* Only support headers that fit within first page for now      */
667         if ((hdr->e_phoff > PAGE_SIZE) ||
668             (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
669                 error = ENOEXEC;
670                 goto fail;
671         }
672
673         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
674         if (!aligned(phdr, Elf_Addr)) {
675                 error = ENOEXEC;
676                 goto fail;
677         }
678
679         for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
680                 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
681                         /* Loadable segment */
682                         prot = __elfN(trans_prot)(phdr[i].p_flags);
683                         if ((error = __elfN(load_section)(vmspace,
684                             imgp->object, phdr[i].p_offset,
685                             (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
686                             phdr[i].p_memsz, phdr[i].p_filesz, prot,
687                             pagesize)) != 0)
688                                 goto fail;
689                         /*
690                          * Establish the base address if this is the
691                          * first segment.
692                          */
693                         if (numsegs == 0)
694                                 base_addr = trunc_page(phdr[i].p_vaddr +
695                                     rbase);
696                         numsegs++;
697                 }
698         }
699         *addr = base_addr;
700         *entry = (unsigned long)hdr->e_entry + rbase;
701
702 fail:
703         if (imgp->firstpage)
704                 exec_unmap_first_page(imgp);
705
706         if (nd->ni_vp)
707                 vput(nd->ni_vp);
708
709         VFS_UNLOCK_GIANT(vfslocked);
710         free(tempdata, M_TEMP);
711
712         return (error);
713 }
714
715 static int
716 __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
717 {
718         const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
719         const Elf_Phdr *phdr;
720         Elf_Auxargs *elf_auxargs;
721         struct vmspace *vmspace;
722         vm_prot_t prot;
723         u_long text_size = 0, data_size = 0, total_size = 0;
724         u_long text_addr = 0, data_addr = 0;
725         u_long seg_size, seg_addr;
726         u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0;
727         int32_t osrel = 0;
728         int error = 0, i, n, interp_name_len = 0;
729         const char *interp = NULL, *newinterp = NULL;
730         Elf_Brandinfo *brand_info;
731         char *path;
732         struct sysentvec *sv;
733
734         /*
735          * Do we have a valid ELF header ?
736          *
737          * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
738          * if particular brand doesn't support it.
739          */
740         if (__elfN(check_header)(hdr) != 0 ||
741             (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
742                 return (-1);
743
744         /*
745          * From here on down, we return an errno, not -1, as we've
746          * detected an ELF file.
747          */
748
749         if ((hdr->e_phoff > PAGE_SIZE) ||
750             (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
751                 /* Only support headers in first page for now */
752                 return (ENOEXEC);
753         }
754         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
755         if (!aligned(phdr, Elf_Addr))
756                 return (ENOEXEC);
757         n = 0;
758         baddr = 0;
759         for (i = 0; i < hdr->e_phnum; i++) {
760                 switch (phdr[i].p_type) {
761                 case PT_LOAD:
762                         if (n == 0)
763                                 baddr = phdr[i].p_vaddr;
764                         n++;
765                         break;
766                 case PT_INTERP:
767                         /* Path to interpreter */
768                         if (phdr[i].p_filesz > MAXPATHLEN ||
769                             phdr[i].p_offset > PAGE_SIZE ||
770                             phdr[i].p_filesz > PAGE_SIZE - phdr[i].p_offset)
771                                 return (ENOEXEC);
772                         interp = imgp->image_header + phdr[i].p_offset;
773                         interp_name_len = phdr[i].p_filesz;
774                         break;
775                 case PT_GNU_STACK:
776                         if (__elfN(nxstack))
777                                 imgp->stack_prot =
778                                     __elfN(trans_prot)(phdr[i].p_flags);
779                         break;
780                 }
781         }
782
783         brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len,
784             &osrel);
785         if (brand_info == NULL) {
786                 uprintf("ELF binary type \"%u\" not known.\n",
787                     hdr->e_ident[EI_OSABI]);
788                 return (ENOEXEC);
789         }
790         if (hdr->e_type == ET_DYN) {
791                 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0)
792                         return (ENOEXEC);
793                 /*
794                  * Honour the base load address from the dso if it is
795                  * non-zero for some reason.
796                  */
797                 if (baddr == 0)
798                         et_dyn_addr = ET_DYN_LOAD_ADDR;
799                 else
800                         et_dyn_addr = 0;
801         } else
802                 et_dyn_addr = 0;
803         sv = brand_info->sysvec;
804         if (interp != NULL && brand_info->interp_newpath != NULL)
805                 newinterp = brand_info->interp_newpath;
806
807         /*
808          * Avoid a possible deadlock if the current address space is destroyed
809          * and that address space maps the locked vnode.  In the common case,
810          * the locked vnode's v_usecount is decremented but remains greater
811          * than zero.  Consequently, the vnode lock is not needed by vrele().
812          * However, in cases where the vnode lock is external, such as nullfs,
813          * v_usecount may become zero.
814          */
815         VOP_UNLOCK(imgp->vp, 0);
816
817         error = exec_new_vmspace(imgp, sv);
818         imgp->proc->p_sysent = sv;
819
820         vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
821         if (error)
822                 return (error);
823
824         vmspace = imgp->proc->p_vmspace;
825
826         for (i = 0; i < hdr->e_phnum; i++) {
827                 switch (phdr[i].p_type) {
828                 case PT_LOAD:   /* Loadable segment */
829                         if (phdr[i].p_memsz == 0)
830                                 break;
831                         prot = __elfN(trans_prot)(phdr[i].p_flags);
832                         if ((error = __elfN(load_section)(vmspace,
833                             imgp->object, phdr[i].p_offset,
834                             (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
835                             phdr[i].p_memsz, phdr[i].p_filesz, prot,
836                             sv->sv_pagesize)) != 0)
837                                 return (error);
838
839                         /*
840                          * If this segment contains the program headers,
841                          * remember their virtual address for the AT_PHDR
842                          * aux entry. Static binaries don't usually include
843                          * a PT_PHDR entry.
844                          */
845                         if (phdr[i].p_offset == 0 &&
846                             hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
847                                 <= phdr[i].p_filesz)
848                                 proghdr = phdr[i].p_vaddr + hdr->e_phoff +
849                                     et_dyn_addr;
850
851                         seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
852                         seg_size = round_page(phdr[i].p_memsz +
853                             phdr[i].p_vaddr + et_dyn_addr - seg_addr);
854
855                         /*
856                          * Make the largest executable segment the official
857                          * text segment and all others data.
858                          *
859                          * Note that obreak() assumes that data_addr + 
860                          * data_size == end of data load area, and the ELF
861                          * file format expects segments to be sorted by
862                          * address.  If multiple data segments exist, the
863                          * last one will be used.
864                          */
865
866                         if (phdr[i].p_flags & PF_X && text_size < seg_size) {
867                                 text_size = seg_size;
868                                 text_addr = seg_addr;
869                         } else {
870                                 data_size = seg_size;
871                                 data_addr = seg_addr;
872                         }
873                         total_size += seg_size;
874                         break;
875                 case PT_PHDR:   /* Program header table info */
876                         proghdr = phdr[i].p_vaddr + et_dyn_addr;
877                         break;
878                 default:
879                         break;
880                 }
881         }
882         
883         if (data_addr == 0 && data_size == 0) {
884                 data_addr = text_addr;
885                 data_size = text_size;
886         }
887
888         entry = (u_long)hdr->e_entry + et_dyn_addr;
889
890         /*
891          * Check limits.  It should be safe to check the
892          * limits after loading the segments since we do
893          * not actually fault in all the segments pages.
894          */
895         PROC_LOCK(imgp->proc);
896         if (data_size > lim_cur(imgp->proc, RLIMIT_DATA) ||
897             text_size > maxtsiz ||
898             total_size > lim_cur(imgp->proc, RLIMIT_VMEM) ||
899             racct_set(imgp->proc, RACCT_DATA, data_size) != 0 ||
900             racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) {
901                 PROC_UNLOCK(imgp->proc);
902                 return (ENOMEM);
903         }
904
905         vmspace->vm_tsize = text_size >> PAGE_SHIFT;
906         vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
907         vmspace->vm_dsize = data_size >> PAGE_SHIFT;
908         vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
909
910         /*
911          * We load the dynamic linker where a userland call
912          * to mmap(0, ...) would put it.  The rationale behind this
913          * calculation is that it leaves room for the heap to grow to
914          * its maximum allowed size.
915          */
916         addr = round_page((vm_offset_t)imgp->proc->p_vmspace->vm_daddr +
917             lim_max(imgp->proc, RLIMIT_DATA));
918         PROC_UNLOCK(imgp->proc);
919
920         imgp->entry_addr = entry;
921
922         if (interp != NULL) {
923                 int have_interp = FALSE;
924                 VOP_UNLOCK(imgp->vp, 0);
925                 if (brand_info->emul_path != NULL &&
926                     brand_info->emul_path[0] != '\0') {
927                         path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
928                         snprintf(path, MAXPATHLEN, "%s%s",
929                             brand_info->emul_path, interp);
930                         error = __elfN(load_file)(imgp->proc, path, &addr,
931                             &imgp->entry_addr, sv->sv_pagesize);
932                         free(path, M_TEMP);
933                         if (error == 0)
934                                 have_interp = TRUE;
935                 }
936                 if (!have_interp && newinterp != NULL) {
937                         error = __elfN(load_file)(imgp->proc, newinterp, &addr,
938                             &imgp->entry_addr, sv->sv_pagesize);
939                         if (error == 0)
940                                 have_interp = TRUE;
941                 }
942                 if (!have_interp) {
943                         error = __elfN(load_file)(imgp->proc, interp, &addr,
944                             &imgp->entry_addr, sv->sv_pagesize);
945                 }
946                 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
947                 if (error != 0) {
948                         uprintf("ELF interpreter %s not found\n", interp);
949                         return (error);
950                 }
951         } else
952                 addr = et_dyn_addr;
953
954         /*
955          * Construct auxargs table (used by the fixup routine)
956          */
957         elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
958         elf_auxargs->execfd = -1;
959         elf_auxargs->phdr = proghdr;
960         elf_auxargs->phent = hdr->e_phentsize;
961         elf_auxargs->phnum = hdr->e_phnum;
962         elf_auxargs->pagesz = PAGE_SIZE;
963         elf_auxargs->base = addr;
964         elf_auxargs->flags = 0;
965         elf_auxargs->entry = entry;
966
967         imgp->auxargs = elf_auxargs;
968         imgp->interpreted = 0;
969         imgp->reloc_base = addr;
970         imgp->proc->p_osrel = osrel;
971
972         return (error);
973 }
974
975 #define suword __CONCAT(suword, __ELF_WORD_SIZE)
976
977 int
978 __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
979 {
980         Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
981         Elf_Addr *base;
982         Elf_Addr *pos;
983
984         base = (Elf_Addr *)*stack_base;
985         pos = base + (imgp->args->argc + imgp->args->envc + 2);
986
987         if (args->execfd != -1)
988                 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
989         AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
990         AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
991         AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
992         AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
993         AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
994         AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
995         AUXARGS_ENTRY(pos, AT_BASE, args->base);
996         if (imgp->execpathp != 0)
997                 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
998         AUXARGS_ENTRY(pos, AT_OSRELDATE, osreldate);
999         if (imgp->canary != 0) {
1000                 AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1001                 AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1002         }
1003         AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1004         if (imgp->pagesizes != 0) {
1005                 AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1006                 AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1007         }
1008         if (imgp->sysent->sv_timekeep_base != 0) {
1009                 AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1010                     imgp->sysent->sv_timekeep_base);
1011         }
1012         AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
1013             != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
1014             imgp->sysent->sv_stackprot);
1015         AUXARGS_ENTRY(pos, AT_NULL, 0);
1016
1017         free(imgp->auxargs, M_TEMP);
1018         imgp->auxargs = NULL;
1019
1020         base--;
1021         suword(base, (long)imgp->args->argc);
1022         *stack_base = (register_t *)base;
1023         return (0);
1024 }
1025
1026 /*
1027  * Code for generating ELF core dumps.
1028  */
1029
1030 typedef void (*segment_callback)(vm_map_entry_t, void *);
1031
1032 /* Closure for cb_put_phdr(). */
1033 struct phdr_closure {
1034         Elf_Phdr *phdr;         /* Program header to fill in */
1035         Elf_Off offset;         /* Offset of segment in core file */
1036 };
1037
1038 /* Closure for cb_size_segment(). */
1039 struct sseg_closure {
1040         int count;              /* Count of writable segments. */
1041         size_t size;            /* Total size of all writable segments. */
1042 };
1043
1044 typedef void (*outfunc_t)(void *, struct sbuf *, size_t *);
1045
1046 struct note_info {
1047         int             type;           /* Note type. */
1048         outfunc_t       outfunc;        /* Output function. */
1049         void            *outarg;        /* Argument for the output function. */
1050         size_t          outsize;        /* Output size. */
1051         TAILQ_ENTRY(note_info) link;    /* Link to the next note info. */
1052 };
1053
1054 TAILQ_HEAD(note_info_list, note_info);
1055
1056 static void cb_put_phdr(vm_map_entry_t, void *);
1057 static void cb_size_segment(vm_map_entry_t, void *);
1058 static void each_writable_segment(struct thread *, segment_callback, void *);
1059 static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *,
1060     int, void *, size_t, struct note_info_list *, size_t, gzFile);
1061 static void __elfN(prepare_notes)(struct thread *, struct note_info_list *,
1062     size_t *);
1063 static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t);
1064 static void __elfN(putnote)(struct note_info *, struct sbuf *);
1065 static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
1066 static int sbuf_drain_core_output(void *, const char *, int);
1067
1068 static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
1069 static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1070 static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *);
1071 static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1072 static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
1073
1074 #ifdef COMPRESS_USER_CORES
1075 extern int compress_user_cores;
1076 extern int compress_user_cores_gzlevel;
1077 #endif
1078
1079 static int
1080 core_output(struct vnode *vp, void *base, size_t len, off_t offset,
1081     struct ucred *active_cred, struct ucred *file_cred,
1082     struct thread *td, char *core_buf, gzFile gzfile) {
1083
1084         int error;
1085         if (gzfile) {
1086 #ifdef COMPRESS_USER_CORES
1087                 error = compress_core(gzfile, base, core_buf, len, td);
1088 #else
1089                 panic("shouldn't be here");
1090 #endif
1091         } else {
1092                 error = vn_rdwr_inchunks(UIO_WRITE, vp, base, len, offset,
1093                     UIO_USERSPACE, IO_UNIT | IO_DIRECT, active_cred, file_cred,
1094                     NULL, td);
1095         }
1096         return (error);
1097 }
1098
1099 /* Coredump output parameters for sbuf drain routine. */
1100 struct sbuf_drain_core_params {
1101         off_t           offset;
1102         struct ucred    *active_cred;
1103         struct ucred    *file_cred;
1104         struct thread   *td;
1105         struct vnode    *vp;
1106 #ifdef COMPRESS_USER_CORES
1107         gzFile          gzfile;
1108 #endif
1109 };
1110
1111 /*
1112  * Drain into a core file.
1113  */
1114 static int
1115 sbuf_drain_core_output(void *arg, const char *data, int len)
1116 {
1117         struct sbuf_drain_core_params *p;
1118         int error;
1119
1120         p = (struct sbuf_drain_core_params *)arg;
1121 #ifdef COMPRESS_USER_CORES
1122         if (p->gzfile != Z_NULL)
1123                 error = compress_core(p->gzfile, NULL, __DECONST(char *, data),
1124                     len, p->td);
1125         else
1126 #endif
1127                 error = vn_rdwr_inchunks(UIO_WRITE, p->vp,
1128                     __DECONST(void *, data), len, p->offset, UIO_SYSSPACE,
1129                     IO_UNIT | IO_DIRECT, p->active_cred, p->file_cred, NULL,
1130                     p->td);
1131         if (error != 0)
1132                 return (-error);
1133         p->offset += len;
1134         return (len);
1135 }
1136
1137 int
1138 __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1139 {
1140         struct ucred *cred = td->td_ucred;
1141         int error = 0;
1142         struct sseg_closure seginfo;
1143         struct note_info_list notelst;
1144         struct note_info *ninfo;
1145         void *hdr;
1146         size_t hdrsize, notesz, coresize;
1147
1148         gzFile gzfile = Z_NULL;
1149         char *core_buf = NULL;
1150 #ifdef COMPRESS_USER_CORES
1151         char gzopen_flags[8];
1152         char *p;
1153         int doing_compress = flags & IMGACT_CORE_COMPRESS;
1154 #endif
1155
1156         hdr = NULL;
1157         TAILQ_INIT(&notelst);
1158
1159 #ifdef COMPRESS_USER_CORES
1160         if (doing_compress) {
1161                 p = gzopen_flags;
1162                 *p++ = 'w';
1163                 if (compress_user_cores_gzlevel >= 0 &&
1164                     compress_user_cores_gzlevel <= 9)
1165                         *p++ = '0' + compress_user_cores_gzlevel;
1166                 *p = 0;
1167                 gzfile = gz_open("", gzopen_flags, vp);
1168                 if (gzfile == Z_NULL) {
1169                         error = EFAULT;
1170                         goto done;
1171                 }
1172                 core_buf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1173                 if (!core_buf) {
1174                         error = ENOMEM;
1175                         goto done;
1176                 }
1177         }
1178 #endif
1179
1180         /* Size the program segments. */
1181         seginfo.count = 0;
1182         seginfo.size = 0;
1183         each_writable_segment(td, cb_size_segment, &seginfo);
1184
1185         /*
1186          * Collect info about the core file header area.
1187          */
1188         hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1189         __elfN(prepare_notes)(td, &notelst, &notesz);
1190         coresize = round_page(hdrsize + notesz) + seginfo.size;
1191
1192 #ifdef RACCT
1193         PROC_LOCK(td->td_proc);
1194         error = racct_add(td->td_proc, RACCT_CORE, coresize);
1195         PROC_UNLOCK(td->td_proc);
1196         if (error != 0) {
1197                 error = EFAULT;
1198                 goto done;
1199         }
1200 #endif
1201         if (coresize >= limit) {
1202                 error = EFAULT;
1203                 goto done;
1204         }
1205
1206         /*
1207          * Allocate memory for building the header, fill it up,
1208          * and write it out following the notes.
1209          */
1210         hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
1211         if (hdr == NULL) {
1212                 error = EINVAL;
1213                 goto done;
1214         }
1215         error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize,
1216             &notelst, notesz, gzfile);
1217
1218         /* Write the contents of all of the writable segments. */
1219         if (error == 0) {
1220                 Elf_Phdr *php;
1221                 off_t offset;
1222                 int i;
1223
1224                 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1225                 offset = round_page(hdrsize + notesz);
1226                 for (i = 0; i < seginfo.count; i++) {
1227                         error = core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr,
1228                             php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile);
1229                         if (error != 0)
1230                                 break;
1231                         offset += php->p_filesz;
1232                         php++;
1233                 }
1234         }
1235         if (error) {
1236                 log(LOG_WARNING,
1237                     "Failed to write core file for process %s (error %d)\n",
1238                     curproc->p_comm, error);
1239         }
1240
1241 done:
1242 #ifdef COMPRESS_USER_CORES
1243         if (core_buf)
1244                 free(core_buf, M_TEMP);
1245         if (gzfile)
1246                 gzclose(gzfile);
1247 #endif
1248         while ((ninfo = TAILQ_FIRST(&notelst)) != NULL) {
1249                 TAILQ_REMOVE(&notelst, ninfo, link);
1250                 free(ninfo, M_TEMP);
1251         }
1252         if (hdr != NULL)
1253                 free(hdr, M_TEMP);
1254
1255         return (error);
1256 }
1257
1258 /*
1259  * A callback for each_writable_segment() to write out the segment's
1260  * program header entry.
1261  */
1262 static void
1263 cb_put_phdr(entry, closure)
1264         vm_map_entry_t entry;
1265         void *closure;
1266 {
1267         struct phdr_closure *phc = (struct phdr_closure *)closure;
1268         Elf_Phdr *phdr = phc->phdr;
1269
1270         phc->offset = round_page(phc->offset);
1271
1272         phdr->p_type = PT_LOAD;
1273         phdr->p_offset = phc->offset;
1274         phdr->p_vaddr = entry->start;
1275         phdr->p_paddr = 0;
1276         phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1277         phdr->p_align = PAGE_SIZE;
1278         phdr->p_flags = __elfN(untrans_prot)(entry->protection);
1279
1280         phc->offset += phdr->p_filesz;
1281         phc->phdr++;
1282 }
1283
1284 /*
1285  * A callback for each_writable_segment() to gather information about
1286  * the number of segments and their total size.
1287  */
1288 static void
1289 cb_size_segment(entry, closure)
1290         vm_map_entry_t entry;
1291         void *closure;
1292 {
1293         struct sseg_closure *ssc = (struct sseg_closure *)closure;
1294
1295         ssc->count++;
1296         ssc->size += entry->end - entry->start;
1297 }
1298
1299 /*
1300  * For each writable segment in the process's memory map, call the given
1301  * function with a pointer to the map entry and some arbitrary
1302  * caller-supplied data.
1303  */
1304 static void
1305 each_writable_segment(td, func, closure)
1306         struct thread *td;
1307         segment_callback func;
1308         void *closure;
1309 {
1310         struct proc *p = td->td_proc;
1311         vm_map_t map = &p->p_vmspace->vm_map;
1312         vm_map_entry_t entry;
1313         vm_object_t backing_object, object;
1314         boolean_t ignore_entry;
1315
1316         vm_map_lock_read(map);
1317         for (entry = map->header.next; entry != &map->header;
1318             entry = entry->next) {
1319                 /*
1320                  * Don't dump inaccessible mappings, deal with legacy
1321                  * coredump mode.
1322                  *
1323                  * Note that read-only segments related to the elf binary
1324                  * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1325                  * need to arbitrarily ignore such segments.
1326                  */
1327                 if (elf_legacy_coredump) {
1328                         if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
1329                                 continue;
1330                 } else {
1331                         if ((entry->protection & VM_PROT_ALL) == 0)
1332                                 continue;
1333                 }
1334
1335                 /*
1336                  * Dont include memory segment in the coredump if
1337                  * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1338                  * madvise(2).  Do not dump submaps (i.e. parts of the
1339                  * kernel map).
1340                  */
1341                 if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
1342                         continue;
1343
1344                 if ((object = entry->object.vm_object) == NULL)
1345                         continue;
1346
1347                 /* Ignore memory-mapped devices and such things. */
1348                 VM_OBJECT_LOCK(object);
1349                 while ((backing_object = object->backing_object) != NULL) {
1350                         VM_OBJECT_LOCK(backing_object);
1351                         VM_OBJECT_UNLOCK(object);
1352                         object = backing_object;
1353                 }
1354                 ignore_entry = object->type != OBJT_DEFAULT &&
1355                     object->type != OBJT_SWAP && object->type != OBJT_VNODE;
1356                 VM_OBJECT_UNLOCK(object);
1357                 if (ignore_entry)
1358                         continue;
1359
1360                 (*func)(entry, closure);
1361         }
1362         vm_map_unlock_read(map);
1363 }
1364
1365 /*
1366  * Write the core file header to the file, including padding up to
1367  * the page boundary.
1368  */
1369 static int
1370 __elfN(corehdr)(struct thread *td, struct vnode *vp, struct ucred *cred,
1371     int numsegs, void *hdr, size_t hdrsize, struct note_info_list *notelst,
1372     size_t notesz, gzFile gzfile)
1373 {
1374         struct sbuf_drain_core_params params;
1375         struct note_info *ninfo;
1376         struct sbuf *sb;
1377         int error;
1378
1379         /* Fill in the header. */
1380         bzero(hdr, hdrsize);
1381         __elfN(puthdr)(td, hdr, hdrsize, numsegs, notesz);
1382
1383         params.offset = 0;
1384         params.active_cred = cred;
1385         params.file_cred = NOCRED;
1386         params.td = td;
1387         params.vp = vp;
1388 #ifdef COMPRESS_USER_CORES
1389         params.gzfile = gzfile;
1390 #endif
1391         sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1392         sbuf_set_drain(sb, sbuf_drain_core_output, &params);
1393         sbuf_start_section(sb, NULL);
1394         sbuf_bcat(sb, hdr, hdrsize);
1395         TAILQ_FOREACH(ninfo, notelst, link)
1396             __elfN(putnote)(ninfo, sb);
1397         /* Align up to a page boundary for the program segments. */
1398         sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1399         error = sbuf_finish(sb);
1400         sbuf_delete(sb);
1401
1402         return (error);
1403 }
1404
1405 static void
1406 __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1407     size_t *sizep)
1408 {
1409         struct proc *p;
1410         struct thread *thr;
1411         size_t size;
1412
1413         p = td->td_proc;
1414         size = 0;
1415
1416         size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p);
1417
1418         /*
1419          * To have the debugger select the right thread (LWP) as the initial
1420          * thread, we dump the state of the thread passed to us in td first.
1421          * This is the thread that causes the core dump and thus likely to
1422          * be the right thread one wants to have selected in the debugger.
1423          */
1424         thr = td;
1425         while (thr != NULL) {
1426                 size += register_note(list, NT_PRSTATUS,
1427                     __elfN(note_prstatus), thr);
1428                 size += register_note(list, NT_FPREGSET,
1429                     __elfN(note_fpregset), thr);
1430                 size += register_note(list, NT_THRMISC,
1431                     __elfN(note_thrmisc), thr);
1432                 size += register_note(list, -1,
1433                     __elfN(note_threadmd), thr);
1434
1435                 thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1436                     TAILQ_NEXT(thr, td_plist);
1437                 if (thr == td)
1438                         thr = TAILQ_NEXT(thr, td_plist);
1439         }
1440
1441         *sizep = size;
1442 }
1443
1444 static void
1445 __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
1446     size_t notesz)
1447 {
1448         Elf_Ehdr *ehdr;
1449         Elf_Phdr *phdr;
1450         struct phdr_closure phc;
1451
1452         ehdr = (Elf_Ehdr *)hdr;
1453         phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
1454
1455         ehdr->e_ident[EI_MAG0] = ELFMAG0;
1456         ehdr->e_ident[EI_MAG1] = ELFMAG1;
1457         ehdr->e_ident[EI_MAG2] = ELFMAG2;
1458         ehdr->e_ident[EI_MAG3] = ELFMAG3;
1459         ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1460         ehdr->e_ident[EI_DATA] = ELF_DATA;
1461         ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1462         ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1463         ehdr->e_ident[EI_ABIVERSION] = 0;
1464         ehdr->e_ident[EI_PAD] = 0;
1465         ehdr->e_type = ET_CORE;
1466 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1467         ehdr->e_machine = ELF_ARCH32;
1468 #else
1469         ehdr->e_machine = ELF_ARCH;
1470 #endif
1471         ehdr->e_version = EV_CURRENT;
1472         ehdr->e_entry = 0;
1473         ehdr->e_phoff = sizeof(Elf_Ehdr);
1474         ehdr->e_flags = 0;
1475         ehdr->e_ehsize = sizeof(Elf_Ehdr);
1476         ehdr->e_phentsize = sizeof(Elf_Phdr);
1477         ehdr->e_phnum = numsegs + 1;
1478         ehdr->e_shentsize = sizeof(Elf_Shdr);
1479         ehdr->e_shnum = 0;
1480         ehdr->e_shstrndx = SHN_UNDEF;
1481
1482         /*
1483          * Fill in the program header entries.
1484          */
1485
1486         /* The note segement. */
1487         phdr->p_type = PT_NOTE;
1488         phdr->p_offset = hdrsize;
1489         phdr->p_vaddr = 0;
1490         phdr->p_paddr = 0;
1491         phdr->p_filesz = notesz;
1492         phdr->p_memsz = 0;
1493         phdr->p_flags = PF_R;
1494         phdr->p_align = sizeof(Elf32_Size);
1495         phdr++;
1496
1497         /* All the writable segments from the program. */
1498         phc.phdr = phdr;
1499         phc.offset = round_page(hdrsize + notesz);
1500         each_writable_segment(td, cb_put_phdr, &phc);
1501 }
1502
1503 static size_t
1504 register_note(struct note_info_list *list, int type, outfunc_t out, void *arg)
1505 {
1506         struct note_info *ninfo;
1507         size_t size, notesize;
1508
1509         size = 0;
1510         out(arg, NULL, &size);
1511         ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
1512         ninfo->type = type;
1513         ninfo->outfunc = out;
1514         ninfo->outarg = arg;
1515         ninfo->outsize = size;
1516         TAILQ_INSERT_TAIL(list, ninfo, link);
1517
1518         if (type == -1)
1519                 return (size);
1520
1521         notesize = sizeof(Elf_Note) +           /* note header */
1522             roundup2(8, sizeof(Elf32_Size)) +   /* note name ("FreeBSD") */
1523             roundup2(size, sizeof(Elf32_Size)); /* note description */
1524
1525         return (notesize);
1526 }
1527
1528 static void
1529 __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb)
1530 {
1531         Elf_Note note;
1532         ssize_t old_len;
1533
1534         if (ninfo->type == -1) {
1535                 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1536                 return;
1537         }
1538
1539         note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
1540         note.n_descsz = ninfo->outsize;
1541         note.n_type = ninfo->type;
1542
1543         sbuf_bcat(sb, &note, sizeof(note));
1544         sbuf_start_section(sb, &old_len);
1545         sbuf_bcat(sb, "FreeBSD", note.n_namesz);
1546         sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
1547         if (note.n_descsz == 0)
1548                 return;
1549         sbuf_start_section(sb, &old_len);
1550         ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1551         sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
1552 }
1553
1554 /*
1555  * Miscellaneous note out functions.
1556  */
1557
1558 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1559 #include <compat/freebsd32/freebsd32.h>
1560
1561 typedef struct prstatus32 elf_prstatus_t;
1562 typedef struct prpsinfo32 elf_prpsinfo_t;
1563 typedef struct fpreg32 elf_prfpregset_t;
1564 typedef struct fpreg32 elf_fpregset_t;
1565 typedef struct reg32 elf_gregset_t;
1566 typedef struct thrmisc32 elf_thrmisc_t;
1567 #else
1568 typedef prstatus_t elf_prstatus_t;
1569 typedef prpsinfo_t elf_prpsinfo_t;
1570 typedef prfpregset_t elf_prfpregset_t;
1571 typedef prfpregset_t elf_fpregset_t;
1572 typedef gregset_t elf_gregset_t;
1573 typedef thrmisc_t elf_thrmisc_t;
1574 #endif
1575
1576 static void
1577 __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
1578 {
1579         struct proc *p;
1580         elf_prpsinfo_t *psinfo;
1581
1582         p = (struct proc *)arg;
1583         if (sb != NULL) {
1584                 KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
1585                 psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
1586                 psinfo->pr_version = PRPSINFO_VERSION;
1587                 psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
1588                 strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
1589                 /*
1590                  * XXX - We don't fill in the command line arguments properly
1591                  * yet.
1592                  */
1593                 strlcpy(psinfo->pr_psargs, p->p_comm,
1594                     sizeof(psinfo->pr_psargs));
1595
1596                 sbuf_bcat(sb, psinfo, sizeof(*psinfo));
1597                 free(psinfo, M_TEMP);
1598         }
1599         *sizep = sizeof(*psinfo);
1600 }
1601
1602 static void
1603 __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
1604 {
1605         struct thread *td;
1606         elf_prstatus_t *status;
1607
1608         td = (struct thread *)arg;
1609         if (sb != NULL) {
1610                 KASSERT(*sizep == sizeof(*status), ("invalid size"));
1611                 status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
1612                 status->pr_version = PRSTATUS_VERSION;
1613                 status->pr_statussz = sizeof(elf_prstatus_t);
1614                 status->pr_gregsetsz = sizeof(elf_gregset_t);
1615                 status->pr_fpregsetsz = sizeof(elf_fpregset_t);
1616                 status->pr_osreldate = osreldate;
1617                 status->pr_cursig = td->td_proc->p_sig;
1618                 status->pr_pid = td->td_tid;
1619 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1620                 fill_regs32(td, &status->pr_reg);
1621 #else
1622                 fill_regs(td, &status->pr_reg);
1623 #endif
1624                 sbuf_bcat(sb, status, sizeof(*status));
1625                 free(status, M_TEMP);
1626         }
1627         *sizep = sizeof(*status);
1628 }
1629
1630 static void
1631 __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
1632 {
1633         struct thread *td;
1634         elf_prfpregset_t *fpregset;
1635
1636         td = (struct thread *)arg;
1637         if (sb != NULL) {
1638                 KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
1639                 fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
1640 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1641                 fill_fpregs32(td, fpregset);
1642 #else
1643                 fill_fpregs(td, fpregset);
1644 #endif
1645                 sbuf_bcat(sb, fpregset, sizeof(*fpregset));
1646                 free(fpregset, M_TEMP);
1647         }
1648         *sizep = sizeof(*fpregset);
1649 }
1650
1651 static void
1652 __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
1653 {
1654         struct thread *td;
1655         elf_thrmisc_t thrmisc;
1656
1657         td = (struct thread *)arg;
1658         if (sb != NULL) {
1659                 KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
1660                 bzero(&thrmisc._pad, sizeof(thrmisc._pad));
1661                 strcpy(thrmisc.pr_tname, td->td_name);
1662                 sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
1663         }
1664         *sizep = sizeof(thrmisc);
1665 }
1666
1667 /*
1668  * Allow for MD specific notes, as well as any MD
1669  * specific preparations for writing MI notes.
1670  */
1671 static void
1672 __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
1673 {
1674         struct thread *td;
1675         void *buf;
1676         size_t size;
1677
1678         td = (struct thread *)arg;
1679         size = *sizep;
1680         buf = NULL;
1681         if (size != 0 && sb != NULL)
1682                 buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
1683         size = 0;
1684         __elfN(dump_thread)(td, buf, &size);
1685         KASSERT(*sizep == size, ("invalid size"));
1686         if (size != 0 && sb != NULL)
1687                 sbuf_bcat(sb, buf, size);
1688         *sizep = size;
1689 }
1690
1691 static boolean_t
1692 __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote,
1693     int32_t *osrel, const Elf_Phdr *pnote)
1694 {
1695         const Elf_Note *note, *note0, *note_end;
1696         const char *note_name;
1697         int i;
1698
1699         if (pnote == NULL || pnote->p_offset > PAGE_SIZE ||
1700             pnote->p_filesz > PAGE_SIZE - pnote->p_offset)
1701                 return (FALSE);
1702
1703         note = note0 = (const Elf_Note *)(imgp->image_header + pnote->p_offset);
1704         note_end = (const Elf_Note *)(imgp->image_header +
1705             pnote->p_offset + pnote->p_filesz);
1706         for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
1707                 if (!aligned(note, Elf32_Addr) || (const char *)note_end -
1708                     (const char *)note < sizeof(Elf_Note))
1709                         return (FALSE);
1710                 if (note->n_namesz != checknote->hdr.n_namesz ||
1711                     note->n_descsz != checknote->hdr.n_descsz ||
1712                     note->n_type != checknote->hdr.n_type)
1713                         goto nextnote;
1714                 note_name = (const char *)(note + 1);
1715                 if (note_name + checknote->hdr.n_namesz >=
1716                     (const char *)note_end || strncmp(checknote->vendor,
1717                     note_name, checknote->hdr.n_namesz) != 0)
1718                         goto nextnote;
1719
1720                 /*
1721                  * Fetch the osreldate for binary
1722                  * from the ELF OSABI-note if necessary.
1723                  */
1724                 if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
1725                     checknote->trans_osrel != NULL)
1726                         return (checknote->trans_osrel(note, osrel));
1727                 return (TRUE);
1728
1729 nextnote:
1730                 note = (const Elf_Note *)((const char *)(note + 1) +
1731                     roundup2(note->n_namesz, sizeof(Elf32_Addr)) +
1732                     roundup2(note->n_descsz, sizeof(Elf32_Addr)));
1733         }
1734
1735         return (FALSE);
1736 }
1737
1738 /*
1739  * Try to find the appropriate ABI-note section for checknote,
1740  * fetch the osreldate for binary from the ELF OSABI-note. Only the
1741  * first page of the image is searched, the same as for headers.
1742  */
1743 static boolean_t
1744 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
1745     int32_t *osrel)
1746 {
1747         const Elf_Phdr *phdr;
1748         const Elf_Ehdr *hdr;
1749         int i;
1750
1751         hdr = (const Elf_Ehdr *)imgp->image_header;
1752         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
1753
1754         for (i = 0; i < hdr->e_phnum; i++) {
1755                 if (phdr[i].p_type == PT_NOTE &&
1756                     __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i]))
1757                         return (TRUE);
1758         }
1759         return (FALSE);
1760
1761 }
1762
1763 /*
1764  * Tell kern_execve.c about it, with a little help from the linker.
1765  */
1766 static struct execsw __elfN(execsw) = {
1767         __CONCAT(exec_, __elfN(imgact)),
1768         __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
1769 };
1770 EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
1771
1772 #ifdef COMPRESS_USER_CORES
1773 /*
1774  * Compress and write out a core segment for a user process.
1775  *
1776  * 'inbuf' is the starting address of a VM segment in the process' address
1777  * space that is to be compressed and written out to the core file.  'dest_buf'
1778  * is a buffer in the kernel's address space.  The segment is copied from 
1779  * 'inbuf' to 'dest_buf' first before being processed by the compression
1780  * routine gzwrite().  This copying is necessary because the content of the VM
1781  * segment may change between the compression pass and the crc-computation pass
1782  * in gzwrite().  This is because realtime threads may preempt the UNIX kernel.
1783  *
1784  * If inbuf is NULL it is assumed that data is already copied to 'dest_buf'.
1785  */
1786 static int
1787 compress_core (gzFile file, char *inbuf, char *dest_buf, unsigned int len,
1788     struct thread *td)
1789 {
1790         int len_compressed;
1791         int error = 0;
1792         unsigned int chunk_len;
1793
1794         while (len) {
1795                 if (inbuf != NULL) {
1796                         chunk_len = (len > CORE_BUF_SIZE) ? CORE_BUF_SIZE : len;
1797                         copyin(inbuf, dest_buf, chunk_len);
1798                         inbuf += chunk_len;
1799                 } else {
1800                         chunk_len = len;
1801                 }
1802                 len_compressed = gzwrite(file, dest_buf, chunk_len);
1803
1804                 EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed);
1805
1806                 if ((unsigned int)len_compressed != chunk_len) {
1807                         log(LOG_WARNING,
1808                             "compress_core: length mismatch (0x%x returned, "
1809                             "0x%x expected)\n", len_compressed, chunk_len);
1810                         EVENTHANDLER_INVOKE(app_coredump_error, td,
1811                             "compress_core: length mismatch %x -> %x",
1812                             chunk_len, len_compressed);
1813                         error = EFAULT;
1814                         break;
1815                 }
1816                 len -= chunk_len;
1817                 maybe_yield();
1818         }
1819
1820         return (error);
1821 }
1822 #endif /* COMPRESS_USER_CORES */
1823
1824 static vm_prot_t
1825 __elfN(trans_prot)(Elf_Word flags)
1826 {
1827         vm_prot_t prot;
1828
1829         prot = 0;
1830         if (flags & PF_X)
1831                 prot |= VM_PROT_EXECUTE;
1832         if (flags & PF_W)
1833                 prot |= VM_PROT_WRITE;
1834         if (flags & PF_R)
1835                 prot |= VM_PROT_READ;
1836 #if __ELF_WORD_SIZE == 32
1837 #if defined(__amd64__) || defined(__ia64__)
1838         if (i386_read_exec && (flags & PF_R))
1839                 prot |= VM_PROT_EXECUTE;
1840 #endif
1841 #endif
1842         return (prot);
1843 }
1844
1845 static Elf_Word
1846 __elfN(untrans_prot)(vm_prot_t prot)
1847 {
1848         Elf_Word flags;
1849
1850         flags = 0;
1851         if (prot & VM_PROT_EXECUTE)
1852                 flags |= PF_X;
1853         if (prot & VM_PROT_READ)
1854                 flags |= PF_R;
1855         if (prot & VM_PROT_WRITE)
1856                 flags |= PF_W;
1857         return (flags);
1858 }