obj.c 28 KB

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