1 /* Kernel module help for M32R.
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
18 #include <linux/moduleloader.h>
19 #include <linux/elf.h>
20 #include <linux/vmalloc.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
28 #define DEBUGP(fmt...)
31 void *module_alloc(unsigned long size
)
36 return vmalloc_exec(size
);
43 /* Free memory returned from module_alloc */
44 void module_free(struct module
*mod
, void *module_region
)
49 /* We don't need anything special. */
50 int module_frob_arch_sections(Elf_Ehdr
*hdr
,
58 #define COPY_UNALIGNED_WORD(sw, tw, align) \
60 void *__s = &(sw), *__t = &(tw); \
61 unsigned short *__s2 = __s, *__t2 =__t; \
62 unsigned char *__s1 = __s, *__t1 =__t; \
66 *(unsigned long *) __t = *(unsigned long *) __s; \
81 #define COPY_UNALIGNED_HWORD(sw, tw, align) \
83 void *__s = &(sw), *__t = &(tw); \
84 unsigned short *__s2 = __s, *__t2 =__t; \
85 unsigned char *__s1 = __s, *__t1 =__t; \
98 int apply_relocate_add(Elf32_Shdr
*sechdrs
,
100 unsigned int symindex
,
105 Elf32_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
107 Elf32_Addr relocation
;
110 unsigned short *hlocation
;
111 unsigned short hvalue
;
115 DEBUGP("Applying relocate section %u to %u\n", relsec
,
116 sechdrs
[relsec
].sh_info
);
117 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
118 /* This is where to make the change */
119 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
121 /* This is the symbol it is referring to. Note that all
122 undefined symbols have been resolved. */
123 sym
= (Elf32_Sym
*)sechdrs
[symindex
].sh_addr
124 + ELF32_R_SYM(rel
[i
].r_info
);
125 relocation
= sym
->st_value
+ rel
[i
].r_addend
;
126 align
= (int)location
& 3;
128 switch (ELF32_R_TYPE(rel
[i
].r_info
)) {
130 COPY_UNALIGNED_WORD (*location
, value
, align
);
132 COPY_UNALIGNED_WORD (value
, *location
, align
);
134 case R_M32R_HI16_ULO_RELA
:
135 COPY_UNALIGNED_WORD (*location
, value
, align
);
136 relocation
= (relocation
>>16) & 0xffff;
137 /* RELA must has 0 at relocation field. */
139 COPY_UNALIGNED_WORD (value
, *location
, align
);
141 case R_M32R_HI16_SLO_RELA
:
142 COPY_UNALIGNED_WORD (*location
, value
, align
);
143 if (relocation
& 0x8000) relocation
+= 0x10000;
144 relocation
= (relocation
>>16) & 0xffff;
145 /* RELA must has 0 at relocation field. */
147 COPY_UNALIGNED_WORD (value
, *location
, align
);
150 hlocation
= (unsigned short *)location
;
151 relocation
= relocation
& 0xffff;
152 /* RELA must has 0 at relocation field. */
154 COPY_UNALIGNED_WORD (hvalue
, *hlocation
, align
);
156 case R_M32R_SDA16_RELA
:
157 case R_M32R_LO16_RELA
:
158 COPY_UNALIGNED_WORD (*location
, value
, align
);
159 relocation
= relocation
& 0xffff;
160 /* RELA must has 0 at relocation field. */
162 COPY_UNALIGNED_WORD (value
, *location
, align
);
165 COPY_UNALIGNED_WORD (*location
, value
, align
);
166 relocation
= relocation
& 0xffffff;
167 /* RELA must has 0 at relocation field. */
169 COPY_UNALIGNED_WORD (value
, *location
, align
);
171 case R_M32R_18_PCREL_RELA
:
172 relocation
= (relocation
- (Elf32_Addr
) location
);
173 if (relocation
< -0x20000 || 0x1fffc < relocation
)
175 printk(KERN_ERR
"module %s: relocation overflow: %u\n",
176 me
->name
, relocation
);
179 COPY_UNALIGNED_WORD (*location
, value
, align
);
182 /* RELA must has 0 at relocation field. */
183 printk(KERN_ERR
"module %s: illegal relocation field: %u\n",
187 relocation
= (relocation
>> 2) & 0xffff;
189 COPY_UNALIGNED_WORD (value
, *location
, align
);
191 case R_M32R_10_PCREL_RELA
:
192 hlocation
= (unsigned short *)location
;
193 relocation
= (relocation
- (Elf32_Addr
) location
);
194 COPY_UNALIGNED_HWORD (*hlocation
, hvalue
, align
);
195 svalue
= (int)hvalue
;
196 svalue
= (signed char)svalue
<< 2;
197 relocation
+= svalue
;
198 relocation
= (relocation
>> 2) & 0xff;
199 hvalue
= hvalue
& 0xff00;
200 hvalue
+= relocation
;
201 COPY_UNALIGNED_HWORD (hvalue
, *hlocation
, align
);
203 case R_M32R_26_PCREL_RELA
:
204 relocation
= (relocation
- (Elf32_Addr
) location
);
205 if (relocation
< -0x2000000 || 0x1fffffc < relocation
)
207 printk(KERN_ERR
"module %s: relocation overflow: %u\n",
208 me
->name
, relocation
);
211 COPY_UNALIGNED_WORD (*location
, value
, align
);
212 if (value
& 0xffffff)
214 /* RELA must has 0 at relocation field. */
215 printk(KERN_ERR
"module %s: illegal relocation field: %u\n",
219 relocation
= (relocation
>> 2) & 0xffffff;
221 COPY_UNALIGNED_WORD (value
, *location
, align
);
224 printk(KERN_ERR
"module %s: Unknown relocation: %u\n",
225 me
->name
, ELF32_R_TYPE(rel
[i
].r_info
));
232 int apply_relocate(Elf32_Shdr
*sechdrs
,
234 unsigned int symindex
,
239 printk(KERN_ERR
"module %s: REL RELOCATION unsupported\n",
247 int module_finalize(const Elf_Ehdr
*hdr
,
248 const Elf_Shdr
*sechdrs
,
254 void module_arch_cleanup(struct module
*mod
)