obj.c 29 KB

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