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