exec_elf32.c revision 1.90
1/* $NetBSD: exec_elf32.c,v 1.90 2003/06/25 13:48:06 christos Exp $ */ 2 3/*- 4 * Copyright (c) 1994, 2000 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#include <sys/cdefs.h> 67__KERNEL_RCSID(1, "$NetBSD: exec_elf32.c,v 1.90 2003/06/25 13:48:06 christos Exp $"); 68 69/* If not included by exec_elf64.c, ELFSIZE won't be defined. */ 70#ifndef ELFSIZE 71#define ELFSIZE 32 72#endif 73 74#include <sys/param.h> 75#include <sys/proc.h> 76#include <sys/malloc.h> 77#include <sys/namei.h> 78#include <sys/vnode.h> 79#include <sys/exec.h> 80#include <sys/exec_elf.h> 81#include <sys/syscall.h> 82#include <sys/signalvar.h> 83#include <sys/mount.h> 84#include <sys/stat.h> 85 86#include <machine/cpu.h> 87#include <machine/reg.h> 88 89extern const struct emul emul_netbsd; 90 91int ELFNAME(check_header)(Elf_Ehdr *, int); 92int ELFNAME(load_file)(struct proc *, struct exec_package *, char *, 93 struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *); 94void ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *, 95 const Elf_Phdr *, Elf_Addr *, u_long *, int *, int); 96 97int ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *, 98 Elf_Ehdr *); 99int ELFNAME2(netbsd,probe)(struct proc *, struct exec_package *, 100 void *, char *, vaddr_t *); 101 102/* round up and down to page boundaries. */ 103#define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 104#define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 105 106#define MAXPHNUM 50 107 108/* 109 * Copy arguments onto the stack in the normal way, but add some 110 * extra information in case of dynamic binding. 111 */ 112int 113ELFNAME(copyargs)(struct proc *p, struct exec_package *pack, 114 struct ps_strings *arginfo, char **stackp, void *argp) 115{ 116 size_t len; 117 AuxInfo ai[ELF_AUX_ENTRIES], *a; 118 struct elf_args *ap; 119 int error; 120 121 if ((error = copyargs(p, pack, arginfo, stackp, argp)) != 0) 122 return error; 123 124 a = ai; 125 126 /* 127 * Push extra arguments on the stack needed by dynamically 128 * linked binaries 129 */ 130 if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 131 struct vattr *vap = pack->ep_vap; 132 133 a->a_type = AT_PHDR; 134 a->a_v = ap->arg_phaddr; 135 a++; 136 137 a->a_type = AT_PHENT; 138 a->a_v = ap->arg_phentsize; 139 a++; 140 141 a->a_type = AT_PHNUM; 142 a->a_v = ap->arg_phnum; 143 a++; 144 145 a->a_type = AT_PAGESZ; 146 a->a_v = PAGE_SIZE; 147 a++; 148 149 a->a_type = AT_BASE; 150 a->a_v = ap->arg_interp; 151 a++; 152 153 a->a_type = AT_FLAGS; 154 a->a_v = 0; 155 a++; 156 157 a->a_type = AT_ENTRY; 158 a->a_v = ap->arg_entry; 159 a++; 160 161 a->a_type = AT_EUID; 162 if (vap->va_mode & S_ISUID) 163 a->a_v = vap->va_uid; 164 else 165 a->a_v = p->p_ucred->cr_uid; 166 a++; 167 168 a->a_type = AT_RUID; 169 a->a_v = p->p_cred->p_ruid; 170 a++; 171 172 a->a_type = AT_EGID; 173 if (vap->va_mode & S_ISGID) 174 a->a_v = vap->va_gid; 175 else 176 a->a_v = p->p_ucred->cr_gid; 177 a++; 178 179 a->a_type = AT_RGID; 180 a->a_v = p->p_cred->p_rgid; 181 a++; 182 183 free(ap, M_TEMP); 184 pack->ep_emul_arg = NULL; 185 } 186 187 a->a_type = AT_NULL; 188 a->a_v = 0; 189 a++; 190 191 len = (a - ai) * sizeof(AuxInfo); 192 if ((error = copyout(ai, *stackp, len)) != 0) 193 return error; 194 *stackp += len; 195 196 return 0; 197} 198 199/* 200 * elf_check_header(): 201 * 202 * Check header for validity; return 0 of ok ENOEXEC if error 203 */ 204int 205ELFNAME(check_header)(Elf_Ehdr *eh, int type) 206{ 207 208 if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 || 209 eh->e_ident[EI_CLASS] != ELFCLASS) 210 return (ENOEXEC); 211 212 switch (eh->e_machine) { 213 214 ELFDEFNNAME(MACHDEP_ID_CASES) 215 216 default: 217 return (ENOEXEC); 218 } 219 220 if (ELF_EHDR_FLAGS_OK(eh) == 0) 221 return (ENOEXEC); 222 223 if (eh->e_type != type) 224 return (ENOEXEC); 225 226 if (eh->e_shnum > 512 || 227 eh->e_phnum > 128) 228 return (ENOEXEC); 229 230 return (0); 231} 232 233/* 234 * elf_load_psection(): 235 * 236 * Load a psection at the appropriate address 237 */ 238void 239ELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp, 240 const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags) 241{ 242 u_long msize, psize, rm, rf; 243 long diff, offset; 244 245 /* 246 * If the user specified an address, then we load there. 247 */ 248 if (*addr == ELFDEFNNAME(NO_ADDR)) 249 *addr = ph->p_vaddr; 250 251 if (ph->p_align > 1) { 252 /* 253 * Make sure we are virtually aligned as we are supposed to be. 254 */ 255 diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align); 256 KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align)); 257 /* 258 * But make sure to not map any pages before the start of the 259 * psection by limiting the difference to within a page. 260 */ 261 diff &= PAGE_MASK; 262 } else 263 diff = 0; 264 265 *prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0; 266 *prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0; 267 *prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0; 268 269 /* 270 * Adjust everything so it all starts on a page boundary. 271 */ 272 *addr -= diff; 273 offset = ph->p_offset - diff; 274 *size = ph->p_filesz + diff; 275 msize = ph->p_memsz + diff; 276 277 if (ph->p_align >= PAGE_SIZE) { 278 if ((ph->p_flags & PF_W) != 0) { 279 /* 280 * Because the pagedvn pager can't handle zero fill 281 * of the last data page if it's not page aligned we 282 * map the last page readvn. 283 */ 284 psize = trunc_page(*size); 285 } else { 286 psize = round_page(*size); 287 } 288 } else { 289 psize = *size; 290 } 291 292 if (psize > 0) { 293 NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ? 294 vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp, 295 offset, *prot, flags); 296 flags &= VMCMD_RELATIVE; 297 } 298 if (psize < *size) { 299 NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize, 300 *addr + psize, vp, offset + psize, *prot, flags); 301 } 302 303 /* 304 * Check if we need to extend the size of the segment (does 305 * bss extend page the next page boundary)? 306 */ 307 rm = round_page(*addr + msize); 308 rf = round_page(*addr + *size); 309 310 if (rm != rf) { 311 NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 312 0, *prot, flags & VMCMD_RELATIVE); 313 *size = msize; 314 } 315} 316 317/* 318 * elf_load_file(): 319 * 320 * Load a file (interpreter/library) pointed to by path 321 * [stolen from coff_load_shlib()]. Made slightly generic 322 * so it might be used externally. 323 */ 324int 325ELFNAME(load_file)(struct proc *p, struct exec_package *epp, char *path, 326 struct exec_vmcmd_set *vcset, u_long *entryoff, struct elf_args *ap, 327 Elf_Addr *last) 328{ 329 int error, i; 330 struct nameidata nd; 331 struct vnode *vp; 332 struct vattr attr; 333 Elf_Ehdr eh; 334 Elf_Phdr *ph = NULL; 335 const Elf_Phdr *ph0; 336 const Elf_Phdr *base_ph; 337 const Elf_Phdr *last_ph; 338 u_long phsize; 339 Elf_Addr addr = *last; 340 341 /* 342 * 1. open file 343 * 2. read filehdr 344 * 3. map text, data, and bss out of it using VM_* 345 */ 346 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p); 347 if ((error = namei(&nd)) != 0) 348 return error; 349 vp = nd.ni_vp; 350 351 /* 352 * Similarly, if it's not marked as executable, or it's not a regular 353 * file, we don't allow it to be used. 354 */ 355 if (vp->v_type != VREG) { 356 error = EACCES; 357 goto badunlock; 358 } 359 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0) 360 goto badunlock; 361 362 /* get attributes */ 363 if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0) 364 goto badunlock; 365 366 /* 367 * Check mount point. Though we're not trying to exec this binary, 368 * we will be executing code from it, so if the mount point 369 * disallows execution or set-id-ness, we punt or kill the set-id. 370 */ 371 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 372 error = EACCES; 373 goto badunlock; 374 } 375 if (vp->v_mount->mnt_flag & MNT_NOSUID) 376 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 377 378#ifdef notyet /* XXX cgd 960926 */ 379 XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?) 380#endif 381 382 error = vn_marktext(vp); 383 if (error) 384 goto badunlock; 385 386 VOP_UNLOCK(vp, 0); 387 388 if ((error = exec_read_from(p, vp, 0, &eh, sizeof(eh))) != 0) 389 goto bad; 390 391 if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0) 392 goto bad; 393 394 if (eh.e_phnum > MAXPHNUM) 395 goto bad; 396 397 phsize = eh.e_phnum * sizeof(Elf_Phdr); 398 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 399 400 if ((error = exec_read_from(p, vp, eh.e_phoff, ph, phsize)) != 0) 401 goto bad; 402 403 /* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */ 404#ifndef ELF_INTERP_NON_RELOCATABLE 405 /* 406 * If no position to load the interpreter was set by a probe 407 * function, pick the same address that a non-fixed mmap(0, ..) 408 * would (i.e. something safely out of the way). 409 */ 410 if (*last == ELFDEFNNAME(NO_ADDR)) { 411 u_long limit = 0; 412 /* 413 * Find the start and ending addresses of the psections to 414 * be loaded. This will give us the size. 415 */ 416 for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum; 417 i++, ph0++) { 418 if (ph0->p_type == PT_LOAD) { 419 u_long psize = ph0->p_vaddr + ph0->p_memsz; 420 if (base_ph == NULL) 421 base_ph = ph0; 422 if (psize > limit) 423 limit = psize; 424 } 425 } 426 427 /* 428 * Now compute the size and load address. 429 */ 430 addr = VM_DEFAULT_ADDRESS(epp->ep_daddr, 431 round_page(limit) - trunc_page(base_ph->p_vaddr)); 432 } else 433#endif /* !ELF_INTERP_NON_RELOCATABLE */ 434 addr = *last; 435 436 /* 437 * Load all the necessary sections 438 */ 439 for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL; 440 i < eh.e_phnum; i++, ph0++) { 441 switch (ph0->p_type) { 442 case PT_LOAD: { 443 u_long size; 444 int prot = 0; 445 int flags; 446 447 if (base_ph == NULL) { 448 /* 449 * First encountered psection is always the 450 * base psection. Make sure it's aligned 451 * properly (align down for topdown and align 452 * upwards for not topdown). 453 */ 454 base_ph = ph0; 455 flags = VMCMD_BASE; 456 if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN) 457 addr = ELF_TRUNC(addr, ph0->p_align); 458 else 459 addr = ELF_ROUND(addr, ph0->p_align); 460 } else { 461 u_long limit = round_page(last_ph->p_vaddr 462 + last_ph->p_memsz); 463 u_long base = trunc_page(ph0->p_vaddr); 464 465 /* 466 * If there is a gap in between the psections, 467 * map it as inaccessible so nothing else 468 * mmap'ed will be placed there. 469 */ 470 if (limit != base) { 471 NEW_VMCMD2(vcset, vmcmd_map_zero, 472 base - limit, 473 limit - base_ph->p_vaddr, NULLVP, 474 0, VM_PROT_NONE, VMCMD_RELATIVE); 475 } 476 477 addr = ph0->p_vaddr - base_ph->p_vaddr; 478 flags = VMCMD_RELATIVE; 479 } 480 last_ph = ph0; 481 ELFNAME(load_psection)(vcset, vp, &ph[i], &addr, 482 &size, &prot, flags); 483 /* 484 * If entry is within this psection then this 485 * must contain the .text section. *entryoff is 486 * relative to the base psection. 487 */ 488 if (eh.e_entry >= ph0->p_vaddr && 489 eh.e_entry < (ph0->p_vaddr + size)) { 490 *entryoff = eh.e_entry - base_ph->p_vaddr; 491 } 492 addr += size; 493 break; 494 } 495 496 case PT_DYNAMIC: 497 case PT_PHDR: 498 case PT_NOTE: 499 break; 500 501 default: 502 break; 503 } 504 } 505 506 free(ph, M_TEMP); 507 /* 508 * This value is ignored if TOPDOWN. 509 */ 510 *last = addr; 511 vrele(vp); 512 return 0; 513 514badunlock: 515 VOP_UNLOCK(vp, 0); 516 517bad: 518 if (ph != NULL) 519 free(ph, M_TEMP); 520#ifdef notyet /* XXX cgd 960926 */ 521 (maybe) VOP_CLOSE it 522#endif 523 vrele(vp); 524 return error; 525} 526 527/* 528 * exec_elf_makecmds(): Prepare an Elf binary's exec package 529 * 530 * First, set of the various offsets/lengths in the exec package. 531 * 532 * Then, mark the text image busy (so it can be demand paged) or error 533 * out if this is not possible. Finally, set up vmcmds for the 534 * text, data, bss, and stack segments. 535 */ 536int 537ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) 538{ 539 Elf_Ehdr *eh = epp->ep_hdr; 540 Elf_Phdr *ph, *pp; 541 Elf_Addr phdr = 0, pos = 0; 542 int error, i, nload; 543 char *interp = NULL; 544 u_long phsize; 545 546 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 547 return ENOEXEC; 548 549 /* 550 * XXX allow for executing shared objects. It seems silly 551 * but other ELF-based systems allow it as well. 552 */ 553 if (ELFNAME(check_header)(eh, ET_EXEC) != 0 && 554 ELFNAME(check_header)(eh, ET_DYN) != 0) 555 return ENOEXEC; 556 557 if (eh->e_phnum > MAXPHNUM) 558 return ENOEXEC; 559 560 error = vn_marktext(epp->ep_vp); 561 if (error) 562 return (error); 563 564 /* 565 * Allocate space to hold all the program headers, and read them 566 * from the file 567 */ 568 phsize = eh->e_phnum * sizeof(Elf_Phdr); 569 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 570 571 if ((error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize)) != 572 0) 573 goto bad; 574 575 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 576 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 577 578 MALLOC(interp, char *, MAXPATHLEN, M_TEMP, M_WAITOK); 579 interp[0] = '\0'; 580 581 for (i = 0; i < eh->e_phnum; i++) { 582 pp = &ph[i]; 583 if (pp->p_type == PT_INTERP) { 584 if (pp->p_filesz >= MAXPATHLEN) 585 goto bad; 586 if ((error = exec_read_from(p, epp->ep_vp, 587 pp->p_offset, interp, pp->p_filesz)) != 0) 588 goto bad; 589 break; 590 } 591 } 592 593 /* 594 * On the same architecture, we may be emulating different systems. 595 * See which one will accept this executable. This currently only 596 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386 597 * and the Alpha. 598 * 599 * Probe functions would normally see if the interpreter (if any) 600 * exists. Emulation packages may possibly replace the interpreter in 601 * interp[] with a changed path (/emul/xxx/<path>). 602 */ 603 if (!epp->ep_esch->u.elf_probe_func) { 604 pos = ELFDEFNNAME(NO_ADDR); 605 } else { 606 vaddr_t startp = 0; 607 608 error = (*epp->ep_esch->u.elf_probe_func)(p, epp, eh, interp, 609 &startp); 610 pos = (Elf_Addr)startp; 611 if (error) 612 goto bad; 613 } 614 615 /* 616 * Load all the necessary sections 617 */ 618 for (i = nload = 0; i < eh->e_phnum; i++) { 619 Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 620 u_long size = 0; 621 int prot = 0; 622 623 pp = &ph[i]; 624 625 switch (ph[i].p_type) { 626 case PT_LOAD: 627 /* 628 * XXX 629 * Can handle only 2 sections: text and data 630 */ 631 if (nload++ == 2) 632 goto bad; 633 ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp, 634 &ph[i], &addr, &size, &prot, VMCMD_FIXED); 635 636 /* 637 * Decide whether it's text or data by looking 638 * at the entry point. 639 */ 640 if (eh->e_entry >= addr && 641 eh->e_entry < (addr + size)) { 642 epp->ep_taddr = addr; 643 epp->ep_tsize = size; 644 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 645 epp->ep_daddr = addr; 646 epp->ep_dsize = size; 647 } 648 } else { 649 epp->ep_daddr = addr; 650 epp->ep_dsize = size; 651 } 652 break; 653 654 case PT_SHLIB: 655 /* SCO has these sections. */ 656 case PT_INTERP: 657 /* Already did this one. */ 658 case PT_DYNAMIC: 659 case PT_NOTE: 660 break; 661 662 case PT_PHDR: 663 /* Note address of program headers (in text segment) */ 664 phdr = pp->p_vaddr; 665 break; 666 667 default: 668 /* 669 * Not fatal; we don't need to understand everything. 670 */ 671 break; 672 } 673 } 674 675 /* 676 * Check if we found a dynamically linked binary and arrange to load 677 * its interpreter 678 */ 679 if (interp[0]) { 680 struct elf_args *ap; 681 int i = epp->ep_vmcmds.evs_used; 682 u_long interp_offset; 683 684 MALLOC(ap, struct elf_args *, sizeof(struct elf_args), 685 M_TEMP, M_WAITOK); 686 if ((error = ELFNAME(load_file)(p, epp, interp, 687 &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) { 688 FREE(ap, M_TEMP); 689 goto bad; 690 } 691 ap->arg_interp = epp->ep_vmcmds.evs_cmds[i].ev_addr; 692 epp->ep_entry = ap->arg_interp + interp_offset; 693 ap->arg_phaddr = phdr; 694 695 ap->arg_phentsize = eh->e_phentsize; 696 ap->arg_phnum = eh->e_phnum; 697 ap->arg_entry = eh->e_entry; 698 699 epp->ep_emul_arg = ap; 700 } else 701 epp->ep_entry = eh->e_entry; 702 703#ifdef ELF_MAP_PAGE_ZERO 704 /* Dell SVR4 maps page zero, yeuch! */ 705 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0, 706 epp->ep_vp, 0, VM_PROT_READ); 707#endif 708 FREE(interp, M_TEMP); 709 free(ph, M_TEMP); 710 return exec_elf_setup_stack(p, epp); 711 712bad: 713 if (interp) 714 FREE(interp, M_TEMP); 715 free(ph, M_TEMP); 716 kill_vmcmds(&epp->ep_vmcmds); 717 return ENOEXEC; 718} 719 720int 721ELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp, 722 Elf_Ehdr *eh) 723{ 724 size_t i; 725 Elf_Phdr *ph; 726 size_t phsize; 727 int error; 728 729 if (eh->e_phnum > MAXPHNUM) 730 return ENOEXEC; 731 732 phsize = eh->e_phnum * sizeof(Elf_Phdr); 733 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 734 error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize); 735 if (error) 736 goto out; 737 738 for (i = 0; i < eh->e_phnum; i++) { 739 Elf_Phdr *ephp = &ph[i]; 740 Elf_Nhdr *np; 741 742 if (ephp->p_type != PT_NOTE || 743 ephp->p_filesz > 1024 || 744 ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ) 745 continue; 746 747 np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK); 748 error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np, 749 ephp->p_filesz); 750 if (error) 751 goto next; 752 753 if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG || 754 np->n_namesz != ELF_NOTE_NETBSD_NAMESZ || 755 np->n_descsz != ELF_NOTE_NETBSD_DESCSZ || 756 memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME, 757 ELF_NOTE_NETBSD_NAMESZ)) 758 goto next; 759 760 error = 0; 761 free(np, M_TEMP); 762 goto out; 763 764 next: 765 free(np, M_TEMP); 766 continue; 767 } 768 769 error = ENOEXEC; 770out: 771 free(ph, M_TEMP); 772 return (error); 773} 774 775int 776ELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp, 777 void *eh, char *itp, vaddr_t *pos) 778{ 779 int error; 780 781 if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0) 782 return error; 783 *pos = ELFDEFNNAME(NO_ADDR); 784 return 0; 785} 786