obj.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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. case AADDL:
  909. if(p->from.type == D_CONST)
  910. if(p->from.offset < 0) {
  911. p->as = ASUBL;
  912. p->from.offset = -p->from.offset;
  913. }
  914. goto casdef;
  915. case ASUBL:
  916. if(p->from.type == D_CONST)
  917. if(p->from.offset < 0) {
  918. p->as = AADDL;
  919. p->from.offset = -p->from.offset;
  920. }
  921. goto casdef;
  922. casdef:
  923. default:
  924. if(skip)
  925. nopout(p);
  926. if(p->to.type == D_BRANCH)
  927. p->to.offset += ipc;
  928. lastp->link = p;
  929. lastp = p;
  930. p->pc = pc;
  931. pc++;
  932. goto loop;
  933. }
  934. goto loop;
  935. eof:
  936. diag("truncated object file: %s", pn);
  937. }
  938. Sym*
  939. lookup(char *symb, int v)
  940. {
  941. Sym *s;
  942. char *p;
  943. long h;
  944. int l, c;
  945. h = v;
  946. for(p=symb; c = *p; p++)
  947. h = h+h+h + c;
  948. l = (p - symb) + 1;
  949. if(h < 0)
  950. h = ~h;
  951. h %= NHASH;
  952. for(s = hash[h]; s != S; s = s->link)
  953. if(s->version == v)
  954. if(memcmp(s->name, symb, l) == 0)
  955. return s;
  956. while(nhunk < sizeof(Sym))
  957. gethunk();
  958. s = (Sym*)hunk;
  959. nhunk -= sizeof(Sym);
  960. hunk += sizeof(Sym);
  961. s->name = malloc(l + 1);
  962. memmove(s->name, symb, l);
  963. s->link = hash[h];
  964. s->type = 0;
  965. s->version = v;
  966. s->value = 0;
  967. hash[h] = s;
  968. nsymbol++;
  969. return s;
  970. }
  971. Prog*
  972. prg(void)
  973. {
  974. Prog *p;
  975. while(nhunk < sizeof(Prog))
  976. gethunk();
  977. p = (Prog*)hunk;
  978. nhunk -= sizeof(Prog);
  979. hunk += sizeof(Prog);
  980. *p = zprg;
  981. return p;
  982. }
  983. Prog*
  984. copyp(Prog *q)
  985. {
  986. Prog *p;
  987. p = prg();
  988. *p = *q;
  989. return p;
  990. }
  991. Prog*
  992. appendp(Prog *q)
  993. {
  994. Prog *p;
  995. p = prg();
  996. p->link = q->link;
  997. q->link = p;
  998. p->line = q->line;
  999. return p;
  1000. }
  1001. void
  1002. gethunk(void)
  1003. {
  1004. char *h;
  1005. long nh;
  1006. nh = NHUNK;
  1007. if(thunk >= 5L*NHUNK) {
  1008. nh = 5L*NHUNK;
  1009. if(thunk >= 25L*NHUNK)
  1010. nh = 25L*NHUNK;
  1011. }
  1012. h = mysbrk(nh);
  1013. if(h == (char*)-1) {
  1014. diag("out of memory");
  1015. errorexit();
  1016. }
  1017. hunk = h;
  1018. nhunk = nh;
  1019. thunk += nh;
  1020. }
  1021. void
  1022. doprof1(void)
  1023. {
  1024. Sym *s;
  1025. long n;
  1026. Prog *p, *q;
  1027. if(debug['v'])
  1028. Bprint(&bso, "%5.2f profile 1\n", cputime());
  1029. Bflush(&bso);
  1030. s = lookup("__mcount", 0);
  1031. n = 1;
  1032. for(p = firstp->link; p != P; p = p->link) {
  1033. if(p->as == ATEXT) {
  1034. q = prg();
  1035. q->line = p->line;
  1036. q->link = datap;
  1037. datap = q;
  1038. q->as = ADATA;
  1039. q->from.type = D_EXTERN;
  1040. q->from.offset = n*4;
  1041. q->from.sym = s;
  1042. q->from.scale = 4;
  1043. q->to = p->from;
  1044. q->to.type = D_CONST;
  1045. q = prg();
  1046. q->line = p->line;
  1047. q->pc = p->pc;
  1048. q->link = p->link;
  1049. p->link = q;
  1050. p = q;
  1051. p->as = AADDL;
  1052. p->from.type = D_CONST;
  1053. p->from.offset = 1;
  1054. p->to.type = D_EXTERN;
  1055. p->to.sym = s;
  1056. p->to.offset = n*4 + 4;
  1057. n += 2;
  1058. continue;
  1059. }
  1060. }
  1061. q = prg();
  1062. q->line = 0;
  1063. q->link = datap;
  1064. datap = q;
  1065. q->as = ADATA;
  1066. q->from.type = D_EXTERN;
  1067. q->from.sym = s;
  1068. q->from.scale = 4;
  1069. q->to.type = D_CONST;
  1070. q->to.offset = n;
  1071. s->type = SBSS;
  1072. s->value = n*4;
  1073. }
  1074. void
  1075. doprof2(void)
  1076. {
  1077. Sym *s2, *s4;
  1078. Prog *p, *q, *ps2, *ps4;
  1079. if(debug['v'])
  1080. Bprint(&bso, "%5.2f profile 2\n", cputime());
  1081. Bflush(&bso);
  1082. s2 = lookup("_profin", 0);
  1083. s4 = lookup("_profout", 0);
  1084. if(s2->type != STEXT || s4->type != STEXT) {
  1085. diag("_profin/_profout not defined");
  1086. return;
  1087. }
  1088. ps2 = P;
  1089. ps4 = P;
  1090. for(p = firstp; p != P; p = p->link) {
  1091. if(p->as == ATEXT) {
  1092. if(p->from.sym == s2) {
  1093. p->from.scale = 1;
  1094. ps2 = p;
  1095. }
  1096. if(p->from.sym == s4) {
  1097. p->from.scale = 1;
  1098. ps4 = p;
  1099. }
  1100. }
  1101. }
  1102. for(p = firstp; p != P; p = p->link) {
  1103. if(p->as == ATEXT) {
  1104. curtext = p;
  1105. if(p->from.scale & NOPROF) { /* dont profile */
  1106. for(;;) {
  1107. q = p->link;
  1108. if(q == P)
  1109. break;
  1110. if(q->as == ATEXT)
  1111. break;
  1112. p = q;
  1113. }
  1114. continue;
  1115. }
  1116. /*
  1117. * JMPL profin
  1118. */
  1119. q = prg();
  1120. q->line = p->line;
  1121. q->pc = p->pc;
  1122. q->link = p->link;
  1123. p->link = q;
  1124. p = q;
  1125. p->as = ACALL;
  1126. p->to.type = D_BRANCH;
  1127. p->pcond = ps2;
  1128. p->to.sym = s2;
  1129. continue;
  1130. }
  1131. if(p->as == ARET) {
  1132. /*
  1133. * RET
  1134. */
  1135. q = prg();
  1136. q->as = ARET;
  1137. q->from = p->from;
  1138. q->to = p->to;
  1139. q->link = p->link;
  1140. p->link = q;
  1141. /*
  1142. * JAL profout
  1143. */
  1144. p->as = ACALL;
  1145. p->from = zprg.from;
  1146. p->to = zprg.to;
  1147. p->to.type = D_BRANCH;
  1148. p->pcond = ps4;
  1149. p->to.sym = s4;
  1150. p = q;
  1151. continue;
  1152. }
  1153. }
  1154. }
  1155. void
  1156. nuxiinit(void)
  1157. {
  1158. int i, c;
  1159. for(i=0; i<4; i++) {
  1160. c = find1(0x04030201L, i+1);
  1161. if(i < 2)
  1162. inuxi2[i] = c;
  1163. if(i < 1)
  1164. inuxi1[i] = c;
  1165. inuxi4[i] = c;
  1166. fnuxi4[i] = c;
  1167. fnuxi8[i] = c;
  1168. fnuxi8[i+4] = c+4;
  1169. }
  1170. if(debug['v']) {
  1171. Bprint(&bso, "inuxi = ");
  1172. for(i=0; i<1; i++)
  1173. Bprint(&bso, "%d", inuxi1[i]);
  1174. Bprint(&bso, " ");
  1175. for(i=0; i<2; i++)
  1176. Bprint(&bso, "%d", inuxi2[i]);
  1177. Bprint(&bso, " ");
  1178. for(i=0; i<4; i++)
  1179. Bprint(&bso, "%d", inuxi4[i]);
  1180. Bprint(&bso, "\nfnuxi = ");
  1181. for(i=0; i<4; i++)
  1182. Bprint(&bso, "%d", fnuxi4[i]);
  1183. Bprint(&bso, " ");
  1184. for(i=0; i<8; i++)
  1185. Bprint(&bso, "%d", fnuxi8[i]);
  1186. Bprint(&bso, "\n");
  1187. }
  1188. Bflush(&bso);
  1189. }
  1190. int
  1191. find1(long l, int c)
  1192. {
  1193. char *p;
  1194. int i;
  1195. p = (char*)&l;
  1196. for(i=0; i<4; i++)
  1197. if(*p++ == c)
  1198. return i;
  1199. return 0;
  1200. }
  1201. int
  1202. find2(long l, int c)
  1203. {
  1204. short *p;
  1205. int i;
  1206. p = (short*)&l;
  1207. for(i=0; i<4; i+=2) {
  1208. if(((*p >> 8) & 0xff) == c)
  1209. return i;
  1210. if((*p++ & 0xff) == c)
  1211. return i+1;
  1212. }
  1213. return 0;
  1214. }
  1215. long
  1216. ieeedtof(Ieee *e)
  1217. {
  1218. int exp;
  1219. long v;
  1220. if(e->h == 0)
  1221. return 0;
  1222. exp = (e->h>>20) & ((1L<<11)-1L);
  1223. exp -= (1L<<10) - 2L;
  1224. v = (e->h & 0xfffffL) << 3;
  1225. v |= (e->l >> 29) & 0x7L;
  1226. if((e->l >> 28) & 1) {
  1227. v++;
  1228. if(v & 0x800000L) {
  1229. v = (v & 0x7fffffL) >> 1;
  1230. exp++;
  1231. }
  1232. }
  1233. if(exp <= -126 || exp >= 130)
  1234. diag("double fp to single fp overflow");
  1235. v |= ((exp + 126) & 0xffL) << 23;
  1236. v |= e->h & 0x80000000L;
  1237. return v;
  1238. }
  1239. double
  1240. ieeedtod(Ieee *ieeep)
  1241. {
  1242. Ieee e;
  1243. double fr;
  1244. int exp;
  1245. if(ieeep->h & (1L<<31)) {
  1246. e.h = ieeep->h & ~(1L<<31);
  1247. e.l = ieeep->l;
  1248. return -ieeedtod(&e);
  1249. }
  1250. if(ieeep->l == 0 && ieeep->h == 0)
  1251. return 0;
  1252. fr = ieeep->l & ((1L<<16)-1L);
  1253. fr /= 1L<<16;
  1254. fr += (ieeep->l>>16) & ((1L<<16)-1L);
  1255. fr /= 1L<<16;
  1256. fr += (ieeep->h & (1L<<20)-1L) | (1L<<20);
  1257. fr /= 1L<<21;
  1258. exp = (ieeep->h>>20) & ((1L<<11)-1L);
  1259. exp -= (1L<<10) - 2L;
  1260. return ldexp(fr, exp);
  1261. }
  1262. void
  1263. readundefs(void)
  1264. {
  1265. int i, n;
  1266. Sym *s;
  1267. Biobuf *b;
  1268. char *l, buf[256], *fields[5];
  1269. for(i = 0; i < undefsp; i++) {
  1270. b = Bopen(undefs[i], OREAD);
  1271. if(b == nil) {
  1272. diag("could not open %s: %r", undefs[i]);
  1273. errorexit();
  1274. }
  1275. while((l = Brdline(b, '\n')) != nil) {
  1276. n = Blinelen(b);
  1277. if(n >= sizeof(buf)) {
  1278. diag("%s: line too long", undefs[i]);
  1279. errorexit();
  1280. }
  1281. strncpy(buf, l, n);
  1282. n = getfields(buf, fields, nelem(fields), 1, " \t\r\n");
  1283. if(n == nelem(fields)) {
  1284. diag("%s: bad format", undefs[i]);
  1285. errorexit();
  1286. }
  1287. s = lookup(fields[0], 0);
  1288. s->type = SUNDEF;
  1289. }
  1290. Bterm(b);
  1291. }
  1292. }