// SPDX-License-Identifier: GPL-2.0-or-later1#include <string.h>23#include <objtool/special.h>4#include <objtool/builtin.h>5#include <objtool/warn.h>67void arch_handle_alternative(struct special_alt *alt)8{9static struct special_alt *group, *prev;1011/*12* Recompute orig_len for nested ALTERNATIVE()s.13*/14if (group && group->orig_sec == alt->orig_sec &&15group->orig_off == alt->orig_off) {1617struct special_alt *iter = group;18for (;;) {19unsigned int len = max(iter->orig_len, alt->orig_len);20iter->orig_len = alt->orig_len = len;2122if (iter == prev)23break;2425iter = list_next_entry(iter, list);26}2728} else group = alt;2930prev = alt;31}3233bool arch_support_alt_relocation(struct special_alt *special_alt,34struct instruction *insn,35struct reloc *reloc)36{37return true;38}3940/*41* There are 3 basic jump table patterns:42*43* 1. jmpq *[rodata addr](,%reg,8)44*45* This is the most common case by far. It jumps to an address in a simple46* jump table which is stored in .rodata.47*48* 2. jmpq *[rodata addr](%rip)49*50* This is caused by a rare GCC quirk, currently only seen in three driver51* functions in the kernel, only with certain obscure non-distro configs.52*53* As part of an optimization, GCC makes a copy of an existing switch jump54* table, modifies it, and then hard-codes the jump (albeit with an indirect55* jump) to use a single entry in the table. The rest of the jump table and56* some of its jump targets remain as dead code.57*58* In such a case we can just crudely ignore all unreachable instruction59* warnings for the entire object file. Ideally we would just ignore them60* for the function, but that would require redesigning the code quite a61* bit. And honestly that's just not worth doing: unreachable instruction62* warnings are of questionable value anyway, and this is such a rare issue.63*64* 3. mov [rodata addr],%reg165* ... some instructions ...66* jmpq *(%reg1,%reg2,8)67*68* This is a fairly uncommon pattern which is new for GCC 6. As of this69* writing, there are 11 occurrences of it in the allmodconfig kernel.70*71* As of GCC 7 there are quite a few more of these and the 'in between' code72* is significant. Esp. with KASAN enabled some of the code between the mov73* and jmpq uses .rodata itself, which can confuse things.74*75* TODO: Once we have DWARF CFI and smarter instruction decoding logic,76* ensure the same register is used in the mov and jump instructions.77*78* NOTE: MITIGATION_RETPOLINE made it harder still to decode dynamic jumps.79*/80struct reloc *arch_find_switch_table(struct objtool_file *file,81struct instruction *insn,82unsigned long *table_size)83{84struct reloc *text_reloc, *rodata_reloc;85struct section *table_sec;86unsigned long table_offset;8788/* look for a relocation which references .rodata */89text_reloc = find_reloc_by_dest_range(file->elf, insn->sec,90insn->offset, insn->len);91if (!text_reloc || text_reloc->sym->type != STT_SECTION ||92!text_reloc->sym->sec->rodata)93return NULL;9495table_offset = reloc_addend(text_reloc);96table_sec = text_reloc->sym->sec;9798if (reloc_type(text_reloc) == R_X86_64_PC32)99table_offset += 4;100101/*102* Make sure the .rodata address isn't associated with a103* symbol. GCC jump tables are anonymous data.104*105* Also support C jump tables which are in the same format as106* switch jump tables. For objtool to recognize them, they107* need to be placed in the C_JUMP_TABLE_SECTION section. They108* have symbols associated with them.109*/110if (find_symbol_containing(table_sec, table_offset) &&111strcmp(table_sec->name, C_JUMP_TABLE_SECTION))112return NULL;113114/*115* Each table entry has a rela associated with it. The rela116* should reference text in the same function as the original117* instruction.118*/119rodata_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset);120if (!rodata_reloc)121return NULL;122123/*124* Use of RIP-relative switch jumps is quite rare, and125* indicates a rare GCC quirk/bug which can leave dead126* code behind.127*/128if (!file->ignore_unreachables && reloc_type(text_reloc) == R_X86_64_PC32) {129WARN_INSN(insn, "ignoring unreachables due to jump table quirk");130file->ignore_unreachables = true;131}132133*table_size = 0;134return rodata_reloc;135}136137138