1 /* Kernel module help for Nios2.
2 Copyright (C) 2004 Microtronix Datacom Ltd.
3 Copyright (C) 2001,03 Rusty Russell
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 Written by Wentao Xu <xuwentao@microtronix.com>
21 #include <linux/moduleloader.h>
22 #include <linux/elf.h>
23 #include <linux/vmalloc.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
31 #define DEBUGP(fmt , ...)
34 void *module_alloc(unsigned long size
)
42 /* Free memory returned from module_alloc */
43 void module_free(struct module
*mod
, void *module_region
)
46 /* FIXME: If module_region == mod->init_region, trim exception
50 /* We don't need anything special. */
51 int module_frob_arch_sections(Elf_Ehdr
*hdr
,
59 int apply_relocate(Elf32_Shdr
*sechdrs
,
61 unsigned int symindex
,
65 printk(KERN_ERR
"module %s: NO-ADD RELOCATION unsupported\n",
71 int apply_relocate_add (Elf32_Shdr
*sechdrs
, const char *strtab
,
72 unsigned int symindex
, unsigned int relsec
,
76 Elf32_Rela
*rela
= (void *)sechdrs
[relsec
].sh_addr
;
78 DEBUGP ("Applying relocate section %u to %u\n", relsec
,
79 sechdrs
[relsec
].sh_info
);
81 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof (*rela
); i
++) {
82 /* This is where to make the change */
85 = ((void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
87 /* This is the symbol it is referring to. Note that all
88 undefined symbols have been resolved. */
90 = ((Elf32_Sym
*)sechdrs
[symindex
].sh_addr
91 + ELF32_R_SYM (rela
[i
].r_info
));
92 uint32_t v
= sym
->st_value
+ rela
[i
].r_addend
;
94 switch (ELF32_R_TYPE (rela
[i
].r_info
)) {
98 case R_NIOS2_BFD_RELOC_32
:
102 case R_NIOS2_PCREL16
:
103 v
-= (uint32_t)loc
+ 4;
104 if ((int32_t)v
> 0x7fff ||
105 (int32_t)v
< -(int32_t)0x8000) {
107 "module %s: relocation overflow\n",
112 *loc
= ((((word
>> 22) << 16) | (v
& 0xffff)) << 6) | (word
& 0x3f);
118 "module %s: dangerous relocation\n",
122 if ((v
>> 28) != ((uint32_t)loc
>> 28)) {
124 "module %s: relocation overflow\n",
128 *loc
= (*loc
& 0x3f) | ((v
>> 2) << 6);
133 *loc
= ((((word
>> 22) << 16) | ((v
>>16) & 0xffff)) << 6) |
139 *loc
= ((((word
>> 22) << 16) | (v
& 0xffff)) << 6) |
143 case R_NIOS2_HIADJ16
:
148 word2
= ((v
>> 16) + ((v
>> 15) & 1)) & 0xffff;
149 *loc
= ((((word
>> 22) << 16) | word2
) << 6) |
155 printk (KERN_ERR
"module %s: Unknown reloc: %u\n",
156 mod
->name
, ELF32_R_TYPE (rela
[i
].r_info
));
164 int module_finalize(const Elf_Ehdr
*hdr
,
165 const Elf_Shdr
*sechdrs
,
171 void module_arch_cleanup(struct module
*mod
)