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