1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/module.h>
22#include <linux/moduleloader.h>
23#include <linux/elf.h>
24#include <linux/vmalloc.h>
25#include <linux/fs.h>
26#include <linux/string.h>
27#include <linux/kernel.h>
28#include <linux/ftrace.h>
29#include <linux/cache.h>
30#include <linux/bug.h>
31#include <linux/sort.h>
32#include <asm/setup.h>
33
34
35
36static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
37{
38 unsigned int i, r_info, r_addend, _count_relocs;
39
40 _count_relocs = 0;
41 r_info = 0;
42 r_addend = 0;
43 for (i = 0; i < num; i++)
44
45 if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
46 (r_info != ELF32_R_SYM(rela[i].r_info) ||
47 r_addend != rela[i].r_addend)) {
48 _count_relocs++;
49 r_info = ELF32_R_SYM(rela[i].r_info);
50 r_addend = rela[i].r_addend;
51 }
52
53#ifdef CONFIG_DYNAMIC_FTRACE
54 _count_relocs++;
55#endif
56 return _count_relocs;
57}
58
59static int relacmp(const void *_x, const void *_y)
60{
61 const Elf32_Rela *x, *y;
62
63 y = (Elf32_Rela *)_x;
64 x = (Elf32_Rela *)_y;
65
66
67
68
69
70 if (x->r_info < y->r_info)
71 return -1;
72 else if (x->r_info > y->r_info)
73 return 1;
74 else if (x->r_addend < y->r_addend)
75 return -1;
76 else if (x->r_addend > y->r_addend)
77 return 1;
78 else
79 return 0;
80}
81
82static void relaswap(void *_x, void *_y, int size)
83{
84 uint32_t *x, *y, tmp;
85 int i;
86
87 y = (uint32_t *)_x;
88 x = (uint32_t *)_y;
89
90 for (i = 0; i < sizeof(Elf32_Rela) / sizeof(uint32_t); i++) {
91 tmp = x[i];
92 x[i] = y[i];
93 y[i] = tmp;
94 }
95}
96
97
98
99static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
100 const Elf32_Shdr *sechdrs,
101 const char *secstrings,
102 int is_init)
103{
104 unsigned long ret = 0;
105 unsigned i;
106
107
108
109 for (i = 1; i < hdr->e_shnum; i++) {
110
111
112 if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != NULL)
113 != is_init)
114 continue;
115
116
117 if (strstr(secstrings + sechdrs[i].sh_name, ".debug"))
118 continue;
119
120 if (sechdrs[i].sh_type == SHT_RELA) {
121 pr_debug("Found relocations in section %u\n", i);
122 pr_debug("Ptr: %p. Number: %u\n",
123 (void *)hdr + sechdrs[i].sh_offset,
124 sechdrs[i].sh_size / sizeof(Elf32_Rela));
125
126
127
128
129
130
131 sort((void *)hdr + sechdrs[i].sh_offset,
132 sechdrs[i].sh_size / sizeof(Elf32_Rela),
133 sizeof(Elf32_Rela), relacmp, relaswap);
134
135 ret += count_relocs((void *)hdr
136 + sechdrs[i].sh_offset,
137 sechdrs[i].sh_size
138 / sizeof(Elf32_Rela))
139 * sizeof(struct ppc_plt_entry);
140 }
141 }
142
143 return ret;
144}
145
146int module_frob_arch_sections(Elf32_Ehdr *hdr,
147 Elf32_Shdr *sechdrs,
148 char *secstrings,
149 struct module *me)
150{
151 unsigned int i;
152
153
154 for (i = 0; i < hdr->e_shnum; i++) {
155 if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
156 me->arch.init_plt_section = i;
157 else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
158 me->arch.core_plt_section = i;
159 }
160 if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
161 pr_err("Module doesn't contain .plt or .init.plt sections.\n");
162 return -ENOEXEC;
163 }
164
165
166 sechdrs[me->arch.core_plt_section].sh_size
167 = get_plt_size(hdr, sechdrs, secstrings, 0);
168 sechdrs[me->arch.init_plt_section].sh_size
169 = get_plt_size(hdr, sechdrs, secstrings, 1);
170 return 0;
171}
172
173static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
174{
175 if (entry->jump[0] == 0x3d800000 + ((val + 0x8000) >> 16)
176 && entry->jump[1] == 0x398c0000 + (val & 0xffff))
177 return 1;
178 return 0;
179}
180
181
182static uint32_t do_plt_call(void *location,
183 Elf32_Addr val,
184 const Elf32_Shdr *sechdrs,
185 struct module *mod)
186{
187 struct ppc_plt_entry *entry;
188
189 pr_debug("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
190
191 if (location >= mod->core_layout.base
192 && location < mod->core_layout.base + mod->core_layout.size)
193 entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
194 else
195 entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
196
197
198 while (entry->jump[0]) {
199 if (entry_matches(entry, val)) return (uint32_t)entry;
200 entry++;
201 }
202
203 entry->jump[0] = 0x3d800000+((val+0x8000)>>16);
204 entry->jump[1] = 0x398c0000 + (val&0xffff);
205 entry->jump[2] = 0x7d8903a6;
206 entry->jump[3] = 0x4e800420;
207
208 pr_debug("Initialized plt for 0x%x at %p\n", val, entry);
209 return (uint32_t)entry;
210}
211
212int apply_relocate_add(Elf32_Shdr *sechdrs,
213 const char *strtab,
214 unsigned int symindex,
215 unsigned int relsec,
216 struct module *module)
217{
218 unsigned int i;
219 Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
220 Elf32_Sym *sym;
221 uint32_t *location;
222 uint32_t value;
223
224 pr_debug("Applying ADD relocate section %u to %u\n", relsec,
225 sechdrs[relsec].sh_info);
226 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
227
228 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
229 + rela[i].r_offset;
230
231
232 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
233 + ELF32_R_SYM(rela[i].r_info);
234
235 value = sym->st_value + rela[i].r_addend;
236
237 switch (ELF32_R_TYPE(rela[i].r_info)) {
238 case R_PPC_ADDR32:
239
240 *(uint32_t *)location = value;
241 break;
242
243 case R_PPC_ADDR16_LO:
244
245 *(uint16_t *)location = value;
246 break;
247
248 case R_PPC_ADDR16_HI:
249
250 *(uint16_t *)location = (value >> 16);
251 break;
252
253 case R_PPC_ADDR16_HA:
254
255
256
257
258 *(uint16_t *)location = (value + 0x8000) >> 16;
259 break;
260
261 case R_PPC_REL24:
262 if ((int)(value - (uint32_t)location) < -0x02000000
263 || (int)(value - (uint32_t)location) >= 0x02000000)
264 value = do_plt_call(location, value,
265 sechdrs, module);
266
267
268 pr_debug("REL24 value = %08X. location = %08X\n",
269 value, (uint32_t)location);
270 pr_debug("Location before: %08X.\n",
271 *(uint32_t *)location);
272 *(uint32_t *)location
273 = (*(uint32_t *)location & ~0x03fffffc)
274 | ((value - (uint32_t)location)
275 & 0x03fffffc);
276 pr_debug("Location after: %08X.\n",
277 *(uint32_t *)location);
278 pr_debug("ie. jump to %08X+%08X = %08X\n",
279 *(uint32_t *)location & 0x03fffffc,
280 (uint32_t)location,
281 (*(uint32_t *)location & 0x03fffffc)
282 + (uint32_t)location);
283 break;
284
285 case R_PPC_REL32:
286
287 *(uint32_t *)location = value - (uint32_t)location;
288 break;
289
290 default:
291 pr_err("%s: unknown ADD relocation: %u\n",
292 module->name,
293 ELF32_R_TYPE(rela[i].r_info));
294 return -ENOEXEC;
295 }
296 }
297
298 return 0;
299}
300
301#ifdef CONFIG_DYNAMIC_FTRACE
302int module_finalize_ftrace(struct module *module, const Elf_Shdr *sechdrs)
303{
304 module->arch.tramp = do_plt_call(module->core_layout.base,
305 (unsigned long)ftrace_caller,
306 sechdrs, module);
307 if (!module->arch.tramp)
308 return -ENOENT;
309
310 return 0;
311}
312#endif
313