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*);
75 { V_MAGIC, /* Mips v.out */
76 "mips plan 9 executable BE",
84 { P_MAGIC, /* Mips 0.out (r3k le) */
85 "mips plan 9 executable LE",
93 { M_MAGIC, /* Mips 4.out */
94 "mips 4k plan 9 executable BE",
95 "mips 4k plan 9 dlm BE",
102 { N_MAGIC, /* Mips 0.out */
103 "mips 4k plan 9 executable LE",
104 "mips 4k plan 9 dlm LE",
111 { 0x160<<16, /* Mips boot image */
112 "mips plan 9 boot image",
117 sizeof(struct mipsexec),
120 { (0x160<<16)|3, /* Mips boot image */
121 "mips 4k plan 9 boot image",
126 sizeof(struct mips4kexec),
129 { K_MAGIC, /* Sparc k.out */
130 "sparc plan 9 executable",
138 { 0x01030107, /* Sparc boot image */
139 "sparc plan 9 boot image",
144 sizeof(struct sparcexec),
147 { U_MAGIC, /* Sparc64 u.out */
148 "sparc64 plan 9 executable",
149 "sparc64 plan 9 dlm",
156 { A_MAGIC, /* 68020 2.out & boot image */
157 "68020 plan 9 executable",
165 { 0xFEEDFACE, /* Next boot image */
166 "next plan 9 boot image",
171 sizeof(struct nextexec),
174 { I_MAGIC, /* I386 8.out & boot image */
175 "386 plan 9 executable",
183 { S_MAGIC, /* amd64 6.out & boot image */
184 "amd64 plan 9 executable",
192 { Q_MAGIC, /* PowerPC q.out & boot image */
193 "power plan 9 executable",
201 { T_MAGIC, /* power64 9.out & boot image */
202 "power64 plan 9 executable",
203 "power64 plan 9 dlm",
210 { ELF_MAG, /* any ELF */
219 { E_MAGIC, /* Arm 5.out and boot image */
220 "arm plan 9 executable",
228 { (143<<16)|0413, /* (Free|Net)BSD Arm */
229 "arm *bsd executable",
237 { L_MAGIC, /* alpha 7.out */
238 "alpha plan 9 executable",
246 { 0x0700e0c3, /* alpha boot image */
247 "alpha plan 9 boot image",
258 Mach *mach = &mi386; /* Global current machine table */
261 couldbe4k(ExecTable *mp)
266 if((d=dirstat("/proc/1/regs")) == nil)
268 if(d->length < 32*8){ /* R3000 */
273 for (f = exectab; f->magic; f++)
274 if(f->magic == M_MAGIC) {
275 f->name = "mips plan 9 executable on mips2 kernel";
282 crackhdr(int fd, Fhdr *fp)
290 nb = read(fd, (char *)&d.e, sizeof(d.e));
295 magic = beswal(d.e.magic); /* big-endian */
296 for (mp = exectab; mp->magic; mp++) {
301 * The magic number has morphed into something
302 * with fields (the straw was DYN_MAGIC) so now
303 * a flag is needed in Fhdr to distinguish _MAGIC()
304 * magic numbers from foreign magic numbers.
306 * This code is creaking a bit and if it has to
307 * be modified/extended much more it's probably
308 * time to step back and redo it all.
311 if(mp->magic != (magic & ~DYN_MAGIC))
314 if(mp->magic == V_MAGIC)
317 if ((magic & DYN_MAGIC) && mp->dlmname != nil)
318 fp->name = mp->dlmname;
323 if(mp->magic != magic)
328 fp->hdrsz = mp->hsize; /* will be zero on bootables */
329 fp->_magic = mp->_magic;
334 hswal(&d, sizeof(d.e)/sizeof(ulong), mp->swal);
335 ret = mp->hparse(fd, fp, &d);
336 seek(fd, mp->hsize, 0); /* seek to end of header */
340 werrstr("unknown header type");
345 * Convert header to canonical form
348 hswal(void *v, int n, ulong (*swap)(ulong))
352 for(ulp = v; n--; ulp++)
353 *ulp = (*swap)(*ulp);
357 * Crack a normal a.out-type header
360 adotout(int fd, Fhdr *fp, ExecHdr *hp)
365 pgsize = mach->pgsize;
366 settext(fp, hp->e.entry, pgsize+sizeof(Exec),
367 hp->e.text, sizeof(Exec));
368 setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize),
369 hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
370 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
377 if (!(fp->entry & mach->ktmask))
380 switch(fp->type) { /* boot image */
383 fp->name = "68020 plan 9 boot image";
387 fp->txtaddr = (u32int)fp->entry;
388 fp->name = "386 plan 9 boot image";
389 fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
393 fp->txtaddr = (u32int)fp->entry;
394 fp->name = "ARM plan 9 boot image";
395 fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
399 fp->txtaddr = (u32int)fp->entry;
400 fp->name = "alpha plan 9 boot image";
401 fp->dataddr = fp->txtaddr+fp->txtsz;
405 fp->txtaddr = (u32int)fp->entry;
406 fp->name = "power plan 9 boot image";
407 fp->dataddr = fp->txtaddr+fp->txtsz;
411 fp->txtaddr = fp->entry;
412 fp->name = "amd64 plan 9 boot image";
413 fp->dataddr = _round(fp->txtaddr+fp->txtsz, 4096);
416 fp->type = FPOWER64B;
417 fp->txtaddr = fp->entry;
418 fp->name = "power64 plan 9 boot image";
419 fp->dataddr = fp->txtaddr+fp->txtsz;
424 fp->hdrsz = 0; /* header stripped */
428 * _MAGIC() style headers and
429 * alpha plan9-style bootable images for axp "headerless" boot
433 common(int fd, Fhdr *fp, ExecHdr *hp)
436 if(hp->e.magic & DYN_MAGIC) {
438 fp->dataddr = fp->txtsz;
446 commonllp64(int, Fhdr *fp, ExecHdr *hp)
451 hswal(&hp->e, sizeof(Exec)/sizeof(long), beswal);
452 if(!(hp->e.magic & HDR_MAGIC))
456 * There can be more magic here if the
457 * header ever needs more expansion.
458 * For now just catch use of any of the
461 if((hp->e.magic & ~DYN_MAGIC)>>16)
463 entry = beswav(hp->e.hdr[0]);
465 pgsize = mach->pgsize;
466 settext(fp, entry, pgsize+fp->hdrsz, hp->e.text, fp->hdrsz);
467 setdata(fp, _round(pgsize+fp->txtsz+fp->hdrsz, pgsize),
468 hp->e.data, fp->txtsz+fp->hdrsz, hp->e.bss);
469 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
471 if(hp->e.magic & DYN_MAGIC) {
473 fp->dataddr = fp->txtsz;
481 * mips bootable image.
484 mipsboot(int fd, Fhdr *fp, ExecHdr *hp)
488 switch(hp->e.amagic) {
490 case 0407: /* some kind of mips */
491 settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
492 hp->e.tsize, sizeof(struct mipsexec)+4);
493 setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
494 fp->txtoff+hp->e.tsize, hp->e.bsize);
496 case 0413: /* some kind of mips */
497 settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
499 setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
500 hp->e.tsize, hp->e.bsize);
503 setsym(fp, hp->e.nsyms, 0, hp->e.pcsize, hp->e.symptr);
504 fp->hdrsz = 0; /* header stripped */
509 * mips4k bootable image.
512 mips4kboot(int fd, Fhdr *fp, ExecHdr *hp)
516 switch(hp->e.h.amagic) {
518 case 0407: /* some kind of mips */
519 settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
520 hp->e.h.tsize, sizeof(struct mips4kexec));
521 setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
522 fp->txtoff+hp->e.h.tsize, hp->e.h.bsize);
524 case 0413: /* some kind of mips */
525 settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
527 setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
528 hp->e.h.tsize, hp->e.h.bsize);
531 setsym(fp, hp->e.h.nsyms, 0, hp->e.h.pcsize, hp->e.h.symptr);
532 fp->hdrsz = 0; /* header stripped */
537 * sparc bootable image
540 sparcboot(int fd, Fhdr *fp, ExecHdr *hp)
544 settext(fp, hp->e.sentry, hp->e.sentry, hp->e.stext,
545 sizeof(struct sparcexec));
546 setdata(fp, hp->e.sentry+hp->e.stext, hp->e.sdata,
547 fp->txtoff+hp->e.stext, hp->e.sbss);
548 setsym(fp, hp->e.ssyms, 0, hp->e.sdrsize, fp->datoff+hp->e.sdata);
549 fp->hdrsz = 0; /* header stripped */
554 * next bootable image
557 nextboot(int fd, Fhdr *fp, ExecHdr *hp)
561 settext(fp, hp->e.textc.vmaddr, hp->e.textc.vmaddr,
562 hp->e.texts.size, hp->e.texts.offset);
563 setdata(fp, hp->e.datac.vmaddr, hp->e.datas.size,
564 hp->e.datas.offset, hp->e.bsss.size);
565 setsym(fp, hp->e.symc.nsyms, hp->e.symc.spoff, hp->e.symc.pcoff,
567 fp->hdrsz = 0; /* header stripped */
575 elf64dotout(int fd, Fhdr *fp, ExecHdr *hp)
579 ushort (*swab)(ushort);
580 ulong (*swal)(ulong);
581 uvlong (*swav)(uvlong);
582 int i, it, id, is, phsz;
586 if(ep->ident[DATA] == ELFDATA2LSB) {
590 } else if(ep->ident[DATA] == ELFDATA2MSB) {
595 werrstr("bad ELF64 encoding - not big or little endian");
599 ep->type = swab(ep->type);
600 ep->machine = swab(ep->machine);
601 ep->version = swal(ep->version);
602 if(ep->type != EXEC || ep->version != CURRENT)
604 ep->elfentry = swav(ep->elfentry);
605 ep->phoff = swav(ep->phoff);
606 ep->shoff = swav(ep->shoff);
607 ep->flags = swal(ep->flags);
608 ep->ehsize = swab(ep->ehsize);
609 ep->phentsize = swab(ep->phentsize);
610 ep->phnum = swab(ep->phnum);
611 ep->shentsize = swab(ep->shentsize);
612 ep->shnum = swab(ep->shnum);
613 ep->shstrndx = swab(ep->shstrndx);
616 fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
617 switch(ep->machine) {
623 fp->name = "amd64 ELF64 executable";
628 fp->name = "power64 ELF64 executable";
632 if(ep->phentsize != sizeof(P64hdr)) {
633 werrstr("bad ELF64 header size");
636 phsz = sizeof(P64hdr)*ep->phnum;
640 seek(fd, ep->phoff, 0);
641 if(read(fd, ph, phsz) < 0) {
645 for(i = 0; i < ep->phnum; i++) {
646 ph[i].type = swal(ph[i].type);
647 ph[i].flags = swal(ph[i].flags);
648 ph[i].offset = swav(ph[i].offset);
649 ph[i].vaddr = swav(ph[i].vaddr);
650 ph[i].paddr = swav(ph[i].paddr);
651 ph[i].filesz = swav(ph[i].filesz);
652 ph[i].memsz = swav(ph[i].memsz);
653 ph[i].align = swav(ph[i].align);
656 /* find text, data and symbols and install them */
658 for(i = 0; i < ep->phnum; i++) {
659 if(ph[i].type == LOAD
660 && (ph[i].flags & (R|X)) == (R|X) && it == -1)
662 else if(ph[i].type == LOAD
663 && (ph[i].flags & (R|W)) == (R|W) && id == -1)
665 else if(ph[i].type == NOPTYPE && is == -1)
668 if(it == -1 || id == -1) {
669 werrstr("No ELF64 TEXT or DATA sections");
674 settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
675 /* 8c: out of fixed registers */
676 uvl = ph[id].memsz - ph[id].filesz;
677 setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, uvl);
679 setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);
688 elf32dotout(int fd, Fhdr *fp, ExecHdr *hp)
690 ulong (*swal)(ulong);
691 ushort (*swab)(ushort);
694 int i, it, id, is, phsz;
696 /* bitswap the header according to the DATA format */
698 if(ep->ident[DATA] == ELFDATA2LSB) {
701 } else if(ep->ident[DATA] == ELFDATA2MSB) {
705 werrstr("bad ELF32 encoding - not big or little endian");
709 ep->type = swab(ep->type);
710 ep->machine = swab(ep->machine);
711 ep->version = swal(ep->version);
712 ep->elfentry = swal(ep->elfentry);
713 ep->phoff = swal(ep->phoff);
714 ep->shoff = swal(ep->shoff);
715 ep->flags = swal(ep->flags);
716 ep->ehsize = swab(ep->ehsize);
717 ep->phentsize = swab(ep->phentsize);
718 ep->phnum = swab(ep->phnum);
719 ep->shentsize = swab(ep->shentsize);
720 ep->shnum = swab(ep->shnum);
721 ep->shstrndx = swab(ep->shstrndx);
722 if(ep->type != EXEC || ep->version != CURRENT)
725 /* we could definitely support a lot more machines here */
727 fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
728 switch(ep->machine) {
732 fp->name = "386 ELF32 executable";
737 fp->name = "mips ELF32 executable";
742 fp->name = "sparc64 ELF32 executable";
747 fp->name = "power ELF32 executable";
752 fp->name = "power64 ELF32 executable";
757 fp->name = "amd64 ELF32 executable";
762 fp->name = "arm ELF32 executable";
768 if(ep->phentsize != sizeof(Phdr)) {
769 werrstr("bad ELF32 header size");
772 phsz = sizeof(Phdr)*ep->phnum;
776 seek(fd, ep->phoff, 0);
777 if(read(fd, ph, phsz) < 0) {
781 hswal(ph, phsz/sizeof(ulong), swal);
783 /* find text, data and symbols and install them */
785 for(i = 0; i < ep->phnum; i++) {
786 if(ph[i].type == LOAD
787 && (ph[i].flags & (R|X)) == (R|X) && it == -1)
789 else if(ph[i].type == LOAD
790 && (ph[i].flags & (R|W)) == (R|W) && id == -1)
792 else if(ph[i].type == NOPTYPE && is == -1)
795 if(it == -1 || id == -1) {
797 * The SPARC64 boot image is something of an ELF hack.
798 * Text+Data+BSS are represented by ph[0]. Symbols
799 * are represented by ph[1]:
801 * filesz, memsz, vaddr, paddr, off
802 * ph[0] : txtsz+datsz, txtsz+datsz+bsssz, txtaddr-KZERO, datasize, txtoff
803 * ph[1] : symsz, lcsz, 0, 0, symoff
805 if(ep->machine == SPARC64 && ep->phnum == 2) {
806 ulong txtaddr, txtsz, dataddr, bsssz;
808 txtaddr = ph[0].vaddr | 0x80000000;
809 txtsz = ph[0].filesz - ph[0].paddr;
810 dataddr = txtaddr + txtsz;
811 bsssz = ph[0].memsz - ph[0].filesz;
812 settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset);
813 setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz);
814 setsym(fp, ph[1].filesz, 0, ph[1].memsz, ph[1].offset);
819 werrstr("No ELF32 TEXT or DATA sections");
824 settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
825 setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz);
827 setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);
836 elfdotout(int fd, Fhdr *fp, ExecHdr *hp)
840 /* bitswap the header according to the DATA format */
842 if(ep->ident[CLASS] == ELFCLASS32)
843 return elf32dotout(fd, fp, hp);
844 else if(ep->ident[CLASS] == ELFCLASS64)
845 return elf64dotout(fd, fp, hp);
847 werrstr("bad ELF class - not 32 bit");
852 * (Free|Net)BSD ARM header.
855 armdotout(int fd, Fhdr *fp, ExecHdr *hp)
860 settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
861 setdata(fp, fp->txtsz, hp->e.data, fp->txtsz, hp->e.bss);
862 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
865 if ((fp->entry & kbase) == kbase) { /* Boot image */
866 fp->txtaddr = kbase+sizeof(Exec);
867 fp->name = "ARM *BSD boot image";
868 fp->hdrsz = 0; /* header stripped */
869 fp->dataddr = kbase+fp->txtsz;
875 settext(Fhdr *fp, uvlong e, uvlong a, long s, vlong off)
884 setdata(Fhdr *fp, uvlong a, long s, vlong off, long bss)
893 setsym(Fhdr *fp, long symsz, long sppcsz, long lnpcsz, vlong symoff)
898 fp->sppcoff = fp->symoff+fp->symsz;
900 fp->lnpcoff = fp->sppcoff+fp->sppcsz;
905 _round(uvlong a, ulong b)