instructions.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include <sys/types.h>
  4. #include <regex.h>
  5. struct arm64_annotate {
  6. regex_t call_insn,
  7. jump_insn;
  8. };
  9. static struct ins_ops *arm64__associate_instruction_ops(struct arch *arch, const char *name)
  10. {
  11. struct arm64_annotate *arm = arch->priv;
  12. struct ins_ops *ops;
  13. regmatch_t match[2];
  14. if (!regexec(&arm->jump_insn, name, 2, match, 0))
  15. ops = &jump_ops;
  16. else if (!regexec(&arm->call_insn, name, 2, match, 0))
  17. ops = &call_ops;
  18. else if (!strcmp(name, "ret"))
  19. ops = &ret_ops;
  20. else
  21. return NULL;
  22. arch__associate_ins_ops(arch, name, ops);
  23. return ops;
  24. }
  25. static int arm64__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
  26. {
  27. struct arm64_annotate *arm;
  28. int err;
  29. if (arch->initialized)
  30. return 0;
  31. arm = zalloc(sizeof(*arm));
  32. if (!arm)
  33. return -1;
  34. /* bl, blr */
  35. err = regcomp(&arm->call_insn, "^blr?$", REG_EXTENDED);
  36. if (err)
  37. goto out_free_arm;
  38. /* b, b.cond, br, cbz/cbnz, tbz/tbnz */
  39. err = regcomp(&arm->jump_insn, "^[ct]?br?\\.?(cc|cs|eq|ge|gt|hi|le|ls|lt|mi|ne|pl)?n?z?$",
  40. REG_EXTENDED);
  41. if (err)
  42. goto out_free_call;
  43. arch->initialized = true;
  44. arch->priv = arm;
  45. arch->associate_instruction_ops = arm64__associate_instruction_ops;
  46. arch->objdump.comment_char = '/';
  47. arch->objdump.skip_functions_char = '+';
  48. return 0;
  49. out_free_call:
  50. regfree(&arm->call_insn);
  51. out_free_arm:
  52. free(arm);
  53. return -1;
  54. }