9 * All a.out header types. The dummy entry allows canonical
10 * processing of the union as a sequence of longs
20 struct mipsexec; /* bootexec.h */
21 struct mips4kexec; /* bootexec.h */
22 struct sparcexec; /* bootexec.h */
23 struct nextexec; /* bootexec.h */
25 long dummy; /* padding to ensure extra long */
28 static int nextboot(int, Fhdr*, ExecHdr*);
29 static int sparcboot(int, Fhdr*, ExecHdr*);
30 static int mipsboot(int, Fhdr*, ExecHdr*);
31 static int mips4kboot(int, Fhdr*, ExecHdr*);
32 static int common(int, Fhdr*, ExecHdr*);
33 static int commonllp64(int, Fhdr*, ExecHdr*);
34 static int adotout(int, Fhdr*, ExecHdr*);
35 static int elfdotout(int, Fhdr*, ExecHdr*);
36 static int armdotout(int, Fhdr*, ExecHdr*);
37 static void setsym(Fhdr*, long, long, long, vlong);
38 static void setdata(Fhdr*, uvlong, long, vlong, long);
39 static void settext(Fhdr*, uvlong, uvlong, long, vlong);
40 static void hswal(void*, int, ulong(*)(ulong));
41 static uvlong _round(uvlong, ulong);
44 * definition of per-executable file type structures
47 typedef struct Exectable{
48 long magic; /* big-endian magic number of file */
49 char *name; /* executable identifier */
50 char *dlmname; /* dynamically loadable module identifier */
51 uchar type; /* Internal code */
52 uchar _magic; /* _MAGIC() magic */
53 Mach *mach; /* Per-machine data */
54 long hsize; /* header size */
55 ulong (*swal)(ulong); /* header swap, beswal or leswal */
56 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 elf32 */
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",
236 { L_MAGIC, /* alpha 7.out */
237 "alpha plan 9 executable",
245 { 0x0700e0c3, /* alpha boot image */
246 "alpha plan 9 boot image",
257 Mach *mach = &mi386; /* Global current machine table */
260 couldbe4k(ExecTable *mp)
265 if((d=dirstat("/proc/1/regs")) == nil)
267 if(d->length < 32*8){ /* R3000 */
272 for (f = exectab; f->magic; f++)
273 if(f->magic == M_MAGIC) {
274 f->name = "mips plan 9 executable on mips2 kernel";
281 crackhdr(int fd, Fhdr *fp)
289 nb = read(fd, (char *)&d.e, sizeof(d.e));
294 magic = beswal(d.e.magic); /* big-endian */
295 for (mp = exectab; mp->magic; mp++) {
300 * The magic number has morphed into something
301 * with fields (the straw was DYN_MAGIC) so now
302 * a flag is needed in Fhdr to distinguish _MAGIC()
303 * magic numbers from foreign magic numbers.
305 * This code is creaking a bit and if it has to
306 * be modified/extended much more it's probably
307 * time to step back and redo it all.
310 if(mp->magic != (magic & ~DYN_MAGIC))
313 if(mp->magic == V_MAGIC)
316 if ((magic & DYN_MAGIC) && mp->dlmname != nil)
317 fp->name = mp->dlmname;
322 if(mp->magic != magic)
327 fp->hdrsz = mp->hsize; /* will be zero on bootables */
328 fp->_magic = mp->_magic;
333 hswal(&d, sizeof(d.e)/sizeof(ulong), mp->swal);
334 ret = mp->hparse(fd, fp, &d);
335 seek(fd, mp->hsize, 0); /* seek to end of header */
339 werrstr("unknown header type");
344 * Convert header to canonical form
347 hswal(void *v, int n, ulong (*swap)(ulong))
351 for(ulp = v; n--; ulp++)
352 *ulp = (*swap)(*ulp);
356 * Crack a normal a.out-type header
359 adotout(int fd, Fhdr *fp, ExecHdr *hp)
364 pgsize = mach->pgsize;
365 settext(fp, hp->e.entry, pgsize+sizeof(Exec),
366 hp->e.text, sizeof(Exec));
367 setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize),
368 hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
369 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
376 if (!(fp->entry & mach->ktmask))
379 switch(fp->type) { /* boot image */
382 fp->name = "68020 plan 9 boot image";
386 fp->txtaddr = (u32int)fp->entry;
387 fp->name = "386 plan 9 boot image";
388 fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
392 fp->txtaddr = (u32int)fp->entry;
393 fp->name = "ARM plan 9 boot image";
394 fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
398 fp->txtaddr = (u32int)fp->entry;
399 fp->name = "alpha plan 9 boot image";
400 fp->dataddr = fp->txtaddr+fp->txtsz;
404 fp->txtaddr = (u32int)fp->entry;
405 fp->name = "power plan 9 boot image";
406 fp->dataddr = fp->txtaddr+fp->txtsz;
410 fp->txtaddr = fp->entry;
411 fp->name = "amd64 plan 9 boot image";
412 fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
417 fp->hdrsz = 0; /* header stripped */
421 * _MAGIC() style headers and
422 * alpha plan9-style bootable images for axp "headerless" boot
426 common(int fd, Fhdr *fp, ExecHdr *hp)
429 if(hp->e.magic & DYN_MAGIC) {
431 fp->dataddr = fp->txtsz;
439 commonllp64(int, Fhdr *fp, ExecHdr *hp)
444 if(!(hp->e.magic & HDR_MAGIC))
448 * There can be more magic here if the
449 * header ever needs more expansion.
450 * For now just catch use of any of the
453 if((hp->e.magic & ~DYN_MAGIC)>>16)
455 entry = beswav(hp->e.hdr[0]);
457 pgsize = mach->pgsize;
458 settext(fp, entry, pgsize+fp->hdrsz, hp->e.text, fp->hdrsz);
459 setdata(fp, _round(pgsize+fp->txtsz+fp->hdrsz, pgsize),
460 hp->e.data, fp->txtsz+fp->hdrsz, hp->e.bss);
461 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
463 if(hp->e.magic & DYN_MAGIC) {
465 fp->dataddr = fp->txtsz;
473 * mips bootable image.
476 mipsboot(int fd, Fhdr *fp, ExecHdr *hp)
480 switch(hp->e.amagic) {
482 case 0407: /* some kind of mips */
483 settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
484 hp->e.tsize, sizeof(struct mipsexec)+4);
485 setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
486 fp->txtoff+hp->e.tsize, hp->e.bsize);
488 case 0413: /* some kind of mips */
489 settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
491 setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
492 hp->e.tsize, hp->e.bsize);
495 setsym(fp, hp->e.nsyms, 0, hp->e.pcsize, hp->e.symptr);
496 fp->hdrsz = 0; /* header stripped */
501 * mips4k bootable image.
504 mips4kboot(int fd, Fhdr *fp, ExecHdr *hp)
508 switch(hp->e.h.amagic) {
510 case 0407: /* some kind of mips */
511 settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
512 hp->e.h.tsize, sizeof(struct mips4kexec));
513 setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
514 fp->txtoff+hp->e.h.tsize, hp->e.h.bsize);
516 case 0413: /* some kind of mips */
517 settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
519 setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
520 hp->e.h.tsize, hp->e.h.bsize);
523 setsym(fp, hp->e.h.nsyms, 0, hp->e.h.pcsize, hp->e.h.symptr);
524 fp->hdrsz = 0; /* header stripped */
529 * sparc bootable image
532 sparcboot(int fd, Fhdr *fp, ExecHdr *hp)
536 settext(fp, hp->e.sentry, hp->e.sentry, hp->e.stext,
537 sizeof(struct sparcexec));
538 setdata(fp, hp->e.sentry+hp->e.stext, hp->e.sdata,
539 fp->txtoff+hp->e.stext, hp->e.sbss);
540 setsym(fp, hp->e.ssyms, 0, hp->e.sdrsize, fp->datoff+hp->e.sdata);
541 fp->hdrsz = 0; /* header stripped */
546 * next bootable image
549 nextboot(int fd, Fhdr *fp, ExecHdr *hp)
553 settext(fp, hp->e.textc.vmaddr, hp->e.textc.vmaddr,
554 hp->e.texts.size, hp->e.texts.offset);
555 setdata(fp, hp->e.datac.vmaddr, hp->e.datas.size,
556 hp->e.datas.offset, hp->e.bsss.size);
557 setsym(fp, hp->e.symc.nsyms, hp->e.symc.spoff, hp->e.symc.pcoff,
559 fp->hdrsz = 0; /* header stripped */
567 elfdotout(int fd, Fhdr *fp, ExecHdr *hp)
570 ulong (*swal)(ulong);
571 ushort (*swab)(ushort);
574 int i, it, id, is, phsz;
576 /* bitswap the header according to the DATA format */
578 if(ep->ident[CLASS] != ELFCLASS32) {
579 werrstr("bad ELF class - not 32 bit");
582 if(ep->ident[DATA] == ELFDATA2LSB) {
585 } else if(ep->ident[DATA] == ELFDATA2MSB) {
589 werrstr("bad ELF encoding - not big or little endian");
593 ep->type = swab(ep->type);
594 ep->machine = swab(ep->machine);
595 ep->version = swal(ep->version);
596 ep->elfentry = swal(ep->elfentry);
597 ep->phoff = swal(ep->phoff);
598 ep->shoff = swal(ep->shoff);
599 ep->flags = swal(ep->flags);
600 ep->ehsize = swab(ep->ehsize);
601 ep->phentsize = swab(ep->phentsize);
602 ep->phnum = swab(ep->phnum);
603 ep->shentsize = swab(ep->shentsize);
604 ep->shnum = swab(ep->shnum);
605 ep->shstrndx = swab(ep->shstrndx);
606 if(ep->type != EXEC || ep->version != CURRENT)
609 /* we could definitely support a lot more machines here */
611 fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
612 switch(ep->machine) {
641 if(ep->phentsize != sizeof(Phdr)) {
642 werrstr("bad ELF header size");
645 phsz = sizeof(Phdr)*ep->phnum;
649 seek(fd, ep->phoff, 0);
650 if(read(fd, ph, phsz) < 0) {
654 hswal(ph, phsz/sizeof(ulong), swal);
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) {
670 * The SPARC64 boot image is something of an ELF hack.
671 * Text+Data+BSS are represented by ph[0]. Symbols
672 * are represented by ph[1]:
674 * filesz, memsz, vaddr, paddr, off
675 * ph[0] : txtsz+datsz, txtsz+datsz+bsssz, txtaddr-KZERO, datasize, txtoff
676 * ph[1] : symsz, lcsz, 0, 0, symoff
678 if(ep->machine == SPARC64 && ep->phnum == 2) {
679 ulong txtaddr, txtsz, dataddr, bsssz;
681 txtaddr = ph[0].vaddr | 0x80000000;
682 txtsz = ph[0].filesz - ph[0].paddr;
683 dataddr = txtaddr + txtsz;
684 bsssz = ph[0].memsz - ph[0].filesz;
685 settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset);
686 setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz);
687 setsym(fp, ph[1].filesz, 0, ph[1].memsz, ph[1].offset);
692 werrstr("No TEXT or DATA sections");
697 settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
698 setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz);
700 setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);
706 * (Free|Net)BSD ARM header.
709 armdotout(int fd, Fhdr *fp, ExecHdr *hp)
714 settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
715 setdata(fp, fp->txtsz, hp->e.data, fp->txtsz, hp->e.bss);
716 setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
719 if ((fp->entry & kbase) == kbase) { /* Boot image */
720 fp->txtaddr = kbase+sizeof(Exec);
721 fp->name = "ARM *BSD boot image";
722 fp->hdrsz = 0; /* header stripped */
723 fp->dataddr = kbase+fp->txtsz;
729 settext(Fhdr *fp, uvlong e, uvlong a, long s, vlong off)
738 setdata(Fhdr *fp, uvlong a, long s, vlong off, long bss)
747 setsym(Fhdr *fp, long symsz, long sppcsz, long lnpcsz, vlong symoff)
752 fp->sppcoff = fp->symoff+fp->symsz;
754 fp->lnpcoff = fp->sppcoff+fp->sppcsz;
759 _round(uvlong a, ulong b)