Skip to content

Commit 8308fd0

Browse files
achartrePeter Zijlstra
authored andcommitted
objtool: Add Function to get the name of a CPU feature
Add a function to get the name of a CPU feature. The function is architecture dependent and currently only implemented for x86. The feature names are automatically generated from the cpufeatures.h include file. Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Josh Poimboeuf <jpoimboe@kernel.org> Link: https://patch.msgid.link/20251121095340.464045-27-alexandre.chartre@oracle.com
1 parent be5ee60 commit 8308fd0

8 files changed

Lines changed: 70 additions & 1 deletion

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/awk -f
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# Copyright (c) 2025, Oracle and/or its affiliates.
5+
#
6+
# Usage: awk -f gen-cpu-feature-names-x86.awk cpufeatures.h > cpu-feature-names.c
7+
#
8+
9+
BEGIN {
10+
print "/* cpu feature name array generated from cpufeatures.h */"
11+
print "/* Do not change this code. */"
12+
print
13+
print "static const char *cpu_feature_names[(NCAPINTS+NBUGINTS)*32] = {"
14+
15+
value_expr = "\\([0-9*+ ]+\\)"
16+
}
17+
18+
/^#define X86_FEATURE_/ {
19+
if (match($0, value_expr)) {
20+
value = substr($0, RSTART + 1, RLENGTH - 2)
21+
print "\t[" value "] = \"" $2 "\","
22+
}
23+
}
24+
25+
/^#define X86_BUG_/ {
26+
if (match($0, value_expr)) {
27+
value = substr($0, RSTART + 1, RLENGTH - 2)
28+
print "\t[NCAPINTS*32+(" value ")] = \"" $2 "\","
29+
}
30+
}
31+
32+
END {
33+
print "};"
34+
}

tools/objtool/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0-only
2+
arch/x86/lib/cpu-feature-names.c
23
arch/x86/lib/inat-tables.c
34
/objtool
45
feature

tools/objtool/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ $(LIBSUBCMD)-clean:
125125
clean: $(LIBSUBCMD)-clean
126126
$(call QUIET_CLEAN, objtool) $(RM) $(OBJTOOL)
127127
$(Q)find $(OUTPUT) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
128+
$(Q)$(RM) $(OUTPUT)arch/x86/lib/cpu-feature-names.c $(OUTPUT)fixdep
128129
$(Q)$(RM) $(OUTPUT)arch/x86/lib/inat-tables.c $(OUTPUT)fixdep
129130
$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.objtool
130131
$(Q)$(RM) -r -- $(OUTPUT)feature

tools/objtool/arch/loongarch/special.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,8 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
194194

195195
return rodata_reloc;
196196
}
197+
198+
const char *arch_cpu_feature_name(int feature_number)
199+
{
200+
return NULL;
201+
}

tools/objtool/arch/powerpc/special.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
1818
{
1919
exit(-1);
2020
}
21+
22+
const char *arch_cpu_feature_name(int feature_number)
23+
{
24+
return NULL;
25+
}

tools/objtool/arch/x86/Build

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
objtool-y += special.o
21
objtool-y += decode.o
2+
objtool-y += special.o
33
objtool-y += orc.o
44

55
inat_tables_script = ../arch/x86/tools/gen-insn-attr-x86.awk
@@ -12,3 +12,14 @@ $(OUTPUT)arch/x86/lib/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
1212
$(OUTPUT)arch/x86/decode.o: $(OUTPUT)arch/x86/lib/inat-tables.c
1313

1414
CFLAGS_decode.o += -I$(OUTPUT)arch/x86/lib
15+
16+
cpu_features = ../arch/x86/include/asm/cpufeatures.h
17+
cpu_features_script = ../arch/x86/tools/gen-cpu-feature-names-x86.awk
18+
19+
$(OUTPUT)arch/x86/lib/cpu-feature-names.c: $(cpu_features_script) $(cpu_features)
20+
$(call rule_mkdir)
21+
$(Q)$(call echo-cmd,gen)$(AWK) -f $(cpu_features_script) $(cpu_features) > $@
22+
23+
$(OUTPUT)arch/x86/special.o: $(OUTPUT)arch/x86/lib/cpu-feature-names.c
24+
25+
CFLAGS_special.o += -I$(OUTPUT)arch/x86/lib

tools/objtool/arch/x86/special.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include <objtool/special.h>
55
#include <objtool/builtin.h>
66
#include <objtool/warn.h>
7+
#include <asm/cpufeatures.h>
8+
9+
/* cpu feature name array generated from cpufeatures.h */
10+
#include "cpu-feature-names.c"
711

812
void arch_handle_alternative(struct special_alt *alt)
913
{
@@ -134,3 +138,9 @@ struct reloc *arch_find_switch_table(struct objtool_file *file,
134138
*table_size = 0;
135139
return rodata_reloc;
136140
}
141+
142+
const char *arch_cpu_feature_name(int feature_number)
143+
{
144+
return (feature_number < ARRAY_SIZE(cpu_feature_names)) ?
145+
cpu_feature_names[feature_number] : NULL;
146+
}

tools/objtool/include/objtool/special.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ bool arch_support_alt_relocation(struct special_alt *special_alt,
3838
struct reloc *arch_find_switch_table(struct objtool_file *file,
3939
struct instruction *insn,
4040
unsigned long *table_size);
41+
const char *arch_cpu_feature_name(int feature_number);
42+
4143
#endif /* _SPECIAL_H */

0 commit comments

Comments
 (0)