With CONFIG_CFI_CLANG, the compiler replaces function references with
references to the CFI jump table, which confuses objtool. This change,
based on Josh's initial patch [1], goes through the list of relocations
and replaces jump table symbols with the actual function symbols.

[1] 
https://lore.kernel.org/r/d743f4b36e120c06506567a9f87a062ae03da47f.1611263462.git.jpoim...@redhat.com/

Reported-by: Sedat Dilek <sedat.di...@gmail.com>
Suggested-by: Josh Poimboeuf <jpoim...@redhat.com>
Signed-off-by: Sami Tolvanen <samitolva...@google.com>
---
 tools/objtool/elf.c                 | 48 +++++++++++++++++++++++++++++
 tools/objtool/include/objtool/elf.h |  2 +-
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index d08f5f3670f8..5cf2c61ce7b8 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -273,6 +273,10 @@ static int read_sections(struct elf *elf)
                }
                sec->len = sec->sh.sh_size;
 
+               /* Detect -fsanitize=cfi jump table sections */
+               if (!strncmp(sec->name, ".text..L.cfi.jumptable", 22))
+                       sec->cfi_jt = true;
+
                list_add_tail(&sec->list, &elf->sections);
                elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
                elf_hash_add(elf->section_name_hash, &sec->name_hash, 
str_hash(sec->name));
@@ -548,6 +552,48 @@ static int read_rela_reloc(struct section *sec, int i, 
struct reloc *reloc, unsi
        return 0;
 }
 
+static int fix_cfi_relocs(const struct elf *elf)
+{
+       struct section *sec, *tmpsec;
+       struct reloc *reloc, *tmpreloc;
+
+       list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
+               list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, 
list) {
+                       struct symbol *sym;
+                       struct reloc *func_reloc;
+
+                       /*
+                        * CONFIG_CFI_CLANG replaces function relocations to 
refer
+                        * to an intermediate jump table. Undo the conversion so
+                        * objtool can make sense of things.
+                        */
+                       if (!reloc->sym->sec->cfi_jt)
+                               continue;
+
+                       if (reloc->sym->type == STT_SECTION)
+                               sym = find_func_by_offset(reloc->sym->sec,
+                                                         reloc->addend);
+                       else
+                               sym = reloc->sym;
+
+                       if (!sym || !sym->sec)
+                               continue;
+
+                       /*
+                        * The jump table immediately jumps to the actual 
function,
+                        * so look up the relocation there.
+                        */
+                       func_reloc = find_reloc_by_dest_range(elf, sym->sec, 
sym->offset, 5);
+                       if (!func_reloc || !func_reloc->sym)
+                               continue;
+
+                       reloc->sym = func_reloc->sym;
+               }
+       }
+
+       return 0;
+}
+
 static int read_relocs(struct elf *elf)
 {
        struct section *sec;
@@ -608,6 +654,8 @@ static int read_relocs(struct elf *elf)
                tot_reloc += nr_reloc;
        }
 
+       fix_cfi_relocs(elf);
+
        if (stats) {
                printf("max_reloc: %lu\n", max_reloc);
                printf("tot_reloc: %lu\n", tot_reloc);
diff --git a/tools/objtool/include/objtool/elf.h 
b/tools/objtool/include/objtool/elf.h
index 45e5ede363b0..ef19578fc5e4 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -39,7 +39,7 @@ struct section {
        char *name;
        int idx;
        unsigned int len;
-       bool changed, text, rodata, noinstr;
+       bool changed, text, rodata, noinstr, cfi_jt;
 };
 
 struct symbol {
-- 
2.31.1.368.gbe11c130af-goog

Reply via email to