123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647 |
- #include <u.h>
- #include <libc.h>
- #include <bio.h>
- #include <bootexec.h>
- #include <mach.h>
- #include "elf.h"
- /*
- * All a.out header types. The dummy entry allows canonical
- * processing of the union as a sequence of longs
- */
- typedef struct {
- union{
- Exec; /* in a.out.h */
- Ehdr; /* in elf.h */
- struct mipsexec;
- struct mips4kexec;
- struct sparcexec;
- struct nextexec;
- } e;
- long dummy; /* padding to ensure extra long */
- } ExecHdr;
- static int nextboot(int, Fhdr*, ExecHdr*);
- static int sparcboot(int, Fhdr*, ExecHdr*);
- static int mipsboot(int, Fhdr*, ExecHdr*);
- static int mips4kboot(int, Fhdr*, ExecHdr*);
- static int common(int, Fhdr*, ExecHdr*);
- static int adotout(int, Fhdr*, ExecHdr*);
- static int elfdotout(int, Fhdr*, ExecHdr*);
- static int armdotout(int, Fhdr*, ExecHdr*);
- static int alphadotout(int, Fhdr*, ExecHdr*);
- static void setsym(Fhdr*, long, long, long, long);
- static void setdata(Fhdr*, long, long, long, long);
- static void settext(Fhdr*, long, long, long, long);
- static void hswal(long*, int, long(*)(long));
- static long _round(long, long);
- /*
- * definition of per-executable file type structures
- */
- typedef struct Exectable{
- long magic; /* big-endian magic number of file */
- char *name; /* executable identifier */
- char *dlmname; /* dynamically loadable module identifier */
- int type; /* Internal code */
- Mach *mach; /* Per-machine data */
- ulong hsize; /* header size */
- long (*swal)(long); /* beswal or leswal */
- int (*hparse)(int, Fhdr*, ExecHdr*);
- } ExecTable;
- extern Mach mmips;
- extern Mach mmips2le;
- extern Mach mmips2be;
- extern Mach msparc;
- extern Mach m68020;
- extern Mach mi386;
- extern Mach marm;
- extern Mach mpower;
- extern Mach malpha;
- ExecTable exectab[] =
- {
- { V_MAGIC, /* Mips v.out */
- "mips plan 9 executable",
- "mips plan 9 dlm",
- FMIPS,
- &mmips,
- sizeof(Exec),
- beswal,
- adotout },
- { M_MAGIC, /* Mips 4.out */
- "mips 4k plan 9 executable BE",
- "mips 4k plan 9 dlm BE",
- FMIPS2BE,
- &mmips2be,
- sizeof(Exec),
- beswal,
- adotout },
- { N_MAGIC, /* Mips 0.out */
- "mips 4k plan 9 executable LE",
- "mips 4k plan 9 dlm LE",
- FMIPS2LE,
- &mmips2le,
- sizeof(Exec),
- beswal,
- adotout },
- { 0x160<<16, /* Mips boot image */
- "mips plan 9 boot image",
- nil,
- FMIPSB,
- &mmips,
- sizeof(struct mipsexec),
- beswal,
- mipsboot },
- { (0x160<<16)|3, /* Mips boot image */
- "mips 4k plan 9 boot image",
- nil,
- FMIPSB,
- &mmips2be,
- sizeof(struct mips4kexec),
- beswal,
- mips4kboot },
- { K_MAGIC, /* Sparc k.out */
- "sparc plan 9 executable",
- "sparc plan 9 dlm",
- FSPARC,
- &msparc,
- sizeof(Exec),
- beswal,
- adotout },
- { 0x01030107, /* Sparc boot image */
- "sparc plan 9 boot image",
- nil,
- FSPARCB,
- &msparc,
- sizeof(struct sparcexec),
- beswal,
- sparcboot },
- { A_MAGIC, /* 68020 2.out & boot image */
- "68020 plan 9 executable",
- "68020 plan 9 dlm",
- F68020,
- &m68020,
- sizeof(Exec),
- beswal,
- common },
- { 0xFEEDFACE, /* Next boot image */
- "next plan 9 boot image",
- nil,
- FNEXTB,
- &m68020,
- sizeof(struct nextexec),
- beswal,
- nextboot },
- { I_MAGIC, /* I386 8.out & boot image */
- "386 plan 9 executable",
- "386 plan 9 dlm",
- FI386,
- &mi386,
- sizeof(Exec),
- beswal,
- common },
- { Q_MAGIC, /* PowerPC q.out & boot image */
- "power plan 9 executable",
- "power plan 9 dlm",
- FPOWER,
- &mpower,
- sizeof(Exec),
- beswal,
- common },
- { ELF_MAG,
- "Irix 5.X Elf executable",
- nil,
- FMIPS,
- &mmips,
- sizeof(Ehdr),
- beswal,
- elfdotout },
- { E_MAGIC, /* Arm 5.out */
- "Arm plan 9 executable",
- "Arm plan 9 dlm",
- FARM,
- &marm,
- sizeof(Exec),
- beswal,
- common },
- { (143<<16)|0413, /* (Free|Net)BSD Arm */
- "Arm *BSD executable",
- nil,
- FARM,
- &marm,
- sizeof(Exec),
- leswal,
- armdotout },
- { L_MAGIC, /* alpha 7.out */
- "alpha plan 9 executable",
- "alpha plan 9 dlm",
- FALPHA,
- &malpha,
- sizeof(Exec),
- beswal,
- common },
- { 0x0700e0c3, /* alpha boot image */
- "alpha plan 9 boot image",
- nil,
- FALPHAB,
- &malpha,
- sizeof(Exec),
- beswal,
- alphadotout },
- { 0 },
- };
- Mach *mach = &mi386; /* Global current machine table */
- static ExecTable*
- couldbe4k(ExecTable *mp)
- {
- Dir *d;
- ExecTable *f;
- if((d=dirstat("/proc/1/regs")) == nil)
- return mp;
- if(d->length < 32*8){ /* R3000 */
- free(d);
- return mp;
- }
- free(d);
- for (f = exectab; f->magic; f++)
- if(f->magic == M_MAGIC) {
- f->name = "mips plan 9 executable on mips2 kernel";
- return f;
- }
- return mp;
- }
- int
- crackhdr(int fd, Fhdr *fp)
- {
- ExecTable *mp;
- ExecHdr d;
- int nb, magic, ret;
- fp->type = FNONE;
- nb = read(fd, (char *)&d.e, sizeof(d.e));
- if (nb <= 0)
- return 0;
- ret = 0;
- fp->magic = magic = beswal(d.e.magic); /* big-endian */
- for (mp = exectab; mp->magic; mp++) {
- if (nb < mp->hsize)
- continue;
- if (mp->magic == (magic & ~DYN_MAGIC)) {
- if(mp->magic == V_MAGIC)
- mp = couldbe4k(mp);
- hswal((long *) &d, sizeof(d.e)/sizeof(long), mp->swal);
- fp->type = mp->type;
- if ((magic & DYN_MAGIC) && mp->dlmname != nil)
- fp->name = mp->dlmname;
- else
- fp->name = mp->name;
- fp->hdrsz = mp->hsize; /* zero on bootables */
- mach = mp->mach;
- ret = mp->hparse(fd, fp, &d);
- seek(fd, mp->hsize, 0); /* seek to end of header */
- break;
- }
- }
- if(mp->magic == 0)
- werrstr("unknown header type");
- return ret;
- }
- /*
- * Convert header to canonical form
- */
- static void
- hswal(long *lp, int n, long (*swap) (long))
- {
- while (n--) {
- *lp = (*swap) (*lp);
- lp++;
- }
- }
- /*
- * Crack a normal a.out-type header
- */
- static int
- adotout(int fd, Fhdr *fp, ExecHdr *hp)
- {
- long pgsize;
- USED(fd);
- pgsize = mach->pgsize;
- settext(fp, hp->e.entry, pgsize+sizeof(Exec),
- hp->e.text, sizeof(Exec));
- setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize),
- hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
- setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
- return 1;
- }
- /*
- * 68020 2.out and 68020 bootable images
- * 386I 8.out and 386I bootable images
- * alpha plan9-style bootable images for axp "headerless" boot
- *
- */
- static int
- common(int fd, Fhdr *fp, ExecHdr *hp)
- {
- long kbase;
- adotout(fd, fp, hp);
- if(hp->e.magic & DYN_MAGIC) {
- fp->txtaddr = 0;
- fp->dataddr = fp->txtsz;
- return 1;
- }
- kbase = mach->kbase;
- if ((fp->entry & kbase) == kbase) { /* Boot image */
- switch(fp->type) {
- case F68020:
- fp->type = F68020B;
- fp->name = "68020 plan 9 boot image";
- fp->hdrsz = 0; /* header stripped */
- break;
- case FI386:
- fp->type = FI386B;
- fp->txtaddr = sizeof(Exec);
- fp->name = "386 plan 9 boot image";
- fp->hdrsz = 0; /* header stripped */
- fp->dataddr = fp->txtaddr+fp->txtsz;
- break;
- case FARM:
- fp->txtaddr = kbase+0x8010;
- fp->name = "ARM plan 9 boot image";
- fp->hdrsz = 0; /* header stripped */
- fp->dataddr = fp->txtaddr+fp->txtsz;
- return 1;
- case FALPHA:
- fp->type = FALPHAB;
- fp->txtaddr = fp->entry;
- fp->name = "alpha plan 9 boot image?";
- fp->hdrsz = 0; /* header stripped */
- fp->dataddr = fp->txtaddr+fp->txtsz;
- break;
- case FPOWER:
- fp->type = FPOWERB;
- fp->txtaddr = fp->entry;
- fp->name = "power plan 9 boot image";
- fp->hdrsz = 0; /* header stripped */
- fp->dataddr = fp->txtaddr+fp->txtsz;
- break;
- default:
- break;
- }
- fp->txtaddr |= kbase;
- fp->entry |= kbase;
- fp->dataddr |= kbase;
- }
- return 1;
- }
- /*
- * mips bootable image.
- */
- static int
- mipsboot(int fd, Fhdr *fp, ExecHdr *hp)
- {
- USED(fd);
- switch(hp->e.amagic) {
- default:
- case 0407: /* some kind of mips */
- fp->type = FMIPSB;
- settext(fp, hp->e.mentry, hp->e.text_start, hp->e.tsize,
- sizeof(struct mipsexec)+4);
- setdata(fp, hp->e.data_start, hp->e.dsize,
- fp->txtoff+hp->e.tsize, hp->e.bsize);
- break;
- case 0413: /* some kind of mips */
- fp->type = FMIPSB;
- settext(fp, hp->e.mentry, hp->e.text_start, hp->e.tsize, 0);
- setdata(fp, hp->e.data_start, hp->e.dsize, hp->e.tsize,
- hp->e.bsize);
- break;
- }
- setsym(fp, hp->e.nsyms, 0, hp->e.pcsize, hp->e.symptr);
- fp->hdrsz = 0; /* header stripped */
- return 1;
- }
- /*
- * mips4k bootable image.
- */
- static int
- mips4kboot(int fd, Fhdr *fp, ExecHdr *hp)
- {
- USED(fd);
- switch(hp->e.h.amagic) {
- default:
- case 0407: /* some kind of mips */
- fp->type = FMIPSB;
- settext(fp, hp->e.h.mentry, hp->e.h.text_start, hp->e.h.tsize,
- sizeof(struct mips4kexec));
- setdata(fp, hp->e.h.data_start, hp->e.h.dsize,
- fp->txtoff+hp->e.h.tsize, hp->e.h.bsize);
- break;
- case 0413: /* some kind of mips */
- fp->type = FMIPSB;
- settext(fp, hp->e.h.mentry, hp->e.h.text_start, hp->e.h.tsize, 0);
- setdata(fp, hp->e.h.data_start, hp->e.h.dsize, hp->e.h.tsize,
- hp->e.h.bsize);
- break;
- }
- setsym(fp, hp->e.h.nsyms, 0, hp->e.h.pcsize, hp->e.h.symptr);
- fp->hdrsz = 0; /* header stripped */
- return 1;
- }
- /*
- * sparc bootable image
- */
- static int
- sparcboot(int fd, Fhdr *fp, ExecHdr *hp)
- {
- USED(fd);
- fp->type = FSPARCB;
- settext(fp, hp->e.sentry, hp->e.sentry, hp->e.stext,
- sizeof(struct sparcexec));
- setdata(fp, hp->e.sentry+hp->e.stext, hp->e.sdata,
- fp->txtoff+hp->e.stext, hp->e.sbss);
- setsym(fp, hp->e.ssyms, 0, hp->e.sdrsize, fp->datoff+hp->e.sdata);
- fp->hdrsz = 0; /* header stripped */
- return 1;
- }
- /*
- * next bootable image
- */
- static int
- nextboot(int fd, Fhdr *fp, ExecHdr *hp)
- {
- USED(fd);
- fp->type = FNEXTB;
- settext(fp, hp->e.textc.vmaddr, hp->e.textc.vmaddr,
- hp->e.texts.size, hp->e.texts.offset);
- setdata(fp, hp->e.datac.vmaddr, hp->e.datas.size,
- hp->e.datas.offset, hp->e.bsss.size);
- setsym(fp, hp->e.symc.nsyms, hp->e.symc.spoff, hp->e.symc.pcoff,
- hp->e.symc.symoff);
- fp->hdrsz = 0; /* header stripped */
- return 1;
- }
- static Shdr*
- elfsectbyname(int fd, Ehdr *hp, Shdr *sp, char *name)
- {
- int i, offset, n;
- char s[64];
- offset = sp[hp->shstrndx].offset;
- for(i = 1; i < hp->shnum; i++) {
- seek(fd, offset+sp[i].name, 0);
- n = read(fd, s, sizeof(s)-1);
- if(n < 0)
- continue;
- s[n] = 0;
- if(strcmp(s, name) == 0)
- return &sp[i];
- }
- return 0;
- }
- /*
- * Decode an Irix 5.x ELF header
- */
- static int
- elfdotout(int fd, Fhdr *fp, ExecHdr *hp)
- {
- Ehdr *ep;
- Shdr *es, *txt, *init, *s;
- long addr, size, offset, bsize;
- ep = &hp->e;
- if(ep->type != 8 || ep->machine != 2 || ep->version != 1)
- return 0;
- fp->magic = ELF_MAG;
- fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
- if(ep->shnum <= 0) {
- werrstr("no ELF header sections");
- return 0;
- }
- es = malloc(sizeof(Shdr)*ep->shnum);
- if(es == 0)
- return 0;
- seek(fd, ep->shoff, 0);
- if(read(fd, es, sizeof(Shdr)*ep->shnum) < 0){
- free(es);
- return 0;
- }
- txt = elfsectbyname(fd, ep, es, ".text");
- init = elfsectbyname(fd, ep, es, ".init");
- if(txt == 0 || init == 0 || init != txt+1)
- goto bad;
- if(txt->addr+txt->size != init->addr)
- goto bad;
- settext(fp, ep->elfentry, txt->addr, txt->size+init->size, txt->offset);
- addr = 0;
- offset = 0;
- size = 0;
- s = elfsectbyname(fd, ep, es, ".data");
- if(s) {
- addr = s->addr;
- size = s->size;
- offset = s->offset;
- }
- s = elfsectbyname(fd, ep, es, ".rodata");
- if(s) {
- if(addr){
- if(addr+size != s->addr)
- goto bad;
- } else {
- addr = s->addr;
- offset = s->offset;
- }
- size += s->size;
- }
- s = elfsectbyname(fd, ep, es, ".got");
- if(s) {
- if(addr){
- if(addr+size != s->addr)
- goto bad;
- } else {
- addr = s->addr;
- offset = s->offset;
- }
- size += s->size;
- }
- bsize = 0;
- s = elfsectbyname(fd, ep, es, ".bss");
- if(s) {
- if(addr){
- if(addr+size != s->addr)
- goto bad;
- } else {
- addr = s->addr;
- offset = s->offset;
- }
- bsize = s->size;
- }
- if(addr == 0)
- goto bad;
- setdata(fp, addr, size, offset, bsize);
- fp->name = "IRIX Elf a.out executable";
- free(es);
- return 1;
- bad:
- free(es);
- werrstr("ELF sections scrambled");
- return 0;
- }
- /*
- * alpha bootable
- */
- static int
- alphadotout(int fd, Fhdr *fp, ExecHdr *hp)
- {
- long kbase;
- USED(fd);
- settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
- setdata(fp, fp->txtsz+sizeof(Exec), hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
- setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
- /*
- * Boot images have some of bits <31:28> set:
- * 0x80400000 kernel
- * 0x20000000 secondary bootstrap
- */
- kbase = 0xF0000000;
- if (fp->entry & kbase) {
- fp->txtaddr = fp->entry;
- fp->name = "alpha plan 9 boot image";
- fp->hdrsz = 0; /* header stripped */
- fp->dataddr = fp->entry+fp->txtsz;
- }
- return 1;
- }
- /*
- * (Free|Net)BSD ARM header.
- */
- static int
- armdotout(int fd, Fhdr *fp, ExecHdr *hp)
- {
- long kbase;
- USED(fd);
- settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
- setdata(fp, fp->txtsz, hp->e.data, fp->txtsz, hp->e.bss);
- setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
- kbase = 0xF0000000;
- if ((fp->entry & kbase) == kbase) { /* Boot image */
- fp->txtaddr = kbase+sizeof(Exec);
- fp->name = "ARM *BSD boot image";
- fp->hdrsz = 0; /* header stripped */
- fp->dataddr = kbase+fp->txtsz;
- }
- return 1;
- }
- static void
- settext(Fhdr *fp, long e, long a, long s, long off)
- {
- fp->txtaddr = a;
- fp->entry = e;
- fp->txtsz = s;
- fp->txtoff = off;
- }
- static void
- setdata(Fhdr *fp, long a, long s, long off, long bss)
- {
- fp->dataddr = a;
- fp->datsz = s;
- fp->datoff = off;
- fp->bsssz = bss;
- }
- static void
- setsym(Fhdr *fp, long sy, long sppc, long lnpc, long symoff)
- {
- fp->symsz = sy;
- fp->symoff = symoff;
- fp->sppcsz = sppc;
- fp->sppcoff = fp->symoff+fp->symsz;
- fp->lnpcsz = lnpc;
- fp->lnpcoff = fp->sppcoff+fp->sppcsz;
- }
- static long
- _round(long a, long b)
- {
- long w;
- w = (a/b)*b;
- if (a!=w)
- w += b;
- return(w);
- }
|