special.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
  4. */
  5. /*
  6. * This file reads all the special sections which have alternate instructions
  7. * which can be patched in or redirected to at runtime.
  8. */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <arch/special.h>
  12. #include <objtool/builtin.h>
  13. #include <objtool/special.h>
  14. #include <objtool/warn.h>
  15. #include <objtool/endianness.h>
  16. struct special_entry {
  17. const char *sec;
  18. bool group, jump_or_nop;
  19. unsigned char size, orig, new;
  20. unsigned char orig_len, new_len; /* group only */
  21. unsigned char feature; /* ALTERNATIVE macro CPU feature */
  22. unsigned char key; /* jump_label key */
  23. };
  24. static const struct special_entry entries[] = {
  25. {
  26. .sec = ".altinstructions",
  27. .group = true,
  28. .size = ALT_ENTRY_SIZE,
  29. .orig = ALT_ORIG_OFFSET,
  30. .orig_len = ALT_ORIG_LEN_OFFSET,
  31. .new = ALT_NEW_OFFSET,
  32. .new_len = ALT_NEW_LEN_OFFSET,
  33. .feature = ALT_FEATURE_OFFSET,
  34. },
  35. {
  36. .sec = "__jump_table",
  37. .jump_or_nop = true,
  38. .size = JUMP_ENTRY_SIZE,
  39. .orig = JUMP_ORIG_OFFSET,
  40. .new = JUMP_NEW_OFFSET,
  41. .key = JUMP_KEY_OFFSET,
  42. },
  43. {
  44. .sec = "__ex_table",
  45. .size = EX_ENTRY_SIZE,
  46. .orig = EX_ORIG_OFFSET,
  47. .new = EX_NEW_OFFSET,
  48. },
  49. {},
  50. };
  51. void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt)
  52. {
  53. }
  54. static void reloc_to_sec_off(struct reloc *reloc, struct section **sec,
  55. unsigned long *off)
  56. {
  57. *sec = reloc->sym->sec;
  58. *off = reloc->sym->offset + reloc_addend(reloc);
  59. }
  60. static int get_alt_entry(struct elf *elf, const struct special_entry *entry,
  61. struct section *sec, int idx,
  62. struct special_alt *alt)
  63. {
  64. struct reloc *orig_reloc, *new_reloc;
  65. unsigned long offset;
  66. offset = idx * entry->size;
  67. alt->group = entry->group;
  68. alt->jump_or_nop = entry->jump_or_nop;
  69. if (alt->group) {
  70. alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
  71. entry->orig_len);
  72. alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
  73. entry->new_len);
  74. }
  75. orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
  76. if (!orig_reloc) {
  77. WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
  78. return -1;
  79. }
  80. reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
  81. if (entry->feature) {
  82. unsigned short feature;
  83. feature = bswap_if_needed(elf,
  84. *(unsigned short *)(sec->data->d_buf +
  85. offset +
  86. entry->feature));
  87. arch_handle_alternative(feature, alt);
  88. }
  89. if (!entry->group || alt->new_len) {
  90. new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
  91. if (!new_reloc) {
  92. WARN_FUNC("can't find new reloc",
  93. sec, offset + entry->new);
  94. return -1;
  95. }
  96. reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off);
  97. /* _ASM_EXTABLE_EX hack */
  98. if (alt->new_off >= 0x7ffffff0)
  99. alt->new_off -= 0x7ffffff0;
  100. }
  101. if (entry->key) {
  102. struct reloc *key_reloc;
  103. key_reloc = find_reloc_by_dest(elf, sec, offset + entry->key);
  104. if (!key_reloc) {
  105. WARN_FUNC("can't find key reloc",
  106. sec, offset + entry->key);
  107. return -1;
  108. }
  109. alt->key_addend = reloc_addend(key_reloc);
  110. }
  111. return 0;
  112. }
  113. /*
  114. * Read all the special sections and create a list of special_alt structs which
  115. * describe all the alternate instructions which can be patched in or
  116. * redirected to at runtime.
  117. */
  118. int special_get_alts(struct elf *elf, struct list_head *alts)
  119. {
  120. const struct special_entry *entry;
  121. struct section *sec;
  122. unsigned int nr_entries;
  123. struct special_alt *alt;
  124. int idx, ret;
  125. INIT_LIST_HEAD(alts);
  126. for (entry = entries; entry->sec; entry++) {
  127. sec = find_section_by_name(elf, entry->sec);
  128. if (!sec)
  129. continue;
  130. if (sec->sh.sh_size % entry->size != 0) {
  131. WARN("%s size not a multiple of %d",
  132. sec->name, entry->size);
  133. return -1;
  134. }
  135. nr_entries = sec->sh.sh_size / entry->size;
  136. for (idx = 0; idx < nr_entries; idx++) {
  137. alt = malloc(sizeof(*alt));
  138. if (!alt) {
  139. WARN("malloc failed");
  140. return -1;
  141. }
  142. memset(alt, 0, sizeof(*alt));
  143. ret = get_alt_entry(elf, entry, sec, idx, alt);
  144. if (ret > 0)
  145. continue;
  146. if (ret < 0)
  147. return ret;
  148. list_add_tail(&alt->list, alts);
  149. }
  150. }
  151. return 0;
  152. }