First Support on Ginger and OMAP TI
[linux-ginger.git] / arch / m32r / kernel / module.c
blobcb5f37d78d490889c0fc80e747d54d35c97c263a
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>
21 #include <linux/fs.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
25 #if 0
26 #define DEBUGP printk
27 #else
28 #define DEBUGP(fmt...)
29 #endif
31 void *module_alloc(unsigned long size)
33 if (size == 0)
34 return NULL;
35 #ifdef CONFIG_MMU
36 return vmalloc_exec(size);
37 #else
38 return vmalloc(size);
39 #endif
43 /* Free memory returned from module_alloc */
44 void module_free(struct module *mod, void *module_region)
46 vfree(module_region);
49 /* We don't need anything special. */
50 int module_frob_arch_sections(Elf_Ehdr *hdr,
51 Elf_Shdr *sechdrs,
52 char *secstrings,
53 struct module *mod)
55 return 0;
58 #define COPY_UNALIGNED_WORD(sw, tw, align) \
59 { \
60 void *__s = &(sw), *__t = &(tw); \
61 unsigned short *__s2 = __s, *__t2 =__t; \
62 unsigned char *__s1 = __s, *__t1 =__t; \
63 switch ((align)) \
64 { \
65 case 0: \
66 *(unsigned long *) __t = *(unsigned long *) __s; \
67 break; \
68 case 2: \
69 *__t2++ = *__s2++; \
70 *__t2 = *__s2; \
71 break; \
72 default: \
73 *__t1++ = *__s1++; \
74 *__t1++ = *__s1++; \
75 *__t1++ = *__s1++; \
76 *__t1 = *__s1; \
77 break; \
78 } \
81 #define COPY_UNALIGNED_HWORD(sw, tw, align) \
82 { \
83 void *__s = &(sw), *__t = &(tw); \
84 unsigned short *__s2 = __s, *__t2 =__t; \
85 unsigned char *__s1 = __s, *__t1 =__t; \
86 switch ((align)) \
87 { \
88 case 0: \
89 *__t2 = *__s2; \
90 break; \
91 default: \
92 *__t1++ = *__s1++; \
93 *__t1 = *__s1; \
94 break; \
95 } \
98 int apply_relocate_add(Elf32_Shdr *sechdrs,
99 const char *strtab,
100 unsigned int symindex,
101 unsigned int relsec,
102 struct module *me)
104 unsigned int i;
105 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
106 Elf32_Sym *sym;
107 Elf32_Addr relocation;
108 uint32_t *location;
109 uint32_t value;
110 unsigned short *hlocation;
111 unsigned short hvalue;
112 int svalue;
113 int align;
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
120 + rel[i].r_offset;
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)) {
129 case R_M32R_32_RELA:
130 COPY_UNALIGNED_WORD (*location, value, align);
131 value += relocation;
132 COPY_UNALIGNED_WORD (value, *location, align);
133 break;
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. */
138 value += relocation;
139 COPY_UNALIGNED_WORD (value, *location, align);
140 break;
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. */
146 value += relocation;
147 COPY_UNALIGNED_WORD (value, *location, align);
148 break;
149 case R_M32R_16_RELA:
150 hlocation = (unsigned short *)location;
151 relocation = relocation & 0xffff;
152 /* RELA must has 0 at relocation field. */
153 hvalue = relocation;
154 COPY_UNALIGNED_WORD (hvalue, *hlocation, align);
155 break;
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. */
161 value += relocation;
162 COPY_UNALIGNED_WORD (value, *location, align);
163 break;
164 case R_M32R_24_RELA:
165 COPY_UNALIGNED_WORD (*location, value, align);
166 relocation = relocation & 0xffffff;
167 /* RELA must has 0 at relocation field. */
168 value += relocation;
169 COPY_UNALIGNED_WORD (value, *location, align);
170 break;
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);
177 return -ENOEXEC;
179 COPY_UNALIGNED_WORD (*location, value, align);
180 if (value & 0xffff)
182 /* RELA must has 0 at relocation field. */
183 printk(KERN_ERR "module %s: illegal relocation field: %u\n",
184 me->name, value);
185 return -ENOEXEC;
187 relocation = (relocation >> 2) & 0xffff;
188 value += relocation;
189 COPY_UNALIGNED_WORD (value, *location, align);
190 break;
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);
202 break;
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);
209 return -ENOEXEC;
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",
216 me->name, value);
217 return -ENOEXEC;
219 relocation = (relocation >> 2) & 0xffffff;
220 value += relocation;
221 COPY_UNALIGNED_WORD (value, *location, align);
222 break;
223 default:
224 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
225 me->name, ELF32_R_TYPE(rel[i].r_info));
226 return -ENOEXEC;
229 return 0;
232 int apply_relocate(Elf32_Shdr *sechdrs,
233 const char *strtab,
234 unsigned int symindex,
235 unsigned int relsec,
236 struct module *me)
238 #if 0
239 printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
240 me->name);
241 return -ENOEXEC;
242 #endif
243 return 0;
247 int module_finalize(const Elf_Ehdr *hdr,
248 const Elf_Shdr *sechdrs,
249 struct module *me)
251 return 0;
254 void module_arch_cleanup(struct module *mod)