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)
47 /* FIXME: If module_region == mod->init_region, trim exception
51 /* We don't need anything special. */
52 int module_frob_arch_sections(Elf_Ehdr *hdr,
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 #define COPY_UNALIGNED_HWORD(sw, tw, align) \
85 void *__s = &(sw), *__t = &(tw); \
86 unsigned short *__s2 = __s, *__t2 =__t; \
87 unsigned char *__s1 = __s, *__t1 =__t; \
100 int apply_relocate_add(Elf32_Shdr *sechdrs,
102 unsigned int symindex,
107 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
109 Elf32_Addr relocation;
112 unsigned short *hlocation;
113 unsigned short hvalue;
117 DEBUGP("Applying relocate section %u to %u\n", relsec,
118 sechdrs[relsec].sh_info);
119 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
120 /* This is where to make the change */
121 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
123 /* This is the symbol it is referring to. Note that all
124 undefined symbols have been resolved. */
125 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
126 + ELF32_R_SYM(rel[i].r_info);
127 relocation = sym->st_value + rel[i].r_addend;
128 align = (int)location & 3;
130 switch (ELF32_R_TYPE(rel[i].r_info)) {
132 COPY_UNALIGNED_WORD (*location, value, align);
134 COPY_UNALIGNED_WORD (value, *location, align);
136 case R_M32R_HI16_ULO_RELA:
137 COPY_UNALIGNED_WORD (*location, value, align);
138 relocation = (relocation >>16) & 0xffff;
139 /* RELA must has 0 at relocation field. */
141 COPY_UNALIGNED_WORD (value, *location, align);
143 case R_M32R_HI16_SLO_RELA:
144 COPY_UNALIGNED_WORD (*location, value, align);
145 if (relocation & 0x8000) relocation += 0x10000;
146 relocation = (relocation >>16) & 0xffff;
147 /* RELA must has 0 at relocation field. */
149 COPY_UNALIGNED_WORD (value, *location, align);
152 hlocation = (unsigned short *)location;
153 relocation = relocation & 0xffff;
154 /* RELA must has 0 at relocation field. */
156 COPY_UNALIGNED_WORD (hvalue, *hlocation, align);
158 case R_M32R_SDA16_RELA:
159 case R_M32R_LO16_RELA:
160 COPY_UNALIGNED_WORD (*location, value, align);
161 relocation = relocation & 0xffff;
162 /* RELA must has 0 at relocation field. */
164 COPY_UNALIGNED_WORD (value, *location, align);
167 COPY_UNALIGNED_WORD (*location, value, align);
168 relocation = relocation & 0xffffff;
169 /* RELA must has 0 at relocation field. */
171 COPY_UNALIGNED_WORD (value, *location, align);
173 case R_M32R_18_PCREL_RELA:
174 relocation = (relocation - (Elf32_Addr) location);
175 if (relocation < -0x20000 || 0x1fffc < relocation)
177 printk(KERN_ERR "module %s: relocation overflow: %u\n",
178 me->name, relocation);
181 COPY_UNALIGNED_WORD (*location, value, align);
184 /* RELA must has 0 at relocation field. */
185 printk(KERN_ERR "module %s: illegal relocation field: %u\n",
189 relocation = (relocation >> 2) & 0xffff;
191 COPY_UNALIGNED_WORD (value, *location, align);
193 case R_M32R_10_PCREL_RELA:
194 hlocation = (unsigned short *)location;
195 relocation = (relocation - (Elf32_Addr) location);
196 COPY_UNALIGNED_HWORD (*hlocation, hvalue, align);
197 svalue = (int)hvalue;
198 svalue = (signed char)svalue << 2;
199 relocation += svalue;
200 relocation = (relocation >> 2) & 0xff;
201 hvalue = hvalue & 0xff00;
202 hvalue += relocation;
203 COPY_UNALIGNED_HWORD (hvalue, *hlocation, align);
205 case R_M32R_26_PCREL_RELA:
206 relocation = (relocation - (Elf32_Addr) location);
207 if (relocation < -0x2000000 || 0x1fffffc < relocation)
209 printk(KERN_ERR "module %s: relocation overflow: %u\n",
210 me->name, relocation);
213 COPY_UNALIGNED_WORD (*location, value, align);
214 if (value & 0xffffff)
216 /* RELA must has 0 at relocation field. */
217 printk(KERN_ERR "module %s: illegal relocation field: %u\n",
221 relocation = (relocation >> 2) & 0xffffff;
223 COPY_UNALIGNED_WORD (value, *location, align);
226 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
227 me->name, ELF32_R_TYPE(rel[i].r_info));
234 int apply_relocate(Elf32_Shdr *sechdrs,
236 unsigned int symindex,
241 printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
249 int module_finalize(const Elf_Ehdr *hdr,
250 const Elf_Shdr *sechdrs,
256 void module_arch_cleanup(struct module *mod)