mmc: don't use weight32()
[sfrench/cifs-2.6.git] / arch / x86 / kernel / module_64.c
1 /*  Kernel module help for x86-64
2     Copyright (C) 2001 Rusty Russell.
3     Copyright (C) 2002,2003 Andi Kleen, SuSE Labs.
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
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/slab.h>
26 #include <linux/bug.h>
27
28 #include <asm/system.h>
29 #include <asm/page.h>
30 #include <asm/pgtable.h>
31
32 #define DEBUGP(fmt...) 
33
34 #ifndef CONFIG_UML
35 void module_free(struct module *mod, void *module_region)
36 {
37         vfree(module_region);
38         /* FIXME: If module_region == mod->init_region, trim exception
39            table entries. */
40 }
41
42 void *module_alloc(unsigned long size)
43 {
44         struct vm_struct *area;
45
46         if (!size)
47                 return NULL;
48         size = PAGE_ALIGN(size);
49         if (size > MODULES_LEN)
50                 return NULL;
51
52         area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
53         if (!area)
54                 return NULL;
55
56         return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
57 }
58 #endif
59
60 /* We don't need anything special. */
61 int module_frob_arch_sections(Elf_Ehdr *hdr,
62                               Elf_Shdr *sechdrs,
63                               char *secstrings,
64                               struct module *mod)
65 {
66         return 0;
67 }
68
69 int apply_relocate_add(Elf64_Shdr *sechdrs,
70                    const char *strtab,
71                    unsigned int symindex,
72                    unsigned int relsec,
73                    struct module *me)
74 {
75         unsigned int i;
76         Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
77         Elf64_Sym *sym;
78         void *loc;
79         u64 val; 
80
81         DEBUGP("Applying relocate section %u to %u\n", relsec,
82                sechdrs[relsec].sh_info);
83         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
84                 /* This is where to make the change */
85                 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
86                         + rel[i].r_offset;
87
88                 /* This is the symbol it is referring to.  Note that all
89                    undefined symbols have been resolved.  */
90                 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
91                         + ELF64_R_SYM(rel[i].r_info);
92
93                 DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
94                        (int)ELF64_R_TYPE(rel[i].r_info), 
95                        sym->st_value, rel[i].r_addend, (u64)loc);
96
97                 val = sym->st_value + rel[i].r_addend; 
98
99                 switch (ELF64_R_TYPE(rel[i].r_info)) {
100                 case R_X86_64_NONE:
101                         break;
102                 case R_X86_64_64:
103                         *(u64 *)loc = val;
104                         break;
105                 case R_X86_64_32:
106                         *(u32 *)loc = val;
107                         if (val != *(u32 *)loc)
108                                 goto overflow;
109                         break;
110                 case R_X86_64_32S:
111                         *(s32 *)loc = val;
112                         if ((s64)val != *(s32 *)loc)
113                                 goto overflow;
114                         break;
115                 case R_X86_64_PC32: 
116                         val -= (u64)loc;
117                         *(u32 *)loc = val;
118 #if 0
119                         if ((s64)val != *(s32 *)loc)
120                                 goto overflow; 
121 #endif
122                         break;
123                 default:
124                         printk(KERN_ERR "module %s: Unknown rela relocation: %Lu\n",
125                                me->name, ELF64_R_TYPE(rel[i].r_info));
126                         return -ENOEXEC;
127                 }
128         }
129         return 0;
130
131 overflow:
132         printk(KERN_ERR "overflow in relocation type %d val %Lx\n", 
133                (int)ELF64_R_TYPE(rel[i].r_info), val);
134         printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
135                me->name);
136         return -ENOEXEC;
137 }
138
139 int apply_relocate(Elf_Shdr *sechdrs,
140                    const char *strtab,
141                    unsigned int symindex,
142                    unsigned int relsec,
143                    struct module *me)
144 {
145         printk("non add relocation not supported\n");
146         return -ENOSYS;
147
148
149 int module_finalize(const Elf_Ehdr *hdr,
150                     const Elf_Shdr *sechdrs,
151                     struct module *me)
152 {
153         const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL;
154         char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
155
156         for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
157                 if (!strcmp(".text", secstrings + s->sh_name))
158                         text = s;
159                 if (!strcmp(".altinstructions", secstrings + s->sh_name))
160                         alt = s;
161                 if (!strcmp(".smp_locks", secstrings + s->sh_name))
162                         locks= s;
163         }
164
165         if (alt) {
166                 /* patch .altinstructions */
167                 void *aseg = (void *)alt->sh_addr;
168                 apply_alternatives(aseg, aseg + alt->sh_size);
169         }
170         if (locks && text) {
171                 void *lseg = (void *)locks->sh_addr;
172                 void *tseg = (void *)text->sh_addr;
173                 alternatives_smp_module_add(me, me->name,
174                                             lseg, lseg + locks->sh_size,
175                                             tseg, tseg + text->sh_size);
176         }
177
178         return module_bug_finalize(hdr, sechdrs, me);
179 }
180
181 void module_arch_cleanup(struct module *mod)
182 {
183         alternatives_smp_module_del(mod);
184         module_bug_cleanup(mod);
185 }