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>
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->init_layout.base;
32 * val -= (uint32_t)me->core_layout.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. */
79 /* three functions to determine where in the module core
80 * or init pieces the location is */
81 static inline int in_init(struct module *me, void *loc)
83 return (loc >= me->init_layout.base &&
84 loc <= (me->init_layout.base + me->init_layout.size));
87 static inline int in_core(struct module *me, void *loc)
89 return (loc >= me->core_layout.base &&
90 loc <= (me->core_layout.base + me->core_layout.size));
93 static inline int in_local(struct module *me, void *loc)
95 return in_init(me, loc) || in_core(me, loc);
104 Elf32_Word insns[2]; /* each stub entry has two insns */
112 Elf64_Word insns[4]; /* each stub entry has four insns */
116 /* Field selection types defined by hppa */
117 #define rnd(x) (((x)+0x1000)&~0x1fff)
118 /* fsel: full 32 bits */
119 #define fsel(v,a) ((v)+(a))
120 /* lsel: select left 21 bits */
121 #define lsel(v,a) (((v)+(a))>>11)
122 /* rsel: select right 11 bits */
123 #define rsel(v,a) (((v)+(a))&0x7ff)
124 /* lrsel with rounding of addend to nearest 8k */
125 #define lrsel(v,a) (((v)+rnd(a))>>11)
126 /* rrsel with rounding of addend to nearest 8k */
127 #define rrsel(v,a) ((((v)+rnd(a))&0x7ff)+((a)-rnd(a)))
129 #define mask(x,sz) ((x) & ~((1<<(sz))-1))
132 /* The reassemble_* functions prepare an immediate value for
133 insertion into an opcode. pa-risc uses all sorts of weird bitfields
134 in the instruction to hold the value. */
135 static inline int sign_unext(int x, int len)
139 len_ones = (1 << len) - 1;
143 static inline int low_sign_unext(int x, int len)
147 sign = (x >> (len-1)) & 1;
148 temp = sign_unext(x, len-1);
149 return (temp << 1) | sign;
152 static inline int reassemble_14(int as14)
154 return (((as14 & 0x1fff) << 1) |
155 ((as14 & 0x2000) >> 13));
158 static inline int reassemble_16a(int as16)
162 /* Unusual 16-bit encoding, for wide mode only. */
163 t = (as16 << 1) & 0xffff;
165 return (t ^ s ^ (s >> 1)) | (s >> 15);
169 static inline int reassemble_17(int as17)
171 return (((as17 & 0x10000) >> 16) |
172 ((as17 & 0x0f800) << 5) |
173 ((as17 & 0x00400) >> 8) |
174 ((as17 & 0x003ff) << 3));
177 static inline int reassemble_21(int as21)
179 return (((as21 & 0x100000) >> 20) |
180 ((as21 & 0x0ffe00) >> 8) |
181 ((as21 & 0x000180) << 7) |
182 ((as21 & 0x00007c) << 14) |
183 ((as21 & 0x000003) << 12));
186 static inline int reassemble_22(int as22)
188 return (((as22 & 0x200000) >> 21) |
189 ((as22 & 0x1f0000) << 5) |
190 ((as22 & 0x00f800) << 5) |
191 ((as22 & 0x000400) >> 8) |
192 ((as22 & 0x0003ff) << 3));
195 void *module_alloc(unsigned long size)
197 /* using RWX means less protection for modules, but it's
198 * easier than trying to map the text, data, init_text and
199 * init_data correctly */
200 return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
202 PAGE_KERNEL_RWX, 0, NUMA_NO_NODE,
203 __builtin_return_address(0));
207 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
212 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
217 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
219 unsigned long cnt = 0;
221 for (; n > 0; n--, rela++)
223 switch (ELF32_R_TYPE(rela->r_info)) {
224 case R_PARISC_PCREL17F:
225 case R_PARISC_PCREL22F:
233 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
235 unsigned long cnt = 0;
237 for (; n > 0; n--, rela++)
239 switch (ELF64_R_TYPE(rela->r_info)) {
240 case R_PARISC_LTOFF21L:
241 case R_PARISC_LTOFF14R:
242 case R_PARISC_PCREL22F:
250 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
252 unsigned long cnt = 0;
254 for (; n > 0; n--, rela++)
256 switch (ELF64_R_TYPE(rela->r_info)) {
257 case R_PARISC_FPTR64:
265 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
267 unsigned long cnt = 0;
269 for (; n > 0; n--, rela++)
271 switch (ELF64_R_TYPE(rela->r_info)) {
272 case R_PARISC_PCREL22F:
281 void module_arch_freeing_init(struct module *mod)
283 kfree(mod->arch.section);
284 mod->arch.section = NULL;
287 /* Additional bytes needed in front of individual sections */
288 unsigned int arch_mod_section_prepend(struct module *mod,
289 unsigned int section)
291 /* size needed for all stubs of this section (including
292 * one additional for correct alignment of the stubs) */
293 return (mod->arch.section[section].stub_entries + 1)
294 * sizeof(struct stub_entry);
298 int module_frob_arch_sections(CONST Elf_Ehdr *hdr,
299 CONST Elf_Shdr *sechdrs,
300 CONST char *secstrings,
303 unsigned long gots = 0, fdescs = 0, len;
306 len = hdr->e_shnum * sizeof(me->arch.section[0]);
307 me->arch.section = kzalloc(len, GFP_KERNEL);
308 if (!me->arch.section)
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;
316 if (strncmp(secstrings + sechdrs[i].sh_name,
317 ".PARISC.unwind", 14) == 0)
318 me->arch.unwind_section = i;
320 if (sechdrs[i].sh_type != SHT_RELA)
323 /* some of these are not relevant for 32-bit/64-bit
324 * we leave them here to make the code common. the
325 * compiler will do its thing and optimize out the
326 * stuff we don't need
328 gots += count_gots(rels, nrels);
329 fdescs += count_fdescs(rels, nrels);
331 /* XXX: By sorting the relocs and finding duplicate entries
332 * we could reduce the number of necessary stubs and save
334 count = count_stubs(rels, nrels);
338 /* so we need relocation stubs. reserve necessary memory. */
339 /* sh_info gives the section for which we need to add stubs. */
340 s = sechdrs[i].sh_info;
342 /* each code section should only have one relocation section */
343 WARN_ON(me->arch.section[s].stub_entries);
345 /* store number of stubs we need for this section */
346 me->arch.section[s].stub_entries += count;
349 /* align things a bit */
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);
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);
358 me->arch.got_max = gots;
359 me->arch.fdesc_max = fdescs;
365 static Elf64_Word get_got(struct module *me, unsigned long value, long addend)
368 struct got_entry *got;
374 got = me->core_layout.base + me->arch.got_offset;
375 for (i = 0; got[i].addr; i++)
376 if (got[i].addr == value)
379 BUG_ON(++me->arch.got_count > me->arch.got_max);
383 pr_debug("GOT ENTRY %d[%lx] val %lx\n", i, i*sizeof(struct got_entry),
385 return i * sizeof(struct got_entry);
387 #endif /* CONFIG_64BIT */
390 static Elf_Addr get_fdesc(struct module *me, unsigned long value)
392 Elf_Fdesc *fdesc = me->core_layout.base + me->arch.fdesc_offset;
395 printk(KERN_ERR "%s: zero OPD requested!\n", me->name);
399 /* Look for existing fdesc entry. */
400 while (fdesc->addr) {
401 if (fdesc->addr == value)
402 return (Elf_Addr)fdesc;
406 BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max);
410 fdesc->gp = (Elf_Addr)me->core_layout.base + me->arch.got_offset;
411 return (Elf_Addr)fdesc;
413 #endif /* CONFIG_64BIT */
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)
424 struct stub_entry *stub;
425 int __maybe_unused d;
427 /* initialize stub_offset to point in front of the section */
428 if (!me->arch.section[targetsec].stub_offset) {
429 loc0 -= (me->arch.section[targetsec].stub_entries + 1) *
430 sizeof(struct stub_entry);
431 /* get correct alignment for the stubs */
432 loc0 = ALIGN(loc0, sizeof(struct stub_entry));
433 me->arch.section[targetsec].stub_offset = loc0;
436 /* get address of stub entry */
437 stub = (void *) me->arch.section[targetsec].stub_offset;
438 me->arch.section[targetsec].stub_offset += sizeof(struct stub_entry);
440 /* do not write outside available stub area */
441 BUG_ON(0 == me->arch.section[targetsec].stub_entries--);
445 /* for 32-bit the stub looks like this:
447 * be,n R'XXX(%sr4,%r1)
449 //value = *(unsigned long *)((value + addend) & ~3); /* why? */
451 stub->insns[0] = 0x20200000; /* ldil L'XXX,%r1 */
452 stub->insns[1] = 0xe0202002; /* be,n R'XXX(%sr4,%r1) */
454 stub->insns[0] |= reassemble_21(lrsel(value, addend));
455 stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4);
458 /* for 64-bit we have three kinds of stubs:
459 * for normal function calls:
471 * for direct branches (jumps between different section of the
479 d = get_got(me, value, addend);
482 stub->insns[0] = 0x0f6010db; /* ldd 0(%dp),%dp */
483 stub->insns[0] |= low_sign_unext(d, 5) << 16;
486 stub->insns[0] = 0x537b0000; /* ldd 0(%dp),%dp */
487 stub->insns[0] |= reassemble_16a(d);
489 stub->insns[1] = 0x53610020; /* ldd 10(%dp),%r1 */
490 stub->insns[2] = 0xe820d000; /* bve (%r1) */
491 stub->insns[3] = 0x537b0030; /* ldd 18(%dp),%dp */
494 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
495 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
496 stub->insns[2] = 0x50210020; /* ldd 10(%r1),%r1 */
497 stub->insns[3] = 0xe820d002; /* bve,n (%r1) */
499 stub->insns[0] |= reassemble_21(lrsel(value, addend));
500 stub->insns[1] |= reassemble_14(rrsel(value, addend));
502 case ELF_STUB_DIRECT:
503 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
504 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
505 stub->insns[2] = 0xe820d002; /* bve,n (%r1) */
507 stub->insns[0] |= reassemble_21(lrsel(value, addend));
508 stub->insns[1] |= reassemble_14(rrsel(value, addend));
514 return (Elf_Addr)stub;
518 int apply_relocate_add(Elf_Shdr *sechdrs,
520 unsigned int symindex,
525 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
532 unsigned int targetsec = sechdrs[relsec].sh_info;
533 //unsigned long dp = (unsigned long)$global$;
534 register unsigned long dp asm ("r27");
536 pr_debug("Applying relocate section %u to %u\n", relsec,
538 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
539 /* This is where to make the change */
540 loc = (void *)sechdrs[targetsec].sh_addr
542 /* This is the start of the target section */
543 loc0 = sechdrs[targetsec].sh_addr;
544 /* This is the symbol it is referring to */
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);
552 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
553 dot = (Elf32_Addr)loc & ~0x03;
556 addend = rel[i].r_addend;
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,
576 switch (ELF32_R_TYPE(rel[i].r_info)) {
577 case R_PARISC_PLABEL32:
578 /* 32-bit function address */
579 /* no function descriptors... */
580 *loc = fsel(val, addend);
583 /* direct 32-bit ref */
584 *loc = fsel(val, addend);
586 case R_PARISC_DIR21L:
587 /* left 21 bits of effective address */
588 val = lrsel(val, addend);
589 *loc = mask(*loc, 21) | reassemble_21(val);
591 case R_PARISC_DIR14R:
592 /* right 14 bits of effective address */
593 val = rrsel(val, addend);
594 *loc = mask(*loc, 14) | reassemble_14(val);
596 case R_PARISC_SEGREL32:
597 /* 32-bit segment relative address */
598 /* See note about special handling of SEGREL32 at
599 * the beginning of this file.
601 *loc = fsel(val, addend);
603 case R_PARISC_SECREL32:
604 /* 32-bit section relative address. */
605 *loc = fsel(val, addend);
607 case R_PARISC_DPREL21L:
608 /* left 21 bit of relative address */
609 val = lrsel(val - dp, addend);
610 *loc = mask(*loc, 21) | reassemble_21(val);
612 case R_PARISC_DPREL14R:
613 /* right 14 bit of relative address */
614 val = rrsel(val - dp, addend);
615 *loc = mask(*loc, 14) | reassemble_14(val);
617 case R_PARISC_PCREL17F:
618 /* 17-bit PC relative address */
619 /* calculate direct call offset */
621 val = (val - dot - 8)/4;
622 if (!RELOC_REACHABLE(val, 17)) {
623 /* direct distance too far, create
624 * stub entry instead */
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);
630 *loc = (*loc & ~0x1f1ffd) | reassemble_17(val);
632 case R_PARISC_PCREL22F:
633 /* 22-bit PC relative address; only defined for pa20 */
634 /* calculate direct call offset */
636 val = (val - dot - 8)/4;
637 if (!RELOC_REACHABLE(val, 22)) {
638 /* direct distance too far, create
639 * stub entry instead */
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);
645 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
647 case R_PARISC_PCREL32:
648 /* 32-bit PC relative address */
649 *loc = val - dot - 8 + addend;
653 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
654 me->name, ELF32_R_TYPE(rel[i].r_info));
663 int apply_relocate_add(Elf_Shdr *sechdrs,
665 unsigned int symindex,
670 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
678 unsigned int targetsec = sechdrs[relsec].sh_info;
680 pr_debug("Applying relocate section %u to %u\n", relsec,
682 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
683 /* This is where to make the change */
684 loc = (void *)sechdrs[targetsec].sh_addr
686 /* This is the start of the target section */
687 loc0 = sechdrs[targetsec].sh_addr;
688 /* This is the symbol it is referring to */
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);
696 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
697 dot = (Elf64_Addr)loc & ~0x03;
698 loc64 = (Elf64_Xword *)loc;
701 addend = rel[i].r_addend;
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,
718 switch (ELF64_R_TYPE(rel[i].r_info)) {
719 case R_PARISC_LTOFF21L:
720 /* LT-relative; left 21 bits */
721 val = get_got(me, val, addend);
722 pr_debug("LTOFF21L Symbol %s loc %p val %llx\n",
723 strtab + sym->st_name,
726 *loc = mask(*loc, 21) | reassemble_21(val);
728 case R_PARISC_LTOFF14R:
729 /* L(ltoff(val+addend)) */
730 /* LT-relative; right 14 bits */
731 val = get_got(me, val, addend);
733 pr_debug("LTOFF14R Symbol %s loc %p val %llx\n",
734 strtab + sym->st_name,
736 *loc = mask(*loc, 14) | reassemble_14(val);
738 case R_PARISC_PCREL22F:
739 /* PC-relative; 22 bits */
740 pr_debug("PCREL22F Symbol %s loc %p val %llx\n",
741 strtab + sym->st_name,
744 /* can we reach it locally? */
745 if (in_local(me, (void *)val)) {
746 /* this is the case where the symbol is local
747 * to the module, but in a different section,
748 * so stub the jump in case it's more than 22
750 val = (val - dot - 8)/4;
751 if (!RELOC_REACHABLE(val, 22)) {
752 /* direct distance too far, create
753 * stub entry instead */
754 val = get_stub(me, sym->st_value,
755 addend, ELF_STUB_DIRECT,
758 /* Ok, we can reach it directly. */
764 if (strncmp(strtab + sym->st_name, "$$", 2)
766 val = get_stub(me, val, addend, ELF_STUB_MILLI,
769 val = get_stub(me, val, addend, ELF_STUB_GOT,
772 pr_debug("STUB FOR %s loc %px, val %llx+%llx at %llx\n",
773 strtab + sym->st_name, loc, sym->st_value,
775 val = (val - dot - 8)/4;
776 CHECK_RELOC(val, 22);
777 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
779 case R_PARISC_PCREL32:
780 /* 32-bit PC relative address */
781 *loc = val - dot - 8 + addend;
783 case R_PARISC_PCREL64:
784 /* 64-bit PC relative address */
785 *loc64 = val - dot - 8 + addend;
788 /* 64-bit effective address */
789 *loc64 = val + addend;
791 case R_PARISC_SEGREL32:
792 /* 32-bit segment relative address */
793 /* See note about special handling of SEGREL32 at
794 * the beginning of this file.
796 *loc = fsel(val, addend);
798 case R_PARISC_SECREL32:
799 /* 32-bit section relative address. */
800 *loc = fsel(val, addend);
802 case R_PARISC_FPTR64:
803 /* 64-bit function address */
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);
810 /* if the symbol is not local to this
811 * module then val+addend is a pointer
812 * to the function descriptor */
813 pr_debug("Non local FPTR64 Symbol %s loc %p val %llx\n",
814 strtab + sym->st_name,
816 *loc64 = val + addend;
821 printk(KERN_ERR "module %s: Unknown relocation: %Lu\n",
822 me->name, ELF64_R_TYPE(rel[i].r_info));
831 register_unwind_table(struct module *me,
832 const Elf_Shdr *sechdrs)
834 unsigned char *table, *end;
837 if (!me->arch.unwind_section)
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;
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);
850 deregister_unwind_table(struct module *me)
853 unwind_table_remove(me->arch.unwind);
856 int module_finalize(const Elf_Ehdr *hdr,
857 const Elf_Shdr *sechdrs,
862 const char *strtab = NULL;
866 Elf_Sym *newptr, *oldptr;
867 Elf_Shdr *symhdr = NULL;
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);
884 register_unwind_table(me, sechdrs);
886 /* haven't filled in me->symtab yet, so have to find it
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;
894 * The cast is to drop the const from
895 * the sechdrs pointer */
896 symhdr = (Elf_Shdr *)&sechdrs[i];
897 strtab = (char *)sechdrs[strindex].sh_addr;
902 pr_debug("module %s: strtab %p, symhdr %p\n",
903 me->name, strtab, symhdr);
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);
911 kfree(me->arch.section);
912 me->arch.section = NULL;
914 /* no symbol table */
918 oldptr = (void *)symhdr->sh_addr;
919 newptr = oldptr + 1; /* we start counting at 1 */
920 nsyms = symhdr->sh_size / sizeof(Elf_Sym);
921 pr_debug("OLD num_symtab %lu\n", nsyms);
923 for (i = 1; i < nsyms; i++) {
924 oldptr++; /* note, count starts at 1 so preincrement */
925 if(strncmp(strtab + oldptr->st_name,
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);
939 /* find .altinstructions section */
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;
945 if (!strcmp(".altinstructions", secname))
946 /* patch .altinstructions */
947 apply_alternatives(aseg, aseg + s->sh_size, me->name);
949 #ifdef CONFIG_DYNAMIC_FTRACE
950 /* For 32 bit kernels we're compiling modules with
951 * -ffunction-sections so we must relocate the addresses in the
952 * ftrace callsite section.
954 if (symindex != -1 && !strcmp(secname, FTRACE_CALLSITE_SECTION)) {
956 if (s->sh_type == SHT_REL)
957 err = apply_relocate((Elf_Shdr *)sechdrs,
960 else if (s->sh_type == SHT_RELA)
961 err = apply_relocate_add((Elf_Shdr *)sechdrs,
972 void module_arch_cleanup(struct module *mod)
974 deregister_unwind_table(mod);
978 void *dereference_module_function_descriptor(struct module *mod, void *ptr)
980 unsigned long start_opd = (Elf64_Addr)mod->core_layout.base +
981 mod->arch.fdesc_offset;
982 unsigned long end_opd = start_opd +
983 mod->arch.fdesc_count * sizeof(Elf64_Fdesc);
985 if (ptr < (void *)start_opd || ptr >= (void *)end_opd)
988 return dereference_function_descriptor(ptr);