executable.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. struct mipsexec; /* bootexec.h */
  19. struct mips4kexec; /* bootexec.h */
  20. struct sparcexec; /* bootexec.h */
  21. struct nextexec; /* bootexec.h */
  22. } e;
  23. long dummy; /* padding to ensure extra long */
  24. } ExecHdr;
  25. static int nextboot(int, Fhdr*, ExecHdr*);
  26. static int sparcboot(int, Fhdr*, ExecHdr*);
  27. static int mipsboot(int, Fhdr*, ExecHdr*);
  28. static int mips4kboot(int, Fhdr*, ExecHdr*);
  29. static int common(int, Fhdr*, ExecHdr*);
  30. static int commonllp64(int, Fhdr*, ExecHdr*);
  31. static int adotout(int, Fhdr*, ExecHdr*);
  32. static int elfdotout(int, Fhdr*, ExecHdr*);
  33. static int armdotout(int, Fhdr*, ExecHdr*);
  34. static int alphadotout(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. int type; /* Internal code */
  48. Mach *mach; /* Per-machine data */
  49. long hsize; /* header size */
  50. ulong (*swal)(ulong); /* beswal or leswal */
  51. int (*hparse)(int, Fhdr*, ExecHdr*);
  52. } ExecTable;
  53. extern Mach mmips;
  54. extern Mach mmips2le;
  55. extern Mach mmips2be;
  56. extern Mach msparc;
  57. extern Mach msparc64;
  58. extern Mach m68020;
  59. extern Mach mi386;
  60. extern Mach mamd64;
  61. extern Mach marm;
  62. extern Mach mpower;
  63. extern Mach malpha;
  64. ExecTable exectab[] =
  65. {
  66. { V_MAGIC, /* Mips v.out */
  67. "mips plan 9 executable",
  68. "mips plan 9 dlm",
  69. FMIPS,
  70. &mmips,
  71. sizeof(Exec),
  72. beswal,
  73. adotout },
  74. { M_MAGIC, /* Mips 4.out */
  75. "mips 4k plan 9 executable BE",
  76. "mips 4k plan 9 dlm BE",
  77. FMIPS2BE,
  78. &mmips2be,
  79. sizeof(Exec),
  80. beswal,
  81. adotout },
  82. { N_MAGIC, /* Mips 0.out */
  83. "mips 4k plan 9 executable LE",
  84. "mips 4k plan 9 dlm LE",
  85. FMIPS2LE,
  86. &mmips2le,
  87. sizeof(Exec),
  88. beswal,
  89. adotout },
  90. { 0x160<<16, /* Mips boot image */
  91. "mips plan 9 boot image",
  92. nil,
  93. FMIPSB,
  94. &mmips,
  95. sizeof(struct mipsexec),
  96. beswal,
  97. mipsboot },
  98. { (0x160<<16)|3, /* Mips boot image */
  99. "mips 4k plan 9 boot image",
  100. nil,
  101. FMIPSB,
  102. &mmips2be,
  103. sizeof(struct mips4kexec),
  104. beswal,
  105. mips4kboot },
  106. { K_MAGIC, /* Sparc k.out */
  107. "sparc plan 9 executable",
  108. "sparc plan 9 dlm",
  109. FSPARC,
  110. &msparc,
  111. sizeof(Exec),
  112. beswal,
  113. adotout },
  114. { 0x01030107, /* Sparc boot image */
  115. "sparc plan 9 boot image",
  116. nil,
  117. FSPARCB,
  118. &msparc,
  119. sizeof(struct sparcexec),
  120. beswal,
  121. sparcboot },
  122. { U_MAGIC, /* Sparc64 u.out */
  123. "sparc64 plan 9 executable",
  124. "sparc64 plan 9 dlm",
  125. FSPARC64,
  126. &msparc64,
  127. sizeof(Exec),
  128. beswal,
  129. adotout },
  130. { A_MAGIC, /* 68020 2.out & boot image */
  131. "68020 plan 9 executable",
  132. "68020 plan 9 dlm",
  133. F68020,
  134. &m68020,
  135. sizeof(Exec),
  136. beswal,
  137. common },
  138. { 0xFEEDFACE, /* Next boot image */
  139. "next plan 9 boot image",
  140. nil,
  141. FNEXTB,
  142. &m68020,
  143. sizeof(struct nextexec),
  144. beswal,
  145. nextboot },
  146. { I_MAGIC, /* I386 8.out & boot image */
  147. "386 plan 9 executable",
  148. "386 plan 9 dlm",
  149. FI386,
  150. &mi386,
  151. sizeof(Exec),
  152. beswal,
  153. common },
  154. { S_MAGIC, /* amd64 6.out & boot image */
  155. "amd64 plan 9 executable",
  156. "amd64 plan 9 dlm",
  157. FAMD64,
  158. &mamd64,
  159. sizeof(Exec)+8,
  160. nil,
  161. commonllp64 },
  162. { Q_MAGIC, /* PowerPC q.out & boot image */
  163. "power plan 9 executable",
  164. "power plan 9 dlm",
  165. FPOWER,
  166. &mpower,
  167. sizeof(Exec),
  168. beswal,
  169. common },
  170. { ELF_MAG, /* any elf32 */
  171. "elf executable",
  172. nil,
  173. FNONE,
  174. &mi386,
  175. sizeof(Ehdr),
  176. nil,
  177. elfdotout },
  178. { E_MAGIC, /* Arm 5.out */
  179. "arm plan 9 executable",
  180. "arm plan 9 dlm",
  181. FARM,
  182. &marm,
  183. sizeof(Exec),
  184. beswal,
  185. common },
  186. { (143<<16)|0413, /* (Free|Net)BSD Arm */
  187. "arm *bsd executable",
  188. nil,
  189. FARM,
  190. &marm,
  191. sizeof(Exec),
  192. leswal,
  193. armdotout },
  194. { L_MAGIC, /* alpha 7.out */
  195. "alpha plan 9 executable",
  196. "alpha plan 9 dlm",
  197. FALPHA,
  198. &malpha,
  199. sizeof(Exec),
  200. beswal,
  201. common },
  202. { 0x0700e0c3, /* alpha boot image */
  203. "alpha plan 9 boot image",
  204. nil,
  205. FALPHAB,
  206. &malpha,
  207. sizeof(Exec),
  208. beswal,
  209. alphadotout },
  210. { 0 },
  211. };
  212. Mach *mach = &mi386; /* Global current machine table */
  213. static ExecTable*
  214. couldbe4k(ExecTable *mp)
  215. {
  216. Dir *d;
  217. ExecTable *f;
  218. if((d=dirstat("/proc/1/regs")) == nil)
  219. return mp;
  220. if(d->length < 32*8){ /* R3000 */
  221. free(d);
  222. return mp;
  223. }
  224. free(d);
  225. for (f = exectab; f->magic; f++)
  226. if(f->magic == M_MAGIC) {
  227. f->name = "mips plan 9 executable on mips2 kernel";
  228. return f;
  229. }
  230. return mp;
  231. }
  232. int
  233. crackhdr(int fd, Fhdr *fp)
  234. {
  235. ExecTable *mp;
  236. ExecHdr d;
  237. int nb, ret;
  238. ulong magic;
  239. fp->type = FNONE;
  240. nb = read(fd, (char *)&d.e, sizeof(d.e));
  241. if (nb <= 0)
  242. return 0;
  243. ret = 0;
  244. fp->magic = magic = beswal(d.e.magic); /* big-endian */
  245. for (mp = exectab; mp->magic; mp++) {
  246. if (nb < mp->hsize)
  247. continue;
  248. if (mp->magic == (magic & ~DYN_MAGIC)) {
  249. if(mp->magic == V_MAGIC)
  250. mp = couldbe4k(mp);
  251. fp->type = mp->type;
  252. if ((magic & DYN_MAGIC) && mp->dlmname != nil)
  253. fp->name = mp->dlmname;
  254. else
  255. fp->name = mp->name;
  256. fp->hdrsz = mp->hsize; /* zero on bootables */
  257. mach = mp->mach;
  258. if(mp->swal != nil)
  259. hswal(&d, sizeof(d.e)/sizeof(ulong), mp->swal);
  260. ret = mp->hparse(fd, fp, &d);
  261. seek(fd, mp->hsize, 0); /* seek to end of header */
  262. break;
  263. }
  264. }
  265. if(mp->magic == 0)
  266. werrstr("unknown header type");
  267. return ret;
  268. }
  269. /*
  270. * Convert header to canonical form
  271. */
  272. static void
  273. hswal(void *v, int n, ulong (*swap)(ulong))
  274. {
  275. ulong *ulp;
  276. for(ulp = v; n--; ulp++)
  277. *ulp = (*swap)(*ulp);
  278. }
  279. /*
  280. * Crack a normal a.out-type header
  281. */
  282. static int
  283. adotout(int fd, Fhdr *fp, ExecHdr *hp)
  284. {
  285. long pgsize;
  286. USED(fd);
  287. pgsize = mach->pgsize;
  288. settext(fp, hp->e.entry, pgsize+sizeof(Exec),
  289. hp->e.text, sizeof(Exec));
  290. setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize),
  291. hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
  292. setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
  293. return 1;
  294. }
  295. static void
  296. commonboot(Fhdr *fp)
  297. {
  298. uvlong kbase;
  299. kbase = mach->kbase;
  300. if ((fp->entry & kbase) != kbase)
  301. return;
  302. switch(fp->type) { /* boot image */
  303. case F68020:
  304. fp->type = F68020B;
  305. fp->name = "68020 plan 9 boot image";
  306. break;
  307. case FI386:
  308. fp->type = FI386B;
  309. fp->txtaddr = (u32int)fp->entry;
  310. fp->name = "386 plan 9 boot image";
  311. fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
  312. break;
  313. case FARM:
  314. fp->txtaddr = kbase+0x8010;
  315. fp->name = "ARM plan 9 boot image";
  316. fp->dataddr = fp->txtaddr+fp->txtsz;
  317. return;
  318. case FALPHA:
  319. fp->type = FALPHAB;
  320. fp->txtaddr = (u32int)fp->entry;
  321. fp->name = "alpha plan 9 boot image?";
  322. fp->dataddr = fp->txtaddr+fp->txtsz;
  323. break;
  324. case FPOWER:
  325. fp->type = FPOWERB;
  326. fp->txtaddr = (u32int)fp->entry;
  327. fp->name = "power plan 9 boot image";
  328. fp->dataddr = fp->txtaddr+fp->txtsz;
  329. break;
  330. case FAMD64:
  331. fp->type = FAMD64B;
  332. fp->txtaddr = fp->entry;
  333. fp->name = "amd64 plan 9 boot image";
  334. fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
  335. break;
  336. default:
  337. return;
  338. }
  339. fp->hdrsz = 0; /* header stripped */
  340. }
  341. /*
  342. * 68020 2.out and 68020 bootable images
  343. * 386I 8.out and 386I bootable images
  344. * alpha plan9-style bootable images for axp "headerless" boot
  345. *
  346. */
  347. static int
  348. common(int fd, Fhdr *fp, ExecHdr *hp)
  349. {
  350. adotout(fd, fp, hp);
  351. if(hp->e.magic & DYN_MAGIC) {
  352. fp->txtaddr = 0;
  353. fp->dataddr = fp->txtsz;
  354. return 1;
  355. }
  356. commonboot(fp);
  357. return 1;
  358. }
  359. static int
  360. commonllp64(int, Fhdr *fp, ExecHdr *hp)
  361. {
  362. long pgsize;
  363. uvlong entry;
  364. hswal(&hp->e, sizeof(Exec)/sizeof(long), beswal);
  365. if(!(hp->e.magic & HDR_MAGIC))
  366. return 0;
  367. /*
  368. * There can be more magic here if the
  369. * header ever needs more expansion.
  370. * For now just catch use of any of the
  371. * unused bits.
  372. */
  373. if((hp->e.magic & ~DYN_MAGIC)>>16)
  374. return 0;
  375. entry = beswav(hp->e.hdr[0]);
  376. pgsize = mach->pgsize;
  377. settext(fp, entry, pgsize+fp->hdrsz, hp->e.text, fp->hdrsz);
  378. setdata(fp, _round(pgsize+fp->txtsz+fp->hdrsz, pgsize),
  379. hp->e.data, fp->txtsz+fp->hdrsz, hp->e.bss);
  380. setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
  381. if(hp->e.magic & DYN_MAGIC) {
  382. fp->txtaddr = 0;
  383. fp->dataddr = fp->txtsz;
  384. return 1;
  385. }
  386. commonboot(fp);
  387. return 1;
  388. }
  389. /*
  390. * mips bootable image.
  391. */
  392. static int
  393. mipsboot(int fd, Fhdr *fp, ExecHdr *hp)
  394. {
  395. USED(fd);
  396. switch(hp->e.amagic) {
  397. default:
  398. case 0407: /* some kind of mips */
  399. fp->type = FMIPSB;
  400. settext(fp, hp->e.mentry, hp->e.text_start, hp->e.tsize,
  401. sizeof(struct mipsexec)+4);
  402. setdata(fp, hp->e.data_start, hp->e.dsize,
  403. fp->txtoff+hp->e.tsize, hp->e.bsize);
  404. break;
  405. case 0413: /* some kind of mips */
  406. fp->type = FMIPSB;
  407. settext(fp, hp->e.mentry, hp->e.text_start, hp->e.tsize, 0);
  408. setdata(fp, hp->e.data_start, hp->e.dsize, hp->e.tsize,
  409. hp->e.bsize);
  410. break;
  411. }
  412. setsym(fp, hp->e.nsyms, 0, hp->e.pcsize, hp->e.symptr);
  413. fp->hdrsz = 0; /* header stripped */
  414. return 1;
  415. }
  416. /*
  417. * mips4k bootable image.
  418. */
  419. static int
  420. mips4kboot(int fd, Fhdr *fp, ExecHdr *hp)
  421. {
  422. USED(fd);
  423. switch(hp->e.h.amagic) {
  424. default:
  425. case 0407: /* some kind of mips */
  426. fp->type = FMIPSB;
  427. settext(fp, hp->e.h.mentry, hp->e.h.text_start, hp->e.h.tsize,
  428. sizeof(struct mips4kexec));
  429. setdata(fp, hp->e.h.data_start, hp->e.h.dsize,
  430. fp->txtoff+hp->e.h.tsize, hp->e.h.bsize);
  431. break;
  432. case 0413: /* some kind of mips */
  433. fp->type = FMIPSB;
  434. settext(fp, hp->e.h.mentry, hp->e.h.text_start, hp->e.h.tsize, 0);
  435. setdata(fp, hp->e.h.data_start, hp->e.h.dsize, hp->e.h.tsize,
  436. hp->e.h.bsize);
  437. break;
  438. }
  439. setsym(fp, hp->e.h.nsyms, 0, hp->e.h.pcsize, hp->e.h.symptr);
  440. fp->hdrsz = 0; /* header stripped */
  441. return 1;
  442. }
  443. /*
  444. * sparc bootable image
  445. */
  446. static int
  447. sparcboot(int fd, Fhdr *fp, ExecHdr *hp)
  448. {
  449. USED(fd);
  450. fp->type = FSPARCB;
  451. settext(fp, hp->e.sentry, hp->e.sentry, hp->e.stext,
  452. sizeof(struct sparcexec));
  453. setdata(fp, hp->e.sentry+hp->e.stext, hp->e.sdata,
  454. fp->txtoff+hp->e.stext, hp->e.sbss);
  455. setsym(fp, hp->e.ssyms, 0, hp->e.sdrsize, fp->datoff+hp->e.sdata);
  456. fp->hdrsz = 0; /* header stripped */
  457. return 1;
  458. }
  459. /*
  460. * next bootable image
  461. */
  462. static int
  463. nextboot(int fd, Fhdr *fp, ExecHdr *hp)
  464. {
  465. USED(fd);
  466. fp->type = FNEXTB;
  467. settext(fp, hp->e.textc.vmaddr, hp->e.textc.vmaddr,
  468. hp->e.texts.size, hp->e.texts.offset);
  469. setdata(fp, hp->e.datac.vmaddr, hp->e.datas.size,
  470. hp->e.datas.offset, hp->e.bsss.size);
  471. setsym(fp, hp->e.symc.nsyms, hp->e.symc.spoff, hp->e.symc.pcoff,
  472. hp->e.symc.symoff);
  473. fp->hdrsz = 0; /* header stripped */
  474. return 1;
  475. }
  476. /*
  477. * Elf32 binaries.
  478. */
  479. static int
  480. elfdotout(int fd, Fhdr *fp, ExecHdr *hp)
  481. {
  482. ulong (*swal)(ulong);
  483. ushort (*swab)(ushort);
  484. Ehdr *ep;
  485. Phdr *ph;
  486. int i, it, id, is, phsz;
  487. /* bitswap the header according to the DATA format */
  488. ep = &hp->e;
  489. if(ep->ident[CLASS] != ELFCLASS32) {
  490. werrstr("bad ELF class - not 32 bit");
  491. return 0;
  492. }
  493. if(ep->ident[DATA] == ELFDATA2LSB) {
  494. swab = leswab;
  495. swal = leswal;
  496. } else if(ep->ident[DATA] == ELFDATA2MSB) {
  497. swab = beswab;
  498. swal = beswal;
  499. } else {
  500. werrstr("bad ELF encoding - not big or little endian");
  501. return 0;
  502. }
  503. ep->type = swab(ep->type);
  504. ep->machine = swab(ep->machine);
  505. ep->version = swal(ep->version);
  506. ep->elfentry = swal(ep->elfentry);
  507. ep->phoff = swal(ep->phoff);
  508. ep->shoff = swal(ep->shoff);
  509. ep->flags = swal(ep->flags);
  510. ep->ehsize = swab(ep->ehsize);
  511. ep->phentsize = swab(ep->phentsize);
  512. ep->phnum = swab(ep->phnum);
  513. ep->shentsize = swab(ep->shentsize);
  514. ep->shnum = swab(ep->shnum);
  515. ep->shstrndx = swab(ep->shstrndx);
  516. if(ep->type != EXEC || ep->version != CURRENT)
  517. return 0;
  518. /* we could definitely support a lot more machines here */
  519. fp->magic = ELF_MAG;
  520. fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
  521. switch(ep->machine) {
  522. case I386:
  523. mach = &mi386;
  524. fp->type = FI386;
  525. break;
  526. case MIPS:
  527. mach = &mmips;
  528. fp->type = FMIPS;
  529. break;
  530. case SPARC64:
  531. mach = &msparc64;
  532. fp->type = FSPARC64;
  533. break;
  534. case POWER:
  535. mach = &mpower;
  536. fp->type = FPOWER;
  537. break;
  538. case AMD64:
  539. mach = &mamd64;
  540. fp->type = FAMD64;
  541. break;
  542. default:
  543. return 0;
  544. }
  545. if(ep->phentsize != sizeof(Phdr)) {
  546. werrstr("bad ELF header size");
  547. return 0;
  548. }
  549. phsz = sizeof(Phdr)*ep->phnum;
  550. ph = malloc(phsz);
  551. if(!ph)
  552. return 0;
  553. seek(fd, ep->phoff, 0);
  554. if(read(fd, ph, phsz) < 0) {
  555. free(ph);
  556. return 0;
  557. }
  558. hswal(ph, phsz/sizeof(ulong), swal);
  559. /* find text, data and symbols and install them */
  560. it = id = is = -1;
  561. for(i = 0; i < ep->phnum; i++) {
  562. if(ph[i].type == LOAD
  563. && (ph[i].flags & (R|X)) == (R|X) && it == -1)
  564. it = i;
  565. else if(ph[i].type == LOAD
  566. && (ph[i].flags & (R|W)) == (R|W) && id == -1)
  567. id = i;
  568. else if(ph[i].type == NOPTYPE && is == -1)
  569. is = i;
  570. }
  571. if(it == -1 || id == -1) {
  572. /*
  573. * The SPARC64 boot image is something of an ELF hack.
  574. * Text+Data+BSS are represented by ph[0]. Symbols
  575. * are represented by ph[1]:
  576. *
  577. * filesz, memsz, vaddr, paddr, off
  578. * ph[0] : txtsz+datsz, txtsz+datsz+bsssz, txtaddr-KZERO, datasize, txtoff
  579. * ph[1] : symsz, lcsz, 0, 0, symoff
  580. */
  581. if(ep->machine == SPARC64 && ep->phnum == 2) {
  582. ulong txtaddr, txtsz, dataddr, bsssz;
  583. txtaddr = ph[0].vaddr | 0x80000000;
  584. txtsz = ph[0].filesz - ph[0].paddr;
  585. dataddr = txtaddr + txtsz;
  586. bsssz = ph[0].memsz - ph[0].filesz;
  587. settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset);
  588. setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz);
  589. setsym(fp, ph[1].filesz, 0, ph[1].memsz, ph[1].offset);
  590. free(ph);
  591. return 1;
  592. }
  593. werrstr("No TEXT or DATA sections");
  594. free(ph);
  595. return 0;
  596. }
  597. settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
  598. setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz);
  599. if(is != -1)
  600. setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);
  601. free(ph);
  602. return 1;
  603. }
  604. /*
  605. * alpha bootable
  606. */
  607. static int
  608. alphadotout(int fd, Fhdr *fp, ExecHdr *hp)
  609. {
  610. uvlong kbase;
  611. USED(fd);
  612. settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
  613. setdata(fp, fp->txtsz+sizeof(Exec), hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
  614. setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
  615. /*
  616. * Boot images have some of bits <31:28> set:
  617. * 0x80400000 kernel
  618. * 0x20000000 secondary bootstrap
  619. */
  620. kbase = 0xF0000000;
  621. if (fp->entry & kbase) {
  622. fp->txtaddr = fp->entry;
  623. fp->name = "alpha plan 9 boot image";
  624. fp->hdrsz = 0; /* header stripped */
  625. fp->dataddr = fp->entry+fp->txtsz;
  626. }
  627. return 1;
  628. }
  629. /*
  630. * (Free|Net)BSD ARM header.
  631. */
  632. static int
  633. armdotout(int fd, Fhdr *fp, ExecHdr *hp)
  634. {
  635. uvlong kbase;
  636. USED(fd);
  637. settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
  638. setdata(fp, fp->txtsz, hp->e.data, fp->txtsz, hp->e.bss);
  639. setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
  640. kbase = 0xF0000000;
  641. if ((fp->entry & kbase) == kbase) { /* Boot image */
  642. fp->txtaddr = kbase+sizeof(Exec);
  643. fp->name = "ARM *BSD boot image";
  644. fp->hdrsz = 0; /* header stripped */
  645. fp->dataddr = kbase+fp->txtsz;
  646. }
  647. return 1;
  648. }
  649. static void
  650. settext(Fhdr *fp, uvlong e, uvlong a, long s, vlong off)
  651. {
  652. fp->txtaddr = a;
  653. fp->entry = e;
  654. fp->txtsz = s;
  655. fp->txtoff = off;
  656. }
  657. static void
  658. setdata(Fhdr *fp, uvlong a, long s, vlong off, long bss)
  659. {
  660. fp->dataddr = a;
  661. fp->datsz = s;
  662. fp->datoff = off;
  663. fp->bsssz = bss;
  664. }
  665. static void
  666. setsym(Fhdr *fp, long symsz, long sppcsz, long lnpcsz, vlong symoff)
  667. {
  668. fp->symsz = symsz;
  669. fp->symoff = symoff;
  670. fp->sppcsz = sppcsz;
  671. fp->sppcoff = fp->symoff+fp->symsz;
  672. fp->lnpcsz = lnpcsz;
  673. fp->lnpcoff = fp->sppcoff+fp->sppcsz;
  674. }
  675. static uvlong
  676. _round(uvlong a, ulong b)
  677. {
  678. uvlong w;
  679. w = (a/b)*b;
  680. if (a!=w)
  681. w += b;
  682. return(w);
  683. }