Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel module help for PPC. |
2 | Copyright (C) 2001 Rusty Russell. | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
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> | |
7cc45e64 | 25 | #include <linux/ftrace.h> |
1da177e4 | 26 | #include <linux/cache.h> |
73c9ceab | 27 | #include <linux/bug.h> |
eda09fbd | 28 | #include <linux/sort.h> |
1da177e4 | 29 | |
21c4ff80 BH |
30 | #include "setup.h" |
31 | ||
1da177e4 LT |
32 | #if 0 |
33 | #define DEBUGP printk | |
34 | #else | |
35 | #define DEBUGP(fmt , ...) | |
36 | #endif | |
37 | ||
1da177e4 LT |
38 | /* Count how many different relocations (different symbol, different |
39 | addend) */ | |
40 | static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num) | |
41 | { | |
eda09fbd EM |
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 | /* Only count 24-bit relocs, others don't need stubs */ | |
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; | |
1da177e4 | 55 | } |
eda09fbd | 56 | |
7cc45e64 SR |
57 | #ifdef CONFIG_DYNAMIC_FTRACE |
58 | _count_relocs++; /* add one for ftrace_caller */ | |
59 | #endif | |
eda09fbd EM |
60 | return _count_relocs; |
61 | } | |
62 | ||
63 | static 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 | /* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to | |
71 | * make the comparison cheaper/faster. It won't affect the sorting or | |
72 | * the counting algorithms' performance | |
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 | ||
86 | static 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; | |
1da177e4 | 98 | } |
1da177e4 LT |
99 | } |
100 | ||
101 | /* Get the potential trampolines size required of the init and | |
102 | non-init sections */ | |
103 | static 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 | /* Everything marked ALLOC (this includes the exported | |
112 | symbols) */ | |
113 | for (i = 1; i < hdr->e_shnum; i++) { | |
114 | /* If it's called *.init*, and we're not init, we're | |
115 | not interested */ | |
116 | if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0) | |
117 | != is_init) | |
118 | continue; | |
119 | ||
120 | /* We don't want to look at debug sections. */ | |
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)); | |
eda09fbd EM |
129 | |
130 | /* Sort the relocation information based on a symbol and | |
131 | * addend key. This is a stable O(n*log n) complexity | |
132 | * alogrithm but it will reduce the complexity of | |
133 | * count_relocs() to linear complexity O(n) | |
134 | */ | |
135 | sort((void *)hdr + sechdrs[i].sh_offset, | |
136 | sechdrs[i].sh_size / sizeof(Elf32_Rela), | |
137 | sizeof(Elf32_Rela), relacmp, relaswap); | |
138 | ||
1da177e4 LT |
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 | ||
150 | int 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 | /* Find .plt and .init.plt sections */ | |
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 | /* Override their sizes */ | |
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 | ||
177 | int apply_relocate(Elf32_Shdr *sechdrs, | |
178 | const char *strtab, | |
179 | unsigned int symindex, | |
180 | unsigned int relsec, | |
181 | struct module *module) | |
182 | { | |
183 | printk(KERN_ERR "%s: Non-ADD RELOCATION unsupported\n", | |
184 | module->name); | |
185 | return -ENOEXEC; | |
186 | } | |
187 | ||
188 | static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val) | |
189 | { | |
190 | if (entry->jump[0] == 0x3d600000 + ((val + 0x8000) >> 16) | |
191 | && entry->jump[1] == 0x396b0000 + (val & 0xffff)) | |
192 | return 1; | |
193 | return 0; | |
194 | } | |
195 | ||
196 | /* Set up a trampoline in the PLT to bounce us to the distant function */ | |
197 | static uint32_t do_plt_call(void *location, | |
198 | Elf32_Addr val, | |
199 | Elf32_Shdr *sechdrs, | |
200 | struct module *mod) | |
201 | { | |
202 | struct ppc_plt_entry *entry; | |
203 | ||
204 | DEBUGP("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location); | |
205 | /* Init, or core PLT? */ | |
206 | if (location >= mod->module_core | |
207 | && location < mod->module_core + mod->core_size) | |
208 | entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr; | |
209 | else | |
210 | entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr; | |
211 | ||
212 | /* Find this entry, or if that fails, the next avail. entry */ | |
213 | while (entry->jump[0]) { | |
214 | if (entry_matches(entry, val)) return (uint32_t)entry; | |
215 | entry++; | |
216 | } | |
217 | ||
218 | /* Stolen from Paul Mackerras as well... */ | |
219 | entry->jump[0] = 0x3d600000+((val+0x8000)>>16); /* lis r11,sym@ha */ | |
220 | entry->jump[1] = 0x396b0000 + (val&0xffff); /* addi r11,r11,sym@l*/ | |
221 | entry->jump[2] = 0x7d6903a6; /* mtctr r11 */ | |
222 | entry->jump[3] = 0x4e800420; /* bctr */ | |
223 | ||
224 | DEBUGP("Initialized plt for 0x%x at %p\n", val, entry); | |
225 | return (uint32_t)entry; | |
226 | } | |
227 | ||
228 | int apply_relocate_add(Elf32_Shdr *sechdrs, | |
229 | const char *strtab, | |
230 | unsigned int symindex, | |
231 | unsigned int relsec, | |
232 | struct module *module) | |
233 | { | |
234 | unsigned int i; | |
235 | Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr; | |
236 | Elf32_Sym *sym; | |
237 | uint32_t *location; | |
238 | uint32_t value; | |
239 | ||
240 | DEBUGP("Applying ADD relocate section %u to %u\n", relsec, | |
241 | sechdrs[relsec].sh_info); | |
242 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { | |
243 | /* This is where to make the change */ | |
244 | location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr | |
245 | + rela[i].r_offset; | |
246 | /* This is the symbol it is referring to. Note that all | |
247 | undefined symbols have been resolved. */ | |
248 | sym = (Elf32_Sym *)sechdrs[symindex].sh_addr | |
249 | + ELF32_R_SYM(rela[i].r_info); | |
250 | /* `Everything is relative'. */ | |
251 | value = sym->st_value + rela[i].r_addend; | |
252 | ||
253 | switch (ELF32_R_TYPE(rela[i].r_info)) { | |
254 | case R_PPC_ADDR32: | |
255 | /* Simply set it */ | |
256 | *(uint32_t *)location = value; | |
257 | break; | |
258 | ||
259 | case R_PPC_ADDR16_LO: | |
260 | /* Low half of the symbol */ | |
261 | *(uint16_t *)location = value; | |
262 | break; | |
9a3d6458 SV |
263 | |
264 | case R_PPC_ADDR16_HI: | |
265 | /* Higher half of the symbol */ | |
266 | *(uint16_t *)location = (value >> 16); | |
267 | break; | |
268 | ||
1da177e4 LT |
269 | case R_PPC_ADDR16_HA: |
270 | /* Sign-adjusted lower 16 bits: PPC ELF ABI says: | |
271 | (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF. | |
272 | This is the same, only sane. | |
273 | */ | |
274 | *(uint16_t *)location = (value + 0x8000) >> 16; | |
275 | break; | |
276 | ||
277 | case R_PPC_REL24: | |
278 | if ((int)(value - (uint32_t)location) < -0x02000000 | |
279 | || (int)(value - (uint32_t)location) >= 0x02000000) | |
280 | value = do_plt_call(location, value, | |
281 | sechdrs, module); | |
282 | ||
283 | /* Only replace bits 2 through 26 */ | |
284 | DEBUGP("REL24 value = %08X. location = %08X\n", | |
285 | value, (uint32_t)location); | |
286 | DEBUGP("Location before: %08X.\n", | |
287 | *(uint32_t *)location); | |
288 | *(uint32_t *)location | |
289 | = (*(uint32_t *)location & ~0x03fffffc) | |
290 | | ((value - (uint32_t)location) | |
291 | & 0x03fffffc); | |
292 | DEBUGP("Location after: %08X.\n", | |
293 | *(uint32_t *)location); | |
294 | DEBUGP("ie. jump to %08X+%08X = %08X\n", | |
295 | *(uint32_t *)location & 0x03fffffc, | |
296 | (uint32_t)location, | |
297 | (*(uint32_t *)location & 0x03fffffc) | |
298 | + (uint32_t)location); | |
299 | break; | |
300 | ||
301 | case R_PPC_REL32: | |
302 | /* 32-bit relative jump. */ | |
303 | *(uint32_t *)location = value - (uint32_t)location; | |
304 | break; | |
305 | ||
306 | default: | |
307 | printk("%s: unknown ADD relocation: %u\n", | |
308 | module->name, | |
309 | ELF32_R_TYPE(rela[i].r_info)); | |
310 | return -ENOEXEC; | |
311 | } | |
312 | } | |
7cc45e64 SR |
313 | #ifdef CONFIG_DYNAMIC_FTRACE |
314 | module->arch.tramp = | |
315 | do_plt_call(module->module_core, | |
316 | (unsigned long)ftrace_caller, | |
317 | sechdrs, module); | |
318 | #endif | |
1da177e4 LT |
319 | return 0; |
320 | } |