scan.c 15 KB

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