1
0

disasm.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // Copyright (C) 2012 Zeex
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the "Software"),
  5. // to deal in the Software without restriction, including without limitation
  6. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. // and/or sell copies of the Software, and to permit persons to whom the
  8. // Software is furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. // DEALINGS IN THE SOFTWARE.
  20. #if defined DISASM_INC
  21. #endinput
  22. #endif
  23. #define DISASM_INC
  24. #include <string>
  25. #include <file>
  26. #include "amx_base"
  27. #include "amx_header"
  28. #include "amx_memory"
  29. #include "opcode"
  30. #define DISASM_MAX_PUBLIC_NAME 32
  31. #define DISASM_MAX_NATIVE_NAME 100
  32. enum DisasmContext {
  33. DisasmContext_start_ip,
  34. DisasmContext_end_ip,
  35. DisasmContext_nip,
  36. DisasmContext_cip,
  37. Opcode:DisasmContext_opcode,
  38. DisasmContext_insn[OpcodeInsnInfo]
  39. }
  40. enum DisasmResult {
  41. DISASM_DONE = 0,
  42. DISASM_NOP = 1,
  43. DISASM_OK = 2
  44. }
  45. static stock gCodBase;
  46. stock DisasmInit(ctx[DisasmContext], start = 0, end = 0) {
  47. new hdr[AMX_HDR];
  48. GetAmxHeader(hdr);
  49. gCodBase = GetAmxBaseAddress() + hdr[AMX_HDR_COD];
  50. new dat = hdr[AMX_HDR_DAT];
  51. new cod = hdr[AMX_HDR_COD];
  52. new code_base = cod - dat;
  53. start += code_base;
  54. ctx[DisasmContext_nip] = start;
  55. ctx[DisasmContext_cip] = start;
  56. ctx[DisasmContext_start_ip] = start;
  57. if (end != 0) {
  58. ctx[DisasmContext_end_ip] = code_base + end;
  59. } else {
  60. // code_base + (dat - cod)
  61. // = (cod - dat) + (dat - cod)
  62. // = cod - dat + dat - cod
  63. // = cod - cod + dat - dat
  64. // = 0
  65. ctx[DisasmContext_end_ip] = 0;
  66. }
  67. }
  68. stock bool:DisasmDecodeInsn(ctx[DisasmContext]) {
  69. new ip = ctx[DisasmContext_nip];
  70. if (ip >= 0) {
  71. return false;
  72. }
  73. new Opcode:opcode = UnrelocateOpcode(Opcode:ReadAmxMemory(ip));
  74. if (opcode <= OP_NONE || _:opcode >= NUM_OPCODES) {
  75. return false;
  76. }
  77. ctx[DisasmContext_cip] = ip;
  78. ctx[DisasmContext_opcode] = opcode;
  79. ctx[DisasmContext_insn] = GetOpcodeInstructionInformation(opcode);
  80. ip += 4;
  81. if (opcode == OP_CASETBL) {
  82. new n = ReadAmxMemory(ip);
  83. ip += 4;
  84. ip += (2 * n + 1) * 4;
  85. ctx[DisasmContext_insn][OpcodeInsnInfo_num_opers] = n;
  86. } else {
  87. ip += 4 * ctx[DisasmContext_insn][OpcodeInsnInfo_num_opers];
  88. }
  89. ctx[DisasmContext_nip] = ip;
  90. return true;
  91. }
  92. stock DisasmResult:DisasmNext(ctx[DisasmContext]) {
  93. // This function is just a slightly more consistent wrapper around
  94. // "DisasmDecodeInsn". I wanted to change that function, but that would
  95. // cause breaking changes. This returns non-zero while there is still data
  96. // being read, even if that data is not a valid opcode. For invalid code,
  97. // it skips the current cell and returns an invalid opcode. It also now
  98. // checks the end point correctly.
  99. if (ctx[DisasmContext_nip] >= ctx[DisasmContext_end_ip]) {
  100. return DISASM_DONE;
  101. } else if (DisasmDecodeInsn(ctx)) {
  102. return DISASM_OK;
  103. } else {
  104. ctx[DisasmContext_cip] = ctx[DisasmContext_nip],
  105. ctx[DisasmContext_nip] += 4,
  106. ctx[DisasmContext_opcode] = Opcode:NUM_OPCODES;
  107. return DISASM_NOP;
  108. }
  109. }
  110. stock Opcode:DisasmNextInsn(ctx[DisasmContext]) {
  111. if (DisasmDecodeInsn(ctx)) {
  112. return ctx[DisasmContext_opcode];
  113. }
  114. return OP_NONE;
  115. }
  116. stock Opcode:DisasmGetOpcode(ctx[DisasmContext]) {
  117. return ctx[DisasmContext_opcode];
  118. }
  119. stock DisasmGetOperand(ctx[DisasmContext], index = 0) {
  120. return ReadAmxMemory(ctx[DisasmContext_cip] + (index + 1) * 4);
  121. }
  122. stock DisasmGetNumOperands(ctx[DisasmContext]) {
  123. return ctx[DisasmContext_insn][OpcodeInsnInfo_num_opers];
  124. }
  125. stock bool:DisasmNeedReloc(ctx[DisasmContext]) {
  126. return ctx[DisasmContext_insn][OpcodeInsnInfo_needs_reloc];
  127. }
  128. stock DisasmReloc(addr) {
  129. return addr - gCodBase;
  130. }
  131. stock DisasmGetNextIp(ctx[DisasmContext]) {
  132. return ctx[DisasmContext_nip];
  133. }
  134. stock DisasmGetCurIp(ctx[DisasmContext]) {
  135. return ctx[DisasmContext_cip];
  136. }
  137. stock DisasmGetRemaining(ctx[DisasmContext]) {
  138. return ctx[DisasmContext_end_ip] - ctx[DisasmContext_nip];
  139. }
  140. stock DisasmGetInsnName(ctx[DisasmContext], name[], size = sizeof(name)) {
  141. name[0] = '\0';
  142. strcat(name, ctx[DisasmContext_insn][OpcodeInsnInfo_name], size);
  143. }
  144. stock DisasmGetOperandReloc(ctx[DisasmContext], index = 0) {
  145. new param = DisasmGetOperand(ctx, index);
  146. // Needs special code for dealing with "CASETBL", which has multiple
  147. // parameters - not all of them to be relocated. If the opcode is NOT that,
  148. // then check if it is any other opcode requiring relocation. This does
  149. // result in the odd pattern of having a triadic operator in a conditional,
  150. // but the alternative would be:
  151. //
  152. // if (ctx[DisasmContext_opcode == OP_CASETBL) {
  153. // if (index & 1) {
  154. // return param - base;
  155. // }
  156. // } else if (DisasmNeedReloc(ctx)) {
  157. // return param - base;
  158. // }
  159. // return param;
  160. //
  161. // Or:
  162. //
  163. // if ((ctx[DisasmContext_opcode == OP_CASETBL && (index & 1)) ||
  164. // (ctx[DisasmContext_opcode != OP_CASETBL && DisasmNeedReloc(ctx))) {
  165. // return param - base;
  166. // } else {
  167. // return param;
  168. // }
  169. //
  170. // I think the conditional ends up nicer in this rare case.
  171. if ((ctx[DisasmContext_opcode] == OP_CASETBL) ? (index & 1) : _:DisasmNeedReloc(ctx)) {
  172. return DisasmReloc(param);
  173. } else {
  174. return param;
  175. }
  176. }
  177. static stock ToHexStr(x) {
  178. new s[11];
  179. new i = 0;
  180. new j = 0;
  181. while (i < sizeof(s) && j < 8) {
  182. new n = x >> (7 - j) * 4 & 0xF;
  183. switch (n) {
  184. case 0x0..0x9:
  185. s[i] = n + '0';
  186. case 0xA..0xF:
  187. s[i] = n + 'a' - 0xA;
  188. }
  189. i++;
  190. j++;
  191. }
  192. return s;
  193. }
  194. static stock bool:IsPrintableAscii(c) {
  195. return 32 <= c <= 126;
  196. }
  197. static stock ToPrintableAscii(c) {
  198. return IsPrintableAscii(c) ? c : ' ';
  199. }
  200. stock DisasmWriteCode(File:file) {
  201. new ctx[DisasmContext];
  202. DisasmInit(ctx);
  203. new hdr[AMX_HDR];
  204. GetAmxHeader(hdr);
  205. new dat = hdr[AMX_HDR_DAT];
  206. new cod = hdr[AMX_HDR_COD];
  207. fwrite(file, "; CODE\n\n");
  208. while (DisasmGetNextIp(ctx) < ctx[DisasmContext_end_ip])
  209. {
  210. if (!DisasmDecodeInsn(ctx)) {
  211. new cip = DisasmGetNextIp(ctx);
  212. ctx[DisasmContext_nip] += 4;
  213. fwrite(file, ToHexStr(cip + dat - cod));
  214. fwrite(file, " ???? ");
  215. fwrite(file, ToHexStr(ReadAmxMemory(cip)));
  216. fwrite(file, "\n");
  217. continue;
  218. }
  219. new cip = DisasmGetCurIp(ctx);
  220. new Opcode:opcode = DisasmGetOpcode(ctx);
  221. if (opcode == OP_PROC) {
  222. fwrite(file, "\n");
  223. }
  224. new insn_name[OPCODE_MAX_INSN_NAME];
  225. DisasmGetInsnName(ctx, insn_name);
  226. fwrite(file, ToHexStr(cip + dat - cod));
  227. fwrite(file, " ");
  228. fwrite(file, insn_name);
  229. fwrite(file, " ");
  230. switch (opcode) {
  231. case OP_PROC: {
  232. new name[32];
  233. new address = cip + dat - cod;
  234. if (address == hdr[AMX_HDR_CIP]) {
  235. strcat(name, "main");
  236. } else {
  237. new index = GetPublicIndexFromAddress(address);
  238. if (index >= 0) {
  239. GetPublicNameFromIndex(index, name);
  240. }
  241. }
  242. if (strlen(name) != 0) {
  243. fwrite(file, "; ");
  244. fwrite(file, name);
  245. }
  246. }
  247. case OP_CASETBL: {
  248. new num = DisasmGetNumOperands(ctx);
  249. fwrite(file, ToHexStr(num));
  250. fwrite(file, " ");
  251. new rel_addr = DisasmGetOperand(ctx, 1) - gCodBase;
  252. fwrite(file, ToHexStr(rel_addr));
  253. for (new i = 1; i <= num; i++) {
  254. fwrite(file, "\n case ");
  255. new val = DisasmGetOperand(ctx, i * 2);
  256. fwrite(file, ToHexStr(val));
  257. fwrite(file, " ");
  258. rel_addr = DisasmGetOperand(ctx, i * 2 + 1) - gCodBase;
  259. fwrite(file, ToHexStr(rel_addr));
  260. }
  261. }
  262. case OP_CALL: {
  263. new name[DISASM_MAX_PUBLIC_NAME];
  264. new address = DisasmGetOperand(ctx) - gCodBase;
  265. if (address == hdr[AMX_HDR_CIP]) {
  266. strcat(name, "main");
  267. } else {
  268. new index = GetPublicIndexFromAddress(address);
  269. if (index >= 0) {
  270. GetPublicNameFromIndex(index, name);
  271. }
  272. }
  273. fwrite(file, ToHexStr(address));
  274. if (strlen(name) > 0) {
  275. fwrite(file, "; ");
  276. fwrite(file, name);
  277. }
  278. }
  279. case OP_SYSREQ_C, OP_SYSREQ_D: {
  280. new name[DISASM_MAX_NATIVE_NAME];
  281. new address = DisasmGetOperand(ctx);
  282. if (opcode == OP_SYSREQ_C) {
  283. new index = DisasmGetOperand(ctx);
  284. GetNativeNameFromIndex(index, name);
  285. } else {
  286. new index = GetNativeIndexFromAddress(address);
  287. if (index >= 0) {
  288. GetNativeNameFromIndex(index, name);
  289. }
  290. }
  291. fwrite(file, ToHexStr(address));
  292. if (strlen(name) > 0) {
  293. fwrite(file, "; ");
  294. fwrite(file, name);
  295. }
  296. }
  297. default: {
  298. new n = DisasmGetNumOperands(ctx);
  299. for (new i = 0; i < n; i++) {
  300. new operand = DisasmGetOperandReloc(ctx, i);
  301. fwrite(file, ToHexStr(operand));
  302. }
  303. }
  304. }
  305. fwrite(file, "\n");
  306. }
  307. }
  308. stock DisasmWriteDataRowChar(File:file, start, num, max) {
  309. new cur = start;
  310. new end = start + num*4;
  311. while (cur < max) {
  312. new p[4 char + 1];
  313. p[0] = ReadAmxMemory(cur);
  314. new u[4 + 1];
  315. u[0] = ToPrintableAscii(p{0});
  316. u[1] = ToPrintableAscii(p{1});
  317. u[2] = ToPrintableAscii(p{2});
  318. u[3] = ToPrintableAscii(p{3});
  319. u[4] = '\0';
  320. if (cur < end) {
  321. fwrite(file, u);
  322. } else {
  323. fwrite(file, " ");
  324. }
  325. cur += 4;
  326. }
  327. }
  328. stock DisasmWriteDataRowHex(File:file, start, num, max) {
  329. new cur = start;
  330. new end = start + num*4;
  331. while (cur < max) {
  332. if (cur < end) {
  333. fwrite(file, ToHexStr(ReadAmxMemory(cur)));
  334. } else {
  335. fwrite(file, " ");
  336. }
  337. fwrite(file, " ");
  338. cur += 4;
  339. }
  340. }
  341. stock DisasmWriteData(File:file) {
  342. fwrite(file, "\n\n; DATA\n");
  343. new hdr[AMX_HDR];
  344. GetAmxHeader(hdr);
  345. new dat = hdr[AMX_HDR_DAT];
  346. new hea = hdr[AMX_HDR_HEA];
  347. new data_end = hea - dat;
  348. for (new i = 0; i < data_end; i += 0x10) {
  349. fwrite(file, ToHexStr(i));
  350. fwrite(file, " ");
  351. DisasmWriteDataRowHex(file, i, 4, min(i + 0x10, data_end));
  352. fwrite(file, " ");
  353. DisasmWriteDataRowChar(file, i, 4, min(i + 0x10, data_end));
  354. fwrite(file, "\n");
  355. }
  356. }
  357. stock DisasmWriteFile(File:file) {
  358. DisasmWriteCode(file);
  359. DisasmWriteData(file);
  360. }
  361. stock bool:DisasmWrite(const filename[]) {
  362. new File:file = fopen(filename, io_write);
  363. if (file) {
  364. DisasmWriteFile(file);
  365. fclose(file);
  366. return true;
  367. }
  368. return false;
  369. }
  370. stock DisasmDump(const filename[]) {
  371. DisasmWrite(filename);
  372. }