special.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. struct special_entry {
  16. const char *sec;
  17. bool group, jump_or_nop;
  18. unsigned char size, orig, new;
  19. unsigned char orig_len, new_len; /* group only */
  20. unsigned char feature; /* ALTERNATIVE macro CPU feature */
  21. unsigned char key; /* jump_label key */
  22. };
  23. static const struct special_entry entries[] = {
  24. {
  25. .sec = ".altinstructions",
  26. .group = true,
  27. .size = ALT_ENTRY_SIZE,
  28. .orig = ALT_ORIG_OFFSET,
  29. .orig_len = ALT_ORIG_LEN_OFFSET,
  30. .new = ALT_NEW_OFFSET,
  31. .new_len = ALT_NEW_LEN_OFFSET,
  32. .feature = ALT_FEATURE_OFFSET,
  33. },
  34. {
  35. .sec = "__jump_table",
  36. .jump_or_nop = true,
  37. .size = JUMP_ENTRY_SIZE,
  38. .orig = JUMP_ORIG_OFFSET,
  39. .new = JUMP_NEW_OFFSET,
  40. .key = JUMP_KEY_OFFSET,
  41. },
  42. {
  43. .sec = "__ex_table",
  44. .size = EX_ENTRY_SIZE,
  45. .orig = EX_ORIG_OFFSET,
  46. .new = EX_NEW_OFFSET,
  47. },
  48. {},
  49. };
  50. void __weak arch_handle_alternative(struct special_alt *alt)
  51. {
  52. }
  53. static void reloc_to_sec_off(struct reloc *reloc, struct section **sec,
  54. unsigned long *off)
  55. {
  56. *sec = reloc->sym->sec;
  57. *off = reloc->sym->offset + reloc_addend(reloc);
  58. }
  59. static int get_alt_entry(struct elf *elf, const struct special_entry *entry,
  60. struct section *sec, int idx,
  61. struct special_alt *alt)
  62. {
  63. struct reloc *orig_reloc, *new_reloc;
  64. unsigned long offset;
  65. offset = idx * entry->size;
  66. alt->group = entry->group;
  67. alt->jump_or_nop = entry->jump_or_nop;
  68. if (alt->group) {
  69. alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
  70. entry->orig_len);
  71. alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
  72. entry->new_len);
  73. alt->feature = *(unsigned int *)(sec->data->d_buf + offset +
  74. entry->feature);
  75. }
  76. orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
  77. if (!orig_reloc) {
  78. ERROR_FUNC(sec, offset + entry->orig, "can't find orig reloc");
  79. return -1;
  80. }
  81. reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
  82. arch_handle_alternative(alt);
  83. if (!entry->group || alt->new_len) {
  84. new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
  85. if (!new_reloc) {
  86. ERROR_FUNC(sec, offset + entry->new, "can't find new reloc");
  87. return -1;
  88. }
  89. reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off);
  90. /* _ASM_EXTABLE_EX hack */
  91. if (alt->new_off >= 0x7ffffff0)
  92. alt->new_off -= 0x7ffffff0;
  93. }
  94. if (entry->key) {
  95. struct reloc *key_reloc;
  96. key_reloc = find_reloc_by_dest(elf, sec, offset + entry->key);
  97. if (!key_reloc) {
  98. ERROR_FUNC(sec, offset + entry->key, "can't find key reloc");
  99. return -1;
  100. }
  101. alt->key_addend = reloc_addend(key_reloc);
  102. }
  103. return 0;
  104. }
  105. /*
  106. * Read all the special sections and create a list of special_alt structs which
  107. * describe all the alternate instructions which can be patched in or
  108. * redirected to at runtime.
  109. */
  110. int special_get_alts(struct elf *elf, struct list_head *alts)
  111. {
  112. const struct special_entry *entry;
  113. struct section *sec;
  114. unsigned int nr_entries;
  115. struct special_alt *alt;
  116. int idx;
  117. INIT_LIST_HEAD(alts);
  118. for (entry = entries; entry->sec; entry++) {
  119. sec = find_section_by_name(elf, entry->sec);
  120. if (!sec)
  121. continue;
  122. if (sec_size(sec) % entry->size != 0) {
  123. ERROR("%s size not a multiple of %d", sec->name, entry->size);
  124. return -1;
  125. }
  126. nr_entries = sec_size(sec) / entry->size;
  127. for (idx = 0; idx < nr_entries; idx++) {
  128. alt = malloc(sizeof(*alt));
  129. if (!alt) {
  130. ERROR_GLIBC("malloc failed");
  131. return -1;
  132. }
  133. memset(alt, 0, sizeof(*alt));
  134. if (get_alt_entry(elf, entry, sec, idx, alt))
  135. return -1;
  136. list_add_tail(&alt->list, alts);
  137. }
  138. }
  139. return 0;
  140. }