frontend.cpp 11 KB

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