executable.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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. struct {
  14. Exec; /* a.out.h */
  15. uvlong hdr[1];
  16. };
  17. Ehdr; /* elf.h */
  18. E64hdr;
  19. struct mipsexec; /* bootexec.h */
  20. struct mips4kexec; /* bootexec.h */
  21. struct sparcexec; /* bootexec.h */
  22. struct nextexec; /* bootexec.h */
  23. } e;
  24. long dummy; /* padding to ensure extra long */
  25. } ExecHdr;
  26. static int nextboot(int, Fhdr*, ExecHdr*);
  27. static int sparcboot(int, Fhdr*, ExecHdr*);
  28. static int mipsboot(int, Fhdr*, ExecHdr*);
  29. static int mips4kboot(int, Fhdr*, ExecHdr*);
  30. static int common(int, Fhdr*, ExecHdr*);
  31. static int commonllp64(int, Fhdr*, ExecHdr*);
  32. static int adotout(int, Fhdr*, ExecHdr*);
  33. static int elfdotout(int, Fhdr*, ExecHdr*);
  34. static int armdotout(int, Fhdr*, ExecHdr*);
  35. static void setsym(Fhdr*, long, long, long, vlong);
  36. static void setdata(Fhdr*, uvlong, long, vlong, long);
  37. static void settext(Fhdr*, uvlong, uvlong, long, vlong);
  38. static void hswal(void*, int, ulong(*)(ulong));
  39. static uvlong _round(uvlong, ulong);
  40. /*
  41. * definition of per-executable file type structures
  42. */
  43. typedef struct Exectable{
  44. long magic; /* big-endian magic number of file */
  45. char *name; /* executable identifier */
  46. char *dlmname; /* dynamically loadable module identifier */
  47. uchar type; /* Internal code */
  48. uchar _magic; /* _MAGIC() magic */
  49. Mach *mach; /* Per-machine data */
  50. long hsize; /* header size */
  51. ulong (*swal)(ulong); /* beswal or leswal */
  52. int (*hparse)(int, Fhdr*, ExecHdr*);
  53. } ExecTable;
  54. extern Mach mmips;
  55. extern Mach mmips2le;
  56. extern Mach mmips2be;
  57. extern Mach msparc;
  58. extern Mach msparc64;
  59. extern Mach m68020;
  60. extern Mach mi386;
  61. extern Mach mamd64;
  62. extern Mach marm;
  63. extern Mach mpower;
  64. extern Mach mpower64;
  65. extern Mach malpha;
  66. ExecTable exectab[] =
  67. {
  68. { V_MAGIC, /* Mips v.out */
  69. "mips plan 9 executable BE",
  70. "mips plan 9 dlm BE",
  71. FMIPS,
  72. 1,
  73. &mmips,
  74. sizeof(Exec),
  75. beswal,
  76. adotout },
  77. { P_MAGIC, /* Mips 0.out (r3k le) */
  78. "mips plan 9 executable LE",
  79. "mips plan 9 dlm LE",
  80. FMIPSLE,
  81. 1,
  82. &mmips,
  83. sizeof(Exec),
  84. beswal,
  85. adotout },
  86. { M_MAGIC, /* Mips 4.out */
  87. "mips 4k plan 9 executable BE",
  88. "mips 4k plan 9 dlm BE",
  89. FMIPS2BE,
  90. 1,
  91. &mmips2be,
  92. sizeof(Exec),
  93. beswal,
  94. adotout },
  95. { N_MAGIC, /* Mips 0.out */
  96. "mips 4k plan 9 executable LE",
  97. "mips 4k plan 9 dlm LE",
  98. FMIPS2LE,
  99. 1,
  100. &mmips2le,
  101. sizeof(Exec),
  102. beswal,
  103. adotout },
  104. { 0x160<<16, /* Mips boot image */
  105. "mips plan 9 boot image",
  106. nil,
  107. FMIPSB,
  108. 0,
  109. &mmips,
  110. sizeof(struct mipsexec),
  111. beswal,
  112. mipsboot },
  113. { (0x160<<16)|3, /* Mips boot image */
  114. "mips 4k plan 9 boot image",
  115. nil,
  116. FMIPSB,
  117. 0,
  118. &mmips2be,
  119. sizeof(struct mips4kexec),
  120. beswal,
  121. mips4kboot },
  122. { K_MAGIC, /* Sparc k.out */
  123. "sparc plan 9 executable",
  124. "sparc plan 9 dlm",
  125. FSPARC,
  126. 1,
  127. &msparc,
  128. sizeof(Exec),
  129. beswal,
  130. adotout },
  131. { 0x01030107, /* Sparc boot image */
  132. "sparc plan 9 boot image",
  133. nil,
  134. FSPARCB,
  135. 0,
  136. &msparc,
  137. sizeof(struct sparcexec),
  138. beswal,
  139. sparcboot },
  140. { U_MAGIC, /* Sparc64 u.out */
  141. "sparc64 plan 9 executable",
  142. "sparc64 plan 9 dlm",
  143. FSPARC64,
  144. 1,
  145. &msparc64,
  146. sizeof(Exec),
  147. beswal,
  148. adotout },
  149. { A_MAGIC, /* 68020 2.out & boot image */
  150. "68020 plan 9 executable",
  151. "68020 plan 9 dlm",
  152. F68020,
  153. 1,
  154. &m68020,
  155. sizeof(Exec),
  156. beswal,
  157. common },
  158. { 0xFEEDFACE, /* Next boot image */
  159. "next plan 9 boot image",
  160. nil,
  161. FNEXTB,
  162. 0,
  163. &m68020,
  164. sizeof(struct nextexec),
  165. beswal,
  166. nextboot },
  167. { I_MAGIC, /* I386 8.out & boot image */
  168. "386 plan 9 executable",
  169. "386 plan 9 dlm",
  170. FI386,
  171. 1,
  172. &mi386,
  173. sizeof(Exec),
  174. beswal,
  175. common },
  176. { S_MAGIC, /* amd64 6.out & boot image */
  177. "amd64 plan 9 executable",
  178. "amd64 plan 9 dlm",
  179. FAMD64,
  180. 1,
  181. &mamd64,
  182. sizeof(Exec)+8,
  183. nil,
  184. commonllp64 },
  185. { Q_MAGIC, /* PowerPC q.out & boot image */
  186. "power plan 9 executable",
  187. "power plan 9 dlm",
  188. FPOWER,
  189. 1,
  190. &mpower,
  191. sizeof(Exec),
  192. beswal,
  193. common },
  194. { T_MAGIC, /* power64 9.out & boot image */
  195. "power64 plan 9 executable",
  196. "power64 plan 9 dlm",
  197. FPOWER64,
  198. 1,
  199. &mpower64,
  200. sizeof(Exec)+8,
  201. nil,
  202. commonllp64 },
  203. { ELF_MAG, /* any ELF */
  204. "elf executable",
  205. nil,
  206. FNONE,
  207. 0,
  208. &mi386,
  209. sizeof(Ehdr),
  210. nil,
  211. elfdotout },
  212. { E_MAGIC, /* Arm 5.out and boot image */
  213. "arm plan 9 executable",
  214. "arm plan 9 dlm",
  215. FARM,
  216. 1,
  217. &marm,
  218. sizeof(Exec),
  219. beswal,
  220. common },
  221. { (143<<16)|0413, /* (Free|Net)BSD Arm */
  222. "arm *bsd executable",
  223. nil,
  224. FARM,
  225. 0,
  226. &marm,
  227. sizeof(Exec),
  228. leswal,
  229. armdotout },
  230. { L_MAGIC, /* alpha 7.out */
  231. "alpha plan 9 executable",
  232. "alpha plan 9 dlm",
  233. FALPHA,
  234. 1,
  235. &malpha,
  236. sizeof(Exec),
  237. beswal,
  238. common },
  239. { 0x0700e0c3, /* alpha boot image */
  240. "alpha plan 9 boot image",
  241. nil,
  242. FALPHA,
  243. 0,
  244. &malpha,
  245. sizeof(Exec),
  246. beswal,
  247. common },
  248. { 0 },
  249. };
  250. Mach *mach = &mi386; /* Global current machine table */
  251. static ExecTable*
  252. couldbe4k(ExecTable *mp)
  253. {
  254. Dir *d;
  255. ExecTable *f;
  256. if((d=dirstat("/proc/1/regs")) == nil)
  257. return mp;
  258. if(d->length < 32*8){ /* R3000 */
  259. free(d);
  260. return mp;
  261. }
  262. free(d);
  263. for (f = exectab; f->magic; f++)
  264. if(f->magic == M_MAGIC) {
  265. f->name = "mips plan 9 executable on mips2 kernel";
  266. return f;
  267. }
  268. return mp;
  269. }
  270. int
  271. crackhdr(int fd, Fhdr *fp)
  272. {
  273. ExecTable *mp;
  274. ExecHdr d;
  275. int nb, ret;
  276. ulong magic;
  277. fp->type = FNONE;
  278. nb = read(fd, (char *)&d.e, sizeof(d.e));
  279. if (nb <= 0)
  280. return 0;
  281. ret = 0;
  282. magic = beswal(d.e.magic); /* big-endian */
  283. for (mp = exectab; mp->magic; mp++) {
  284. if (nb < mp->hsize)
  285. continue;
  286. /*
  287. * The magic number has morphed into something
  288. * with fields (the straw was DYN_MAGIC) so now
  289. * a flag is needed in Fhdr to distinguish _MAGIC()
  290. * magic numbers from foreign magic numbers.
  291. *
  292. * This code is creaking a bit and if it has to
  293. * be modified/extended much more it's probably
  294. * time to step back and redo it all.
  295. */
  296. if(mp->_magic){
  297. if(mp->magic != (magic & ~DYN_MAGIC))
  298. continue;
  299. if(mp->magic == V_MAGIC)
  300. mp = couldbe4k(mp);
  301. if ((magic & DYN_MAGIC) && mp->dlmname != nil)
  302. fp->name = mp->dlmname;
  303. else
  304. fp->name = mp->name;
  305. }
  306. else{
  307. if(mp->magic != magic)
  308. continue;
  309. fp->name = mp->name;
  310. }
  311. fp->type = mp->type;
  312. fp->hdrsz = mp->hsize; /* will be zero on bootables */
  313. fp->_magic = mp->_magic;
  314. fp->magic = magic;
  315. mach = mp->mach;
  316. if(mp->swal != nil)
  317. hswal(&d, sizeof(d.e)/sizeof(ulong), mp->swal);
  318. ret = mp->hparse(fd, fp, &d);
  319. seek(fd, mp->hsize, 0); /* seek to end of header */
  320. break;
  321. }
  322. if(mp->magic == 0)
  323. werrstr("unknown header type");
  324. return ret;
  325. }
  326. /*
  327. * Convert header to canonical form
  328. */
  329. static void
  330. hswal(void *v, int n, ulong (*swap)(ulong))
  331. {
  332. ulong *ulp;
  333. for(ulp = v; n--; ulp++)
  334. *ulp = (*swap)(*ulp);
  335. }
  336. /*
  337. * Crack a normal a.out-type header
  338. */
  339. static int
  340. adotout(int fd, Fhdr *fp, ExecHdr *hp)
  341. {
  342. long pgsize;
  343. USED(fd);
  344. pgsize = mach->pgsize;
  345. settext(fp, hp->e.entry, pgsize+sizeof(Exec),
  346. hp->e.text, sizeof(Exec));
  347. setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize),
  348. hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
  349. setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
  350. return 1;
  351. }
  352. static void
  353. commonboot(Fhdr *fp)
  354. {
  355. if (!(fp->entry & mach->ktmask))
  356. return;
  357. switch(fp->type) { /* boot image */
  358. case F68020:
  359. fp->type = F68020B;
  360. fp->name = "68020 plan 9 boot image";
  361. break;
  362. case FI386:
  363. fp->type = FI386B;
  364. fp->txtaddr = (u32int)fp->entry;
  365. fp->name = "386 plan 9 boot image";
  366. fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
  367. break;
  368. case FARM:
  369. fp->type = FARMB;
  370. fp->txtaddr = (u32int)fp->entry;
  371. fp->name = "ARM plan 9 boot image";
  372. fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
  373. return;
  374. case FALPHA:
  375. fp->type = FALPHAB;
  376. fp->txtaddr = (u32int)fp->entry;
  377. fp->name = "alpha plan 9 boot image";
  378. fp->dataddr = fp->txtaddr+fp->txtsz;
  379. break;
  380. case FPOWER:
  381. fp->type = FPOWERB;
  382. fp->txtaddr = (u32int)fp->entry;
  383. fp->name = "power plan 9 boot image";
  384. fp->dataddr = fp->txtaddr+fp->txtsz;
  385. break;
  386. case FAMD64:
  387. fp->type = FAMD64B;
  388. fp->txtaddr = fp->entry;
  389. fp->name = "amd64 plan 9 boot image";
  390. fp->dataddr = _round(fp->txtaddr+fp->txtsz, 4096);
  391. break;
  392. case FPOWER64:
  393. fp->type = FPOWER64B;
  394. fp->txtaddr = fp->entry;
  395. fp->name = "power64 plan 9 boot image";
  396. fp->dataddr = fp->txtaddr+fp->txtsz;
  397. break;
  398. default:
  399. return;
  400. }
  401. fp->hdrsz = 0; /* header stripped */
  402. }
  403. /*
  404. * _MAGIC() style headers and
  405. * alpha plan9-style bootable images for axp "headerless" boot
  406. *
  407. */
  408. static int
  409. common(int fd, Fhdr *fp, ExecHdr *hp)
  410. {
  411. adotout(fd, fp, hp);
  412. if(hp->e.magic & DYN_MAGIC) {
  413. fp->txtaddr = 0;
  414. fp->dataddr = fp->txtsz;
  415. return 1;
  416. }
  417. commonboot(fp);
  418. return 1;
  419. }
  420. static int
  421. commonllp64(int, Fhdr *fp, ExecHdr *hp)
  422. {
  423. long pgsize;
  424. uvlong entry;
  425. hswal(&hp->e, sizeof(Exec)/sizeof(long), beswal);
  426. if(!(hp->e.magic & HDR_MAGIC))
  427. return 0;
  428. /*
  429. * There can be more magic here if the
  430. * header ever needs more expansion.
  431. * For now just catch use of any of the
  432. * unused bits.
  433. */
  434. if((hp->e.magic & ~DYN_MAGIC)>>16)
  435. return 0;
  436. entry = beswav(hp->e.hdr[0]);
  437. pgsize = mach->pgsize;
  438. settext(fp, entry, pgsize+fp->hdrsz, hp->e.text, fp->hdrsz);
  439. setdata(fp, _round(pgsize+fp->txtsz+fp->hdrsz, pgsize),
  440. hp->e.data, fp->txtsz+fp->hdrsz, hp->e.bss);
  441. setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
  442. if(hp->e.magic & DYN_MAGIC) {
  443. fp->txtaddr = 0;
  444. fp->dataddr = fp->txtsz;
  445. return 1;
  446. }
  447. commonboot(fp);
  448. return 1;
  449. }
  450. /*
  451. * mips bootable image.
  452. */
  453. static int
  454. mipsboot(int fd, Fhdr *fp, ExecHdr *hp)
  455. {
  456. USED(fd);
  457. fp->type = FMIPSB;
  458. switch(hp->e.amagic) {
  459. default:
  460. case 0407: /* some kind of mips */
  461. settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
  462. hp->e.tsize, sizeof(struct mipsexec)+4);
  463. setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
  464. fp->txtoff+hp->e.tsize, hp->e.bsize);
  465. break;
  466. case 0413: /* some kind of mips */
  467. settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
  468. hp->e.tsize, 0);
  469. setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
  470. hp->e.tsize, hp->e.bsize);
  471. break;
  472. }
  473. setsym(fp, hp->e.nsyms, 0, hp->e.pcsize, hp->e.symptr);
  474. fp->hdrsz = 0; /* header stripped */
  475. return 1;
  476. }
  477. /*
  478. * mips4k bootable image.
  479. */
  480. static int
  481. mips4kboot(int fd, Fhdr *fp, ExecHdr *hp)
  482. {
  483. USED(fd);
  484. fp->type = FMIPSB;
  485. switch(hp->e.h.amagic) {
  486. default:
  487. case 0407: /* some kind of mips */
  488. settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
  489. hp->e.h.tsize, sizeof(struct mips4kexec));
  490. setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
  491. fp->txtoff+hp->e.h.tsize, hp->e.h.bsize);
  492. break;
  493. case 0413: /* some kind of mips */
  494. settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
  495. hp->e.h.tsize, 0);
  496. setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
  497. hp->e.h.tsize, hp->e.h.bsize);
  498. break;
  499. }
  500. setsym(fp, hp->e.h.nsyms, 0, hp->e.h.pcsize, hp->e.h.symptr);
  501. fp->hdrsz = 0; /* header stripped */
  502. return 1;
  503. }
  504. /*
  505. * sparc bootable image
  506. */
  507. static int
  508. sparcboot(int fd, Fhdr *fp, ExecHdr *hp)
  509. {
  510. USED(fd);
  511. fp->type = FSPARCB;
  512. settext(fp, hp->e.sentry, hp->e.sentry, hp->e.stext,
  513. sizeof(struct sparcexec));
  514. setdata(fp, hp->e.sentry+hp->e.stext, hp->e.sdata,
  515. fp->txtoff+hp->e.stext, hp->e.sbss);
  516. setsym(fp, hp->e.ssyms, 0, hp->e.sdrsize, fp->datoff+hp->e.sdata);
  517. fp->hdrsz = 0; /* header stripped */
  518. return 1;
  519. }
  520. /*
  521. * next bootable image
  522. */
  523. static int
  524. nextboot(int fd, Fhdr *fp, ExecHdr *hp)
  525. {
  526. USED(fd);
  527. fp->type = FNEXTB;
  528. settext(fp, hp->e.textc.vmaddr, hp->e.textc.vmaddr,
  529. hp->e.texts.size, hp->e.texts.offset);
  530. setdata(fp, hp->e.datac.vmaddr, hp->e.datas.size,
  531. hp->e.datas.offset, hp->e.bsss.size);
  532. setsym(fp, hp->e.symc.nsyms, hp->e.symc.spoff, hp->e.symc.pcoff,
  533. hp->e.symc.symoff);
  534. fp->hdrsz = 0; /* header stripped */
  535. return 1;
  536. }
  537. /*
  538. * ELF64 binaries.
  539. */
  540. static int
  541. elf64dotout(int fd, Fhdr *fp, ExecHdr *hp)
  542. {
  543. E64hdr *ep;
  544. P64hdr *ph;
  545. ushort (*swab)(ushort);
  546. ulong (*swal)(ulong);
  547. uvlong (*swav)(uvlong);
  548. int i, it, id, is, phsz;
  549. uvlong uvl;
  550. ep = &hp->e;
  551. if(ep->ident[DATA] == ELFDATA2LSB) {
  552. swab = leswab;
  553. swal = leswal;
  554. swav = leswav;
  555. } else if(ep->ident[DATA] == ELFDATA2MSB) {
  556. swab = beswab;
  557. swal = beswal;
  558. swav = beswav;
  559. } else {
  560. werrstr("bad ELF64 encoding - not big or little endian");
  561. return 0;
  562. }
  563. ep->type = swab(ep->type);
  564. ep->machine = swab(ep->machine);
  565. ep->version = swal(ep->version);
  566. if(ep->type != EXEC || ep->version != CURRENT)
  567. return 0;
  568. ep->elfentry = swav(ep->elfentry);
  569. ep->phoff = swav(ep->phoff);
  570. ep->shoff = swav(ep->shoff);
  571. ep->flags = swal(ep->flags);
  572. ep->ehsize = swab(ep->ehsize);
  573. ep->phentsize = swab(ep->phentsize);
  574. ep->phnum = swab(ep->phnum);
  575. ep->shentsize = swab(ep->shentsize);
  576. ep->shnum = swab(ep->shnum);
  577. ep->shstrndx = swab(ep->shstrndx);
  578. fp->magic = ELF_MAG;
  579. fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
  580. switch(ep->machine) {
  581. default:
  582. return 0;
  583. case AMD64:
  584. mach = &mamd64;
  585. fp->type = FAMD64;
  586. fp->name = "amd64 ELF64 executable";
  587. break;
  588. case POWER64:
  589. mach = &mpower64;
  590. fp->type = FPOWER64;
  591. fp->name = "power64 ELF64 executable";
  592. break;
  593. }
  594. if(ep->phentsize != sizeof(P64hdr)) {
  595. werrstr("bad ELF64 header size");
  596. return 0;
  597. }
  598. phsz = sizeof(P64hdr)*ep->phnum;
  599. ph = malloc(phsz);
  600. if(!ph)
  601. return 0;
  602. seek(fd, ep->phoff, 0);
  603. if(read(fd, ph, phsz) < 0) {
  604. free(ph);
  605. return 0;
  606. }
  607. for(i = 0; i < ep->phnum; i++) {
  608. ph[i].type = swal(ph[i].type);
  609. ph[i].flags = swal(ph[i].flags);
  610. ph[i].offset = swav(ph[i].offset);
  611. ph[i].vaddr = swav(ph[i].vaddr);
  612. ph[i].paddr = swav(ph[i].paddr);
  613. ph[i].filesz = swav(ph[i].filesz);
  614. ph[i].memsz = swav(ph[i].memsz);
  615. ph[i].align = swav(ph[i].align);
  616. }
  617. /* find text, data and symbols and install them */
  618. it = id = is = -1;
  619. for(i = 0; i < ep->phnum; i++) {
  620. if(ph[i].type == LOAD
  621. && (ph[i].flags & (R|X)) == (R|X) && it == -1)
  622. it = i;
  623. else if(ph[i].type == LOAD
  624. && (ph[i].flags & (R|W)) == (R|W) && id == -1)
  625. id = i;
  626. else if(ph[i].type == NOPTYPE && is == -1)
  627. is = i;
  628. }
  629. if(it == -1 || id == -1) {
  630. werrstr("No ELF64 TEXT or DATA sections");
  631. free(ph);
  632. return 0;
  633. }
  634. settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
  635. /* 8c: out of fixed registers */
  636. uvl = ph[id].memsz - ph[id].filesz;
  637. setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, uvl);
  638. if(is != -1)
  639. setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);
  640. free(ph);
  641. return 1;
  642. }
  643. /*
  644. * ELF32 binaries.
  645. */
  646. static int
  647. elf32dotout(int fd, Fhdr *fp, ExecHdr *hp)
  648. {
  649. ulong (*swal)(ulong);
  650. ushort (*swab)(ushort);
  651. Ehdr *ep;
  652. Phdr *ph;
  653. int i, it, id, is, phsz;
  654. /* bitswap the header according to the DATA format */
  655. ep = &hp->e;
  656. if(ep->ident[DATA] == ELFDATA2LSB) {
  657. swab = leswab;
  658. swal = leswal;
  659. } else if(ep->ident[DATA] == ELFDATA2MSB) {
  660. swab = beswab;
  661. swal = beswal;
  662. } else {
  663. werrstr("bad ELF32 encoding - not big or little endian");
  664. return 0;
  665. }
  666. ep->type = swab(ep->type);
  667. ep->machine = swab(ep->machine);
  668. ep->version = swal(ep->version);
  669. ep->elfentry = swal(ep->elfentry);
  670. ep->phoff = swal(ep->phoff);
  671. ep->shoff = swal(ep->shoff);
  672. ep->flags = swal(ep->flags);
  673. ep->ehsize = swab(ep->ehsize);
  674. ep->phentsize = swab(ep->phentsize);
  675. ep->phnum = swab(ep->phnum);
  676. ep->shentsize = swab(ep->shentsize);
  677. ep->shnum = swab(ep->shnum);
  678. ep->shstrndx = swab(ep->shstrndx);
  679. if(ep->type != EXEC || ep->version != CURRENT)
  680. return 0;
  681. /* we could definitely support a lot more machines here */
  682. fp->magic = ELF_MAG;
  683. fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
  684. switch(ep->machine) {
  685. case I386:
  686. mach = &mi386;
  687. fp->type = FI386;
  688. fp->name = "386 ELF32 executable";
  689. break;
  690. case MIPS:
  691. mach = &mmips;
  692. fp->type = FMIPS;
  693. fp->name = "mips ELF32 executable";
  694. break;
  695. case SPARC64:
  696. mach = &msparc64;
  697. fp->type = FSPARC64;
  698. fp->name = "sparc64 ELF32 executable";
  699. break;
  700. case POWER:
  701. mach = &mpower;
  702. fp->type = FPOWER;
  703. fp->name = "power ELF32 executable";
  704. break;
  705. case POWER64:
  706. mach = &mpower64;
  707. fp->type = FPOWER64;
  708. fp->name = "power64 ELF32 executable";
  709. break;
  710. case AMD64:
  711. mach = &mamd64;
  712. fp->type = FAMD64;
  713. fp->name = "amd64 ELF32 executable";
  714. break;
  715. case ARM:
  716. mach = &marm;
  717. fp->type = FARM;
  718. fp->name = "arm ELF32 executable";
  719. break;
  720. default:
  721. return 0;
  722. }
  723. if(ep->phentsize != sizeof(Phdr)) {
  724. werrstr("bad ELF32 header size");
  725. return 0;
  726. }
  727. phsz = sizeof(Phdr)*ep->phnum;
  728. ph = malloc(phsz);
  729. if(!ph)
  730. return 0;
  731. seek(fd, ep->phoff, 0);
  732. if(read(fd, ph, phsz) < 0) {
  733. free(ph);
  734. return 0;
  735. }
  736. hswal(ph, phsz/sizeof(ulong), swal);
  737. /* find text, data and symbols and install them */
  738. it = id = is = -1;
  739. for(i = 0; i < ep->phnum; i++) {
  740. if(ph[i].type == LOAD
  741. && (ph[i].flags & (R|X)) == (R|X) && it == -1)
  742. it = i;
  743. else if(ph[i].type == LOAD
  744. && (ph[i].flags & (R|W)) == (R|W) && id == -1)
  745. id = i;
  746. else if(ph[i].type == NOPTYPE && is == -1)
  747. is = i;
  748. }
  749. if(it == -1 || id == -1) {
  750. /*
  751. * The SPARC64 boot image is something of an ELF hack.
  752. * Text+Data+BSS are represented by ph[0]. Symbols
  753. * are represented by ph[1]:
  754. *
  755. * filesz, memsz, vaddr, paddr, off
  756. * ph[0] : txtsz+datsz, txtsz+datsz+bsssz, txtaddr-KZERO, datasize, txtoff
  757. * ph[1] : symsz, lcsz, 0, 0, symoff
  758. */
  759. if(ep->machine == SPARC64 && ep->phnum == 2) {
  760. ulong txtaddr, txtsz, dataddr, bsssz;
  761. txtaddr = ph[0].vaddr | 0x80000000;
  762. txtsz = ph[0].filesz - ph[0].paddr;
  763. dataddr = txtaddr + txtsz;
  764. bsssz = ph[0].memsz - ph[0].filesz;
  765. settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset);
  766. setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz);
  767. setsym(fp, ph[1].filesz, 0, ph[1].memsz, ph[1].offset);
  768. free(ph);
  769. return 1;
  770. }
  771. werrstr("No ELF32 TEXT or DATA sections");
  772. free(ph);
  773. return 0;
  774. }
  775. settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
  776. setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz);
  777. if(is != -1)
  778. setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);
  779. free(ph);
  780. return 1;
  781. }
  782. /*
  783. * Elf binaries.
  784. */
  785. static int
  786. elfdotout(int fd, Fhdr *fp, ExecHdr *hp)
  787. {
  788. Ehdr *ep;
  789. /* bitswap the header according to the DATA format */
  790. ep = &hp->e;
  791. if(ep->ident[CLASS] == ELFCLASS32)
  792. return elf32dotout(fd, fp, hp);
  793. else if(ep->ident[CLASS] == ELFCLASS64)
  794. return elf64dotout(fd, fp, hp);
  795. werrstr("bad ELF class - not 32 bit");
  796. return 0;
  797. }
  798. /*
  799. * (Free|Net)BSD ARM header.
  800. */
  801. static int
  802. armdotout(int fd, Fhdr *fp, ExecHdr *hp)
  803. {
  804. uvlong kbase;
  805. USED(fd);
  806. settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
  807. setdata(fp, fp->txtsz, hp->e.data, fp->txtsz, hp->e.bss);
  808. setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
  809. kbase = 0xF0000000;
  810. if ((fp->entry & kbase) == kbase) { /* Boot image */
  811. fp->txtaddr = kbase+sizeof(Exec);
  812. fp->name = "ARM *BSD boot image";
  813. fp->hdrsz = 0; /* header stripped */
  814. fp->dataddr = kbase+fp->txtsz;
  815. }
  816. return 1;
  817. }
  818. static void
  819. settext(Fhdr *fp, uvlong e, uvlong a, long s, vlong off)
  820. {
  821. fp->txtaddr = a;
  822. fp->entry = e;
  823. fp->txtsz = s;
  824. fp->txtoff = off;
  825. }
  826. static void
  827. setdata(Fhdr *fp, uvlong a, long s, vlong off, long bss)
  828. {
  829. fp->dataddr = a;
  830. fp->datsz = s;
  831. fp->datoff = off;
  832. fp->bsssz = bss;
  833. }
  834. static void
  835. setsym(Fhdr *fp, long symsz, long sppcsz, long lnpcsz, vlong symoff)
  836. {
  837. fp->symsz = symsz;
  838. fp->symoff = symoff;
  839. fp->sppcsz = sppcsz;
  840. fp->sppcoff = fp->symoff+fp->symsz;
  841. fp->lnpcsz = lnpcsz;
  842. fp->lnpcoff = fp->sppcoff+fp->sppcsz;
  843. }
  844. static uvlong
  845. _round(uvlong a, ulong b)
  846. {
  847. uvlong w;
  848. w = (a/b)*b;
  849. if (a!=w)
  850. w += b;
  851. return(w);
  852. }