output.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 void 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. void 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 void generate_section_names()
  48. {
  49. struct outname *name;
  50. int sectindex;
  51. long size;
  52. extern struct outsect outsect[];
  53. extern char *core_alloc();
  54. size = (long)outhead.oh_nsect * sizeof(struct outname);
  55. name = (struct outname *)core_alloc(ALLOGLOB, size);
  56. if (name == (struct outname *)0)
  57. return;
  58. for (sectindex = 0; sectindex < outhead.oh_nsect; sectindex++, name++) {
  59. name->on_foff = (long)0; /* No string name. */
  60. name->on_type = (S_MIN + sectindex) | S_SCT;
  61. name->on_desc = (unsigned short)0;
  62. name->on_valu = outsect[sectindex].os_base;
  63. }
  64. }
  65. /*
  66. * If we didn't keep the whole output file in core, most of it has been
  67. * written out, and we just finish that.
  68. * If we did, we write out our pieces of core.
  69. */
  70. void endoutput()
  71. {
  72. if (!incore) {
  73. if (!(flagword & SFLAG))
  74. end_write();
  75. } else {
  76. write_bytes();
  77. }
  78. wr_close();
  79. }