Merge branch 'e1000-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[linux-2.6] / arch / powerpc / kernel / module_32.c
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>
25 #include <linux/cache.h>
26 #include <linux/bug.h>
27
28 #include "setup.h"
29
30 #if 0
31 #define DEBUGP printk
32 #else
33 #define DEBUGP(fmt , ...)
34 #endif
35
36 LIST_HEAD(module_bug_list);
37
38 void *module_alloc(unsigned long size)
39 {
40         if (size == 0)
41                 return NULL;
42         return vmalloc(size);
43 }
44
45 /* Free memory returned from module_alloc */
46 void module_free(struct module *mod, void *module_region)
47 {
48         vfree(module_region);
49         /* FIXME: If module_region == mod->init_region, trim exception
50            table entries. */
51 }
52
53 /* Count how many different relocations (different symbol, different
54    addend) */
55 static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
56 {
57         unsigned int i, j, ret = 0;
58
59         /* Sure, this is order(n^2), but it's usually short, and not
60            time critical */
61         for (i = 0; i < num; i++) {
62                 for (j = 0; j < i; j++) {
63                         /* If this addend appeared before, it's
64                            already been counted */
65                         if (ELF32_R_SYM(rela[i].r_info)
66                             == ELF32_R_SYM(rela[j].r_info)
67                             && rela[i].r_addend == rela[j].r_addend)
68                                 break;
69                 }
70                 if (j == i) ret++;
71         }
72         return ret;
73 }
74
75 /* Get the potential trampolines size required of the init and
76    non-init sections */
77 static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
78                                   const Elf32_Shdr *sechdrs,
79                                   const char *secstrings,
80                                   int is_init)
81 {
82         unsigned long ret = 0;
83         unsigned i;
84
85         /* Everything marked ALLOC (this includes the exported
86            symbols) */
87         for (i = 1; i < hdr->e_shnum; i++) {
88                 /* If it's called *.init*, and we're not init, we're
89                    not interested */
90                 if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
91                     != is_init)
92                         continue;
93
94                 /* We don't want to look at debug sections. */
95                 if (strstr(secstrings + sechdrs[i].sh_name, ".debug") != 0)
96                         continue;
97
98                 if (sechdrs[i].sh_type == SHT_RELA) {
99                         DEBUGP("Found relocations in section %u\n", i);
100                         DEBUGP("Ptr: %p.  Number: %u\n",
101                                (void *)hdr + sechdrs[i].sh_offset,
102                                sechdrs[i].sh_size / sizeof(Elf32_Rela));
103                         ret += count_relocs((void *)hdr
104                                              + sechdrs[i].sh_offset,
105                                              sechdrs[i].sh_size
106                                              / sizeof(Elf32_Rela))
107                                 * sizeof(struct ppc_plt_entry);
108                 }
109         }
110
111         return ret;
112 }
113
114 int module_frob_arch_sections(Elf32_Ehdr *hdr,
115                               Elf32_Shdr *sechdrs,
116                               char *secstrings,
117                               struct module *me)
118 {
119         unsigned int i;
120
121         /* Find .plt and .init.plt sections */
122         for (i = 0; i < hdr->e_shnum; i++) {
123                 if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
124                         me->arch.init_plt_section = i;
125                 else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
126                         me->arch.core_plt_section = i;
127         }
128         if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
129                 printk("Module doesn't contain .plt or .init.plt sections.\n");
130                 return -ENOEXEC;
131         }
132
133         /* Override their sizes */
134         sechdrs[me->arch.core_plt_section].sh_size
135                 = get_plt_size(hdr, sechdrs, secstrings, 0);
136         sechdrs[me->arch.init_plt_section].sh_size
137                 = get_plt_size(hdr, sechdrs, secstrings, 1);
138         return 0;
139 }
140
141 int apply_relocate(Elf32_Shdr *sechdrs,
142                    const char *strtab,
143                    unsigned int symindex,
144                    unsigned int relsec,
145                    struct module *module)
146 {
147         printk(KERN_ERR "%s: Non-ADD RELOCATION unsupported\n",
148                module->name);
149         return -ENOEXEC;
150 }
151
152 static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
153 {
154         if (entry->jump[0] == 0x3d600000 + ((val + 0x8000) >> 16)
155             && entry->jump[1] == 0x396b0000 + (val & 0xffff))
156                 return 1;
157         return 0;
158 }
159
160 /* Set up a trampoline in the PLT to bounce us to the distant function */
161 static uint32_t do_plt_call(void *location,
162                             Elf32_Addr val,
163                             Elf32_Shdr *sechdrs,
164                             struct module *mod)
165 {
166         struct ppc_plt_entry *entry;
167
168         DEBUGP("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
169         /* Init, or core PLT? */
170         if (location >= mod->module_core
171             && location < mod->module_core + mod->core_size)
172                 entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
173         else
174                 entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
175
176         /* Find this entry, or if that fails, the next avail. entry */
177         while (entry->jump[0]) {
178                 if (entry_matches(entry, val)) return (uint32_t)entry;
179                 entry++;
180         }
181
182         /* Stolen from Paul Mackerras as well... */
183         entry->jump[0] = 0x3d600000+((val+0x8000)>>16); /* lis r11,sym@ha */
184         entry->jump[1] = 0x396b0000 + (val&0xffff);     /* addi r11,r11,sym@l*/
185         entry->jump[2] = 0x7d6903a6;                    /* mtctr r11 */
186         entry->jump[3] = 0x4e800420;                    /* bctr */
187
188         DEBUGP("Initialized plt for 0x%x at %p\n", val, entry);
189         return (uint32_t)entry;
190 }
191
192 int apply_relocate_add(Elf32_Shdr *sechdrs,
193                        const char *strtab,
194                        unsigned int symindex,
195                        unsigned int relsec,
196                        struct module *module)
197 {
198         unsigned int i;
199         Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
200         Elf32_Sym *sym;
201         uint32_t *location;
202         uint32_t value;
203
204         DEBUGP("Applying ADD relocate section %u to %u\n", relsec,
205                sechdrs[relsec].sh_info);
206         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
207                 /* This is where to make the change */
208                 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
209                         + rela[i].r_offset;
210                 /* This is the symbol it is referring to.  Note that all
211                    undefined symbols have been resolved.  */
212                 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
213                         + ELF32_R_SYM(rela[i].r_info);
214                 /* `Everything is relative'. */
215                 value = sym->st_value + rela[i].r_addend;
216
217                 switch (ELF32_R_TYPE(rela[i].r_info)) {
218                 case R_PPC_ADDR32:
219                         /* Simply set it */
220                         *(uint32_t *)location = value;
221                         break;
222
223                 case R_PPC_ADDR16_LO:
224                         /* Low half of the symbol */
225                         *(uint16_t *)location = value;
226                         break;
227
228                 case R_PPC_ADDR16_HI:
229                         /* Higher half of the symbol */
230                         *(uint16_t *)location = (value >> 16);
231                         break;
232
233                 case R_PPC_ADDR16_HA:
234                         /* Sign-adjusted lower 16 bits: PPC ELF ABI says:
235                            (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF.
236                            This is the same, only sane.
237                          */
238                         *(uint16_t *)location = (value + 0x8000) >> 16;
239                         break;
240
241                 case R_PPC_REL24:
242                         if ((int)(value - (uint32_t)location) < -0x02000000
243                             || (int)(value - (uint32_t)location) >= 0x02000000)
244                                 value = do_plt_call(location, value,
245                                                     sechdrs, module);
246
247                         /* Only replace bits 2 through 26 */
248                         DEBUGP("REL24 value = %08X. location = %08X\n",
249                                value, (uint32_t)location);
250                         DEBUGP("Location before: %08X.\n",
251                                *(uint32_t *)location);
252                         *(uint32_t *)location
253                                 = (*(uint32_t *)location & ~0x03fffffc)
254                                 | ((value - (uint32_t)location)
255                                    & 0x03fffffc);
256                         DEBUGP("Location after: %08X.\n",
257                                *(uint32_t *)location);
258                         DEBUGP("ie. jump to %08X+%08X = %08X\n",
259                                *(uint32_t *)location & 0x03fffffc,
260                                (uint32_t)location,
261                                (*(uint32_t *)location & 0x03fffffc)
262                                + (uint32_t)location);
263                         break;
264
265                 case R_PPC_REL32:
266                         /* 32-bit relative jump. */
267                         *(uint32_t *)location = value - (uint32_t)location;
268                         break;
269
270                 default:
271                         printk("%s: unknown ADD relocation: %u\n",
272                                module->name,
273                                ELF32_R_TYPE(rela[i].r_info));
274                         return -ENOEXEC;
275                 }
276         }
277         return 0;
278 }
279
280 static const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
281                                     const Elf_Shdr *sechdrs,
282                                     const char *name)
283 {
284         char *secstrings;
285         unsigned int i;
286
287         secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
288         for (i = 1; i < hdr->e_shnum; i++)
289                 if (strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
290                         return &sechdrs[i];
291         return NULL;
292 }
293
294 int module_finalize(const Elf_Ehdr *hdr,
295                     const Elf_Shdr *sechdrs,
296                     struct module *me)
297 {
298         const Elf_Shdr *sect;
299         int err;
300
301         err = module_bug_finalize(hdr, sechdrs, me);
302         if (err)                /* never true, currently */
303                 return err;
304
305         /* Apply feature fixups */
306         sect = find_section(hdr, sechdrs, "__ftr_fixup");
307         if (sect != NULL)
308                 do_feature_fixups(cur_cpu_spec->cpu_features,
309                                   (void *)sect->sh_addr,
310                                   (void *)sect->sh_addr + sect->sh_size);
311
312         return 0;
313 }
314
315 void module_arch_cleanup(struct module *mod)
316 {
317         module_bug_cleanup(mod);
318 }
319
320 struct bug_entry *module_find_bug(unsigned long bugaddr)
321 {
322         struct mod_arch_specific *mod;
323         unsigned int i;
324         struct bug_entry *bug;
325
326         list_for_each_entry(mod, &module_bug_list, bug_list) {
327                 bug = mod->bug_table;
328                 for (i = 0; i < mod->num_bugs; ++i, ++bug)
329                         if (bugaddr == bug->bug_addr)
330                                 return bug;
331         }
332         return NULL;
333 }