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