obj.c 23 KB

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