Skip to content

Commit a61674b

Browse files
uweigandmpe
authored andcommitted
powerpc/module: Handle R_PPC64_ENTRY relocations
GCC 6 will include changes to generated code with -mcmodel=large, which is used to build kernel modules on powerpc64le. This was necessary because the large model is supposed to allow arbitrary sizes and locations of the code and data sections, but the ELFv2 global entry point prolog still made the unconditional assumption that the TOC associated with any particular function can be found within 2 GB of the function entry point: func: addis r2,r12,(.TOC.-func)@ha addi r2,r2,(.TOC.-func)@l .localentry func, .-func To remove this assumption, GCC will now generate instead this global entry point prolog sequence when using -mcmodel=large: .quad .TOC.-func func: .reloc ., R_PPC64_ENTRY ld r2, -8(r12) add r2, r2, r12 .localentry func, .-func The new .reloc triggers an optimization in the linker that will replace this new prolog with the original code (see above) if the linker determines that the distance between .TOC. and func is in range after all. Since this new relocation is now present in module object files, the kernel module loader is required to handle them too. This patch adds support for the new relocation and implements the same optimization done by the GNU linker. Cc: stable@vger.kernel.org Signed-off-by: Ulrich Weigand <ulrich.weigand@de.ibm.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent 2e50c4b commit a61674b

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎arch/powerpc/include/uapi/asm/elf.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ do { \
295295
#define R_PPC64_TLSLD 108
296296
#define R_PPC64_TOCSAVE 109
297297

298+
#define R_PPC64_ENTRY 118
299+
298300
#define R_PPC64_REL16 249
299301
#define R_PPC64_REL16_LO 250
300302
#define R_PPC64_REL16_HI 251

‎arch/powerpc/kernel/module_64.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,33 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
635635
*/
636636
break;
637637

638+
case R_PPC64_ENTRY:
639+
/*
640+
* Optimize ELFv2 large code model entry point if
641+
* the TOC is within 2GB range of current location.
642+
*/
643+
value = my_r2(sechdrs, me) - (unsigned long)location;
644+
if (value + 0x80008000 > 0xffffffff)
645+
break;
646+
/*
647+
* Check for the large code model prolog sequence:
648+
* ld r2, ...(r12)
649+
* add r2, r2, r12
650+
*/
651+
if ((((uint32_t *)location)[0] & ~0xfffc)
652+
!= 0xe84c0000)
653+
break;
654+
if (((uint32_t *)location)[1] != 0x7c426214)
655+
break;
656+
/*
657+
* If found, replace it with:
658+
* addis r2, r12, (.TOC.-func)@ha
659+
* addi r2, r12, (.TOC.-func)@l
660+
*/
661+
((uint32_t *)location)[0] = 0x3c4c0000 + PPC_HA(value);
662+
((uint32_t *)location)[1] = 0x38420000 + PPC_LO(value);
663+
break;
664+
638665
case R_PPC64_REL16_HA:
639666
/* Subtract location pointer */
640667
value -= (unsigned long)location;

0 commit comments

Comments
 (0)