2020
2121#define FAKE_JUMP_OFFSET -1
2222
23- #define C_JUMP_TABLE_SECTION ".rodata..c_jump_table"
24-
2523struct alternative {
2624 struct list_head list ;
2725 struct instruction * insn ;
@@ -1190,56 +1188,15 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
11901188}
11911189
11921190/*
1193- * find_jump_table() - Given a dynamic jump, find the switch jump table in
1194- * .rodata associated with it.
1195- *
1196- * There are 3 basic patterns:
1197- *
1198- * 1. jmpq *[rodata addr](,%reg,8)
1199- *
1200- * This is the most common case by far. It jumps to an address in a simple
1201- * jump table which is stored in .rodata.
1202- *
1203- * 2. jmpq *[rodata addr](%rip)
1204- *
1205- * This is caused by a rare GCC quirk, currently only seen in three driver
1206- * functions in the kernel, only with certain obscure non-distro configs.
1207- *
1208- * As part of an optimization, GCC makes a copy of an existing switch jump
1209- * table, modifies it, and then hard-codes the jump (albeit with an indirect
1210- * jump) to use a single entry in the table. The rest of the jump table and
1211- * some of its jump targets remain as dead code.
1212- *
1213- * In such a case we can just crudely ignore all unreachable instruction
1214- * warnings for the entire object file. Ideally we would just ignore them
1215- * for the function, but that would require redesigning the code quite a
1216- * bit. And honestly that's just not worth doing: unreachable instruction
1217- * warnings are of questionable value anyway, and this is such a rare issue.
1218- *
1219- * 3. mov [rodata addr],%reg1
1220- * ... some instructions ...
1221- * jmpq *(%reg1,%reg2,8)
1222- *
1223- * This is a fairly uncommon pattern which is new for GCC 6. As of this
1224- * writing, there are 11 occurrences of it in the allmodconfig kernel.
1225- *
1226- * As of GCC 7 there are quite a few more of these and the 'in between' code
1227- * is significant. Esp. with KASAN enabled some of the code between the mov
1228- * and jmpq uses .rodata itself, which can confuse things.
1229- *
1230- * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
1231- * ensure the same register is used in the mov and jump instructions.
1232- *
1233- * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
1191+ * find_jump_table() - Given a dynamic jump, find the switch jump table
1192+ * associated with it.
12341193 */
12351194static struct reloc * find_jump_table (struct objtool_file * file ,
12361195 struct symbol * func ,
12371196 struct instruction * insn )
12381197{
1239- struct reloc * text_reloc , * table_reloc ;
1198+ struct reloc * table_reloc ;
12401199 struct instruction * dest_insn , * orig_insn = insn ;
1241- struct section * table_sec ;
1242- unsigned long table_offset ;
12431200
12441201 /*
12451202 * Backward search using the @first_jump_src links, these help avoid
@@ -1260,52 +1217,13 @@ static struct reloc *find_jump_table(struct objtool_file *file,
12601217 insn -> jump_dest -> offset > orig_insn -> offset ))
12611218 break ;
12621219
1263- /* look for a relocation which references .rodata */
1264- text_reloc = find_reloc_by_dest_range (file -> elf , insn -> sec ,
1265- insn -> offset , insn -> len );
1266- if (!text_reloc || text_reloc -> sym -> type != STT_SECTION ||
1267- !text_reloc -> sym -> sec -> rodata )
1268- continue ;
1269-
1270- table_offset = text_reloc -> addend ;
1271- table_sec = text_reloc -> sym -> sec ;
1272-
1273- if (text_reloc -> type == R_X86_64_PC32 )
1274- table_offset += 4 ;
1275-
1276- /*
1277- * Make sure the .rodata address isn't associated with a
1278- * symbol. GCC jump tables are anonymous data.
1279- *
1280- * Also support C jump tables which are in the same format as
1281- * switch jump tables. For objtool to recognize them, they
1282- * need to be placed in the C_JUMP_TABLE_SECTION section. They
1283- * have symbols associated with them.
1284- */
1285- if (find_symbol_containing (table_sec , table_offset ) &&
1286- strcmp (table_sec -> name , C_JUMP_TABLE_SECTION ))
1287- continue ;
1288-
1289- /*
1290- * Each table entry has a reloc associated with it. The reloc
1291- * should reference text in the same function as the original
1292- * instruction.
1293- */
1294- table_reloc = find_reloc_by_dest (file -> elf , table_sec , table_offset );
1220+ table_reloc = arch_find_switch_table (file , insn );
12951221 if (!table_reloc )
12961222 continue ;
12971223 dest_insn = find_insn (file , table_reloc -> sym -> sec , table_reloc -> addend );
12981224 if (!dest_insn || !dest_insn -> func || dest_insn -> func -> pfunc != func )
12991225 continue ;
13001226
1301- /*
1302- * Use of RIP-relative switch jumps is quite rare, and
1303- * indicates a rare GCC quirk/bug which can leave dead code
1304- * behind.
1305- */
1306- if (text_reloc -> type == R_X86_64_PC32 )
1307- file -> ignore_unreachables = true;
1308-
13091227 return table_reloc ;
13101228 }
13111229
0 commit comments