output.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <out.h>
  6. #include "const.h"
  7. #include "memory.h"
  8. #include "object.h"
  9. #include "defs.h"
  10. static void generate_section_names();
  11. extern struct outhead outhead;
  12. extern bool incore;
  13. extern int flagword;
  14. /*
  15. * We have counted all relocation structs but we know only now if
  16. * these must be emitted.We add all names here,unless the -s(trip)
  17. * flag was given.
  18. * If this flag is given we don't need the string table either.
  19. */
  20. void beginoutput()
  21. {
  22. extern unsigned short NLocals, NGlobals;
  23. extern long NLChars, NGChars;
  24. extern char *outputname;
  25. if (! wr_open(outputname)) {
  26. fatal("can't create %s", outputname);
  27. }
  28. if (incore)
  29. generate_section_names();
  30. if (!(flagword & (CFLAG|RFLAG)))
  31. outhead.oh_nrelo = (unsigned short)0;
  32. if (flagword & SFLAG) {
  33. outhead.oh_nname = (unsigned short)0;
  34. outhead.oh_nchar = (long)0;
  35. } else {
  36. outhead.oh_nname = NLocals + NGlobals + outhead.oh_nsect;
  37. outhead.oh_nchar = NLChars + NGChars;
  38. }
  39. if (!incore)
  40. begin_write();
  41. }
  42. /*
  43. * Generate names for all sections and put them after the global names.
  44. * Section names are used for relocation.
  45. */
  46. static void generate_section_names()
  47. {
  48. struct outname *name;
  49. int sectindex;
  50. long size;
  51. extern struct outsect outsect[];
  52. extern char *core_alloc();
  53. size = (long)outhead.oh_nsect * sizeof(struct outname);
  54. name = (struct outname *)core_alloc(ALLOGLOB, size);
  55. if (name == (struct outname *)0)
  56. return;
  57. for (sectindex = 0; sectindex < outhead.oh_nsect; sectindex++, name++) {
  58. name->on_foff = (long)0; /* No string name. */
  59. name->on_type = (S_MIN + sectindex) | S_SCT;
  60. name->on_desc = (unsigned short)0;
  61. name->on_valu = outsect[sectindex].os_base;
  62. }
  63. }
  64. /*
  65. * If we didn't keep the whole output file in core, most of it has been
  66. * written out, and we just finish that.
  67. * If we did, we write out our pieces of core.
  68. */
  69. void endoutput()
  70. {
  71. if (!incore) {
  72. if (!(flagword & SFLAG))
  73. end_write();
  74. } else {
  75. write_bytes();
  76. }
  77. wr_close();
  78. }