1
0

obj.c 29 KB

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