aslod.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 <inttypes.h>
  26. #include <unistd.h>
  27. #include <sys/stat.h>
  28. #include "out.h"
  29. #define ASSERT(x) switch (2) { case 0: case (x): ; }
  30. /*
  31. * Header and section table of ack object file.
  32. */
  33. struct outhead outhead;
  34. struct outsect outsect[S_MAX];
  35. char* stringarea;
  36. char* outputfile = NULL; /* Name of output file, or NULL */
  37. char* program; /* Name of current program: argv[0] */
  38. FILE* input; /* Input stream */
  39. FILE* output; /* Output stream */
  40. #define readf(a, b, c) fread((a), (b), (int)(c), input)
  41. #define writef(a, b, c) fwrite((a), (b), (int)(c), output)
  42. /* Output file definitions and such */
  43. #define HDR_LENGTH 32
  44. char hdr[HDR_LENGTH] ;
  45. /* Segment numbers understood by aslod. */
  46. enum {
  47. TEXT = 0,
  48. ROM,
  49. DATA,
  50. BSS,
  51. NUM_SEGMENTS
  52. };
  53. #define N_EXT 040
  54. #define N_UNDEF 00
  55. #define N_ABS 01
  56. #define N_TEXT 02
  57. #define N_DATA 03
  58. #define N_BSS 04
  59. #define N_FN 037
  60. /* Produce an error message and exit. */
  61. void fatal(const char* s, ...)
  62. {
  63. va_list ap;
  64. fprintf(stderr, "%s: ",program) ;
  65. va_start(ap, s);
  66. vfprintf(stderr, s, ap);
  67. va_end(ap);
  68. fprintf(stderr, "\n");
  69. if (outputfile)
  70. unlink(outputfile);
  71. exit(1);
  72. }
  73. /* Calculate the result of a aligned to b (rounding up if necessary). */
  74. long align(long a, long b)
  75. {
  76. a += b - 1;
  77. return a - a % b;
  78. }
  79. int follows(struct outsect* pa, struct outsect* pb)
  80. {
  81. /* return 1 if pa follows pb */
  82. return (pa->os_base == align(pb->os_base+pb->os_size, pa->os_lign));
  83. }
  84. /* Copies the contents of a section from the input stream
  85. * to the output stream, zero filling any uninitialised
  86. * space. */
  87. void emits(struct outsect* section)
  88. {
  89. char buffer[BUFSIZ];
  90. /* Copy the actual data. */
  91. {
  92. long n = section->os_flen;
  93. while (n > 0)
  94. {
  95. int blocksize = (n > BUFSIZ) ? BUFSIZ : n;
  96. readf(buffer, 1, blocksize);
  97. writef(buffer, 1, blocksize);
  98. n -= blocksize;
  99. }
  100. }
  101. /* Zero fill any remaining space. */
  102. if (section->os_flen != section->os_size)
  103. {
  104. long n = section->os_size - section->os_flen;
  105. memset(buffer, 0, BUFSIZ);
  106. while (n > 0)
  107. {
  108. int blocksize = (n > BUFSIZ) ? BUFSIZ : n;
  109. writef(buffer, 1, blocksize);
  110. n -= blocksize;
  111. }
  112. }
  113. }
  114. /* Macros from modules/src/object/obj.h */
  115. #define Xchar(ch) ((ch) & 0377)
  116. #define uget2(c) (Xchar((c)[0]) | ((unsigned) Xchar((c)[1]) << 8))
  117. #define get4(c) (uget2(c) | ((long) uget2((c)+2) << 16))
  118. /* Read the ack.out file header. */
  119. int rhead(FILE* f, struct outhead* head)
  120. {
  121. char buf[SZ_HEAD], *c;
  122. if (fread(buf, sizeof(buf), 1, f) != 1)
  123. return 0;
  124. c = buf;
  125. head->oh_magic = uget2(c); c += 2;
  126. head->oh_stamp = uget2(c); c += 2;
  127. head->oh_flags = uget2(c); c += 2;
  128. head->oh_nsect = uget2(c); c += 2;
  129. head->oh_nrelo = uget2(c); c += 2;
  130. head->oh_nname = uget2(c); c += 2;
  131. head->oh_nemit = get4(c); c += 4;
  132. head->oh_nchar = get4(c);
  133. return 1;
  134. }
  135. /* Read an ack.out section header. */
  136. int rsect(FILE* f, struct outsect* sect)
  137. {
  138. char buf[SZ_SECT], *c;
  139. if (fread(buf, sizeof(buf), 1, f) != 1)
  140. return 0;
  141. c = buf;
  142. sect->os_base = get4(c); c += 4;
  143. sect->os_size = get4(c); c += 4;
  144. sect->os_foff = get4(c); c += 4;
  145. sect->os_flen = get4(c); c += 4;
  146. sect->os_lign = get4(c);
  147. return 1 ;
  148. }
  149. int main(int argc, char* argv[])
  150. {
  151. /* General housecleaning and setup. */
  152. input = stdin;
  153. output = stdout;
  154. program = argv[0];
  155. /* Read in and process any flags. */
  156. while ((argc > 1) && (argv[1][0] == '-'))
  157. {
  158. switch (argv[1][1])
  159. {
  160. case 'h':
  161. fprintf(stderr, "%s: Syntax: aslod [-h] <inputfile> <outputfile>\n",
  162. program);
  163. exit(0);
  164. default:
  165. syntaxerror:
  166. fatal("syntax error --- try -h for help");
  167. }
  168. argv++;
  169. argc--;
  170. }
  171. /* Process the rest of the arguments. */
  172. switch (argc)
  173. {
  174. case 1: /* No parameters --- read from stdin, write to stdout. */
  175. break;
  176. case 3: /* Both input and output files specified. */
  177. output = fopen(argv[2], "w");
  178. if (!output)
  179. fatal("unable to open output file.");
  180. outputfile = argv[2];
  181. /* fall through */
  182. case 2: /* Input file specified. */
  183. input = fopen(argv[1], "r");
  184. if (!input)
  185. fatal("unable to open input file.");
  186. break;
  187. default:
  188. goto syntaxerror;
  189. }
  190. /* Read and check the ack.out file header. */
  191. if (!rhead(input,&outhead))
  192. fatal("failed to read file header.");
  193. if (BADMAGIC(outhead))
  194. fatal("this isn't an ack object file.");
  195. if (outhead.oh_nrelo > 0)
  196. fprintf(stderr, "Warning: relocation information present.");
  197. if (!((outhead.oh_nsect == NUM_SEGMENTS) ||
  198. (outhead.oh_nsect == (NUM_SEGMENTS+1))))
  199. fatal("the input file must have %d sections, not %ld.",
  200. NUM_SEGMENTS, outhead.oh_nsect);
  201. /* Read in the section headers. */
  202. {
  203. int i;
  204. for (i=0; i<outhead.oh_nsect; i++)
  205. {
  206. if (!rsect(input, &outsect[i]))
  207. fatal("failed to read section header %d.", i);
  208. }
  209. }
  210. /* A few checks */
  211. if (outsect[BSS].os_flen != 0)
  212. fatal("the bss space contains initialized data.");
  213. if (!follows(&outsect[BSS], &outsect[DATA]))
  214. fatal("the bss segment must follow the data segment.");
  215. if (!follows(& outsect[ROM], &outsect[TEXT]))
  216. fatal("the rom segment must follow the text segment.");
  217. if (!follows(&outsect[DATA], &outsect[ROM]))
  218. fatal("the data segment must follow the rom segment.") ;
  219. /* Check for an optional end segment (which is otherwise
  220. * ignored). */
  221. if (outhead.oh_nsect == (NUM_SEGMENTS+1))
  222. {
  223. if (!follows(&outsect[NUM_SEGMENTS], &outsect[BSS]))
  224. fatal("end segment must follow bss");
  225. if ( outsect[NUM_SEGMENTS].os_size != 0 )
  226. fatal("end segment must be empty");
  227. }
  228. /* And go! */
  229. emits(&outsect[TEXT]);
  230. emits(&outsect[ROM]);
  231. emits(&outsect[DATA]);
  232. if (ferror(output))
  233. fatal("output write error");
  234. if (outputfile)
  235. chmod(outputfile, 0755);
  236. /* Summarise what we've done. */
  237. {
  238. long ss = 0;
  239. printf(" base : %08"PRIx32"\n", outsect[TEXT].os_base) ;
  240. printf(" text = %08"PRIx32"\n", outsect[TEXT].os_size);
  241. printf(" rom = %08"PRIx32"\n", outsect[ROM].os_size);
  242. printf(" data = %08"PRIx32"\n", outsect[DATA].os_size);
  243. printf(" bss = %08"PRIx32"\n", outsect[BSS].os_size);
  244. ss += outsect[TEXT].os_size;
  245. ss += outsect[ROM].os_size;
  246. ss += outsect[DATA].os_size;
  247. ss += outsect[BSS].os_size;
  248. printf("TOTAL = %08lX\n", ss);
  249. }
  250. return 0;
  251. }