* [PATCH 2/2] powerpc/module: Handle R_PPC64_ENTRY relocations
2016-01-12 12:14 [PATCH 1/2] scripts/recordmcount.pl: support data in text section on powerpc Michael Ellerman
@ 2016-01-12 12:14 ` Michael Ellerman
2016-01-13 13:09 ` [2/2] " Michael Ellerman
2016-01-12 15:42 ` [PATCH 1/2] scripts/recordmcount.pl: support data in text section on powerpc Steven Rostedt
2016-01-13 13:09 ` [1/2] " Michael Ellerman
2 siblings, 1 reply; 6+ messages in thread
From: Michael Ellerman @ 2016-01-12 12:14 UTC (permalink / raw)
To: linuxppc-dev; +Cc: Anton Blanchard, amodra, ulrich.weigand, rostedt
From: Ulrich Weigand <ulrich.weigand@de.ibm.com>
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>
---
arch/powerpc/include/uapi/asm/elf.h | 2 ++
arch/powerpc/kernel/module_64.c | 27 +++++++++++++++++++++++++++
2 files changed, 29 insertions(+)
diff --git a/arch/powerpc/include/uapi/asm/elf.h b/arch/powerpc/include/uapi/asm/elf.h
index 59dad113897b..c2d21d11c2d2 100644
--- a/arch/powerpc/include/uapi/asm/elf.h
+++ b/arch/powerpc/include/uapi/asm/elf.h
@@ -295,6 +295,8 @@ do { \
#define R_PPC64_TLSLD 108
#define R_PPC64_TOCSAVE 109
+#define R_PPC64_ENTRY 118
+
#define R_PPC64_REL16 249
#define R_PPC64_REL16_LO 250
#define R_PPC64_REL16_HI 251
diff --git a/arch/powerpc/kernel/module_64.c b/arch/powerpc/kernel/module_64.c
index 68384514506b..59663af9315f 100644
--- a/arch/powerpc/kernel/module_64.c
+++ b/arch/powerpc/kernel/module_64.c
@@ -635,6 +635,33 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,
*/
break;
+ case R_PPC64_ENTRY:
+ /*
+ * Optimize ELFv2 large code model entry point if
+ * the TOC is within 2GB range of current location.
+ */
+ value = my_r2(sechdrs, me) - (unsigned long)location;
+ if (value + 0x80008000 > 0xffffffff)
+ break;
+ /*
+ * Check for the large code model prolog sequence:
+ * ld r2, ...(r12)
+ * add r2, r2, r12
+ */
+ if ((((uint32_t *)location)[0] & ~0xfffc)
+ != 0xe84c0000)
+ break;
+ if (((uint32_t *)location)[1] != 0x7c426214)
+ break;
+ /*
+ * If found, replace it with:
+ * addis r2, r12, (.TOC.-func)@ha
+ * addi r2, r12, (.TOC.-func)@l
+ */
+ ((uint32_t *)location)[0] = 0x3c4c0000 + PPC_HA(value);
+ ((uint32_t *)location)[1] = 0x38420000 + PPC_LO(value);
+ break;
+
case R_PPC64_REL16_HA:
/* Subtract location pointer */
value -= (unsigned long)location;
--
2.5.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] scripts/recordmcount.pl: support data in text section on powerpc
2016-01-12 12:14 [PATCH 1/2] scripts/recordmcount.pl: support data in text section on powerpc Michael Ellerman
2016-01-12 12:14 ` [PATCH 2/2] powerpc/module: Handle R_PPC64_ENTRY relocations Michael Ellerman
@ 2016-01-12 15:42 ` Steven Rostedt
2016-01-12 22:43 ` Michael Ellerman
2016-01-13 13:09 ` [1/2] " Michael Ellerman
2 siblings, 1 reply; 6+ messages in thread
From: Steven Rostedt @ 2016-01-12 15:42 UTC (permalink / raw)
To: Michael Ellerman; +Cc: linuxppc-dev, Anton Blanchard, amodra, ulrich.weigand
On Tue, 12 Jan 2016 23:14:22 +1100
Michael Ellerman <mpe@ellerman.id.au> wrote:
> From: Ulrich Weigand <ulrich.weigand@de.ibm.com>
>
> If a text section starts out with a data blob before the first
> function start label, disassembly parsing doing in recordmcount.pl
> gets confused on powerpc, leading to creation of corrupted module
> objects.
>
> This was not a problem so far since the compiler would never create
> such text sections. However, this has changed with a recent change
> in GCC 6 to support distances of > 2GB between a function and its
> assoicated TOC in the ELFv2 ABI, exposing this problem.
>
> There is already code in recordmcount.pl to handle such data blobs
> on the sparc64 platform. This patch uses the same method to handle
> those on powerpc as well.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Ulrich Weigand <ulrich.weigand@de.ibm.com>
> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
> ---
> scripts/recordmcount.pl | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> Steve can we get an ack for this one, to go via powerpc? cheers
Acked-by: Steven Rostedt <rostedt@goodmis.org>
-- Steve
>
> diff --git a/scripts/recordmcount.pl b/scripts/recordmcount.pl
> index 826470d7f000..96e2486a6fc4 100755
> --- a/scripts/recordmcount.pl
> +++ b/scripts/recordmcount.pl
> @@ -263,7 +263,8 @@ if ($arch eq "x86_64") {
>
> } elsif ($arch eq "powerpc") {
> $local_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\.?\\S+)";
> - $function_regex = "^([0-9a-fA-F]+)\\s+<(\\.?.*?)>:";
> + # See comment in the sparc64 section for why we use '\w'.
> + $function_regex = "^([0-9a-fA-F]+)\\s+<(\\.?\\w*?)>:";
> $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s\\.?_mcount\$";
>
> if ($bits == 64) {
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [1/2] scripts/recordmcount.pl: support data in text section on powerpc
2016-01-12 12:14 [PATCH 1/2] scripts/recordmcount.pl: support data in text section on powerpc Michael Ellerman
2016-01-12 12:14 ` [PATCH 2/2] powerpc/module: Handle R_PPC64_ENTRY relocations Michael Ellerman
2016-01-12 15:42 ` [PATCH 1/2] scripts/recordmcount.pl: support data in text section on powerpc Steven Rostedt
@ 2016-01-13 13:09 ` Michael Ellerman
2 siblings, 0 replies; 6+ messages in thread
From: Michael Ellerman @ 2016-01-13 13:09 UTC (permalink / raw)
To: Michael Ellerman, linuxppc-dev
Cc: ulrich.weigand, Anton Blanchard, rostedt, amodra
On Tue, 2016-12-01 at 12:14:22 UTC, Michael Ellerman wrote:
> From: Ulrich Weigand <ulrich.weigand@de.ibm.com>
>
> If a text section starts out with a data blob before the first
> function start label, disassembly parsing doing in recordmcount.pl
> gets confused on powerpc, leading to creation of corrupted module
> objects.
>
> This was not a problem so far since the compiler would never create
> such text sections. However, this has changed with a recent change
> in GCC 6 to support distances of > 2GB between a function and its
> assoicated TOC in the ELFv2 ABI, exposing this problem.
>
> There is already code in recordmcount.pl to handle such data blobs
> on the sparc64 platform. This patch uses the same method to handle
> those on powerpc as well.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Ulrich Weigand <ulrich.weigand@de.ibm.com>
> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
> Acked-by: Steven Rostedt <rostedt@goodmis.org>
Applied to powerpc next.
https://git.kernel.org/powerpc/c/2e50c4bef77511b42cc226865d
cheers
^ permalink raw reply [flat|nested] 6+ messages in thread