objtool: Abstract alternative special case handling
authorJulien Thierry <jthierry@redhat.com>
Fri, 4 Sep 2020 15:30:22 +0000 (16:30 +0100)
committerJosh Poimboeuf <jpoimboe@redhat.com>
Thu, 10 Sep 2020 15:43:13 +0000 (10:43 -0500)
Some alternatives associated with a specific feature need to be treated
in a special way. Since the features and how to treat them vary from one
architecture to another, move the special case handling to arch specific
code.

Reviewed-by: Miroslav Benes <mbenes@suse.cz>
Signed-off-by: Julien Thierry <jthierry@redhat.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
tools/objtool/arch/x86/Build
tools/objtool/arch/x86/special.c [new file with mode: 0644]
tools/objtool/objtool.h
tools/objtool/special.c
tools/objtool/special.h
tools/objtool/weak.c

index 7c5004008e974156194f5ab185305dfbeafc4ae0..9f7869b5c5e0caf1c0128e0f177d98e931017583 100644 (file)
@@ -1,3 +1,4 @@
+objtool-y += special.o
 objtool-y += decode.o
 
 inat_tables_script = ../arch/x86/tools/gen-insn-attr-x86.awk
diff --git a/tools/objtool/arch/x86/special.c b/tools/objtool/arch/x86/special.c
new file mode 100644 (file)
index 0000000..823561e
--- /dev/null
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+#include "../../special.h"
+#include "../../builtin.h"
+
+#define X86_FEATURE_POPCNT (4 * 32 + 23)
+#define X86_FEATURE_SMAP   (9 * 32 + 20)
+
+void arch_handle_alternative(unsigned short feature, struct special_alt *alt)
+{
+       switch (feature) {
+       case X86_FEATURE_SMAP:
+               /*
+                * If UACCESS validation is enabled; force that alternative;
+                * otherwise force it the other way.
+                *
+                * What we want to avoid is having both the original and the
+                * alternative code flow at the same time, in that case we can
+                * find paths that see the STAC but take the NOP instead of
+                * CLAC and the other way around.
+                */
+               if (uaccess)
+                       alt->skip_orig = true;
+               else
+                       alt->skip_alt = true;
+               break;
+       case X86_FEATURE_POPCNT:
+               /*
+                * It has been requested that we don't validate the !POPCNT
+                * feature path which is a "very very small percentage of
+                * machines".
+                */
+               alt->skip_orig = true;
+               break;
+       default:
+               break;
+       }
+}
index a635f68a9b09f5520ff0845273f553c30eda2a06..4125d4578b23ba092bc03a6f2de5bee16b3fcf27 100644 (file)
@@ -12,6 +12,8 @@
 
 #include "elf.h"
 
+#define __weak __attribute__((weak))
+
 struct objtool_file {
        struct elf *elf;
        struct list_head insn_list;
index b04f395de5de2672ec63424af15739a4fdaede3c..1a2420febd08a4f48e954490f92676d99466a54d 100644 (file)
@@ -16,9 +16,6 @@
 #include "warn.h"
 #include "arch_special.h"
 
-#define X86_FEATURE_POPCNT (4*32+23)
-#define X86_FEATURE_SMAP   (9*32+20)
-
 struct special_entry {
        const char *sec;
        bool group, jump_or_nop;
@@ -54,6 +51,10 @@ struct special_entry entries[] = {
        {},
 };
 
+void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt)
+{
+}
+
 static int get_alt_entry(struct elf *elf, struct special_entry *entry,
                         struct section *sec, int idx,
                         struct special_alt *alt)
@@ -78,30 +79,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
 
                feature = *(unsigned short *)(sec->data->d_buf + offset +
                                              entry->feature);
-
-               /*
-                * It has been requested that we don't validate the !POPCNT
-                * feature path which is a "very very small percentage of
-                * machines".
-                */
-               if (feature == X86_FEATURE_POPCNT)
-                       alt->skip_orig = true;
-
-               /*
-                * If UACCESS validation is enabled; force that alternative;
-                * otherwise force it the other way.
-                *
-                * What we want to avoid is having both the original and the
-                * alternative code flow at the same time, in that case we can
-                * find paths that see the STAC but take the NOP instead of
-                * CLAC and the other way around.
-                */
-               if (feature == X86_FEATURE_SMAP) {
-                       if (uaccess)
-                               alt->skip_orig = true;
-                       else
-                               alt->skip_alt = true;
-               }
+               arch_handle_alternative(feature, alt);
        }
 
        orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
index 35061530e46e8dac222d7364b04c93a02e439d17..44da89afeda21244f4016e5bd105022b103eec09 100644 (file)
@@ -28,4 +28,6 @@ struct special_alt {
 
 int special_get_alts(struct elf *elf, struct list_head *alts);
 
+void arch_handle_alternative(unsigned short feature, struct special_alt *alt);
+
 #endif /* _SPECIAL_H */
index 29180d599b0876ac8af823f48a8368d08f2afdaa..7843e9a7a72f4885285c4d6b4bb0a7d98fae19f9 100644 (file)
@@ -9,8 +9,6 @@
 #include <errno.h>
 #include "objtool.h"
 
-#define __weak __attribute__((weak))
-
 #define UNSUPPORTED(name)                                              \
 ({                                                                     \
        fprintf(stderr, "error: objtool: " name " not implemented\n");  \