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