You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

146 lines
3.4 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2019 by Mete Balci *
  3. * metebalci@gmail.com *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <helper/log.h>
  22. #include "target.h"
  23. #include "a64_disassembler.h"
  24. #if HAVE_CAPSTONE
  25. #include <capstone/capstone.h>
  26. static void print_opcode(struct command_invocation *cmd, const cs_insn *insn)
  27. {
  28. uint32_t opcode = 0;
  29. memcpy(&opcode, insn->bytes, insn->size);
  30. if (insn->size == 4) {
  31. uint16_t opcode_high = opcode >> 16;
  32. opcode = opcode & 0xffff;
  33. command_print(cmd,
  34. "0x%08" PRIx64" %04x %04x\t%s\t%s",
  35. insn->address,
  36. opcode,
  37. opcode_high,
  38. insn->mnemonic,
  39. insn->op_str);
  40. } else {
  41. command_print(
  42. cmd,
  43. "0x%08" PRIx64" %04x\t%s\t%s",
  44. insn->address,
  45. opcode,
  46. insn->mnemonic,
  47. insn->op_str);
  48. }
  49. }
  50. int a64_disassemble(struct command_invocation *cmd, struct target *target, target_addr_t address, size_t count)
  51. {
  52. int ret;
  53. int csret;
  54. csh handle;
  55. csret = cs_open(CS_ARCH_ARM64, CS_MODE_LITTLE_ENDIAN, &handle);
  56. if (csret != CS_ERR_OK) {
  57. LOG_ERROR("cs_open() failed: %s", cs_strerror(csret));
  58. return ERROR_FAIL;
  59. }
  60. csret = cs_option(handle, CS_OPT_SKIPDATA, CS_OPT_ON);
  61. if (csret != CS_ERR_OK) {
  62. LOG_ERROR("cs_option() failed: %s", cs_strerror(csret));
  63. cs_close(&handle);
  64. return ERROR_FAIL;
  65. }
  66. cs_insn *insn = cs_malloc(handle);
  67. if (csret != CS_ERR_OK) {
  68. LOG_ERROR("cs_malloc() failed: %s", cs_strerror(csret));
  69. cs_close(&handle);
  70. return ERROR_FAIL;
  71. }
  72. while (count > 0) {
  73. uint8_t buffer[4];
  74. ret = target_read_buffer(target, address, sizeof(buffer), buffer);
  75. if (ret != ERROR_OK) {
  76. cs_free(insn, 1);
  77. cs_close(&handle);
  78. return ret;
  79. }
  80. size_t size = sizeof(buffer);
  81. const uint8_t *tmp = buffer;
  82. ret = cs_disasm_iter(handle, &tmp, &size, &address, insn);
  83. if (!ret) {
  84. LOG_ERROR("cs_disasm_iter() failed: %s", cs_strerror(cs_errno(handle)));
  85. cs_free(insn, 1);
  86. cs_close(&handle);
  87. return ERROR_FAIL;
  88. }
  89. print_opcode(cmd, insn);
  90. count--;
  91. }
  92. cs_free(insn, 1);
  93. cs_close(&handle);
  94. return ERROR_OK;
  95. }
  96. #else
  97. int a64_disassemble(struct command_invocation *cmd, struct target *target, target_addr_t address, size_t count)
  98. {
  99. command_print(cmd, "capstone disassembly framework required");
  100. return ERROR_FAIL;
  101. }
  102. #endif