// 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>6#include <asm/cpufeatures.h>78/* cpu feature name array generated from cpufeatures.h */9#include "cpu-feature-names.c"1011void arch_handle_alternative(struct special_alt *alt)12{13static struct special_alt *group, *prev;1415/*16* Recompute orig_len for nested ALTERNATIVE()s.17*/18if (group && group->orig_sec == alt->orig_sec &&19group->orig_off == alt->orig_off) {2021struct special_alt *iter = group;22for (;;) {23unsigned int len = max(iter->orig_len, alt->orig_len);24iter->orig_len = alt->orig_len = len;2526if (iter == prev)27break;2829iter = list_next_entry(iter, list);30}3132} else group = alt;3334prev = alt;35}3637bool arch_support_alt_relocation(struct special_alt *special_alt,38struct instruction *insn,39struct reloc *reloc)40{41return true;42}4344/*45* There are 3 basic jump table patterns:46*47* 1. jmpq *[rodata addr](,%reg,8)48*49* This is the most common case by far. It jumps to an address in a simple50* jump table which is stored in .rodata.51*52* 2. jmpq *[rodata addr](%rip)53*54* This is caused by a rare GCC quirk, currently only seen in three driver55* functions in the kernel, only with certain obscure non-distro configs.56*57* As part of an optimization, GCC makes a copy of an existing switch jump58* table, modifies it, and then hard-codes the jump (albeit with an indirect59* jump) to use a single entry in the table. The rest of the jump table and60* some of its jump targets remain as dead code.61*62* In such a case we can just crudely ignore all unreachable instruction63* warnings for the entire object file. Ideally we would just ignore them64* for the function, but that would require redesigning the code quite a65* bit. And honestly that's just not worth doing: unreachable instruction66* warnings are of questionable value anyway, and this is such a rare issue.67*68* 3. mov [rodata addr],%reg169* ... some instructions ...70* jmpq *(%reg1,%reg2,8)71*72* This is a fairly uncommon pattern which is new for GCC 6. As of this73* writing, there are 11 occurrences of it in the allmodconfig kernel.74*75* As of GCC 7 there are quite a few more of these and the 'in between' code76* is significant. Esp. with KASAN enabled some of the code between the mov77* and jmpq uses .rodata itself, which can confuse things.78*79* TODO: Once we have DWARF CFI and smarter instruction decoding logic,80* ensure the same register is used in the mov and jump instructions.81*82* NOTE: MITIGATION_RETPOLINE made it harder still to decode dynamic jumps.83*/84struct reloc *arch_find_switch_table(struct objtool_file *file,85struct instruction *insn,86unsigned long *table_size)87{88struct reloc *text_reloc, *rodata_reloc;89struct section *table_sec;90unsigned long table_offset;9192/* look for a relocation which references .rodata */93text_reloc = find_reloc_by_dest_range(file->elf, insn->sec,94insn->offset, insn->len);95if (!text_reloc || !is_sec_sym(text_reloc->sym) ||96!text_reloc->sym->sec->rodata)97return NULL;9899table_offset = reloc_addend(text_reloc);100table_sec = text_reloc->sym->sec;101102if (reloc_type(text_reloc) == R_X86_64_PC32)103table_offset += 4;104105/*106* Make sure the .rodata address isn't associated with a107* symbol. GCC jump tables are anonymous data.108*109* Also support C jump tables which are in the same format as110* switch jump tables. For objtool to recognize them, they111* need to be placed in the C_JUMP_TABLE_SECTION section. They112* have symbols associated with them.113*/114if (find_symbol_containing(table_sec, table_offset) &&115strcmp(table_sec->name, C_JUMP_TABLE_SECTION))116return NULL;117118/*119* Each table entry has a rela associated with it. The rela120* should reference text in the same function as the original121* instruction.122*/123rodata_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset);124if (!rodata_reloc)125return NULL;126127/*128* Use of RIP-relative switch jumps is quite rare, and129* indicates a rare GCC quirk/bug which can leave dead130* code behind.131*/132if (!file->ignore_unreachables && reloc_type(text_reloc) == R_X86_64_PC32) {133WARN_INSN(insn, "ignoring unreachables due to jump table quirk");134file->ignore_unreachables = true;135}136137*table_size = 0;138return rodata_reloc;139}140141const char *arch_cpu_feature_name(int feature_number)142{143return (feature_number < ARRAY_SIZE(cpu_feature_names)) ?144cpu_feature_names[feature_number] : NULL;145}146147148