fixwild.cpp 17 KB

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