write.c 2.3 KB

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