2 * arch/s390/kernel/module.c - Kernel module help for s390.
5 * Copyright (C) 2002, 2003 IBM Deutschland Entwicklung GmbH,
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>
31 #include <linux/string.h>
32 #include <linux/kernel.h>
37 #define DEBUGP(fmt , ...)
41 #define PLT_ENTRY_SIZE 12
42 #else /* CONFIG_64BIT */
43 #define PLT_ENTRY_SIZE 20
44 #endif /* CONFIG_64BIT */
46 void *module_alloc(unsigned long size)
53 /* Free memory returned from module_alloc */
54 void module_free(struct module *mod, void *module_region)
57 /* FIXME: If module_region == mod->init_region, trim exception
62 check_rela(Elf_Rela *rela, struct module *me)
64 struct mod_arch_syminfo *info;
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_GOT20: /* 20 bit GOT offset. */
71 case R_390_GOT32: /* 32 bit GOT offset. */
72 case R_390_GOT64: /* 64 bit GOT offset. */
73 case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
74 case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
75 case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
76 case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
77 case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
78 case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
79 case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
80 if (info->got_offset == -1UL) {
81 info->got_offset = me->arch.got_size;
82 me->arch.got_size += sizeof(void*);
85 case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
86 case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
87 case R_390_PLT32: /* 32 bit PC relative PLT address. */
88 case R_390_PLT64: /* 64 bit PC relative PLT address. */
89 case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
90 case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
91 case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
92 if (info->plt_offset == -1UL) {
93 info->plt_offset = me->arch.plt_size;
94 me->arch.plt_size += PLT_ENTRY_SIZE;
101 /* Only needed if we want to support loading of
102 modules linked with -shared. */
108 * Account for GOT and PLT relocations. We can't add sections for
109 * got and plt but we can increase the core module size.
112 module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
113 char *secstrings, struct module *me)
121 /* Find symbol table and string table. */
123 for (i = 0; i < hdr->e_shnum; i++)
124 switch (sechdrs[i].sh_type) {
126 symtab = sechdrs + i;
130 printk(KERN_ERR "module %s: no symbol table\n", me->name);
134 /* Allocate one syminfo structure per symbol. */
135 me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
136 me->arch.syminfo = vmalloc(me->arch.nsyms *
137 sizeof(struct mod_arch_syminfo));
138 if (!me->arch.syminfo)
140 symbols = (void *) hdr + symtab->sh_offset;
141 strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset;
142 for (i = 0; i < me->arch.nsyms; i++) {
143 if (symbols[i].st_shndx == SHN_UNDEF &&
144 strcmp(strings + symbols[i].st_name,
145 "_GLOBAL_OFFSET_TABLE_") == 0)
146 /* "Define" it as absolute. */
147 symbols[i].st_shndx = SHN_ABS;
148 me->arch.syminfo[i].got_offset = -1UL;
149 me->arch.syminfo[i].plt_offset = -1UL;
150 me->arch.syminfo[i].got_initialized = 0;
151 me->arch.syminfo[i].plt_initialized = 0;
154 /* Search for got/plt relocations. */
155 me->arch.got_size = me->arch.plt_size = 0;
156 for (i = 0; i < hdr->e_shnum; i++) {
157 if (sechdrs[i].sh_type != SHT_RELA)
159 nrela = sechdrs[i].sh_size / sizeof(Elf_Rela);
160 rela = (void *) hdr + sechdrs[i].sh_offset;
161 for (j = 0; j < nrela; j++)
162 check_rela(rela + j, me);
165 /* Increase core size by size of got & plt and set start
166 offsets for got and plt. */
167 me->core_size = ALIGN(me->core_size, 4);
168 me->arch.got_offset = me->core_size;
169 me->core_size += me->arch.got_size;
170 me->arch.plt_offset = me->core_size;
171 me->core_size += me->arch.plt_size;
176 apply_relocate(Elf_Shdr *sechdrs, const char *strtab, unsigned int symindex,
177 unsigned int relsec, struct module *me)
179 printk(KERN_ERR "module %s: RELOCATION unsupported\n",
185 apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
188 struct mod_arch_syminfo *info;
192 /* This is where to make the change */
193 loc = base + rela->r_offset;
194 /* This is the symbol it is referring to. Note that all
195 undefined symbols have been resolved. */
196 r_sym = ELF_R_SYM(rela->r_info);
197 r_type = ELF_R_TYPE(rela->r_info);
198 info = me->arch.syminfo + r_sym;
199 val = symtab[r_sym].st_value;
202 case R_390_8: /* Direct 8 bit. */
203 case R_390_12: /* Direct 12 bit. */
204 case R_390_16: /* Direct 16 bit. */
205 case R_390_20: /* Direct 20 bit. */
206 case R_390_32: /* Direct 32 bit. */
207 case R_390_64: /* Direct 64 bit. */
208 val += rela->r_addend;
209 if (r_type == R_390_8)
210 *(unsigned char *) loc = val;
211 else if (r_type == R_390_12)
212 *(unsigned short *) loc = (val & 0xfff) |
213 (*(unsigned short *) loc & 0xf000);
214 else if (r_type == R_390_16)
215 *(unsigned short *) loc = val;
216 else if (r_type == R_390_20)
217 *(unsigned int *) loc =
218 (*(unsigned int *) loc & 0xf00000ff) |
219 (val & 0xfff) << 16 | (val & 0xff000) >> 4;
220 else if (r_type == R_390_32)
221 *(unsigned int *) loc = val;
222 else if (r_type == R_390_64)
223 *(unsigned long *) loc = val;
225 case R_390_PC16: /* PC relative 16 bit. */
226 case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */
227 case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */
228 case R_390_PC32: /* PC relative 32 bit. */
229 case R_390_PC64: /* PC relative 64 bit. */
230 val += rela->r_addend - loc;
231 if (r_type == R_390_PC16)
232 *(unsigned short *) loc = val;
233 else if (r_type == R_390_PC16DBL)
234 *(unsigned short *) loc = val >> 1;
235 else if (r_type == R_390_PC32DBL)
236 *(unsigned int *) loc = val >> 1;
237 else if (r_type == R_390_PC32)
238 *(unsigned int *) loc = val;
239 else if (r_type == R_390_PC64)
240 *(unsigned long *) loc = val;
242 case R_390_GOT12: /* 12 bit GOT offset. */
243 case R_390_GOT16: /* 16 bit GOT offset. */
244 case R_390_GOT20: /* 20 bit GOT offset. */
245 case R_390_GOT32: /* 32 bit GOT offset. */
246 case R_390_GOT64: /* 64 bit GOT offset. */
247 case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
248 case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
249 case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
250 case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
251 case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
252 case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
253 case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
254 if (info->got_initialized == 0) {
257 gotent = me->module_core + me->arch.got_offset +
260 info->got_initialized = 1;
262 val = info->got_offset + rela->r_addend;
263 if (r_type == R_390_GOT12 ||
264 r_type == R_390_GOTPLT12)
265 *(unsigned short *) loc = (val & 0xfff) |
266 (*(unsigned short *) loc & 0xf000);
267 else if (r_type == R_390_GOT16 ||
268 r_type == R_390_GOTPLT16)
269 *(unsigned short *) loc = val;
270 else if (r_type == R_390_GOT20 ||
271 r_type == R_390_GOTPLT20)
272 *(unsigned int *) loc =
273 (*(unsigned int *) loc & 0xf00000ff) |
274 (val & 0xfff) << 16 | (val & 0xff000) >> 4;
275 else if (r_type == R_390_GOT32 ||
276 r_type == R_390_GOTPLT32)
277 *(unsigned int *) loc = val;
278 else if (r_type == R_390_GOTENT ||
279 r_type == R_390_GOTPLTENT)
280 *(unsigned int *) loc =
281 (val + (Elf_Addr) me->module_core - loc) >> 1;
282 else if (r_type == R_390_GOT64 ||
283 r_type == R_390_GOTPLT64)
284 *(unsigned long *) loc = val;
286 case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
287 case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
288 case R_390_PLT32: /* 32 bit PC relative PLT address. */
289 case R_390_PLT64: /* 64 bit PC relative PLT address. */
290 case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
291 case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
292 case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
293 if (info->plt_initialized == 0) {
295 ip = me->module_core + me->arch.plt_offset +
298 ip[0] = 0x0d105810; /* basr 1,0; l 1,6(1); br 1 */
301 #else /* CONFIG_64BIT */
302 ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
305 ip[3] = (unsigned int) (val >> 32);
306 ip[4] = (unsigned int) val;
307 #endif /* CONFIG_64BIT */
308 info->plt_initialized = 1;
310 if (r_type == R_390_PLTOFF16 ||
311 r_type == R_390_PLTOFF32
312 || r_type == R_390_PLTOFF64
314 val = me->arch.plt_offset - me->arch.got_offset +
315 info->plt_offset + rela->r_addend;
317 val = (Elf_Addr) me->module_core +
318 me->arch.plt_offset + info->plt_offset +
319 rela->r_addend - loc;
320 if (r_type == R_390_PLT16DBL)
321 *(unsigned short *) loc = val >> 1;
322 else if (r_type == R_390_PLTOFF16)
323 *(unsigned short *) loc = val;
324 else if (r_type == R_390_PLT32DBL)
325 *(unsigned int *) loc = val >> 1;
326 else if (r_type == R_390_PLT32 ||
327 r_type == R_390_PLTOFF32)
328 *(unsigned int *) loc = val;
329 else if (r_type == R_390_PLT64 ||
330 r_type == R_390_PLTOFF64)
331 *(unsigned long *) loc = val;
333 case R_390_GOTOFF16: /* 16 bit offset to GOT. */
334 case R_390_GOTOFF32: /* 32 bit offset to GOT. */
335 case R_390_GOTOFF64: /* 64 bit offset to GOT. */
336 val = val + rela->r_addend -
337 ((Elf_Addr) me->module_core + me->arch.got_offset);
338 if (r_type == R_390_GOTOFF16)
339 *(unsigned short *) loc = val;
340 else if (r_type == R_390_GOTOFF32)
341 *(unsigned int *) loc = val;
342 else if (r_type == R_390_GOTOFF64)
343 *(unsigned long *) loc = val;
345 case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */
346 case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */
347 val = (Elf_Addr) me->module_core + me->arch.got_offset +
348 rela->r_addend - loc;
349 if (r_type == R_390_GOTPC)
350 *(unsigned int *) loc = val;
351 else if (r_type == R_390_GOTPCDBL)
352 *(unsigned int *) loc = val >> 1;
355 case R_390_GLOB_DAT: /* Create GOT entry. */
356 case R_390_JMP_SLOT: /* Create PLT entry. */
357 case R_390_RELATIVE: /* Adjust by program base. */
358 /* Only needed if we want to support loading of
359 modules linked with -shared. */
362 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
370 apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
371 unsigned int symindex, unsigned int relsec,
380 DEBUGP("Applying relocate section %u to %u\n",
381 relsec, sechdrs[relsec].sh_info);
382 base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
383 symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
384 rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
385 n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
387 for (i = 0; i < n; i++, rela++) {
388 rc = apply_rela(rela, base, symtab, me);
395 int module_finalize(const Elf_Ehdr *hdr,
396 const Elf_Shdr *sechdrs,
399 vfree(me->arch.syminfo);
403 void module_arch_cleanup(struct module *mod)