scan.c 15 KB

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