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