xref: /linux/tools/objtool/special.c (revision 2092007aa32f8dd968c38751bd1b7cac9b1f738d)
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 
19 struct special_entry {
20 	const char *sec;
21 	bool group, jump_or_nop;
22 	unsigned char size, orig, new;
23 	unsigned char orig_len, new_len; /* group only */
24 	unsigned char feature; /* ALTERNATIVE macro CPU feature */
25 	unsigned char key; /* jump_label key */
26 };
27 
28 static const struct 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 		.key = JUMP_KEY_OFFSET,
46 	},
47 	{
48 		.sec = "__ex_table",
49 		.size = EX_ENTRY_SIZE,
50 		.orig = EX_ORIG_OFFSET,
51 		.new = EX_NEW_OFFSET,
52 	},
53 	{},
54 };
55 
56 void __weak arch_handle_alternative(struct special_alt *alt)
57 {
58 }
59 
60 static void reloc_to_sec_off(struct reloc *reloc, struct section **sec,
61 			     unsigned long *off)
62 {
63 	*sec = reloc->sym->sec;
64 	*off = reloc->sym->offset + reloc_addend(reloc);
65 }
66 
67 static int get_alt_entry(struct elf *elf, const struct special_entry *entry,
68 			 struct section *sec, int idx,
69 			 struct special_alt *alt)
70 {
71 	struct reloc *orig_reloc, *new_reloc;
72 	unsigned long offset;
73 
74 	offset = idx * entry->size;
75 
76 	alt->group = entry->group;
77 	alt->jump_or_nop = entry->jump_or_nop;
78 
79 	if (alt->group) {
80 		alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
81 						   entry->orig_len);
82 		alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
83 						  entry->new_len);
84 	}
85 
86 	orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
87 	if (!orig_reloc) {
88 		ERROR_FUNC(sec, offset + entry->orig, "can't find orig reloc");
89 		return -1;
90 	}
91 
92 	reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
93 
94 	arch_handle_alternative(alt);
95 
96 	if (!entry->group || alt->new_len) {
97 		new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
98 		if (!new_reloc) {
99 			ERROR_FUNC(sec, offset + entry->new, "can't find new reloc");
100 			return -1;
101 		}
102 
103 		reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off);
104 
105 		/* _ASM_EXTABLE_EX hack */
106 		if (alt->new_off >= 0x7ffffff0)
107 			alt->new_off -= 0x7ffffff0;
108 	}
109 
110 	if (entry->key) {
111 		struct reloc *key_reloc;
112 
113 		key_reloc = find_reloc_by_dest(elf, sec, offset + entry->key);
114 		if (!key_reloc) {
115 			ERROR_FUNC(sec, offset + entry->key, "can't find key reloc");
116 			return -1;
117 		}
118 		alt->key_addend = reloc_addend(key_reloc);
119 	}
120 
121 	return 0;
122 }
123 
124 /*
125  * Read all the special sections and create a list of special_alt structs which
126  * describe all the alternate instructions which can be patched in or
127  * redirected to at runtime.
128  */
129 int special_get_alts(struct elf *elf, struct list_head *alts)
130 {
131 	const struct special_entry *entry;
132 	struct section *sec;
133 	unsigned int nr_entries;
134 	struct special_alt *alt;
135 	int idx;
136 
137 	INIT_LIST_HEAD(alts);
138 
139 	for (entry = entries; entry->sec; entry++) {
140 		sec = find_section_by_name(elf, entry->sec);
141 		if (!sec)
142 			continue;
143 
144 		if (sec_size(sec) % entry->size != 0) {
145 			ERROR("%s size not a multiple of %d", sec->name, entry->size);
146 			return -1;
147 		}
148 
149 		nr_entries = sec_size(sec) / entry->size;
150 
151 		for (idx = 0; idx < nr_entries; idx++) {
152 			alt = malloc(sizeof(*alt));
153 			if (!alt) {
154 				ERROR_GLIBC("malloc failed");
155 				return -1;
156 			}
157 			memset(alt, 0, sizeof(*alt));
158 
159 			if (get_alt_entry(elf, entry, sec, idx, alt))
160 				return -1;
161 
162 			list_add_tail(&alt->list, alts);
163 		}
164 	}
165 
166 	return 0;
167 }
168