1e1743d02SSøren Schmidt /*- 221a3ee0eSDavid E. O'Brien * Copyright (c) 2000 David O'Brien 3e1743d02SSøren Schmidt * 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 3462919d78SPeter Wemm #include "opt_compat.h" 35e7228204SAlfred Perlstein #include "opt_core.h" 3662919d78SPeter Wemm 37e1743d02SSøren Schmidt #include <sys/param.h> 38e1743d02SSøren Schmidt #include <sys/exec.h> 398c64af4fSJohn Polstra #include <sys/fcntl.h> 40e1743d02SSøren Schmidt #include <sys/imgact.h> 41e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 42e1743d02SSøren Schmidt #include <sys/kernel.h> 43f34fa851SJohn Baldwin #include <sys/lock.h> 44e1743d02SSøren Schmidt #include <sys/malloc.h> 4568ff2a43SChristian S.J. Peron #include <sys/mount.h> 4635e0e5b3SJohn Baldwin #include <sys/mutex.h> 478c64af4fSJohn Polstra #include <sys/mman.h> 48a794e791SBruce Evans #include <sys/namei.h> 498c64af4fSJohn Polstra #include <sys/pioctl.h> 50a794e791SBruce Evans #include <sys/proc.h> 518c64af4fSJohn Polstra #include <sys/procfs.h> 528c64af4fSJohn Polstra #include <sys/resourcevar.h> 53da61b9a6SAlan Cox #include <sys/sf_buf.h> 54ee235befSKonstantin Belousov #include <sys/smp.h> 5536240ea5SDoug Rabson #include <sys/systm.h> 56e1743d02SSøren Schmidt #include <sys/signalvar.h> 578c64af4fSJohn Polstra #include <sys/stat.h> 581005a129SJohn Baldwin #include <sys/sx.h> 598c64af4fSJohn Polstra #include <sys/syscall.h> 60e1743d02SSøren Schmidt #include <sys/sysctl.h> 618c64af4fSJohn Polstra #include <sys/sysent.h> 62a794e791SBruce Evans #include <sys/vnode.h> 63e7228204SAlfred Perlstein #include <sys/syslog.h> 64e7228204SAlfred Perlstein #include <sys/eventhandler.h> 65e7228204SAlfred Perlstein 66e7228204SAlfred Perlstein #include <net/zlib.h> 67e1743d02SSøren Schmidt 68e1743d02SSøren Schmidt #include <vm/vm.h> 69e1743d02SSøren Schmidt #include <vm/vm_kern.h> 70e1743d02SSøren Schmidt #include <vm/vm_param.h> 71e1743d02SSøren Schmidt #include <vm/pmap.h> 72e1743d02SSøren Schmidt #include <vm/vm_map.h> 730ff27d31SJohn Polstra #include <vm/vm_object.h> 74e1743d02SSøren Schmidt #include <vm/vm_extern.h> 75e1743d02SSøren Schmidt 7652c24af7SPeter Wemm #include <machine/elf.h> 77e1743d02SSøren Schmidt #include <machine/md_var.h> 78e1743d02SSøren Schmidt 79c815a20cSDavid E. O'Brien #define OLD_EI_BRAND 8 80c815a20cSDavid E. O'Brien 813ebc1248SPeter Wemm static int __elfN(check_header)(const Elf_Ehdr *hdr); 8232c01de2SDmitry Chagin static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp, 8332c01de2SDmitry Chagin const char *interp, int32_t *osrel); 843ebc1248SPeter Wemm static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 853ebc1248SPeter Wemm u_long *entry, size_t pagesize); 86373d1a3fSAlan Cox static int __elfN(load_section)(struct vmspace *vmspace, vm_object_t object, 878c64af4fSJohn Polstra vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, 883ebc1248SPeter Wemm vm_prot_t prot, size_t pagesize); 893ebc1248SPeter Wemm static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp); 9089ffc202SBjoern A. Zeeb static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note, 9189ffc202SBjoern A. Zeeb int32_t *osrel); 9289ffc202SBjoern A. Zeeb static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel); 9332c01de2SDmitry Chagin static boolean_t __elfN(check_note)(struct image_params *imgp, 9432c01de2SDmitry Chagin Elf_Brandnote *checknote, int32_t *osrel); 95*ed167eaaSKonstantin Belousov static vm_prot_t __elfN(trans_prot)(Elf_Word); 96*ed167eaaSKonstantin Belousov static Elf_Word __elfN(untrans_prot)(vm_prot_t); 97e1743d02SSøren Schmidt 98a360a43dSJake Burkholder SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0, 99a360a43dSJake Burkholder ""); 100a360a43dSJake Burkholder 101e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 102e7228204SAlfred Perlstein static int compress_core(gzFile, char *, char *, unsigned int, 103e7228204SAlfred Perlstein struct thread * td); 104e7228204SAlfred Perlstein #define CORE_BUF_SIZE (16 * 1024) 105e7228204SAlfred Perlstein #endif 106e7228204SAlfred Perlstein 107e548a1d4SJake Burkholder int __elfN(fallback_brand) = -1; 108e548a1d4SJake Burkholder SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 109e548a1d4SJake Burkholder fallback_brand, CTLFLAG_RW, &__elfN(fallback_brand), 0, 110a360a43dSJake Burkholder __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort"); 111a360a43dSJake Burkholder TUNABLE_INT("kern.elf" __XSTRING(__ELF_WORD_SIZE) ".fallback_brand", 112e548a1d4SJake Burkholder &__elfN(fallback_brand)); 113a360a43dSJake Burkholder 114551d79e1SMarcel Moolenaar static int elf_legacy_coredump = 0; 115a360a43dSJake Burkholder SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW, 116551d79e1SMarcel Moolenaar &elf_legacy_coredump, 0, ""); 117e1743d02SSøren Schmidt 1183ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS]; 119e1743d02SSøren Schmidt 12093d1c728SKonstantin Belousov #define trunc_page_ps(va, ps) ((va) & ~(ps - 1)) 12193d1c728SKonstantin Belousov #define round_page_ps(va, ps) (((va) + (ps - 1)) & ~(ps - 1)) 12293d1c728SKonstantin Belousov #define aligned(a, t) (trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a)) 12393d1c728SKonstantin Belousov 12432c01de2SDmitry Chagin static const char FREEBSD_ABI_VENDOR[] = "FreeBSD"; 12532c01de2SDmitry Chagin 12632c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = { 12732c01de2SDmitry Chagin .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR), 12832c01de2SDmitry Chagin .hdr.n_descsz = sizeof(int32_t), 12932c01de2SDmitry Chagin .hdr.n_type = 1, 13032c01de2SDmitry Chagin .vendor = FREEBSD_ABI_VENDOR, 13189ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 13289ffc202SBjoern A. Zeeb .trans_osrel = __elfN(freebsd_trans_osrel) 13332c01de2SDmitry Chagin }; 13432c01de2SDmitry Chagin 13589ffc202SBjoern A. Zeeb static boolean_t 13689ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel) 13789ffc202SBjoern A. Zeeb { 13889ffc202SBjoern A. Zeeb uintptr_t p; 13989ffc202SBjoern A. Zeeb 14089ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 14189ffc202SBjoern A. Zeeb p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 14289ffc202SBjoern A. Zeeb *osrel = *(const int32_t *)(p); 14389ffc202SBjoern A. Zeeb 14489ffc202SBjoern A. Zeeb return (TRUE); 14589ffc202SBjoern A. Zeeb } 14689ffc202SBjoern A. Zeeb 14789ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU"; 14889ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3; 14989ffc202SBjoern A. Zeeb 15089ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = { 15189ffc202SBjoern A. Zeeb .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 15289ffc202SBjoern A. Zeeb .hdr.n_descsz = 16, /* XXX at least 16 */ 15389ffc202SBjoern A. Zeeb .hdr.n_type = 1, 15489ffc202SBjoern A. Zeeb .vendor = GNU_ABI_VENDOR, 15589ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 15689ffc202SBjoern A. Zeeb .trans_osrel = kfreebsd_trans_osrel 15789ffc202SBjoern A. Zeeb }; 15889ffc202SBjoern A. Zeeb 15989ffc202SBjoern A. Zeeb static boolean_t 16089ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel) 16189ffc202SBjoern A. Zeeb { 16289ffc202SBjoern A. Zeeb const Elf32_Word *desc; 16389ffc202SBjoern A. Zeeb uintptr_t p; 16489ffc202SBjoern A. Zeeb 16589ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 16689ffc202SBjoern A. Zeeb p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 16789ffc202SBjoern A. Zeeb 16889ffc202SBjoern A. Zeeb desc = (const Elf32_Word *)p; 16989ffc202SBjoern A. Zeeb if (desc[0] != GNU_KFREEBSD_ABI_DESC) 17089ffc202SBjoern A. Zeeb return (FALSE); 17189ffc202SBjoern A. Zeeb 17289ffc202SBjoern A. Zeeb /* 17389ffc202SBjoern A. Zeeb * Debian GNU/kFreeBSD embed the earliest compatible kernel version 17489ffc202SBjoern A. Zeeb * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way. 17589ffc202SBjoern A. Zeeb */ 17689ffc202SBjoern A. Zeeb *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3]; 17789ffc202SBjoern A. Zeeb 17889ffc202SBjoern A. Zeeb return (TRUE); 17989ffc202SBjoern A. Zeeb } 18089ffc202SBjoern A. Zeeb 181e1743d02SSøren Schmidt int 1823ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry) 183e1743d02SSøren Schmidt { 184e1743d02SSøren Schmidt int i; 185e1743d02SSøren Schmidt 1863ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 187ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == NULL) { 188ea5a2b2eSSøren Schmidt elf_brand_list[i] = entry; 189e1743d02SSøren Schmidt break; 190e1743d02SSøren Schmidt } 191e1743d02SSøren Schmidt } 192925c8b5bSBjoern A. Zeeb if (i == MAX_BRANDS) { 193925c8b5bSBjoern A. Zeeb printf("WARNING: %s: could not insert brandinfo entry: %p\n", 194925c8b5bSBjoern A. Zeeb __func__, entry); 195a7cddfedSJake Burkholder return (-1); 196925c8b5bSBjoern A. Zeeb } 197a7cddfedSJake Burkholder return (0); 198e1743d02SSøren Schmidt } 199e1743d02SSøren Schmidt 200e1743d02SSøren Schmidt int 2013ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry) 202e1743d02SSøren Schmidt { 203e1743d02SSøren Schmidt int i; 204e1743d02SSøren Schmidt 2053ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 206ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == entry) { 207ea5a2b2eSSøren Schmidt elf_brand_list[i] = NULL; 208e1743d02SSøren Schmidt break; 209e1743d02SSøren Schmidt } 210e1743d02SSøren Schmidt } 211ea5a2b2eSSøren Schmidt if (i == MAX_BRANDS) 212a7cddfedSJake Burkholder return (-1); 213a7cddfedSJake Burkholder return (0); 214e1743d02SSøren Schmidt } 215e1743d02SSøren Schmidt 216096977faSMark Newton int 2173ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry) 218096977faSMark Newton { 219096977faSMark Newton struct proc *p; 220553629ebSJake Burkholder int rval = FALSE; 221096977faSMark Newton 2221005a129SJohn Baldwin sx_slock(&allproc_lock); 2234f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 224553629ebSJake Burkholder if (p->p_sysent == entry->sysvec) { 225553629ebSJake Burkholder rval = TRUE; 226553629ebSJake Burkholder break; 227096977faSMark Newton } 228553629ebSJake Burkholder } 2291005a129SJohn Baldwin sx_sunlock(&allproc_lock); 230096977faSMark Newton 231553629ebSJake Burkholder return (rval); 232096977faSMark Newton } 233096977faSMark Newton 2345fe3ed62SJake Burkholder static Elf_Brandinfo * 23532c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, 23632c01de2SDmitry Chagin int32_t *osrel) 2375fe3ed62SJake Burkholder { 23832c01de2SDmitry Chagin const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 2395fe3ed62SJake Burkholder Elf_Brandinfo *bi; 24032c01de2SDmitry Chagin boolean_t ret; 2415fe3ed62SJake Burkholder int i; 2425fe3ed62SJake Burkholder 2435fe3ed62SJake Burkholder /* 24432c01de2SDmitry Chagin * We support four types of branding -- (1) the ELF EI_OSABI field 2455fe3ed62SJake Burkholder * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string 24632c01de2SDmitry Chagin * branding w/in the ELF header, (3) path of the `interp_path' 24732c01de2SDmitry Chagin * field, and (4) the ".note.ABI-tag" ELF section. 2485fe3ed62SJake Burkholder */ 2495fe3ed62SJake Burkholder 25032c01de2SDmitry Chagin /* Look for an ".note.ABI-tag" ELF section */ 25132c01de2SDmitry Chagin for (i = 0; i < MAX_BRANDS; i++) { 25232c01de2SDmitry Chagin bi = elf_brand_list[i]; 253ecc2fda8SBjoern A. Zeeb if (bi == NULL) 254ecc2fda8SBjoern A. Zeeb continue; 255ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && (bi->flags & 256ecc2fda8SBjoern A. Zeeb (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) { 25732c01de2SDmitry Chagin ret = __elfN(check_note)(imgp, bi->brand_note, osrel); 25832c01de2SDmitry Chagin if (ret) 25932c01de2SDmitry Chagin return (bi); 26032c01de2SDmitry Chagin } 26132c01de2SDmitry Chagin } 26232c01de2SDmitry Chagin 2635fe3ed62SJake Burkholder /* If the executable has a brand, search for it in the brand list. */ 2645fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 2655fe3ed62SJake Burkholder bi = elf_brand_list[i]; 266ecc2fda8SBjoern A. Zeeb if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 267ecc2fda8SBjoern A. Zeeb continue; 268ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 2695fe3ed62SJake Burkholder (hdr->e_ident[EI_OSABI] == bi->brand || 2705fe3ed62SJake Burkholder strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND], 2715fe3ed62SJake Burkholder bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0)) 2725fe3ed62SJake Burkholder return (bi); 2735fe3ed62SJake Burkholder } 2745fe3ed62SJake Burkholder 2755fe3ed62SJake Burkholder /* Lacking a known brand, search for a recognized interpreter. */ 2765fe3ed62SJake Burkholder if (interp != NULL) { 2775fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 2785fe3ed62SJake Burkholder bi = elf_brand_list[i]; 279ecc2fda8SBjoern A. Zeeb if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 280ecc2fda8SBjoern A. Zeeb continue; 281ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 2825fe3ed62SJake Burkholder strcmp(interp, bi->interp_path) == 0) 2835fe3ed62SJake Burkholder return (bi); 2845fe3ed62SJake Burkholder } 2855fe3ed62SJake Burkholder } 2865fe3ed62SJake Burkholder 2875fe3ed62SJake Burkholder /* Lacking a recognized interpreter, try the default brand */ 2885fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 2895fe3ed62SJake Burkholder bi = elf_brand_list[i]; 290ecc2fda8SBjoern A. Zeeb if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 291ecc2fda8SBjoern A. Zeeb continue; 292ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 293e548a1d4SJake Burkholder __elfN(fallback_brand) == bi->brand) 2945fe3ed62SJake Burkholder return (bi); 2955fe3ed62SJake Burkholder } 2965fe3ed62SJake Burkholder return (NULL); 2975fe3ed62SJake Burkholder } 2985fe3ed62SJake Burkholder 299e1743d02SSøren Schmidt static int 3003ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr) 301e1743d02SSøren Schmidt { 302d0ca7c29SPeter Wemm Elf_Brandinfo *bi; 3033ebc1248SPeter Wemm int i; 3043ebc1248SPeter Wemm 30552c24af7SPeter Wemm if (!IS_ELF(*hdr) || 30652c24af7SPeter Wemm hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 30752c24af7SPeter Wemm hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 3083dc19c46SJacques Vidrine hdr->e_ident[EI_VERSION] != EV_CURRENT || 3093dc19c46SJacques Vidrine hdr->e_phentsize != sizeof(Elf_Phdr) || 3103dc19c46SJacques Vidrine hdr->e_version != ELF_TARG_VER) 311a7cddfedSJake Burkholder return (ENOEXEC); 312e1743d02SSøren Schmidt 3133ebc1248SPeter Wemm /* 3143ebc1248SPeter Wemm * Make sure we have at least one brand for this machine. 3153ebc1248SPeter Wemm */ 3163ebc1248SPeter Wemm 3173ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 318d0ca7c29SPeter Wemm bi = elf_brand_list[i]; 319d0ca7c29SPeter Wemm if (bi != NULL && bi->machine == hdr->e_machine) 3203ebc1248SPeter Wemm break; 3213ebc1248SPeter Wemm } 3223ebc1248SPeter Wemm if (i == MAX_BRANDS) 323a7cddfedSJake Burkholder return (ENOEXEC); 324e1743d02SSøren Schmidt 325a7cddfedSJake Burkholder return (0); 326e1743d02SSøren Schmidt } 327e1743d02SSøren Schmidt 328e1743d02SSøren Schmidt static int 3293ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 330ff6f03c7SAlan Cox vm_offset_t start, vm_offset_t end, vm_prot_t prot) 3313ebc1248SPeter Wemm { 332da61b9a6SAlan Cox struct sf_buf *sf; 333da61b9a6SAlan Cox int error; 3343ebc1248SPeter Wemm vm_offset_t off; 3353ebc1248SPeter Wemm 3363ebc1248SPeter Wemm /* 3373ebc1248SPeter Wemm * Create the page if it doesn't exist yet. Ignore errors. 3383ebc1248SPeter Wemm */ 3393ebc1248SPeter Wemm vm_map_lock(map); 340ff6f03c7SAlan Cox vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end), 341ff6f03c7SAlan Cox VM_PROT_ALL, VM_PROT_ALL, 0); 3423ebc1248SPeter Wemm vm_map_unlock(map); 3433ebc1248SPeter Wemm 3443ebc1248SPeter Wemm /* 3453ebc1248SPeter Wemm * Find the page from the underlying object. 3463ebc1248SPeter Wemm */ 3473ebc1248SPeter Wemm if (object) { 348da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 349da61b9a6SAlan Cox if (sf == NULL) 350da61b9a6SAlan Cox return (KERN_FAILURE); 3513ebc1248SPeter Wemm off = offset - trunc_page(offset); 352da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start, 353ca0387efSJake Burkholder end - start); 354da61b9a6SAlan Cox vm_imgact_unmap_page(sf); 3553ebc1248SPeter Wemm if (error) { 356a7cddfedSJake Burkholder return (KERN_FAILURE); 3573ebc1248SPeter Wemm } 3583ebc1248SPeter Wemm } 3593ebc1248SPeter Wemm 360a7cddfedSJake Burkholder return (KERN_SUCCESS); 3613ebc1248SPeter Wemm } 3623ebc1248SPeter Wemm 3633ebc1248SPeter Wemm static int 3643ebc1248SPeter Wemm __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 365ff6f03c7SAlan Cox vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow) 3663ebc1248SPeter Wemm { 367da61b9a6SAlan Cox struct sf_buf *sf; 368da61b9a6SAlan Cox vm_offset_t off; 369a063facbSMarcel Moolenaar vm_size_t sz; 370a063facbSMarcel Moolenaar int error, rv; 3713ebc1248SPeter Wemm 3723ebc1248SPeter Wemm if (start != trunc_page(start)) { 37381f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset, start, 374ff6f03c7SAlan Cox round_page(start), prot); 3753ebc1248SPeter Wemm if (rv) 376a7cddfedSJake Burkholder return (rv); 3773ebc1248SPeter Wemm offset += round_page(start) - start; 3783ebc1248SPeter Wemm start = round_page(start); 3793ebc1248SPeter Wemm } 3803ebc1248SPeter Wemm if (end != round_page(end)) { 38181f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset + 382ff6f03c7SAlan Cox trunc_page(end) - start, trunc_page(end), end, prot); 3833ebc1248SPeter Wemm if (rv) 384a7cddfedSJake Burkholder return (rv); 3853ebc1248SPeter Wemm end = trunc_page(end); 3863ebc1248SPeter Wemm } 3873ebc1248SPeter Wemm if (end > start) { 3883ebc1248SPeter Wemm if (offset & PAGE_MASK) { 3893ebc1248SPeter Wemm /* 3903ebc1248SPeter Wemm * The mapping is not page aligned. This means we have 3913ebc1248SPeter Wemm * to copy the data. Sigh. 3923ebc1248SPeter Wemm */ 393584716b0SAlan Cox rv = vm_map_find(map, NULL, 0, &start, end - start, 394ff6f03c7SAlan Cox FALSE, prot | VM_PROT_WRITE, VM_PROT_ALL, 0); 3953ebc1248SPeter Wemm if (rv) 396a7cddfedSJake Burkholder return (rv); 397da61b9a6SAlan Cox if (object == NULL) 398da61b9a6SAlan Cox return (KERN_SUCCESS); 399da61b9a6SAlan Cox for (; start < end; start += sz) { 400da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 401da61b9a6SAlan Cox if (sf == NULL) 402da61b9a6SAlan Cox return (KERN_FAILURE); 4033ebc1248SPeter Wemm off = offset - trunc_page(offset); 4043ebc1248SPeter Wemm sz = end - start; 405da61b9a6SAlan Cox if (sz > PAGE_SIZE - off) 406da61b9a6SAlan Cox sz = PAGE_SIZE - off; 407da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, 4083ebc1248SPeter Wemm (caddr_t)start, sz); 409da61b9a6SAlan Cox vm_imgact_unmap_page(sf); 4103ebc1248SPeter Wemm if (error) { 411a7cddfedSJake Burkholder return (KERN_FAILURE); 4123ebc1248SPeter Wemm } 413da61b9a6SAlan Cox offset += sz; 4143ebc1248SPeter Wemm } 4153ebc1248SPeter Wemm rv = KERN_SUCCESS; 4163ebc1248SPeter Wemm } else { 417e5e6093bSAlan Cox vm_object_reference(object); 4183ebc1248SPeter Wemm vm_map_lock(map); 4193ebc1248SPeter Wemm rv = vm_map_insert(map, object, offset, start, end, 420ff6f03c7SAlan Cox prot, VM_PROT_ALL, cow); 4213ebc1248SPeter Wemm vm_map_unlock(map); 422e5e6093bSAlan Cox if (rv != KERN_SUCCESS) 423e5e6093bSAlan Cox vm_object_deallocate(object); 4243ebc1248SPeter Wemm } 425a7cddfedSJake Burkholder return (rv); 4263ebc1248SPeter Wemm } else { 427a7cddfedSJake Burkholder return (KERN_SUCCESS); 4283ebc1248SPeter Wemm } 4293ebc1248SPeter Wemm } 4303ebc1248SPeter Wemm 4313ebc1248SPeter Wemm static int 432373d1a3fSAlan Cox __elfN(load_section)(struct vmspace *vmspace, 433373d1a3fSAlan Cox vm_object_t object, vm_offset_t offset, 4343ebc1248SPeter Wemm caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot, 4353ebc1248SPeter Wemm size_t pagesize) 436e1743d02SSøren Schmidt { 437da61b9a6SAlan Cox struct sf_buf *sf; 438e1743d02SSøren Schmidt size_t map_len; 439e1743d02SSøren Schmidt vm_offset_t map_addr; 440fa7dd9c5SMatthew Dillon int error, rv, cow; 441e1743d02SSøren Schmidt size_t copy_len; 44252c24af7SPeter Wemm vm_offset_t file_addr; 44352c24af7SPeter Wemm 44425ead034SBrian Feldman /* 44525ead034SBrian Feldman * It's necessary to fail if the filsz + offset taken from the 44625ead034SBrian Feldman * header is greater than the actual file pager object's size. 44725ead034SBrian Feldman * If we were to allow this, then the vm_map_find() below would 44825ead034SBrian Feldman * walk right off the end of the file object and into the ether. 44925ead034SBrian Feldman * 45025ead034SBrian Feldman * While I'm here, might as well check for something else that 45125ead034SBrian Feldman * is invalid: filsz cannot be greater than memsz. 45225ead034SBrian Feldman */ 45325ead034SBrian Feldman if ((off_t)filsz + offset > object->un_pager.vnp.vnp_size || 45425ead034SBrian Feldman filsz > memsz) { 45525ead034SBrian Feldman uprintf("elf_load_section: truncated ELF file\n"); 45625ead034SBrian Feldman return (ENOEXEC); 45725ead034SBrian Feldman } 45825ead034SBrian Feldman 4593ebc1248SPeter Wemm map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize); 4603ebc1248SPeter Wemm file_addr = trunc_page_ps(offset, pagesize); 461e1743d02SSøren Schmidt 462e1743d02SSøren Schmidt /* 46352c24af7SPeter Wemm * We have two choices. We can either clear the data in the last page 46452c24af7SPeter Wemm * of an oversized mapping, or we can start the anon mapping a page 46552c24af7SPeter Wemm * early and copy the initialized data into that first page. We 46652c24af7SPeter Wemm * choose the second.. 46752c24af7SPeter Wemm */ 46852c24af7SPeter Wemm if (memsz > filsz) 4693ebc1248SPeter Wemm map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr; 47052c24af7SPeter Wemm else 4713ebc1248SPeter Wemm map_len = round_page_ps(offset + filsz, pagesize) - file_addr; 47252c24af7SPeter Wemm 47352c24af7SPeter Wemm if (map_len != 0) { 474fa7dd9c5SMatthew Dillon /* cow flags: don't dump readonly sections in core */ 475fa7dd9c5SMatthew Dillon cow = MAP_COPY_ON_WRITE | MAP_PREFAULT | 476fa7dd9c5SMatthew Dillon (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP); 477fa7dd9c5SMatthew Dillon 4783ebc1248SPeter Wemm rv = __elfN(map_insert)(&vmspace->vm_map, 47952c24af7SPeter Wemm object, 48052c24af7SPeter Wemm file_addr, /* file offset */ 48152c24af7SPeter Wemm map_addr, /* virtual start */ 48252c24af7SPeter Wemm map_addr + map_len,/* virtual end */ 48352c24af7SPeter Wemm prot, 484fa7dd9c5SMatthew Dillon cow); 485e5e6093bSAlan Cox if (rv != KERN_SUCCESS) 486a7cddfedSJake Burkholder return (EINVAL); 48752c24af7SPeter Wemm 48852c24af7SPeter Wemm /* we can stop now if we've covered it all */ 48923955314SAlfred Perlstein if (memsz == filsz) { 490a7cddfedSJake Burkholder return (0); 49152c24af7SPeter Wemm } 49223955314SAlfred Perlstein } 49352c24af7SPeter Wemm 49452c24af7SPeter Wemm 49552c24af7SPeter Wemm /* 49652c24af7SPeter Wemm * We have to get the remaining bit of the file into the first part 49752c24af7SPeter Wemm * of the oversized map segment. This is normally because the .data 49852c24af7SPeter Wemm * segment in the file is extended to provide bss. It's a neat idea 49952c24af7SPeter Wemm * to try and save a page, but it's a pain in the behind to implement. 500e1743d02SSøren Schmidt */ 5013ebc1248SPeter Wemm copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize); 5023ebc1248SPeter Wemm map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize); 503ca0387efSJake Burkholder map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) - 504ca0387efSJake Burkholder map_addr; 505e1743d02SSøren Schmidt 50652c24af7SPeter Wemm /* This had damn well better be true! */ 5078191d577SPeter Wemm if (map_len != 0) { 50881f223caSJake Burkholder rv = __elfN(map_insert)(&vmspace->vm_map, NULL, 0, map_addr, 509ff6f03c7SAlan Cox map_addr + map_len, VM_PROT_ALL, 0); 51023955314SAlfred Perlstein if (rv != KERN_SUCCESS) { 511a7cddfedSJake Burkholder return (EINVAL); 5128191d577SPeter Wemm } 51323955314SAlfred Perlstein } 514e1743d02SSøren Schmidt 51552c24af7SPeter Wemm if (copy_len != 0) { 5163ebc1248SPeter Wemm vm_offset_t off; 517da61b9a6SAlan Cox 518da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset + filsz); 519da61b9a6SAlan Cox if (sf == NULL) 520da61b9a6SAlan Cox return (EIO); 521e1743d02SSøren Schmidt 52252c24af7SPeter Wemm /* send the page fragment to user space */ 52381f223caSJake Burkholder off = trunc_page_ps(offset + filsz, pagesize) - 52481f223caSJake Burkholder trunc_page(offset + filsz); 525da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, 526da61b9a6SAlan Cox (caddr_t)map_addr, copy_len); 527da61b9a6SAlan Cox vm_imgact_unmap_page(sf); 52823955314SAlfred Perlstein if (error) { 52952c24af7SPeter Wemm return (error); 53052c24af7SPeter Wemm } 53123955314SAlfred Perlstein } 532e1743d02SSøren Schmidt 533e1743d02SSøren Schmidt /* 5343ebc1248SPeter Wemm * set it to the specified protection. 5353ebc1248SPeter Wemm * XXX had better undo the damage from pasting over the cracks here! 536e1743d02SSøren Schmidt */ 5373ebc1248SPeter Wemm vm_map_protect(&vmspace->vm_map, trunc_page(map_addr), 5383ebc1248SPeter Wemm round_page(map_addr + map_len), prot, FALSE); 5398191d577SPeter Wemm 540ff6f03c7SAlan Cox return (0); 541e1743d02SSøren Schmidt } 542e1743d02SSøren Schmidt 543c33fe779SJohn Polstra /* 544c33fe779SJohn Polstra * Load the file "file" into memory. It may be either a shared object 545c33fe779SJohn Polstra * or an executable. 546c33fe779SJohn Polstra * 547c33fe779SJohn Polstra * The "addr" reference parameter is in/out. On entry, it specifies 548c33fe779SJohn Polstra * the address where a shared object should be loaded. If the file is 549c33fe779SJohn Polstra * an executable, this value is ignored. On exit, "addr" specifies 550c33fe779SJohn Polstra * where the file was actually loaded. 551c33fe779SJohn Polstra * 552c33fe779SJohn Polstra * The "entry" reference parameter is out only. On exit, it specifies 553c33fe779SJohn Polstra * the entry point for the loaded file. 554c33fe779SJohn Polstra */ 555e1743d02SSøren Schmidt static int 5563ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 5573ebc1248SPeter Wemm u_long *entry, size_t pagesize) 558e1743d02SSøren Schmidt { 559911c2be0SMark Peek struct { 560911c2be0SMark Peek struct nameidata nd; 561911c2be0SMark Peek struct vattr attr; 562911c2be0SMark Peek struct image_params image_params; 563911c2be0SMark Peek } *tempdata; 564d254af07SMatthew Dillon const Elf_Ehdr *hdr = NULL; 565d254af07SMatthew Dillon const Elf_Phdr *phdr = NULL; 566911c2be0SMark Peek struct nameidata *nd; 567e1743d02SSøren Schmidt struct vmspace *vmspace = p->p_vmspace; 568911c2be0SMark Peek struct vattr *attr; 569911c2be0SMark Peek struct image_params *imgp; 57052c24af7SPeter Wemm vm_prot_t prot; 571c33fe779SJohn Polstra u_long rbase; 572c33fe779SJohn Polstra u_long base_addr = 0; 57368ff2a43SChristian S.J. Peron int vfslocked, error, i, numsegs; 574e1743d02SSøren Schmidt 575a163d034SWarner Losh tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK); 576911c2be0SMark Peek nd = &tempdata->nd; 577911c2be0SMark Peek attr = &tempdata->attr; 578911c2be0SMark Peek imgp = &tempdata->image_params; 579911c2be0SMark Peek 580c8a79999SPeter Wemm /* 581c8a79999SPeter Wemm * Initialize part of the common data 582c8a79999SPeter Wemm */ 583c8a79999SPeter Wemm imgp->proc = p; 584911c2be0SMark Peek imgp->attr = attr; 585c8a79999SPeter Wemm imgp->firstpage = NULL; 58659c8bc40SAlan Cox imgp->image_header = NULL; 5870b2ed1aeSJeff Roberson imgp->object = NULL; 5886d7bdc8dSRobert Watson imgp->execlabel = NULL; 589c8a79999SPeter Wemm 59068ff2a43SChristian S.J. Peron NDINIT(nd, LOOKUP, MPSAFE|LOCKLEAF|FOLLOW, UIO_SYSSPACE, file, 59168ff2a43SChristian S.J. Peron curthread); 59268ff2a43SChristian S.J. Peron vfslocked = 0; 593911c2be0SMark Peek if ((error = namei(nd)) != 0) { 594911c2be0SMark Peek nd->ni_vp = NULL; 595e1743d02SSøren Schmidt goto fail; 596e1743d02SSøren Schmidt } 59768ff2a43SChristian S.J. Peron vfslocked = NDHASGIANT(nd); 598911c2be0SMark Peek NDFREE(nd, NDF_ONLY_PNBUF); 599911c2be0SMark Peek imgp->vp = nd->ni_vp; 600c8a79999SPeter Wemm 601e1743d02SSøren Schmidt /* 602e1743d02SSøren Schmidt * Check permissions, modes, uid, etc on the file, and "open" it. 603e1743d02SSøren Schmidt */ 604c8a79999SPeter Wemm error = exec_check_permissions(imgp); 605373d1a3fSAlan Cox if (error) 606c8a79999SPeter Wemm goto fail; 607e1743d02SSøren Schmidt 608c8a79999SPeter Wemm error = exec_map_first_page(imgp); 609373d1a3fSAlan Cox if (error) 610373d1a3fSAlan Cox goto fail; 611373d1a3fSAlan Cox 61225ead034SBrian Feldman /* 61325ead034SBrian Feldman * Also make certain that the interpreter stays the same, so set 614e6e370a7SJeff Roberson * its VV_TEXT flag, too. 61525ead034SBrian Feldman */ 616e6e370a7SJeff Roberson nd->ni_vp->v_vflag |= VV_TEXT; 617e6e370a7SJeff Roberson 6188516dd18SPoul-Henning Kamp imgp->object = nd->ni_vp->v_object; 619e1743d02SSøren Schmidt 620d254af07SMatthew Dillon hdr = (const Elf_Ehdr *)imgp->image_header; 6213ebc1248SPeter Wemm if ((error = __elfN(check_header)(hdr)) != 0) 622e1743d02SSøren Schmidt goto fail; 623c33fe779SJohn Polstra if (hdr->e_type == ET_DYN) 624c33fe779SJohn Polstra rbase = *addr; 625c33fe779SJohn Polstra else if (hdr->e_type == ET_EXEC) 626c33fe779SJohn Polstra rbase = 0; 627c33fe779SJohn Polstra else { 628c33fe779SJohn Polstra error = ENOEXEC; 629c33fe779SJohn Polstra goto fail; 630c33fe779SJohn Polstra } 631e1743d02SSøren Schmidt 632c8a79999SPeter Wemm /* Only support headers that fit within first page for now */ 6333dc19c46SJacques Vidrine /* (multiplication of two Elf_Half fields will not overflow) */ 63452c24af7SPeter Wemm if ((hdr->e_phoff > PAGE_SIZE) || 6353dc19c46SJacques Vidrine (hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE - hdr->e_phoff) { 636c8a79999SPeter Wemm error = ENOEXEC; 637e1743d02SSøren Schmidt goto fail; 638c8a79999SPeter Wemm } 639c8a79999SPeter Wemm 640d254af07SMatthew Dillon phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 64193d1c728SKonstantin Belousov if (!aligned(phdr, Elf_Addr)) { 64293d1c728SKonstantin Belousov error = ENOEXEC; 64393d1c728SKonstantin Belousov goto fail; 64493d1c728SKonstantin Belousov } 645e1743d02SSøren Schmidt 646c33fe779SJohn Polstra for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) { 6475b33842aSKonstantin Belousov if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) { 6485b33842aSKonstantin Belousov /* Loadable segment */ 649*ed167eaaSKonstantin Belousov prot = __elfN(trans_prot)(phdr[i].p_flags); 650373d1a3fSAlan Cox if ((error = __elfN(load_section)(vmspace, 651373d1a3fSAlan Cox imgp->object, phdr[i].p_offset, 65281f223caSJake Burkholder (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase, 65381f223caSJake Burkholder phdr[i].p_memsz, phdr[i].p_filesz, prot, 65481f223caSJake Burkholder pagesize)) != 0) 655e1743d02SSøren Schmidt goto fail; 656e1743d02SSøren Schmidt /* 657c33fe779SJohn Polstra * Establish the base address if this is the 658c33fe779SJohn Polstra * first segment. 659e1743d02SSøren Schmidt */ 660c33fe779SJohn Polstra if (numsegs == 0) 661ca0387efSJake Burkholder base_addr = trunc_page(phdr[i].p_vaddr + 662ca0387efSJake Burkholder rbase); 663c33fe779SJohn Polstra numsegs++; 664e1743d02SSøren Schmidt } 665e1743d02SSøren Schmidt } 666c33fe779SJohn Polstra *addr = base_addr; 667c33fe779SJohn Polstra *entry = (unsigned long)hdr->e_entry + rbase; 668e1743d02SSøren Schmidt 669e1743d02SSøren Schmidt fail: 670c8a79999SPeter Wemm if (imgp->firstpage) 671c8a79999SPeter Wemm exec_unmap_first_page(imgp); 6720b2ed1aeSJeff Roberson 673911c2be0SMark Peek if (nd->ni_vp) 674373d1a3fSAlan Cox vput(nd->ni_vp); 675911c2be0SMark Peek 67668ff2a43SChristian S.J. Peron VFS_UNLOCK_GIANT(vfslocked); 677911c2be0SMark Peek free(tempdata, M_TEMP); 678e1743d02SSøren Schmidt 679a7cddfedSJake Burkholder return (error); 680e1743d02SSøren Schmidt } 681e1743d02SSøren Schmidt 682303b270bSEivind Eklund static int 6833ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) 684e1743d02SSøren Schmidt { 685ecbb00a2SDoug Rabson const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 68632c01de2SDmitry Chagin const Elf_Phdr *phdr; 687e5e6093bSAlan Cox Elf_Auxargs *elf_auxargs; 6885856e12eSJohn Dyson struct vmspace *vmspace; 68952c24af7SPeter Wemm vm_prot_t prot; 69021c2d047SMatthew Dillon u_long text_size = 0, data_size = 0, total_size = 0; 691e1743d02SSøren Schmidt u_long text_addr = 0, data_addr = 0; 692cac45152SMatthew Dillon u_long seg_size, seg_addr; 6937564c4adSKonstantin Belousov u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0; 69432c01de2SDmitry Chagin int32_t osrel = 0; 6957564c4adSKonstantin Belousov int error = 0, i, n; 6964113f8d7SPeter Wemm const char *interp = NULL, *newinterp = NULL; 697d1dbc694SJohn Polstra Elf_Brandinfo *brand_info; 698911c2be0SMark Peek char *path; 6995fe3ed62SJake Burkholder struct sysentvec *sv; 700e1743d02SSøren Schmidt 701e1743d02SSøren Schmidt /* 702e1743d02SSøren Schmidt * Do we have a valid ELF header ? 703900b28f9SMaxim Sobolev * 704900b28f9SMaxim Sobolev * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later 705900b28f9SMaxim Sobolev * if particular brand doesn't support it. 706e1743d02SSøren Schmidt */ 707900b28f9SMaxim Sobolev if (__elfN(check_header)(hdr) != 0 || 708900b28f9SMaxim Sobolev (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)) 709a7cddfedSJake Burkholder return (-1); 710e1743d02SSøren Schmidt 711e1743d02SSøren Schmidt /* 712e1743d02SSøren Schmidt * From here on down, we return an errno, not -1, as we've 713e1743d02SSøren Schmidt * detected an ELF file. 714e1743d02SSøren Schmidt */ 715e1743d02SSøren Schmidt 716e1743d02SSøren Schmidt if ((hdr->e_phoff > PAGE_SIZE) || 71752c24af7SPeter Wemm (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) { 718c8a79999SPeter Wemm /* Only support headers in first page for now */ 719a7cddfedSJake Burkholder return (ENOEXEC); 720e1743d02SSøren Schmidt } 72152c24af7SPeter Wemm phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 72293d1c728SKonstantin Belousov if (!aligned(phdr, Elf_Addr)) 72393d1c728SKonstantin Belousov return (ENOEXEC); 7247564c4adSKonstantin Belousov n = 0; 7257564c4adSKonstantin Belousov baddr = 0; 7265fe3ed62SJake Burkholder for (i = 0; i < hdr->e_phnum; i++) { 7277564c4adSKonstantin Belousov if (phdr[i].p_type == PT_LOAD) { 7287564c4adSKonstantin Belousov if (n == 0) 7297564c4adSKonstantin Belousov baddr = phdr[i].p_vaddr; 7307564c4adSKonstantin Belousov n++; 7317564c4adSKonstantin Belousov continue; 7327564c4adSKonstantin Belousov } 733e5e6093bSAlan Cox if (phdr[i].p_type == PT_INTERP) { 734e5e6093bSAlan Cox /* Path to interpreter */ 7355fe3ed62SJake Burkholder if (phdr[i].p_filesz > MAXPATHLEN || 73660bb3943SAlan Cox phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) 73760bb3943SAlan Cox return (ENOEXEC); 7385fe3ed62SJake Burkholder interp = imgp->image_header + phdr[i].p_offset; 7397564c4adSKonstantin Belousov continue; 7403ebc1248SPeter Wemm } 7413ebc1248SPeter Wemm } 7423ebc1248SPeter Wemm 74332c01de2SDmitry Chagin brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel); 7445fe3ed62SJake Burkholder if (brand_info == NULL) { 7455fe3ed62SJake Burkholder uprintf("ELF binary type \"%u\" not known.\n", 7465fe3ed62SJake Burkholder hdr->e_ident[EI_OSABI]); 74760bb3943SAlan Cox return (ENOEXEC); 7483ebc1248SPeter Wemm } 749ab02d85fSKonstantin Belousov if (hdr->e_type == ET_DYN) { 750ab02d85fSKonstantin Belousov if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) 751d49b2109SMaxim Sobolev return (ENOEXEC); 7527564c4adSKonstantin Belousov /* 7537564c4adSKonstantin Belousov * Honour the base load address from the dso if it is 7547564c4adSKonstantin Belousov * non-zero for some reason. 7557564c4adSKonstantin Belousov */ 7567564c4adSKonstantin Belousov if (baddr == 0) 757ab02d85fSKonstantin Belousov et_dyn_addr = ET_DYN_LOAD_ADDR; 7587564c4adSKonstantin Belousov else 7597564c4adSKonstantin Belousov et_dyn_addr = 0; 760ab02d85fSKonstantin Belousov } else 761ab02d85fSKonstantin Belousov et_dyn_addr = 0; 7625fe3ed62SJake Burkholder sv = brand_info->sysvec; 7639b68618dSPeter Wemm if (interp != NULL && brand_info->interp_newpath != NULL) 7644113f8d7SPeter Wemm newinterp = brand_info->interp_newpath; 7653ebc1248SPeter Wemm 76660bb3943SAlan Cox /* 76760bb3943SAlan Cox * Avoid a possible deadlock if the current address space is destroyed 76860bb3943SAlan Cox * and that address space maps the locked vnode. In the common case, 76960bb3943SAlan Cox * the locked vnode's v_usecount is decremented but remains greater 77060bb3943SAlan Cox * than zero. Consequently, the vnode lock is not needed by vrele(). 77160bb3943SAlan Cox * However, in cases where the vnode lock is external, such as nullfs, 77260bb3943SAlan Cox * v_usecount may become zero. 77360bb3943SAlan Cox */ 77422db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 77560bb3943SAlan Cox 77689b57fcfSKonstantin Belousov error = exec_new_vmspace(imgp, sv); 77719059a13SJohn Baldwin imgp->proc->p_sysent = sv; 778e1743d02SSøren Schmidt 779cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 78089b57fcfSKonstantin Belousov if (error) 78189b57fcfSKonstantin Belousov return (error); 78260bb3943SAlan Cox 7835856e12eSJohn Dyson vmspace = imgp->proc->p_vmspace; 7845856e12eSJohn Dyson 785e1743d02SSøren Schmidt for (i = 0; i < hdr->e_phnum; i++) { 786e1743d02SSøren Schmidt switch (phdr[i].p_type) { 787e1743d02SSøren Schmidt case PT_LOAD: /* Loadable segment */ 7885b33842aSKonstantin Belousov if (phdr[i].p_memsz == 0) 7895b33842aSKonstantin Belousov break; 790*ed167eaaSKonstantin Belousov prot = __elfN(trans_prot)(phdr[i].p_flags); 791e1743d02SSøren Schmidt 7923ebc1248SPeter Wemm #if defined(__ia64__) && __ELF_WORD_SIZE == 32 && defined(IA32_ME_HARDER) 7933ebc1248SPeter Wemm /* 7943ebc1248SPeter Wemm * Some x86 binaries assume read == executable, 7953ebc1248SPeter Wemm * notably the M3 runtime and therefore cvsup 7963ebc1248SPeter Wemm */ 7973ebc1248SPeter Wemm if (prot & VM_PROT_READ) 7983ebc1248SPeter Wemm prot |= VM_PROT_EXECUTE; 7993ebc1248SPeter Wemm #endif 8003ebc1248SPeter Wemm 801373d1a3fSAlan Cox if ((error = __elfN(load_section)(vmspace, 802373d1a3fSAlan Cox imgp->object, phdr[i].p_offset, 803ab02d85fSKonstantin Belousov (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr, 80481f223caSJake Burkholder phdr[i].p_memsz, phdr[i].p_filesz, prot, 8055fe3ed62SJake Burkholder sv->sv_pagesize)) != 0) 80660bb3943SAlan Cox return (error); 807e1743d02SSøren Schmidt 808cfaf7e60SDoug Rabson /* 809cfaf7e60SDoug Rabson * If this segment contains the program headers, 810cfaf7e60SDoug Rabson * remember their virtual address for the AT_PHDR 811cfaf7e60SDoug Rabson * aux entry. Static binaries don't usually include 812cfaf7e60SDoug Rabson * a PT_PHDR entry. 813cfaf7e60SDoug Rabson */ 814cfaf7e60SDoug Rabson if (phdr[i].p_offset == 0 && 815cfaf7e60SDoug Rabson hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize 816cfaf7e60SDoug Rabson <= phdr[i].p_filesz) 817ab02d85fSKonstantin Belousov proghdr = phdr[i].p_vaddr + hdr->e_phoff + 818ab02d85fSKonstantin Belousov et_dyn_addr; 819cfaf7e60SDoug Rabson 820ab02d85fSKonstantin Belousov seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr); 821cac45152SMatthew Dillon seg_size = round_page(phdr[i].p_memsz + 822ab02d85fSKonstantin Belousov phdr[i].p_vaddr + et_dyn_addr - seg_addr); 823cac45152SMatthew Dillon 824e1743d02SSøren Schmidt /* 825920acedbSNathan Whitehorn * Make the largest executable segment the official 826920acedbSNathan Whitehorn * text segment and all others data. 82721c2d047SMatthew Dillon * 82821c2d047SMatthew Dillon * Note that obreak() assumes that data_addr + 82921c2d047SMatthew Dillon * data_size == end of data load area, and the ELF 83021c2d047SMatthew Dillon * file format expects segments to be sorted by 83121c2d047SMatthew Dillon * address. If multiple data segments exist, the 83221c2d047SMatthew Dillon * last one will be used. 833e1743d02SSøren Schmidt */ 834920acedbSNathan Whitehorn 835920acedbSNathan Whitehorn if (phdr[i].p_flags & PF_X && text_size < seg_size) { 8369782ecbaSPeter Wemm text_size = seg_size; 8379782ecbaSPeter Wemm text_addr = seg_addr; 8389782ecbaSPeter Wemm } else { 83921c2d047SMatthew Dillon data_size = seg_size; 840cac45152SMatthew Dillon data_addr = seg_addr; 841cac45152SMatthew Dillon } 84221c2d047SMatthew Dillon total_size += seg_size; 84396725dd0SAlexander Kabaev break; 84496725dd0SAlexander Kabaev case PT_PHDR: /* Program header table info */ 845ab02d85fSKonstantin Belousov proghdr = phdr[i].p_vaddr + et_dyn_addr; 84696725dd0SAlexander Kabaev break; 84796725dd0SAlexander Kabaev default: 84896725dd0SAlexander Kabaev break; 84996725dd0SAlexander Kabaev } 85096725dd0SAlexander Kabaev } 85196725dd0SAlexander Kabaev 85296725dd0SAlexander Kabaev if (data_addr == 0 && data_size == 0) { 85396725dd0SAlexander Kabaev data_addr = text_addr; 85496725dd0SAlexander Kabaev data_size = text_size; 85596725dd0SAlexander Kabaev } 856cac45152SMatthew Dillon 857920acedbSNathan Whitehorn entry = (u_long)hdr->e_entry + et_dyn_addr; 858920acedbSNathan Whitehorn 859cac45152SMatthew Dillon /* 860cac45152SMatthew Dillon * Check limits. It should be safe to check the 86196725dd0SAlexander Kabaev * limits after loading the segments since we do 86296725dd0SAlexander Kabaev * not actually fault in all the segments pages. 863cac45152SMatthew Dillon */ 86491d5354aSJohn Baldwin PROC_LOCK(imgp->proc); 86591d5354aSJohn Baldwin if (data_size > lim_cur(imgp->proc, RLIMIT_DATA) || 866cac45152SMatthew Dillon text_size > maxtsiz || 86791d5354aSJohn Baldwin total_size > lim_cur(imgp->proc, RLIMIT_VMEM)) { 86891d5354aSJohn Baldwin PROC_UNLOCK(imgp->proc); 86960bb3943SAlan Cox return (ENOMEM); 870cac45152SMatthew Dillon } 871e1743d02SSøren Schmidt 872e1743d02SSøren Schmidt vmspace->vm_tsize = text_size >> PAGE_SHIFT; 8737cd99438SBruce Evans vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr; 874e1743d02SSøren Schmidt vmspace->vm_dsize = data_size >> PAGE_SHIFT; 8757cd99438SBruce Evans vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr; 876e1743d02SSøren Schmidt 877c460ac3aSPeter Wemm /* 878c460ac3aSPeter Wemm * We load the dynamic linker where a userland call 879c460ac3aSPeter Wemm * to mmap(0, ...) would put it. The rationale behind this 880c460ac3aSPeter Wemm * calculation is that it leaves room for the heap to grow to 881c460ac3aSPeter Wemm * its maximum allowed size. 882c460ac3aSPeter Wemm */ 883c460ac3aSPeter Wemm addr = round_page((vm_offset_t)imgp->proc->p_vmspace->vm_daddr + 88491d5354aSJohn Baldwin lim_max(imgp->proc, RLIMIT_DATA)); 88591d5354aSJohn Baldwin PROC_UNLOCK(imgp->proc); 886e1743d02SSøren Schmidt 887ea5a2b2eSSøren Schmidt imgp->entry_addr = entry; 888ea5a2b2eSSøren Schmidt 88960bb3943SAlan Cox if (interp != NULL) { 8904113f8d7SPeter Wemm int have_interp = FALSE; 89122db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 89260bb3943SAlan Cox if (brand_info->emul_path != NULL && 8939b68618dSPeter Wemm brand_info->emul_path[0] != '\0') { 894a163d034SWarner Losh path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 89560bb3943SAlan Cox snprintf(path, MAXPATHLEN, "%s%s", 89660bb3943SAlan Cox brand_info->emul_path, interp); 8979b68618dSPeter Wemm error = __elfN(load_file)(imgp->proc, path, &addr, 8989b68618dSPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 899911c2be0SMark Peek free(path, M_TEMP); 9009b68618dSPeter Wemm if (error == 0) 9014113f8d7SPeter Wemm have_interp = TRUE; 9029b68618dSPeter Wemm } 9034113f8d7SPeter Wemm if (!have_interp && newinterp != NULL) { 9044113f8d7SPeter Wemm error = __elfN(load_file)(imgp->proc, newinterp, &addr, 9054113f8d7SPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 906387ad998SKonstantin Belousov if (error == 0) 9074113f8d7SPeter Wemm have_interp = TRUE; 9084113f8d7SPeter Wemm } 9094113f8d7SPeter Wemm if (!have_interp) { 9109b68618dSPeter Wemm error = __elfN(load_file)(imgp->proc, interp, &addr, 9119b68618dSPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 91260bb3943SAlan Cox } 913cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 9149b68618dSPeter Wemm if (error != 0) { 9159b68618dSPeter Wemm uprintf("ELF interpreter %s not found\n", interp); 91660bb3943SAlan Cox return (error); 917e1743d02SSøren Schmidt } 91895c807cfSRobert Watson } else 9197564c4adSKonstantin Belousov addr = et_dyn_addr; 920ea5a2b2eSSøren Schmidt 921e1743d02SSøren Schmidt /* 922e1743d02SSøren Schmidt * Construct auxargs table (used by the fixup routine) 923e1743d02SSøren Schmidt */ 924a163d034SWarner Losh elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK); 925e1743d02SSøren Schmidt elf_auxargs->execfd = -1; 926e1743d02SSøren Schmidt elf_auxargs->phdr = proghdr; 927e1743d02SSøren Schmidt elf_auxargs->phent = hdr->e_phentsize; 928e1743d02SSøren Schmidt elf_auxargs->phnum = hdr->e_phnum; 929e1743d02SSøren Schmidt elf_auxargs->pagesz = PAGE_SIZE; 930e1743d02SSøren Schmidt elf_auxargs->base = addr; 931e1743d02SSøren Schmidt elf_auxargs->flags = 0; 932e1743d02SSøren Schmidt elf_auxargs->entry = entry; 933e1743d02SSøren Schmidt 934e1743d02SSøren Schmidt imgp->auxargs = elf_auxargs; 935e1743d02SSøren Schmidt imgp->interpreted = 0; 936a0ea661fSNathan Whitehorn imgp->reloc_base = addr; 93732c01de2SDmitry Chagin imgp->proc->p_osrel = osrel; 938f231de47SKonstantin Belousov 939a7cddfedSJake Burkholder return (error); 940e1743d02SSøren Schmidt } 941e1743d02SSøren Schmidt 942a360a43dSJake Burkholder #define suword __CONCAT(suword, __ELF_WORD_SIZE) 9433ebc1248SPeter Wemm 9443ebc1248SPeter Wemm int 9453ebc1248SPeter Wemm __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp) 946e1743d02SSøren Schmidt { 947ecbb00a2SDoug Rabson Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs; 948a360a43dSJake Burkholder Elf_Addr *base; 949a360a43dSJake Burkholder Elf_Addr *pos; 950e1743d02SSøren Schmidt 951a360a43dSJake Burkholder base = (Elf_Addr *)*stack_base; 952610ecfe0SMaxim Sobolev pos = base + (imgp->args->argc + imgp->args->envc + 2); 953e1743d02SSøren Schmidt 95435c2a5a8SWarner Losh if (args->execfd != -1) 955e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 956e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 957e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 958e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 959e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 960e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 961e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 962e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_BASE, args->base); 9633ff06357SKonstantin Belousov if (imgp->execpathp != 0) 9643ff06357SKonstantin Belousov AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp); 965ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_OSRELDATE, osreldate); 966ee235befSKonstantin Belousov if (imgp->canary != 0) { 967ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary); 968ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen); 969ee235befSKonstantin Belousov } 970ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus); 971ee235befSKonstantin Belousov if (imgp->pagesizes != 0) { 972ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes); 973ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen); 974ee235befSKonstantin Belousov } 975e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_NULL, 0); 976e1743d02SSøren Schmidt 977e1743d02SSøren Schmidt free(imgp->auxargs, M_TEMP); 978e1743d02SSøren Schmidt imgp->auxargs = NULL; 979e1743d02SSøren Schmidt 9803ebc1248SPeter Wemm base--; 981610ecfe0SMaxim Sobolev suword(base, (long)imgp->args->argc); 9823ebc1248SPeter Wemm *stack_base = (register_t *)base; 983a7cddfedSJake Burkholder return (0); 984e1743d02SSøren Schmidt } 985e1743d02SSøren Schmidt 986e1743d02SSøren Schmidt /* 9878c64af4fSJohn Polstra * Code for generating ELF core dumps. 9888c64af4fSJohn Polstra */ 9898c64af4fSJohn Polstra 9904d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *); 9910ff27d31SJohn Polstra 9920ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */ 9930ff27d31SJohn Polstra struct phdr_closure { 9940ff27d31SJohn Polstra Elf_Phdr *phdr; /* Program header to fill in */ 9950ff27d31SJohn Polstra Elf_Off offset; /* Offset of segment in core file */ 9960ff27d31SJohn Polstra }; 9970ff27d31SJohn Polstra 9980ff27d31SJohn Polstra /* Closure for cb_size_segment(). */ 9990ff27d31SJohn Polstra struct sseg_closure { 10000ff27d31SJohn Polstra int count; /* Count of writable segments. */ 10010ff27d31SJohn Polstra size_t size; /* Total size of all writable segments. */ 10020ff27d31SJohn Polstra }; 10030ff27d31SJohn Polstra 10044d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *); 10054d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *); 1006247aba24SMarcel Moolenaar static void each_writable_segment(struct thread *, segment_callback, void *); 10073ebc1248SPeter Wemm static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *, 1008e7228204SAlfred Perlstein int, void *, size_t, gzFile); 1009247aba24SMarcel Moolenaar static void __elfN(puthdr)(struct thread *, void *, size_t *, int); 10103ebc1248SPeter Wemm static void __elfN(putnote)(void *, size_t *, const char *, int, 10114d77a549SAlfred Perlstein const void *, size_t); 10128c64af4fSJohn Polstra 1013e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1014e7228204SAlfred Perlstein extern int compress_user_cores; 1015e7228204SAlfred Perlstein extern int compress_user_cores_gzlevel; 1016e7228204SAlfred Perlstein #endif 1017e7228204SAlfred Perlstein 1018e7228204SAlfred Perlstein static int 1019e7228204SAlfred Perlstein core_output(struct vnode *vp, void *base, size_t len, off_t offset, 1020e7228204SAlfred Perlstein struct ucred *active_cred, struct ucred *file_cred, 1021e7228204SAlfred Perlstein struct thread *td, char *core_buf, gzFile gzfile) { 1022e7228204SAlfred Perlstein 1023e7228204SAlfred Perlstein int error; 1024e7228204SAlfred Perlstein if (gzfile) { 1025e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1026e7228204SAlfred Perlstein error = compress_core(gzfile, base, core_buf, len, td); 1027e7228204SAlfred Perlstein #else 1028e7228204SAlfred Perlstein panic("shouldn't be here"); 1029e7228204SAlfred Perlstein #endif 1030e7228204SAlfred Perlstein } else { 1031e7228204SAlfred Perlstein error = vn_rdwr_inchunks(UIO_WRITE, vp, base, len, offset, 1032e7228204SAlfred Perlstein UIO_USERSPACE, IO_UNIT | IO_DIRECT, active_cred, file_cred, 1033e7228204SAlfred Perlstein NULL, td); 1034e7228204SAlfred Perlstein } 1035e7228204SAlfred Perlstein return (error); 1036e7228204SAlfred Perlstein } 1037e7228204SAlfred Perlstein 10388c64af4fSJohn Polstra int 1039e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags) 1040fca666a1SJulian Elischer { 1041247aba24SMarcel Moolenaar struct ucred *cred = td->td_ucred; 1042fca666a1SJulian Elischer int error = 0; 10430ff27d31SJohn Polstra struct sseg_closure seginfo; 10440ff27d31SJohn Polstra void *hdr; 10458c64af4fSJohn Polstra size_t hdrsize; 10468c64af4fSJohn Polstra 1047e7228204SAlfred Perlstein gzFile gzfile = Z_NULL; 1048e7228204SAlfred Perlstein char *core_buf = NULL; 1049e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1050e7228204SAlfred Perlstein char gzopen_flags[8]; 1051e7228204SAlfred Perlstein char *p; 1052e7228204SAlfred Perlstein int doing_compress = flags & IMGACT_CORE_COMPRESS; 1053e7228204SAlfred Perlstein #endif 1054e7228204SAlfred Perlstein 1055e7228204SAlfred Perlstein hdr = NULL; 1056e7228204SAlfred Perlstein 1057e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1058e7228204SAlfred Perlstein if (doing_compress) { 1059e7228204SAlfred Perlstein p = gzopen_flags; 1060e7228204SAlfred Perlstein *p++ = 'w'; 1061e7228204SAlfred Perlstein if (compress_user_cores_gzlevel >= 0 && 1062e7228204SAlfred Perlstein compress_user_cores_gzlevel <= 9) 1063e7228204SAlfred Perlstein *p++ = '0' + compress_user_cores_gzlevel; 1064e7228204SAlfred Perlstein *p = 0; 1065e7228204SAlfred Perlstein gzfile = gz_open("", gzopen_flags, vp); 1066e7228204SAlfred Perlstein if (gzfile == Z_NULL) { 1067e7228204SAlfred Perlstein error = EFAULT; 1068e7228204SAlfred Perlstein goto done; 1069e7228204SAlfred Perlstein } 1070e7228204SAlfred Perlstein core_buf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO); 1071e7228204SAlfred Perlstein if (!core_buf) { 1072e7228204SAlfred Perlstein error = ENOMEM; 1073e7228204SAlfred Perlstein goto done; 1074e7228204SAlfred Perlstein } 1075e7228204SAlfred Perlstein } 1076e7228204SAlfred Perlstein #endif 1077e7228204SAlfred Perlstein 10780ff27d31SJohn Polstra /* Size the program segments. */ 10790ff27d31SJohn Polstra seginfo.count = 0; 10800ff27d31SJohn Polstra seginfo.size = 0; 1081247aba24SMarcel Moolenaar each_writable_segment(td, cb_size_segment, &seginfo); 10820ff27d31SJohn Polstra 10830ff27d31SJohn Polstra /* 10840ff27d31SJohn Polstra * Calculate the size of the core file header area by making 10850ff27d31SJohn Polstra * a dry run of generating it. Nothing is written, but the 10860ff27d31SJohn Polstra * size is calculated. 10870ff27d31SJohn Polstra */ 10880ff27d31SJohn Polstra hdrsize = 0; 1089247aba24SMarcel Moolenaar __elfN(puthdr)(td, (void *)NULL, &hdrsize, seginfo.count); 10900ff27d31SJohn Polstra 1091fba6b1afSAlfred Perlstein if (hdrsize + seginfo.size >= limit) { 1092fba6b1afSAlfred Perlstein error = EFAULT; 1093fba6b1afSAlfred Perlstein goto done; 1094fba6b1afSAlfred Perlstein } 10950ff27d31SJohn Polstra 10960ff27d31SJohn Polstra /* 10970ff27d31SJohn Polstra * Allocate memory for building the header, fill it up, 10980ff27d31SJohn Polstra * and write it out. 10990ff27d31SJohn Polstra */ 1100a163d034SWarner Losh hdr = malloc(hdrsize, M_TEMP, M_WAITOK); 11010ff27d31SJohn Polstra if (hdr == NULL) { 1102fba6b1afSAlfred Perlstein error = EINVAL; 1103fba6b1afSAlfred Perlstein goto done; 11040ff27d31SJohn Polstra } 1105e7228204SAlfred Perlstein error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize, 1106e7228204SAlfred Perlstein gzfile); 11070ff27d31SJohn Polstra 11080ff27d31SJohn Polstra /* Write the contents of all of the writable segments. */ 11090ff27d31SJohn Polstra if (error == 0) { 11100ff27d31SJohn Polstra Elf_Phdr *php; 11112b471bc6STim J. Robbins off_t offset; 11120ff27d31SJohn Polstra int i; 11130ff27d31SJohn Polstra 11140ff27d31SJohn Polstra php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 11150ff27d31SJohn Polstra offset = hdrsize; 11160ff27d31SJohn Polstra for (i = 0; i < seginfo.count; i++) { 1117e7228204SAlfred Perlstein error = core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr, 1118e7228204SAlfred Perlstein php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile); 11190ff27d31SJohn Polstra if (error != 0) 11202b471bc6STim J. Robbins break; 11210ff27d31SJohn Polstra offset += php->p_filesz; 11220ff27d31SJohn Polstra php++; 11230ff27d31SJohn Polstra } 11240ff27d31SJohn Polstra } 1125e7228204SAlfred Perlstein if (error) { 1126e7228204SAlfred Perlstein log(LOG_WARNING, 1127e7228204SAlfred Perlstein "Failed to write core file for process %s (error %d)\n", 1128e7228204SAlfred Perlstein curproc->p_comm, error); 1129e7228204SAlfred Perlstein } 1130e7228204SAlfred Perlstein 1131e7228204SAlfred Perlstein done: 1132fba6b1afSAlfred Perlstein #ifdef COMPRESS_USER_CORES 1133e7228204SAlfred Perlstein if (core_buf) 1134e7228204SAlfred Perlstein free(core_buf, M_TEMP); 1135e7228204SAlfred Perlstein if (gzfile) 1136e7228204SAlfred Perlstein gzclose(gzfile); 11378b325009SAlfred Perlstein #endif 1138e7228204SAlfred Perlstein 11390ff27d31SJohn Polstra free(hdr, M_TEMP); 11400ff27d31SJohn Polstra 1141a7cddfedSJake Burkholder return (error); 11428c64af4fSJohn Polstra } 11438c64af4fSJohn Polstra 11440ff27d31SJohn Polstra /* 11450ff27d31SJohn Polstra * A callback for each_writable_segment() to write out the segment's 11460ff27d31SJohn Polstra * program header entry. 11470ff27d31SJohn Polstra */ 11480ff27d31SJohn Polstra static void 11490ff27d31SJohn Polstra cb_put_phdr(entry, closure) 11500ff27d31SJohn Polstra vm_map_entry_t entry; 11510ff27d31SJohn Polstra void *closure; 11520ff27d31SJohn Polstra { 11530ff27d31SJohn Polstra struct phdr_closure *phc = (struct phdr_closure *)closure; 11540ff27d31SJohn Polstra Elf_Phdr *phdr = phc->phdr; 11550ff27d31SJohn Polstra 11560ff27d31SJohn Polstra phc->offset = round_page(phc->offset); 11570ff27d31SJohn Polstra 11580ff27d31SJohn Polstra phdr->p_type = PT_LOAD; 11590ff27d31SJohn Polstra phdr->p_offset = phc->offset; 11600ff27d31SJohn Polstra phdr->p_vaddr = entry->start; 11610ff27d31SJohn Polstra phdr->p_paddr = 0; 11620ff27d31SJohn Polstra phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 11630ff27d31SJohn Polstra phdr->p_align = PAGE_SIZE; 1164*ed167eaaSKonstantin Belousov phdr->p_flags = __elfN(untrans_prot)(entry->protection); 11650ff27d31SJohn Polstra 11660ff27d31SJohn Polstra phc->offset += phdr->p_filesz; 11670ff27d31SJohn Polstra phc->phdr++; 11680ff27d31SJohn Polstra } 11690ff27d31SJohn Polstra 11700ff27d31SJohn Polstra /* 11710ff27d31SJohn Polstra * A callback for each_writable_segment() to gather information about 11720ff27d31SJohn Polstra * the number of segments and their total size. 11730ff27d31SJohn Polstra */ 11740ff27d31SJohn Polstra static void 11750ff27d31SJohn Polstra cb_size_segment(entry, closure) 11760ff27d31SJohn Polstra vm_map_entry_t entry; 11770ff27d31SJohn Polstra void *closure; 11780ff27d31SJohn Polstra { 11790ff27d31SJohn Polstra struct sseg_closure *ssc = (struct sseg_closure *)closure; 11800ff27d31SJohn Polstra 11810ff27d31SJohn Polstra ssc->count++; 11820ff27d31SJohn Polstra ssc->size += entry->end - entry->start; 11830ff27d31SJohn Polstra } 11840ff27d31SJohn Polstra 11850ff27d31SJohn Polstra /* 11860ff27d31SJohn Polstra * For each writable segment in the process's memory map, call the given 11870ff27d31SJohn Polstra * function with a pointer to the map entry and some arbitrary 11880ff27d31SJohn Polstra * caller-supplied data. 11890ff27d31SJohn Polstra */ 11900ff27d31SJohn Polstra static void 1191247aba24SMarcel Moolenaar each_writable_segment(td, func, closure) 1192247aba24SMarcel Moolenaar struct thread *td; 11930ff27d31SJohn Polstra segment_callback func; 11940ff27d31SJohn Polstra void *closure; 11950ff27d31SJohn Polstra { 1196247aba24SMarcel Moolenaar struct proc *p = td->td_proc; 11970ff27d31SJohn Polstra vm_map_t map = &p->p_vmspace->vm_map; 11980ff27d31SJohn Polstra vm_map_entry_t entry; 1199976a87a2SAlan Cox vm_object_t backing_object, object; 1200976a87a2SAlan Cox boolean_t ignore_entry; 12010ff27d31SJohn Polstra 1202976a87a2SAlan Cox vm_map_lock_read(map); 12030ff27d31SJohn Polstra for (entry = map->header.next; entry != &map->header; 12040ff27d31SJohn Polstra entry = entry->next) { 1205fa7dd9c5SMatthew Dillon /* 1206fa7dd9c5SMatthew Dillon * Don't dump inaccessible mappings, deal with legacy 1207fa7dd9c5SMatthew Dillon * coredump mode. 1208fa7dd9c5SMatthew Dillon * 1209fa7dd9c5SMatthew Dillon * Note that read-only segments related to the elf binary 1210fa7dd9c5SMatthew Dillon * are marked MAP_ENTRY_NOCOREDUMP now so we no longer 1211fa7dd9c5SMatthew Dillon * need to arbitrarily ignore such segments. 1212fa7dd9c5SMatthew Dillon */ 1213fa7dd9c5SMatthew Dillon if (elf_legacy_coredump) { 1214fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_RW) != VM_PROT_RW) 12150ff27d31SJohn Polstra continue; 1216fa7dd9c5SMatthew Dillon } else { 1217fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_ALL) == 0) 1218fa7dd9c5SMatthew Dillon continue; 1219fa7dd9c5SMatthew Dillon } 12200ff27d31SJohn Polstra 12219730a5daSPaul Saab /* 1222fa7dd9c5SMatthew Dillon * Dont include memory segment in the coredump if 1223fa7dd9c5SMatthew Dillon * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in 1224fa7dd9c5SMatthew Dillon * madvise(2). Do not dump submaps (i.e. parts of the 1225fa7dd9c5SMatthew Dillon * kernel map). 12269730a5daSPaul Saab */ 1227fa7dd9c5SMatthew Dillon if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP)) 12289730a5daSPaul Saab continue; 12299730a5daSPaul Saab 1230976a87a2SAlan Cox if ((object = entry->object.vm_object) == NULL) 12310ff27d31SJohn Polstra continue; 12320ff27d31SJohn Polstra 12330ff27d31SJohn Polstra /* Ignore memory-mapped devices and such things. */ 1234976a87a2SAlan Cox VM_OBJECT_LOCK(object); 1235976a87a2SAlan Cox while ((backing_object = object->backing_object) != NULL) { 1236976a87a2SAlan Cox VM_OBJECT_LOCK(backing_object); 1237976a87a2SAlan Cox VM_OBJECT_UNLOCK(object); 1238976a87a2SAlan Cox object = backing_object; 1239976a87a2SAlan Cox } 1240976a87a2SAlan Cox ignore_entry = object->type != OBJT_DEFAULT && 1241976a87a2SAlan Cox object->type != OBJT_SWAP && object->type != OBJT_VNODE; 1242976a87a2SAlan Cox VM_OBJECT_UNLOCK(object); 1243976a87a2SAlan Cox if (ignore_entry) 12440ff27d31SJohn Polstra continue; 12450ff27d31SJohn Polstra 12460ff27d31SJohn Polstra (*func)(entry, closure); 12470ff27d31SJohn Polstra } 1248976a87a2SAlan Cox vm_map_unlock_read(map); 12490ff27d31SJohn Polstra } 12500ff27d31SJohn Polstra 12510ff27d31SJohn Polstra /* 12520ff27d31SJohn Polstra * Write the core file header to the file, including padding up to 12530ff27d31SJohn Polstra * the page boundary. 12540ff27d31SJohn Polstra */ 12558c64af4fSJohn Polstra static int 1256e7228204SAlfred Perlstein __elfN(corehdr)(td, vp, cred, numsegs, hdr, hdrsize, gzfile) 1257b40ce416SJulian Elischer struct thread *td; 12588c64af4fSJohn Polstra struct vnode *vp; 12598c64af4fSJohn Polstra struct ucred *cred; 12600ff27d31SJohn Polstra int numsegs; 12610ff27d31SJohn Polstra size_t hdrsize; 12620ff27d31SJohn Polstra void *hdr; 1263e7228204SAlfred Perlstein gzFile gzfile; 12648c64af4fSJohn Polstra { 1265911c2be0SMark Peek size_t off; 12668c64af4fSJohn Polstra 12678c64af4fSJohn Polstra /* Fill in the header. */ 12680ff27d31SJohn Polstra bzero(hdr, hdrsize); 12698c64af4fSJohn Polstra off = 0; 1270247aba24SMarcel Moolenaar __elfN(puthdr)(td, hdr, &off, numsegs); 12718c64af4fSJohn Polstra 1272e7228204SAlfred Perlstein if (!gzfile) { 12738c64af4fSJohn Polstra /* Write it to the core file. */ 1274a7cddfedSJake Burkholder return (vn_rdwr_inchunks(UIO_WRITE, vp, hdr, hdrsize, (off_t)0, 12759ca43589SRobert Watson UIO_SYSSPACE, IO_UNIT | IO_DIRECT, cred, NOCRED, NULL, 12766617724cSJeff Roberson td)); 1277e7228204SAlfred Perlstein } else { 1278e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1279e7228204SAlfred Perlstein if (gzwrite(gzfile, hdr, hdrsize) != hdrsize) { 1280e7228204SAlfred Perlstein log(LOG_WARNING, 1281e7228204SAlfred Perlstein "Failed to compress core file header for process" 1282e7228204SAlfred Perlstein " %s.\n", curproc->p_comm); 1283e7228204SAlfred Perlstein return (EFAULT); 1284e7228204SAlfred Perlstein } 1285e7228204SAlfred Perlstein else { 1286e7228204SAlfred Perlstein return (0); 1287e7228204SAlfred Perlstein } 1288e7228204SAlfred Perlstein #else 1289e7228204SAlfred Perlstein panic("shouldn't be here"); 1290e7228204SAlfred Perlstein #endif 1291e7228204SAlfred Perlstein } 1292dada0278SJohn Polstra } 1293dada0278SJohn Polstra 1294841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1295841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h> 1296841c0c7eSNathan Whitehorn 129762919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t; 129862919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t; 129962919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t; 130062919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t; 130162919d78SPeter Wemm typedef struct reg32 elf_gregset_t; 13027f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t; 130362919d78SPeter Wemm #else 130462919d78SPeter Wemm typedef prstatus_t elf_prstatus_t; 130562919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t; 130662919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t; 130762919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t; 130862919d78SPeter Wemm typedef gregset_t elf_gregset_t; 13097f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t; 131062919d78SPeter Wemm #endif 131162919d78SPeter Wemm 13128c64af4fSJohn Polstra static void 1313247aba24SMarcel Moolenaar __elfN(puthdr)(struct thread *td, void *dst, size_t *off, int numsegs) 13148c64af4fSJohn Polstra { 13158c9b7b2cSMarcel Moolenaar struct { 131662919d78SPeter Wemm elf_prstatus_t status; 131762919d78SPeter Wemm elf_prfpregset_t fpregset; 131862919d78SPeter Wemm elf_prpsinfo_t psinfo; 13197f08176eSAttilio Rao elf_thrmisc_t thrmisc; 13208c9b7b2cSMarcel Moolenaar } *tempdata; 132162919d78SPeter Wemm elf_prstatus_t *status; 132262919d78SPeter Wemm elf_prfpregset_t *fpregset; 132362919d78SPeter Wemm elf_prpsinfo_t *psinfo; 13247f08176eSAttilio Rao elf_thrmisc_t *thrmisc; 1325247aba24SMarcel Moolenaar struct proc *p; 1326247aba24SMarcel Moolenaar struct thread *thr; 13278c9b7b2cSMarcel Moolenaar size_t ehoff, noteoff, notesz, phoff; 13288c64af4fSJohn Polstra 1329247aba24SMarcel Moolenaar p = td->td_proc; 1330247aba24SMarcel Moolenaar 13318c64af4fSJohn Polstra ehoff = *off; 13328c64af4fSJohn Polstra *off += sizeof(Elf_Ehdr); 13338c64af4fSJohn Polstra 13348c64af4fSJohn Polstra phoff = *off; 13350ff27d31SJohn Polstra *off += (numsegs + 1) * sizeof(Elf_Phdr); 13368c64af4fSJohn Polstra 13378c64af4fSJohn Polstra noteoff = *off; 13388c9b7b2cSMarcel Moolenaar /* 13398c9b7b2cSMarcel Moolenaar * Don't allocate space for the notes if we're just calculating 13408c9b7b2cSMarcel Moolenaar * the size of the header. We also don't collect the data. 13418c9b7b2cSMarcel Moolenaar */ 13428c9b7b2cSMarcel Moolenaar if (dst != NULL) { 13438c9b7b2cSMarcel Moolenaar tempdata = malloc(sizeof(*tempdata), M_TEMP, M_ZERO|M_WAITOK); 13448c9b7b2cSMarcel Moolenaar status = &tempdata->status; 13458c9b7b2cSMarcel Moolenaar fpregset = &tempdata->fpregset; 13468c9b7b2cSMarcel Moolenaar psinfo = &tempdata->psinfo; 13477f08176eSAttilio Rao thrmisc = &tempdata->thrmisc; 13488c9b7b2cSMarcel Moolenaar } else { 13498c9b7b2cSMarcel Moolenaar tempdata = NULL; 13508c9b7b2cSMarcel Moolenaar status = NULL; 13518c9b7b2cSMarcel Moolenaar fpregset = NULL; 13528c9b7b2cSMarcel Moolenaar psinfo = NULL; 13537f08176eSAttilio Rao thrmisc = NULL; 13548c9b7b2cSMarcel Moolenaar } 13558c9b7b2cSMarcel Moolenaar 13568c9b7b2cSMarcel Moolenaar if (dst != NULL) { 13578c9b7b2cSMarcel Moolenaar psinfo->pr_version = PRPSINFO_VERSION; 135862919d78SPeter Wemm psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t); 1359ccd3953eSJohn Baldwin strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname)); 13608c9b7b2cSMarcel Moolenaar /* 13618c9b7b2cSMarcel Moolenaar * XXX - We don't fill in the command line arguments properly 13628c9b7b2cSMarcel Moolenaar * yet. 13638c9b7b2cSMarcel Moolenaar */ 1364ccd3953eSJohn Baldwin strlcpy(psinfo->pr_psargs, p->p_comm, 13658c9b7b2cSMarcel Moolenaar sizeof(psinfo->pr_psargs)); 13668c9b7b2cSMarcel Moolenaar } 13678c9b7b2cSMarcel Moolenaar __elfN(putnote)(dst, off, "FreeBSD", NT_PRPSINFO, psinfo, 13688c9b7b2cSMarcel Moolenaar sizeof *psinfo); 13698c9b7b2cSMarcel Moolenaar 13708c9b7b2cSMarcel Moolenaar /* 13711f7a1baaSMarcel Moolenaar * To have the debugger select the right thread (LWP) as the initial 13721f7a1baaSMarcel Moolenaar * thread, we dump the state of the thread passed to us in td first. 13731f7a1baaSMarcel Moolenaar * This is the thread that causes the core dump and thus likely to 13741f7a1baaSMarcel Moolenaar * be the right thread one wants to have selected in the debugger. 13758c9b7b2cSMarcel Moolenaar */ 1376247aba24SMarcel Moolenaar thr = td; 1377247aba24SMarcel Moolenaar while (thr != NULL) { 13788c9b7b2cSMarcel Moolenaar if (dst != NULL) { 13798c9b7b2cSMarcel Moolenaar status->pr_version = PRSTATUS_VERSION; 138062919d78SPeter Wemm status->pr_statussz = sizeof(elf_prstatus_t); 138162919d78SPeter Wemm status->pr_gregsetsz = sizeof(elf_gregset_t); 138262919d78SPeter Wemm status->pr_fpregsetsz = sizeof(elf_fpregset_t); 13838c9b7b2cSMarcel Moolenaar status->pr_osreldate = osreldate; 13848c9b7b2cSMarcel Moolenaar status->pr_cursig = p->p_sig; 13851f7a1baaSMarcel Moolenaar status->pr_pid = thr->td_tid; 1386841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 138762919d78SPeter Wemm fill_regs32(thr, &status->pr_reg); 138862919d78SPeter Wemm fill_fpregs32(thr, fpregset); 138962919d78SPeter Wemm #else 13908c9b7b2cSMarcel Moolenaar fill_regs(thr, &status->pr_reg); 13918c9b7b2cSMarcel Moolenaar fill_fpregs(thr, fpregset); 139262919d78SPeter Wemm #endif 13937f08176eSAttilio Rao memset(&thrmisc->_pad, 0, sizeof (thrmisc->_pad)); 13947f08176eSAttilio Rao strcpy(thrmisc->pr_tname, thr->td_name); 13958c9b7b2cSMarcel Moolenaar } 13963ebc1248SPeter Wemm __elfN(putnote)(dst, off, "FreeBSD", NT_PRSTATUS, status, 13978c64af4fSJohn Polstra sizeof *status); 13983ebc1248SPeter Wemm __elfN(putnote)(dst, off, "FreeBSD", NT_FPREGSET, fpregset, 13998c64af4fSJohn Polstra sizeof *fpregset); 14007f08176eSAttilio Rao __elfN(putnote)(dst, off, "FreeBSD", NT_THRMISC, thrmisc, 14017f08176eSAttilio Rao sizeof *thrmisc); 14024da47b2fSMarcel Moolenaar /* 14034da47b2fSMarcel Moolenaar * Allow for MD specific notes, as well as any MD 14044da47b2fSMarcel Moolenaar * specific preparations for writing MI notes. 14054da47b2fSMarcel Moolenaar */ 14064da47b2fSMarcel Moolenaar __elfN(dump_thread)(thr, dst, off); 1407247aba24SMarcel Moolenaar 1408247aba24SMarcel Moolenaar thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) : 14098c9b7b2cSMarcel Moolenaar TAILQ_NEXT(thr, td_plist); 1410247aba24SMarcel Moolenaar if (thr == td) 14118c9b7b2cSMarcel Moolenaar thr = TAILQ_NEXT(thr, td_plist); 1412247aba24SMarcel Moolenaar } 14138c9b7b2cSMarcel Moolenaar 14148c64af4fSJohn Polstra notesz = *off - noteoff; 14158c64af4fSJohn Polstra 14168c9b7b2cSMarcel Moolenaar if (dst != NULL) 14178c9b7b2cSMarcel Moolenaar free(tempdata, M_TEMP); 14188c9b7b2cSMarcel Moolenaar 14190ff27d31SJohn Polstra /* Align up to a page boundary for the program segments. */ 14208c64af4fSJohn Polstra *off = round_page(*off); 14218c64af4fSJohn Polstra 14228c64af4fSJohn Polstra if (dst != NULL) { 14238c64af4fSJohn Polstra Elf_Ehdr *ehdr; 14248c64af4fSJohn Polstra Elf_Phdr *phdr; 14250ff27d31SJohn Polstra struct phdr_closure phc; 14268c64af4fSJohn Polstra 14278c64af4fSJohn Polstra /* 14288c64af4fSJohn Polstra * Fill in the ELF header. 14298c64af4fSJohn Polstra */ 14308c64af4fSJohn Polstra ehdr = (Elf_Ehdr *)((char *)dst + ehoff); 14318c64af4fSJohn Polstra ehdr->e_ident[EI_MAG0] = ELFMAG0; 14328c64af4fSJohn Polstra ehdr->e_ident[EI_MAG1] = ELFMAG1; 14338c64af4fSJohn Polstra ehdr->e_ident[EI_MAG2] = ELFMAG2; 14348c64af4fSJohn Polstra ehdr->e_ident[EI_MAG3] = ELFMAG3; 14358c64af4fSJohn Polstra ehdr->e_ident[EI_CLASS] = ELF_CLASS; 14368c64af4fSJohn Polstra ehdr->e_ident[EI_DATA] = ELF_DATA; 14378c64af4fSJohn Polstra ehdr->e_ident[EI_VERSION] = EV_CURRENT; 1438c815a20cSDavid E. O'Brien ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 1439c815a20cSDavid E. O'Brien ehdr->e_ident[EI_ABIVERSION] = 0; 14408c64af4fSJohn Polstra ehdr->e_ident[EI_PAD] = 0; 14418c64af4fSJohn Polstra ehdr->e_type = ET_CORE; 1442841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1443841c0c7eSNathan Whitehorn ehdr->e_machine = ELF_ARCH32; 144462919d78SPeter Wemm #else 14458c64af4fSJohn Polstra ehdr->e_machine = ELF_ARCH; 144662919d78SPeter Wemm #endif 14478c64af4fSJohn Polstra ehdr->e_version = EV_CURRENT; 14488c64af4fSJohn Polstra ehdr->e_entry = 0; 14498c64af4fSJohn Polstra ehdr->e_phoff = phoff; 14508c64af4fSJohn Polstra ehdr->e_flags = 0; 14518c64af4fSJohn Polstra ehdr->e_ehsize = sizeof(Elf_Ehdr); 14528c64af4fSJohn Polstra ehdr->e_phentsize = sizeof(Elf_Phdr); 14530ff27d31SJohn Polstra ehdr->e_phnum = numsegs + 1; 14548c64af4fSJohn Polstra ehdr->e_shentsize = sizeof(Elf_Shdr); 14558c64af4fSJohn Polstra ehdr->e_shnum = 0; 14568c64af4fSJohn Polstra ehdr->e_shstrndx = SHN_UNDEF; 14578c64af4fSJohn Polstra 14588c64af4fSJohn Polstra /* 14598c64af4fSJohn Polstra * Fill in the program header entries. 14608c64af4fSJohn Polstra */ 14618c64af4fSJohn Polstra phdr = (Elf_Phdr *)((char *)dst + phoff); 14628c64af4fSJohn Polstra 14638c64af4fSJohn Polstra /* The note segement. */ 14648c64af4fSJohn Polstra phdr->p_type = PT_NOTE; 14658c64af4fSJohn Polstra phdr->p_offset = noteoff; 14668c64af4fSJohn Polstra phdr->p_vaddr = 0; 14678c64af4fSJohn Polstra phdr->p_paddr = 0; 14688c64af4fSJohn Polstra phdr->p_filesz = notesz; 14698c64af4fSJohn Polstra phdr->p_memsz = 0; 14708c64af4fSJohn Polstra phdr->p_flags = 0; 14718c64af4fSJohn Polstra phdr->p_align = 0; 14728c64af4fSJohn Polstra phdr++; 14738c64af4fSJohn Polstra 14740ff27d31SJohn Polstra /* All the writable segments from the program. */ 14750ff27d31SJohn Polstra phc.phdr = phdr; 14760ff27d31SJohn Polstra phc.offset = *off; 1477247aba24SMarcel Moolenaar each_writable_segment(td, cb_put_phdr, &phc); 14788c64af4fSJohn Polstra } 14798c64af4fSJohn Polstra } 14808c64af4fSJohn Polstra 14818c64af4fSJohn Polstra static void 14823ebc1248SPeter Wemm __elfN(putnote)(void *dst, size_t *off, const char *name, int type, 14838c64af4fSJohn Polstra const void *desc, size_t descsz) 14848c64af4fSJohn Polstra { 14858c64af4fSJohn Polstra Elf_Note note; 14868c64af4fSJohn Polstra 14878c64af4fSJohn Polstra note.n_namesz = strlen(name) + 1; 14888c64af4fSJohn Polstra note.n_descsz = descsz; 14898c64af4fSJohn Polstra note.n_type = type; 14908c64af4fSJohn Polstra if (dst != NULL) 14918c64af4fSJohn Polstra bcopy(¬e, (char *)dst + *off, sizeof note); 14928c64af4fSJohn Polstra *off += sizeof note; 14938c64af4fSJohn Polstra if (dst != NULL) 14948c64af4fSJohn Polstra bcopy(name, (char *)dst + *off, note.n_namesz); 14958c64af4fSJohn Polstra *off += roundup2(note.n_namesz, sizeof(Elf_Size)); 14968c64af4fSJohn Polstra if (dst != NULL) 14978c64af4fSJohn Polstra bcopy(desc, (char *)dst + *off, note.n_descsz); 14988c64af4fSJohn Polstra *off += roundup2(note.n_descsz, sizeof(Elf_Size)); 14998c64af4fSJohn Polstra } 15008c64af4fSJohn Polstra 15018c64af4fSJohn Polstra /* 150232c01de2SDmitry Chagin * Try to find the appropriate ABI-note section for checknote, 150332c01de2SDmitry Chagin * fetch the osreldate for binary from the ELF OSABI-note. Only the 150432c01de2SDmitry Chagin * first page of the image is searched, the same as for headers. 150532c01de2SDmitry Chagin */ 150632c01de2SDmitry Chagin static boolean_t 150732c01de2SDmitry Chagin __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote, 150832c01de2SDmitry Chagin int32_t *osrel) 150932c01de2SDmitry Chagin { 1510267c52fcSKonstantin Belousov const Elf_Note *note, *note0, *note_end; 1511429f5a58SKonstantin Belousov const Elf_Phdr *phdr, *pnote; 1512429f5a58SKonstantin Belousov const Elf_Ehdr *hdr; 151332c01de2SDmitry Chagin const char *note_name; 151432c01de2SDmitry Chagin int i; 151532c01de2SDmitry Chagin 151632c01de2SDmitry Chagin pnote = NULL; 1517429f5a58SKonstantin Belousov hdr = (const Elf_Ehdr *)imgp->image_header; 1518429f5a58SKonstantin Belousov phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 151932c01de2SDmitry Chagin 152032c01de2SDmitry Chagin for (i = 0; i < hdr->e_phnum; i++) { 152132c01de2SDmitry Chagin if (phdr[i].p_type == PT_NOTE) { 152232c01de2SDmitry Chagin pnote = &phdr[i]; 152332c01de2SDmitry Chagin break; 152432c01de2SDmitry Chagin } 152532c01de2SDmitry Chagin } 152632c01de2SDmitry Chagin 152732c01de2SDmitry Chagin if (pnote == NULL || pnote->p_offset >= PAGE_SIZE || 152832c01de2SDmitry Chagin pnote->p_offset + pnote->p_filesz >= PAGE_SIZE) 152932c01de2SDmitry Chagin return (FALSE); 153032c01de2SDmitry Chagin 1531267c52fcSKonstantin Belousov note = note0 = (const Elf_Note *)(imgp->image_header + pnote->p_offset); 153232c01de2SDmitry Chagin note_end = (const Elf_Note *)(imgp->image_header + 153332c01de2SDmitry Chagin pnote->p_offset + pnote->p_filesz); 1534267c52fcSKonstantin Belousov for (i = 0; i < 100 && note >= note0 && note < note_end; i++) { 1535267c52fcSKonstantin Belousov if (!aligned(note, Elf32_Addr)) 1536267c52fcSKonstantin Belousov return (FALSE); 153732c01de2SDmitry Chagin if (note->n_namesz != checknote->hdr.n_namesz || 153832c01de2SDmitry Chagin note->n_descsz != checknote->hdr.n_descsz || 153932c01de2SDmitry Chagin note->n_type != checknote->hdr.n_type) 154032c01de2SDmitry Chagin goto nextnote; 154132c01de2SDmitry Chagin note_name = (const char *)(note + 1); 154232c01de2SDmitry Chagin if (strncmp(checknote->vendor, note_name, 154332c01de2SDmitry Chagin checknote->hdr.n_namesz) != 0) 154432c01de2SDmitry Chagin goto nextnote; 154532c01de2SDmitry Chagin 154632c01de2SDmitry Chagin /* 154732c01de2SDmitry Chagin * Fetch the osreldate for binary 154832c01de2SDmitry Chagin * from the ELF OSABI-note if necessary. 154932c01de2SDmitry Chagin */ 155089ffc202SBjoern A. Zeeb if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 && 155189ffc202SBjoern A. Zeeb checknote->trans_osrel != NULL) 155289ffc202SBjoern A. Zeeb return (checknote->trans_osrel(note, osrel)); 155332c01de2SDmitry Chagin return (TRUE); 155432c01de2SDmitry Chagin 155532c01de2SDmitry Chagin nextnote: 155632c01de2SDmitry Chagin note = (const Elf_Note *)((const char *)(note + 1) + 155732c01de2SDmitry Chagin roundup2(note->n_namesz, sizeof(Elf32_Addr)) + 155832c01de2SDmitry Chagin roundup2(note->n_descsz, sizeof(Elf32_Addr))); 155932c01de2SDmitry Chagin } 156032c01de2SDmitry Chagin 156132c01de2SDmitry Chagin return (FALSE); 156232c01de2SDmitry Chagin } 156332c01de2SDmitry Chagin 156432c01de2SDmitry Chagin /* 1565e1743d02SSøren Schmidt * Tell kern_execve.c about it, with a little help from the linker. 1566e1743d02SSøren Schmidt */ 1567a360a43dSJake Burkholder static struct execsw __elfN(execsw) = { 1568a360a43dSJake Burkholder __CONCAT(exec_, __elfN(imgact)), 1569a360a43dSJake Burkholder __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 1570a360a43dSJake Burkholder }; 1571a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw)); 1572e7228204SAlfred Perlstein 1573e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1574e7228204SAlfred Perlstein /* 1575e7228204SAlfred Perlstein * Compress and write out a core segment for a user process. 1576e7228204SAlfred Perlstein * 1577e7228204SAlfred Perlstein * 'inbuf' is the starting address of a VM segment in the process' address 1578e7228204SAlfred Perlstein * space that is to be compressed and written out to the core file. 'dest_buf' 1579e7228204SAlfred Perlstein * is a buffer in the kernel's address space. The segment is copied from 1580e7228204SAlfred Perlstein * 'inbuf' to 'dest_buf' first before being processed by the compression 1581e7228204SAlfred Perlstein * routine gzwrite(). This copying is necessary because the content of the VM 1582e7228204SAlfred Perlstein * segment may change between the compression pass and the crc-computation pass 1583e7228204SAlfred Perlstein * in gzwrite(). This is because realtime threads may preempt the UNIX kernel. 1584e7228204SAlfred Perlstein */ 1585e7228204SAlfred Perlstein static int 1586e7228204SAlfred Perlstein compress_core (gzFile file, char *inbuf, char *dest_buf, unsigned int len, 1587e7228204SAlfred Perlstein struct thread *td) 1588e7228204SAlfred Perlstein { 1589e7228204SAlfred Perlstein int len_compressed; 1590e7228204SAlfred Perlstein int error = 0; 1591e7228204SAlfred Perlstein unsigned int chunk_len; 1592e7228204SAlfred Perlstein 1593e7228204SAlfred Perlstein while (len) { 1594e7228204SAlfred Perlstein chunk_len = (len > CORE_BUF_SIZE) ? CORE_BUF_SIZE : len; 1595e7228204SAlfred Perlstein copyin(inbuf, dest_buf, chunk_len); 1596e7228204SAlfred Perlstein len_compressed = gzwrite(file, dest_buf, chunk_len); 1597e7228204SAlfred Perlstein 1598e7228204SAlfred Perlstein EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed); 1599e7228204SAlfred Perlstein 1600e7228204SAlfred Perlstein if ((unsigned int)len_compressed != chunk_len) { 1601e7228204SAlfred Perlstein log(LOG_WARNING, 1602e7228204SAlfred Perlstein "compress_core: length mismatch (0x%x returned, " 1603e7228204SAlfred Perlstein "0x%x expected)\n", len_compressed, chunk_len); 1604e7228204SAlfred Perlstein EVENTHANDLER_INVOKE(app_coredump_error, td, 1605e7228204SAlfred Perlstein "compress_core: length mismatch %x -> %x", 1606e7228204SAlfred Perlstein chunk_len, len_compressed); 1607e7228204SAlfred Perlstein error = EFAULT; 1608e7228204SAlfred Perlstein break; 1609e7228204SAlfred Perlstein } 1610e7228204SAlfred Perlstein inbuf += chunk_len; 1611e7228204SAlfred Perlstein len -= chunk_len; 1612e7228204SAlfred Perlstein if (ticks - PCPU_GET(switchticks) >= hogticks) 1613e7228204SAlfred Perlstein uio_yield(); 1614e7228204SAlfred Perlstein } 1615e7228204SAlfred Perlstein 1616e7228204SAlfred Perlstein return (error); 1617e7228204SAlfred Perlstein } 1618e7228204SAlfred Perlstein #endif /* COMPRESS_USER_CORES */ 1619*ed167eaaSKonstantin Belousov 1620*ed167eaaSKonstantin Belousov static vm_prot_t 1621*ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags) 1622*ed167eaaSKonstantin Belousov { 1623*ed167eaaSKonstantin Belousov vm_prot_t prot; 1624*ed167eaaSKonstantin Belousov 1625*ed167eaaSKonstantin Belousov prot = 0; 1626*ed167eaaSKonstantin Belousov if (flags & PF_X) 1627*ed167eaaSKonstantin Belousov prot |= VM_PROT_EXECUTE; 1628*ed167eaaSKonstantin Belousov if (flags & PF_W) 1629*ed167eaaSKonstantin Belousov prot |= VM_PROT_WRITE; 1630*ed167eaaSKonstantin Belousov if (flags & PF_R) 1631*ed167eaaSKonstantin Belousov prot |= VM_PROT_READ; 1632*ed167eaaSKonstantin Belousov return (prot); 1633*ed167eaaSKonstantin Belousov } 1634*ed167eaaSKonstantin Belousov 1635*ed167eaaSKonstantin Belousov static Elf_Word 1636*ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot) 1637*ed167eaaSKonstantin Belousov { 1638*ed167eaaSKonstantin Belousov Elf_Word flags; 1639*ed167eaaSKonstantin Belousov 1640*ed167eaaSKonstantin Belousov flags = 0; 1641*ed167eaaSKonstantin Belousov if (prot & VM_PROT_EXECUTE) 1642*ed167eaaSKonstantin Belousov flags |= PF_X; 1643*ed167eaaSKonstantin Belousov if (prot & VM_PROT_READ) 1644*ed167eaaSKonstantin Belousov flags |= PF_R; 1645*ed167eaaSKonstantin Belousov if (prot & VM_PROT_WRITE) 1646*ed167eaaSKonstantin Belousov flags |= PF_W; 1647*ed167eaaSKonstantin Belousov return (flags); 1648*ed167eaaSKonstantin Belousov } 1649