instructions.c 2.6 KB

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