1 #include <u.h> 2 #include <libc.h> 3 #include <bio.h> 4 #include <bootexec.h> 5 #include <mach.h> 6 #include "elf.h" 7 8 /* 9 * All a.out header types. The dummy entry allows canonical 10 * processing of the union as a sequence of longs 11 */ 12 13 typedef struct { 14 union{ 15 Exec; /* in a.out.h */ 16 Ehdr; /* in elf.h */ 17 struct mipsexec; 18 struct mips4kexec; 19 struct sparcexec; 20 struct nextexec; 21 } e; 22 long dummy; /* padding to ensure extra long */ 23 } ExecHdr; 24 25 static int nextboot(int, Fhdr*, ExecHdr*); 26 static int sparcboot(int, Fhdr*, ExecHdr*); 27 static int mipsboot(int, Fhdr*, ExecHdr*); 28 static int mips4kboot(int, Fhdr*, ExecHdr*); 29 static int common(int, Fhdr*, ExecHdr*); 30 static int adotout(int, Fhdr*, ExecHdr*); 31 static int elfdotout(int, Fhdr*, ExecHdr*); 32 static int armdotout(int, Fhdr*, ExecHdr*); 33 static int alphadotout(int, Fhdr*, ExecHdr*); 34 static void setsym(Fhdr*, long, long, long, long); 35 static void setdata(Fhdr*, long, long, long, long); 36 static void settext(Fhdr*, long, long, long, long); 37 static void hswal(long*, int, long(*)(long)); 38 static long noswal(long); 39 static long _round(long, long); 40 41 /* 42 * definition of per-executable file type structures 43 */ 44 45 typedef struct Exectable{ 46 long magic; /* big-endian magic number of file */ 47 char *name; /* executable identifier */ 48 char *dlmname; /* dynamically loadable module identifier */ 49 int type; /* Internal code */ 50 Mach *mach; /* Per-machine data */ 51 ulong hsize; /* header size */ 52 long (*swal)(long); /* beswal or leswal */ 53 int (*hparse)(int, Fhdr*, ExecHdr*); 54 } ExecTable; 55 56 extern Mach mmips; 57 extern Mach mmips2le; 58 extern Mach mmips2be; 59 extern Mach msparc; 60 extern Mach msparc64; 61 extern Mach m68020; 62 extern Mach mi386; 63 extern Mach mamd64; 64 extern Mach marm; 65 extern Mach mpower; 66 extern Mach malpha; 67 68 ExecTable exectab[] = 69 { 70 { V_MAGIC, /* Mips v.out */ 71 "mips plan 9 executable", 72 "mips plan 9 dlm", 73 FMIPS, 74 &mmips, 75 sizeof(Exec), 76 beswal, 77 adotout }, 78 { M_MAGIC, /* Mips 4.out */ 79 "mips 4k plan 9 executable BE", 80 "mips 4k plan 9 dlm BE", 81 FMIPS2BE, 82 &mmips2be, 83 sizeof(Exec), 84 beswal, 85 adotout }, 86 { N_MAGIC, /* Mips 0.out */ 87 "mips 4k plan 9 executable LE", 88 "mips 4k plan 9 dlm LE", 89 FMIPS2LE, 90 &mmips2le, 91 sizeof(Exec), 92 beswal, 93 adotout }, 94 { 0x160<<16, /* Mips boot image */ 95 "mips plan 9 boot image", 96 nil, 97 FMIPSB, 98 &mmips, 99 sizeof(struct mipsexec), 100 beswal, 101 mipsboot }, 102 { (0x160<<16)|3, /* Mips boot image */ 103 "mips 4k plan 9 boot image", 104 nil, 105 FMIPSB, 106 &mmips2be, 107 sizeof(struct mips4kexec), 108 beswal, 109 mips4kboot }, 110 { K_MAGIC, /* Sparc k.out */ 111 "sparc plan 9 executable", 112 "sparc plan 9 dlm", 113 FSPARC, 114 &msparc, 115 sizeof(Exec), 116 beswal, 117 adotout }, 118 { 0x01030107, /* Sparc boot image */ 119 "sparc plan 9 boot image", 120 nil, 121 FSPARCB, 122 &msparc, 123 sizeof(struct sparcexec), 124 beswal, 125 sparcboot }, 126 { U_MAGIC, /* Sparc64 u.out */ 127 "sparc64 plan 9 executable", 128 "sparc64 plan 9 dlm", 129 FSPARC64, 130 &msparc64, 131 sizeof(Exec), 132 beswal, 133 adotout }, 134 { A_MAGIC, /* 68020 2.out & boot image */ 135 "68020 plan 9 executable", 136 "68020 plan 9 dlm", 137 F68020, 138 &m68020, 139 sizeof(Exec), 140 beswal, 141 common }, 142 { 0xFEEDFACE, /* Next boot image */ 143 "next plan 9 boot image", 144 nil, 145 FNEXTB, 146 &m68020, 147 sizeof(struct nextexec), 148 beswal, 149 nextboot }, 150 { I_MAGIC, /* I386 8.out & boot image */ 151 "386 plan 9 executable", 152 "386 plan 9 dlm", 153 FI386, 154 &mi386, 155 sizeof(Exec), 156 beswal, 157 common }, 158 { S_MAGIC, /* amd64 6.out & boot image */ 159 "amd64 plan 9 executable", 160 "amd64 plan 9 dlm", 161 FAMD64, 162 &mamd64, 163 sizeof(Exec), 164 beswal, 165 common }, 166 { Q_MAGIC, /* PowerPC q.out & boot image */ 167 "power plan 9 executable", 168 "power plan 9 dlm", 169 FPOWER, 170 &mpower, 171 sizeof(Exec), 172 beswal, 173 common }, 174 { ELF_MAG, /* any elf32 */ 175 "elf executable", 176 nil, 177 FNONE, 178 &mi386, 179 sizeof(Ehdr), 180 noswal, 181 elfdotout }, 182 { E_MAGIC, /* Arm 5.out */ 183 "arm plan 9 executable", 184 "arm plan 9 dlm", 185 FARM, 186 &marm, 187 sizeof(Exec), 188 beswal, 189 common }, 190 { (143<<16)|0413, /* (Free|Net)BSD Arm */ 191 "arm *bsd executable", 192 nil, 193 FARM, 194 &marm, 195 sizeof(Exec), 196 leswal, 197 armdotout }, 198 { L_MAGIC, /* alpha 7.out */ 199 "alpha plan 9 executable", 200 "alpha plan 9 dlm", 201 FALPHA, 202 &malpha, 203 sizeof(Exec), 204 beswal, 205 common }, 206 { 0x0700e0c3, /* alpha boot image */ 207 "alpha plan 9 boot image", 208 nil, 209 FALPHAB, 210 &malpha, 211 sizeof(Exec), 212 beswal, 213 alphadotout }, 214 { 0 }, 215 }; 216 217 Mach *mach = &mi386; /* Global current machine table */ 218 219 static ExecTable* 220 couldbe4k(ExecTable *mp) 221 { 222 Dir *d; 223 ExecTable *f; 224 225 if((d=dirstat("/proc/1/regs")) == nil) 226 return mp; 227 if(d->length < 32*8){ /* R3000 */ 228 free(d); 229 return mp; 230 } 231 free(d); 232 for (f = exectab; f->magic; f++) 233 if(f->magic == M_MAGIC) { 234 f->name = "mips plan 9 executable on mips2 kernel"; 235 return f; 236 } 237 return mp; 238 } 239 240 int 241 crackhdr(int fd, Fhdr *fp) 242 { 243 ExecTable *mp; 244 ExecHdr d; 245 int nb, magic, ret; 246 247 fp->type = FNONE; 248 nb = read(fd, (char *)&d.e, sizeof(d.e)); 249 if (nb <= 0) 250 return 0; 251 252 ret = 0; 253 fp->magic = magic = beswal(d.e.magic); /* big-endian */ 254 for (mp = exectab; mp->magic; mp++) { 255 if (nb < mp->hsize) 256 continue; 257 if (mp->magic == (magic & ~DYN_MAGIC)) { 258 if(mp->magic == V_MAGIC) 259 mp = couldbe4k(mp); 260 261 hswal((long *) &d, sizeof(d.e)/sizeof(long), mp->swal); 262 fp->type = mp->type; 263 if ((magic & DYN_MAGIC) && mp->dlmname != nil) 264 fp->name = mp->dlmname; 265 else 266 fp->name = mp->name; 267 fp->hdrsz = mp->hsize; /* zero on bootables */ 268 mach = mp->mach; 269 ret = mp->hparse(fd, fp, &d); 270 seek(fd, mp->hsize, 0); /* seek to end of header */ 271 break; 272 } 273 } 274 if(mp->magic == 0) 275 werrstr("unknown header type"); 276 return ret; 277 } 278 /* 279 * Convert header to canonical form 280 */ 281 static void 282 hswal(long *lp, int n, long (*swap) (long)) 283 { 284 while (n--) { 285 *lp = (*swap) (*lp); 286 lp++; 287 } 288 } 289 /* 290 * noop 291 */ 292 static long 293 noswal(long x) 294 { 295 return x; 296 } 297 /* 298 * Crack a normal a.out-type header 299 */ 300 static int 301 adotout(int fd, Fhdr *fp, ExecHdr *hp) 302 { 303 long pgsize; 304 305 USED(fd); 306 pgsize = mach->pgsize; 307 settext(fp, hp->e.entry, pgsize+sizeof(Exec), 308 hp->e.text, sizeof(Exec)); 309 setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize), 310 hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss); 311 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz); 312 return 1; 313 } 314 315 /* 316 * 68020 2.out and 68020 bootable images 317 * 386I 8.out and 386I bootable images 318 * alpha plan9-style bootable images for axp "headerless" boot 319 * 320 */ 321 static int 322 common(int fd, Fhdr *fp, ExecHdr *hp) 323 { 324 long kbase; 325 326 adotout(fd, fp, hp); 327 if(hp->e.magic & DYN_MAGIC) { 328 fp->txtaddr = 0; 329 fp->dataddr = fp->txtsz; 330 return 1; 331 } 332 kbase = mach->kbase; 333 if ((fp->entry & kbase) == kbase) { /* Boot image */ 334 switch(fp->type) { 335 case F68020: 336 fp->type = F68020B; 337 fp->name = "68020 plan 9 boot image"; 338 fp->hdrsz = 0; /* header stripped */ 339 break; 340 case FI386: 341 fp->type = FI386B; 342 fp->txtaddr = sizeof(Exec); 343 fp->name = "386 plan 9 boot image"; 344 fp->hdrsz = 0; /* header stripped */ 345 fp->dataddr = fp->txtaddr+fp->txtsz; 346 break; 347 case FARM: 348 fp->txtaddr = kbase+0x8010; 349 fp->name = "ARM plan 9 boot image"; 350 fp->hdrsz = 0; /* header stripped */ 351 fp->dataddr = fp->txtaddr+fp->txtsz; 352 return 1; 353 case FALPHA: 354 fp->type = FALPHAB; 355 fp->txtaddr = fp->entry; 356 fp->name = "alpha plan 9 boot image?"; 357 fp->hdrsz = 0; /* header stripped */ 358 fp->dataddr = fp->txtaddr+fp->txtsz; 359 break; 360 case FPOWER: 361 fp->type = FPOWERB; 362 fp->txtaddr = fp->entry; 363 fp->name = "power plan 9 boot image"; 364 fp->hdrsz = 0; /* header stripped */ 365 fp->dataddr = fp->txtaddr+fp->txtsz; 366 break; 367 default: 368 break; 369 } 370 fp->txtaddr |= kbase; 371 fp->entry |= kbase; 372 fp->dataddr |= kbase; 373 } 374 return 1; 375 } 376 377 /* 378 * mips bootable image. 379 */ 380 static int 381 mipsboot(int fd, Fhdr *fp, ExecHdr *hp) 382 { 383 USED(fd); 384 switch(hp->e.amagic) { 385 default: 386 case 0407: /* some kind of mips */ 387 fp->type = FMIPSB; 388 settext(fp, hp->e.mentry, hp->e.text_start, hp->e.tsize, 389 sizeof(struct mipsexec)+4); 390 setdata(fp, hp->e.data_start, hp->e.dsize, 391 fp->txtoff+hp->e.tsize, hp->e.bsize); 392 break; 393 case 0413: /* some kind of mips */ 394 fp->type = FMIPSB; 395 settext(fp, hp->e.mentry, hp->e.text_start, hp->e.tsize, 0); 396 setdata(fp, hp->e.data_start, hp->e.dsize, hp->e.tsize, 397 hp->e.bsize); 398 break; 399 } 400 setsym(fp, hp->e.nsyms, 0, hp->e.pcsize, hp->e.symptr); 401 fp->hdrsz = 0; /* header stripped */ 402 return 1; 403 } 404 405 /* 406 * mips4k bootable image. 407 */ 408 static int 409 mips4kboot(int fd, Fhdr *fp, ExecHdr *hp) 410 { 411 USED(fd); 412 switch(hp->e.h.amagic) { 413 default: 414 case 0407: /* some kind of mips */ 415 fp->type = FMIPSB; 416 settext(fp, hp->e.h.mentry, hp->e.h.text_start, hp->e.h.tsize, 417 sizeof(struct mips4kexec)); 418 setdata(fp, hp->e.h.data_start, hp->e.h.dsize, 419 fp->txtoff+hp->e.h.tsize, hp->e.h.bsize); 420 break; 421 case 0413: /* some kind of mips */ 422 fp->type = FMIPSB; 423 settext(fp, hp->e.h.mentry, hp->e.h.text_start, hp->e.h.tsize, 0); 424 setdata(fp, hp->e.h.data_start, hp->e.h.dsize, hp->e.h.tsize, 425 hp->e.h.bsize); 426 break; 427 } 428 setsym(fp, hp->e.h.nsyms, 0, hp->e.h.pcsize, hp->e.h.symptr); 429 fp->hdrsz = 0; /* header stripped */ 430 return 1; 431 } 432 433 /* 434 * sparc bootable image 435 */ 436 static int 437 sparcboot(int fd, Fhdr *fp, ExecHdr *hp) 438 { 439 USED(fd); 440 fp->type = FSPARCB; 441 settext(fp, hp->e.sentry, hp->e.sentry, hp->e.stext, 442 sizeof(struct sparcexec)); 443 setdata(fp, hp->e.sentry+hp->e.stext, hp->e.sdata, 444 fp->txtoff+hp->e.stext, hp->e.sbss); 445 setsym(fp, hp->e.ssyms, 0, hp->e.sdrsize, fp->datoff+hp->e.sdata); 446 fp->hdrsz = 0; /* header stripped */ 447 return 1; 448 } 449 450 /* 451 * next bootable image 452 */ 453 static int 454 nextboot(int fd, Fhdr *fp, ExecHdr *hp) 455 { 456 USED(fd); 457 fp->type = FNEXTB; 458 settext(fp, hp->e.textc.vmaddr, hp->e.textc.vmaddr, 459 hp->e.texts.size, hp->e.texts.offset); 460 setdata(fp, hp->e.datac.vmaddr, hp->e.datas.size, 461 hp->e.datas.offset, hp->e.bsss.size); 462 setsym(fp, hp->e.symc.nsyms, hp->e.symc.spoff, hp->e.symc.pcoff, 463 hp->e.symc.symoff); 464 fp->hdrsz = 0; /* header stripped */ 465 return 1; 466 } 467 468 469 /* 470 * Elf32 binaries. 471 */ 472 static int 473 elfdotout(int fd, Fhdr *fp, ExecHdr *hp) 474 { 475 476 long (*swal)(long); 477 ushort (*swab)(ushort); 478 Ehdr *ep; 479 Phdr *ph; 480 int i, it, id, is, phsz; 481 482 /* bitswap the header according to the DATA format */ 483 ep = &hp->e; 484 if(ep->ident[CLASS] != ELFCLASS32) { 485 werrstr("bad ELF class - not 32 bit"); 486 return 0; 487 } 488 if(ep->ident[DATA] == ELFDATA2LSB) { 489 swab = leswab; 490 swal = leswal; 491 } else if(ep->ident[DATA] == ELFDATA2MSB) { 492 swab = beswab; 493 swal = beswal; 494 } else { 495 werrstr("bad ELF encoding - not big or little endian"); 496 return 0; 497 } 498 499 ep->type = swab(ep->type); 500 ep->machine = swab(ep->machine); 501 ep->version = swal(ep->version); 502 ep->elfentry = swal(ep->elfentry); 503 ep->phoff = swal(ep->phoff); 504 ep->shoff = swal(ep->shoff); 505 ep->flags = swal(ep->flags); 506 ep->ehsize = swab(ep->ehsize); 507 ep->phentsize = swab(ep->phentsize); 508 ep->phnum = swab(ep->phnum); 509 ep->shentsize = swab(ep->shentsize); 510 ep->shnum = swab(ep->shnum); 511 ep->shstrndx = swab(ep->shstrndx); 512 if(ep->type != EXEC || ep->version != CURRENT) 513 return 0; 514 515 /* we could definitely support a lot more machines here */ 516 fp->magic = ELF_MAG; 517 fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15; 518 switch(ep->machine) { 519 case I386: 520 mach = &mi386; 521 fp->type = FI386; 522 break; 523 case MIPS: 524 mach = &mmips; 525 fp->type = FMIPS; 526 break; 527 case SPARC64: 528 mach = &msparc64; 529 fp->type = FSPARC64; 530 break; 531 case POWER: 532 mach = &mpower; 533 fp->type = FPOWER; 534 break; 535 case AMD64: 536 mach = &mamd64; 537 fp->type = FAMD64; 538 break; 539 default: 540 return 0; 541 } 542 543 if(ep->phentsize != sizeof(Phdr)) { 544 werrstr("bad ELF header size"); 545 return 0; 546 } 547 phsz = sizeof(Phdr)*ep->phnum; 548 ph = malloc(phsz); 549 if(!ph) 550 return 0; 551 seek(fd, ep->phoff, 0); 552 if(read(fd, ph, phsz) < 0) { 553 free(ph); 554 return 0; 555 } 556 hswal((long*)ph, phsz/sizeof(long), swal); 557 558 /* find text, data and symbols and install them */ 559 it = id = is = -1; 560 for(i = 0; i < ep->phnum; i++) { 561 if(ph[i].type == LOAD 562 && (ph[i].flags & (R|X)) == (R|X) && it == -1) 563 it = i; 564 else if(ph[i].type == LOAD 565 && (ph[i].flags & (R|W)) == (R|W) && id == -1) 566 id = i; 567 else if(ph[i].type == NOPTYPE && is == -1) 568 is = i; 569 } 570 if(it == -1 || id == -1) { 571 /* 572 * The SPARC64 boot image is something of an ELF hack. 573 * Text+Data+BSS are represented by ph[0]. Symbols 574 * are represented by ph[1]: 575 * 576 * filesz, memsz, vaddr, paddr, off 577 * ph[0] : txtsz+datsz, txtsz+datsz+bsssz, txtaddr-KZERO, datasize, txtoff 578 * ph[1] : symsz, lcsz, 0, 0, symoff 579 */ 580 if(ep->machine == SPARC64 && ep->phnum == 2) { 581 ulong txtaddr, txtsz, dataddr, bsssz; 582 583 txtaddr = ph[0].vaddr | 0x80000000; 584 txtsz = ph[0].filesz - ph[0].paddr; 585 dataddr = txtaddr + txtsz; 586 bsssz = ph[0].memsz - ph[0].filesz; 587 settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset); 588 setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz); 589 setsym(fp, ph[1].filesz, 0, ph[1].memsz, ph[1].offset); 590 free(ph); 591 return 1; 592 } 593 594 werrstr("No TEXT or DATA sections"); 595 free(ph); 596 return 0; 597 } 598 599 settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset); 600 setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz); 601 if(is != -1) 602 setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset); 603 free(ph); 604 return 1; 605 } 606 607 /* 608 * alpha bootable 609 */ 610 static int 611 alphadotout(int fd, Fhdr *fp, ExecHdr *hp) 612 { 613 long kbase; 614 615 USED(fd); 616 settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec)); 617 setdata(fp, fp->txtsz+sizeof(Exec), hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss); 618 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz); 619 620 /* 621 * Boot images have some of bits <31:28> set: 622 * 0x80400000 kernel 623 * 0x20000000 secondary bootstrap 624 */ 625 kbase = 0xF0000000; 626 if (fp->entry & kbase) { 627 fp->txtaddr = fp->entry; 628 fp->name = "alpha plan 9 boot image"; 629 fp->hdrsz = 0; /* header stripped */ 630 fp->dataddr = fp->entry+fp->txtsz; 631 } 632 return 1; 633 } 634 635 /* 636 * (Free|Net)BSD ARM header. 637 */ 638 static int 639 armdotout(int fd, Fhdr *fp, ExecHdr *hp) 640 { 641 long kbase; 642 643 USED(fd); 644 settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec)); 645 setdata(fp, fp->txtsz, hp->e.data, fp->txtsz, hp->e.bss); 646 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz); 647 648 kbase = 0xF0000000; 649 if ((fp->entry & kbase) == kbase) { /* Boot image */ 650 fp->txtaddr = kbase+sizeof(Exec); 651 fp->name = "ARM *BSD boot image"; 652 fp->hdrsz = 0; /* header stripped */ 653 fp->dataddr = kbase+fp->txtsz; 654 } 655 return 1; 656 } 657 658 static void 659 settext(Fhdr *fp, long e, long a, long s, long off) 660 { 661 fp->txtaddr = a; 662 fp->entry = e; 663 fp->txtsz = s; 664 fp->txtoff = off; 665 } 666 static void 667 setdata(Fhdr *fp, long a, long s, long off, long bss) 668 { 669 fp->dataddr = a; 670 fp->datsz = s; 671 fp->datoff = off; 672 fp->bsssz = bss; 673 } 674 static void 675 setsym(Fhdr *fp, long sy, long sppc, long lnpc, long symoff) 676 { 677 fp->symsz = sy; 678 fp->symoff = symoff; 679 fp->sppcsz = sppc; 680 fp->sppcoff = fp->symoff+fp->symsz; 681 fp->lnpcsz = lnpc; 682 fp->lnpcoff = fp->sppcoff+fp->sppcsz; 683 } 684 685 686 static long 687 _round(long a, long b) 688 { 689 long w; 690 691 w = (a/b)*b; 692 if (a!=w) 693 w += b; 694 return(w); 695 } 696