1 /* Kernel module help for SH.
3 SHcompact version by Kaz Kojima and Paul Mundt.
7 Copyright 2004 SuperH (UK) Ltd
10 Based on the sh version, and on code from the sh64-specific parts of
11 modutils, originally written by Richard Curnow and Ben Gaster.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/moduleloader.h>
28 #include <linux/elf.h>
29 #include <linux/vmalloc.h>
31 #include <linux/string.h>
32 #include <linux/kernel.h>
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 #ifdef CONFIG_SUPERH32
60 #define COPY_UNALIGNED_WORD(sw, tw, align) \
62 void *__s = &(sw), *__t = &(tw); \
63 unsigned short *__s2 = __s, *__t2 = __t; \
64 unsigned char *__s1 = __s, *__t1 = __t; \
68 *(unsigned long *) __t = *(unsigned long *) __s; \
83 /* One thing SHmedia doesn't screw up! */
84 #define COPY_UNALIGNED_WORD(sw, tw, align) { (tw) = (sw); }
87 int apply_relocate_add(Elf32_Shdr
*sechdrs
,
89 unsigned int symindex
,
94 Elf32_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
96 Elf32_Addr relocation
;
101 pr_debug("Applying relocate section %u to %u\n", relsec
,
102 sechdrs
[relsec
].sh_info
);
103 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
104 /* This is where to make the change */
105 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
107 /* This is the symbol it is referring to. Note that all
108 undefined symbols have been resolved. */
109 sym
= (Elf32_Sym
*)sechdrs
[symindex
].sh_addr
110 + ELF32_R_SYM(rel
[i
].r_info
);
111 relocation
= sym
->st_value
+ rel
[i
].r_addend
;
112 align
= (int)location
& 3;
114 #ifdef CONFIG_SUPERH64
115 /* For text addresses, bit2 of the st_other field indicates
116 * whether the symbol is SHmedia (1) or SHcompact (0). If
117 * SHmedia, the LSB of the symbol needs to be asserted
118 * for the CPU to be in SHmedia mode when it starts executing
119 * the branch target. */
120 relocation
|= (sym
->st_other
& 4);
123 switch (ELF32_R_TYPE(rel
[i
].r_info
)) {
125 COPY_UNALIGNED_WORD (*location
, value
, align
);
127 COPY_UNALIGNED_WORD (value
, *location
, align
);
130 relocation
= (relocation
- (Elf32_Addr
) location
);
131 COPY_UNALIGNED_WORD (*location
, value
, align
);
133 COPY_UNALIGNED_WORD (value
, *location
, align
);
136 *location
= (*location
& ~0x3fffc00) |
137 ((relocation
& 0xffff) << 10);
139 case R_SH_IMM_MEDLOW16
:
140 *location
= (*location
& ~0x3fffc00) |
141 (((relocation
>> 16) & 0xffff) << 10);
143 case R_SH_IMM_LOW16_PCREL
:
144 relocation
-= (Elf32_Addr
) location
;
145 *location
= (*location
& ~0x3fffc00) |
146 ((relocation
& 0xffff) << 10);
148 case R_SH_IMM_MEDLOW16_PCREL
:
149 relocation
-= (Elf32_Addr
) location
;
150 *location
= (*location
& ~0x3fffc00) |
151 (((relocation
>> 16) & 0xffff) << 10);
154 printk(KERN_ERR
"module %s: Unknown relocation: %u\n",
155 me
->name
, ELF32_R_TYPE(rel
[i
].r_info
));
162 int apply_relocate(Elf32_Shdr
*sechdrs
,
164 unsigned int symindex
,
168 printk(KERN_ERR
"module %s: REL RELOCATION unsupported\n",
173 int module_finalize(const Elf_Ehdr
*hdr
,
174 const Elf_Shdr
*sechdrs
,
180 void module_arch_cleanup(struct module
*mod
)