xref: /linux/tools/objtool/special.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
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 		alt->feature = *(unsigned int *)(sec->data->d_buf + offset +
85 						 entry->feature);
86 	}
87 
88 	orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
89 	if (!orig_reloc) {
90 		ERROR_FUNC(sec, offset + entry->orig, "can't find orig reloc");
91 		return -1;
92 	}
93 
94 	reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
95 
96 	arch_handle_alternative(alt);
97 
98 	if (!entry->group || alt->new_len) {
99 		new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
100 		if (!new_reloc) {
101 			ERROR_FUNC(sec, offset + entry->new, "can't find new reloc");
102 			return -1;
103 		}
104 
105 		reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off);
106 
107 		/* _ASM_EXTABLE_EX hack */
108 		if (alt->new_off >= 0x7ffffff0)
109 			alt->new_off -= 0x7ffffff0;
110 	}
111 
112 	if (entry->key) {
113 		struct reloc *key_reloc;
114 
115 		key_reloc = find_reloc_by_dest(elf, sec, offset + entry->key);
116 		if (!key_reloc) {
117 			ERROR_FUNC(sec, offset + entry->key, "can't find key reloc");
118 			return -1;
119 		}
120 		alt->key_addend = reloc_addend(key_reloc);
121 	}
122 
123 	return 0;
124 }
125 
126 /*
127  * Read all the special sections and create a list of special_alt structs which
128  * describe all the alternate instructions which can be patched in or
129  * redirected to at runtime.
130  */
131 int special_get_alts(struct elf *elf, struct list_head *alts)
132 {
133 	const struct special_entry *entry;
134 	struct section *sec;
135 	unsigned int nr_entries;
136 	struct special_alt *alt;
137 	int idx;
138 
139 	INIT_LIST_HEAD(alts);
140 
141 	for (entry = entries; entry->sec; entry++) {
142 		sec = find_section_by_name(elf, entry->sec);
143 		if (!sec)
144 			continue;
145 
146 		if (sec_size(sec) % entry->size != 0) {
147 			ERROR("%s size not a multiple of %d", sec->name, entry->size);
148 			return -1;
149 		}
150 
151 		nr_entries = sec_size(sec) / entry->size;
152 
153 		for (idx = 0; idx < nr_entries; idx++) {
154 			alt = malloc(sizeof(*alt));
155 			if (!alt) {
156 				ERROR_GLIBC("malloc failed");
157 				return -1;
158 			}
159 			memset(alt, 0, sizeof(*alt));
160 
161 			if (get_alt_entry(elf, entry, sec, idx, alt))
162 				return -1;
163 
164 			list_add_tail(&alt->list, alts);
165 		}
166 	}
167 
168 	return 0;
169 }
170