8db.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <mach.h>
  5. /*
  6. * i386-specific debugger interface
  7. * also amd64 extensions
  8. */
  9. static char *i386excep(Map*, Rgetter);
  10. static int i386trace(Map*, uvlong, uvlong, uvlong, Tracer);
  11. static uvlong i386frame(Map*, uvlong, uvlong, uvlong, uvlong);
  12. static int i386foll(Map*, uvlong, Rgetter, uvlong*);
  13. static int i386inst(Map*, uvlong, char, char*, int);
  14. static int i386das(Map*, uvlong, char*, int);
  15. static int i386instlen(Map*, uvlong);
  16. static char STARTSYM[] = "_main";
  17. static char PROFSYM[] = "_mainp";
  18. static char FRAMENAME[] = ".frame";
  19. static char *excname[] =
  20. {
  21. [0] "divide error",
  22. [1] "debug exception",
  23. [4] "overflow",
  24. [5] "bounds check",
  25. [6] "invalid opcode",
  26. [7] "math coprocessor emulation",
  27. [8] "double fault",
  28. [9] "math coprocessor overrun",
  29. [10] "invalid TSS",
  30. [11] "segment not present",
  31. [12] "stack exception",
  32. [13] "general protection violation",
  33. [14] "page fault",
  34. [16] "math coprocessor error",
  35. [17] "alignment check",
  36. [18] "machine check",
  37. [19] "floating-point exception",
  38. [24] "clock",
  39. [25] "keyboard",
  40. [27] "modem status",
  41. [28] "serial line status",
  42. [30] "floppy disk",
  43. [36] "mouse",
  44. [37] "math coprocessor",
  45. [38] "hard disk",
  46. [64] "system call",
  47. };
  48. Machdata i386mach =
  49. {
  50. {0xCC, 0, 0, 0}, /* break point: INT 3 */
  51. 1, /* break point size */
  52. leswab, /* convert short to local byte order */
  53. leswal, /* convert long to local byte order */
  54. leswav, /* convert vlong to local byte order */
  55. i386trace, /* C traceback */
  56. i386frame, /* frame finder */
  57. i386excep, /* print exception */
  58. 0, /* breakpoint fixup */
  59. leieeesftos, /* single precision float printer */
  60. leieeedftos, /* double precision float printer */
  61. i386foll, /* following addresses */
  62. i386inst, /* print instruction */
  63. i386das, /* dissembler */
  64. i386instlen, /* instruction size calculation */
  65. };
  66. static char*
  67. i386excep(Map *map, Rgetter rget)
  68. {
  69. ulong c;
  70. uvlong pc;
  71. static char buf[16];
  72. c = (*rget)(map, "TRAP");
  73. if(c > 64 || excname[c] == 0) {
  74. if (c == 3) {
  75. pc = (*rget)(map, "PC");
  76. if (get1(map, pc, (uchar*)buf, machdata->bpsize) > 0)
  77. if (memcmp(buf, machdata->bpinst, machdata->bpsize) == 0)
  78. return "breakpoint";
  79. }
  80. snprint(buf, sizeof(buf), "exception %ld", c);
  81. return buf;
  82. } else
  83. return excname[c];
  84. }
  85. static int
  86. i386trace(Map *map, uvlong pc, uvlong sp, uvlong link, Tracer trace)
  87. {
  88. int i;
  89. uvlong osp;
  90. Symbol s, f;
  91. USED(link);
  92. i = 0;
  93. osp = 0;
  94. while(findsym(pc, CTEXT, &s)) {
  95. if (osp == sp)
  96. break;
  97. osp = sp;
  98. if(strcmp(STARTSYM, s.name) == 0 || strcmp(PROFSYM, s.name) == 0)
  99. break;
  100. if(pc != s.value) { /* not at first instruction */
  101. if(findlocal(&s, FRAMENAME, &f) == 0)
  102. break;
  103. sp += f.value-mach->szaddr;
  104. }
  105. if (geta(map, sp, &pc) < 0)
  106. break;
  107. if(pc == 0)
  108. break;
  109. (*trace)(map, pc, sp, &s);
  110. sp += mach->szaddr;
  111. if(++i > 1000)
  112. break;
  113. }
  114. return i;
  115. }
  116. static uvlong
  117. i386frame(Map *map, uvlong addr, uvlong pc, uvlong sp, uvlong link)
  118. {
  119. Symbol s, f;
  120. USED(link);
  121. while (findsym(pc, CTEXT, &s)) {
  122. if(strcmp(STARTSYM, s.name) == 0 || strcmp(PROFSYM, s.name) == 0)
  123. break;
  124. if(pc != s.value) { /* not first instruction */
  125. if(findlocal(&s, FRAMENAME, &f) == 0)
  126. break;
  127. sp += f.value-mach->szaddr;
  128. }
  129. if (s.value == addr)
  130. return sp;
  131. if (geta(map, sp, &pc) < 0)
  132. break;
  133. sp += mach->szaddr;
  134. }
  135. return 0;
  136. }
  137. /* I386/486 - Disassembler and related functions */
  138. /*
  139. * an instruction
  140. */
  141. typedef struct Instr Instr;
  142. struct Instr
  143. {
  144. uchar mem[1+1+1+1+2+1+1+4+4]; /* raw instruction */
  145. uvlong addr; /* address of start of instruction */
  146. int n; /* number of bytes in instruction */
  147. char *prefix; /* instr prefix */
  148. char *segment; /* segment override */
  149. uchar jumptype; /* set to the operand type for jump/ret/call */
  150. uchar amd64;
  151. uchar rex; /* REX prefix (or zero) */
  152. char osize; /* 'W' or 'L' (or 'Q' on amd64) */
  153. char asize; /* address size 'W' or 'L' (or 'Q' or amd64) */
  154. uchar mod; /* bits 6-7 of mod r/m field */
  155. uchar reg; /* bits 3-5 of mod r/m field */
  156. char ss; /* bits 6-7 of SIB */
  157. char index; /* bits 3-5 of SIB */
  158. char base; /* bits 0-2 of SIB */
  159. char rip; /* RIP-relative in amd64 mode */
  160. uchar opre; /* f2/f3 could introduce media */
  161. short seg; /* segment of far address */
  162. ulong disp; /* displacement */
  163. ulong imm; /* immediate */
  164. ulong imm2; /* second immediate operand */
  165. uvlong imm64; /* big immediate */
  166. char *curr; /* fill level in output buffer */
  167. char *end; /* end of output buffer */
  168. char *err; /* error message */
  169. };
  170. /* 386 register (ha!) set */
  171. enum{
  172. AX=0,
  173. CX,
  174. DX,
  175. BX,
  176. SP,
  177. BP,
  178. SI,
  179. DI,
  180. /* amd64 */
  181. R8,
  182. R9,
  183. R10,
  184. R11,
  185. R12,
  186. R13,
  187. R14,
  188. R15
  189. };
  190. /* amd64 rex extension byte */
  191. enum{
  192. REXW = 1<<3, /* =1, 64-bit operand size */
  193. REXR = 1<<2, /* extend modrm reg */
  194. REXX = 1<<1, /* extend sib index */
  195. REXB = 1<<0 /* extend modrm r/m, sib base, or opcode reg */
  196. };
  197. /* Operand Format codes */
  198. /*
  199. %A - address size register modifier (!asize -> 'E')
  200. %C - Control register CR0/CR1/CR2
  201. %D - Debug register DR0/DR1/DR2/DR3/DR6/DR7
  202. %I - second immediate operand
  203. %O - Operand size register modifier (!osize -> 'E')
  204. %T - Test register TR6/TR7
  205. %S - size code ('W' or 'L')
  206. %W - Weird opcode: OSIZE == 'W' => "CBW"; else => "CWDE"
  207. %d - displacement 16-32 bits
  208. %e - effective address - Mod R/M value
  209. %f - floating point register F0-F7 - from Mod R/M register
  210. %g - segment register
  211. %i - immediate operand 8-32 bits
  212. %p - PC-relative - signed displacement in immediate field
  213. %r - Reg from Mod R/M
  214. %w - Weird opcode: OSIZE == 'W' => "CWD"; else => "CDQ"
  215. */
  216. typedef struct Optable Optable;
  217. struct Optable
  218. {
  219. char operand[2];
  220. void *proto; /* actually either (char*) or (Optable*) */
  221. };
  222. /* Operand decoding codes */
  223. enum {
  224. Ib = 1, /* 8-bit immediate - (no sign extension)*/
  225. Ibs, /* 8-bit immediate (sign extended) */
  226. Jbs, /* 8-bit sign-extended immediate in jump or call */
  227. Iw, /* 16-bit immediate -> imm */
  228. Iw2, /* 16-bit immediate -> imm2 */
  229. Iwd, /* Operand-sized immediate (no sign extension)*/
  230. Iwdq, /* Operand-sized immediate, possibly 64 bits */
  231. Awd, /* Address offset */
  232. Iwds, /* Operand-sized immediate (sign extended) */
  233. RM, /* Word or long R/M field with register (/r) */
  234. RMB, /* Byte R/M field with register (/r) */
  235. RMOP, /* Word or long R/M field with op code (/digit) */
  236. RMOPB, /* Byte R/M field with op code (/digit) */
  237. RMR, /* R/M register only (mod = 11) */
  238. RMM, /* R/M memory only (mod = 0/1/2) */
  239. R0, /* Base reg of Mod R/M is literal 0x00 */
  240. R1, /* Base reg of Mod R/M is literal 0x01 */
  241. FRMOP, /* Floating point R/M field with opcode */
  242. FRMEX, /* Extended floating point R/M field with opcode */
  243. JUMP, /* Jump or Call flag - no operand */
  244. RET, /* Return flag - no operand */
  245. OA, /* literal 0x0a byte */
  246. PTR, /* Seg:Displacement addr (ptr16:16 or ptr16:32) */
  247. AUX, /* Multi-byte op code - Auxiliary table */
  248. AUXMM, /* multi-byte op code - auxiliary table chosen by prefix */
  249. PRE, /* Instr Prefix */
  250. OPRE, /* Instr Prefix or media op extension */
  251. SEG, /* Segment Prefix */
  252. OPOVER, /* Operand size override */
  253. ADDOVER, /* Address size override */
  254. };
  255. static Optable optab0F00[8]=
  256. {
  257. [0x00] 0,0, "MOVW LDT,%e",
  258. [0x01] 0,0, "MOVW TR,%e",
  259. [0x02] 0,0, "MOVW %e,LDT",
  260. [0x03] 0,0, "MOVW %e,TR",
  261. [0x04] 0,0, "VERR %e",
  262. [0x05] 0,0, "VERW %e",
  263. };
  264. static Optable optab0F01[8]=
  265. {
  266. [0x00] 0,0, "MOVL GDTR,%e",
  267. [0x01] 0,0, "MOVL IDTR,%e",
  268. [0x02] 0,0, "MOVL %e,GDTR",
  269. [0x03] 0,0, "MOVL %e,IDTR",
  270. [0x04] 0,0, "MOVW MSW,%e", /* word */
  271. [0x06] 0,0, "MOVW %e,MSW", /* word */
  272. [0x07] 0,0, "INVLPG %e", /* or SWAPGS */
  273. };
  274. static Optable optab0F01F8[1]=
  275. {
  276. [0x00] 0,0, "SWAPGS",
  277. };
  278. /* 0F71 */
  279. /* 0F72 */
  280. /* 0F73 */
  281. static Optable optab0FAE[8]=
  282. {
  283. [0x00] 0,0, "FXSAVE %e",
  284. [0x01] 0,0, "FXRSTOR %e",
  285. [0x02] 0,0, "LDMXCSR %e",
  286. [0x03] 0,0, "STMXCSR %e",
  287. [0x05] 0,0, "LFENCE",
  288. [0x06] 0,0, "MFENCE",
  289. [0x07] 0,0, "SFENCE",
  290. };
  291. /* 0F18 */
  292. /* 0F0D */
  293. static Optable optab0FBA[8]=
  294. {
  295. [0x04] Ib,0, "BT%S %i,%e",
  296. [0x05] Ib,0, "BTS%S %i,%e",
  297. [0x06] Ib,0, "BTR%S %i,%e",
  298. [0x07] Ib,0, "BTC%S %i,%e",
  299. };
  300. static Optable optab0F0F[256]=
  301. {
  302. [0x0c] 0,0, "PI2FW %m,%M",
  303. [0x0d] 0,0, "PI2L %m,%M",
  304. [0x1c] 0,0, "PF2IW %m,%M",
  305. [0x1d] 0,0, "PF2IL %m,%M",
  306. [0x8a] 0,0, "PFNACC %m,%M",
  307. [0x8e] 0,0, "PFPNACC %m,%M",
  308. [0x90] 0,0, "PFCMPGE %m,%M",
  309. [0x94] 0,0, "PFMIN %m,%M",
  310. [0x96] 0,0, "PFRCP %m,%M",
  311. [0x97] 0,0, "PFRSQRT %m,%M",
  312. [0x9a] 0,0, "PFSUB %m,%M",
  313. [0x9e] 0,0, "PFADD %m,%M",
  314. [0xa0] 0,0, "PFCMPGT %m,%M",
  315. [0xa4] 0,0, "PFMAX %m,%M",
  316. [0xa6] 0,0, "PFRCPIT1 %m,%M",
  317. [0xa7] 0,0, "PFRSQIT1 %m,%M",
  318. [0xaa] 0,0, "PFSUBR %m,%M",
  319. [0xae] 0,0, "PFACC %m,%M",
  320. [0xb0] 0,0, "PFCMPEQ %m,%M",
  321. [0xb4] 0,0, "PFMUL %m,%M",
  322. [0xb6] 0,0, "PFRCPI2T %m,%M",
  323. [0xb7] 0,0, "PMULHRW %m,%M",
  324. [0xbb] 0,0, "PSWAPL %m,%M",
  325. };
  326. static Optable optab0FC7[8]=
  327. {
  328. [0x01] 0,0, "CMPXCHG8B %e",
  329. };
  330. static Optable optab660F71[8]=
  331. {
  332. [0x02] Ib,0, "PSRLW %i,%X",
  333. [0x04] Ib,0, "PSRAW %i,%X",
  334. [0x06] Ib,0, "PSLLW %i,%X",
  335. };
  336. static Optable optab660F72[8]=
  337. {
  338. [0x02] Ib,0, "PSRLL %i,%X",
  339. [0x04] Ib,0, "PSRAL %i,%X",
  340. [0x06] Ib,0, "PSLLL %i,%X",
  341. };
  342. static Optable optab660F73[8]=
  343. {
  344. [0x02] Ib,0, "PSRLQ %i,%X",
  345. [0x03] Ib,0, "PSRLO %i,%X",
  346. [0x06] Ib,0, "PSLLQ %i,%X",
  347. [0x07] Ib,0, "PSLLO %i,%X",
  348. };
  349. static Optable optab660F[256]=
  350. {
  351. [0x2B] RM,0, "MOVNTPD %x,%e",
  352. [0x2E] RM,0, "UCOMISD %x,%X",
  353. [0x2F] RM,0, "COMISD %x,%X",
  354. [0x5A] RM,0, "CVTPD2PS %x,%X",
  355. [0x5B] RM,0, "CVTPS2PL %x,%X",
  356. [0x6A] RM,0, "PUNPCKHLQ %x,%X",
  357. [0x6B] RM,0, "PACKSSLW %x,%X",
  358. [0x6C] RM,0, "PUNPCKLQDQ %x,%X",
  359. [0x6D] RM,0, "PUNPCKHQDQ %x,%X",
  360. [0x6E] RM,0, "MOV%S %e,%X",
  361. [0x6F] RM,0, "MOVO %x,%X", /* MOVDQA */
  362. [0x70] RM,Ib, "PSHUFL %i,%x,%X",
  363. [0x71] RMOP,0, optab660F71,
  364. [0x72] RMOP,0, optab660F72,
  365. [0x73] RMOP,0, optab660F73,
  366. [0x7E] RM,0, "MOV%S %X,%e",
  367. [0x7F] RM,0, "MOVO %X,%x",
  368. [0xC4] RM,Ib, "PINSRW %i,%e,%X",
  369. [0xC5] RMR,Ib, "PEXTRW %i,%X,%e",
  370. [0xD4] RM,0, "PADDQ %x,%X",
  371. [0xD5] RM,0, "PMULLW %x,%X",
  372. [0xD6] RM,0, "MOVQ %X,%x",
  373. [0xE6] RM,0, "CVTTPD2PL %x,%X",
  374. [0xE7] RM,0, "MOVNTO %X,%e",
  375. [0xF7] RM,0, "MASKMOVOU %x,%X",
  376. };
  377. static Optable optabF20F[256]=
  378. {
  379. [0x10] RM,0, "MOVSD %x,%X",
  380. [0x11] RM,0, "MOVSD %X,%x",
  381. [0x2A] RM,0, "CVTS%S2SD %e,%X",
  382. [0x2C] RM,0, "CVTTSD2S%S %x,%r",
  383. [0x2D] RM,0, "CVTSD2S%S %x,%r",
  384. [0x5A] RM,0, "CVTSD2SS %x,%X",
  385. [0x6F] RM,0, "MOVOU %x,%X",
  386. [0x70] RM,Ib, "PSHUFLW %i,%x,%X",
  387. [0x7F] RM,0, "MOVOU %X,%x",
  388. [0xD6] RM,0, "MOVQOZX %M,%X",
  389. [0xE6] RM,0, "CVTPD2PL %x,%X",
  390. };
  391. static Optable optabF30F[256]=
  392. {
  393. [0x10] RM,0, "MOVSS %x,%X",
  394. [0x11] RM,0, "MOVSS %X,%x",
  395. [0x2A] RM,0, "CVTS%S2SS %e,%X",
  396. [0x2C] RM,0, "CVTTSS2S%S %x,%r",
  397. [0x2D] RM,0, "CVTSS2S%S %x,%r",
  398. [0x5A] RM,0, "CVTSS2SD %x,%X",
  399. [0x5B] RM,0, "CVTTPS2PL %x,%X",
  400. [0x6F] RM,0, "MOVOU %x,%X",
  401. [0x70] RM,Ib, "PSHUFHW %i,%x,%X",
  402. [0x7E] RM,0, "MOVQOZX %x,%X",
  403. [0x7F] RM,0, "MOVOU %X,%x",
  404. [0xD6] RM,0, "MOVQOZX %m*,%X",
  405. [0xE6] RM,0, "CVTPL2PD %x,%X",
  406. };
  407. static Optable optab0F[256]=
  408. {
  409. [0x00] RMOP,0, optab0F00,
  410. [0x01] RMOP,0, optab0F01,
  411. [0x02] RM,0, "LAR %e,%r",
  412. [0x03] RM,0, "LSL %e,%r",
  413. [0x05] 0,0, "SYSCALL",
  414. [0x06] 0,0, "CLTS",
  415. [0x07] 0,0, "SYSRET",
  416. [0x08] 0,0, "INVD",
  417. [0x09] 0,0, "WBINVD",
  418. [0x0B] 0,0, "UD2",
  419. [0x0F] RM,AUX, optab0F0F, /* 3DNow! */
  420. [0x10] RM,0, "MOVU%s %x,%X",
  421. [0x11] RM,0, "MOVU%s %X,%x",
  422. [0x12] RM,0, "MOV[H]L%s %x,%X", /* TO DO: H if source is XMM */
  423. [0x13] RM,0, "MOVL%s %X,%e",
  424. [0x14] RM,0, "UNPCKL%s %x,%X",
  425. [0x15] RM,0, "UNPCKH%s %x,%X",
  426. [0x16] RM,0, "MOV[L]H%s %x,%X", /* TO DO: L if source is XMM */
  427. [0x17] RM,0, "MOVH%s %X,%x",
  428. [0x20] RMR,0, "MOVL %C,%e",
  429. [0x21] RMR,0, "MOVL %D,%e",
  430. [0x22] RMR,0, "MOVL %e,%C",
  431. [0x23] RMR,0, "MOVL %e,%D",
  432. [0x24] RMR,0, "MOVL %T,%e",
  433. [0x26] RMR,0, "MOVL %e,%T",
  434. [0x28] RM,0, "MOVA%s %x,%X",
  435. [0x29] RM,0, "MOVA%s %X,%x",
  436. [0x2A] RM,0, "CVTPL2%s %m*,%X",
  437. [0x2B] RM,0, "MOVNT%s %X,%e",
  438. [0x2C] RM,0, "CVTT%s2PL %x,%M",
  439. [0x2D] RM,0, "CVT%s2PL %x,%M",
  440. [0x2E] RM,0, "UCOMISS %x,%X",
  441. [0x2F] RM,0, "COMISS %x,%X",
  442. [0x30] 0,0, "WRMSR",
  443. [0x31] 0,0, "RDTSC",
  444. [0x32] 0,0, "RDMSR",
  445. [0x33] 0,0, "RDPMC",
  446. [0x42] RM,0, "CMOVC %e,%r", /* CF */
  447. [0x43] RM,0, "CMOVNC %e,%r", /* ¬ CF */
  448. [0x44] RM,0, "CMOVZ %e,%r", /* ZF */
  449. [0x45] RM,0, "CMOVNZ %e,%r", /* ¬ ZF */
  450. [0x46] RM,0, "CMOVBE %e,%r", /* CF ∨ ZF */
  451. [0x47] RM,0, "CMOVA %e,%r", /* ¬CF ∧ ¬ZF */
  452. [0x48] RM,0, "CMOVS %e,%r", /* SF */
  453. [0x49] RM,0, "CMOVNS %e,%r", /* ¬ SF */
  454. [0x4A] RM,0, "CMOVP %e,%r", /* PF */
  455. [0x4B] RM,0, "CMOVNP %e,%r", /* ¬ PF */
  456. [0x4C] RM,0, "CMOVLT %e,%r", /* LT ≡ OF ≠ SF */
  457. [0x4D] RM,0, "CMOVGE %e,%r", /* GE ≡ ZF ∨ SF */
  458. [0x4E] RM,0, "CMOVLE %e,%r", /* LE ≡ ZF ∨ LT */
  459. [0x4F] RM,0, "CMOVGT %e,%r", /* GT ≡ ¬ZF ∧ GE */
  460. [0x50] RM,0, "MOVMSK%s %X,%r", /* TO DO: check */
  461. [0x51] RM,0, "SQRT%s %x,%X",
  462. [0x52] RM,0, "RSQRT%s %x,%X",
  463. [0x53] RM,0, "RCP%s %x,%X",
  464. [0x54] RM,0, "AND%s %x,%X",
  465. [0x55] RM,0, "ANDN%s %x,%X",
  466. [0x56] RM,0, "OR%s %x,%X", /* TO DO: S/D */
  467. [0x57] RM,0, "XOR%s %x,%X", /* S/D */
  468. [0x58] RM,0, "ADD%s %x,%X", /* S/P S/D */
  469. [0x59] RM,0, "MUL%s %x,%X",
  470. [0x5A] RM,0, "CVTPS2PD %x,%X",
  471. [0x5B] RM,0, "CVTPL2PS %x,%X",
  472. [0x5C] RM,0, "SUB%s %x,%X",
  473. [0x5D] RM,0, "MIN%s %x,%X",
  474. [0x5E] RM,0, "DIV%s %x,%X", /* TO DO: S/P S/D */
  475. [0x5F] RM,0, "MAX%s %x,%X",
  476. [0x60] RM,0, "PUNPCKLBW %m,%M",
  477. [0x61] RM,0, "PUNPCKLWL %m,%M",
  478. [0x62] RM,0, "PUNPCKLLQ %m,%M",
  479. [0x63] RM,0, "PACKSSWB %m,%M",
  480. [0x64] RM,0, "PCMPGTB %m,%M",
  481. [0x65] RM,0, "PCMPGTW %m,%M",
  482. [0x66] RM,0, "PCMPGTL %m,%M",
  483. [0x67] RM,0, "PACKUSWB %m,%M",
  484. [0x68] RM,0, "PUNPCKHBW %m,%M",
  485. [0x69] RM,0, "PUNPCKHWL %m,%M",
  486. [0x6A] RM,0, "PUNPCKHLQ %m,%M",
  487. [0x6B] RM,0, "PACKSSLW %m,%M",
  488. [0x6E] RM,0, "MOV%S %e,%M",
  489. [0x6F] RM,0, "MOVQ %m,%M",
  490. [0x70] RM,Ib, "PSHUFW %i,%m,%M",
  491. [0x74] RM,0, "PCMPEQB %m,%M",
  492. [0x75] RM,0, "PCMPEQW %m,%M",
  493. [0x76] RM,0, "PCMPEQL %m,%M",
  494. [0x7E] RM,0, "MOV%S %M,%e",
  495. [0x7F] RM,0, "MOVQ %M,%m",
  496. [0xAE] RMOP,0, optab0FAE,
  497. [0xAA] 0,0, "RSM",
  498. [0xB0] RM,0, "CMPXCHGB %r,%e",
  499. [0xB1] RM,0, "CMPXCHG%S %r,%e",
  500. [0xC0] RMB,0, "XADDB %r,%e",
  501. [0xC1] RM,0, "XADD%S %r,%e",
  502. [0xC2] RM,Ib, "CMP%s %i,%x,%X",
  503. [0xC3] RM,0, "MOVNTI%S %r,%e",
  504. [0xC6] RM,Ib, "SHUF%s %i,%x,%X",
  505. [0xC8] 0,0, "BSWAP AX",
  506. [0xC9] 0,0, "BSWAP CX",
  507. [0xCA] 0,0, "BSWAP DX",
  508. [0xCB] 0,0, "BSWAP BX",
  509. [0xCC] 0,0, "BSWAP SP",
  510. [0xCD] 0,0, "BSWAP BP",
  511. [0xCE] 0,0, "BSWAP SI",
  512. [0xCF] 0,0, "BSWAP DI",
  513. [0xD1] RM,0, "PSRLW %m,%M",
  514. [0xD2] RM,0, "PSRLL %m,%M",
  515. [0xD3] RM,0, "PSRLQ %m,%M",
  516. [0xD5] RM,0, "PMULLW %m,%M",
  517. [0xD6] RM,0, "MOVQOZX %m*,%X",
  518. [0xD7] RM,0, "PMOVMSKB %m,%r",
  519. [0xD8] RM,0, "PSUBUSB %m,%M",
  520. [0xD9] RM,0, "PSUBUSW %m,%M",
  521. [0xDA] RM,0, "PMINUB %m,%M",
  522. [0xDB] RM,0, "PAND %m,%M",
  523. [0xDC] RM,0, "PADDUSB %m,%M",
  524. [0xDD] RM,0, "PADDUSW %m,%M",
  525. [0xDE] RM,0, "PMAXUB %m,%M",
  526. [0xDF] RM,0, "PANDN %m,%M",
  527. [0xE0] RM,0, "PAVGB %m,%M",
  528. [0xE1] RM,0, "PSRAW %m,%M",
  529. [0xE2] RM,0, "PSRAL %m,%M",
  530. [0xE3] RM,0, "PAVGW %m,%M",
  531. [0xE4] RM,0, "PMULHUW %m,%M",
  532. [0xE5] RM,0, "PMULHW %m,%M",
  533. [0xE7] RM,0, "MOVNTQ %M,%e",
  534. [0xE8] RM,0, "PSUBSB %m,%M",
  535. [0xE9] RM,0, "PSUBSW %m,%M",
  536. [0xEA] RM,0, "PMINSW %m,%M",
  537. [0xEB] RM,0, "POR %m,%M",
  538. [0xEC] RM,0, "PADDSB %m,%M",
  539. [0xED] RM,0, "PADDSW %m,%M",
  540. [0xEE] RM,0, "PMAXSW %m,%M",
  541. [0xEF] RM,0, "PXOR %m,%M",
  542. [0xF1] RM,0, "PSLLW %m,%M",
  543. [0xF2] RM,0, "PSLLL %m,%M",
  544. [0xF3] RM,0, "PSLLQ %m,%M",
  545. [0xF4] RM,0, "PMULULQ %m,%M",
  546. [0xF5] RM,0, "PMADDWL %m,%M",
  547. [0xF6] RM,0, "PSADBW %m,%M",
  548. [0xF7] RMR,0, "MASKMOVQ %m,%M",
  549. [0xF8] RM,0, "PSUBB %m,%M",
  550. [0xF9] RM,0, "PSUBW %m,%M",
  551. [0xFA] RM,0, "PSUBL %m,%M",
  552. [0xFC] RM,0, "PADDB %m,%M",
  553. [0xFD] RM,0, "PADDW %m,%M",
  554. [0xFE] RM,0, "PADDL %m,%M",
  555. [0x80] Iwds,0, "JOS %p",
  556. [0x81] Iwds,0, "JOC %p",
  557. [0x82] Iwds,0, "JCS %p",
  558. [0x83] Iwds,0, "JCC %p",
  559. [0x84] Iwds,0, "JEQ %p",
  560. [0x85] Iwds,0, "JNE %p",
  561. [0x86] Iwds,0, "JLS %p",
  562. [0x87] Iwds,0, "JHI %p",
  563. [0x88] Iwds,0, "JMI %p",
  564. [0x89] Iwds,0, "JPL %p",
  565. [0x8a] Iwds,0, "JPS %p",
  566. [0x8b] Iwds,0, "JPC %p",
  567. [0x8c] Iwds,0, "JLT %p",
  568. [0x8d] Iwds,0, "JGE %p",
  569. [0x8e] Iwds,0, "JLE %p",
  570. [0x8f] Iwds,0, "JGT %p",
  571. [0x90] RMB,0, "SETOS %e",
  572. [0x91] RMB,0, "SETOC %e",
  573. [0x92] RMB,0, "SETCS %e",
  574. [0x93] RMB,0, "SETCC %e",
  575. [0x94] RMB,0, "SETEQ %e",
  576. [0x95] RMB,0, "SETNE %e",
  577. [0x96] RMB,0, "SETLS %e",
  578. [0x97] RMB,0, "SETHI %e",
  579. [0x98] RMB,0, "SETMI %e",
  580. [0x99] RMB,0, "SETPL %e",
  581. [0x9a] RMB,0, "SETPS %e",
  582. [0x9b] RMB,0, "SETPC %e",
  583. [0x9c] RMB,0, "SETLT %e",
  584. [0x9d] RMB,0, "SETGE %e",
  585. [0x9e] RMB,0, "SETLE %e",
  586. [0x9f] RMB,0, "SETGT %e",
  587. [0xa0] 0,0, "PUSHL FS",
  588. [0xa1] 0,0, "POPL FS",
  589. [0xa2] 0,0, "CPUID",
  590. [0xa3] RM,0, "BT%S %r,%e",
  591. [0xa4] RM,Ib, "SHLD%S %r,%i,%e",
  592. [0xa5] RM,0, "SHLD%S %r,CL,%e",
  593. [0xa8] 0,0, "PUSHL GS",
  594. [0xa9] 0,0, "POPL GS",
  595. [0xab] RM,0, "BTS%S %r,%e",
  596. [0xac] RM,Ib, "SHRD%S %r,%i,%e",
  597. [0xad] RM,0, "SHRD%S %r,CL,%e",
  598. [0xaf] RM,0, "IMUL%S %e,%r",
  599. [0xb2] RMM,0, "LSS %e,%r",
  600. [0xb3] RM,0, "BTR%S %r,%e",
  601. [0xb4] RMM,0, "LFS %e,%r",
  602. [0xb5] RMM,0, "LGS %e,%r",
  603. [0xb6] RMB,0, "MOVBZX %e,%R",
  604. [0xb7] RM,0, "MOVWZX %e,%R",
  605. [0xba] RMOP,0, optab0FBA,
  606. [0xbb] RM,0, "BTC%S %e,%r",
  607. [0xbc] RM,0, "BSF%S %e,%r",
  608. [0xbd] RM,0, "BSR%S %e,%r",
  609. [0xbe] RMB,0, "MOVBSX %e,%R",
  610. [0xbf] RM,0, "MOVWSX %e,%R",
  611. [0xc7] RMOP,0, optab0FC7,
  612. };
  613. static Optable optab80[8]=
  614. {
  615. [0x00] Ib,0, "ADDB %i,%e",
  616. [0x01] Ib,0, "ORB %i,%e",
  617. [0x02] Ib,0, "ADCB %i,%e",
  618. [0x03] Ib,0, "SBBB %i,%e",
  619. [0x04] Ib,0, "ANDB %i,%e",
  620. [0x05] Ib,0, "SUBB %i,%e",
  621. [0x06] Ib,0, "XORB %i,%e",
  622. [0x07] Ib,0, "CMPB %e,%i",
  623. };
  624. static Optable optab81[8]=
  625. {
  626. [0x00] Iwd,0, "ADD%S %i,%e",
  627. [0x01] Iwd,0, "OR%S %i,%e",
  628. [0x02] Iwd,0, "ADC%S %i,%e",
  629. [0x03] Iwd,0, "SBB%S %i,%e",
  630. [0x04] Iwd,0, "AND%S %i,%e",
  631. [0x05] Iwd,0, "SUB%S %i,%e",
  632. [0x06] Iwd,0, "XOR%S %i,%e",
  633. [0x07] Iwd,0, "CMP%S %e,%i",
  634. };
  635. static Optable optab83[8]=
  636. {
  637. [0x00] Ibs,0, "ADD%S %i,%e",
  638. [0x01] Ibs,0, "OR%S %i,%e",
  639. [0x02] Ibs,0, "ADC%S %i,%e",
  640. [0x03] Ibs,0, "SBB%S %i,%e",
  641. [0x04] Ibs,0, "AND%S %i,%e",
  642. [0x05] Ibs,0, "SUB%S %i,%e",
  643. [0x06] Ibs,0, "XOR%S %i,%e",
  644. [0x07] Ibs,0, "CMP%S %e,%i",
  645. };
  646. static Optable optabC0[8] =
  647. {
  648. [0x00] Ib,0, "ROLB %i,%e",
  649. [0x01] Ib,0, "RORB %i,%e",
  650. [0x02] Ib,0, "RCLB %i,%e",
  651. [0x03] Ib,0, "RCRB %i,%e",
  652. [0x04] Ib,0, "SHLB %i,%e",
  653. [0x05] Ib,0, "SHRB %i,%e",
  654. [0x07] Ib,0, "SARB %i,%e",
  655. };
  656. static Optable optabC1[8] =
  657. {
  658. [0x00] Ib,0, "ROL%S %i,%e",
  659. [0x01] Ib,0, "ROR%S %i,%e",
  660. [0x02] Ib,0, "RCL%S %i,%e",
  661. [0x03] Ib,0, "RCR%S %i,%e",
  662. [0x04] Ib,0, "SHL%S %i,%e",
  663. [0x05] Ib,0, "SHR%S %i,%e",
  664. [0x07] Ib,0, "SAR%S %i,%e",
  665. };
  666. static Optable optabD0[8] =
  667. {
  668. [0x00] 0,0, "ROLB %e",
  669. [0x01] 0,0, "RORB %e",
  670. [0x02] 0,0, "RCLB %e",
  671. [0x03] 0,0, "RCRB %e",
  672. [0x04] 0,0, "SHLB %e",
  673. [0x05] 0,0, "SHRB %e",
  674. [0x07] 0,0, "SARB %e",
  675. };
  676. static Optable optabD1[8] =
  677. {
  678. [0x00] 0,0, "ROL%S %e",
  679. [0x01] 0,0, "ROR%S %e",
  680. [0x02] 0,0, "RCL%S %e",
  681. [0x03] 0,0, "RCR%S %e",
  682. [0x04] 0,0, "SHL%S %e",
  683. [0x05] 0,0, "SHR%S %e",
  684. [0x07] 0,0, "SAR%S %e",
  685. };
  686. static Optable optabD2[8] =
  687. {
  688. [0x00] 0,0, "ROLB CL,%e",
  689. [0x01] 0,0, "RORB CL,%e",
  690. [0x02] 0,0, "RCLB CL,%e",
  691. [0x03] 0,0, "RCRB CL,%e",
  692. [0x04] 0,0, "SHLB CL,%e",
  693. [0x05] 0,0, "SHRB CL,%e",
  694. [0x07] 0,0, "SARB CL,%e",
  695. };
  696. static Optable optabD3[8] =
  697. {
  698. [0x00] 0,0, "ROL%S CL,%e",
  699. [0x01] 0,0, "ROR%S CL,%e",
  700. [0x02] 0,0, "RCL%S CL,%e",
  701. [0x03] 0,0, "RCR%S CL,%e",
  702. [0x04] 0,0, "SHL%S CL,%e",
  703. [0x05] 0,0, "SHR%S CL,%e",
  704. [0x07] 0,0, "SAR%S CL,%e",
  705. };
  706. static Optable optabD8[8+8] =
  707. {
  708. [0x00] 0,0, "FADDF %e,F0",
  709. [0x01] 0,0, "FMULF %e,F0",
  710. [0x02] 0,0, "FCOMF %e,F0",
  711. [0x03] 0,0, "FCOMFP %e,F0",
  712. [0x04] 0,0, "FSUBF %e,F0",
  713. [0x05] 0,0, "FSUBRF %e,F0",
  714. [0x06] 0,0, "FDIVF %e,F0",
  715. [0x07] 0,0, "FDIVRF %e,F0",
  716. [0x08] 0,0, "FADDD %f,F0",
  717. [0x09] 0,0, "FMULD %f,F0",
  718. [0x0a] 0,0, "FCOMD %f,F0",
  719. [0x0b] 0,0, "FCOMPD %f,F0",
  720. [0x0c] 0,0, "FSUBD %f,F0",
  721. [0x0d] 0,0, "FSUBRD %f,F0",
  722. [0x0e] 0,0, "FDIVD %f,F0",
  723. [0x0f] 0,0, "FDIVRD %f,F0",
  724. };
  725. /*
  726. * optabD9 and optabDB use the following encoding:
  727. * if (0 <= modrm <= 2) instruction = optabDx[modrm&0x07];
  728. * else instruction = optabDx[(modrm&0x3f)+8];
  729. *
  730. * the instructions for MOD == 3, follow the 8 instructions
  731. * for the other MOD values stored at the front of the table.
  732. */
  733. static Optable optabD9[64+8] =
  734. {
  735. [0x00] 0,0, "FMOVF %e,F0",
  736. [0x02] 0,0, "FMOVF F0,%e",
  737. [0x03] 0,0, "FMOVFP F0,%e",
  738. [0x04] 0,0, "FLDENV%S %e",
  739. [0x05] 0,0, "FLDCW %e",
  740. [0x06] 0,0, "FSTENV%S %e",
  741. [0x07] 0,0, "FSTCW %e",
  742. [0x08] 0,0, "FMOVD F0,F0", /* Mod R/M = 11xx xxxx*/
  743. [0x09] 0,0, "FMOVD F1,F0",
  744. [0x0a] 0,0, "FMOVD F2,F0",
  745. [0x0b] 0,0, "FMOVD F3,F0",
  746. [0x0c] 0,0, "FMOVD F4,F0",
  747. [0x0d] 0,0, "FMOVD F5,F0",
  748. [0x0e] 0,0, "FMOVD F6,F0",
  749. [0x0f] 0,0, "FMOVD F7,F0",
  750. [0x10] 0,0, "FXCHD F0,F0",
  751. [0x11] 0,0, "FXCHD F1,F0",
  752. [0x12] 0,0, "FXCHD F2,F0",
  753. [0x13] 0,0, "FXCHD F3,F0",
  754. [0x14] 0,0, "FXCHD F4,F0",
  755. [0x15] 0,0, "FXCHD F5,F0",
  756. [0x16] 0,0, "FXCHD F6,F0",
  757. [0x17] 0,0, "FXCHD F7,F0",
  758. [0x18] 0,0, "FNOP",
  759. [0x28] 0,0, "FCHS",
  760. [0x29] 0,0, "FABS",
  761. [0x2c] 0,0, "FTST",
  762. [0x2d] 0,0, "FXAM",
  763. [0x30] 0,0, "FLD1",
  764. [0x31] 0,0, "FLDL2T",
  765. [0x32] 0,0, "FLDL2E",
  766. [0x33] 0,0, "FLDPI",
  767. [0x34] 0,0, "FLDLG2",
  768. [0x35] 0,0, "FLDLN2",
  769. [0x36] 0,0, "FLDZ",
  770. [0x38] 0,0, "F2XM1",
  771. [0x39] 0,0, "FYL2X",
  772. [0x3a] 0,0, "FPTAN",
  773. [0x3b] 0,0, "FPATAN",
  774. [0x3c] 0,0, "FXTRACT",
  775. [0x3d] 0,0, "FPREM1",
  776. [0x3e] 0,0, "FDECSTP",
  777. [0x3f] 0,0, "FNCSTP",
  778. [0x40] 0,0, "FPREM",
  779. [0x41] 0,0, "FYL2XP1",
  780. [0x42] 0,0, "FSQRT",
  781. [0x43] 0,0, "FSINCOS",
  782. [0x44] 0,0, "FRNDINT",
  783. [0x45] 0,0, "FSCALE",
  784. [0x46] 0,0, "FSIN",
  785. [0x47] 0,0, "FCOS",
  786. };
  787. static Optable optabDA[8+8] =
  788. {
  789. [0x00] 0,0, "FADDL %e,F0",
  790. [0x01] 0,0, "FMULL %e,F0",
  791. [0x02] 0,0, "FCOML %e,F0",
  792. [0x03] 0,0, "FCOMLP %e,F0",
  793. [0x04] 0,0, "FSUBL %e,F0",
  794. [0x05] 0,0, "FSUBRL %e,F0",
  795. [0x06] 0,0, "FDIVL %e,F0",
  796. [0x07] 0,0, "FDIVRL %e,F0",
  797. [0x0d] R1,0, "FUCOMPP",
  798. };
  799. static Optable optabDB[8+64] =
  800. {
  801. [0x00] 0,0, "FMOVL %e,F0",
  802. [0x02] 0,0, "FMOVL F0,%e",
  803. [0x03] 0,0, "FMOVLP F0,%e",
  804. [0x05] 0,0, "FMOVX %e,F0",
  805. [0x07] 0,0, "FMOVXP F0,%e",
  806. [0x2a] 0,0, "FCLEX",
  807. [0x2b] 0,0, "FINIT",
  808. };
  809. static Optable optabDC[8+8] =
  810. {
  811. [0x00] 0,0, "FADDD %e,F0",
  812. [0x01] 0,0, "FMULD %e,F0",
  813. [0x02] 0,0, "FCOMD %e,F0",
  814. [0x03] 0,0, "FCOMDP %e,F0",
  815. [0x04] 0,0, "FSUBD %e,F0",
  816. [0x05] 0,0, "FSUBRD %e,F0",
  817. [0x06] 0,0, "FDIVD %e,F0",
  818. [0x07] 0,0, "FDIVRD %e,F0",
  819. [0x08] 0,0, "FADDD F0,%f",
  820. [0x09] 0,0, "FMULD F0,%f",
  821. [0x0c] 0,0, "FSUBRD F0,%f",
  822. [0x0d] 0,0, "FSUBD F0,%f",
  823. [0x0e] 0,0, "FDIVRD F0,%f",
  824. [0x0f] 0,0, "FDIVD F0,%f",
  825. };
  826. static Optable optabDD[8+8] =
  827. {
  828. [0x00] 0,0, "FMOVD %e,F0",
  829. [0x02] 0,0, "FMOVD F0,%e",
  830. [0x03] 0,0, "FMOVDP F0,%e",
  831. [0x04] 0,0, "FRSTOR%S %e",
  832. [0x06] 0,0, "FSAVE%S %e",
  833. [0x07] 0,0, "FSTSW %e",
  834. [0x08] 0,0, "FFREED %f",
  835. [0x0a] 0,0, "FMOVD %f,F0",
  836. [0x0b] 0,0, "FMOVDP %f,F0",
  837. [0x0c] 0,0, "FUCOMD %f,F0",
  838. [0x0d] 0,0, "FUCOMDP %f,F0",
  839. };
  840. static Optable optabDE[8+8] =
  841. {
  842. [0x00] 0,0, "FADDW %e,F0",
  843. [0x01] 0,0, "FMULW %e,F0",
  844. [0x02] 0,0, "FCOMW %e,F0",
  845. [0x03] 0,0, "FCOMWP %e,F0",
  846. [0x04] 0,0, "FSUBW %e,F0",
  847. [0x05] 0,0, "FSUBRW %e,F0",
  848. [0x06] 0,0, "FDIVW %e,F0",
  849. [0x07] 0,0, "FDIVRW %e,F0",
  850. [0x08] 0,0, "FADDDP F0,%f",
  851. [0x09] 0,0, "FMULDP F0,%f",
  852. [0x0b] R1,0, "FCOMPDP",
  853. [0x0c] 0,0, "FSUBRDP F0,%f",
  854. [0x0d] 0,0, "FSUBDP F0,%f",
  855. [0x0e] 0,0, "FDIVRDP F0,%f",
  856. [0x0f] 0,0, "FDIVDP F0,%f",
  857. };
  858. static Optable optabDF[8+8] =
  859. {
  860. [0x00] 0,0, "FMOVW %e,F0",
  861. [0x02] 0,0, "FMOVW F0,%e",
  862. [0x03] 0,0, "FMOVWP F0,%e",
  863. [0x04] 0,0, "FBLD %e",
  864. [0x05] 0,0, "FMOVL %e,F0",
  865. [0x06] 0,0, "FBSTP %e",
  866. [0x07] 0,0, "FMOVLP F0,%e",
  867. [0x0c] R0,0, "FSTSW %OAX",
  868. };
  869. static Optable optabF6[8] =
  870. {
  871. [0x00] Ib,0, "TESTB %i,%e",
  872. [0x02] 0,0, "NOTB %e",
  873. [0x03] 0,0, "NEGB %e",
  874. [0x04] 0,0, "MULB AL,%e",
  875. [0x05] 0,0, "IMULB AL,%e",
  876. [0x06] 0,0, "DIVB AL,%e",
  877. [0x07] 0,0, "IDIVB AL,%e",
  878. };
  879. static Optable optabF7[8] =
  880. {
  881. [0x00] Iwd,0, "TEST%S %i,%e",
  882. [0x02] 0,0, "NOT%S %e",
  883. [0x03] 0,0, "NEG%S %e",
  884. [0x04] 0,0, "MUL%S %OAX,%e",
  885. [0x05] 0,0, "IMUL%S %OAX,%e",
  886. [0x06] 0,0, "DIV%S %OAX,%e",
  887. [0x07] 0,0, "IDIV%S %OAX,%e",
  888. };
  889. static Optable optabFE[8] =
  890. {
  891. [0x00] 0,0, "INCB %e",
  892. [0x01] 0,0, "DECB %e",
  893. };
  894. static Optable optabFF[8] =
  895. {
  896. [0x00] 0,0, "INC%S %e",
  897. [0x01] 0,0, "DEC%S %e",
  898. [0x02] JUMP,0, "CALL* %e",
  899. [0x03] JUMP,0, "CALLF* %e",
  900. [0x04] JUMP,0, "JMP* %e",
  901. [0x05] JUMP,0, "JMPF* %e",
  902. [0x06] 0,0, "PUSHL %e",
  903. };
  904. static Optable optable[256+1] =
  905. {
  906. [0x00] RMB,0, "ADDB %r,%e",
  907. [0x01] RM,0, "ADD%S %r,%e",
  908. [0x02] RMB,0, "ADDB %e,%r",
  909. [0x03] RM,0, "ADD%S %e,%r",
  910. [0x04] Ib,0, "ADDB %i,AL",
  911. [0x05] Iwd,0, "ADD%S %i,%OAX",
  912. [0x06] 0,0, "PUSHL ES",
  913. [0x07] 0,0, "POPL ES",
  914. [0x08] RMB,0, "ORB %r,%e",
  915. [0x09] RM,0, "OR%S %r,%e",
  916. [0x0a] RMB,0, "ORB %e,%r",
  917. [0x0b] RM,0, "OR%S %e,%r",
  918. [0x0c] Ib,0, "ORB %i,AL",
  919. [0x0d] Iwd,0, "OR%S %i,%OAX",
  920. [0x0e] 0,0, "PUSHL CS",
  921. [0x0f] AUXMM,0, optab0F,
  922. [0x10] RMB,0, "ADCB %r,%e",
  923. [0x11] RM,0, "ADC%S %r,%e",
  924. [0x12] RMB,0, "ADCB %e,%r",
  925. [0x13] RM,0, "ADC%S %e,%r",
  926. [0x14] Ib,0, "ADCB %i,AL",
  927. [0x15] Iwd,0, "ADC%S %i,%OAX",
  928. [0x16] 0,0, "PUSHL SS",
  929. [0x17] 0,0, "POPL SS",
  930. [0x18] RMB,0, "SBBB %r,%e",
  931. [0x19] RM,0, "SBB%S %r,%e",
  932. [0x1a] RMB,0, "SBBB %e,%r",
  933. [0x1b] RM,0, "SBB%S %e,%r",
  934. [0x1c] Ib,0, "SBBB %i,AL",
  935. [0x1d] Iwd,0, "SBB%S %i,%OAX",
  936. [0x1e] 0,0, "PUSHL DS",
  937. [0x1f] 0,0, "POPL DS",
  938. [0x20] RMB,0, "ANDB %r,%e",
  939. [0x21] RM,0, "AND%S %r,%e",
  940. [0x22] RMB,0, "ANDB %e,%r",
  941. [0x23] RM,0, "AND%S %e,%r",
  942. [0x24] Ib,0, "ANDB %i,AL",
  943. [0x25] Iwd,0, "AND%S %i,%OAX",
  944. [0x26] SEG,0, "ES:",
  945. [0x27] 0,0, "DAA",
  946. [0x28] RMB,0, "SUBB %r,%e",
  947. [0x29] RM,0, "SUB%S %r,%e",
  948. [0x2a] RMB,0, "SUBB %e,%r",
  949. [0x2b] RM,0, "SUB%S %e,%r",
  950. [0x2c] Ib,0, "SUBB %i,AL",
  951. [0x2d] Iwd,0, "SUB%S %i,%OAX",
  952. [0x2e] SEG,0, "CS:",
  953. [0x2f] 0,0, "DAS",
  954. [0x30] RMB,0, "XORB %r,%e",
  955. [0x31] RM,0, "XOR%S %r,%e",
  956. [0x32] RMB,0, "XORB %e,%r",
  957. [0x33] RM,0, "XOR%S %e,%r",
  958. [0x34] Ib,0, "XORB %i,AL",
  959. [0x35] Iwd,0, "XOR%S %i,%OAX",
  960. [0x36] SEG,0, "SS:",
  961. [0x37] 0,0, "AAA",
  962. [0x38] RMB,0, "CMPB %r,%e",
  963. [0x39] RM,0, "CMP%S %r,%e",
  964. [0x3a] RMB,0, "CMPB %e,%r",
  965. [0x3b] RM,0, "CMP%S %e,%r",
  966. [0x3c] Ib,0, "CMPB %i,AL",
  967. [0x3d] Iwd,0, "CMP%S %i,%OAX",
  968. [0x3e] SEG,0, "DS:",
  969. [0x3f] 0,0, "AAS",
  970. [0x40] 0,0, "INC%S %OAX",
  971. [0x41] 0,0, "INC%S %OCX",
  972. [0x42] 0,0, "INC%S %ODX",
  973. [0x43] 0,0, "INC%S %OBX",
  974. [0x44] 0,0, "INC%S %OSP",
  975. [0x45] 0,0, "INC%S %OBP",
  976. [0x46] 0,0, "INC%S %OSI",
  977. [0x47] 0,0, "INC%S %ODI",
  978. [0x48] 0,0, "DEC%S %OAX",
  979. [0x49] 0,0, "DEC%S %OCX",
  980. [0x4a] 0,0, "DEC%S %ODX",
  981. [0x4b] 0,0, "DEC%S %OBX",
  982. [0x4c] 0,0, "DEC%S %OSP",
  983. [0x4d] 0,0, "DEC%S %OBP",
  984. [0x4e] 0,0, "DEC%S %OSI",
  985. [0x4f] 0,0, "DEC%S %ODI",
  986. [0x50] 0,0, "PUSH%S %OAX",
  987. [0x51] 0,0, "PUSH%S %OCX",
  988. [0x52] 0,0, "PUSH%S %ODX",
  989. [0x53] 0,0, "PUSH%S %OBX",
  990. [0x54] 0,0, "PUSH%S %OSP",
  991. [0x55] 0,0, "PUSH%S %OBP",
  992. [0x56] 0,0, "PUSH%S %OSI",
  993. [0x57] 0,0, "PUSH%S %ODI",
  994. [0x58] 0,0, "POP%S %OAX",
  995. [0x59] 0,0, "POP%S %OCX",
  996. [0x5a] 0,0, "POP%S %ODX",
  997. [0x5b] 0,0, "POP%S %OBX",
  998. [0x5c] 0,0, "POP%S %OSP",
  999. [0x5d] 0,0, "POP%S %OBP",
  1000. [0x5e] 0,0, "POP%S %OSI",
  1001. [0x5f] 0,0, "POP%S %ODI",
  1002. [0x60] 0,0, "PUSHA%S",
  1003. [0x61] 0,0, "POPA%S",
  1004. [0x62] RMM,0, "BOUND %e,%r",
  1005. [0x63] RM,0, "ARPL %r,%e",
  1006. [0x64] SEG,0, "FS:",
  1007. [0x65] SEG,0, "GS:",
  1008. [0x66] OPOVER,0, "",
  1009. [0x67] ADDOVER,0, "",
  1010. [0x68] Iwd,0, "PUSH%S %i",
  1011. [0x69] RM,Iwd, "IMUL%S %e,%i,%r",
  1012. [0x6a] Ib,0, "PUSH%S %i",
  1013. [0x6b] RM,Ibs, "IMUL%S %e,%i,%r",
  1014. [0x6c] 0,0, "INSB DX,(%ODI)",
  1015. [0x6d] 0,0, "INS%S DX,(%ODI)",
  1016. [0x6e] 0,0, "OUTSB (%ASI),DX",
  1017. [0x6f] 0,0, "OUTS%S (%ASI),DX",
  1018. [0x70] Jbs,0, "JOS %p",
  1019. [0x71] Jbs,0, "JOC %p",
  1020. [0x72] Jbs,0, "JCS %p",
  1021. [0x73] Jbs,0, "JCC %p",
  1022. [0x74] Jbs,0, "JEQ %p",
  1023. [0x75] Jbs,0, "JNE %p",
  1024. [0x76] Jbs,0, "JLS %p",
  1025. [0x77] Jbs,0, "JHI %p",
  1026. [0x78] Jbs,0, "JMI %p",
  1027. [0x79] Jbs,0, "JPL %p",
  1028. [0x7a] Jbs,0, "JPS %p",
  1029. [0x7b] Jbs,0, "JPC %p",
  1030. [0x7c] Jbs,0, "JLT %p",
  1031. [0x7d] Jbs,0, "JGE %p",
  1032. [0x7e] Jbs,0, "JLE %p",
  1033. [0x7f] Jbs,0, "JGT %p",
  1034. [0x80] RMOPB,0, optab80,
  1035. [0x81] RMOP,0, optab81,
  1036. [0x83] RMOP,0, optab83,
  1037. [0x84] RMB,0, "TESTB %r,%e",
  1038. [0x85] RM,0, "TEST%S %r,%e",
  1039. [0x86] RMB,0, "XCHGB %r,%e",
  1040. [0x87] RM,0, "XCHG%S %r,%e",
  1041. [0x88] RMB,0, "MOVB %r,%e",
  1042. [0x89] RM,0, "MOV%S %r,%e",
  1043. [0x8a] RMB,0, "MOVB %e,%r",
  1044. [0x8b] RM,0, "MOV%S %e,%r",
  1045. [0x8c] RM,0, "MOVW %g,%e",
  1046. [0x8d] RM,0, "LEA%S %e,%r",
  1047. [0x8e] RM,0, "MOVW %e,%g",
  1048. [0x8f] RM,0, "POP%S %e",
  1049. [0x90] 0,0, "NOP",
  1050. [0x91] 0,0, "XCHG %OCX,%OAX",
  1051. [0x92] 0,0, "XCHG %ODX,%OAX",
  1052. [0x93] 0,0, "XCHG %OBX,%OAX",
  1053. [0x94] 0,0, "XCHG %OSP,%OAX",
  1054. [0x95] 0,0, "XCHG %OBP,%OAX",
  1055. [0x96] 0,0, "XCHG %OSI,%OAX",
  1056. [0x97] 0,0, "XCHG %ODI,%OAX",
  1057. [0x98] 0,0, "%W", /* miserable CBW or CWDE */
  1058. [0x99] 0,0, "%w", /* idiotic CWD or CDQ */
  1059. [0x9a] PTR,0, "CALL%S %d",
  1060. [0x9b] 0,0, "WAIT",
  1061. [0x9c] 0,0, "PUSHF",
  1062. [0x9d] 0,0, "POPF",
  1063. [0x9e] 0,0, "SAHF",
  1064. [0x9f] 0,0, "LAHF",
  1065. [0xa0] Awd,0, "MOVB %i,AL",
  1066. [0xa1] Awd,0, "MOV%S %i,%OAX",
  1067. [0xa2] Awd,0, "MOVB AL,%i",
  1068. [0xa3] Awd,0, "MOV%S %OAX,%i",
  1069. [0xa4] 0,0, "MOVSB (%ASI),(%ADI)",
  1070. [0xa5] 0,0, "MOVS%S (%ASI),(%ADI)",
  1071. [0xa6] 0,0, "CMPSB (%ASI),(%ADI)",
  1072. [0xa7] 0,0, "CMPS%S (%ASI),(%ADI)",
  1073. [0xa8] Ib,0, "TESTB %i,AL",
  1074. [0xa9] Iwd,0, "TEST%S %i,%OAX",
  1075. [0xaa] 0,0, "STOSB AL,(%ADI)",
  1076. [0xab] 0,0, "STOS%S %OAX,(%ADI)",
  1077. [0xac] 0,0, "LODSB (%ASI),AL",
  1078. [0xad] 0,0, "LODS%S (%ASI),%OAX",
  1079. [0xae] 0,0, "SCASB (%ADI),AL",
  1080. [0xaf] 0,0, "SCAS%S (%ADI),%OAX",
  1081. [0xb0] Ib,0, "MOVB %i,AL",
  1082. [0xb1] Ib,0, "MOVB %i,CL",
  1083. [0xb2] Ib,0, "MOVB %i,DL",
  1084. [0xb3] Ib,0, "MOVB %i,BL",
  1085. [0xb4] Ib,0, "MOVB %i,AH",
  1086. [0xb5] Ib,0, "MOVB %i,CH",
  1087. [0xb6] Ib,0, "MOVB %i,DH",
  1088. [0xb7] Ib,0, "MOVB %i,BH",
  1089. [0xb8] Iwdq,0, "MOV%S %i,%OAX",
  1090. [0xb9] Iwdq,0, "MOV%S %i,%OCX",
  1091. [0xba] Iwdq,0, "MOV%S %i,%ODX",
  1092. [0xbb] Iwdq,0, "MOV%S %i,%OBX",
  1093. [0xbc] Iwdq,0, "MOV%S %i,%OSP",
  1094. [0xbd] Iwdq,0, "MOV%S %i,%OBP",
  1095. [0xbe] Iwdq,0, "MOV%S %i,%OSI",
  1096. [0xbf] Iwdq,0, "MOV%S %i,%ODI",
  1097. [0xc0] RMOPB,0, optabC0,
  1098. [0xc1] RMOP,0, optabC1,
  1099. [0xc2] Iw,0, "RET %i",
  1100. [0xc3] RET,0, "RET",
  1101. [0xc4] RM,0, "LES %e,%r",
  1102. [0xc5] RM,0, "LDS %e,%r",
  1103. [0xc6] RMB,Ib, "MOVB %i,%e",
  1104. [0xc7] RM,Iwd, "MOV%S %i,%e",
  1105. [0xc8] Iw2,Ib, "ENTER %i,%I", /* loony ENTER */
  1106. [0xc9] RET,0, "LEAVE", /* bizarre LEAVE */
  1107. [0xca] Iw,0, "RETF %i",
  1108. [0xcb] RET,0, "RETF",
  1109. [0xcc] 0,0, "INT 3",
  1110. [0xcd] Ib,0, "INTB %i",
  1111. [0xce] 0,0, "INTO",
  1112. [0xcf] 0,0, "IRET",
  1113. [0xd0] RMOPB,0, optabD0,
  1114. [0xd1] RMOP,0, optabD1,
  1115. [0xd2] RMOPB,0, optabD2,
  1116. [0xd3] RMOP,0, optabD3,
  1117. [0xd4] OA,0, "AAM",
  1118. [0xd5] OA,0, "AAD",
  1119. [0xd7] 0,0, "XLAT",
  1120. [0xd8] FRMOP,0, optabD8,
  1121. [0xd9] FRMEX,0, optabD9,
  1122. [0xda] FRMOP,0, optabDA,
  1123. [0xdb] FRMEX,0, optabDB,
  1124. [0xdc] FRMOP,0, optabDC,
  1125. [0xdd] FRMOP,0, optabDD,
  1126. [0xde] FRMOP,0, optabDE,
  1127. [0xdf] FRMOP,0, optabDF,
  1128. [0xe0] Jbs,0, "LOOPNE %p",
  1129. [0xe1] Jbs,0, "LOOPE %p",
  1130. [0xe2] Jbs,0, "LOOP %p",
  1131. [0xe3] Jbs,0, "JCXZ %p",
  1132. [0xe4] Ib,0, "INB %i,AL",
  1133. [0xe5] Ib,0, "IN%S %i,%OAX",
  1134. [0xe6] Ib,0, "OUTB AL,%i",
  1135. [0xe7] Ib,0, "OUT%S %OAX,%i",
  1136. [0xe8] Iwds,0, "CALL %p",
  1137. [0xe9] Iwds,0, "JMP %p",
  1138. [0xea] PTR,0, "JMP %d",
  1139. [0xeb] Jbs,0, "JMP %p",
  1140. [0xec] 0,0, "INB DX,AL",
  1141. [0xed] 0,0, "IN%S DX,%OAX",
  1142. [0xee] 0,0, "OUTB AL,DX",
  1143. [0xef] 0,0, "OUT%S %OAX,DX",
  1144. [0xf0] PRE,0, "LOCK",
  1145. [0xf2] OPRE,0, "REPNE",
  1146. [0xf3] OPRE,0, "REP",
  1147. [0xf4] 0,0, "HALT",
  1148. [0xf5] 0,0, "CMC",
  1149. [0xf6] RMOPB,0, optabF6,
  1150. [0xf7] RMOP,0, optabF7,
  1151. [0xf8] 0,0, "CLC",
  1152. [0xf9] 0,0, "STC",
  1153. [0xfa] 0,0, "CLI",
  1154. [0xfb] 0,0, "STI",
  1155. [0xfc] 0,0, "CLD",
  1156. [0xfd] 0,0, "STD",
  1157. [0xfe] RMOPB,0, optabFE,
  1158. [0xff] RMOP,0, optabFF,
  1159. [0x100] RM,0, "MOVLQSX %r,%e",
  1160. };
  1161. /*
  1162. * get a byte of the instruction
  1163. */
  1164. static int
  1165. igetc(Map *map, Instr *ip, uchar *c)
  1166. {
  1167. if(ip->n+1 > sizeof(ip->mem)){
  1168. werrstr("instruction too long");
  1169. return -1;
  1170. }
  1171. if (get1(map, ip->addr+ip->n, c, 1) < 0) {
  1172. werrstr("can't read instruction: %r");
  1173. return -1;
  1174. }
  1175. ip->mem[ip->n++] = *c;
  1176. return 1;
  1177. }
  1178. /*
  1179. * get two bytes of the instruction
  1180. */
  1181. static int
  1182. igets(Map *map, Instr *ip, ushort *sp)
  1183. {
  1184. uchar c;
  1185. ushort s;
  1186. if (igetc(map, ip, &c) < 0)
  1187. return -1;
  1188. s = c;
  1189. if (igetc(map, ip, &c) < 0)
  1190. return -1;
  1191. s |= (c<<8);
  1192. *sp = s;
  1193. return 1;
  1194. }
  1195. /*
  1196. * get 4 bytes of the instruction
  1197. */
  1198. static int
  1199. igetl(Map *map, Instr *ip, ulong *lp)
  1200. {
  1201. ushort s;
  1202. long l;
  1203. if (igets(map, ip, &s) < 0)
  1204. return -1;
  1205. l = s;
  1206. if (igets(map, ip, &s) < 0)
  1207. return -1;
  1208. l |= (s<<16);
  1209. *lp = l;
  1210. return 1;
  1211. }
  1212. /*
  1213. * get 8 bytes of the instruction
  1214. */
  1215. static int
  1216. igetq(Map *map, Instr *ip, vlong *qp)
  1217. {
  1218. ulong l;
  1219. uvlong q;
  1220. if (igetl(map, ip, &l) < 0)
  1221. return -1;
  1222. q = l;
  1223. if (igetl(map, ip, &l) < 0)
  1224. return -1;
  1225. q |= ((uvlong)l<<32);
  1226. *qp = q;
  1227. return 1;
  1228. }
  1229. static int
  1230. getdisp(Map *map, Instr *ip, int mod, int rm, int code, int pcrel)
  1231. {
  1232. uchar c;
  1233. ushort s;
  1234. if (mod > 2)
  1235. return 1;
  1236. if (mod == 1) {
  1237. if (igetc(map, ip, &c) < 0)
  1238. return -1;
  1239. if (c&0x80)
  1240. ip->disp = c|0xffffff00;
  1241. else
  1242. ip->disp = c&0xff;
  1243. } else if (mod == 2 || rm == code) {
  1244. if (ip->asize == 'E') {
  1245. if (igetl(map, ip, &ip->disp) < 0)
  1246. return -1;
  1247. if (mod == 0)
  1248. ip->rip = pcrel;
  1249. } else {
  1250. if (igets(map, ip, &s) < 0)
  1251. return -1;
  1252. if (s&0x8000)
  1253. ip->disp = s|0xffff0000;
  1254. else
  1255. ip->disp = s;
  1256. }
  1257. if (mod == 0)
  1258. ip->base = -1;
  1259. }
  1260. return 1;
  1261. }
  1262. static int
  1263. modrm(Map *map, Instr *ip, uchar c)
  1264. {
  1265. uchar rm, mod;
  1266. mod = (c>>6)&3;
  1267. rm = c&7;
  1268. ip->mod = mod;
  1269. ip->base = rm;
  1270. ip->reg = (c>>3)&7;
  1271. ip->rip = 0;
  1272. if (mod == 3) /* register */
  1273. return 1;
  1274. if (ip->asize == 0) { /* 16-bit mode */
  1275. switch(rm) {
  1276. case 0:
  1277. ip->base = BX; ip->index = SI;
  1278. break;
  1279. case 1:
  1280. ip->base = BX; ip->index = DI;
  1281. break;
  1282. case 2:
  1283. ip->base = BP; ip->index = SI;
  1284. break;
  1285. case 3:
  1286. ip->base = BP; ip->index = DI;
  1287. break;
  1288. case 4:
  1289. ip->base = SI;
  1290. break;
  1291. case 5:
  1292. ip->base = DI;
  1293. break;
  1294. case 6:
  1295. ip->base = BP;
  1296. break;
  1297. case 7:
  1298. ip->base = BX;
  1299. break;
  1300. default:
  1301. break;
  1302. }
  1303. return getdisp(map, ip, mod, rm, 6, 0);
  1304. }
  1305. if (rm == 4) { /* scummy sib byte */
  1306. if (igetc(map, ip, &c) < 0)
  1307. return -1;
  1308. ip->ss = (c>>6)&0x03;
  1309. ip->index = (c>>3)&0x07;
  1310. if (ip->index == 4)
  1311. ip->index = -1;
  1312. ip->base = c&0x07;
  1313. return getdisp(map, ip, mod, ip->base, 5, 0);
  1314. }
  1315. return getdisp(map, ip, mod, rm, 5, ip->amd64);
  1316. }
  1317. static Optable *
  1318. mkinstr(Map *map, Instr *ip, uvlong pc)
  1319. {
  1320. int i, n, norex;
  1321. uchar c;
  1322. ushort s;
  1323. Optable *op, *obase;
  1324. char buf[128];
  1325. memset(ip, 0, sizeof(*ip));
  1326. norex = 1;
  1327. ip->base = -1;
  1328. ip->index = -1;
  1329. if(asstype == AI8086)
  1330. ip->osize = 'W';
  1331. else {
  1332. ip->osize = 'L';
  1333. ip->asize = 'E';
  1334. ip->amd64 = asstype != AI386;
  1335. norex = 0;
  1336. }
  1337. ip->addr = pc;
  1338. if (igetc(map, ip, &c) < 0)
  1339. return 0;
  1340. obase = optable;
  1341. newop:
  1342. if(ip->amd64 && !norex){
  1343. if(c >= 0x40 && c <= 0x4f) {
  1344. ip->rex = c;
  1345. if(igetc(map, ip, &c) < 0)
  1346. return 0;
  1347. }
  1348. if(c == 0x63){
  1349. op = &obase[0x100]; /* MOVLQSX */
  1350. goto hack;
  1351. }
  1352. }
  1353. op = &obase[c];
  1354. hack:
  1355. if (op->proto == 0) {
  1356. badop:
  1357. n = snprint(buf, sizeof(buf), "opcode: ??");
  1358. for (i = 0; i < ip->n && n < sizeof(buf)-3; i++, n+=2)
  1359. _hexify(buf+n, ip->mem[i], 1);
  1360. strcpy(buf+n, "??");
  1361. werrstr(buf);
  1362. return 0;
  1363. }
  1364. for(i = 0; i < 2 && op->operand[i]; i++) {
  1365. switch(op->operand[i]) {
  1366. case Ib: /* 8-bit immediate - (no sign extension)*/
  1367. if (igetc(map, ip, &c) < 0)
  1368. return 0;
  1369. ip->imm = c&0xff;
  1370. ip->imm64 = ip->imm;
  1371. break;
  1372. case Jbs: /* 8-bit jump immediate (sign extended) */
  1373. if (igetc(map, ip, &c) < 0)
  1374. return 0;
  1375. if (c&0x80)
  1376. ip->imm = c|0xffffff00;
  1377. else
  1378. ip->imm = c&0xff;
  1379. ip->imm64 = (long)ip->imm;
  1380. ip->jumptype = Jbs;
  1381. break;
  1382. case Ibs: /* 8-bit immediate (sign extended) */
  1383. if (igetc(map, ip, &c) < 0)
  1384. return 0;
  1385. if (c&0x80)
  1386. if (ip->osize == 'L')
  1387. ip->imm = c|0xffffff00;
  1388. else
  1389. ip->imm = c|0xff00;
  1390. else
  1391. ip->imm = c&0xff;
  1392. ip->imm64 = (long)ip->imm;
  1393. break;
  1394. case Iw: /* 16-bit immediate -> imm */
  1395. if (igets(map, ip, &s) < 0)
  1396. return 0;
  1397. ip->imm = s&0xffff;
  1398. ip->imm64 = ip->imm;
  1399. ip->jumptype = Iw;
  1400. break;
  1401. case Iw2: /* 16-bit immediate -> in imm2*/
  1402. if (igets(map, ip, &s) < 0)
  1403. return 0;
  1404. ip->imm2 = s&0xffff;
  1405. break;
  1406. case Iwd: /* Operand-sized immediate (no sign extension unless 64 bits)*/
  1407. if (ip->osize == 'L') {
  1408. if (igetl(map, ip, &ip->imm) < 0)
  1409. return 0;
  1410. ip->imm64 = ip->imm;
  1411. if(ip->rex&REXW && (ip->imm & (1<<31)) != 0)
  1412. ip->imm64 |= (vlong)~0 << 32;
  1413. } else {
  1414. if (igets(map, ip, &s)< 0)
  1415. return 0;
  1416. ip->imm = s&0xffff;
  1417. ip->imm64 = ip->imm;
  1418. }
  1419. break;
  1420. case Iwdq: /* Operand-sized immediate, possibly big */
  1421. if (ip->osize == 'L') {
  1422. if (igetl(map, ip, &ip->imm) < 0)
  1423. return 0;
  1424. ip->imm64 = ip->imm;
  1425. if (ip->rex & REXW) {
  1426. ulong l;
  1427. if (igetl(map, ip, &l) < 0)
  1428. return 0;
  1429. ip->imm64 |= (uvlong)l << 32;
  1430. }
  1431. } else {
  1432. if (igets(map, ip, &s)< 0)
  1433. return 0;
  1434. ip->imm = s&0xffff;
  1435. }
  1436. break;
  1437. case Awd: /* Address-sized immediate (no sign extension)*/
  1438. if (ip->asize == 'E') {
  1439. if (igetl(map, ip, &ip->imm) < 0)
  1440. return 0;
  1441. /* TO DO: REX */
  1442. } else {
  1443. if (igets(map, ip, &s)< 0)
  1444. return 0;
  1445. ip->imm = s&0xffff;
  1446. }
  1447. break;
  1448. case Iwds: /* Operand-sized immediate (sign extended) */
  1449. if (ip->osize == 'L') {
  1450. if (igetl(map, ip, &ip->imm) < 0)
  1451. return 0;
  1452. } else {
  1453. if (igets(map, ip, &s)< 0)
  1454. return 0;
  1455. if (s&0x8000)
  1456. ip->imm = s|0xffff0000;
  1457. else
  1458. ip->imm = s&0xffff;
  1459. }
  1460. ip->jumptype = Iwds;
  1461. break;
  1462. case OA: /* literal 0x0a byte */
  1463. if (igetc(map, ip, &c) < 0)
  1464. return 0;
  1465. if (c != 0x0a)
  1466. goto badop;
  1467. break;
  1468. case R0: /* base register must be R0 */
  1469. if (ip->base != 0)
  1470. goto badop;
  1471. break;
  1472. case R1: /* base register must be R1 */
  1473. if (ip->base != 1)
  1474. goto badop;
  1475. break;
  1476. case RMB: /* R/M field with byte register (/r)*/
  1477. if (igetc(map, ip, &c) < 0)
  1478. return 0;
  1479. if (modrm(map, ip, c) < 0)
  1480. return 0;
  1481. ip->osize = 'B';
  1482. break;
  1483. case RM: /* R/M field with register (/r) */
  1484. if (igetc(map, ip, &c) < 0)
  1485. return 0;
  1486. if (modrm(map, ip, c) < 0)
  1487. return 0;
  1488. break;
  1489. case RMOPB: /* R/M field with op code (/digit) */
  1490. if (igetc(map, ip, &c) < 0)
  1491. return 0;
  1492. if (modrm(map, ip, c) < 0)
  1493. return 0;
  1494. c = ip->reg; /* secondary op code */
  1495. obase = (Optable*)op->proto;
  1496. ip->osize = 'B';
  1497. goto newop;
  1498. case RMOP: /* R/M field with op code (/digit) */
  1499. if (igetc(map, ip, &c) < 0)
  1500. return 0;
  1501. if (modrm(map, ip, c) < 0)
  1502. return 0;
  1503. obase = (Optable*)op->proto;
  1504. if(ip->amd64 && obase == optab0F01 && c == 0xF8)
  1505. return optab0F01F8;
  1506. c = ip->reg;
  1507. goto newop;
  1508. case FRMOP: /* FP R/M field with op code (/digit) */
  1509. if (igetc(map, ip, &c) < 0)
  1510. return 0;
  1511. if (modrm(map, ip, c) < 0)
  1512. return 0;
  1513. if ((c&0xc0) == 0xc0)
  1514. c = ip->reg+8; /* 16 entry table */
  1515. else
  1516. c = ip->reg;
  1517. obase = (Optable*)op->proto;
  1518. goto newop;
  1519. case FRMEX: /* Extended FP R/M field with op code (/digit) */
  1520. if (igetc(map, ip, &c) < 0)
  1521. return 0;
  1522. if (modrm(map, ip, c) < 0)
  1523. return 0;
  1524. if ((c&0xc0) == 0xc0)
  1525. c = (c&0x3f)+8; /* 64-entry table */
  1526. else
  1527. c = ip->reg;
  1528. obase = (Optable*)op->proto;
  1529. goto newop;
  1530. case RMR: /* R/M register only (mod = 11) */
  1531. if (igetc(map, ip, &c) < 0)
  1532. return 0;
  1533. if ((c&0xc0) != 0xc0) {
  1534. werrstr("invalid R/M register: %x", c);
  1535. return 0;
  1536. }
  1537. if (modrm(map, ip, c) < 0)
  1538. return 0;
  1539. break;
  1540. case RMM: /* R/M register only (mod = 11) */
  1541. if (igetc(map, ip, &c) < 0)
  1542. return 0;
  1543. if ((c&0xc0) == 0xc0) {
  1544. werrstr("invalid R/M memory mode: %x", c);
  1545. return 0;
  1546. }
  1547. if (modrm(map, ip, c) < 0)
  1548. return 0;
  1549. break;
  1550. case PTR: /* Seg:Displacement addr (ptr16:16 or ptr16:32) */
  1551. if (ip->osize == 'L') {
  1552. if (igetl(map, ip, &ip->disp) < 0)
  1553. return 0;
  1554. } else {
  1555. if (igets(map, ip, &s)< 0)
  1556. return 0;
  1557. ip->disp = s&0xffff;
  1558. }
  1559. if (igets(map, ip, (ushort*)&ip->seg) < 0)
  1560. return 0;
  1561. ip->jumptype = PTR;
  1562. break;
  1563. case AUXMM: /* Multi-byte op code; prefix determines table selection */
  1564. if (igetc(map, ip, &c) < 0)
  1565. return 0;
  1566. obase = (Optable*)op->proto;
  1567. switch (ip->opre) {
  1568. case 0x66: op = optab660F; break;
  1569. case 0xF2: op = optabF20F; break;
  1570. case 0xF3: op = optabF30F; break;
  1571. default: op = nil; break;
  1572. }
  1573. if(op != nil && op[c].proto != nil)
  1574. obase = op;
  1575. norex = 1; /* no more rex prefixes */
  1576. /* otherwise the optab entry captures it */
  1577. goto newop;
  1578. case AUX: /* Multi-byte op code - Auxiliary table */
  1579. obase = (Optable*)op->proto;
  1580. if (igetc(map, ip, &c) < 0)
  1581. return 0;
  1582. goto newop;
  1583. case OPRE: /* Instr Prefix or media op */
  1584. ip->opre = c;
  1585. /* fall through */
  1586. case PRE: /* Instr Prefix */
  1587. ip->prefix = (char*)op->proto;
  1588. if (igetc(map, ip, &c) < 0)
  1589. return 0;
  1590. if (ip->opre && c == 0x0F)
  1591. ip->prefix = 0;
  1592. goto newop;
  1593. case SEG: /* Segment Prefix */
  1594. ip->segment = (char*)op->proto;
  1595. if (igetc(map, ip, &c) < 0)
  1596. return 0;
  1597. goto newop;
  1598. case OPOVER: /* Operand size override */
  1599. ip->opre = c;
  1600. ip->osize = 'W';
  1601. if (igetc(map, ip, &c) < 0)
  1602. return 0;
  1603. if (c == 0x0F)
  1604. ip->osize = 'L';
  1605. else if (ip->amd64 && (c&0xF0) == 0x40)
  1606. ip->osize = 'Q';
  1607. goto newop;
  1608. case ADDOVER: /* Address size override */
  1609. ip->asize = 0;
  1610. if (igetc(map, ip, &c) < 0)
  1611. return 0;
  1612. goto newop;
  1613. case JUMP: /* mark instruction as JUMP or RET */
  1614. case RET:
  1615. ip->jumptype = op->operand[i];
  1616. break;
  1617. default:
  1618. werrstr("bad operand type %d", op->operand[i]);
  1619. return 0;
  1620. }
  1621. }
  1622. return op;
  1623. }
  1624. #pragma varargck argpos bprint 2
  1625. static void
  1626. bprint(Instr *ip, char *fmt, ...)
  1627. {
  1628. va_list arg;
  1629. va_start(arg, fmt);
  1630. ip->curr = vseprint(ip->curr, ip->end, fmt, arg);
  1631. va_end(arg);
  1632. }
  1633. /*
  1634. * if we want to call 16 bit regs AX,BX,CX,...
  1635. * and 32 bit regs EAX,EBX,ECX,... then
  1636. * change the defs of ANAME and ONAME to:
  1637. * #define ANAME(ip) ((ip->asize == 'E' ? "E" : "")
  1638. * #define ONAME(ip) ((ip)->osize == 'L' ? "E" : "")
  1639. */
  1640. #define ANAME(ip) ""
  1641. #define ONAME(ip) ""
  1642. static char *reg[] = {
  1643. [AX] "AX",
  1644. [CX] "CX",
  1645. [DX] "DX",
  1646. [BX] "BX",
  1647. [SP] "SP",
  1648. [BP] "BP",
  1649. [SI] "SI",
  1650. [DI] "DI",
  1651. /* amd64 */
  1652. [R8] "R8",
  1653. [R9] "R9",
  1654. [R10] "R10",
  1655. [R11] "R11",
  1656. [R12] "R12",
  1657. [R13] "R13",
  1658. [R14] "R14",
  1659. [R15] "R15",
  1660. };
  1661. static char *breg[] = { "AL", "CL", "DL", "BL", "AH", "CH", "DH", "BH" };
  1662. static char *breg64[] = { "AL", "CL", "DL", "BL", "SPB", "BPB", "SIB", "DIB",
  1663. "R8B", "R9B", "R10B", "R11B", "R12B", "R13B", "R14B", "R15B" };
  1664. static char *sreg[] = { "ES", "CS", "SS", "DS", "FS", "GS" };
  1665. static void
  1666. plocal(Instr *ip)
  1667. {
  1668. int ret;
  1669. long offset;
  1670. Symbol s;
  1671. char *reg;
  1672. offset = ip->disp;
  1673. if (!findsym(ip->addr, CTEXT, &s) || !findlocal(&s, FRAMENAME, &s)) {
  1674. bprint(ip, "%lux(SP)", offset);
  1675. return;
  1676. }
  1677. if (s.value > ip->disp) {
  1678. ret = getauto(&s, s.value-ip->disp-mach->szaddr, CAUTO, &s);
  1679. reg = "(SP)";
  1680. } else {
  1681. offset -= s.value;
  1682. ret = getauto(&s, offset, CPARAM, &s);
  1683. reg = "(FP)";
  1684. }
  1685. if (ret)
  1686. bprint(ip, "%s+", s.name);
  1687. else
  1688. offset = ip->disp;
  1689. bprint(ip, "%lux%s", offset, reg);
  1690. }
  1691. static int
  1692. isjmp(Instr *ip)
  1693. {
  1694. switch(ip->jumptype){
  1695. case Iwds:
  1696. case Jbs:
  1697. case JUMP:
  1698. return 1;
  1699. default:
  1700. return 0;
  1701. }
  1702. }
  1703. /*
  1704. * This is too smart for its own good, but it really is nice
  1705. * to have accurate translations when debugging, and it
  1706. * helps us identify which code is different in binaries that
  1707. * are changed on sources.
  1708. */
  1709. static int
  1710. issymref(Instr *ip, Symbol *s, long w, long val)
  1711. {
  1712. Symbol next, tmp;
  1713. long isstring, size;
  1714. if (isjmp(ip))
  1715. return 1;
  1716. if (s->class==CTEXT && w==0)
  1717. return 1;
  1718. if (s->class==CDATA) {
  1719. /* use first bss symbol (or "end") rather than edata */
  1720. if (s->name[0]=='e' && strcmp(s->name, "edata") == 0){
  1721. if((s ->index >= 0 && globalsym(&tmp, s->index+1) && tmp.value==s->value)
  1722. || (s->index > 0 && globalsym(&tmp, s->index-1) && tmp.value==s->value))
  1723. *s = tmp;
  1724. }
  1725. if (w == 0)
  1726. return 1;
  1727. for (next=*s; next.value==s->value; next=tmp)
  1728. if (!globalsym(&tmp, next.index+1))
  1729. break;
  1730. size = next.value - s->value;
  1731. if (w >= size)
  1732. return 0;
  1733. if (w > size-w)
  1734. w = size-w;
  1735. /* huge distances are usually wrong except in .string */
  1736. isstring = (s->name[0]=='.' && strcmp(s->name, ".string") == 0);
  1737. if (w > 8192 && !isstring)
  1738. return 0;
  1739. /* medium distances are tricky - look for constants */
  1740. /* near powers of two */
  1741. if ((val&(val-1)) == 0 || (val&(val+1)) == 0)
  1742. return 0;
  1743. return 1;
  1744. }
  1745. return 0;
  1746. }
  1747. static void
  1748. immediate(Instr *ip, vlong val)
  1749. {
  1750. Symbol s;
  1751. long w;
  1752. if (findsym(val, CANY, &s)) { /* TO DO */
  1753. w = val - s.value;
  1754. if (w < 0)
  1755. w = -w;
  1756. if (issymref(ip, &s, w, val)) {
  1757. if (w)
  1758. bprint(ip, "%s+%lux(SB)", s.name, w);
  1759. else
  1760. bprint(ip, "%s(SB)", s.name);
  1761. return;
  1762. }
  1763. /*
  1764. if (s.class==CDATA && globalsym(&s, s.index+1)) {
  1765. w = s.value - val;
  1766. if (w < 0)
  1767. w = -w;
  1768. if (w < 4096) {
  1769. bprint(ip, "%s-%lux(SB)", s.name, w);
  1770. return;
  1771. }
  1772. }
  1773. */
  1774. }
  1775. if((ip->rex & REXW) == 0)
  1776. bprint(ip, "%lux", (long)val);
  1777. else
  1778. bprint(ip, "%llux", val);
  1779. }
  1780. static void
  1781. pea(Instr *ip)
  1782. {
  1783. if (ip->mod == 3) {
  1784. if (ip->osize == 'B')
  1785. bprint(ip, (ip->rex & REXB? breg64: breg)[ip->base]);
  1786. else if(ip->rex & REXB)
  1787. bprint(ip, "%s%s", ANAME(ip), reg[ip->base+8]);
  1788. else
  1789. bprint(ip, "%s%s", ANAME(ip), reg[ip->base]);
  1790. return;
  1791. }
  1792. if (ip->segment)
  1793. bprint(ip, ip->segment);
  1794. if (ip->asize == 'E' && ip->base == SP)
  1795. plocal(ip);
  1796. else {
  1797. if (ip->base < 0)
  1798. immediate(ip, ip->disp);
  1799. else {
  1800. bprint(ip, "%lux", ip->disp);
  1801. if(ip->rip)
  1802. bprint(ip, "(RIP)");
  1803. bprint(ip,"(%s%s)", ANAME(ip), reg[ip->rex&REXB? ip->base+8: ip->base]);
  1804. }
  1805. }
  1806. if (ip->index >= 0)
  1807. bprint(ip,"(%s%s*%d)", ANAME(ip), reg[ip->rex&REXX? ip->index+8: ip->index], 1<<ip->ss);
  1808. }
  1809. static void
  1810. prinstr(Instr *ip, char *fmt)
  1811. {
  1812. vlong v;
  1813. if (ip->prefix)
  1814. bprint(ip, "%s ", ip->prefix);
  1815. for (; *fmt && ip->curr < ip->end; fmt++) {
  1816. if (*fmt != '%'){
  1817. *ip->curr++ = *fmt;
  1818. continue;
  1819. }
  1820. switch(*++fmt){
  1821. case '%':
  1822. *ip->curr++ = '%';
  1823. break;
  1824. case 'A':
  1825. bprint(ip, "%s", ANAME(ip));
  1826. break;
  1827. case 'C':
  1828. bprint(ip, "CR%d", ip->reg);
  1829. break;
  1830. case 'D':
  1831. if (ip->reg < 4 || ip->reg == 6 || ip->reg == 7)
  1832. bprint(ip, "DR%d",ip->reg);
  1833. else
  1834. bprint(ip, "???");
  1835. break;
  1836. case 'I':
  1837. bprint(ip, "$");
  1838. immediate(ip, ip->imm2);
  1839. break;
  1840. case 'O':
  1841. bprint(ip,"%s", ONAME(ip));
  1842. break;
  1843. case 'i':
  1844. bprint(ip, "$");
  1845. v = ip->imm;
  1846. if(ip->rex & REXW)
  1847. v = ip->imm64;
  1848. immediate(ip, v);
  1849. break;
  1850. case 'R':
  1851. bprint(ip, "%s%s", ONAME(ip), reg[ip->rex&REXR? ip->reg+8: ip->reg]);
  1852. break;
  1853. case 'S':
  1854. if(ip->osize == 'Q' || ip->osize == 'L' && ip->rex & REXW)
  1855. bprint(ip, "Q");
  1856. else
  1857. bprint(ip, "%c", ip->osize);
  1858. break;
  1859. case 's':
  1860. if(ip->opre == 0 || ip->opre == 0x66)
  1861. bprint(ip, "P");
  1862. else
  1863. bprint(ip, "S");
  1864. if(ip->opre == 0xf2 || ip->opre == 0x66)
  1865. bprint(ip, "D");
  1866. else
  1867. bprint(ip, "S");
  1868. break;
  1869. case 'T':
  1870. if (ip->reg == 6 || ip->reg == 7)
  1871. bprint(ip, "TR%d",ip->reg);
  1872. else
  1873. bprint(ip, "???");
  1874. break;
  1875. case 'W':
  1876. if (ip->osize == 'Q' || ip->osize == 'L' && ip->rex & REXW)
  1877. bprint(ip, "CDQE");
  1878. else if (ip->osize == 'L')
  1879. bprint(ip,"CWDE");
  1880. else
  1881. bprint(ip, "CBW");
  1882. break;
  1883. case 'd':
  1884. bprint(ip,"%ux:%lux",ip->seg,ip->disp);
  1885. break;
  1886. case 'm':
  1887. if (ip->mod == 3 && ip->osize != 'B') {
  1888. if(fmt[1] != '*'){
  1889. if(ip->opre != 0) {
  1890. bprint(ip, "X%d", ip->rex&REXB? ip->base+8: ip->base);
  1891. break;
  1892. }
  1893. } else
  1894. fmt++;
  1895. bprint(ip, "M%d", ip->base);
  1896. break;
  1897. }
  1898. pea(ip);
  1899. break;
  1900. case 'e':
  1901. pea(ip);
  1902. break;
  1903. case 'f':
  1904. bprint(ip, "F%d", ip->base);
  1905. break;
  1906. case 'g':
  1907. if (ip->reg < 6)
  1908. bprint(ip,"%s",sreg[ip->reg]);
  1909. else
  1910. bprint(ip,"???");
  1911. break;
  1912. case 'p':
  1913. /*
  1914. * signed immediate in the ulong ip->imm.
  1915. */
  1916. v = (long)ip->imm;
  1917. immediate(ip, v+ip->addr+ip->n);
  1918. break;
  1919. case 'r':
  1920. if (ip->osize == 'B')
  1921. bprint(ip,"%s", (ip->rex? breg64: breg)[ip->rex&REXR? ip->reg+8: ip->reg]);
  1922. else
  1923. bprint(ip, reg[ip->rex&REXR? ip->reg+8: ip->reg]);
  1924. break;
  1925. case 'w':
  1926. if (ip->osize == 'Q' || ip->rex & REXW)
  1927. bprint(ip, "CQO");
  1928. else if (ip->osize == 'L')
  1929. bprint(ip,"CDQ");
  1930. else
  1931. bprint(ip, "CWD");
  1932. break;
  1933. case 'M':
  1934. if(ip->opre != 0)
  1935. bprint(ip, "X%d", ip->rex&REXR? ip->reg+8: ip->reg);
  1936. else
  1937. bprint(ip, "M%d", ip->reg);
  1938. break;
  1939. case 'x':
  1940. if (ip->mod == 3 && ip->osize != 'B') {
  1941. bprint(ip, "X%d", ip->rex&REXB? ip->base+8: ip->base);
  1942. break;
  1943. }
  1944. pea(ip);
  1945. break;
  1946. case 'X':
  1947. bprint(ip, "X%d", ip->rex&REXR? ip->reg+8: ip->reg);
  1948. break;
  1949. default:
  1950. bprint(ip, "%%%c", *fmt);
  1951. break;
  1952. }
  1953. }
  1954. *ip->curr = 0; /* there's always room for 1 byte */
  1955. }
  1956. static int
  1957. i386inst(Map *map, uvlong pc, char modifier, char *buf, int n)
  1958. {
  1959. Instr instr;
  1960. Optable *op;
  1961. USED(modifier);
  1962. op = mkinstr(map, &instr, pc);
  1963. if (op == 0) {
  1964. errstr(buf, n);
  1965. return -1;
  1966. }
  1967. instr.curr = buf;
  1968. instr.end = buf+n-1;
  1969. prinstr(&instr, op->proto);
  1970. return instr.n;
  1971. }
  1972. static int
  1973. i386das(Map *map, uvlong pc, char *buf, int n)
  1974. {
  1975. Instr instr;
  1976. int i;
  1977. if (mkinstr(map, &instr, pc) == 0) {
  1978. errstr(buf, n);
  1979. return -1;
  1980. }
  1981. for(i = 0; i < instr.n && n > 2; i++) {
  1982. _hexify(buf, instr.mem[i], 1);
  1983. buf += 2;
  1984. n -= 2;
  1985. }
  1986. *buf = 0;
  1987. return instr.n;
  1988. }
  1989. static int
  1990. i386instlen(Map *map, uvlong pc)
  1991. {
  1992. Instr i;
  1993. if (mkinstr(map, &i, pc))
  1994. return i.n;
  1995. return -1;
  1996. }
  1997. static int
  1998. i386foll(Map *map, uvlong pc, Rgetter rget, uvlong *foll)
  1999. {
  2000. Instr i;
  2001. Optable *op;
  2002. ushort s;
  2003. uvlong l, addr;
  2004. vlong v;
  2005. int n;
  2006. op = mkinstr(map, &i, pc);
  2007. if (!op)
  2008. return -1;
  2009. n = 0;
  2010. switch(i.jumptype) {
  2011. case RET: /* RETURN or LEAVE */
  2012. case Iw: /* RETURN */
  2013. if (strcmp(op->proto, "LEAVE") == 0) {
  2014. if (geta(map, (*rget)(map, "BP"), &l) < 0)
  2015. return -1;
  2016. } else if (geta(map, (*rget)(map, mach->sp), &l) < 0)
  2017. return -1;
  2018. foll[0] = l;
  2019. return 1;
  2020. case Iwds: /* pc relative JUMP or CALL*/
  2021. case Jbs: /* pc relative JUMP or CALL */
  2022. v = (long)i.imm;
  2023. foll[0] = pc+v+i.n;
  2024. n = 1;
  2025. break;
  2026. case PTR: /* seg:displacement JUMP or CALL */
  2027. foll[0] = (i.seg<<4)+i.disp;
  2028. return 1;
  2029. case JUMP: /* JUMP or CALL EA */
  2030. if(i.mod == 3) {
  2031. foll[0] = (*rget)(map, reg[i.rex&REXB? i.base+8: i.base]);
  2032. return 1;
  2033. }
  2034. /* calculate the effective address */
  2035. addr = i.disp;
  2036. if (i.base >= 0) {
  2037. if (geta(map, (*rget)(map, reg[i.rex&REXB? i.base+8: i.base]), &l) < 0)
  2038. return -1;
  2039. addr += l;
  2040. }
  2041. if (i.index >= 0) {
  2042. if (geta(map, (*rget)(map, reg[i.rex&REXX? i.index+8: i.index]), &l) < 0)
  2043. return -1;
  2044. addr += l*(1<<i.ss);
  2045. }
  2046. /* now retrieve a seg:disp value at that address */
  2047. if (get2(map, addr, &s) < 0) /* seg */
  2048. return -1;
  2049. foll[0] = s<<4;
  2050. addr += 2;
  2051. if (i.asize == 'L') {
  2052. if (geta(map, addr, &l) < 0) /* disp32 */
  2053. return -1;
  2054. foll[0] += l;
  2055. } else { /* disp16 */
  2056. if (get2(map, addr, &s) < 0)
  2057. return -1;
  2058. foll[0] += s;
  2059. }
  2060. return 1;
  2061. default:
  2062. break;
  2063. }
  2064. if (strncmp(op->proto,"JMP", 3) == 0 || strncmp(op->proto,"CALL", 4) == 0)
  2065. return 1;
  2066. foll[n++] = pc+i.n;
  2067. return n;
  2068. }