dis_x86.lua 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. ----------------------------------------------------------------------------
  2. -- LuaJIT x86/x64 disassembler module.
  3. --
  4. -- Copyright (C) 2005-2025 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, AVX, AVX2 and even privileged and hypervisor
  19. -- (VMX/SVM) 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. ------------------------------------------------------------------------------
  25. local type = type
  26. local sub, byte, format = string.sub, string.byte, string.format
  27. local match, gmatch, gsub = string.match, string.gmatch, string.gsub
  28. local lower, rep = string.lower, string.rep
  29. local bit = require("bit")
  30. local tohex = bit.tohex
  31. -- Map for 1st opcode byte in 32 bit mode. Ugly? Well ... read on.
  32. local map_opc1_32 = {
  33. --0x
  34. [0]="addBmr","addVmr","addBrm","addVrm","addBai","addVai","push es","pop es",
  35. "orBmr","orVmr","orBrm","orVrm","orBai","orVai","push cs","opc2*",
  36. --1x
  37. "adcBmr","adcVmr","adcBrm","adcVrm","adcBai","adcVai","push ss","pop ss",
  38. "sbbBmr","sbbVmr","sbbBrm","sbbVrm","sbbBai","sbbVai","push ds","pop ds",
  39. --2x
  40. "andBmr","andVmr","andBrm","andVrm","andBai","andVai","es:seg","daa",
  41. "subBmr","subVmr","subBrm","subVrm","subBai","subVai","cs:seg","das",
  42. --3x
  43. "xorBmr","xorVmr","xorBrm","xorVrm","xorBai","xorVai","ss:seg","aaa",
  44. "cmpBmr","cmpVmr","cmpBrm","cmpVrm","cmpBai","cmpVai","ds:seg","aas",
  45. --4x
  46. "incVR","incVR","incVR","incVR","incVR","incVR","incVR","incVR",
  47. "decVR","decVR","decVR","decVR","decVR","decVR","decVR","decVR",
  48. --5x
  49. "pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR",
  50. "popUR","popUR","popUR","popUR","popUR","popUR","popUR","popUR",
  51. --6x
  52. "sz*pushaw,pusha","sz*popaw,popa","boundVrm","arplWmr",
  53. "fs:seg","gs:seg","o16:","a16",
  54. "pushUi","imulVrmi","pushBs","imulVrms",
  55. "insb","insVS","outsb","outsVS",
  56. --7x
  57. "joBj","jnoBj","jbBj","jnbBj","jzBj","jnzBj","jbeBj","jaBj",
  58. "jsBj","jnsBj","jpeBj","jpoBj","jlBj","jgeBj","jleBj","jgBj",
  59. --8x
  60. "arith!Bmi","arith!Vmi","arith!Bmi","arith!Vms",
  61. "testBmr","testVmr","xchgBrm","xchgVrm",
  62. "movBmr","movVmr","movBrm","movVrm",
  63. "movVmg","leaVrm","movWgm","popUm",
  64. --9x
  65. "nop*xchgVaR|pause|xchgWaR|repne nop","xchgVaR","xchgVaR","xchgVaR",
  66. "xchgVaR","xchgVaR","xchgVaR","xchgVaR",
  67. "sz*cbw,cwde,cdqe","sz*cwd,cdq,cqo","call farViw","wait",
  68. "sz*pushfw,pushf","sz*popfw,popf","sahf","lahf",
  69. --Ax
  70. "movBao","movVao","movBoa","movVoa",
  71. "movsb","movsVS","cmpsb","cmpsVS",
  72. "testBai","testVai","stosb","stosVS",
  73. "lodsb","lodsVS","scasb","scasVS",
  74. --Bx
  75. "movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi",
  76. "movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI",
  77. --Cx
  78. "shift!Bmu","shift!Vmu","retBw","ret","vex*3$lesVrm","vex*2$ldsVrm","movBmi","movVmi",
  79. "enterBwu","leave","retfBw","retf","int3","intBu","into","iretVS",
  80. --Dx
  81. "shift!Bm1","shift!Vm1","shift!Bmc","shift!Vmc","aamBu","aadBu","salc","xlatb",
  82. "fp*0","fp*1","fp*2","fp*3","fp*4","fp*5","fp*6","fp*7",
  83. --Ex
  84. "loopneBj","loopeBj","loopBj","sz*jcxzBj,jecxzBj,jrcxzBj",
  85. "inBau","inVau","outBua","outVua",
  86. "callVj","jmpVj","jmp farViw","jmpBj","inBad","inVad","outBda","outVda",
  87. --Fx
  88. "lock:","int1","repne:rep","rep:","hlt","cmc","testb!Bm","testv!Vm",
  89. "clc","stc","cli","sti","cld","std","incb!Bm","incd!Vm",
  90. }
  91. assert(#map_opc1_32 == 255)
  92. -- Map for 1st opcode byte in 64 bit mode (overrides only).
  93. local map_opc1_64 = setmetatable({
  94. [0x06]=false, [0x07]=false, [0x0e]=false,
  95. [0x16]=false, [0x17]=false, [0x1e]=false, [0x1f]=false,
  96. [0x27]=false, [0x2f]=false, [0x37]=false, [0x3f]=false,
  97. [0x60]=false, [0x61]=false, [0x62]=false, [0x63]="movsxdVrDmt", [0x67]="a32:",
  98. [0x40]="rex*", [0x41]="rex*b", [0x42]="rex*x", [0x43]="rex*xb",
  99. [0x44]="rex*r", [0x45]="rex*rb", [0x46]="rex*rx", [0x47]="rex*rxb",
  100. [0x48]="rex*w", [0x49]="rex*wb", [0x4a]="rex*wx", [0x4b]="rex*wxb",
  101. [0x4c]="rex*wr", [0x4d]="rex*wrb", [0x4e]="rex*wrx", [0x4f]="rex*wrxb",
  102. [0x82]=false, [0x9a]=false, [0xc4]="vex*3", [0xc5]="vex*2", [0xce]=false,
  103. [0xd4]=false, [0xd5]=false, [0xd6]=false, [0xea]=false,
  104. }, { __index = map_opc1_32 })
  105. -- Map for 2nd opcode byte (0F xx). True CISC hell. Hey, I told you.
  106. -- Prefix dependent MMX/SSE opcodes: (none)|rep|o16|repne, -|F3|66|F2
  107. local map_opc2 = {
  108. --0x
  109. [0]="sldt!Dmp","sgdt!Ump","larVrm","lslVrm",nil,"syscall","clts","sysret",
  110. "invd","wbinvd",nil,"ud1",nil,"$prefetch!Bm","femms","3dnowMrmu",
  111. --1x
  112. "movupsXrm|movssXrvm|movupdXrm|movsdXrvm",
  113. "movupsXmr|movssXmvr|movupdXmr|movsdXmvr",
  114. "movhlpsXrm$movlpsXrm|movsldupXrm|movlpdXrm|movddupXrm",
  115. "movlpsXmr||movlpdXmr",
  116. "unpcklpsXrvm||unpcklpdXrvm",
  117. "unpckhpsXrvm||unpckhpdXrvm",
  118. "movlhpsXrm$movhpsXrm|movshdupXrm|movhpdXrm",
  119. "movhpsXmr||movhpdXmr",
  120. "$prefetcht!Bm","hintnopVm","hintnopVm","hintnopVm",
  121. "hintnopVm","hintnopVm","endbr*hintnopVm","hintnopVm",
  122. --2x
  123. "movUmx$","movUmy$","movUxm$","movUym$","movUmz$",nil,"movUzm$",nil,
  124. "movapsXrm||movapdXrm",
  125. "movapsXmr||movapdXmr",
  126. "cvtpi2psXrMm|cvtsi2ssXrvVmt|cvtpi2pdXrMm|cvtsi2sdXrvVmt",
  127. "movntpsXmr|movntssXmr|movntpdXmr|movntsdXmr",
  128. "cvttps2piMrXm|cvttss2siVrXm|cvttpd2piMrXm|cvttsd2siVrXm",
  129. "cvtps2piMrXm|cvtss2siVrXm|cvtpd2piMrXm|cvtsd2siVrXm",
  130. "ucomissXrm||ucomisdXrm",
  131. "comissXrm||comisdXrm",
  132. --3x
  133. "wrmsr","rdtsc","rdmsr","rdpmc","sysenter","sysexit",nil,"getsec",
  134. "opc3*38",nil,"opc3*3a",nil,nil,nil,nil,nil,
  135. --4x
  136. "cmovoVrm","cmovnoVrm","cmovbVrm","cmovnbVrm",
  137. "cmovzVrm","cmovnzVrm","cmovbeVrm","cmovaVrm",
  138. "cmovsVrm","cmovnsVrm","cmovpeVrm","cmovpoVrm",
  139. "cmovlVrm","cmovgeVrm","cmovleVrm","cmovgVrm",
  140. --5x
  141. "movmskpsVrXm$||movmskpdVrXm$","sqrtpsXrm|sqrtssXrm|sqrtpdXrm|sqrtsdXrm",
  142. "rsqrtpsXrm|rsqrtssXrvm","rcppsXrm|rcpssXrvm",
  143. "andpsXrvm||andpdXrvm","andnpsXrvm||andnpdXrvm",
  144. "orpsXrvm||orpdXrvm","xorpsXrvm||xorpdXrvm",
  145. "addpsXrvm|addssXrvm|addpdXrvm|addsdXrvm","mulpsXrvm|mulssXrvm|mulpdXrvm|mulsdXrvm",
  146. "cvtps2pdXrm|cvtss2sdXrvm|cvtpd2psXrm|cvtsd2ssXrvm",
  147. "cvtdq2psXrm|cvttps2dqXrm|cvtps2dqXrm",
  148. "subpsXrvm|subssXrvm|subpdXrvm|subsdXrvm","minpsXrvm|minssXrvm|minpdXrvm|minsdXrvm",
  149. "divpsXrvm|divssXrvm|divpdXrvm|divsdXrvm","maxpsXrvm|maxssXrvm|maxpdXrvm|maxsdXrvm",
  150. --6x
  151. "punpcklbwPrvm","punpcklwdPrvm","punpckldqPrvm","packsswbPrvm",
  152. "pcmpgtbPrvm","pcmpgtwPrvm","pcmpgtdPrvm","packuswbPrvm",
  153. "punpckhbwPrvm","punpckhwdPrvm","punpckhdqPrvm","packssdwPrvm",
  154. "||punpcklqdqXrvm","||punpckhqdqXrvm",
  155. "movPrVSm","movqMrm|movdquXrm|movdqaXrm",
  156. --7x
  157. "pshufwMrmu|pshufhwXrmu|pshufdXrmu|pshuflwXrmu","pshiftw!Pvmu",
  158. "pshiftd!Pvmu","pshiftq!Mvmu||pshiftdq!Xvmu",
  159. "pcmpeqbPrvm","pcmpeqwPrvm","pcmpeqdPrvm","emms*|",
  160. "vmreadUmr||extrqXmuu$|insertqXrmuu$","vmwriteUrm||extrqXrm$|insertqXrm$",
  161. nil,nil,
  162. "||haddpdXrvm|haddpsXrvm","||hsubpdXrvm|hsubpsXrvm",
  163. "movVSmMr|movqXrm|movVSmXr","movqMmr|movdquXmr|movdqaXmr",
  164. --8x
  165. "joVj","jnoVj","jbVj","jnbVj","jzVj","jnzVj","jbeVj","jaVj",
  166. "jsVj","jnsVj","jpeVj","jpoVj","jlVj","jgeVj","jleVj","jgVj",
  167. --9x
  168. "setoBm","setnoBm","setbBm","setnbBm","setzBm","setnzBm","setbeBm","setaBm",
  169. "setsBm","setnsBm","setpeBm","setpoBm","setlBm","setgeBm","setleBm","setgBm",
  170. --Ax
  171. "push fs","pop fs","cpuid","btVmr","shldVmru","shldVmrc",nil,nil,
  172. "push gs","pop gs","rsm","btsVmr","shrdVmru","shrdVmrc","fxsave!Dmp","imulVrm",
  173. --Bx
  174. "cmpxchgBmr","cmpxchgVmr","$lssVrm","btrVmr",
  175. "$lfsVrm","$lgsVrm","movzxVrBmt","movzxVrWmt",
  176. "|popcntVrm","ud2Dp","bt!Vmu","btcVmr",
  177. "bsfVrm","bsrVrm|lzcntVrm|bsrWrm","movsxVrBmt","movsxVrWmt",
  178. --Cx
  179. "xaddBmr","xaddVmr",
  180. "cmppsXrvmu|cmpssXrvmu|cmppdXrvmu|cmpsdXrvmu","$movntiVmr|",
  181. "pinsrwPrvWmu","pextrwDrPmu",
  182. "shufpsXrvmu||shufpdXrvmu","$cmpxchg!Qmp",
  183. "bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR",
  184. --Dx
  185. "||addsubpdXrvm|addsubpsXrvm","psrlwPrvm","psrldPrvm","psrlqPrvm",
  186. "paddqPrvm","pmullwPrvm",
  187. "|movq2dqXrMm|movqXmr|movdq2qMrXm$","pmovmskbVrMm||pmovmskbVrXm",
  188. "psubusbPrvm","psubuswPrvm","pminubPrvm","pandPrvm",
  189. "paddusbPrvm","padduswPrvm","pmaxubPrvm","pandnPrvm",
  190. --Ex
  191. "pavgbPrvm","psrawPrvm","psradPrvm","pavgwPrvm",
  192. "pmulhuwPrvm","pmulhwPrvm",
  193. "|cvtdq2pdXrm|cvttpd2dqXrm|cvtpd2dqXrm","$movntqMmr||$movntdqXmr",
  194. "psubsbPrvm","psubswPrvm","pminswPrvm","porPrvm",
  195. "paddsbPrvm","paddswPrvm","pmaxswPrvm","pxorPrvm",
  196. --Fx
  197. "|||lddquXrm","psllwPrvm","pslldPrvm","psllqPrvm",
  198. "pmuludqPrvm","pmaddwdPrvm","psadbwPrvm","maskmovqMrm||maskmovdquXrm$",
  199. "psubbPrvm","psubwPrvm","psubdPrvm","psubqPrvm",
  200. "paddbPrvm","paddwPrvm","padddPrvm","ud",
  201. }
  202. assert(map_opc2[255] == "ud")
  203. -- Map for three-byte opcodes. Can't wait for their next invention.
  204. local map_opc3 = {
  205. ["38"] = { -- [66] 0f 38 xx
  206. --0x
  207. [0]="pshufbPrvm","phaddwPrvm","phadddPrvm","phaddswPrvm",
  208. "pmaddubswPrvm","phsubwPrvm","phsubdPrvm","phsubswPrvm",
  209. "psignbPrvm","psignwPrvm","psigndPrvm","pmulhrswPrvm",
  210. "||permilpsXrvm","||permilpdXrvm",nil,nil,
  211. --1x
  212. "||pblendvbXrma",nil,nil,nil,
  213. "||blendvpsXrma","||blendvpdXrma","||permpsXrvm","||ptestXrm",
  214. "||broadcastssXrm","||broadcastsdXrm","||broadcastf128XrlXm",nil,
  215. "pabsbPrm","pabswPrm","pabsdPrm",nil,
  216. --2x
  217. "||pmovsxbwXrm","||pmovsxbdXrm","||pmovsxbqXrm","||pmovsxwdXrm",
  218. "||pmovsxwqXrm","||pmovsxdqXrm",nil,nil,
  219. "||pmuldqXrvm","||pcmpeqqXrvm","||$movntdqaXrm","||packusdwXrvm",
  220. "||maskmovpsXrvm","||maskmovpdXrvm","||maskmovpsXmvr","||maskmovpdXmvr",
  221. --3x
  222. "||pmovzxbwXrm","||pmovzxbdXrm","||pmovzxbqXrm","||pmovzxwdXrm",
  223. "||pmovzxwqXrm","||pmovzxdqXrm","||permdXrvm","||pcmpgtqXrvm",
  224. "||pminsbXrvm","||pminsdXrvm","||pminuwXrvm","||pminudXrvm",
  225. "||pmaxsbXrvm","||pmaxsdXrvm","||pmaxuwXrvm","||pmaxudXrvm",
  226. --4x
  227. "||pmulddXrvm","||phminposuwXrm",nil,nil,
  228. nil,"||psrlvVSXrvm","||psravdXrvm","||psllvVSXrvm",
  229. --5x
  230. [0x58] = "||pbroadcastdXrlXm",[0x59] = "||pbroadcastqXrlXm",
  231. [0x5a] = "||broadcasti128XrlXm",
  232. --7x
  233. [0x78] = "||pbroadcastbXrlXm",[0x79] = "||pbroadcastwXrlXm",
  234. --8x
  235. [0x8c] = "||pmaskmovXrvVSm",
  236. [0x8e] = "||pmaskmovVSmXvr",
  237. --9x
  238. [0x96] = "||fmaddsub132pHXrvm",[0x97] = "||fmsubadd132pHXrvm",
  239. [0x98] = "||fmadd132pHXrvm",[0x99] = "||fmadd132sHXrvm",
  240. [0x9a] = "||fmsub132pHXrvm",[0x9b] = "||fmsub132sHXrvm",
  241. [0x9c] = "||fnmadd132pHXrvm",[0x9d] = "||fnmadd132sHXrvm",
  242. [0x9e] = "||fnmsub132pHXrvm",[0x9f] = "||fnmsub132sHXrvm",
  243. --Ax
  244. [0xa6] = "||fmaddsub213pHXrvm",[0xa7] = "||fmsubadd213pHXrvm",
  245. [0xa8] = "||fmadd213pHXrvm",[0xa9] = "||fmadd213sHXrvm",
  246. [0xaa] = "||fmsub213pHXrvm",[0xab] = "||fmsub213sHXrvm",
  247. [0xac] = "||fnmadd213pHXrvm",[0xad] = "||fnmadd213sHXrvm",
  248. [0xae] = "||fnmsub213pHXrvm",[0xaf] = "||fnmsub213sHXrvm",
  249. --Bx
  250. [0xb6] = "||fmaddsub231pHXrvm",[0xb7] = "||fmsubadd231pHXrvm",
  251. [0xb8] = "||fmadd231pHXrvm",[0xb9] = "||fmadd231sHXrvm",
  252. [0xba] = "||fmsub231pHXrvm",[0xbb] = "||fmsub231sHXrvm",
  253. [0xbc] = "||fnmadd231pHXrvm",[0xbd] = "||fnmadd231sHXrvm",
  254. [0xbe] = "||fnmsub231pHXrvm",[0xbf] = "||fnmsub231sHXrvm",
  255. --Dx
  256. [0xdc] = "||aesencXrvm", [0xdd] = "||aesenclastXrvm",
  257. [0xde] = "||aesdecXrvm", [0xdf] = "||aesdeclastXrvm",
  258. --Fx
  259. [0xf0] = "|||crc32TrBmt",[0xf1] = "|||crc32TrVmt",
  260. [0xf7] = "| sarxVrmv| shlxVrmv| shrxVrmv",
  261. },
  262. ["3a"] = { -- [66] 0f 3a xx
  263. --0x
  264. [0x00]="||permqXrmu","||permpdXrmu","||pblenddXrvmu",nil,
  265. "||permilpsXrmu","||permilpdXrmu","||perm2f128Xrvmu",nil,
  266. "||roundpsXrmu","||roundpdXrmu","||roundssXrvmu","||roundsdXrvmu",
  267. "||blendpsXrvmu","||blendpdXrvmu","||pblendwXrvmu","palignrPrvmu",
  268. --1x
  269. nil,nil,nil,nil,
  270. "||pextrbVmXru","||pextrwVmXru","||pextrVmSXru","||extractpsVmXru",
  271. "||insertf128XrvlXmu","||extractf128XlXmYru",nil,nil,
  272. nil,nil,nil,nil,
  273. --2x
  274. "||pinsrbXrvVmu","||insertpsXrvmu","||pinsrXrvVmuS",nil,
  275. --3x
  276. [0x38] = "||inserti128Xrvmu",[0x39] = "||extracti128XlXmYru",
  277. --4x
  278. [0x40] = "||dppsXrvmu",
  279. [0x41] = "||dppdXrvmu",
  280. [0x42] = "||mpsadbwXrvmu",
  281. [0x44] = "||pclmulqdqXrvmu",
  282. [0x46] = "||perm2i128Xrvmu",
  283. [0x4a] = "||blendvpsXrvmb",[0x4b] = "||blendvpdXrvmb",
  284. [0x4c] = "||pblendvbXrvmb",
  285. --6x
  286. [0x60] = "||pcmpestrmXrmu",[0x61] = "||pcmpestriXrmu",
  287. [0x62] = "||pcmpistrmXrmu",[0x63] = "||pcmpistriXrmu",
  288. [0xdf] = "||aeskeygenassistXrmu",
  289. --Fx
  290. [0xf0] = "||| rorxVrmu",
  291. },
  292. }
  293. -- Map for VMX/SVM opcodes 0F 01 C0-FF (sgdt group with register operands).
  294. local map_opcvm = {
  295. [0xc1]="vmcall",[0xc2]="vmlaunch",[0xc3]="vmresume",[0xc4]="vmxoff",
  296. [0xc8]="monitor",[0xc9]="mwait",
  297. [0xd8]="vmrun",[0xd9]="vmmcall",[0xda]="vmload",[0xdb]="vmsave",
  298. [0xdc]="stgi",[0xdd]="clgi",[0xde]="skinit",[0xdf]="invlpga",
  299. [0xf8]="swapgs",[0xf9]="rdtscp",
  300. }
  301. -- Map for FP opcodes. And you thought stack machines are simple?
  302. local map_opcfp = {
  303. -- D8-DF 00-BF: opcodes with a memory operand.
  304. -- D8
  305. [0]="faddFm","fmulFm","fcomFm","fcompFm","fsubFm","fsubrFm","fdivFm","fdivrFm",
  306. "fldFm",nil,"fstFm","fstpFm","fldenvVm","fldcwWm","fnstenvVm","fnstcwWm",
  307. -- DA
  308. "fiaddDm","fimulDm","ficomDm","ficompDm",
  309. "fisubDm","fisubrDm","fidivDm","fidivrDm",
  310. -- DB
  311. "fildDm","fisttpDm","fistDm","fistpDm",nil,"fld twordFmp",nil,"fstp twordFmp",
  312. -- DC
  313. "faddGm","fmulGm","fcomGm","fcompGm","fsubGm","fsubrGm","fdivGm","fdivrGm",
  314. -- DD
  315. "fldGm","fisttpQm","fstGm","fstpGm","frstorDmp",nil,"fnsaveDmp","fnstswWm",
  316. -- DE
  317. "fiaddWm","fimulWm","ficomWm","ficompWm",
  318. "fisubWm","fisubrWm","fidivWm","fidivrWm",
  319. -- DF
  320. "fildWm","fisttpWm","fistWm","fistpWm",
  321. "fbld twordFmp","fildQm","fbstp twordFmp","fistpQm",
  322. -- xx C0-FF: opcodes with a pseudo-register operand.
  323. -- D8
  324. "faddFf","fmulFf","fcomFf","fcompFf","fsubFf","fsubrFf","fdivFf","fdivrFf",
  325. -- D9
  326. "fldFf","fxchFf",{"fnop"},nil,
  327. {"fchs","fabs",nil,nil,"ftst","fxam"},
  328. {"fld1","fldl2t","fldl2e","fldpi","fldlg2","fldln2","fldz"},
  329. {"f2xm1","fyl2x","fptan","fpatan","fxtract","fprem1","fdecstp","fincstp"},
  330. {"fprem","fyl2xp1","fsqrt","fsincos","frndint","fscale","fsin","fcos"},
  331. -- DA
  332. "fcmovbFf","fcmoveFf","fcmovbeFf","fcmovuFf",nil,{nil,"fucompp"},nil,nil,
  333. -- DB
  334. "fcmovnbFf","fcmovneFf","fcmovnbeFf","fcmovnuFf",
  335. {nil,nil,"fnclex","fninit"},"fucomiFf","fcomiFf",nil,
  336. -- DC
  337. "fadd toFf","fmul toFf",nil,nil,
  338. "fsub toFf","fsubr toFf","fdivr toFf","fdiv toFf",
  339. -- DD
  340. "ffreeFf",nil,"fstFf","fstpFf","fucomFf","fucompFf",nil,nil,
  341. -- DE
  342. "faddpFf","fmulpFf",nil,{nil,"fcompp"},
  343. "fsubrpFf","fsubpFf","fdivrpFf","fdivpFf",
  344. -- DF
  345. nil,nil,nil,nil,{"fnstsw ax"},"fucomipFf","fcomipFf",nil,
  346. }
  347. assert(map_opcfp[126] == "fcomipFf")
  348. -- Map for opcode groups. The subkey is sp from the ModRM byte.
  349. local map_opcgroup = {
  350. arith = { "add", "or", "adc", "sbb", "and", "sub", "xor", "cmp" },
  351. shift = { "rol", "ror", "rcl", "rcr", "shl", "shr", "sal", "sar" },
  352. testb = { "testBmi", "testBmi", "not", "neg", "mul", "imul", "div", "idiv" },
  353. testv = { "testVmi", "testVmi", "not", "neg", "mul", "imul", "div", "idiv" },
  354. incb = { "inc", "dec" },
  355. incd = { "inc", "dec", "callUmp", "$call farDmp",
  356. "jmpUmp", "$jmp farDmp", "pushUm" },
  357. sldt = { "sldt", "str", "lldt", "ltr", "verr", "verw" },
  358. sgdt = { "vm*$sgdt", "vm*$sidt", "$lgdt", "vm*$lidt",
  359. "smsw", nil, "lmsw", "vm*$invlpg" },
  360. bt = { nil, nil, nil, nil, "bt", "bts", "btr", "btc" },
  361. cmpxchg = { nil, "sz*,cmpxchg8bQmp,cmpxchg16bXmp", nil, nil,
  362. nil, nil, "vmptrld|vmxon|vmclear", "vmptrst" },
  363. pshiftw = { nil, nil, "psrlw", nil, "psraw", nil, "psllw" },
  364. pshiftd = { nil, nil, "psrld", nil, "psrad", nil, "pslld" },
  365. pshiftq = { nil, nil, "psrlq", nil, nil, nil, "psllq" },
  366. pshiftdq = { nil, nil, "psrlq", "psrldq", nil, nil, "psllq", "pslldq" },
  367. fxsave = { "$fxsave", "$fxrstor", "$ldmxcsr", "$stmxcsr",
  368. nil, "lfenceDp$", "mfenceDp$", "sfenceDp$clflush" },
  369. prefetch = { "prefetch", "prefetchw" },
  370. prefetcht = { "prefetchnta", "prefetcht0", "prefetcht1", "prefetcht2" },
  371. }
  372. ------------------------------------------------------------------------------
  373. -- Maps for register names.
  374. local map_regs = {
  375. B = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh",
  376. "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" },
  377. B64 = { "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
  378. "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" },
  379. W = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
  380. "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w" },
  381. D = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
  382. "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" },
  383. Q = { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
  384. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" },
  385. M = { "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
  386. "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7" }, -- No x64 ext!
  387. X = { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
  388. "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15" },
  389. Y = { "ymm0", "ymm1", "ymm2", "ymm3", "ymm4", "ymm5", "ymm6", "ymm7",
  390. "ymm8", "ymm9", "ymm10", "ymm11", "ymm12", "ymm13", "ymm14", "ymm15" },
  391. }
  392. local map_segregs = { "es", "cs", "ss", "ds", "fs", "gs", "segr6", "segr7" }
  393. -- Maps for size names.
  394. local map_sz2n = {
  395. B = 1, W = 2, D = 4, Q = 8, M = 8, X = 16, Y = 32,
  396. }
  397. local map_sz2prefix = {
  398. B = "byte", W = "word", D = "dword",
  399. Q = "qword",
  400. M = "qword", X = "xword", Y = "yword",
  401. F = "dword", G = "qword", -- No need for sizes/register names for these two.
  402. }
  403. ------------------------------------------------------------------------------
  404. -- Output a nicely formatted line with an opcode and operands.
  405. local function putop(ctx, text, operands)
  406. local code, pos, hex = ctx.code, ctx.pos, ""
  407. local hmax = ctx.hexdump
  408. if hmax > 0 then
  409. for i=ctx.start,pos-1 do
  410. hex = hex..format("%02X", byte(code, i, i))
  411. end
  412. if #hex > hmax then hex = sub(hex, 1, hmax)..". "
  413. else hex = hex..rep(" ", hmax-#hex+2) end
  414. end
  415. if operands then text = text.." "..operands end
  416. if ctx.o16 then text = "o16 "..text; ctx.o16 = false end
  417. if ctx.a32 then text = "a32 "..text; ctx.a32 = false end
  418. if ctx.rep then text = ctx.rep.." "..text; ctx.rep = false end
  419. if ctx.rex then
  420. local t = (ctx.rexw and "w" or "")..(ctx.rexr and "r" or "")..
  421. (ctx.rexx and "x" or "")..(ctx.rexb and "b" or "")..
  422. (ctx.vexl and "l" or "")
  423. if ctx.vexv and ctx.vexv ~= 0 then t = t.."v"..ctx.vexv end
  424. if t ~= "" then text = ctx.rex.."."..t.." "..gsub(text, "^ ", "")
  425. elseif ctx.rex == "vex" then text = gsub("v"..text, "^v ", "") end
  426. ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false
  427. ctx.rex = false; ctx.vexl = false; ctx.vexv = false
  428. end
  429. if ctx.seg then
  430. local text2, n = gsub(text, "%[", "["..ctx.seg..":")
  431. if n == 0 then text = ctx.seg.." "..text else text = text2 end
  432. ctx.seg = false
  433. end
  434. if ctx.lock then text = "lock "..text; ctx.lock = false end
  435. local imm = ctx.imm
  436. if imm then
  437. local sym = ctx.symtab[imm]
  438. if sym then text = text.."\t->"..sym end
  439. end
  440. ctx.out(format("%08x %s%s\n", ctx.addr+ctx.start, hex, text))
  441. ctx.mrm = false
  442. ctx.vexv = false
  443. ctx.start = pos
  444. ctx.imm = nil
  445. end
  446. -- Clear all prefix flags.
  447. local function clearprefixes(ctx)
  448. ctx.o16 = false; ctx.seg = false; ctx.lock = false; ctx.rep = false
  449. ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false
  450. ctx.rex = false; ctx.a32 = false; ctx.vexl = false
  451. end
  452. -- Fallback for incomplete opcodes at the end.
  453. local function incomplete(ctx)
  454. ctx.pos = ctx.stop+1
  455. clearprefixes(ctx)
  456. return putop(ctx, "(incomplete)")
  457. end
  458. -- Fallback for unknown opcodes.
  459. local function unknown(ctx)
  460. clearprefixes(ctx)
  461. return putop(ctx, "(unknown)")
  462. end
  463. -- Return an immediate of the specified size.
  464. local function getimm(ctx, pos, n)
  465. if pos+n-1 > ctx.stop then return incomplete(ctx) end
  466. local code = ctx.code
  467. if n == 1 then
  468. local b1 = byte(code, pos, pos)
  469. return b1
  470. elseif n == 2 then
  471. local b1, b2 = byte(code, pos, pos+1)
  472. return b1+b2*256
  473. else
  474. local b1, b2, b3, b4 = byte(code, pos, pos+3)
  475. local imm = b1+b2*256+b3*65536+b4*16777216
  476. ctx.imm = imm
  477. return imm
  478. end
  479. end
  480. -- Process pattern string and generate the operands.
  481. local function putpat(ctx, name, pat)
  482. local operands, regs, sz, mode, sp, rm, sc, rx, sdisp
  483. local code, pos, stop, vexl = ctx.code, ctx.pos, ctx.stop, ctx.vexl
  484. -- Chars used: 1DFGHIMPQRSTUVWXYabcdfgijlmoprstuvwxyz
  485. for p in gmatch(pat, ".") do
  486. local x = nil
  487. if p == "V" or p == "U" then
  488. if ctx.rexw then sz = "Q"; ctx.rexw = false
  489. elseif ctx.o16 then sz = "W"; ctx.o16 = false
  490. elseif p == "U" and ctx.x64 then sz = "Q"
  491. else sz = "D" end
  492. regs = map_regs[sz]
  493. elseif p == "T" then
  494. if ctx.rexw then sz = "Q"; ctx.rexw = false else sz = "D" end
  495. regs = map_regs[sz]
  496. elseif p == "B" then
  497. sz = "B"
  498. regs = ctx.rex and map_regs.B64 or map_regs.B
  499. elseif match(p, "[WDQMXYFG]") then
  500. sz = p
  501. if sz == "X" and vexl then sz = "Y"; ctx.vexl = false end
  502. regs = map_regs[sz]
  503. elseif p == "P" then
  504. sz = ctx.o16 and "X" or "M"; ctx.o16 = false
  505. if sz == "X" and vexl then sz = "Y"; ctx.vexl = false end
  506. regs = map_regs[sz]
  507. elseif p == "H" then
  508. name = name..(ctx.rexw and "d" or "s")
  509. ctx.rexw = false
  510. elseif p == "S" then
  511. name = name..lower(sz)
  512. elseif p == "s" then
  513. local imm = getimm(ctx, pos, 1); if not imm then return end
  514. x = imm <= 127 and format("+0x%02x", imm)
  515. or format("-0x%02x", 256-imm)
  516. pos = pos+1
  517. elseif p == "u" then
  518. local imm = getimm(ctx, pos, 1); if not imm then return end
  519. x = format("0x%02x", imm)
  520. pos = pos+1
  521. elseif p == "b" then
  522. local imm = getimm(ctx, pos, 1); if not imm then return end
  523. x = regs[imm/16+1]
  524. pos = pos+1
  525. elseif p == "w" then
  526. local imm = getimm(ctx, pos, 2); if not imm then return end
  527. x = format("0x%x", imm)
  528. pos = pos+2
  529. elseif p == "o" then -- [offset]
  530. if ctx.x64 then
  531. local imm1 = getimm(ctx, pos, 4); if not imm1 then return end
  532. local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end
  533. x = format("[0x%08x%08x]", imm2, imm1)
  534. pos = pos+8
  535. else
  536. local imm = getimm(ctx, pos, 4); if not imm then return end
  537. x = format("[0x%08x]", imm)
  538. pos = pos+4
  539. end
  540. elseif p == "i" or p == "I" then
  541. local n = map_sz2n[sz]
  542. if n == 8 and ctx.x64 and p == "I" then
  543. local imm1 = getimm(ctx, pos, 4); if not imm1 then return end
  544. local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end
  545. x = format("0x%08x%08x", imm2, imm1)
  546. else
  547. if n == 8 then n = 4 end
  548. local imm = getimm(ctx, pos, n); if not imm then return end
  549. if sz == "Q" and (imm < 0 or imm > 0x7fffffff) then
  550. imm = (0xffffffff+1)-imm
  551. x = format(imm > 65535 and "-0x%08x" or "-0x%x", imm)
  552. else
  553. x = format(imm > 65535 and "0x%08x" or "0x%x", imm)
  554. end
  555. end
  556. pos = pos+n
  557. elseif p == "j" then
  558. local n = map_sz2n[sz]
  559. if n == 8 then n = 4 end
  560. local imm = getimm(ctx, pos, n); if not imm then return end
  561. if sz == "B" and imm > 127 then imm = imm-256
  562. elseif imm > 2147483647 then imm = imm-4294967296 end
  563. pos = pos+n
  564. imm = imm + pos + ctx.addr
  565. if imm > 4294967295 and not ctx.x64 then imm = imm-4294967296 end
  566. ctx.imm = imm
  567. if sz == "W" then
  568. x = format("word 0x%04x", imm%65536)
  569. elseif ctx.x64 then
  570. local lo = imm % 0x1000000
  571. x = format("0x%02x%06x", (imm-lo) / 0x1000000, lo)
  572. else
  573. x = "0x"..tohex(imm)
  574. end
  575. elseif p == "R" then
  576. local r = byte(code, pos-1, pos-1)%8
  577. if ctx.rexb then r = r + 8; ctx.rexb = false end
  578. x = regs[r+1]
  579. elseif p == "a" then x = regs[1]
  580. elseif p == "c" then x = "cl"
  581. elseif p == "d" then x = "dx"
  582. elseif p == "1" then x = "1"
  583. else
  584. if not mode then
  585. mode = ctx.mrm
  586. if not mode then
  587. if pos > stop then return incomplete(ctx) end
  588. mode = byte(code, pos, pos)
  589. pos = pos+1
  590. end
  591. rm = mode%8; mode = (mode-rm)/8
  592. sp = mode%8; mode = (mode-sp)/8
  593. sdisp = ""
  594. if mode < 3 then
  595. if rm == 4 then
  596. if pos > stop then return incomplete(ctx) end
  597. sc = byte(code, pos, pos)
  598. pos = pos+1
  599. rm = sc%8; sc = (sc-rm)/8
  600. rx = sc%8; sc = (sc-rx)/8
  601. if ctx.rexx then rx = rx + 8; ctx.rexx = false end
  602. if rx == 4 then rx = nil end
  603. end
  604. if mode > 0 or rm == 5 then
  605. local dsz = mode
  606. if dsz ~= 1 then dsz = 4 end
  607. local disp = getimm(ctx, pos, dsz); if not disp then return end
  608. if mode == 0 then rm = nil end
  609. if rm or rx or (not sc and ctx.x64 and not ctx.a32) then
  610. if dsz == 1 and disp > 127 then
  611. sdisp = format("-0x%x", 256-disp)
  612. elseif disp >= 0 and disp <= 0x7fffffff then
  613. sdisp = format("+0x%x", disp)
  614. else
  615. sdisp = format("-0x%x", (0xffffffff+1)-disp)
  616. end
  617. else
  618. sdisp = format(ctx.x64 and not ctx.a32 and
  619. not (disp >= 0 and disp <= 0x7fffffff)
  620. and "0xffffffff%08x" or "0x%08x", disp)
  621. end
  622. pos = pos+dsz
  623. end
  624. end
  625. if rm and ctx.rexb then rm = rm + 8; ctx.rexb = false end
  626. if ctx.rexr then sp = sp + 8; ctx.rexr = false end
  627. end
  628. if p == "m" then
  629. if mode == 3 then x = regs[rm+1]
  630. else
  631. local aregs = ctx.a32 and map_regs.D or ctx.aregs
  632. local srm, srx = "", ""
  633. if rm then srm = aregs[rm+1]
  634. elseif not sc and ctx.x64 and not ctx.a32 then srm = "rip" end
  635. ctx.a32 = false
  636. if rx then
  637. if rm then srm = srm.."+" end
  638. srx = aregs[rx+1]
  639. if sc > 0 then srx = srx.."*"..(2^sc) end
  640. end
  641. x = format("[%s%s%s]", srm, srx, sdisp)
  642. end
  643. if mode < 3 and
  644. (not match(pat, "[aRrgp]") or match(pat, "t")) then -- Yuck.
  645. x = map_sz2prefix[sz].." "..x
  646. end
  647. elseif p == "r" then x = regs[sp+1]
  648. elseif p == "g" then x = map_segregs[sp+1]
  649. elseif p == "p" then -- Suppress prefix.
  650. elseif p == "f" then x = "st"..rm
  651. elseif p == "x" then
  652. if sp == 0 and ctx.lock and not ctx.x64 then
  653. x = "CR8"; ctx.lock = false
  654. else
  655. x = "CR"..sp
  656. end
  657. elseif p == "v" then
  658. if ctx.vexv then
  659. x = regs[ctx.vexv+1]; ctx.vexv = false
  660. end
  661. elseif p == "y" then x = "DR"..sp
  662. elseif p == "z" then x = "TR"..sp
  663. elseif p == "l" then vexl = false
  664. elseif p == "t" then
  665. else
  666. error("bad pattern `"..pat.."'")
  667. end
  668. end
  669. if x then operands = operands and operands..", "..x or x end
  670. end
  671. ctx.pos = pos
  672. return putop(ctx, name, operands)
  673. end
  674. -- Forward declaration.
  675. local map_act
  676. -- Fetch and cache MRM byte.
  677. local function getmrm(ctx)
  678. local mrm = ctx.mrm
  679. if not mrm then
  680. local pos = ctx.pos
  681. if pos > ctx.stop then return nil end
  682. mrm = byte(ctx.code, pos, pos)
  683. ctx.pos = pos+1
  684. ctx.mrm = mrm
  685. end
  686. return mrm
  687. end
  688. -- Dispatch to handler depending on pattern.
  689. local function dispatch(ctx, opat, patgrp)
  690. if not opat then return unknown(ctx) end
  691. if match(opat, "%|") then -- MMX/SSE variants depending on prefix.
  692. local p
  693. if ctx.rep then
  694. p = ctx.rep=="rep" and "%|([^%|]*)" or "%|[^%|]*%|[^%|]*%|([^%|]*)"
  695. ctx.rep = false
  696. elseif ctx.o16 then p = "%|[^%|]*%|([^%|]*)"; ctx.o16 = false
  697. else p = "^[^%|]*" end
  698. opat = match(opat, p)
  699. if not opat then return unknown(ctx) end
  700. -- ctx.rep = false; ctx.o16 = false
  701. --XXX fails for 66 f2 0f 38 f1 06 crc32 eax,WORD PTR [esi]
  702. --XXX remove in branches?
  703. end
  704. if match(opat, "%$") then -- reg$mem variants.
  705. local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end
  706. opat = match(opat, mrm >= 192 and "^[^%$]*" or "%$(.*)")
  707. if opat == "" then return unknown(ctx) end
  708. end
  709. if opat == "" then return unknown(ctx) end
  710. local name, pat = match(opat, "^([a-z0-9 ]*)(.*)")
  711. if pat == "" and patgrp then pat = patgrp end
  712. return map_act[sub(pat, 1, 1)](ctx, name, pat)
  713. end
  714. -- Get a pattern from an opcode map and dispatch to handler.
  715. local function dispatchmap(ctx, opcmap)
  716. local pos = ctx.pos
  717. local opat = opcmap[byte(ctx.code, pos, pos)]
  718. pos = pos + 1
  719. ctx.pos = pos
  720. return dispatch(ctx, opat)
  721. end
  722. -- Map for action codes. The key is the first char after the name.
  723. map_act = {
  724. -- Simple opcodes without operands.
  725. [""] = function(ctx, name, pat)
  726. return putop(ctx, name)
  727. end,
  728. -- Operand size chars fall right through.
  729. B = putpat, W = putpat, D = putpat, Q = putpat,
  730. V = putpat, U = putpat, T = putpat,
  731. M = putpat, X = putpat, P = putpat,
  732. F = putpat, G = putpat, Y = putpat,
  733. H = putpat,
  734. -- Collect prefixes.
  735. [":"] = function(ctx, name, pat)
  736. ctx[pat == ":" and name or sub(pat, 2)] = name
  737. if ctx.pos - ctx.start > 5 then return unknown(ctx) end -- Limit #prefixes.
  738. end,
  739. -- Chain to special handler specified by name.
  740. ["*"] = function(ctx, name, pat)
  741. return map_act[name](ctx, name, sub(pat, 2))
  742. end,
  743. -- Use named subtable for opcode group.
  744. ["!"] = function(ctx, name, pat)
  745. local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end
  746. return dispatch(ctx, map_opcgroup[name][((mrm-(mrm%8))/8)%8+1], sub(pat, 2))
  747. end,
  748. -- o16,o32[,o64] variants.
  749. sz = function(ctx, name, pat)
  750. if ctx.o16 then ctx.o16 = false
  751. else
  752. pat = match(pat, ",(.*)")
  753. if ctx.rexw then
  754. local p = match(pat, ",(.*)")
  755. if p then pat = p; ctx.rexw = false end
  756. end
  757. end
  758. pat = match(pat, "^[^,]*")
  759. return dispatch(ctx, pat)
  760. end,
  761. -- Two-byte opcode dispatch.
  762. opc2 = function(ctx, name, pat)
  763. return dispatchmap(ctx, map_opc2)
  764. end,
  765. -- Three-byte opcode dispatch.
  766. opc3 = function(ctx, name, pat)
  767. return dispatchmap(ctx, map_opc3[pat])
  768. end,
  769. -- VMX/SVM dispatch.
  770. vm = function(ctx, name, pat)
  771. return dispatch(ctx, map_opcvm[ctx.mrm])
  772. end,
  773. -- Special NOP for endbr64/endbr32.
  774. endbr = function(ctx, name, pat)
  775. if ctx.rep then
  776. local pos = ctx.pos
  777. local b = byte(ctx.code, pos)
  778. local text
  779. if b == 0xfa then text = "endbr64"
  780. elseif b == 0xfb then text = "endbr64"
  781. end
  782. if text then
  783. ctx.pos = pos + 1
  784. ctx.rep = nil
  785. return putop(ctx, text)
  786. end
  787. end
  788. return dispatch(ctx, pat)
  789. end,
  790. -- Floating point opcode dispatch.
  791. fp = function(ctx, name, pat)
  792. local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end
  793. local rm = mrm%8
  794. local idx = pat*8 + ((mrm-rm)/8)%8
  795. if mrm >= 192 then idx = idx + 64 end
  796. local opat = map_opcfp[idx]
  797. if type(opat) == "table" then opat = opat[rm+1] end
  798. return dispatch(ctx, opat)
  799. end,
  800. -- REX prefix.
  801. rex = function(ctx, name, pat)
  802. if ctx.rex then return unknown(ctx) end -- Only 1 REX or VEX prefix allowed.
  803. for p in gmatch(pat, ".") do ctx["rex"..p] = true end
  804. ctx.rex = "rex"
  805. end,
  806. -- VEX prefix.
  807. vex = function(ctx, name, pat)
  808. if ctx.rex then return unknown(ctx) end -- Only 1 REX or VEX prefix allowed.
  809. ctx.rex = "vex"
  810. local pos = ctx.pos
  811. if ctx.mrm then
  812. ctx.mrm = nil
  813. pos = pos-1
  814. end
  815. local b = byte(ctx.code, pos, pos)
  816. if not b then return incomplete(ctx) end
  817. pos = pos+1
  818. if b < 128 then ctx.rexr = true end
  819. local m = 1
  820. if pat == "3" then
  821. m = b%32; b = (b-m)/32
  822. local nb = b%2; b = (b-nb)/2
  823. if nb == 0 then ctx.rexb = true end
  824. local nx = b%2
  825. if nx == 0 then ctx.rexx = true end
  826. b = byte(ctx.code, pos, pos)
  827. if not b then return incomplete(ctx) end
  828. pos = pos+1
  829. if b >= 128 then ctx.rexw = true end
  830. end
  831. ctx.pos = pos
  832. local map
  833. if m == 1 then map = map_opc2
  834. elseif m == 2 then map = map_opc3["38"]
  835. elseif m == 3 then map = map_opc3["3a"]
  836. else return unknown(ctx) end
  837. local p = b%4; b = (b-p)/4
  838. if p == 1 then ctx.o16 = "o16"
  839. elseif p == 2 then ctx.rep = "rep"
  840. elseif p == 3 then ctx.rep = "repne" end
  841. local l = b%2; b = (b-l)/2
  842. if l ~= 0 then ctx.vexl = true end
  843. ctx.vexv = (-1-b)%16
  844. return dispatchmap(ctx, map)
  845. end,
  846. -- Special case for nop with REX prefix.
  847. nop = function(ctx, name, pat)
  848. return dispatch(ctx, ctx.rex and pat or "nop")
  849. end,
  850. -- Special case for 0F 77.
  851. emms = function(ctx, name, pat)
  852. if ctx.rex ~= "vex" then
  853. return putop(ctx, "emms")
  854. elseif ctx.vexl then
  855. ctx.vexl = false
  856. return putop(ctx, "zeroall")
  857. else
  858. return putop(ctx, "zeroupper")
  859. end
  860. end,
  861. }
  862. ------------------------------------------------------------------------------
  863. -- Disassemble a block of code.
  864. local function disass_block(ctx, ofs, len)
  865. if not ofs then ofs = 0 end
  866. local stop = len and ofs+len or #ctx.code
  867. ofs = ofs + 1
  868. ctx.start = ofs
  869. ctx.pos = ofs
  870. ctx.stop = stop
  871. ctx.imm = nil
  872. ctx.mrm = false
  873. clearprefixes(ctx)
  874. while ctx.pos <= stop do dispatchmap(ctx, ctx.map1) end
  875. if ctx.pos ~= ctx.start then incomplete(ctx) end
  876. end
  877. -- Extended API: create a disassembler context. Then call ctx:disass(ofs, len).
  878. local function create(code, addr, out)
  879. local ctx = {}
  880. ctx.code = code
  881. ctx.addr = (addr or 0) - 1
  882. ctx.out = out or io.write
  883. ctx.symtab = {}
  884. ctx.disass = disass_block
  885. ctx.hexdump = 16
  886. ctx.x64 = false
  887. ctx.map1 = map_opc1_32
  888. ctx.aregs = map_regs.D
  889. return ctx
  890. end
  891. local function create64(code, addr, out)
  892. local ctx = create(code, addr, out)
  893. ctx.x64 = true
  894. ctx.map1 = map_opc1_64
  895. ctx.aregs = map_regs.Q
  896. return ctx
  897. end
  898. -- Simple API: disassemble code (a string) at address and output via out.
  899. local function disass(code, addr, out)
  900. create(code, addr, out):disass()
  901. end
  902. local function disass64(code, addr, out)
  903. create64(code, addr, out):disass()
  904. end
  905. -- Return register name for RID.
  906. local function regname(r)
  907. if r < 8 then return map_regs.D[r+1] end
  908. return map_regs.X[r-7]
  909. end
  910. local function regname64(r)
  911. if r < 16 then return map_regs.Q[r+1] end
  912. return map_regs.X[r-15]
  913. end
  914. -- Public module functions.
  915. return {
  916. create = create,
  917. create64 = create64,
  918. disass = disass,
  919. disass64 = disass64,
  920. regname = regname,
  921. regname64 = regname64
  922. }