obj.c 23 KB

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