1e1743d02SSøren Schmidt /*- 221a3ee0eSDavid E. O'Brien * Copyright (c) 2000 David O'Brien 39a14aa01SUlrich Spörlein * Copyright (c) 1995-1996 Søren Schmidt 4e1743d02SSøren Schmidt * Copyright (c) 1996 Peter Wemm 5e1743d02SSøren Schmidt * All rights reserved. 6e1743d02SSøren Schmidt * 7e1743d02SSøren Schmidt * Redistribution and use in source and binary forms, with or without 8e1743d02SSøren Schmidt * modification, are permitted provided that the following conditions 9e1743d02SSøren Schmidt * are met: 10e1743d02SSøren Schmidt * 1. Redistributions of source code must retain the above copyright 11e1743d02SSøren Schmidt * notice, this list of conditions and the following disclaimer 12e1743d02SSøren Schmidt * in this position and unchanged. 13e1743d02SSøren Schmidt * 2. Redistributions in binary form must reproduce the above copyright 14e1743d02SSøren Schmidt * notice, this list of conditions and the following disclaimer in the 15e1743d02SSøren Schmidt * documentation and/or other materials provided with the distribution. 16e1743d02SSøren Schmidt * 3. The name of the author may not be used to endorse or promote products 1721dc7d4fSJens Schweikhardt * derived from this software without specific prior written permission 18e1743d02SSøren Schmidt * 19e1743d02SSøren Schmidt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20e1743d02SSøren Schmidt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21e1743d02SSøren Schmidt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22e1743d02SSøren Schmidt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23e1743d02SSøren Schmidt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24e1743d02SSøren Schmidt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25e1743d02SSøren Schmidt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26e1743d02SSøren Schmidt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27e1743d02SSøren Schmidt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28e1743d02SSøren Schmidt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29e1743d02SSøren Schmidt */ 30e1743d02SSøren Schmidt 31677b542eSDavid E. O'Brien #include <sys/cdefs.h> 32677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 33677b542eSDavid E. O'Brien 3412bc222eSJonathan Anderson #include "opt_capsicum.h" 3562919d78SPeter Wemm #include "opt_compat.h" 36aa14e9b7SMark Johnston #include "opt_gzio.h" 3762919d78SPeter Wemm 38e1743d02SSøren Schmidt #include <sys/param.h> 394a144410SRobert Watson #include <sys/capsicum.h> 40e1743d02SSøren Schmidt #include <sys/exec.h> 418c64af4fSJohn Polstra #include <sys/fcntl.h> 42aa14e9b7SMark Johnston #include <sys/gzio.h> 43e1743d02SSøren Schmidt #include <sys/imgact.h> 44e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 45b96bd95bSIan Lepore #include <sys/jail.h> 46e1743d02SSøren Schmidt #include <sys/kernel.h> 47f34fa851SJohn Baldwin #include <sys/lock.h> 48e1743d02SSøren Schmidt #include <sys/malloc.h> 4968ff2a43SChristian S.J. Peron #include <sys/mount.h> 508c64af4fSJohn Polstra #include <sys/mman.h> 51a794e791SBruce Evans #include <sys/namei.h> 528c64af4fSJohn Polstra #include <sys/pioctl.h> 53a794e791SBruce Evans #include <sys/proc.h> 548c64af4fSJohn Polstra #include <sys/procfs.h> 551ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 568c64af4fSJohn Polstra #include <sys/resourcevar.h> 5789f6b863SAttilio Rao #include <sys/rwlock.h> 58bd390213SMikolaj Golub #include <sys/sbuf.h> 59da61b9a6SAlan Cox #include <sys/sf_buf.h> 60ee235befSKonstantin Belousov #include <sys/smp.h> 6136240ea5SDoug Rabson #include <sys/systm.h> 62e1743d02SSøren Schmidt #include <sys/signalvar.h> 638c64af4fSJohn Polstra #include <sys/stat.h> 641005a129SJohn Baldwin #include <sys/sx.h> 658c64af4fSJohn Polstra #include <sys/syscall.h> 66e1743d02SSøren Schmidt #include <sys/sysctl.h> 678c64af4fSJohn Polstra #include <sys/sysent.h> 68a794e791SBruce Evans #include <sys/vnode.h> 69e7228204SAlfred Perlstein #include <sys/syslog.h> 70e7228204SAlfred Perlstein #include <sys/eventhandler.h> 71f1fca82eSMikolaj Golub #include <sys/user.h> 72e7228204SAlfred Perlstein 73e1743d02SSøren Schmidt #include <vm/vm.h> 74e1743d02SSøren Schmidt #include <vm/vm_kern.h> 75e1743d02SSøren Schmidt #include <vm/vm_param.h> 76e1743d02SSøren Schmidt #include <vm/pmap.h> 77e1743d02SSøren Schmidt #include <vm/vm_map.h> 780ff27d31SJohn Polstra #include <vm/vm_object.h> 79e1743d02SSøren Schmidt #include <vm/vm_extern.h> 80e1743d02SSøren Schmidt 8152c24af7SPeter Wemm #include <machine/elf.h> 82e1743d02SSøren Schmidt #include <machine/md_var.h> 83e1743d02SSøren Schmidt 841b8388cdSMikolaj Golub #define ELF_NOTE_ROUNDSIZE 4 85c815a20cSDavid E. O'Brien #define OLD_EI_BRAND 8 86c815a20cSDavid E. O'Brien 873ebc1248SPeter Wemm static int __elfN(check_header)(const Elf_Ehdr *hdr); 8832c01de2SDmitry Chagin static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp, 89d1ae5c83SKonstantin Belousov const char *interp, int interp_name_len, int32_t *osrel); 903ebc1248SPeter Wemm static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 913ebc1248SPeter Wemm u_long *entry, size_t pagesize); 920bbee4cdSKonstantin Belousov static int __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset, 93292177e6SAlan Cox caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot, 94292177e6SAlan Cox size_t pagesize); 953ebc1248SPeter Wemm static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp); 9689ffc202SBjoern A. Zeeb static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note, 9789ffc202SBjoern A. Zeeb int32_t *osrel); 9889ffc202SBjoern A. Zeeb static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel); 9932c01de2SDmitry Chagin static boolean_t __elfN(check_note)(struct image_params *imgp, 10032c01de2SDmitry Chagin Elf_Brandnote *checknote, int32_t *osrel); 101ed167eaaSKonstantin Belousov static vm_prot_t __elfN(trans_prot)(Elf_Word); 102ed167eaaSKonstantin Belousov static Elf_Word __elfN(untrans_prot)(vm_prot_t); 103e1743d02SSøren Schmidt 104a360a43dSJake Burkholder SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0, 105a360a43dSJake Burkholder ""); 106a360a43dSJake Burkholder 107bd390213SMikolaj Golub #define CORE_BUF_SIZE (16 * 1024) 108e7228204SAlfred Perlstein 109e548a1d4SJake Burkholder int __elfN(fallback_brand) = -1; 110e548a1d4SJake Burkholder SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 111af3b2549SHans Petter Selasky fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0, 112a360a43dSJake Burkholder __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort"); 113a360a43dSJake Burkholder 114551d79e1SMarcel Moolenaar static int elf_legacy_coredump = 0; 115a360a43dSJake Burkholder SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW, 1161cbb879dSEd Maste &elf_legacy_coredump, 0, 1171cbb879dSEd Maste "include all and only RW pages in core dumps"); 118e1743d02SSøren Schmidt 11962c625fdSKonstantin Belousov int __elfN(nxstack) = 1204d22d07aSKonstantin Belousov #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */ || \ 1214d22d07aSKonstantin Belousov (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__) 12262c625fdSKonstantin Belousov 1; 12362c625fdSKonstantin Belousov #else 12462c625fdSKonstantin Belousov 0; 12562c625fdSKonstantin Belousov #endif 126291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 127291c06a1SKonstantin Belousov nxstack, CTLFLAG_RW, &__elfN(nxstack), 0, 128291c06a1SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack"); 129291c06a1SKonstantin Belousov 130126b36a2SKonstantin Belousov #if __ELF_WORD_SIZE == 32 131e7d939bdSMarcel Moolenaar #if defined(__amd64__) 132126b36a2SKonstantin Belousov int i386_read_exec = 0; 133126b36a2SKonstantin Belousov SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0, 134126b36a2SKonstantin Belousov "enable execution from readable segments"); 135126b36a2SKonstantin Belousov #endif 136126b36a2SKonstantin Belousov #endif 137126b36a2SKonstantin Belousov 1383ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS]; 139e1743d02SSøren Schmidt 140d9c9c81cSPedro F. Giffuni #define trunc_page_ps(va, ps) rounddown2(va, ps) 141d9c9c81cSPedro F. Giffuni #define round_page_ps(va, ps) roundup2(va, ps) 14293d1c728SKonstantin Belousov #define aligned(a, t) (trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a)) 14393d1c728SKonstantin Belousov 14432c01de2SDmitry Chagin static const char FREEBSD_ABI_VENDOR[] = "FreeBSD"; 14532c01de2SDmitry Chagin 14632c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = { 14732c01de2SDmitry Chagin .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR), 14832c01de2SDmitry Chagin .hdr.n_descsz = sizeof(int32_t), 1494c22b468SEd Maste .hdr.n_type = NT_FREEBSD_ABI_TAG, 15032c01de2SDmitry Chagin .vendor = FREEBSD_ABI_VENDOR, 15189ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 15289ffc202SBjoern A. Zeeb .trans_osrel = __elfN(freebsd_trans_osrel) 15332c01de2SDmitry Chagin }; 15432c01de2SDmitry Chagin 15589ffc202SBjoern A. Zeeb static boolean_t 15689ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel) 15789ffc202SBjoern A. Zeeb { 15889ffc202SBjoern A. Zeeb uintptr_t p; 15989ffc202SBjoern A. Zeeb 16089ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 1611b8388cdSMikolaj Golub p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 16289ffc202SBjoern A. Zeeb *osrel = *(const int32_t *)(p); 16389ffc202SBjoern A. Zeeb 16489ffc202SBjoern A. Zeeb return (TRUE); 16589ffc202SBjoern A. Zeeb } 16689ffc202SBjoern A. Zeeb 16789ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU"; 16889ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3; 16989ffc202SBjoern A. Zeeb 17089ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = { 17189ffc202SBjoern A. Zeeb .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 17289ffc202SBjoern A. Zeeb .hdr.n_descsz = 16, /* XXX at least 16 */ 17389ffc202SBjoern A. Zeeb .hdr.n_type = 1, 17489ffc202SBjoern A. Zeeb .vendor = GNU_ABI_VENDOR, 17589ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 17689ffc202SBjoern A. Zeeb .trans_osrel = kfreebsd_trans_osrel 17789ffc202SBjoern A. Zeeb }; 17889ffc202SBjoern A. Zeeb 17989ffc202SBjoern A. Zeeb static boolean_t 18089ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel) 18189ffc202SBjoern A. Zeeb { 18289ffc202SBjoern A. Zeeb const Elf32_Word *desc; 18389ffc202SBjoern A. Zeeb uintptr_t p; 18489ffc202SBjoern A. Zeeb 18589ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 1861b8388cdSMikolaj Golub p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE); 18789ffc202SBjoern A. Zeeb 18889ffc202SBjoern A. Zeeb desc = (const Elf32_Word *)p; 18989ffc202SBjoern A. Zeeb if (desc[0] != GNU_KFREEBSD_ABI_DESC) 19089ffc202SBjoern A. Zeeb return (FALSE); 19189ffc202SBjoern A. Zeeb 19289ffc202SBjoern A. Zeeb /* 19389ffc202SBjoern A. Zeeb * Debian GNU/kFreeBSD embed the earliest compatible kernel version 19489ffc202SBjoern A. Zeeb * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way. 19589ffc202SBjoern A. Zeeb */ 19689ffc202SBjoern A. Zeeb *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3]; 19789ffc202SBjoern A. Zeeb 19889ffc202SBjoern A. Zeeb return (TRUE); 19989ffc202SBjoern A. Zeeb } 20089ffc202SBjoern A. Zeeb 201e1743d02SSøren Schmidt int 2023ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry) 203e1743d02SSøren Schmidt { 204e1743d02SSøren Schmidt int i; 205e1743d02SSøren Schmidt 2063ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 207ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == NULL) { 208ea5a2b2eSSøren Schmidt elf_brand_list[i] = entry; 209e1743d02SSøren Schmidt break; 210e1743d02SSøren Schmidt } 211e1743d02SSøren Schmidt } 212925c8b5bSBjoern A. Zeeb if (i == MAX_BRANDS) { 213925c8b5bSBjoern A. Zeeb printf("WARNING: %s: could not insert brandinfo entry: %p\n", 214925c8b5bSBjoern A. Zeeb __func__, entry); 215a7cddfedSJake Burkholder return (-1); 216925c8b5bSBjoern A. Zeeb } 217a7cddfedSJake Burkholder return (0); 218e1743d02SSøren Schmidt } 219e1743d02SSøren Schmidt 220e1743d02SSøren Schmidt int 2213ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry) 222e1743d02SSøren Schmidt { 223e1743d02SSøren Schmidt int i; 224e1743d02SSøren Schmidt 2253ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 226ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == entry) { 227ea5a2b2eSSøren Schmidt elf_brand_list[i] = NULL; 228e1743d02SSøren Schmidt break; 229e1743d02SSøren Schmidt } 230e1743d02SSøren Schmidt } 231ea5a2b2eSSøren Schmidt if (i == MAX_BRANDS) 232a7cddfedSJake Burkholder return (-1); 233a7cddfedSJake Burkholder return (0); 234e1743d02SSøren Schmidt } 235e1743d02SSøren Schmidt 236096977faSMark Newton int 2373ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry) 238096977faSMark Newton { 239096977faSMark Newton struct proc *p; 240553629ebSJake Burkholder int rval = FALSE; 241096977faSMark Newton 2421005a129SJohn Baldwin sx_slock(&allproc_lock); 2434f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 244553629ebSJake Burkholder if (p->p_sysent == entry->sysvec) { 245553629ebSJake Burkholder rval = TRUE; 246553629ebSJake Burkholder break; 247096977faSMark Newton } 248553629ebSJake Burkholder } 2491005a129SJohn Baldwin sx_sunlock(&allproc_lock); 250096977faSMark Newton 251553629ebSJake Burkholder return (rval); 252096977faSMark Newton } 253096977faSMark Newton 2545fe3ed62SJake Burkholder static Elf_Brandinfo * 25532c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, 256d1ae5c83SKonstantin Belousov int interp_name_len, int32_t *osrel) 2575fe3ed62SJake Burkholder { 25832c01de2SDmitry Chagin const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 259af582aaeSKonstantin Belousov Elf_Brandinfo *bi, *bi_m; 26032c01de2SDmitry Chagin boolean_t ret; 2615fe3ed62SJake Burkholder int i; 2625fe3ed62SJake Burkholder 2635fe3ed62SJake Burkholder /* 26432c01de2SDmitry Chagin * We support four types of branding -- (1) the ELF EI_OSABI field 2655fe3ed62SJake Burkholder * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string 26632c01de2SDmitry Chagin * branding w/in the ELF header, (3) path of the `interp_path' 26732c01de2SDmitry Chagin * field, and (4) the ".note.ABI-tag" ELF section. 2685fe3ed62SJake Burkholder */ 2695fe3ed62SJake Burkholder 27032c01de2SDmitry Chagin /* Look for an ".note.ABI-tag" ELF section */ 271af582aaeSKonstantin Belousov bi_m = NULL; 27232c01de2SDmitry Chagin for (i = 0; i < MAX_BRANDS; i++) { 27332c01de2SDmitry Chagin bi = elf_brand_list[i]; 274ecc2fda8SBjoern A. Zeeb if (bi == NULL) 275ecc2fda8SBjoern A. Zeeb continue; 276*1438fe3cSKonstantin Belousov if (interp != NULL && 277*1438fe3cSKonstantin Belousov (bi->flags & BI_BRAND_NOTE_ONLY_STATIC) != 0) 278*1438fe3cSKonstantin Belousov continue; 279ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && (bi->flags & 280ecc2fda8SBjoern A. Zeeb (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) { 28132c01de2SDmitry Chagin ret = __elfN(check_note)(imgp, bi->brand_note, osrel); 282f19d421aSNathan Whitehorn /* Give brand a chance to veto check_note's guess */ 283f19d421aSNathan Whitehorn if (ret && bi->header_supported) 284f19d421aSNathan Whitehorn ret = bi->header_supported(imgp); 285af582aaeSKonstantin Belousov /* 286af582aaeSKonstantin Belousov * If note checker claimed the binary, but the 287af582aaeSKonstantin Belousov * interpreter path in the image does not 288af582aaeSKonstantin Belousov * match default one for the brand, try to 289af582aaeSKonstantin Belousov * search for other brands with the same 290af582aaeSKonstantin Belousov * interpreter. Either there is better brand 291af582aaeSKonstantin Belousov * with the right interpreter, or, failing 292af582aaeSKonstantin Belousov * this, we return first brand which accepted 293af582aaeSKonstantin Belousov * our note and, optionally, header. 294af582aaeSKonstantin Belousov */ 295af582aaeSKonstantin Belousov if (ret && bi_m == NULL && (strlen(bi->interp_path) + 296af582aaeSKonstantin Belousov 1 != interp_name_len || strncmp(interp, 297af582aaeSKonstantin Belousov bi->interp_path, interp_name_len) != 0)) { 298af582aaeSKonstantin Belousov bi_m = bi; 299af582aaeSKonstantin Belousov ret = 0; 300af582aaeSKonstantin Belousov } 30132c01de2SDmitry Chagin if (ret) 30232c01de2SDmitry Chagin return (bi); 30332c01de2SDmitry Chagin } 30432c01de2SDmitry Chagin } 305af582aaeSKonstantin Belousov if (bi_m != NULL) 306af582aaeSKonstantin Belousov return (bi_m); 30732c01de2SDmitry Chagin 3085fe3ed62SJake Burkholder /* If the executable has a brand, search for it in the brand list. */ 3095fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 3105fe3ed62SJake Burkholder bi = elf_brand_list[i]; 311*1438fe3cSKonstantin Belousov if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 || 312*1438fe3cSKonstantin Belousov (interp != NULL && (bi->flags & 313*1438fe3cSKonstantin Belousov BI_BRAND_NOTE_ONLY_STATIC) != 0)) 314ecc2fda8SBjoern A. Zeeb continue; 315ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 3165fe3ed62SJake Burkholder (hdr->e_ident[EI_OSABI] == bi->brand || 3173d560b4bSKonstantin Belousov strcmp((const char *)&hdr->e_ident[OLD_EI_BRAND], 3183d560b4bSKonstantin Belousov bi->compat_3_brand) == 0)) { 319686d2f31SNathan Whitehorn /* Looks good, but give brand a chance to veto */ 32015a9aedfSKonstantin Belousov if (!bi->header_supported || 32115a9aedfSKonstantin Belousov bi->header_supported(imgp)) { 32215a9aedfSKonstantin Belousov /* 32315a9aedfSKonstantin Belousov * Again, prefer strictly matching 32415a9aedfSKonstantin Belousov * interpreter path. 32515a9aedfSKonstantin Belousov */ 3267aab7a80SKonstantin Belousov if (interp_name_len == 0 && 3277aab7a80SKonstantin Belousov bi->interp_path == NULL) 3287aab7a80SKonstantin Belousov return (bi); 3297aab7a80SKonstantin Belousov if (bi->interp_path != NULL && 3307aab7a80SKonstantin Belousov strlen(bi->interp_path) + 1 == 33115a9aedfSKonstantin Belousov interp_name_len && strncmp(interp, 33215a9aedfSKonstantin Belousov bi->interp_path, interp_name_len) == 0) 3335fe3ed62SJake Burkholder return (bi); 33415a9aedfSKonstantin Belousov if (bi_m == NULL) 33515a9aedfSKonstantin Belousov bi_m = bi; 3365fe3ed62SJake Burkholder } 337686d2f31SNathan Whitehorn } 33815a9aedfSKonstantin Belousov } 33915a9aedfSKonstantin Belousov if (bi_m != NULL) 34015a9aedfSKonstantin Belousov return (bi_m); 3415fe3ed62SJake Burkholder 342817dc004SWarner Losh /* No known brand, see if the header is recognized by any brand */ 343817dc004SWarner Losh for (i = 0; i < MAX_BRANDS; i++) { 344817dc004SWarner Losh bi = elf_brand_list[i]; 345817dc004SWarner Losh if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY || 346817dc004SWarner Losh bi->header_supported == NULL) 347817dc004SWarner Losh continue; 348817dc004SWarner Losh if (hdr->e_machine == bi->machine) { 349817dc004SWarner Losh ret = bi->header_supported(imgp); 350817dc004SWarner Losh if (ret) 351817dc004SWarner Losh return (bi); 352817dc004SWarner Losh } 353817dc004SWarner Losh } 354817dc004SWarner Losh 3555fe3ed62SJake Burkholder /* Lacking a known brand, search for a recognized interpreter. */ 3565fe3ed62SJake Burkholder if (interp != NULL) { 3575fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 3585fe3ed62SJake Burkholder bi = elf_brand_list[i]; 359*1438fe3cSKonstantin Belousov if (bi == NULL || (bi->flags & (BI_BRAND_NOTE_MANDATORY | 360*1438fe3cSKonstantin Belousov BI_BRAND_NOTE_ONLY_STATIC)) != 0) 361ecc2fda8SBjoern A. Zeeb continue; 362ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 363d1ae5c83SKonstantin Belousov /* ELF image p_filesz includes terminating zero */ 364d1ae5c83SKonstantin Belousov strlen(bi->interp_path) + 1 == interp_name_len && 365d1ae5c83SKonstantin Belousov strncmp(interp, bi->interp_path, interp_name_len) 366d1ae5c83SKonstantin Belousov == 0) 3675fe3ed62SJake Burkholder return (bi); 3685fe3ed62SJake Burkholder } 3695fe3ed62SJake Burkholder } 3705fe3ed62SJake Burkholder 3715fe3ed62SJake Burkholder /* Lacking a recognized interpreter, try the default brand */ 3725fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 3735fe3ed62SJake Burkholder bi = elf_brand_list[i]; 374*1438fe3cSKonstantin Belousov if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 || 375*1438fe3cSKonstantin Belousov (interp != NULL && (bi->flags & 376*1438fe3cSKonstantin Belousov BI_BRAND_NOTE_ONLY_STATIC) != 0)) 377ecc2fda8SBjoern A. Zeeb continue; 378ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 379e548a1d4SJake Burkholder __elfN(fallback_brand) == bi->brand) 3805fe3ed62SJake Burkholder return (bi); 3815fe3ed62SJake Burkholder } 3825fe3ed62SJake Burkholder return (NULL); 3835fe3ed62SJake Burkholder } 3845fe3ed62SJake Burkholder 385e1743d02SSøren Schmidt static int 3863ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr) 387e1743d02SSøren Schmidt { 388d0ca7c29SPeter Wemm Elf_Brandinfo *bi; 3893ebc1248SPeter Wemm int i; 3903ebc1248SPeter Wemm 39152c24af7SPeter Wemm if (!IS_ELF(*hdr) || 39252c24af7SPeter Wemm hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 39352c24af7SPeter Wemm hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 3943dc19c46SJacques Vidrine hdr->e_ident[EI_VERSION] != EV_CURRENT || 3953dc19c46SJacques Vidrine hdr->e_phentsize != sizeof(Elf_Phdr) || 3963dc19c46SJacques Vidrine hdr->e_version != ELF_TARG_VER) 397a7cddfedSJake Burkholder return (ENOEXEC); 398e1743d02SSøren Schmidt 3993ebc1248SPeter Wemm /* 4003ebc1248SPeter Wemm * Make sure we have at least one brand for this machine. 4013ebc1248SPeter Wemm */ 4023ebc1248SPeter Wemm 4033ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 404d0ca7c29SPeter Wemm bi = elf_brand_list[i]; 405d0ca7c29SPeter Wemm if (bi != NULL && bi->machine == hdr->e_machine) 4063ebc1248SPeter Wemm break; 4073ebc1248SPeter Wemm } 4083ebc1248SPeter Wemm if (i == MAX_BRANDS) 409a7cddfedSJake Burkholder return (ENOEXEC); 410e1743d02SSøren Schmidt 411a7cddfedSJake Burkholder return (0); 412e1743d02SSøren Schmidt } 413e1743d02SSøren Schmidt 414e1743d02SSøren Schmidt static int 4153ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 416ff6f03c7SAlan Cox vm_offset_t start, vm_offset_t end, vm_prot_t prot) 4173ebc1248SPeter Wemm { 418da61b9a6SAlan Cox struct sf_buf *sf; 419da61b9a6SAlan Cox int error; 4203ebc1248SPeter Wemm vm_offset_t off; 4213ebc1248SPeter Wemm 4223ebc1248SPeter Wemm /* 4233ebc1248SPeter Wemm * Create the page if it doesn't exist yet. Ignore errors. 4243ebc1248SPeter Wemm */ 425aaadc41fSKonstantin Belousov vm_map_fixed(map, NULL, 0, trunc_page(start), round_page(end) - 426aaadc41fSKonstantin Belousov trunc_page(start), VM_PROT_ALL, VM_PROT_ALL, MAP_CHECK_EXCL); 4273ebc1248SPeter Wemm 4283ebc1248SPeter Wemm /* 4293ebc1248SPeter Wemm * Find the page from the underlying object. 4303ebc1248SPeter Wemm */ 43128e8da65SAlan Cox if (object != NULL) { 432da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 433da61b9a6SAlan Cox if (sf == NULL) 434da61b9a6SAlan Cox return (KERN_FAILURE); 4353ebc1248SPeter Wemm off = offset - trunc_page(offset); 436da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start, 437ca0387efSJake Burkholder end - start); 438be996836SAttilio Rao vm_imgact_unmap_page(sf); 439fe0a8a39SKonstantin Belousov if (error != 0) 440a7cddfedSJake Burkholder return (KERN_FAILURE); 4413ebc1248SPeter Wemm } 4423ebc1248SPeter Wemm 443a7cddfedSJake Burkholder return (KERN_SUCCESS); 4443ebc1248SPeter Wemm } 4453ebc1248SPeter Wemm 4463ebc1248SPeter Wemm static int 447e3d8f8feSKonstantin Belousov __elfN(map_insert)(struct image_params *imgp, vm_map_t map, vm_object_t object, 448e3d8f8feSKonstantin Belousov vm_ooffset_t offset, vm_offset_t start, vm_offset_t end, vm_prot_t prot, 449e3d8f8feSKonstantin Belousov int cow) 4503ebc1248SPeter Wemm { 451da61b9a6SAlan Cox struct sf_buf *sf; 452da61b9a6SAlan Cox vm_offset_t off; 453a063facbSMarcel Moolenaar vm_size_t sz; 454e3d8f8feSKonstantin Belousov int error, locked, rv; 4553ebc1248SPeter Wemm 4563ebc1248SPeter Wemm if (start != trunc_page(start)) { 45781f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset, start, 458ff6f03c7SAlan Cox round_page(start), prot); 45928e8da65SAlan Cox if (rv != KERN_SUCCESS) 460a7cddfedSJake Burkholder return (rv); 4613ebc1248SPeter Wemm offset += round_page(start) - start; 4623ebc1248SPeter Wemm start = round_page(start); 4633ebc1248SPeter Wemm } 4643ebc1248SPeter Wemm if (end != round_page(end)) { 46581f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset + 466ff6f03c7SAlan Cox trunc_page(end) - start, trunc_page(end), end, prot); 46728e8da65SAlan Cox if (rv != KERN_SUCCESS) 468a7cddfedSJake Burkholder return (rv); 4693ebc1248SPeter Wemm end = trunc_page(end); 4703ebc1248SPeter Wemm } 471e383e820SAlan Cox if (start >= end) 472e383e820SAlan Cox return (KERN_SUCCESS); 473e383e820SAlan Cox if ((offset & PAGE_MASK) != 0) { 4743ebc1248SPeter Wemm /* 475e383e820SAlan Cox * The mapping is not page aligned. This means that we have 476e383e820SAlan Cox * to copy the data. 4773ebc1248SPeter Wemm */ 478aaadc41fSKonstantin Belousov rv = vm_map_fixed(map, NULL, 0, start, end - start, 479aaadc41fSKonstantin Belousov prot | VM_PROT_WRITE, VM_PROT_ALL, MAP_CHECK_EXCL); 4805420f76bSKonstantin Belousov if (rv != KERN_SUCCESS) 481a7cddfedSJake Burkholder return (rv); 482da61b9a6SAlan Cox if (object == NULL) 483da61b9a6SAlan Cox return (KERN_SUCCESS); 484da61b9a6SAlan Cox for (; start < end; start += sz) { 485da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 486da61b9a6SAlan Cox if (sf == NULL) 487da61b9a6SAlan Cox return (KERN_FAILURE); 4883ebc1248SPeter Wemm off = offset - trunc_page(offset); 4893ebc1248SPeter Wemm sz = end - start; 490da61b9a6SAlan Cox if (sz > PAGE_SIZE - off) 491da61b9a6SAlan Cox sz = PAGE_SIZE - off; 492da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, 4933ebc1248SPeter Wemm (caddr_t)start, sz); 494be996836SAttilio Rao vm_imgact_unmap_page(sf); 4955420f76bSKonstantin Belousov if (error != 0) 496a7cddfedSJake Burkholder return (KERN_FAILURE); 497da61b9a6SAlan Cox offset += sz; 4983ebc1248SPeter Wemm } 4993ebc1248SPeter Wemm } else { 500e5e6093bSAlan Cox vm_object_reference(object); 501e383e820SAlan Cox rv = vm_map_fixed(map, object, offset, start, end - start, 502e383e820SAlan Cox prot, VM_PROT_ALL, cow | MAP_CHECK_EXCL); 503e3d8f8feSKonstantin Belousov if (rv != KERN_SUCCESS) { 504e3d8f8feSKonstantin Belousov locked = VOP_ISLOCKED(imgp->vp); 505e3d8f8feSKonstantin Belousov VOP_UNLOCK(imgp->vp, 0); 506e5e6093bSAlan Cox vm_object_deallocate(object); 507e3d8f8feSKonstantin Belousov vn_lock(imgp->vp, locked | LK_RETRY); 508a7cddfedSJake Burkholder return (rv); 5093ebc1248SPeter Wemm } 5103ebc1248SPeter Wemm } 511e383e820SAlan Cox return (KERN_SUCCESS); 512e383e820SAlan Cox } 5133ebc1248SPeter Wemm 5143ebc1248SPeter Wemm static int 5150bbee4cdSKonstantin Belousov __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset, 5163ebc1248SPeter Wemm caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot, 5173ebc1248SPeter Wemm size_t pagesize) 518e1743d02SSøren Schmidt { 519da61b9a6SAlan Cox struct sf_buf *sf; 520e1743d02SSøren Schmidt size_t map_len; 521292177e6SAlan Cox vm_map_t map; 522292177e6SAlan Cox vm_object_t object; 523973d67c4SKonstantin Belousov vm_offset_t off, map_addr; 524fa7dd9c5SMatthew Dillon int error, rv, cow; 525e1743d02SSøren Schmidt size_t copy_len; 5260bbee4cdSKonstantin Belousov vm_ooffset_t file_addr; 52752c24af7SPeter Wemm 52825ead034SBrian Feldman /* 52925ead034SBrian Feldman * It's necessary to fail if the filsz + offset taken from the 53025ead034SBrian Feldman * header is greater than the actual file pager object's size. 53125ead034SBrian Feldman * If we were to allow this, then the vm_map_find() below would 53225ead034SBrian Feldman * walk right off the end of the file object and into the ether. 53325ead034SBrian Feldman * 53425ead034SBrian Feldman * While I'm here, might as well check for something else that 53525ead034SBrian Feldman * is invalid: filsz cannot be greater than memsz. 53625ead034SBrian Feldman */ 5379bcf2f2dSKonstantin Belousov if ((filsz != 0 && (off_t)filsz + offset > imgp->attr->va_size) || 5389bcf2f2dSKonstantin Belousov filsz > memsz) { 53925ead034SBrian Feldman uprintf("elf_load_section: truncated ELF file\n"); 54025ead034SBrian Feldman return (ENOEXEC); 54125ead034SBrian Feldman } 54225ead034SBrian Feldman 543292177e6SAlan Cox object = imgp->object; 544292177e6SAlan Cox map = &imgp->proc->p_vmspace->vm_map; 5453ebc1248SPeter Wemm map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize); 5463ebc1248SPeter Wemm file_addr = trunc_page_ps(offset, pagesize); 547e1743d02SSøren Schmidt 548e1743d02SSøren Schmidt /* 54952c24af7SPeter Wemm * We have two choices. We can either clear the data in the last page 55052c24af7SPeter Wemm * of an oversized mapping, or we can start the anon mapping a page 55152c24af7SPeter Wemm * early and copy the initialized data into that first page. We 55228e8da65SAlan Cox * choose the second. 55352c24af7SPeter Wemm */ 5549bcf2f2dSKonstantin Belousov if (filsz == 0) 5559bcf2f2dSKonstantin Belousov map_len = 0; 5569bcf2f2dSKonstantin Belousov else if (memsz > filsz) 5573ebc1248SPeter Wemm map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr; 55852c24af7SPeter Wemm else 5593ebc1248SPeter Wemm map_len = round_page_ps(offset + filsz, pagesize) - file_addr; 56052c24af7SPeter Wemm 56152c24af7SPeter Wemm if (map_len != 0) { 562fa7dd9c5SMatthew Dillon /* cow flags: don't dump readonly sections in core */ 563fa7dd9c5SMatthew Dillon cow = MAP_COPY_ON_WRITE | MAP_PREFAULT | 564fa7dd9c5SMatthew Dillon (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP); 565fa7dd9c5SMatthew Dillon 566e3d8f8feSKonstantin Belousov rv = __elfN(map_insert)(imgp, map, 56752c24af7SPeter Wemm object, 56852c24af7SPeter Wemm file_addr, /* file offset */ 56952c24af7SPeter Wemm map_addr, /* virtual start */ 57052c24af7SPeter Wemm map_addr + map_len,/* virtual end */ 57152c24af7SPeter Wemm prot, 572fa7dd9c5SMatthew Dillon cow); 573e5e6093bSAlan Cox if (rv != KERN_SUCCESS) 574a7cddfedSJake Burkholder return (EINVAL); 57552c24af7SPeter Wemm 57652c24af7SPeter Wemm /* we can stop now if we've covered it all */ 577973d67c4SKonstantin Belousov if (memsz == filsz) 578a7cddfedSJake Burkholder return (0); 57952c24af7SPeter Wemm } 58052c24af7SPeter Wemm 58152c24af7SPeter Wemm 58252c24af7SPeter Wemm /* 58352c24af7SPeter Wemm * We have to get the remaining bit of the file into the first part 58452c24af7SPeter Wemm * of the oversized map segment. This is normally because the .data 58552c24af7SPeter Wemm * segment in the file is extended to provide bss. It's a neat idea 58652c24af7SPeter Wemm * to try and save a page, but it's a pain in the behind to implement. 587e1743d02SSøren Schmidt */ 5889bcf2f2dSKonstantin Belousov copy_len = filsz == 0 ? 0 : (offset + filsz) - trunc_page_ps(offset + 5899bcf2f2dSKonstantin Belousov filsz, pagesize); 5903ebc1248SPeter Wemm map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize); 591ca0387efSJake Burkholder map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) - 592ca0387efSJake Burkholder map_addr; 593e1743d02SSøren Schmidt 59452c24af7SPeter Wemm /* This had damn well better be true! */ 5958191d577SPeter Wemm if (map_len != 0) { 596e3d8f8feSKonstantin Belousov rv = __elfN(map_insert)(imgp, map, NULL, 0, map_addr, 597c547cbb4SAlan Cox map_addr + map_len, prot, 0); 598973d67c4SKonstantin Belousov if (rv != KERN_SUCCESS) 599a7cddfedSJake Burkholder return (EINVAL); 6008191d577SPeter Wemm } 601e1743d02SSøren Schmidt 60252c24af7SPeter Wemm if (copy_len != 0) { 603da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset + filsz); 604da61b9a6SAlan Cox if (sf == NULL) 605da61b9a6SAlan Cox return (EIO); 606e1743d02SSøren Schmidt 60752c24af7SPeter Wemm /* send the page fragment to user space */ 60881f223caSJake Burkholder off = trunc_page_ps(offset + filsz, pagesize) - 60981f223caSJake Burkholder trunc_page(offset + filsz); 610da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, 611da61b9a6SAlan Cox (caddr_t)map_addr, copy_len); 612be996836SAttilio Rao vm_imgact_unmap_page(sf); 613973d67c4SKonstantin Belousov if (error != 0) 61452c24af7SPeter Wemm return (error); 61552c24af7SPeter Wemm } 616e1743d02SSøren Schmidt 617e1743d02SSøren Schmidt /* 618c547cbb4SAlan Cox * Remove write access to the page if it was only granted by map_insert 619c547cbb4SAlan Cox * to allow copyout. 620e1743d02SSøren Schmidt */ 621c547cbb4SAlan Cox if ((prot & VM_PROT_WRITE) == 0) 622292177e6SAlan Cox vm_map_protect(map, trunc_page(map_addr), round_page(map_addr + 623292177e6SAlan Cox map_len), prot, FALSE); 6248191d577SPeter Wemm 625ff6f03c7SAlan Cox return (0); 626e1743d02SSøren Schmidt } 627e1743d02SSøren Schmidt 628c33fe779SJohn Polstra /* 629c33fe779SJohn Polstra * Load the file "file" into memory. It may be either a shared object 630c33fe779SJohn Polstra * or an executable. 631c33fe779SJohn Polstra * 632c33fe779SJohn Polstra * The "addr" reference parameter is in/out. On entry, it specifies 633c33fe779SJohn Polstra * the address where a shared object should be loaded. If the file is 634c33fe779SJohn Polstra * an executable, this value is ignored. On exit, "addr" specifies 635c33fe779SJohn Polstra * where the file was actually loaded. 636c33fe779SJohn Polstra * 637c33fe779SJohn Polstra * The "entry" reference parameter is out only. On exit, it specifies 638c33fe779SJohn Polstra * the entry point for the loaded file. 639c33fe779SJohn Polstra */ 640e1743d02SSøren Schmidt static int 6413ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 6423ebc1248SPeter Wemm u_long *entry, size_t pagesize) 643e1743d02SSøren Schmidt { 644911c2be0SMark Peek struct { 645911c2be0SMark Peek struct nameidata nd; 646911c2be0SMark Peek struct vattr attr; 647911c2be0SMark Peek struct image_params image_params; 648911c2be0SMark Peek } *tempdata; 649d254af07SMatthew Dillon const Elf_Ehdr *hdr = NULL; 650d254af07SMatthew Dillon const Elf_Phdr *phdr = NULL; 651911c2be0SMark Peek struct nameidata *nd; 652911c2be0SMark Peek struct vattr *attr; 653911c2be0SMark Peek struct image_params *imgp; 65452c24af7SPeter Wemm vm_prot_t prot; 655c33fe779SJohn Polstra u_long rbase; 656c33fe779SJohn Polstra u_long base_addr = 0; 6575050aa86SKonstantin Belousov int error, i, numsegs; 658e1743d02SSøren Schmidt 65912bc222eSJonathan Anderson #ifdef CAPABILITY_MODE 66012bc222eSJonathan Anderson /* 66112bc222eSJonathan Anderson * XXXJA: This check can go away once we are sufficiently confident 66212bc222eSJonathan Anderson * that the checks in namei() are correct. 66312bc222eSJonathan Anderson */ 66412bc222eSJonathan Anderson if (IN_CAPABILITY_MODE(curthread)) 66512bc222eSJonathan Anderson return (ECAPMODE); 66612bc222eSJonathan Anderson #endif 66712bc222eSJonathan Anderson 668a163d034SWarner Losh tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK); 669911c2be0SMark Peek nd = &tempdata->nd; 670911c2be0SMark Peek attr = &tempdata->attr; 671911c2be0SMark Peek imgp = &tempdata->image_params; 672911c2be0SMark Peek 673c8a79999SPeter Wemm /* 674c8a79999SPeter Wemm * Initialize part of the common data 675c8a79999SPeter Wemm */ 676c8a79999SPeter Wemm imgp->proc = p; 677911c2be0SMark Peek imgp->attr = attr; 678c8a79999SPeter Wemm imgp->firstpage = NULL; 67959c8bc40SAlan Cox imgp->image_header = NULL; 6800b2ed1aeSJeff Roberson imgp->object = NULL; 6816d7bdc8dSRobert Watson imgp->execlabel = NULL; 682c8a79999SPeter Wemm 6835050aa86SKonstantin Belousov NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread); 684911c2be0SMark Peek if ((error = namei(nd)) != 0) { 685911c2be0SMark Peek nd->ni_vp = NULL; 686e1743d02SSøren Schmidt goto fail; 687e1743d02SSøren Schmidt } 688911c2be0SMark Peek NDFREE(nd, NDF_ONLY_PNBUF); 689911c2be0SMark Peek imgp->vp = nd->ni_vp; 690c8a79999SPeter Wemm 691e1743d02SSøren Schmidt /* 692e1743d02SSøren Schmidt * Check permissions, modes, uid, etc on the file, and "open" it. 693e1743d02SSøren Schmidt */ 694c8a79999SPeter Wemm error = exec_check_permissions(imgp); 695373d1a3fSAlan Cox if (error) 696c8a79999SPeter Wemm goto fail; 697e1743d02SSøren Schmidt 698c8a79999SPeter Wemm error = exec_map_first_page(imgp); 699373d1a3fSAlan Cox if (error) 700373d1a3fSAlan Cox goto fail; 701373d1a3fSAlan Cox 70225ead034SBrian Feldman /* 70325ead034SBrian Feldman * Also make certain that the interpreter stays the same, so set 704e6e370a7SJeff Roberson * its VV_TEXT flag, too. 70525ead034SBrian Feldman */ 706877d24acSKonstantin Belousov VOP_SET_TEXT(nd->ni_vp); 707e6e370a7SJeff Roberson 7088516dd18SPoul-Henning Kamp imgp->object = nd->ni_vp->v_object; 709e1743d02SSøren Schmidt 710d254af07SMatthew Dillon hdr = (const Elf_Ehdr *)imgp->image_header; 7113ebc1248SPeter Wemm if ((error = __elfN(check_header)(hdr)) != 0) 712e1743d02SSøren Schmidt goto fail; 713c33fe779SJohn Polstra if (hdr->e_type == ET_DYN) 714c33fe779SJohn Polstra rbase = *addr; 715c33fe779SJohn Polstra else if (hdr->e_type == ET_EXEC) 716c33fe779SJohn Polstra rbase = 0; 717c33fe779SJohn Polstra else { 718c33fe779SJohn Polstra error = ENOEXEC; 719c33fe779SJohn Polstra goto fail; 720c33fe779SJohn Polstra } 721e1743d02SSøren Schmidt 722c8a79999SPeter Wemm /* Only support headers that fit within first page for now */ 72352c24af7SPeter Wemm if ((hdr->e_phoff > PAGE_SIZE) || 724d19d5bf4STijl Coosemans (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) { 725c8a79999SPeter Wemm error = ENOEXEC; 726e1743d02SSøren Schmidt goto fail; 727c8a79999SPeter Wemm } 728c8a79999SPeter Wemm 729d254af07SMatthew Dillon phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 73093d1c728SKonstantin Belousov if (!aligned(phdr, Elf_Addr)) { 73193d1c728SKonstantin Belousov error = ENOEXEC; 73293d1c728SKonstantin Belousov goto fail; 73393d1c728SKonstantin Belousov } 734e1743d02SSøren Schmidt 735c33fe779SJohn Polstra for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) { 7365b33842aSKonstantin Belousov if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) { 7375b33842aSKonstantin Belousov /* Loadable segment */ 738ed167eaaSKonstantin Belousov prot = __elfN(trans_prot)(phdr[i].p_flags); 739292177e6SAlan Cox error = __elfN(load_section)(imgp, phdr[i].p_offset, 74081f223caSJake Burkholder (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase, 741292177e6SAlan Cox phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize); 742292177e6SAlan Cox if (error != 0) 743e1743d02SSøren Schmidt goto fail; 744e1743d02SSøren Schmidt /* 745c33fe779SJohn Polstra * Establish the base address if this is the 746c33fe779SJohn Polstra * first segment. 747e1743d02SSøren Schmidt */ 748c33fe779SJohn Polstra if (numsegs == 0) 749ca0387efSJake Burkholder base_addr = trunc_page(phdr[i].p_vaddr + 750ca0387efSJake Burkholder rbase); 751c33fe779SJohn Polstra numsegs++; 752e1743d02SSøren Schmidt } 753e1743d02SSøren Schmidt } 754c33fe779SJohn Polstra *addr = base_addr; 755c33fe779SJohn Polstra *entry = (unsigned long)hdr->e_entry + rbase; 756e1743d02SSøren Schmidt 757e1743d02SSøren Schmidt fail: 758c8a79999SPeter Wemm if (imgp->firstpage) 759c8a79999SPeter Wemm exec_unmap_first_page(imgp); 7600b2ed1aeSJeff Roberson 761911c2be0SMark Peek if (nd->ni_vp) 762373d1a3fSAlan Cox vput(nd->ni_vp); 763911c2be0SMark Peek 764911c2be0SMark Peek free(tempdata, M_TEMP); 765e1743d02SSøren Schmidt 766a7cddfedSJake Burkholder return (error); 767e1743d02SSøren Schmidt } 768e1743d02SSøren Schmidt 769303b270bSEivind Eklund static int 7703ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) 771e1743d02SSøren Schmidt { 7726c775eb6SKonstantin Belousov struct thread *td; 7736c775eb6SKonstantin Belousov const Elf_Ehdr *hdr; 77432c01de2SDmitry Chagin const Elf_Phdr *phdr; 775e5e6093bSAlan Cox Elf_Auxargs *elf_auxargs; 7765856e12eSJohn Dyson struct vmspace *vmspace; 7776c775eb6SKonstantin Belousov const char *err_str, *newinterp; 7786c775eb6SKonstantin Belousov char *interp, *interp_buf, *path; 779d1dbc694SJohn Polstra Elf_Brandinfo *brand_info; 7805fe3ed62SJake Burkholder struct sysentvec *sv; 7816c775eb6SKonstantin Belousov vm_prot_t prot; 7826c775eb6SKonstantin Belousov u_long text_size, data_size, total_size, text_addr, data_addr; 7836c775eb6SKonstantin Belousov u_long seg_size, seg_addr, addr, baddr, et_dyn_addr, entry, proghdr; 7846c775eb6SKonstantin Belousov int32_t osrel; 7856c775eb6SKonstantin Belousov int error, i, n, interp_name_len, have_interp; 7866c775eb6SKonstantin Belousov 7876c775eb6SKonstantin Belousov hdr = (const Elf_Ehdr *)imgp->image_header; 788e1743d02SSøren Schmidt 789e1743d02SSøren Schmidt /* 790e1743d02SSøren Schmidt * Do we have a valid ELF header ? 791900b28f9SMaxim Sobolev * 792900b28f9SMaxim Sobolev * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later 793900b28f9SMaxim Sobolev * if particular brand doesn't support it. 794e1743d02SSøren Schmidt */ 795900b28f9SMaxim Sobolev if (__elfN(check_header)(hdr) != 0 || 796900b28f9SMaxim Sobolev (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)) 797a7cddfedSJake Burkholder return (-1); 798e1743d02SSøren Schmidt 799e1743d02SSøren Schmidt /* 800e1743d02SSøren Schmidt * From here on down, we return an errno, not -1, as we've 801e1743d02SSøren Schmidt * detected an ELF file. 802e1743d02SSøren Schmidt */ 803e1743d02SSøren Schmidt 804e1743d02SSøren Schmidt if ((hdr->e_phoff > PAGE_SIZE) || 805d19d5bf4STijl Coosemans (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) { 806c8a79999SPeter Wemm /* Only support headers in first page for now */ 8076b16d664SEd Maste uprintf("Program headers not in the first page\n"); 808a7cddfedSJake Burkholder return (ENOEXEC); 809e1743d02SSøren Schmidt } 81052c24af7SPeter Wemm phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 8116b16d664SEd Maste if (!aligned(phdr, Elf_Addr)) { 8126b16d664SEd Maste uprintf("Unaligned program headers\n"); 81393d1c728SKonstantin Belousov return (ENOEXEC); 8146b16d664SEd Maste } 8156c775eb6SKonstantin Belousov 8166c775eb6SKonstantin Belousov n = error = 0; 8177564c4adSKonstantin Belousov baddr = 0; 8186c775eb6SKonstantin Belousov osrel = 0; 8196c775eb6SKonstantin Belousov text_size = data_size = total_size = text_addr = data_addr = 0; 8206c775eb6SKonstantin Belousov entry = proghdr = 0; 8216c775eb6SKonstantin Belousov interp_name_len = 0; 8226c775eb6SKonstantin Belousov err_str = newinterp = NULL; 8236c775eb6SKonstantin Belousov interp = interp_buf = NULL; 8246c775eb6SKonstantin Belousov td = curthread; 8256c775eb6SKonstantin Belousov 8265fe3ed62SJake Burkholder for (i = 0; i < hdr->e_phnum; i++) { 827291c06a1SKonstantin Belousov switch (phdr[i].p_type) { 828291c06a1SKonstantin Belousov case PT_LOAD: 8297564c4adSKonstantin Belousov if (n == 0) 8307564c4adSKonstantin Belousov baddr = phdr[i].p_vaddr; 8317564c4adSKonstantin Belousov n++; 832291c06a1SKonstantin Belousov break; 833291c06a1SKonstantin Belousov case PT_INTERP: 834e5e6093bSAlan Cox /* Path to interpreter */ 8356c775eb6SKonstantin Belousov if (phdr[i].p_filesz > MAXPATHLEN) { 8366b16d664SEd Maste uprintf("Invalid PT_INTERP\n"); 8376c775eb6SKonstantin Belousov error = ENOEXEC; 8386c775eb6SKonstantin Belousov goto ret; 8396b16d664SEd Maste } 840d3ee0a15SJonathan T. Looney if (interp != NULL) { 841d3ee0a15SJonathan T. Looney uprintf("Multiple PT_INTERP headers\n"); 842d3ee0a15SJonathan T. Looney error = ENOEXEC; 843d3ee0a15SJonathan T. Looney goto ret; 844d3ee0a15SJonathan T. Looney } 845d1ae5c83SKonstantin Belousov interp_name_len = phdr[i].p_filesz; 8466c775eb6SKonstantin Belousov if (phdr[i].p_offset > PAGE_SIZE || 8476c775eb6SKonstantin Belousov interp_name_len > PAGE_SIZE - phdr[i].p_offset) { 8486c775eb6SKonstantin Belousov VOP_UNLOCK(imgp->vp, 0); 8496c775eb6SKonstantin Belousov interp_buf = malloc(interp_name_len + 1, M_TEMP, 8506c775eb6SKonstantin Belousov M_WAITOK); 8516c775eb6SKonstantin Belousov vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 8526c775eb6SKonstantin Belousov error = vn_rdwr(UIO_READ, imgp->vp, interp_buf, 8536c775eb6SKonstantin Belousov interp_name_len, phdr[i].p_offset, 8546c775eb6SKonstantin Belousov UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, 8556c775eb6SKonstantin Belousov NOCRED, NULL, td); 8566c775eb6SKonstantin Belousov if (error != 0) { 8576c775eb6SKonstantin Belousov uprintf("i/o error PT_INTERP\n"); 8586c775eb6SKonstantin Belousov goto ret; 8596c775eb6SKonstantin Belousov } 8606c775eb6SKonstantin Belousov interp_buf[interp_name_len] = '\0'; 8616c775eb6SKonstantin Belousov interp = interp_buf; 8626c775eb6SKonstantin Belousov } else { 8636c775eb6SKonstantin Belousov interp = __DECONST(char *, imgp->image_header) + 8646c775eb6SKonstantin Belousov phdr[i].p_offset; 8656c775eb6SKonstantin Belousov } 866291c06a1SKonstantin Belousov break; 867291c06a1SKonstantin Belousov case PT_GNU_STACK: 868291c06a1SKonstantin Belousov if (__elfN(nxstack)) 869291c06a1SKonstantin Belousov imgp->stack_prot = 870291c06a1SKonstantin Belousov __elfN(trans_prot)(phdr[i].p_flags); 871316b3843SKonstantin Belousov imgp->stack_sz = phdr[i].p_memsz; 872291c06a1SKonstantin Belousov break; 8733ebc1248SPeter Wemm } 8743ebc1248SPeter Wemm } 8753ebc1248SPeter Wemm 876d1ae5c83SKonstantin Belousov brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len, 877d1ae5c83SKonstantin Belousov &osrel); 8785fe3ed62SJake Burkholder if (brand_info == NULL) { 8795fe3ed62SJake Burkholder uprintf("ELF binary type \"%u\" not known.\n", 8805fe3ed62SJake Burkholder hdr->e_ident[EI_OSABI]); 8816c775eb6SKonstantin Belousov error = ENOEXEC; 8826c775eb6SKonstantin Belousov goto ret; 8833ebc1248SPeter Wemm } 88477ebe276SEd Maste et_dyn_addr = 0; 885ab02d85fSKonstantin Belousov if (hdr->e_type == ET_DYN) { 8866b16d664SEd Maste if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) { 8876b16d664SEd Maste uprintf("Cannot execute shared object\n"); 8886c775eb6SKonstantin Belousov error = ENOEXEC; 8896c775eb6SKonstantin Belousov goto ret; 8906b16d664SEd Maste } 8917564c4adSKonstantin Belousov /* 8927564c4adSKonstantin Belousov * Honour the base load address from the dso if it is 8937564c4adSKonstantin Belousov * non-zero for some reason. 8947564c4adSKonstantin Belousov */ 8957564c4adSKonstantin Belousov if (baddr == 0) 896ab02d85fSKonstantin Belousov et_dyn_addr = ET_DYN_LOAD_ADDR; 89777ebe276SEd Maste } 8985fe3ed62SJake Burkholder sv = brand_info->sysvec; 8999b68618dSPeter Wemm if (interp != NULL && brand_info->interp_newpath != NULL) 9004113f8d7SPeter Wemm newinterp = brand_info->interp_newpath; 9013ebc1248SPeter Wemm 90260bb3943SAlan Cox /* 90360bb3943SAlan Cox * Avoid a possible deadlock if the current address space is destroyed 90460bb3943SAlan Cox * and that address space maps the locked vnode. In the common case, 90560bb3943SAlan Cox * the locked vnode's v_usecount is decremented but remains greater 90660bb3943SAlan Cox * than zero. Consequently, the vnode lock is not needed by vrele(). 90760bb3943SAlan Cox * However, in cases where the vnode lock is external, such as nullfs, 90860bb3943SAlan Cox * v_usecount may become zero. 9091dfab802SAlan Cox * 9101dfab802SAlan Cox * The VV_TEXT flag prevents modifications to the executable while 9111dfab802SAlan Cox * the vnode is unlocked. 91260bb3943SAlan Cox */ 91322db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 91460bb3943SAlan Cox 91589b57fcfSKonstantin Belousov error = exec_new_vmspace(imgp, sv); 91619059a13SJohn Baldwin imgp->proc->p_sysent = sv; 917e1743d02SSøren Schmidt 918cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 9196c775eb6SKonstantin Belousov if (error != 0) 9206c775eb6SKonstantin Belousov goto ret; 92160bb3943SAlan Cox 922e1743d02SSøren Schmidt for (i = 0; i < hdr->e_phnum; i++) { 923e1743d02SSøren Schmidt switch (phdr[i].p_type) { 924e1743d02SSøren Schmidt case PT_LOAD: /* Loadable segment */ 9255b33842aSKonstantin Belousov if (phdr[i].p_memsz == 0) 9265b33842aSKonstantin Belousov break; 927ed167eaaSKonstantin Belousov prot = __elfN(trans_prot)(phdr[i].p_flags); 928292177e6SAlan Cox error = __elfN(load_section)(imgp, phdr[i].p_offset, 929ab02d85fSKonstantin Belousov (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr, 93081f223caSJake Burkholder phdr[i].p_memsz, phdr[i].p_filesz, prot, 931292177e6SAlan Cox sv->sv_pagesize); 932292177e6SAlan Cox if (error != 0) 9336c775eb6SKonstantin Belousov goto ret; 934e1743d02SSøren Schmidt 935cfaf7e60SDoug Rabson /* 936cfaf7e60SDoug Rabson * If this segment contains the program headers, 937cfaf7e60SDoug Rabson * remember their virtual address for the AT_PHDR 938cfaf7e60SDoug Rabson * aux entry. Static binaries don't usually include 939cfaf7e60SDoug Rabson * a PT_PHDR entry. 940cfaf7e60SDoug Rabson */ 941cfaf7e60SDoug Rabson if (phdr[i].p_offset == 0 && 942cfaf7e60SDoug Rabson hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize 943cfaf7e60SDoug Rabson <= phdr[i].p_filesz) 944ab02d85fSKonstantin Belousov proghdr = phdr[i].p_vaddr + hdr->e_phoff + 945ab02d85fSKonstantin Belousov et_dyn_addr; 946cfaf7e60SDoug Rabson 947ab02d85fSKonstantin Belousov seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr); 948cac45152SMatthew Dillon seg_size = round_page(phdr[i].p_memsz + 949ab02d85fSKonstantin Belousov phdr[i].p_vaddr + et_dyn_addr - seg_addr); 950cac45152SMatthew Dillon 951e1743d02SSøren Schmidt /* 952920acedbSNathan Whitehorn * Make the largest executable segment the official 953920acedbSNathan Whitehorn * text segment and all others data. 95421c2d047SMatthew Dillon * 95521c2d047SMatthew Dillon * Note that obreak() assumes that data_addr + 95621c2d047SMatthew Dillon * data_size == end of data load area, and the ELF 95721c2d047SMatthew Dillon * file format expects segments to be sorted by 95821c2d047SMatthew Dillon * address. If multiple data segments exist, the 95921c2d047SMatthew Dillon * last one will be used. 960e1743d02SSøren Schmidt */ 961920acedbSNathan Whitehorn 962920acedbSNathan Whitehorn if (phdr[i].p_flags & PF_X && text_size < seg_size) { 9639782ecbaSPeter Wemm text_size = seg_size; 9649782ecbaSPeter Wemm text_addr = seg_addr; 9659782ecbaSPeter Wemm } else { 96621c2d047SMatthew Dillon data_size = seg_size; 967cac45152SMatthew Dillon data_addr = seg_addr; 968cac45152SMatthew Dillon } 96921c2d047SMatthew Dillon total_size += seg_size; 97096725dd0SAlexander Kabaev break; 97196725dd0SAlexander Kabaev case PT_PHDR: /* Program header table info */ 972ab02d85fSKonstantin Belousov proghdr = phdr[i].p_vaddr + et_dyn_addr; 97396725dd0SAlexander Kabaev break; 97496725dd0SAlexander Kabaev default: 97596725dd0SAlexander Kabaev break; 97696725dd0SAlexander Kabaev } 97796725dd0SAlexander Kabaev } 97896725dd0SAlexander Kabaev 97996725dd0SAlexander Kabaev if (data_addr == 0 && data_size == 0) { 98096725dd0SAlexander Kabaev data_addr = text_addr; 98196725dd0SAlexander Kabaev data_size = text_size; 98296725dd0SAlexander Kabaev } 983cac45152SMatthew Dillon 984920acedbSNathan Whitehorn entry = (u_long)hdr->e_entry + et_dyn_addr; 985920acedbSNathan Whitehorn 986cac45152SMatthew Dillon /* 987cac45152SMatthew Dillon * Check limits. It should be safe to check the 98896725dd0SAlexander Kabaev * limits after loading the segments since we do 98996725dd0SAlexander Kabaev * not actually fault in all the segments pages. 990cac45152SMatthew Dillon */ 99191d5354aSJohn Baldwin PROC_LOCK(imgp->proc); 992f6f6d240SMateusz Guzik if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA)) 9936b16d664SEd Maste err_str = "Data segment size exceeds process limit"; 9946b16d664SEd Maste else if (text_size > maxtsiz) 9956b16d664SEd Maste err_str = "Text segment size exceeds system limit"; 996f6f6d240SMateusz Guzik else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM)) 9976b16d664SEd Maste err_str = "Total segment size exceeds process limit"; 9986b16d664SEd Maste else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0) 9996b16d664SEd Maste err_str = "Data segment size exceeds resource limit"; 10006b16d664SEd Maste else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) 10016b16d664SEd Maste err_str = "Total segment size exceeds resource limit"; 10026b16d664SEd Maste if (err_str != NULL) { 100391d5354aSJohn Baldwin PROC_UNLOCK(imgp->proc); 10046b16d664SEd Maste uprintf("%s\n", err_str); 10056c775eb6SKonstantin Belousov error = ENOMEM; 10066c775eb6SKonstantin Belousov goto ret; 1007cac45152SMatthew Dillon } 1008e1743d02SSøren Schmidt 1009292177e6SAlan Cox vmspace = imgp->proc->p_vmspace; 1010e1743d02SSøren Schmidt vmspace->vm_tsize = text_size >> PAGE_SHIFT; 10117cd99438SBruce Evans vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr; 1012e1743d02SSøren Schmidt vmspace->vm_dsize = data_size >> PAGE_SHIFT; 10137cd99438SBruce Evans vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr; 1014e1743d02SSøren Schmidt 1015c460ac3aSPeter Wemm /* 1016c460ac3aSPeter Wemm * We load the dynamic linker where a userland call 1017c460ac3aSPeter Wemm * to mmap(0, ...) would put it. The rationale behind this 1018c460ac3aSPeter Wemm * calculation is that it leaves room for the heap to grow to 1019c460ac3aSPeter Wemm * its maximum allowed size. 1020c460ac3aSPeter Wemm */ 10216c775eb6SKonstantin Belousov addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(td, 1022292177e6SAlan Cox RLIMIT_DATA)); 102391d5354aSJohn Baldwin PROC_UNLOCK(imgp->proc); 1024e1743d02SSøren Schmidt 1025ea5a2b2eSSøren Schmidt imgp->entry_addr = entry; 1026ea5a2b2eSSøren Schmidt 102760bb3943SAlan Cox if (interp != NULL) { 10286c775eb6SKonstantin Belousov have_interp = FALSE; 102922db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 103060bb3943SAlan Cox if (brand_info->emul_path != NULL && 10319b68618dSPeter Wemm brand_info->emul_path[0] != '\0') { 1032a163d034SWarner Losh path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 103360bb3943SAlan Cox snprintf(path, MAXPATHLEN, "%s%s", 103460bb3943SAlan Cox brand_info->emul_path, interp); 10359b68618dSPeter Wemm error = __elfN(load_file)(imgp->proc, path, &addr, 10369b68618dSPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 1037911c2be0SMark Peek free(path, M_TEMP); 10389b68618dSPeter Wemm if (error == 0) 10394113f8d7SPeter Wemm have_interp = TRUE; 10409b68618dSPeter Wemm } 104118995077SKonstantin Belousov if (!have_interp && newinterp != NULL && 104218995077SKonstantin Belousov (brand_info->interp_path == NULL || 104318995077SKonstantin Belousov strcmp(interp, brand_info->interp_path) == 0)) { 10444113f8d7SPeter Wemm error = __elfN(load_file)(imgp->proc, newinterp, &addr, 10454113f8d7SPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 1046387ad998SKonstantin Belousov if (error == 0) 10474113f8d7SPeter Wemm have_interp = TRUE; 10484113f8d7SPeter Wemm } 10494113f8d7SPeter Wemm if (!have_interp) { 10509b68618dSPeter Wemm error = __elfN(load_file)(imgp->proc, interp, &addr, 10519b68618dSPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 105260bb3943SAlan Cox } 1053cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 10549b68618dSPeter Wemm if (error != 0) { 1055d943fa35SKonstantin Belousov uprintf("ELF interpreter %s not found, error %d\n", 1056d943fa35SKonstantin Belousov interp, error); 10576c775eb6SKonstantin Belousov goto ret; 1058e1743d02SSøren Schmidt } 105995c807cfSRobert Watson } else 10607564c4adSKonstantin Belousov addr = et_dyn_addr; 1061ea5a2b2eSSøren Schmidt 1062e1743d02SSøren Schmidt /* 1063e1743d02SSøren Schmidt * Construct auxargs table (used by the fixup routine) 1064e1743d02SSøren Schmidt */ 1065a163d034SWarner Losh elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK); 1066e1743d02SSøren Schmidt elf_auxargs->execfd = -1; 1067e1743d02SSøren Schmidt elf_auxargs->phdr = proghdr; 1068e1743d02SSøren Schmidt elf_auxargs->phent = hdr->e_phentsize; 1069e1743d02SSøren Schmidt elf_auxargs->phnum = hdr->e_phnum; 1070e1743d02SSøren Schmidt elf_auxargs->pagesz = PAGE_SIZE; 1071e1743d02SSøren Schmidt elf_auxargs->base = addr; 1072e1743d02SSøren Schmidt elf_auxargs->flags = 0; 1073e1743d02SSøren Schmidt elf_auxargs->entry = entry; 1074d36eec69SWarner Losh elf_auxargs->hdr_eflags = hdr->e_flags; 1075e1743d02SSøren Schmidt 1076e1743d02SSøren Schmidt imgp->auxargs = elf_auxargs; 1077e1743d02SSøren Schmidt imgp->interpreted = 0; 1078a0ea661fSNathan Whitehorn imgp->reloc_base = addr; 107932c01de2SDmitry Chagin imgp->proc->p_osrel = osrel; 1080885f13dcSJohn Baldwin imgp->proc->p_elf_machine = hdr->e_machine; 1081885f13dcSJohn Baldwin imgp->proc->p_elf_flags = hdr->e_flags; 1082f231de47SKonstantin Belousov 10836c775eb6SKonstantin Belousov ret: 10846c775eb6SKonstantin Belousov free(interp_buf, M_TEMP); 1085a7cddfedSJake Burkholder return (error); 1086e1743d02SSøren Schmidt } 1087e1743d02SSøren Schmidt 1088a360a43dSJake Burkholder #define suword __CONCAT(suword, __ELF_WORD_SIZE) 10893ebc1248SPeter Wemm 10903ebc1248SPeter Wemm int 10913ebc1248SPeter Wemm __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp) 1092e1743d02SSøren Schmidt { 1093ecbb00a2SDoug Rabson Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs; 1094a360a43dSJake Burkholder Elf_Addr *base; 1095a360a43dSJake Burkholder Elf_Addr *pos; 1096e1743d02SSøren Schmidt 1097a360a43dSJake Burkholder base = (Elf_Addr *)*stack_base; 1098610ecfe0SMaxim Sobolev pos = base + (imgp->args->argc + imgp->args->envc + 2); 1099e1743d02SSøren Schmidt 110035c2a5a8SWarner Losh if (args->execfd != -1) 1101e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 1102e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 1103e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 1104e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 1105e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 1106e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 1107e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 1108e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_BASE, args->base); 1109ee960398SWarner Losh #ifdef AT_EHDRFLAGS 1110d36eec69SWarner Losh AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags); 1111d36eec69SWarner Losh #endif 11123ff06357SKonstantin Belousov if (imgp->execpathp != 0) 11133ff06357SKonstantin Belousov AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp); 1114b96bd95bSIan Lepore AUXARGS_ENTRY(pos, AT_OSRELDATE, 1115b96bd95bSIan Lepore imgp->proc->p_ucred->cr_prison->pr_osreldate); 1116ee235befSKonstantin Belousov if (imgp->canary != 0) { 1117ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary); 1118ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen); 1119ee235befSKonstantin Belousov } 1120ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus); 1121ee235befSKonstantin Belousov if (imgp->pagesizes != 0) { 1122ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes); 1123ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen); 1124ee235befSKonstantin Belousov } 1125aea81038SKonstantin Belousov if (imgp->sysent->sv_timekeep_base != 0) { 1126aea81038SKonstantin Belousov AUXARGS_ENTRY(pos, AT_TIMEKEEP, 1127aea81038SKonstantin Belousov imgp->sysent->sv_timekeep_base); 1128aea81038SKonstantin Belousov } 112926d8f3e1SKonstantin Belousov AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj 113026d8f3e1SKonstantin Belousov != NULL && imgp->stack_prot != 0 ? imgp->stack_prot : 113126d8f3e1SKonstantin Belousov imgp->sysent->sv_stackprot); 1132e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_NULL, 0); 1133e1743d02SSøren Schmidt 1134e1743d02SSøren Schmidt free(imgp->auxargs, M_TEMP); 1135e1743d02SSøren Schmidt imgp->auxargs = NULL; 1136e1743d02SSøren Schmidt 11373ebc1248SPeter Wemm base--; 1138610ecfe0SMaxim Sobolev suword(base, (long)imgp->args->argc); 11393ebc1248SPeter Wemm *stack_base = (register_t *)base; 1140a7cddfedSJake Burkholder return (0); 1141e1743d02SSøren Schmidt } 1142e1743d02SSøren Schmidt 1143e1743d02SSøren Schmidt /* 11448c64af4fSJohn Polstra * Code for generating ELF core dumps. 11458c64af4fSJohn Polstra */ 11468c64af4fSJohn Polstra 11474d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *); 11480ff27d31SJohn Polstra 11490ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */ 11500ff27d31SJohn Polstra struct phdr_closure { 11510ff27d31SJohn Polstra Elf_Phdr *phdr; /* Program header to fill in */ 11520ff27d31SJohn Polstra Elf_Off offset; /* Offset of segment in core file */ 11530ff27d31SJohn Polstra }; 11540ff27d31SJohn Polstra 11550ff27d31SJohn Polstra /* Closure for cb_size_segment(). */ 11560ff27d31SJohn Polstra struct sseg_closure { 11570ff27d31SJohn Polstra int count; /* Count of writable segments. */ 11580ff27d31SJohn Polstra size_t size; /* Total size of all writable segments. */ 11590ff27d31SJohn Polstra }; 11600ff27d31SJohn Polstra 1161bd390213SMikolaj Golub typedef void (*outfunc_t)(void *, struct sbuf *, size_t *); 1162bd390213SMikolaj Golub 1163bd390213SMikolaj Golub struct note_info { 1164bd390213SMikolaj Golub int type; /* Note type. */ 1165bd390213SMikolaj Golub outfunc_t outfunc; /* Output function. */ 1166bd390213SMikolaj Golub void *outarg; /* Argument for the output function. */ 1167bd390213SMikolaj Golub size_t outsize; /* Output size. */ 1168bd390213SMikolaj Golub TAILQ_ENTRY(note_info) link; /* Link to the next note info. */ 1169bd390213SMikolaj Golub }; 1170bd390213SMikolaj Golub 1171bd390213SMikolaj Golub TAILQ_HEAD(note_info_list, note_info); 1172bd390213SMikolaj Golub 1173aa14e9b7SMark Johnston /* Coredump output parameters. */ 1174aa14e9b7SMark Johnston struct coredump_params { 1175aa14e9b7SMark Johnston off_t offset; 1176aa14e9b7SMark Johnston struct ucred *active_cred; 1177aa14e9b7SMark Johnston struct ucred *file_cred; 1178aa14e9b7SMark Johnston struct thread *td; 1179aa14e9b7SMark Johnston struct vnode *vp; 1180aa14e9b7SMark Johnston struct gzio_stream *gzs; 1181aa14e9b7SMark Johnston }; 1182aa14e9b7SMark Johnston 11834d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *); 11844d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *); 1185c468ff88SAndriy Gapon static int core_write(struct coredump_params *, const void *, size_t, off_t, 1186aa14e9b7SMark Johnston enum uio_seg); 11871005d8afSConrad Meyer static void each_dumpable_segment(struct thread *, segment_callback, void *); 1188aa14e9b7SMark Johnston static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t, 1189aa14e9b7SMark Johnston struct note_info_list *, size_t); 1190bd390213SMikolaj Golub static void __elfN(prepare_notes)(struct thread *, struct note_info_list *, 1191bd390213SMikolaj Golub size_t *); 1192bd390213SMikolaj Golub static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t); 1193bd390213SMikolaj Golub static void __elfN(putnote)(struct note_info *, struct sbuf *); 1194bd390213SMikolaj Golub static size_t register_note(struct note_info_list *, int, outfunc_t, void *); 1195bd390213SMikolaj Golub static int sbuf_drain_core_output(void *, const char *, int); 1196f1fca82eSMikolaj Golub static int sbuf_drain_count(void *arg, const char *data, int len); 1197bd390213SMikolaj Golub 1198bd390213SMikolaj Golub static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *); 1199bd390213SMikolaj Golub static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *); 1200bd390213SMikolaj Golub static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *); 1201bd390213SMikolaj Golub static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *); 1202bd390213SMikolaj Golub static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *); 1203f1fca82eSMikolaj Golub static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *); 1204f1fca82eSMikolaj Golub static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *); 1205f1fca82eSMikolaj Golub static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *); 1206f1fca82eSMikolaj Golub static void note_procstat_files(void *, struct sbuf *, size_t *); 1207f1fca82eSMikolaj Golub static void note_procstat_groups(void *, struct sbuf *, size_t *); 1208f1fca82eSMikolaj Golub static void note_procstat_osrel(void *, struct sbuf *, size_t *); 1209f1fca82eSMikolaj Golub static void note_procstat_rlimit(void *, struct sbuf *, size_t *); 1210f1fca82eSMikolaj Golub static void note_procstat_umask(void *, struct sbuf *, size_t *); 1211f1fca82eSMikolaj Golub static void note_procstat_vmmap(void *, struct sbuf *, size_t *); 12128c64af4fSJohn Polstra 1213aa14e9b7SMark Johnston #ifdef GZIO 1214e7228204SAlfred Perlstein extern int compress_user_cores_gzlevel; 1215e7228204SAlfred Perlstein 1216aa14e9b7SMark Johnston /* 1217aa14e9b7SMark Johnston * Write out a core segment to the compression stream. 1218aa14e9b7SMark Johnston */ 1219e7228204SAlfred Perlstein static int 1220aa14e9b7SMark Johnston compress_chunk(struct coredump_params *p, char *base, char *buf, u_int len) 1221aa14e9b7SMark Johnston { 1222aa14e9b7SMark Johnston u_int chunk_len; 1223e7228204SAlfred Perlstein int error; 1224aa14e9b7SMark Johnston 1225aa14e9b7SMark Johnston while (len > 0) { 1226aa14e9b7SMark Johnston chunk_len = MIN(len, CORE_BUF_SIZE); 1227c468ff88SAndriy Gapon 1228c468ff88SAndriy Gapon /* 1229c468ff88SAndriy Gapon * We can get EFAULT error here. 1230c468ff88SAndriy Gapon * In that case zero out the current chunk of the segment. 1231c468ff88SAndriy Gapon */ 1232c468ff88SAndriy Gapon error = copyin(base, buf, chunk_len); 1233c468ff88SAndriy Gapon if (error != 0) 1234c468ff88SAndriy Gapon bzero(buf, chunk_len); 1235aa14e9b7SMark Johnston error = gzio_write(p->gzs, buf, chunk_len); 1236aa14e9b7SMark Johnston if (error != 0) 1237aa14e9b7SMark Johnston break; 1238aa14e9b7SMark Johnston base += chunk_len; 1239aa14e9b7SMark Johnston len -= chunk_len; 1240e7228204SAlfred Perlstein } 1241e7228204SAlfred Perlstein return (error); 1242e7228204SAlfred Perlstein } 1243e7228204SAlfred Perlstein 1244aa14e9b7SMark Johnston static int 1245aa14e9b7SMark Johnston core_gz_write(void *base, size_t len, off_t offset, void *arg) 1246aa14e9b7SMark Johnston { 1247aa14e9b7SMark Johnston 1248aa14e9b7SMark Johnston return (core_write((struct coredump_params *)arg, base, len, offset, 1249aa14e9b7SMark Johnston UIO_SYSSPACE)); 1250aa14e9b7SMark Johnston } 1251aa14e9b7SMark Johnston #endif /* GZIO */ 1252aa14e9b7SMark Johnston 1253aa14e9b7SMark Johnston static int 1254c468ff88SAndriy Gapon core_write(struct coredump_params *p, const void *base, size_t len, 1255c468ff88SAndriy Gapon off_t offset, enum uio_seg seg) 1256aa14e9b7SMark Johnston { 1257aa14e9b7SMark Johnston 1258c468ff88SAndriy Gapon return (vn_rdwr_inchunks(UIO_WRITE, p->vp, __DECONST(void *, base), 1259c468ff88SAndriy Gapon len, offset, seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED, 1260aa14e9b7SMark Johnston p->active_cred, p->file_cred, NULL, p->td)); 1261aa14e9b7SMark Johnston } 1262aa14e9b7SMark Johnston 1263aa14e9b7SMark Johnston static int 1264aa14e9b7SMark Johnston core_output(void *base, size_t len, off_t offset, struct coredump_params *p, 1265aa14e9b7SMark Johnston void *tmpbuf) 1266aa14e9b7SMark Johnston { 1267c468ff88SAndriy Gapon int error; 1268aa14e9b7SMark Johnston 1269aa14e9b7SMark Johnston #ifdef GZIO 1270aa14e9b7SMark Johnston if (p->gzs != NULL) 1271aa14e9b7SMark Johnston return (compress_chunk(p, base, tmpbuf, len)); 1272bd390213SMikolaj Golub #endif 1273c468ff88SAndriy Gapon /* 1274c468ff88SAndriy Gapon * EFAULT is a non-fatal error that we can get, for example, 1275c468ff88SAndriy Gapon * if the segment is backed by a file but extends beyond its 1276c468ff88SAndriy Gapon * end. 1277c468ff88SAndriy Gapon */ 1278c468ff88SAndriy Gapon error = core_write(p, base, len, offset, UIO_USERSPACE); 1279c468ff88SAndriy Gapon if (error == EFAULT) { 1280c468ff88SAndriy Gapon log(LOG_WARNING, "Failed to fully fault in a core file segment " 1281c468ff88SAndriy Gapon "at VA %p with size 0x%zx to be written at offset 0x%jx " 1282c468ff88SAndriy Gapon "for process %s\n", base, len, offset, curproc->p_comm); 1283c468ff88SAndriy Gapon 1284c468ff88SAndriy Gapon /* 1285c468ff88SAndriy Gapon * Write a "real" zero byte at the end of the target region 1286c468ff88SAndriy Gapon * in the case this is the last segment. 1287c468ff88SAndriy Gapon * The intermediate space will be implicitly zero-filled. 1288c468ff88SAndriy Gapon */ 1289c468ff88SAndriy Gapon error = core_write(p, zero_region, 1, offset + len - 1, 1290c468ff88SAndriy Gapon UIO_SYSSPACE); 1291c468ff88SAndriy Gapon } 1292c468ff88SAndriy Gapon return (error); 1293aa14e9b7SMark Johnston } 1294bd390213SMikolaj Golub 1295bd390213SMikolaj Golub /* 1296bd390213SMikolaj Golub * Drain into a core file. 1297bd390213SMikolaj Golub */ 1298bd390213SMikolaj Golub static int 1299bd390213SMikolaj Golub sbuf_drain_core_output(void *arg, const char *data, int len) 1300bd390213SMikolaj Golub { 1301aa14e9b7SMark Johnston struct coredump_params *p; 1302f1fca82eSMikolaj Golub int error, locked; 1303bd390213SMikolaj Golub 1304aa14e9b7SMark Johnston p = (struct coredump_params *)arg; 1305f1fca82eSMikolaj Golub 1306f1fca82eSMikolaj Golub /* 1307f1fca82eSMikolaj Golub * Some kern_proc out routines that print to this sbuf may 1308f1fca82eSMikolaj Golub * call us with the process lock held. Draining with the 1309f1fca82eSMikolaj Golub * non-sleepable lock held is unsafe. The lock is needed for 1310f1fca82eSMikolaj Golub * those routines when dumping a live process. In our case we 1311f1fca82eSMikolaj Golub * can safely release the lock before draining and acquire 1312f1fca82eSMikolaj Golub * again after. 1313f1fca82eSMikolaj Golub */ 1314f1fca82eSMikolaj Golub locked = PROC_LOCKED(p->td->td_proc); 1315f1fca82eSMikolaj Golub if (locked) 1316f1fca82eSMikolaj Golub PROC_UNLOCK(p->td->td_proc); 1317aa14e9b7SMark Johnston #ifdef GZIO 1318aa14e9b7SMark Johnston if (p->gzs != NULL) 1319aa14e9b7SMark Johnston error = gzio_write(p->gzs, __DECONST(char *, data), len); 1320bd390213SMikolaj Golub else 1321bd390213SMikolaj Golub #endif 1322aa14e9b7SMark Johnston error = core_write(p, __DECONST(void *, data), len, p->offset, 1323aa14e9b7SMark Johnston UIO_SYSSPACE); 1324f1fca82eSMikolaj Golub if (locked) 1325f1fca82eSMikolaj Golub PROC_LOCK(p->td->td_proc); 1326bd390213SMikolaj Golub if (error != 0) 1327bd390213SMikolaj Golub return (-error); 1328bd390213SMikolaj Golub p->offset += len; 1329bd390213SMikolaj Golub return (len); 1330bd390213SMikolaj Golub } 1331bd390213SMikolaj Golub 1332f1fca82eSMikolaj Golub /* 1333f1fca82eSMikolaj Golub * Drain into a counter. 1334f1fca82eSMikolaj Golub */ 1335f1fca82eSMikolaj Golub static int 1336f1fca82eSMikolaj Golub sbuf_drain_count(void *arg, const char *data __unused, int len) 1337f1fca82eSMikolaj Golub { 1338f1fca82eSMikolaj Golub size_t *sizep; 1339f1fca82eSMikolaj Golub 1340f1fca82eSMikolaj Golub sizep = (size_t *)arg; 1341f1fca82eSMikolaj Golub *sizep += len; 1342f1fca82eSMikolaj Golub return (len); 1343f1fca82eSMikolaj Golub } 1344f1fca82eSMikolaj Golub 13458c64af4fSJohn Polstra int 1346e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags) 1347fca666a1SJulian Elischer { 1348247aba24SMarcel Moolenaar struct ucred *cred = td->td_ucred; 1349fca666a1SJulian Elischer int error = 0; 13500ff27d31SJohn Polstra struct sseg_closure seginfo; 1351bd390213SMikolaj Golub struct note_info_list notelst; 1352aa14e9b7SMark Johnston struct coredump_params params; 1353bd390213SMikolaj Golub struct note_info *ninfo; 1354aa14e9b7SMark Johnston void *hdr, *tmpbuf; 1355bd390213SMikolaj Golub size_t hdrsize, notesz, coresize; 13569a12e282SEnji Cooper #ifdef GZIO 1357aa14e9b7SMark Johnston boolean_t compress; 13588c64af4fSJohn Polstra 1359aa14e9b7SMark Johnston compress = (flags & IMGACT_CORE_COMPRESS) != 0; 13609a12e282SEnji Cooper #endif 1361e7228204SAlfred Perlstein hdr = NULL; 136202d131adSMark Johnston tmpbuf = NULL; 1363bd390213SMikolaj Golub TAILQ_INIT(¬elst); 1364e7228204SAlfred Perlstein 13650ff27d31SJohn Polstra /* Size the program segments. */ 13660ff27d31SJohn Polstra seginfo.count = 0; 13670ff27d31SJohn Polstra seginfo.size = 0; 13681005d8afSConrad Meyer each_dumpable_segment(td, cb_size_segment, &seginfo); 13690ff27d31SJohn Polstra 13700ff27d31SJohn Polstra /* 1371bd390213SMikolaj Golub * Collect info about the core file header area. 13720ff27d31SJohn Polstra */ 1373bd390213SMikolaj Golub hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count); 1374c17b0bd2SConrad Meyer if (seginfo.count + 1 >= PN_XNUM) 1375c17b0bd2SConrad Meyer hdrsize += sizeof(Elf_Shdr); 1376bd390213SMikolaj Golub __elfN(prepare_notes)(td, ¬elst, ¬esz); 1377bd390213SMikolaj Golub coresize = round_page(hdrsize + notesz) + seginfo.size; 13780ff27d31SJohn Polstra 137902d131adSMark Johnston /* Set up core dump parameters. */ 138002d131adSMark Johnston params.offset = 0; 138102d131adSMark Johnston params.active_cred = cred; 138202d131adSMark Johnston params.file_cred = NOCRED; 138302d131adSMark Johnston params.td = td; 138402d131adSMark Johnston params.vp = vp; 138502d131adSMark Johnston params.gzs = NULL; 138602d131adSMark Johnston 1387afcc55f3SEdward Tomasz Napierala #ifdef RACCT 13884b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 13891ba5ad42SEdward Tomasz Napierala PROC_LOCK(td->td_proc); 1390bd390213SMikolaj Golub error = racct_add(td->td_proc, RACCT_CORE, coresize); 13911ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(td->td_proc); 13921ba5ad42SEdward Tomasz Napierala if (error != 0) { 13931ba5ad42SEdward Tomasz Napierala error = EFAULT; 13941ba5ad42SEdward Tomasz Napierala goto done; 13951ba5ad42SEdward Tomasz Napierala } 13964b5c9cf6SEdward Tomasz Napierala } 1397afcc55f3SEdward Tomasz Napierala #endif 1398bd390213SMikolaj Golub if (coresize >= limit) { 1399fba6b1afSAlfred Perlstein error = EFAULT; 1400fba6b1afSAlfred Perlstein goto done; 1401fba6b1afSAlfred Perlstein } 14020ff27d31SJohn Polstra 1403aa14e9b7SMark Johnston #ifdef GZIO 1404aa14e9b7SMark Johnston /* Create a compression stream if necessary. */ 1405aa14e9b7SMark Johnston if (compress) { 1406aa14e9b7SMark Johnston params.gzs = gzio_init(core_gz_write, GZIO_DEFLATE, 1407aa14e9b7SMark Johnston CORE_BUF_SIZE, compress_user_cores_gzlevel, ¶ms); 1408aa14e9b7SMark Johnston if (params.gzs == NULL) { 1409aa14e9b7SMark Johnston error = EFAULT; 1410aa14e9b7SMark Johnston goto done; 1411aa14e9b7SMark Johnston } 1412aa14e9b7SMark Johnston tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO); 1413aa14e9b7SMark Johnston } 1414aa14e9b7SMark Johnston #endif 1415aa14e9b7SMark Johnston 14160ff27d31SJohn Polstra /* 14170ff27d31SJohn Polstra * Allocate memory for building the header, fill it up, 1418bd390213SMikolaj Golub * and write it out following the notes. 14190ff27d31SJohn Polstra */ 1420a163d034SWarner Losh hdr = malloc(hdrsize, M_TEMP, M_WAITOK); 1421aa14e9b7SMark Johnston error = __elfN(corehdr)(¶ms, seginfo.count, hdr, hdrsize, ¬elst, 1422aa14e9b7SMark Johnston notesz); 14230ff27d31SJohn Polstra 14240ff27d31SJohn Polstra /* Write the contents of all of the writable segments. */ 14250ff27d31SJohn Polstra if (error == 0) { 14260ff27d31SJohn Polstra Elf_Phdr *php; 14272b471bc6STim J. Robbins off_t offset; 14280ff27d31SJohn Polstra int i; 14290ff27d31SJohn Polstra 14300ff27d31SJohn Polstra php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 1431bd390213SMikolaj Golub offset = round_page(hdrsize + notesz); 14320ff27d31SJohn Polstra for (i = 0; i < seginfo.count; i++) { 1433aa14e9b7SMark Johnston error = core_output((caddr_t)(uintptr_t)php->p_vaddr, 1434aa14e9b7SMark Johnston php->p_filesz, offset, ¶ms, tmpbuf); 14350ff27d31SJohn Polstra if (error != 0) 14362b471bc6STim J. Robbins break; 14370ff27d31SJohn Polstra offset += php->p_filesz; 14380ff27d31SJohn Polstra php++; 14390ff27d31SJohn Polstra } 1440aa14e9b7SMark Johnston #ifdef GZIO 1441aa14e9b7SMark Johnston if (error == 0 && compress) 1442aa14e9b7SMark Johnston error = gzio_flush(params.gzs); 1443aa14e9b7SMark Johnston #endif 14440ff27d31SJohn Polstra } 1445e7228204SAlfred Perlstein if (error) { 1446e7228204SAlfred Perlstein log(LOG_WARNING, 1447e7228204SAlfred Perlstein "Failed to write core file for process %s (error %d)\n", 1448e7228204SAlfred Perlstein curproc->p_comm, error); 1449e7228204SAlfred Perlstein } 1450e7228204SAlfred Perlstein 1451e7228204SAlfred Perlstein done: 1452aa14e9b7SMark Johnston #ifdef GZIO 1453aa14e9b7SMark Johnston if (compress) { 1454aa14e9b7SMark Johnston free(tmpbuf, M_TEMP); 145502d131adSMark Johnston if (params.gzs != NULL) 1456aa14e9b7SMark Johnston gzio_fini(params.gzs); 1457aa14e9b7SMark Johnston } 14588b325009SAlfred Perlstein #endif 1459bd390213SMikolaj Golub while ((ninfo = TAILQ_FIRST(¬elst)) != NULL) { 1460bd390213SMikolaj Golub TAILQ_REMOVE(¬elst, ninfo, link); 1461bd390213SMikolaj Golub free(ninfo, M_TEMP); 1462bd390213SMikolaj Golub } 1463bd390213SMikolaj Golub if (hdr != NULL) 14640ff27d31SJohn Polstra free(hdr, M_TEMP); 14650ff27d31SJohn Polstra 1466a7cddfedSJake Burkholder return (error); 14678c64af4fSJohn Polstra } 14688c64af4fSJohn Polstra 14690ff27d31SJohn Polstra /* 14701005d8afSConrad Meyer * A callback for each_dumpable_segment() to write out the segment's 14710ff27d31SJohn Polstra * program header entry. 14720ff27d31SJohn Polstra */ 14730ff27d31SJohn Polstra static void 14740ff27d31SJohn Polstra cb_put_phdr(entry, closure) 14750ff27d31SJohn Polstra vm_map_entry_t entry; 14760ff27d31SJohn Polstra void *closure; 14770ff27d31SJohn Polstra { 14780ff27d31SJohn Polstra struct phdr_closure *phc = (struct phdr_closure *)closure; 14790ff27d31SJohn Polstra Elf_Phdr *phdr = phc->phdr; 14800ff27d31SJohn Polstra 14810ff27d31SJohn Polstra phc->offset = round_page(phc->offset); 14820ff27d31SJohn Polstra 14830ff27d31SJohn Polstra phdr->p_type = PT_LOAD; 14840ff27d31SJohn Polstra phdr->p_offset = phc->offset; 14850ff27d31SJohn Polstra phdr->p_vaddr = entry->start; 14860ff27d31SJohn Polstra phdr->p_paddr = 0; 14870ff27d31SJohn Polstra phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 14880ff27d31SJohn Polstra phdr->p_align = PAGE_SIZE; 1489ed167eaaSKonstantin Belousov phdr->p_flags = __elfN(untrans_prot)(entry->protection); 14900ff27d31SJohn Polstra 14910ff27d31SJohn Polstra phc->offset += phdr->p_filesz; 14920ff27d31SJohn Polstra phc->phdr++; 14930ff27d31SJohn Polstra } 14940ff27d31SJohn Polstra 14950ff27d31SJohn Polstra /* 14961005d8afSConrad Meyer * A callback for each_dumpable_segment() to gather information about 14970ff27d31SJohn Polstra * the number of segments and their total size. 14980ff27d31SJohn Polstra */ 14990ff27d31SJohn Polstra static void 1500f3325003SConrad Meyer cb_size_segment(vm_map_entry_t entry, void *closure) 15010ff27d31SJohn Polstra { 15020ff27d31SJohn Polstra struct sseg_closure *ssc = (struct sseg_closure *)closure; 15030ff27d31SJohn Polstra 15040ff27d31SJohn Polstra ssc->count++; 15050ff27d31SJohn Polstra ssc->size += entry->end - entry->start; 15060ff27d31SJohn Polstra } 15070ff27d31SJohn Polstra 15080ff27d31SJohn Polstra /* 15090ff27d31SJohn Polstra * For each writable segment in the process's memory map, call the given 15100ff27d31SJohn Polstra * function with a pointer to the map entry and some arbitrary 15110ff27d31SJohn Polstra * caller-supplied data. 15120ff27d31SJohn Polstra */ 15130ff27d31SJohn Polstra static void 15141005d8afSConrad Meyer each_dumpable_segment(struct thread *td, segment_callback func, void *closure) 15150ff27d31SJohn Polstra { 1516247aba24SMarcel Moolenaar struct proc *p = td->td_proc; 15170ff27d31SJohn Polstra vm_map_t map = &p->p_vmspace->vm_map; 15180ff27d31SJohn Polstra vm_map_entry_t entry; 1519976a87a2SAlan Cox vm_object_t backing_object, object; 1520976a87a2SAlan Cox boolean_t ignore_entry; 15210ff27d31SJohn Polstra 1522976a87a2SAlan Cox vm_map_lock_read(map); 15230ff27d31SJohn Polstra for (entry = map->header.next; entry != &map->header; 15240ff27d31SJohn Polstra entry = entry->next) { 1525fa7dd9c5SMatthew Dillon /* 1526fa7dd9c5SMatthew Dillon * Don't dump inaccessible mappings, deal with legacy 1527fa7dd9c5SMatthew Dillon * coredump mode. 1528fa7dd9c5SMatthew Dillon * 1529fa7dd9c5SMatthew Dillon * Note that read-only segments related to the elf binary 1530fa7dd9c5SMatthew Dillon * are marked MAP_ENTRY_NOCOREDUMP now so we no longer 1531fa7dd9c5SMatthew Dillon * need to arbitrarily ignore such segments. 1532fa7dd9c5SMatthew Dillon */ 1533fa7dd9c5SMatthew Dillon if (elf_legacy_coredump) { 1534fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_RW) != VM_PROT_RW) 15350ff27d31SJohn Polstra continue; 1536fa7dd9c5SMatthew Dillon } else { 1537fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_ALL) == 0) 1538fa7dd9c5SMatthew Dillon continue; 1539fa7dd9c5SMatthew Dillon } 15400ff27d31SJohn Polstra 15419730a5daSPaul Saab /* 1542fa7dd9c5SMatthew Dillon * Dont include memory segment in the coredump if 1543fa7dd9c5SMatthew Dillon * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in 1544fa7dd9c5SMatthew Dillon * madvise(2). Do not dump submaps (i.e. parts of the 1545fa7dd9c5SMatthew Dillon * kernel map). 15469730a5daSPaul Saab */ 1547fa7dd9c5SMatthew Dillon if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP)) 15489730a5daSPaul Saab continue; 15499730a5daSPaul Saab 1550976a87a2SAlan Cox if ((object = entry->object.vm_object) == NULL) 15510ff27d31SJohn Polstra continue; 15520ff27d31SJohn Polstra 15530ff27d31SJohn Polstra /* Ignore memory-mapped devices and such things. */ 1554bc403f03SAttilio Rao VM_OBJECT_RLOCK(object); 1555976a87a2SAlan Cox while ((backing_object = object->backing_object) != NULL) { 1556bc403f03SAttilio Rao VM_OBJECT_RLOCK(backing_object); 1557bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(object); 1558976a87a2SAlan Cox object = backing_object; 1559976a87a2SAlan Cox } 1560976a87a2SAlan Cox ignore_entry = object->type != OBJT_DEFAULT && 1561bc411bc2SJohn Baldwin object->type != OBJT_SWAP && object->type != OBJT_VNODE && 1562bc411bc2SJohn Baldwin object->type != OBJT_PHYS; 1563bc403f03SAttilio Rao VM_OBJECT_RUNLOCK(object); 1564976a87a2SAlan Cox if (ignore_entry) 15650ff27d31SJohn Polstra continue; 15660ff27d31SJohn Polstra 15670ff27d31SJohn Polstra (*func)(entry, closure); 15680ff27d31SJohn Polstra } 1569976a87a2SAlan Cox vm_map_unlock_read(map); 15700ff27d31SJohn Polstra } 15710ff27d31SJohn Polstra 15720ff27d31SJohn Polstra /* 15730ff27d31SJohn Polstra * Write the core file header to the file, including padding up to 15740ff27d31SJohn Polstra * the page boundary. 15750ff27d31SJohn Polstra */ 15768c64af4fSJohn Polstra static int 1577aa14e9b7SMark Johnston __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr, 1578aa14e9b7SMark Johnston size_t hdrsize, struct note_info_list *notelst, size_t notesz) 15798c64af4fSJohn Polstra { 1580bd390213SMikolaj Golub struct note_info *ninfo; 1581bd390213SMikolaj Golub struct sbuf *sb; 1582bd390213SMikolaj Golub int error; 15838c64af4fSJohn Polstra 15848c64af4fSJohn Polstra /* Fill in the header. */ 15850ff27d31SJohn Polstra bzero(hdr, hdrsize); 1586aa14e9b7SMark Johnston __elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz); 15878c64af4fSJohn Polstra 1588bd390213SMikolaj Golub sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN); 1589aa14e9b7SMark Johnston sbuf_set_drain(sb, sbuf_drain_core_output, p); 1590bd390213SMikolaj Golub sbuf_start_section(sb, NULL); 1591bd390213SMikolaj Golub sbuf_bcat(sb, hdr, hdrsize); 1592bd390213SMikolaj Golub TAILQ_FOREACH(ninfo, notelst, link) 1593bd390213SMikolaj Golub __elfN(putnote)(ninfo, sb); 1594bd390213SMikolaj Golub /* Align up to a page boundary for the program segments. */ 1595bd390213SMikolaj Golub sbuf_end_section(sb, -1, PAGE_SIZE, 0); 1596bd390213SMikolaj Golub error = sbuf_finish(sb); 1597bd390213SMikolaj Golub sbuf_delete(sb); 1598bd390213SMikolaj Golub 1599bd390213SMikolaj Golub return (error); 1600e7228204SAlfred Perlstein } 1601bd390213SMikolaj Golub 1602bd390213SMikolaj Golub static void 1603bd390213SMikolaj Golub __elfN(prepare_notes)(struct thread *td, struct note_info_list *list, 1604bd390213SMikolaj Golub size_t *sizep) 1605bd390213SMikolaj Golub { 1606bd390213SMikolaj Golub struct proc *p; 1607bd390213SMikolaj Golub struct thread *thr; 1608bd390213SMikolaj Golub size_t size; 1609bd390213SMikolaj Golub 1610bd390213SMikolaj Golub p = td->td_proc; 1611bd390213SMikolaj Golub size = 0; 1612bd390213SMikolaj Golub 1613bd390213SMikolaj Golub size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p); 1614bd390213SMikolaj Golub 1615bd390213SMikolaj Golub /* 1616bd390213SMikolaj Golub * To have the debugger select the right thread (LWP) as the initial 1617bd390213SMikolaj Golub * thread, we dump the state of the thread passed to us in td first. 1618bd390213SMikolaj Golub * This is the thread that causes the core dump and thus likely to 1619bd390213SMikolaj Golub * be the right thread one wants to have selected in the debugger. 1620bd390213SMikolaj Golub */ 1621bd390213SMikolaj Golub thr = td; 1622bd390213SMikolaj Golub while (thr != NULL) { 1623bd390213SMikolaj Golub size += register_note(list, NT_PRSTATUS, 1624bd390213SMikolaj Golub __elfN(note_prstatus), thr); 1625bd390213SMikolaj Golub size += register_note(list, NT_FPREGSET, 1626bd390213SMikolaj Golub __elfN(note_fpregset), thr); 1627bd390213SMikolaj Golub size += register_note(list, NT_THRMISC, 1628bd390213SMikolaj Golub __elfN(note_thrmisc), thr); 1629bd390213SMikolaj Golub size += register_note(list, -1, 1630bd390213SMikolaj Golub __elfN(note_threadmd), thr); 1631bd390213SMikolaj Golub 1632bd390213SMikolaj Golub thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) : 1633bd390213SMikolaj Golub TAILQ_NEXT(thr, td_plist); 1634bd390213SMikolaj Golub if (thr == td) 1635bd390213SMikolaj Golub thr = TAILQ_NEXT(thr, td_plist); 1636dada0278SJohn Polstra } 1637dada0278SJohn Polstra 1638f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_PROC, 1639f1fca82eSMikolaj Golub __elfN(note_procstat_proc), p); 1640f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_FILES, 1641f1fca82eSMikolaj Golub note_procstat_files, p); 1642f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_VMMAP, 1643f1fca82eSMikolaj Golub note_procstat_vmmap, p); 1644f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_GROUPS, 1645f1fca82eSMikolaj Golub note_procstat_groups, p); 1646f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_UMASK, 1647f1fca82eSMikolaj Golub note_procstat_umask, p); 1648f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_RLIMIT, 1649f1fca82eSMikolaj Golub note_procstat_rlimit, p); 1650f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_OSREL, 1651f1fca82eSMikolaj Golub note_procstat_osrel, p); 1652f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_PSSTRINGS, 1653f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings), p); 1654f1fca82eSMikolaj Golub size += register_note(list, NT_PROCSTAT_AUXV, 1655f1fca82eSMikolaj Golub __elfN(note_procstat_auxv), p); 1656f1fca82eSMikolaj Golub 1657bd390213SMikolaj Golub *sizep = size; 1658bd390213SMikolaj Golub } 1659bd390213SMikolaj Golub 1660bd390213SMikolaj Golub static void 1661bd390213SMikolaj Golub __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs, 1662bd390213SMikolaj Golub size_t notesz) 1663bd390213SMikolaj Golub { 1664bd390213SMikolaj Golub Elf_Ehdr *ehdr; 1665bd390213SMikolaj Golub Elf_Phdr *phdr; 1666c17b0bd2SConrad Meyer Elf_Shdr *shdr; 1667bd390213SMikolaj Golub struct phdr_closure phc; 1668bd390213SMikolaj Golub 1669bd390213SMikolaj Golub ehdr = (Elf_Ehdr *)hdr; 1670bd390213SMikolaj Golub 1671bd390213SMikolaj Golub ehdr->e_ident[EI_MAG0] = ELFMAG0; 1672bd390213SMikolaj Golub ehdr->e_ident[EI_MAG1] = ELFMAG1; 1673bd390213SMikolaj Golub ehdr->e_ident[EI_MAG2] = ELFMAG2; 1674bd390213SMikolaj Golub ehdr->e_ident[EI_MAG3] = ELFMAG3; 1675bd390213SMikolaj Golub ehdr->e_ident[EI_CLASS] = ELF_CLASS; 1676bd390213SMikolaj Golub ehdr->e_ident[EI_DATA] = ELF_DATA; 1677bd390213SMikolaj Golub ehdr->e_ident[EI_VERSION] = EV_CURRENT; 1678bd390213SMikolaj Golub ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 1679bd390213SMikolaj Golub ehdr->e_ident[EI_ABIVERSION] = 0; 1680bd390213SMikolaj Golub ehdr->e_ident[EI_PAD] = 0; 1681bd390213SMikolaj Golub ehdr->e_type = ET_CORE; 1682885f13dcSJohn Baldwin ehdr->e_machine = td->td_proc->p_elf_machine; 1683bd390213SMikolaj Golub ehdr->e_version = EV_CURRENT; 1684bd390213SMikolaj Golub ehdr->e_entry = 0; 1685bd390213SMikolaj Golub ehdr->e_phoff = sizeof(Elf_Ehdr); 1686885f13dcSJohn Baldwin ehdr->e_flags = td->td_proc->p_elf_flags; 1687bd390213SMikolaj Golub ehdr->e_ehsize = sizeof(Elf_Ehdr); 1688bd390213SMikolaj Golub ehdr->e_phentsize = sizeof(Elf_Phdr); 1689bd390213SMikolaj Golub ehdr->e_shentsize = sizeof(Elf_Shdr); 1690bd390213SMikolaj Golub ehdr->e_shstrndx = SHN_UNDEF; 1691c17b0bd2SConrad Meyer if (numsegs + 1 < PN_XNUM) { 1692c17b0bd2SConrad Meyer ehdr->e_phnum = numsegs + 1; 1693c17b0bd2SConrad Meyer ehdr->e_shnum = 0; 1694c17b0bd2SConrad Meyer } else { 1695c17b0bd2SConrad Meyer ehdr->e_phnum = PN_XNUM; 1696c17b0bd2SConrad Meyer ehdr->e_shnum = 1; 1697c17b0bd2SConrad Meyer 1698c17b0bd2SConrad Meyer ehdr->e_shoff = ehdr->e_phoff + 1699c17b0bd2SConrad Meyer (numsegs + 1) * ehdr->e_phentsize; 1700c17b0bd2SConrad Meyer KASSERT(ehdr->e_shoff == hdrsize - sizeof(Elf_Shdr), 1701c17b0bd2SConrad Meyer ("e_shoff: %zu, hdrsize - shdr: %zu", 170207f825e8SConrad Meyer (size_t)ehdr->e_shoff, hdrsize - sizeof(Elf_Shdr))); 1703c17b0bd2SConrad Meyer 1704c17b0bd2SConrad Meyer shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff); 1705c17b0bd2SConrad Meyer memset(shdr, 0, sizeof(*shdr)); 1706c17b0bd2SConrad Meyer /* 1707c17b0bd2SConrad Meyer * A special first section is used to hold large segment and 1708c17b0bd2SConrad Meyer * section counts. This was proposed by Sun Microsystems in 1709c17b0bd2SConrad Meyer * Solaris and has been adopted by Linux; the standard ELF 1710c17b0bd2SConrad Meyer * tools are already familiar with the technique. 1711c17b0bd2SConrad Meyer * 1712c17b0bd2SConrad Meyer * See table 7-7 of the Solaris "Linker and Libraries Guide" 1713c17b0bd2SConrad Meyer * (or 12-7 depending on the version of the document) for more 1714c17b0bd2SConrad Meyer * details. 1715c17b0bd2SConrad Meyer */ 1716c17b0bd2SConrad Meyer shdr->sh_type = SHT_NULL; 1717c17b0bd2SConrad Meyer shdr->sh_size = ehdr->e_shnum; 1718c17b0bd2SConrad Meyer shdr->sh_link = ehdr->e_shstrndx; 1719c17b0bd2SConrad Meyer shdr->sh_info = numsegs + 1; 1720c17b0bd2SConrad Meyer } 1721bd390213SMikolaj Golub 1722bd390213SMikolaj Golub /* 1723bd390213SMikolaj Golub * Fill in the program header entries. 1724bd390213SMikolaj Golub */ 1725c17b0bd2SConrad Meyer phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff); 1726bd390213SMikolaj Golub 1727bd390213SMikolaj Golub /* The note segement. */ 1728bd390213SMikolaj Golub phdr->p_type = PT_NOTE; 1729bd390213SMikolaj Golub phdr->p_offset = hdrsize; 1730bd390213SMikolaj Golub phdr->p_vaddr = 0; 1731bd390213SMikolaj Golub phdr->p_paddr = 0; 1732bd390213SMikolaj Golub phdr->p_filesz = notesz; 1733bd390213SMikolaj Golub phdr->p_memsz = 0; 1734bd390213SMikolaj Golub phdr->p_flags = PF_R; 17351b8388cdSMikolaj Golub phdr->p_align = ELF_NOTE_ROUNDSIZE; 1736bd390213SMikolaj Golub phdr++; 1737bd390213SMikolaj Golub 1738bd390213SMikolaj Golub /* All the writable segments from the program. */ 1739bd390213SMikolaj Golub phc.phdr = phdr; 1740bd390213SMikolaj Golub phc.offset = round_page(hdrsize + notesz); 17411005d8afSConrad Meyer each_dumpable_segment(td, cb_put_phdr, &phc); 1742bd390213SMikolaj Golub } 1743bd390213SMikolaj Golub 1744bd390213SMikolaj Golub static size_t 1745bd390213SMikolaj Golub register_note(struct note_info_list *list, int type, outfunc_t out, void *arg) 1746bd390213SMikolaj Golub { 1747bd390213SMikolaj Golub struct note_info *ninfo; 1748bd390213SMikolaj Golub size_t size, notesize; 1749bd390213SMikolaj Golub 1750bd390213SMikolaj Golub size = 0; 1751bd390213SMikolaj Golub out(arg, NULL, &size); 1752bd390213SMikolaj Golub ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK); 1753bd390213SMikolaj Golub ninfo->type = type; 1754bd390213SMikolaj Golub ninfo->outfunc = out; 1755bd390213SMikolaj Golub ninfo->outarg = arg; 1756bd390213SMikolaj Golub ninfo->outsize = size; 1757bd390213SMikolaj Golub TAILQ_INSERT_TAIL(list, ninfo, link); 1758bd390213SMikolaj Golub 1759bd390213SMikolaj Golub if (type == -1) 1760bd390213SMikolaj Golub return (size); 1761bd390213SMikolaj Golub 1762bd390213SMikolaj Golub notesize = sizeof(Elf_Note) + /* note header */ 1763180e57e5SJohn Baldwin roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) + 1764180e57e5SJohn Baldwin /* note name */ 1765180e57e5SJohn Baldwin roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */ 1766180e57e5SJohn Baldwin 1767180e57e5SJohn Baldwin return (notesize); 1768180e57e5SJohn Baldwin } 1769180e57e5SJohn Baldwin 1770180e57e5SJohn Baldwin static size_t 1771180e57e5SJohn Baldwin append_note_data(const void *src, void *dst, size_t len) 1772180e57e5SJohn Baldwin { 1773180e57e5SJohn Baldwin size_t padded_len; 1774180e57e5SJohn Baldwin 1775180e57e5SJohn Baldwin padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE); 1776180e57e5SJohn Baldwin if (dst != NULL) { 1777180e57e5SJohn Baldwin bcopy(src, dst, len); 1778180e57e5SJohn Baldwin bzero((char *)dst + len, padded_len - len); 1779180e57e5SJohn Baldwin } 1780180e57e5SJohn Baldwin return (padded_len); 1781180e57e5SJohn Baldwin } 1782180e57e5SJohn Baldwin 1783180e57e5SJohn Baldwin size_t 1784180e57e5SJohn Baldwin __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp) 1785180e57e5SJohn Baldwin { 1786180e57e5SJohn Baldwin Elf_Note *note; 1787180e57e5SJohn Baldwin char *buf; 1788180e57e5SJohn Baldwin size_t notesize; 1789180e57e5SJohn Baldwin 1790180e57e5SJohn Baldwin buf = dst; 1791180e57e5SJohn Baldwin if (buf != NULL) { 1792180e57e5SJohn Baldwin note = (Elf_Note *)buf; 1793180e57e5SJohn Baldwin note->n_namesz = sizeof(FREEBSD_ABI_VENDOR); 1794180e57e5SJohn Baldwin note->n_descsz = size; 1795180e57e5SJohn Baldwin note->n_type = type; 1796180e57e5SJohn Baldwin buf += sizeof(*note); 1797180e57e5SJohn Baldwin buf += append_note_data(FREEBSD_ABI_VENDOR, buf, 1798180e57e5SJohn Baldwin sizeof(FREEBSD_ABI_VENDOR)); 1799180e57e5SJohn Baldwin append_note_data(src, buf, size); 1800180e57e5SJohn Baldwin if (descp != NULL) 1801180e57e5SJohn Baldwin *descp = buf; 1802180e57e5SJohn Baldwin } 1803180e57e5SJohn Baldwin 1804180e57e5SJohn Baldwin notesize = sizeof(Elf_Note) + /* note header */ 1805180e57e5SJohn Baldwin roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) + 1806180e57e5SJohn Baldwin /* note name */ 18071b8388cdSMikolaj Golub roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */ 1808bd390213SMikolaj Golub 1809bd390213SMikolaj Golub return (notesize); 1810bd390213SMikolaj Golub } 1811bd390213SMikolaj Golub 1812bd390213SMikolaj Golub static void 1813bd390213SMikolaj Golub __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb) 1814bd390213SMikolaj Golub { 1815bd390213SMikolaj Golub Elf_Note note; 181614bdbaf2SConrad Meyer ssize_t old_len, sect_len; 181714bdbaf2SConrad Meyer size_t new_len, descsz, i; 1818bd390213SMikolaj Golub 1819bd390213SMikolaj Golub if (ninfo->type == -1) { 1820bd390213SMikolaj Golub ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize); 1821bd390213SMikolaj Golub return; 1822bd390213SMikolaj Golub } 1823bd390213SMikolaj Golub 1824180e57e5SJohn Baldwin note.n_namesz = sizeof(FREEBSD_ABI_VENDOR); 1825bd390213SMikolaj Golub note.n_descsz = ninfo->outsize; 1826bd390213SMikolaj Golub note.n_type = ninfo->type; 1827bd390213SMikolaj Golub 1828bd390213SMikolaj Golub sbuf_bcat(sb, ¬e, sizeof(note)); 1829bd390213SMikolaj Golub sbuf_start_section(sb, &old_len); 1830180e57e5SJohn Baldwin sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR)); 18311b8388cdSMikolaj Golub sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0); 1832bd390213SMikolaj Golub if (note.n_descsz == 0) 1833bd390213SMikolaj Golub return; 1834bd390213SMikolaj Golub sbuf_start_section(sb, &old_len); 1835bd390213SMikolaj Golub ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize); 183614bdbaf2SConrad Meyer sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0); 183714bdbaf2SConrad Meyer if (sect_len < 0) 183814bdbaf2SConrad Meyer return; 183914bdbaf2SConrad Meyer 184014bdbaf2SConrad Meyer new_len = (size_t)sect_len; 184114bdbaf2SConrad Meyer descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE); 184214bdbaf2SConrad Meyer if (new_len < descsz) { 184314bdbaf2SConrad Meyer /* 184414bdbaf2SConrad Meyer * It is expected that individual note emitters will correctly 184514bdbaf2SConrad Meyer * predict their expected output size and fill up to that size 184614bdbaf2SConrad Meyer * themselves, padding in a format-specific way if needed. 184714bdbaf2SConrad Meyer * However, in case they don't, just do it here with zeros. 184814bdbaf2SConrad Meyer */ 184914bdbaf2SConrad Meyer for (i = 0; i < descsz - new_len; i++) 185014bdbaf2SConrad Meyer sbuf_putc(sb, 0); 185114bdbaf2SConrad Meyer } else if (new_len > descsz) { 185214bdbaf2SConrad Meyer /* 185314bdbaf2SConrad Meyer * We can't always truncate sb -- we may have drained some 185414bdbaf2SConrad Meyer * of it already. 185514bdbaf2SConrad Meyer */ 185614bdbaf2SConrad Meyer KASSERT(new_len == descsz, ("%s: Note type %u changed as we " 185714bdbaf2SConrad Meyer "read it (%zu > %zu). Since it is longer than " 185814bdbaf2SConrad Meyer "expected, this coredump's notes are corrupt. THIS " 185914bdbaf2SConrad Meyer "IS A BUG in the note_procstat routine for type %u.\n", 186014bdbaf2SConrad Meyer __func__, (unsigned)note.n_type, new_len, descsz, 186114bdbaf2SConrad Meyer (unsigned)note.n_type)); 186214bdbaf2SConrad Meyer } 1863bd390213SMikolaj Golub } 1864bd390213SMikolaj Golub 1865bd390213SMikolaj Golub /* 1866bd390213SMikolaj Golub * Miscellaneous note out functions. 1867bd390213SMikolaj Golub */ 1868bd390213SMikolaj Golub 1869841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1870841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h> 1871841c0c7eSNathan Whitehorn 187262919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t; 187362919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t; 187462919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t; 187562919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t; 187662919d78SPeter Wemm typedef struct reg32 elf_gregset_t; 18777f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t; 1878f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK KERN_PROC_MASK32 1879f1fca82eSMikolaj Golub typedef struct kinfo_proc32 elf_kinfo_proc_t; 1880f1fca82eSMikolaj Golub typedef uint32_t elf_ps_strings_t; 188162919d78SPeter Wemm #else 188262919d78SPeter Wemm typedef prstatus_t elf_prstatus_t; 188362919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t; 188462919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t; 188562919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t; 188662919d78SPeter Wemm typedef gregset_t elf_gregset_t; 18877f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t; 1888f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK 0 1889f1fca82eSMikolaj Golub typedef struct kinfo_proc elf_kinfo_proc_t; 1890f1fca82eSMikolaj Golub typedef vm_offset_t elf_ps_strings_t; 189162919d78SPeter Wemm #endif 189262919d78SPeter Wemm 18938c64af4fSJohn Polstra static void 1894bd390213SMikolaj Golub __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep) 18958c64af4fSJohn Polstra { 1896c77547d2SJohn Baldwin struct sbuf sbarg; 1897c77547d2SJohn Baldwin size_t len; 1898c77547d2SJohn Baldwin char *cp, *end; 1899247aba24SMarcel Moolenaar struct proc *p; 1900bd390213SMikolaj Golub elf_prpsinfo_t *psinfo; 1901c77547d2SJohn Baldwin int error; 19028c64af4fSJohn Polstra 1903bd390213SMikolaj Golub p = (struct proc *)arg; 1904bd390213SMikolaj Golub if (sb != NULL) { 1905bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*psinfo), ("invalid size")); 1906bd390213SMikolaj Golub psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK); 19078c9b7b2cSMarcel Moolenaar psinfo->pr_version = PRPSINFO_VERSION; 190862919d78SPeter Wemm psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t); 1909ccd3953eSJohn Baldwin strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname)); 1910c77547d2SJohn Baldwin PROC_LOCK(p); 1911c77547d2SJohn Baldwin if (p->p_args != NULL) { 1912c77547d2SJohn Baldwin len = sizeof(psinfo->pr_psargs) - 1; 1913c77547d2SJohn Baldwin if (len > p->p_args->ar_length) 1914c77547d2SJohn Baldwin len = p->p_args->ar_length; 1915c77547d2SJohn Baldwin memcpy(psinfo->pr_psargs, p->p_args->ar_args, len); 1916c77547d2SJohn Baldwin PROC_UNLOCK(p); 1917c77547d2SJohn Baldwin error = 0; 1918c77547d2SJohn Baldwin } else { 1919c77547d2SJohn Baldwin _PHOLD(p); 1920c77547d2SJohn Baldwin PROC_UNLOCK(p); 1921c77547d2SJohn Baldwin sbuf_new(&sbarg, psinfo->pr_psargs, 1922c77547d2SJohn Baldwin sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN); 1923c77547d2SJohn Baldwin error = proc_getargv(curthread, p, &sbarg); 1924c77547d2SJohn Baldwin PRELE(p); 1925c77547d2SJohn Baldwin if (sbuf_finish(&sbarg) == 0) 1926c77547d2SJohn Baldwin len = sbuf_len(&sbarg) - 1; 1927c77547d2SJohn Baldwin else 1928c77547d2SJohn Baldwin len = sizeof(psinfo->pr_psargs) - 1; 1929c77547d2SJohn Baldwin sbuf_delete(&sbarg); 1930c77547d2SJohn Baldwin } 1931c77547d2SJohn Baldwin if (error || len == 0) 1932ccd3953eSJohn Baldwin strlcpy(psinfo->pr_psargs, p->p_comm, 19338c9b7b2cSMarcel Moolenaar sizeof(psinfo->pr_psargs)); 1934c77547d2SJohn Baldwin else { 1935c77547d2SJohn Baldwin KASSERT(len < sizeof(psinfo->pr_psargs), 1936c77547d2SJohn Baldwin ("len is too long: %zu vs %zu", len, 1937c77547d2SJohn Baldwin sizeof(psinfo->pr_psargs))); 1938c77547d2SJohn Baldwin cp = psinfo->pr_psargs; 1939c77547d2SJohn Baldwin end = cp + len - 1; 1940c77547d2SJohn Baldwin for (;;) { 1941c77547d2SJohn Baldwin cp = memchr(cp, '\0', end - cp); 1942c77547d2SJohn Baldwin if (cp == NULL) 1943c77547d2SJohn Baldwin break; 1944c77547d2SJohn Baldwin *cp = ' '; 1945c77547d2SJohn Baldwin } 1946c77547d2SJohn Baldwin } 1947ccb83afdSJohn Baldwin psinfo->pr_pid = p->p_pid; 1948bd390213SMikolaj Golub sbuf_bcat(sb, psinfo, sizeof(*psinfo)); 1949bd390213SMikolaj Golub free(psinfo, M_TEMP); 1950bd390213SMikolaj Golub } 1951bd390213SMikolaj Golub *sizep = sizeof(*psinfo); 1952bd390213SMikolaj Golub } 1953bd390213SMikolaj Golub 1954bd390213SMikolaj Golub static void 1955bd390213SMikolaj Golub __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep) 1956bd390213SMikolaj Golub { 1957bd390213SMikolaj Golub struct thread *td; 1958bd390213SMikolaj Golub elf_prstatus_t *status; 1959bd390213SMikolaj Golub 1960bd390213SMikolaj Golub td = (struct thread *)arg; 1961bd390213SMikolaj Golub if (sb != NULL) { 1962bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*status), ("invalid size")); 1963bd390213SMikolaj Golub status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK); 19648c9b7b2cSMarcel Moolenaar status->pr_version = PRSTATUS_VERSION; 196562919d78SPeter Wemm status->pr_statussz = sizeof(elf_prstatus_t); 196662919d78SPeter Wemm status->pr_gregsetsz = sizeof(elf_gregset_t); 196762919d78SPeter Wemm status->pr_fpregsetsz = sizeof(elf_fpregset_t); 19688c9b7b2cSMarcel Moolenaar status->pr_osreldate = osreldate; 1969bd390213SMikolaj Golub status->pr_cursig = td->td_proc->p_sig; 1970bd390213SMikolaj Golub status->pr_pid = td->td_tid; 1971841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1972bd390213SMikolaj Golub fill_regs32(td, &status->pr_reg); 197362919d78SPeter Wemm #else 1974bd390213SMikolaj Golub fill_regs(td, &status->pr_reg); 197562919d78SPeter Wemm #endif 1976bd390213SMikolaj Golub sbuf_bcat(sb, status, sizeof(*status)); 1977bd390213SMikolaj Golub free(status, M_TEMP); 19788c9b7b2cSMarcel Moolenaar } 1979bd390213SMikolaj Golub *sizep = sizeof(*status); 1980bd390213SMikolaj Golub } 1981bd390213SMikolaj Golub 1982bd390213SMikolaj Golub static void 1983bd390213SMikolaj Golub __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep) 1984bd390213SMikolaj Golub { 1985bd390213SMikolaj Golub struct thread *td; 1986bd390213SMikolaj Golub elf_prfpregset_t *fpregset; 1987bd390213SMikolaj Golub 1988bd390213SMikolaj Golub td = (struct thread *)arg; 1989bd390213SMikolaj Golub if (sb != NULL) { 1990bd390213SMikolaj Golub KASSERT(*sizep == sizeof(*fpregset), ("invalid size")); 1991bd390213SMikolaj Golub fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK); 1992bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1993bd390213SMikolaj Golub fill_fpregs32(td, fpregset); 1994bd390213SMikolaj Golub #else 1995bd390213SMikolaj Golub fill_fpregs(td, fpregset); 1996bd390213SMikolaj Golub #endif 1997bd390213SMikolaj Golub sbuf_bcat(sb, fpregset, sizeof(*fpregset)); 1998bd390213SMikolaj Golub free(fpregset, M_TEMP); 1999bd390213SMikolaj Golub } 2000bd390213SMikolaj Golub *sizep = sizeof(*fpregset); 2001bd390213SMikolaj Golub } 2002bd390213SMikolaj Golub 2003bd390213SMikolaj Golub static void 2004bd390213SMikolaj Golub __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep) 2005bd390213SMikolaj Golub { 2006bd390213SMikolaj Golub struct thread *td; 2007bd390213SMikolaj Golub elf_thrmisc_t thrmisc; 2008bd390213SMikolaj Golub 2009bd390213SMikolaj Golub td = (struct thread *)arg; 2010bd390213SMikolaj Golub if (sb != NULL) { 2011bd390213SMikolaj Golub KASSERT(*sizep == sizeof(thrmisc), ("invalid size")); 2012bd390213SMikolaj Golub bzero(&thrmisc._pad, sizeof(thrmisc._pad)); 2013bd390213SMikolaj Golub strcpy(thrmisc.pr_tname, td->td_name); 2014bd390213SMikolaj Golub sbuf_bcat(sb, &thrmisc, sizeof(thrmisc)); 2015bd390213SMikolaj Golub } 2016bd390213SMikolaj Golub *sizep = sizeof(thrmisc); 2017bd390213SMikolaj Golub } 2018bd390213SMikolaj Golub 20194da47b2fSMarcel Moolenaar /* 20204da47b2fSMarcel Moolenaar * Allow for MD specific notes, as well as any MD 20214da47b2fSMarcel Moolenaar * specific preparations for writing MI notes. 20224da47b2fSMarcel Moolenaar */ 20238c64af4fSJohn Polstra static void 2024bd390213SMikolaj Golub __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep) 20258c64af4fSJohn Polstra { 2026bd390213SMikolaj Golub struct thread *td; 2027bd390213SMikolaj Golub void *buf; 2028bd390213SMikolaj Golub size_t size; 20298c64af4fSJohn Polstra 2030bd390213SMikolaj Golub td = (struct thread *)arg; 2031bd390213SMikolaj Golub size = *sizep; 2032bd390213SMikolaj Golub if (size != 0 && sb != NULL) 2033bd390213SMikolaj Golub buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK); 203483a396ceSChristian Brueffer else 203583a396ceSChristian Brueffer buf = NULL; 2036bd390213SMikolaj Golub size = 0; 2037bd390213SMikolaj Golub __elfN(dump_thread)(td, buf, &size); 203864779280SKonstantin Belousov KASSERT(sb == NULL || *sizep == size, ("invalid size")); 203983a396ceSChristian Brueffer if (size != 0 && sb != NULL) 2040bd390213SMikolaj Golub sbuf_bcat(sb, buf, size); 2041a1761d73SChristian Brueffer free(buf, M_TEMP); 2042bd390213SMikolaj Golub *sizep = size; 20438c64af4fSJohn Polstra } 20448c64af4fSJohn Polstra 2045f1fca82eSMikolaj Golub #ifdef KINFO_PROC_SIZE 2046f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 2047f1fca82eSMikolaj Golub #endif 2048f1fca82eSMikolaj Golub 2049f1fca82eSMikolaj Golub static void 2050f1fca82eSMikolaj Golub __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep) 2051f1fca82eSMikolaj Golub { 2052f1fca82eSMikolaj Golub struct proc *p; 2053f1fca82eSMikolaj Golub size_t size; 2054f1fca82eSMikolaj Golub int structsize; 2055f1fca82eSMikolaj Golub 2056f1fca82eSMikolaj Golub p = (struct proc *)arg; 2057f1fca82eSMikolaj Golub size = sizeof(structsize) + p->p_numthreads * 2058f1fca82eSMikolaj Golub sizeof(elf_kinfo_proc_t); 2059f1fca82eSMikolaj Golub 2060f1fca82eSMikolaj Golub if (sb != NULL) { 2061f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2062f1fca82eSMikolaj Golub structsize = sizeof(elf_kinfo_proc_t); 2063f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 20646662ce5aSMateusz Guzik sx_slock(&proctree_lock); 2065f1fca82eSMikolaj Golub PROC_LOCK(p); 2066f1fca82eSMikolaj Golub kern_proc_out(p, sb, ELF_KERN_PROC_MASK); 20676662ce5aSMateusz Guzik sx_sunlock(&proctree_lock); 2068f1fca82eSMikolaj Golub } 2069f1fca82eSMikolaj Golub *sizep = size; 2070f1fca82eSMikolaj Golub } 2071f1fca82eSMikolaj Golub 2072f1fca82eSMikolaj Golub #ifdef KINFO_FILE_SIZE 2073f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE); 2074f1fca82eSMikolaj Golub #endif 2075f1fca82eSMikolaj Golub 2076f1fca82eSMikolaj Golub static void 2077f1fca82eSMikolaj Golub note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep) 2078f1fca82eSMikolaj Golub { 2079f1fca82eSMikolaj Golub struct proc *p; 208014bdbaf2SConrad Meyer size_t size, sect_sz, i; 208114bdbaf2SConrad Meyer ssize_t start_len, sect_len; 208214bdbaf2SConrad Meyer int structsize, filedesc_flags; 208314bdbaf2SConrad Meyer 2084bcb60d52SConrad Meyer if (coredump_pack_fileinfo) 208514bdbaf2SConrad Meyer filedesc_flags = KERN_FILEDESC_PACK_KINFO; 208614bdbaf2SConrad Meyer else 208714bdbaf2SConrad Meyer filedesc_flags = 0; 2088f1fca82eSMikolaj Golub 2089f1fca82eSMikolaj Golub p = (struct proc *)arg; 209014bdbaf2SConrad Meyer structsize = sizeof(struct kinfo_file); 2091f1fca82eSMikolaj Golub if (sb == NULL) { 2092f1fca82eSMikolaj Golub size = 0; 2093f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 2094f1fca82eSMikolaj Golub sbuf_set_drain(sb, sbuf_drain_count, &size); 2095f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2096f1fca82eSMikolaj Golub PROC_LOCK(p); 209714bdbaf2SConrad Meyer kern_proc_filedesc_out(p, sb, -1, filedesc_flags); 2098f1fca82eSMikolaj Golub sbuf_finish(sb); 2099f1fca82eSMikolaj Golub sbuf_delete(sb); 2100f1fca82eSMikolaj Golub *sizep = size; 2101f1fca82eSMikolaj Golub } else { 210214bdbaf2SConrad Meyer sbuf_start_section(sb, &start_len); 210314bdbaf2SConrad Meyer 2104f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2105f1fca82eSMikolaj Golub PROC_LOCK(p); 210614bdbaf2SConrad Meyer kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize), 210714bdbaf2SConrad Meyer filedesc_flags); 210814bdbaf2SConrad Meyer 210914bdbaf2SConrad Meyer sect_len = sbuf_end_section(sb, start_len, 0, 0); 211014bdbaf2SConrad Meyer if (sect_len < 0) 211114bdbaf2SConrad Meyer return; 211214bdbaf2SConrad Meyer sect_sz = sect_len; 211314bdbaf2SConrad Meyer 211414bdbaf2SConrad Meyer KASSERT(sect_sz <= *sizep, 211514bdbaf2SConrad Meyer ("kern_proc_filedesc_out did not respect maxlen; " 211614bdbaf2SConrad Meyer "requested %zu, got %zu", *sizep - sizeof(structsize), 211714bdbaf2SConrad Meyer sect_sz - sizeof(structsize))); 211814bdbaf2SConrad Meyer 211914bdbaf2SConrad Meyer for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++) 212014bdbaf2SConrad Meyer sbuf_putc(sb, 0); 2121f1fca82eSMikolaj Golub } 2122f1fca82eSMikolaj Golub } 2123f1fca82eSMikolaj Golub 2124f1fca82eSMikolaj Golub #ifdef KINFO_VMENTRY_SIZE 2125f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 2126f1fca82eSMikolaj Golub #endif 2127f1fca82eSMikolaj Golub 2128f1fca82eSMikolaj Golub static void 2129f1fca82eSMikolaj Golub note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep) 2130f1fca82eSMikolaj Golub { 2131f1fca82eSMikolaj Golub struct proc *p; 2132f1fca82eSMikolaj Golub size_t size; 2133e6b95927SConrad Meyer int structsize, vmmap_flags; 2134e6b95927SConrad Meyer 2135e6b95927SConrad Meyer if (coredump_pack_vmmapinfo) 2136e6b95927SConrad Meyer vmmap_flags = KERN_VMMAP_PACK_KINFO; 2137e6b95927SConrad Meyer else 2138e6b95927SConrad Meyer vmmap_flags = 0; 2139f1fca82eSMikolaj Golub 2140f1fca82eSMikolaj Golub p = (struct proc *)arg; 2141e6b95927SConrad Meyer structsize = sizeof(struct kinfo_vmentry); 2142f1fca82eSMikolaj Golub if (sb == NULL) { 2143f1fca82eSMikolaj Golub size = 0; 2144f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 2145f1fca82eSMikolaj Golub sbuf_set_drain(sb, sbuf_drain_count, &size); 2146f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2147f1fca82eSMikolaj Golub PROC_LOCK(p); 2148e6b95927SConrad Meyer kern_proc_vmmap_out(p, sb, -1, vmmap_flags); 2149f1fca82eSMikolaj Golub sbuf_finish(sb); 2150f1fca82eSMikolaj Golub sbuf_delete(sb); 2151f1fca82eSMikolaj Golub *sizep = size; 2152f1fca82eSMikolaj Golub } else { 2153f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2154f1fca82eSMikolaj Golub PROC_LOCK(p); 2155e6b95927SConrad Meyer kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize), 2156e6b95927SConrad Meyer vmmap_flags); 2157f1fca82eSMikolaj Golub } 2158f1fca82eSMikolaj Golub } 2159f1fca82eSMikolaj Golub 2160f1fca82eSMikolaj Golub static void 2161f1fca82eSMikolaj Golub note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep) 2162f1fca82eSMikolaj Golub { 2163f1fca82eSMikolaj Golub struct proc *p; 2164f1fca82eSMikolaj Golub size_t size; 2165f1fca82eSMikolaj Golub int structsize; 2166f1fca82eSMikolaj Golub 2167f1fca82eSMikolaj Golub p = (struct proc *)arg; 2168f1fca82eSMikolaj Golub size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t); 2169f1fca82eSMikolaj Golub if (sb != NULL) { 2170f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2171f1fca82eSMikolaj Golub structsize = sizeof(gid_t); 2172f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2173f1fca82eSMikolaj Golub sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups * 2174f1fca82eSMikolaj Golub sizeof(gid_t)); 2175f1fca82eSMikolaj Golub } 2176f1fca82eSMikolaj Golub *sizep = size; 2177f1fca82eSMikolaj Golub } 2178f1fca82eSMikolaj Golub 2179f1fca82eSMikolaj Golub static void 2180f1fca82eSMikolaj Golub note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep) 2181f1fca82eSMikolaj Golub { 2182f1fca82eSMikolaj Golub struct proc *p; 2183f1fca82eSMikolaj Golub size_t size; 2184f1fca82eSMikolaj Golub int structsize; 2185f1fca82eSMikolaj Golub 2186f1fca82eSMikolaj Golub p = (struct proc *)arg; 2187f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask); 2188f1fca82eSMikolaj Golub if (sb != NULL) { 2189f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2190f1fca82eSMikolaj Golub structsize = sizeof(p->p_fd->fd_cmask); 2191f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2192f1fca82eSMikolaj Golub sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask)); 2193f1fca82eSMikolaj Golub } 2194f1fca82eSMikolaj Golub *sizep = size; 2195f1fca82eSMikolaj Golub } 2196f1fca82eSMikolaj Golub 2197f1fca82eSMikolaj Golub static void 2198f1fca82eSMikolaj Golub note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep) 2199f1fca82eSMikolaj Golub { 2200f1fca82eSMikolaj Golub struct proc *p; 2201f1fca82eSMikolaj Golub struct rlimit rlim[RLIM_NLIMITS]; 2202f1fca82eSMikolaj Golub size_t size; 2203f1fca82eSMikolaj Golub int structsize, i; 2204f1fca82eSMikolaj Golub 2205f1fca82eSMikolaj Golub p = (struct proc *)arg; 2206f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(rlim); 2207f1fca82eSMikolaj Golub if (sb != NULL) { 2208f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2209f1fca82eSMikolaj Golub structsize = sizeof(rlim); 2210f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2211f1fca82eSMikolaj Golub PROC_LOCK(p); 2212f1fca82eSMikolaj Golub for (i = 0; i < RLIM_NLIMITS; i++) 2213f6f6d240SMateusz Guzik lim_rlimit_proc(p, i, &rlim[i]); 2214f1fca82eSMikolaj Golub PROC_UNLOCK(p); 2215f1fca82eSMikolaj Golub sbuf_bcat(sb, rlim, sizeof(rlim)); 2216f1fca82eSMikolaj Golub } 2217f1fca82eSMikolaj Golub *sizep = size; 2218f1fca82eSMikolaj Golub } 2219f1fca82eSMikolaj Golub 2220f1fca82eSMikolaj Golub static void 2221f1fca82eSMikolaj Golub note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep) 2222f1fca82eSMikolaj Golub { 2223f1fca82eSMikolaj Golub struct proc *p; 2224f1fca82eSMikolaj Golub size_t size; 2225f1fca82eSMikolaj Golub int structsize; 2226f1fca82eSMikolaj Golub 2227f1fca82eSMikolaj Golub p = (struct proc *)arg; 2228f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(p->p_osrel); 2229f1fca82eSMikolaj Golub if (sb != NULL) { 2230f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2231f1fca82eSMikolaj Golub structsize = sizeof(p->p_osrel); 2232f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2233f1fca82eSMikolaj Golub sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel)); 2234f1fca82eSMikolaj Golub } 2235f1fca82eSMikolaj Golub *sizep = size; 2236f1fca82eSMikolaj Golub } 2237f1fca82eSMikolaj Golub 2238f1fca82eSMikolaj Golub static void 2239f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep) 2240f1fca82eSMikolaj Golub { 2241f1fca82eSMikolaj Golub struct proc *p; 2242f1fca82eSMikolaj Golub elf_ps_strings_t ps_strings; 2243f1fca82eSMikolaj Golub size_t size; 2244f1fca82eSMikolaj Golub int structsize; 2245f1fca82eSMikolaj Golub 2246f1fca82eSMikolaj Golub p = (struct proc *)arg; 2247f1fca82eSMikolaj Golub size = sizeof(structsize) + sizeof(ps_strings); 2248f1fca82eSMikolaj Golub if (sb != NULL) { 2249f1fca82eSMikolaj Golub KASSERT(*sizep == size, ("invalid size")); 2250f1fca82eSMikolaj Golub structsize = sizeof(ps_strings); 2251f1fca82eSMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 2252f1fca82eSMikolaj Golub ps_strings = PTROUT(p->p_sysent->sv_psstrings); 2253f1fca82eSMikolaj Golub #else 2254f1fca82eSMikolaj Golub ps_strings = p->p_sysent->sv_psstrings; 2255f1fca82eSMikolaj Golub #endif 2256f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2257f1fca82eSMikolaj Golub sbuf_bcat(sb, &ps_strings, sizeof(ps_strings)); 2258f1fca82eSMikolaj Golub } 2259f1fca82eSMikolaj Golub *sizep = size; 2260f1fca82eSMikolaj Golub } 2261f1fca82eSMikolaj Golub 2262f1fca82eSMikolaj Golub static void 2263f1fca82eSMikolaj Golub __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep) 2264f1fca82eSMikolaj Golub { 2265f1fca82eSMikolaj Golub struct proc *p; 2266f1fca82eSMikolaj Golub size_t size; 2267f1fca82eSMikolaj Golub int structsize; 2268f1fca82eSMikolaj Golub 2269f1fca82eSMikolaj Golub p = (struct proc *)arg; 2270f1fca82eSMikolaj Golub if (sb == NULL) { 2271f1fca82eSMikolaj Golub size = 0; 2272f1fca82eSMikolaj Golub sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN); 2273f1fca82eSMikolaj Golub sbuf_set_drain(sb, sbuf_drain_count, &size); 2274f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2275f1fca82eSMikolaj Golub PHOLD(p); 2276f1fca82eSMikolaj Golub proc_getauxv(curthread, p, sb); 2277f1fca82eSMikolaj Golub PRELE(p); 2278f1fca82eSMikolaj Golub sbuf_finish(sb); 2279f1fca82eSMikolaj Golub sbuf_delete(sb); 2280f1fca82eSMikolaj Golub *sizep = size; 2281f1fca82eSMikolaj Golub } else { 2282f1fca82eSMikolaj Golub structsize = sizeof(Elf_Auxinfo); 2283f1fca82eSMikolaj Golub sbuf_bcat(sb, &structsize, sizeof(structsize)); 2284f1fca82eSMikolaj Golub PHOLD(p); 2285f1fca82eSMikolaj Golub proc_getauxv(curthread, p, sb); 2286f1fca82eSMikolaj Golub PRELE(p); 2287f1fca82eSMikolaj Golub } 2288f1fca82eSMikolaj Golub } 2289f1fca82eSMikolaj Golub 229032c01de2SDmitry Chagin static boolean_t 22911a9c7decSKonstantin Belousov __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote, 22921a9c7decSKonstantin Belousov int32_t *osrel, const Elf_Phdr *pnote) 229332c01de2SDmitry Chagin { 2294267c52fcSKonstantin Belousov const Elf_Note *note, *note0, *note_end; 229532c01de2SDmitry Chagin const char *note_name; 22966c775eb6SKonstantin Belousov char *buf; 22976c775eb6SKonstantin Belousov int i, error; 22986c775eb6SKonstantin Belousov boolean_t res; 229932c01de2SDmitry Chagin 23006c775eb6SKonstantin Belousov /* We need some limit, might as well use PAGE_SIZE. */ 23016c775eb6SKonstantin Belousov if (pnote == NULL || pnote->p_filesz > PAGE_SIZE) 230232c01de2SDmitry Chagin return (FALSE); 23036c775eb6SKonstantin Belousov ASSERT_VOP_LOCKED(imgp->vp, "parse_notes"); 23046c775eb6SKonstantin Belousov if (pnote->p_offset > PAGE_SIZE || 23056c775eb6SKonstantin Belousov pnote->p_filesz > PAGE_SIZE - pnote->p_offset) { 23066c775eb6SKonstantin Belousov VOP_UNLOCK(imgp->vp, 0); 23076c775eb6SKonstantin Belousov buf = malloc(pnote->p_filesz, M_TEMP, M_WAITOK); 23086c775eb6SKonstantin Belousov vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 23096c775eb6SKonstantin Belousov error = vn_rdwr(UIO_READ, imgp->vp, buf, pnote->p_filesz, 23106c775eb6SKonstantin Belousov pnote->p_offset, UIO_SYSSPACE, IO_NODELOCKED, 23116c775eb6SKonstantin Belousov curthread->td_ucred, NOCRED, NULL, curthread); 23126c775eb6SKonstantin Belousov if (error != 0) { 23136c775eb6SKonstantin Belousov uprintf("i/o error PT_NOTE\n"); 23146c775eb6SKonstantin Belousov res = FALSE; 23156c775eb6SKonstantin Belousov goto ret; 23166c775eb6SKonstantin Belousov } 23176c775eb6SKonstantin Belousov note = note0 = (const Elf_Note *)buf; 23186c775eb6SKonstantin Belousov note_end = (const Elf_Note *)(buf + pnote->p_filesz); 23196c775eb6SKonstantin Belousov } else { 23206c775eb6SKonstantin Belousov note = note0 = (const Elf_Note *)(imgp->image_header + 23216c775eb6SKonstantin Belousov pnote->p_offset); 232232c01de2SDmitry Chagin note_end = (const Elf_Note *)(imgp->image_header + 232332c01de2SDmitry Chagin pnote->p_offset + pnote->p_filesz); 23246c775eb6SKonstantin Belousov buf = NULL; 23256c775eb6SKonstantin Belousov } 2326267c52fcSKonstantin Belousov for (i = 0; i < 100 && note >= note0 && note < note_end; i++) { 2327d1ae5c83SKonstantin Belousov if (!aligned(note, Elf32_Addr) || (const char *)note_end - 23286c775eb6SKonstantin Belousov (const char *)note < sizeof(Elf_Note)) { 23296c775eb6SKonstantin Belousov res = FALSE; 23306c775eb6SKonstantin Belousov goto ret; 23316c775eb6SKonstantin Belousov } 233232c01de2SDmitry Chagin if (note->n_namesz != checknote->hdr.n_namesz || 233332c01de2SDmitry Chagin note->n_descsz != checknote->hdr.n_descsz || 233432c01de2SDmitry Chagin note->n_type != checknote->hdr.n_type) 233532c01de2SDmitry Chagin goto nextnote; 233632c01de2SDmitry Chagin note_name = (const char *)(note + 1); 2337d1ae5c83SKonstantin Belousov if (note_name + checknote->hdr.n_namesz >= 2338d1ae5c83SKonstantin Belousov (const char *)note_end || strncmp(checknote->vendor, 2339d1ae5c83SKonstantin Belousov note_name, checknote->hdr.n_namesz) != 0) 234032c01de2SDmitry Chagin goto nextnote; 234132c01de2SDmitry Chagin 234232c01de2SDmitry Chagin /* 234332c01de2SDmitry Chagin * Fetch the osreldate for binary 234432c01de2SDmitry Chagin * from the ELF OSABI-note if necessary. 234532c01de2SDmitry Chagin */ 234689ffc202SBjoern A. Zeeb if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 && 23476c775eb6SKonstantin Belousov checknote->trans_osrel != NULL) { 23486c775eb6SKonstantin Belousov res = checknote->trans_osrel(note, osrel); 23496c775eb6SKonstantin Belousov goto ret; 23506c775eb6SKonstantin Belousov } 23516c775eb6SKonstantin Belousov res = TRUE; 23526c775eb6SKonstantin Belousov goto ret; 235332c01de2SDmitry Chagin nextnote: 235432c01de2SDmitry Chagin note = (const Elf_Note *)((const char *)(note + 1) + 23551b8388cdSMikolaj Golub roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) + 23561b8388cdSMikolaj Golub roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE)); 235732c01de2SDmitry Chagin } 23586c775eb6SKonstantin Belousov res = FALSE; 23596c775eb6SKonstantin Belousov ret: 23606c775eb6SKonstantin Belousov free(buf, M_TEMP); 23616c775eb6SKonstantin Belousov return (res); 236232c01de2SDmitry Chagin } 236332c01de2SDmitry Chagin 236432c01de2SDmitry Chagin /* 23651a9c7decSKonstantin Belousov * Try to find the appropriate ABI-note section for checknote, 23661a9c7decSKonstantin Belousov * fetch the osreldate for binary from the ELF OSABI-note. Only the 23671a9c7decSKonstantin Belousov * first page of the image is searched, the same as for headers. 23681a9c7decSKonstantin Belousov */ 23691a9c7decSKonstantin Belousov static boolean_t 23701a9c7decSKonstantin Belousov __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote, 23711a9c7decSKonstantin Belousov int32_t *osrel) 23721a9c7decSKonstantin Belousov { 23731a9c7decSKonstantin Belousov const Elf_Phdr *phdr; 23741a9c7decSKonstantin Belousov const Elf_Ehdr *hdr; 23751a9c7decSKonstantin Belousov int i; 23761a9c7decSKonstantin Belousov 23771a9c7decSKonstantin Belousov hdr = (const Elf_Ehdr *)imgp->image_header; 23781a9c7decSKonstantin Belousov phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 23791a9c7decSKonstantin Belousov 23801a9c7decSKonstantin Belousov for (i = 0; i < hdr->e_phnum; i++) { 23811a9c7decSKonstantin Belousov if (phdr[i].p_type == PT_NOTE && 23821a9c7decSKonstantin Belousov __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i])) 23831a9c7decSKonstantin Belousov return (TRUE); 23841a9c7decSKonstantin Belousov } 23851a9c7decSKonstantin Belousov return (FALSE); 23861a9c7decSKonstantin Belousov 23871a9c7decSKonstantin Belousov } 23881a9c7decSKonstantin Belousov 23891a9c7decSKonstantin Belousov /* 2390e1743d02SSøren Schmidt * Tell kern_execve.c about it, with a little help from the linker. 2391e1743d02SSøren Schmidt */ 2392a360a43dSJake Burkholder static struct execsw __elfN(execsw) = { 2393a360a43dSJake Burkholder __CONCAT(exec_, __elfN(imgact)), 2394a360a43dSJake Burkholder __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 2395a360a43dSJake Burkholder }; 2396a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw)); 2397e7228204SAlfred Perlstein 2398ed167eaaSKonstantin Belousov static vm_prot_t 2399ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags) 2400ed167eaaSKonstantin Belousov { 2401ed167eaaSKonstantin Belousov vm_prot_t prot; 2402ed167eaaSKonstantin Belousov 2403ed167eaaSKonstantin Belousov prot = 0; 2404ed167eaaSKonstantin Belousov if (flags & PF_X) 2405ed167eaaSKonstantin Belousov prot |= VM_PROT_EXECUTE; 2406ed167eaaSKonstantin Belousov if (flags & PF_W) 2407ed167eaaSKonstantin Belousov prot |= VM_PROT_WRITE; 2408ed167eaaSKonstantin Belousov if (flags & PF_R) 2409ed167eaaSKonstantin Belousov prot |= VM_PROT_READ; 2410676eda08SMarcel Moolenaar #if __ELF_WORD_SIZE == 32 2411e7d939bdSMarcel Moolenaar #if defined(__amd64__) 2412126b36a2SKonstantin Belousov if (i386_read_exec && (flags & PF_R)) 2413676eda08SMarcel Moolenaar prot |= VM_PROT_EXECUTE; 2414676eda08SMarcel Moolenaar #endif 2415676eda08SMarcel Moolenaar #endif 2416ed167eaaSKonstantin Belousov return (prot); 2417ed167eaaSKonstantin Belousov } 2418ed167eaaSKonstantin Belousov 2419ed167eaaSKonstantin Belousov static Elf_Word 2420ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot) 2421ed167eaaSKonstantin Belousov { 2422ed167eaaSKonstantin Belousov Elf_Word flags; 2423ed167eaaSKonstantin Belousov 2424ed167eaaSKonstantin Belousov flags = 0; 2425ed167eaaSKonstantin Belousov if (prot & VM_PROT_EXECUTE) 2426ed167eaaSKonstantin Belousov flags |= PF_X; 2427ed167eaaSKonstantin Belousov if (prot & VM_PROT_READ) 2428ed167eaaSKonstantin Belousov flags |= PF_R; 2429ed167eaaSKonstantin Belousov if (prot & VM_PROT_WRITE) 2430ed167eaaSKonstantin Belousov flags |= PF_W; 2431ed167eaaSKonstantin Belousov return (flags); 2432ed167eaaSKonstantin Belousov } 2433