cv.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /* This is a special cv.c program for testing purposes ;
  7. * it converts to a stripped version of COFF
  8. */
  9. #include <stdio.h>
  10. #include <filehdr.h>
  11. #include <aouthdr.h>
  12. #include <scnhdr.h>
  13. #include <out.h>
  14. #define AOUTHSZ sizeof(AOUTHDR)
  15. #define ASSERT(x) switch (2) { case 0: case (x): ; }
  16. /*
  17. * Header and section table of new format object file.
  18. */
  19. struct outhead outhead;
  20. struct outsect outsect[S_MAX];
  21. char *output_file;
  22. int outputfile_created;
  23. int rom_in_data;
  24. char *program ;
  25. char flag ;
  26. #define readf(a, b, c) fread((a), (b), (int)(c), input)
  27. #define writef(a, b, c) fwrite((a), (b), (int)(c), output)
  28. /* Output file definitions and such */
  29. #define TS 0
  30. #define DS 1
  31. #define BS 2
  32. #define NS 3
  33. FILHDR filh;
  34. AOUTHDR aouth;
  35. SCNHDR scnh[NS];
  36. #define ENTRY 0
  37. #define MC68MAGIC 0520
  38. #define MYFLAGS 01017
  39. #define STYP_TEXT 0x20
  40. #define STYP_DATA 0x40
  41. #define STYP_BSS 0x80
  42. #define TEXTSG 0
  43. #define ROMSG 1
  44. #define DATASG 2
  45. #define BSSSG 3
  46. #define LSECT BSSSG+1
  47. #define NSECT LSECT+1
  48. FILE *input;
  49. FILE *output;
  50. main(argc, argv)
  51. int argc;
  52. char *argv[];
  53. {
  54. register int nsect;
  55. long magic ;
  56. long textsize, datasize, bsssize;
  57. extern long ftell();
  58. long filepos;
  59. int scn;
  60. ASSERT(sizeof(struct outhead) == SZ_HEAD);
  61. ASSERT(sizeof(struct outsect) == SZ_SECT);
  62. input = stdin; output = stdout;
  63. program= argv[0] ;
  64. if ( argc>1 && argv[1][0]=='-' ) {
  65. flag=argv[1][1] ;
  66. argc-- ; argv++ ;
  67. }
  68. switch (argc) {
  69. case 1: break;
  70. case 3: if ((output = fopen(argv[2], "w")) == (FILE *)0)
  71. fatal("Can't write %s.\n", argv[2]);
  72. output_file = argv[2];
  73. outputfile_created = 1;
  74. /* FALLTHROUGH */
  75. case 2: if ((input = fopen(argv[1], "r")) == (FILE *)0)
  76. fatal("Can't read %s.\n", argv[1]);
  77. break;
  78. default:fatal("Usage: %s <as object> <dl object>.\n", argv[0]);
  79. }
  80. if ( !rhead(input,&outhead) )
  81. fatal("Reading header failed.\n");
  82. if (BADMAGIC(outhead))
  83. fatal("Not an ack object file.\n");
  84. if (outhead.oh_nrelo > 0)
  85. fprintf(stderr, "Warning: relocation information present.\n");
  86. if ( outhead.oh_nsect!=LSECT && outhead.oh_nsect!=NSECT )
  87. fatal("Input file must have %d sections, not %ld\n",
  88. NSECT,outhead.oh_nsect) ;
  89. for ( nsect=0 ; nsect<outhead.oh_nsect ; nsect++ )
  90. if ( !rsect(input,&outsect[nsect]) )
  91. fatal("Reading section table failed.\n");
  92. /* A few checks */
  93. if ( outsect[TEXTSG].os_base != ENTRY)
  94. fatal("text must start at %lx not at 0x%lx\n", ENTRY,
  95. outsect[TEXTSG].os_base) ;
  96. if ( outsect[BSSSG].os_flen != 0 )
  97. printf("Warning: bss space contains initialized data\n") ;
  98. /* as actually writes zeroes in the bss segment */
  99. if ( outsect[BSSSG].os_base != outsect[DATASG].os_base+
  100. outsect[DATASG].os_size )
  101. fatal("bss segment must follow data segment\n") ;
  102. /* 410 file with ROMSG in instruction space */
  103. rom_in_data = 0;
  104. magic= 0410 ;
  105. textsize= outsect[TEXTSG].os_size + outsect[ROMSG].os_size ;
  106. datasize= outsect[DATASG].os_size ;
  107. if ( outsect[ROMSG].os_base != outsect[TEXTSG].os_base+
  108. outsect[TEXTSG].os_size )
  109. fatal("rom segment must follow text\n") ;
  110. bsssize = outsect[BSSSG].os_size;
  111. if ( outhead.oh_nsect==NSECT ) {
  112. if ( outsect[LSECT].os_base != outsect[BSSSG].os_base+
  113. outsect[BSSSG].os_size )
  114. fatal("end segment must follow bss\n") ;
  115. if ( outsect[LSECT].os_size != 0 )
  116. fatal("end segment must be empty\n") ;
  117. }
  118. filh.f_magic = MC68MAGIC;
  119. filh.f_nscns = 3; /* three sections: .text, data, .bss */
  120. filh.f_timdat = (long) time(0); /* doesn't really matter */
  121. filh.f_symptr = 0L; /* no symbol table */
  122. filh.f_nsyms = 0L; /* ditto */
  123. filh.f_opthdr = AOUTHSZ;
  124. filh.f_flags = MYFLAGS;
  125. aouth.magic = magic;
  126. aouth.vstamp = 0;
  127. aouth.tsize = textsize;
  128. aouth.dsize = datasize;
  129. aouth.bsize = bsssize;
  130. aouth.entry = ENTRY;
  131. aouth.text_start= ENTRY;
  132. aouth.data_start= outsect[DATASG].os_base;
  133. strcpy(scnh[TS].s_name, _TEXT);
  134. strcpy(scnh[DS].s_name, _DATA);
  135. strcpy(scnh[BS].s_name, _BSS);
  136. scnh[TS].s_vaddr = (scnh[TS].s_paddr = aouth.text_start);
  137. scnh[DS].s_vaddr = (scnh[DS].s_paddr = aouth.data_start);
  138. scnh[BS].s_vaddr = (scnh[BS].s_paddr = aouth.data_start + datasize);
  139. scnh[TS].s_size = textsize;
  140. scnh[DS].s_size = datasize;
  141. scnh[BS].s_size = bsssize;
  142. scnh[TS].s_scnptr = FILHSZ + AOUTHSZ + 3 * SCNHSZ;
  143. scnh[DS].s_scnptr = FILHSZ + AOUTHSZ + 3 * SCNHSZ + textsize;
  144. scnh[BS].s_scnptr = 0L; /* always 0 for .bss section */
  145. for (scn=TS; scn<=BS; scn++) {
  146. scnh[scn].s_relptr = 0L; /* no relocation */
  147. scnh[scn].s_lnnoptr = 0L; /* no line numbers */
  148. scnh[scn].s_nreloc = 0; /* no relocation */
  149. scnh[scn].s_nlnno = 0; /* no line numbers */
  150. }
  151. scnh[TS].s_flags = STYP_TEXT;
  152. scnh[DS].s_flags = STYP_DATA;
  153. scnh[BS].s_flags = STYP_BSS;
  154. /* now write the new file headers */
  155. ffwrite(&filh, FILHSZ, 1);
  156. ffwrite(&aouth, AOUTHSZ, 1);
  157. ffwrite(scnh, SCNHSZ, 3); /* write all three section headers */
  158. emits(&outsect[TEXTSG]) ;
  159. emits(&outsect[ROMSG]) ;
  160. emits(&outsect[DATASG]) ;
  161. if ( outputfile_created ) chmod(argv[2],0755);
  162. return 0;
  163. }
  164. ffwrite(ptr, size, nitems)
  165. char *ptr;
  166. {
  167. writef(ptr, size, nitems);
  168. if (ferror(output))
  169. fatal("output write error\n");
  170. }
  171. /*
  172. * Transfer the emitted bytes from one file to another.
  173. */
  174. emits(section) struct outsect *section ; {
  175. register long n ;
  176. register int blk;
  177. char buffer[BUFSIZ];
  178. n= section->os_flen ;
  179. while (n > 0) {
  180. blk = n > BUFSIZ ? BUFSIZ : n;
  181. readf(buffer, 1, blk);
  182. writef(buffer, 1, blk);
  183. n -= blk;
  184. }
  185. if ((n=section->os_size - section->os_flen) > 0) {
  186. fseek(output, n-1, 1);
  187. writef("\0",1,1);
  188. }
  189. }
  190. rhead(f,head) struct outhead *head ; FILE *f ; {
  191. char buf[SZ_HEAD] ;
  192. if ( fread(buf,SZ_HEAD,1,f)!=1 ) return 0 ;
  193. iconvert(buf,(char *)head,SF_HEAD) ;
  194. return 1 ;
  195. }
  196. rsect(f,sect) struct outsect *sect ; FILE *f ; {
  197. char buf[SZ_SECT] ;
  198. if ( fread(buf,SZ_SECT,1,f)!=1 ) return 0 ;
  199. iconvert(buf,(char *)sect,SF_SECT) ;
  200. return 1 ;
  201. }
  202. iconvert(buf,str,fmt) char *buf, *str, *fmt ; {
  203. register char *nf, *ni, *no ;
  204. int last, i ;
  205. long value ;
  206. ni=buf ; no=str ; nf=fmt ;
  207. while ( last = *nf++ ) {
  208. last -= '0' ;
  209. if ( last<1 || last >9 ) fatal("illegal out.h format string\n");
  210. value=0 ;
  211. i=last ;
  212. while ( i-- ) {
  213. value = (value<<8) + (ni[i]&0xFF) ;
  214. }
  215. switch ( last ) {
  216. case 0 : break ;
  217. case 1 : *no= value ; break ;
  218. case 2 : *(unsigned short *)no = value ; break ;
  219. case 4 : *(long *)no = value ; break ;
  220. default :
  221. fatal("illegal out.h format string\n");
  222. }
  223. ni += last ; no += last ;
  224. }
  225. }
  226. /* VARARGS1 */
  227. fatal(s, a1, a2)
  228. char *s;
  229. {
  230. fprintf(stderr,"%s: ",program) ;
  231. fprintf(stderr, s, a1, a2);
  232. if (outputfile_created)
  233. unlink(output_file);
  234. exit(-1);
  235. }