can: Do not call dev_put if restart timer is running upon close
[linux/fpc-iii.git] / arch / s390 / kernel / module.c
blobf7167ee4604cf7033e30eb3845aa8f9ba9fd538b
1 /*
2 * arch/s390/kernel/module.c - Kernel module help for s390.
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)
10 * based on i386 version
11 * Copyright (C) 2001 Rusty Russell.
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.
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.
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
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 #include <linux/moduleloader.h>
34 #include <linux/bug.h>
36 #if 0
37 #define DEBUGP printk
38 #else
39 #define DEBUGP(fmt , ...)
40 #endif
42 #ifndef CONFIG_64BIT
43 #define PLT_ENTRY_SIZE 12
44 #else /* CONFIG_64BIT */
45 #define PLT_ENTRY_SIZE 20
46 #endif /* CONFIG_64BIT */
48 void *module_alloc(unsigned long size)
50 if (size == 0)
51 return NULL;
52 return vmalloc(size);
55 /* Free memory returned from module_alloc */
56 void module_free(struct module *mod, void *module_region)
58 if (mod) {
59 vfree(mod->arch.syminfo);
60 mod->arch.syminfo = NULL;
62 vfree(module_region);
65 static void
66 check_rela(Elf_Rela *rela, struct module *me)
68 struct mod_arch_syminfo *info;
70 info = me->arch.syminfo + ELF_R_SYM (rela->r_info);
71 switch (ELF_R_TYPE (rela->r_info)) {
72 case R_390_GOT12: /* 12 bit GOT offset. */
73 case R_390_GOT16: /* 16 bit GOT offset. */
74 case R_390_GOT20: /* 20 bit GOT offset. */
75 case R_390_GOT32: /* 32 bit GOT offset. */
76 case R_390_GOT64: /* 64 bit GOT offset. */
77 case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
78 case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
79 case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
80 case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
81 case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
82 case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
83 case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
84 if (info->got_offset == -1UL) {
85 info->got_offset = me->arch.got_size;
86 me->arch.got_size += sizeof(void*);
88 break;
89 case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
90 case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
91 case R_390_PLT32: /* 32 bit PC relative PLT address. */
92 case R_390_PLT64: /* 64 bit PC relative PLT address. */
93 case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
94 case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
95 case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
96 if (info->plt_offset == -1UL) {
97 info->plt_offset = me->arch.plt_size;
98 me->arch.plt_size += PLT_ENTRY_SIZE;
100 break;
101 case R_390_COPY:
102 case R_390_GLOB_DAT:
103 case R_390_JMP_SLOT:
104 case R_390_RELATIVE:
105 /* Only needed if we want to support loading of
106 modules linked with -shared. */
107 break;
112 * Account for GOT and PLT relocations. We can't add sections for
113 * got and plt but we can increase the core module size.
116 module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
117 char *secstrings, struct module *me)
119 Elf_Shdr *symtab;
120 Elf_Sym *symbols;
121 Elf_Rela *rela;
122 char *strings;
123 int nrela, i, j;
125 /* Find symbol table and string table. */
126 symtab = NULL;
127 for (i = 0; i < hdr->e_shnum; i++)
128 switch (sechdrs[i].sh_type) {
129 case SHT_SYMTAB:
130 symtab = sechdrs + i;
131 break;
133 if (!symtab) {
134 printk(KERN_ERR "module %s: no symbol table\n", me->name);
135 return -ENOEXEC;
138 /* Allocate one syminfo structure per symbol. */
139 me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
140 me->arch.syminfo = vmalloc(me->arch.nsyms *
141 sizeof(struct mod_arch_syminfo));
142 if (!me->arch.syminfo)
143 return -ENOMEM;
144 symbols = (void *) hdr + symtab->sh_offset;
145 strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset;
146 for (i = 0; i < me->arch.nsyms; i++) {
147 if (symbols[i].st_shndx == SHN_UNDEF &&
148 strcmp(strings + symbols[i].st_name,
149 "_GLOBAL_OFFSET_TABLE_") == 0)
150 /* "Define" it as absolute. */
151 symbols[i].st_shndx = SHN_ABS;
152 me->arch.syminfo[i].got_offset = -1UL;
153 me->arch.syminfo[i].plt_offset = -1UL;
154 me->arch.syminfo[i].got_initialized = 0;
155 me->arch.syminfo[i].plt_initialized = 0;
158 /* Search for got/plt relocations. */
159 me->arch.got_size = me->arch.plt_size = 0;
160 for (i = 0; i < hdr->e_shnum; i++) {
161 if (sechdrs[i].sh_type != SHT_RELA)
162 continue;
163 nrela = sechdrs[i].sh_size / sizeof(Elf_Rela);
164 rela = (void *) hdr + sechdrs[i].sh_offset;
165 for (j = 0; j < nrela; j++)
166 check_rela(rela + j, me);
169 /* Increase core size by size of got & plt and set start
170 offsets for got and plt. */
171 me->core_size = ALIGN(me->core_size, 4);
172 me->arch.got_offset = me->core_size;
173 me->core_size += me->arch.got_size;
174 me->arch.plt_offset = me->core_size;
175 me->core_size += me->arch.plt_size;
176 return 0;
180 apply_relocate(Elf_Shdr *sechdrs, const char *strtab, unsigned int symindex,
181 unsigned int relsec, struct module *me)
183 printk(KERN_ERR "module %s: RELOCATION unsupported\n",
184 me->name);
185 return -ENOEXEC;
188 static int
189 apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
190 struct module *me)
192 struct mod_arch_syminfo *info;
193 Elf_Addr loc, val;
194 int r_type, r_sym;
196 /* This is where to make the change */
197 loc = base + rela->r_offset;
198 /* This is the symbol it is referring to. Note that all
199 undefined symbols have been resolved. */
200 r_sym = ELF_R_SYM(rela->r_info);
201 r_type = ELF_R_TYPE(rela->r_info);
202 info = me->arch.syminfo + r_sym;
203 val = symtab[r_sym].st_value;
205 switch (r_type) {
206 case R_390_8: /* Direct 8 bit. */
207 case R_390_12: /* Direct 12 bit. */
208 case R_390_16: /* Direct 16 bit. */
209 case R_390_20: /* Direct 20 bit. */
210 case R_390_32: /* Direct 32 bit. */
211 case R_390_64: /* Direct 64 bit. */
212 val += rela->r_addend;
213 if (r_type == R_390_8)
214 *(unsigned char *) loc = val;
215 else if (r_type == R_390_12)
216 *(unsigned short *) loc = (val & 0xfff) |
217 (*(unsigned short *) loc & 0xf000);
218 else if (r_type == R_390_16)
219 *(unsigned short *) loc = val;
220 else if (r_type == R_390_20)
221 *(unsigned int *) loc =
222 (*(unsigned int *) loc & 0xf00000ff) |
223 (val & 0xfff) << 16 | (val & 0xff000) >> 4;
224 else if (r_type == R_390_32)
225 *(unsigned int *) loc = val;
226 else if (r_type == R_390_64)
227 *(unsigned long *) loc = val;
228 break;
229 case R_390_PC16: /* PC relative 16 bit. */
230 case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */
231 case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */
232 case R_390_PC32: /* PC relative 32 bit. */
233 case R_390_PC64: /* PC relative 64 bit. */
234 val += rela->r_addend - loc;
235 if (r_type == R_390_PC16)
236 *(unsigned short *) loc = val;
237 else if (r_type == R_390_PC16DBL)
238 *(unsigned short *) loc = val >> 1;
239 else if (r_type == R_390_PC32DBL)
240 *(unsigned int *) loc = val >> 1;
241 else if (r_type == R_390_PC32)
242 *(unsigned int *) loc = val;
243 else if (r_type == R_390_PC64)
244 *(unsigned long *) loc = val;
245 break;
246 case R_390_GOT12: /* 12 bit GOT offset. */
247 case R_390_GOT16: /* 16 bit GOT offset. */
248 case R_390_GOT20: /* 20 bit GOT offset. */
249 case R_390_GOT32: /* 32 bit GOT offset. */
250 case R_390_GOT64: /* 64 bit GOT offset. */
251 case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
252 case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
253 case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
254 case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
255 case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
256 case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
257 case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
258 if (info->got_initialized == 0) {
259 Elf_Addr *gotent;
261 gotent = me->module_core + me->arch.got_offset +
262 info->got_offset;
263 *gotent = val;
264 info->got_initialized = 1;
266 val = info->got_offset + rela->r_addend;
267 if (r_type == R_390_GOT12 ||
268 r_type == R_390_GOTPLT12)
269 *(unsigned short *) loc = (val & 0xfff) |
270 (*(unsigned short *) loc & 0xf000);
271 else if (r_type == R_390_GOT16 ||
272 r_type == R_390_GOTPLT16)
273 *(unsigned short *) loc = val;
274 else if (r_type == R_390_GOT20 ||
275 r_type == R_390_GOTPLT20)
276 *(unsigned int *) loc =
277 (*(unsigned int *) loc & 0xf00000ff) |
278 (val & 0xfff) << 16 | (val & 0xff000) >> 4;
279 else if (r_type == R_390_GOT32 ||
280 r_type == R_390_GOTPLT32)
281 *(unsigned int *) loc = val;
282 else if (r_type == R_390_GOTENT ||
283 r_type == R_390_GOTPLTENT)
284 *(unsigned int *) loc =
285 (val + (Elf_Addr) me->module_core - loc) >> 1;
286 else if (r_type == R_390_GOT64 ||
287 r_type == R_390_GOTPLT64)
288 *(unsigned long *) loc = val;
289 break;
290 case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
291 case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
292 case R_390_PLT32: /* 32 bit PC relative PLT address. */
293 case R_390_PLT64: /* 64 bit PC relative PLT address. */
294 case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
295 case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
296 case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
297 if (info->plt_initialized == 0) {
298 unsigned int *ip;
299 ip = me->module_core + me->arch.plt_offset +
300 info->plt_offset;
301 #ifndef CONFIG_64BIT
302 ip[0] = 0x0d105810; /* basr 1,0; l 1,6(1); br 1 */
303 ip[1] = 0x100607f1;
304 ip[2] = val;
305 #else /* CONFIG_64BIT */
306 ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
307 ip[1] = 0x100a0004;
308 ip[2] = 0x07f10000;
309 ip[3] = (unsigned int) (val >> 32);
310 ip[4] = (unsigned int) val;
311 #endif /* CONFIG_64BIT */
312 info->plt_initialized = 1;
314 if (r_type == R_390_PLTOFF16 ||
315 r_type == R_390_PLTOFF32 ||
316 r_type == R_390_PLTOFF64)
317 val = me->arch.plt_offset - me->arch.got_offset +
318 info->plt_offset + rela->r_addend;
319 else {
320 if (!((r_type == R_390_PLT16DBL &&
321 val - loc + 0xffffUL < 0x1ffffeUL) ||
322 (r_type == R_390_PLT32DBL &&
323 val - loc + 0xffffffffULL < 0x1fffffffeULL)))
324 val = (Elf_Addr) me->module_core +
325 me->arch.plt_offset +
326 info->plt_offset;
327 val += rela->r_addend - loc;
329 if (r_type == R_390_PLT16DBL)
330 *(unsigned short *) loc = val >> 1;
331 else if (r_type == R_390_PLTOFF16)
332 *(unsigned short *) loc = val;
333 else if (r_type == R_390_PLT32DBL)
334 *(unsigned int *) loc = val >> 1;
335 else if (r_type == R_390_PLT32 ||
336 r_type == R_390_PLTOFF32)
337 *(unsigned int *) loc = val;
338 else if (r_type == R_390_PLT64 ||
339 r_type == R_390_PLTOFF64)
340 *(unsigned long *) loc = val;
341 break;
342 case R_390_GOTOFF16: /* 16 bit offset to GOT. */
343 case R_390_GOTOFF32: /* 32 bit offset to GOT. */
344 case R_390_GOTOFF64: /* 64 bit offset to GOT. */
345 val = val + rela->r_addend -
346 ((Elf_Addr) me->module_core + me->arch.got_offset);
347 if (r_type == R_390_GOTOFF16)
348 *(unsigned short *) loc = val;
349 else if (r_type == R_390_GOTOFF32)
350 *(unsigned int *) loc = val;
351 else if (r_type == R_390_GOTOFF64)
352 *(unsigned long *) loc = val;
353 break;
354 case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */
355 case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */
356 val = (Elf_Addr) me->module_core + me->arch.got_offset +
357 rela->r_addend - loc;
358 if (r_type == R_390_GOTPC)
359 *(unsigned int *) loc = val;
360 else if (r_type == R_390_GOTPCDBL)
361 *(unsigned int *) loc = val >> 1;
362 break;
363 case R_390_COPY:
364 case R_390_GLOB_DAT: /* Create GOT entry. */
365 case R_390_JMP_SLOT: /* Create PLT entry. */
366 case R_390_RELATIVE: /* Adjust by program base. */
367 /* Only needed if we want to support loading of
368 modules linked with -shared. */
369 break;
370 default:
371 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
372 me->name, r_type);
373 return -ENOEXEC;
375 return 0;
379 apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
380 unsigned int symindex, unsigned int relsec,
381 struct module *me)
383 Elf_Addr base;
384 Elf_Sym *symtab;
385 Elf_Rela *rela;
386 unsigned long i, n;
387 int rc;
389 DEBUGP("Applying relocate section %u to %u\n",
390 relsec, sechdrs[relsec].sh_info);
391 base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
392 symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
393 rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
394 n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
396 for (i = 0; i < n; i++, rela++) {
397 rc = apply_rela(rela, base, symtab, me);
398 if (rc)
399 return rc;
401 return 0;
404 int module_finalize(const Elf_Ehdr *hdr,
405 const Elf_Shdr *sechdrs,
406 struct module *me)
408 vfree(me->arch.syminfo);
409 me->arch.syminfo = NULL;
410 return 0;
413 void module_arch_cleanup(struct module *mod)