9 * All a.out header types. The dummy entry allows canonical
10 * processing of the union as a sequence of longs
21 struct mipsexec; /* bootexec.h */
22 struct mips4kexec; /* bootexec.h */
23 struct sparcexec; /* bootexec.h */
24 struct nextexec; /* bootexec.h */
26 long dummy; /* padding to ensure extra long */
29 static int nextboot(int, Fhdr*, ExecHdr*);
30 static int sparcboot(int, Fhdr*, ExecHdr*);
31 static int mipsboot(int, Fhdr*, ExecHdr*);
32 static int mips4kboot(int, Fhdr*, ExecHdr*);
33 static int common(int, Fhdr*, ExecHdr*);
34 static int commonllp64(int, Fhdr*, ExecHdr*);
35 static int adotout(int, Fhdr*, ExecHdr*);
36 static int elfdotout(int, Fhdr*, ExecHdr*);
37 static int armdotout(int, Fhdr*, ExecHdr*);
38 static void setsym(Fhdr*, long, long, long, vlong);
39 static void setdata(Fhdr*, uvlong, long, vlong, long);
40 static void settext(Fhdr*, uvlong, uvlong, long, vlong);
41 static void hswal(void*, int, ulong(*)(ulong));
42 static uvlong _round(uvlong, ulong);
45 * definition of per-executable file type structures
48 typedef struct Exectable{
49 long magic; /* big-endian magic number of file */
50 char *name; /* executable identifier */
51 char *dlmname; /* dynamically loadable module identifier */
52 uchar type; /* Internal code */
53 uchar _magic; /* _MAGIC() magic */
54 Mach *mach; /* Per-machine data */
55 long hsize; /* header size */
56 ulong (*swal)(ulong); /* beswal or leswal */
57 int (*hparse)(int, Fhdr*, ExecHdr*);
74 { V_MAGIC, /* Mips v.out */
75 "mips plan 9 executable BE",
83 { P_MAGIC, /* Mips 0.out (r3k le) */
84 "mips plan 9 executable LE",
92 { M_MAGIC, /* Mips 4.out */
93 "mips 4k plan 9 executable BE",
94 "mips 4k plan 9 dlm BE",
101 { N_MAGIC, /* Mips 0.out */
102 "mips 4k plan 9 executable LE",
103 "mips 4k plan 9 dlm LE",
110 { 0x160<<16, /* Mips boot image */
111 "mips plan 9 boot image",
116 sizeof(struct mipsexec),
119 { (0x160<<16)|3, /* Mips boot image */
120 "mips 4k plan 9 boot image",
125 sizeof(struct mips4kexec),
128 { K_MAGIC, /* Sparc k.out */
129 "sparc plan 9 executable",
137 { 0x01030107, /* Sparc boot image */
138 "sparc plan 9 boot image",
143 sizeof(struct sparcexec),
146 { U_MAGIC, /* Sparc64 u.out */
147 "sparc64 plan 9 executable",
148 "sparc64 plan 9 dlm",
155 { A_MAGIC, /* 68020 2.out & boot image */
156 "68020 plan 9 executable",
164 { 0xFEEDFACE, /* Next boot image */
165 "next plan 9 boot image",
170 sizeof(struct nextexec),
173 { I_MAGIC, /* I386 8.out & boot image */
174 "386 plan 9 executable",
182 { S_MAGIC, /* amd64 6.out & boot image */
183 "amd64 plan 9 executable",
191 { Q_MAGIC, /* PowerPC q.out & boot image */
192 "power plan 9 executable",
200 { T_MAGIC, /* power64 9.out & boot image */
201 "power64 plan 9 executable",
202 "power64 plan 9 dlm",
209 { ELF_MAG, /* any ELF */
218 { E_MAGIC, /* Arm 5.out and boot image */
219 "arm plan 9 executable",
227 { (143<<16)|0413, /* (Free|Net)BSD Arm */
228 "arm *bsd executable",
239 Mach *mach = &mi386; /* Global current machine table */
242 couldbe4k(ExecTable *mp)
247 if((d=dirstat("/proc/1/regs")) == nil)
249 if(d->length < 32*8){ /* R3000 */
254 for (f = exectab; f->magic; f++)
255 if(f->magic == M_MAGIC) {
256 f->name = "mips plan 9 executable on mips2 kernel";
263 crackhdr(int fd, Fhdr *fp)
271 nb = read(fd, (char *)&d.e, sizeof(d.e));
276 magic = beswal(d.e.magic); /* big-endian */
277 for (mp = exectab; mp->magic; mp++) {
282 * The magic number has morphed into something
283 * with fields (the straw was DYN_MAGIC) so now
284 * a flag is needed in Fhdr to distinguish _MAGIC()
285 * magic numbers from foreign magic numbers.
287 * This code is creaking a bit and if it has to
288 * be modified/extended much more it's probably
289 * time to step back and redo it all.
292 if(mp->magic != (magic & ~DYN_MAGIC))
295 if(mp->magic == V_MAGIC)
298 if ((magic & DYN_MAGIC) && mp->dlmname != nil)
299 fp->name = mp->dlmname;
304 if(mp->magic != magic)
309 fp->hdrsz = mp->hsize; /* will be zero on bootables */
310 fp->_magic = mp->_magic;
315 hswal(&d, sizeof(d.e)/sizeof(ulong), mp->swal);
316 ret = mp->hparse(fd, fp, &d);
317 seek(fd, mp->hsize, 0); /* seek to end of header */
321 werrstr("unknown header type");
326 * Convert header to canonical form
329 hswal(void *v, int n, ulong (*swap)(ulong))
333 for(ulp = v; n--; ulp++)
334 *ulp = (*swap)(*ulp);
338 * Crack a normal a.out-type header
341 adotout(int fd, Fhdr *fp, ExecHdr *hp)
346 pgsize = mach->pgsize;
347 settext(fp, hp->e.entry, pgsize+sizeof(Exec),
348 hp->e.text, sizeof(Exec));
349 setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize),
350 hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
351 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
358 if (!(fp->entry & mach->ktmask))
361 switch(fp->type) { /* boot image */
364 fp->name = "68020 plan 9 boot image";
368 fp->txtaddr = (u32int)fp->entry;
369 fp->name = "386 plan 9 boot image";
370 fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
374 fp->txtaddr = (u32int)fp->entry;
375 fp->name = "ARM plan 9 boot image";
376 fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
380 fp->txtaddr = (u32int)fp->entry;
381 fp->name = "power plan 9 boot image";
382 fp->dataddr = fp->txtaddr+fp->txtsz;
386 fp->txtaddr = fp->entry;
387 fp->name = "amd64 plan 9 boot image";
388 fp->dataddr = _round(fp->txtaddr+fp->txtsz, 4096);
391 fp->type = FPOWER64B;
392 fp->txtaddr = fp->entry;
393 fp->name = "power64 plan 9 boot image";
394 fp->dataddr = fp->txtaddr+fp->txtsz;
399 fp->hdrsz = 0; /* header stripped */
403 * _MAGIC() style headers and
404 * alpha plan9-style bootable images for axp "headerless" boot
408 common(int fd, Fhdr *fp, ExecHdr *hp)
411 if(hp->e.magic & DYN_MAGIC) {
413 fp->dataddr = fp->txtsz;
421 commonllp64(int, Fhdr *fp, ExecHdr *hp)
426 hswal(&hp->e, sizeof(Exec)/sizeof(long), beswal);
427 if(!(hp->e.magic & HDR_MAGIC))
431 * There can be more magic here if the
432 * header ever needs more expansion.
433 * For now just catch use of any of the
436 if((hp->e.magic & ~DYN_MAGIC)>>16)
438 entry = beswav(hp->e.hdr[0]);
440 pgsize = mach->pgsize;
441 settext(fp, entry, pgsize+fp->hdrsz, hp->e.text, fp->hdrsz);
442 setdata(fp, _round(pgsize+fp->txtsz+fp->hdrsz, pgsize),
443 hp->e.data, fp->txtsz+fp->hdrsz, hp->e.bss);
444 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
446 if(hp->e.magic & DYN_MAGIC) {
448 fp->dataddr = fp->txtsz;
456 * mips bootable image.
459 mipsboot(int fd, Fhdr *fp, ExecHdr *hp)
463 switch(hp->e.amagic) {
465 case 0407: /* some kind of mips */
466 settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
467 hp->e.tsize, sizeof(struct mipsexec)+4);
468 setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
469 fp->txtoff+hp->e.tsize, hp->e.bsize);
471 case 0413: /* some kind of mips */
472 settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
474 setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
475 hp->e.tsize, hp->e.bsize);
478 setsym(fp, hp->e.nsyms, 0, hp->e.pcsize, hp->e.symptr);
479 fp->hdrsz = 0; /* header stripped */
484 * mips4k bootable image.
487 mips4kboot(int fd, Fhdr *fp, ExecHdr *hp)
491 switch(hp->e.h.amagic) {
493 case 0407: /* some kind of mips */
494 settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
495 hp->e.h.tsize, sizeof(struct mips4kexec));
496 setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
497 fp->txtoff+hp->e.h.tsize, hp->e.h.bsize);
499 case 0413: /* some kind of mips */
500 settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
502 setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
503 hp->e.h.tsize, hp->e.h.bsize);
506 setsym(fp, hp->e.h.nsyms, 0, hp->e.h.pcsize, hp->e.h.symptr);
507 fp->hdrsz = 0; /* header stripped */
512 * sparc bootable image
515 sparcboot(int fd, Fhdr *fp, ExecHdr *hp)
519 settext(fp, hp->e.sentry, hp->e.sentry, hp->e.stext,
520 sizeof(struct sparcexec));
521 setdata(fp, hp->e.sentry+hp->e.stext, hp->e.sdata,
522 fp->txtoff+hp->e.stext, hp->e.sbss);
523 setsym(fp, hp->e.ssyms, 0, hp->e.sdrsize, fp->datoff+hp->e.sdata);
524 fp->hdrsz = 0; /* header stripped */
529 * next bootable image
532 nextboot(int fd, Fhdr *fp, ExecHdr *hp)
536 settext(fp, hp->e.textc.vmaddr, hp->e.textc.vmaddr,
537 hp->e.texts.size, hp->e.texts.offset);
538 setdata(fp, hp->e.datac.vmaddr, hp->e.datas.size,
539 hp->e.datas.offset, hp->e.bsss.size);
540 setsym(fp, hp->e.symc.nsyms, hp->e.symc.spoff, hp->e.symc.pcoff,
542 fp->hdrsz = 0; /* header stripped */
550 elf64dotout(int fd, Fhdr *fp, ExecHdr *hp)
554 ushort (*swab)(ushort);
555 ulong (*swal)(ulong);
556 uvlong (*swav)(uvlong);
557 int i, it, id, is, phsz;
561 if(ep->ident[DATA] == ELFDATA2LSB) {
565 } else if(ep->ident[DATA] == ELFDATA2MSB) {
570 werrstr("bad ELF64 encoding - not big or little endian");
574 ep->type = swab(ep->type);
575 ep->machine = swab(ep->machine);
576 ep->version = swal(ep->version);
577 if(ep->type != EXEC || ep->version != CURRENT)
579 ep->elfentry = swav(ep->elfentry);
580 ep->phoff = swav(ep->phoff);
581 ep->shoff = swav(ep->shoff);
582 ep->flags = swal(ep->flags);
583 ep->ehsize = swab(ep->ehsize);
584 ep->phentsize = swab(ep->phentsize);
585 ep->phnum = swab(ep->phnum);
586 ep->shentsize = swab(ep->shentsize);
587 ep->shnum = swab(ep->shnum);
588 ep->shstrndx = swab(ep->shstrndx);
591 fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
592 switch(ep->machine) {
598 fp->name = "amd64 ELF64 executable";
603 fp->name = "power64 ELF64 executable";
607 if(ep->phentsize != sizeof(P64hdr)) {
608 werrstr("bad ELF64 header size");
611 phsz = sizeof(P64hdr)*ep->phnum;
615 seek(fd, ep->phoff, 0);
616 if(read(fd, ph, phsz) < 0) {
620 for(i = 0; i < ep->phnum; i++) {
621 ph[i].type = swal(ph[i].type);
622 ph[i].flags = swal(ph[i].flags);
623 ph[i].offset = swav(ph[i].offset);
624 ph[i].vaddr = swav(ph[i].vaddr);
625 ph[i].paddr = swav(ph[i].paddr);
626 ph[i].filesz = swav(ph[i].filesz);
627 ph[i].memsz = swav(ph[i].memsz);
628 ph[i].align = swav(ph[i].align);
631 /* find text, data and symbols and install them */
633 for(i = 0; i < ep->phnum; i++) {
634 if(ph[i].type == LOAD
635 && (ph[i].flags & (R|X)) == (R|X) && it == -1)
637 else if(ph[i].type == LOAD
638 && (ph[i].flags & (R|W)) == (R|W) && id == -1)
640 else if(ph[i].type == NOPTYPE && is == -1)
643 if(it == -1 || id == -1) {
644 werrstr("No ELF64 TEXT or DATA sections");
649 settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
650 /* 8c: out of fixed registers */
651 uvl = ph[id].memsz - ph[id].filesz;
652 setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, uvl);
654 setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);
663 elf32dotout(int fd, Fhdr *fp, ExecHdr *hp)
665 ulong (*swal)(ulong);
666 ushort (*swab)(ushort);
669 int i, it, id, is, phsz;
671 /* bitswap the header according to the DATA format */
673 if(ep->ident[DATA] == ELFDATA2LSB) {
676 } else if(ep->ident[DATA] == ELFDATA2MSB) {
680 werrstr("bad ELF32 encoding - not big or little endian");
684 ep->type = swab(ep->type);
685 ep->machine = swab(ep->machine);
686 ep->version = swal(ep->version);
687 ep->elfentry = swal(ep->elfentry);
688 ep->phoff = swal(ep->phoff);
689 ep->shoff = swal(ep->shoff);
690 ep->flags = swal(ep->flags);
691 ep->ehsize = swab(ep->ehsize);
692 ep->phentsize = swab(ep->phentsize);
693 ep->phnum = swab(ep->phnum);
694 ep->shentsize = swab(ep->shentsize);
695 ep->shnum = swab(ep->shnum);
696 ep->shstrndx = swab(ep->shstrndx);
697 if(ep->type != EXEC || ep->version != CURRENT)
700 /* we could definitely support a lot more machines here */
702 fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
703 switch(ep->machine) {
707 fp->name = "386 ELF32 executable";
711 if(ep->ident[DATA] == ELFDATA2LSB){
713 fp->name = "mips le ELF32 executable";
716 fp->name = "mips be ELF32 executable";
722 fp->name = "sparc64 ELF32 executable";
727 fp->name = "power ELF32 executable";
732 fp->name = "power64 ELF32 executable";
737 fp->name = "amd64 ELF32 executable";
742 fp->name = "arm ELF32 executable";
748 if(ep->phentsize != sizeof(Phdr)) {
749 werrstr("bad ELF32 header size");
752 phsz = sizeof(Phdr)*ep->phnum;
756 seek(fd, ep->phoff, 0);
757 if(read(fd, ph, phsz) < 0) {
761 hswal(ph, phsz/sizeof(ulong), swal);
763 /* find text, data and symbols and install them */
765 for(i = 0; i < ep->phnum; i++) {
766 if(ph[i].type == LOAD
767 && (ph[i].flags & (R|X)) == (R|X) && it == -1)
769 else if(ph[i].type == LOAD
770 && (ph[i].flags & (R|W)) == (R|W) && id == -1)
772 else if(ph[i].type == NOPTYPE && is == -1)
775 if(it == -1 || id == -1) {
777 * The SPARC64 boot image is something of an ELF hack.
778 * Text+Data+BSS are represented by ph[0]. Symbols
779 * are represented by ph[1]:
781 * filesz, memsz, vaddr, paddr, off
782 * ph[0] : txtsz+datsz, txtsz+datsz+bsssz, txtaddr-KZERO, datasize, txtoff
783 * ph[1] : symsz, lcsz, 0, 0, symoff
785 if(ep->machine == SPARC64 && ep->phnum == 2) {
786 ulong txtaddr, txtsz, dataddr, bsssz;
788 txtaddr = ph[0].vaddr | 0x80000000;
789 txtsz = ph[0].filesz - ph[0].paddr;
790 dataddr = txtaddr + txtsz;
791 bsssz = ph[0].memsz - ph[0].filesz;
792 settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset);
793 setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz);
794 setsym(fp, ph[1].filesz, 0, ph[1].memsz, ph[1].offset);
799 werrstr("No ELF32 TEXT or DATA sections");
804 settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
805 setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz);
807 setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);
816 elfdotout(int fd, Fhdr *fp, ExecHdr *hp)
820 /* bitswap the header according to the DATA format */
822 if(ep->ident[CLASS] == ELFCLASS32)
823 return elf32dotout(fd, fp, hp);
824 else if(ep->ident[CLASS] == ELFCLASS64)
825 return elf64dotout(fd, fp, hp);
827 werrstr("bad ELF class - not 32 bit");
832 * (Free|Net)BSD ARM header.
835 armdotout(int fd, Fhdr *fp, ExecHdr *hp)
840 settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
841 setdata(fp, fp->txtsz, hp->e.data, fp->txtsz, hp->e.bss);
842 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
845 if ((fp->entry & kbase) == kbase) { /* Boot image */
846 fp->txtaddr = kbase+sizeof(Exec);
847 fp->name = "ARM *BSD boot image";
848 fp->hdrsz = 0; /* header stripped */
849 fp->dataddr = kbase+fp->txtsz;
855 settext(Fhdr *fp, uvlong e, uvlong a, long s, vlong off)
864 setdata(Fhdr *fp, uvlong a, long s, vlong off, long bss)
873 setsym(Fhdr *fp, long symsz, long sppcsz, long lnpcsz, vlong symoff)
878 fp->sppcoff = fp->symoff+fp->symsz;
880 fp->lnpcoff = fp->sppcoff+fp->sppcsz;
885 _round(uvlong a, ulong b)