annotate-arm64.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <linux/zalloc.h>
  7. #include <regex.h>
  8. #include "../annotate.h"
  9. #include "../disasm.h"
  10. struct arch_arm64 {
  11. struct arch arch;
  12. regex_t call_insn;
  13. regex_t jump_insn;
  14. };
  15. static int arm64_mov__parse(const struct arch *arch __maybe_unused,
  16. struct ins_operands *ops,
  17. struct map_symbol *ms __maybe_unused,
  18. struct disasm_line *dl __maybe_unused)
  19. {
  20. char *s = strchr(ops->raw, ','), *target, *endptr;
  21. if (s == NULL)
  22. return -1;
  23. *s = '\0';
  24. ops->source.raw = strdup(ops->raw);
  25. *s = ',';
  26. if (ops->source.raw == NULL)
  27. return -1;
  28. target = ++s;
  29. ops->target.raw = strdup(target);
  30. if (ops->target.raw == NULL)
  31. goto out_free_source;
  32. ops->target.addr = strtoull(target, &endptr, 16);
  33. if (endptr == target)
  34. goto out_free_target;
  35. s = strchr(endptr, '<');
  36. if (s == NULL)
  37. goto out_free_target;
  38. endptr = strchr(s + 1, '>');
  39. if (endptr == NULL)
  40. goto out_free_target;
  41. *endptr = '\0';
  42. *s = ' ';
  43. ops->target.name = strdup(s);
  44. *s = '<';
  45. *endptr = '>';
  46. if (ops->target.name == NULL)
  47. goto out_free_target;
  48. return 0;
  49. out_free_target:
  50. zfree(&ops->target.raw);
  51. out_free_source:
  52. zfree(&ops->source.raw);
  53. return -1;
  54. }
  55. static const struct ins_ops arm64_mov_ops = {
  56. .parse = arm64_mov__parse,
  57. .scnprintf = mov__scnprintf,
  58. };
  59. static const struct ins_ops *arm64__associate_instruction_ops(struct arch *arch, const char *name)
  60. {
  61. struct arch_arm64 *arm = container_of(arch, struct arch_arm64, arch);
  62. const struct ins_ops *ops;
  63. regmatch_t match[2];
  64. if (!regexec(&arm->jump_insn, name, 2, match, 0))
  65. ops = &jump_ops;
  66. else if (!regexec(&arm->call_insn, name, 2, match, 0))
  67. ops = &call_ops;
  68. else if (!strcmp(name, "ret"))
  69. ops = &ret_ops;
  70. else
  71. ops = &arm64_mov_ops;
  72. arch__associate_ins_ops(arch, name, ops);
  73. return ops;
  74. }
  75. const struct arch *arch__new_arm64(const struct e_machine_and_e_flags *id,
  76. const char *cpuid __maybe_unused)
  77. {
  78. int err;
  79. struct arch_arm64 *arm = zalloc(sizeof(*arm));
  80. struct arch *arch;
  81. if (!arm)
  82. return NULL;
  83. arch = &arm->arch;
  84. arch->name = "arm64";
  85. arch->id = *id;
  86. arch->objdump.comment_char = '/';
  87. arch->objdump.skip_functions_char = '+';
  88. arch->associate_instruction_ops = arm64__associate_instruction_ops;
  89. /* bl, blr */
  90. err = regcomp(&arm->call_insn, "^blr?$", REG_EXTENDED);
  91. if (err)
  92. goto out_free_arm;
  93. /* b, b.cond, br, cbz/cbnz, tbz/tbnz */
  94. err = regcomp(&arm->jump_insn, "^[ct]?br?\\.?(cc|cs|eq|ge|gt|hi|hs|le|lo|ls|lt|mi|ne|pl|vc|vs)?n?z?$",
  95. REG_EXTENDED);
  96. if (err)
  97. goto out_free_call;
  98. return arch;
  99. out_free_call:
  100. regfree(&arm->call_insn);
  101. out_free_arm:
  102. free(arm);
  103. errno = SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP;
  104. return NULL;
  105. }