scan.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. #include <string.h>
  11. #ifdef SYMDBUG
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #endif /* SYMDBUG */
  15. #include "arch.h"
  16. #include "out.h"
  17. #include "ranlib.h"
  18. #include "const.h"
  19. #include "assert.h"
  20. #include "memory.h"
  21. #include "scan.h"
  22. #include "debug.h"
  23. #include "defs.h"
  24. #define READ 0
  25. #define IND_EMIT(x) (IND_CHAR(x) + (ind_t)align((x).oh_nchar))
  26. #define IND_RELO(x) (IND_EMIT(x) + (x).oh_nsect * sizeof(ind_t))
  27. #ifdef SYMDBUG
  28. #define IND_DBUG(x) (IND_RELO(x) + sizeof(ind_t))
  29. #endif /* SYMDBUG */
  30. extern char *core_alloc();
  31. extern bool incore;
  32. extern int infile;
  33. extern int passnumber;
  34. char *archname; /* Name of archive, if reading from archive. */
  35. char *modulname; /* Name of object module. */
  36. #ifdef SYMDBUG
  37. long objectsize;
  38. #endif /* SYMDBUG */
  39. static long align(long size);
  40. static char *modulbase;
  41. static long modulsize(struct outhead *head);
  42. static void scan_modul();
  43. static bool all_alloc();
  44. static bool direct_alloc(struct outhead *head);
  45. static bool indirect_alloc(struct outhead *head);
  46. static bool putemitindex(ind_t sectindex, ind_t emitoff, int allopiece);
  47. static bool putreloindex(ind_t relooff, long nrelobytes);
  48. #ifdef SYMDBUG
  49. static bool putdbugindex(ind_t dbugoff, long ndbugbytes);
  50. #endif /* SYMDBUG */
  51. static void get_indirect(struct outhead *head, struct outsect *sect);
  52. static void read_modul();
  53. /*
  54. * Open the file with name `filename' (if necessary) and examine the first
  55. * few bytes to see if it's a plain file or an archive.
  56. * In case of a plain file, the file pointer is repositioned after the
  57. * examination. Otherwise it is at the beginning of the table of contents.
  58. */
  59. int getfile(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. return 0;
  107. }
  108. /* ARGSUSED */
  109. void closefile(char *filename)
  110. {
  111. if (passnumber == FIRST || !incore)
  112. close(infile);
  113. }
  114. void get_archive_header(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. void 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 void 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 all_alloc()
  173. {
  174. struct outhead head;
  175. extern ind_t hard_alloc();
  176. if (hard_alloc(ALLOMODL, (long)sizeof(struct outhead)) == BADOFF)
  177. fatal("no space for module header");
  178. rd_ohead((struct outhead *)modulptr(IND_HEAD));
  179. /*
  180. * Copy the header because we need it so often.
  181. */
  182. head = *(struct outhead *)modulptr(IND_HEAD);
  183. return direct_alloc(&head) && indirect_alloc(&head);
  184. }
  185. /*
  186. * Allocate space for the rest of the direct bytes.
  187. * First allocate the section table and read it in, then allocate the rest
  188. * and return whether this succeeded.
  189. */
  190. static bool direct_alloc(struct outhead *head)
  191. {
  192. ind_t sectindex = IND_SECT(*head);
  193. struct outsect *sects;
  194. unsigned short nsect = head->oh_nsect;
  195. long size, rest;
  196. extern ind_t hard_alloc();
  197. extern ind_t alloc();
  198. #ifdef SYMDBUG
  199. rest = nsect * sizeof(ind_t) + sizeof(ind_t) + sizeof(ind_t);
  200. #else /* SYMDBUG */
  201. rest = nsect * sizeof(ind_t) + sizeof(ind_t);
  202. #endif /* SYMDBUG */
  203. /*
  204. * We already allocated space for the header, we now need
  205. * the section, name an string table.
  206. */
  207. size = modulsize(head) - sizeof(struct outhead) - rest;
  208. if (hard_alloc(ALLOMODL, size) == BADOFF)
  209. fatal("no space for module");
  210. rd_sect(sects = ((struct outsect *)modulptr(sectindex)), nsect);
  211. while (nsect--) {
  212. if (sects->os_lign > 1) {
  213. sects->os_size += sects->os_lign - 1;
  214. sects->os_size -= sects->os_size % sects->os_lign;
  215. }
  216. sects++;
  217. }
  218. return incore && alloc(ALLOMODL, rest) != BADOFF;
  219. }
  220. /*
  221. * Allocate space for the indirectly accessed pieces: the section contents and
  222. * the relocation table, and put their indices in the right place.
  223. */
  224. static bool indirect_alloc(struct outhead *head)
  225. {
  226. int allopiece;
  227. unsigned short nsect = head->oh_nsect;
  228. unsigned short nrelo = head->oh_nrelo;
  229. ind_t sectindex = IND_SECT(*head);
  230. ind_t emitoff = IND_EMIT(*head);
  231. ind_t relooff = IND_RELO(*head);
  232. #ifdef SYMDBUG
  233. ind_t dbugoff = IND_DBUG(*head);
  234. extern long objectsize;
  235. long dbugsize = objectsize - OFF_DBUG(*head);
  236. #endif /* SYMDBUG */
  237. assert(incore);
  238. for (allopiece = ALLOEMIT; allopiece < ALLOEMIT + nsect; allopiece++) {
  239. if (!putemitindex(sectindex, emitoff, allopiece))
  240. return FALSE;
  241. sectindex += sizeof(struct outsect);
  242. emitoff += sizeof(ind_t);
  243. }
  244. #ifdef SYMDBUG
  245. return putreloindex(relooff, (long)nrelo * sizeof(struct outrelo))
  246. &&
  247. putdbugindex(dbugoff, dbugsize);
  248. #else /* SYMDBUG */
  249. return putreloindex(relooff, (long)nrelo * sizeof(struct outrelo));
  250. #endif /* SYMDBUG */
  251. }
  252. /*
  253. * Allocate space for the contents of the section of which the table entry is
  254. * at offset `sectindex'. Put the offset of the allocated piece at offset
  255. * `emitoff'.
  256. */
  257. static bool putemitindex(ind_t sectindex, ind_t emitoff, int allopiece)
  258. {
  259. long flen;
  260. ind_t emitindex;
  261. extern ind_t alloc();
  262. static long zeros[MAXSECT];
  263. long zero = zeros[allopiece - ALLOEMIT];
  264. /*
  265. * Notice that "sectindex" is not a section number!
  266. * It contains the offset of the section from the beginning
  267. * of the module. Thus, it cannot be used to index "zeros".
  268. * AIAIAIAIA
  269. */
  270. flen = ((struct outsect *)modulptr(sectindex))->os_flen;
  271. if (flen && zero) {
  272. if ((emitindex = alloc(allopiece, zero)) != BADOFF){
  273. char *p = address(allopiece, emitindex);
  274. debug("Zeros %ld\n", zero);
  275. while (zero--) *p++ = 0;
  276. }
  277. else return FALSE;
  278. zero = 0;
  279. }
  280. zeros[allopiece - ALLOEMIT] =
  281. zero + ((struct outsect *) modulptr(sectindex))->os_size - flen;
  282. if ((emitindex = alloc(allopiece, flen)) != BADOFF) {
  283. *(ind_t *)modulptr(emitoff) = emitindex;
  284. return TRUE;
  285. }
  286. return FALSE;
  287. }
  288. /*
  289. * Allocate space for a relocation table with `nrelobytes' bytes, and put the
  290. * offset at `relooff'.
  291. */
  292. static bool putreloindex(ind_t relooff, long nrelobytes)
  293. {
  294. ind_t reloindex;
  295. extern ind_t alloc();
  296. if ((reloindex = alloc(ALLORELO, nrelobytes)) != BADOFF) {
  297. *(ind_t *)modulptr(relooff) = reloindex;
  298. return TRUE;
  299. }
  300. return FALSE;
  301. }
  302. #ifdef SYMDBUG
  303. /*
  304. * Allocate space for debugging information and put the offset at `dbugoff'.
  305. */
  306. static bool putdbugindex(ind_t dbugoff, long ndbugbytes)
  307. {
  308. ind_t dbugindex;
  309. extern ind_t alloc();
  310. if ((dbugindex = alloc(ALLODBUG, ndbugbytes)) != BADOFF) {
  311. *(ind_t *)modulptr(dbugoff) = dbugindex;
  312. return TRUE;
  313. }
  314. return FALSE;
  315. }
  316. #endif /* SYMDBUG */
  317. /*
  318. * Compute addresses and read in. Remember that the contents of the sections
  319. * and also the relocation table are accessed indirectly.
  320. */
  321. static void get_indirect(struct outhead *head, struct outsect *sect)
  322. {
  323. ind_t *emitindex;
  324. int nsect;
  325. int piece;
  326. ind_t *reloindex;
  327. emitindex = (ind_t *)modulptr(IND_EMIT(*head));
  328. piece = ALLOEMIT;
  329. for (nsect = 0; nsect < head->oh_nsect; nsect++) {
  330. rd_outsect(nsect);
  331. rd_emit(address(piece, *emitindex), sect->os_flen);
  332. piece++; emitindex++; sect++;
  333. }
  334. reloindex = (ind_t *)modulptr(IND_RELO(*head));
  335. rd_relo((struct outrelo *)address(ALLORELO, *reloindex),
  336. head->oh_nrelo
  337. );
  338. }
  339. /*
  340. * Set the file pointer at `pos'.
  341. */
  342. void seek(long pos)
  343. {
  344. if (passnumber == FIRST || !incore)
  345. lseek(infile, pos, 0);
  346. }
  347. /*
  348. * A file pointer is advanced automatically when reading, a char pointer
  349. * is not. That's why we do it here. If we don't keep everything in core,
  350. * we give the space allocated for a module back.
  351. */
  352. void skip_modul(struct outhead *head)
  353. {
  354. ind_t skip = modulsize(head);
  355. if (incore) {
  356. core_position += int_align(skip);
  357. if (passnumber == SECOND)
  358. modulbase += int_align(skip);
  359. } else {
  360. dealloc(ALLOMODL);
  361. core_position = (ind_t)0;
  362. }
  363. }
  364. /*
  365. * Read in what we need in pass 2, because we couldn't keep it in core.
  366. */
  367. static void read_modul()
  368. {
  369. struct outhead *head;
  370. struct outsect *sects;
  371. struct outname *names;
  372. char *chars;
  373. ind_t sectindex, nameindex, charindex;
  374. unsigned short nsect, nname;
  375. long size;
  376. long nchar;
  377. extern ind_t hard_alloc();
  378. assert(passnumber == SECOND);
  379. assert(!incore);
  380. if (hard_alloc(ALLOMODL, (long)sizeof(struct outhead)) == BADOFF)
  381. fatal("no space for module header");
  382. head = (struct outhead *)modulptr(IND_HEAD);
  383. rd_ohead(head);
  384. nsect = head->oh_nsect; sectindex = IND_SECT(*head);
  385. nname = head->oh_nname; nameindex = IND_NAME(*head);
  386. nchar = head->oh_nchar; charindex = IND_CHAR(*head);
  387. #ifdef SYMDBUG
  388. size = modulsize(head) - (nsect * sizeof(ind_t) + 2 * sizeof(ind_t));
  389. #else /* SYMDBUG */
  390. size = modulsize(head) - (nsect * sizeof(ind_t) + sizeof(ind_t));
  391. #endif /* SYMDBUG */
  392. if (hard_alloc(ALLOMODL, size) == BADOFF)
  393. fatal("no space for module");
  394. sects = (struct outsect *)modulptr(sectindex);
  395. names = (struct outname *)modulptr(nameindex);
  396. chars = modulptr(charindex);
  397. rd_sect(sects, nsect);
  398. while (nsect--) {
  399. if (sects->os_lign > 1) {
  400. sects->os_size += sects->os_lign - 1;
  401. sects->os_size -= sects->os_size % sects->os_lign;
  402. }
  403. sects++;
  404. }
  405. rd_name(names, nname);
  406. rd_string(chars, nchar);
  407. }
  408. /*
  409. * Align `size' to a multiple of the size of a double.
  410. * This is assumed to be a power of 2.
  411. */
  412. static long align(long size)
  413. {
  414. return (size + (sizeof(double) - 1)) & ~(int)(sizeof(double) - 1);
  415. }
  416. /*
  417. * Compute how many DIRECT bytes must be allocated for a module of which the
  418. * header is pointed to by `head':
  419. * 0. the header,
  420. * 1. the section table,
  421. * 2. the name table,
  422. * 3. the string table,
  423. * 4. for each section the offset of its contents,
  424. * 5. the offset of the relocation table.
  425. #ifdef SYMDBUG
  426. * 6. the offset of the debugging information.
  427. #endif
  428. */
  429. static long modulsize(struct outhead *head)
  430. {
  431. return sizeof(struct outhead) + /* 0 */
  432. head->oh_nsect * sizeof(struct outsect) + /* 1 */
  433. head->oh_nname * sizeof(struct outname) + /* 2 */
  434. align(head->oh_nchar) + /* 3 */
  435. head->oh_nsect * sizeof(ind_t) + /* 4 */
  436. #ifdef SYMDBUG
  437. sizeof(ind_t) + /* 5 */
  438. sizeof(ind_t); /* 6 */
  439. #else /* SYMDBUG */
  440. sizeof(ind_t); /* 5 */
  441. #endif /* SYMDBUG */
  442. }
  443. /* ------------------------------------------------------------------------- */
  444. /*
  445. * Walk through the relocation table of the current module. We must either walk
  446. * through core or through file. Startrelo() should be called first.
  447. */
  448. static struct outrelo *walkrelo;
  449. static unsigned short cnt_relos;
  450. static unsigned short relind;
  451. #define _RELSIZ 64
  452. void startrelo(struct outhead *head)
  453. {
  454. ind_t reloindex;
  455. if (incore) {
  456. reloindex = *(ind_t *)(modulbase + IND_RELO(*head));
  457. walkrelo = (struct outrelo *)address(ALLORELO, reloindex);
  458. }
  459. else {
  460. relind = _RELSIZ;
  461. rd_rew_relos(head);
  462. cnt_relos = head->oh_nrelo;
  463. }
  464. }
  465. struct outrelo *nextrelo()
  466. {
  467. static struct outrelo relobuf[_RELSIZ];
  468. if (incore)
  469. return walkrelo++;
  470. if (relind == _RELSIZ) {
  471. unsigned int i = cnt_relos >= _RELSIZ ? _RELSIZ : cnt_relos;
  472. cnt_relos -= i;
  473. rd_relo(relobuf, i);
  474. relind = 0;
  475. }
  476. return &relobuf[relind++];
  477. }
  478. /* ------------------------------------------------------------------------- */
  479. /*
  480. * Get the section contents in core of which the describing struct has index
  481. * `sectindex'. `Head' points to the header of the module.
  482. */
  483. char *getemit(struct outhead *head, struct outsect *sects, int sectindex)
  484. {
  485. char *ret;
  486. ind_t off;
  487. extern char *core_alloc();
  488. if (!incore) {
  489. ret = core_alloc(ALLOMODL, sects[sectindex].os_flen);
  490. if (ret == (char *)0)
  491. return 0;
  492. rd_outsect(sectindex);
  493. rd_emit(ret, sects[sectindex].os_flen);
  494. return ret;
  495. }
  496. /*
  497. * We have an offset in the contents of the final output
  498. * "file" where normally the contents would be.
  499. */
  500. off = *((ind_t *)(modulbase + IND_EMIT(*head)) + sectindex);
  501. return address(ALLOEMIT + sectindex, off);
  502. }
  503. char *getblk(long totalsz, long *pblksz, int sectindex)
  504. {
  505. char *ret;
  506. long sz = (1L << 30);
  507. assert(!incore);
  508. while (sz >= totalsz) sz >>= 1;
  509. while (sz) {
  510. ret = core_alloc(ALLOMODL, sz);
  511. if (ret != (char *) 0) {
  512. rd_outsect(sectindex);
  513. *pblksz = sz;
  514. return ret;
  515. }
  516. sz >>= 1;
  517. }
  518. fatal("no space for section contents");
  519. return (char *) 0;
  520. }
  521. void endemit(char *emit)
  522. {
  523. core_free(ALLOMODL, emit);
  524. }