special.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <string.h>
  3. #include <objtool/special.h>
  4. #include <objtool/builtin.h>
  5. #include <objtool/warn.h>
  6. #include <asm/cpufeatures.h>
  7. /* cpu feature name array generated from cpufeatures.h */
  8. #include "cpu-feature-names.c"
  9. void arch_handle_alternative(struct special_alt *alt)
  10. {
  11. static struct special_alt *group, *prev;
  12. /*
  13. * Recompute orig_len for nested ALTERNATIVE()s.
  14. */
  15. if (group && group->orig_sec == alt->orig_sec &&
  16. group->orig_off == alt->orig_off) {
  17. struct special_alt *iter = group;
  18. for (;;) {
  19. unsigned int len = max(iter->orig_len, alt->orig_len);
  20. iter->orig_len = alt->orig_len = len;
  21. if (iter == prev)
  22. break;
  23. iter = list_next_entry(iter, list);
  24. }
  25. } else group = alt;
  26. prev = alt;
  27. }
  28. bool arch_support_alt_relocation(struct special_alt *special_alt,
  29. struct instruction *insn,
  30. struct reloc *reloc)
  31. {
  32. return true;
  33. }
  34. /*
  35. * There are 3 basic jump table patterns:
  36. *
  37. * 1. jmpq *[rodata addr](,%reg,8)
  38. *
  39. * This is the most common case by far. It jumps to an address in a simple
  40. * jump table which is stored in .rodata.
  41. *
  42. * 2. jmpq *[rodata addr](%rip)
  43. *
  44. * This is caused by a rare GCC quirk, currently only seen in three driver
  45. * functions in the kernel, only with certain obscure non-distro configs.
  46. *
  47. * As part of an optimization, GCC makes a copy of an existing switch jump
  48. * table, modifies it, and then hard-codes the jump (albeit with an indirect
  49. * jump) to use a single entry in the table. The rest of the jump table and
  50. * some of its jump targets remain as dead code.
  51. *
  52. * In such a case we can just crudely ignore all unreachable instruction
  53. * warnings for the entire object file. Ideally we would just ignore them
  54. * for the function, but that would require redesigning the code quite a
  55. * bit. And honestly that's just not worth doing: unreachable instruction
  56. * warnings are of questionable value anyway, and this is such a rare issue.
  57. *
  58. * 3. mov [rodata addr],%reg1
  59. * ... some instructions ...
  60. * jmpq *(%reg1,%reg2,8)
  61. *
  62. * This is a fairly uncommon pattern which is new for GCC 6. As of this
  63. * writing, there are 11 occurrences of it in the allmodconfig kernel.
  64. *
  65. * As of GCC 7 there are quite a few more of these and the 'in between' code
  66. * is significant. Esp. with KASAN enabled some of the code between the mov
  67. * and jmpq uses .rodata itself, which can confuse things.
  68. *
  69. * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
  70. * ensure the same register is used in the mov and jump instructions.
  71. *
  72. * NOTE: MITIGATION_RETPOLINE made it harder still to decode dynamic jumps.
  73. */
  74. struct reloc *arch_find_switch_table(struct objtool_file *file,
  75. struct instruction *insn,
  76. unsigned long *table_size)
  77. {
  78. struct reloc *text_reloc, *rodata_reloc;
  79. struct section *table_sec;
  80. unsigned long table_offset;
  81. /* look for a relocation which references .rodata */
  82. text_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
  83. insn->offset, insn->len);
  84. if (!text_reloc || !is_sec_sym(text_reloc->sym) ||
  85. !text_reloc->sym->sec->rodata)
  86. return NULL;
  87. table_offset = reloc_addend(text_reloc);
  88. table_sec = text_reloc->sym->sec;
  89. if (reloc_type(text_reloc) == R_X86_64_PC32)
  90. table_offset += 4;
  91. /*
  92. * Make sure the .rodata address isn't associated with a
  93. * symbol. GCC jump tables are anonymous data.
  94. *
  95. * Also support C jump tables which are in the same format as
  96. * switch jump tables. For objtool to recognize them, they
  97. * need to be placed in the C_JUMP_TABLE_SECTION section. They
  98. * have symbols associated with them.
  99. */
  100. if (find_symbol_containing(table_sec, table_offset) &&
  101. strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
  102. return NULL;
  103. /*
  104. * Each table entry has a rela associated with it. The rela
  105. * should reference text in the same function as the original
  106. * instruction.
  107. */
  108. rodata_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset);
  109. if (!rodata_reloc)
  110. return NULL;
  111. /*
  112. * Use of RIP-relative switch jumps is quite rare, and
  113. * indicates a rare GCC quirk/bug which can leave dead
  114. * code behind.
  115. */
  116. if (!file->ignore_unreachables && reloc_type(text_reloc) == R_X86_64_PC32) {
  117. WARN_INSN(insn, "ignoring unreachables due to jump table quirk");
  118. file->ignore_unreachables = true;
  119. }
  120. *table_size = 0;
  121. return rodata_reloc;
  122. }
  123. const char *arch_cpu_feature_name(int feature_number)
  124. {
  125. return (feature_number < ARRAY_SIZE(cpu_feature_names)) ?
  126. cpu_feature_names[feature_number] : NULL;
  127. }