exec_elf32.c revision 1.25
1/* $NetBSD: exec_elf32.c,v 1.25 1997/05/08 12:17:47 mycroft Exp $ */ 2 3/* 4 * Copyright (c) 1996 Christopher G. Demetriou 5 * Copyright (c) 1994 Christos Zoulas 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 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 17 * derived from this software without specific prior written permission 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 31/* If not included by exec_elf64.c, ELFSIZE won't be defined. */ 32#ifndef ELFSIZE 33#define ELFSIZE 32 34#endif 35 36#include <sys/param.h> 37#include <sys/systm.h> 38#include <sys/kernel.h> 39#include <sys/proc.h> 40#include <sys/malloc.h> 41#include <sys/namei.h> 42#include <sys/vnode.h> 43#include <sys/exec.h> 44#include <sys/exec_elf.h> 45#include <sys/fcntl.h> 46#include <sys/syscall.h> 47#include <sys/signalvar.h> 48#include <sys/mount.h> 49#include <sys/stat.h> 50 51#include <sys/mman.h> 52#include <vm/vm.h> 53#include <vm/vm_param.h> 54#include <vm/vm_map.h> 55 56#include <machine/cpu.h> 57#include <machine/reg.h> 58 59#ifdef COMPAT_LINUX 60#include <compat/linux/linux_exec.h> 61#endif 62 63#ifdef COMPAT_SVR4 64#include <compat/svr4/svr4_exec.h> 65#endif 66 67#define CONCAT(x,y) __CONCAT(x,y) 68#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x))) 69#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y)))) 70#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE)) 71#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x))) 72 73int ELFNAME(check_header) __P((Elf_Ehdr *, int)); 74int ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *, 75 struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *)); 76void ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *, 77 Elf_Phdr *, Elf_Addr *, u_long *, int *)); 78 79extern char sigcode[], esigcode[]; 80#ifdef SYSCALL_DEBUG 81extern char *syscallnames[]; 82#endif 83 84struct emul ELFNAMEEND(emul_netbsd) = { 85 "netbsd", 86 NULL, 87 sendsig, 88 SYS_syscall, 89 SYS_MAXSYSCALL, 90 sysent, 91#ifdef SYSCALL_DEBUG 92 syscallnames, 93#else 94 NULL, 95#endif 96 ELF_AUX_ENTRIES * sizeof(AuxInfo), 97 ELFNAME(copyargs), 98 setregs, 99 sigcode, 100 esigcode, 101}; 102 103int (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *, 104 Elf_Ehdr *, char *, Elf_Addr *)) = { 105#if defined(COMPAT_LINUX) && (ELFSIZE == 32) 106 ELFNAME2(linux,probe), /* XXX not 64-bit safe */ 107#endif 108#if defined(COMPAT_SVR4) && (ELFSIZE == 32) 109 ELFNAME2(svr4,probe), /* XXX not 64-bit safe */ 110#endif 111}; 112 113/* round up and down to page boundaries. */ 114#define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 115#define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 116 117/* 118 * Copy arguments onto the stack in the normal way, but add some 119 * extra information in case of dynamic binding. 120 */ 121void * 122ELFNAME(copyargs)(pack, arginfo, stack, argp) 123 struct exec_package *pack; 124 struct ps_strings *arginfo; 125 void *stack; 126 void *argp; 127{ 128 size_t len; 129 AuxInfo ai[ELF_AUX_ENTRIES], *a; 130 struct elf_args *ap; 131 132 stack = copyargs(pack, arginfo, stack, argp); 133 if (!stack) 134 return NULL; 135 136 a = ai; 137 138 /* 139 * Push extra arguments on the stack needed by dynamically 140 * linked binaries 141 */ 142 if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 143 144 a->au_id = AUX_phdr; 145 a->au_v = ap->arg_phaddr; 146 a++; 147 148 a->au_id = AUX_phent; 149 a->au_v = ap->arg_phentsize; 150 a++; 151 152 a->au_id = AUX_phnum; 153 a->au_v = ap->arg_phnum; 154 a++; 155 156 a->au_id = AUX_pagesz; 157 a->au_v = NBPG; 158 a++; 159 160 a->au_id = AUX_base; 161 a->au_v = ap->arg_interp; 162 a++; 163 164 a->au_id = AUX_flags; 165 a->au_v = 0; 166 a++; 167 168 a->au_id = AUX_entry; 169 a->au_v = ap->arg_entry; 170 a++; 171 172 free((char *)ap, M_TEMP); 173 pack->ep_emul_arg = NULL; 174 } 175 176 a->au_id = AUX_null; 177 a->au_v = 0; 178 a++; 179 180 len = (a - ai) * sizeof (AuxInfo); 181 if (copyout(ai, stack, len)) 182 return NULL; 183 stack += len; 184 185 return stack; 186} 187 188/* 189 * elf_check_header(): 190 * 191 * Check header for validity; return 0 of ok ENOEXEC if error 192 */ 193int 194ELFNAME(check_header)(eh, type) 195 Elf_Ehdr *eh; 196 int type; 197{ 198 199 if (bcmp(eh->e_ident, Elf_e_ident, Elf_e_siz) != 0) 200 return ENOEXEC; 201 202 switch (eh->e_machine) { 203 204 ELFDEFNNAME(MACHDEP_ID_CASES) 205 206 default: 207 return ENOEXEC; 208 } 209 210 if (eh->e_type != type) 211 return ENOEXEC; 212 213 return 0; 214} 215 216/* 217 * elf_load_psection(): 218 * 219 * Load a psection at the appropriate address 220 */ 221void 222ELFNAME(load_psection)(vcset, vp, ph, addr, size, prot) 223 struct exec_vmcmd_set *vcset; 224 struct vnode *vp; 225 Elf_Phdr *ph; 226 Elf_Addr *addr; 227 u_long *size; 228 int *prot; 229{ 230 u_long uaddr, msize, psize, rm, rf; 231 long diff, offset; 232 233 /* 234 * If the user specified an address, then we load there. 235 */ 236 if (*addr != ELFDEFNNAME(NO_ADDR)) { 237 if (ph->p_align > 1) { 238 *addr = ELF_ROUND(*addr, ph->p_align); 239 uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align); 240 } else 241 uaddr = ph->p_vaddr; 242 diff = ph->p_vaddr - uaddr; 243 } else { 244 *addr = uaddr = ph->p_vaddr; 245 if (ph->p_align > 1) 246 *addr = ELF_TRUNC(uaddr, ph->p_align); 247 diff = uaddr - *addr; 248 } 249 250 *prot |= (ph->p_flags & Elf_pf_r) ? VM_PROT_READ : 0; 251 *prot |= (ph->p_flags & Elf_pf_w) ? VM_PROT_WRITE : 0; 252 *prot |= (ph->p_flags & Elf_pf_x) ? VM_PROT_EXECUTE : 0; 253 254 offset = ph->p_offset - diff; 255 *size = ph->p_filesz + diff; 256 msize = ph->p_memsz + diff; 257 psize = round_page(*size); 258 259 if ((ph->p_flags & Elf_pf_w) != 0) { 260 /* 261 * Because the pagedvn pager can't handle zero fill of the last 262 * data page if it's not page aligned we map the last page 263 * readvn. 264 */ 265 psize = trunc_page(*size); 266 NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp, 267 offset, *prot); 268 if(psize != *size) 269 NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize, 270 *addr + psize, vp, offset + psize, *prot); 271 } else 272 NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp, 273 offset, *prot); 274 275 /* 276 * Check if we need to extend the size of the segment 277 */ 278 rm = round_page(*addr + msize); 279 rf = round_page(*addr + *size); 280 281 if (rm != rf) { 282 NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 283 0, *prot); 284 *size = msize; 285 } 286} 287 288/* 289 * elf_read_from(): 290 * 291 * Read from vnode into buffer at offset. 292 */ 293int 294ELFNAME(read_from)(p, vp, off, buf, size) 295 struct vnode *vp; 296 u_long off; 297 struct proc *p; 298 caddr_t buf; 299 int size; 300{ 301 int error; 302 int resid; 303 304 if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE, 305 0, p->p_ucred, &resid, p)) != 0) 306 return error; 307 /* 308 * See if we got all of it 309 */ 310 if (resid != 0) 311 return ENOEXEC; 312 return 0; 313} 314 315/* 316 * elf_load_file(): 317 * 318 * Load a file (interpreter/library) pointed to by path 319 * [stolen from coff_load_shlib()]. Made slightly generic 320 * so it might be used externally. 321 */ 322int 323ELFNAME(load_file)(p, epp, path, vcset, entry, ap, last) 324 struct proc *p; 325 struct exec_package *epp; 326 char *path; 327 struct exec_vmcmd_set *vcset; 328 u_long *entry; 329 struct elf_args *ap; 330 Elf_Addr *last; 331{ 332 int error, i; 333 struct nameidata nd; 334 struct vnode *vp; 335 struct vattr attr; 336 Elf_Ehdr eh; 337 Elf_Phdr *ph = NULL; 338 u_long phsize; 339 char *bp = NULL; 340 Elf_Addr addr = *last; 341 342 bp = path; 343 /* 344 * 1. open file 345 * 2. read filehdr 346 * 3. map text, data, and bss out of it using VM_* 347 */ 348 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p); 349 if ((error = namei(&nd)) != 0) 350 return error; 351 vp = nd.ni_vp; 352 353 /* check for regular file */ 354 if (vp->v_type != VREG) { 355 error = EACCES; 356 goto badunlock; 357 } 358 359 /* get attributes */ 360 if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0) 361 goto badunlock; 362 363 /* 364 * Check mount point. Though we're not trying to exec this binary, 365 * we will be executing code from it, so if the mount point 366 * disallows execution or set-id-ness, we punt or kill the set-id. 367 */ 368 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 369 error = EACCES; 370 goto badunlock; 371 } 372 if (vp->v_mount->mnt_flag & MNT_NOSUID) 373 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 374 375 /* 376 * Similarly, if it's not marked as executable, we don't allow 377 * it to be used. For root we have to see if any exec bit on. 378 */ 379 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0) 380 goto badunlock; 381 382#ifdef notyet /* XXX cgd 960926 */ 383 XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?) 384#endif 385 VOP_UNLOCK(vp); 386 387 if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh, 388 sizeof(eh))) != 0) 389 goto bad; 390 391 if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0) 392 goto bad; 393 394 phsize = eh.e_phnum * sizeof(Elf_Phdr); 395 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 396 397 if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff, 398 (caddr_t) ph, phsize)) != 0) 399 goto bad; 400 401 /* 402 * Load all the necessary sections 403 */ 404 for (i = 0; i < eh.e_phnum; i++) { 405 u_long size = 0; 406 int prot = 0; 407 408 switch (ph[i].p_type) { 409 case Elf_pt_load: 410 ELFNAME(load_psection)(vcset, vp, &ph[i], &addr, 411 &size, &prot); 412 /* If entry is within this section it must be text */ 413 if (eh.e_entry >= ph[i].p_vaddr && 414 eh.e_entry < (ph[i].p_vaddr + size)) { 415 /* XXX */ 416 *entry = addr + eh.e_entry; 417#ifdef mips 418 *entry -= ph[i].p_vaddr; 419#endif 420 ap->arg_interp = addr; 421 } 422 addr += size; 423 break; 424 425 case Elf_pt_dynamic: 426 case Elf_pt_phdr: 427 case Elf_pt_note: 428 break; 429 430 default: 431 break; 432 } 433 } 434 435 free((char *)ph, M_TEMP); 436 *last = addr; 437 vrele(vp); 438 return 0; 439 440badunlock: 441 VOP_UNLOCK(vp); 442 443bad: 444 if (ph != NULL) 445 free((char *)ph, M_TEMP); 446#ifdef notyet /* XXX cgd 960926 */ 447 (maybe) VOP_CLOSE it 448#endif 449 vrele(vp); 450 return error; 451} 452 453/* 454 * exec_elf_makecmds(): Prepare an Elf binary's exec package 455 * 456 * First, set of the various offsets/lengths in the exec package. 457 * 458 * Then, mark the text image busy (so it can be demand paged) or error 459 * out if this is not possible. Finally, set up vmcmds for the 460 * text, data, bss, and stack segments. 461 */ 462int 463ELFNAME2(exec,makecmds)(p, epp) 464 struct proc *p; 465 struct exec_package *epp; 466{ 467 Elf_Ehdr *eh = epp->ep_hdr; 468 Elf_Phdr *ph, *pp; 469 Elf_Addr phdr = 0, pos = 0; 470 int error, i, n, nload; 471 char interp[MAXPATHLEN]; 472 u_long phsize; 473 474 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 475 return ENOEXEC; 476 477 if (ELFNAME(check_header)(eh, Elf_et_exec)) 478 return ENOEXEC; 479 480 /* 481 * check if vnode is in open for writing, because we want to 482 * demand-page out of it. if it is, don't do it, for various 483 * reasons 484 */ 485 if (epp->ep_vp->v_writecount != 0) { 486#ifdef DIAGNOSTIC 487 if (epp->ep_vp->v_flag & VTEXT) 488 panic("exec: a VTEXT vnode has writecount != 0\n"); 489#endif 490 return ETXTBSY; 491 } 492 /* 493 * Allocate space to hold all the program headers, and read them 494 * from the file 495 */ 496 phsize = eh->e_phnum * sizeof(Elf_Phdr); 497 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 498 499 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, 500 (caddr_t) ph, phsize)) != 0) 501 goto bad; 502 503 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 504 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 505 506 interp[0] = '\0'; 507 508 for (i = 0; i < eh->e_phnum; i++) { 509 pp = &ph[i]; 510 if (pp->p_type == Elf_pt_interp) { 511 if (pp->p_filesz >= sizeof(interp)) 512 goto bad; 513 if ((error = ELFNAME(read_from)(p, epp->ep_vp, 514 pp->p_offset, (caddr_t) interp, 515 pp->p_filesz)) != 0) 516 goto bad; 517 break; 518 } 519 } 520 521 /* 522 * Setup things for native emulation. 523 */ 524 epp->ep_emul = &ELFNAMEEND(emul_netbsd); 525 pos = ELFDEFNNAME(NO_ADDR); 526 527 /* 528 * On the same architecture, we may be emulating different systems. 529 * See which one will accept this executable. This currently only 530 * applies to Linux and SVR4 on the i386. 531 * 532 * Probe functions would normally see if the interpreter (if any) 533 * exists. Emulation packages may possibly replace the interpreter in 534 * interp[] with a changed path (/emul/xxx/<path>), and also 535 * set the ep_emul field in the exec package structure. 536 */ 537 n = sizeof ELFNAME(probe_funcs) / sizeof ELFNAME(probe_funcs)[0]; 538 if (n != 0) { 539 error = ENOEXEC; 540 for (i = 0; i < n && error; i++) 541 error = ELFNAME(probe_funcs)[i](p, epp, eh, 542 interp, &pos); 543 544#ifdef notyet 545 /* 546 * We should really use a signature in our native binaries 547 * and have our own probe function for matching binaries, 548 * before trying the emulations. For now, if the emulation 549 * probes failed we default to native. 550 */ 551 if (error) 552 goto bad; 553#endif 554 } 555 556 /* 557 * Load all the necessary sections 558 */ 559 for (i = nload = 0; i < eh->e_phnum; i++) { 560 Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 561 u_long size = 0; 562 int prot = 0; 563 564 pp = &ph[i]; 565 566 switch (ph[i].p_type) { 567 case Elf_pt_load: 568 /* 569 * XXX 570 * Can handle only 2 sections: text and data 571 */ 572 if (nload++ == 2) 573 goto bad; 574 ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp, 575 &ph[i], &addr, &size, &prot); 576 577 /* 578 * Decide whether it's text or data by looking 579 * at the entry point. 580 */ 581 if (eh->e_entry >= addr && 582 eh->e_entry < (addr + size)) { 583 epp->ep_taddr = addr; 584 epp->ep_tsize = size; 585 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 586 epp->ep_daddr = addr; 587 epp->ep_dsize = size; 588 } 589 } else { 590 epp->ep_daddr = addr; 591 epp->ep_dsize = size; 592 } 593 break; 594 595 case Elf_pt_shlib: 596 error = ENOEXEC; 597 goto bad; 598 599 case Elf_pt_interp: 600 /* Already did this one */ 601 case Elf_pt_dynamic: 602 case Elf_pt_note: 603 break; 604 605 case Elf_pt_phdr: 606 /* Note address of program headers (in text segment) */ 607 phdr = pp->p_vaddr; 608 break; 609 610 default: 611 /* 612 * Not fatal; we don't need to understand everything. 613 */ 614 break; 615 } 616 } 617 618 /* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */ 619#ifndef ELF_INTERP_NON_RELOCATABLE 620 /* 621 * If no position to load the interpreter was set by a probe 622 * function, pick the same address that a non-fixed mmap(0, ..) 623 * would (i.e. something safely out of the way). 624 */ 625 if (pos == ELFDEFNNAME(NO_ADDR)) 626 pos = round_page(epp->ep_daddr + MAXDSIZ); 627#endif /* !ELF_INTERP_NON_RELOCATABLE */ 628 629 /* 630 * Check if we found a dynamically linked binary and arrange to load 631 * it's interpreter 632 */ 633 if (interp[0]) { 634 struct elf_args *ap; 635 636 ap = (struct elf_args *)malloc(sizeof(struct elf_args), 637 M_TEMP, M_WAITOK); 638 if ((error = ELFNAME(load_file)(p, epp, interp, 639 &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) { 640 free((char *)ap, M_TEMP); 641 goto bad; 642 } 643 pos += phsize; 644 ap->arg_phaddr = phdr; 645 646 ap->arg_phentsize = eh->e_phentsize; 647 ap->arg_phnum = eh->e_phnum; 648 ap->arg_entry = eh->e_entry; 649 650 epp->ep_emul_arg = ap; 651 } else 652 epp->ep_entry = eh->e_entry; 653 654#ifdef ELF_MAP_PAGE_ZERO 655 /* Dell SVR4 maps page zero, yeuch! */ 656 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0, 657 VM_PROT_READ); 658#endif 659 free((char *)ph, M_TEMP); 660 epp->ep_vp->v_flag |= VTEXT; 661 return exec_elf_setup_stack(p, epp); 662 663bad: 664 free((char *)ph, M_TEMP); 665 kill_vmcmds(&epp->ep_vmcmds); 666 return ENOEXEC; 667} 668