write.c 2.3 KB

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