obj.c 18 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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 = '6';
  9. char *thestring = "960";
  10. /*
  11. * -H0 -T0x40004C -D0x10000000 is garbage unix
  12. * -H1 -T0xd0 -R4 is unix coff
  13. * -H2 -T4128 -R4096 is plan9 format
  14. * -H3 -Tx -Rx is msdos boot
  15. * -H4 -T0x8000 -R4 is Intel 960 coff
  16. */
  17. void
  18. main(int argc, char *argv[])
  19. {
  20. int i, c;
  21. char *a;
  22. Binit(&bso, 1, OWRITE);
  23. cout = -1;
  24. listinit();
  25. memset(debug, 0, sizeof(debug));
  26. nerrors = 0;
  27. outfile = "6.out";
  28. HEADTYPE = -1;
  29. INITTEXT = -1;
  30. INITDAT = -1;
  31. INITRND = -1;
  32. INITENTRY = 0;
  33. ARGBEGIN {
  34. default:
  35. c = ARGC();
  36. if(c >= 0 && c < sizeof(debug))
  37. debug[c]++;
  38. break;
  39. case 'o': /* output to (next arg) */
  40. outfile = ARGF();
  41. break;
  42. case 'E':
  43. a = ARGF();
  44. if(a)
  45. INITENTRY = a;
  46. break;
  47. case 'H':
  48. a = ARGF();
  49. if(a)
  50. HEADTYPE = atolwhex(a);
  51. break;
  52. case 'T':
  53. a = ARGF();
  54. if(a)
  55. INITTEXT = atolwhex(a);
  56. break;
  57. case 'D':
  58. a = ARGF();
  59. if(a)
  60. INITDAT = atolwhex(a);
  61. break;
  62. case 'R':
  63. a = ARGF();
  64. if(a)
  65. INITRND = atolwhex(a);
  66. break;
  67. } ARGEND
  68. USED(argc);
  69. if(*argv == 0) {
  70. diag("usage: 6l [-options] objects");
  71. errorexit();
  72. }
  73. if(!debug['9'] && !debug['U'] && !debug['B'])
  74. debug[DEFAULT] = 1;
  75. if(HEADTYPE == -1) {
  76. if(debug['U'])
  77. HEADTYPE = 1;
  78. if(debug['B'])
  79. HEADTYPE = 2;
  80. if(debug['9'])
  81. HEADTYPE = 2;
  82. }
  83. switch(HEADTYPE) {
  84. default:
  85. diag("unknown -H option");
  86. errorexit();
  87. case 0: /* this is garbage */
  88. HEADR = 20L+56L;
  89. if(INITTEXT == -1)
  90. INITTEXT = 0x40004CL;
  91. if(INITDAT == -1)
  92. INITDAT = 0x10000000L;
  93. if(INITRND == -1)
  94. INITRND = 0;
  95. break;
  96. case 1: /* is unix coff */
  97. HEADR = 0xd0L;
  98. if(INITTEXT == -1)
  99. INITTEXT = 0xd0;
  100. if(INITDAT == -1)
  101. INITDAT = 0x400000;
  102. if(INITRND == -1)
  103. INITRND = 0;
  104. break;
  105. case 2: /* plan 9 */
  106. HEADR = 32L;
  107. if(INITTEXT == -1)
  108. INITTEXT = 4096+32;
  109. if(INITDAT == -1)
  110. INITDAT = 0;
  111. if(INITRND == -1)
  112. INITRND = 4096;
  113. break;
  114. case 3: /* msdos boot */
  115. HEADR = 0;
  116. if(INITTEXT == -1)
  117. INITTEXT = 0x0100;
  118. if(INITDAT == -1)
  119. INITDAT = 0;
  120. if(INITRND == -1)
  121. INITRND = 4;
  122. break;
  123. case 4: /* is 960 coff */
  124. HEADR = 0x8CL;
  125. if(INITTEXT == -1)
  126. INITTEXT = 0x8000;
  127. if(INITDAT == -1)
  128. INITDAT = 0;
  129. if(INITRND == -1)
  130. INITRND = 4;
  131. break;
  132. }
  133. if(INITDAT != 0 && INITRND != 0)
  134. print("warning: -D0x%lux is ignored because of -R0x%lux\n",
  135. INITDAT, INITRND);
  136. if(debug['v'])
  137. Bprint(&bso, "HEADER = -H0x%ld -T0x%lux -D0x%lux -R0x%lux\n",
  138. HEADTYPE, INITTEXT, INITDAT, INITRND);
  139. Bflush(&bso);
  140. for(i=1; optab[i].as; i++)
  141. if(i != optab[i].as) {
  142. diag("phase error in optab: %d", i);
  143. errorexit();
  144. }
  145. maxop = i;
  146. for(i=0; i<Ymax; i++)
  147. ycover[i*Ymax + i] = 1;
  148. ycover[Yi5*Ymax + Yri5] = 1;
  149. ycover[Yr*Ymax + Yri5] = 1;
  150. ycover[Yi5*Ymax + Ynri5] = 1;
  151. ycover[Yr*Ymax + Ynri5] = 1;
  152. ycover[Ynone*Ymax + Ynri5] = 1;
  153. ycover[Yi5*Ymax + Yi32] = 1;
  154. zprg.link = P;
  155. zprg.cond = P;
  156. zprg.back = 2;
  157. zprg.as = AGOK;
  158. zprg.type = D_NONE;
  159. zprg.offset = 0;
  160. zprg.from.type = D_NONE;
  161. zprg.from.index = D_NONE;
  162. zprg.from.scale = 1;
  163. zprg.to = zprg.from;
  164. pcstr = "%.6lux ";
  165. nuxiinit();
  166. histgen = 0;
  167. textp = P;
  168. datap = P;
  169. edatap = P;
  170. pc = 0;
  171. cout = create(outfile, 1, 0775);
  172. if(cout < 0) {
  173. diag("cannot create %s", outfile);
  174. errorexit();
  175. }
  176. version = 0;
  177. cbp = buf.cbuf;
  178. cbc = sizeof(buf.cbuf);
  179. firstp = prg();
  180. lastp = firstp;
  181. if(INITENTRY == 0) {
  182. INITENTRY = "_main";
  183. if(debug['p'])
  184. INITENTRY = "_mainp";
  185. if(!debug['l'])
  186. lookup(INITENTRY, 0)->type = SXREF;
  187. } else
  188. lookup(INITENTRY, 0)->type = SXREF;
  189. while(*argv)
  190. objfile(*argv++);
  191. if(!debug['l'])
  192. loadlib(0, libraryp);
  193. firstp = firstp->link;
  194. if(firstp == P)
  195. errorexit();
  196. patch();
  197. if(debug['p'])
  198. doprof2();
  199. dodata();
  200. follow();
  201. noops();
  202. span();
  203. doinit();
  204. asmb();
  205. undef();
  206. if(debug['v']) {
  207. Bprint(&bso, "%5.2f cpu time\n", cputime());
  208. Bprint(&bso, "%ld symbols\n", nsymbol);
  209. Bprint(&bso, "%ld memory used\n", thunk);
  210. Bprint(&bso, "%d sizeof adr\n", sizeof(Adr));
  211. Bprint(&bso, "%d sizeof prog\n", sizeof(Prog));
  212. }
  213. Bflush(&bso);
  214. errorexit();
  215. }
  216. void
  217. loadlib(int beg, int end)
  218. {
  219. int i, t;
  220. for(i=end-1; i>=beg; i--) {
  221. t = libraryp;
  222. if(debug['v'])
  223. Bprint(&bso, "%5.2f autolib: %s\n", cputime(), library[i]);
  224. objfile(library[i]);
  225. if(t != libraryp)
  226. loadlib(t, libraryp);
  227. }
  228. }
  229. void
  230. errorexit(void)
  231. {
  232. if(nerrors) {
  233. if(cout >= 0)
  234. remove(outfile);
  235. exits("error");
  236. }
  237. exits(0);
  238. }
  239. void
  240. objfile(char *file)
  241. {
  242. long off, esym, cnt, l;
  243. int f, work;
  244. Sym *s;
  245. char magbuf[SARMAG];
  246. char name[100], pname[150];
  247. struct ar_hdr arhdr;
  248. char *e, *start, *stop;
  249. if(file[0] == '-' && file[1] == 'l') {
  250. if(debug['9'])
  251. sprint(name, "/%s/lib/lib", thestring);
  252. else
  253. sprint(name, "/usr/%clib/lib", thechar);
  254. strcat(name, file+2);
  255. strcat(name, ".a");
  256. file = name;
  257. }
  258. if(debug['v'])
  259. Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
  260. Bflush(&bso);
  261. f = open(file, 0);
  262. if(f < 0) {
  263. diag("cannot open file: %s", file);
  264. errorexit();
  265. }
  266. l = read(f, magbuf, SARMAG);
  267. if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
  268. /* load it as a regular file */
  269. l = seek(f, 0L, 2);
  270. seek(f, 0L, 0);
  271. ldobj(f, l, file);
  272. close(f);
  273. return;
  274. }
  275. if(debug['v'])
  276. Bprint(&bso, "%5.2f ldlib: %s\n", cputime(), file);
  277. l = read(f, &arhdr, SAR_HDR);
  278. if(l != SAR_HDR) {
  279. diag("%s: short read on archive file symbol header", file);
  280. goto out;
  281. }
  282. if(strncmp(arhdr.name, symname, strlen(symname))) {
  283. diag("%s: first entry not symbol header", file);
  284. goto out;
  285. }
  286. esym = SARMAG + SAR_HDR + atolwhex(arhdr.size);
  287. off = SARMAG + SAR_HDR;
  288. /*
  289. * just bang the whole symbol file into memory
  290. */
  291. seek(f, off, 0);
  292. cnt = esym - off;
  293. start = malloc(cnt + 10);
  294. cnt = read(f, start, cnt);
  295. if(cnt <= 0){
  296. close(f);
  297. return;
  298. }
  299. stop = &start[cnt];
  300. memset(stop, 0, 10);
  301. work = 1;
  302. while(work){
  303. if(debug['v'])
  304. Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file);
  305. Bflush(&bso);
  306. work = 0;
  307. for(e = start; e < stop; e = strchr(e+5, 0) + 1) {
  308. s = lookup(e+5, 0);
  309. if(s->type != SXREF)
  310. continue;
  311. sprint(pname, "%s(%s)", file, s->name);
  312. if(debug['v'])
  313. Bprint(&bso, "%5.2f library: %s\n", cputime(), pname);
  314. Bflush(&bso);
  315. l = e[1] & 0xff;
  316. l |= (e[2] & 0xff) << 8;
  317. l |= (e[3] & 0xff) << 16;
  318. l |= (e[4] & 0xff) << 24;
  319. seek(f, l, 0);
  320. l = read(f, &arhdr, SAR_HDR);
  321. if(l != SAR_HDR)
  322. goto bad;
  323. if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
  324. goto bad;
  325. l = atolwhex(arhdr.size);
  326. ldobj(f, l, pname);
  327. if(s->type == SXREF) {
  328. diag("%s: failed to load: %s", file, s->name);
  329. errorexit();
  330. }
  331. work = 1;
  332. }
  333. }
  334. return;
  335. bad:
  336. diag("%s: bad or out of date archive", file);
  337. out:
  338. close(f);
  339. }
  340. int
  341. zaddr(uchar *p, Adr *a, Sym *h[])
  342. {
  343. int c, t, i;
  344. long l;
  345. Sym *s;
  346. Auto *u;
  347. t = p[0];
  348. c = 1;
  349. if(t & T_INDEX) {
  350. a->index = p[c];
  351. a->scale = p[c+1];
  352. c += 2;
  353. } else {
  354. a->index = D_NONE;
  355. a->scale = 0;
  356. }
  357. a->offset = 0;
  358. if(t & T_OFFSET) {
  359. a->offset = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
  360. c += 4;
  361. }
  362. a->sym = S;
  363. if(t & T_SYM) {
  364. a->sym = h[p[c]];
  365. c++;
  366. }
  367. a->type = D_NONE;
  368. if(t & T_FCONST) {
  369. a->ieee.l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
  370. a->ieee.h = p[c+4] | (p[c+5]<<8) | (p[c+6]<<16) | (p[c+7]<<24);
  371. c += 8;
  372. a->type = D_FCONST;
  373. } else
  374. if(t & T_SCONST) {
  375. for(i=0; i<NSNAME; i++)
  376. a->scon[i] = p[c+i];
  377. c += NSNAME;
  378. a->type = D_SCONST;
  379. }
  380. if(t & T_TYPE) {
  381. a->type = p[c];
  382. c++;
  383. }
  384. s = a->sym;
  385. if(s == S)
  386. return c;
  387. t = a->type;
  388. if(t != D_AUTO && t != D_PARAM)
  389. return c;
  390. l = a->offset;
  391. for(u=curauto; u; u=u->link) {
  392. if(u->sym == s)
  393. if(u->type == t) {
  394. if(u->offset > l)
  395. u->offset = l;
  396. return c;
  397. }
  398. }
  399. while(nhunk < sizeof(Auto))
  400. gethunk();
  401. u = (Auto*)hunk;
  402. nhunk -= sizeof(Auto);
  403. hunk += sizeof(Auto);
  404. u->link = curauto;
  405. curauto = u;
  406. u->sym = s;
  407. u->offset = l;
  408. u->type = t;
  409. return c;
  410. }
  411. void
  412. addlib(char *obj)
  413. {
  414. char name[1024], comp[256], *p;
  415. int i;
  416. if(histfrogp <= 0)
  417. return;
  418. if(histfrog[0]->name[1] == '/') {
  419. sprint(name, "");
  420. i = 1;
  421. } else
  422. if(histfrog[0]->name[1] == '.') {
  423. sprint(name, ".");
  424. i = 0;
  425. } else {
  426. if(debug['9'])
  427. sprint(name, "/%s/lib", thestring);
  428. else
  429. sprint(name, "/usr/%clib", thechar);
  430. i = 0;
  431. }
  432. for(; i<histfrogp; i++) {
  433. snprint(comp, sizeof comp, histfrog[i]->name+1);
  434. for(;;) {
  435. p = strstr(comp, "$O");
  436. if(p == 0)
  437. break;
  438. memmove(p+1, p+2, strlen(p+2)+1);
  439. p[0] = thechar;
  440. }
  441. for(;;) {
  442. p = strstr(comp, "$M");
  443. if(p == 0)
  444. break;
  445. if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) {
  446. diag("library component too long");
  447. return;
  448. }
  449. memmove(p+strlen(thestring), p+2, strlen(p+2)+1);
  450. memmove(p, thestring, strlen(thestring));
  451. }
  452. if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) {
  453. diag("library component too long");
  454. return;
  455. }
  456. strcat(name, "/");
  457. strcat(name, comp);
  458. }
  459. for(i=0; i<libraryp; i++)
  460. if(strcmp(name, library[i]) == 0)
  461. return;
  462. if(libraryp == nelem(library)){
  463. diag("too many autolibs; skipping %s", name);
  464. return;
  465. }
  466. p = malloc(strlen(name) + 1);
  467. strcpy(p, name);
  468. library[libraryp] = p;
  469. p = malloc(strlen(obj) + 1);
  470. strcpy(p, obj);
  471. libraryobj[libraryp] = p;
  472. libraryp++;
  473. }
  474. void
  475. addhist(long line, int type)
  476. {
  477. Auto *u;
  478. Sym *s;
  479. int i, j, k;
  480. u = malloc(sizeof(Auto));
  481. s = malloc(sizeof(Sym));
  482. s->name = malloc(2*(histfrogp+1) + 1);
  483. u->sym = s;
  484. u->type = type;
  485. u->offset = line;
  486. u->link = curhist;
  487. curhist = u;
  488. j = 1;
  489. for(i=0; i<histfrogp; i++) {
  490. k = histfrog[i]->value;
  491. s->name[j+0] = k>>8;
  492. s->name[j+1] = k;
  493. j += 2;
  494. }
  495. }
  496. void
  497. histtoauto(void)
  498. {
  499. Auto *l;
  500. while(l = curhist) {
  501. curhist = l->link;
  502. l->link = curauto;
  503. curauto = l;
  504. }
  505. }
  506. void
  507. collapsefrog(Sym *s)
  508. {
  509. int i;
  510. /*
  511. * bad encoding of path components only allows
  512. * MAXHIST components. if there is an overflow,
  513. * first try to collapse xxx/..
  514. */
  515. for(i=1; i<histfrogp; i++)
  516. if(strcmp(histfrog[i]->name+1, "..") == 0) {
  517. memmove(histfrog+i-1, histfrog+i+1,
  518. (histfrogp-i-1)*sizeof(histfrog[0]));
  519. histfrogp--;
  520. goto out;
  521. }
  522. /*
  523. * next try to collapse .
  524. */
  525. for(i=0; i<histfrogp; i++)
  526. if(strcmp(histfrog[i]->name+1, ".") == 0) {
  527. memmove(histfrog+i, histfrog+i+1,
  528. (histfrogp-i-1)*sizeof(histfrog[0]));
  529. goto out;
  530. }
  531. /*
  532. * last chance, just truncate from front
  533. */
  534. memmove(histfrog+0, histfrog+1,
  535. (histfrogp-1)*sizeof(histfrog[0]));
  536. out:
  537. histfrog[histfrogp-1] = s;
  538. }
  539. uchar*
  540. readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
  541. {
  542. int n;
  543. n = stop - good;
  544. memmove(buf, good, stop - good);
  545. stop = buf + n;
  546. n = MAXIO - n;
  547. if(n > max)
  548. n = max;
  549. n = read(f, stop, n);
  550. if(n <= 0)
  551. return 0;
  552. return stop + n;
  553. }
  554. void
  555. ldobj(int f, long c, char *pn)
  556. {
  557. Prog *p;
  558. Sym *h[NSYM], *s;
  559. int v, o, r;
  560. long ipc;
  561. uchar *bloc, *bsize, *stop;
  562. bsize = buf.xbuf;
  563. bloc = buf.xbuf;
  564. newloop:
  565. memset(h, 0, sizeof(h));
  566. version++;
  567. histfrogp = 0;
  568. ipc = pc;
  569. loop:
  570. if(c <= 0)
  571. goto eof;
  572. r = bsize - bloc;
  573. if(r < 100 && r < c) { /* enough for largest prog */
  574. bsize = readsome(f, buf.xbuf, bloc, bsize, c);
  575. if(bsize == 0)
  576. goto eof;
  577. bloc = buf.xbuf;
  578. goto loop;
  579. }
  580. o = bloc[0];
  581. if(o <= 0 || o >= maxop) {
  582. if(o < 0)
  583. goto eof;
  584. diag("%s: opcode out of range %d", pn, o);
  585. print(" probably not a .8 file\n");
  586. errorexit();
  587. }
  588. if(o == ANAME) {
  589. stop = memchr(&bloc[3], 0, bsize-&bloc[3]);
  590. if(stop == 0){
  591. bsize = readsome(f, buf.xbuf, bloc, bsize, c);
  592. if(bsize == 0)
  593. goto eof;
  594. bloc = buf.xbuf;
  595. stop = memchr(&bloc[3], 0, bsize-&bloc[3]);
  596. if(stop == 0){
  597. fprint(2, "%s: name too long\n", pn);
  598. errorexit();
  599. }
  600. }
  601. v = bloc[1]; /* type */
  602. o = bloc[2]; /* sym */
  603. bloc += 3;
  604. c -= 3;
  605. r = 0;
  606. if(v == D_STATIC)
  607. r = version;
  608. s = lookup((char*)bloc, r);
  609. c -= &stop[1] - bloc;
  610. bloc = stop + 1;
  611. if(debug['W'])
  612. print(" ANAME %s\n", s->name);
  613. h[o] = s;
  614. if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
  615. s->type = SXREF;
  616. if(v == D_FILE) {
  617. if(s->type != SFILE) {
  618. histgen++;
  619. s->type = SFILE;
  620. s->value = histgen;
  621. }
  622. if(histfrogp < MAXHIST) {
  623. histfrog[histfrogp] = s;
  624. histfrogp++;
  625. } else
  626. collapsefrog(s);
  627. }
  628. goto loop;
  629. }
  630. while(nhunk < sizeof(Prog))
  631. gethunk();
  632. p = (Prog*)hunk;
  633. nhunk -= sizeof(Prog);
  634. hunk += sizeof(Prog);
  635. p->as = o;
  636. p->line = bloc[1] | (bloc[2] << 8) | (bloc[3] << 16) | (bloc[4] << 24);
  637. p->back = 2;
  638. r = bloc[5];
  639. p->type = D_NONE;
  640. p->offset = 0;
  641. if(r > 0) {
  642. if(r >= 33) {
  643. p->type = D_CONST;
  644. p->offset = r - 33;
  645. } else
  646. p->type = D_R0 + (r - 1);
  647. }
  648. r = zaddr(bloc+6, &p->from, h) + 6;
  649. r += zaddr(bloc+r, &p->to, h);
  650. bloc += r;
  651. c -= r;
  652. if(debug['W'])
  653. print("%P\n", p);
  654. switch(p->as) {
  655. case AHISTORY:
  656. if(p->to.offset == -1) {
  657. addlib(pn);
  658. histfrogp = 0;
  659. goto loop;
  660. }
  661. addhist(p->line, D_FILE);
  662. if(p->to.offset)
  663. addhist(p->to.offset, D_FILE1);
  664. histfrogp = 0;
  665. goto loop;
  666. case AEND:
  667. histtoauto();
  668. if(curtext != P)
  669. curtext->to.autom = curauto;
  670. curauto = 0;
  671. curtext = P;
  672. if(c)
  673. goto newloop;
  674. return;
  675. case AGLOBL:
  676. s = p->from.sym;
  677. if(s->type == 0 || s->type == SXREF) {
  678. s->type = SBSS;
  679. s->value = 0;
  680. }
  681. if(s->type != SBSS) {
  682. diag("%s: redefinition: %s in %s",
  683. pn, s->name, TNAME);
  684. s->type = SBSS;
  685. s->value = 0;
  686. }
  687. if(p->to.offset > s->value)
  688. s->value = p->to.offset;
  689. goto loop;
  690. case ADATA:
  691. if(edatap == P)
  692. datap = p;
  693. else
  694. edatap->link = p;
  695. edatap = p;
  696. p->link = P;
  697. goto loop;
  698. case AGOK:
  699. diag("%s: GOK opcode in %s", pn, TNAME);
  700. pc++;
  701. goto loop;
  702. case ATEXT:
  703. if(curtext != P) {
  704. histtoauto();
  705. curtext->to.autom = curauto;
  706. curauto = 0;
  707. }
  708. curtext = p;
  709. lastp->link = p;
  710. lastp = p;
  711. p->pc = pc;
  712. s = p->from.sym;
  713. if(s == S) {
  714. diag("%s: no TEXT symbol: %P", pn, p);
  715. errorexit();
  716. }
  717. if(s->type != 0 && s->type != SXREF)
  718. diag("%s: redefinition: %s", pn, s->name);
  719. s->type = STEXT;
  720. s->value = p->pc;
  721. pc++;
  722. p->cond = P;
  723. if(textp == P) {
  724. textp = p;
  725. etextp = p;
  726. goto loop;
  727. }
  728. etextp->cond = p;
  729. etextp = p;
  730. goto loop;
  731. case AADDO:
  732. if(p->from.type == D_CONST)
  733. if(p->from.offset < 0) {
  734. p->as = ASUBO;
  735. p->from.offset = -p->from.offset;
  736. }
  737. goto casdef;
  738. case AADDI:
  739. if(p->from.type == D_CONST)
  740. if(p->from.offset < 0) {
  741. p->as = ASUBI;
  742. p->from.offset = -p->from.offset;
  743. }
  744. goto casdef;
  745. case ASUBO:
  746. if(p->from.type == D_CONST)
  747. if(p->from.offset < 0) {
  748. p->as = AADDO;
  749. p->from.offset = -p->from.offset;
  750. }
  751. goto casdef;
  752. case ASUBI:
  753. if(p->from.type == D_CONST)
  754. if(p->from.offset < 0) {
  755. p->as = AADDI;
  756. p->from.offset = -p->from.offset;
  757. }
  758. goto casdef;
  759. casdef:
  760. default:
  761. if(p->to.type == D_BRANCH)
  762. p->to.offset += ipc;
  763. lastp->link = p;
  764. lastp = p;
  765. p->pc = pc;
  766. pc++;
  767. goto loop;
  768. }
  769. goto loop;
  770. eof:
  771. diag("truncated object file: %s", pn);
  772. }
  773. Sym*
  774. lookup(char *symb, int v)
  775. {
  776. Sym *s;
  777. char *p;
  778. long h;
  779. int c, l;
  780. h = v;
  781. for(p=symb; c = *p; p++)
  782. h = h+h+h + c;
  783. l = (p - symb) + 1;
  784. if(h < 0)
  785. h = ~h;
  786. h %= NHASH;
  787. for(s = hash[h]; s != S; s = s->link)
  788. if(s->version == v)
  789. if(memcmp(s->name, symb, l) == 0)
  790. return s;
  791. while(nhunk < sizeof(Sym))
  792. gethunk();
  793. s = (Sym*)hunk;
  794. nhunk -= sizeof(Sym);
  795. hunk += sizeof(Sym);
  796. s->name = malloc(l);
  797. memmove(s->name, symb, l);
  798. s->link = hash[h];
  799. s->type = 0;
  800. s->version = v;
  801. s->value = 0;
  802. hash[h] = s;
  803. nsymbol++;
  804. return s;
  805. }
  806. Prog*
  807. prg(void)
  808. {
  809. Prog *p;
  810. while(nhunk < sizeof(Prog))
  811. gethunk();
  812. p = (Prog*)hunk;
  813. nhunk -= sizeof(Prog);
  814. hunk += sizeof(Prog);
  815. *p = zprg;
  816. return p;
  817. }
  818. Prog*
  819. copyp(Prog *q)
  820. {
  821. Prog *p;
  822. p = prg();
  823. *p = *q;
  824. return p;
  825. }
  826. Prog*
  827. appendp(Prog *q)
  828. {
  829. Prog *p;
  830. p = prg();
  831. p->link = q->link;
  832. q->link = p;
  833. p->line = q->line;
  834. return p;
  835. }
  836. void
  837. gethunk(void)
  838. {
  839. char *h;
  840. long nh;
  841. nh = NHUNK;
  842. if(thunk >= 5L*NHUNK) {
  843. nh = 5L*NHUNK;
  844. if(thunk >= 25L*NHUNK)
  845. nh = 25L*NHUNK;
  846. }
  847. h = sbrk(nh);
  848. if(h == (char*)-1) {
  849. diag("out of memory");
  850. errorexit();
  851. }
  852. hunk = h;
  853. nhunk = nh;
  854. thunk += nh;
  855. }
  856. void
  857. doprof1(void)
  858. {}
  859. void
  860. doprof2(void)
  861. {}
  862. void
  863. nuxiinit(void)
  864. {
  865. int i, c;
  866. for(i=0; i<4; i++) {
  867. c = find1(0x04030201L, i+1);
  868. if(i < 2)
  869. inuxi2[i] = c;
  870. if(i < 1)
  871. inuxi1[i] = c;
  872. inuxi4[i] = c;
  873. fnuxi4[i] = c;
  874. fnuxi8[i] = c;
  875. fnuxi8[i+4] = c+4;
  876. }
  877. if(debug['v']) {
  878. Bprint(&bso, "inuxi = ");
  879. for(i=0; i<1; i++)
  880. Bprint(&bso, "%d", inuxi1[i]);
  881. Bprint(&bso, " ");
  882. for(i=0; i<2; i++)
  883. Bprint(&bso, "%d", inuxi2[i]);
  884. Bprint(&bso, " ");
  885. for(i=0; i<4; i++)
  886. Bprint(&bso, "%d", inuxi4[i]);
  887. Bprint(&bso, "\nfnuxi = ");
  888. for(i=0; i<4; i++)
  889. Bprint(&bso, "%d", fnuxi4[i]);
  890. Bprint(&bso, " ");
  891. for(i=0; i<8; i++)
  892. Bprint(&bso, "%d", fnuxi8[i]);
  893. Bprint(&bso, "\n");
  894. }
  895. Bflush(&bso);
  896. }
  897. int
  898. find1(long l, int c)
  899. {
  900. char *p;
  901. int i;
  902. p = (char*)&l;
  903. for(i=0; i<4; i++)
  904. if(*p++ == c)
  905. return i;
  906. return 0;
  907. }
  908. int
  909. find2(long l, int c)
  910. {
  911. short *p;
  912. int i;
  913. p = (short*)&l;
  914. for(i=0; i<4; i+=2) {
  915. if(((*p >> 8) & 0xff) == c)
  916. return i;
  917. if((*p++ & 0xff) == c)
  918. return i+1;
  919. }
  920. return 0;
  921. }
  922. long
  923. ieeedtof(Ieee *e)
  924. {
  925. int exp;
  926. long v;
  927. if(e->h == 0)
  928. return 0;
  929. exp = (e->h>>20) & ((1L<<11)-1L);
  930. exp -= (1L<<10) - 2L;
  931. v = (e->h & 0xfffffL) << 3;
  932. v |= (e->l >> 29) & 0x7L;
  933. if((e->l >> 28) & 1) {
  934. v++;
  935. if(v & 0x800000L) {
  936. v = (v & 0x7fffffL) >> 1;
  937. exp++;
  938. }
  939. }
  940. if(exp <= -126 || exp >= 130)
  941. diag("double fp to single fp overflow");
  942. v |= ((exp + 126) & 0xffL) << 23;
  943. v |= e->h & 0x80000000L;
  944. return v;
  945. }
  946. double
  947. ieeedtod(Ieee *ieee)
  948. {
  949. Ieee e;
  950. double fr;
  951. int exp;
  952. if(ieee->h & (1L<<31)) {
  953. e.h = ieee->h & ~(1L<<31);
  954. e.l = ieee->l;
  955. return -ieeedtod(&e);
  956. }
  957. if(ieee->l == 0 && ieee->h == 0)
  958. return 0;
  959. fr = ieee->l & ((1L<<16)-1L);
  960. fr /= 1L<<16;
  961. fr += (ieee->l>>16) & ((1L<<16)-1L);
  962. fr /= 1L<<16;
  963. fr += (ieee->h & (1L<<20)-1L) | (1L<<20);
  964. fr /= 1L<<21;
  965. exp = (ieee->h>>20) & ((1L<<11)-1L);
  966. exp -= (1L<<10) - 2L;
  967. return ldexp(fr, exp);
  968. }
  969. /*
  970. * fake malloc
  971. */
  972. void*
  973. malloc(ulong n)
  974. {
  975. void *p;
  976. while(n & 7)
  977. n++;
  978. while(nhunk < n)
  979. gethunk();
  980. p = hunk;
  981. nhunk -= n;
  982. hunk += n;
  983. return p;
  984. }
  985. void
  986. free(void *p)
  987. {
  988. USED(p);
  989. }
  990. void*
  991. calloc(ulong m, ulong n)
  992. {
  993. void *p;
  994. n *= m;
  995. p = malloc(n);
  996. memset(p, 0, n);
  997. return p;
  998. }
  999. void*
  1000. realloc(void *p, ulong n)
  1001. {
  1002. fprint(2, "realloc(0x%p %ld) called\n", p, n);
  1003. abort();
  1004. return 0;
  1005. }