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); 95ed167eaaSKonstantin Belousov static vm_prot_t __elfN(trans_prot)(Elf_Word); 96ed167eaaSKonstantin 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 118*291c06a1SKonstantin Belousov static int __elfN(nxstack) = 0; 119*291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, 120*291c06a1SKonstantin Belousov nxstack, CTLFLAG_RW, &__elfN(nxstack), 0, 121*291c06a1SKonstantin Belousov __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack"); 122*291c06a1SKonstantin Belousov 1233ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS]; 124e1743d02SSøren Schmidt 12593d1c728SKonstantin Belousov #define trunc_page_ps(va, ps) ((va) & ~(ps - 1)) 12693d1c728SKonstantin Belousov #define round_page_ps(va, ps) (((va) + (ps - 1)) & ~(ps - 1)) 12793d1c728SKonstantin Belousov #define aligned(a, t) (trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a)) 12893d1c728SKonstantin Belousov 12932c01de2SDmitry Chagin static const char FREEBSD_ABI_VENDOR[] = "FreeBSD"; 13032c01de2SDmitry Chagin 13132c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = { 13232c01de2SDmitry Chagin .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR), 13332c01de2SDmitry Chagin .hdr.n_descsz = sizeof(int32_t), 13432c01de2SDmitry Chagin .hdr.n_type = 1, 13532c01de2SDmitry Chagin .vendor = FREEBSD_ABI_VENDOR, 13689ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 13789ffc202SBjoern A. Zeeb .trans_osrel = __elfN(freebsd_trans_osrel) 13832c01de2SDmitry Chagin }; 13932c01de2SDmitry Chagin 14089ffc202SBjoern A. Zeeb static boolean_t 14189ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel) 14289ffc202SBjoern A. Zeeb { 14389ffc202SBjoern A. Zeeb uintptr_t p; 14489ffc202SBjoern A. Zeeb 14589ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 14689ffc202SBjoern A. Zeeb p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 14789ffc202SBjoern A. Zeeb *osrel = *(const int32_t *)(p); 14889ffc202SBjoern A. Zeeb 14989ffc202SBjoern A. Zeeb return (TRUE); 15089ffc202SBjoern A. Zeeb } 15189ffc202SBjoern A. Zeeb 15289ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU"; 15389ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3; 15489ffc202SBjoern A. Zeeb 15589ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = { 15689ffc202SBjoern A. Zeeb .hdr.n_namesz = sizeof(GNU_ABI_VENDOR), 15789ffc202SBjoern A. Zeeb .hdr.n_descsz = 16, /* XXX at least 16 */ 15889ffc202SBjoern A. Zeeb .hdr.n_type = 1, 15989ffc202SBjoern A. Zeeb .vendor = GNU_ABI_VENDOR, 16089ffc202SBjoern A. Zeeb .flags = BN_TRANSLATE_OSREL, 16189ffc202SBjoern A. Zeeb .trans_osrel = kfreebsd_trans_osrel 16289ffc202SBjoern A. Zeeb }; 16389ffc202SBjoern A. Zeeb 16489ffc202SBjoern A. Zeeb static boolean_t 16589ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel) 16689ffc202SBjoern A. Zeeb { 16789ffc202SBjoern A. Zeeb const Elf32_Word *desc; 16889ffc202SBjoern A. Zeeb uintptr_t p; 16989ffc202SBjoern A. Zeeb 17089ffc202SBjoern A. Zeeb p = (uintptr_t)(note + 1); 17189ffc202SBjoern A. Zeeb p += roundup2(note->n_namesz, sizeof(Elf32_Addr)); 17289ffc202SBjoern A. Zeeb 17389ffc202SBjoern A. Zeeb desc = (const Elf32_Word *)p; 17489ffc202SBjoern A. Zeeb if (desc[0] != GNU_KFREEBSD_ABI_DESC) 17589ffc202SBjoern A. Zeeb return (FALSE); 17689ffc202SBjoern A. Zeeb 17789ffc202SBjoern A. Zeeb /* 17889ffc202SBjoern A. Zeeb * Debian GNU/kFreeBSD embed the earliest compatible kernel version 17989ffc202SBjoern A. Zeeb * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way. 18089ffc202SBjoern A. Zeeb */ 18189ffc202SBjoern A. Zeeb *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3]; 18289ffc202SBjoern A. Zeeb 18389ffc202SBjoern A. Zeeb return (TRUE); 18489ffc202SBjoern A. Zeeb } 18589ffc202SBjoern A. Zeeb 186e1743d02SSøren Schmidt int 1873ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry) 188e1743d02SSøren Schmidt { 189e1743d02SSøren Schmidt int i; 190e1743d02SSøren Schmidt 1913ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 192ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == NULL) { 193ea5a2b2eSSøren Schmidt elf_brand_list[i] = entry; 194e1743d02SSøren Schmidt break; 195e1743d02SSøren Schmidt } 196e1743d02SSøren Schmidt } 197925c8b5bSBjoern A. Zeeb if (i == MAX_BRANDS) { 198925c8b5bSBjoern A. Zeeb printf("WARNING: %s: could not insert brandinfo entry: %p\n", 199925c8b5bSBjoern A. Zeeb __func__, entry); 200a7cddfedSJake Burkholder return (-1); 201925c8b5bSBjoern A. Zeeb } 202a7cddfedSJake Burkholder return (0); 203e1743d02SSøren Schmidt } 204e1743d02SSøren Schmidt 205e1743d02SSøren Schmidt int 2063ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry) 207e1743d02SSøren Schmidt { 208e1743d02SSøren Schmidt int i; 209e1743d02SSøren Schmidt 2103ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 211ea5a2b2eSSøren Schmidt if (elf_brand_list[i] == entry) { 212ea5a2b2eSSøren Schmidt elf_brand_list[i] = NULL; 213e1743d02SSøren Schmidt break; 214e1743d02SSøren Schmidt } 215e1743d02SSøren Schmidt } 216ea5a2b2eSSøren Schmidt if (i == MAX_BRANDS) 217a7cddfedSJake Burkholder return (-1); 218a7cddfedSJake Burkholder return (0); 219e1743d02SSøren Schmidt } 220e1743d02SSøren Schmidt 221096977faSMark Newton int 2223ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry) 223096977faSMark Newton { 224096977faSMark Newton struct proc *p; 225553629ebSJake Burkholder int rval = FALSE; 226096977faSMark Newton 2271005a129SJohn Baldwin sx_slock(&allproc_lock); 2284f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 229553629ebSJake Burkholder if (p->p_sysent == entry->sysvec) { 230553629ebSJake Burkholder rval = TRUE; 231553629ebSJake Burkholder break; 232096977faSMark Newton } 233553629ebSJake Burkholder } 2341005a129SJohn Baldwin sx_sunlock(&allproc_lock); 235096977faSMark Newton 236553629ebSJake Burkholder return (rval); 237096977faSMark Newton } 238096977faSMark Newton 2395fe3ed62SJake Burkholder static Elf_Brandinfo * 24032c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, 24132c01de2SDmitry Chagin int32_t *osrel) 2425fe3ed62SJake Burkholder { 24332c01de2SDmitry Chagin const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 2445fe3ed62SJake Burkholder Elf_Brandinfo *bi; 24532c01de2SDmitry Chagin boolean_t ret; 2465fe3ed62SJake Burkholder int i; 2475fe3ed62SJake Burkholder 2485fe3ed62SJake Burkholder /* 24932c01de2SDmitry Chagin * We support four types of branding -- (1) the ELF EI_OSABI field 2505fe3ed62SJake Burkholder * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string 25132c01de2SDmitry Chagin * branding w/in the ELF header, (3) path of the `interp_path' 25232c01de2SDmitry Chagin * field, and (4) the ".note.ABI-tag" ELF section. 2535fe3ed62SJake Burkholder */ 2545fe3ed62SJake Burkholder 25532c01de2SDmitry Chagin /* Look for an ".note.ABI-tag" ELF section */ 25632c01de2SDmitry Chagin for (i = 0; i < MAX_BRANDS; i++) { 25732c01de2SDmitry Chagin bi = elf_brand_list[i]; 258ecc2fda8SBjoern A. Zeeb if (bi == NULL) 259ecc2fda8SBjoern A. Zeeb continue; 260ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && (bi->flags & 261ecc2fda8SBjoern A. Zeeb (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) { 26232c01de2SDmitry Chagin ret = __elfN(check_note)(imgp, bi->brand_note, osrel); 26332c01de2SDmitry Chagin if (ret) 26432c01de2SDmitry Chagin return (bi); 26532c01de2SDmitry Chagin } 26632c01de2SDmitry Chagin } 26732c01de2SDmitry Chagin 2685fe3ed62SJake Burkholder /* If the executable has a brand, search for it in the brand list. */ 2695fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 2705fe3ed62SJake Burkholder bi = elf_brand_list[i]; 271ecc2fda8SBjoern A. Zeeb if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 272ecc2fda8SBjoern A. Zeeb continue; 273ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 2745fe3ed62SJake Burkholder (hdr->e_ident[EI_OSABI] == bi->brand || 2755fe3ed62SJake Burkholder strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND], 2765fe3ed62SJake Burkholder bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0)) 2775fe3ed62SJake Burkholder return (bi); 2785fe3ed62SJake Burkholder } 2795fe3ed62SJake Burkholder 2805fe3ed62SJake Burkholder /* Lacking a known brand, search for a recognized interpreter. */ 2815fe3ed62SJake Burkholder if (interp != NULL) { 2825fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 2835fe3ed62SJake Burkholder bi = elf_brand_list[i]; 284ecc2fda8SBjoern A. Zeeb if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 285ecc2fda8SBjoern A. Zeeb continue; 286ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 2875fe3ed62SJake Burkholder strcmp(interp, bi->interp_path) == 0) 2885fe3ed62SJake Burkholder return (bi); 2895fe3ed62SJake Burkholder } 2905fe3ed62SJake Burkholder } 2915fe3ed62SJake Burkholder 2925fe3ed62SJake Burkholder /* Lacking a recognized interpreter, try the default brand */ 2935fe3ed62SJake Burkholder for (i = 0; i < MAX_BRANDS; i++) { 2945fe3ed62SJake Burkholder bi = elf_brand_list[i]; 295ecc2fda8SBjoern A. Zeeb if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) 296ecc2fda8SBjoern A. Zeeb continue; 297ecc2fda8SBjoern A. Zeeb if (hdr->e_machine == bi->machine && 298e548a1d4SJake Burkholder __elfN(fallback_brand) == bi->brand) 2995fe3ed62SJake Burkholder return (bi); 3005fe3ed62SJake Burkholder } 3015fe3ed62SJake Burkholder return (NULL); 3025fe3ed62SJake Burkholder } 3035fe3ed62SJake Burkholder 304e1743d02SSøren Schmidt static int 3053ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr) 306e1743d02SSøren Schmidt { 307d0ca7c29SPeter Wemm Elf_Brandinfo *bi; 3083ebc1248SPeter Wemm int i; 3093ebc1248SPeter Wemm 31052c24af7SPeter Wemm if (!IS_ELF(*hdr) || 31152c24af7SPeter Wemm hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 31252c24af7SPeter Wemm hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 3133dc19c46SJacques Vidrine hdr->e_ident[EI_VERSION] != EV_CURRENT || 3143dc19c46SJacques Vidrine hdr->e_phentsize != sizeof(Elf_Phdr) || 3153dc19c46SJacques Vidrine hdr->e_version != ELF_TARG_VER) 316a7cddfedSJake Burkholder return (ENOEXEC); 317e1743d02SSøren Schmidt 3183ebc1248SPeter Wemm /* 3193ebc1248SPeter Wemm * Make sure we have at least one brand for this machine. 3203ebc1248SPeter Wemm */ 3213ebc1248SPeter Wemm 3223ebc1248SPeter Wemm for (i = 0; i < MAX_BRANDS; i++) { 323d0ca7c29SPeter Wemm bi = elf_brand_list[i]; 324d0ca7c29SPeter Wemm if (bi != NULL && bi->machine == hdr->e_machine) 3253ebc1248SPeter Wemm break; 3263ebc1248SPeter Wemm } 3273ebc1248SPeter Wemm if (i == MAX_BRANDS) 328a7cddfedSJake Burkholder return (ENOEXEC); 329e1743d02SSøren Schmidt 330a7cddfedSJake Burkholder return (0); 331e1743d02SSøren Schmidt } 332e1743d02SSøren Schmidt 333e1743d02SSøren Schmidt static int 3343ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 335ff6f03c7SAlan Cox vm_offset_t start, vm_offset_t end, vm_prot_t prot) 3363ebc1248SPeter Wemm { 337da61b9a6SAlan Cox struct sf_buf *sf; 338da61b9a6SAlan Cox int error; 3393ebc1248SPeter Wemm vm_offset_t off; 3403ebc1248SPeter Wemm 3413ebc1248SPeter Wemm /* 3423ebc1248SPeter Wemm * Create the page if it doesn't exist yet. Ignore errors. 3433ebc1248SPeter Wemm */ 3443ebc1248SPeter Wemm vm_map_lock(map); 345ff6f03c7SAlan Cox vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end), 346ff6f03c7SAlan Cox VM_PROT_ALL, VM_PROT_ALL, 0); 3473ebc1248SPeter Wemm vm_map_unlock(map); 3483ebc1248SPeter Wemm 3493ebc1248SPeter Wemm /* 3503ebc1248SPeter Wemm * Find the page from the underlying object. 3513ebc1248SPeter Wemm */ 3523ebc1248SPeter Wemm if (object) { 353da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 354da61b9a6SAlan Cox if (sf == NULL) 355da61b9a6SAlan Cox return (KERN_FAILURE); 3563ebc1248SPeter Wemm off = offset - trunc_page(offset); 357da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start, 358ca0387efSJake Burkholder end - start); 359da61b9a6SAlan Cox vm_imgact_unmap_page(sf); 3603ebc1248SPeter Wemm if (error) { 361a7cddfedSJake Burkholder return (KERN_FAILURE); 3623ebc1248SPeter Wemm } 3633ebc1248SPeter Wemm } 3643ebc1248SPeter Wemm 365a7cddfedSJake Burkholder return (KERN_SUCCESS); 3663ebc1248SPeter Wemm } 3673ebc1248SPeter Wemm 3683ebc1248SPeter Wemm static int 3693ebc1248SPeter Wemm __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 370ff6f03c7SAlan Cox vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow) 3713ebc1248SPeter Wemm { 372da61b9a6SAlan Cox struct sf_buf *sf; 373da61b9a6SAlan Cox vm_offset_t off; 374a063facbSMarcel Moolenaar vm_size_t sz; 375a063facbSMarcel Moolenaar int error, rv; 3763ebc1248SPeter Wemm 3773ebc1248SPeter Wemm if (start != trunc_page(start)) { 37881f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset, start, 379ff6f03c7SAlan Cox round_page(start), prot); 3803ebc1248SPeter Wemm if (rv) 381a7cddfedSJake Burkholder return (rv); 3823ebc1248SPeter Wemm offset += round_page(start) - start; 3833ebc1248SPeter Wemm start = round_page(start); 3843ebc1248SPeter Wemm } 3853ebc1248SPeter Wemm if (end != round_page(end)) { 38681f223caSJake Burkholder rv = __elfN(map_partial)(map, object, offset + 387ff6f03c7SAlan Cox trunc_page(end) - start, trunc_page(end), end, prot); 3883ebc1248SPeter Wemm if (rv) 389a7cddfedSJake Burkholder return (rv); 3903ebc1248SPeter Wemm end = trunc_page(end); 3913ebc1248SPeter Wemm } 3923ebc1248SPeter Wemm if (end > start) { 3933ebc1248SPeter Wemm if (offset & PAGE_MASK) { 3943ebc1248SPeter Wemm /* 3953ebc1248SPeter Wemm * The mapping is not page aligned. This means we have 3963ebc1248SPeter Wemm * to copy the data. Sigh. 3973ebc1248SPeter Wemm */ 398584716b0SAlan Cox rv = vm_map_find(map, NULL, 0, &start, end - start, 399ff6f03c7SAlan Cox FALSE, prot | VM_PROT_WRITE, VM_PROT_ALL, 0); 4003ebc1248SPeter Wemm if (rv) 401a7cddfedSJake Burkholder return (rv); 402da61b9a6SAlan Cox if (object == NULL) 403da61b9a6SAlan Cox return (KERN_SUCCESS); 404da61b9a6SAlan Cox for (; start < end; start += sz) { 405da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset); 406da61b9a6SAlan Cox if (sf == NULL) 407da61b9a6SAlan Cox return (KERN_FAILURE); 4083ebc1248SPeter Wemm off = offset - trunc_page(offset); 4093ebc1248SPeter Wemm sz = end - start; 410da61b9a6SAlan Cox if (sz > PAGE_SIZE - off) 411da61b9a6SAlan Cox sz = PAGE_SIZE - off; 412da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, 4133ebc1248SPeter Wemm (caddr_t)start, sz); 414da61b9a6SAlan Cox vm_imgact_unmap_page(sf); 4153ebc1248SPeter Wemm if (error) { 416a7cddfedSJake Burkholder return (KERN_FAILURE); 4173ebc1248SPeter Wemm } 418da61b9a6SAlan Cox offset += sz; 4193ebc1248SPeter Wemm } 4203ebc1248SPeter Wemm rv = KERN_SUCCESS; 4213ebc1248SPeter Wemm } else { 422e5e6093bSAlan Cox vm_object_reference(object); 4233ebc1248SPeter Wemm vm_map_lock(map); 4243ebc1248SPeter Wemm rv = vm_map_insert(map, object, offset, start, end, 425ff6f03c7SAlan Cox prot, VM_PROT_ALL, cow); 4263ebc1248SPeter Wemm vm_map_unlock(map); 427e5e6093bSAlan Cox if (rv != KERN_SUCCESS) 428e5e6093bSAlan Cox vm_object_deallocate(object); 4293ebc1248SPeter Wemm } 430a7cddfedSJake Burkholder return (rv); 4313ebc1248SPeter Wemm } else { 432a7cddfedSJake Burkholder return (KERN_SUCCESS); 4333ebc1248SPeter Wemm } 4343ebc1248SPeter Wemm } 4353ebc1248SPeter Wemm 4363ebc1248SPeter Wemm static int 437373d1a3fSAlan Cox __elfN(load_section)(struct vmspace *vmspace, 438373d1a3fSAlan Cox vm_object_t object, vm_offset_t offset, 4393ebc1248SPeter Wemm caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot, 4403ebc1248SPeter Wemm size_t pagesize) 441e1743d02SSøren Schmidt { 442da61b9a6SAlan Cox struct sf_buf *sf; 443e1743d02SSøren Schmidt size_t map_len; 444e1743d02SSøren Schmidt vm_offset_t map_addr; 445fa7dd9c5SMatthew Dillon int error, rv, cow; 446e1743d02SSøren Schmidt size_t copy_len; 44752c24af7SPeter Wemm vm_offset_t file_addr; 44852c24af7SPeter Wemm 44925ead034SBrian Feldman /* 45025ead034SBrian Feldman * It's necessary to fail if the filsz + offset taken from the 45125ead034SBrian Feldman * header is greater than the actual file pager object's size. 45225ead034SBrian Feldman * If we were to allow this, then the vm_map_find() below would 45325ead034SBrian Feldman * walk right off the end of the file object and into the ether. 45425ead034SBrian Feldman * 45525ead034SBrian Feldman * While I'm here, might as well check for something else that 45625ead034SBrian Feldman * is invalid: filsz cannot be greater than memsz. 45725ead034SBrian Feldman */ 45825ead034SBrian Feldman if ((off_t)filsz + offset > object->un_pager.vnp.vnp_size || 45925ead034SBrian Feldman filsz > memsz) { 46025ead034SBrian Feldman uprintf("elf_load_section: truncated ELF file\n"); 46125ead034SBrian Feldman return (ENOEXEC); 46225ead034SBrian Feldman } 46325ead034SBrian Feldman 4643ebc1248SPeter Wemm map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize); 4653ebc1248SPeter Wemm file_addr = trunc_page_ps(offset, pagesize); 466e1743d02SSøren Schmidt 467e1743d02SSøren Schmidt /* 46852c24af7SPeter Wemm * We have two choices. We can either clear the data in the last page 46952c24af7SPeter Wemm * of an oversized mapping, or we can start the anon mapping a page 47052c24af7SPeter Wemm * early and copy the initialized data into that first page. We 47152c24af7SPeter Wemm * choose the second.. 47252c24af7SPeter Wemm */ 47352c24af7SPeter Wemm if (memsz > filsz) 4743ebc1248SPeter Wemm map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr; 47552c24af7SPeter Wemm else 4763ebc1248SPeter Wemm map_len = round_page_ps(offset + filsz, pagesize) - file_addr; 47752c24af7SPeter Wemm 47852c24af7SPeter Wemm if (map_len != 0) { 479fa7dd9c5SMatthew Dillon /* cow flags: don't dump readonly sections in core */ 480fa7dd9c5SMatthew Dillon cow = MAP_COPY_ON_WRITE | MAP_PREFAULT | 481fa7dd9c5SMatthew Dillon (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP); 482fa7dd9c5SMatthew Dillon 4833ebc1248SPeter Wemm rv = __elfN(map_insert)(&vmspace->vm_map, 48452c24af7SPeter Wemm object, 48552c24af7SPeter Wemm file_addr, /* file offset */ 48652c24af7SPeter Wemm map_addr, /* virtual start */ 48752c24af7SPeter Wemm map_addr + map_len,/* virtual end */ 48852c24af7SPeter Wemm prot, 489fa7dd9c5SMatthew Dillon cow); 490e5e6093bSAlan Cox if (rv != KERN_SUCCESS) 491a7cddfedSJake Burkholder return (EINVAL); 49252c24af7SPeter Wemm 49352c24af7SPeter Wemm /* we can stop now if we've covered it all */ 49423955314SAlfred Perlstein if (memsz == filsz) { 495a7cddfedSJake Burkholder return (0); 49652c24af7SPeter Wemm } 49723955314SAlfred Perlstein } 49852c24af7SPeter Wemm 49952c24af7SPeter Wemm 50052c24af7SPeter Wemm /* 50152c24af7SPeter Wemm * We have to get the remaining bit of the file into the first part 50252c24af7SPeter Wemm * of the oversized map segment. This is normally because the .data 50352c24af7SPeter Wemm * segment in the file is extended to provide bss. It's a neat idea 50452c24af7SPeter Wemm * to try and save a page, but it's a pain in the behind to implement. 505e1743d02SSøren Schmidt */ 5063ebc1248SPeter Wemm copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize); 5073ebc1248SPeter Wemm map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize); 508ca0387efSJake Burkholder map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) - 509ca0387efSJake Burkholder map_addr; 510e1743d02SSøren Schmidt 51152c24af7SPeter Wemm /* This had damn well better be true! */ 5128191d577SPeter Wemm if (map_len != 0) { 51381f223caSJake Burkholder rv = __elfN(map_insert)(&vmspace->vm_map, NULL, 0, map_addr, 514ff6f03c7SAlan Cox map_addr + map_len, VM_PROT_ALL, 0); 51523955314SAlfred Perlstein if (rv != KERN_SUCCESS) { 516a7cddfedSJake Burkholder return (EINVAL); 5178191d577SPeter Wemm } 51823955314SAlfred Perlstein } 519e1743d02SSøren Schmidt 52052c24af7SPeter Wemm if (copy_len != 0) { 5213ebc1248SPeter Wemm vm_offset_t off; 522da61b9a6SAlan Cox 523da61b9a6SAlan Cox sf = vm_imgact_map_page(object, offset + filsz); 524da61b9a6SAlan Cox if (sf == NULL) 525da61b9a6SAlan Cox return (EIO); 526e1743d02SSøren Schmidt 52752c24af7SPeter Wemm /* send the page fragment to user space */ 52881f223caSJake Burkholder off = trunc_page_ps(offset + filsz, pagesize) - 52981f223caSJake Burkholder trunc_page(offset + filsz); 530da61b9a6SAlan Cox error = copyout((caddr_t)sf_buf_kva(sf) + off, 531da61b9a6SAlan Cox (caddr_t)map_addr, copy_len); 532da61b9a6SAlan Cox vm_imgact_unmap_page(sf); 53323955314SAlfred Perlstein if (error) { 53452c24af7SPeter Wemm return (error); 53552c24af7SPeter Wemm } 53623955314SAlfred Perlstein } 537e1743d02SSøren Schmidt 538e1743d02SSøren Schmidt /* 5393ebc1248SPeter Wemm * set it to the specified protection. 5403ebc1248SPeter Wemm * XXX had better undo the damage from pasting over the cracks here! 541e1743d02SSøren Schmidt */ 5423ebc1248SPeter Wemm vm_map_protect(&vmspace->vm_map, trunc_page(map_addr), 5433ebc1248SPeter Wemm round_page(map_addr + map_len), prot, FALSE); 5448191d577SPeter Wemm 545ff6f03c7SAlan Cox return (0); 546e1743d02SSøren Schmidt } 547e1743d02SSøren Schmidt 548c33fe779SJohn Polstra /* 549c33fe779SJohn Polstra * Load the file "file" into memory. It may be either a shared object 550c33fe779SJohn Polstra * or an executable. 551c33fe779SJohn Polstra * 552c33fe779SJohn Polstra * The "addr" reference parameter is in/out. On entry, it specifies 553c33fe779SJohn Polstra * the address where a shared object should be loaded. If the file is 554c33fe779SJohn Polstra * an executable, this value is ignored. On exit, "addr" specifies 555c33fe779SJohn Polstra * where the file was actually loaded. 556c33fe779SJohn Polstra * 557c33fe779SJohn Polstra * The "entry" reference parameter is out only. On exit, it specifies 558c33fe779SJohn Polstra * the entry point for the loaded file. 559c33fe779SJohn Polstra */ 560e1743d02SSøren Schmidt static int 5613ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr, 5623ebc1248SPeter Wemm u_long *entry, size_t pagesize) 563e1743d02SSøren Schmidt { 564911c2be0SMark Peek struct { 565911c2be0SMark Peek struct nameidata nd; 566911c2be0SMark Peek struct vattr attr; 567911c2be0SMark Peek struct image_params image_params; 568911c2be0SMark Peek } *tempdata; 569d254af07SMatthew Dillon const Elf_Ehdr *hdr = NULL; 570d254af07SMatthew Dillon const Elf_Phdr *phdr = NULL; 571911c2be0SMark Peek struct nameidata *nd; 572e1743d02SSøren Schmidt struct vmspace *vmspace = p->p_vmspace; 573911c2be0SMark Peek struct vattr *attr; 574911c2be0SMark Peek struct image_params *imgp; 57552c24af7SPeter Wemm vm_prot_t prot; 576c33fe779SJohn Polstra u_long rbase; 577c33fe779SJohn Polstra u_long base_addr = 0; 57868ff2a43SChristian S.J. Peron int vfslocked, error, i, numsegs; 579e1743d02SSøren Schmidt 580a163d034SWarner Losh tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK); 581911c2be0SMark Peek nd = &tempdata->nd; 582911c2be0SMark Peek attr = &tempdata->attr; 583911c2be0SMark Peek imgp = &tempdata->image_params; 584911c2be0SMark Peek 585c8a79999SPeter Wemm /* 586c8a79999SPeter Wemm * Initialize part of the common data 587c8a79999SPeter Wemm */ 588c8a79999SPeter Wemm imgp->proc = p; 589911c2be0SMark Peek imgp->attr = attr; 590c8a79999SPeter Wemm imgp->firstpage = NULL; 59159c8bc40SAlan Cox imgp->image_header = NULL; 5920b2ed1aeSJeff Roberson imgp->object = NULL; 5936d7bdc8dSRobert Watson imgp->execlabel = NULL; 594c8a79999SPeter Wemm 59568ff2a43SChristian S.J. Peron NDINIT(nd, LOOKUP, MPSAFE|LOCKLEAF|FOLLOW, UIO_SYSSPACE, file, 59668ff2a43SChristian S.J. Peron curthread); 59768ff2a43SChristian S.J. Peron vfslocked = 0; 598911c2be0SMark Peek if ((error = namei(nd)) != 0) { 599911c2be0SMark Peek nd->ni_vp = NULL; 600e1743d02SSøren Schmidt goto fail; 601e1743d02SSøren Schmidt } 60268ff2a43SChristian S.J. Peron vfslocked = NDHASGIANT(nd); 603911c2be0SMark Peek NDFREE(nd, NDF_ONLY_PNBUF); 604911c2be0SMark Peek imgp->vp = nd->ni_vp; 605c8a79999SPeter Wemm 606e1743d02SSøren Schmidt /* 607e1743d02SSøren Schmidt * Check permissions, modes, uid, etc on the file, and "open" it. 608e1743d02SSøren Schmidt */ 609c8a79999SPeter Wemm error = exec_check_permissions(imgp); 610373d1a3fSAlan Cox if (error) 611c8a79999SPeter Wemm goto fail; 612e1743d02SSøren Schmidt 613c8a79999SPeter Wemm error = exec_map_first_page(imgp); 614373d1a3fSAlan Cox if (error) 615373d1a3fSAlan Cox goto fail; 616373d1a3fSAlan Cox 61725ead034SBrian Feldman /* 61825ead034SBrian Feldman * Also make certain that the interpreter stays the same, so set 619e6e370a7SJeff Roberson * its VV_TEXT flag, too. 62025ead034SBrian Feldman */ 621e6e370a7SJeff Roberson nd->ni_vp->v_vflag |= VV_TEXT; 622e6e370a7SJeff Roberson 6238516dd18SPoul-Henning Kamp imgp->object = nd->ni_vp->v_object; 624e1743d02SSøren Schmidt 625d254af07SMatthew Dillon hdr = (const Elf_Ehdr *)imgp->image_header; 6263ebc1248SPeter Wemm if ((error = __elfN(check_header)(hdr)) != 0) 627e1743d02SSøren Schmidt goto fail; 628c33fe779SJohn Polstra if (hdr->e_type == ET_DYN) 629c33fe779SJohn Polstra rbase = *addr; 630c33fe779SJohn Polstra else if (hdr->e_type == ET_EXEC) 631c33fe779SJohn Polstra rbase = 0; 632c33fe779SJohn Polstra else { 633c33fe779SJohn Polstra error = ENOEXEC; 634c33fe779SJohn Polstra goto fail; 635c33fe779SJohn Polstra } 636e1743d02SSøren Schmidt 637c8a79999SPeter Wemm /* Only support headers that fit within first page for now */ 6383dc19c46SJacques Vidrine /* (multiplication of two Elf_Half fields will not overflow) */ 63952c24af7SPeter Wemm if ((hdr->e_phoff > PAGE_SIZE) || 6403dc19c46SJacques Vidrine (hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE - hdr->e_phoff) { 641c8a79999SPeter Wemm error = ENOEXEC; 642e1743d02SSøren Schmidt goto fail; 643c8a79999SPeter Wemm } 644c8a79999SPeter Wemm 645d254af07SMatthew Dillon phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 64693d1c728SKonstantin Belousov if (!aligned(phdr, Elf_Addr)) { 64793d1c728SKonstantin Belousov error = ENOEXEC; 64893d1c728SKonstantin Belousov goto fail; 64993d1c728SKonstantin Belousov } 650e1743d02SSøren Schmidt 651c33fe779SJohn Polstra for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) { 6525b33842aSKonstantin Belousov if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) { 6535b33842aSKonstantin Belousov /* Loadable segment */ 654ed167eaaSKonstantin Belousov prot = __elfN(trans_prot)(phdr[i].p_flags); 655373d1a3fSAlan Cox if ((error = __elfN(load_section)(vmspace, 656373d1a3fSAlan Cox imgp->object, phdr[i].p_offset, 65781f223caSJake Burkholder (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase, 65881f223caSJake Burkholder phdr[i].p_memsz, phdr[i].p_filesz, prot, 65981f223caSJake Burkholder pagesize)) != 0) 660e1743d02SSøren Schmidt goto fail; 661e1743d02SSøren Schmidt /* 662c33fe779SJohn Polstra * Establish the base address if this is the 663c33fe779SJohn Polstra * first segment. 664e1743d02SSøren Schmidt */ 665c33fe779SJohn Polstra if (numsegs == 0) 666ca0387efSJake Burkholder base_addr = trunc_page(phdr[i].p_vaddr + 667ca0387efSJake Burkholder rbase); 668c33fe779SJohn Polstra numsegs++; 669e1743d02SSøren Schmidt } 670e1743d02SSøren Schmidt } 671c33fe779SJohn Polstra *addr = base_addr; 672c33fe779SJohn Polstra *entry = (unsigned long)hdr->e_entry + rbase; 673e1743d02SSøren Schmidt 674e1743d02SSøren Schmidt fail: 675c8a79999SPeter Wemm if (imgp->firstpage) 676c8a79999SPeter Wemm exec_unmap_first_page(imgp); 6770b2ed1aeSJeff Roberson 678911c2be0SMark Peek if (nd->ni_vp) 679373d1a3fSAlan Cox vput(nd->ni_vp); 680911c2be0SMark Peek 68168ff2a43SChristian S.J. Peron VFS_UNLOCK_GIANT(vfslocked); 682911c2be0SMark Peek free(tempdata, M_TEMP); 683e1743d02SSøren Schmidt 684a7cddfedSJake Burkholder return (error); 685e1743d02SSøren Schmidt } 686e1743d02SSøren Schmidt 687303b270bSEivind Eklund static int 6883ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) 689e1743d02SSøren Schmidt { 690ecbb00a2SDoug Rabson const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header; 69132c01de2SDmitry Chagin const Elf_Phdr *phdr; 692e5e6093bSAlan Cox Elf_Auxargs *elf_auxargs; 6935856e12eSJohn Dyson struct vmspace *vmspace; 69452c24af7SPeter Wemm vm_prot_t prot; 69521c2d047SMatthew Dillon u_long text_size = 0, data_size = 0, total_size = 0; 696e1743d02SSøren Schmidt u_long text_addr = 0, data_addr = 0; 697cac45152SMatthew Dillon u_long seg_size, seg_addr; 6987564c4adSKonstantin Belousov u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0; 69932c01de2SDmitry Chagin int32_t osrel = 0; 7007564c4adSKonstantin Belousov int error = 0, i, n; 7014113f8d7SPeter Wemm const char *interp = NULL, *newinterp = NULL; 702d1dbc694SJohn Polstra Elf_Brandinfo *brand_info; 703911c2be0SMark Peek char *path; 7045fe3ed62SJake Burkholder struct sysentvec *sv; 705e1743d02SSøren Schmidt 706e1743d02SSøren Schmidt /* 707e1743d02SSøren Schmidt * Do we have a valid ELF header ? 708900b28f9SMaxim Sobolev * 709900b28f9SMaxim Sobolev * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later 710900b28f9SMaxim Sobolev * if particular brand doesn't support it. 711e1743d02SSøren Schmidt */ 712900b28f9SMaxim Sobolev if (__elfN(check_header)(hdr) != 0 || 713900b28f9SMaxim Sobolev (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)) 714a7cddfedSJake Burkholder return (-1); 715e1743d02SSøren Schmidt 716e1743d02SSøren Schmidt /* 717e1743d02SSøren Schmidt * From here on down, we return an errno, not -1, as we've 718e1743d02SSøren Schmidt * detected an ELF file. 719e1743d02SSøren Schmidt */ 720e1743d02SSøren Schmidt 721e1743d02SSøren Schmidt if ((hdr->e_phoff > PAGE_SIZE) || 72252c24af7SPeter Wemm (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) { 723c8a79999SPeter Wemm /* Only support headers in first page for now */ 724a7cddfedSJake Burkholder return (ENOEXEC); 725e1743d02SSøren Schmidt } 72652c24af7SPeter Wemm phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 72793d1c728SKonstantin Belousov if (!aligned(phdr, Elf_Addr)) 72893d1c728SKonstantin Belousov return (ENOEXEC); 7297564c4adSKonstantin Belousov n = 0; 7307564c4adSKonstantin Belousov baddr = 0; 7315fe3ed62SJake Burkholder for (i = 0; i < hdr->e_phnum; i++) { 732*291c06a1SKonstantin Belousov switch (phdr[i].p_type) { 733*291c06a1SKonstantin Belousov case PT_LOAD: 7347564c4adSKonstantin Belousov if (n == 0) 7357564c4adSKonstantin Belousov baddr = phdr[i].p_vaddr; 7367564c4adSKonstantin Belousov n++; 737*291c06a1SKonstantin Belousov break; 738*291c06a1SKonstantin Belousov case PT_INTERP: 739e5e6093bSAlan Cox /* Path to interpreter */ 7405fe3ed62SJake Burkholder if (phdr[i].p_filesz > MAXPATHLEN || 74160bb3943SAlan Cox phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) 74260bb3943SAlan Cox return (ENOEXEC); 7435fe3ed62SJake Burkholder interp = imgp->image_header + phdr[i].p_offset; 744*291c06a1SKonstantin Belousov break; 745*291c06a1SKonstantin Belousov case PT_GNU_STACK: 746*291c06a1SKonstantin Belousov if (__elfN(nxstack)) 747*291c06a1SKonstantin Belousov imgp->stack_prot = 748*291c06a1SKonstantin Belousov __elfN(trans_prot)(phdr[i].p_flags); 749*291c06a1SKonstantin Belousov break; 7503ebc1248SPeter Wemm } 7513ebc1248SPeter Wemm } 7523ebc1248SPeter Wemm 75332c01de2SDmitry Chagin brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel); 7545fe3ed62SJake Burkholder if (brand_info == NULL) { 7555fe3ed62SJake Burkholder uprintf("ELF binary type \"%u\" not known.\n", 7565fe3ed62SJake Burkholder hdr->e_ident[EI_OSABI]); 75760bb3943SAlan Cox return (ENOEXEC); 7583ebc1248SPeter Wemm } 759ab02d85fSKonstantin Belousov if (hdr->e_type == ET_DYN) { 760ab02d85fSKonstantin Belousov if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) 761d49b2109SMaxim Sobolev return (ENOEXEC); 7627564c4adSKonstantin Belousov /* 7637564c4adSKonstantin Belousov * Honour the base load address from the dso if it is 7647564c4adSKonstantin Belousov * non-zero for some reason. 7657564c4adSKonstantin Belousov */ 7667564c4adSKonstantin Belousov if (baddr == 0) 767ab02d85fSKonstantin Belousov et_dyn_addr = ET_DYN_LOAD_ADDR; 7687564c4adSKonstantin Belousov else 7697564c4adSKonstantin Belousov et_dyn_addr = 0; 770ab02d85fSKonstantin Belousov } else 771ab02d85fSKonstantin Belousov et_dyn_addr = 0; 7725fe3ed62SJake Burkholder sv = brand_info->sysvec; 7739b68618dSPeter Wemm if (interp != NULL && brand_info->interp_newpath != NULL) 7744113f8d7SPeter Wemm newinterp = brand_info->interp_newpath; 7753ebc1248SPeter Wemm 77660bb3943SAlan Cox /* 77760bb3943SAlan Cox * Avoid a possible deadlock if the current address space is destroyed 77860bb3943SAlan Cox * and that address space maps the locked vnode. In the common case, 77960bb3943SAlan Cox * the locked vnode's v_usecount is decremented but remains greater 78060bb3943SAlan Cox * than zero. Consequently, the vnode lock is not needed by vrele(). 78160bb3943SAlan Cox * However, in cases where the vnode lock is external, such as nullfs, 78260bb3943SAlan Cox * v_usecount may become zero. 78360bb3943SAlan Cox */ 78422db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 78560bb3943SAlan Cox 78689b57fcfSKonstantin Belousov error = exec_new_vmspace(imgp, sv); 78719059a13SJohn Baldwin imgp->proc->p_sysent = sv; 788e1743d02SSøren Schmidt 789cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 79089b57fcfSKonstantin Belousov if (error) 79189b57fcfSKonstantin Belousov return (error); 79260bb3943SAlan Cox 7935856e12eSJohn Dyson vmspace = imgp->proc->p_vmspace; 7945856e12eSJohn Dyson 795e1743d02SSøren Schmidt for (i = 0; i < hdr->e_phnum; i++) { 796e1743d02SSøren Schmidt switch (phdr[i].p_type) { 797e1743d02SSøren Schmidt case PT_LOAD: /* Loadable segment */ 7985b33842aSKonstantin Belousov if (phdr[i].p_memsz == 0) 7995b33842aSKonstantin Belousov break; 800ed167eaaSKonstantin Belousov prot = __elfN(trans_prot)(phdr[i].p_flags); 801e1743d02SSøren Schmidt 8023ebc1248SPeter Wemm #if defined(__ia64__) && __ELF_WORD_SIZE == 32 && defined(IA32_ME_HARDER) 8033ebc1248SPeter Wemm /* 8043ebc1248SPeter Wemm * Some x86 binaries assume read == executable, 8053ebc1248SPeter Wemm * notably the M3 runtime and therefore cvsup 8063ebc1248SPeter Wemm */ 8073ebc1248SPeter Wemm if (prot & VM_PROT_READ) 8083ebc1248SPeter Wemm prot |= VM_PROT_EXECUTE; 8093ebc1248SPeter Wemm #endif 8103ebc1248SPeter Wemm 811373d1a3fSAlan Cox if ((error = __elfN(load_section)(vmspace, 812373d1a3fSAlan Cox imgp->object, phdr[i].p_offset, 813ab02d85fSKonstantin Belousov (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr, 81481f223caSJake Burkholder phdr[i].p_memsz, phdr[i].p_filesz, prot, 8155fe3ed62SJake Burkholder sv->sv_pagesize)) != 0) 81660bb3943SAlan Cox return (error); 817e1743d02SSøren Schmidt 818cfaf7e60SDoug Rabson /* 819cfaf7e60SDoug Rabson * If this segment contains the program headers, 820cfaf7e60SDoug Rabson * remember their virtual address for the AT_PHDR 821cfaf7e60SDoug Rabson * aux entry. Static binaries don't usually include 822cfaf7e60SDoug Rabson * a PT_PHDR entry. 823cfaf7e60SDoug Rabson */ 824cfaf7e60SDoug Rabson if (phdr[i].p_offset == 0 && 825cfaf7e60SDoug Rabson hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize 826cfaf7e60SDoug Rabson <= phdr[i].p_filesz) 827ab02d85fSKonstantin Belousov proghdr = phdr[i].p_vaddr + hdr->e_phoff + 828ab02d85fSKonstantin Belousov et_dyn_addr; 829cfaf7e60SDoug Rabson 830ab02d85fSKonstantin Belousov seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr); 831cac45152SMatthew Dillon seg_size = round_page(phdr[i].p_memsz + 832ab02d85fSKonstantin Belousov phdr[i].p_vaddr + et_dyn_addr - seg_addr); 833cac45152SMatthew Dillon 834e1743d02SSøren Schmidt /* 835920acedbSNathan Whitehorn * Make the largest executable segment the official 836920acedbSNathan Whitehorn * text segment and all others data. 83721c2d047SMatthew Dillon * 83821c2d047SMatthew Dillon * Note that obreak() assumes that data_addr + 83921c2d047SMatthew Dillon * data_size == end of data load area, and the ELF 84021c2d047SMatthew Dillon * file format expects segments to be sorted by 84121c2d047SMatthew Dillon * address. If multiple data segments exist, the 84221c2d047SMatthew Dillon * last one will be used. 843e1743d02SSøren Schmidt */ 844920acedbSNathan Whitehorn 845920acedbSNathan Whitehorn if (phdr[i].p_flags & PF_X && text_size < seg_size) { 8469782ecbaSPeter Wemm text_size = seg_size; 8479782ecbaSPeter Wemm text_addr = seg_addr; 8489782ecbaSPeter Wemm } else { 84921c2d047SMatthew Dillon data_size = seg_size; 850cac45152SMatthew Dillon data_addr = seg_addr; 851cac45152SMatthew Dillon } 85221c2d047SMatthew Dillon total_size += seg_size; 85396725dd0SAlexander Kabaev break; 85496725dd0SAlexander Kabaev case PT_PHDR: /* Program header table info */ 855ab02d85fSKonstantin Belousov proghdr = phdr[i].p_vaddr + et_dyn_addr; 85696725dd0SAlexander Kabaev break; 85796725dd0SAlexander Kabaev default: 85896725dd0SAlexander Kabaev break; 85996725dd0SAlexander Kabaev } 86096725dd0SAlexander Kabaev } 86196725dd0SAlexander Kabaev 86296725dd0SAlexander Kabaev if (data_addr == 0 && data_size == 0) { 86396725dd0SAlexander Kabaev data_addr = text_addr; 86496725dd0SAlexander Kabaev data_size = text_size; 86596725dd0SAlexander Kabaev } 866cac45152SMatthew Dillon 867920acedbSNathan Whitehorn entry = (u_long)hdr->e_entry + et_dyn_addr; 868920acedbSNathan Whitehorn 869cac45152SMatthew Dillon /* 870cac45152SMatthew Dillon * Check limits. It should be safe to check the 87196725dd0SAlexander Kabaev * limits after loading the segments since we do 87296725dd0SAlexander Kabaev * not actually fault in all the segments pages. 873cac45152SMatthew Dillon */ 87491d5354aSJohn Baldwin PROC_LOCK(imgp->proc); 87591d5354aSJohn Baldwin if (data_size > lim_cur(imgp->proc, RLIMIT_DATA) || 876cac45152SMatthew Dillon text_size > maxtsiz || 87791d5354aSJohn Baldwin total_size > lim_cur(imgp->proc, RLIMIT_VMEM)) { 87891d5354aSJohn Baldwin PROC_UNLOCK(imgp->proc); 87960bb3943SAlan Cox return (ENOMEM); 880cac45152SMatthew Dillon } 881e1743d02SSøren Schmidt 882e1743d02SSøren Schmidt vmspace->vm_tsize = text_size >> PAGE_SHIFT; 8837cd99438SBruce Evans vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr; 884e1743d02SSøren Schmidt vmspace->vm_dsize = data_size >> PAGE_SHIFT; 8857cd99438SBruce Evans vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr; 886e1743d02SSøren Schmidt 887c460ac3aSPeter Wemm /* 888c460ac3aSPeter Wemm * We load the dynamic linker where a userland call 889c460ac3aSPeter Wemm * to mmap(0, ...) would put it. The rationale behind this 890c460ac3aSPeter Wemm * calculation is that it leaves room for the heap to grow to 891c460ac3aSPeter Wemm * its maximum allowed size. 892c460ac3aSPeter Wemm */ 893c460ac3aSPeter Wemm addr = round_page((vm_offset_t)imgp->proc->p_vmspace->vm_daddr + 89491d5354aSJohn Baldwin lim_max(imgp->proc, RLIMIT_DATA)); 89591d5354aSJohn Baldwin PROC_UNLOCK(imgp->proc); 896e1743d02SSøren Schmidt 897ea5a2b2eSSøren Schmidt imgp->entry_addr = entry; 898ea5a2b2eSSøren Schmidt 89960bb3943SAlan Cox if (interp != NULL) { 9004113f8d7SPeter Wemm int have_interp = FALSE; 90122db15c0SAttilio Rao VOP_UNLOCK(imgp->vp, 0); 90260bb3943SAlan Cox if (brand_info->emul_path != NULL && 9039b68618dSPeter Wemm brand_info->emul_path[0] != '\0') { 904a163d034SWarner Losh path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 90560bb3943SAlan Cox snprintf(path, MAXPATHLEN, "%s%s", 90660bb3943SAlan Cox brand_info->emul_path, interp); 9079b68618dSPeter Wemm error = __elfN(load_file)(imgp->proc, path, &addr, 9089b68618dSPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 909911c2be0SMark Peek free(path, M_TEMP); 9109b68618dSPeter Wemm if (error == 0) 9114113f8d7SPeter Wemm have_interp = TRUE; 9129b68618dSPeter Wemm } 9134113f8d7SPeter Wemm if (!have_interp && newinterp != NULL) { 9144113f8d7SPeter Wemm error = __elfN(load_file)(imgp->proc, newinterp, &addr, 9154113f8d7SPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 916387ad998SKonstantin Belousov if (error == 0) 9174113f8d7SPeter Wemm have_interp = TRUE; 9184113f8d7SPeter Wemm } 9194113f8d7SPeter Wemm if (!have_interp) { 9209b68618dSPeter Wemm error = __elfN(load_file)(imgp->proc, interp, &addr, 9219b68618dSPeter Wemm &imgp->entry_addr, sv->sv_pagesize); 92260bb3943SAlan Cox } 923cb05b60aSAttilio Rao vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY); 9249b68618dSPeter Wemm if (error != 0) { 9259b68618dSPeter Wemm uprintf("ELF interpreter %s not found\n", interp); 92660bb3943SAlan Cox return (error); 927e1743d02SSøren Schmidt } 92895c807cfSRobert Watson } else 9297564c4adSKonstantin Belousov addr = et_dyn_addr; 930ea5a2b2eSSøren Schmidt 931e1743d02SSøren Schmidt /* 932e1743d02SSøren Schmidt * Construct auxargs table (used by the fixup routine) 933e1743d02SSøren Schmidt */ 934a163d034SWarner Losh elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK); 935e1743d02SSøren Schmidt elf_auxargs->execfd = -1; 936e1743d02SSøren Schmidt elf_auxargs->phdr = proghdr; 937e1743d02SSøren Schmidt elf_auxargs->phent = hdr->e_phentsize; 938e1743d02SSøren Schmidt elf_auxargs->phnum = hdr->e_phnum; 939e1743d02SSøren Schmidt elf_auxargs->pagesz = PAGE_SIZE; 940e1743d02SSøren Schmidt elf_auxargs->base = addr; 941e1743d02SSøren Schmidt elf_auxargs->flags = 0; 942e1743d02SSøren Schmidt elf_auxargs->entry = entry; 943e1743d02SSøren Schmidt 944e1743d02SSøren Schmidt imgp->auxargs = elf_auxargs; 945e1743d02SSøren Schmidt imgp->interpreted = 0; 946a0ea661fSNathan Whitehorn imgp->reloc_base = addr; 94732c01de2SDmitry Chagin imgp->proc->p_osrel = osrel; 948f231de47SKonstantin Belousov 949a7cddfedSJake Burkholder return (error); 950e1743d02SSøren Schmidt } 951e1743d02SSøren Schmidt 952a360a43dSJake Burkholder #define suword __CONCAT(suword, __ELF_WORD_SIZE) 9533ebc1248SPeter Wemm 9543ebc1248SPeter Wemm int 9553ebc1248SPeter Wemm __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp) 956e1743d02SSøren Schmidt { 957ecbb00a2SDoug Rabson Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs; 958a360a43dSJake Burkholder Elf_Addr *base; 959a360a43dSJake Burkholder Elf_Addr *pos; 960e1743d02SSøren Schmidt 961a360a43dSJake Burkholder base = (Elf_Addr *)*stack_base; 962610ecfe0SMaxim Sobolev pos = base + (imgp->args->argc + imgp->args->envc + 2); 963e1743d02SSøren Schmidt 96435c2a5a8SWarner Losh if (args->execfd != -1) 965e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 966e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 967e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 968e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 969e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 970e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 971e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 972e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_BASE, args->base); 9733ff06357SKonstantin Belousov if (imgp->execpathp != 0) 9743ff06357SKonstantin Belousov AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp); 975ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_OSRELDATE, osreldate); 976ee235befSKonstantin Belousov if (imgp->canary != 0) { 977ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary); 978ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen); 979ee235befSKonstantin Belousov } 980ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus); 981ee235befSKonstantin Belousov if (imgp->pagesizes != 0) { 982ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes); 983ee235befSKonstantin Belousov AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen); 984ee235befSKonstantin Belousov } 985*291c06a1SKonstantin Belousov AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->stack_prot != 0 ? 986*291c06a1SKonstantin Belousov imgp->stack_prot : imgp->sysent->sv_stackprot); 987e1743d02SSøren Schmidt AUXARGS_ENTRY(pos, AT_NULL, 0); 988e1743d02SSøren Schmidt 989e1743d02SSøren Schmidt free(imgp->auxargs, M_TEMP); 990e1743d02SSøren Schmidt imgp->auxargs = NULL; 991e1743d02SSøren Schmidt 9923ebc1248SPeter Wemm base--; 993610ecfe0SMaxim Sobolev suword(base, (long)imgp->args->argc); 9943ebc1248SPeter Wemm *stack_base = (register_t *)base; 995a7cddfedSJake Burkholder return (0); 996e1743d02SSøren Schmidt } 997e1743d02SSøren Schmidt 998e1743d02SSøren Schmidt /* 9998c64af4fSJohn Polstra * Code for generating ELF core dumps. 10008c64af4fSJohn Polstra */ 10018c64af4fSJohn Polstra 10024d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *); 10030ff27d31SJohn Polstra 10040ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */ 10050ff27d31SJohn Polstra struct phdr_closure { 10060ff27d31SJohn Polstra Elf_Phdr *phdr; /* Program header to fill in */ 10070ff27d31SJohn Polstra Elf_Off offset; /* Offset of segment in core file */ 10080ff27d31SJohn Polstra }; 10090ff27d31SJohn Polstra 10100ff27d31SJohn Polstra /* Closure for cb_size_segment(). */ 10110ff27d31SJohn Polstra struct sseg_closure { 10120ff27d31SJohn Polstra int count; /* Count of writable segments. */ 10130ff27d31SJohn Polstra size_t size; /* Total size of all writable segments. */ 10140ff27d31SJohn Polstra }; 10150ff27d31SJohn Polstra 10164d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *); 10174d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *); 1018247aba24SMarcel Moolenaar static void each_writable_segment(struct thread *, segment_callback, void *); 10193ebc1248SPeter Wemm static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *, 1020e7228204SAlfred Perlstein int, void *, size_t, gzFile); 1021247aba24SMarcel Moolenaar static void __elfN(puthdr)(struct thread *, void *, size_t *, int); 10223ebc1248SPeter Wemm static void __elfN(putnote)(void *, size_t *, const char *, int, 10234d77a549SAlfred Perlstein const void *, size_t); 10248c64af4fSJohn Polstra 1025e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1026e7228204SAlfred Perlstein extern int compress_user_cores; 1027e7228204SAlfred Perlstein extern int compress_user_cores_gzlevel; 1028e7228204SAlfred Perlstein #endif 1029e7228204SAlfred Perlstein 1030e7228204SAlfred Perlstein static int 1031e7228204SAlfred Perlstein core_output(struct vnode *vp, void *base, size_t len, off_t offset, 1032e7228204SAlfred Perlstein struct ucred *active_cred, struct ucred *file_cred, 1033e7228204SAlfred Perlstein struct thread *td, char *core_buf, gzFile gzfile) { 1034e7228204SAlfred Perlstein 1035e7228204SAlfred Perlstein int error; 1036e7228204SAlfred Perlstein if (gzfile) { 1037e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1038e7228204SAlfred Perlstein error = compress_core(gzfile, base, core_buf, len, td); 1039e7228204SAlfred Perlstein #else 1040e7228204SAlfred Perlstein panic("shouldn't be here"); 1041e7228204SAlfred Perlstein #endif 1042e7228204SAlfred Perlstein } else { 1043e7228204SAlfred Perlstein error = vn_rdwr_inchunks(UIO_WRITE, vp, base, len, offset, 1044e7228204SAlfred Perlstein UIO_USERSPACE, IO_UNIT | IO_DIRECT, active_cred, file_cred, 1045e7228204SAlfred Perlstein NULL, td); 1046e7228204SAlfred Perlstein } 1047e7228204SAlfred Perlstein return (error); 1048e7228204SAlfred Perlstein } 1049e7228204SAlfred Perlstein 10508c64af4fSJohn Polstra int 1051e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags) 1052fca666a1SJulian Elischer { 1053247aba24SMarcel Moolenaar struct ucred *cred = td->td_ucred; 1054fca666a1SJulian Elischer int error = 0; 10550ff27d31SJohn Polstra struct sseg_closure seginfo; 10560ff27d31SJohn Polstra void *hdr; 10578c64af4fSJohn Polstra size_t hdrsize; 10588c64af4fSJohn Polstra 1059e7228204SAlfred Perlstein gzFile gzfile = Z_NULL; 1060e7228204SAlfred Perlstein char *core_buf = NULL; 1061e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1062e7228204SAlfred Perlstein char gzopen_flags[8]; 1063e7228204SAlfred Perlstein char *p; 1064e7228204SAlfred Perlstein int doing_compress = flags & IMGACT_CORE_COMPRESS; 1065e7228204SAlfred Perlstein #endif 1066e7228204SAlfred Perlstein 1067e7228204SAlfred Perlstein hdr = NULL; 1068e7228204SAlfred Perlstein 1069e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1070e7228204SAlfred Perlstein if (doing_compress) { 1071e7228204SAlfred Perlstein p = gzopen_flags; 1072e7228204SAlfred Perlstein *p++ = 'w'; 1073e7228204SAlfred Perlstein if (compress_user_cores_gzlevel >= 0 && 1074e7228204SAlfred Perlstein compress_user_cores_gzlevel <= 9) 1075e7228204SAlfred Perlstein *p++ = '0' + compress_user_cores_gzlevel; 1076e7228204SAlfred Perlstein *p = 0; 1077e7228204SAlfred Perlstein gzfile = gz_open("", gzopen_flags, vp); 1078e7228204SAlfred Perlstein if (gzfile == Z_NULL) { 1079e7228204SAlfred Perlstein error = EFAULT; 1080e7228204SAlfred Perlstein goto done; 1081e7228204SAlfred Perlstein } 1082e7228204SAlfred Perlstein core_buf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO); 1083e7228204SAlfred Perlstein if (!core_buf) { 1084e7228204SAlfred Perlstein error = ENOMEM; 1085e7228204SAlfred Perlstein goto done; 1086e7228204SAlfred Perlstein } 1087e7228204SAlfred Perlstein } 1088e7228204SAlfred Perlstein #endif 1089e7228204SAlfred Perlstein 10900ff27d31SJohn Polstra /* Size the program segments. */ 10910ff27d31SJohn Polstra seginfo.count = 0; 10920ff27d31SJohn Polstra seginfo.size = 0; 1093247aba24SMarcel Moolenaar each_writable_segment(td, cb_size_segment, &seginfo); 10940ff27d31SJohn Polstra 10950ff27d31SJohn Polstra /* 10960ff27d31SJohn Polstra * Calculate the size of the core file header area by making 10970ff27d31SJohn Polstra * a dry run of generating it. Nothing is written, but the 10980ff27d31SJohn Polstra * size is calculated. 10990ff27d31SJohn Polstra */ 11000ff27d31SJohn Polstra hdrsize = 0; 1101247aba24SMarcel Moolenaar __elfN(puthdr)(td, (void *)NULL, &hdrsize, seginfo.count); 11020ff27d31SJohn Polstra 1103fba6b1afSAlfred Perlstein if (hdrsize + seginfo.size >= limit) { 1104fba6b1afSAlfred Perlstein error = EFAULT; 1105fba6b1afSAlfred Perlstein goto done; 1106fba6b1afSAlfred Perlstein } 11070ff27d31SJohn Polstra 11080ff27d31SJohn Polstra /* 11090ff27d31SJohn Polstra * Allocate memory for building the header, fill it up, 11100ff27d31SJohn Polstra * and write it out. 11110ff27d31SJohn Polstra */ 1112a163d034SWarner Losh hdr = malloc(hdrsize, M_TEMP, M_WAITOK); 11130ff27d31SJohn Polstra if (hdr == NULL) { 1114fba6b1afSAlfred Perlstein error = EINVAL; 1115fba6b1afSAlfred Perlstein goto done; 11160ff27d31SJohn Polstra } 1117e7228204SAlfred Perlstein error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize, 1118e7228204SAlfred Perlstein gzfile); 11190ff27d31SJohn Polstra 11200ff27d31SJohn Polstra /* Write the contents of all of the writable segments. */ 11210ff27d31SJohn Polstra if (error == 0) { 11220ff27d31SJohn Polstra Elf_Phdr *php; 11232b471bc6STim J. Robbins off_t offset; 11240ff27d31SJohn Polstra int i; 11250ff27d31SJohn Polstra 11260ff27d31SJohn Polstra php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 11270ff27d31SJohn Polstra offset = hdrsize; 11280ff27d31SJohn Polstra for (i = 0; i < seginfo.count; i++) { 1129e7228204SAlfred Perlstein error = core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr, 1130e7228204SAlfred Perlstein php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile); 11310ff27d31SJohn Polstra if (error != 0) 11322b471bc6STim J. Robbins break; 11330ff27d31SJohn Polstra offset += php->p_filesz; 11340ff27d31SJohn Polstra php++; 11350ff27d31SJohn Polstra } 11360ff27d31SJohn Polstra } 1137e7228204SAlfred Perlstein if (error) { 1138e7228204SAlfred Perlstein log(LOG_WARNING, 1139e7228204SAlfred Perlstein "Failed to write core file for process %s (error %d)\n", 1140e7228204SAlfred Perlstein curproc->p_comm, error); 1141e7228204SAlfred Perlstein } 1142e7228204SAlfred Perlstein 1143e7228204SAlfred Perlstein done: 1144fba6b1afSAlfred Perlstein #ifdef COMPRESS_USER_CORES 1145e7228204SAlfred Perlstein if (core_buf) 1146e7228204SAlfred Perlstein free(core_buf, M_TEMP); 1147e7228204SAlfred Perlstein if (gzfile) 1148e7228204SAlfred Perlstein gzclose(gzfile); 11498b325009SAlfred Perlstein #endif 1150e7228204SAlfred Perlstein 11510ff27d31SJohn Polstra free(hdr, M_TEMP); 11520ff27d31SJohn Polstra 1153a7cddfedSJake Burkholder return (error); 11548c64af4fSJohn Polstra } 11558c64af4fSJohn Polstra 11560ff27d31SJohn Polstra /* 11570ff27d31SJohn Polstra * A callback for each_writable_segment() to write out the segment's 11580ff27d31SJohn Polstra * program header entry. 11590ff27d31SJohn Polstra */ 11600ff27d31SJohn Polstra static void 11610ff27d31SJohn Polstra cb_put_phdr(entry, closure) 11620ff27d31SJohn Polstra vm_map_entry_t entry; 11630ff27d31SJohn Polstra void *closure; 11640ff27d31SJohn Polstra { 11650ff27d31SJohn Polstra struct phdr_closure *phc = (struct phdr_closure *)closure; 11660ff27d31SJohn Polstra Elf_Phdr *phdr = phc->phdr; 11670ff27d31SJohn Polstra 11680ff27d31SJohn Polstra phc->offset = round_page(phc->offset); 11690ff27d31SJohn Polstra 11700ff27d31SJohn Polstra phdr->p_type = PT_LOAD; 11710ff27d31SJohn Polstra phdr->p_offset = phc->offset; 11720ff27d31SJohn Polstra phdr->p_vaddr = entry->start; 11730ff27d31SJohn Polstra phdr->p_paddr = 0; 11740ff27d31SJohn Polstra phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 11750ff27d31SJohn Polstra phdr->p_align = PAGE_SIZE; 1176ed167eaaSKonstantin Belousov phdr->p_flags = __elfN(untrans_prot)(entry->protection); 11770ff27d31SJohn Polstra 11780ff27d31SJohn Polstra phc->offset += phdr->p_filesz; 11790ff27d31SJohn Polstra phc->phdr++; 11800ff27d31SJohn Polstra } 11810ff27d31SJohn Polstra 11820ff27d31SJohn Polstra /* 11830ff27d31SJohn Polstra * A callback for each_writable_segment() to gather information about 11840ff27d31SJohn Polstra * the number of segments and their total size. 11850ff27d31SJohn Polstra */ 11860ff27d31SJohn Polstra static void 11870ff27d31SJohn Polstra cb_size_segment(entry, closure) 11880ff27d31SJohn Polstra vm_map_entry_t entry; 11890ff27d31SJohn Polstra void *closure; 11900ff27d31SJohn Polstra { 11910ff27d31SJohn Polstra struct sseg_closure *ssc = (struct sseg_closure *)closure; 11920ff27d31SJohn Polstra 11930ff27d31SJohn Polstra ssc->count++; 11940ff27d31SJohn Polstra ssc->size += entry->end - entry->start; 11950ff27d31SJohn Polstra } 11960ff27d31SJohn Polstra 11970ff27d31SJohn Polstra /* 11980ff27d31SJohn Polstra * For each writable segment in the process's memory map, call the given 11990ff27d31SJohn Polstra * function with a pointer to the map entry and some arbitrary 12000ff27d31SJohn Polstra * caller-supplied data. 12010ff27d31SJohn Polstra */ 12020ff27d31SJohn Polstra static void 1203247aba24SMarcel Moolenaar each_writable_segment(td, func, closure) 1204247aba24SMarcel Moolenaar struct thread *td; 12050ff27d31SJohn Polstra segment_callback func; 12060ff27d31SJohn Polstra void *closure; 12070ff27d31SJohn Polstra { 1208247aba24SMarcel Moolenaar struct proc *p = td->td_proc; 12090ff27d31SJohn Polstra vm_map_t map = &p->p_vmspace->vm_map; 12100ff27d31SJohn Polstra vm_map_entry_t entry; 1211976a87a2SAlan Cox vm_object_t backing_object, object; 1212976a87a2SAlan Cox boolean_t ignore_entry; 12130ff27d31SJohn Polstra 1214976a87a2SAlan Cox vm_map_lock_read(map); 12150ff27d31SJohn Polstra for (entry = map->header.next; entry != &map->header; 12160ff27d31SJohn Polstra entry = entry->next) { 1217fa7dd9c5SMatthew Dillon /* 1218fa7dd9c5SMatthew Dillon * Don't dump inaccessible mappings, deal with legacy 1219fa7dd9c5SMatthew Dillon * coredump mode. 1220fa7dd9c5SMatthew Dillon * 1221fa7dd9c5SMatthew Dillon * Note that read-only segments related to the elf binary 1222fa7dd9c5SMatthew Dillon * are marked MAP_ENTRY_NOCOREDUMP now so we no longer 1223fa7dd9c5SMatthew Dillon * need to arbitrarily ignore such segments. 1224fa7dd9c5SMatthew Dillon */ 1225fa7dd9c5SMatthew Dillon if (elf_legacy_coredump) { 1226fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_RW) != VM_PROT_RW) 12270ff27d31SJohn Polstra continue; 1228fa7dd9c5SMatthew Dillon } else { 1229fa7dd9c5SMatthew Dillon if ((entry->protection & VM_PROT_ALL) == 0) 1230fa7dd9c5SMatthew Dillon continue; 1231fa7dd9c5SMatthew Dillon } 12320ff27d31SJohn Polstra 12339730a5daSPaul Saab /* 1234fa7dd9c5SMatthew Dillon * Dont include memory segment in the coredump if 1235fa7dd9c5SMatthew Dillon * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in 1236fa7dd9c5SMatthew Dillon * madvise(2). Do not dump submaps (i.e. parts of the 1237fa7dd9c5SMatthew Dillon * kernel map). 12389730a5daSPaul Saab */ 1239fa7dd9c5SMatthew Dillon if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP)) 12409730a5daSPaul Saab continue; 12419730a5daSPaul Saab 1242976a87a2SAlan Cox if ((object = entry->object.vm_object) == NULL) 12430ff27d31SJohn Polstra continue; 12440ff27d31SJohn Polstra 12450ff27d31SJohn Polstra /* Ignore memory-mapped devices and such things. */ 1246976a87a2SAlan Cox VM_OBJECT_LOCK(object); 1247976a87a2SAlan Cox while ((backing_object = object->backing_object) != NULL) { 1248976a87a2SAlan Cox VM_OBJECT_LOCK(backing_object); 1249976a87a2SAlan Cox VM_OBJECT_UNLOCK(object); 1250976a87a2SAlan Cox object = backing_object; 1251976a87a2SAlan Cox } 1252976a87a2SAlan Cox ignore_entry = object->type != OBJT_DEFAULT && 1253976a87a2SAlan Cox object->type != OBJT_SWAP && object->type != OBJT_VNODE; 1254976a87a2SAlan Cox VM_OBJECT_UNLOCK(object); 1255976a87a2SAlan Cox if (ignore_entry) 12560ff27d31SJohn Polstra continue; 12570ff27d31SJohn Polstra 12580ff27d31SJohn Polstra (*func)(entry, closure); 12590ff27d31SJohn Polstra } 1260976a87a2SAlan Cox vm_map_unlock_read(map); 12610ff27d31SJohn Polstra } 12620ff27d31SJohn Polstra 12630ff27d31SJohn Polstra /* 12640ff27d31SJohn Polstra * Write the core file header to the file, including padding up to 12650ff27d31SJohn Polstra * the page boundary. 12660ff27d31SJohn Polstra */ 12678c64af4fSJohn Polstra static int 1268e7228204SAlfred Perlstein __elfN(corehdr)(td, vp, cred, numsegs, hdr, hdrsize, gzfile) 1269b40ce416SJulian Elischer struct thread *td; 12708c64af4fSJohn Polstra struct vnode *vp; 12718c64af4fSJohn Polstra struct ucred *cred; 12720ff27d31SJohn Polstra int numsegs; 12730ff27d31SJohn Polstra size_t hdrsize; 12740ff27d31SJohn Polstra void *hdr; 1275e7228204SAlfred Perlstein gzFile gzfile; 12768c64af4fSJohn Polstra { 1277911c2be0SMark Peek size_t off; 12788c64af4fSJohn Polstra 12798c64af4fSJohn Polstra /* Fill in the header. */ 12800ff27d31SJohn Polstra bzero(hdr, hdrsize); 12818c64af4fSJohn Polstra off = 0; 1282247aba24SMarcel Moolenaar __elfN(puthdr)(td, hdr, &off, numsegs); 12838c64af4fSJohn Polstra 1284e7228204SAlfred Perlstein if (!gzfile) { 12858c64af4fSJohn Polstra /* Write it to the core file. */ 1286a7cddfedSJake Burkholder return (vn_rdwr_inchunks(UIO_WRITE, vp, hdr, hdrsize, (off_t)0, 12879ca43589SRobert Watson UIO_SYSSPACE, IO_UNIT | IO_DIRECT, cred, NOCRED, NULL, 12886617724cSJeff Roberson td)); 1289e7228204SAlfred Perlstein } else { 1290e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1291e7228204SAlfred Perlstein if (gzwrite(gzfile, hdr, hdrsize) != hdrsize) { 1292e7228204SAlfred Perlstein log(LOG_WARNING, 1293e7228204SAlfred Perlstein "Failed to compress core file header for process" 1294e7228204SAlfred Perlstein " %s.\n", curproc->p_comm); 1295e7228204SAlfred Perlstein return (EFAULT); 1296e7228204SAlfred Perlstein } 1297e7228204SAlfred Perlstein else { 1298e7228204SAlfred Perlstein return (0); 1299e7228204SAlfred Perlstein } 1300e7228204SAlfred Perlstein #else 1301e7228204SAlfred Perlstein panic("shouldn't be here"); 1302e7228204SAlfred Perlstein #endif 1303e7228204SAlfred Perlstein } 1304dada0278SJohn Polstra } 1305dada0278SJohn Polstra 1306841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1307841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h> 1308841c0c7eSNathan Whitehorn 130962919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t; 131062919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t; 131162919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t; 131262919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t; 131362919d78SPeter Wemm typedef struct reg32 elf_gregset_t; 13147f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t; 131562919d78SPeter Wemm #else 131662919d78SPeter Wemm typedef prstatus_t elf_prstatus_t; 131762919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t; 131862919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t; 131962919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t; 132062919d78SPeter Wemm typedef gregset_t elf_gregset_t; 13217f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t; 132262919d78SPeter Wemm #endif 132362919d78SPeter Wemm 13248c64af4fSJohn Polstra static void 1325247aba24SMarcel Moolenaar __elfN(puthdr)(struct thread *td, void *dst, size_t *off, int numsegs) 13268c64af4fSJohn Polstra { 13278c9b7b2cSMarcel Moolenaar struct { 132862919d78SPeter Wemm elf_prstatus_t status; 132962919d78SPeter Wemm elf_prfpregset_t fpregset; 133062919d78SPeter Wemm elf_prpsinfo_t psinfo; 13317f08176eSAttilio Rao elf_thrmisc_t thrmisc; 13328c9b7b2cSMarcel Moolenaar } *tempdata; 133362919d78SPeter Wemm elf_prstatus_t *status; 133462919d78SPeter Wemm elf_prfpregset_t *fpregset; 133562919d78SPeter Wemm elf_prpsinfo_t *psinfo; 13367f08176eSAttilio Rao elf_thrmisc_t *thrmisc; 1337247aba24SMarcel Moolenaar struct proc *p; 1338247aba24SMarcel Moolenaar struct thread *thr; 13398c9b7b2cSMarcel Moolenaar size_t ehoff, noteoff, notesz, phoff; 13408c64af4fSJohn Polstra 1341247aba24SMarcel Moolenaar p = td->td_proc; 1342247aba24SMarcel Moolenaar 13438c64af4fSJohn Polstra ehoff = *off; 13448c64af4fSJohn Polstra *off += sizeof(Elf_Ehdr); 13458c64af4fSJohn Polstra 13468c64af4fSJohn Polstra phoff = *off; 13470ff27d31SJohn Polstra *off += (numsegs + 1) * sizeof(Elf_Phdr); 13488c64af4fSJohn Polstra 13498c64af4fSJohn Polstra noteoff = *off; 13508c9b7b2cSMarcel Moolenaar /* 13518c9b7b2cSMarcel Moolenaar * Don't allocate space for the notes if we're just calculating 13528c9b7b2cSMarcel Moolenaar * the size of the header. We also don't collect the data. 13538c9b7b2cSMarcel Moolenaar */ 13548c9b7b2cSMarcel Moolenaar if (dst != NULL) { 13558c9b7b2cSMarcel Moolenaar tempdata = malloc(sizeof(*tempdata), M_TEMP, M_ZERO|M_WAITOK); 13568c9b7b2cSMarcel Moolenaar status = &tempdata->status; 13578c9b7b2cSMarcel Moolenaar fpregset = &tempdata->fpregset; 13588c9b7b2cSMarcel Moolenaar psinfo = &tempdata->psinfo; 13597f08176eSAttilio Rao thrmisc = &tempdata->thrmisc; 13608c9b7b2cSMarcel Moolenaar } else { 13618c9b7b2cSMarcel Moolenaar tempdata = NULL; 13628c9b7b2cSMarcel Moolenaar status = NULL; 13638c9b7b2cSMarcel Moolenaar fpregset = NULL; 13648c9b7b2cSMarcel Moolenaar psinfo = NULL; 13657f08176eSAttilio Rao thrmisc = NULL; 13668c9b7b2cSMarcel Moolenaar } 13678c9b7b2cSMarcel Moolenaar 13688c9b7b2cSMarcel Moolenaar if (dst != NULL) { 13698c9b7b2cSMarcel Moolenaar psinfo->pr_version = PRPSINFO_VERSION; 137062919d78SPeter Wemm psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t); 1371ccd3953eSJohn Baldwin strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname)); 13728c9b7b2cSMarcel Moolenaar /* 13738c9b7b2cSMarcel Moolenaar * XXX - We don't fill in the command line arguments properly 13748c9b7b2cSMarcel Moolenaar * yet. 13758c9b7b2cSMarcel Moolenaar */ 1376ccd3953eSJohn Baldwin strlcpy(psinfo->pr_psargs, p->p_comm, 13778c9b7b2cSMarcel Moolenaar sizeof(psinfo->pr_psargs)); 13788c9b7b2cSMarcel Moolenaar } 13798c9b7b2cSMarcel Moolenaar __elfN(putnote)(dst, off, "FreeBSD", NT_PRPSINFO, psinfo, 13808c9b7b2cSMarcel Moolenaar sizeof *psinfo); 13818c9b7b2cSMarcel Moolenaar 13828c9b7b2cSMarcel Moolenaar /* 13831f7a1baaSMarcel Moolenaar * To have the debugger select the right thread (LWP) as the initial 13841f7a1baaSMarcel Moolenaar * thread, we dump the state of the thread passed to us in td first. 13851f7a1baaSMarcel Moolenaar * This is the thread that causes the core dump and thus likely to 13861f7a1baaSMarcel Moolenaar * be the right thread one wants to have selected in the debugger. 13878c9b7b2cSMarcel Moolenaar */ 1388247aba24SMarcel Moolenaar thr = td; 1389247aba24SMarcel Moolenaar while (thr != NULL) { 13908c9b7b2cSMarcel Moolenaar if (dst != NULL) { 13918c9b7b2cSMarcel Moolenaar status->pr_version = PRSTATUS_VERSION; 139262919d78SPeter Wemm status->pr_statussz = sizeof(elf_prstatus_t); 139362919d78SPeter Wemm status->pr_gregsetsz = sizeof(elf_gregset_t); 139462919d78SPeter Wemm status->pr_fpregsetsz = sizeof(elf_fpregset_t); 13958c9b7b2cSMarcel Moolenaar status->pr_osreldate = osreldate; 13968c9b7b2cSMarcel Moolenaar status->pr_cursig = p->p_sig; 13971f7a1baaSMarcel Moolenaar status->pr_pid = thr->td_tid; 1398841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 139962919d78SPeter Wemm fill_regs32(thr, &status->pr_reg); 140062919d78SPeter Wemm fill_fpregs32(thr, fpregset); 140162919d78SPeter Wemm #else 14028c9b7b2cSMarcel Moolenaar fill_regs(thr, &status->pr_reg); 14038c9b7b2cSMarcel Moolenaar fill_fpregs(thr, fpregset); 140462919d78SPeter Wemm #endif 14057f08176eSAttilio Rao memset(&thrmisc->_pad, 0, sizeof (thrmisc->_pad)); 14067f08176eSAttilio Rao strcpy(thrmisc->pr_tname, thr->td_name); 14078c9b7b2cSMarcel Moolenaar } 14083ebc1248SPeter Wemm __elfN(putnote)(dst, off, "FreeBSD", NT_PRSTATUS, status, 14098c64af4fSJohn Polstra sizeof *status); 14103ebc1248SPeter Wemm __elfN(putnote)(dst, off, "FreeBSD", NT_FPREGSET, fpregset, 14118c64af4fSJohn Polstra sizeof *fpregset); 14127f08176eSAttilio Rao __elfN(putnote)(dst, off, "FreeBSD", NT_THRMISC, thrmisc, 14137f08176eSAttilio Rao sizeof *thrmisc); 14144da47b2fSMarcel Moolenaar /* 14154da47b2fSMarcel Moolenaar * Allow for MD specific notes, as well as any MD 14164da47b2fSMarcel Moolenaar * specific preparations for writing MI notes. 14174da47b2fSMarcel Moolenaar */ 14184da47b2fSMarcel Moolenaar __elfN(dump_thread)(thr, dst, off); 1419247aba24SMarcel Moolenaar 1420247aba24SMarcel Moolenaar thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) : 14218c9b7b2cSMarcel Moolenaar TAILQ_NEXT(thr, td_plist); 1422247aba24SMarcel Moolenaar if (thr == td) 14238c9b7b2cSMarcel Moolenaar thr = TAILQ_NEXT(thr, td_plist); 1424247aba24SMarcel Moolenaar } 14258c9b7b2cSMarcel Moolenaar 14268c64af4fSJohn Polstra notesz = *off - noteoff; 14278c64af4fSJohn Polstra 14288c9b7b2cSMarcel Moolenaar if (dst != NULL) 14298c9b7b2cSMarcel Moolenaar free(tempdata, M_TEMP); 14308c9b7b2cSMarcel Moolenaar 14310ff27d31SJohn Polstra /* Align up to a page boundary for the program segments. */ 14328c64af4fSJohn Polstra *off = round_page(*off); 14338c64af4fSJohn Polstra 14348c64af4fSJohn Polstra if (dst != NULL) { 14358c64af4fSJohn Polstra Elf_Ehdr *ehdr; 14368c64af4fSJohn Polstra Elf_Phdr *phdr; 14370ff27d31SJohn Polstra struct phdr_closure phc; 14388c64af4fSJohn Polstra 14398c64af4fSJohn Polstra /* 14408c64af4fSJohn Polstra * Fill in the ELF header. 14418c64af4fSJohn Polstra */ 14428c64af4fSJohn Polstra ehdr = (Elf_Ehdr *)((char *)dst + ehoff); 14438c64af4fSJohn Polstra ehdr->e_ident[EI_MAG0] = ELFMAG0; 14448c64af4fSJohn Polstra ehdr->e_ident[EI_MAG1] = ELFMAG1; 14458c64af4fSJohn Polstra ehdr->e_ident[EI_MAG2] = ELFMAG2; 14468c64af4fSJohn Polstra ehdr->e_ident[EI_MAG3] = ELFMAG3; 14478c64af4fSJohn Polstra ehdr->e_ident[EI_CLASS] = ELF_CLASS; 14488c64af4fSJohn Polstra ehdr->e_ident[EI_DATA] = ELF_DATA; 14498c64af4fSJohn Polstra ehdr->e_ident[EI_VERSION] = EV_CURRENT; 1450c815a20cSDavid E. O'Brien ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 1451c815a20cSDavid E. O'Brien ehdr->e_ident[EI_ABIVERSION] = 0; 14528c64af4fSJohn Polstra ehdr->e_ident[EI_PAD] = 0; 14538c64af4fSJohn Polstra ehdr->e_type = ET_CORE; 1454841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32 1455841c0c7eSNathan Whitehorn ehdr->e_machine = ELF_ARCH32; 145662919d78SPeter Wemm #else 14578c64af4fSJohn Polstra ehdr->e_machine = ELF_ARCH; 145862919d78SPeter Wemm #endif 14598c64af4fSJohn Polstra ehdr->e_version = EV_CURRENT; 14608c64af4fSJohn Polstra ehdr->e_entry = 0; 14618c64af4fSJohn Polstra ehdr->e_phoff = phoff; 14628c64af4fSJohn Polstra ehdr->e_flags = 0; 14638c64af4fSJohn Polstra ehdr->e_ehsize = sizeof(Elf_Ehdr); 14648c64af4fSJohn Polstra ehdr->e_phentsize = sizeof(Elf_Phdr); 14650ff27d31SJohn Polstra ehdr->e_phnum = numsegs + 1; 14668c64af4fSJohn Polstra ehdr->e_shentsize = sizeof(Elf_Shdr); 14678c64af4fSJohn Polstra ehdr->e_shnum = 0; 14688c64af4fSJohn Polstra ehdr->e_shstrndx = SHN_UNDEF; 14698c64af4fSJohn Polstra 14708c64af4fSJohn Polstra /* 14718c64af4fSJohn Polstra * Fill in the program header entries. 14728c64af4fSJohn Polstra */ 14738c64af4fSJohn Polstra phdr = (Elf_Phdr *)((char *)dst + phoff); 14748c64af4fSJohn Polstra 14758c64af4fSJohn Polstra /* The note segement. */ 14768c64af4fSJohn Polstra phdr->p_type = PT_NOTE; 14778c64af4fSJohn Polstra phdr->p_offset = noteoff; 14788c64af4fSJohn Polstra phdr->p_vaddr = 0; 14798c64af4fSJohn Polstra phdr->p_paddr = 0; 14808c64af4fSJohn Polstra phdr->p_filesz = notesz; 14818c64af4fSJohn Polstra phdr->p_memsz = 0; 14828c64af4fSJohn Polstra phdr->p_flags = 0; 14838c64af4fSJohn Polstra phdr->p_align = 0; 14848c64af4fSJohn Polstra phdr++; 14858c64af4fSJohn Polstra 14860ff27d31SJohn Polstra /* All the writable segments from the program. */ 14870ff27d31SJohn Polstra phc.phdr = phdr; 14880ff27d31SJohn Polstra phc.offset = *off; 1489247aba24SMarcel Moolenaar each_writable_segment(td, cb_put_phdr, &phc); 14908c64af4fSJohn Polstra } 14918c64af4fSJohn Polstra } 14928c64af4fSJohn Polstra 14938c64af4fSJohn Polstra static void 14943ebc1248SPeter Wemm __elfN(putnote)(void *dst, size_t *off, const char *name, int type, 14958c64af4fSJohn Polstra const void *desc, size_t descsz) 14968c64af4fSJohn Polstra { 14978c64af4fSJohn Polstra Elf_Note note; 14988c64af4fSJohn Polstra 14998c64af4fSJohn Polstra note.n_namesz = strlen(name) + 1; 15008c64af4fSJohn Polstra note.n_descsz = descsz; 15018c64af4fSJohn Polstra note.n_type = type; 15028c64af4fSJohn Polstra if (dst != NULL) 15038c64af4fSJohn Polstra bcopy(¬e, (char *)dst + *off, sizeof note); 15048c64af4fSJohn Polstra *off += sizeof note; 15058c64af4fSJohn Polstra if (dst != NULL) 15068c64af4fSJohn Polstra bcopy(name, (char *)dst + *off, note.n_namesz); 15078c64af4fSJohn Polstra *off += roundup2(note.n_namesz, sizeof(Elf_Size)); 15088c64af4fSJohn Polstra if (dst != NULL) 15098c64af4fSJohn Polstra bcopy(desc, (char *)dst + *off, note.n_descsz); 15108c64af4fSJohn Polstra *off += roundup2(note.n_descsz, sizeof(Elf_Size)); 15118c64af4fSJohn Polstra } 15128c64af4fSJohn Polstra 15138c64af4fSJohn Polstra /* 151432c01de2SDmitry Chagin * Try to find the appropriate ABI-note section for checknote, 151532c01de2SDmitry Chagin * fetch the osreldate for binary from the ELF OSABI-note. Only the 151632c01de2SDmitry Chagin * first page of the image is searched, the same as for headers. 151732c01de2SDmitry Chagin */ 151832c01de2SDmitry Chagin static boolean_t 151932c01de2SDmitry Chagin __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote, 152032c01de2SDmitry Chagin int32_t *osrel) 152132c01de2SDmitry Chagin { 1522267c52fcSKonstantin Belousov const Elf_Note *note, *note0, *note_end; 1523429f5a58SKonstantin Belousov const Elf_Phdr *phdr, *pnote; 1524429f5a58SKonstantin Belousov const Elf_Ehdr *hdr; 152532c01de2SDmitry Chagin const char *note_name; 152632c01de2SDmitry Chagin int i; 152732c01de2SDmitry Chagin 152832c01de2SDmitry Chagin pnote = NULL; 1529429f5a58SKonstantin Belousov hdr = (const Elf_Ehdr *)imgp->image_header; 1530429f5a58SKonstantin Belousov phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 153132c01de2SDmitry Chagin 153232c01de2SDmitry Chagin for (i = 0; i < hdr->e_phnum; i++) { 153332c01de2SDmitry Chagin if (phdr[i].p_type == PT_NOTE) { 153432c01de2SDmitry Chagin pnote = &phdr[i]; 153532c01de2SDmitry Chagin break; 153632c01de2SDmitry Chagin } 153732c01de2SDmitry Chagin } 153832c01de2SDmitry Chagin 153932c01de2SDmitry Chagin if (pnote == NULL || pnote->p_offset >= PAGE_SIZE || 154032c01de2SDmitry Chagin pnote->p_offset + pnote->p_filesz >= PAGE_SIZE) 154132c01de2SDmitry Chagin return (FALSE); 154232c01de2SDmitry Chagin 1543267c52fcSKonstantin Belousov note = note0 = (const Elf_Note *)(imgp->image_header + pnote->p_offset); 154432c01de2SDmitry Chagin note_end = (const Elf_Note *)(imgp->image_header + 154532c01de2SDmitry Chagin pnote->p_offset + pnote->p_filesz); 1546267c52fcSKonstantin Belousov for (i = 0; i < 100 && note >= note0 && note < note_end; i++) { 1547267c52fcSKonstantin Belousov if (!aligned(note, Elf32_Addr)) 1548267c52fcSKonstantin Belousov return (FALSE); 154932c01de2SDmitry Chagin if (note->n_namesz != checknote->hdr.n_namesz || 155032c01de2SDmitry Chagin note->n_descsz != checknote->hdr.n_descsz || 155132c01de2SDmitry Chagin note->n_type != checknote->hdr.n_type) 155232c01de2SDmitry Chagin goto nextnote; 155332c01de2SDmitry Chagin note_name = (const char *)(note + 1); 155432c01de2SDmitry Chagin if (strncmp(checknote->vendor, note_name, 155532c01de2SDmitry Chagin checknote->hdr.n_namesz) != 0) 155632c01de2SDmitry Chagin goto nextnote; 155732c01de2SDmitry Chagin 155832c01de2SDmitry Chagin /* 155932c01de2SDmitry Chagin * Fetch the osreldate for binary 156032c01de2SDmitry Chagin * from the ELF OSABI-note if necessary. 156132c01de2SDmitry Chagin */ 156289ffc202SBjoern A. Zeeb if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 && 156389ffc202SBjoern A. Zeeb checknote->trans_osrel != NULL) 156489ffc202SBjoern A. Zeeb return (checknote->trans_osrel(note, osrel)); 156532c01de2SDmitry Chagin return (TRUE); 156632c01de2SDmitry Chagin 156732c01de2SDmitry Chagin nextnote: 156832c01de2SDmitry Chagin note = (const Elf_Note *)((const char *)(note + 1) + 156932c01de2SDmitry Chagin roundup2(note->n_namesz, sizeof(Elf32_Addr)) + 157032c01de2SDmitry Chagin roundup2(note->n_descsz, sizeof(Elf32_Addr))); 157132c01de2SDmitry Chagin } 157232c01de2SDmitry Chagin 157332c01de2SDmitry Chagin return (FALSE); 157432c01de2SDmitry Chagin } 157532c01de2SDmitry Chagin 157632c01de2SDmitry Chagin /* 1577e1743d02SSøren Schmidt * Tell kern_execve.c about it, with a little help from the linker. 1578e1743d02SSøren Schmidt */ 1579a360a43dSJake Burkholder static struct execsw __elfN(execsw) = { 1580a360a43dSJake Burkholder __CONCAT(exec_, __elfN(imgact)), 1581a360a43dSJake Burkholder __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) 1582a360a43dSJake Burkholder }; 1583a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw)); 1584e7228204SAlfred Perlstein 1585e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES 1586e7228204SAlfred Perlstein /* 1587e7228204SAlfred Perlstein * Compress and write out a core segment for a user process. 1588e7228204SAlfred Perlstein * 1589e7228204SAlfred Perlstein * 'inbuf' is the starting address of a VM segment in the process' address 1590e7228204SAlfred Perlstein * space that is to be compressed and written out to the core file. 'dest_buf' 1591e7228204SAlfred Perlstein * is a buffer in the kernel's address space. The segment is copied from 1592e7228204SAlfred Perlstein * 'inbuf' to 'dest_buf' first before being processed by the compression 1593e7228204SAlfred Perlstein * routine gzwrite(). This copying is necessary because the content of the VM 1594e7228204SAlfred Perlstein * segment may change between the compression pass and the crc-computation pass 1595e7228204SAlfred Perlstein * in gzwrite(). This is because realtime threads may preempt the UNIX kernel. 1596e7228204SAlfred Perlstein */ 1597e7228204SAlfred Perlstein static int 1598e7228204SAlfred Perlstein compress_core (gzFile file, char *inbuf, char *dest_buf, unsigned int len, 1599e7228204SAlfred Perlstein struct thread *td) 1600e7228204SAlfred Perlstein { 1601e7228204SAlfred Perlstein int len_compressed; 1602e7228204SAlfred Perlstein int error = 0; 1603e7228204SAlfred Perlstein unsigned int chunk_len; 1604e7228204SAlfred Perlstein 1605e7228204SAlfred Perlstein while (len) { 1606e7228204SAlfred Perlstein chunk_len = (len > CORE_BUF_SIZE) ? CORE_BUF_SIZE : len; 1607e7228204SAlfred Perlstein copyin(inbuf, dest_buf, chunk_len); 1608e7228204SAlfred Perlstein len_compressed = gzwrite(file, dest_buf, chunk_len); 1609e7228204SAlfred Perlstein 1610e7228204SAlfred Perlstein EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed); 1611e7228204SAlfred Perlstein 1612e7228204SAlfred Perlstein if ((unsigned int)len_compressed != chunk_len) { 1613e7228204SAlfred Perlstein log(LOG_WARNING, 1614e7228204SAlfred Perlstein "compress_core: length mismatch (0x%x returned, " 1615e7228204SAlfred Perlstein "0x%x expected)\n", len_compressed, chunk_len); 1616e7228204SAlfred Perlstein EVENTHANDLER_INVOKE(app_coredump_error, td, 1617e7228204SAlfred Perlstein "compress_core: length mismatch %x -> %x", 1618e7228204SAlfred Perlstein chunk_len, len_compressed); 1619e7228204SAlfred Perlstein error = EFAULT; 1620e7228204SAlfred Perlstein break; 1621e7228204SAlfred Perlstein } 1622e7228204SAlfred Perlstein inbuf += chunk_len; 1623e7228204SAlfred Perlstein len -= chunk_len; 1624e7228204SAlfred Perlstein if (ticks - PCPU_GET(switchticks) >= hogticks) 1625e7228204SAlfred Perlstein uio_yield(); 1626e7228204SAlfred Perlstein } 1627e7228204SAlfred Perlstein 1628e7228204SAlfred Perlstein return (error); 1629e7228204SAlfred Perlstein } 1630e7228204SAlfred Perlstein #endif /* COMPRESS_USER_CORES */ 1631ed167eaaSKonstantin Belousov 1632ed167eaaSKonstantin Belousov static vm_prot_t 1633ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags) 1634ed167eaaSKonstantin Belousov { 1635ed167eaaSKonstantin Belousov vm_prot_t prot; 1636ed167eaaSKonstantin Belousov 1637ed167eaaSKonstantin Belousov prot = 0; 1638ed167eaaSKonstantin Belousov if (flags & PF_X) 1639ed167eaaSKonstantin Belousov prot |= VM_PROT_EXECUTE; 1640ed167eaaSKonstantin Belousov if (flags & PF_W) 1641ed167eaaSKonstantin Belousov prot |= VM_PROT_WRITE; 1642ed167eaaSKonstantin Belousov if (flags & PF_R) 1643ed167eaaSKonstantin Belousov prot |= VM_PROT_READ; 1644ed167eaaSKonstantin Belousov return (prot); 1645ed167eaaSKonstantin Belousov } 1646ed167eaaSKonstantin Belousov 1647ed167eaaSKonstantin Belousov static Elf_Word 1648ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot) 1649ed167eaaSKonstantin Belousov { 1650ed167eaaSKonstantin Belousov Elf_Word flags; 1651ed167eaaSKonstantin Belousov 1652ed167eaaSKonstantin Belousov flags = 0; 1653ed167eaaSKonstantin Belousov if (prot & VM_PROT_EXECUTE) 1654ed167eaaSKonstantin Belousov flags |= PF_X; 1655ed167eaaSKonstantin Belousov if (prot & VM_PROT_READ) 1656ed167eaaSKonstantin Belousov flags |= PF_R; 1657ed167eaaSKonstantin Belousov if (prot & VM_PROT_WRITE) 1658ed167eaaSKonstantin Belousov flags |= PF_W; 1659ed167eaaSKonstantin Belousov return (flags); 1660ed167eaaSKonstantin Belousov } 1661