• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "builtin.h"
15 #include "special.h"
16 #include "warn.h"
17 #include "arch_special.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 };
26 
27 struct special_entry entries[] = {
28 	{
29 		.sec = ".altinstructions",
30 		.group = true,
31 		.size = ALT_ENTRY_SIZE,
32 		.orig = ALT_ORIG_OFFSET,
33 		.orig_len = ALT_ORIG_LEN_OFFSET,
34 		.new = ALT_NEW_OFFSET,
35 		.new_len = ALT_NEW_LEN_OFFSET,
36 		.feature = ALT_FEATURE_OFFSET,
37 	},
38 	{
39 		.sec = "__jump_table",
40 		.jump_or_nop = true,
41 		.size = JUMP_ENTRY_SIZE,
42 		.orig = JUMP_ORIG_OFFSET,
43 		.new = JUMP_NEW_OFFSET,
44 	},
45 	{
46 		.sec = "__ex_table",
47 		.size = EX_ENTRY_SIZE,
48 		.orig = EX_ORIG_OFFSET,
49 		.new = EX_NEW_OFFSET,
50 	},
51 	{},
52 };
53 
arch_handle_alternative(unsigned short feature,struct special_alt * alt)54 void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt)
55 {
56 }
57 
reloc_to_sec_off(struct reloc * reloc,struct section ** sec,unsigned long * off)58 static void reloc_to_sec_off(struct reloc *reloc, struct section **sec,
59 			     unsigned long *off)
60 {
61 	*sec = reloc->sym->sec;
62 	*off = reloc->sym->offset + reloc->addend;
63 }
64 
get_alt_entry(struct elf * elf,struct special_entry * entry,struct section * sec,int idx,struct special_alt * alt)65 static int get_alt_entry(struct elf *elf, struct special_entry *entry,
66 			 struct section *sec, int idx,
67 			 struct special_alt *alt)
68 {
69 	struct reloc *orig_reloc, *new_reloc;
70 	unsigned long offset;
71 
72 	offset = idx * entry->size;
73 
74 	alt->group = entry->group;
75 	alt->jump_or_nop = entry->jump_or_nop;
76 
77 	if (alt->group) {
78 		alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
79 						   entry->orig_len);
80 		alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
81 						  entry->new_len);
82 	}
83 
84 	if (entry->feature) {
85 		unsigned short feature;
86 
87 		feature = *(unsigned short *)(sec->data->d_buf + offset +
88 					      entry->feature);
89 		arch_handle_alternative(feature, alt);
90 	}
91 
92 	orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
93 	if (!orig_reloc) {
94 		WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
95 		return -1;
96 	}
97 
98 	reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
99 
100 	if (!entry->group || alt->new_len) {
101 		new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
102 		if (!new_reloc) {
103 			WARN_FUNC("can't find new reloc",
104 				  sec, offset + entry->new);
105 			return -1;
106 		}
107 
108 		reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off);
109 
110 		/* _ASM_EXTABLE_EX hack */
111 		if (alt->new_off >= 0x7ffffff0)
112 			alt->new_off -= 0x7ffffff0;
113 	}
114 
115 	return 0;
116 }
117 
118 /*
119  * Read all the special sections and create a list of special_alt structs which
120  * describe all the alternate instructions which can be patched in or
121  * redirected to at runtime.
122  */
special_get_alts(struct elf * elf,struct list_head * alts)123 int special_get_alts(struct elf *elf, struct list_head *alts)
124 {
125 	struct special_entry *entry;
126 	struct section *sec;
127 	unsigned int nr_entries;
128 	struct special_alt *alt;
129 	int idx, ret;
130 
131 	INIT_LIST_HEAD(alts);
132 
133 	for (entry = entries; entry->sec; entry++) {
134 		sec = find_section_by_name(elf, entry->sec);
135 		if (!sec)
136 			continue;
137 
138 		if (sec->len % entry->size != 0) {
139 			WARN("%s size not a multiple of %d",
140 			     sec->name, entry->size);
141 			return -1;
142 		}
143 
144 		nr_entries = sec->len / entry->size;
145 
146 		for (idx = 0; idx < nr_entries; idx++) {
147 			alt = malloc(sizeof(*alt));
148 			if (!alt) {
149 				WARN("malloc failed");
150 				return -1;
151 			}
152 			memset(alt, 0, sizeof(*alt));
153 
154 			ret = get_alt_entry(elf, entry, sec, idx, alt);
155 			if (ret > 0)
156 				continue;
157 			if (ret < 0)
158 				return ret;
159 
160 			list_add_tail(&alt->list, alts);
161 		}
162 	}
163 
164 	return 0;
165 }
166