objtool: Improve call destination function detection
authorJosh Poimboeuf <jpoimboe@redhat.com>
Tue, 18 Feb 2020 03:41:54 +0000 (21:41 -0600)
committerBorislav Petkov <bp@suse.de>
Fri, 21 Feb 2020 09:20:34 +0000 (10:20 +0100)
A recent clang change, combined with a binutils bug, can trigger a
situation where a ".Lprintk$local" STT_NOTYPE symbol gets created at the
same offset as the "printk" STT_FUNC symbol.  This confuses objtool:

  kernel/printk/printk.o: warning: objtool: ignore_loglevel_setup()+0x10: can't find call dest symbol at .text+0xc67

Improve the call destination detection by looking specifically for an
STT_FUNC symbol.

Reported-by: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Nathan Chancellor <natechancellor@gmail.com>
Link: https://github.com/ClangBuiltLinux/linux/issues/872
Link: https://sourceware.org/bugzilla/show_bug.cgi?id=25551
Link: https://lkml.kernel.org/r/0a7ee320bc0ea4469bd3dc450a7b4725669e0ea9.1581997059.git.jpoimboe@redhat.com
tools/objtool/check.c
tools/objtool/elf.c
tools/objtool/elf.h

index 4d6e283..6b6178e 100644 (file)
@@ -420,8 +420,8 @@ static void add_ignores(struct objtool_file *file)
                        break;
 
                case STT_SECTION:
-                       func = find_symbol_by_offset(rela->sym->sec, rela->addend);
-                       if (!func || func->type != STT_FUNC)
+                       func = find_func_by_offset(rela->sym->sec, rela->addend);
+                       if (!func)
                                continue;
                        break;
 
@@ -665,10 +665,14 @@ static int add_call_destinations(struct objtool_file *file)
                                               insn->len);
                if (!rela) {
                        dest_off = insn->offset + insn->len + insn->immediate;
-                       insn->call_dest = find_symbol_by_offset(insn->sec,
-                                                               dest_off);
+                       insn->call_dest = find_func_by_offset(insn->sec, dest_off);
+                       if (!insn->call_dest)
+                               insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
 
-                       if (!insn->call_dest && !insn->ignore) {
+                       if (insn->ignore)
+                               continue;
+
+                       if (!insn->call_dest) {
                                WARN_FUNC("unsupported intra-function call",
                                          insn->sec, insn->offset);
                                if (retpoline)
@@ -676,11 +680,16 @@ static int add_call_destinations(struct objtool_file *file)
                                return -1;
                        }
 
+                       if (insn->func && insn->call_dest->type != STT_FUNC) {
+                               WARN_FUNC("unsupported call to non-function",
+                                         insn->sec, insn->offset);
+                               return -1;
+                       }
+
                } else if (rela->sym->type == STT_SECTION) {
-                       insn->call_dest = find_symbol_by_offset(rela->sym->sec,
-                                                               rela->addend+4);
-                       if (!insn->call_dest ||
-                           insn->call_dest->type != STT_FUNC) {
+                       insn->call_dest = find_func_by_offset(rela->sym->sec,
+                                                             rela->addend+4);
+                       if (!insn->call_dest) {
                                WARN_FUNC("can't find call dest symbol at %s+0x%x",
                                          insn->sec, insn->offset,
                                          rela->sym->sec->name,
index edba474..cc4601c 100644 (file)
@@ -62,8 +62,18 @@ struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
        struct symbol *sym;
 
        list_for_each_entry(sym, &sec->symbol_list, list)
-               if (sym->type != STT_SECTION &&
-                   sym->offset == offset)
+               if (sym->type != STT_SECTION && sym->offset == offset)
+                       return sym;
+
+       return NULL;
+}
+
+struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
+{
+       struct symbol *sym;
+
+       list_for_each_entry(sym, &sec->symbol_list, list)
+               if (sym->type == STT_FUNC && sym->offset == offset)
                        return sym;
 
        return NULL;
index 4415020..a196325 100644 (file)
@@ -77,6 +77,7 @@ struct elf {
 
 struct elf *elf_read(const char *name, int flags);
 struct section *find_section_by_name(struct elf *elf, const char *name);
+struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
 struct symbol *find_symbol_by_name(struct elf *elf, const char *name);
 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);