x86 module: merge the same functions in module_32.c and module_64.c
[linux-2.6.git] / arch / x86 / kernel / module_64.c
blob68ec7d87bb473aafca9cfcf8fac5b20f46b46f73
1 /* Kernel module help for x86-64
2 Copyright (C) 2001 Rusty Russell.
3 Copyright (C) 2002,2003 Andi Kleen, SuSE Labs.
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.
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.
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
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/mm.h>
26 #include <linux/slab.h>
27 #include <linux/bug.h>
29 #include <asm/system.h>
30 #include <asm/page.h>
31 #include <asm/pgtable.h>
33 #define DEBUGP(fmt...)
35 #ifndef CONFIG_UML
36 void *module_alloc(unsigned long size)
38 struct vm_struct *area;
40 if (!size)
41 return NULL;
42 size = PAGE_ALIGN(size);
43 if (size > MODULES_LEN)
44 return NULL;
46 area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
47 if (!area)
48 return NULL;
50 return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL_EXEC);
52 #endif
54 int apply_relocate_add(Elf64_Shdr *sechdrs,
55 const char *strtab,
56 unsigned int symindex,
57 unsigned int relsec,
58 struct module *me)
60 unsigned int i;
61 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
62 Elf64_Sym *sym;
63 void *loc;
64 u64 val;
66 DEBUGP("Applying relocate section %u to %u\n", relsec,
67 sechdrs[relsec].sh_info);
68 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
69 /* This is where to make the change */
70 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
71 + rel[i].r_offset;
73 /* This is the symbol it is referring to. Note that all
74 undefined symbols have been resolved. */
75 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
76 + ELF64_R_SYM(rel[i].r_info);
78 DEBUGP("type %d st_value %Lx r_addend %Lx loc %Lx\n",
79 (int)ELF64_R_TYPE(rel[i].r_info),
80 sym->st_value, rel[i].r_addend, (u64)loc);
82 val = sym->st_value + rel[i].r_addend;
84 switch (ELF64_R_TYPE(rel[i].r_info)) {
85 case R_X86_64_NONE:
86 break;
87 case R_X86_64_64:
88 *(u64 *)loc = val;
89 break;
90 case R_X86_64_32:
91 *(u32 *)loc = val;
92 if (val != *(u32 *)loc)
93 goto overflow;
94 break;
95 case R_X86_64_32S:
96 *(s32 *)loc = val;
97 if ((s64)val != *(s32 *)loc)
98 goto overflow;
99 break;
100 case R_X86_64_PC32:
101 val -= (u64)loc;
102 *(u32 *)loc = val;
103 #if 0
104 if ((s64)val != *(s32 *)loc)
105 goto overflow;
106 #endif
107 break;
108 default:
109 printk(KERN_ERR "module %s: Unknown rela relocation: %llu\n",
110 me->name, ELF64_R_TYPE(rel[i].r_info));
111 return -ENOEXEC;
114 return 0;
116 overflow:
117 printk(KERN_ERR "overflow in relocation type %d val %Lx\n",
118 (int)ELF64_R_TYPE(rel[i].r_info), val);
119 printk(KERN_ERR "`%s' likely not compiled with -mcmodel=kernel\n",
120 me->name);
121 return -ENOEXEC;
124 int apply_relocate(Elf_Shdr *sechdrs,
125 const char *strtab,
126 unsigned int symindex,
127 unsigned int relsec,
128 struct module *me)
130 printk(KERN_ERR "non add relocation not supported\n");
131 return -ENOSYS;