exec_elf32.c revision 1.57
1/* $NetBSD: exec_elf32.c,v 1.57 2000/11/14 22:14:53 thorpej Exp $ */ 2 3/*- 4 * Copyright (c) 1994 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39/* 40 * Copyright (c) 1996 Christopher G. Demetriou 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. The name of the author may not be used to endorse or promote products 52 * derived from this software without specific prior written permission 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 57 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 58 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 59 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 60 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 61 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 63 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 */ 65 66/* If not included by exec_elf64.c, ELFSIZE won't be defined. */ 67#ifndef ELFSIZE 68#define ELFSIZE 32 69#endif 70 71#include "opt_compat_linux.h" 72#include "opt_compat_ibcs2.h" 73#include "opt_compat_svr4.h" 74#include "opt_compat_freebsd.h" 75#include "opt_compat_netbsd32.h" 76#include "opt_syscall_debug.h" 77 78#include <sys/param.h> 79#include <sys/proc.h> 80#include <sys/malloc.h> 81#include <sys/namei.h> 82#include <sys/vnode.h> 83#include <sys/exec.h> 84#include <sys/exec_elf.h> 85#include <sys/syscall.h> 86#include <sys/signalvar.h> 87#include <sys/mount.h> 88#include <sys/stat.h> 89 90#include <uvm/uvm_extern.h> 91 92#ifdef COMPAT_NETBSD32 93#include <compat/netbsd32/netbsd32_exec.h> 94#endif 95 96#ifdef COMPAT_LINUX 97#include <compat/linux/common/linux_exec.h> 98#endif 99 100#ifdef COMPAT_SVR4 101#include <compat/svr4/svr4_exec.h> 102#endif 103 104#ifdef COMPAT_IBCS2 105#include <compat/ibcs2/ibcs2_exec.h> 106#endif 107 108#ifdef COMPAT_FREEBSD 109#include <compat/freebsd/freebsd_exec.h> 110#endif 111 112int ELFNAME(check_header)(Elf_Ehdr *, int); 113int ELFNAME(load_file)(struct proc *, struct exec_package *, char *, 114 struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *); 115void ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *, 116 const Elf_Phdr *, Elf_Addr *, u_long *, int *, int); 117 118int ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *, 119 Elf_Ehdr *); 120static int ELFNAME2(netbsd,probe)(struct proc *, struct exec_package *, 121 Elf_Ehdr *, char *, Elf_Addr *); 122 123extern char sigcode[], esigcode[]; 124#ifdef SYSCALL_DEBUG 125extern const char * const syscallnames[]; 126#endif 127 128struct emul ELFNAMEEND(emul_netbsd) = { 129 "netbsd", 130 NULL, 131 sendsig, 132 SYS_syscall, 133 SYS_MAXSYSCALL, 134 sysent, 135#ifdef SYSCALL_DEBUG 136 syscallnames, 137#else 138 NULL, 139#endif 140 howmany(ELF_AUX_ENTRIES * sizeof(AuxInfo), sizeof (Elf_Addr)), 141 ELFNAME(copyargs), 142 setregs, 143 sigcode, 144 esigcode, 145}; 146 147int (*ELFNAME(probe_funcs)[])(struct proc *, struct exec_package *, 148 Elf_Ehdr *, char *, Elf_Addr *) = { 149#if defined(COMPAT_NETBSD32) && (ELFSIZE == 32) 150 /* This one should go first so it matches instead of netbsd */ 151 ELFNAME2(netbsd32,probe), 152#endif 153 ELFNAME2(netbsd,probe), 154#if defined(COMPAT_FREEBSD) && (ELFSIZE == 32) 155 ELFNAME2(freebsd,probe), /* XXX not 64-bit safe */ 156#endif 157#if defined(COMPAT_LINUX) 158 ELFNAME2(linux,probe), 159#endif 160#if defined(COMPAT_SVR4) && (ELFSIZE == 32) 161 ELFNAME2(svr4,probe), /* XXX not 64-bit safe */ 162#endif 163#if defined(COMPAT_IBCS2) && (ELFSIZE == 32) 164 ELFNAME2(ibcs2,probe), /* XXX not 64-bit safe */ 165#endif 166}; 167 168/* round up and down to page boundaries. */ 169#define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 170#define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 171 172/* 173 * Copy arguments onto the stack in the normal way, but add some 174 * extra information in case of dynamic binding. 175 */ 176void * 177ELFNAME(copyargs)(struct exec_package *pack, struct ps_strings *arginfo, 178 void *stack, void *argp) 179{ 180 size_t len; 181 AuxInfo ai[ELF_AUX_ENTRIES], *a; 182 struct elf_args *ap; 183 184 stack = copyargs(pack, arginfo, stack, argp); 185 if (!stack) 186 return NULL; 187 188 a = ai; 189 190 /* 191 * Push extra arguments on the stack needed by dynamically 192 * linked binaries 193 */ 194 if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 195 196 a->a_type = AT_PHDR; 197 a->a_v = ap->arg_phaddr; 198 a++; 199 200 a->a_type = AT_PHENT; 201 a->a_v = ap->arg_phentsize; 202 a++; 203 204 a->a_type = AT_PHNUM; 205 a->a_v = ap->arg_phnum; 206 a++; 207 208 a->a_type = AT_PAGESZ; 209 a->a_v = PAGE_SIZE; 210 a++; 211 212 a->a_type = AT_BASE; 213 a->a_v = ap->arg_interp; 214 a++; 215 216 a->a_type = AT_FLAGS; 217 a->a_v = 0; 218 a++; 219 220 a->a_type = AT_ENTRY; 221 a->a_v = ap->arg_entry; 222 a++; 223 224 free((char *)ap, M_TEMP); 225 pack->ep_emul_arg = NULL; 226 } 227 228 a->a_type = AT_NULL; 229 a->a_v = 0; 230 a++; 231 232 len = (a - ai) * sizeof(AuxInfo); 233 if (copyout(ai, stack, len)) 234 return NULL; 235 stack = (caddr_t)stack + len; 236 237 return stack; 238} 239 240/* 241 * elf_check_header(): 242 * 243 * Check header for validity; return 0 of ok ENOEXEC if error 244 */ 245int 246ELFNAME(check_header)(Elf_Ehdr *eh, int type) 247{ 248 249 if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 || 250 eh->e_ident[EI_CLASS] != ELFCLASS) 251 return ENOEXEC; 252 253 switch (eh->e_machine) { 254 255 ELFDEFNNAME(MACHDEP_ID_CASES) 256 257 default: 258 return ENOEXEC; 259 } 260 261 if (eh->e_type != type) 262 return ENOEXEC; 263 264 return 0; 265} 266 267/* 268 * elf_load_psection(): 269 * 270 * Load a psection at the appropriate address 271 */ 272void 273ELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp, 274 const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags) 275{ 276 u_long uaddr, msize, psize, rm, rf; 277 long diff, offset; 278 279 /* 280 * If the user specified an address, then we load there. 281 */ 282 if (*addr != ELFDEFNNAME(NO_ADDR)) { 283 if (ph->p_align > 1) { 284 *addr = ELF_TRUNC(*addr, ph->p_align); 285 uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align); 286 } else 287 uaddr = ph->p_vaddr; 288 diff = ph->p_vaddr - uaddr; 289 } else { 290 *addr = uaddr = ph->p_vaddr; 291 if (ph->p_align > 1) 292 *addr = ELF_TRUNC(uaddr, ph->p_align); 293 diff = uaddr - *addr; 294 } 295 296 *prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0; 297 *prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0; 298 *prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0; 299 300 offset = ph->p_offset - diff; 301 *size = ph->p_filesz + diff; 302 msize = ph->p_memsz + diff; 303 psize = round_page(*size); 304 305 if ((ph->p_flags & PF_W) != 0) { 306 /* 307 * Because the pagedvn pager can't handle zero fill of the last 308 * data page if it's not page aligned we map the last page 309 * readvn. 310 */ 311 psize = trunc_page(*size); 312 } 313 if (psize > 0) { 314 NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, *addr, vp, 315 offset, *prot, flags); 316 } 317 if (psize < *size) { 318 NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize, 319 *addr + psize, vp, offset + psize, *prot, 320 psize > 0 ? flags & VMCMD_RELATIVE : flags); 321 } 322 323 /* 324 * Check if we need to extend the size of the segment 325 */ 326 rm = round_page(*addr + msize); 327 rf = round_page(*addr + *size); 328 329 if (rm != rf) { 330 NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 331 0, *prot, flags & VMCMD_RELATIVE); 332 *size = msize; 333 } 334} 335 336/* 337 * elf_read_from(): 338 * 339 * Read from vnode into buffer at offset. 340 */ 341int 342ELFNAME(read_from)(struct proc *p, struct vnode *vp, u_long off, 343 caddr_t buf, int size) 344{ 345 int error; 346 size_t resid; 347 348 if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE, 349 0, p->p_ucred, &resid, p)) != 0) 350 return error; 351 /* 352 * See if we got all of it 353 */ 354 if (resid != 0) 355 return ENOEXEC; 356 return 0; 357} 358 359/* 360 * elf_load_file(): 361 * 362 * Load a file (interpreter/library) pointed to by path 363 * [stolen from coff_load_shlib()]. Made slightly generic 364 * so it might be used externally. 365 */ 366int 367ELFNAME(load_file)(struct proc *p, struct exec_package *epp, char *path, 368 struct exec_vmcmd_set *vcset, u_long *entry, struct elf_args *ap, 369 Elf_Addr *last) 370{ 371 int error, i; 372 struct nameidata nd; 373 struct vnode *vp; 374 struct vattr attr; 375 Elf_Ehdr eh; 376 Elf_Phdr *ph = NULL; 377 Elf_Phdr *base_ph = NULL; 378 u_long phsize; 379 char *bp = NULL; 380 Elf_Addr addr = *last; 381 382 bp = path; 383 /* 384 * 1. open file 385 * 2. read filehdr 386 * 3. map text, data, and bss out of it using VM_* 387 */ 388 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p); 389 if ((error = namei(&nd)) != 0) 390 return error; 391 vp = nd.ni_vp; 392 393 /* 394 * Similarly, if it's not marked as executable, or it's not a regular 395 * file, we don't allow it to be used. 396 */ 397 if (vp->v_type != VREG) { 398 error = EACCES; 399 goto badunlock; 400 } 401 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0) 402 goto badunlock; 403 404 /* get attributes */ 405 if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0) 406 goto badunlock; 407 408 /* 409 * Check mount point. Though we're not trying to exec this binary, 410 * we will be executing code from it, so if the mount point 411 * disallows execution or set-id-ness, we punt or kill the set-id. 412 */ 413 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 414 error = EACCES; 415 goto badunlock; 416 } 417 if (vp->v_mount->mnt_flag & MNT_NOSUID) 418 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 419 420#ifdef notyet /* XXX cgd 960926 */ 421 XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?) 422#endif 423 VOP_UNLOCK(vp, 0); 424 425 if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh, 426 sizeof(eh))) != 0) 427 goto bad; 428 429 if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0) 430 goto bad; 431 432 phsize = eh.e_phnum * sizeof(Elf_Phdr); 433 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 434 435 if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff, 436 (caddr_t) ph, phsize)) != 0) 437 goto bad; 438 439 /* 440 * Load all the necessary sections 441 */ 442 for (i = 0; i < eh.e_phnum; i++) { 443 u_long size = 0; 444 int prot = 0; 445 int flags; 446 447 switch (ph[i].p_type) { 448 case PT_LOAD: 449 if (base_ph == NULL) { 450 addr = *last; 451 flags = VMCMD_BASE; 452 } else { 453 addr = ph[i].p_vaddr - base_ph->p_vaddr; 454 flags = VMCMD_RELATIVE; 455 } 456 ELFNAME(load_psection)(vcset, vp, &ph[i], &addr, 457 &size, &prot, flags); 458 /* If entry is within this section it must be text */ 459 if (eh.e_entry >= ph[i].p_vaddr && 460 eh.e_entry < (ph[i].p_vaddr + size)) { 461 /* XXX */ 462 *entry = addr + eh.e_entry; 463#ifdef mips 464 *entry -= ph[i].p_vaddr; 465#endif 466 ap->arg_interp = addr; 467 } 468 if (base_ph == NULL) 469 base_ph = &ph[i]; 470 addr += size; 471 break; 472 473 case PT_DYNAMIC: 474 case PT_PHDR: 475 case PT_NOTE: 476 break; 477 478 default: 479 break; 480 } 481 } 482 483 free((char *)ph, M_TEMP); 484 *last = addr; 485 vrele(vp); 486 return 0; 487 488badunlock: 489 VOP_UNLOCK(vp, 0); 490 491bad: 492 if (ph != NULL) 493 free((char *)ph, M_TEMP); 494#ifdef notyet /* XXX cgd 960926 */ 495 (maybe) VOP_CLOSE it 496#endif 497 vrele(vp); 498 return error; 499} 500 501/* 502 * exec_elf_makecmds(): Prepare an Elf binary's exec package 503 * 504 * First, set of the various offsets/lengths in the exec package. 505 * 506 * Then, mark the text image busy (so it can be demand paged) or error 507 * out if this is not possible. Finally, set up vmcmds for the 508 * text, data, bss, and stack segments. 509 */ 510int 511ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) 512{ 513 Elf_Ehdr *eh = epp->ep_hdr; 514 Elf_Phdr *ph, *pp; 515 Elf_Addr phdr = 0, pos = 0; 516 int error, i, n, nload; 517 char interp[MAXPATHLEN]; 518 u_long phsize; 519 520 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 521 return ENOEXEC; 522 523 /* 524 * XXX allow for executing shared objects. It seems silly 525 * but other ELF-based systems allow it as well. 526 */ 527 if (ELFNAME(check_header)(eh, ET_EXEC) != 0 && 528 ELFNAME(check_header)(eh, ET_DYN) != 0) 529 return ENOEXEC; 530 531 /* 532 * check if vnode is in open for writing, because we want to 533 * demand-page out of it. if it is, don't do it, for various 534 * reasons 535 */ 536 if (epp->ep_vp->v_writecount != 0) { 537#ifdef DIAGNOSTIC 538 if (epp->ep_vp->v_flag & VTEXT) 539 panic("exec: a VTEXT vnode has writecount != 0\n"); 540#endif 541 return ETXTBSY; 542 } 543 /* 544 * Allocate space to hold all the program headers, and read them 545 * from the file 546 */ 547 phsize = eh->e_phnum * sizeof(Elf_Phdr); 548 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 549 550 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, 551 (caddr_t) ph, phsize)) != 0) 552 goto bad; 553 554 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 555 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 556 557 interp[0] = '\0'; 558 559 for (i = 0; i < eh->e_phnum; i++) { 560 pp = &ph[i]; 561 if (pp->p_type == PT_INTERP) { 562 if (pp->p_filesz >= sizeof(interp)) 563 goto bad; 564 if ((error = ELFNAME(read_from)(p, epp->ep_vp, 565 pp->p_offset, (caddr_t) interp, 566 pp->p_filesz)) != 0) 567 goto bad; 568 break; 569 } 570 } 571 572 /* 573 * Setup things for native emulation. 574 */ 575 epp->ep_emul = &ELFNAMEEND(emul_netbsd); 576 pos = ELFDEFNNAME(NO_ADDR); 577 578 /* 579 * On the same architecture, we may be emulating different systems. 580 * See which one will accept this executable. This currently only 581 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386 582 * and the Alpha. 583 * 584 * Probe functions would normally see if the interpreter (if any) 585 * exists. Emulation packages may possibly replace the interpreter in 586 * interp[] with a changed path (/emul/xxx/<path>), and also 587 * set the ep_emul field in the exec package structure. 588 */ 589 n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]); 590 if (n != 0) { 591 error = ENOEXEC; 592 for (i = 0; i < n && error; i++) 593 error = ELFNAME(probe_funcs)[i](p, epp, eh, 594 interp, &pos); 595#ifdef notyet 596 /* 597 * We should really use a signature in our native binaries 598 * and have our own probe function for matching binaries, 599 * before trying the emulations. For now, if the emulation 600 * probes failed we default to native. 601 */ 602 if (error) 603 goto bad; 604#endif 605 } 606 607 /* 608 * Load all the necessary sections 609 */ 610 for (i = nload = 0; i < eh->e_phnum; i++) { 611 Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 612 u_long size = 0; 613 int prot = 0; 614 615 pp = &ph[i]; 616 617 switch (ph[i].p_type) { 618 case PT_LOAD: 619 /* 620 * XXX 621 * Can handle only 2 sections: text and data 622 */ 623 if (nload++ == 2) 624 goto bad; 625 ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp, 626 &ph[i], &addr, &size, &prot, 0); 627 628 /* 629 * Decide whether it's text or data by looking 630 * at the entry point. 631 */ 632 if (eh->e_entry >= addr && 633 eh->e_entry < (addr + size)) { 634 epp->ep_taddr = addr; 635 epp->ep_tsize = size; 636 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 637 epp->ep_daddr = addr; 638 epp->ep_dsize = size; 639 } 640 } else { 641 epp->ep_daddr = addr; 642 epp->ep_dsize = size; 643 } 644 break; 645 646 case PT_SHLIB: 647#ifndef COMPAT_IBCS2 /* SCO has these sections */ 648 error = ENOEXEC; 649 goto bad; 650#endif 651 652 case PT_INTERP: 653 /* Already did this one */ 654 case PT_DYNAMIC: 655 case PT_NOTE: 656 break; 657 658 case PT_PHDR: 659 /* Note address of program headers (in text segment) */ 660 phdr = pp->p_vaddr; 661 break; 662 663 default: 664 /* 665 * Not fatal; we don't need to understand everything. 666 */ 667 break; 668 } 669 } 670 671 /* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */ 672#ifndef ELF_INTERP_NON_RELOCATABLE 673 /* 674 * If no position to load the interpreter was set by a probe 675 * function, pick the same address that a non-fixed mmap(0, ..) 676 * would (i.e. something safely out of the way). 677 */ 678 if (pos == ELFDEFNNAME(NO_ADDR)) 679 pos = round_page(epp->ep_daddr + MAXDSIZ); 680#endif /* !ELF_INTERP_NON_RELOCATABLE */ 681 682 /* 683 * Check if we found a dynamically linked binary and arrange to load 684 * it's interpreter 685 */ 686 if (interp[0]) { 687 struct elf_args *ap; 688 689 ap = (struct elf_args *)malloc(sizeof(struct elf_args), 690 M_TEMP, M_WAITOK); 691 if ((error = ELFNAME(load_file)(p, epp, interp, 692 &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) { 693 free((char *)ap, M_TEMP); 694 goto bad; 695 } 696 pos += phsize; 697 ap->arg_phaddr = phdr; 698 699 ap->arg_phentsize = eh->e_phentsize; 700 ap->arg_phnum = eh->e_phnum; 701 ap->arg_entry = eh->e_entry; 702 703 epp->ep_emul_arg = ap; 704 } else 705 epp->ep_entry = eh->e_entry; 706 707#ifdef ELF_MAP_PAGE_ZERO 708 /* Dell SVR4 maps page zero, yeuch! */ 709 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0, 710 epp->ep_vp, 0, VM_PROT_READ); 711#endif 712 free((char *)ph, M_TEMP); 713 vn_marktext(epp->ep_vp); 714 return exec_elf_setup_stack(p, epp); 715 716bad: 717 free((char *)ph, M_TEMP); 718 kill_vmcmds(&epp->ep_vmcmds); 719 return ENOEXEC; 720} 721 722int 723ELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp, 724 Elf_Ehdr *eh) 725{ 726 Elf_Phdr *hph, *ph; 727 Elf_Nhdr *np = NULL; 728 size_t phsize; 729 int error; 730 731 phsize = eh->e_phnum * sizeof(Elf_Phdr); 732 hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 733 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, 734 (caddr_t)hph, phsize)) != 0) 735 goto out1; 736 737 for (ph = hph; ph < &hph[eh->e_phnum]; ph++) { 738 if (ph->p_type != PT_NOTE || 739 ph->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ) 740 continue; 741 742 np = (Elf_Nhdr *)malloc(ph->p_filesz, M_TEMP, M_WAITOK); 743 if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset, 744 (caddr_t)np, ph->p_filesz)) != 0) 745 goto out2; 746 747 if (np->n_type != ELF_NOTE_TYPE_OSVERSION) { 748 free(np, M_TEMP); 749 np = NULL; 750 continue; 751 } 752 753 /* Check the name and description sizes. */ 754 if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ || 755 np->n_descsz != ELF_NOTE_NETBSD_DESCSZ) 756 goto out3; 757 758 /* Is the name "NetBSD\0\0"? */ 759 if (memcmp((np + 1), ELF_NOTE_NETBSD_NAME, 760 ELF_NOTE_NETBSD_NAMESZ)) 761 goto out3; 762 763 /* XXX: We could check for the specific emulation here */ 764 /* All checks succeeded. */ 765 error = 0; 766 goto out2; 767 } 768 769out3: 770 error = ENOEXEC; 771out2: 772 if (np) 773 free(np, M_TEMP); 774out1: 775 free(hph, M_TEMP); 776 return error; 777} 778 779static int 780ELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp, 781 Elf_Ehdr *eh, char *itp, Elf_Addr *pos) 782{ 783 int error; 784 785 if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0) 786 return error; 787 epp->ep_emul = &ELFNAMEEND(emul_netbsd); 788 *pos = ELFDEFNNAME(NO_ADDR); 789 return 0; 790} 791