Commit 7e578441 authored by Josh Poimboeuf's avatar Josh Poimboeuf Committed by Ingo Molnar

objtool: Add workaround for GCC switch jump table bug

GCC has a rare quirk, currently only seen in three driver functions in
the kernel, and only with certain obscure non-distro configs, which can
cause objtool to produce "unreachable instruction" false positive
warnings.

As part of an optimization, GCC makes a copy of an existing switch jump
table, modifies it, and then hard-codes the jump (albeit with an
indirect jump) to use a single entry in the table.  The rest of the jump
table and some of its jump targets remain as dead code.

In such a case we can just crudely ignore all unreachable instruction
warnings for the entire object file.  Ideally we would just ignore them
for the function, but that would require redesigning the code quite a
bit.  And honestly that's just not worth doing: unreachable instruction
warnings are of questionable value anyway, and this is a very rare
issue.

kbuild reports:

  https://lkml.kernel.org/r/201603231906.LWcVUpxm%25fengguang.wu@intel.com
  https://lkml.kernel.org/r/201603271114.K9i45biy%25fengguang.wu@intel.com
  https://lkml.kernel.org/r/201603291058.zuJ6ben1%25fengguang.wu@intel.com

GCC bug:

  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70604Reported-by: default avatarkbuild test robot <fengguang.wu@intel.com>
Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/700fa029bbb0feff34f03ffc69d666a3c3b57a61.1460663532.git.jpoimboe@redhat.comSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent 806fdcce
...@@ -66,6 +66,7 @@ struct objtool_file { ...@@ -66,6 +66,7 @@ struct objtool_file {
struct list_head insn_list; struct list_head insn_list;
DECLARE_HASHTABLE(insn_hash, 16); DECLARE_HASHTABLE(insn_hash, 16);
struct section *rodata, *whitelist; struct section *rodata, *whitelist;
bool ignore_unreachables;
}; };
const char *objname; const char *objname;
...@@ -664,13 +665,40 @@ static int add_func_switch_tables(struct objtool_file *file, ...@@ -664,13 +665,40 @@ static int add_func_switch_tables(struct objtool_file *file,
text_rela->addend); text_rela->addend);
/* /*
* TODO: Document where this is needed, or get rid of it.
*
* rare case: jmpq *[addr](%rip) * rare case: jmpq *[addr](%rip)
*
* This check is for a rare gcc quirk, currently only seen in
* three driver functions in the kernel, only with certain
* obscure non-distro configs.
*
* As part of an optimization, gcc makes a copy of an existing
* switch jump table, modifies it, and then hard-codes the jump
* (albeit with an indirect jump) to use a single entry in the
* table. The rest of the jump table and some of its jump
* targets remain as dead code.
*
* In such a case we can just crudely ignore all unreachable
* instruction warnings for the entire object file. Ideally we
* would just ignore them for the function, but that would
* require redesigning the code quite a bit. And honestly
* that's just not worth doing: unreachable instruction
* warnings are of questionable value anyway, and this is such
* a rare issue.
*
* kbuild reports:
* - https://lkml.kernel.org/r/201603231906.LWcVUpxm%25fengguang.wu@intel.com
* - https://lkml.kernel.org/r/201603271114.K9i45biy%25fengguang.wu@intel.com
* - https://lkml.kernel.org/r/201603291058.zuJ6ben1%25fengguang.wu@intel.com
*
* gcc bug:
* - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70604
*/ */
if (!rodata_rela) if (!rodata_rela) {
rodata_rela = find_rela_by_dest(file->rodata, rodata_rela = find_rela_by_dest(file->rodata,
text_rela->addend + 4); text_rela->addend + 4);
if (rodata_rela)
file->ignore_unreachables = true;
}
if (!rodata_rela) if (!rodata_rela)
continue; continue;
...@@ -732,9 +760,6 @@ static int decode_sections(struct objtool_file *file) ...@@ -732,9 +760,6 @@ static int decode_sections(struct objtool_file *file)
{ {
int ret; int ret;
file->whitelist = find_section_by_name(file->elf, "__func_stack_frame_non_standard");
file->rodata = find_section_by_name(file->elf, ".rodata");
ret = decode_instructions(file); ret = decode_instructions(file);
if (ret) if (ret)
return ret; return ret;
...@@ -1056,13 +1081,14 @@ static int validate_functions(struct objtool_file *file) ...@@ -1056,13 +1081,14 @@ static int validate_functions(struct objtool_file *file)
if (insn->visited) if (insn->visited)
continue; continue;
if (!ignore_unreachable_insn(func, insn) &&
!warnings) {
WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset);
warnings++;
}
insn->visited = true; insn->visited = true;
if (file->ignore_unreachables || warnings ||
ignore_unreachable_insn(func, insn))
continue;
WARN_FUNC("function has unreachable instruction", insn->sec, insn->offset);
warnings++;
} }
} }
} }
...@@ -1133,6 +1159,9 @@ int cmd_check(int argc, const char **argv) ...@@ -1133,6 +1159,9 @@ int cmd_check(int argc, const char **argv)
INIT_LIST_HEAD(&file.insn_list); INIT_LIST_HEAD(&file.insn_list);
hash_init(file.insn_hash); hash_init(file.insn_hash);
file.whitelist = find_section_by_name(file.elf, "__func_stack_frame_non_standard");
file.rodata = find_section_by_name(file.elf, ".rodata");
file.ignore_unreachables = false;
ret = decode_sections(&file); ret = decode_sections(&file);
if (ret < 0) if (ret < 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment