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