fixwild.cpp 17 KB

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