cv.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* $Header$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. *
  6. */
  7. /*
  8. * Fortunately we sold our PMDS machine. Unfortunately, we also sold the
  9. * documentation that came with it, so I don't know the a.out format
  10. * of the machine. This program is written partly by guessing, and partly from
  11. * an older conversion program for the PMDS, which put the whole program
  12. * in data. The produced object file does not contain a namelist.
  13. */
  14. #include <stdio.h>
  15. #include <out.h>
  16. #define ASSERT(x) switch (2) { case 0: case (x): ; }
  17. /*
  18. * Header and section table of new format object file.
  19. */
  20. struct outhead outhead;
  21. struct outsect outsect[S_MAX];
  22. char *output_file;
  23. int outputfile_created;
  24. int rom_in_data;
  25. char *program ;
  26. char flag ;
  27. #define writef(a, b, c) fwrite((a), (b), (int)(c), output)
  28. /* Output file definitions and such */
  29. #define ENTRY 0
  30. #define HDRSIZE 40
  31. char hdr[HDRSIZE];
  32. #define TEXTSG 0
  33. #define ROMSG 1
  34. #define DATASG 2
  35. #define BSSSG 3
  36. #define LSECT BSSSG+1
  37. #define NSECT LSECT+1
  38. FILE *output;
  39. main(argc, argv)
  40. int argc;
  41. char *argv[];
  42. {
  43. register int nsect;
  44. long magic ;
  45. long textsize ;
  46. long datasize ;
  47. long bsssize;
  48. long symstart;
  49. extern long ftell();
  50. output = stdout;
  51. program= argv[0] ;
  52. if ( argc>1 && argv[1][0]=='-' ) {
  53. flag=argv[1][1] ;
  54. argc-- ; argv++ ;
  55. }
  56. switch (argc) {
  57. case 3: if ((output = fopen(argv[2], "w")) == (FILE *)0)
  58. fatal("Can't write %s.\n", argv[2]);
  59. output_file = argv[2];
  60. outputfile_created = 1;
  61. if (! rd_open(argv[1]))
  62. fatal("Can't read %s.\n", argv[1]);
  63. break;
  64. default:fatal("Usage: %s <ACK object> <Mantra object>.\n", argv[0]);
  65. }
  66. rd_ohead(&outhead);
  67. if (BADMAGIC(outhead))
  68. fatal("Not an ack object file.\n");
  69. if (outhead.oh_flags & HF_LINK)
  70. fatal("Contains unresolved references.\n");
  71. if (outhead.oh_nrelo > 0)
  72. fprintf(stderr, "Warning: relocation information present.\n");
  73. if ( outhead.oh_nsect!=LSECT && outhead.oh_nsect!=NSECT )
  74. fatal("Input file must have %d sections, not %ld\n",
  75. NSECT,outhead.oh_nsect) ;
  76. rd_sect(outsect, outhead.oh_nsect);
  77. /* A few checks */
  78. if ( outsect[TEXTSG].os_base != ENTRY)
  79. fatal("text must start at %d not at 0x%lx\n", ENTRY,
  80. outsect[TEXTSG].os_base) ;
  81. if ( outsect[BSSSG].os_flen != 0 )
  82. fatal("bss space contains initialized data\n") ;
  83. if ( outsect[BSSSG].os_base != outsect[DATASG].os_base+
  84. outsect[DATASG].os_size )
  85. fatal("bss segment must follow data segment\n") ;
  86. if ( outsect[ROMSG].os_lign == 0x8000 ) {
  87. /* 410 file with ROMSG in data space */
  88. rom_in_data = 1;
  89. magic= 0410 ;
  90. textsize= outsect[TEXTSG].os_size ;
  91. datasize= outsect[ROMSG].os_size + outsect[DATASG].os_size ;
  92. if ( outsect[DATASG].os_base != outsect[ROMSG].os_base+
  93. outsect[ROMSG].os_size )
  94. fatal("data segment must follow rom\n") ;
  95. } else
  96. if ( outsect[DATASG].os_lign == 0x8000 ) {
  97. /* 410 file with ROMSG in instruction space */
  98. rom_in_data = 0;
  99. magic= 0410 ;
  100. textsize= outsect[TEXTSG].os_size + outsect[ROMSG].os_size ;
  101. datasize= outsect[DATASG].os_size ;
  102. if ( outsect[ROMSG].os_base != outsect[TEXTSG].os_base+
  103. outsect[TEXTSG].os_size )
  104. fatal("rom segment must follow text\n") ;
  105. } else {
  106. /* Plain 407 file */
  107. rom_in_data = 1;
  108. magic= 0407 ;
  109. textsize= outsect[TEXTSG].os_size ;
  110. datasize= outsect[ROMSG].os_size + outsect[DATASG].os_size ;
  111. if ( outsect[ROMSG].os_base != outsect[TEXTSG].os_base+
  112. outsect[TEXTSG].os_size )
  113. fatal("rom segment must follow text\n") ;
  114. if ( outsect[DATASG].os_base != outsect[ROMSG].os_base+
  115. outsect[ROMSG].os_size )
  116. fatal("data segment must follow rom\n") ;
  117. }
  118. bsssize = outsect[BSSSG].os_size;
  119. if ( outhead.oh_nsect==NSECT ) {
  120. if ( outsect[LSECT].os_base != outsect[BSSSG].os_base+
  121. outsect[BSSSG].os_size )
  122. fatal("end segment must follow bss\n") ;
  123. if ( outsect[LSECT].os_size != 0 )
  124. fatal("end segment must be empty\n") ;
  125. }
  126. /* Action at last */
  127. fseek(output,(long) HDRSIZE,0);
  128. emits(&outsect[TEXTSG]) ;
  129. emits(&outsect[ROMSG]) ;
  130. emits(&outsect[DATASG]) ;
  131. fseek(output,0L,0);
  132. sethdr(0, (long) magic);
  133. sethdr(4, textsize);
  134. sethdr(8, datasize);
  135. sethdr(12, bsssize);
  136. writef(hdr, 1, 40);
  137. if ( ferror(output) ) {
  138. fatal("output write error\n") ;
  139. }
  140. if ( outputfile_created ) chmod(argv[2],0755);
  141. return 0;
  142. }
  143. sethdr(off,val) long val ; {
  144. hdr[off] = val>>24 ;
  145. hdr[off+1] = val>>16 ;
  146. hdr[off+2] = val>>8 ;
  147. hdr[off+3] = val ;
  148. }
  149. /*
  150. * Transfer the emitted byted from one file to another.
  151. */
  152. emits(section) struct outsect *section ; {
  153. char *p;
  154. char *calloc();
  155. long length = section->os_size;
  156. rd_outsect(section - outsect);
  157. while (section->os_size > 0) {
  158. int i = section->os_size > 10*1024 ? 10*1024 : section->os_size;
  159. section->os_size -= i;
  160. if (!(p = calloc(i, 1))) {
  161. fatal("No memory.\n");
  162. }
  163. rd_emit(p, i >= section->os_flen ? section->os_flen:(long) i);
  164. section->os_flen -= i;
  165. writef(p, 1, i);
  166. free(p);
  167. }
  168. }
  169. /* VARARGS1 */
  170. fatal(s, a1, a2)
  171. char *s;
  172. {
  173. fprintf(stderr,"%s: ",program) ;
  174. fprintf(stderr, s, a1, a2);
  175. if (outputfile_created)
  176. unlink(output_file);
  177. exit(-1);
  178. }
  179. rd_fatal() { fatal("read error.\n"); }