1 /* Kernel module help for sh64.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 Copyright 2004 SuperH (UK) Ltd
18 Author: Richard Curnow
20 Based on the sh version, and on code from the sh64-specific parts of
21 modutils, originally written by Richard Curnow and Ben Gaster.
24 #include <linux/moduleloader.h>
25 #include <linux/elf.h>
26 #include <linux/vmalloc.h>
28 #include <linux/string.h>
29 #include <linux/kernel.h>
34 #define DEBUGP(fmt...)
37 void *module_alloc(unsigned long size
)
45 /* Free memory returned from module_alloc */
46 void module_free(struct module
*mod
, void *module_region
)
49 /* FIXME: If module_region == mod->init_region, trim exception
53 /* We don't need anything special. */
54 int module_frob_arch_sections(Elf_Ehdr
*hdr
,
62 int apply_relocate_add(Elf32_Shdr
*sechdrs
,
64 unsigned int symindex
,
69 Elf32_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
71 Elf32_Addr relocation
;
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 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
82 /* This is the symbol it is referring to. Note that all
83 undefined symbols have been resolved. */
84 sym
= (Elf32_Sym
*)sechdrs
[symindex
].sh_addr
85 + ELF32_R_SYM(rel
[i
].r_info
);
86 relocation
= sym
->st_value
+ rel
[i
].r_addend
;
87 align
= (int)location
& 3;
89 /* For text addresses, bit2 of the st_other field indicates
90 * whether the symbol is SHmedia (1) or SHcompact (0). If
91 * SHmedia, the LSB of the symbol needs to be asserted
92 * for the CPU to be in SHmedia mode when it starts executing
93 * the branch target. */
94 is_shmedia
= (sym
->st_other
& 4) ? 1 : 0;
99 switch (ELF32_R_TYPE(rel
[i
].r_info
)) {
101 DEBUGP("R_SH_DIR32 @%08lx = %08lx\n", (unsigned long) location
, (unsigned long) relocation
);
102 *location
+= relocation
;
105 DEBUGP("R_SH_REL32 @%08lx = %08lx\n", (unsigned long) location
, (unsigned long) relocation
);
106 relocation
-= (Elf32_Addr
) location
;
107 *location
+= relocation
;
110 DEBUGP("R_SH_IMM_LOW16 @%08lx = %08lx\n", (unsigned long) location
, (unsigned long) relocation
);
111 *location
= (*location
& ~0x3fffc00) |
112 ((relocation
& 0xffff) << 10);
114 case R_SH_IMM_MEDLOW16
:
115 DEBUGP("R_SH_IMM_MEDLOW16 @%08lx = %08lx\n", (unsigned long) location
, (unsigned long) relocation
);
116 *location
= (*location
& ~0x3fffc00) |
117 (((relocation
>> 16) & 0xffff) << 10);
119 case R_SH_IMM_LOW16_PCREL
:
120 DEBUGP("R_SH_IMM_LOW16_PCREL @%08lx = %08lx\n", (unsigned long) location
, (unsigned long) relocation
);
121 relocation
-= (Elf32_Addr
) location
;
122 *location
= (*location
& ~0x3fffc00) |
123 ((relocation
& 0xffff) << 10);
125 case R_SH_IMM_MEDLOW16_PCREL
:
126 DEBUGP("R_SH_IMM_MEDLOW16_PCREL @%08lx = %08lx\n", (unsigned long) location
, (unsigned long) relocation
);
127 relocation
-= (Elf32_Addr
) location
;
128 *location
= (*location
& ~0x3fffc00) |
129 (((relocation
>> 16) & 0xffff) << 10);
132 printk(KERN_ERR
"module %s: Unknown relocation: %u\n",
133 me
->name
, ELF32_R_TYPE(rel
[i
].r_info
));
140 int apply_relocate(Elf32_Shdr
*sechdrs
,
142 unsigned int symindex
,
146 printk(KERN_ERR
"module %s: REL RELOCATION unsupported\n",
151 int module_finalize(const Elf_Ehdr
*hdr
,
152 const Elf_Shdr
*sechdrs
,
158 void module_arch_cleanup(struct module
*mod
)