scan.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. #ifndef lint
  6. static char rcsid[] = "$Header$";
  7. #endif
  8. #ifdef SYMDBUG
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #endif /* SYMDBUG */
  12. #include <arch.h>
  13. #include <out.h>
  14. #include <ranlib.h>
  15. #include "const.h"
  16. #include "assert.h"
  17. #include "memory.h"
  18. #include "scan.h"
  19. #include "debug.h"
  20. #define READ 0
  21. #define IND_EMIT(x) (IND_CHAR(x) + (ind_t)align((x).oh_nchar))
  22. #define IND_RELO(x) (IND_EMIT(x) + (x).oh_nsect * sizeof(ind_t))
  23. #ifdef SYMDBUG
  24. #define IND_DBUG(x) (IND_RELO(x) + sizeof(ind_t))
  25. #endif /* SYMDBUG */
  26. extern long lseek();
  27. extern char *core_alloc();
  28. extern bool incore;
  29. extern int infile;
  30. extern int passnumber;
  31. char *archname; /* Name of archive, if reading from archive. */
  32. char *modulname; /* Name of object module. */
  33. #ifdef SYMDBUG
  34. long objectsize;
  35. #endif /* SYMDBUG */
  36. static long align();
  37. static char *modulbase;
  38. static long modulsize();
  39. static scan_modul();
  40. static bool all_alloc();
  41. static bool direct_alloc();
  42. static bool indirect_alloc();
  43. static bool putemitindex();
  44. static bool putreloindex();
  45. #ifdef SYMDBUG
  46. static bool putdbugindex();
  47. #endif /* SYMDBUG */
  48. static get_indirect();
  49. static read_modul();
  50. /*
  51. * Open the file with name `filename' (if necessary) and examine the first
  52. * few bytes to see if it's a plain file or an archive.
  53. * In case of a plain file, the file pointer is repositioned after the
  54. * examination. Otherwise it is at the beginning of the table of contents.
  55. */
  56. int
  57. getfile(filename)
  58. char *filename;
  59. {
  60. unsigned int rd_unsigned2();
  61. struct ar_hdr archive_header;
  62. unsigned short magic_number;
  63. #ifdef SYMDBUG
  64. struct stat statbuf;
  65. extern int fstat();
  66. #endif /* SYMDBUG */
  67. archname = (char *)0;
  68. modulname = (char *)0;
  69. if (passnumber == FIRST || !incore) {
  70. if ((infile = open(filename, READ)) < 0)
  71. fatal("can't read %s", filename);
  72. magic_number = rd_unsigned2(infile);
  73. } else {
  74. modulbase = modulptr((ind_t)0);
  75. magic_number = *(unsigned short *)modulbase;
  76. }
  77. switch (magic_number) {
  78. case O_MAGIC:
  79. #ifdef SYMDBUG
  80. if (passnumber == FIRST || !incore) {
  81. if (fstat(infile, &statbuf) < 0)
  82. fatal("cannot stat");
  83. objectsize = statbuf.st_size;
  84. }
  85. #endif /* SYMDBUG */
  86. seek((long)0);
  87. modulname = filename;
  88. return PLAIN;
  89. case ARMAG:
  90. case AALMAG:
  91. archname = filename;
  92. if (passnumber == FIRST) {
  93. rd_arhdr(infile, &archive_header);
  94. if (strcmp(archive_header.ar_name, SYMDEF))
  95. fatal("no table of contents");
  96. } else if (incore) {
  97. modulbase += sizeof(int);
  98. core_position += sizeof(int);
  99. }
  100. return ARCHIVE;
  101. default:
  102. fatal("wrong magic number");
  103. }
  104. /* NOTREACHED */
  105. }
  106. /* ARGSUSED */
  107. closefile(filename)
  108. char *filename;
  109. {
  110. if (passnumber == FIRST || !incore)
  111. close(infile);
  112. }
  113. get_archive_header(archive_header)
  114. register struct ar_hdr *archive_header;
  115. {
  116. if (passnumber == FIRST || !incore) {
  117. rd_arhdr(infile, archive_header);
  118. } else {
  119. /* Copy structs. */
  120. *archive_header = *(struct ar_hdr *)modulbase;
  121. modulbase += int_align(sizeof(struct ar_hdr));
  122. core_position += int_align(sizeof(struct ar_hdr));
  123. }
  124. #ifdef SYMDBUG
  125. objectsize = archive_header.ar_size;
  126. #endif /* SYMDBUG */
  127. }
  128. get_modul()
  129. {
  130. if (passnumber == FIRST) {
  131. rd_fdopen(infile);
  132. scan_modul();
  133. } else if (!incore) {
  134. rd_fdopen(infile);
  135. read_modul();
  136. }
  137. }
  138. /*
  139. * Read module from the current file. If it doesn't fit into core, the strategy
  140. * to keep everything in core is abandoned, but we will always put the header,
  141. * the section table, and the name and string table into core.
  142. */
  143. static
  144. scan_modul()
  145. {
  146. bool space;
  147. struct outhead *head;
  148. struct outsect *sect;
  149. space = all_alloc();
  150. head = (struct outhead *)modulptr(IND_HEAD);
  151. if (space) {
  152. sect = (struct outsect *)modulptr(IND_SECT(*head));
  153. get_indirect(head, sect);
  154. }
  155. rd_name((struct outname *)modulptr(IND_NAME(*head)), head->oh_nname);
  156. rd_string((char *)modulptr(IND_CHAR(*head)), head->oh_nchar);
  157. #ifdef SYMDBUG
  158. if (space) {
  159. get_dbug(*(ind_t *)modulptr(IND_DBUG(*head)),
  160. ojectsize - OFF_DBUG(*head)
  161. );
  162. }
  163. #endif /* SYMDBUG */
  164. }
  165. /*
  166. * Allocate space for and read in the header and section table.
  167. * First get the header. With this we can determine what to allocate
  168. * for the rest of the module, and with the rest we can determine what
  169. * to allocate for the section contents.
  170. * If possible, allocate space for the rest of the module. Return whether
  171. * this was possible.
  172. */
  173. static bool
  174. all_alloc()
  175. {
  176. struct outhead head;
  177. extern ind_t hard_alloc();
  178. if (hard_alloc(ALLOMODL, (long)sizeof(struct outhead)) == BADOFF)
  179. fatal("no space for module header");
  180. rd_ohead((struct outhead *)modulptr(IND_HEAD));
  181. /*
  182. * Copy the header because we need it so often.
  183. */
  184. head = *(struct outhead *)modulptr(IND_HEAD);
  185. return direct_alloc(&head) && indirect_alloc(&head);
  186. }
  187. /*
  188. * Allocate space for the rest of the direct bytes.
  189. * First allocate the section table and read it in, then allocate the rest
  190. * and return whether this succeeded.
  191. */
  192. static bool
  193. direct_alloc(head)
  194. struct outhead *head;
  195. {
  196. ind_t sectindex = IND_SECT(*head);
  197. register struct outsect *sects;
  198. unsigned short nsect = head->oh_nsect;
  199. long size, rest;
  200. extern ind_t hard_alloc();
  201. extern ind_t alloc();
  202. #ifdef SYMDBUG
  203. rest = nsect * sizeof(ind_t) + sizeof(ind_t) + sizeof(ind_t);
  204. #else /* SYMDBUG */
  205. rest = nsect * sizeof(ind_t) + sizeof(ind_t);
  206. #endif /* SYMDBUG */
  207. /*
  208. * We already allocated space for the header, we now need
  209. * the section, name an string table.
  210. */
  211. size = modulsize(head) - sizeof(struct outhead) - rest;
  212. if (hard_alloc(ALLOMODL, size) == BADOFF)
  213. fatal("no space for module");
  214. rd_sect(sects = ((struct outsect *)modulptr(sectindex)), nsect);
  215. while (nsect--) {
  216. if (sects->os_lign > 1) {
  217. sects->os_size += sects->os_lign - 1;
  218. sects->os_size -= sects->os_size % sects->os_lign;
  219. }
  220. sects++;
  221. }
  222. return incore && alloc(ALLOMODL, rest) != BADOFF;
  223. }
  224. /*
  225. * Allocate space for the indirectly accessed pieces: the section contents and
  226. * the relocation table, and put their indices in the right place.
  227. */
  228. static bool
  229. indirect_alloc(head)
  230. struct outhead *head;
  231. {
  232. register int allopiece;
  233. unsigned short nsect = head->oh_nsect;
  234. unsigned short nrelo = head->oh_nrelo;
  235. ind_t sectindex = IND_SECT(*head);
  236. ind_t emitoff = IND_EMIT(*head);
  237. ind_t relooff = IND_RELO(*head);
  238. #ifdef SYMDBUG
  239. ind_t dbugoff = IND_DBUG(*head);
  240. extern long objectsize;
  241. long dbugsize = objectsize - OFF_DBUG(*head);
  242. #endif /* SYMDBUG */
  243. assert(incore);
  244. for (allopiece = ALLOEMIT; allopiece < ALLOEMIT + nsect; allopiece++) {
  245. if (!putemitindex(sectindex, emitoff, allopiece))
  246. return FALSE;
  247. sectindex += sizeof(struct outsect);
  248. emitoff += sizeof(ind_t);
  249. }
  250. #ifdef SYMDBUG
  251. return putreloindex(relooff, (long)nrelo * sizeof(struct outrelo))
  252. &&
  253. putdbugindex(dbugoff, dbugsize);
  254. #else /* SYMDBUG */
  255. return putreloindex(relooff, (long)nrelo * sizeof(struct outrelo));
  256. #endif /* SYMDBUG */
  257. }
  258. /*
  259. * Allocate space for the contents of the section of which the table entry is
  260. * at offset `sectindex'. Put the offset of the allocated piece at offset
  261. * `emitoff'.
  262. */
  263. static bool
  264. putemitindex(sectindex, emitoff, allopiece)
  265. ind_t sectindex;
  266. ind_t emitoff;
  267. int allopiece;
  268. {
  269. long flen;
  270. ind_t emitindex;
  271. extern ind_t alloc();
  272. static long zeros[MAXSECT];
  273. register long zero = zeros[allopiece - ALLOEMIT];
  274. /*
  275. * Notice that "sectindex" is not a section number!
  276. * It contains the offset of the section from the beginning
  277. * of the module. Thus, it cannot be used to index "zeros".
  278. * AIAIAIAIA
  279. */
  280. flen = ((struct outsect *)modulptr(sectindex))->os_flen;
  281. if (flen && zero) {
  282. if ((emitindex = alloc(allopiece, zero)) != BADOFF){
  283. register char *p = address(allopiece, emitindex);
  284. debug("Zeros %ld\n", zero, 0,0,0);
  285. while (zero--) *p++ = 0;
  286. }
  287. else return FALSE;
  288. zero = 0;
  289. }
  290. zeros[allopiece - ALLOEMIT] =
  291. zero + ((struct outsect *) modulptr(sectindex))->os_size - flen;
  292. if ((emitindex = alloc(allopiece, flen)) != BADOFF) {
  293. *(ind_t *)modulptr(emitoff) = emitindex;
  294. return TRUE;
  295. }
  296. return FALSE;
  297. }
  298. /*
  299. * Allocate space for a relocation table with `nrelobytes' bytes, and put the
  300. * offset at `relooff'.
  301. */
  302. static bool
  303. putreloindex(relooff, nrelobytes)
  304. ind_t relooff;
  305. long nrelobytes;
  306. {
  307. ind_t reloindex;
  308. extern ind_t alloc();
  309. if ((reloindex = alloc(ALLORELO, nrelobytes)) != BADOFF) {
  310. *(ind_t *)modulptr(relooff) = reloindex;
  311. return TRUE;
  312. }
  313. return FALSE;
  314. }
  315. #ifdef SYMDBUG
  316. /*
  317. * Allocate space for debugging information and put the offset at `dbugoff'.
  318. */
  319. static bool
  320. putdbugindex(dbugoff, ndbugbytes)
  321. ind_t relooff;
  322. long ndbugbytes;
  323. {
  324. ind_t dbugindex;
  325. extern ind_t alloc();
  326. if ((dbugindex = alloc(ALLODBUG, ndbugbytes)) != BADOFF) {
  327. *(ind_t *)modulptr(dbugoff) = dbugindex;
  328. return TRUE;
  329. }
  330. return FALSE;
  331. }
  332. #endif /* SYMDBUG */
  333. /*
  334. * Compute addresses and read in. Remember that the contents of the sections
  335. * and also the relocation table are accessed indirectly.
  336. */
  337. static
  338. get_indirect(head, sect)
  339. struct outhead *head; /* not register! Won't compile on
  340. SCO Xenix 386 if it is!
  341. */
  342. register struct outsect *sect;
  343. {
  344. register ind_t *emitindex;
  345. register int nsect;
  346. register int piece;
  347. ind_t *reloindex;
  348. emitindex = (ind_t *)modulptr(IND_EMIT(*head));
  349. piece = ALLOEMIT;
  350. for (nsect = 0; nsect < head->oh_nsect; nsect++) {
  351. rd_outsect(nsect);
  352. rd_emit(address(piece, *emitindex), sect->os_flen);
  353. piece++; emitindex++; sect++;
  354. }
  355. reloindex = (ind_t *)modulptr(IND_RELO(*head));
  356. rd_relo((struct outrelo *)address(ALLORELO, *reloindex),
  357. head->oh_nrelo
  358. );
  359. }
  360. /*
  361. * Set the file pointer at `pos'.
  362. */
  363. seek(pos)
  364. long pos;
  365. {
  366. if (passnumber == FIRST || !incore)
  367. lseek(infile, pos, 0);
  368. }
  369. /*
  370. * A file pointer is advanced automatically when reading, a char pointer
  371. * is not. That's why we do it here. If we don't keep everything in core,
  372. * we give the space allocated for a module back.
  373. */
  374. skip_modul(head)
  375. struct outhead *head;
  376. {
  377. register ind_t skip = modulsize(head);
  378. if (incore) {
  379. core_position += int_align(skip);
  380. if (passnumber == SECOND)
  381. modulbase += int_align(skip);
  382. } else {
  383. dealloc(ALLOMODL);
  384. core_position = (ind_t)0;
  385. }
  386. }
  387. /*
  388. * Read in what we need in pass 2, because we couldn't keep it in core.
  389. */
  390. static
  391. read_modul()
  392. {
  393. struct outhead *head;
  394. register struct outsect *sects;
  395. struct outname *names;
  396. char *chars;
  397. ind_t sectindex, nameindex, charindex;
  398. unsigned short nsect, nname;
  399. long size;
  400. long nchar;
  401. extern ind_t hard_alloc();
  402. assert(passnumber == SECOND);
  403. assert(!incore);
  404. if (hard_alloc(ALLOMODL, (long)sizeof(struct outhead)) == BADOFF)
  405. fatal("no space for module header");
  406. head = (struct outhead *)modulptr(IND_HEAD);
  407. rd_ohead(head);
  408. nsect = head->oh_nsect; sectindex = IND_SECT(*head);
  409. nname = head->oh_nname; nameindex = IND_NAME(*head);
  410. nchar = head->oh_nchar; charindex = IND_CHAR(*head);
  411. #ifdef SYMDBUG
  412. size = modulsize(head) - (nsect * sizeof(ind_t) + 2 * sizeof(ind_t));
  413. #else /* SYMDBUG */
  414. size = modulsize(head) - (nsect * sizeof(ind_t) + sizeof(ind_t));
  415. #endif /* SYMDBUG */
  416. if (hard_alloc(ALLOMODL, size) == BADOFF)
  417. fatal("no space for module");
  418. sects = (struct outsect *)modulptr(sectindex);
  419. names = (struct outname *)modulptr(nameindex);
  420. chars = modulptr(charindex);
  421. rd_sect(sects, nsect);
  422. while (nsect--) {
  423. if (sects->os_lign > 1) {
  424. sects->os_size += sects->os_lign - 1;
  425. sects->os_size -= sects->os_size % sects->os_lign;
  426. }
  427. sects++;
  428. }
  429. rd_name(names, nname);
  430. rd_string(chars, nchar);
  431. }
  432. /*
  433. * Align `size' to a multiple of the size of a double.
  434. * This is assumed to be a power of 2.
  435. */
  436. static long
  437. align(size)
  438. register long size;
  439. {
  440. return (size + (sizeof(double) - 1)) & ~(int)(sizeof(double) - 1);
  441. }
  442. /*
  443. * Compute how many DIRECT bytes must be allocated for a module of which the
  444. * header is pointed to by `head':
  445. * 0. the header,
  446. * 1. the section table,
  447. * 2. the name table,
  448. * 3. the string table,
  449. * 4. for each section the offset of its contents,
  450. * 5. the offset of the relocation table.
  451. #ifdef SYMDBUG
  452. * 6. the offset of the debugging information.
  453. #endif /* SYMDBUG */
  454. */
  455. static long
  456. modulsize(head)
  457. register struct outhead *head;
  458. {
  459. return sizeof(struct outhead) + /* 0 */
  460. head->oh_nsect * sizeof(struct outsect) + /* 1 */
  461. head->oh_nname * sizeof(struct outname) + /* 2 */
  462. align(head->oh_nchar) + /* 3 */
  463. head->oh_nsect * sizeof(ind_t) + /* 4 */
  464. #ifdef SYMDBUG
  465. sizeof(ind_t) + /* 5 */
  466. sizeof(ind_t); /* 6 */
  467. #else /* SYMDBUG */
  468. sizeof(ind_t); /* 5 */
  469. #endif /* SYMDBUG */
  470. }
  471. /* ------------------------------------------------------------------------- */
  472. /*
  473. * Walk through the relocation table of the current module. We must either walk
  474. * through core or through file. Startrelo() should be called first.
  475. */
  476. static struct outrelo *walkrelo;
  477. static unsigned short cnt_relos;
  478. static unsigned short relind;
  479. #define _RELSIZ 64
  480. startrelo(head)
  481. register struct outhead *head;
  482. {
  483. ind_t reloindex;
  484. if (incore) {
  485. reloindex = *(ind_t *)(modulbase + IND_RELO(*head));
  486. walkrelo = (struct outrelo *)address(ALLORELO, reloindex);
  487. }
  488. else {
  489. relind = _RELSIZ;
  490. rd_rew_relos(head);
  491. cnt_relos = head->oh_nrelo;
  492. }
  493. }
  494. struct outrelo *
  495. nextrelo()
  496. {
  497. static struct outrelo relobuf[_RELSIZ];
  498. if (incore)
  499. return walkrelo++;
  500. if (relind == _RELSIZ) {
  501. unsigned int i = cnt_relos >= _RELSIZ ? _RELSIZ : cnt_relos;
  502. cnt_relos -= i;
  503. rd_relo(relobuf, i);
  504. relind = 0;
  505. }
  506. return &relobuf[relind++];
  507. }
  508. /* ------------------------------------------------------------------------- */
  509. /*
  510. * Get the section contents in core of which the describing struct has index
  511. * `sectindex'. `Head' points to the header of the module.
  512. */
  513. char *
  514. getemit(head, sects, sectindex)
  515. struct outhead *head;
  516. struct outsect *sects;
  517. int sectindex;
  518. {
  519. char *ret;
  520. ind_t off;
  521. extern char *core_alloc();
  522. if (!incore) {
  523. ret = core_alloc(ALLOMODL, sects[sectindex].os_flen);
  524. if (ret == (char *)0)
  525. return 0;
  526. rd_outsect(sectindex);
  527. rd_emit(ret, sects[sectindex].os_flen);
  528. return ret;
  529. }
  530. /*
  531. * We have an offset in the contents of the final output
  532. * "file" where normally the contents would be.
  533. */
  534. off = *((ind_t *)(modulbase + IND_EMIT(*head)) + sectindex);
  535. return address(ALLOEMIT + sectindex, off);
  536. }
  537. char *
  538. getblk(totalsz, pblksz, sectindex)
  539. long totalsz;
  540. long *pblksz;
  541. int sectindex;
  542. {
  543. char *ret;
  544. long sz = (1L << 30);
  545. assert(!incore);
  546. while (sz >= totalsz) sz >>= 1;
  547. while (sz) {
  548. ret = core_alloc(ALLOMODL, sz);
  549. if (ret != (char *) 0) {
  550. rd_outsect(sectindex);
  551. *pblksz = sz;
  552. return ret;
  553. }
  554. sz >>= 1;
  555. }
  556. fatal("no space for section contents");
  557. return (char *) 0;
  558. }
  559. endemit(emit)
  560. char *emit;
  561. {
  562. core_free(ALLOMODL, emit);
  563. }