This source file includes following definitions.
- in_init
- in_core
- in_local
- sign_unext
- low_sign_unext
- reassemble_14
- reassemble_16a
- reassemble_17
- reassemble_21
- reassemble_22
- module_alloc
- count_gots
- count_fdescs
- count_stubs
- count_gots
- count_fdescs
- count_stubs
- module_arch_freeing_init
- arch_mod_section_prepend
- module_frob_arch_sections
- get_got
- get_fdesc
- get_stub
- apply_relocate_add
- apply_relocate_add
- register_unwind_table
- deregister_unwind_table
- module_finalize
- module_arch_cleanup
- dereference_module_function_descriptor
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 #include <linux/moduleloader.h>
43 #include <linux/elf.h>
44 #include <linux/vmalloc.h>
45 #include <linux/fs.h>
46 #include <linux/string.h>
47 #include <linux/kernel.h>
48 #include <linux/bug.h>
49 #include <linux/mm.h>
50 #include <linux/slab.h>
51
52 #include <asm/pgtable.h>
53 #include <asm/unwind.h>
54 #include <asm/sections.h>
55
56 #define RELOC_REACHABLE(val, bits) \
57 (( ( !((val) & (1<<((bits)-1))) && ((val)>>(bits)) != 0 ) || \
58 ( ((val) & (1<<((bits)-1))) && ((val)>>(bits)) != (((__typeof__(val))(~0))>>((bits)+2)))) ? \
59 0 : 1)
60
61 #define CHECK_RELOC(val, bits) \
62 if (!RELOC_REACHABLE(val, bits)) { \
63 printk(KERN_ERR "module %s relocation of symbol %s is out of range (0x%lx in %d bits)\n", \
64 me->name, strtab + sym->st_name, (unsigned long)val, bits); \
65 return -ENOEXEC; \
66 }
67
68
69
70
71
72
73
74
75
76
77 #define MAX_GOTS 4095
78
79
80
81 static inline int in_init(struct module *me, void *loc)
82 {
83 return (loc >= me->init_layout.base &&
84 loc <= (me->init_layout.base + me->init_layout.size));
85 }
86
87 static inline int in_core(struct module *me, void *loc)
88 {
89 return (loc >= me->core_layout.base &&
90 loc <= (me->core_layout.base + me->core_layout.size));
91 }
92
93 static inline int in_local(struct module *me, void *loc)
94 {
95 return in_init(me, loc) || in_core(me, loc);
96 }
97
98 #ifndef CONFIG_64BIT
99 struct got_entry {
100 Elf32_Addr addr;
101 };
102
103 struct stub_entry {
104 Elf32_Word insns[2];
105 };
106 #else
107 struct got_entry {
108 Elf64_Addr addr;
109 };
110
111 struct stub_entry {
112 Elf64_Word insns[4];
113 };
114 #endif
115
116
117 #define rnd(x) (((x)+0x1000)&~0x1fff)
118
119 #define fsel(v,a) ((v)+(a))
120
121 #define lsel(v,a) (((v)+(a))>>11)
122
123 #define rsel(v,a) (((v)+(a))&0x7ff)
124
125 #define lrsel(v,a) (((v)+rnd(a))>>11)
126
127 #define rrsel(v,a) ((((v)+rnd(a))&0x7ff)+((a)-rnd(a)))
128
129 #define mask(x,sz) ((x) & ~((1<<(sz))-1))
130
131
132
133
134
135 static inline int sign_unext(int x, int len)
136 {
137 int len_ones;
138
139 len_ones = (1 << len) - 1;
140 return x & len_ones;
141 }
142
143 static inline int low_sign_unext(int x, int len)
144 {
145 int sign, temp;
146
147 sign = (x >> (len-1)) & 1;
148 temp = sign_unext(x, len-1);
149 return (temp << 1) | sign;
150 }
151
152 static inline int reassemble_14(int as14)
153 {
154 return (((as14 & 0x1fff) << 1) |
155 ((as14 & 0x2000) >> 13));
156 }
157
158 static inline int reassemble_16a(int as16)
159 {
160 int s, t;
161
162
163 t = (as16 << 1) & 0xffff;
164 s = (as16 & 0x8000);
165 return (t ^ s ^ (s >> 1)) | (s >> 15);
166 }
167
168
169 static inline int reassemble_17(int as17)
170 {
171 return (((as17 & 0x10000) >> 16) |
172 ((as17 & 0x0f800) << 5) |
173 ((as17 & 0x00400) >> 8) |
174 ((as17 & 0x003ff) << 3));
175 }
176
177 static inline int reassemble_21(int as21)
178 {
179 return (((as21 & 0x100000) >> 20) |
180 ((as21 & 0x0ffe00) >> 8) |
181 ((as21 & 0x000180) << 7) |
182 ((as21 & 0x00007c) << 14) |
183 ((as21 & 0x000003) << 12));
184 }
185
186 static inline int reassemble_22(int as22)
187 {
188 return (((as22 & 0x200000) >> 21) |
189 ((as22 & 0x1f0000) << 5) |
190 ((as22 & 0x00f800) << 5) |
191 ((as22 & 0x000400) >> 8) |
192 ((as22 & 0x0003ff) << 3));
193 }
194
195 void *module_alloc(unsigned long size)
196 {
197
198
199
200 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
201 GFP_KERNEL,
202 PAGE_KERNEL_RWX, 0, NUMA_NO_NODE,
203 __builtin_return_address(0));
204 }
205
206 #ifndef CONFIG_64BIT
207 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
208 {
209 return 0;
210 }
211
212 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
213 {
214 return 0;
215 }
216
217 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
218 {
219 unsigned long cnt = 0;
220
221 for (; n > 0; n--, rela++)
222 {
223 switch (ELF32_R_TYPE(rela->r_info)) {
224 case R_PARISC_PCREL17F:
225 case R_PARISC_PCREL22F:
226 cnt++;
227 }
228 }
229
230 return cnt;
231 }
232 #else
233 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
234 {
235 unsigned long cnt = 0;
236
237 for (; n > 0; n--, rela++)
238 {
239 switch (ELF64_R_TYPE(rela->r_info)) {
240 case R_PARISC_LTOFF21L:
241 case R_PARISC_LTOFF14R:
242 case R_PARISC_PCREL22F:
243 cnt++;
244 }
245 }
246
247 return cnt;
248 }
249
250 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
251 {
252 unsigned long cnt = 0;
253
254 for (; n > 0; n--, rela++)
255 {
256 switch (ELF64_R_TYPE(rela->r_info)) {
257 case R_PARISC_FPTR64:
258 cnt++;
259 }
260 }
261
262 return cnt;
263 }
264
265 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
266 {
267 unsigned long cnt = 0;
268
269 for (; n > 0; n--, rela++)
270 {
271 switch (ELF64_R_TYPE(rela->r_info)) {
272 case R_PARISC_PCREL22F:
273 cnt++;
274 }
275 }
276
277 return cnt;
278 }
279 #endif
280
281 void module_arch_freeing_init(struct module *mod)
282 {
283 kfree(mod->arch.section);
284 mod->arch.section = NULL;
285 }
286
287
288 unsigned int arch_mod_section_prepend(struct module *mod,
289 unsigned int section)
290 {
291
292
293 return (mod->arch.section[section].stub_entries + 1)
294 * sizeof(struct stub_entry);
295 }
296
297 #define CONST
298 int module_frob_arch_sections(CONST Elf_Ehdr *hdr,
299 CONST Elf_Shdr *sechdrs,
300 CONST char *secstrings,
301 struct module *me)
302 {
303 unsigned long gots = 0, fdescs = 0, len;
304 unsigned int i;
305
306 len = hdr->e_shnum * sizeof(me->arch.section[0]);
307 me->arch.section = kzalloc(len, GFP_KERNEL);
308 if (!me->arch.section)
309 return -ENOMEM;
310
311 for (i = 1; i < hdr->e_shnum; i++) {
312 const Elf_Rela *rels = (void *)sechdrs[i].sh_addr;
313 unsigned long nrels = sechdrs[i].sh_size / sizeof(*rels);
314 unsigned int count, s;
315
316 if (strncmp(secstrings + sechdrs[i].sh_name,
317 ".PARISC.unwind", 14) == 0)
318 me->arch.unwind_section = i;
319
320 if (sechdrs[i].sh_type != SHT_RELA)
321 continue;
322
323
324
325
326
327
328 gots += count_gots(rels, nrels);
329 fdescs += count_fdescs(rels, nrels);
330
331
332
333
334 count = count_stubs(rels, nrels);
335 if (!count)
336 continue;
337
338
339
340 s = sechdrs[i].sh_info;
341
342
343 WARN_ON(me->arch.section[s].stub_entries);
344
345
346 me->arch.section[s].stub_entries += count;
347 }
348
349
350 me->core_layout.size = ALIGN(me->core_layout.size, 16);
351 me->arch.got_offset = me->core_layout.size;
352 me->core_layout.size += gots * sizeof(struct got_entry);
353
354 me->core_layout.size = ALIGN(me->core_layout.size, 16);
355 me->arch.fdesc_offset = me->core_layout.size;
356 me->core_layout.size += fdescs * sizeof(Elf_Fdesc);
357
358 me->arch.got_max = gots;
359 me->arch.fdesc_max = fdescs;
360
361 return 0;
362 }
363
364 #ifdef CONFIG_64BIT
365 static Elf64_Word get_got(struct module *me, unsigned long value, long addend)
366 {
367 unsigned int i;
368 struct got_entry *got;
369
370 value += addend;
371
372 BUG_ON(value == 0);
373
374 got = me->core_layout.base + me->arch.got_offset;
375 for (i = 0; got[i].addr; i++)
376 if (got[i].addr == value)
377 goto out;
378
379 BUG_ON(++me->arch.got_count > me->arch.got_max);
380
381 got[i].addr = value;
382 out:
383 pr_debug("GOT ENTRY %d[%lx] val %lx\n", i, i*sizeof(struct got_entry),
384 value);
385 return i * sizeof(struct got_entry);
386 }
387 #endif
388
389 #ifdef CONFIG_64BIT
390 static Elf_Addr get_fdesc(struct module *me, unsigned long value)
391 {
392 Elf_Fdesc *fdesc = me->core_layout.base + me->arch.fdesc_offset;
393
394 if (!value) {
395 printk(KERN_ERR "%s: zero OPD requested!\n", me->name);
396 return 0;
397 }
398
399
400 while (fdesc->addr) {
401 if (fdesc->addr == value)
402 return (Elf_Addr)fdesc;
403 fdesc++;
404 }
405
406 BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max);
407
408
409 fdesc->addr = value;
410 fdesc->gp = (Elf_Addr)me->core_layout.base + me->arch.got_offset;
411 return (Elf_Addr)fdesc;
412 }
413 #endif
414
415 enum elf_stub_type {
416 ELF_STUB_GOT,
417 ELF_STUB_MILLI,
418 ELF_STUB_DIRECT,
419 };
420
421 static Elf_Addr get_stub(struct module *me, unsigned long value, long addend,
422 enum elf_stub_type stub_type, Elf_Addr loc0, unsigned int targetsec)
423 {
424 struct stub_entry *stub;
425 int __maybe_unused d;
426
427
428 if (!me->arch.section[targetsec].stub_offset) {
429 loc0 -= (me->arch.section[targetsec].stub_entries + 1) *
430 sizeof(struct stub_entry);
431
432 loc0 = ALIGN(loc0, sizeof(struct stub_entry));
433 me->arch.section[targetsec].stub_offset = loc0;
434 }
435
436
437 stub = (void *) me->arch.section[targetsec].stub_offset;
438 me->arch.section[targetsec].stub_offset += sizeof(struct stub_entry);
439
440
441 BUG_ON(0 == me->arch.section[targetsec].stub_entries--);
442
443
444 #ifndef CONFIG_64BIT
445
446
447
448
449
450
451 stub->insns[0] = 0x20200000;
452 stub->insns[1] = 0xe0202002;
453
454 stub->insns[0] |= reassemble_21(lrsel(value, addend));
455 stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4);
456
457 #else
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477 switch (stub_type) {
478 case ELF_STUB_GOT:
479 d = get_got(me, value, addend);
480 if (d <= 15) {
481
482 stub->insns[0] = 0x0f6010db;
483 stub->insns[0] |= low_sign_unext(d, 5) << 16;
484 } else {
485
486 stub->insns[0] = 0x537b0000;
487 stub->insns[0] |= reassemble_16a(d);
488 }
489 stub->insns[1] = 0x53610020;
490 stub->insns[2] = 0xe820d000;
491 stub->insns[3] = 0x537b0030;
492 break;
493 case ELF_STUB_MILLI:
494 stub->insns[0] = 0x20200000;
495 stub->insns[1] = 0x34210000;
496 stub->insns[2] = 0x50210020;
497 stub->insns[3] = 0xe820d002;
498
499 stub->insns[0] |= reassemble_21(lrsel(value, addend));
500 stub->insns[1] |= reassemble_14(rrsel(value, addend));
501 break;
502 case ELF_STUB_DIRECT:
503 stub->insns[0] = 0x20200000;
504 stub->insns[1] = 0x34210000;
505 stub->insns[2] = 0xe820d002;
506
507 stub->insns[0] |= reassemble_21(lrsel(value, addend));
508 stub->insns[1] |= reassemble_14(rrsel(value, addend));
509 break;
510 }
511
512 #endif
513
514 return (Elf_Addr)stub;
515 }
516
517 #ifndef CONFIG_64BIT
518 int apply_relocate_add(Elf_Shdr *sechdrs,
519 const char *strtab,
520 unsigned int symindex,
521 unsigned int relsec,
522 struct module *me)
523 {
524 int i;
525 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
526 Elf32_Sym *sym;
527 Elf32_Word *loc;
528 Elf32_Addr val;
529 Elf32_Sword addend;
530 Elf32_Addr dot;
531 Elf_Addr loc0;
532 unsigned int targetsec = sechdrs[relsec].sh_info;
533
534 register unsigned long dp asm ("r27");
535
536 pr_debug("Applying relocate section %u to %u\n", relsec,
537 targetsec);
538 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
539
540 loc = (void *)sechdrs[targetsec].sh_addr
541 + rel[i].r_offset;
542
543 loc0 = sechdrs[targetsec].sh_addr;
544
545 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
546 + ELF32_R_SYM(rel[i].r_info);
547 if (!sym->st_value) {
548 printk(KERN_WARNING "%s: Unknown symbol %s\n",
549 me->name, strtab + sym->st_name);
550 return -ENOENT;
551 }
552
553 dot = (Elf32_Addr)loc & ~0x03;
554
555 val = sym->st_value;
556 addend = rel[i].r_addend;
557
558 #if 0
559 #define r(t) ELF32_R_TYPE(rel[i].r_info)==t ? #t :
560 pr_debug("Symbol %s loc 0x%x val 0x%x addend 0x%x: %s\n",
561 strtab + sym->st_name,
562 (uint32_t)loc, val, addend,
563 r(R_PARISC_PLABEL32)
564 r(R_PARISC_DIR32)
565 r(R_PARISC_DIR21L)
566 r(R_PARISC_DIR14R)
567 r(R_PARISC_SEGREL32)
568 r(R_PARISC_DPREL21L)
569 r(R_PARISC_DPREL14R)
570 r(R_PARISC_PCREL17F)
571 r(R_PARISC_PCREL22F)
572 "UNKNOWN");
573 #undef r
574 #endif
575
576 switch (ELF32_R_TYPE(rel[i].r_info)) {
577 case R_PARISC_PLABEL32:
578
579
580 *loc = fsel(val, addend);
581 break;
582 case R_PARISC_DIR32:
583
584 *loc = fsel(val, addend);
585 break;
586 case R_PARISC_DIR21L:
587
588 val = lrsel(val, addend);
589 *loc = mask(*loc, 21) | reassemble_21(val);
590 break;
591 case R_PARISC_DIR14R:
592
593 val = rrsel(val, addend);
594 *loc = mask(*loc, 14) | reassemble_14(val);
595 break;
596 case R_PARISC_SEGREL32:
597
598
599
600
601 *loc = fsel(val, addend);
602 break;
603 case R_PARISC_SECREL32:
604
605 *loc = fsel(val, addend);
606 break;
607 case R_PARISC_DPREL21L:
608
609 val = lrsel(val - dp, addend);
610 *loc = mask(*loc, 21) | reassemble_21(val);
611 break;
612 case R_PARISC_DPREL14R:
613
614 val = rrsel(val - dp, addend);
615 *loc = mask(*loc, 14) | reassemble_14(val);
616 break;
617 case R_PARISC_PCREL17F:
618
619
620 val += addend;
621 val = (val - dot - 8)/4;
622 if (!RELOC_REACHABLE(val, 17)) {
623
624
625 val = get_stub(me, sym->st_value, addend,
626 ELF_STUB_DIRECT, loc0, targetsec);
627 val = (val - dot - 8)/4;
628 CHECK_RELOC(val, 17);
629 }
630 *loc = (*loc & ~0x1f1ffd) | reassemble_17(val);
631 break;
632 case R_PARISC_PCREL22F:
633
634
635 val += addend;
636 val = (val - dot - 8)/4;
637 if (!RELOC_REACHABLE(val, 22)) {
638
639
640 val = get_stub(me, sym->st_value, addend,
641 ELF_STUB_DIRECT, loc0, targetsec);
642 val = (val - dot - 8)/4;
643 CHECK_RELOC(val, 22);
644 }
645 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
646 break;
647 case R_PARISC_PCREL32:
648
649 *loc = val - dot - 8 + addend;
650 break;
651
652 default:
653 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
654 me->name, ELF32_R_TYPE(rel[i].r_info));
655 return -ENOEXEC;
656 }
657 }
658
659 return 0;
660 }
661
662 #else
663 int apply_relocate_add(Elf_Shdr *sechdrs,
664 const char *strtab,
665 unsigned int symindex,
666 unsigned int relsec,
667 struct module *me)
668 {
669 int i;
670 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
671 Elf64_Sym *sym;
672 Elf64_Word *loc;
673 Elf64_Xword *loc64;
674 Elf64_Addr val;
675 Elf64_Sxword addend;
676 Elf64_Addr dot;
677 Elf_Addr loc0;
678 unsigned int targetsec = sechdrs[relsec].sh_info;
679
680 pr_debug("Applying relocate section %u to %u\n", relsec,
681 targetsec);
682 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
683
684 loc = (void *)sechdrs[targetsec].sh_addr
685 + rel[i].r_offset;
686
687 loc0 = sechdrs[targetsec].sh_addr;
688
689 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
690 + ELF64_R_SYM(rel[i].r_info);
691 if (!sym->st_value) {
692 printk(KERN_WARNING "%s: Unknown symbol %s\n",
693 me->name, strtab + sym->st_name);
694 return -ENOENT;
695 }
696
697 dot = (Elf64_Addr)loc & ~0x03;
698 loc64 = (Elf64_Xword *)loc;
699
700 val = sym->st_value;
701 addend = rel[i].r_addend;
702
703 #if 0
704 #define r(t) ELF64_R_TYPE(rel[i].r_info)==t ? #t :
705 printk("Symbol %s loc %p val 0x%Lx addend 0x%Lx: %s\n",
706 strtab + sym->st_name,
707 loc, val, addend,
708 r(R_PARISC_LTOFF14R)
709 r(R_PARISC_LTOFF21L)
710 r(R_PARISC_PCREL22F)
711 r(R_PARISC_DIR64)
712 r(R_PARISC_SEGREL32)
713 r(R_PARISC_FPTR64)
714 "UNKNOWN");
715 #undef r
716 #endif
717
718 switch (ELF64_R_TYPE(rel[i].r_info)) {
719 case R_PARISC_LTOFF21L:
720
721 val = get_got(me, val, addend);
722 pr_debug("LTOFF21L Symbol %s loc %p val %llx\n",
723 strtab + sym->st_name,
724 loc, val);
725 val = lrsel(val, 0);
726 *loc = mask(*loc, 21) | reassemble_21(val);
727 break;
728 case R_PARISC_LTOFF14R:
729
730
731 val = get_got(me, val, addend);
732 val = rrsel(val, 0);
733 pr_debug("LTOFF14R Symbol %s loc %p val %llx\n",
734 strtab + sym->st_name,
735 loc, val);
736 *loc = mask(*loc, 14) | reassemble_14(val);
737 break;
738 case R_PARISC_PCREL22F:
739
740 pr_debug("PCREL22F Symbol %s loc %p val %llx\n",
741 strtab + sym->st_name,
742 loc, val);
743 val += addend;
744
745 if (in_local(me, (void *)val)) {
746
747
748
749
750 val = (val - dot - 8)/4;
751 if (!RELOC_REACHABLE(val, 22)) {
752
753
754 val = get_stub(me, sym->st_value,
755 addend, ELF_STUB_DIRECT,
756 loc0, targetsec);
757 } else {
758
759 val = sym->st_value;
760 val += addend;
761 }
762 } else {
763 val = sym->st_value;
764 if (strncmp(strtab + sym->st_name, "$$", 2)
765 == 0)
766 val = get_stub(me, val, addend, ELF_STUB_MILLI,
767 loc0, targetsec);
768 else
769 val = get_stub(me, val, addend, ELF_STUB_GOT,
770 loc0, targetsec);
771 }
772 pr_debug("STUB FOR %s loc %px, val %llx+%llx at %llx\n",
773 strtab + sym->st_name, loc, sym->st_value,
774 addend, val);
775 val = (val - dot - 8)/4;
776 CHECK_RELOC(val, 22);
777 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
778 break;
779 case R_PARISC_PCREL32:
780
781 *loc = val - dot - 8 + addend;
782 break;
783 case R_PARISC_PCREL64:
784
785 *loc64 = val - dot - 8 + addend;
786 break;
787 case R_PARISC_DIR64:
788
789 *loc64 = val + addend;
790 break;
791 case R_PARISC_SEGREL32:
792
793
794
795
796 *loc = fsel(val, addend);
797 break;
798 case R_PARISC_SECREL32:
799
800 *loc = fsel(val, addend);
801 break;
802 case R_PARISC_FPTR64:
803
804 if(in_local(me, (void *)(val + addend))) {
805 *loc64 = get_fdesc(me, val+addend);
806 pr_debug("FDESC for %s at %llx points to %llx\n",
807 strtab + sym->st_name, *loc64,
808 ((Elf_Fdesc *)*loc64)->addr);
809 } else {
810
811
812
813 pr_debug("Non local FPTR64 Symbol %s loc %p val %llx\n",
814 strtab + sym->st_name,
815 loc, val);
816 *loc64 = val + addend;
817 }
818 break;
819
820 default:
821 printk(KERN_ERR "module %s: Unknown relocation: %Lu\n",
822 me->name, ELF64_R_TYPE(rel[i].r_info));
823 return -ENOEXEC;
824 }
825 }
826 return 0;
827 }
828 #endif
829
830 static void
831 register_unwind_table(struct module *me,
832 const Elf_Shdr *sechdrs)
833 {
834 unsigned char *table, *end;
835 unsigned long gp;
836
837 if (!me->arch.unwind_section)
838 return;
839
840 table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr;
841 end = table + sechdrs[me->arch.unwind_section].sh_size;
842 gp = (Elf_Addr)me->core_layout.base + me->arch.got_offset;
843
844 pr_debug("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n",
845 me->arch.unwind_section, table, end, gp);
846 me->arch.unwind = unwind_table_add(me->name, 0, gp, table, end);
847 }
848
849 static void
850 deregister_unwind_table(struct module *me)
851 {
852 if (me->arch.unwind)
853 unwind_table_remove(me->arch.unwind);
854 }
855
856 int module_finalize(const Elf_Ehdr *hdr,
857 const Elf_Shdr *sechdrs,
858 struct module *me)
859 {
860 int i;
861 unsigned long nsyms;
862 const char *strtab = NULL;
863 const Elf_Shdr *s;
864 char *secstrings;
865 int err, symindex = -1;
866 Elf_Sym *newptr, *oldptr;
867 Elf_Shdr *symhdr = NULL;
868 #ifdef DEBUG
869 Elf_Fdesc *entry;
870 u32 *addr;
871
872 entry = (Elf_Fdesc *)me->init;
873 printk("FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry,
874 entry->gp, entry->addr);
875 addr = (u32 *)entry->addr;
876 printk("INSNS: %x %x %x %x\n",
877 addr[0], addr[1], addr[2], addr[3]);
878 printk("got entries used %ld, gots max %ld\n"
879 "fdescs used %ld, fdescs max %ld\n",
880 me->arch.got_count, me->arch.got_max,
881 me->arch.fdesc_count, me->arch.fdesc_max);
882 #endif
883
884 register_unwind_table(me, sechdrs);
885
886
887
888 for (i = 1; i < hdr->e_shnum; i++) {
889 if(sechdrs[i].sh_type == SHT_SYMTAB
890 && (sechdrs[i].sh_flags & SHF_ALLOC)) {
891 int strindex = sechdrs[i].sh_link;
892 symindex = i;
893
894
895
896 symhdr = (Elf_Shdr *)&sechdrs[i];
897 strtab = (char *)sechdrs[strindex].sh_addr;
898 break;
899 }
900 }
901
902 pr_debug("module %s: strtab %p, symhdr %p\n",
903 me->name, strtab, symhdr);
904
905 if(me->arch.got_count > MAX_GOTS) {
906 printk(KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d)\n",
907 me->name, me->arch.got_count, MAX_GOTS);
908 return -EINVAL;
909 }
910
911 kfree(me->arch.section);
912 me->arch.section = NULL;
913
914
915 if(symhdr == NULL)
916 return 0;
917
918 oldptr = (void *)symhdr->sh_addr;
919 newptr = oldptr + 1;
920 nsyms = symhdr->sh_size / sizeof(Elf_Sym);
921 pr_debug("OLD num_symtab %lu\n", nsyms);
922
923 for (i = 1; i < nsyms; i++) {
924 oldptr++;
925 if(strncmp(strtab + oldptr->st_name,
926 ".L", 2) == 0)
927 continue;
928
929 if(newptr != oldptr)
930 *newptr++ = *oldptr;
931 else
932 newptr++;
933
934 }
935 nsyms = newptr - (Elf_Sym *)symhdr->sh_addr;
936 pr_debug("NEW num_symtab %lu\n", nsyms);
937 symhdr->sh_size = nsyms * sizeof(Elf_Sym);
938
939
940 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
941 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
942 void *aseg = (void *) s->sh_addr;
943 char *secname = secstrings + s->sh_name;
944
945 if (!strcmp(".altinstructions", secname))
946
947 apply_alternatives(aseg, aseg + s->sh_size, me->name);
948
949
950
951
952
953 if (symindex != -1 && !strcmp(secname, "__mcount_loc")) {
954 if (s->sh_type == SHT_REL)
955 err = apply_relocate((Elf_Shdr *)sechdrs,
956 strtab, symindex,
957 s - sechdrs, me);
958 else if (s->sh_type == SHT_RELA)
959 err = apply_relocate_add((Elf_Shdr *)sechdrs,
960 strtab, symindex,
961 s - sechdrs, me);
962 if (err)
963 return err;
964 }
965 }
966 return 0;
967 }
968
969 void module_arch_cleanup(struct module *mod)
970 {
971 deregister_unwind_table(mod);
972 }
973
974 #ifdef CONFIG_64BIT
975 void *dereference_module_function_descriptor(struct module *mod, void *ptr)
976 {
977 unsigned long start_opd = (Elf64_Addr)mod->core_layout.base +
978 mod->arch.fdesc_offset;
979 unsigned long end_opd = start_opd +
980 mod->arch.fdesc_count * sizeof(Elf64_Fdesc);
981
982 if (ptr < (void *)start_opd || ptr >= (void *)end_opd)
983 return ptr;
984
985 return dereference_function_descriptor(ptr);
986 }
987 #endif