aslod.c 6.8 KB

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