xref: /openbmc/linux/tools/objtool/special.c (revision a9d85efb)
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 
20 struct 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 	unsigned char key; /* jump_label key */
27 };
28 
29 struct special_entry entries[] = {
30 	{
31 		.sec = ".altinstructions",
32 		.group = true,
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,
39 	},
40 	{
41 		.sec = "__jump_table",
42 		.jump_or_nop = true,
43 		.size = JUMP_ENTRY_SIZE,
44 		.orig = JUMP_ORIG_OFFSET,
45 		.new = JUMP_NEW_OFFSET,
46 		.key = JUMP_KEY_OFFSET,
47 	},
48 	{
49 		.sec = "__ex_table",
50 		.size = EX_ENTRY_SIZE,
51 		.orig = EX_ORIG_OFFSET,
52 		.new = EX_NEW_OFFSET,
53 	},
54 	{},
55 };
56 
57 void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt)
58 {
59 }
60 
61 static bool reloc2sec_off(struct reloc *reloc, struct section **sec, unsigned long *off)
62 {
63 	switch (reloc->sym->type) {
64 	case STT_FUNC:
65 		*sec = reloc->sym->sec;
66 		*off = reloc->sym->offset + reloc->addend;
67 		return true;
68 
69 	case STT_SECTION:
70 		*sec = reloc->sym->sec;
71 		*off = reloc->addend;
72 		return true;
73 
74 	default:
75 		return false;
76 	}
77 }
78 
79 static int get_alt_entry(struct elf *elf, struct special_entry *entry,
80 			 struct section *sec, int idx,
81 			 struct special_alt *alt)
82 {
83 	struct reloc *orig_reloc, *new_reloc;
84 	unsigned long offset;
85 
86 	offset = idx * entry->size;
87 
88 	alt->group = entry->group;
89 	alt->jump_or_nop = entry->jump_or_nop;
90 
91 	if (alt->group) {
92 		alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
93 						   entry->orig_len);
94 		alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
95 						  entry->new_len);
96 	}
97 
98 	if (entry->feature) {
99 		unsigned short feature;
100 
101 		feature = bswap_if_needed(*(unsigned short *)(sec->data->d_buf +
102 							      offset +
103 							      entry->feature));
104 		arch_handle_alternative(feature, alt);
105 	}
106 
107 	orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
108 	if (!orig_reloc) {
109 		WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
110 		return -1;
111 	}
112 	if (!reloc2sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off)) {
113 		WARN_FUNC("don't know how to handle reloc symbol type %d: %s",
114 			   sec, offset + entry->orig,
115 			   orig_reloc->sym->type,
116 			   orig_reloc->sym->name);
117 		return -1;
118 	}
119 
120 	if (!entry->group || alt->new_len) {
121 		new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
122 		if (!new_reloc) {
123 			WARN_FUNC("can't find new reloc",
124 				  sec, offset + entry->new);
125 			return -1;
126 		}
127 
128 		/*
129 		 * Skip retpoline .altinstr_replacement... we already rewrite the
130 		 * instructions for retpolines anyway, see arch_is_retpoline()
131 		 * usage in add_{call,jump}_destinations().
132 		 */
133 		if (arch_is_retpoline(new_reloc->sym))
134 			return 1;
135 
136 		if (!reloc2sec_off(new_reloc, &alt->new_sec, &alt->new_off)) {
137 			WARN_FUNC("don't know how to handle reloc symbol type %d: %s",
138 				  sec, offset + entry->new,
139 				  new_reloc->sym->type,
140 				  new_reloc->sym->name);
141 			return -1;
142 		}
143 
144 		/* _ASM_EXTABLE_EX hack */
145 		if (alt->new_off >= 0x7ffffff0)
146 			alt->new_off -= 0x7ffffff0;
147 	}
148 
149 	if (entry->key) {
150 		struct reloc *key_reloc;
151 
152 		key_reloc = find_reloc_by_dest(elf, sec, offset + entry->key);
153 		if (!key_reloc) {
154 			WARN_FUNC("can't find key reloc",
155 				  sec, offset + entry->key);
156 			return -1;
157 		}
158 		alt->key_addend = key_reloc->addend;
159 	}
160 
161 	return 0;
162 }
163 
164 /*
165  * Read all the special sections and create a list of special_alt structs which
166  * describe all the alternate instructions which can be patched in or
167  * redirected to at runtime.
168  */
169 int special_get_alts(struct elf *elf, struct list_head *alts)
170 {
171 	struct special_entry *entry;
172 	struct section *sec;
173 	unsigned int nr_entries;
174 	struct special_alt *alt;
175 	int idx, ret;
176 
177 	INIT_LIST_HEAD(alts);
178 
179 	for (entry = entries; entry->sec; entry++) {
180 		sec = find_section_by_name(elf, entry->sec);
181 		if (!sec)
182 			continue;
183 
184 		if (sec->len % entry->size != 0) {
185 			WARN("%s size not a multiple of %d",
186 			     sec->name, entry->size);
187 			return -1;
188 		}
189 
190 		nr_entries = sec->len / entry->size;
191 
192 		for (idx = 0; idx < nr_entries; idx++) {
193 			alt = malloc(sizeof(*alt));
194 			if (!alt) {
195 				WARN("malloc failed");
196 				return -1;
197 			}
198 			memset(alt, 0, sizeof(*alt));
199 
200 			ret = get_alt_entry(elf, entry, sec, idx, alt);
201 			if (ret > 0)
202 				continue;
203 			if (ret < 0)
204 				return ret;
205 
206 			list_add_tail(&alt->list, alts);
207 		}
208 	}
209 
210 	return 0;
211 }
212