executable.c 16 KB

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