1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/zalloc.h>
4 #include <sys/types.h>
5 #include <regex.h>
6 
7 struct arm_annotate {
8 	regex_t call_insn,
9 		jump_insn;
10 };
11 
12 static struct ins_ops *arm__associate_instruction_ops(struct arch *arch, const char *name)
13 {
14 	struct arm_annotate *arm = arch->priv;
15 	struct ins_ops *ops;
16 	regmatch_t match[2];
17 
18 	if (!regexec(&arm->call_insn, name, 2, match, 0))
19 		ops = &call_ops;
20 	else if (!regexec(&arm->jump_insn, name, 2, match, 0))
21 		ops = &jump_ops;
22 	else
23 		return NULL;
24 
25 	arch__associate_ins_ops(arch, name, ops);
26 	return ops;
27 }
28 
29 static int arm__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
30 {
31 	struct arm_annotate *arm;
32 	int err;
33 
34 	if (arch->initialized)
35 		return 0;
36 
37 	arm = zalloc(sizeof(*arm));
38 	if (!arm)
39 		return -1;
40 
41 #define ARM_CONDS "(cc|cs|eq|ge|gt|hi|le|ls|lt|mi|ne|pl|vc|vs)"
42 	err = regcomp(&arm->call_insn, "^blx?" ARM_CONDS "?$", REG_EXTENDED);
43 	if (err)
44 		goto out_free_arm;
45 	err = regcomp(&arm->jump_insn, "^bx?" ARM_CONDS "?$", REG_EXTENDED);
46 	if (err)
47 		goto out_free_call;
48 #undef ARM_CONDS
49 
50 	arch->initialized = true;
51 	arch->priv	  = arm;
52 	arch->associate_instruction_ops   = arm__associate_instruction_ops;
53 	arch->objdump.comment_char	  = ';';
54 	arch->objdump.skip_functions_char = '+';
55 	return 0;
56 
57 out_free_call:
58 	regfree(&arm->call_insn);
59 out_free_arm:
60 	free(arm);
61 	return -1;
62 }
63