1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * dwarf-regs.c : Mapping of DWARF debug register numbers into register names.
4 *
5 * Written by: Masami Hiramatsu <[email protected]>
6 */
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <debug.h>
11 #include <dwarf-regs.h>
12 #include <elf.h>
13 #include <errno.h>
14 #include <linux/kernel.h>
15
16 /* Define const char * {arch}_register_tbl[] */
17 #define DEFINE_DWARF_REGSTR_TABLE
18 #include "../arch/x86/include/dwarf-regs-table.h"
19 #include "../arch/arm/include/dwarf-regs-table.h"
20 #include "../arch/arm64/include/dwarf-regs-table.h"
21 #include "../arch/sh/include/dwarf-regs-table.h"
22 #include "../arch/powerpc/include/dwarf-regs-table.h"
23 #include "../arch/riscv/include/dwarf-regs-table.h"
24 #include "../arch/s390/include/dwarf-regs-table.h"
25 #include "../arch/sparc/include/dwarf-regs-table.h"
26 #include "../arch/xtensa/include/dwarf-regs-table.h"
27 #include "../arch/mips/include/dwarf-regs-table.h"
28 #include "../arch/loongarch/include/dwarf-regs-table.h"
29
30 #define __get_dwarf_regstr(tbl, n) (((n) < ARRAY_SIZE(tbl)) ? (tbl)[(n)] : NULL)
31
32 /* Return architecture dependent register string (for kprobe-tracer) */
get_dwarf_regstr(unsigned int n,unsigned int machine,unsigned int flags)33 const char *get_dwarf_regstr(unsigned int n, unsigned int machine, unsigned int flags)
34 {
35 if (machine == EM_NONE) {
36 /* Generic arch - use host arch */
37 machine = EM_HOST;
38 }
39 switch (machine) {
40 case EM_386:
41 return __get_dwarf_regstr(x86_32_regstr_tbl, n);
42 case EM_X86_64:
43 return __get_dwarf_regstr(x86_64_regstr_tbl, n);
44 case EM_ARM:
45 return __get_dwarf_regstr(arm_regstr_tbl, n);
46 case EM_AARCH64:
47 return __get_dwarf_regstr(aarch64_regstr_tbl, n);
48 case EM_CSKY:
49 return get_csky_regstr(n, flags);
50 case EM_SH:
51 return __get_dwarf_regstr(sh_regstr_tbl, n);
52 case EM_S390:
53 return __get_dwarf_regstr(s390_regstr_tbl, n);
54 case EM_PPC:
55 case EM_PPC64:
56 return __get_dwarf_regstr(powerpc_regstr_tbl, n);
57 case EM_RISCV:
58 return __get_dwarf_regstr(riscv_regstr_tbl, n);
59 case EM_SPARC:
60 case EM_SPARCV9:
61 return __get_dwarf_regstr(sparc_regstr_tbl, n);
62 case EM_XTENSA:
63 return __get_dwarf_regstr(xtensa_regstr_tbl, n);
64 case EM_MIPS:
65 return __get_dwarf_regstr(mips_regstr_tbl, n);
66 case EM_LOONGARCH:
67 return __get_dwarf_regstr(loongarch_regstr_tbl, n);
68 default:
69 pr_err("ELF MACHINE %x is not supported.\n", machine);
70 }
71 return NULL;
72 }
73
74 #if EM_HOST != EM_X86_64 && EM_HOST != EM_386
get_arch_regnum(const char * name __maybe_unused)75 __weak int get_arch_regnum(const char *name __maybe_unused)
76 {
77 return -ENOTSUP;
78 }
79 #endif
80
81 /* Return DWARF register number from architecture register name */
get_dwarf_regnum(const char * name,unsigned int machine,unsigned int flags __maybe_unused)82 int get_dwarf_regnum(const char *name, unsigned int machine, unsigned int flags __maybe_unused)
83 {
84 char *regname = strdup(name);
85 int reg = -1;
86 char *p;
87
88 if (regname == NULL)
89 return -EINVAL;
90
91 /* For convenience, remove trailing characters */
92 p = strpbrk(regname, " ,)");
93 if (p)
94 *p = '\0';
95
96 if (machine == EM_NONE) {
97 /* Generic arch - use host arch */
98 machine = EM_HOST;
99 }
100 switch (machine) {
101 #if EM_HOST != EM_X86_64 && EM_HOST != EM_386
102 case EM_HOST:
103 reg = get_arch_regnum(regname);
104 break;
105 #endif
106 case EM_X86_64:
107 fallthrough;
108 case EM_386:
109 reg = get_x86_regnum(regname);
110 break;
111 default:
112 pr_err("ELF MACHINE %x is not supported.\n", machine);
113 }
114 free(regname);
115 return reg;
116 }
117