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 */
48 void *module_alloc(unsigned long size
)
50 if (PAGE_ALIGN(size
) > MODULES_LEN
)
52 return __vmalloc_node_range(size
, 1, MODULES_VADDR
, MODULES_END
,
53 GFP_KERNEL
, PAGE_KERNEL
, -1,
54 __builtin_return_address(0));
58 /* Free memory returned from module_alloc */
59 void module_free(struct module
*mod
, void *module_region
)
62 vfree(mod
->arch
.syminfo
);
63 mod
->arch
.syminfo
= NULL
;
69 check_rela(Elf_Rela
*rela
, struct module
*me
)
71 struct mod_arch_syminfo
*info
;
73 info
= me
->arch
.syminfo
+ ELF_R_SYM (rela
->r_info
);
74 switch (ELF_R_TYPE (rela
->r_info
)) {
75 case R_390_GOT12
: /* 12 bit GOT offset. */
76 case R_390_GOT16
: /* 16 bit GOT offset. */
77 case R_390_GOT20
: /* 20 bit GOT offset. */
78 case R_390_GOT32
: /* 32 bit GOT offset. */
79 case R_390_GOT64
: /* 64 bit GOT offset. */
80 case R_390_GOTENT
: /* 32 bit PC rel. to GOT entry shifted by 1. */
81 case R_390_GOTPLT12
: /* 12 bit offset to jump slot. */
82 case R_390_GOTPLT16
: /* 16 bit offset to jump slot. */
83 case R_390_GOTPLT20
: /* 20 bit offset to jump slot. */
84 case R_390_GOTPLT32
: /* 32 bit offset to jump slot. */
85 case R_390_GOTPLT64
: /* 64 bit offset to jump slot. */
86 case R_390_GOTPLTENT
: /* 32 bit rel. offset to jump slot >> 1. */
87 if (info
->got_offset
== -1UL) {
88 info
->got_offset
= me
->arch
.got_size
;
89 me
->arch
.got_size
+= sizeof(void*);
92 case R_390_PLT16DBL
: /* 16 bit PC rel. PLT shifted by 1. */
93 case R_390_PLT32DBL
: /* 32 bit PC rel. PLT shifted by 1. */
94 case R_390_PLT32
: /* 32 bit PC relative PLT address. */
95 case R_390_PLT64
: /* 64 bit PC relative PLT address. */
96 case R_390_PLTOFF16
: /* 16 bit offset from GOT to PLT. */
97 case R_390_PLTOFF32
: /* 32 bit offset from GOT to PLT. */
98 case R_390_PLTOFF64
: /* 16 bit offset from GOT to PLT. */
99 if (info
->plt_offset
== -1UL) {
100 info
->plt_offset
= me
->arch
.plt_size
;
101 me
->arch
.plt_size
+= PLT_ENTRY_SIZE
;
108 /* Only needed if we want to support loading of
109 modules linked with -shared. */
115 * Account for GOT and PLT relocations. We can't add sections for
116 * got and plt but we can increase the core module size.
119 module_frob_arch_sections(Elf_Ehdr
*hdr
, Elf_Shdr
*sechdrs
,
120 char *secstrings
, struct module
*me
)
128 /* Find symbol table and string table. */
130 for (i
= 0; i
< hdr
->e_shnum
; i
++)
131 switch (sechdrs
[i
].sh_type
) {
133 symtab
= sechdrs
+ i
;
137 printk(KERN_ERR
"module %s: no symbol table\n", me
->name
);
141 /* Allocate one syminfo structure per symbol. */
142 me
->arch
.nsyms
= symtab
->sh_size
/ sizeof(Elf_Sym
);
143 me
->arch
.syminfo
= vmalloc(me
->arch
.nsyms
*
144 sizeof(struct mod_arch_syminfo
));
145 if (!me
->arch
.syminfo
)
147 symbols
= (void *) hdr
+ symtab
->sh_offset
;
148 strings
= (void *) hdr
+ sechdrs
[symtab
->sh_link
].sh_offset
;
149 for (i
= 0; i
< me
->arch
.nsyms
; i
++) {
150 if (symbols
[i
].st_shndx
== SHN_UNDEF
&&
151 strcmp(strings
+ symbols
[i
].st_name
,
152 "_GLOBAL_OFFSET_TABLE_") == 0)
153 /* "Define" it as absolute. */
154 symbols
[i
].st_shndx
= SHN_ABS
;
155 me
->arch
.syminfo
[i
].got_offset
= -1UL;
156 me
->arch
.syminfo
[i
].plt_offset
= -1UL;
157 me
->arch
.syminfo
[i
].got_initialized
= 0;
158 me
->arch
.syminfo
[i
].plt_initialized
= 0;
161 /* Search for got/plt relocations. */
162 me
->arch
.got_size
= me
->arch
.plt_size
= 0;
163 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
164 if (sechdrs
[i
].sh_type
!= SHT_RELA
)
166 nrela
= sechdrs
[i
].sh_size
/ sizeof(Elf_Rela
);
167 rela
= (void *) hdr
+ sechdrs
[i
].sh_offset
;
168 for (j
= 0; j
< nrela
; j
++)
169 check_rela(rela
+ j
, me
);
172 /* Increase core size by size of got & plt and set start
173 offsets for got and plt. */
174 me
->core_size
= ALIGN(me
->core_size
, 4);
175 me
->arch
.got_offset
= me
->core_size
;
176 me
->core_size
+= me
->arch
.got_size
;
177 me
->arch
.plt_offset
= me
->core_size
;
178 me
->core_size
+= me
->arch
.plt_size
;
183 apply_rela(Elf_Rela
*rela
, Elf_Addr base
, Elf_Sym
*symtab
,
186 struct mod_arch_syminfo
*info
;
190 /* This is where to make the change */
191 loc
= base
+ rela
->r_offset
;
192 /* This is the symbol it is referring to. Note that all
193 undefined symbols have been resolved. */
194 r_sym
= ELF_R_SYM(rela
->r_info
);
195 r_type
= ELF_R_TYPE(rela
->r_info
);
196 info
= me
->arch
.syminfo
+ r_sym
;
197 val
= symtab
[r_sym
].st_value
;
200 case R_390_8
: /* Direct 8 bit. */
201 case R_390_12
: /* Direct 12 bit. */
202 case R_390_16
: /* Direct 16 bit. */
203 case R_390_20
: /* Direct 20 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_20
)
215 *(unsigned int *) loc
=
216 (*(unsigned int *) loc
& 0xf00000ff) |
217 (val
& 0xfff) << 16 | (val
& 0xff000) >> 4;
218 else if (r_type
== R_390_32
)
219 *(unsigned int *) loc
= val
;
220 else if (r_type
== R_390_64
)
221 *(unsigned long *) loc
= val
;
223 case R_390_PC16
: /* PC relative 16 bit. */
224 case R_390_PC16DBL
: /* PC relative 16 bit shifted by 1. */
225 case R_390_PC32DBL
: /* PC relative 32 bit shifted by 1. */
226 case R_390_PC32
: /* PC relative 32 bit. */
227 case R_390_PC64
: /* PC relative 64 bit. */
228 val
+= rela
->r_addend
- loc
;
229 if (r_type
== R_390_PC16
)
230 *(unsigned short *) loc
= val
;
231 else if (r_type
== R_390_PC16DBL
)
232 *(unsigned short *) loc
= val
>> 1;
233 else if (r_type
== R_390_PC32DBL
)
234 *(unsigned int *) loc
= val
>> 1;
235 else if (r_type
== R_390_PC32
)
236 *(unsigned int *) loc
= val
;
237 else if (r_type
== R_390_PC64
)
238 *(unsigned long *) loc
= val
;
240 case R_390_GOT12
: /* 12 bit GOT offset. */
241 case R_390_GOT16
: /* 16 bit GOT offset. */
242 case R_390_GOT20
: /* 20 bit GOT offset. */
243 case R_390_GOT32
: /* 32 bit GOT offset. */
244 case R_390_GOT64
: /* 64 bit GOT offset. */
245 case R_390_GOTENT
: /* 32 bit PC rel. to GOT entry shifted by 1. */
246 case R_390_GOTPLT12
: /* 12 bit offset to jump slot. */
247 case R_390_GOTPLT20
: /* 20 bit offset to jump slot. */
248 case R_390_GOTPLT16
: /* 16 bit offset to jump slot. */
249 case R_390_GOTPLT32
: /* 32 bit offset to jump slot. */
250 case R_390_GOTPLT64
: /* 64 bit offset to jump slot. */
251 case R_390_GOTPLTENT
: /* 32 bit rel. offset to jump slot >> 1. */
252 if (info
->got_initialized
== 0) {
255 gotent
= me
->module_core
+ me
->arch
.got_offset
+
258 info
->got_initialized
= 1;
260 val
= info
->got_offset
+ rela
->r_addend
;
261 if (r_type
== R_390_GOT12
||
262 r_type
== R_390_GOTPLT12
)
263 *(unsigned short *) loc
= (val
& 0xfff) |
264 (*(unsigned short *) loc
& 0xf000);
265 else if (r_type
== R_390_GOT16
||
266 r_type
== R_390_GOTPLT16
)
267 *(unsigned short *) loc
= val
;
268 else if (r_type
== R_390_GOT20
||
269 r_type
== R_390_GOTPLT20
)
270 *(unsigned int *) loc
=
271 (*(unsigned int *) loc
& 0xf00000ff) |
272 (val
& 0xfff) << 16 | (val
& 0xff000) >> 4;
273 else if (r_type
== R_390_GOT32
||
274 r_type
== R_390_GOTPLT32
)
275 *(unsigned int *) loc
= val
;
276 else if (r_type
== R_390_GOTENT
||
277 r_type
== R_390_GOTPLTENT
)
278 *(unsigned int *) loc
=
279 (val
+ (Elf_Addr
) me
->module_core
- loc
) >> 1;
280 else if (r_type
== R_390_GOT64
||
281 r_type
== R_390_GOTPLT64
)
282 *(unsigned long *) loc
= val
;
284 case R_390_PLT16DBL
: /* 16 bit PC rel. PLT shifted by 1. */
285 case R_390_PLT32DBL
: /* 32 bit PC rel. PLT shifted by 1. */
286 case R_390_PLT32
: /* 32 bit PC relative PLT address. */
287 case R_390_PLT64
: /* 64 bit PC relative PLT address. */
288 case R_390_PLTOFF16
: /* 16 bit offset from GOT to PLT. */
289 case R_390_PLTOFF32
: /* 32 bit offset from GOT to PLT. */
290 case R_390_PLTOFF64
: /* 16 bit offset from GOT to PLT. */
291 if (info
->plt_initialized
== 0) {
293 ip
= me
->module_core
+ me
->arch
.plt_offset
+
296 ip
[0] = 0x0d105810; /* basr 1,0; l 1,6(1); br 1 */
299 #else /* CONFIG_64BIT */
300 ip
[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
303 ip
[3] = (unsigned int) (val
>> 32);
304 ip
[4] = (unsigned int) val
;
305 #endif /* CONFIG_64BIT */
306 info
->plt_initialized
= 1;
308 if (r_type
== R_390_PLTOFF16
||
309 r_type
== R_390_PLTOFF32
||
310 r_type
== R_390_PLTOFF64
)
311 val
= me
->arch
.plt_offset
- me
->arch
.got_offset
+
312 info
->plt_offset
+ rela
->r_addend
;
314 if (!((r_type
== R_390_PLT16DBL
&&
315 val
- loc
+ 0xffffUL
< 0x1ffffeUL
) ||
316 (r_type
== R_390_PLT32DBL
&&
317 val
- loc
+ 0xffffffffULL
< 0x1fffffffeULL
)))
318 val
= (Elf_Addr
) me
->module_core
+
319 me
->arch
.plt_offset
+
321 val
+= rela
->r_addend
- loc
;
323 if (r_type
== R_390_PLT16DBL
)
324 *(unsigned short *) loc
= val
>> 1;
325 else if (r_type
== R_390_PLTOFF16
)
326 *(unsigned short *) loc
= val
;
327 else if (r_type
== R_390_PLT32DBL
)
328 *(unsigned int *) loc
= val
>> 1;
329 else if (r_type
== R_390_PLT32
||
330 r_type
== R_390_PLTOFF32
)
331 *(unsigned int *) loc
= val
;
332 else if (r_type
== R_390_PLT64
||
333 r_type
== R_390_PLTOFF64
)
334 *(unsigned long *) loc
= val
;
336 case R_390_GOTOFF16
: /* 16 bit offset to GOT. */
337 case R_390_GOTOFF32
: /* 32 bit offset to GOT. */
338 case R_390_GOTOFF64
: /* 64 bit offset to GOT. */
339 val
= val
+ rela
->r_addend
-
340 ((Elf_Addr
) me
->module_core
+ me
->arch
.got_offset
);
341 if (r_type
== R_390_GOTOFF16
)
342 *(unsigned short *) loc
= val
;
343 else if (r_type
== R_390_GOTOFF32
)
344 *(unsigned int *) loc
= val
;
345 else if (r_type
== R_390_GOTOFF64
)
346 *(unsigned long *) loc
= val
;
348 case R_390_GOTPC
: /* 32 bit PC relative offset to GOT. */
349 case R_390_GOTPCDBL
: /* 32 bit PC rel. off. to GOT shifted by 1. */
350 val
= (Elf_Addr
) me
->module_core
+ me
->arch
.got_offset
+
351 rela
->r_addend
- loc
;
352 if (r_type
== R_390_GOTPC
)
353 *(unsigned int *) loc
= val
;
354 else if (r_type
== R_390_GOTPCDBL
)
355 *(unsigned int *) loc
= val
>> 1;
358 case R_390_GLOB_DAT
: /* Create GOT entry. */
359 case R_390_JMP_SLOT
: /* Create PLT entry. */
360 case R_390_RELATIVE
: /* Adjust by program base. */
361 /* Only needed if we want to support loading of
362 modules linked with -shared. */
365 printk(KERN_ERR
"module %s: Unknown relocation: %u\n",
373 apply_relocate_add(Elf_Shdr
*sechdrs
, const char *strtab
,
374 unsigned int symindex
, unsigned int relsec
,
383 DEBUGP("Applying relocate section %u to %u\n",
384 relsec
, sechdrs
[relsec
].sh_info
);
385 base
= sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
;
386 symtab
= (Elf_Sym
*) sechdrs
[symindex
].sh_addr
;
387 rela
= (Elf_Rela
*) sechdrs
[relsec
].sh_addr
;
388 n
= sechdrs
[relsec
].sh_size
/ sizeof(Elf_Rela
);
390 for (i
= 0; i
< n
; i
++, rela
++) {
391 rc
= apply_rela(rela
, base
, symtab
, me
);
398 int module_finalize(const Elf_Ehdr
*hdr
,
399 const Elf_Shdr
*sechdrs
,
402 vfree(me
->arch
.syminfo
);
403 me
->arch
.syminfo
= NULL
;