1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
7 * This file reads all the special sections which have alternate instructions
8 * which can be patched in or redirected to at runtime.
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>
20 struct special_entry {
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 */
29 struct special_entry entries[] = {
31 .sec = ".altinstructions",
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,
41 .sec = "__jump_table",
43 .size = JUMP_ENTRY_SIZE,
44 .orig = JUMP_ORIG_OFFSET,
45 .new = JUMP_NEW_OFFSET,
46 .key = JUMP_KEY_OFFSET,
50 .size = EX_ENTRY_SIZE,
51 .orig = EX_ORIG_OFFSET,
57 void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt)
61 static int get_alt_entry(struct elf *elf, struct special_entry *entry,
62 struct section *sec, int idx,
63 struct special_alt *alt)
65 struct reloc *orig_reloc, *new_reloc;
68 offset = idx * entry->size;
70 alt->group = entry->group;
71 alt->jump_or_nop = entry->jump_or_nop;
74 alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
76 alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
81 unsigned short feature;
83 feature = bswap_if_needed(*(unsigned short *)(sec->data->d_buf +
86 arch_handle_alternative(feature, alt);
89 orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
91 WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
94 if (orig_reloc->sym->type != STT_SECTION) {
95 WARN_FUNC("don't know how to handle non-section reloc symbol %s",
96 sec, offset + entry->orig, orig_reloc->sym->name);
100 alt->orig_sec = orig_reloc->sym->sec;
101 alt->orig_off = orig_reloc->addend;
103 if (!entry->group || alt->new_len) {
104 new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
106 WARN_FUNC("can't find new reloc",
107 sec, offset + entry->new);
112 * Skip retpoline .altinstr_replacement... we already rewrite the
113 * instructions for retpolines anyway, see arch_is_retpoline()
114 * usage in add_{call,jump}_destinations().
116 if (arch_is_retpoline(new_reloc->sym))
119 alt->new_sec = new_reloc->sym->sec;
120 alt->new_off = (unsigned int)new_reloc->addend;
122 /* _ASM_EXTABLE_EX hack */
123 if (alt->new_off >= 0x7ffffff0)
124 alt->new_off -= 0x7ffffff0;
128 struct reloc *key_reloc;
130 key_reloc = find_reloc_by_dest(elf, sec, offset + entry->key);
132 WARN_FUNC("can't find key reloc",
133 sec, offset + entry->key);
136 alt->key_addend = key_reloc->addend;
143 * Read all the special sections and create a list of special_alt structs which
144 * describe all the alternate instructions which can be patched in or
145 * redirected to at runtime.
147 int special_get_alts(struct elf *elf, struct list_head *alts)
149 struct special_entry *entry;
151 unsigned int nr_entries;
152 struct special_alt *alt;
155 INIT_LIST_HEAD(alts);
157 for (entry = entries; entry->sec; entry++) {
158 sec = find_section_by_name(elf, entry->sec);
162 if (sec->len % entry->size != 0) {
163 WARN("%s size not a multiple of %d",
164 sec->name, entry->size);
168 nr_entries = sec->len / entry->size;
170 for (idx = 0; idx < nr_entries; idx++) {
171 alt = malloc(sizeof(*alt));
173 WARN("malloc failed");
176 memset(alt, 0, sizeof(*alt));
178 ret = get_alt_entry(elf, entry, sec, idx, alt);
184 list_add_tail(&alt->list, alts);