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