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