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