Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / arch / x86_64 / kernel / module.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
27 #include <asm/system.h>
28 #include <asm/page.h>
29 #include <asm/pgtable.h>
30
31 #define DEBUGP(fmt...) 
32
33 void module_free(struct module *mod, void *module_region)
34 {
35         vfree(module_region);
36 }
37
38 void *module_alloc(unsigned long size)
39 {
40         struct vm_struct *area;
41
42         if (!size)
43                 return NULL;
44         size = PAGE_ALIGN(size);
45         if (size > MODULES_LEN)
46                 return NULL;
47
48         area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
49         if (!area)
50                 return NULL;
51
52         return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
53 }
54
55 /* We don't need anything special. */
56 int module_frob_arch_sections(Elf_Ehdr *hdr,
57                               Elf_Shdr *sechdrs,
58                               char *secstrings,
59                               struct module *mod)
60 {
61         return 0;
62 }
63
64 int apply_relocate_add(Elf64_Shdr *sechdrs,
65                    const char *strtab,
66                    unsigned int symindex,
67                    unsigned int relsec,
68                    struct module *me)
69 {
70         unsigned int i;
71         Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
72         Elf64_Sym *sym;
73         void *loc;
74         u64 val; 
75
76         DEBUGP("Applying relocate section %u to %u\n", relsec,
77                sechdrs[relsec].sh_info);
78         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
79                 /* This is where to make the change */
80                 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
81                         + rel[i].r_offset;
82
83                 /* This is the symbol it is referring to.  Note that all
84                    undefined symbols have been resolved.  */
85                 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
86                         + ELF64_R_SYM(rel[i].r_info);
87
88                 DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
89                        (int)ELF64_R_TYPE(rel[i].r_info), 
90                        sym->st_value, rel[i].r_addend, (u64)loc);
91
92                 val = sym->st_value + rel[i].r_addend; 
93
94                 switch (ELF64_R_TYPE(rel[i].r_info)) {
95                 case R_X86_64_NONE:
96                         break;
97                 case R_X86_64_64:
98                         *(u64 *)loc = val;
99                         break;
100                 case R_X86_64_32:
101                         *(u32 *)loc = val;
102                         if (val != *(u32 *)loc)
103                                 goto overflow;
104                         break;
105                 case R_X86_64_32S:
106                         *(s32 *)loc = val;
107                         if ((s64)val != *(s32 *)loc)
108                                 goto overflow;
109                         break;
110                 case R_X86_64_PC32: 
111                         val -= (u64)loc;
112                         *(u32 *)loc = val;
113 #if 0
114                         if ((s64)val != *(s32 *)loc)
115                                 goto overflow; 
116 #endif
117                         break;
118                 default:
119                         printk(KERN_ERR "module %s: Unknown rela relocation: %Lu\n",
120                                me->name, ELF64_R_TYPE(rel[i].r_info));
121                         return -ENOEXEC;
122                 }
123         }
124         return 0;
125
126 overflow:
127         printk(KERN_ERR "overflow in relocation type %d val %Lx\n", 
128                (int)ELF64_R_TYPE(rel[i].r_info), val);
129         printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
130                me->name);
131         return -ENOEXEC;
132 }
133
134 int apply_relocate(Elf_Shdr *sechdrs,
135                    const char *strtab,
136                    unsigned int symindex,
137                    unsigned int relsec,
138                    struct module *me)
139 {
140         printk("non add relocation not supported\n");
141         return -ENOSYS;
142
143
144 extern void apply_alternatives(void *start, void *end); 
145
146 int module_finalize(const Elf_Ehdr *hdr,
147                     const Elf_Shdr *sechdrs,
148                     struct module *me)
149 {
150         const Elf_Shdr *s;
151         char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
152
153         /* look for .altinstructions to patch */ 
154         for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { 
155                 void *seg;              
156                 if (strcmp(".altinstructions", secstrings + s->sh_name))
157                         continue;
158                 seg = (void *)s->sh_addr; 
159                 apply_alternatives(seg, seg + s->sh_size); 
160         }       
161         return 0;
162 }
163
164 void module_arch_cleanup(struct module *mod)
165 {
166 }