Skip to contentSkip to conversation

powerpc/64: ELFv2 conversion

PATCH Applied Nicholas Piggin 2 participants 6 messages Jun 6, 2023 – Jul 3, 2023

Subsystems (2)
lore
Share & tools
lore

Nicholas Piggin

author reviewer
#0 lore raw

v2 cover powerpc/64: ELFv2 conversion

This is a follow on from the series here

https://lore.kernel.org/linuxppc-dev/[email protected]/

With feedback addressed and Christophe's -mprofile-kernel for BE added,

https://lore.kernel.org/linuxppc-dev/[email protected]/

And dropped the last RFC patch that removes ELFv1 entirely. Maybe we can wait one or two releases after this before doing that.

Thanks,
Nick

Nicholas Piggin (4):
powerpc/64: Force ELFv2 when building with LLVM linker powerpc/64: Make ELFv2 the default for big-endian builds powerpc/64: Use -mprofile-kernel for big endian ELFv2 kernels powerpc/64s: Remove support for ELFv1 little endian userspace

 arch/powerpc/Kconfig                            | 13 +++++++------
 arch/powerpc/include/asm/elf.h                  |  6 ++++++
 arch/powerpc/include/asm/thread_info.h          |  6 +++++-
 arch/powerpc/tools/gcc-check-mprofile-kernel.sh | 11 ++++++-----
 4 files changed, 24 insertions(+), 12 deletions(-)
Signature · 1 line
2.40.1
author reviewer
#1 lore raw

PATCH v2 1/4 powerpc/64: Force ELFv2 when building with LLVM linker

The LLVM linker does not support ELFv1 at all, so BE kernels must be built with ELFv2. The LLD version check was added to be conservative, LLD simply fails to link ELFv1 entirely, effectively requiring LLD >= 15 and ELFv2 for BE builds. Instead remove that restriction until proven otherwise (LLD 14.0 links a booting ELFv2 BE vmlinux for me).

The minimum GNU binutils has increased such that ELFv2 is always supported, so remove that check while we're here.

---
 arch/powerpc/Kconfig | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/powerpc/Kconfig +2 -4
index bff5820b7cda..e34ff0e59585 100644
@@ -624,10 +624,10 @@ config ARCH_HAS_KEXEC_PURGATORY
 	def_bool KEXEC_FILE
 
 config PPC64_BIG_ENDIAN_ELF_ABI_V2
-	bool "Build big-endian kernel using ELF ABI V2 (EXPERIMENTAL)"
+	prompt "Build big-endian kernel using ELF ABI V2 (EXPERIMENTAL)" if LD_IS_BFD
+	def_bool y if LD_IS_LLD
 	depends on PPC64 && CPU_BIG_ENDIAN
 	depends on CC_HAS_ELFV2
-	depends on LD_VERSION >= 22400 || LLD_VERSION >= 150000
 	help
 	  This builds the kernel image using the "Power Architecture 64-Bit ELF
 	  V2 ABI Specification", which has a reduced stack overhead and faster
@@ -638,8 +638,6 @@ config PPC64_BIG_ENDIAN_ELF_ABI_V2
 	  it is less well tested by kernel and toolchain. However some distros
 	  build userspace this way, and it can produce a functioning kernel.
 
-	  This requires GCC and binutils 2.24 or newer.
-
 config RELOCATABLE
 	bool "Build a relocatable kernel"
 	depends on PPC64 || (FLATMEM && (44x || PPC_85xx))
Signature · 1 line
2.40.1
author reviewer
#2 lore raw

PATCH v2 2/4 powerpc/64: Make ELFv2 the default for big-endian builds

In reply to Nicholas Piggin

All supported toolchains now support ELFv2 on big-endian, so flip the default on this and hide the option behind EXPERT for the purpose of bug hunting.

---
 arch/powerpc/Kconfig | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/powerpc/Kconfig +4 -2
index e34ff0e59585..296e0448b5fc 100644
@@ -624,8 +624,10 @@ config ARCH_HAS_KEXEC_PURGATORY
 	def_bool KEXEC_FILE
 
 config PPC64_BIG_ENDIAN_ELF_ABI_V2
-	prompt "Build big-endian kernel using ELF ABI V2 (EXPERIMENTAL)" if LD_IS_BFD
-	def_bool y if LD_IS_LLD
+	# Option is available to BFD, but LLD does not support ELFv1 so this is
+	# always true there.
+	prompt "Build big-endian kernel using ELF ABI V2" if LD_IS_BFD && EXPERT
+	def_bool y
 	depends on PPC64 && CPU_BIG_ENDIAN
 	depends on CC_HAS_ELFV2
 	help
Signature · 1 line
2.40.1
author reviewer
#3 lore raw

PATCH v2 3/4 powerpc/64: Use -mprofile-kernel for big endian ELFv2 kernels

In reply to Nicholas Piggin

-mprofile-kernel is an optimised calling convention for mcount that Linux has only implemented with the ELFv2 ABI, so it was disabled for big endian kernels. However it does work with ELFv2 big endian, so let's allow that if the compiler supports it.

---
 arch/powerpc/Kconfig                            |  5 +++--
 arch/powerpc/tools/gcc-check-mprofile-kernel.sh | 11 ++++++-----
 2 files changed, 9 insertions(+), 7 deletions(-)
arch/powerpc/Kconfig +3 -2
index 296e0448b5fc..9db707829638 100644
@@ -547,8 +547,9 @@ config LD_HEAD_STUB_CATCH
 	  If unsure, say "N".
 
 config MPROFILE_KERNEL
-	depends on PPC64 && CPU_LITTLE_ENDIAN && FUNCTION_TRACER
-	def_bool $(success,$(srctree)/arch/powerpc/tools/gcc-check-mprofile-kernel.sh $(CC) -I$(srctree)/include -D__KERNEL__)
+	depends on PPC64_ELF_ABI_V2 && FUNCTION_TRACER
+	def_bool $(success,$(srctree)/arch/powerpc/tools/gcc-check-mprofile-kernel.sh $(CC) -mlittle-endian) if CPU_LITTLE_ENDIAN
+	def_bool $(success,$(srctree)/arch/powerpc/tools/gcc-check-mprofile-kernel.sh $(CC) -mbig-endian) if CPU_BIG_ENDIAN
 
 config HOTPLUG_CPU
 	bool "Support for enabling/disabling CPUs"
arch/powerpc/tools/gcc-check-mprofile-kernel.sh +6 -5
index 137f3376ac2b..a31a56016c09 100755
@@ -7,20 +7,21 @@ set -o pipefail
 # To debug, uncomment the following line
 # set -x
 
-# -mprofile-kernel is only supported on 64le, so this should not be invoked
-# for other targets. Therefore we can pass in -m64 and -mlittle-endian
-# explicitly, to take care of toolchains defaulting to other targets.
+# -mprofile-kernel is only supported on 64-bit, so this should not be invoked
+# for 32-bit. We pass in -m64 explicitly, and -mbig-endian and -mlittle-endian
+# are passed in from Kconfig, which takes care of toolchains defaulting to
+# other targets.
 
 # Test whether the compile option -mprofile-kernel exists and generates
 # profiling code (ie. a call to _mcount()).
 echo "int func() { return 0; }" | \
-    $* -m64 -mlittle-endian -S -x c -O2 -p -mprofile-kernel - -o - \
+    $* -m64 -S -x c -O2 -p -mprofile-kernel - -o - \
     2> /dev/null | grep -q "_mcount"
 
 # Test whether the notrace attribute correctly suppresses calls to _mcount().
 
 echo -e "#include <linux/compiler.h>\nnotrace int func() { return 0; }" | \
-    $* -m64 -mlittle-endian -S -x c -O2 -p -mprofile-kernel - -o - \
+    $* -m64 -S -x c -O2 -p -mprofile-kernel - -o - \
     2> /dev/null | grep -q "_mcount" && \
     exit 1
 
Signature · 1 line
2.40.1
author reviewer
#4 lore raw

PATCH v2 4/4 powerpc/64s: Remove support for ELFv1 little endian userspace

In reply to Nicholas Piggin

ELFv2 was introduced together with little-endian. ELFv1 with LE has never been a thing. The GNU toolchain can create such a beast, but anyone doing that is a maniac who needs to be stopped so I consider this patch a feature.

---
 arch/powerpc/include/asm/elf.h         | 6 ++++++
 arch/powerpc/include/asm/thread_info.h | 6 +++++-
 2 files changed, 11 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/elf.h +6 -0
index 79f1c480b5eb..a26ca097d032 100644
@@ -12,8 +12,14 @@
 
 /*
  * This is used to ensure we don't load something for the wrong architecture.
+ * 64le only supports ELFv2 64-bit binaries (64be supports v1 and v2).
  */
+#if defined(CONFIG_PPC64) && defined(CONFIG_CPU_LITTLE_ENDIAN)
+#define elf_check_arch(x) (((x)->e_machine == ELF_ARCH) && \
+			   (((x)->e_flags & 0x3) == 0x2))
+#else
 #define elf_check_arch(x) ((x)->e_machine == ELF_ARCH)
+#endif
 #define compat_elf_check_arch(x)	((x)->e_machine == EM_PPC)
 
 #define CORE_DUMP_USE_REGSET
arch/powerpc/include/asm/thread_info.h +5 -1
index bf5dde1a4114..bc5d39a835fe 100644
@@ -183,9 +183,13 @@ static inline bool test_thread_local_flags(unsigned int flags)
 #define clear_tsk_compat_task(tsk) do { } while (0)
 #endif
 
-#if defined(CONFIG_PPC64)
+#ifdef CONFIG_PPC64
+#ifdef CONFIG_CPU_BIG_ENDIAN
 #define is_elf2_task() (test_thread_flag(TIF_ELF2ABI))
 #else
+#define is_elf2_task() (1)
+#endif
+#else
 #define is_elf2_task() (0)
 #endif
 
Signature · 1 line
2.40.1
#5 lore raw
In reply to Nicholas Piggin
Quoted 9 lines · Nicholas Piggin

This is a follow on from the series here

https://lore.kernel.org/linuxppc-dev/[email protected]/

With feedback addressed and Christophe's -mprofile-kernel for BE added,

https://lore.kernel.org/linuxppc-dev/[email protected]/

[...]

Applied to powerpc/next.

[1/4] powerpc/64: Force ELFv2 when building with LLVM linker

      https://git.kernel.org/powerpc/c/9d90161ca5c7234e80e14e563d198f322ca0c1d0
[2/4] powerpc/64: Make ELFv2 the default for big-endian builds
      https://git.kernel.org/powerpc/c/8c5fa3b5c4df3d071dab42b04b971df370d99354
[3/4] powerpc/64: Use -mprofile-kernel for big endian ELFv2 kernels
      https://git.kernel.org/powerpc/c/aec0ba7472a7b0c1c293932e7978197f1489a332
[4/4] powerpc/64s: Remove support for ELFv1 little endian userspace
      https://git.kernel.org/powerpc/c/606787fed7268feb256957872586370b56af697a

cheers