8db.c 49 KB

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