fixwild.cpp 17 KB

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