aslod.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. /* Macros from modules/src/object/obj.h */
  112. #define Xchar(ch) ((ch) & 0377)
  113. #define uget2(c) (Xchar((c)[0]) | ((unsigned) Xchar((c)[1]) << 8))
  114. #define get4(c) (uget2(c) | ((long) uget2((c)+2) << 16))
  115. /* Read the ack.out file header. */
  116. int rhead(FILE* f, struct outhead* head)
  117. {
  118. char buf[SZ_HEAD], *c;
  119. if (fread(buf, sizeof(buf), 1, f) != 1)
  120. return 0;
  121. c = buf;
  122. head->oh_magic = uget2(c); c += 2;
  123. head->oh_stamp = uget2(c); c += 2;
  124. head->oh_flags = uget2(c); c += 2;
  125. head->oh_nsect = uget2(c); c += 2;
  126. head->oh_nrelo = uget2(c); c += 2;
  127. head->oh_nname = uget2(c); c += 2;
  128. head->oh_nemit = get4(c); c += 4;
  129. head->oh_nchar = get4(c);
  130. return 1;
  131. }
  132. /* Read an ack.out section header. */
  133. int rsect(FILE* f, struct outsect* sect)
  134. {
  135. char buf[SZ_SECT], *c;
  136. if (fread(buf, sizeof(buf), 1, f) != 1)
  137. return 0;
  138. c = buf;
  139. sect->os_base = get4(c); c += 4;
  140. sect->os_size = get4(c); c += 4;
  141. sect->os_foff = get4(c); c += 4;
  142. sect->os_flen = get4(c); c += 4;
  143. sect->os_lign = get4(c);
  144. return 1 ;
  145. }
  146. int main(int argc, char* argv[])
  147. {
  148. /* General housecleaning and setup. */
  149. input = stdin;
  150. output = stdout;
  151. program = argv[0];
  152. /* Read in and process any flags. */
  153. while ((argc > 1) && (argv[1][0] == '-'))
  154. {
  155. switch (argv[1][1])
  156. {
  157. case 'h':
  158. fprintf(stderr, "%s: Syntax: aslod [-h] <inputfile> <outputfile>\n",
  159. program);
  160. exit(0);
  161. default:
  162. syntaxerror:
  163. fatal("syntax error --- try -h for help)");
  164. }
  165. argv++;
  166. argc--;
  167. }
  168. /* Process the rest of the arguments. */
  169. switch (argc)
  170. {
  171. case 1: /* No parameters --- read from stdin, write to stdout. */
  172. break;
  173. case 3: /* Both input and output files specified. */
  174. output = fopen(argv[2], "w");
  175. if (!output)
  176. fatal("unable to open output file.");
  177. outputfile = argv[2];
  178. /* fall through */
  179. case 2: /* Input file specified. */
  180. input = fopen(argv[1], "r");
  181. if (!input)
  182. fatal("unable to open input file.");
  183. break;
  184. default:
  185. goto syntaxerror;
  186. }
  187. /* Read and check the ack.out file header. */
  188. if (!rhead(input,&outhead))
  189. fatal("failed to read file header.");
  190. if (BADMAGIC(outhead))
  191. fatal("this isn't an ack object file.");
  192. if (outhead.oh_nrelo > 0)
  193. fprintf(stderr, "Warning: relocation information present.");
  194. if (!((outhead.oh_nsect == NUM_SEGMENTS) ||
  195. (outhead.oh_nsect == (NUM_SEGMENTS+1))))
  196. fatal("the input file must have %d sections, not %ld.",
  197. NUM_SEGMENTS, outhead.oh_nsect);
  198. /* Read in the section headers. */
  199. {
  200. int i;
  201. for (i=0; i<outhead.oh_nsect; i++)
  202. {
  203. if (!rsect(input, &outsect[i]))
  204. fatal("failed to read section header %d.", i);
  205. }
  206. }
  207. /* A few checks */
  208. if (outsect[BSS].os_flen != 0)
  209. fatal("the bss space contains initialized data.");
  210. if (!follows(&outsect[BSS], &outsect[DATA]))
  211. fatal("the bss segment must follow the data segment.");
  212. if (!follows(& outsect[ROM], &outsect[TEXT]))
  213. fatal("the rom segment must follow the text segment.");
  214. if (!follows(&outsect[DATA], &outsect[ROM]))
  215. fatal("the data segment must follow the rom segment.") ;
  216. /* Check for an optional end segment (which is otherwise
  217. * ignored). */
  218. if (outhead.oh_nsect == (NUM_SEGMENTS+1))
  219. {
  220. if (!follows(&outsect[NUM_SEGMENTS], &outsect[BSS]))
  221. fatal("end segment must follow bss");
  222. if ( outsect[NUM_SEGMENTS].os_size != 0 )
  223. fatal("end segment must be empty");
  224. }
  225. /* And go! */
  226. emits(&outsect[TEXT]);
  227. emits(&outsect[ROM]);
  228. emits(&outsect[DATA]);
  229. if (ferror(output))
  230. fatal("output write error");
  231. if (outputfile)
  232. chmod(outputfile, 0755);
  233. /* Summarise what we've done. */
  234. {
  235. long ss = 0;
  236. printf(" base : %08lX\n", outsect[TEXT].os_base) ;
  237. printf(" text = %08lX\n", outsect[TEXT].os_size);
  238. printf(" rom = %08lX\n", outsect[ROM].os_size);
  239. printf(" data = %08lX\n", outsect[DATA].os_size);
  240. printf(" bss = %08lX\n", outsect[BSS].os_size);
  241. ss += outsect[TEXT].os_size;
  242. ss += outsect[ROM].os_size;
  243. ss += outsect[DATA].os_size;
  244. ss += outsect[BSS].os_size;
  245. printf("TOTAL = %08lX\n", ss);
  246. }
  247. return 0;
  248. }