frontend.cpp 11 KB

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