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 alt->new_sec = new_reloc->sym->sec;
110 alt->new_off = (unsigned int)new_reloc->addend;
111
112 /* _ASM_EXTABLE_EX hack */
113 if (alt->new_off >= 0x7ffffff0)
114 alt->new_off -= 0x7ffffff0;
115 }
116
117 return 0;
118}
119
120/*
121 * Read all the special sections and create a list of special_alt structs which
122 * describe all the alternate instructions which can be patched in or
123 * redirected to at runtime.
124 */
125int special_get_alts(struct elf *elf, struct list_head *alts)
126{
127 struct special_entry *entry;
128 struct section *sec;
129 unsigned int nr_entries;
130 struct special_alt *alt;
131 int idx, ret;
132
133 INIT_LIST_HEAD(alts);
134
135 for (entry = entries; entry->sec; entry++) {
136 sec = find_section_by_name(elf, entry->sec);
137 if (!sec)
138 continue;
139
140 if (sec->len % entry->size != 0) {
141 WARN("%s size not a multiple of %d",
142 sec->name, entry->size);
143 return -1;
144 }
145
146 nr_entries = sec->len / entry->size;
147
148 for (idx = 0; idx < nr_entries; idx++) {
149 alt = malloc(sizeof(*alt));
150 if (!alt) {
151 WARN("malloc failed");
152 return -1;
153 }
154 memset(alt, 0, sizeof(*alt));
155
156 ret = get_alt_entry(elf, entry, sec, idx, alt);
157 if (ret)
158 return ret;
159
160 list_add_tail(&alt->list, alts);
161 }
162 }
163
164 return 0;
165}