obj.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. #include "l.h"
  2. #include <ar.h>
  3. #ifndef DEFAULT
  4. #define DEFAULT '9'
  5. #endif
  6. char *noname = "<none>";
  7. char symname[] = SYMDEF;
  8. char thechar = '7';
  9. char *thestring = "alpha";
  10. /*
  11. * -H0 -T0x12000004C -D0x140000000 is abbrev unix
  12. * -H1 -T0x20000000 -R4 is bootp() format
  13. * -H2 -T8224 -R8192 is plan9 format
  14. */
  15. void
  16. main(int argc, char *argv[])
  17. {
  18. int c;
  19. char *a;
  20. Binit(&bso, 1, OWRITE);
  21. cout = -1;
  22. listinit();
  23. outfile = 0;
  24. nerrors = 0;
  25. curtext = P;
  26. HEADTYPE = -1;
  27. INITTEXT = -1;
  28. INITDAT = -1;
  29. INITRND = -1;
  30. INITENTRY = 0;
  31. ARGBEGIN {
  32. default:
  33. c = ARGC();
  34. if(c >= 0 && c < sizeof(debug))
  35. debug[c]++;
  36. break;
  37. case 'o':
  38. outfile = ARGF();
  39. break;
  40. case 'E':
  41. a = ARGF();
  42. if(a)
  43. INITENTRY = a;
  44. break;
  45. case 'T':
  46. a = ARGF();
  47. if(a)
  48. INITTEXT = atolwhex(a);
  49. break;
  50. case 'D':
  51. a = ARGF();
  52. if(a)
  53. INITDAT = atolwhex(a);
  54. break;
  55. case 'R':
  56. a = ARGF();
  57. if(a)
  58. INITRND = atolwhex(a);
  59. break;
  60. case 'H':
  61. a = ARGF();
  62. if(a)
  63. HEADTYPE = atolwhex(a);
  64. /* do something about setting INITTEXT */
  65. break;
  66. } ARGEND
  67. USED(argc);
  68. if(*argv == 0) {
  69. diag("usage: 7l [-options] objects");
  70. errorexit();
  71. }
  72. if(!debug['9'] && !debug['B'] && !debug['U'])
  73. debug[DEFAULT] = 1;
  74. if(HEADTYPE == -1) {
  75. if(debug['U'])
  76. HEADTYPE = 0;
  77. if(debug['B'])
  78. HEADTYPE = 1;
  79. if(debug['9'])
  80. HEADTYPE = 2;
  81. }
  82. switch(HEADTYPE) {
  83. default:
  84. diag("unknown -H option");
  85. errorexit();
  86. case 0: /* unix simple */
  87. HEADR = 32L+80L;
  88. if(INITTEXT == -1)
  89. INITTEXT = 0x120000070LL; /* BUG */
  90. if(INITDAT == -1)
  91. INITDAT = 0x140000000LL; /* BUG */
  92. if(INITRND == -1)
  93. INITRND = 0;
  94. break;
  95. case 1: /* boot */
  96. HEADR = 32L+84L;
  97. if(INITTEXT == -1)
  98. INITTEXT = 0x20000074L;
  99. if(INITDAT == -1)
  100. INITDAT = 0;
  101. if(INITRND == -1)
  102. INITRND = 8;
  103. break;
  104. case 2: /* plan 9 */
  105. HEADR = 32L;
  106. if(INITTEXT == -1)
  107. INITTEXT = 8224;
  108. if(INITDAT == -1)
  109. INITDAT = 0;
  110. if(INITRND == -1)
  111. INITRND = 8192;
  112. break;
  113. case 3: /* ``headerless'' tftp boot -- uses branch as magic */
  114. HEADR = 32L;
  115. if(INITTEXT == -1)
  116. INITTEXT = 0x20000020L;
  117. if(INITDAT == -1)
  118. INITDAT = 0;
  119. if(INITRND == -1)
  120. INITRND = 8;
  121. break;
  122. }
  123. if(INITDAT != 0 && INITRND != 0)
  124. print("warning: -D0x%lux is ignored because of -R0x%lux\n",
  125. INITDAT, INITRND);
  126. if(debug['v'])
  127. Bprint(&bso, "HEADER = -H0x%d -T0x%lux -D0x%lux -R0x%lux\n",
  128. HEADTYPE, INITTEXT, INITDAT, INITRND);
  129. Bflush(&bso);
  130. zprg.as = AGOK;
  131. zprg.reg = NREG;
  132. zprg.from.name = D_NONE;
  133. zprg.from.type = D_NONE;
  134. zprg.from.reg = NREG;
  135. zprg.to = zprg.from;
  136. buildop();
  137. histgen = 0;
  138. textp = P;
  139. datap = P;
  140. pc = 0;
  141. dtype = 4;
  142. if(outfile == 0)
  143. outfile = "7.out";
  144. cout = create(outfile, 1, 0775);
  145. if(cout < 0) {
  146. diag("%s: cannot create", outfile);
  147. errorexit();
  148. }
  149. nuxiinit();
  150. version = 0;
  151. cbp = buf.cbuf;
  152. cbc = sizeof(buf.cbuf);
  153. firstp = prg();
  154. lastp = firstp;
  155. if(INITENTRY == 0) {
  156. INITENTRY = "_main";
  157. if(debug['p'])
  158. INITENTRY = "_mainp";
  159. if(!debug['l'])
  160. lookup(INITENTRY, 0)->type = SXREF;
  161. } else
  162. lookup(INITENTRY, 0)->type = SXREF;
  163. while(*argv)
  164. objfile(*argv++);
  165. if(!debug['l'])
  166. loadlib();
  167. firstp = firstp->link;
  168. if(firstp == P)
  169. goto out;
  170. patch();
  171. if(debug['p'])
  172. if(debug['1'])
  173. doprof1();
  174. else
  175. doprof2();
  176. follow();
  177. if(firstp == P)
  178. goto out;
  179. noops();
  180. dodata(); /* is before follow() on other arch */
  181. span();
  182. asmb();
  183. undef();
  184. out:
  185. if(debug['v']) {
  186. Bprint(&bso, "%5.2f cpu time\n", cputime());
  187. Bprint(&bso, "%ld memory used\n", thunk);
  188. Bprint(&bso, "%d sizeof adr\n", sizeof(Adr));
  189. Bprint(&bso, "%d sizeof prog\n", sizeof(Prog));
  190. }
  191. Bflush(&bso);
  192. errorexit();
  193. }
  194. void
  195. loadlib(void)
  196. {
  197. int i;
  198. long h;
  199. Sym *s;
  200. loop:
  201. xrefresolv = 0;
  202. for(i=0; i<libraryp; i++) {
  203. if(debug['v'])
  204. Bprint(&bso, "%5.2f autolib: %s\n", cputime(), library[i]);
  205. objfile(library[i]);
  206. }
  207. if(xrefresolv)
  208. for(h=0; h<nelem(hash); h++)
  209. for(s = hash[h]; s != S; s = s->link)
  210. if(s->type == SXREF)
  211. goto loop;
  212. }
  213. void
  214. errorexit(void)
  215. {
  216. Bflush(&bso);
  217. if(nerrors) {
  218. if(cout >= 0)
  219. remove(outfile);
  220. exits("error");
  221. }
  222. exits(0);
  223. }
  224. void
  225. objfile(char *file)
  226. {
  227. long off, esym, cnt, l;
  228. int f, work;
  229. Sym *s;
  230. char magbuf[SARMAG];
  231. char name[100], pname[150];
  232. struct ar_hdr arhdr;
  233. char *e, *start, *stop;
  234. if(file[0] == '-' && file[1] == 'l') {
  235. if(debug['9'])
  236. sprint(name, "/%s/lib/lib", thestring);
  237. else
  238. sprint(name, "/usr/%clib/lib", thechar);
  239. strcat(name, file+2);
  240. strcat(name, ".a");
  241. file = name;
  242. }
  243. if(debug['v'])
  244. Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
  245. Bflush(&bso);
  246. f = open(file, 0);
  247. if(f < 0) {
  248. diag("cannot open file: %s", file);
  249. errorexit();
  250. }
  251. l = read(f, magbuf, SARMAG);
  252. if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
  253. /* load it as a regular file */
  254. l = seek(f, 0L, 2);
  255. seek(f, 0L, 0);
  256. ldobj(f, l, file);
  257. close(f);
  258. return;
  259. }
  260. if(debug['v'])
  261. Bprint(&bso, "%5.2f ldlib: %s\n", cputime(), file);
  262. l = read(f, &arhdr, SAR_HDR);
  263. if(l != SAR_HDR) {
  264. diag("%s: short read on archive file symbol header", file);
  265. goto out;
  266. }
  267. if(strncmp(arhdr.name, symname, strlen(symname))) {
  268. diag("%s: first entry not symbol header", file);
  269. goto out;
  270. }
  271. esym = SARMAG + SAR_HDR + atolwhex(arhdr.size);
  272. off = SARMAG + SAR_HDR;
  273. /*
  274. * just bang the whole symbol file into memory
  275. */
  276. seek(f, off, 0);
  277. cnt = esym - off;
  278. start = malloc(cnt + 10);
  279. cnt = read(f, start, cnt);
  280. if(cnt <= 0){
  281. close(f);
  282. return;
  283. }
  284. stop = &start[cnt];
  285. memset(stop, 0, 10);
  286. work = 1;
  287. while(work){
  288. if(debug['v'])
  289. Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file);
  290. Bflush(&bso);
  291. work = 0;
  292. for(e = start; e < stop; e = strchr(e+5, 0) + 1) {
  293. s = lookup(e+5, 0);
  294. if(s->type != SXREF)
  295. continue;
  296. sprint(pname, "%s(%s)", file, s->name);
  297. if(debug['v'])
  298. Bprint(&bso, "%5.2f library: %s\n", cputime(), pname);
  299. Bflush(&bso);
  300. l = e[1] & 0xff;
  301. l |= (e[2] & 0xff) << 8;
  302. l |= (e[3] & 0xff) << 16;
  303. l |= (e[4] & 0xff) << 24;
  304. seek(f, l, 0);
  305. l = read(f, &arhdr, SAR_HDR);
  306. if(l != SAR_HDR)
  307. goto bad;
  308. if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
  309. goto bad;
  310. l = atolwhex(arhdr.size);
  311. ldobj(f, l, pname);
  312. if(s->type == SXREF) {
  313. diag("%s: failed to load: %s", file, s->name);
  314. errorexit();
  315. }
  316. work = 1;
  317. xrefresolv = 1;
  318. }
  319. }
  320. return;
  321. bad:
  322. diag("%s: bad or out of date archive", file);
  323. out:
  324. close(f);
  325. }
  326. int
  327. zaddr(uchar *p, Adr *a, Sym *h[])
  328. {
  329. int i, c;
  330. vlong l;
  331. Sym *s;
  332. Auto *u;
  333. c = p[2];
  334. if(c < 0 || c > NSYM){
  335. print("sym out of range: %d\n", c);
  336. p[0] = ALAST+1;
  337. return 0;
  338. }
  339. a->type = p[0];
  340. a->reg = p[1];
  341. a->sym = h[p[2]];
  342. a->name = p[3];
  343. c = 4;
  344. if(a->reg < 0 || a->reg > NREG) {
  345. print("register out of range %d\n", a->reg);
  346. p[0] = ALAST+1;
  347. return 0; /* force real diagnostic */
  348. }
  349. switch(a->type) {
  350. default:
  351. print("unknown type %d\n", a->type);
  352. p[0] = ALAST+1;
  353. return 0; /* force real diagnostic */
  354. case D_NONE:
  355. case D_REG:
  356. case D_FREG:
  357. case D_PREG:
  358. case D_FCREG:
  359. case D_PCC:
  360. break;
  361. case D_BRANCH:
  362. case D_OREG:
  363. case D_CONST:
  364. a->offset = (uvlong)p[4] | ((uvlong)p[5]<<8L) |
  365. ((uvlong) p[6]<<16L) | ((uvlong) p[7]<<24L) |
  366. ((uvlong) p[8]<<32L) | ((uvlong) p[9]<<40L) |
  367. ((uvlong) p[10]<<48L) | ((uvlong) p[11]<<56L);
  368. c += 8;
  369. break;
  370. case D_SCONST:
  371. while(nhunk < NSNAME)
  372. gethunk();
  373. a->sval = (char*)hunk;
  374. nhunk -= NSNAME;
  375. hunk += NSNAME;
  376. memmove(a->sval, p+4, NSNAME);
  377. c += NSNAME;
  378. break;
  379. case D_FCONST:
  380. while(nhunk < sizeof(Ieee))
  381. gethunk();
  382. a->ieee = (Ieee*)hunk;
  383. nhunk -= sizeof(Ieee);
  384. hunk += sizeof(Ieee);
  385. a->ieee->l = p[4] | (p[5]<<8) |
  386. (p[6]<<16) | (p[7]<<24);
  387. a->ieee->h = p[8] | (p[9]<<8) |
  388. (p[10]<<16) | (p[11]<<24);
  389. c += 8;
  390. break;
  391. }
  392. s = a->sym;
  393. if(s == S)
  394. return c;
  395. i = a->name;
  396. if(i != D_AUTO && i != D_PARAM)
  397. return c;
  398. l = a->offset;
  399. for(u=curauto; u; u=u->link)
  400. if(u->sym == s)
  401. if(u->type == i) {
  402. if(u->offset > l)
  403. u->offset = l;
  404. return c;
  405. }
  406. while(nhunk < sizeof(Auto))
  407. gethunk();
  408. u = (Auto*)hunk;
  409. nhunk -= sizeof(Auto);
  410. hunk += sizeof(Auto);
  411. u->link = curauto;
  412. curauto = u;
  413. u->sym = s;
  414. u->offset = l;
  415. u->type = i;
  416. return c;
  417. }
  418. void
  419. addlib(char *obj)
  420. {
  421. char name[1024], comp[256], *p;
  422. int i;
  423. if(histfrogp <= 0)
  424. return;
  425. if(histfrog[0]->name[1] == '/') {
  426. sprint(name, "");
  427. i = 1;
  428. } else
  429. if(histfrog[0]->name[1] == '.') {
  430. sprint(name, ".");
  431. i = 0;
  432. } else {
  433. if(debug['9'])
  434. sprint(name, "/%s/lib", thestring);
  435. else
  436. sprint(name, "/usr/%clib", thechar);
  437. i = 0;
  438. }
  439. for(; i<histfrogp; i++) {
  440. snprint(comp, sizeof comp, histfrog[i]->name+1);
  441. for(;;) {
  442. p = strstr(comp, "$O");
  443. if(p == 0)
  444. break;
  445. memmove(p+1, p+2, strlen(p+2)+1);
  446. p[0] = thechar;
  447. }
  448. for(;;) {
  449. p = strstr(comp, "$M");
  450. if(p == 0)
  451. break;
  452. if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) {
  453. diag("library component too long");
  454. return;
  455. }
  456. memmove(p+strlen(thestring), p+2, strlen(p+2)+1);
  457. memmove(p, thestring, strlen(thestring));
  458. }
  459. if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) {
  460. diag("library component too long");
  461. return;
  462. }
  463. strcat(name, "/");
  464. strcat(name, comp);
  465. }
  466. for(i=0; i<libraryp; i++)
  467. if(strcmp(name, library[i]) == 0)
  468. return;
  469. if(libraryp == nelem(library)){
  470. diag("too many autolibs; skipping %s", name);
  471. return;
  472. }
  473. p = malloc(strlen(name) + 1);
  474. strcpy(p, name);
  475. library[libraryp] = p;
  476. p = malloc(strlen(obj) + 1);
  477. strcpy(p, obj);
  478. libraryobj[libraryp] = p;
  479. libraryp++;
  480. }
  481. void
  482. addhist(long line, int type)
  483. {
  484. Auto *u;
  485. Sym *s;
  486. int i, j, k;
  487. u = malloc(sizeof(Auto));
  488. s = malloc(sizeof(Sym));
  489. s->name = malloc(2*(histfrogp+1) + 1);
  490. u->sym = s;
  491. u->type = type;
  492. u->offset = line;
  493. u->link = curhist;
  494. curhist = u;
  495. j = 1;
  496. for(i=0; i<histfrogp; i++) {
  497. k = histfrog[i]->value;
  498. s->name[j+0] = k>>8;
  499. s->name[j+1] = k;
  500. j += 2;
  501. }
  502. }
  503. void
  504. histtoauto(void)
  505. {
  506. Auto *l;
  507. while(l = curhist) {
  508. curhist = l->link;
  509. l->link = curauto;
  510. curauto = l;
  511. }
  512. }
  513. void
  514. collapsefrog(Sym *s)
  515. {
  516. int i;
  517. /*
  518. * bad encoding of path components only allows
  519. * MAXHIST components. if there is an overflow,
  520. * first try to collapse xxx/..
  521. */
  522. for(i=1; i<histfrogp; i++)
  523. if(strcmp(histfrog[i]->name+1, "..") == 0) {
  524. memmove(histfrog+i-1, histfrog+i+1,
  525. (histfrogp-i-1)*sizeof(histfrog[0]));
  526. histfrogp--;
  527. goto out;
  528. }
  529. /*
  530. * next try to collapse .
  531. */
  532. for(i=0; i<histfrogp; i++)
  533. if(strcmp(histfrog[i]->name+1, ".") == 0) {
  534. memmove(histfrog+i, histfrog+i+1,
  535. (histfrogp-i-1)*sizeof(histfrog[0]));
  536. goto out;
  537. }
  538. /*
  539. * last chance, just truncate from front
  540. */
  541. memmove(histfrog+0, histfrog+1,
  542. (histfrogp-1)*sizeof(histfrog[0]));
  543. out:
  544. histfrog[histfrogp-1] = s;
  545. }
  546. void
  547. nopout(Prog *p)
  548. {
  549. p->as = ANOP;
  550. p->from.type = D_NONE;
  551. p->to.type = D_NONE;
  552. }
  553. uchar*
  554. readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
  555. {
  556. int n;
  557. n = stop - good;
  558. memmove(buf, good, stop - good);
  559. stop = buf + n;
  560. n = MAXIO - n;
  561. if(n > max)
  562. n = max;
  563. n = read(f, stop, n);
  564. if(n <= 0)
  565. return 0;
  566. return stop + n;
  567. }
  568. void
  569. ldobj(int f, long c, char *pn)
  570. {
  571. long ipc;
  572. Prog *p, *t;
  573. uchar *bloc, *bsize, *stop;
  574. Sym *h[NSYM], *s, *di;
  575. int v, o, r, skip;
  576. bsize = buf.xbuf;
  577. bloc = buf.xbuf;
  578. di = S;
  579. newloop:
  580. memset(h, 0, sizeof(h));
  581. version++;
  582. histfrogp = 0;
  583. ipc = pc;
  584. skip = 0;
  585. loop:
  586. if(c <= 0)
  587. goto eof;
  588. r = bsize - bloc;
  589. if(r < 100 && r < c) { /* enough for largest prog */
  590. bsize = readsome(f, buf.xbuf, bloc, bsize, c);
  591. if(bsize == 0)
  592. goto eof;
  593. bloc = buf.xbuf;
  594. goto loop;
  595. }
  596. o = bloc[0]; /* as */
  597. if(o <= AXXX || o >= ALAST) {
  598. diag("%s: line %ld: opcode out of range %d", pn, pc-ipc, o);
  599. print(" probably not a .7 file\n");
  600. errorexit();
  601. }
  602. if(o == ANAME) {
  603. stop = memchr(&bloc[3], 0, bsize-&bloc[3]);
  604. if(stop == 0){
  605. bsize = readsome(f, buf.xbuf, bloc, bsize, c);
  606. if(bsize == 0)
  607. goto eof;
  608. bloc = buf.xbuf;
  609. stop = memchr(&bloc[3], 0, bsize-&bloc[3]);
  610. if(stop == 0){
  611. fprint(2, "%s: name too long\n", pn);
  612. errorexit();
  613. }
  614. }
  615. v = bloc[1]; /* type */
  616. o = bloc[2]; /* sym */
  617. bloc += 3;
  618. c -= 3;
  619. r = 0;
  620. if(v == D_STATIC)
  621. r = version;
  622. s = lookup((char*)bloc, r);
  623. c -= &stop[1] - bloc;
  624. bloc = stop + 1;
  625. if(debug['W'])
  626. print(" ANAME %s\n", s->name);
  627. h[o] = s;
  628. if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
  629. s->type = SXREF;
  630. if(v == D_FILE) {
  631. if(s->type != SFILE) {
  632. histgen++;
  633. s->type = SFILE;
  634. s->value = histgen;
  635. }
  636. if(histfrogp < MAXHIST) {
  637. histfrog[histfrogp] = s;
  638. histfrogp++;
  639. } else
  640. collapsefrog(s);
  641. }
  642. goto loop;
  643. }
  644. if(nhunk < sizeof(Prog))
  645. gethunk();
  646. p = (Prog*)hunk;
  647. nhunk -= sizeof(Prog);
  648. hunk += sizeof(Prog);
  649. p->as = o;
  650. p->reg = bloc[1] & 0x7f;
  651. if(bloc[1] & 0x80)
  652. p->mark = NOSCHED;
  653. p->line = bloc[2] | (bloc[3]<<8) | (bloc[4]<<16) | (bloc[5]<<24);
  654. r = zaddr(bloc+6, &p->from, h) + 6;
  655. r += zaddr(bloc+r, &p->to, h);
  656. bloc += r;
  657. c -= r;
  658. if(p->reg < 0 || p->reg > NREG)
  659. diag("register out of range %d", p->reg);
  660. p->link = P;
  661. p->cond = P;
  662. if(debug['W'])
  663. print("%P\n", p);
  664. switch(o) {
  665. case AHISTORY:
  666. if(p->to.offset == -1) {
  667. addlib(pn);
  668. histfrogp = 0;
  669. goto loop;
  670. }
  671. addhist(p->line, D_FILE); /* 'z' */
  672. if(p->to.offset)
  673. addhist(p->to.offset, D_PREG); /* 'Z' */
  674. histfrogp = 0;
  675. goto loop;
  676. case AEND:
  677. histtoauto();
  678. if(curtext != P)
  679. curtext->to.autom = curauto;
  680. curauto = 0;
  681. curtext = P;
  682. if(c)
  683. goto newloop;
  684. return;
  685. case AGLOBL:
  686. s = p->from.sym;
  687. if(s == S) {
  688. diag("GLOBL must have a name\n%P", p);
  689. errorexit();
  690. }
  691. if(s->type == 0 || s->type == SXREF) {
  692. s->type = SBSS;
  693. s->value = 0;
  694. }
  695. if(s->type != SBSS) {
  696. diag("redefinition: %s\n%P", s->name, p);
  697. s->type = SBSS;
  698. s->value = 0;
  699. }
  700. if(p->to.offset > s->value)
  701. s->value = p->to.offset;
  702. break;
  703. case ADYNT:
  704. if(p->to.sym == S) {
  705. diag("DYNT without a sym\n%P", p);
  706. break;
  707. }
  708. di = p->to.sym;
  709. p->reg = 4;
  710. if(di->type == SXREF) {
  711. if(debug['z'])
  712. Bprint(&bso, "%P set to %d\n", p, dtype);
  713. di->type = SCONST;
  714. di->value = dtype;
  715. dtype += 4;
  716. }
  717. if(p->from.sym == S)
  718. break;
  719. p->from.offset = di->value;
  720. p->from.sym->type = SDATA;
  721. if(curtext == P) {
  722. diag("DYNT not in text: %P", p);
  723. break;
  724. }
  725. p->to.sym = curtext->from.sym;
  726. p->to.type = D_CONST;
  727. p->link = datap;
  728. datap = p;
  729. break;
  730. case AINIT:
  731. if(p->from.sym == S) {
  732. diag("INIT without a sym\n%P", p);
  733. break;
  734. }
  735. if(di == S) {
  736. diag("INIT without previous DYNT\n%P", p);
  737. break;
  738. }
  739. p->from.offset = di->value;
  740. p->from.sym->type = SDATA;
  741. p->link = datap;
  742. datap = p;
  743. break;
  744. case ADATA:
  745. if(p->from.sym == S) {
  746. diag("DATA without a sym\n%P", p);
  747. break;
  748. }
  749. p->link = datap;
  750. datap = p;
  751. break;
  752. case AGOK:
  753. diag("unknown opcode\n%P", p);
  754. p->pc = pc;
  755. pc++;
  756. break;
  757. case ATEXT:
  758. if(curtext != P) {
  759. histtoauto();
  760. curtext->to.autom = curauto;
  761. curauto = 0;
  762. }
  763. skip = 0;
  764. curtext = p;
  765. autosize = (p->to.offset+3L) & ~3L;
  766. if (autosize & 4)
  767. autosize += 4;
  768. p->to.offset = autosize;
  769. autosize += 8;
  770. s = p->from.sym;
  771. if(s == S) {
  772. diag("TEXT must have a name\n%P", p);
  773. errorexit();
  774. }
  775. if(s->type != 0 && s->type != SXREF) {
  776. if(p->reg & DUPOK) {
  777. skip = 1;
  778. goto casedef;
  779. }
  780. diag("redefinition: %s\n%P", s->name, p);
  781. }
  782. s->type = STEXT;
  783. s->value = pc;
  784. lastp->link = p;
  785. lastp = p;
  786. p->pc = pc;
  787. pc++;
  788. if(textp == P) {
  789. textp = p;
  790. etextp = p;
  791. goto loop;
  792. }
  793. etextp->cond = p;
  794. etextp = p;
  795. break;
  796. case AMOVS:
  797. if(skip)
  798. goto casedef;
  799. if(p->from.type == D_FCONST) {
  800. /* size sb 9 max */
  801. sprint(literal, "$%lux", ieeedtof(p->from.ieee));
  802. s = lookup(literal, 0);
  803. if(s->type == 0) {
  804. s->type = SBSS;
  805. s->value = 4;
  806. t = prg();
  807. t->as = ADATA;
  808. t->line = p->line;
  809. t->from.type = D_OREG;
  810. t->from.sym = s;
  811. t->from.name = D_EXTERN;
  812. t->reg = 4;
  813. t->to = p->from;
  814. t->link = datap;
  815. datap = t;
  816. }
  817. p->from.type = D_OREG;
  818. p->from.sym = s;
  819. p->from.name = D_EXTERN;
  820. p->from.offset = 0;
  821. }
  822. goto casedef;
  823. case AMOVT:
  824. if(skip)
  825. goto casedef;
  826. if(p->from.type == D_FCONST) {
  827. /* size sb 18 max */
  828. sprint(literal, "$%lux.%lux",
  829. p->from.ieee->l, p->from.ieee->h);
  830. s = lookup(literal, 0);
  831. if(s->type == 0) {
  832. s->type = SBSS;
  833. s->value = 8;
  834. t = prg();
  835. t->as = ADATA;
  836. t->line = p->line;
  837. t->from.type = D_OREG;
  838. t->from.sym = s;
  839. t->from.name = D_EXTERN;
  840. t->reg = 8;
  841. t->to = p->from;
  842. t->link = datap;
  843. datap = t;
  844. }
  845. p->from.type = D_OREG;
  846. p->from.sym = s;
  847. p->from.name = D_EXTERN;
  848. p->from.offset = 0;
  849. }
  850. goto casedef;
  851. case AMOVQ:
  852. if(skip)
  853. goto casedef;
  854. if (p->from.type == D_FREG || p->to.type == D_FREG)
  855. p->as = AMOVT; /* stupid compiler optimiser kkludge */
  856. case AMOVL:
  857. case AMOVW:
  858. case AMOVB:
  859. /* BOTCH */
  860. if (p->from.type == D_CONST)
  861. if (p->from.offset == 0LL && p->from.name == D_NONE) {
  862. p->from.type = D_REG;
  863. p->from.reg = REGZERO;
  864. }
  865. goto casedef;
  866. default:
  867. casedef:
  868. if(skip)
  869. nopout(p);
  870. if(p->to.type == D_BRANCH)
  871. p->to.offset += ipc;
  872. lastp->link = p;
  873. lastp = p;
  874. p->pc = pc;
  875. pc++;
  876. break;
  877. }
  878. goto loop;
  879. eof:
  880. diag("truncated object file: %s", pn);
  881. }
  882. Sym*
  883. lookup(char *symb, int v)
  884. {
  885. Sym *s;
  886. char *p;
  887. long h;
  888. int c0, l;
  889. h = v;
  890. for(p=symb; c0 = *p; p++)
  891. h = h+h+h + c0;
  892. l = (p - symb) + 1;
  893. if(h < 0)
  894. h = ~h;
  895. h %= NHASH;
  896. for(s = hash[h]; s != S; s = s->link)
  897. if(s->version == v)
  898. if(memcmp(s->name, symb, l) == 0)
  899. return s;
  900. while(nhunk < sizeof(Sym))
  901. gethunk();
  902. s = (Sym*)hunk;
  903. nhunk -= sizeof(Sym);
  904. hunk += sizeof(Sym);
  905. s->name = malloc(l);
  906. memmove(s->name, symb, l);
  907. s->link = hash[h];
  908. s->type = 0;
  909. s->version = v;
  910. s->value = 0;
  911. hash[h] = s;
  912. return s;
  913. }
  914. Prog*
  915. prg(void)
  916. {
  917. Prog *p;
  918. while(nhunk < sizeof(Prog))
  919. gethunk();
  920. p = (Prog*)hunk;
  921. nhunk -= sizeof(Prog);
  922. hunk += sizeof(Prog);
  923. *p = zprg;
  924. return p;
  925. }
  926. void
  927. gethunk(void)
  928. {
  929. char *h;
  930. long nh;
  931. nh = NHUNK;
  932. if(thunk >= 5L*NHUNK) {
  933. nh = 5L*NHUNK;
  934. if(thunk >= 25L*NHUNK)
  935. nh = 25L*NHUNK;
  936. }
  937. h = mysbrk(nh);
  938. if(h == (char*)-1) {
  939. diag("out of memory");
  940. errorexit();
  941. }
  942. hunk = h;
  943. nhunk = nh;
  944. thunk += nh;
  945. }
  946. void
  947. doprof1(void)
  948. {
  949. Sym *s;
  950. long n;
  951. Prog *p, *q;
  952. if(debug['v'])
  953. Bprint(&bso, "%5.2f profile 1\n", cputime());
  954. Bflush(&bso);
  955. s = lookup("__mcount", 0);
  956. n = 1;
  957. for(p = firstp->link; p != P; p = p->link) {
  958. if(p->as == ATEXT) {
  959. q = prg();
  960. q->line = p->line;
  961. q->link = datap;
  962. datap = q;
  963. q->as = ADATA;
  964. q->from.type = D_OREG;
  965. q->from.name = D_EXTERN;
  966. q->from.offset = n*4;
  967. q->from.sym = s;
  968. q->reg = 4;
  969. q->to = p->from;
  970. q->to.type = D_CONST;
  971. q = prg();
  972. q->line = p->line;
  973. q->pc = p->pc;
  974. q->link = p->link;
  975. p->link = q;
  976. p = q;
  977. p->as = AMOVL;
  978. p->from.type = D_OREG;
  979. p->from.name = D_EXTERN;
  980. p->from.sym = s;
  981. p->from.offset = n*4 + 4;
  982. p->to.type = D_REG;
  983. p->to.reg = REGTMP;
  984. q = prg();
  985. q->line = p->line;
  986. q->pc = p->pc;
  987. q->link = p->link;
  988. p->link = q;
  989. p = q;
  990. p->as = AADDQ;
  991. p->from.type = D_CONST;
  992. p->from.offset = 1;
  993. p->to.type = D_REG;
  994. p->to.reg = REGTMP;
  995. q = prg();
  996. q->line = p->line;
  997. q->pc = p->pc;
  998. q->link = p->link;
  999. p->link = q;
  1000. p = q;
  1001. p->as = AMOVL;
  1002. p->from.type = D_REG;
  1003. p->from.reg = REGTMP;
  1004. p->to.type = D_OREG;
  1005. p->to.name = D_EXTERN;
  1006. p->to.sym = s;
  1007. p->to.offset = n*4 + 4;
  1008. n += 2;
  1009. continue;
  1010. }
  1011. }
  1012. q = prg();
  1013. q->line = 0;
  1014. q->link = datap;
  1015. datap = q;
  1016. q->as = ADATA;
  1017. q->from.type = D_OREG;
  1018. q->from.name = D_EXTERN;
  1019. q->from.sym = s;
  1020. q->reg = 4;
  1021. q->to.type = D_CONST;
  1022. q->to.offset = n;
  1023. s->type = SBSS;
  1024. s->value = n*4;
  1025. }
  1026. void
  1027. doprof2(void)
  1028. {
  1029. Sym *s2, *s4;
  1030. Prog *p, *q, *ps2, *ps4;
  1031. if(debug['v'])
  1032. Bprint(&bso, "%5.2f profile 2\n", cputime());
  1033. Bflush(&bso);
  1034. s2 = lookup("_profin", 0);
  1035. s4 = lookup("_profout", 0);
  1036. if(s2->type != STEXT || s4->type != STEXT) {
  1037. diag("_profin/_profout not defined");
  1038. return;
  1039. }
  1040. ps2 = P;
  1041. ps4 = P;
  1042. for(p = firstp; p != P; p = p->link) {
  1043. if(p->as == ATEXT) {
  1044. if(p->from.sym == s2) {
  1045. ps2 = p;
  1046. p->reg = 1;
  1047. }
  1048. if(p->from.sym == s4) {
  1049. ps4 = p;
  1050. p->reg = 1;
  1051. }
  1052. }
  1053. }
  1054. for(p = firstp; p != P; p = p->link) {
  1055. if(p->as == ATEXT) {
  1056. if(p->reg != NREG) {
  1057. for(;;) {
  1058. q = p->link;
  1059. if(q == P)
  1060. break;
  1061. if(q->as == ATEXT)
  1062. break;
  1063. p = q;
  1064. }
  1065. continue;
  1066. }
  1067. /*
  1068. * JSR profin, R2
  1069. */
  1070. q = prg();
  1071. q->line = p->line;
  1072. q->pc = p->pc;
  1073. q->link = p->link;
  1074. p->link = q;
  1075. p = q;
  1076. p->as = AJSR;
  1077. p->to.type = D_BRANCH;
  1078. p->cond = ps2;
  1079. p->to.sym = s2;
  1080. continue;
  1081. }
  1082. if(p->as == ARET) {
  1083. /*
  1084. * JSR profout
  1085. */
  1086. p->as = AJSR;
  1087. p->to.type = D_BRANCH;
  1088. p->cond = ps4;
  1089. p->to.sym = s4;
  1090. /*
  1091. * RET
  1092. */
  1093. q = prg();
  1094. q->line = p->line;
  1095. q->pc = p->pc;
  1096. q->link = p->link;
  1097. p->link = q;
  1098. p = q;
  1099. p->as = ARET;
  1100. continue;
  1101. }
  1102. }
  1103. }
  1104. void
  1105. nuxiinit(void)
  1106. {
  1107. int i, c;
  1108. for(i=0; i<4; i++) {
  1109. c = find1(0x04030201L, i+1);
  1110. if(i <= 4)
  1111. inuxi4[i] = c;
  1112. if(i <= 2)
  1113. inuxi2[i] = c;
  1114. if(i <= 1)
  1115. inuxi1[i] = c;
  1116. }
  1117. for (i = 0; i < 4; i++) {
  1118. c = find1(0x04030201L, i+1);
  1119. fnuxi8[i] = c;
  1120. fnuxi8[i+4] = c + sizeof(long);
  1121. }
  1122. if(debug['v']) {
  1123. Bprint(&bso, "inuxi = ");
  1124. for(i=0; i<1; i++)
  1125. Bprint(&bso, "%d", inuxi1[i]);
  1126. Bprint(&bso, " ");
  1127. for(i=0; i<2; i++)
  1128. Bprint(&bso, "%d", inuxi2[i]);
  1129. Bprint(&bso, " ");
  1130. for(i=0; i<4; i++)
  1131. Bprint(&bso, "%d", inuxi4[i]);
  1132. Bprint(&bso, " ");
  1133. Bprint(&bso, "\nfnuxi = ");
  1134. for(i=0; i<8; i++)
  1135. Bprint(&bso, "%d", fnuxi8[i]);
  1136. Bprint(&bso, "\n");
  1137. }
  1138. Bflush(&bso);
  1139. }
  1140. find1(long l, int c)
  1141. {
  1142. char *p;
  1143. int i;
  1144. p = (char*)&l;
  1145. for(i=0; i<4; i++)
  1146. if(*p++ == c)
  1147. return i;
  1148. return 0;
  1149. }
  1150. long
  1151. ieeedtof(Ieee *ieee)
  1152. {
  1153. int exp;
  1154. long v;
  1155. if(ieee->h == 0)
  1156. return 0;
  1157. exp = (ieee->h>>20) & ((1L<<11)-1L);
  1158. exp -= (1L<<10) - 2L;
  1159. v = (ieee->h & 0xfffffL) << 3;
  1160. v |= (ieee->l >> 29) & 0x7L;
  1161. if((ieee->l >> 28) & 1) {
  1162. v++;
  1163. if(v & 0x800000L) {
  1164. v = (v & 0x7fffffL) >> 1;
  1165. exp++;
  1166. }
  1167. }
  1168. if(exp <= -126 || exp >= 130)
  1169. diag("double fp to single fp overflow");
  1170. v |= ((exp + 126) & 0xffL) << 23;
  1171. v |= ieee->h & 0x80000000L;
  1172. return v;
  1173. }
  1174. double
  1175. ieeedtod(Ieee *ieee)
  1176. {
  1177. Ieee e;
  1178. double fr;
  1179. int exp;
  1180. if(ieee->h & (1L<<31)) {
  1181. e.h = ieee->h & ~(1L<<31);
  1182. e.l = ieee->l;
  1183. return -ieeedtod(&e);
  1184. }
  1185. if(ieee->l == 0 && ieee->h == 0)
  1186. return 0;
  1187. fr = ieee->l & ((1L<<16)-1L);
  1188. fr /= 1L<<16;
  1189. fr += (ieee->l>>16) & ((1L<<16)-1L);
  1190. fr /= 1L<<16;
  1191. fr += (ieee->h & (1L<<20)-1L) | (1L<<20);
  1192. fr /= 1L<<21;
  1193. exp = (ieee->h>>20) & ((1L<<11)-1L);
  1194. exp -= (1L<<10) - 2L;
  1195. return ldexp(fr, exp);
  1196. }