output.c 2.1 KB

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