fixwild.cpp 17 KB

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