special.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 Josh Poimboeuf <[email protected]>
  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. 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;
  59. }
  60. static int get_alt_entry(struct elf *elf, 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. if (entry->feature) {
  76. unsigned short feature;
  77. feature = bswap_if_needed(*(unsigned short *)(sec->data->d_buf +
  78. offset +
  79. entry->feature));
  80. arch_handle_alternative(feature, alt);
  81. }
  82. orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
  83. if (!orig_reloc) {
  84. WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
  85. return -1;
  86. }
  87. reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
  88. if (!entry->group || alt->new_len) {
  89. new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
  90. if (!new_reloc) {
  91. WARN_FUNC("can't find new reloc",
  92. sec, offset + entry->new);
  93. return -1;
  94. }
  95. reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off);
  96. /* _ASM_EXTABLE_EX hack */
  97. if (alt->new_off >= 0x7ffffff0)
  98. alt->new_off -= 0x7ffffff0;
  99. }
  100. if (entry->key) {
  101. struct reloc *key_reloc;
  102. key_reloc = find_reloc_by_dest(elf, sec, offset + entry->key);
  103. if (!key_reloc) {
  104. WARN_FUNC("can't find key reloc",
  105. sec, offset + entry->key);
  106. return -1;
  107. }
  108. alt->key_addend = key_reloc->addend;
  109. }
  110. return 0;
  111. }
  112. /*
  113. * Read all the special sections and create a list of special_alt structs which
  114. * describe all the alternate instructions which can be patched in or
  115. * redirected to at runtime.
  116. */
  117. int special_get_alts(struct elf *elf, struct list_head *alts)
  118. {
  119. struct special_entry *entry;
  120. struct section *sec;
  121. unsigned int nr_entries;
  122. struct special_alt *alt;
  123. int idx, ret;
  124. INIT_LIST_HEAD(alts);
  125. for (entry = entries; entry->sec; entry++) {
  126. sec = find_section_by_name(elf, entry->sec);
  127. if (!sec)
  128. continue;
  129. if (sec->sh.sh_size % entry->size != 0) {
  130. WARN("%s size not a multiple of %d",
  131. sec->name, entry->size);
  132. return -1;
  133. }
  134. nr_entries = sec->sh.sh_size / entry->size;
  135. for (idx = 0; idx < nr_entries; idx++) {
  136. alt = malloc(sizeof(*alt));
  137. if (!alt) {
  138. WARN("malloc failed");
  139. return -1;
  140. }
  141. memset(alt, 0, sizeof(*alt));
  142. ret = get_alt_entry(elf, entry, sec, idx, alt);
  143. if (ret > 0)
  144. continue;
  145. if (ret < 0)
  146. return ret;
  147. list_add_tail(&alt->list, alts);
  148. }
  149. }
  150. return 0;
  151. }