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