s390/comments: unify copyright messages and remove file names
[pandora-kernel.git] / arch / s390 / kernel / module.c
1 /*
2  *  Kernel module help for s390.
3  *
4  *  S390 version
5  *    Copyright IBM Corp. 2002, 2003
6  *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
7  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
8  *
9  *  based on i386 version
10  *    Copyright (C) 2001 Rusty Russell.
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26 #include <linux/module.h>
27 #include <linux/elf.h>
28 #include <linux/vmalloc.h>
29 #include <linux/fs.h>
30 #include <linux/string.h>
31 #include <linux/kernel.h>
32 #include <linux/moduleloader.h>
33 #include <linux/bug.h>
34
35 #if 0
36 #define DEBUGP printk
37 #else
38 #define DEBUGP(fmt , ...)
39 #endif
40
41 #ifndef CONFIG_64BIT
42 #define PLT_ENTRY_SIZE 12
43 #else /* CONFIG_64BIT */
44 #define PLT_ENTRY_SIZE 20
45 #endif /* CONFIG_64BIT */
46
47 /* Free memory returned from module_alloc */
48 void module_free(struct module *mod, void *module_region)
49 {
50         if (mod) {
51                 vfree(mod->arch.syminfo);
52                 mod->arch.syminfo = NULL;
53         }
54         vfree(module_region);
55 }
56
57 static void
58 check_rela(Elf_Rela *rela, struct module *me)
59 {
60         struct mod_arch_syminfo *info;
61
62         info = me->arch.syminfo + ELF_R_SYM (rela->r_info);
63         switch (ELF_R_TYPE (rela->r_info)) {
64         case R_390_GOT12:       /* 12 bit GOT offset.  */
65         case R_390_GOT16:       /* 16 bit GOT offset.  */
66         case R_390_GOT20:       /* 20 bit GOT offset.  */
67         case R_390_GOT32:       /* 32 bit GOT offset.  */
68         case R_390_GOT64:       /* 64 bit GOT offset.  */
69         case R_390_GOTENT:      /* 32 bit PC rel. to GOT entry shifted by 1. */
70         case R_390_GOTPLT12:    /* 12 bit offset to jump slot.  */
71         case R_390_GOTPLT16:    /* 16 bit offset to jump slot.  */
72         case R_390_GOTPLT20:    /* 20 bit offset to jump slot.  */
73         case R_390_GOTPLT32:    /* 32 bit offset to jump slot.  */
74         case R_390_GOTPLT64:    /* 64 bit offset to jump slot.  */
75         case R_390_GOTPLTENT:   /* 32 bit rel. offset to jump slot >> 1. */
76                 if (info->got_offset == -1UL) {
77                         info->got_offset = me->arch.got_size;
78                         me->arch.got_size += sizeof(void*);
79                 }
80                 break;
81         case R_390_PLT16DBL:    /* 16 bit PC rel. PLT shifted by 1.  */
82         case R_390_PLT32DBL:    /* 32 bit PC rel. PLT shifted by 1.  */
83         case R_390_PLT32:       /* 32 bit PC relative PLT address.  */
84         case R_390_PLT64:       /* 64 bit PC relative PLT address.  */
85         case R_390_PLTOFF16:    /* 16 bit offset from GOT to PLT. */
86         case R_390_PLTOFF32:    /* 32 bit offset from GOT to PLT. */
87         case R_390_PLTOFF64:    /* 16 bit offset from GOT to PLT. */
88                 if (info->plt_offset == -1UL) {
89                         info->plt_offset = me->arch.plt_size;
90                         me->arch.plt_size += PLT_ENTRY_SIZE;
91                 }
92                 break;
93         case R_390_COPY:
94         case R_390_GLOB_DAT:
95         case R_390_JMP_SLOT:
96         case R_390_RELATIVE:
97                 /* Only needed if we want to support loading of 
98                    modules linked with -shared. */
99                 break;
100         }
101 }
102
103 /*
104  * Account for GOT and PLT relocations. We can't add sections for
105  * got and plt but we can increase the core module size.
106  */
107 int
108 module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
109                           char *secstrings, struct module *me)
110 {
111         Elf_Shdr *symtab;
112         Elf_Sym *symbols;
113         Elf_Rela *rela;
114         char *strings;
115         int nrela, i, j;
116
117         /* Find symbol table and string table. */
118         symtab = NULL;
119         for (i = 0; i < hdr->e_shnum; i++)
120                 switch (sechdrs[i].sh_type) {
121                 case SHT_SYMTAB:
122                         symtab = sechdrs + i;
123                         break;
124                 }
125         if (!symtab) {
126                 printk(KERN_ERR "module %s: no symbol table\n", me->name);
127                 return -ENOEXEC;
128         }
129
130         /* Allocate one syminfo structure per symbol. */
131         me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
132         me->arch.syminfo = vmalloc(me->arch.nsyms *
133                                    sizeof(struct mod_arch_syminfo));
134         if (!me->arch.syminfo)
135                 return -ENOMEM;
136         symbols = (void *) hdr + symtab->sh_offset;
137         strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset;
138         for (i = 0; i < me->arch.nsyms; i++) {
139                 if (symbols[i].st_shndx == SHN_UNDEF &&
140                     strcmp(strings + symbols[i].st_name,
141                            "_GLOBAL_OFFSET_TABLE_") == 0)
142                         /* "Define" it as absolute. */
143                         symbols[i].st_shndx = SHN_ABS;
144                 me->arch.syminfo[i].got_offset = -1UL;
145                 me->arch.syminfo[i].plt_offset = -1UL;
146                 me->arch.syminfo[i].got_initialized = 0;
147                 me->arch.syminfo[i].plt_initialized = 0;
148         }
149
150         /* Search for got/plt relocations. */
151         me->arch.got_size = me->arch.plt_size = 0;
152         for (i = 0; i < hdr->e_shnum; i++) {
153                 if (sechdrs[i].sh_type != SHT_RELA)
154                         continue;
155                 nrela = sechdrs[i].sh_size / sizeof(Elf_Rela);
156                 rela = (void *) hdr + sechdrs[i].sh_offset;
157                 for (j = 0; j < nrela; j++)
158                         check_rela(rela + j, me);
159         }
160
161         /* Increase core size by size of got & plt and set start
162            offsets for got and plt. */
163         me->core_size = ALIGN(me->core_size, 4);
164         me->arch.got_offset = me->core_size;
165         me->core_size += me->arch.got_size;
166         me->arch.plt_offset = me->core_size;
167         me->core_size += me->arch.plt_size;
168         return 0;
169 }
170
171 static int
172 apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab, 
173            struct module *me)
174 {
175         struct mod_arch_syminfo *info;
176         Elf_Addr loc, val;
177         int r_type, r_sym;
178
179         /* This is where to make the change */
180         loc = base + rela->r_offset;
181         /* This is the symbol it is referring to.  Note that all
182            undefined symbols have been resolved.  */
183         r_sym = ELF_R_SYM(rela->r_info);
184         r_type = ELF_R_TYPE(rela->r_info);
185         info = me->arch.syminfo + r_sym;
186         val = symtab[r_sym].st_value;
187
188         switch (r_type) {
189         case R_390_8:           /* Direct 8 bit.   */
190         case R_390_12:          /* Direct 12 bit.  */
191         case R_390_16:          /* Direct 16 bit.  */
192         case R_390_20:          /* Direct 20 bit.  */
193         case R_390_32:          /* Direct 32 bit.  */
194         case R_390_64:          /* Direct 64 bit.  */
195                 val += rela->r_addend;
196                 if (r_type == R_390_8)
197                         *(unsigned char *) loc = val;
198                 else if (r_type == R_390_12)
199                         *(unsigned short *) loc = (val & 0xfff) |
200                                 (*(unsigned short *) loc & 0xf000);
201                 else if (r_type == R_390_16)
202                         *(unsigned short *) loc = val;
203                 else if (r_type == R_390_20)
204                         *(unsigned int *) loc =
205                                 (*(unsigned int *) loc & 0xf00000ff) |
206                                 (val & 0xfff) << 16 | (val & 0xff000) >> 4;
207                 else if (r_type == R_390_32)
208                         *(unsigned int *) loc = val;
209                 else if (r_type == R_390_64)
210                         *(unsigned long *) loc = val;
211                 break;
212         case R_390_PC16:        /* PC relative 16 bit.  */
213         case R_390_PC16DBL:     /* PC relative 16 bit shifted by 1.  */
214         case R_390_PC32DBL:     /* PC relative 32 bit shifted by 1.  */
215         case R_390_PC32:        /* PC relative 32 bit.  */
216         case R_390_PC64:        /* PC relative 64 bit.  */
217                 val += rela->r_addend - loc;
218                 if (r_type == R_390_PC16)
219                         *(unsigned short *) loc = val;
220                 else if (r_type == R_390_PC16DBL)
221                         *(unsigned short *) loc = val >> 1;
222                 else if (r_type == R_390_PC32DBL)
223                         *(unsigned int *) loc = val >> 1;
224                 else if (r_type == R_390_PC32)
225                         *(unsigned int *) loc = val;
226                 else if (r_type == R_390_PC64)
227                         *(unsigned long *) loc = val;
228                 break;
229         case R_390_GOT12:       /* 12 bit GOT offset.  */
230         case R_390_GOT16:       /* 16 bit GOT offset.  */
231         case R_390_GOT20:       /* 20 bit GOT offset.  */
232         case R_390_GOT32:       /* 32 bit GOT offset.  */
233         case R_390_GOT64:       /* 64 bit GOT offset.  */
234         case R_390_GOTENT:      /* 32 bit PC rel. to GOT entry shifted by 1. */
235         case R_390_GOTPLT12:    /* 12 bit offset to jump slot.  */
236         case R_390_GOTPLT20:    /* 20 bit offset to jump slot.  */
237         case R_390_GOTPLT16:    /* 16 bit offset to jump slot.  */
238         case R_390_GOTPLT32:    /* 32 bit offset to jump slot.  */
239         case R_390_GOTPLT64:    /* 64 bit offset to jump slot.  */
240         case R_390_GOTPLTENT:   /* 32 bit rel. offset to jump slot >> 1. */
241                 if (info->got_initialized == 0) {
242                         Elf_Addr *gotent;
243
244                         gotent = me->module_core + me->arch.got_offset +
245                                 info->got_offset;
246                         *gotent = val;
247                         info->got_initialized = 1;
248                 }
249                 val = info->got_offset + rela->r_addend;
250                 if (r_type == R_390_GOT12 ||
251                     r_type == R_390_GOTPLT12)
252                         *(unsigned short *) loc = (val & 0xfff) |
253                                 (*(unsigned short *) loc & 0xf000);
254                 else if (r_type == R_390_GOT16 ||
255                          r_type == R_390_GOTPLT16)
256                         *(unsigned short *) loc = val;
257                 else if (r_type == R_390_GOT20 ||
258                          r_type == R_390_GOTPLT20)
259                         *(unsigned int *) loc =
260                                 (*(unsigned int *) loc & 0xf00000ff) |
261                                 (val & 0xfff) << 16 | (val & 0xff000) >> 4;
262                 else if (r_type == R_390_GOT32 ||
263                          r_type == R_390_GOTPLT32)
264                         *(unsigned int *) loc = val;
265                 else if (r_type == R_390_GOTENT ||
266                          r_type == R_390_GOTPLTENT)
267                         *(unsigned int *) loc =
268                                 (val + (Elf_Addr) me->module_core - loc) >> 1;
269                 else if (r_type == R_390_GOT64 ||
270                          r_type == R_390_GOTPLT64)
271                         *(unsigned long *) loc = val;
272                 break;
273         case R_390_PLT16DBL:    /* 16 bit PC rel. PLT shifted by 1.  */
274         case R_390_PLT32DBL:    /* 32 bit PC rel. PLT shifted by 1.  */
275         case R_390_PLT32:       /* 32 bit PC relative PLT address.  */
276         case R_390_PLT64:       /* 64 bit PC relative PLT address.  */
277         case R_390_PLTOFF16:    /* 16 bit offset from GOT to PLT. */
278         case R_390_PLTOFF32:    /* 32 bit offset from GOT to PLT. */
279         case R_390_PLTOFF64:    /* 16 bit offset from GOT to PLT. */
280                 if (info->plt_initialized == 0) {
281                         unsigned int *ip;
282                         ip = me->module_core + me->arch.plt_offset +
283                                 info->plt_offset;
284 #ifndef CONFIG_64BIT
285                         ip[0] = 0x0d105810; /* basr 1,0; l 1,6(1); br 1 */
286                         ip[1] = 0x100607f1;
287                         ip[2] = val;
288 #else /* CONFIG_64BIT */
289                         ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
290                         ip[1] = 0x100a0004;
291                         ip[2] = 0x07f10000;
292                         ip[3] = (unsigned int) (val >> 32);
293                         ip[4] = (unsigned int) val;
294 #endif /* CONFIG_64BIT */
295                         info->plt_initialized = 1;
296                 }
297                 if (r_type == R_390_PLTOFF16 ||
298                     r_type == R_390_PLTOFF32 ||
299                     r_type == R_390_PLTOFF64)
300                         val = me->arch.plt_offset - me->arch.got_offset +
301                                 info->plt_offset + rela->r_addend;
302                 else {
303                         if (!((r_type == R_390_PLT16DBL &&
304                                val - loc + 0xffffUL < 0x1ffffeUL) ||
305                               (r_type == R_390_PLT32DBL &&
306                                val - loc + 0xffffffffULL < 0x1fffffffeULL)))
307                                 val = (Elf_Addr) me->module_core +
308                                         me->arch.plt_offset +
309                                         info->plt_offset;
310                         val += rela->r_addend - loc;
311                 }
312                 if (r_type == R_390_PLT16DBL)
313                         *(unsigned short *) loc = val >> 1;
314                 else if (r_type == R_390_PLTOFF16)
315                         *(unsigned short *) loc = val;
316                 else if (r_type == R_390_PLT32DBL)
317                         *(unsigned int *) loc = val >> 1;
318                 else if (r_type == R_390_PLT32 ||
319                          r_type == R_390_PLTOFF32)
320                         *(unsigned int *) loc = val;
321                 else if (r_type == R_390_PLT64 ||
322                          r_type == R_390_PLTOFF64)
323                         *(unsigned long *) loc = val;
324                 break;
325         case R_390_GOTOFF16:    /* 16 bit offset to GOT.  */
326         case R_390_GOTOFF32:    /* 32 bit offset to GOT.  */
327         case R_390_GOTOFF64:    /* 64 bit offset to GOT. */
328                 val = val + rela->r_addend -
329                         ((Elf_Addr) me->module_core + me->arch.got_offset);
330                 if (r_type == R_390_GOTOFF16)
331                         *(unsigned short *) loc = val;
332                 else if (r_type == R_390_GOTOFF32)
333                         *(unsigned int *) loc = val;
334                 else if (r_type == R_390_GOTOFF64)
335                         *(unsigned long *) loc = val;
336                 break;
337         case R_390_GOTPC:       /* 32 bit PC relative offset to GOT. */
338         case R_390_GOTPCDBL:    /* 32 bit PC rel. off. to GOT shifted by 1. */
339                 val = (Elf_Addr) me->module_core + me->arch.got_offset +
340                         rela->r_addend - loc;
341                 if (r_type == R_390_GOTPC)
342                         *(unsigned int *) loc = val;
343                 else if (r_type == R_390_GOTPCDBL)
344                         *(unsigned int *) loc = val >> 1;
345                 break;
346         case R_390_COPY:
347         case R_390_GLOB_DAT:    /* Create GOT entry.  */
348         case R_390_JMP_SLOT:    /* Create PLT entry.  */
349         case R_390_RELATIVE:    /* Adjust by program base.  */
350                 /* Only needed if we want to support loading of 
351                    modules linked with -shared. */
352                 break;
353         default:
354                 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
355                        me->name, r_type);
356                 return -ENOEXEC;
357         }
358         return 0;
359 }
360
361 int
362 apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
363                    unsigned int symindex, unsigned int relsec,
364                    struct module *me)
365 {
366         Elf_Addr base;
367         Elf_Sym *symtab;
368         Elf_Rela *rela;
369         unsigned long i, n;
370         int rc;
371
372         DEBUGP("Applying relocate section %u to %u\n",
373                relsec, sechdrs[relsec].sh_info);
374         base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
375         symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
376         rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
377         n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
378
379         for (i = 0; i < n; i++, rela++) {
380                 rc = apply_rela(rela, base, symtab, me);
381                 if (rc)
382                         return rc;
383         }
384         return 0;
385 }
386
387 int module_finalize(const Elf_Ehdr *hdr,
388                     const Elf_Shdr *sechdrs,
389                     struct module *me)
390 {
391         vfree(me->arch.syminfo);
392         me->arch.syminfo = NULL;
393         return 0;
394 }