2 * Kernel module support for Nios II.
4 * Copyright (C) 2004 Microtronix Datacom Ltd.
5 * Written by Wentao Xu <xuwentao@microtronix.com>
6 * Copyright (C) 2001, 2003 Rusty Russell
8 * This file is subject to the terms and conditions of the GNU General
9 * Public License. See the file COPYING in the main directory of this
10 * archive for more details.
13 #include <linux/moduleloader.h>
14 #include <linux/elf.h>
16 #include <linux/vmalloc.h>
17 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kernel.h>
22 #include <asm/pgtable.h>
23 #include <asm/cacheflush.h>
26 * Modules should NOT be allocated with kmalloc for (obvious) reasons.
27 * But we do it for now to avoid relocation issues. CALL26/PCREL26 cannot reach
28 * from 0x80000000 (vmalloc area) to 0xc00000000 (kernel) (kmalloc returns
29 * addresses in 0xc0000000)
31 void *module_alloc(unsigned long size
)
35 return kmalloc(size
, GFP_KERNEL
);
38 /* Free memory returned from module_alloc */
39 void module_memfree(void *module_region
)
44 int apply_relocate_add(Elf32_Shdr
*sechdrs
, const char *strtab
,
45 unsigned int symindex
, unsigned int relsec
,
49 Elf32_Rela
*rela
= (void *)sechdrs
[relsec
].sh_addr
;
51 pr_debug("Applying relocate section %u to %u\n", relsec
,
52 sechdrs
[relsec
].sh_info
);
54 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rela
); i
++) {
55 /* This is where to make the change */
58 = ((void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
60 /* This is the symbol it is referring to. Note that all
61 undefined symbols have been resolved. */
63 = ((Elf32_Sym
*)sechdrs
[symindex
].sh_addr
64 + ELF32_R_SYM(rela
[i
].r_info
));
65 uint32_t v
= sym
->st_value
+ rela
[i
].r_addend
;
67 pr_debug("reltype %d 0x%x name:<%s>\n",
68 ELF32_R_TYPE(rela
[i
].r_info
),
69 rela
[i
].r_offset
, strtab
+ sym
->st_name
);
71 switch (ELF32_R_TYPE(rela
[i
].r_info
)) {
74 case R_NIOS2_BFD_RELOC_32
:
78 v
-= (uint32_t)loc
+ 4;
79 if ((int32_t)v
> 0x7fff ||
80 (int32_t)v
< -(int32_t)0x8000) {
81 pr_err("module %s: relocation overflow\n",
86 *loc
= ((((word
>> 22) << 16) | (v
& 0xffff)) << 6) |
91 pr_err("module %s: dangerous relocation\n",
95 if ((v
>> 28) != ((uint32_t)loc
>> 28)) {
96 pr_err("module %s: relocation overflow\n",
100 *loc
= (*loc
& 0x3f) | ((v
>> 2) << 6);
104 *loc
= ((((word
>> 22) << 16) |
105 ((v
>> 16) & 0xffff)) << 6) | (word
& 0x3f);
109 *loc
= ((((word
>> 22) << 16) | (v
& 0xffff)) << 6) |
112 case R_NIOS2_HIADJ16
:
117 word2
= ((v
>> 16) + ((v
>> 15) & 1)) & 0xffff;
118 *loc
= ((((word
>> 22) << 16) | word2
) << 6) |
124 pr_err("module %s: Unknown reloc: %u\n",
125 mod
->name
, ELF32_R_TYPE(rela
[i
].r_info
));
133 int module_finalize(const Elf_Ehdr
*hdr
, const Elf_Shdr
*sechdrs
,