instructions.c 1.3 KB

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