objtool: Teach get_alt_entry() about more relocation types

Occasionally objtool encounters symbol (as opposed to section)
relocations in .altinstructions. Typically they are the alternatives
written by elf_add_alternative() as encountered on a noinstr
validation run on vmlinux after having already ran objtool on the
individual .o files.

Basically this is the counterpart of commit 44f6a7c0755d ("objtool:
Fix seg fault with Clang non-section symbols"), because when these new
assemblers (binutils now also does this) strip the section symbols,
elf_add_reloc_to_insn() is forced to emit symbol based relocations.

As such, teach get_alt_entry() about different relocation types.

Fixes: 9bc0bb50727c ("objtool/x86: Rewrite retpoline thunk calls")
Reported-by: Stephen Rothwell <sfr@canb.auug.org.au>
Reported-by: Borislav Petkov <bp@alien8.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Josh Poimboeuf <jpoimboe@redhat.com>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Link: https://lore.kernel.org/r/YVWUvknIEVNkPvnP@hirez.programming.kicks-ass.net

+25 -7
+25 -7
tools/objtool/special.c
··· 58 58 { 59 59 } 60 60 61 + static bool reloc2sec_off(struct reloc *reloc, struct section **sec, unsigned long *off) 62 + { 63 + switch (reloc->sym->type) { 64 + case STT_FUNC: 65 + *sec = reloc->sym->sec; 66 + *off = reloc->sym->offset + reloc->addend; 67 + return true; 68 + 69 + case STT_SECTION: 70 + *sec = reloc->sym->sec; 71 + *off = reloc->addend; 72 + return true; 73 + 74 + default: 75 + return false; 76 + } 77 + } 78 + 61 79 static int get_alt_entry(struct elf *elf, struct special_entry *entry, 62 80 struct section *sec, int idx, 63 81 struct special_alt *alt) ··· 109 91 WARN_FUNC("can't find orig reloc", sec, offset + entry->orig); 110 92 return -1; 111 93 } 112 - if (orig_reloc->sym->type != STT_SECTION) { 113 - WARN_FUNC("don't know how to handle non-section reloc symbol %s", 94 + if (!reloc2sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off)) { 95 + WARN_FUNC("don't know how to handle reloc symbol type: %s", 114 96 sec, offset + entry->orig, orig_reloc->sym->name); 115 97 return -1; 116 98 } 117 - 118 - alt->orig_sec = orig_reloc->sym->sec; 119 - alt->orig_off = orig_reloc->addend; 120 99 121 100 if (!entry->group || alt->new_len) { 122 101 new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new); ··· 131 116 if (arch_is_retpoline(new_reloc->sym)) 132 117 return 1; 133 118 134 - alt->new_sec = new_reloc->sym->sec; 135 - alt->new_off = (unsigned int)new_reloc->addend; 119 + if (!reloc2sec_off(new_reloc, &alt->new_sec, &alt->new_off)) { 120 + WARN_FUNC("don't know how to handle reloc symbol type: %s", 121 + sec, offset + entry->new, new_reloc->sym->name); 122 + return -1; 123 + } 136 124 137 125 /* _ASM_EXTABLE_EX hack */ 138 126 if (alt->new_off >= 0x7ffffff0)