Skip to content

Commit eda3dc9

Browse files
Julien Thierryjpoimboe
authored andcommitted
objtool: Abstract alternative special case handling
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>
1 parent c8ea0d6 commit eda3dc9

6 files changed

Lines changed: 47 additions & 29 deletions

File tree

tools/objtool/arch/x86/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
objtool-y += special.o
12
objtool-y += decode.o
23

34
inat_tables_script = ../arch/x86/tools/gen-insn-attr-x86.awk

tools/objtool/arch/x86/special.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
#include "../../special.h"
3+
#include "../../builtin.h"
4+
5+
#define X86_FEATURE_POPCNT (4 * 32 + 23)
6+
#define X86_FEATURE_SMAP (9 * 32 + 20)
7+
8+
void arch_handle_alternative(unsigned short feature, struct special_alt *alt)
9+
{
10+
switch (feature) {
11+
case X86_FEATURE_SMAP:
12+
/*
13+
* If UACCESS validation is enabled; force that alternative;
14+
* otherwise force it the other way.
15+
*
16+
* What we want to avoid is having both the original and the
17+
* alternative code flow at the same time, in that case we can
18+
* find paths that see the STAC but take the NOP instead of
19+
* CLAC and the other way around.
20+
*/
21+
if (uaccess)
22+
alt->skip_orig = true;
23+
else
24+
alt->skip_alt = true;
25+
break;
26+
case X86_FEATURE_POPCNT:
27+
/*
28+
* It has been requested that we don't validate the !POPCNT
29+
* feature path which is a "very very small percentage of
30+
* machines".
31+
*/
32+
alt->skip_orig = true;
33+
break;
34+
default:
35+
break;
36+
}
37+
}

tools/objtool/objtool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "elf.h"
1414

15+
#define __weak __attribute__((weak))
16+
1517
struct objtool_file {
1618
struct elf *elf;
1719
struct list_head insn_list;

tools/objtool/special.c

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
#include "warn.h"
1717
#include "arch_special.h"
1818

19-
#define X86_FEATURE_POPCNT (4*32+23)
20-
#define X86_FEATURE_SMAP (9*32+20)
21-
2219
struct special_entry {
2320
const char *sec;
2421
bool group, jump_or_nop;
@@ -54,6 +51,10 @@ struct special_entry entries[] = {
5451
{},
5552
};
5653

54+
void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt)
55+
{
56+
}
57+
5758
static int get_alt_entry(struct elf *elf, struct special_entry *entry,
5859
struct section *sec, int idx,
5960
struct special_alt *alt)
@@ -78,30 +79,7 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
7879

7980
feature = *(unsigned short *)(sec->data->d_buf + offset +
8081
entry->feature);
81-
82-
/*
83-
* It has been requested that we don't validate the !POPCNT
84-
* feature path which is a "very very small percentage of
85-
* machines".
86-
*/
87-
if (feature == X86_FEATURE_POPCNT)
88-
alt->skip_orig = true;
89-
90-
/*
91-
* If UACCESS validation is enabled; force that alternative;
92-
* otherwise force it the other way.
93-
*
94-
* What we want to avoid is having both the original and the
95-
* alternative code flow at the same time, in that case we can
96-
* find paths that see the STAC but take the NOP instead of
97-
* CLAC and the other way around.
98-
*/
99-
if (feature == X86_FEATURE_SMAP) {
100-
if (uaccess)
101-
alt->skip_orig = true;
102-
else
103-
alt->skip_alt = true;
104-
}
82+
arch_handle_alternative(feature, alt);
10583
}
10684

10785
orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);

tools/objtool/special.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ struct special_alt {
2828

2929
int special_get_alts(struct elf *elf, struct list_head *alts);
3030

31+
void arch_handle_alternative(unsigned short feature, struct special_alt *alt);
32+
3133
#endif /* _SPECIAL_H */

tools/objtool/weak.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <errno.h>
1010
#include "objtool.h"
1111

12-
#define __weak __attribute__((weak))
13-
1412
#define UNSUPPORTED(name) \
1513
({ \
1614
fprintf(stderr, "error: objtool: " name " not implemented\n"); \

0 commit comments

Comments
 (0)