obj.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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: 1l [-options] objects\n");
  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\n", 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\n", 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\n", file);
  265. goto out;
  266. }
  267. if(strncmp(arhdr.name, symname, strlen(symname))) {
  268. diag("%s: first entry not symbol header\n", 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\n", 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\n", 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\n", 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\n", 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\n", 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\n", 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\n", 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\n", 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\n", p);
  733. break;
  734. }
  735. if(di == S) {
  736. diag("INIT without previous DYNT\n%P\n", 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\n", p);
  747. break;
  748. }
  749. p->link = datap;
  750. datap = p;
  751. break;
  752. case AGOK:
  753. diag("unknown opcode\n%P\n", 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\n", 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\n", 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\n", 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 = sbrk(nh);
  938. if(h == (char*)-1) {
  939. diag("out of memory\n");
  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\n");
  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\n");
  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. }
  1197. /*
  1198. * fake malloc
  1199. */
  1200. void*
  1201. malloc(ulong n)
  1202. {
  1203. void *p;
  1204. while(n & 7)
  1205. n++;
  1206. while(nhunk < n)
  1207. gethunk();
  1208. p = hunk;
  1209. nhunk -= n;
  1210. hunk += n;
  1211. return p;
  1212. }
  1213. void
  1214. free(void *p)
  1215. {
  1216. USED(p);
  1217. }
  1218. void*
  1219. calloc(ulong m, ulong n)
  1220. {
  1221. void *p;
  1222. n *= m;
  1223. p = malloc(n);
  1224. memset(p, 0, n);
  1225. return p;
  1226. }
  1227. void*
  1228. realloc(void*, ulong)
  1229. {
  1230. fprint(2, "realloc called\n");
  1231. abort();
  1232. return 0;
  1233. }