2 * Kernel module help for s390.
5 * Copyright IBM Corp. 2002, 2003
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 * based on i386 version
10 * Copyright (C) 2001 Rusty Russell.
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.
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.
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
26 #include <linux/module.h>
27 #include <linux/elf.h>
28 #include <linux/vmalloc.h>
30 #include <linux/string.h>
31 #include <linux/kernel.h>
32 #include <linux/moduleloader.h>
33 #include <linux/bug.h>
38 #define DEBUGP(fmt , ...)
42 #define PLT_ENTRY_SIZE 12
43 #else /* CONFIG_64BIT */
44 #define PLT_ENTRY_SIZE 20
45 #endif /* CONFIG_64BIT */
47 /* Free memory returned from module_alloc */
48 void module_free(struct module *mod, void *module_region)
51 vfree(mod->arch.syminfo);
52 mod->arch.syminfo = NULL;
58 check_rela(Elf_Rela *rela, struct module *me)
60 struct mod_arch_syminfo *info;
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*);
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;
97 /* Only needed if we want to support loading of
98 modules linked with -shared. */
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.
108 module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
109 char *secstrings, struct module *me)
117 /* Find symbol table and string table. */
119 for (i = 0; i < hdr->e_shnum; i++)
120 switch (sechdrs[i].sh_type) {
122 symtab = sechdrs + i;
126 printk(KERN_ERR "module %s: no symbol table\n", me->name);
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)
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;
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)
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);
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;
172 apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
175 struct mod_arch_syminfo *info;
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;
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;
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;
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) {
244 gotent = me->module_core + me->arch.got_offset +
247 info->got_initialized = 1;
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;
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) {
282 ip = me->module_core + me->arch.plt_offset +
285 ip[0] = 0x0d105810; /* basr 1,0; l 1,6(1); br 1 */
288 #else /* CONFIG_64BIT */
289 ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
292 ip[3] = (unsigned int) (val >> 32);
293 ip[4] = (unsigned int) val;
294 #endif /* CONFIG_64BIT */
295 info->plt_initialized = 1;
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;
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 +
310 val += rela->r_addend - loc;
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;
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;
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;
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. */
354 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
362 apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
363 unsigned int symindex, unsigned int relsec,
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);
379 for (i = 0; i < n; i++, rela++) {
380 rc = apply_rela(rela, base, symtab, me);
387 int module_finalize(const Elf_Ehdr *hdr,
388 const Elf_Shdr *sechdrs,
391 vfree(me->arch.syminfo);
392 me->arch.syminfo = NULL;