1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kernel dynamically loadable module help for PARISC.
4 * The best reference for this stuff is probably the Processor-
5 * Specific ELF Supplement for PA-RISC:
6 * https://parisc.wiki.kernel.org/index.php/File:Elf-pa-hp.pdf
8 * Linux/PA-RISC Project
9 * Copyright (C) 2003 Randolph Chung <tausq at debian . org>
10 * Copyright (C) 2008 Helge Deller <deller@gmx.de>
14 * On 32bit (and sometimes 64bit) and with big kernel modules like xfs or
15 * ipv6 the relocation types R_PARISC_PCREL17F and R_PARISC_PCREL22F may
16 * fail to reach their PLT stub if we only create one big stub array for
17 * all sections at the beginning of the core or init section.
18 * Instead we now insert individual PLT stub entries directly in front of
19 * of the code sections where the stubs are actually called.
20 * This reduces the distance between the PCREL location and the stub entry
21 * so that the relocations can be fulfilled.
22 * While calculating the final layout of the kernel module in memory, the
23 * kernel module loader calls arch_mod_section_prepend() to request the
24 * to be reserved amount of memory in front of each individual section.
27 * We are not doing SEGREL32 handling correctly. According to the ABI, we
28 * should do a value offset, like this:
29 * if (in_init(me, (void *)val))
30 * val -= (uint32_t)me->mem[MOD_INIT_TEXT].base;
32 * val -= (uint32_t)me->mem[MOD_TEXT].base;
33 * However, SEGREL32 is used only for PARISC unwind entries, and we want
34 * those entries to have an absolute address, and not just an offset.
36 * The unwind table mechanism has the ability to specify an offset for
37 * the unwind table; however, because we split off the init functions into
38 * a different piece of memory, it is not possible to do this using a
39 * single offset. Instead, we use the above hack for now.
42 #include <linux/moduleloader.h>
43 #include <linux/elf.h>
44 #include <linux/vmalloc.h>
46 #include <linux/ftrace.h>
47 #include <linux/string.h>
48 #include <linux/kernel.h>
49 #include <linux/bug.h>
51 #include <linux/slab.h>
53 #include <asm/unwind.h>
54 #include <asm/sections.h>
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)))) ? \
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); \
68 /* Maximum number of GOT entries. We use a long displacement ldd from
69 * the bottom of the table, which has a maximum signed displacement of
70 * 0x3fff; however, since we're only going forward, this becomes
71 * 0x1fff, and thus, since each GOT entry is 8 bytes long we can have
72 * at most 1023 entries.
73 * To overcome this 14bit displacement with some kernel modules, we'll
74 * use instead the unusal 16bit displacement method (see reassemble_16a)
75 * which gives us a maximum positive displacement of 0x7fff, and as such
76 * allows us to allocate up to 4095 GOT entries. */
85 Elf32_Word insns[2]; /* each stub entry has two insns */
93 Elf64_Word insns[4]; /* each stub entry has four insns */
97 /* Field selection types defined by hppa */
98 #define rnd(x) (((x)+0x1000)&~0x1fff)
99 /* fsel: full 32 bits */
100 #define fsel(v,a) ((v)+(a))
101 /* lsel: select left 21 bits */
102 #define lsel(v,a) (((v)+(a))>>11)
103 /* rsel: select right 11 bits */
104 #define rsel(v,a) (((v)+(a))&0x7ff)
105 /* lrsel with rounding of addend to nearest 8k */
106 #define lrsel(v,a) (((v)+rnd(a))>>11)
107 /* rrsel with rounding of addend to nearest 8k */
108 #define rrsel(v,a) ((((v)+rnd(a))&0x7ff)+((a)-rnd(a)))
110 #define mask(x,sz) ((x) & ~((1<<(sz))-1))
113 /* The reassemble_* functions prepare an immediate value for
114 insertion into an opcode. pa-risc uses all sorts of weird bitfields
115 in the instruction to hold the value. */
116 static inline int sign_unext(int x, int len)
120 len_ones = (1 << len) - 1;
124 static inline int low_sign_unext(int x, int len)
128 sign = (x >> (len-1)) & 1;
129 temp = sign_unext(x, len-1);
130 return (temp << 1) | sign;
133 static inline int reassemble_14(int as14)
135 return (((as14 & 0x1fff) << 1) |
136 ((as14 & 0x2000) >> 13));
139 static inline int reassemble_16a(int as16)
143 /* Unusual 16-bit encoding, for wide mode only. */
144 t = (as16 << 1) & 0xffff;
146 return (t ^ s ^ (s >> 1)) | (s >> 15);
150 static inline int reassemble_17(int as17)
152 return (((as17 & 0x10000) >> 16) |
153 ((as17 & 0x0f800) << 5) |
154 ((as17 & 0x00400) >> 8) |
155 ((as17 & 0x003ff) << 3));
158 static inline int reassemble_21(int as21)
160 return (((as21 & 0x100000) >> 20) |
161 ((as21 & 0x0ffe00) >> 8) |
162 ((as21 & 0x000180) << 7) |
163 ((as21 & 0x00007c) << 14) |
164 ((as21 & 0x000003) << 12));
167 static inline int reassemble_22(int as22)
169 return (((as22 & 0x200000) >> 21) |
170 ((as22 & 0x1f0000) << 5) |
171 ((as22 & 0x00f800) << 5) |
172 ((as22 & 0x000400) >> 8) |
173 ((as22 & 0x0003ff) << 3));
176 void *module_alloc(unsigned long size)
178 /* using RWX means less protection for modules, but it's
179 * easier than trying to map the text, data, init_text and
180 * init_data correctly */
181 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
183 PAGE_KERNEL_RWX, 0, NUMA_NO_NODE,
184 __builtin_return_address(0));
188 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
193 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
198 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
200 unsigned long cnt = 0;
202 for (; n > 0; n--, rela++)
204 switch (ELF32_R_TYPE(rela->r_info)) {
205 case R_PARISC_PCREL17F:
206 case R_PARISC_PCREL22F:
214 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
216 unsigned long cnt = 0;
218 for (; n > 0; n--, rela++)
220 switch (ELF64_R_TYPE(rela->r_info)) {
221 case R_PARISC_LTOFF21L:
222 case R_PARISC_LTOFF14R:
223 case R_PARISC_PCREL22F:
231 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
233 unsigned long cnt = 0;
235 for (; n > 0; n--, rela++)
237 switch (ELF64_R_TYPE(rela->r_info)) {
238 case R_PARISC_FPTR64:
246 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
248 unsigned long cnt = 0;
250 for (; n > 0; n--, rela++)
252 switch (ELF64_R_TYPE(rela->r_info)) {
253 case R_PARISC_PCREL22F:
262 void module_arch_freeing_init(struct module *mod)
264 kfree(mod->arch.section);
265 mod->arch.section = NULL;
268 /* Additional bytes needed in front of individual sections */
269 unsigned int arch_mod_section_prepend(struct module *mod,
270 unsigned int section)
272 /* size needed for all stubs of this section (including
273 * one additional for correct alignment of the stubs) */
274 return (mod->arch.section[section].stub_entries + 1)
275 * sizeof(struct stub_entry);
279 int module_frob_arch_sections(CONST Elf_Ehdr *hdr,
280 CONST Elf_Shdr *sechdrs,
281 CONST char *secstrings,
284 unsigned long gots = 0, fdescs = 0, len;
286 struct module_memory *mod_mem;
288 len = hdr->e_shnum * sizeof(me->arch.section[0]);
289 me->arch.section = kzalloc(len, GFP_KERNEL);
290 if (!me->arch.section)
293 for (i = 1; i < hdr->e_shnum; i++) {
294 const Elf_Rela *rels = (void *)sechdrs[i].sh_addr;
295 unsigned long nrels = sechdrs[i].sh_size / sizeof(*rels);
296 unsigned int count, s;
298 if (strncmp(secstrings + sechdrs[i].sh_name,
299 ".PARISC.unwind", 14) == 0)
300 me->arch.unwind_section = i;
302 if (sechdrs[i].sh_type != SHT_RELA)
305 /* some of these are not relevant for 32-bit/64-bit
306 * we leave them here to make the code common. the
307 * compiler will do its thing and optimize out the
308 * stuff we don't need
310 gots += count_gots(rels, nrels);
311 fdescs += count_fdescs(rels, nrels);
313 /* XXX: By sorting the relocs and finding duplicate entries
314 * we could reduce the number of necessary stubs and save
316 count = count_stubs(rels, nrels);
320 /* so we need relocation stubs. reserve necessary memory. */
321 /* sh_info gives the section for which we need to add stubs. */
322 s = sechdrs[i].sh_info;
324 /* each code section should only have one relocation section */
325 WARN_ON(me->arch.section[s].stub_entries);
327 /* store number of stubs we need for this section */
328 me->arch.section[s].stub_entries += count;
331 mod_mem = &me->mem[MOD_TEXT];
332 /* align things a bit */
333 mod_mem->size = ALIGN(mod_mem->size, 16);
334 me->arch.got_offset = mod_mem->size;
335 mod_mem->size += gots * sizeof(struct got_entry);
337 mod_mem->size = ALIGN(mod_mem->size, 16);
338 me->arch.fdesc_offset = mod_mem->size;
339 mod_mem->size += fdescs * sizeof(Elf_Fdesc);
341 me->arch.got_max = gots;
342 me->arch.fdesc_max = fdescs;
348 static Elf64_Word get_got(struct module *me, unsigned long value, long addend)
351 struct got_entry *got;
357 got = me->mem[MOD_TEXT].base + me->arch.got_offset;
358 for (i = 0; got[i].addr; i++)
359 if (got[i].addr == value)
362 BUG_ON(++me->arch.got_count > me->arch.got_max);
366 pr_debug("GOT ENTRY %d[%lx] val %lx\n", i, i*sizeof(struct got_entry),
368 return i * sizeof(struct got_entry);
370 #endif /* CONFIG_64BIT */
373 static Elf_Addr get_fdesc(struct module *me, unsigned long value)
375 Elf_Fdesc *fdesc = me->mem[MOD_TEXT].base + me->arch.fdesc_offset;
378 printk(KERN_ERR "%s: zero OPD requested!\n", me->name);
382 /* Look for existing fdesc entry. */
383 while (fdesc->addr) {
384 if (fdesc->addr == value)
385 return (Elf_Addr)fdesc;
389 BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max);
393 fdesc->gp = (Elf_Addr)me->mem[MOD_TEXT].base + me->arch.got_offset;
394 return (Elf_Addr)fdesc;
396 #endif /* CONFIG_64BIT */
404 static Elf_Addr get_stub(struct module *me, unsigned long value, long addend,
405 enum elf_stub_type stub_type, Elf_Addr loc0, unsigned int targetsec)
407 struct stub_entry *stub;
408 int __maybe_unused d;
410 /* initialize stub_offset to point in front of the section */
411 if (!me->arch.section[targetsec].stub_offset) {
412 loc0 -= (me->arch.section[targetsec].stub_entries + 1) *
413 sizeof(struct stub_entry);
414 /* get correct alignment for the stubs */
415 loc0 = ALIGN(loc0, sizeof(struct stub_entry));
416 me->arch.section[targetsec].stub_offset = loc0;
419 /* get address of stub entry */
420 stub = (void *) me->arch.section[targetsec].stub_offset;
421 me->arch.section[targetsec].stub_offset += sizeof(struct stub_entry);
423 /* do not write outside available stub area */
424 BUG_ON(0 == me->arch.section[targetsec].stub_entries--);
428 /* for 32-bit the stub looks like this:
430 * be,n R'XXX(%sr4,%r1)
432 //value = *(unsigned long *)((value + addend) & ~3); /* why? */
434 stub->insns[0] = 0x20200000; /* ldil L'XXX,%r1 */
435 stub->insns[1] = 0xe0202002; /* be,n R'XXX(%sr4,%r1) */
437 stub->insns[0] |= reassemble_21(lrsel(value, addend));
438 stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4);
441 /* for 64-bit we have three kinds of stubs:
442 * for normal function calls:
454 * for direct branches (jumps between different section of the
462 d = get_got(me, value, addend);
465 stub->insns[0] = 0x0f6010db; /* ldd 0(%dp),%dp */
466 stub->insns[0] |= low_sign_unext(d, 5) << 16;
469 stub->insns[0] = 0x537b0000; /* ldd 0(%dp),%dp */
470 stub->insns[0] |= reassemble_16a(d);
472 stub->insns[1] = 0x53610020; /* ldd 10(%dp),%r1 */
473 stub->insns[2] = 0xe820d000; /* bve (%r1) */
474 stub->insns[3] = 0x537b0030; /* ldd 18(%dp),%dp */
477 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
478 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
479 stub->insns[2] = 0x50210020; /* ldd 10(%r1),%r1 */
480 stub->insns[3] = 0xe820d002; /* bve,n (%r1) */
482 stub->insns[0] |= reassemble_21(lrsel(value, addend));
483 stub->insns[1] |= reassemble_14(rrsel(value, addend));
485 case ELF_STUB_DIRECT:
486 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
487 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
488 stub->insns[2] = 0xe820d002; /* bve,n (%r1) */
490 stub->insns[0] |= reassemble_21(lrsel(value, addend));
491 stub->insns[1] |= reassemble_14(rrsel(value, addend));
497 return (Elf_Addr)stub;
501 int apply_relocate_add(Elf_Shdr *sechdrs,
503 unsigned int symindex,
508 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
515 unsigned int targetsec = sechdrs[relsec].sh_info;
516 //unsigned long dp = (unsigned long)$global$;
517 register unsigned long dp asm ("r27");
519 pr_debug("Applying relocate section %u to %u\n", relsec,
521 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
522 /* This is where to make the change */
523 loc = (void *)sechdrs[targetsec].sh_addr
525 /* This is the start of the target section */
526 loc0 = sechdrs[targetsec].sh_addr;
527 /* This is the symbol it is referring to */
528 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
529 + ELF32_R_SYM(rel[i].r_info);
530 if (!sym->st_value) {
531 printk(KERN_WARNING "%s: Unknown symbol %s\n",
532 me->name, strtab + sym->st_name);
535 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
536 dot = (Elf32_Addr)loc & ~0x03;
539 addend = rel[i].r_addend;
542 #define r(t) ELF32_R_TYPE(rel[i].r_info)==t ? #t :
543 pr_debug("Symbol %s loc 0x%x val 0x%x addend 0x%x: %s\n",
544 strtab + sym->st_name,
545 (uint32_t)loc, val, addend,
559 switch (ELF32_R_TYPE(rel[i].r_info)) {
560 case R_PARISC_PLABEL32:
561 /* 32-bit function address */
562 /* no function descriptors... */
563 *loc = fsel(val, addend);
566 /* direct 32-bit ref */
567 *loc = fsel(val, addend);
569 case R_PARISC_DIR21L:
570 /* left 21 bits of effective address */
571 val = lrsel(val, addend);
572 *loc = mask(*loc, 21) | reassemble_21(val);
574 case R_PARISC_DIR14R:
575 /* right 14 bits of effective address */
576 val = rrsel(val, addend);
577 *loc = mask(*loc, 14) | reassemble_14(val);
579 case R_PARISC_SEGREL32:
580 /* 32-bit segment relative address */
581 /* See note about special handling of SEGREL32 at
582 * the beginning of this file.
584 *loc = fsel(val, addend);
586 case R_PARISC_SECREL32:
587 /* 32-bit section relative address. */
588 *loc = fsel(val, addend);
590 case R_PARISC_DPREL21L:
591 /* left 21 bit of relative address */
592 val = lrsel(val - dp, addend);
593 *loc = mask(*loc, 21) | reassemble_21(val);
595 case R_PARISC_DPREL14R:
596 /* right 14 bit of relative address */
597 val = rrsel(val - dp, addend);
598 *loc = mask(*loc, 14) | reassemble_14(val);
600 case R_PARISC_PCREL17F:
601 /* 17-bit PC relative address */
602 /* calculate direct call offset */
604 val = (val - dot - 8)/4;
605 if (!RELOC_REACHABLE(val, 17)) {
606 /* direct distance too far, create
607 * stub entry instead */
608 val = get_stub(me, sym->st_value, addend,
609 ELF_STUB_DIRECT, loc0, targetsec);
610 val = (val - dot - 8)/4;
611 CHECK_RELOC(val, 17);
613 *loc = (*loc & ~0x1f1ffd) | reassemble_17(val);
615 case R_PARISC_PCREL22F:
616 /* 22-bit PC relative address; only defined for pa20 */
617 /* calculate direct call offset */
619 val = (val - dot - 8)/4;
620 if (!RELOC_REACHABLE(val, 22)) {
621 /* direct distance too far, create
622 * stub entry instead */
623 val = get_stub(me, sym->st_value, addend,
624 ELF_STUB_DIRECT, loc0, targetsec);
625 val = (val - dot - 8)/4;
626 CHECK_RELOC(val, 22);
628 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
630 case R_PARISC_PCREL32:
631 /* 32-bit PC relative address */
632 *loc = val - dot - 8 + addend;
636 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
637 me->name, ELF32_R_TYPE(rel[i].r_info));
646 int apply_relocate_add(Elf_Shdr *sechdrs,
648 unsigned int symindex,
653 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
661 unsigned int targetsec = sechdrs[relsec].sh_info;
663 pr_debug("Applying relocate section %u to %u\n", relsec,
665 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
666 /* This is where to make the change */
667 loc = (void *)sechdrs[targetsec].sh_addr
669 /* This is the start of the target section */
670 loc0 = sechdrs[targetsec].sh_addr;
671 /* This is the symbol it is referring to */
672 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
673 + ELF64_R_SYM(rel[i].r_info);
674 if (!sym->st_value) {
675 printk(KERN_WARNING "%s: Unknown symbol %s\n",
676 me->name, strtab + sym->st_name);
679 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
680 dot = (Elf64_Addr)loc & ~0x03;
681 loc64 = (Elf64_Xword *)loc;
684 addend = rel[i].r_addend;
687 #define r(t) ELF64_R_TYPE(rel[i].r_info)==t ? #t :
688 printk("Symbol %s loc %p val 0x%Lx addend 0x%Lx: %s\n",
689 strtab + sym->st_name,
701 switch (ELF64_R_TYPE(rel[i].r_info)) {
702 case R_PARISC_LTOFF21L:
703 /* LT-relative; left 21 bits */
704 val = get_got(me, val, addend);
705 pr_debug("LTOFF21L Symbol %s loc %p val %llx\n",
706 strtab + sym->st_name,
709 *loc = mask(*loc, 21) | reassemble_21(val);
711 case R_PARISC_LTOFF14R:
712 /* L(ltoff(val+addend)) */
713 /* LT-relative; right 14 bits */
714 val = get_got(me, val, addend);
716 pr_debug("LTOFF14R Symbol %s loc %p val %llx\n",
717 strtab + sym->st_name,
719 *loc = mask(*loc, 14) | reassemble_14(val);
721 case R_PARISC_PCREL22F:
722 /* PC-relative; 22 bits */
723 pr_debug("PCREL22F Symbol %s loc %p val %llx\n",
724 strtab + sym->st_name,
727 /* can we reach it locally? */
728 if (within_module(val, me)) {
729 /* this is the case where the symbol is local
730 * to the module, but in a different section,
731 * so stub the jump in case it's more than 22
733 val = (val - dot - 8)/4;
734 if (!RELOC_REACHABLE(val, 22)) {
735 /* direct distance too far, create
736 * stub entry instead */
737 val = get_stub(me, sym->st_value,
738 addend, ELF_STUB_DIRECT,
741 /* Ok, we can reach it directly. */
747 if (strncmp(strtab + sym->st_name, "$$", 2)
749 val = get_stub(me, val, addend, ELF_STUB_MILLI,
752 val = get_stub(me, val, addend, ELF_STUB_GOT,
755 pr_debug("STUB FOR %s loc %px, val %llx+%llx at %llx\n",
756 strtab + sym->st_name, loc, sym->st_value,
758 val = (val - dot - 8)/4;
759 CHECK_RELOC(val, 22);
760 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
762 case R_PARISC_PCREL32:
763 /* 32-bit PC relative address */
764 *loc = val - dot - 8 + addend;
766 case R_PARISC_PCREL64:
767 /* 64-bit PC relative address */
768 *loc64 = val - dot - 8 + addend;
771 /* 64-bit effective address */
772 *loc64 = val + addend;
774 case R_PARISC_SEGREL32:
775 /* 32-bit segment relative address */
776 /* See note about special handling of SEGREL32 at
777 * the beginning of this file.
779 *loc = fsel(val, addend);
781 case R_PARISC_SECREL32:
782 /* 32-bit section relative address. */
783 *loc = fsel(val, addend);
785 case R_PARISC_FPTR64:
786 /* 64-bit function address */
787 if (within_module(val + addend, me)) {
788 *loc64 = get_fdesc(me, val+addend);
789 pr_debug("FDESC for %s at %llx points to %llx\n",
790 strtab + sym->st_name, *loc64,
791 ((Elf_Fdesc *)*loc64)->addr);
793 /* if the symbol is not local to this
794 * module then val+addend is a pointer
795 * to the function descriptor */
796 pr_debug("Non local FPTR64 Symbol %s loc %p val %llx\n",
797 strtab + sym->st_name,
799 *loc64 = val + addend;
804 printk(KERN_ERR "module %s: Unknown relocation: %Lu\n",
805 me->name, ELF64_R_TYPE(rel[i].r_info));
814 register_unwind_table(struct module *me,
815 const Elf_Shdr *sechdrs)
817 unsigned char *table, *end;
820 if (!me->arch.unwind_section)
823 table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr;
824 end = table + sechdrs[me->arch.unwind_section].sh_size;
825 gp = (Elf_Addr)me->mem[MOD_TEXT].base + me->arch.got_offset;
827 pr_debug("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n",
828 me->arch.unwind_section, table, end, gp);
829 me->arch.unwind = unwind_table_add(me->name, 0, gp, table, end);
833 deregister_unwind_table(struct module *me)
836 unwind_table_remove(me->arch.unwind);
839 int module_finalize(const Elf_Ehdr *hdr,
840 const Elf_Shdr *sechdrs,
845 const char *strtab = NULL;
848 int symindex __maybe_unused = -1;
849 Elf_Sym *newptr, *oldptr;
850 Elf_Shdr *symhdr = NULL;
855 entry = (Elf_Fdesc *)me->init;
856 printk("FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry,
857 entry->gp, entry->addr);
858 addr = (u32 *)entry->addr;
859 printk("INSNS: %x %x %x %x\n",
860 addr[0], addr[1], addr[2], addr[3]);
861 printk("got entries used %ld, gots max %ld\n"
862 "fdescs used %ld, fdescs max %ld\n",
863 me->arch.got_count, me->arch.got_max,
864 me->arch.fdesc_count, me->arch.fdesc_max);
867 register_unwind_table(me, sechdrs);
869 /* haven't filled in me->symtab yet, so have to find it
871 for (i = 1; i < hdr->e_shnum; i++) {
872 if(sechdrs[i].sh_type == SHT_SYMTAB
873 && (sechdrs[i].sh_flags & SHF_ALLOC)) {
874 int strindex = sechdrs[i].sh_link;
877 * The cast is to drop the const from
878 * the sechdrs pointer */
879 symhdr = (Elf_Shdr *)&sechdrs[i];
880 strtab = (char *)sechdrs[strindex].sh_addr;
885 pr_debug("module %s: strtab %p, symhdr %p\n",
886 me->name, strtab, symhdr);
888 if(me->arch.got_count > MAX_GOTS) {
889 printk(KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d)\n",
890 me->name, me->arch.got_count, MAX_GOTS);
894 kfree(me->arch.section);
895 me->arch.section = NULL;
897 /* no symbol table */
901 oldptr = (void *)symhdr->sh_addr;
902 newptr = oldptr + 1; /* we start counting at 1 */
903 nsyms = symhdr->sh_size / sizeof(Elf_Sym);
904 pr_debug("OLD num_symtab %lu\n", nsyms);
906 for (i = 1; i < nsyms; i++) {
907 oldptr++; /* note, count starts at 1 so preincrement */
908 if(strncmp(strtab + oldptr->st_name,
918 nsyms = newptr - (Elf_Sym *)symhdr->sh_addr;
919 pr_debug("NEW num_symtab %lu\n", nsyms);
920 symhdr->sh_size = nsyms * sizeof(Elf_Sym);
922 /* find .altinstructions section */
923 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
924 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
925 void *aseg = (void *) s->sh_addr;
926 char *secname = secstrings + s->sh_name;
928 if (!strcmp(".altinstructions", secname))
929 /* patch .altinstructions */
930 apply_alternatives(aseg, aseg + s->sh_size, me->name);
932 #ifdef CONFIG_DYNAMIC_FTRACE
933 /* For 32 bit kernels we're compiling modules with
934 * -ffunction-sections so we must relocate the addresses in the
935 * ftrace callsite section.
937 if (symindex != -1 && !strcmp(secname, FTRACE_CALLSITE_SECTION)) {
939 if (s->sh_type == SHT_REL)
940 err = apply_relocate((Elf_Shdr *)sechdrs,
943 else if (s->sh_type == SHT_RELA)
944 err = apply_relocate_add((Elf_Shdr *)sechdrs,
955 void module_arch_cleanup(struct module *mod)
957 deregister_unwind_table(mod);
961 void *dereference_module_function_descriptor(struct module *mod, void *ptr)
963 unsigned long start_opd = (Elf64_Addr)mod->mem[MOD_TEXT].base +
964 mod->arch.fdesc_offset;
965 unsigned long end_opd = start_opd +
966 mod->arch.fdesc_count * sizeof(Elf64_Fdesc);
968 if (ptr < (void *)start_opd || ptr >= (void *)end_opd)
971 return dereference_function_descriptor(ptr);