scan.c 13 KB

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