write.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include "out.h"
  12. #include "const.h"
  13. #include "assert.h"
  14. #include "memory.h"
  15. extern struct outhead outhead;
  16. extern struct outsect outsect[];
  17. extern int flagword;
  18. extern bool incore;
  19. wr_fatal()
  20. {
  21. fatal("write error");
  22. }
  23. static long off_char;
  24. /*
  25. * Open the output file according to the chosen strategy.
  26. * Write away the header and section table: they will not change anymore.
  27. */
  28. begin_write()
  29. {
  30. register struct outhead *hd = &outhead;
  31. assert(! incore);
  32. wr_ohead(hd);
  33. wr_sect(outsect, hd->oh_nsect);
  34. off_char = OFF_CHAR(*hd);
  35. }
  36. static struct outname *
  37. sectname(sectindex)
  38. int sectindex;
  39. {
  40. static struct outname namebuf;
  41. namebuf.on_foff = (long)0; /* No string name. */
  42. namebuf.on_type = (S_MIN + sectindex) | S_SCT;
  43. namebuf.on_desc = 0;
  44. namebuf.on_valu = outsect[sectindex].os_base;
  45. return &namebuf;
  46. }
  47. /*
  48. * Write out the symbol table and the section names.
  49. */
  50. end_write()
  51. {
  52. register struct outname *name;
  53. register int sectindex;
  54. extern unsigned short NGlobals;
  55. extern long NGChars;
  56. assert(!incore);
  57. assert(!(flagword & SFLAG));
  58. name = (struct outname *)address(ALLOGLOB, (ind_t)0);
  59. namecpy(name, NGlobals, off_char);
  60. wr_name(name, NGlobals);
  61. wr_string(mems[ALLOGCHR].mem_base+1, NGChars);
  62. off_char += NGChars;
  63. for (sectindex = 0; sectindex < outhead.oh_nsect; sectindex++)
  64. wrt_name(sectname(sectindex), 1);
  65. }
  66. wrt_emit(emit, sectindex, cnt)
  67. char *emit;
  68. int sectindex;
  69. long cnt;
  70. {
  71. wr_outsect(sectindex);
  72. wr_emit(emit, cnt);
  73. }
  74. wrt_nulls(sectindex, cnt)
  75. register long cnt;
  76. {
  77. static char nullbuf[BUFSIZ];
  78. wr_outsect(sectindex);
  79. while (cnt) {
  80. register int n = cnt >= BUFSIZ ? BUFSIZ : cnt;
  81. wr_emit(nullbuf, (long)n);
  82. cnt -= n;
  83. }
  84. }
  85. wrt_name(name, writename)
  86. register struct outname *name;
  87. {
  88. assert(!incore);
  89. assert(!(flagword & SFLAG));
  90. if (name->on_mptr != (char *)0) {
  91. register long len = strlen(name->on_mptr) + 1;
  92. wr_string(name->on_mptr, len);
  93. name->on_foff = off_char;
  94. off_char += len;
  95. } else {
  96. name->on_foff = (long)0;
  97. }
  98. if (writename) wr_name(name, 1);
  99. }