xref: /linux/tools/objtool/arch/x86/special.c (revision 54a012b6223580c74b77f3dc2a7c6b3c29084d18)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include <string.h>
3 
4 #include <objtool/special.h>
5 #include <objtool/builtin.h>
6 #include <objtool/warn.h>
7 
arch_handle_alternative(struct special_alt * alt)8 void arch_handle_alternative(struct special_alt *alt)
9 {
10 	static struct special_alt *group, *prev;
11 
12 	/*
13 	 * Recompute orig_len for nested ALTERNATIVE()s.
14 	 */
15 	if (group && group->orig_sec == alt->orig_sec &&
16 	             group->orig_off == alt->orig_off) {
17 
18 		struct special_alt *iter = group;
19 		for (;;) {
20 			unsigned int len = max(iter->orig_len, alt->orig_len);
21 			iter->orig_len = alt->orig_len = len;
22 
23 			if (iter == prev)
24 				break;
25 
26 			iter = list_next_entry(iter, list);
27 		}
28 
29 	} else group = alt;
30 
31 	prev = alt;
32 }
33 
arch_support_alt_relocation(struct special_alt * special_alt,struct instruction * insn,struct reloc * reloc)34 bool arch_support_alt_relocation(struct special_alt *special_alt,
35 				 struct instruction *insn,
36 				 struct reloc *reloc)
37 {
38 	return true;
39 }
40 
41 /*
42  * There are 3 basic jump table patterns:
43  *
44  * 1. jmpq *[rodata addr](,%reg,8)
45  *
46  *    This is the most common case by far.  It jumps to an address in a simple
47  *    jump table which is stored in .rodata.
48  *
49  * 2. jmpq *[rodata addr](%rip)
50  *
51  *    This is caused by a rare GCC quirk, currently only seen in three driver
52  *    functions in the kernel, only with certain obscure non-distro configs.
53  *
54  *    As part of an optimization, GCC makes a copy of an existing switch jump
55  *    table, modifies it, and then hard-codes the jump (albeit with an indirect
56  *    jump) to use a single entry in the table.  The rest of the jump table and
57  *    some of its jump targets remain as dead code.
58  *
59  *    In such a case we can just crudely ignore all unreachable instruction
60  *    warnings for the entire object file.  Ideally we would just ignore them
61  *    for the function, but that would require redesigning the code quite a
62  *    bit.  And honestly that's just not worth doing: unreachable instruction
63  *    warnings are of questionable value anyway, and this is such a rare issue.
64  *
65  * 3. mov [rodata addr],%reg1
66  *    ... some instructions ...
67  *    jmpq *(%reg1,%reg2,8)
68  *
69  *    This is a fairly uncommon pattern which is new for GCC 6.  As of this
70  *    writing, there are 11 occurrences of it in the allmodconfig kernel.
71  *
72  *    As of GCC 7 there are quite a few more of these and the 'in between' code
73  *    is significant. Esp. with KASAN enabled some of the code between the mov
74  *    and jmpq uses .rodata itself, which can confuse things.
75  *
76  *    TODO: Once we have DWARF CFI and smarter instruction decoding logic,
77  *    ensure the same register is used in the mov and jump instructions.
78  *
79  *    NOTE: MITIGATION_RETPOLINE made it harder still to decode dynamic jumps.
80  */
arch_find_switch_table(struct objtool_file * file,struct instruction * insn,unsigned long * table_size)81 struct reloc *arch_find_switch_table(struct objtool_file *file,
82 				     struct instruction *insn,
83 				     unsigned long *table_size)
84 {
85 	struct reloc  *text_reloc, *rodata_reloc;
86 	struct section *table_sec;
87 	unsigned long table_offset;
88 
89 	/* look for a relocation which references .rodata */
90 	text_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
91 					      insn->offset, insn->len);
92 	if (!text_reloc || text_reloc->sym->type != STT_SECTION ||
93 	    !text_reloc->sym->sec->rodata)
94 		return NULL;
95 
96 	table_offset = reloc_addend(text_reloc);
97 	table_sec = text_reloc->sym->sec;
98 
99 	if (reloc_type(text_reloc) == R_X86_64_PC32)
100 		table_offset += 4;
101 
102 	/*
103 	 * Make sure the .rodata address isn't associated with a
104 	 * symbol.  GCC jump tables are anonymous data.
105 	 *
106 	 * Also support C jump tables which are in the same format as
107 	 * switch jump tables.  For objtool to recognize them, they
108 	 * need to be placed in the C_JUMP_TABLE_SECTION section.  They
109 	 * have symbols associated with them.
110 	 */
111 	if (find_symbol_containing(table_sec, table_offset) &&
112 	    strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
113 		return NULL;
114 
115 	/*
116 	 * Each table entry has a rela associated with it.  The rela
117 	 * should reference text in the same function as the original
118 	 * instruction.
119 	 */
120 	rodata_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset);
121 	if (!rodata_reloc)
122 		return NULL;
123 
124 	/*
125 	 * Use of RIP-relative switch jumps is quite rare, and
126 	 * indicates a rare GCC quirk/bug which can leave dead
127 	 * code behind.
128 	 */
129 	if (!file->ignore_unreachables && reloc_type(text_reloc) == R_X86_64_PC32) {
130 		WARN_INSN(insn, "ignoring unreachables due to jump table quirk");
131 		file->ignore_unreachables = true;
132 	}
133 
134 	*table_size = 0;
135 	return rodata_reloc;
136 }
137