aslod.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Simple tool to produce a memory dump for an absolute
  3. * ack.out file. Suitable for noddy operating systems
  4. * like DOS, CP/M, Arthur, etc. Also useful for RAM
  5. * images (but *not* ROM images, note) and, more
  6. * importantly, general test purposes.
  7. *
  8. * Mostly pinched from the ARM cv (and then rewritten in
  9. * ANSI C). Which, according to the comment, was pinched
  10. * from m68k2; therefore I am merely continuing a time-
  11. * honoured tradition.
  12. *
  13. * (I was 10 when the original for this was checked into
  14. * CVS...)
  15. *
  16. * dtrg, 2006-10-17
  17. *
  18. * $Source$
  19. * $State$
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include "out.h"
  26. #define ASSERT(x) switch (2) { case 0: case (x): ; }
  27. /*
  28. * Header and section table of ack object file.
  29. */
  30. struct outhead outhead;
  31. struct outsect outsect[S_MAX];
  32. char* stringarea;
  33. char* outputfile = NULL; /* Name of output file, or NULL */
  34. char* program; /* Name of current program: argv[0] */
  35. FILE* input; /* Input stream */
  36. FILE* output; /* Output stream */
  37. #define readf(a, b, c) fread((a), (b), (int)(c), input)
  38. #define writef(a, b, c) fwrite((a), (b), (int)(c), output)
  39. /* Output file definitions and such */
  40. #define HDR_LENGTH 32
  41. char hdr[HDR_LENGTH] ;
  42. /* Segment numbers understood by aslod. */
  43. enum {
  44. TEXT = 0,
  45. ROM,
  46. DATA,
  47. BSS,
  48. NUM_SEGMENTS
  49. };
  50. #define N_EXT 040
  51. #define N_UNDEF 00
  52. #define N_ABS 01
  53. #define N_TEXT 02
  54. #define N_DATA 03
  55. #define N_BSS 04
  56. #define N_FN 037
  57. /* Produce an error message and exit. */
  58. void fatal(const char* s, ...)
  59. {
  60. va_list ap;
  61. fprintf(stderr, "%s: ",program) ;
  62. va_start(ap, s);
  63. vfprintf(stderr, s, ap);
  64. va_end(ap);
  65. fprintf(stderr, "\n");
  66. if (outputfile)
  67. unlink(outputfile);
  68. exit(1);
  69. }
  70. /* Calculate the result of a aligned to b (rounding up if necessary). */
  71. long align(long a, long b)
  72. {
  73. a += b - 1;
  74. return a - a % b;
  75. }
  76. int follows(struct outsect* pa, struct outsect* pb)
  77. {
  78. /* return 1 if pa follows pb */
  79. return (pa->os_base == align(pb->os_base+pb->os_size, pa->os_lign));
  80. }
  81. /* Copies the contents of a section from the input stream
  82. * to the output stream, zero filling any uninitialised
  83. * space. */
  84. void emits(struct outsect* section)
  85. {
  86. char buffer[BUFSIZ];
  87. /* Copy the actual data. */
  88. {
  89. long n = section->os_flen;
  90. while (n > 0)
  91. {
  92. int blocksize = (n > BUFSIZ) ? BUFSIZ : n;
  93. readf(buffer, 1, blocksize);
  94. writef(buffer, 1, blocksize);
  95. n -= blocksize;
  96. }
  97. }
  98. /* Zero fill any remaining space. */
  99. if (section->os_flen != section->os_size)
  100. {
  101. long n = section->os_size - section->os_flen;
  102. memset(buffer, 0, BUFSIZ);
  103. while (n > 0)
  104. {
  105. int blocksize = (n > BUFSIZ) ? BUFSIZ : n;
  106. writef(buffer, 1, blocksize);
  107. n -= blocksize;
  108. }
  109. }
  110. }
  111. void iconvert(char* buf, char* str, char* fmt)
  112. {
  113. register char *nf, *ni, *no ;
  114. int last, i ;
  115. long value ;
  116. ni=buf ; no=str ; nf=fmt ;
  117. while ( last = *nf++ ) {
  118. last -= '0' ;
  119. if ( last<1 || last >9 ) fatal("illegal out.h format string\n");
  120. value=0 ;
  121. i=last ;
  122. while ( i-- ) {
  123. value = (value<<8) + (ni[i]&0xFF) ;
  124. }
  125. switch ( last ) {
  126. case 0 : break ;
  127. case 1 : *no= value ; break ;
  128. case 2 : *(unsigned short *)no = value ; break ;
  129. case 4 : *(long *)no = value ; break ;
  130. default :
  131. fatal("illegal out.h format string\n");
  132. }
  133. ni += last ; no += last ;
  134. }
  135. }
  136. /* Read the ack.out file header. */
  137. int rhead(FILE* f, struct outhead* head)
  138. {
  139. char buf[sizeof(struct outhead)];
  140. if (fread(buf, sizeof(buf), 1, f) != 1)
  141. return 0;
  142. iconvert(buf, (char*) head, SF_HEAD);
  143. return 1;
  144. }
  145. /* Read an ack.out section header. */
  146. int rsect(FILE* f, struct outsect* sect)
  147. {
  148. char buf[sizeof(struct outsect)];
  149. if (fread(buf, sizeof(buf), 1, f) != 1)
  150. return 0;
  151. iconvert(buf, (char*) sect, SF_SECT);
  152. return 1 ;
  153. }
  154. int main(int argc, char* argv[])
  155. {
  156. /* General housecleaning and setup. */
  157. input = stdin;
  158. output = stdout;
  159. program = argv[0];
  160. /* Read in and process any flags. */
  161. while ((argc > 1) && (argv[1][0] == '-'))
  162. {
  163. switch (argv[1][1])
  164. {
  165. case 'h':
  166. fprintf(stderr, "%s: Syntax: aslod [-h] <inputfile> <outputfile>\n",
  167. program);
  168. exit(0);
  169. default:
  170. syntaxerror:
  171. fatal("syntax error --- try -h for help)");
  172. }
  173. argv++;
  174. argc--;
  175. }
  176. /* Process the rest of the arguments. */
  177. switch (argc)
  178. {
  179. case 1: /* No parameters --- read from stdin, write to stdout. */
  180. break;
  181. case 3: /* Both input and output files specified. */
  182. output = fopen(argv[2], "w");
  183. if (!output)
  184. fatal("unable to open output file.");
  185. outputfile = argv[2];
  186. /* fall through */
  187. case 2: /* Input file specified. */
  188. input = fopen(argv[1], "r");
  189. if (!input)
  190. fatal("unable to open input file.");
  191. break;
  192. default:
  193. goto syntaxerror;
  194. }
  195. /* Read and check the ack.out file header. */
  196. if (!rhead(input,&outhead))
  197. fatal("failed to read file header.");
  198. if (BADMAGIC(outhead))
  199. fatal("this isn't an ack object file.");
  200. if (outhead.oh_nrelo > 0)
  201. fprintf(stderr, "Warning: relocation information present.");
  202. if (!((outhead.oh_nsect == NUM_SEGMENTS) ||
  203. (outhead.oh_nsect == (NUM_SEGMENTS+1))))
  204. fatal("the input file must have %d sections, not %ld.",
  205. NUM_SEGMENTS, outhead.oh_nsect);
  206. /* Read in the section headers. */
  207. {
  208. int i;
  209. for (i=0; i<outhead.oh_nsect; i++)
  210. {
  211. if (!rsect(input, &outsect[i]))
  212. fatal("failed to read section header %d.", i);
  213. }
  214. }
  215. /* A few checks */
  216. if (outsect[BSS].os_flen != 0)
  217. fatal("the bss space contains initialized data.");
  218. if (!follows(&outsect[BSS], &outsect[DATA]))
  219. fatal("the bss segment must follow the data segment.");
  220. if (!follows(& outsect[ROM], &outsect[TEXT]))
  221. fatal("the rom segment must follow the text segment.");
  222. if (!follows(&outsect[DATA], &outsect[ROM]))
  223. fatal("the data segment must follow the rom segment.") ;
  224. /* Check for an optional end segment (which is otherwise
  225. * ignored). */
  226. if (outhead.oh_nsect == (NUM_SEGMENTS+1))
  227. {
  228. if (!follows(&outsect[NUM_SEGMENTS], &outsect[BSS]))
  229. fatal("end segment must follow bss");
  230. if ( outsect[NUM_SEGMENTS].os_size != 0 )
  231. fatal("end segment must be empty");
  232. }
  233. /* And go! */
  234. emits(&outsect[TEXT]);
  235. emits(&outsect[ROM]);
  236. emits(&outsect[DATA]);
  237. if (ferror(output))
  238. fatal("output write error");
  239. if (outputfile)
  240. chmod(outputfile, 0755);
  241. /* Summarise what we've done. */
  242. {
  243. long ss = 0;
  244. printf(" base : %08lX\n", outsect[TEXT].os_base) ;
  245. printf(" text = %08lX\n", outsect[TEXT].os_size);
  246. printf(" rom = %08lX\n", outsect[ROM].os_size);
  247. printf(" data = %08lX\n", outsect[DATA].os_size);
  248. printf(" bss = %08lX\n", outsect[BSS].os_size);
  249. ss += outsect[TEXT].os_size;
  250. ss += outsect[ROM].os_size;
  251. ss += outsect[DATA].os_size;
  252. ss += outsect[BSS].os_size;
  253. printf("TOTAL = %08lX\n", ss);
  254. }
  255. return 0;
  256. }