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 { R_MAGIC, /* Arm64 7.out and boot image */
238 "arm64 plan 9 executable",
249 Mach *mach = &mi386; /* Global current machine table */
252 couldbe4k(ExecTable *mp)
257 if((d=dirstat("/proc/1/regs")) == nil)
259 if(d->length < 32*8){ /* R3000 */
264 for (f = exectab; f->magic; f++)
265 if(f->magic == M_MAGIC) {
266 f->name = "mips plan 9 executable on mips2 kernel";
273 crackhdr(int fd, Fhdr *fp)
281 nb = read(fd, (char *)&d.e, sizeof(d.e));
286 magic = beswal(d.e.magic); /* big-endian */
287 for (mp = exectab; mp->magic; mp++) {
292 * The magic number has morphed into something
293 * with fields (the straw was DYN_MAGIC) so now
294 * a flag is needed in Fhdr to distinguish _MAGIC()
295 * magic numbers from foreign magic numbers.
297 * This code is creaking a bit and if it has to
298 * be modified/extended much more it's probably
299 * time to step back and redo it all.
302 if(mp->magic != (magic & ~DYN_MAGIC))
305 if(mp->magic == V_MAGIC)
308 if ((magic & DYN_MAGIC) && mp->dlmname != nil)
309 fp->name = mp->dlmname;
314 if(mp->magic != magic)
319 fp->hdrsz = mp->hsize; /* will be zero on bootables */
320 fp->_magic = mp->_magic;
325 hswal(&d, sizeof(d.e)/sizeof(ulong), mp->swal);
326 ret = mp->hparse(fd, fp, &d);
327 seek(fd, mp->hsize, 0); /* seek to end of header */
331 werrstr("unknown header type");
336 * Convert header to canonical form
339 hswal(void *v, int n, ulong (*swap)(ulong))
343 for(ulp = v; n--; ulp++)
344 *ulp = (*swap)(*ulp);
348 * Crack a normal a.out-type header
351 adotout(int fd, Fhdr *fp, ExecHdr *hp)
356 pgsize = mach->pgsize;
357 settext(fp, hp->e.entry, pgsize+sizeof(Exec),
358 hp->e.text, sizeof(Exec));
359 setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize),
360 hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
361 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
368 if (!(fp->entry & mach->ktmask))
371 switch(fp->type) { /* boot image */
374 fp->name = "68020 plan 9 boot image";
378 fp->txtaddr = (u32int)fp->entry;
379 fp->name = "386 plan 9 boot image";
380 fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
384 fp->txtaddr = (u32int)fp->entry;
385 fp->name = "ARM plan 9 boot image";
386 fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
390 fp->txtaddr = fp->entry;
391 fp->name = "arm64 plan 9 boot image";
392 fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
396 fp->txtaddr = (u32int)fp->entry;
397 fp->name = "power plan 9 boot image";
398 fp->dataddr = fp->txtaddr+fp->txtsz;
402 fp->txtaddr = fp->entry;
403 fp->name = "amd64 plan 9 boot image";
404 fp->dataddr = _round(fp->txtaddr+fp->txtsz, 4096);
407 fp->type = FPOWER64B;
408 fp->txtaddr = fp->entry;
409 fp->name = "power64 plan 9 boot image";
410 fp->dataddr = fp->txtaddr+fp->txtsz;
415 fp->hdrsz = 0; /* header stripped */
419 * _MAGIC() style headers and
420 * alpha plan9-style bootable images for axp "headerless" boot
424 common(int fd, Fhdr *fp, ExecHdr *hp)
427 if(hp->e.magic & DYN_MAGIC) {
429 fp->dataddr = fp->txtsz;
437 commonllp64(int, Fhdr *fp, ExecHdr *hp)
442 hswal(&hp->e, sizeof(Exec)/sizeof(long), beswal);
443 if(!(hp->e.magic & HDR_MAGIC))
447 * There can be more magic here if the
448 * header ever needs more expansion.
449 * For now just catch use of any of the
452 if((hp->e.magic & ~DYN_MAGIC)>>16)
454 entry = beswav(hp->e.hdr[0]);
456 pgsize = mach->pgsize;
457 settext(fp, entry, pgsize+fp->hdrsz, hp->e.text, fp->hdrsz);
458 setdata(fp, _round(pgsize+fp->txtsz+fp->hdrsz, pgsize),
459 hp->e.data, fp->txtsz+fp->hdrsz, hp->e.bss);
460 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
462 if(hp->e.magic & DYN_MAGIC) {
464 fp->dataddr = fp->txtsz;
472 * mips bootable image.
475 mipsboot(int fd, Fhdr *fp, ExecHdr *hp)
479 switch(hp->e.amagic) {
481 case 0407: /* some kind of mips */
482 settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
483 hp->e.tsize, sizeof(struct mipsexec)+4);
484 setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
485 fp->txtoff+hp->e.tsize, hp->e.bsize);
487 case 0413: /* some kind of mips */
488 settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
490 setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
491 hp->e.tsize, hp->e.bsize);
494 setsym(fp, hp->e.nsyms, 0, hp->e.pcsize, hp->e.symptr);
495 fp->hdrsz = 0; /* header stripped */
500 * mips4k bootable image.
503 mips4kboot(int fd, Fhdr *fp, ExecHdr *hp)
507 switch(hp->e.h.amagic) {
509 case 0407: /* some kind of mips */
510 settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
511 hp->e.h.tsize, sizeof(struct mips4kexec));
512 setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
513 fp->txtoff+hp->e.h.tsize, hp->e.h.bsize);
515 case 0413: /* some kind of mips */
516 settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
518 setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
519 hp->e.h.tsize, hp->e.h.bsize);
522 setsym(fp, hp->e.h.nsyms, 0, hp->e.h.pcsize, hp->e.h.symptr);
523 fp->hdrsz = 0; /* header stripped */
528 * sparc bootable image
531 sparcboot(int fd, Fhdr *fp, ExecHdr *hp)
535 settext(fp, hp->e.sentry, hp->e.sentry, hp->e.stext,
536 sizeof(struct sparcexec));
537 setdata(fp, hp->e.sentry+hp->e.stext, hp->e.sdata,
538 fp->txtoff+hp->e.stext, hp->e.sbss);
539 setsym(fp, hp->e.ssyms, 0, hp->e.sdrsize, fp->datoff+hp->e.sdata);
540 fp->hdrsz = 0; /* header stripped */
545 * next bootable image
548 nextboot(int fd, Fhdr *fp, ExecHdr *hp)
552 settext(fp, hp->e.textc.vmaddr, hp->e.textc.vmaddr,
553 hp->e.texts.size, hp->e.texts.offset);
554 setdata(fp, hp->e.datac.vmaddr, hp->e.datas.size,
555 hp->e.datas.offset, hp->e.bsss.size);
556 setsym(fp, hp->e.symc.nsyms, hp->e.symc.spoff, hp->e.symc.pcoff,
558 fp->hdrsz = 0; /* header stripped */
566 elf64dotout(int fd, Fhdr *fp, ExecHdr *hp)
570 ushort (*swab)(ushort);
571 ulong (*swal)(ulong);
572 uvlong (*swav)(uvlong);
573 int i, it, id, is, phsz;
577 if(ep->ident[DATA] == ELFDATA2LSB) {
581 } else if(ep->ident[DATA] == ELFDATA2MSB) {
586 werrstr("bad ELF64 encoding - not big or little endian");
590 ep->type = swab(ep->type);
591 ep->machine = swab(ep->machine);
592 ep->version = swal(ep->version);
593 if(ep->type != EXEC || ep->version != CURRENT)
595 ep->elfentry = swav(ep->elfentry);
596 ep->phoff = swav(ep->phoff);
597 ep->shoff = swav(ep->shoff);
598 ep->flags = swal(ep->flags);
599 ep->ehsize = swab(ep->ehsize);
600 ep->phentsize = swab(ep->phentsize);
601 ep->phnum = swab(ep->phnum);
602 ep->shentsize = swab(ep->shentsize);
603 ep->shnum = swab(ep->shnum);
604 ep->shstrndx = swab(ep->shstrndx);
607 fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
608 switch(ep->machine) {
614 fp->name = "amd64 ELF64 executable";
619 fp->name = "power64 ELF64 executable";
623 if(ep->phentsize != sizeof(P64hdr)) {
624 werrstr("bad ELF64 header size");
627 phsz = sizeof(P64hdr)*ep->phnum;
631 seek(fd, ep->phoff, 0);
632 if(read(fd, ph, phsz) < 0) {
636 for(i = 0; i < ep->phnum; i++) {
637 ph[i].type = swal(ph[i].type);
638 ph[i].flags = swal(ph[i].flags);
639 ph[i].offset = swav(ph[i].offset);
640 ph[i].vaddr = swav(ph[i].vaddr);
641 ph[i].paddr = swav(ph[i].paddr);
642 ph[i].filesz = swav(ph[i].filesz);
643 ph[i].memsz = swav(ph[i].memsz);
644 ph[i].align = swav(ph[i].align);
647 /* find text, data and symbols and install them */
649 for(i = 0; i < ep->phnum; i++) {
650 if(ph[i].type == LOAD
651 && (ph[i].flags & (R|X)) == (R|X) && it == -1)
653 else if(ph[i].type == LOAD
654 && (ph[i].flags & (R|W)) == (R|W) && id == -1)
656 else if(ph[i].type == NOPTYPE && is == -1)
659 if(it == -1 || id == -1) {
660 werrstr("No ELF64 TEXT or DATA sections");
665 settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
666 /* 8c: out of fixed registers */
667 uvl = ph[id].memsz - ph[id].filesz;
668 setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, uvl);
670 setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);
679 elf32dotout(int fd, Fhdr *fp, ExecHdr *hp)
681 ulong (*swal)(ulong);
682 ushort (*swab)(ushort);
685 int i, it, id, is, phsz;
687 /* bitswap the header according to the DATA format */
689 if(ep->ident[DATA] == ELFDATA2LSB) {
692 } else if(ep->ident[DATA] == ELFDATA2MSB) {
696 werrstr("bad ELF32 encoding - not big or little endian");
700 ep->type = swab(ep->type);
701 ep->machine = swab(ep->machine);
702 ep->version = swal(ep->version);
703 ep->elfentry = swal(ep->elfentry);
704 ep->phoff = swal(ep->phoff);
705 ep->shoff = swal(ep->shoff);
706 ep->flags = swal(ep->flags);
707 ep->ehsize = swab(ep->ehsize);
708 ep->phentsize = swab(ep->phentsize);
709 ep->phnum = swab(ep->phnum);
710 ep->shentsize = swab(ep->shentsize);
711 ep->shnum = swab(ep->shnum);
712 ep->shstrndx = swab(ep->shstrndx);
713 if(ep->type != EXEC || ep->version != CURRENT)
716 /* we could definitely support a lot more machines here */
718 fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
719 switch(ep->machine) {
723 fp->name = "386 ELF32 executable";
727 if(ep->ident[DATA] == ELFDATA2LSB){
729 fp->name = "mips le ELF32 executable";
732 fp->name = "mips be ELF32 executable";
738 fp->name = "sparc64 ELF32 executable";
743 fp->name = "power ELF32 executable";
748 fp->name = "power64 ELF32 executable";
753 fp->name = "amd64 ELF32 executable";
758 fp->name = "arm ELF32 executable";
764 if(ep->phentsize != sizeof(Phdr)) {
765 werrstr("bad ELF32 header size");
768 phsz = sizeof(Phdr)*ep->phnum;
772 seek(fd, ep->phoff, 0);
773 if(read(fd, ph, phsz) < 0) {
777 hswal(ph, phsz/sizeof(ulong), swal);
779 /* find text, data and symbols and install them */
781 for(i = 0; i < ep->phnum; i++) {
782 if(ph[i].type == LOAD
783 && (ph[i].flags & (R|X)) == (R|X) && it == -1)
785 else if(ph[i].type == LOAD
786 && (ph[i].flags & (R|W)) == (R|W) && id == -1)
788 else if(ph[i].type == NOPTYPE && is == -1)
791 if(it == -1 || id == -1) {
793 * The SPARC64 boot image is something of an ELF hack.
794 * Text+Data+BSS are represented by ph[0]. Symbols
795 * are represented by ph[1]:
797 * filesz, memsz, vaddr, paddr, off
798 * ph[0] : txtsz+datsz, txtsz+datsz+bsssz, txtaddr-KZERO, datasize, txtoff
799 * ph[1] : symsz, lcsz, 0, 0, symoff
801 if(ep->machine == SPARC64 && ep->phnum == 2) {
802 ulong txtaddr, txtsz, dataddr, bsssz;
804 txtaddr = ph[0].vaddr | 0x80000000;
805 txtsz = ph[0].filesz - ph[0].paddr;
806 dataddr = txtaddr + txtsz;
807 bsssz = ph[0].memsz - ph[0].filesz;
808 settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset);
809 setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz);
810 setsym(fp, ph[1].filesz, 0, ph[1].memsz, ph[1].offset);
815 werrstr("No ELF32 TEXT or DATA sections");
820 settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
821 setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz);
823 setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);
832 elfdotout(int fd, Fhdr *fp, ExecHdr *hp)
836 /* bitswap the header according to the DATA format */
838 if(ep->ident[CLASS] == ELFCLASS32)
839 return elf32dotout(fd, fp, hp);
840 else if(ep->ident[CLASS] == ELFCLASS64)
841 return elf64dotout(fd, fp, hp);
843 werrstr("bad ELF class - not 32 bit");
848 * (Free|Net)BSD ARM header.
851 armdotout(int fd, Fhdr *fp, ExecHdr *hp)
856 settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
857 setdata(fp, fp->txtsz, hp->e.data, fp->txtsz, hp->e.bss);
858 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
861 if ((fp->entry & kbase) == kbase) { /* Boot image */
862 fp->txtaddr = kbase+sizeof(Exec);
863 fp->name = "ARM *BSD boot image";
864 fp->hdrsz = 0; /* header stripped */
865 fp->dataddr = kbase+fp->txtsz;
871 settext(Fhdr *fp, uvlong e, uvlong a, long s, vlong off)
880 setdata(Fhdr *fp, uvlong a, long s, vlong off, long bss)
889 setsym(Fhdr *fp, long symsz, long sppcsz, long lnpcsz, vlong symoff)
894 fp->sppcoff = fp->symoff+fp->symsz;
896 fp->lnpcoff = fp->sppcoff+fp->sppcsz;
901 _round(uvlong a, ulong b)