fixwild.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. *$Log: fixwild.c,v $
  3. * Revision 1.10 93/10/28 11:10:10 emmerik
  4. * Addressing mode [reg+nnnn] is now wildcarded
  5. *
  6. * Revision 1.9 93/10/26 13:40:11 cifuente
  7. * op0F(byte pat[])
  8. *
  9. * Revision 1.8 93/10/26 13:01:29 emmerik
  10. * Completed the odd opcodes, like 0F XX and F7. Result: some library
  11. * functions that were not recognised before are recognised now.
  12. *
  13. * Revision 1.7 93/10/11 11:37:01 cifuente
  14. * First walk of HIGH_LEVEL icodes.
  15. *
  16. * Revision 1.6 93/10/01 14:36:21 emmerik
  17. * Added $ log, and made independant of dcc.h
  18. *
  19. *
  20. */
  21. /* * * * * * * * * * * * *\
  22. * *
  23. * Fix Wild Cards Code *
  24. * *
  25. \* * * * * * * * * * * * */
  26. #include "msvc_fixes.h"
  27. #include <memory.h>
  28. #include <stdint.h>
  29. #ifndef PATLEN
  30. #define PATLEN 23
  31. #define WILD 0xF4
  32. #endif
  33. static int pc; /* Indexes into pat[] */
  34. /* prototypes */
  35. static bool ModRM(uint8_t pat[]); /* Handle the mod/rm byte */
  36. static bool TwoWild(uint8_t pat[]); /* Make the next 2 bytes wild */
  37. static bool FourWild(uint8_t pat[]); /* Make the next 4 bytes wild */
  38. void fixWildCards(uint8_t pat[]); /* Main routine */
  39. /* Handle the mod/rm case. Returns true if pattern exhausted */
  40. static bool ModRM(uint8_t pat[])
  41. {
  42. uint8_t op;
  43. /* A standard mod/rm byte follows opcode */
  44. op = pat[pc++]; /* The mod/rm byte */
  45. if (pc >= PATLEN) return true; /* Skip Mod/RM */
  46. switch (op & 0xC0)
  47. {
  48. case 0x00: /* [reg] or [nnnn] */
  49. if ((op & 0xC7) == 6)
  50. {
  51. /* Uses [nnnn] address mode */
  52. pat[pc++] = WILD;
  53. if (pc >= PATLEN) return true;
  54. pat[pc++] = WILD;
  55. if (pc >= PATLEN) return true;
  56. }
  57. break;
  58. case 0x40: /* [reg + nn] */
  59. if ((pc+=1) >= PATLEN) return true;
  60. break;
  61. case 0x80: /* [reg + nnnn] */
  62. /* Possibly just a long constant offset from a register,
  63. but often will be an index from a variable */
  64. pat[pc++] = WILD;
  65. if (pc >= PATLEN) return true;
  66. pat[pc++] = WILD;
  67. if (pc >= PATLEN) return true;
  68. break;
  69. case 0xC0: /* reg */
  70. break;
  71. }
  72. return false;
  73. }
  74. /* Change the next two bytes to wild cards */
  75. static bool TwoWild(uint8_t pat[])
  76. {
  77. pat[pc++] = WILD;
  78. if (pc >= PATLEN) return true; /* Pattern exhausted */
  79. pat[pc++] = WILD;
  80. if (pc >= PATLEN) return true;
  81. return false;
  82. }
  83. /* Change the next four bytes to wild cards */
  84. static bool FourWild(uint8_t pat[])
  85. {
  86. TwoWild(pat);
  87. return TwoWild(pat);
  88. }
  89. /* Chop from the current point by wiping with zeroes. Can't rely on anything
  90. after this point */
  91. static void chop(uint8_t pat[])
  92. {
  93. if (pc >= PATLEN) return; /* Could go negative otherwise */
  94. memset(&pat[pc], 0, PATLEN - pc);
  95. }
  96. static bool op0F(uint8_t pat[])
  97. {
  98. /* The two byte opcodes */
  99. uint8_t op = pat[pc++];
  100. switch (op & 0xF0)
  101. {
  102. case 0x00: /* 00 - 0F */
  103. if (op >= 0x06) /* Clts, Invd, Wbinvd */
  104. return false;
  105. else
  106. {
  107. /* Grp 6, Grp 7, LAR, LSL */
  108. return ModRM(pat);
  109. }
  110. case 0x20: /* Various funnies, all with Mod/RM */
  111. return ModRM(pat);
  112. case 0x80:
  113. pc += 2; /* Word displacement cond jumps */
  114. return false;
  115. case 0x90: /* Byte set on condition */
  116. return ModRM(pat);
  117. case 0xA0:
  118. switch (op)
  119. {
  120. case 0xA0: /* Push FS */
  121. case 0xA1: /* Pop FS */
  122. case 0xA8: /* Push GS */
  123. case 0xA9: /* Pop GS */
  124. return false;
  125. case 0xA3: /* Bt Ev,Gv */
  126. case 0xAB: /* Bts Ev,Gv */
  127. return ModRM(pat);
  128. case 0xA4: /* Shld EvGbIb */
  129. case 0xAC: /* Shrd EvGbIb */
  130. if (ModRM(pat)) return true;
  131. pc++; /* The #num bits to shift */
  132. return false;
  133. case 0xA5: /* Shld EvGb CL */
  134. case 0xAD: /* Shrd EvGb CL */
  135. return ModRM(pat);
  136. default: /* CmpXchg, Imul */
  137. return ModRM(pat);
  138. }
  139. case 0xB0:
  140. if (op == 0xBA)
  141. {
  142. /* Grp 8: bt/bts/btr/btc Ev,#nn */
  143. if (ModRM(pat)) return true;
  144. pc++; /* The #num bits to shift */
  145. return false;
  146. }
  147. return ModRM(pat);
  148. case 0xC0:
  149. if (op <= 0xC1)
  150. {
  151. /* Xadd */
  152. return ModRM(pat);
  153. }
  154. /* Else BSWAP */
  155. return false;
  156. default:
  157. return false; /* Treat as double byte opcodes */
  158. }
  159. }
  160. /* Scan through the instructions in pat[], looking for opcodes that may
  161. have operands that vary with different instances. For example, load and
  162. store from statics, calls to other procs (even relative calls; they may
  163. call procs loaded in a different order, etc).
  164. Note that this procedure is architecture specific, and assumes the
  165. processor is in 16 bit address mode (real mode).
  166. PATLEN bytes are scanned.
  167. */
  168. void fixWildCards(uint8_t pat[])
  169. {
  170. uint8_t op, quad, intArg;
  171. pc=0;
  172. while (pc < PATLEN)
  173. {
  174. op = pat[pc++];
  175. if (pc >= PATLEN) return;
  176. quad = op & 0xC0; /* Quadrant of the opcode map */
  177. if (quad == 0)
  178. {
  179. /* Arithmetic group 00-3F */
  180. if ((op & 0xE7) == 0x26) /* First check for the odds */
  181. {
  182. /* Segment prefix: treat as 1 byte opcode */
  183. continue;
  184. }
  185. if (op == 0x0F) /* 386 2 byte opcodes */
  186. {
  187. if (op0F(pat)) return;
  188. continue;
  189. }
  190. if (op & 0x04)
  191. {
  192. /* All these are constant. Work out the instr length */
  193. if (op & 2)
  194. {
  195. /* Push, pop, other 1 byte opcodes */
  196. continue;
  197. }
  198. else
  199. {
  200. if (op & 1)
  201. {
  202. /* Word immediate operands */
  203. pc += 2;
  204. continue;
  205. }
  206. else
  207. {
  208. /* Byte immediate operands */
  209. pc++;
  210. continue;
  211. }
  212. }
  213. }
  214. else
  215. {
  216. /* All these have mod/rm bytes */
  217. if (ModRM(pat)) return;
  218. continue;
  219. }
  220. }
  221. else if (quad == 0x40)
  222. {
  223. if ((op & 0x60) == 0x40)
  224. {
  225. /* 0x40 - 0x5F -- these are inc, dec, push, pop of general
  226. registers */
  227. continue;
  228. }
  229. else
  230. {
  231. /* 0x60 - 0x70 */
  232. if (op & 0x10)
  233. {
  234. /* 70-7F 2 byte jump opcodes */
  235. pc++;
  236. continue;
  237. }
  238. else
  239. {
  240. /* Odds and sods */
  241. switch (op)
  242. {
  243. case 0x60: /* pusha */
  244. case 0x61: /* popa */
  245. case 0x64: /* overrides */
  246. case 0x65:
  247. case 0x66:
  248. case 0x67:
  249. case 0x6C: /* insb DX */
  250. case 0x6E: /* outsb DX */
  251. continue;
  252. case 0x62: /* bound */
  253. pc += 4;
  254. continue;
  255. case 0x63: /* arpl */
  256. if (TwoWild(pat)) return;
  257. continue;
  258. case 0x68: /* Push byte */
  259. case 0x6A: /* Push byte */
  260. case 0x6D: /* insb port */
  261. case 0x6F: /* outsb port */
  262. /* 2 byte instr, no wilds */
  263. pc++;
  264. continue;
  265. }
  266. }
  267. }
  268. }
  269. else if (quad == 0x80)
  270. {
  271. switch (op & 0xF0)
  272. {
  273. case 0x80: /* 80 - 8F */
  274. /* All have a mod/rm byte */
  275. if (ModRM(pat)) return;
  276. /* These also have immediate values */
  277. switch (op)
  278. {
  279. case 0x80:
  280. case 0x83:
  281. /* One byte immediate */
  282. pc++;
  283. continue;
  284. case 0x81:
  285. /* Immediate 16 bit values might be constant, but
  286. also might be relocatable. Have to make them
  287. wild */
  288. if (TwoWild(pat)) return;
  289. continue;
  290. }
  291. continue;
  292. case 0x90: /* 90 - 9F */
  293. if (op == 0x9A)
  294. {
  295. /* far call */
  296. if (FourWild(pat)) return;
  297. continue;
  298. }
  299. /* All others are 1 byte opcodes */
  300. continue;
  301. case 0xA0: /* A0 - AF */
  302. if ((op & 0x0C) == 0)
  303. {
  304. /* mov al/ax to/from [nnnn] */
  305. if (TwoWild(pat)) return;
  306. continue;
  307. }
  308. else if ((op & 0xFE) == 0xA8)
  309. {
  310. /* test al,#byte or test ax,#word */
  311. if (op & 1) pc += 2;
  312. else pc += 1;
  313. continue;
  314. }
  315. case 0xB0: /* B0 - BF */
  316. {
  317. if (op & 8)
  318. {
  319. /* mov reg, #16 */
  320. /* Immediate 16 bit values might be constant, but also
  321. might be relocatable. For now, make them wild */
  322. if (TwoWild(pat)) return;
  323. }
  324. else
  325. {
  326. /* mov reg, #8 */
  327. pc++;
  328. }
  329. continue;
  330. }
  331. }
  332. }
  333. else
  334. {
  335. /* In the last quadrant of the op code table */
  336. switch (op)
  337. {
  338. case 0xC0: /* 386: Rotate group 2 ModRM, byte, #byte */
  339. case 0xC1: /* 386: Rotate group 2 ModRM, word, #byte */
  340. if (ModRM(pat)) return;
  341. /* Byte immediate value follows ModRM */
  342. pc++;
  343. continue;
  344. case 0xC3: /* Return */
  345. case 0xCB: /* Return far */
  346. chop(pat);
  347. return;
  348. case 0xC2: /* Ret nnnn */
  349. case 0xCA: /* Retf nnnn */
  350. pc += 2;
  351. chop(pat);
  352. return;
  353. case 0xC4: /* les Gv, Mp */
  354. case 0xC5: /* lds Gv, Mp */
  355. if (ModRM(pat)) return;
  356. continue;
  357. case 0xC6: /* Mov ModRM, #nn */
  358. if (ModRM(pat)) return;
  359. /* Byte immediate value follows ModRM */
  360. pc++;
  361. continue;
  362. case 0xC7: /* Mov ModRM, #nnnn */
  363. if (ModRM(pat)) return;
  364. /* Word immediate value follows ModRM */
  365. /* Immediate 16 bit values might be constant, but also
  366. might be relocatable. For now, make them wild */
  367. if (TwoWild(pat)) return;
  368. continue;
  369. case 0xC8: /* Enter Iw, Ib */
  370. pc += 3; /* Constant word, byte */
  371. continue;
  372. case 0xC9: /* Leave */
  373. continue;
  374. case 0xCC: /* Int 3 */
  375. continue;
  376. case 0xCD: /* Int nn */
  377. intArg = pat[pc++];
  378. if ((intArg >= 0x34) and (intArg <= 0x3B))
  379. {
  380. /* Borland/Microsoft FP emulations */
  381. if (ModRM(pat)) return;
  382. }
  383. continue;
  384. case 0xCE: /* Into */
  385. continue;
  386. case 0xCF: /* Iret */
  387. continue;
  388. case 0xD0: /* Group 2 rotate, byte, 1 bit */
  389. case 0xD1: /* Group 2 rotate, word, 1 bit */
  390. case 0xD2: /* Group 2 rotate, byte, CL bits */
  391. case 0xD3: /* Group 2 rotate, word, CL bits */
  392. if (ModRM(pat)) return;
  393. continue;
  394. case 0xD4: /* Aam */
  395. case 0xD5: /* Aad */
  396. case 0xD7: /* Xlat */
  397. continue;
  398. case 0xD8:
  399. case 0xD9:
  400. case 0xDA:
  401. case 0xDB: /* Esc opcodes */
  402. case 0xDC: /* i.e. floating point */
  403. case 0xDD: /* coprocessor calls */
  404. case 0xDE:
  405. case 0xDF:
  406. if (ModRM(pat)) return;
  407. continue;
  408. case 0xE0: /* Loopne */
  409. case 0xE1: /* Loope */
  410. case 0xE2: /* Loop */
  411. case 0xE3: /* Jcxz */
  412. pc++; /* Short jump offset */
  413. continue;
  414. case 0xE4: /* in al,nn */
  415. case 0xE6: /* out nn,al */
  416. pc++;
  417. continue;
  418. case 0xE5: /* in ax,nn */
  419. case 0xE7: /* in nn,ax */
  420. pc += 2;
  421. continue;
  422. case 0xE8: /* Call rel */
  423. if (TwoWild(pat)) return;
  424. continue;
  425. case 0xE9: /* Jump rel, unconditional */
  426. if (TwoWild(pat)) return;
  427. chop(pat);
  428. return;
  429. case 0xEA: /* Jump abs */
  430. if (FourWild(pat)) return;
  431. chop(pat);
  432. return;
  433. case 0xEB: /* Jmp short unconditional */
  434. pc++;
  435. chop(pat);
  436. return;
  437. case 0xEC: /* In al,dx */
  438. case 0xED: /* In ax,dx */
  439. case 0xEE: /* Out dx,al */
  440. case 0xEF: /* Out dx,ax */
  441. continue;
  442. case 0xF0: /* Lock */
  443. case 0xF2: /* Repne */
  444. case 0xF3: /* Rep/repe */
  445. case 0xF4: /* Halt */
  446. case 0xF5: /* Cmc */
  447. case 0xF8: /* Clc */
  448. case 0xF9: /* Stc */
  449. case 0xFA: /* Cli */
  450. case 0xFB: /* Sti */
  451. case 0xFC: /* Cld */
  452. case 0xFD: /* Std */
  453. continue;
  454. case 0xF6: /* Group 3 byte test/not/mul/div */
  455. case 0xF7: /* Group 3 word test/not/mul/div */
  456. case 0xFE: /* Inc/Dec group 4 */
  457. if (ModRM(pat)) return;
  458. continue;
  459. case 0xFF: /* Group 5 Inc/Dec/Call/Jmp/Push */
  460. /* Most are like standard ModRM */
  461. if (ModRM(pat)) return;
  462. continue;
  463. default: /* Rest are single byte opcodes */
  464. continue;
  465. }
  466. }
  467. }
  468. }