cv.prev.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 <stdio.h>
  6. #include "out.h"
  7. #ifndef NORCSID
  8. static char rcs_id[] = "$Id$" ;
  9. #endif
  10. #define ASSERT(x) switch (2) { case 0: case (x): ; }
  11. /*
  12. * Header and section table of new format object file.
  13. */
  14. struct outhead outhead;
  15. struct outsect outsect[S_MAX];
  16. char *output_file;
  17. int output_file_created;
  18. char *program ;
  19. main(argc, argv)
  20. int argc;
  21. char *argv[];
  22. {
  23. register int nsect;
  24. register struct outsect *sectp;
  25. register FILE *input;
  26. register FILE *output;
  27. ASSERT(sizeof(struct outhead) == SZ_HEAD);
  28. ASSERT(sizeof(struct outsect) == SZ_SECT);
  29. input = stdin; output = stdout;
  30. program= argv[0] ;
  31. switch (argc) {
  32. case 1: break;
  33. case 3: if ((output = fopen(argv[2], "w")) == (FILE *)0)
  34. fatal("Can't write %s.\n", argv[2]);
  35. output_file = argv[2];
  36. output_file_created = 1;
  37. /* FALLTHROUGH */
  38. case 2: if ((input = fopen(argv[1], "r")) == (FILE *)0)
  39. fatal("Can't read %s.\n", argv[1]);
  40. break;
  41. default:fatal("Usage: %s <as object> <dl object>.\n", argv[0]);
  42. }
  43. if ( !rhead(input,&outhead) )
  44. fatal("Reading header failed.\n");
  45. if (BADMAGIC(outhead))
  46. fatal("Not an ack object file.\n");
  47. if (outhead.oh_nrelo > 0)
  48. fprintf(stderr, "Warning: relocation information present.\n");
  49. for ( nsect=0 ; nsect<outhead.oh_nsect ; nsect++ )
  50. if ( !rsect(input,&outsect[nsect]) )
  51. fatal("Reading section table failed.\n");
  52. nsect = outhead.oh_nsect;
  53. sectp = outsect;
  54. while (nsect--) {
  55. register long flen;
  56. #ifdef DO_BSS
  57. register long zero;
  58. #endif DO_BSS
  59. long base;
  60. short cnt;
  61. char buffer[BUFSIZ];
  62. base = sectp->os_base;
  63. flen = sectp->os_flen;
  64. #ifdef DO_BSS
  65. zero = sectp->os_size - flen;
  66. #endif DO_BSS
  67. while (flen) {
  68. cnt = flen > BUFSIZ ? BUFSIZ : flen;
  69. if (fread((char *)buffer, 1, cnt, input) != cnt)
  70. fatal("Reading code bytes failed.\n");
  71. if (fwrite((char *)&base, 4, 1, output) != 1)
  72. fatal("Writing start address failed.\n");
  73. if (fwrite((char *)&cnt, 2, 1, output) != 1)
  74. fatal("Writing byte count failed.\n");
  75. if (fwrite((char *)buffer, 1, cnt, output) != cnt)
  76. fatal("Writing byte count failed.\n");
  77. base += cnt;
  78. flen -= cnt;
  79. }
  80. #ifdef DO_BSS
  81. while (zero) {
  82. cnt = zero > BUFSIZ ? BUFSIZ : zero;
  83. if (fwrite((char *)&base, 4, 1, output) != 1)
  84. fatal("Writing start address failed.\n");
  85. if (fwrite((char *)&cnt, 2, 1, output) != 1)
  86. fatal("Writing byte count failed.\n");
  87. if (fseek(output, (long)cnt, 1) < (long)0)
  88. fatal("Fseek failed.\n");
  89. base += cnt;
  90. zero -= cnt;
  91. }
  92. #endif DO_BSS
  93. sectp++;
  94. }
  95. exit(0);
  96. }
  97. rhead(f,head) struct outhead *head ; FILE *f ; {
  98. char buf[SZ_HEAD] ;
  99. if ( fread(buf,SZ_HEAD,1,f)!=1 ) return 0 ;
  100. iconvert(buf,(char *)head,SF_HEAD) ;
  101. return 1 ;
  102. }
  103. rsect(f,sect) struct outsect *sect ; FILE *f ; {
  104. char buf[SZ_SECT] ;
  105. if ( fread(buf,SZ_SECT,1,f)!=1 ) return 0 ;
  106. iconvert(buf,(char *)sect,SF_SECT) ;
  107. return 1 ;
  108. }
  109. rrelo(f,relo) struct outrelo *relo ; FILE *f ; {
  110. char buf[SZ_RELO] ;
  111. if ( fread(buf,SZ_RELO,1,f)!=1 ) return 0 ;
  112. iconvert(buf,(char *)relo,SF_RELO) ;
  113. return 1 ;
  114. }
  115. rname(f,name) struct outname *name ; FILE *f ; {
  116. char buf[SZ_NAME] ;
  117. if ( fread(buf,SZ_NAME,1,f)!=1 ) return 0 ;
  118. iconvert(buf,(char *)name,SF_NAME) ;
  119. return 1 ;
  120. }
  121. iconvert(buf,str,fmt) char *buf, *str, *fmt ; {
  122. register char *nf, *ni, *no ;
  123. int last, i ;
  124. long value ;
  125. ni=buf ; no=str ; nf=fmt ;
  126. while ( last = *nf++ ) {
  127. last -= '0' ;
  128. if ( last<1 || last >9 ) fatal("illegal out.h format string\n");
  129. value=0 ;
  130. i=last ;
  131. while ( i-- ) {
  132. value = (value<<8) + (ni[i]&0xFF) ;
  133. }
  134. switch ( last ) {
  135. case 0 : break ;
  136. case 1 : *no= value ; break ;
  137. case 2 : *(unsigned short *)no = value ; break ;
  138. case 4 : *(long *)no = value ; break ;
  139. default :
  140. fatal("illegal out.h format string\n");
  141. }
  142. ni += last ; no += last ;
  143. }
  144. }
  145. /* VARARGS1 */
  146. fatal(s, a1, a2)
  147. char *s;
  148. {
  149. fprintf(stderr,"%s: ",program) ;
  150. fprintf(stderr, s, a1, a2);
  151. if (output_file_created)
  152. unlink(output_file);
  153. exit(-1);
  154. }