executable.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <bootexec.h>
  5. #include <mach.h>
  6. #include "elf.h"
  7. /*
  8. * All a.out header types. The dummy entry allows canonical
  9. * processing of the union as a sequence of longs
  10. */
  11. typedef struct {
  12. union{
  13. Exec; /* in a.out.h */
  14. Ehdr; /* in elf.h */
  15. struct mipsexec;
  16. struct mips4kexec;
  17. struct sparcexec;
  18. struct nextexec;
  19. } e;
  20. long dummy; /* padding to ensure extra long */
  21. } ExecHdr;
  22. static int nextboot(int, Fhdr*, ExecHdr*);
  23. static int sparcboot(int, Fhdr*, ExecHdr*);
  24. static int mipsboot(int, Fhdr*, ExecHdr*);
  25. static int mips4kboot(int, Fhdr*, ExecHdr*);
  26. static int common(int, Fhdr*, ExecHdr*);
  27. static int adotout(int, Fhdr*, ExecHdr*);
  28. static int elfdotout(int, Fhdr*, ExecHdr*);
  29. static int armdotout(int, Fhdr*, ExecHdr*);
  30. static int alphadotout(int, Fhdr*, ExecHdr*);
  31. static void setsym(Fhdr*, long, long, long, long);
  32. static void setdata(Fhdr*, long, long, long, long);
  33. static void settext(Fhdr*, long, long, long, long);
  34. static void hswal(long*, int, long(*)(long));
  35. static long _round(long, long);
  36. /*
  37. * definition of per-executable file type structures
  38. */
  39. typedef struct Exectable{
  40. long magic; /* big-endian magic number of file */
  41. char *name; /* executable identifier */
  42. char *dlmname; /* dynamically loadable module identifier */
  43. int type; /* Internal code */
  44. Mach *mach; /* Per-machine data */
  45. ulong hsize; /* header size */
  46. long (*swal)(long); /* beswal or leswal */
  47. int (*hparse)(int, Fhdr*, ExecHdr*);
  48. } ExecTable;
  49. extern Mach mmips;
  50. extern Mach mmips2le;
  51. extern Mach mmips2be;
  52. extern Mach msparc;
  53. extern Mach m68020;
  54. extern Mach mi386;
  55. extern Mach marm;
  56. extern Mach mpower;
  57. extern Mach malpha;
  58. ExecTable exectab[] =
  59. {
  60. { V_MAGIC, /* Mips v.out */
  61. "mips plan 9 executable",
  62. "mips plan 9 dlm",
  63. FMIPS,
  64. &mmips,
  65. sizeof(Exec),
  66. beswal,
  67. adotout },
  68. { M_MAGIC, /* Mips 4.out */
  69. "mips 4k plan 9 executable BE",
  70. "mips 4k plan 9 dlm BE",
  71. FMIPS2BE,
  72. &mmips2be,
  73. sizeof(Exec),
  74. beswal,
  75. adotout },
  76. { N_MAGIC, /* Mips 0.out */
  77. "mips 4k plan 9 executable LE",
  78. "mips 4k plan 9 dlm LE",
  79. FMIPS2LE,
  80. &mmips2le,
  81. sizeof(Exec),
  82. beswal,
  83. adotout },
  84. { 0x160<<16, /* Mips boot image */
  85. "mips plan 9 boot image",
  86. nil,
  87. FMIPSB,
  88. &mmips,
  89. sizeof(struct mipsexec),
  90. beswal,
  91. mipsboot },
  92. { (0x160<<16)|3, /* Mips boot image */
  93. "mips 4k plan 9 boot image",
  94. nil,
  95. FMIPSB,
  96. &mmips2be,
  97. sizeof(struct mips4kexec),
  98. beswal,
  99. mips4kboot },
  100. { K_MAGIC, /* Sparc k.out */
  101. "sparc plan 9 executable",
  102. "sparc plan 9 dlm",
  103. FSPARC,
  104. &msparc,
  105. sizeof(Exec),
  106. beswal,
  107. adotout },
  108. { 0x01030107, /* Sparc boot image */
  109. "sparc plan 9 boot image",
  110. nil,
  111. FSPARCB,
  112. &msparc,
  113. sizeof(struct sparcexec),
  114. beswal,
  115. sparcboot },
  116. { A_MAGIC, /* 68020 2.out & boot image */
  117. "68020 plan 9 executable",
  118. "68020 plan 9 dlm",
  119. F68020,
  120. &m68020,
  121. sizeof(Exec),
  122. beswal,
  123. common },
  124. { 0xFEEDFACE, /* Next boot image */
  125. "next plan 9 boot image",
  126. nil,
  127. FNEXTB,
  128. &m68020,
  129. sizeof(struct nextexec),
  130. beswal,
  131. nextboot },
  132. { I_MAGIC, /* I386 8.out & boot image */
  133. "386 plan 9 executable",
  134. "386 plan 9 dlm",
  135. FI386,
  136. &mi386,
  137. sizeof(Exec),
  138. beswal,
  139. common },
  140. { Q_MAGIC, /* PowerPC q.out & boot image */
  141. "power plan 9 executable",
  142. "power plan 9 dlm",
  143. FPOWER,
  144. &mpower,
  145. sizeof(Exec),
  146. beswal,
  147. common },
  148. { ELF_MAG,
  149. "Irix 5.X Elf executable",
  150. nil,
  151. FMIPS,
  152. &mmips,
  153. sizeof(Ehdr),
  154. beswal,
  155. elfdotout },
  156. { E_MAGIC, /* Arm 5.out */
  157. "Arm plan 9 executable",
  158. "Arm plan 9 dlm",
  159. FARM,
  160. &marm,
  161. sizeof(Exec),
  162. beswal,
  163. common },
  164. { (143<<16)|0413, /* (Free|Net)BSD Arm */
  165. "Arm *BSD executable",
  166. nil,
  167. FARM,
  168. &marm,
  169. sizeof(Exec),
  170. leswal,
  171. armdotout },
  172. { L_MAGIC, /* alpha 7.out */
  173. "alpha plan 9 executable",
  174. "alpha plan 9 dlm",
  175. FALPHA,
  176. &malpha,
  177. sizeof(Exec),
  178. beswal,
  179. common },
  180. { 0x0700e0c3, /* alpha boot image */
  181. "alpha plan 9 boot image",
  182. nil,
  183. FALPHAB,
  184. &malpha,
  185. sizeof(Exec),
  186. beswal,
  187. alphadotout },
  188. { 0 },
  189. };
  190. Mach *mach = &mi386; /* Global current machine table */
  191. static ExecTable*
  192. couldbe4k(ExecTable *mp)
  193. {
  194. Dir *d;
  195. ExecTable *f;
  196. if((d=dirstat("/proc/1/regs")) == nil)
  197. return mp;
  198. if(d->length < 32*8){ /* R3000 */
  199. free(d);
  200. return mp;
  201. }
  202. free(d);
  203. for (f = exectab; f->magic; f++)
  204. if(f->magic == M_MAGIC) {
  205. f->name = "mips plan 9 executable on mips2 kernel";
  206. return f;
  207. }
  208. return mp;
  209. }
  210. int
  211. crackhdr(int fd, Fhdr *fp)
  212. {
  213. ExecTable *mp;
  214. ExecHdr d;
  215. int nb, magic, ret;
  216. fp->type = FNONE;
  217. nb = read(fd, (char *)&d.e, sizeof(d.e));
  218. if (nb <= 0)
  219. return 0;
  220. ret = 0;
  221. fp->magic = magic = beswal(d.e.magic); /* big-endian */
  222. for (mp = exectab; mp->magic; mp++) {
  223. if (nb < mp->hsize)
  224. continue;
  225. if (mp->magic == (magic & ~DYN_MAGIC)) {
  226. if(mp->magic == V_MAGIC)
  227. mp = couldbe4k(mp);
  228. hswal((long *) &d, sizeof(d.e)/sizeof(long), mp->swal);
  229. fp->type = mp->type;
  230. if ((magic & DYN_MAGIC) && mp->dlmname != nil)
  231. fp->name = mp->dlmname;
  232. else
  233. fp->name = mp->name;
  234. fp->hdrsz = mp->hsize; /* zero on bootables */
  235. mach = mp->mach;
  236. ret = mp->hparse(fd, fp, &d);
  237. seek(fd, mp->hsize, 0); /* seek to end of header */
  238. break;
  239. }
  240. }
  241. if(mp->magic == 0)
  242. werrstr("unknown header type");
  243. return ret;
  244. }
  245. /*
  246. * Convert header to canonical form
  247. */
  248. static void
  249. hswal(long *lp, int n, long (*swap) (long))
  250. {
  251. while (n--) {
  252. *lp = (*swap) (*lp);
  253. lp++;
  254. }
  255. }
  256. /*
  257. * Crack a normal a.out-type header
  258. */
  259. static int
  260. adotout(int fd, Fhdr *fp, ExecHdr *hp)
  261. {
  262. long pgsize;
  263. USED(fd);
  264. pgsize = mach->pgsize;
  265. settext(fp, hp->e.entry, pgsize+sizeof(Exec),
  266. hp->e.text, sizeof(Exec));
  267. setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize),
  268. hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
  269. setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
  270. return 1;
  271. }
  272. /*
  273. * 68020 2.out and 68020 bootable images
  274. * 386I 8.out and 386I bootable images
  275. * alpha plan9-style bootable images for axp "headerless" boot
  276. *
  277. */
  278. static int
  279. common(int fd, Fhdr *fp, ExecHdr *hp)
  280. {
  281. long kbase;
  282. adotout(fd, fp, hp);
  283. if(hp->e.magic & DYN_MAGIC) {
  284. fp->txtaddr = 0;
  285. fp->dataddr = fp->txtsz;
  286. return 1;
  287. }
  288. kbase = mach->kbase;
  289. if ((fp->entry & kbase) == kbase) { /* Boot image */
  290. switch(fp->type) {
  291. case F68020:
  292. fp->type = F68020B;
  293. fp->name = "68020 plan 9 boot image";
  294. fp->hdrsz = 0; /* header stripped */
  295. break;
  296. case FI386:
  297. fp->type = FI386B;
  298. fp->txtaddr = sizeof(Exec);
  299. fp->name = "386 plan 9 boot image";
  300. fp->hdrsz = 0; /* header stripped */
  301. fp->dataddr = fp->txtaddr+fp->txtsz;
  302. break;
  303. case FARM:
  304. fp->txtaddr = kbase+0x8010;
  305. fp->name = "ARM plan 9 boot image";
  306. fp->hdrsz = 0; /* header stripped */
  307. fp->dataddr = fp->txtaddr+fp->txtsz;
  308. return 1;
  309. case FALPHA:
  310. fp->type = FALPHAB;
  311. fp->txtaddr = fp->entry;
  312. fp->name = "alpha plan 9 boot image?";
  313. fp->hdrsz = 0; /* header stripped */
  314. fp->dataddr = fp->txtaddr+fp->txtsz;
  315. break;
  316. case FPOWER:
  317. fp->type = FPOWERB;
  318. fp->txtaddr = fp->entry;
  319. fp->name = "power plan 9 boot image";
  320. fp->hdrsz = 0; /* header stripped */
  321. fp->dataddr = fp->txtaddr+fp->txtsz;
  322. break;
  323. default:
  324. break;
  325. }
  326. fp->txtaddr |= kbase;
  327. fp->entry |= kbase;
  328. fp->dataddr |= kbase;
  329. }
  330. return 1;
  331. }
  332. /*
  333. * mips bootable image.
  334. */
  335. static int
  336. mipsboot(int fd, Fhdr *fp, ExecHdr *hp)
  337. {
  338. USED(fd);
  339. switch(hp->e.amagic) {
  340. default:
  341. case 0407: /* some kind of mips */
  342. fp->type = FMIPSB;
  343. settext(fp, hp->e.mentry, hp->e.text_start, hp->e.tsize,
  344. sizeof(struct mipsexec)+4);
  345. setdata(fp, hp->e.data_start, hp->e.dsize,
  346. fp->txtoff+hp->e.tsize, hp->e.bsize);
  347. break;
  348. case 0413: /* some kind of mips */
  349. fp->type = FMIPSB;
  350. settext(fp, hp->e.mentry, hp->e.text_start, hp->e.tsize, 0);
  351. setdata(fp, hp->e.data_start, hp->e.dsize, hp->e.tsize,
  352. hp->e.bsize);
  353. break;
  354. }
  355. setsym(fp, hp->e.nsyms, 0, hp->e.pcsize, hp->e.symptr);
  356. fp->hdrsz = 0; /* header stripped */
  357. return 1;
  358. }
  359. /*
  360. * mips4k bootable image.
  361. */
  362. static int
  363. mips4kboot(int fd, Fhdr *fp, ExecHdr *hp)
  364. {
  365. USED(fd);
  366. switch(hp->e.h.amagic) {
  367. default:
  368. case 0407: /* some kind of mips */
  369. fp->type = FMIPSB;
  370. settext(fp, hp->e.h.mentry, hp->e.h.text_start, hp->e.h.tsize,
  371. sizeof(struct mips4kexec));
  372. setdata(fp, hp->e.h.data_start, hp->e.h.dsize,
  373. fp->txtoff+hp->e.h.tsize, hp->e.h.bsize);
  374. break;
  375. case 0413: /* some kind of mips */
  376. fp->type = FMIPSB;
  377. settext(fp, hp->e.h.mentry, hp->e.h.text_start, hp->e.h.tsize, 0);
  378. setdata(fp, hp->e.h.data_start, hp->e.h.dsize, hp->e.h.tsize,
  379. hp->e.h.bsize);
  380. break;
  381. }
  382. setsym(fp, hp->e.h.nsyms, 0, hp->e.h.pcsize, hp->e.h.symptr);
  383. fp->hdrsz = 0; /* header stripped */
  384. return 1;
  385. }
  386. /*
  387. * sparc bootable image
  388. */
  389. static int
  390. sparcboot(int fd, Fhdr *fp, ExecHdr *hp)
  391. {
  392. USED(fd);
  393. fp->type = FSPARCB;
  394. settext(fp, hp->e.sentry, hp->e.sentry, hp->e.stext,
  395. sizeof(struct sparcexec));
  396. setdata(fp, hp->e.sentry+hp->e.stext, hp->e.sdata,
  397. fp->txtoff+hp->e.stext, hp->e.sbss);
  398. setsym(fp, hp->e.ssyms, 0, hp->e.sdrsize, fp->datoff+hp->e.sdata);
  399. fp->hdrsz = 0; /* header stripped */
  400. return 1;
  401. }
  402. /*
  403. * next bootable image
  404. */
  405. static int
  406. nextboot(int fd, Fhdr *fp, ExecHdr *hp)
  407. {
  408. USED(fd);
  409. fp->type = FNEXTB;
  410. settext(fp, hp->e.textc.vmaddr, hp->e.textc.vmaddr,
  411. hp->e.texts.size, hp->e.texts.offset);
  412. setdata(fp, hp->e.datac.vmaddr, hp->e.datas.size,
  413. hp->e.datas.offset, hp->e.bsss.size);
  414. setsym(fp, hp->e.symc.nsyms, hp->e.symc.spoff, hp->e.symc.pcoff,
  415. hp->e.symc.symoff);
  416. fp->hdrsz = 0; /* header stripped */
  417. return 1;
  418. }
  419. static Shdr*
  420. elfsectbyname(int fd, Ehdr *hp, Shdr *sp, char *name)
  421. {
  422. int i, offset, n;
  423. char s[64];
  424. offset = sp[hp->shstrndx].offset;
  425. for(i = 1; i < hp->shnum; i++) {
  426. seek(fd, offset+sp[i].name, 0);
  427. n = read(fd, s, sizeof(s)-1);
  428. if(n < 0)
  429. continue;
  430. s[n] = 0;
  431. if(strcmp(s, name) == 0)
  432. return &sp[i];
  433. }
  434. return 0;
  435. }
  436. /*
  437. * Decode an Irix 5.x ELF header
  438. */
  439. static int
  440. elfdotout(int fd, Fhdr *fp, ExecHdr *hp)
  441. {
  442. Ehdr *ep;
  443. Shdr *es, *txt, *init, *s;
  444. long addr, size, offset, bsize;
  445. ep = &hp->e;
  446. if(ep->type != 8 || ep->machine != 2 || ep->version != 1)
  447. return 0;
  448. fp->magic = ELF_MAG;
  449. fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
  450. if(ep->shnum <= 0) {
  451. werrstr("no ELF header sections");
  452. return 0;
  453. }
  454. es = malloc(sizeof(Shdr)*ep->shnum);
  455. if(es == 0)
  456. return 0;
  457. seek(fd, ep->shoff, 0);
  458. if(read(fd, es, sizeof(Shdr)*ep->shnum) < 0){
  459. free(es);
  460. return 0;
  461. }
  462. txt = elfsectbyname(fd, ep, es, ".text");
  463. init = elfsectbyname(fd, ep, es, ".init");
  464. if(txt == 0 || init == 0 || init != txt+1)
  465. goto bad;
  466. if(txt->addr+txt->size != init->addr)
  467. goto bad;
  468. settext(fp, ep->elfentry, txt->addr, txt->size+init->size, txt->offset);
  469. addr = 0;
  470. offset = 0;
  471. size = 0;
  472. s = elfsectbyname(fd, ep, es, ".data");
  473. if(s) {
  474. addr = s->addr;
  475. size = s->size;
  476. offset = s->offset;
  477. }
  478. s = elfsectbyname(fd, ep, es, ".rodata");
  479. if(s) {
  480. if(addr){
  481. if(addr+size != s->addr)
  482. goto bad;
  483. } else {
  484. addr = s->addr;
  485. offset = s->offset;
  486. }
  487. size += s->size;
  488. }
  489. s = elfsectbyname(fd, ep, es, ".got");
  490. if(s) {
  491. if(addr){
  492. if(addr+size != s->addr)
  493. goto bad;
  494. } else {
  495. addr = s->addr;
  496. offset = s->offset;
  497. }
  498. size += s->size;
  499. }
  500. bsize = 0;
  501. s = elfsectbyname(fd, ep, es, ".bss");
  502. if(s) {
  503. if(addr){
  504. if(addr+size != s->addr)
  505. goto bad;
  506. } else {
  507. addr = s->addr;
  508. offset = s->offset;
  509. }
  510. bsize = s->size;
  511. }
  512. if(addr == 0)
  513. goto bad;
  514. setdata(fp, addr, size, offset, bsize);
  515. fp->name = "IRIX Elf a.out executable";
  516. free(es);
  517. return 1;
  518. bad:
  519. free(es);
  520. werrstr("ELF sections scrambled");
  521. return 0;
  522. }
  523. /*
  524. * alpha bootable
  525. */
  526. static int
  527. alphadotout(int fd, Fhdr *fp, ExecHdr *hp)
  528. {
  529. long kbase;
  530. USED(fd);
  531. settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
  532. setdata(fp, fp->txtsz+sizeof(Exec), hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
  533. setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
  534. /*
  535. * Boot images have some of bits <31:28> set:
  536. * 0x80400000 kernel
  537. * 0x20000000 secondary bootstrap
  538. */
  539. kbase = 0xF0000000;
  540. if (fp->entry & kbase) {
  541. fp->txtaddr = fp->entry;
  542. fp->name = "alpha plan 9 boot image";
  543. fp->hdrsz = 0; /* header stripped */
  544. fp->dataddr = fp->entry+fp->txtsz;
  545. }
  546. return 1;
  547. }
  548. /*
  549. * (Free|Net)BSD ARM header.
  550. */
  551. static int
  552. armdotout(int fd, Fhdr *fp, ExecHdr *hp)
  553. {
  554. long kbase;
  555. USED(fd);
  556. settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
  557. setdata(fp, fp->txtsz, hp->e.data, fp->txtsz, hp->e.bss);
  558. setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
  559. kbase = 0xF0000000;
  560. if ((fp->entry & kbase) == kbase) { /* Boot image */
  561. fp->txtaddr = kbase+sizeof(Exec);
  562. fp->name = "ARM *BSD boot image";
  563. fp->hdrsz = 0; /* header stripped */
  564. fp->dataddr = kbase+fp->txtsz;
  565. }
  566. return 1;
  567. }
  568. static void
  569. settext(Fhdr *fp, long e, long a, long s, long off)
  570. {
  571. fp->txtaddr = a;
  572. fp->entry = e;
  573. fp->txtsz = s;
  574. fp->txtoff = off;
  575. }
  576. static void
  577. setdata(Fhdr *fp, long a, long s, long off, long bss)
  578. {
  579. fp->dataddr = a;
  580. fp->datsz = s;
  581. fp->datoff = off;
  582. fp->bsssz = bss;
  583. }
  584. static void
  585. setsym(Fhdr *fp, long sy, long sppc, long lnpc, long symoff)
  586. {
  587. fp->symsz = sy;
  588. fp->symoff = symoff;
  589. fp->sppcsz = sppc;
  590. fp->sppcoff = fp->symoff+fp->symsz;
  591. fp->lnpcsz = lnpc;
  592. fp->lnpcoff = fp->sppcoff+fp->sppcsz;
  593. }
  594. static long
  595. _round(long a, long b)
  596. {
  597. long w;
  598. w = (a/b)*b;
  599. if (a!=w)
  600. w += b;
  601. return(w);
  602. }