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 */
28 struct special_entry entries[] = {
30 .sec = ".altinstructions",
32 .size = ALT_ENTRY_SIZE,
33 .orig = ALT_ORIG_OFFSET,
34 .orig_len = ALT_ORIG_LEN_OFFSET,
35 .new = ALT_NEW_OFFSET,
36 .new_len = ALT_NEW_LEN_OFFSET,
37 .feature = ALT_FEATURE_OFFSET,
40 .sec = "__jump_table",
42 .size = JUMP_ENTRY_SIZE,
43 .orig = JUMP_ORIG_OFFSET,
44 .new = JUMP_NEW_OFFSET,
48 .size = EX_ENTRY_SIZE,
49 .orig = EX_ORIG_OFFSET,
55 void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt)
59 static int get_alt_entry(struct elf *elf, struct special_entry *entry,
60 struct section *sec, int idx,
61 struct special_alt *alt)
63 struct reloc *orig_reloc, *new_reloc;
66 offset = idx * entry->size;
68 alt->group = entry->group;
69 alt->jump_or_nop = entry->jump_or_nop;
72 alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
74 alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
79 unsigned short feature;
81 feature = bswap_if_needed(*(unsigned short *)(sec->data->d_buf +
84 arch_handle_alternative(feature, alt);
87 orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
89 WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
92 if (orig_reloc->sym->type != STT_SECTION) {
93 WARN_FUNC("don't know how to handle non-section reloc symbol %s",
94 sec, offset + entry->orig, orig_reloc->sym->name);
98 alt->orig_sec = orig_reloc->sym->sec;
99 alt->orig_off = orig_reloc->addend;
101 if (!entry->group || alt->new_len) {
102 new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
104 WARN_FUNC("can't find new reloc",
105 sec, offset + entry->new);
110 * Skip retpoline .altinstr_replacement... we already rewrite the
111 * instructions for retpolines anyway, see arch_is_retpoline()
112 * usage in add_{call,jump}_destinations().
114 if (arch_is_retpoline(new_reloc->sym))
117 alt->new_sec = new_reloc->sym->sec;
118 alt->new_off = (unsigned int)new_reloc->addend;
120 /* _ASM_EXTABLE_EX hack */
121 if (alt->new_off >= 0x7ffffff0)
122 alt->new_off -= 0x7ffffff0;
129 * Read all the special sections and create a list of special_alt structs which
130 * describe all the alternate instructions which can be patched in or
131 * redirected to at runtime.
133 int special_get_alts(struct elf *elf, struct list_head *alts)
135 struct special_entry *entry;
137 unsigned int nr_entries;
138 struct special_alt *alt;
141 INIT_LIST_HEAD(alts);
143 for (entry = entries; entry->sec; entry++) {
144 sec = find_section_by_name(elf, entry->sec);
148 if (sec->len % entry->size != 0) {
149 WARN("%s size not a multiple of %d",
150 sec->name, entry->size);
154 nr_entries = sec->len / entry->size;
156 for (idx = 0; idx < nr_entries; idx++) {
157 alt = malloc(sizeof(*alt));
159 WARN("malloc failed");
162 memset(alt, 0, sizeof(*alt));
164 ret = get_alt_entry(elf, entry, sec, idx, alt);
170 list_add_tail(&alt->list, alts);