Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
20struct 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};
27
28struct special_entry entries[] = {
29 {
30 .sec = ".altinstructions",
31 .group = true,
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,
38 },
39 {
40 .sec = "__jump_table",
41 .jump_or_nop = true,
42 .size = JUMP_ENTRY_SIZE,
43 .orig = JUMP_ORIG_OFFSET,
44 .new = JUMP_NEW_OFFSET,
45 },
46 {
47 .sec = "__ex_table",
48 .size = EX_ENTRY_SIZE,
49 .orig = EX_ORIG_OFFSET,
50 .new = EX_NEW_OFFSET,
51 },
52 {},
53};
54
55void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt)
56{
57}
58
59static int get_alt_entry(struct elf *elf, 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
66 offset = idx * entry->size;
67
68 alt->group = entry->group;
69 alt->jump_or_nop = entry->jump_or_nop;
70
71 if (alt->group) {
72 alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
73 entry->orig_len);
74 alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
75 entry->new_len);
76 }
77
78 if (entry->feature) {
79 unsigned short feature;
80
81 feature = bswap_if_needed(*(unsigned short *)(sec->data->d_buf +
82 offset +
83 entry->feature));
84 arch_handle_alternative(feature, alt);
85 }
86
87 orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
88 if (!orig_reloc) {
89 WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
90 return -1;
91 }
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);
95 return -1;
96 }
97
98 alt->orig_sec = orig_reloc->sym->sec;
99 alt->orig_off = orig_reloc->addend;
100
101 if (!entry->group || alt->new_len) {
102 new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
103 if (!new_reloc) {
104 WARN_FUNC("can't find new reloc",
105 sec, offset + entry->new);
106 return -1;
107 }
108
109 /*
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().
113 */
114 if (arch_is_retpoline(new_reloc->sym))
115 return 1;
116
117 alt->new_sec = new_reloc->sym->sec;
118 alt->new_off = (unsigned int)new_reloc->addend;
119
120 /* _ASM_EXTABLE_EX hack */
121 if (alt->new_off >= 0x7ffffff0)
122 alt->new_off -= 0x7ffffff0;
123 }
124
125 return 0;
126}
127
128/*
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.
132 */
133int special_get_alts(struct elf *elf, struct list_head *alts)
134{
135 struct special_entry *entry;
136 struct section *sec;
137 unsigned int nr_entries;
138 struct special_alt *alt;
139 int idx, ret;
140
141 INIT_LIST_HEAD(alts);
142
143 for (entry = entries; entry->sec; entry++) {
144 sec = find_section_by_name(elf, entry->sec);
145 if (!sec)
146 continue;
147
148 if (sec->len % entry->size != 0) {
149 WARN("%s size not a multiple of %d",
150 sec->name, entry->size);
151 return -1;
152 }
153
154 nr_entries = sec->len / entry->size;
155
156 for (idx = 0; idx < nr_entries; idx++) {
157 alt = malloc(sizeof(*alt));
158 if (!alt) {
159 WARN("malloc failed");
160 return -1;
161 }
162 memset(alt, 0, sizeof(*alt));
163
164 ret = get_alt_entry(elf, entry, sec, idx, alt);
165 if (ret > 0)
166 continue;
167 if (ret < 0)
168 return ret;
169
170 list_add_tail(&alt->list, alts);
171 }
172 }
173
174 return 0;
175}