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