1eda3dc90SJulien Thierry // SPDX-License-Identifier: GPL-2.0-or-later
2d871f7b5SRaphael Gault #include <string.h>
3d871f7b5SRaphael Gault
47786032eSVasily Gorbik #include <objtool/special.h>
57786032eSVasily Gorbik #include <objtool/builtin.h>
6eda3dc90SJulien Thierry
7eda3dc90SJulien Thierry #define X86_FEATURE_POPCNT (4 * 32 + 23)
8eda3dc90SJulien Thierry #define X86_FEATURE_SMAP (9 * 32 + 20)
9eda3dc90SJulien Thierry
arch_handle_alternative(unsigned short feature,struct special_alt * alt)10eda3dc90SJulien Thierry void arch_handle_alternative(unsigned short feature, struct special_alt *alt)
11eda3dc90SJulien Thierry {
12eda3dc90SJulien Thierry switch (feature) {
13eda3dc90SJulien Thierry case X86_FEATURE_SMAP:
14eda3dc90SJulien Thierry /*
15eda3dc90SJulien Thierry * If UACCESS validation is enabled; force that alternative;
16eda3dc90SJulien Thierry * otherwise force it the other way.
17eda3dc90SJulien Thierry *
18eda3dc90SJulien Thierry * What we want to avoid is having both the original and the
19eda3dc90SJulien Thierry * alternative code flow at the same time, in that case we can
20eda3dc90SJulien Thierry * find paths that see the STAC but take the NOP instead of
21eda3dc90SJulien Thierry * CLAC and the other way around.
22eda3dc90SJulien Thierry */
232daf7fabSJosh Poimboeuf if (opts.uaccess)
24eda3dc90SJulien Thierry alt->skip_orig = true;
25eda3dc90SJulien Thierry else
26eda3dc90SJulien Thierry alt->skip_alt = true;
27eda3dc90SJulien Thierry break;
28eda3dc90SJulien Thierry case X86_FEATURE_POPCNT:
29eda3dc90SJulien Thierry /*
30eda3dc90SJulien Thierry * It has been requested that we don't validate the !POPCNT
31eda3dc90SJulien Thierry * feature path which is a "very very small percentage of
32eda3dc90SJulien Thierry * machines".
33eda3dc90SJulien Thierry */
34eda3dc90SJulien Thierry alt->skip_orig = true;
35eda3dc90SJulien Thierry break;
36eda3dc90SJulien Thierry default:
37eda3dc90SJulien Thierry break;
38eda3dc90SJulien Thierry }
39eda3dc90SJulien Thierry }
4045245f51SJulien Thierry
arch_support_alt_relocation(struct special_alt * special_alt,struct instruction * insn,struct reloc * reloc)4145245f51SJulien Thierry bool arch_support_alt_relocation(struct special_alt *special_alt,
4245245f51SJulien Thierry struct instruction *insn,
4345245f51SJulien Thierry struct reloc *reloc)
4445245f51SJulien Thierry {
45270a69c4SPeter Zijlstra return true;
4645245f51SJulien Thierry }
47d871f7b5SRaphael Gault
48d871f7b5SRaphael Gault /*
49d871f7b5SRaphael Gault * There are 3 basic jump table patterns:
50d871f7b5SRaphael Gault *
51d871f7b5SRaphael Gault * 1. jmpq *[rodata addr](,%reg,8)
52d871f7b5SRaphael Gault *
53d871f7b5SRaphael Gault * This is the most common case by far. It jumps to an address in a simple
54d871f7b5SRaphael Gault * jump table which is stored in .rodata.
55d871f7b5SRaphael Gault *
56d871f7b5SRaphael Gault * 2. jmpq *[rodata addr](%rip)
57d871f7b5SRaphael Gault *
58d871f7b5SRaphael Gault * This is caused by a rare GCC quirk, currently only seen in three driver
59d871f7b5SRaphael Gault * functions in the kernel, only with certain obscure non-distro configs.
60d871f7b5SRaphael Gault *
61d871f7b5SRaphael Gault * As part of an optimization, GCC makes a copy of an existing switch jump
62d871f7b5SRaphael Gault * table, modifies it, and then hard-codes the jump (albeit with an indirect
63d871f7b5SRaphael Gault * jump) to use a single entry in the table. The rest of the jump table and
64d871f7b5SRaphael Gault * some of its jump targets remain as dead code.
65d871f7b5SRaphael Gault *
66d871f7b5SRaphael Gault * In such a case we can just crudely ignore all unreachable instruction
67d871f7b5SRaphael Gault * warnings for the entire object file. Ideally we would just ignore them
68d871f7b5SRaphael Gault * for the function, but that would require redesigning the code quite a
69d871f7b5SRaphael Gault * bit. And honestly that's just not worth doing: unreachable instruction
70d871f7b5SRaphael Gault * warnings are of questionable value anyway, and this is such a rare issue.
71d871f7b5SRaphael Gault *
72d871f7b5SRaphael Gault * 3. mov [rodata addr],%reg1
73d871f7b5SRaphael Gault * ... some instructions ...
74d871f7b5SRaphael Gault * jmpq *(%reg1,%reg2,8)
75d871f7b5SRaphael Gault *
76d871f7b5SRaphael Gault * This is a fairly uncommon pattern which is new for GCC 6. As of this
77d871f7b5SRaphael Gault * writing, there are 11 occurrences of it in the allmodconfig kernel.
78d871f7b5SRaphael Gault *
79d871f7b5SRaphael Gault * As of GCC 7 there are quite a few more of these and the 'in between' code
80d871f7b5SRaphael Gault * is significant. Esp. with KASAN enabled some of the code between the mov
81d871f7b5SRaphael Gault * and jmpq uses .rodata itself, which can confuse things.
82d871f7b5SRaphael Gault *
83d871f7b5SRaphael Gault * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
84d871f7b5SRaphael Gault * ensure the same register is used in the mov and jump instructions.
85d871f7b5SRaphael Gault *
86d871f7b5SRaphael Gault * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
87d871f7b5SRaphael Gault */
arch_find_switch_table(struct objtool_file * file,struct instruction * insn)88d871f7b5SRaphael Gault struct reloc *arch_find_switch_table(struct objtool_file *file,
89d871f7b5SRaphael Gault struct instruction *insn)
90d871f7b5SRaphael Gault {
91d871f7b5SRaphael Gault struct reloc *text_reloc, *rodata_reloc;
92d871f7b5SRaphael Gault struct section *table_sec;
93d871f7b5SRaphael Gault unsigned long table_offset;
94d871f7b5SRaphael Gault
95d871f7b5SRaphael Gault /* look for a relocation which references .rodata */
96d871f7b5SRaphael Gault text_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
97d871f7b5SRaphael Gault insn->offset, insn->len);
98d871f7b5SRaphael Gault if (!text_reloc || text_reloc->sym->type != STT_SECTION ||
99d871f7b5SRaphael Gault !text_reloc->sym->sec->rodata)
100d871f7b5SRaphael Gault return NULL;
101d871f7b5SRaphael Gault
102*0696b6e3SJosh Poimboeuf table_offset = reloc_addend(text_reloc);
103d871f7b5SRaphael Gault table_sec = text_reloc->sym->sec;
104d871f7b5SRaphael Gault
105fcee899dSJosh Poimboeuf if (reloc_type(text_reloc) == R_X86_64_PC32)
106d871f7b5SRaphael Gault table_offset += 4;
107d871f7b5SRaphael Gault
108d871f7b5SRaphael Gault /*
109d871f7b5SRaphael Gault * Make sure the .rodata address isn't associated with a
110d871f7b5SRaphael Gault * symbol. GCC jump tables are anonymous data.
111d871f7b5SRaphael Gault *
112d871f7b5SRaphael Gault * Also support C jump tables which are in the same format as
113d871f7b5SRaphael Gault * switch jump tables. For objtool to recognize them, they
114d871f7b5SRaphael Gault * need to be placed in the C_JUMP_TABLE_SECTION section. They
115d871f7b5SRaphael Gault * have symbols associated with them.
116d871f7b5SRaphael Gault */
117d871f7b5SRaphael Gault if (find_symbol_containing(table_sec, table_offset) &&
118d871f7b5SRaphael Gault strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
119d871f7b5SRaphael Gault return NULL;
120d871f7b5SRaphael Gault
121d871f7b5SRaphael Gault /*
122d871f7b5SRaphael Gault * Each table entry has a rela associated with it. The rela
123d871f7b5SRaphael Gault * should reference text in the same function as the original
124d871f7b5SRaphael Gault * instruction.
125d871f7b5SRaphael Gault */
126d871f7b5SRaphael Gault rodata_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset);
127d871f7b5SRaphael Gault if (!rodata_reloc)
128d871f7b5SRaphael Gault return NULL;
129d871f7b5SRaphael Gault
130d871f7b5SRaphael Gault /*
131d871f7b5SRaphael Gault * Use of RIP-relative switch jumps is quite rare, and
132d871f7b5SRaphael Gault * indicates a rare GCC quirk/bug which can leave dead
133d871f7b5SRaphael Gault * code behind.
134d871f7b5SRaphael Gault */
135fcee899dSJosh Poimboeuf if (reloc_type(text_reloc) == R_X86_64_PC32)
136d871f7b5SRaphael Gault file->ignore_unreachables = true;
137d871f7b5SRaphael Gault
138d871f7b5SRaphael Gault return rodata_reloc;
139d871f7b5SRaphael Gault }
140