fixwild.cpp 17 KB

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