disasm.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. }
  39. enum DisasmResult {
  40. DISASM_DONE = 0,
  41. DISASM_NOP = 1,
  42. DISASM_OK = 2
  43. }
  44. static stock gCodBase;
  45. stock DisasmInit(ctx[DisasmContext], start = 0, end = 0) {
  46. new hdr[AMX_HDR];
  47. GetAmxHeader(hdr);
  48. gCodBase = GetAmxBaseAddress() + hdr[AMX_HDR_COD];
  49. new dat = hdr[AMX_HDR_DAT];
  50. new cod = hdr[AMX_HDR_COD];
  51. new code_base = cod - dat;
  52. start += code_base;
  53. ctx[DisasmContext_nip] = start;
  54. ctx[DisasmContext_cip] = start;
  55. ctx[DisasmContext_start_ip] = start;
  56. if (end != 0) {
  57. ctx[DisasmContext_end_ip] = code_base + end;
  58. } else {
  59. // code_base + (dat - cod)
  60. // = (cod - dat) + (dat - cod)
  61. // = cod - dat + dat - cod
  62. // = cod - cod + dat - dat
  63. // = 0
  64. ctx[DisasmContext_end_ip] = 0;
  65. }
  66. }
  67. stock bool:DisasmDecodeInsn(ctx[DisasmContext]) {
  68. new ip = ctx[DisasmContext_nip];
  69. if (ip >= 0) {
  70. return false;
  71. }
  72. new Opcode:opcode = UnrelocateOpcode(Opcode:ReadAmxMemory(ip));
  73. if (opcode <= OP_NONE || _:opcode >= NUM_OPCODES) {
  74. return false;
  75. }
  76. ctx[DisasmContext_cip] = ip;
  77. ctx[DisasmContext_opcode] = opcode;
  78. ip += 4;
  79. if (opcode == OP_CASETBL) {
  80. new n = ReadAmxMemory(ip);
  81. ip += 4;
  82. ip += (2 * n + 1) * 4;
  83. } else {
  84. ip += 4 * GetOpcodeInstructionParameters(opcode);
  85. }
  86. ctx[DisasmContext_nip] = ip;
  87. return true;
  88. }
  89. stock DisasmResult:DisasmNext(ctx[DisasmContext]) {
  90. // This function is just a slightly more consistent wrapper around
  91. // "DisasmDecodeInsn". I wanted to change that function, but that would
  92. // cause breaking changes. This returns non-zero while there is still data
  93. // being read, even if that data is not a valid opcode. For invalid code,
  94. // it skips the current cell and returns an invalid opcode. It also now
  95. // checks the end point correctly.
  96. if (ctx[DisasmContext_nip] >= ctx[DisasmContext_end_ip]) {
  97. return DISASM_DONE;
  98. } else if (DisasmDecodeInsn(ctx)) {
  99. return DISASM_OK;
  100. } else {
  101. ctx[DisasmContext_cip] = ctx[DisasmContext_nip],
  102. ctx[DisasmContext_nip] += 4,
  103. ctx[DisasmContext_opcode] = Opcode:NUM_OPCODES;
  104. return DISASM_NOP;
  105. }
  106. }
  107. stock Opcode:DisasmNextInsn(ctx[DisasmContext]) {
  108. if (DisasmDecodeInsn(ctx)) {
  109. return ctx[DisasmContext_opcode];
  110. }
  111. return OP_NONE;
  112. }
  113. stock Opcode:DisasmGetOpcode(const ctx[DisasmContext]) {
  114. return ctx[DisasmContext_opcode];
  115. }
  116. stock DisasmGetOperand(const ctx[DisasmContext], index = 0) {
  117. return ReadAmxMemory(ctx[DisasmContext_cip] + (index + 1) * 4);
  118. }
  119. stock DisasmGetNumOperands(const ctx[DisasmContext]) {
  120. new Opcode:opcode = ctx[DisasmContext_opcode];
  121. if (opcode == OP_CASETBL) {
  122. return ReadAmxMemory(ctx[DisasmContext_cip] + 4);
  123. } else {
  124. return GetOpcodeInstructionParameters(opcode);
  125. }
  126. }
  127. stock bool:DisasmNeedReloc(const ctx[DisasmContext]) {
  128. return GetOpcodeInstructionRelocatable(ctx[DisasmContext_opcode]);
  129. }
  130. stock DisasmReloc(addr) {
  131. return addr - gCodBase;
  132. }
  133. stock DisasmGetNextIp(const ctx[DisasmContext]) {
  134. return ctx[DisasmContext_nip];
  135. }
  136. stock DisasmGetCurIp(const ctx[DisasmContext]) {
  137. return ctx[DisasmContext_cip];
  138. }
  139. stock DisasmGetRemaining(const ctx[DisasmContext]) {
  140. return ctx[DisasmContext_end_ip] - ctx[DisasmContext_nip];
  141. }
  142. stock DisasmGetInsnName(const ctx[DisasmContext], name[], size = sizeof(name)) {
  143. name[0] = '\0';
  144. strcat(name, GetOpcodeInstructionName(ctx[DisasmContext_opcode]), size);
  145. }
  146. stock DisasmGetOperandReloc(const ctx[DisasmContext], index = 0) {
  147. new param = DisasmGetOperand(ctx, index);
  148. // Needs special code for dealing with "CASETBL", which has multiple
  149. // parameters - not all of them to be relocated. If the opcode is NOT that,
  150. // then check if it is any other opcode requiring relocation. This does
  151. // result in the odd pattern of having a triadic operator in a conditional,
  152. // but the alternative would be:
  153. //
  154. // if (ctx[DisasmContext_opcode == OP_CASETBL) {
  155. // if (index & 1) {
  156. // return param - base;
  157. // }
  158. // } else if (DisasmNeedReloc(ctx)) {
  159. // return param - base;
  160. // }
  161. // return param;
  162. //
  163. // Or:
  164. //
  165. // if ((ctx[DisasmContext_opcode == OP_CASETBL && (index & 1)) ||
  166. // (ctx[DisasmContext_opcode != OP_CASETBL && DisasmNeedReloc(ctx))) {
  167. // return param - base;
  168. // } else {
  169. // return param;
  170. // }
  171. //
  172. // I think the conditional ends up nicer in this rare case.
  173. if ((ctx[DisasmContext_opcode] == OP_CASETBL) ? (index & 1) : _:DisasmNeedReloc(ctx)) {
  174. return DisasmReloc(param);
  175. } else {
  176. return param;
  177. }
  178. }
  179. static stock ToHexStr(x) {
  180. new s[11];
  181. new i = 0;
  182. new j = 0;
  183. while (i < sizeof(s) && j < 8) {
  184. new n = x >> (7 - j) * 4 & 0xF;
  185. switch (n) {
  186. case 0x0..0x9:
  187. s[i] = n + '0';
  188. case 0xA..0xF:
  189. s[i] = n + 'a' - 0xA;
  190. }
  191. i++;
  192. j++;
  193. }
  194. return s;
  195. }
  196. static stock bool:IsPrintableAscii(c) {
  197. return 32 <= c <= 126;
  198. }
  199. static stock ToPrintableAscii(c) {
  200. return IsPrintableAscii(c) ? c : ' ';
  201. }
  202. stock DisasmWriteCode(File:file) {
  203. new ctx[DisasmContext];
  204. DisasmInit(ctx);
  205. new hdr[AMX_HDR];
  206. GetAmxHeader(hdr);
  207. new dat = hdr[AMX_HDR_DAT];
  208. new cod = hdr[AMX_HDR_COD];
  209. fwrite(file, "; CODE\n\n");
  210. while (DisasmGetNextIp(ctx) < ctx[DisasmContext_end_ip])
  211. {
  212. if (!DisasmDecodeInsn(ctx)) {
  213. new cip = DisasmGetNextIp(ctx);
  214. ctx[DisasmContext_nip] += 4;
  215. fwrite(file, ToHexStr(cip + dat - cod));
  216. fwrite(file, " ???? ");
  217. fwrite(file, ToHexStr(ReadAmxMemory(cip)));
  218. fwrite(file, "\n");
  219. continue;
  220. }
  221. new cip = DisasmGetCurIp(ctx);
  222. new Opcode:opcode = DisasmGetOpcode(ctx);
  223. if (opcode == OP_PROC) {
  224. fwrite(file, "\n");
  225. }
  226. new insn_name[OPCODE_MAX_INSN_NAME];
  227. DisasmGetInsnName(ctx, insn_name);
  228. fwrite(file, ToHexStr(cip + dat - cod));
  229. fwrite(file, " ");
  230. fwrite(file, insn_name);
  231. fwrite(file, " ");
  232. switch (opcode) {
  233. case OP_PROC: {
  234. new name[32];
  235. new address = cip + dat - cod;
  236. if (address == hdr[AMX_HDR_CIP]) {
  237. strcat(name, "main");
  238. } else {
  239. new index = GetPublicIndexFromAddress(address);
  240. if (index >= 0) {
  241. GetPublicNameFromIndex(index, name);
  242. }
  243. }
  244. if (strlen(name) != 0) {
  245. fwrite(file, "; ");
  246. fwrite(file, name);
  247. }
  248. }
  249. case OP_CASETBL: {
  250. new num = DisasmGetNumOperands(ctx);
  251. fwrite(file, ToHexStr(num));
  252. fwrite(file, " ");
  253. new rel_addr = DisasmGetOperand(ctx, 1) - gCodBase;
  254. fwrite(file, ToHexStr(rel_addr));
  255. for (new i = 1; i <= num; i++) {
  256. fwrite(file, "\n case ");
  257. new val = DisasmGetOperand(ctx, i * 2);
  258. fwrite(file, ToHexStr(val));
  259. fwrite(file, " ");
  260. rel_addr = DisasmGetOperand(ctx, i * 2 + 1) - gCodBase;
  261. fwrite(file, ToHexStr(rel_addr));
  262. }
  263. }
  264. case OP_CALL: {
  265. new name[DISASM_MAX_PUBLIC_NAME];
  266. new address = DisasmGetOperand(ctx) - gCodBase;
  267. if (address == hdr[AMX_HDR_CIP]) {
  268. strcat(name, "main");
  269. } else {
  270. new index = GetPublicIndexFromAddress(address);
  271. if (index >= 0) {
  272. GetPublicNameFromIndex(index, name);
  273. }
  274. }
  275. fwrite(file, ToHexStr(address));
  276. if (strlen(name) > 0) {
  277. fwrite(file, "; ");
  278. fwrite(file, name);
  279. }
  280. }
  281. case OP_SYSREQ_C, OP_SYSREQ_D: {
  282. new name[DISASM_MAX_NATIVE_NAME];
  283. new address = DisasmGetOperand(ctx);
  284. if (opcode == OP_SYSREQ_C) {
  285. new index = DisasmGetOperand(ctx);
  286. GetNativeNameFromIndex(index, name);
  287. } else {
  288. new index = GetNativeIndexFromAddress(address);
  289. if (index >= 0) {
  290. GetNativeNameFromIndex(index, name);
  291. }
  292. }
  293. fwrite(file, ToHexStr(address));
  294. if (strlen(name) > 0) {
  295. fwrite(file, "; ");
  296. fwrite(file, name);
  297. }
  298. }
  299. default: {
  300. new n = DisasmGetNumOperands(ctx);
  301. for (new i = 0; i < n; i++) {
  302. new operand = DisasmGetOperandReloc(ctx, i);
  303. fwrite(file, ToHexStr(operand));
  304. }
  305. }
  306. }
  307. fwrite(file, "\n");
  308. }
  309. }
  310. stock DisasmWriteDataRowChar(File:file, start, num, max) {
  311. new cur = start;
  312. new end = start + num*4;
  313. while (cur < max) {
  314. new p[4 char + 1];
  315. p[0] = ReadAmxMemory(cur);
  316. new u[4 + 1];
  317. u[0] = ToPrintableAscii(p{0});
  318. u[1] = ToPrintableAscii(p{1});
  319. u[2] = ToPrintableAscii(p{2});
  320. u[3] = ToPrintableAscii(p{3});
  321. u[4] = '\0';
  322. if (cur < end) {
  323. fwrite(file, u);
  324. } else {
  325. fwrite(file, " ");
  326. }
  327. cur += 4;
  328. }
  329. }
  330. stock DisasmWriteDataRowHex(File:file, start, num, max) {
  331. new cur = start;
  332. new end = start + num*4;
  333. while (cur < max) {
  334. if (cur < end) {
  335. fwrite(file, ToHexStr(ReadAmxMemory(cur)));
  336. } else {
  337. fwrite(file, " ");
  338. }
  339. fwrite(file, " ");
  340. cur += 4;
  341. }
  342. }
  343. stock DisasmWriteData(File:file) {
  344. fwrite(file, "\n\n; DATA\n");
  345. new hdr[AMX_HDR];
  346. GetAmxHeader(hdr);
  347. new dat = hdr[AMX_HDR_DAT];
  348. new hea = hdr[AMX_HDR_HEA];
  349. new data_end = hea - dat;
  350. for (new i = 0; i < data_end; i += 0x10) {
  351. fwrite(file, ToHexStr(i));
  352. fwrite(file, " ");
  353. DisasmWriteDataRowHex(file, i, 4, min(i + 0x10, data_end));
  354. fwrite(file, " ");
  355. DisasmWriteDataRowChar(file, i, 4, min(i + 0x10, data_end));
  356. fwrite(file, "\n");
  357. }
  358. }
  359. stock DisasmWriteFile(File:file) {
  360. DisasmWriteCode(file);
  361. DisasmWriteData(file);
  362. }
  363. stock bool:DisasmWrite(const filename[]) {
  364. new File:file = fopen(filename, io_write);
  365. if (file) {
  366. DisasmWriteFile(file);
  367. fclose(file);
  368. return true;
  369. }
  370. return false;
  371. }
  372. stock DisasmDump(const filename[]) {
  373. DisasmWrite(filename);
  374. }