fixwild.cpp 17 KB

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