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>
23 #include <linux/string.h>
24 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/bug.h>
29 #include <asm/system.h>
31 #include <asm/pgtable.h>
33 #define DEBUGP(fmt...)
36 void *module_alloc(unsigned long size
)
38 struct vm_struct
*area
;
42 size
= PAGE_ALIGN(size
);
43 if (size
> MODULES_LEN
)
46 area
= __get_vm_area(size
, VM_ALLOC
, MODULES_VADDR
, MODULES_END
);
50 return __vmalloc_area(area
, GFP_KERNEL
, PAGE_KERNEL_EXEC
);
54 int apply_relocate_add(Elf64_Shdr
*sechdrs
,
56 unsigned int symindex
,
61 Elf64_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
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
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
)) {
92 if (val
!= *(u32
*)loc
)
97 if ((s64
)val
!= *(s32
*)loc
)
104 if ((s64
)val
!= *(s32
*)loc
)
109 printk(KERN_ERR
"module %s: Unknown rela relocation: %llu\n",
110 me
->name
, ELF64_R_TYPE(rel
[i
].r_info
));
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",
124 int apply_relocate(Elf_Shdr
*sechdrs
,
126 unsigned int symindex
,
130 printk(KERN_ERR
"non add relocation not supported\n");