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