frontend.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. PROG &prog(Project::get()->prog);
  102. int i;
  103. printf("File type is %s\n", (prog.fCOM)?"COM":"EXE");
  104. if (! prog.fCOM) {
  105. printf("Signature = %02X%02X\n", header.sigLo, header.sigHi);
  106. printf("File size %% 512 = %04X\n", LH(&header.lastPageSize));
  107. printf("File size / 512 = %04X pages\n", LH(&header.numPages));
  108. printf("# relocation items = %04X\n", LH(&header.numReloc));
  109. printf("Offset to load image = %04X paras\n", LH(&header.numParaHeader));
  110. printf("Minimum allocation = %04X paras\n", LH(&header.minAlloc));
  111. printf("Maximum allocation = %04X paras\n", LH(&header.maxAlloc));
  112. }
  113. printf("Load image size = %04X\n", prog.cbImage - sizeof(PSP));
  114. printf("Initial SS:SP = %04X:%04X\n", prog.initSS, prog.initSP);
  115. printf("Initial CS:IP = %04X:%04X\n", prog.initCS, prog.initIP);
  116. if (option.VeryVerbose && prog.cReloc)
  117. {
  118. printf("\nRelocation Table\n");
  119. for (i = 0; i < prog.cReloc; i++)
  120. {
  121. printf("%06X -> [%04X]\n", prog.relocTable[i],LH(prog.Image + prog.relocTable[i]));
  122. }
  123. }
  124. printf("\n");
  125. }
  126. /*****************************************************************************
  127. * fill - Fills line for displayMemMap()
  128. ****************************************************************************/
  129. static void fill(int ip, char *bf)
  130. {
  131. PROG &prog(Project::get()->prog);
  132. static uint8_t type[4] = {'.', 'd', 'c', 'x'};
  133. uint8_t i;
  134. for (i = 0; i < 16; i++, ip++)
  135. {
  136. *bf++ = ' ';
  137. *bf++ = (ip < prog.cbImage)? type[(prog.map[ip >> 2] >> ((ip & 3) * 2)) & 3]: ' ';
  138. }
  139. *bf = '\0';
  140. }
  141. /*****************************************************************************
  142. * displayMemMap - Displays the memory bitmap
  143. ****************************************************************************/
  144. static void displayMemMap(void)
  145. {
  146. PROG &prog(Project::get()->prog);
  147. char c, b1[33], b2[33], b3[33];
  148. uint8_t i;
  149. int ip = 0;
  150. printf("\nMemory Map\n");
  151. while (ip < prog.cbImage)
  152. {
  153. fill(ip, b1);
  154. printf("%06X %s\n", ip, b1);
  155. ip += 16;
  156. for (i = 3, c = b1[1]; i < 32 && c == b1[i]; i += 2)
  157. ; /* Check if all same */
  158. if (i > 32)
  159. {
  160. fill(ip, b2); /* Skip until next two are not same */
  161. fill(ip+16, b3);
  162. if (! (strcmp(b1, b2) || strcmp(b1, b3)))
  163. {
  164. printf(" :\n");
  165. do
  166. {
  167. ip += 16;
  168. fill(ip+16, b1);
  169. } while (! strcmp(b1, b2));
  170. }
  171. }
  172. }
  173. printf("\n");
  174. }
  175. /*****************************************************************************
  176. * LoadImage
  177. ****************************************************************************/
  178. void DccFrontend::LoadImage(Project &proj)
  179. {
  180. PROG &prog(Project::get()->prog);
  181. FILE *fp;
  182. int i, cb;
  183. uint8_t buf[4];
  184. /* Open the input file */
  185. if ((fp = fopen(proj.m_fname.c_str(), "rb")) == NULL)
  186. {
  187. fatalError(CANNOT_OPEN, proj.m_fname.c_str());
  188. }
  189. /* Read in first 2 bytes to check EXE signature */
  190. if (fread(&header, 1, 2, fp) != 2)
  191. {
  192. fatalError(CANNOT_READ, proj.m_fname.c_str());
  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, proj.m_fname.c_str());
  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. uint32_t start_of_image= LH(&header.numParaHeader) * 16;
  244. fseek(fp, start_of_image, SEEK_SET);
  245. }
  246. else
  247. { /* COM file
  248. * In this case the load module size is just the file length
  249. */
  250. fseek(fp, 0, SEEK_END);
  251. cb = ftell(fp);
  252. /* COM programs start off with an ORG 100H (to leave room for a PSP)
  253. * This is also the implied start address so if we load the image
  254. * at offset 100H addresses should all line up properly again.
  255. */
  256. prog.initCS = 0;
  257. prog.initIP = 0x100;
  258. prog.initSS = 0;
  259. prog.initSP = 0xFFFE;
  260. prog.cReloc = 0;
  261. fseek(fp, 0, SEEK_SET);
  262. }
  263. /* Allocate a block of memory for the program. */
  264. prog.cbImage = cb + sizeof(PSP);
  265. prog.Image = new uint8_t [prog.cbImage];
  266. prog.Image[0] = 0xCD; /* Fill in PSP int 20h location */
  267. prog.Image[1] = 0x20; /* for termination checking */
  268. /* Read in the image past where a PSP would go */
  269. if (cb != (int)fread(prog.Image + sizeof(PSP), 1, (size_t)cb, fp))
  270. {
  271. fatalError(CANNOT_READ, proj.m_fname.c_str());
  272. }
  273. /* Set up memory map */
  274. cb = (prog.cbImage + 3) / 4;
  275. prog.map = (uint8_t *)malloc(cb);
  276. memset(prog.map, BM_UNKNOWN, (size_t)cb);
  277. /* Relocate segment constants */
  278. if (prog.cReloc)
  279. {
  280. for (i = 0; i < prog.cReloc; i++)
  281. {
  282. uint8_t *p = &prog.Image[prog.relocTable[i]];
  283. uint16_t w = (uint16_t)LH(p) + EXE_RELOCATION;
  284. *p++ = (uint8_t)(w & 0x00FF);
  285. *p = (uint8_t)((w & 0xFF00) >> 8);
  286. }
  287. }
  288. fclose(fp);
  289. }
  290. /*****************************************************************************
  291. * allocMem - malloc with failure test
  292. ****************************************************************************/
  293. void *allocMem(int cb)
  294. {
  295. uint8_t *p;
  296. //printf("Attempt to allocMem %5ld bytes\n", cb);
  297. if (! (p = (uint8_t*)malloc((size_t)cb)))
  298. /* if (! (p = (uint8_t*)calloc((size_t)cb, 1))) */
  299. {
  300. fatalError(MALLOC_FAILED, cb);
  301. }
  302. // printf("allocMem: %p\n", p);
  303. return p;
  304. }