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