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