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>
25 #define DEBUGP(fmt...)
27 void *module_alloc(unsigned long size
)
32 return vmalloc_exec(size
);
39 /* Free memory returned from module_alloc */
40 void module_free(struct module
*mod
, void *module_region
)
45 /* We don't need anything special. */
46 int module_frob_arch_sections(Elf_Ehdr
*hdr
,
54 #define COPY_UNALIGNED_WORD(sw, tw, align) \
56 void *__s = &(sw), *__t = &(tw); \
57 unsigned short *__s2 = __s, *__t2 =__t; \
58 unsigned char *__s1 = __s, *__t1 =__t; \
62 *(unsigned long *) __t = *(unsigned long *) __s; \
77 #define COPY_UNALIGNED_HWORD(sw, tw, align) \
79 void *__s = &(sw), *__t = &(tw); \
80 unsigned short *__s2 = __s, *__t2 =__t; \
81 unsigned char *__s1 = __s, *__t1 =__t; \
94 int apply_relocate_add(Elf32_Shdr
*sechdrs
,
96 unsigned int symindex
,
101 Elf32_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
103 Elf32_Addr relocation
;
106 unsigned short *hlocation
;
107 unsigned short hvalue
;
111 DEBUGP("Applying relocate section %u to %u\n", relsec
,
112 sechdrs
[relsec
].sh_info
);
113 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
114 /* This is where to make the change */
115 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
117 /* This is the symbol it is referring to. Note that all
118 undefined symbols have been resolved. */
119 sym
= (Elf32_Sym
*)sechdrs
[symindex
].sh_addr
120 + ELF32_R_SYM(rel
[i
].r_info
);
121 relocation
= sym
->st_value
+ rel
[i
].r_addend
;
122 align
= (int)location
& 3;
124 switch (ELF32_R_TYPE(rel
[i
].r_info
)) {
126 COPY_UNALIGNED_WORD (*location
, value
, align
);
128 COPY_UNALIGNED_WORD (value
, *location
, align
);
130 case R_M32R_HI16_ULO_RELA
:
131 COPY_UNALIGNED_WORD (*location
, value
, align
);
132 relocation
= (relocation
>>16) & 0xffff;
133 /* RELA must has 0 at relocation field. */
135 COPY_UNALIGNED_WORD (value
, *location
, align
);
137 case R_M32R_HI16_SLO_RELA
:
138 COPY_UNALIGNED_WORD (*location
, value
, align
);
139 if (relocation
& 0x8000) relocation
+= 0x10000;
140 relocation
= (relocation
>>16) & 0xffff;
141 /* RELA must has 0 at relocation field. */
143 COPY_UNALIGNED_WORD (value
, *location
, align
);
146 hlocation
= (unsigned short *)location
;
147 relocation
= relocation
& 0xffff;
148 /* RELA must has 0 at relocation field. */
150 COPY_UNALIGNED_WORD (hvalue
, *hlocation
, align
);
152 case R_M32R_SDA16_RELA
:
153 case R_M32R_LO16_RELA
:
154 COPY_UNALIGNED_WORD (*location
, value
, align
);
155 relocation
= relocation
& 0xffff;
156 /* RELA must has 0 at relocation field. */
158 COPY_UNALIGNED_WORD (value
, *location
, align
);
161 COPY_UNALIGNED_WORD (*location
, value
, align
);
162 relocation
= relocation
& 0xffffff;
163 /* RELA must has 0 at relocation field. */
165 COPY_UNALIGNED_WORD (value
, *location
, align
);
167 case R_M32R_18_PCREL_RELA
:
168 relocation
= (relocation
- (Elf32_Addr
) location
);
169 if (relocation
< -0x20000 || 0x1fffc < relocation
)
171 printk(KERN_ERR
"module %s: relocation overflow: %u\n",
172 me
->name
, relocation
);
175 COPY_UNALIGNED_WORD (*location
, value
, align
);
178 /* RELA must has 0 at relocation field. */
179 printk(KERN_ERR
"module %s: illegal relocation field: %u\n",
183 relocation
= (relocation
>> 2) & 0xffff;
185 COPY_UNALIGNED_WORD (value
, *location
, align
);
187 case R_M32R_10_PCREL_RELA
:
188 hlocation
= (unsigned short *)location
;
189 relocation
= (relocation
- (Elf32_Addr
) location
);
190 COPY_UNALIGNED_HWORD (*hlocation
, hvalue
, align
);
191 svalue
= (int)hvalue
;
192 svalue
= (signed char)svalue
<< 2;
193 relocation
+= svalue
;
194 relocation
= (relocation
>> 2) & 0xff;
195 hvalue
= hvalue
& 0xff00;
196 hvalue
+= relocation
;
197 COPY_UNALIGNED_HWORD (hvalue
, *hlocation
, align
);
199 case R_M32R_26_PCREL_RELA
:
200 relocation
= (relocation
- (Elf32_Addr
) location
);
201 if (relocation
< -0x2000000 || 0x1fffffc < relocation
)
203 printk(KERN_ERR
"module %s: relocation overflow: %u\n",
204 me
->name
, relocation
);
207 COPY_UNALIGNED_WORD (*location
, value
, align
);
208 if (value
& 0xffffff)
210 /* RELA must has 0 at relocation field. */
211 printk(KERN_ERR
"module %s: illegal relocation field: %u\n",
215 relocation
= (relocation
>> 2) & 0xffffff;
217 COPY_UNALIGNED_WORD (value
, *location
, align
);
220 printk(KERN_ERR
"module %s: Unknown relocation: %u\n",
221 me
->name
, ELF32_R_TYPE(rel
[i
].r_info
));
228 int apply_relocate(Elf32_Shdr
*sechdrs
,
230 unsigned int symindex
,
238 int module_finalize(const Elf_Ehdr
*hdr
,
239 const Elf_Shdr
*sechdrs
,
245 void module_arch_cleanup(struct module
*mod
)