frontend.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*****************************************************************************
  2. * dcc project Front End module
  3. * Loads a program into simulated main memory and builds the procedure list.
  4. * (C) Cristina Cifuentes
  5. ****************************************************************************/
  6. #include "dcc.h"
  7. #include "disassem.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <malloc.h> /* For malloc, free, realloc */
  12. typedef struct { /* PSP structure */
  13. uint16_t int20h; /* interrupt 20h */
  14. uint16_t eof; /* segment, end of allocation block */
  15. uint8_t res1; /* reserved */
  16. uint8_t dosDisp[5]; /* far call to DOS function dispatcher */
  17. uint8_t int22h[4]; /* vector for terminate routine */
  18. uint8_t int23h[4]; /* vector for ctrl+break routine */
  19. uint8_t int24h[4]; /* vector for error routine */
  20. uint8_t res2[22]; /* reserved */
  21. uint16_t segEnv; /* segment address of environment block */
  22. uint8_t res3[34]; /* reserved */
  23. uint8_t int21h[6]; /* opcode for int21h and far return */
  24. uint8_t res4[6]; /* reserved */
  25. uint8_t fcb1[16]; /* default file control block 1 */
  26. uint8_t fcb2[16]; /* default file control block 2 */
  27. uint8_t res5[4]; /* reserved */
  28. uint8_t cmdTail[0x80]; /* command tail and disk transfer area */
  29. } PSP;
  30. static struct { /* EXE file header */
  31. uint8_t sigLo; /* .EXE signature: 0x4D 0x5A */
  32. uint8_t sigHi;
  33. uint16_t lastPageSize; /* Size of the last page */
  34. uint16_t numPages; /* Number of pages in the file */
  35. uint16_t numReloc; /* Number of relocation items */
  36. uint16_t numParaHeader; /* # of paragraphs in the header */
  37. uint16_t minAlloc; /* Minimum number of paragraphs */
  38. uint16_t maxAlloc; /* Maximum number of paragraphs */
  39. uint16_t initSS; /* Segment displacement of stack */
  40. uint16_t initSP; /* Contents of SP at entry */
  41. uint16_t checkSum; /* Complemented checksum */
  42. uint16_t initIP; /* Contents of IP at entry */
  43. uint16_t initCS; /* Segment displacement of code */
  44. uint16_t relocTabOffset; /* Relocation table offset */
  45. uint16_t overlayNum; /* Overlay number */
  46. } header;
  47. #define EXE_RELOCATION 0x10 /* EXE images rellocated to above PSP */
  48. static void LoadImage(char *filename);
  49. static void displayLoadInfo(void);
  50. static void displayMemMap(void);
  51. /*****************************************************************************
  52. * FrontEnd - invokes the loader, parser, disassembler (if asm1), icode
  53. * rewritter, and displays any useful information.
  54. ****************************************************************************/
  55. void FrontEnd (char *filename, CALL_GRAPH * *pcallGraph)
  56. {
  57. /* Load program into memory */
  58. LoadImage(filename);
  59. if (option.verbose)
  60. displayLoadInfo();
  61. /* Do depth first flow analysis building call graph and procedure list,
  62. * and attaching the I-code to each procedure */
  63. parse (pcallGraph);
  64. if (option.asm1)
  65. {
  66. printf("dcc: writing assembler file %s\n", asm1_name);
  67. }
  68. /* Search through code looking for impure references and flag them */
  69. Disassembler ds(1);
  70. for(Function &f : pProcList)
  71. {
  72. f.markImpure();
  73. if (option.asm1)
  74. {
  75. ds.disassem(&f);
  76. }
  77. }
  78. if (option.Interact)
  79. {
  80. interactDis(&pProcList.front(), 0); /* Interactive disassembler */
  81. }
  82. /* Converts jump target addresses to icode offsets */
  83. for(Function &f : pProcList)
  84. {
  85. f.bindIcodeOff();
  86. }
  87. /* Print memory bitmap */
  88. if (option.Map)
  89. displayMemMap();
  90. }
  91. /****************************************************************************
  92. * displayLoadInfo - Displays low level loader type info.
  93. ***************************************************************************/
  94. static void displayLoadInfo(void)
  95. {
  96. int i;
  97. printf("File type is %s\n", (prog.fCOM)?"COM":"EXE");
  98. if (! prog.fCOM) {
  99. printf("Signature = %02X%02X\n", header.sigLo, header.sigHi);
  100. printf("File size %% 512 = %04X\n", LH(&header.lastPageSize));
  101. printf("File size / 512 = %04X pages\n", LH(&header.numPages));
  102. printf("# relocation items = %04X\n", LH(&header.numReloc));
  103. printf("Offset to load image = %04X paras\n", LH(&header.numParaHeader));
  104. printf("Minimum allocation = %04X paras\n", LH(&header.minAlloc));
  105. printf("Maximum allocation = %04X paras\n", LH(&header.maxAlloc));
  106. }
  107. printf("Load image size = %04X\n", prog.cbImage - sizeof(PSP));
  108. printf("Initial SS:SP = %04X:%04X\n", prog.initSS, prog.initSP);
  109. printf("Initial CS:IP = %04X:%04X\n", prog.initCS, prog.initIP);
  110. if (option.VeryVerbose && prog.cReloc)
  111. {
  112. printf("\nRelocation Table\n");
  113. for (i = 0; i < prog.cReloc; i++)
  114. {
  115. printf("%06X -> [%04X]\n", prog.relocTable[i],LH(prog.Image + prog.relocTable[i]));
  116. }
  117. }
  118. printf("\n");
  119. }
  120. /*****************************************************************************
  121. * fill - Fills line for displayMemMap()
  122. ****************************************************************************/
  123. static void fill(int ip, char *bf)
  124. {
  125. static uint8_t type[4] = {'.', 'd', 'c', 'x'};
  126. uint8_t i;
  127. for (i = 0; i < 16; i++, ip++)
  128. {
  129. *bf++ = ' ';
  130. *bf++ = (ip < prog.cbImage)?
  131. type[(prog.map[ip >> 2] >> ((ip & 3) * 2)) & 3]: ' ';
  132. }
  133. *bf = '\0';
  134. }
  135. /*****************************************************************************
  136. * displayMemMap - Displays the memory bitmap
  137. ****************************************************************************/
  138. static void displayMemMap(void)
  139. {
  140. char c, b1[33], b2[33], b3[33];
  141. uint8_t i;
  142. int ip = 0;
  143. printf("\nMemory Map\n");
  144. while (ip < prog.cbImage)
  145. {
  146. fill(ip, b1);
  147. printf("%06X %s\n", ip, b1);
  148. ip += 16;
  149. for (i = 3, c = b1[1]; i < 32 && c == b1[i]; i += 2)
  150. ; /* Check if all same */
  151. if (i > 32)
  152. {
  153. fill(ip, b2); /* Skip until next two are not same */
  154. fill(ip+16, b3);
  155. if (! (strcmp(b1, b2) || strcmp(b1, b3)))
  156. {
  157. printf(" :\n");
  158. do
  159. {
  160. ip += 16;
  161. fill(ip+16, b1);
  162. } while (! strcmp(b1, b2));
  163. }
  164. }
  165. }
  166. printf("\n");
  167. }
  168. /*****************************************************************************
  169. * LoadImage
  170. ****************************************************************************/
  171. static void LoadImage(char *filename)
  172. {
  173. FILE *fp;
  174. int i, cb;
  175. uint8_t buf[4];
  176. /* Open the input file */
  177. if ((fp = fopen(filename, "rb")) == NULL)
  178. {
  179. fatalError(CANNOT_OPEN, filename);
  180. }
  181. /* Read in first 2 bytes to check EXE signature */
  182. if (fread(&header, 1, 2, fp) != 2)
  183. {
  184. fatalError(CANNOT_READ, filename);
  185. }
  186. if (! (prog.fCOM = (boolT)(header.sigLo != 0x4D || header.sigHi != 0x5A))) {
  187. /* Read rest of header */
  188. fseek(fp, 0, SEEK_SET);
  189. if (fread(&header, sizeof(header), 1, fp) != 1)
  190. {
  191. fatalError(CANNOT_READ, filename);
  192. }
  193. /* This is a typical DOS kludge! */
  194. if (LH(&header.relocTabOffset) == 0x40)
  195. {
  196. fatalError(NEWEXE_FORMAT);
  197. }
  198. /* Calculate the load module size.
  199. * This is the number of pages in the file
  200. * less the length of the header and reloc table
  201. * less the number of bytes unused on last page
  202. */
  203. cb = (uint32_t)LH(&header.numPages) * 512 - (uint32_t)LH(&header.numParaHeader) * 16;
  204. if (header.lastPageSize)
  205. {
  206. cb -= 512 - LH(&header.lastPageSize);
  207. }
  208. /* We quietly ignore minAlloc and maxAlloc since for our
  209. * purposes it doesn't really matter where in real memory
  210. * the program would end up. EXE programs can't really rely on
  211. * their load location so setting the PSP segment to 0 is fine.
  212. * Certainly programs that prod around in DOS or BIOS are going
  213. * to have to load DS from a constant so it'll be pretty
  214. * obvious.
  215. */
  216. prog.initCS = (int16_t)LH(&header.initCS) + EXE_RELOCATION;
  217. prog.initIP = (int16_t)LH(&header.initIP);
  218. prog.initSS = (int16_t)LH(&header.initSS) + EXE_RELOCATION;
  219. prog.initSP = (int16_t)LH(&header.initSP);
  220. prog.cReloc = (int16_t)LH(&header.numReloc);
  221. /* Allocate the relocation table */
  222. if (prog.cReloc)
  223. {
  224. prog.relocTable = new uint32_t [prog.cReloc];
  225. fseek(fp, LH(&header.relocTabOffset), SEEK_SET);
  226. /* Read in seg:offset pairs and convert to Image ptrs */
  227. for (i = 0; i < prog.cReloc; i++)
  228. {
  229. fread(buf, 1, 4, fp);
  230. prog.relocTable[i] = LH(buf) +
  231. (((int)LH(buf+2) + EXE_RELOCATION)<<4);
  232. }
  233. }
  234. /* Seek to start of image */
  235. fseek(fp, (int)LH(&header.numParaHeader) * 16, SEEK_SET);
  236. }
  237. else
  238. { /* COM file
  239. * In this case the load module size is just the file length
  240. */
  241. fseek(fp, 0, SEEK_END);
  242. cb = ftell(fp);
  243. /* COM programs start off with an ORG 100H (to leave room for a PSP)
  244. * This is also the implied start address so if we load the image
  245. * at offset 100H addresses should all line up properly again.
  246. */
  247. prog.initCS = 0;
  248. prog.initIP = 0x100;
  249. prog.initSS = 0;
  250. prog.initSP = 0xFFFE;
  251. prog.cReloc = 0;
  252. fseek(fp, 0, SEEK_SET);
  253. }
  254. /* Allocate a block of memory for the program. */
  255. prog.cbImage = cb + sizeof(PSP);
  256. prog.Image = new uint8_t [prog.cbImage];
  257. prog.Image[0] = 0xCD; /* Fill in PSP int 20h location */
  258. prog.Image[1] = 0x20; /* for termination checking */
  259. /* Read in the image past where a PSP would go */
  260. #ifdef __DOSWIN__
  261. if (cb > 0xFFFF)
  262. {
  263. printf("Image size of %ld bytes too large for fread!\n", cb);
  264. fatalError(CANNOT_READ, filename);
  265. }
  266. #endif
  267. if (cb != (int)fread(prog.Image + sizeof(PSP), 1, (size_t)cb, fp))
  268. {
  269. fatalError(CANNOT_READ, filename);
  270. }
  271. /* Set up memory map */
  272. cb = (prog.cbImage + 3) / 4;
  273. prog.map = (uint8_t *)malloc(cb);
  274. memset(prog.map, BM_UNKNOWN, (size_t)cb);
  275. /* Relocate segment constants */
  276. if (prog.cReloc)
  277. {
  278. for (i = 0; i < prog.cReloc; i++)
  279. {
  280. uint8_t *p = &prog.Image[prog.relocTable[i]];
  281. uint16_t w = (uint16_t)LH(p) + EXE_RELOCATION;
  282. *p++ = (uint8_t)(w & 0x00FF);
  283. *p = (uint8_t)((w & 0xFF00) >> 8);
  284. }
  285. }
  286. fclose(fp);
  287. }
  288. /*****************************************************************************
  289. * allocMem - malloc with failure test
  290. ****************************************************************************/
  291. void *allocMem(int cb)
  292. {
  293. uint8_t *p;
  294. //printf("Attempt to allocMem %5ld bytes\n", cb);
  295. if (! (p = (uint8_t*)malloc((size_t)cb)))
  296. /* if (! (p = (uint8_t*)calloc((size_t)cb, 1))) */
  297. {
  298. fatalError(MALLOC_FAILED, cb);
  299. }
  300. // printf("allocMem: %p\n", p);
  301. return p;
  302. }