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
17 #include <linux/moduleloader.h>
18 #include <linux/elf.h>
19 #include <linux/vmalloc.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
27 #define DEBUGP(fmt...)
30 void *module_alloc(unsigned long size)
34 return vmalloc_exec(size);
38 /* Free memory returned from module_alloc */
39 void module_free(struct module *mod, void *module_region)
42 /* FIXME: If module_region == mod->init_region, trim exception
46 /* We don't need anything special. */
47 int module_frob_arch_sections(Elf_Ehdr *hdr,
55 #define COPY_UNALIGNED_WORD(sw, tw, align) \
57 void *__s = &(sw), *__t = &(tw); \
58 unsigned short *__s2 = __s, *__t2 =__t; \
59 unsigned char *__s1 = __s, *__t1 =__t; \
63 *(unsigned long *) __t = *(unsigned long *) __s; \
78 #define COPY_UNALIGNED_HWORD(sw, tw, align) \
80 void *__s = &(sw), *__t = &(tw); \
81 unsigned short *__s2 = __s, *__t2 =__t; \
82 unsigned char *__s1 = __s, *__t1 =__t; \
95 int apply_relocate_add(Elf32_Shdr *sechdrs,
97 unsigned int symindex,
102 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
104 Elf32_Addr relocation;
107 unsigned short *hlocation;
108 unsigned short hvalue;
112 DEBUGP("Applying relocate section %u to %u\n", relsec,
113 sechdrs[relsec].sh_info);
114 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
115 /* This is where to make the change */
116 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
118 /* This is the symbol it is referring to. Note that all
119 undefined symbols have been resolved. */
120 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
121 + ELF32_R_SYM(rel[i].r_info);
122 relocation = sym->st_value + rel[i].r_addend;
123 align = (int)location & 3;
125 switch (ELF32_R_TYPE(rel[i].r_info)) {
127 COPY_UNALIGNED_WORD (*location, value, align);
129 COPY_UNALIGNED_WORD (value, *location, align);
131 case R_M32R_HI16_ULO_RELA:
132 COPY_UNALIGNED_WORD (*location, value, align);
133 relocation = (relocation >>16) & 0xffff;
134 /* RELA must has 0 at relocation field. */
136 COPY_UNALIGNED_WORD (value, *location, align);
138 case R_M32R_HI16_SLO_RELA:
139 COPY_UNALIGNED_WORD (*location, value, align);
140 if (relocation & 0x8000) relocation += 0x10000;
141 relocation = (relocation >>16) & 0xffff;
142 /* RELA must has 0 at relocation field. */
144 COPY_UNALIGNED_WORD (value, *location, align);
147 hlocation = (unsigned short *)location;
148 relocation = relocation & 0xffff;
149 /* RELA must has 0 at relocation field. */
151 COPY_UNALIGNED_WORD (hvalue, *hlocation, align);
153 case R_M32R_SDA16_RELA:
154 case R_M32R_LO16_RELA:
155 COPY_UNALIGNED_WORD (*location, value, align);
156 relocation = relocation & 0xffff;
157 /* RELA must has 0 at relocation field. */
159 COPY_UNALIGNED_WORD (value, *location, align);
162 COPY_UNALIGNED_WORD (*location, value, align);
163 relocation = relocation & 0xffffff;
164 /* RELA must has 0 at relocation field. */
166 COPY_UNALIGNED_WORD (value, *location, align);
168 case R_M32R_18_PCREL_RELA:
169 relocation = (relocation - (Elf32_Addr) location);
170 if (relocation < -0x20000 || 0x1fffc < relocation)
172 printk(KERN_ERR "module %s: relocation overflow: %u\n",
173 me->name, relocation);
176 COPY_UNALIGNED_WORD (*location, value, align);
179 /* RELA must has 0 at relocation field. */
180 printk(KERN_ERR "module %s: illegal relocation field: %u\n",
184 relocation = (relocation >> 2) & 0xffff;
186 COPY_UNALIGNED_WORD (value, *location, align);
188 case R_M32R_10_PCREL_RELA:
189 hlocation = (unsigned short *)location;
190 relocation = (relocation - (Elf32_Addr) location);
191 COPY_UNALIGNED_HWORD (*hlocation, hvalue, align);
192 svalue = (int)hvalue;
193 svalue = (signed char)svalue << 2;
194 relocation += svalue;
195 relocation = (relocation >> 2) & 0xff;
196 hvalue = hvalue & 0xff00;
197 hvalue += relocation;
198 COPY_UNALIGNED_HWORD (hvalue, *hlocation, align);
200 case R_M32R_26_PCREL_RELA:
201 relocation = (relocation - (Elf32_Addr) location);
202 if (relocation < -0x2000000 || 0x1fffffc < relocation)
204 printk(KERN_ERR "module %s: relocation overflow: %u\n",
205 me->name, relocation);
208 COPY_UNALIGNED_WORD (*location, value, align);
209 if (value & 0xffffff)
211 /* RELA must has 0 at relocation field. */
212 printk(KERN_ERR "module %s: illegal relocation field: %u\n",
216 relocation = (relocation >> 2) & 0xffffff;
218 COPY_UNALIGNED_WORD (value, *location, align);
221 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
222 me->name, ELF32_R_TYPE(rel[i].r_info));
229 int apply_relocate(Elf32_Shdr *sechdrs,
231 unsigned int symindex,
236 printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
244 int module_finalize(const Elf_Ehdr *hdr,
245 const Elf_Shdr *sechdrs,
251 void module_arch_cleanup(struct module *mod)