generic: add __FINITDATA
[wrt350n-kernel.git] / arch / powerpc / kernel / module_32.c
blobeab313858315ef991b5efc0d6886433105e1ac89
1 /* Kernel module help for PPC.
2 Copyright (C) 2001 Rusty Russell.
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.
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.
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
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/cache.h>
26 #include <linux/bug.h>
27 #include <linux/sort.h>
29 #include "setup.h"
31 #if 0
32 #define DEBUGP printk
33 #else
34 #define DEBUGP(fmt , ...)
35 #endif
37 LIST_HEAD(module_bug_list);
39 void *module_alloc(unsigned long size)
41 if (size == 0)
42 return NULL;
43 return vmalloc(size);
46 /* Free memory returned from module_alloc */
47 void module_free(struct module *mod, void *module_region)
49 vfree(module_region);
50 /* FIXME: If module_region == mod->init_region, trim exception
51 table entries. */
54 /* Count how many different relocations (different symbol, different
55 addend) */
56 static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
58 unsigned int i, r_info, r_addend, _count_relocs;
60 _count_relocs = 0;
61 r_info = 0;
62 r_addend = 0;
63 for (i = 0; i < num; i++)
64 /* Only count 24-bit relocs, others don't need stubs */
65 if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
66 (r_info != ELF32_R_SYM(rela[i].r_info) ||
67 r_addend != rela[i].r_addend)) {
68 _count_relocs++;
69 r_info = ELF32_R_SYM(rela[i].r_info);
70 r_addend = rela[i].r_addend;
73 return _count_relocs;
76 static int relacmp(const void *_x, const void *_y)
78 const Elf32_Rela *x, *y;
80 y = (Elf32_Rela *)_x;
81 x = (Elf32_Rela *)_y;
83 /* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to
84 * make the comparison cheaper/faster. It won't affect the sorting or
85 * the counting algorithms' performance
87 if (x->r_info < y->r_info)
88 return -1;
89 else if (x->r_info > y->r_info)
90 return 1;
91 else if (x->r_addend < y->r_addend)
92 return -1;
93 else if (x->r_addend > y->r_addend)
94 return 1;
95 else
96 return 0;
99 static void relaswap(void *_x, void *_y, int size)
101 uint32_t *x, *y, tmp;
102 int i;
104 y = (uint32_t *)_x;
105 x = (uint32_t *)_y;
107 for (i = 0; i < sizeof(Elf32_Rela) / sizeof(uint32_t); i++) {
108 tmp = x[i];
109 x[i] = y[i];
110 y[i] = tmp;
114 /* Get the potential trampolines size required of the init and
115 non-init sections */
116 static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
117 const Elf32_Shdr *sechdrs,
118 const char *secstrings,
119 int is_init)
121 unsigned long ret = 0;
122 unsigned i;
124 /* Everything marked ALLOC (this includes the exported
125 symbols) */
126 for (i = 1; i < hdr->e_shnum; i++) {
127 /* If it's called *.init*, and we're not init, we're
128 not interested */
129 if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
130 != is_init)
131 continue;
133 /* We don't want to look at debug sections. */
134 if (strstr(secstrings + sechdrs[i].sh_name, ".debug") != 0)
135 continue;
137 if (sechdrs[i].sh_type == SHT_RELA) {
138 DEBUGP("Found relocations in section %u\n", i);
139 DEBUGP("Ptr: %p. Number: %u\n",
140 (void *)hdr + sechdrs[i].sh_offset,
141 sechdrs[i].sh_size / sizeof(Elf32_Rela));
143 /* Sort the relocation information based on a symbol and
144 * addend key. This is a stable O(n*log n) complexity
145 * alogrithm but it will reduce the complexity of
146 * count_relocs() to linear complexity O(n)
148 sort((void *)hdr + sechdrs[i].sh_offset,
149 sechdrs[i].sh_size / sizeof(Elf32_Rela),
150 sizeof(Elf32_Rela), relacmp, relaswap);
152 ret += count_relocs((void *)hdr
153 + sechdrs[i].sh_offset,
154 sechdrs[i].sh_size
155 / sizeof(Elf32_Rela))
156 * sizeof(struct ppc_plt_entry);
160 return ret;
163 int module_frob_arch_sections(Elf32_Ehdr *hdr,
164 Elf32_Shdr *sechdrs,
165 char *secstrings,
166 struct module *me)
168 unsigned int i;
170 /* Find .plt and .init.plt sections */
171 for (i = 0; i < hdr->e_shnum; i++) {
172 if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
173 me->arch.init_plt_section = i;
174 else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
175 me->arch.core_plt_section = i;
177 if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
178 printk("Module doesn't contain .plt or .init.plt sections.\n");
179 return -ENOEXEC;
182 /* Override their sizes */
183 sechdrs[me->arch.core_plt_section].sh_size
184 = get_plt_size(hdr, sechdrs, secstrings, 0);
185 sechdrs[me->arch.init_plt_section].sh_size
186 = get_plt_size(hdr, sechdrs, secstrings, 1);
187 return 0;
190 int apply_relocate(Elf32_Shdr *sechdrs,
191 const char *strtab,
192 unsigned int symindex,
193 unsigned int relsec,
194 struct module *module)
196 printk(KERN_ERR "%s: Non-ADD RELOCATION unsupported\n",
197 module->name);
198 return -ENOEXEC;
201 static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
203 if (entry->jump[0] == 0x3d600000 + ((val + 0x8000) >> 16)
204 && entry->jump[1] == 0x396b0000 + (val & 0xffff))
205 return 1;
206 return 0;
209 /* Set up a trampoline in the PLT to bounce us to the distant function */
210 static uint32_t do_plt_call(void *location,
211 Elf32_Addr val,
212 Elf32_Shdr *sechdrs,
213 struct module *mod)
215 struct ppc_plt_entry *entry;
217 DEBUGP("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
218 /* Init, or core PLT? */
219 if (location >= mod->module_core
220 && location < mod->module_core + mod->core_size)
221 entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
222 else
223 entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
225 /* Find this entry, or if that fails, the next avail. entry */
226 while (entry->jump[0]) {
227 if (entry_matches(entry, val)) return (uint32_t)entry;
228 entry++;
231 /* Stolen from Paul Mackerras as well... */
232 entry->jump[0] = 0x3d600000+((val+0x8000)>>16); /* lis r11,sym@ha */
233 entry->jump[1] = 0x396b0000 + (val&0xffff); /* addi r11,r11,sym@l*/
234 entry->jump[2] = 0x7d6903a6; /* mtctr r11 */
235 entry->jump[3] = 0x4e800420; /* bctr */
237 DEBUGP("Initialized plt for 0x%x at %p\n", val, entry);
238 return (uint32_t)entry;
241 int apply_relocate_add(Elf32_Shdr *sechdrs,
242 const char *strtab,
243 unsigned int symindex,
244 unsigned int relsec,
245 struct module *module)
247 unsigned int i;
248 Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
249 Elf32_Sym *sym;
250 uint32_t *location;
251 uint32_t value;
253 DEBUGP("Applying ADD relocate section %u to %u\n", relsec,
254 sechdrs[relsec].sh_info);
255 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
256 /* This is where to make the change */
257 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
258 + rela[i].r_offset;
259 /* This is the symbol it is referring to. Note that all
260 undefined symbols have been resolved. */
261 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
262 + ELF32_R_SYM(rela[i].r_info);
263 /* `Everything is relative'. */
264 value = sym->st_value + rela[i].r_addend;
266 switch (ELF32_R_TYPE(rela[i].r_info)) {
267 case R_PPC_ADDR32:
268 /* Simply set it */
269 *(uint32_t *)location = value;
270 break;
272 case R_PPC_ADDR16_LO:
273 /* Low half of the symbol */
274 *(uint16_t *)location = value;
275 break;
277 case R_PPC_ADDR16_HI:
278 /* Higher half of the symbol */
279 *(uint16_t *)location = (value >> 16);
280 break;
282 case R_PPC_ADDR16_HA:
283 /* Sign-adjusted lower 16 bits: PPC ELF ABI says:
284 (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF.
285 This is the same, only sane.
287 *(uint16_t *)location = (value + 0x8000) >> 16;
288 break;
290 case R_PPC_REL24:
291 if ((int)(value - (uint32_t)location) < -0x02000000
292 || (int)(value - (uint32_t)location) >= 0x02000000)
293 value = do_plt_call(location, value,
294 sechdrs, module);
296 /* Only replace bits 2 through 26 */
297 DEBUGP("REL24 value = %08X. location = %08X\n",
298 value, (uint32_t)location);
299 DEBUGP("Location before: %08X.\n",
300 *(uint32_t *)location);
301 *(uint32_t *)location
302 = (*(uint32_t *)location & ~0x03fffffc)
303 | ((value - (uint32_t)location)
304 & 0x03fffffc);
305 DEBUGP("Location after: %08X.\n",
306 *(uint32_t *)location);
307 DEBUGP("ie. jump to %08X+%08X = %08X\n",
308 *(uint32_t *)location & 0x03fffffc,
309 (uint32_t)location,
310 (*(uint32_t *)location & 0x03fffffc)
311 + (uint32_t)location);
312 break;
314 case R_PPC_REL32:
315 /* 32-bit relative jump. */
316 *(uint32_t *)location = value - (uint32_t)location;
317 break;
319 default:
320 printk("%s: unknown ADD relocation: %u\n",
321 module->name,
322 ELF32_R_TYPE(rela[i].r_info));
323 return -ENOEXEC;
326 return 0;
329 static const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
330 const Elf_Shdr *sechdrs,
331 const char *name)
333 char *secstrings;
334 unsigned int i;
336 secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
337 for (i = 1; i < hdr->e_shnum; i++)
338 if (strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
339 return &sechdrs[i];
340 return NULL;
343 int module_finalize(const Elf_Ehdr *hdr,
344 const Elf_Shdr *sechdrs,
345 struct module *me)
347 const Elf_Shdr *sect;
348 int err;
350 err = module_bug_finalize(hdr, sechdrs, me);
351 if (err) /* never true, currently */
352 return err;
354 /* Apply feature fixups */
355 sect = find_section(hdr, sechdrs, "__ftr_fixup");
356 if (sect != NULL)
357 do_feature_fixups(cur_cpu_spec->cpu_features,
358 (void *)sect->sh_addr,
359 (void *)sect->sh_addr + sect->sh_size);
361 return 0;
364 void module_arch_cleanup(struct module *mod)
366 module_bug_cleanup(mod);
369 struct bug_entry *module_find_bug(unsigned long bugaddr)
371 struct mod_arch_specific *mod;
372 unsigned int i;
373 struct bug_entry *bug;
375 list_for_each_entry(mod, &module_bug_list, bug_list) {
376 bug = mod->bug_table;
377 for (i = 0; i < mod->num_bugs; ++i, ++bug)
378 if (bugaddr == bug->bug_addr)
379 return bug;
381 return NULL;