executable.c 16 KB

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