This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / m32r / kernel / module.c
1 /*  Kernel module help for M32R.
2
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.
7
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.
12
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
16 */
17 #include <linux/moduleloader.h>
18 #include <linux/elf.h>
19 #include <linux/vmalloc.h>
20 #include <linux/fs.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
23
24 #if 0
25 #define DEBUGP printk
26 #else
27 #define DEBUGP(fmt...)
28 #endif
29
30 void *module_alloc(unsigned long size)
31 {
32         if (size == 0)
33                 return NULL;
34         return vmalloc_exec(size);
35 }
36
37
38 /* Free memory returned from module_alloc */
39 void module_free(struct module *mod, void *module_region)
40 {
41         vfree(module_region);
42         /* FIXME: If module_region == mod->init_region, trim exception
43            table entries. */
44 }
45
46 /* We don't need anything special. */
47 int module_frob_arch_sections(Elf_Ehdr *hdr,
48                               Elf_Shdr *sechdrs,
49                               char *secstrings,
50                               struct module *mod)
51 {
52         return 0;
53 }
54
55 #define COPY_UNALIGNED_WORD(sw, tw, align) \
56 { \
57         void *__s = &(sw), *__t = &(tw); \
58         unsigned short *__s2 = __s, *__t2 =__t; \
59         unsigned char *__s1 = __s, *__t1 =__t; \
60         switch ((align)) \
61         { \
62         case 0: \
63                 *(unsigned long *) __t = *(unsigned long *) __s; \
64                 break; \
65         case 2: \
66                 *__t2++ = *__s2++; \
67                 *__t2 = *__s2; \
68                 break; \
69         default: \
70                 *__t1++ = *__s1++; \
71                 *__t1++ = *__s1++; \
72                 *__t1++ = *__s1++; \
73                 *__t1 = *__s1; \
74                 break; \
75         } \
76 }
77
78 #define COPY_UNALIGNED_HWORD(sw, tw, align) \
79   { \
80     void *__s = &(sw), *__t = &(tw); \
81     unsigned short *__s2 = __s, *__t2 =__t; \
82     unsigned char *__s1 = __s, *__t1 =__t; \
83     switch ((align)) \
84     { \
85     case 0: \
86       *__t2 = *__s2; \
87       break; \
88     default: \
89       *__t1++ = *__s1++; \
90       *__t1 = *__s1; \
91       break; \
92     } \
93   }
94
95 int apply_relocate_add(Elf32_Shdr *sechdrs,
96                    const char *strtab,
97                    unsigned int symindex,
98                    unsigned int relsec,
99                    struct module *me)
100 {
101         unsigned int i;
102         Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
103         Elf32_Sym *sym;
104         Elf32_Addr relocation;
105         uint32_t *location;
106         uint32_t value;
107         unsigned short *hlocation;
108         unsigned short hvalue;
109         int svalue;
110         int align;
111
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
117                         + rel[i].r_offset;
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;
124
125                 switch (ELF32_R_TYPE(rel[i].r_info)) {
126                 case R_M32R_32_RELA:
127                         COPY_UNALIGNED_WORD (*location, value, align);
128                         value += relocation;
129                         COPY_UNALIGNED_WORD (value, *location, align);
130                         break;
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. */
135                         value += relocation;
136                         COPY_UNALIGNED_WORD (value, *location, align);
137                         break;
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. */
143                         value += relocation;
144                         COPY_UNALIGNED_WORD (value, *location, align);
145                         break;
146                 case R_M32R_16_RELA:
147                         hlocation = (unsigned short *)location;
148                         relocation = relocation & 0xffff;
149                         /* RELA must has 0 at relocation field. */
150                         hvalue = relocation;
151                         COPY_UNALIGNED_WORD (hvalue, *hlocation, align);
152                         break;
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. */
158                         value += relocation;
159                         COPY_UNALIGNED_WORD (value, *location, align);
160                         break;
161                 case R_M32R_24_RELA:
162                         COPY_UNALIGNED_WORD (*location, value, align);
163                         relocation = relocation & 0xffffff;
164                         /* RELA must has 0 at relocation field. */
165                         value += relocation;
166                         COPY_UNALIGNED_WORD (value, *location, align);
167                         break;
168                 case R_M32R_18_PCREL_RELA:
169                         relocation = (relocation - (Elf32_Addr) location);
170                         if (relocation < -0x20000 || 0x1fffc < relocation)
171                                 {
172                                         printk(KERN_ERR "module %s: relocation overflow: %u\n",
173                                         me->name, relocation);
174                                         return -ENOEXEC;
175                                 }
176                         COPY_UNALIGNED_WORD (*location, value, align);
177                         if (value & 0xffff)
178                                 {
179                                         /* RELA must has 0 at relocation field. */
180                                         printk(KERN_ERR "module %s: illegal relocation field: %u\n",
181                                         me->name, value);
182                                         return -ENOEXEC;
183                                 }
184                         relocation = (relocation >> 2) & 0xffff;
185                         value += relocation;
186                         COPY_UNALIGNED_WORD (value, *location, align);
187                         break;
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);
199                         break;
200                 case R_M32R_26_PCREL_RELA:
201                         relocation = (relocation - (Elf32_Addr) location);
202                         if (relocation < -0x2000000 || 0x1fffffc < relocation)
203                                 {
204                                         printk(KERN_ERR "module %s: relocation overflow: %u\n",
205                                         me->name, relocation);
206                                         return -ENOEXEC;
207                                 }
208                         COPY_UNALIGNED_WORD (*location, value, align);
209                         if (value & 0xffffff)
210                                 {
211                                         /* RELA must has 0 at relocation field. */
212                                         printk(KERN_ERR "module %s: illegal relocation field: %u\n",
213                                         me->name, value);
214                                         return -ENOEXEC;
215                                 }
216                         relocation = (relocation >> 2) & 0xffffff;
217                         value += relocation;
218                         COPY_UNALIGNED_WORD (value, *location, align);
219                         break;
220                 default:
221                         printk(KERN_ERR "module %s: Unknown relocation: %u\n",
222                                me->name, ELF32_R_TYPE(rel[i].r_info));
223                         return -ENOEXEC;
224                 }
225         }
226         return 0;
227 }
228
229 int apply_relocate(Elf32_Shdr *sechdrs,
230                        const char *strtab,
231                        unsigned int symindex,
232                        unsigned int relsec,
233                        struct module *me)
234 {
235 #if 0
236         printk(KERN_ERR "module %s: REL RELOCATION unsupported\n",
237                me->name);
238         return -ENOEXEC;
239 #endif
240         return 0;
241
242 }
243
244 int module_finalize(const Elf_Ehdr *hdr,
245                     const Elf_Shdr *sechdrs,
246                     struct module *me)
247 {
248         return 0;
249 }
250
251 void module_arch_cleanup(struct module *mod)
252 {
253 }