obj.c 24 KB

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