dis_x86.lua 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. ----------------------------------------------------------------------------
  2. -- LuaJIT x86/x64 disassembler module.
  3. --
  4. -- Copyright (C) 2005-2014 Mike Pall. All rights reserved.
  5. -- Released under the MIT license. See Copyright Notice in luajit.h
  6. ----------------------------------------------------------------------------
  7. -- This is a helper module used by the LuaJIT machine code dumper module.
  8. --
  9. -- Sending small code snippets to an external disassembler and mixing the
  10. -- output with our own stuff was too fragile. So I had to bite the bullet
  11. -- and write yet another x86 disassembler. Oh well ...
  12. --
  13. -- The output format is very similar to what ndisasm generates. But it has
  14. -- been developed independently by looking at the opcode tables from the
  15. -- Intel and AMD manuals. The supported instruction set is quite extensive
  16. -- and reflects what a current generation Intel or AMD CPU implements in
  17. -- 32 bit and 64 bit mode. Yes, this includes MMX, SSE, SSE2, SSE3, SSSE3,
  18. -- SSE4.1, SSE4.2, SSE4a and even privileged and hypervisor (VMX/SVM)
  19. -- instructions.
  20. --
  21. -- Notes:
  22. -- * The (useless) a16 prefix, 3DNow and pre-586 opcodes are unsupported.
  23. -- * No attempt at optimization has been made -- it's fast enough for my needs.
  24. -- * The public API may change when more architectures are added.
  25. ------------------------------------------------------------------------------
  26. local type = type
  27. local sub, byte, format = string.sub, string.byte, string.format
  28. local match, gmatch, gsub = string.match, string.gmatch, string.gsub
  29. local lower, rep = string.lower, string.rep
  30. -- Map for 1st opcode byte in 32 bit mode. Ugly? Well ... read on.
  31. local map_opc1_32 = {
  32. --0x
  33. [0]="addBmr","addVmr","addBrm","addVrm","addBai","addVai","push es","pop es",
  34. "orBmr","orVmr","orBrm","orVrm","orBai","orVai","push cs","opc2*",
  35. --1x
  36. "adcBmr","adcVmr","adcBrm","adcVrm","adcBai","adcVai","push ss","pop ss",
  37. "sbbBmr","sbbVmr","sbbBrm","sbbVrm","sbbBai","sbbVai","push ds","pop ds",
  38. --2x
  39. "andBmr","andVmr","andBrm","andVrm","andBai","andVai","es:seg","daa",
  40. "subBmr","subVmr","subBrm","subVrm","subBai","subVai","cs:seg","das",
  41. --3x
  42. "xorBmr","xorVmr","xorBrm","xorVrm","xorBai","xorVai","ss:seg","aaa",
  43. "cmpBmr","cmpVmr","cmpBrm","cmpVrm","cmpBai","cmpVai","ds:seg","aas",
  44. --4x
  45. "incVR","incVR","incVR","incVR","incVR","incVR","incVR","incVR",
  46. "decVR","decVR","decVR","decVR","decVR","decVR","decVR","decVR",
  47. --5x
  48. "pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR",
  49. "popUR","popUR","popUR","popUR","popUR","popUR","popUR","popUR",
  50. --6x
  51. "sz*pushaw,pusha","sz*popaw,popa","boundVrm","arplWmr",
  52. "fs:seg","gs:seg","o16:","a16",
  53. "pushUi","imulVrmi","pushBs","imulVrms",
  54. "insb","insVS","outsb","outsVS",
  55. --7x
  56. "joBj","jnoBj","jbBj","jnbBj","jzBj","jnzBj","jbeBj","jaBj",
  57. "jsBj","jnsBj","jpeBj","jpoBj","jlBj","jgeBj","jleBj","jgBj",
  58. --8x
  59. "arith!Bmi","arith!Vmi","arith!Bmi","arith!Vms",
  60. "testBmr","testVmr","xchgBrm","xchgVrm",
  61. "movBmr","movVmr","movBrm","movVrm",
  62. "movVmg","leaVrm","movWgm","popUm",
  63. --9x
  64. "nop*xchgVaR|pause|xchgWaR|repne nop","xchgVaR","xchgVaR","xchgVaR",
  65. "xchgVaR","xchgVaR","xchgVaR","xchgVaR",
  66. "sz*cbw,cwde,cdqe","sz*cwd,cdq,cqo","call farViw","wait",
  67. "sz*pushfw,pushf","sz*popfw,popf","sahf","lahf",
  68. --Ax
  69. "movBao","movVao","movBoa","movVoa",
  70. "movsb","movsVS","cmpsb","cmpsVS",
  71. "testBai","testVai","stosb","stosVS",
  72. "lodsb","lodsVS","scasb","scasVS",
  73. --Bx
  74. "movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi",
  75. "movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI",
  76. --Cx
  77. "shift!Bmu","shift!Vmu","retBw","ret","$lesVrm","$ldsVrm","movBmi","movVmi",
  78. "enterBwu","leave","retfBw","retf","int3","intBu","into","iretVS",
  79. --Dx
  80. "shift!Bm1","shift!Vm1","shift!Bmc","shift!Vmc","aamBu","aadBu","salc","xlatb",
  81. "fp*0","fp*1","fp*2","fp*3","fp*4","fp*5","fp*6","fp*7",
  82. --Ex
  83. "loopneBj","loopeBj","loopBj","sz*jcxzBj,jecxzBj,jrcxzBj",
  84. "inBau","inVau","outBua","outVua",
  85. "callVj","jmpVj","jmp farViw","jmpBj","inBad","inVad","outBda","outVda",
  86. --Fx
  87. "lock:","int1","repne:rep","rep:","hlt","cmc","testb!Bm","testv!Vm",
  88. "clc","stc","cli","sti","cld","std","incb!Bm","incd!Vm",
  89. }
  90. assert(#map_opc1_32 == 255)
  91. -- Map for 1st opcode byte in 64 bit mode (overrides only).
  92. local map_opc1_64 = setmetatable({
  93. [0x06]=false, [0x07]=false, [0x0e]=false,
  94. [0x16]=false, [0x17]=false, [0x1e]=false, [0x1f]=false,
  95. [0x27]=false, [0x2f]=false, [0x37]=false, [0x3f]=false,
  96. [0x60]=false, [0x61]=false, [0x62]=false, [0x63]="movsxdVrDmt", [0x67]="a32:",
  97. [0x40]="rex*", [0x41]="rex*b", [0x42]="rex*x", [0x43]="rex*xb",
  98. [0x44]="rex*r", [0x45]="rex*rb", [0x46]="rex*rx", [0x47]="rex*rxb",
  99. [0x48]="rex*w", [0x49]="rex*wb", [0x4a]="rex*wx", [0x4b]="rex*wxb",
  100. [0x4c]="rex*wr", [0x4d]="rex*wrb", [0x4e]="rex*wrx", [0x4f]="rex*wrxb",
  101. [0x82]=false, [0x9a]=false, [0xc4]=false, [0xc5]=false, [0xce]=false,
  102. [0xd4]=false, [0xd5]=false, [0xd6]=false, [0xea]=false,
  103. }, { __index = map_opc1_32 })
  104. -- Map for 2nd opcode byte (0F xx). True CISC hell. Hey, I told you.
  105. -- Prefix dependent MMX/SSE opcodes: (none)|rep|o16|repne, -|F3|66|F2
  106. local map_opc2 = {
  107. --0x
  108. [0]="sldt!Dmp","sgdt!Ump","larVrm","lslVrm",nil,"syscall","clts","sysret",
  109. "invd","wbinvd",nil,"ud1",nil,"$prefetch!Bm","femms","3dnowMrmu",
  110. --1x
  111. "movupsXrm|movssXrm|movupdXrm|movsdXrm",
  112. "movupsXmr|movssXmr|movupdXmr|movsdXmr",
  113. "movhlpsXrm$movlpsXrm|movsldupXrm|movlpdXrm|movddupXrm",
  114. "movlpsXmr||movlpdXmr",
  115. "unpcklpsXrm||unpcklpdXrm",
  116. "unpckhpsXrm||unpckhpdXrm",
  117. "movlhpsXrm$movhpsXrm|movshdupXrm|movhpdXrm",
  118. "movhpsXmr||movhpdXmr",
  119. "$prefetcht!Bm","hintnopVm","hintnopVm","hintnopVm",
  120. "hintnopVm","hintnopVm","hintnopVm","hintnopVm",
  121. --2x
  122. "movUmx$","movUmy$","movUxm$","movUym$","movUmz$",nil,"movUzm$",nil,
  123. "movapsXrm||movapdXrm",
  124. "movapsXmr||movapdXmr",
  125. "cvtpi2psXrMm|cvtsi2ssXrVmt|cvtpi2pdXrMm|cvtsi2sdXrVmt",
  126. "movntpsXmr|movntssXmr|movntpdXmr|movntsdXmr",
  127. "cvttps2piMrXm|cvttss2siVrXm|cvttpd2piMrXm|cvttsd2siVrXm",
  128. "cvtps2piMrXm|cvtss2siVrXm|cvtpd2piMrXm|cvtsd2siVrXm",
  129. "ucomissXrm||ucomisdXrm",
  130. "comissXrm||comisdXrm",
  131. --3x
  132. "wrmsr","rdtsc","rdmsr","rdpmc","sysenter","sysexit",nil,"getsec",
  133. "opc3*38",nil,"opc3*3a",nil,nil,nil,nil,nil,
  134. --4x
  135. "cmovoVrm","cmovnoVrm","cmovbVrm","cmovnbVrm",
  136. "cmovzVrm","cmovnzVrm","cmovbeVrm","cmovaVrm",
  137. "cmovsVrm","cmovnsVrm","cmovpeVrm","cmovpoVrm",
  138. "cmovlVrm","cmovgeVrm","cmovleVrm","cmovgVrm",
  139. --5x
  140. "movmskpsVrXm$||movmskpdVrXm$","sqrtpsXrm|sqrtssXrm|sqrtpdXrm|sqrtsdXrm",
  141. "rsqrtpsXrm|rsqrtssXrm","rcppsXrm|rcpssXrm",
  142. "andpsXrm||andpdXrm","andnpsXrm||andnpdXrm",
  143. "orpsXrm||orpdXrm","xorpsXrm||xorpdXrm",
  144. "addpsXrm|addssXrm|addpdXrm|addsdXrm","mulpsXrm|mulssXrm|mulpdXrm|mulsdXrm",
  145. "cvtps2pdXrm|cvtss2sdXrm|cvtpd2psXrm|cvtsd2ssXrm",
  146. "cvtdq2psXrm|cvttps2dqXrm|cvtps2dqXrm",
  147. "subpsXrm|subssXrm|subpdXrm|subsdXrm","minpsXrm|minssXrm|minpdXrm|minsdXrm",
  148. "divpsXrm|divssXrm|divpdXrm|divsdXrm","maxpsXrm|maxssXrm|maxpdXrm|maxsdXrm",
  149. --6x
  150. "punpcklbwPrm","punpcklwdPrm","punpckldqPrm","packsswbPrm",
  151. "pcmpgtbPrm","pcmpgtwPrm","pcmpgtdPrm","packuswbPrm",
  152. "punpckhbwPrm","punpckhwdPrm","punpckhdqPrm","packssdwPrm",
  153. "||punpcklqdqXrm","||punpckhqdqXrm",
  154. "movPrVSm","movqMrm|movdquXrm|movdqaXrm",
  155. --7x
  156. "pshufwMrmu|pshufhwXrmu|pshufdXrmu|pshuflwXrmu","pshiftw!Pmu",
  157. "pshiftd!Pmu","pshiftq!Mmu||pshiftdq!Xmu",
  158. "pcmpeqbPrm","pcmpeqwPrm","pcmpeqdPrm","emms|",
  159. "vmreadUmr||extrqXmuu$|insertqXrmuu$","vmwriteUrm||extrqXrm$|insertqXrm$",
  160. nil,nil,
  161. "||haddpdXrm|haddpsXrm","||hsubpdXrm|hsubpsXrm",
  162. "movVSmMr|movqXrm|movVSmXr","movqMmr|movdquXmr|movdqaXmr",
  163. --8x
  164. "joVj","jnoVj","jbVj","jnbVj","jzVj","jnzVj","jbeVj","jaVj",
  165. "jsVj","jnsVj","jpeVj","jpoVj","jlVj","jgeVj","jleVj","jgVj",
  166. --9x
  167. "setoBm","setnoBm","setbBm","setnbBm","setzBm","setnzBm","setbeBm","setaBm",
  168. "setsBm","setnsBm","setpeBm","setpoBm","setlBm","setgeBm","setleBm","setgBm",
  169. --Ax
  170. "push fs","pop fs","cpuid","btVmr","shldVmru","shldVmrc",nil,nil,
  171. "push gs","pop gs","rsm","btsVmr","shrdVmru","shrdVmrc","fxsave!Dmp","imulVrm",
  172. --Bx
  173. "cmpxchgBmr","cmpxchgVmr","$lssVrm","btrVmr",
  174. "$lfsVrm","$lgsVrm","movzxVrBmt","movzxVrWmt",
  175. "|popcntVrm","ud2Dp","bt!Vmu","btcVmr",
  176. "bsfVrm","bsrVrm|lzcntVrm|bsrWrm","movsxVrBmt","movsxVrWmt",
  177. --Cx
  178. "xaddBmr","xaddVmr",
  179. "cmppsXrmu|cmpssXrmu|cmppdXrmu|cmpsdXrmu","$movntiVmr|",
  180. "pinsrwPrWmu","pextrwDrPmu",
  181. "shufpsXrmu||shufpdXrmu","$cmpxchg!Qmp",
  182. "bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR",
  183. --Dx
  184. "||addsubpdXrm|addsubpsXrm","psrlwPrm","psrldPrm","psrlqPrm",
  185. "paddqPrm","pmullwPrm",
  186. "|movq2dqXrMm|movqXmr|movdq2qMrXm$","pmovmskbVrMm||pmovmskbVrXm",
  187. "psubusbPrm","psubuswPrm","pminubPrm","pandPrm",
  188. "paddusbPrm","padduswPrm","pmaxubPrm","pandnPrm",
  189. --Ex
  190. "pavgbPrm","psrawPrm","psradPrm","pavgwPrm",
  191. "pmulhuwPrm","pmulhwPrm",
  192. "|cvtdq2pdXrm|cvttpd2dqXrm|cvtpd2dqXrm","$movntqMmr||$movntdqXmr",
  193. "psubsbPrm","psubswPrm","pminswPrm","porPrm",
  194. "paddsbPrm","paddswPrm","pmaxswPrm","pxorPrm",
  195. --Fx
  196. "|||lddquXrm","psllwPrm","pslldPrm","psllqPrm",
  197. "pmuludqPrm","pmaddwdPrm","psadbwPrm","maskmovqMrm||maskmovdquXrm$",
  198. "psubbPrm","psubwPrm","psubdPrm","psubqPrm",
  199. "paddbPrm","paddwPrm","padddPrm","ud",
  200. }
  201. assert(map_opc2[255] == "ud")
  202. -- Map for three-byte opcodes. Can't wait for their next invention.
  203. local map_opc3 = {
  204. ["38"] = { -- [66] 0f 38 xx
  205. --0x
  206. [0]="pshufbPrm","phaddwPrm","phadddPrm","phaddswPrm",
  207. "pmaddubswPrm","phsubwPrm","phsubdPrm","phsubswPrm",
  208. "psignbPrm","psignwPrm","psigndPrm","pmulhrswPrm",
  209. nil,nil,nil,nil,
  210. --1x
  211. "||pblendvbXrma",nil,nil,nil,
  212. "||blendvpsXrma","||blendvpdXrma",nil,"||ptestXrm",
  213. nil,nil,nil,nil,
  214. "pabsbPrm","pabswPrm","pabsdPrm",nil,
  215. --2x
  216. "||pmovsxbwXrm","||pmovsxbdXrm","||pmovsxbqXrm","||pmovsxwdXrm",
  217. "||pmovsxwqXrm","||pmovsxdqXrm",nil,nil,
  218. "||pmuldqXrm","||pcmpeqqXrm","||$movntdqaXrm","||packusdwXrm",
  219. nil,nil,nil,nil,
  220. --3x
  221. "||pmovzxbwXrm","||pmovzxbdXrm","||pmovzxbqXrm","||pmovzxwdXrm",
  222. "||pmovzxwqXrm","||pmovzxdqXrm",nil,"||pcmpgtqXrm",
  223. "||pminsbXrm","||pminsdXrm","||pminuwXrm","||pminudXrm",
  224. "||pmaxsbXrm","||pmaxsdXrm","||pmaxuwXrm","||pmaxudXrm",
  225. --4x
  226. "||pmulddXrm","||phminposuwXrm",
  227. --Fx
  228. [0xf0] = "|||crc32TrBmt",[0xf1] = "|||crc32TrVmt",
  229. },
  230. ["3a"] = { -- [66] 0f 3a xx
  231. --0x
  232. [0x00]=nil,nil,nil,nil,nil,nil,nil,nil,
  233. "||roundpsXrmu","||roundpdXrmu","||roundssXrmu","||roundsdXrmu",
  234. "||blendpsXrmu","||blendpdXrmu","||pblendwXrmu","palignrPrmu",
  235. --1x
  236. nil,nil,nil,nil,
  237. "||pextrbVmXru","||pextrwVmXru","||pextrVmSXru","||extractpsVmXru",
  238. nil,nil,nil,nil,nil,nil,nil,nil,
  239. --2x
  240. "||pinsrbXrVmu","||insertpsXrmu","||pinsrXrVmuS",nil,
  241. --4x
  242. [0x40] = "||dppsXrmu",
  243. [0x41] = "||dppdXrmu",
  244. [0x42] = "||mpsadbwXrmu",
  245. --6x
  246. [0x60] = "||pcmpestrmXrmu",[0x61] = "||pcmpestriXrmu",
  247. [0x62] = "||pcmpistrmXrmu",[0x63] = "||pcmpistriXrmu",
  248. },
  249. }
  250. -- Map for VMX/SVM opcodes 0F 01 C0-FF (sgdt group with register operands).
  251. local map_opcvm = {
  252. [0xc1]="vmcall",[0xc2]="vmlaunch",[0xc3]="vmresume",[0xc4]="vmxoff",
  253. [0xc8]="monitor",[0xc9]="mwait",
  254. [0xd8]="vmrun",[0xd9]="vmmcall",[0xda]="vmload",[0xdb]="vmsave",
  255. [0xdc]="stgi",[0xdd]="clgi",[0xde]="skinit",[0xdf]="invlpga",
  256. [0xf8]="swapgs",[0xf9]="rdtscp",
  257. }
  258. -- Map for FP opcodes. And you thought stack machines are simple?
  259. local map_opcfp = {
  260. -- D8-DF 00-BF: opcodes with a memory operand.
  261. -- D8
  262. [0]="faddFm","fmulFm","fcomFm","fcompFm","fsubFm","fsubrFm","fdivFm","fdivrFm",
  263. "fldFm",nil,"fstFm","fstpFm","fldenvVm","fldcwWm","fnstenvVm","fnstcwWm",
  264. -- DA
  265. "fiaddDm","fimulDm","ficomDm","ficompDm",
  266. "fisubDm","fisubrDm","fidivDm","fidivrDm",
  267. -- DB
  268. "fildDm","fisttpDm","fistDm","fistpDm",nil,"fld twordFmp",nil,"fstp twordFmp",
  269. -- DC
  270. "faddGm","fmulGm","fcomGm","fcompGm","fsubGm","fsubrGm","fdivGm","fdivrGm",
  271. -- DD
  272. "fldGm","fisttpQm","fstGm","fstpGm","frstorDmp",nil,"fnsaveDmp","fnstswWm",
  273. -- DE
  274. "fiaddWm","fimulWm","ficomWm","ficompWm",
  275. "fisubWm","fisubrWm","fidivWm","fidivrWm",
  276. -- DF
  277. "fildWm","fisttpWm","fistWm","fistpWm",
  278. "fbld twordFmp","fildQm","fbstp twordFmp","fistpQm",
  279. -- xx C0-FF: opcodes with a pseudo-register operand.
  280. -- D8
  281. "faddFf","fmulFf","fcomFf","fcompFf","fsubFf","fsubrFf","fdivFf","fdivrFf",
  282. -- D9
  283. "fldFf","fxchFf",{"fnop"},nil,
  284. {"fchs","fabs",nil,nil,"ftst","fxam"},
  285. {"fld1","fldl2t","fldl2e","fldpi","fldlg2","fldln2","fldz"},
  286. {"f2xm1","fyl2x","fptan","fpatan","fxtract","fprem1","fdecstp","fincstp"},
  287. {"fprem","fyl2xp1","fsqrt","fsincos","frndint","fscale","fsin","fcos"},
  288. -- DA
  289. "fcmovbFf","fcmoveFf","fcmovbeFf","fcmovuFf",nil,{nil,"fucompp"},nil,nil,
  290. -- DB
  291. "fcmovnbFf","fcmovneFf","fcmovnbeFf","fcmovnuFf",
  292. {nil,nil,"fnclex","fninit"},"fucomiFf","fcomiFf",nil,
  293. -- DC
  294. "fadd toFf","fmul toFf",nil,nil,
  295. "fsub toFf","fsubr toFf","fdivr toFf","fdiv toFf",
  296. -- DD
  297. "ffreeFf",nil,"fstFf","fstpFf","fucomFf","fucompFf",nil,nil,
  298. -- DE
  299. "faddpFf","fmulpFf",nil,{nil,"fcompp"},
  300. "fsubrpFf","fsubpFf","fdivrpFf","fdivpFf",
  301. -- DF
  302. nil,nil,nil,nil,{"fnstsw ax"},"fucomipFf","fcomipFf",nil,
  303. }
  304. assert(map_opcfp[126] == "fcomipFf")
  305. -- Map for opcode groups. The subkey is sp from the ModRM byte.
  306. local map_opcgroup = {
  307. arith = { "add", "or", "adc", "sbb", "and", "sub", "xor", "cmp" },
  308. shift = { "rol", "ror", "rcl", "rcr", "shl", "shr", "sal", "sar" },
  309. testb = { "testBmi", "testBmi", "not", "neg", "mul", "imul", "div", "idiv" },
  310. testv = { "testVmi", "testVmi", "not", "neg", "mul", "imul", "div", "idiv" },
  311. incb = { "inc", "dec" },
  312. incd = { "inc", "dec", "callUmp", "$call farDmp",
  313. "jmpUmp", "$jmp farDmp", "pushUm" },
  314. sldt = { "sldt", "str", "lldt", "ltr", "verr", "verw" },
  315. sgdt = { "vm*$sgdt", "vm*$sidt", "$lgdt", "vm*$lidt",
  316. "smsw", nil, "lmsw", "vm*$invlpg" },
  317. bt = { nil, nil, nil, nil, "bt", "bts", "btr", "btc" },
  318. cmpxchg = { nil, "sz*,cmpxchg8bQmp,cmpxchg16bXmp", nil, nil,
  319. nil, nil, "vmptrld|vmxon|vmclear", "vmptrst" },
  320. pshiftw = { nil, nil, "psrlw", nil, "psraw", nil, "psllw" },
  321. pshiftd = { nil, nil, "psrld", nil, "psrad", nil, "pslld" },
  322. pshiftq = { nil, nil, "psrlq", nil, nil, nil, "psllq" },
  323. pshiftdq = { nil, nil, "psrlq", "psrldq", nil, nil, "psllq", "pslldq" },
  324. fxsave = { "$fxsave", "$fxrstor", "$ldmxcsr", "$stmxcsr",
  325. nil, "lfenceDp$", "mfenceDp$", "sfenceDp$clflush" },
  326. prefetch = { "prefetch", "prefetchw" },
  327. prefetcht = { "prefetchnta", "prefetcht0", "prefetcht1", "prefetcht2" },
  328. }
  329. ------------------------------------------------------------------------------
  330. -- Maps for register names.
  331. local map_regs = {
  332. B = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh",
  333. "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" },
  334. B64 = { "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
  335. "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" },
  336. W = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
  337. "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w" },
  338. D = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
  339. "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" },
  340. Q = { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
  341. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" },
  342. M = { "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
  343. "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7" }, -- No x64 ext!
  344. X = { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
  345. "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15" },
  346. }
  347. local map_segregs = { "es", "cs", "ss", "ds", "fs", "gs", "segr6", "segr7" }
  348. -- Maps for size names.
  349. local map_sz2n = {
  350. B = 1, W = 2, D = 4, Q = 8, M = 8, X = 16,
  351. }
  352. local map_sz2prefix = {
  353. B = "byte", W = "word", D = "dword",
  354. Q = "qword",
  355. M = "qword", X = "xword",
  356. F = "dword", G = "qword", -- No need for sizes/register names for these two.
  357. }
  358. ------------------------------------------------------------------------------
  359. -- Output a nicely formatted line with an opcode and operands.
  360. local function putop(ctx, text, operands)
  361. local code, pos, hex = ctx.code, ctx.pos, ""
  362. local hmax = ctx.hexdump
  363. if hmax > 0 then
  364. for i=ctx.start,pos-1 do
  365. hex = hex..format("%02X", byte(code, i, i))
  366. end
  367. if #hex > hmax then hex = sub(hex, 1, hmax)..". "
  368. else hex = hex..rep(" ", hmax-#hex+2) end
  369. end
  370. if operands then text = text.." "..operands end
  371. if ctx.o16 then text = "o16 "..text; ctx.o16 = false end
  372. if ctx.a32 then text = "a32 "..text; ctx.a32 = false end
  373. if ctx.rep then text = ctx.rep.." "..text; ctx.rep = false end
  374. if ctx.rex then
  375. local t = (ctx.rexw and "w" or "")..(ctx.rexr and "r" or "")..
  376. (ctx.rexx and "x" or "")..(ctx.rexb and "b" or "")
  377. if t ~= "" then text = "rex."..t.." "..text end
  378. ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false
  379. ctx.rex = false
  380. end
  381. if ctx.seg then
  382. local text2, n = gsub(text, "%[", "["..ctx.seg..":")
  383. if n == 0 then text = ctx.seg.." "..text else text = text2 end
  384. ctx.seg = false
  385. end
  386. if ctx.lock then text = "lock "..text; ctx.lock = false end
  387. local imm = ctx.imm
  388. if imm then
  389. local sym = ctx.symtab[imm]
  390. if sym then text = text.."\t->"..sym end
  391. end
  392. ctx.out(format("%08x %s%s\n", ctx.addr+ctx.start, hex, text))
  393. ctx.mrm = false
  394. ctx.start = pos
  395. ctx.imm = nil
  396. end
  397. -- Clear all prefix flags.
  398. local function clearprefixes(ctx)
  399. ctx.o16 = false; ctx.seg = false; ctx.lock = false; ctx.rep = false
  400. ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false
  401. ctx.rex = false; ctx.a32 = false
  402. end
  403. -- Fallback for incomplete opcodes at the end.
  404. local function incomplete(ctx)
  405. ctx.pos = ctx.stop+1
  406. clearprefixes(ctx)
  407. return putop(ctx, "(incomplete)")
  408. end
  409. -- Fallback for unknown opcodes.
  410. local function unknown(ctx)
  411. clearprefixes(ctx)
  412. return putop(ctx, "(unknown)")
  413. end
  414. -- Return an immediate of the specified size.
  415. local function getimm(ctx, pos, n)
  416. if pos+n-1 > ctx.stop then return incomplete(ctx) end
  417. local code = ctx.code
  418. if n == 1 then
  419. local b1 = byte(code, pos, pos)
  420. return b1
  421. elseif n == 2 then
  422. local b1, b2 = byte(code, pos, pos+1)
  423. return b1+b2*256
  424. else
  425. local b1, b2, b3, b4 = byte(code, pos, pos+3)
  426. local imm = b1+b2*256+b3*65536+b4*16777216
  427. ctx.imm = imm
  428. return imm
  429. end
  430. end
  431. -- Process pattern string and generate the operands.
  432. local function putpat(ctx, name, pat)
  433. local operands, regs, sz, mode, sp, rm, sc, rx, sdisp
  434. local code, pos, stop = ctx.code, ctx.pos, ctx.stop
  435. -- Chars used: 1DFGIMPQRSTUVWXacdfgijmoprstuwxyz
  436. for p in gmatch(pat, ".") do
  437. local x = nil
  438. if p == "V" or p == "U" then
  439. if ctx.rexw then sz = "Q"; ctx.rexw = false
  440. elseif ctx.o16 then sz = "W"; ctx.o16 = false
  441. elseif p == "U" and ctx.x64 then sz = "Q"
  442. else sz = "D" end
  443. regs = map_regs[sz]
  444. elseif p == "T" then
  445. if ctx.rexw then sz = "Q"; ctx.rexw = false else sz = "D" end
  446. regs = map_regs[sz]
  447. elseif p == "B" then
  448. sz = "B"
  449. regs = ctx.rex and map_regs.B64 or map_regs.B
  450. elseif match(p, "[WDQMXFG]") then
  451. sz = p
  452. regs = map_regs[sz]
  453. elseif p == "P" then
  454. sz = ctx.o16 and "X" or "M"; ctx.o16 = false
  455. regs = map_regs[sz]
  456. elseif p == "S" then
  457. name = name..lower(sz)
  458. elseif p == "s" then
  459. local imm = getimm(ctx, pos, 1); if not imm then return end
  460. x = imm <= 127 and format("+0x%02x", imm)
  461. or format("-0x%02x", 256-imm)
  462. pos = pos+1
  463. elseif p == "u" then
  464. local imm = getimm(ctx, pos, 1); if not imm then return end
  465. x = format("0x%02x", imm)
  466. pos = pos+1
  467. elseif p == "w" then
  468. local imm = getimm(ctx, pos, 2); if not imm then return end
  469. x = format("0x%x", imm)
  470. pos = pos+2
  471. elseif p == "o" then -- [offset]
  472. if ctx.x64 then
  473. local imm1 = getimm(ctx, pos, 4); if not imm1 then return end
  474. local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end
  475. x = format("[0x%08x%08x]", imm2, imm1)
  476. pos = pos+8
  477. else
  478. local imm = getimm(ctx, pos, 4); if not imm then return end
  479. x = format("[0x%08x]", imm)
  480. pos = pos+4
  481. end
  482. elseif p == "i" or p == "I" then
  483. local n = map_sz2n[sz]
  484. if n == 8 and ctx.x64 and p == "I" then
  485. local imm1 = getimm(ctx, pos, 4); if not imm1 then return end
  486. local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end
  487. x = format("0x%08x%08x", imm2, imm1)
  488. else
  489. if n == 8 then n = 4 end
  490. local imm = getimm(ctx, pos, n); if not imm then return end
  491. if sz == "Q" and (imm < 0 or imm > 0x7fffffff) then
  492. imm = (0xffffffff+1)-imm
  493. x = format(imm > 65535 and "-0x%08x" or "-0x%x", imm)
  494. else
  495. x = format(imm > 65535 and "0x%08x" or "0x%x", imm)
  496. end
  497. end
  498. pos = pos+n
  499. elseif p == "j" then
  500. local n = map_sz2n[sz]
  501. if n == 8 then n = 4 end
  502. local imm = getimm(ctx, pos, n); if not imm then return end
  503. if sz == "B" and imm > 127 then imm = imm-256
  504. elseif imm > 2147483647 then imm = imm-4294967296 end
  505. pos = pos+n
  506. imm = imm + pos + ctx.addr
  507. if imm > 4294967295 and not ctx.x64 then imm = imm-4294967296 end
  508. ctx.imm = imm
  509. if sz == "W" then
  510. x = format("word 0x%04x", imm%65536)
  511. elseif ctx.x64 then
  512. local lo = imm % 0x1000000
  513. x = format("0x%02x%06x", (imm-lo) / 0x1000000, lo)
  514. else
  515. x = format("0x%08x", imm)
  516. end
  517. elseif p == "R" then
  518. local r = byte(code, pos-1, pos-1)%8
  519. if ctx.rexb then r = r + 8; ctx.rexb = false end
  520. x = regs[r+1]
  521. elseif p == "a" then x = regs[1]
  522. elseif p == "c" then x = "cl"
  523. elseif p == "d" then x = "dx"
  524. elseif p == "1" then x = "1"
  525. else
  526. if not mode then
  527. mode = ctx.mrm
  528. if not mode then
  529. if pos > stop then return incomplete(ctx) end
  530. mode = byte(code, pos, pos)
  531. pos = pos+1
  532. end
  533. rm = mode%8; mode = (mode-rm)/8
  534. sp = mode%8; mode = (mode-sp)/8
  535. sdisp = ""
  536. if mode < 3 then
  537. if rm == 4 then
  538. if pos > stop then return incomplete(ctx) end
  539. sc = byte(code, pos, pos)
  540. pos = pos+1
  541. rm = sc%8; sc = (sc-rm)/8
  542. rx = sc%8; sc = (sc-rx)/8
  543. if ctx.rexx then rx = rx + 8; ctx.rexx = false end
  544. if rx == 4 then rx = nil end
  545. end
  546. if mode > 0 or rm == 5 then
  547. local dsz = mode
  548. if dsz ~= 1 then dsz = 4 end
  549. local disp = getimm(ctx, pos, dsz); if not disp then return end
  550. if mode == 0 then rm = nil end
  551. if rm or rx or (not sc and ctx.x64 and not ctx.a32) then
  552. if dsz == 1 and disp > 127 then
  553. sdisp = format("-0x%x", 256-disp)
  554. elseif disp >= 0 and disp <= 0x7fffffff then
  555. sdisp = format("+0x%x", disp)
  556. else
  557. sdisp = format("-0x%x", (0xffffffff+1)-disp)
  558. end
  559. else
  560. sdisp = format(ctx.x64 and not ctx.a32 and
  561. not (disp >= 0 and disp <= 0x7fffffff)
  562. and "0xffffffff%08x" or "0x%08x", disp)
  563. end
  564. pos = pos+dsz
  565. end
  566. end
  567. if rm and ctx.rexb then rm = rm + 8; ctx.rexb = false end
  568. if ctx.rexr then sp = sp + 8; ctx.rexr = false end
  569. end
  570. if p == "m" then
  571. if mode == 3 then x = regs[rm+1]
  572. else
  573. local aregs = ctx.a32 and map_regs.D or ctx.aregs
  574. local srm, srx = "", ""
  575. if rm then srm = aregs[rm+1]
  576. elseif not sc and ctx.x64 and not ctx.a32 then srm = "rip" end
  577. ctx.a32 = false
  578. if rx then
  579. if rm then srm = srm.."+" end
  580. srx = aregs[rx+1]
  581. if sc > 0 then srx = srx.."*"..(2^sc) end
  582. end
  583. x = format("[%s%s%s]", srm, srx, sdisp)
  584. end
  585. if mode < 3 and
  586. (not match(pat, "[aRrgp]") or match(pat, "t")) then -- Yuck.
  587. x = map_sz2prefix[sz].." "..x
  588. end
  589. elseif p == "r" then x = regs[sp+1]
  590. elseif p == "g" then x = map_segregs[sp+1]
  591. elseif p == "p" then -- Suppress prefix.
  592. elseif p == "f" then x = "st"..rm
  593. elseif p == "x" then
  594. if sp == 0 and ctx.lock and not ctx.x64 then
  595. x = "CR8"; ctx.lock = false
  596. else
  597. x = "CR"..sp
  598. end
  599. elseif p == "y" then x = "DR"..sp
  600. elseif p == "z" then x = "TR"..sp
  601. elseif p == "t" then
  602. else
  603. error("bad pattern `"..pat.."'")
  604. end
  605. end
  606. if x then operands = operands and operands..", "..x or x end
  607. end
  608. ctx.pos = pos
  609. return putop(ctx, name, operands)
  610. end
  611. -- Forward declaration.
  612. local map_act
  613. -- Fetch and cache MRM byte.
  614. local function getmrm(ctx)
  615. local mrm = ctx.mrm
  616. if not mrm then
  617. local pos = ctx.pos
  618. if pos > ctx.stop then return nil end
  619. mrm = byte(ctx.code, pos, pos)
  620. ctx.pos = pos+1
  621. ctx.mrm = mrm
  622. end
  623. return mrm
  624. end
  625. -- Dispatch to handler depending on pattern.
  626. local function dispatch(ctx, opat, patgrp)
  627. if not opat then return unknown(ctx) end
  628. if match(opat, "%|") then -- MMX/SSE variants depending on prefix.
  629. local p
  630. if ctx.rep then
  631. p = ctx.rep=="rep" and "%|([^%|]*)" or "%|[^%|]*%|[^%|]*%|([^%|]*)"
  632. ctx.rep = false
  633. elseif ctx.o16 then p = "%|[^%|]*%|([^%|]*)"; ctx.o16 = false
  634. else p = "^[^%|]*" end
  635. opat = match(opat, p)
  636. if not opat then return unknown(ctx) end
  637. -- ctx.rep = false; ctx.o16 = false
  638. --XXX fails for 66 f2 0f 38 f1 06 crc32 eax,WORD PTR [esi]
  639. --XXX remove in branches?
  640. end
  641. if match(opat, "%$") then -- reg$mem variants.
  642. local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end
  643. opat = match(opat, mrm >= 192 and "^[^%$]*" or "%$(.*)")
  644. if opat == "" then return unknown(ctx) end
  645. end
  646. if opat == "" then return unknown(ctx) end
  647. local name, pat = match(opat, "^([a-z0-9 ]*)(.*)")
  648. if pat == "" and patgrp then pat = patgrp end
  649. return map_act[sub(pat, 1, 1)](ctx, name, pat)
  650. end
  651. -- Get a pattern from an opcode map and dispatch to handler.
  652. local function dispatchmap(ctx, opcmap)
  653. local pos = ctx.pos
  654. local opat = opcmap[byte(ctx.code, pos, pos)]
  655. pos = pos + 1
  656. ctx.pos = pos
  657. return dispatch(ctx, opat)
  658. end
  659. -- Map for action codes. The key is the first char after the name.
  660. map_act = {
  661. -- Simple opcodes without operands.
  662. [""] = function(ctx, name, pat)
  663. return putop(ctx, name)
  664. end,
  665. -- Operand size chars fall right through.
  666. B = putpat, W = putpat, D = putpat, Q = putpat,
  667. V = putpat, U = putpat, T = putpat,
  668. M = putpat, X = putpat, P = putpat,
  669. F = putpat, G = putpat,
  670. -- Collect prefixes.
  671. [":"] = function(ctx, name, pat)
  672. ctx[pat == ":" and name or sub(pat, 2)] = name
  673. if ctx.pos - ctx.start > 5 then return unknown(ctx) end -- Limit #prefixes.
  674. end,
  675. -- Chain to special handler specified by name.
  676. ["*"] = function(ctx, name, pat)
  677. return map_act[name](ctx, name, sub(pat, 2))
  678. end,
  679. -- Use named subtable for opcode group.
  680. ["!"] = function(ctx, name, pat)
  681. local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end
  682. return dispatch(ctx, map_opcgroup[name][((mrm-(mrm%8))/8)%8+1], sub(pat, 2))
  683. end,
  684. -- o16,o32[,o64] variants.
  685. sz = function(ctx, name, pat)
  686. if ctx.o16 then ctx.o16 = false
  687. else
  688. pat = match(pat, ",(.*)")
  689. if ctx.rexw then
  690. local p = match(pat, ",(.*)")
  691. if p then pat = p; ctx.rexw = false end
  692. end
  693. end
  694. pat = match(pat, "^[^,]*")
  695. return dispatch(ctx, pat)
  696. end,
  697. -- Two-byte opcode dispatch.
  698. opc2 = function(ctx, name, pat)
  699. return dispatchmap(ctx, map_opc2)
  700. end,
  701. -- Three-byte opcode dispatch.
  702. opc3 = function(ctx, name, pat)
  703. return dispatchmap(ctx, map_opc3[pat])
  704. end,
  705. -- VMX/SVM dispatch.
  706. vm = function(ctx, name, pat)
  707. return dispatch(ctx, map_opcvm[ctx.mrm])
  708. end,
  709. -- Floating point opcode dispatch.
  710. fp = function(ctx, name, pat)
  711. local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end
  712. local rm = mrm%8
  713. local idx = pat*8 + ((mrm-rm)/8)%8
  714. if mrm >= 192 then idx = idx + 64 end
  715. local opat = map_opcfp[idx]
  716. if type(opat) == "table" then opat = opat[rm+1] end
  717. return dispatch(ctx, opat)
  718. end,
  719. -- REX prefix.
  720. rex = function(ctx, name, pat)
  721. if ctx.rex then return unknown(ctx) end -- Only 1 REX prefix allowed.
  722. for p in gmatch(pat, ".") do ctx["rex"..p] = true end
  723. ctx.rex = true
  724. end,
  725. -- Special case for nop with REX prefix.
  726. nop = function(ctx, name, pat)
  727. return dispatch(ctx, ctx.rex and pat or "nop")
  728. end,
  729. }
  730. ------------------------------------------------------------------------------
  731. -- Disassemble a block of code.
  732. local function disass_block(ctx, ofs, len)
  733. if not ofs then ofs = 0 end
  734. local stop = len and ofs+len or #ctx.code
  735. ofs = ofs + 1
  736. ctx.start = ofs
  737. ctx.pos = ofs
  738. ctx.stop = stop
  739. ctx.imm = nil
  740. ctx.mrm = false
  741. clearprefixes(ctx)
  742. while ctx.pos <= stop do dispatchmap(ctx, ctx.map1) end
  743. if ctx.pos ~= ctx.start then incomplete(ctx) end
  744. end
  745. -- Extended API: create a disassembler context. Then call ctx:disass(ofs, len).
  746. local function create_(code, addr, out)
  747. local ctx = {}
  748. ctx.code = code
  749. ctx.addr = (addr or 0) - 1
  750. ctx.out = out or io.write
  751. ctx.symtab = {}
  752. ctx.disass = disass_block
  753. ctx.hexdump = 16
  754. ctx.x64 = false
  755. ctx.map1 = map_opc1_32
  756. ctx.aregs = map_regs.D
  757. return ctx
  758. end
  759. local function create64_(code, addr, out)
  760. local ctx = create_(code, addr, out)
  761. ctx.x64 = true
  762. ctx.map1 = map_opc1_64
  763. ctx.aregs = map_regs.Q
  764. return ctx
  765. end
  766. -- Simple API: disassemble code (a string) at address and output via out.
  767. local function disass_(code, addr, out)
  768. create_(code, addr, out):disass()
  769. end
  770. local function disass64_(code, addr, out)
  771. create64_(code, addr, out):disass()
  772. end
  773. -- Return register name for RID.
  774. local function regname_(r)
  775. if r < 8 then return map_regs.D[r+1] end
  776. return map_regs.X[r-7]
  777. end
  778. local function regname64_(r)
  779. if r < 16 then return map_regs.Q[r+1] end
  780. return map_regs.X[r-15]
  781. end
  782. -- Public module functions.
  783. module(...)
  784. create = create_
  785. create64 = create64_
  786. disass = disass_
  787. disass64 = disass64_
  788. regname = regname_
  789. regname64 = regname64_