obj.c 29 KB

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