Re: [PATCH] arm64: build with baremetal linker target instead of Linux when available

2018-07-18 Thread Ard Biesheuvel
On 14 July 2018 at 00:30, Olof Johansson  wrote:
> Not all toolchains have the baremetal elf targets, RedHat/Fedora ones
> in particular. So, probe for whether it's available and use the previous
> (linux) targets if it isn't.
>
> Reported-by: Laura Abbott 
> Cc: Paul Kocialkowski 
> Signed-off-by: Olof Johansson 
> ---
>  arch/arm64/Makefile| 9 +
>  scripts/Kbuild.include | 4 ++--
>  2 files changed, 7 insertions(+), 6 deletions(-)
>
> diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
> index e7101b19d590..efe61a2e4b5e 100644
> --- a/arch/arm64/Makefile
> +++ b/arch/arm64/Makefile
> @@ -60,15 +60,16 @@ ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
>  KBUILD_CPPFLAGS+= -mbig-endian
>  CHECKFLAGS += -D__AARCH64EB__
>  AS += -EB
> -# We must use the linux target here, since distributions don't tend to 
> package
> -# the ELF linker scripts with binutils, and this results in a build failure.
> -LDFLAGS+= -EB -maarch64linuxb
> +# Prefer the baremetal ELF build target, but not all toolchains include
> +# it so fall back to the standard linux version if needed.
> +LDFLAGS+= -EB $(call ld-option, -maarch64elfb, 
> -maarch64linuxb)
>  UTS_MACHINE:= aarch64_be
>  else
>  KBUILD_CPPFLAGS+= -mlittle-endian
>  CHECKFLAGS += -D__AARCH64EL__
>  AS += -EL
> -LDFLAGS+= -EL -maarch64linux # See comment above
> +# Same as above, prefer ELF but fall back to linux target if needed.
> +LDFLAGS+= -EL $(call ld-option, -maarch64elf, -maarch64linux)
>  UTS_MACHINE:= aarch64
>  endif
>

Did anyone check if just removing these options altogether would be an option?

The commit that introduced them does not even mention them, but only
mentions adding -mabi=lp64 to the cc/as flags:

commit 3d6a7b99e3fa29b92d6288487e057e0a596bd2b0
Author: Andrew Pinski 
Date:   Mon Sep 18 11:20:20 2017 +0100

arm64: ensure the kernel is compiled for LP64

The kernel needs to be compiled as a LP64 binary for ARM64, even when
using a compiler that defaults to code-generation for the ILP32 ABI.
Consequently, we need to explicitly pass '-mabi=lp64' (supported on
gcc-4.9 and newer).



> diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
> index c8156d61678c..1e13f502b42f 100644
> --- a/scripts/Kbuild.include
> +++ b/scripts/Kbuild.include
> @@ -163,8 +163,8 @@ cc-ldoption = $(call try-run,\
> $(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c 
> /dev/null -o "$$TMP",$(1),$(2))
>
>  # ld-option
> -# Usage: LDFLAGS += $(call ld-option, -X)
> -ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2))
> +# Usage: LDFLAGS += $(call ld-option, -X, -Y)
> +ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2),$(3))
>
>  # ar-option
>  # Usage: KBUILD_ARFLAGS := $(call ar-option,D)
> --
> 2.11.0
>
>
> ___
> linux-arm-kernel mailing list
> linux-arm-ker...@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel


Re: [PATCH] arm64: build with baremetal linker target instead of Linux when available

2018-07-18 Thread Ard Biesheuvel
On 14 July 2018 at 00:30, Olof Johansson  wrote:
> Not all toolchains have the baremetal elf targets, RedHat/Fedora ones
> in particular. So, probe for whether it's available and use the previous
> (linux) targets if it isn't.
>
> Reported-by: Laura Abbott 
> Cc: Paul Kocialkowski 
> Signed-off-by: Olof Johansson 
> ---
>  arch/arm64/Makefile| 9 +
>  scripts/Kbuild.include | 4 ++--
>  2 files changed, 7 insertions(+), 6 deletions(-)
>
> diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
> index e7101b19d590..efe61a2e4b5e 100644
> --- a/arch/arm64/Makefile
> +++ b/arch/arm64/Makefile
> @@ -60,15 +60,16 @@ ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
>  KBUILD_CPPFLAGS+= -mbig-endian
>  CHECKFLAGS += -D__AARCH64EB__
>  AS += -EB
> -# We must use the linux target here, since distributions don't tend to 
> package
> -# the ELF linker scripts with binutils, and this results in a build failure.
> -LDFLAGS+= -EB -maarch64linuxb
> +# Prefer the baremetal ELF build target, but not all toolchains include
> +# it so fall back to the standard linux version if needed.
> +LDFLAGS+= -EB $(call ld-option, -maarch64elfb, 
> -maarch64linuxb)
>  UTS_MACHINE:= aarch64_be
>  else
>  KBUILD_CPPFLAGS+= -mlittle-endian
>  CHECKFLAGS += -D__AARCH64EL__
>  AS += -EL
> -LDFLAGS+= -EL -maarch64linux # See comment above
> +# Same as above, prefer ELF but fall back to linux target if needed.
> +LDFLAGS+= -EL $(call ld-option, -maarch64elf, -maarch64linux)
>  UTS_MACHINE:= aarch64
>  endif
>

Did anyone check if just removing these options altogether would be an option?

The commit that introduced them does not even mention them, but only
mentions adding -mabi=lp64 to the cc/as flags:

commit 3d6a7b99e3fa29b92d6288487e057e0a596bd2b0
Author: Andrew Pinski 
Date:   Mon Sep 18 11:20:20 2017 +0100

arm64: ensure the kernel is compiled for LP64

The kernel needs to be compiled as a LP64 binary for ARM64, even when
using a compiler that defaults to code-generation for the ILP32 ABI.
Consequently, we need to explicitly pass '-mabi=lp64' (supported on
gcc-4.9 and newer).



> diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
> index c8156d61678c..1e13f502b42f 100644
> --- a/scripts/Kbuild.include
> +++ b/scripts/Kbuild.include
> @@ -163,8 +163,8 @@ cc-ldoption = $(call try-run,\
> $(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c 
> /dev/null -o "$$TMP",$(1),$(2))
>
>  # ld-option
> -# Usage: LDFLAGS += $(call ld-option, -X)
> -ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2))
> +# Usage: LDFLAGS += $(call ld-option, -X, -Y)
> +ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2),$(3))
>
>  # ar-option
>  # Usage: KBUILD_ARFLAGS := $(call ar-option,D)
> --
> 2.11.0
>
>
> ___
> linux-arm-kernel mailing list
> linux-arm-ker...@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel


Re: [PATCH] arm64: build with baremetal linker target instead of Linux when available

2018-07-18 Thread Masahiro Yamada
2018-07-14 0:30 GMT+09:00 Olof Johansson :
> Not all toolchains have the baremetal elf targets, RedHat/Fedora ones
> in particular. So, probe for whether it's available and use the previous
> (linux) targets if it isn't.
>
> Reported-by: Laura Abbott 
> Cc: Paul Kocialkowski 
> Signed-off-by: Olof Johansson 
> ---
>  arch/arm64/Makefile| 9 +
>  scripts/Kbuild.include | 4 ++--
>  2 files changed, 7 insertions(+), 6 deletions(-)
>
> diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
> index e7101b19d590..efe61a2e4b5e 100644
> --- a/arch/arm64/Makefile
> +++ b/arch/arm64/Makefile
> @@ -60,15 +60,16 @@ ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
>  KBUILD_CPPFLAGS+= -mbig-endian
>  CHECKFLAGS += -D__AARCH64EB__
>  AS += -EB
> -# We must use the linux target here, since distributions don't tend to 
> package
> -# the ELF linker scripts with binutils, and this results in a build failure.
> -LDFLAGS+= -EB -maarch64linuxb
> +# Prefer the baremetal ELF build target, but not all toolchains include
> +# it so fall back to the standard linux version if needed.
> +LDFLAGS+= -EB $(call ld-option, -maarch64elfb, 
> -maarch64linuxb)
>  UTS_MACHINE:= aarch64_be
>  else
>  KBUILD_CPPFLAGS+= -mlittle-endian
>  CHECKFLAGS += -D__AARCH64EL__
>  AS += -EL
> -LDFLAGS+= -EL -maarch64linux # See comment above
> +# Same as above, prefer ELF but fall back to linux target if needed.
> +LDFLAGS+= -EL $(call ld-option, -maarch64elf, -maarch64linux)
>  UTS_MACHINE:= aarch64
>  endif
>
> diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
> index c8156d61678c..1e13f502b42f 100644
> --- a/scripts/Kbuild.include
> +++ b/scripts/Kbuild.include
> @@ -163,8 +163,8 @@ cc-ldoption = $(call try-run,\
> $(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c 
> /dev/null -o "$$TMP",$(1),$(2))
>
>  # ld-option
> -# Usage: LDFLAGS += $(call ld-option, -X)
> -ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2))
> +# Usage: LDFLAGS += $(call ld-option, -X, -Y)
> +ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2),$(3))
>
>  # ar-option
>  # Usage: KBUILD_ARFLAGS := $(call ar-option,D)


I guess this patch will go through the arm64 tree.

I am fine with the change of scripts/Kbuild.include

Acked-by: Masahiro Yamada 

-- 
Best Regards
Masahiro Yamada


Re: [PATCH] arm64: build with baremetal linker target instead of Linux when available

2018-07-18 Thread Masahiro Yamada
2018-07-14 0:30 GMT+09:00 Olof Johansson :
> Not all toolchains have the baremetal elf targets, RedHat/Fedora ones
> in particular. So, probe for whether it's available and use the previous
> (linux) targets if it isn't.
>
> Reported-by: Laura Abbott 
> Cc: Paul Kocialkowski 
> Signed-off-by: Olof Johansson 
> ---
>  arch/arm64/Makefile| 9 +
>  scripts/Kbuild.include | 4 ++--
>  2 files changed, 7 insertions(+), 6 deletions(-)
>
> diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
> index e7101b19d590..efe61a2e4b5e 100644
> --- a/arch/arm64/Makefile
> +++ b/arch/arm64/Makefile
> @@ -60,15 +60,16 @@ ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
>  KBUILD_CPPFLAGS+= -mbig-endian
>  CHECKFLAGS += -D__AARCH64EB__
>  AS += -EB
> -# We must use the linux target here, since distributions don't tend to 
> package
> -# the ELF linker scripts with binutils, and this results in a build failure.
> -LDFLAGS+= -EB -maarch64linuxb
> +# Prefer the baremetal ELF build target, but not all toolchains include
> +# it so fall back to the standard linux version if needed.
> +LDFLAGS+= -EB $(call ld-option, -maarch64elfb, 
> -maarch64linuxb)
>  UTS_MACHINE:= aarch64_be
>  else
>  KBUILD_CPPFLAGS+= -mlittle-endian
>  CHECKFLAGS += -D__AARCH64EL__
>  AS += -EL
> -LDFLAGS+= -EL -maarch64linux # See comment above
> +# Same as above, prefer ELF but fall back to linux target if needed.
> +LDFLAGS+= -EL $(call ld-option, -maarch64elf, -maarch64linux)
>  UTS_MACHINE:= aarch64
>  endif
>
> diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
> index c8156d61678c..1e13f502b42f 100644
> --- a/scripts/Kbuild.include
> +++ b/scripts/Kbuild.include
> @@ -163,8 +163,8 @@ cc-ldoption = $(call try-run,\
> $(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c 
> /dev/null -o "$$TMP",$(1),$(2))
>
>  # ld-option
> -# Usage: LDFLAGS += $(call ld-option, -X)
> -ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2))
> +# Usage: LDFLAGS += $(call ld-option, -X, -Y)
> +ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2),$(3))
>
>  # ar-option
>  # Usage: KBUILD_ARFLAGS := $(call ar-option,D)


I guess this patch will go through the arm64 tree.

I am fine with the change of scripts/Kbuild.include

Acked-by: Masahiro Yamada 

-- 
Best Regards
Masahiro Yamada


Re: [PATCH] arm64: build with baremetal linker target instead of Linux when available

2018-07-13 Thread Olof Johansson
On Fri, Jul 13, 2018 at 12:21 PM, Laura Abbott  wrote:
> On 07/13/2018 08:30 AM, Olof Johansson wrote:
>>
>> Not all toolchains have the baremetal elf targets, RedHat/Fedora ones
>> in particular. So, probe for whether it's available and use the previous
>> (linux) targets if it isn't.
>>
>
>
> For the Fedora toolchains:
>
> Tested-by: Laura Abbott 

Thanks!


-Olof


Re: [PATCH] arm64: build with baremetal linker target instead of Linux when available

2018-07-13 Thread Olof Johansson
On Fri, Jul 13, 2018 at 12:21 PM, Laura Abbott  wrote:
> On 07/13/2018 08:30 AM, Olof Johansson wrote:
>>
>> Not all toolchains have the baremetal elf targets, RedHat/Fedora ones
>> in particular. So, probe for whether it's available and use the previous
>> (linux) targets if it isn't.
>>
>
>
> For the Fedora toolchains:
>
> Tested-by: Laura Abbott 

Thanks!


-Olof


Re: [PATCH] arm64: build with baremetal linker target instead of Linux when available

2018-07-13 Thread Laura Abbott

On 07/13/2018 08:30 AM, Olof Johansson wrote:

Not all toolchains have the baremetal elf targets, RedHat/Fedora ones
in particular. So, probe for whether it's available and use the previous
(linux) targets if it isn't.




For the Fedora toolchains:

Tested-by: Laura Abbott 


Reported-by: Laura Abbott 
Cc: Paul Kocialkowski 
Signed-off-by: Olof Johansson 
---
  arch/arm64/Makefile| 9 +
  scripts/Kbuild.include | 4 ++--
  2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index e7101b19d590..efe61a2e4b5e 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -60,15 +60,16 @@ ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
  KBUILD_CPPFLAGS   += -mbig-endian
  CHECKFLAGS+= -D__AARCH64EB__
  AS+= -EB
-# We must use the linux target here, since distributions don't tend to package
-# the ELF linker scripts with binutils, and this results in a build failure.
-LDFLAGS+= -EB -maarch64linuxb
+# Prefer the baremetal ELF build target, but not all toolchains include
+# it so fall back to the standard linux version if needed.
+LDFLAGS+= -EB $(call ld-option, -maarch64elfb, -maarch64linuxb)
  UTS_MACHINE   := aarch64_be
  else
  KBUILD_CPPFLAGS   += -mlittle-endian
  CHECKFLAGS+= -D__AARCH64EL__
  AS+= -EL
-LDFLAGS+= -EL -maarch64linux # See comment above
+# Same as above, prefer ELF but fall back to linux target if needed.
+LDFLAGS+= -EL $(call ld-option, -maarch64elf, -maarch64linux)
  UTS_MACHINE   := aarch64
  endif
  
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include

index c8156d61678c..1e13f502b42f 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -163,8 +163,8 @@ cc-ldoption = $(call try-run,\
$(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c /dev/null -o 
"$$TMP",$(1),$(2))
  
  # ld-option

-# Usage: LDFLAGS += $(call ld-option, -X)
-ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2))
+# Usage: LDFLAGS += $(call ld-option, -X, -Y)
+ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2),$(3))
  
  # ar-option

  # Usage: KBUILD_ARFLAGS := $(call ar-option,D)





Re: [PATCH] arm64: build with baremetal linker target instead of Linux when available

2018-07-13 Thread Laura Abbott

On 07/13/2018 08:30 AM, Olof Johansson wrote:

Not all toolchains have the baremetal elf targets, RedHat/Fedora ones
in particular. So, probe for whether it's available and use the previous
(linux) targets if it isn't.




For the Fedora toolchains:

Tested-by: Laura Abbott 


Reported-by: Laura Abbott 
Cc: Paul Kocialkowski 
Signed-off-by: Olof Johansson 
---
  arch/arm64/Makefile| 9 +
  scripts/Kbuild.include | 4 ++--
  2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index e7101b19d590..efe61a2e4b5e 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -60,15 +60,16 @@ ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
  KBUILD_CPPFLAGS   += -mbig-endian
  CHECKFLAGS+= -D__AARCH64EB__
  AS+= -EB
-# We must use the linux target here, since distributions don't tend to package
-# the ELF linker scripts with binutils, and this results in a build failure.
-LDFLAGS+= -EB -maarch64linuxb
+# Prefer the baremetal ELF build target, but not all toolchains include
+# it so fall back to the standard linux version if needed.
+LDFLAGS+= -EB $(call ld-option, -maarch64elfb, -maarch64linuxb)
  UTS_MACHINE   := aarch64_be
  else
  KBUILD_CPPFLAGS   += -mlittle-endian
  CHECKFLAGS+= -D__AARCH64EL__
  AS+= -EL
-LDFLAGS+= -EL -maarch64linux # See comment above
+# Same as above, prefer ELF but fall back to linux target if needed.
+LDFLAGS+= -EL $(call ld-option, -maarch64elf, -maarch64linux)
  UTS_MACHINE   := aarch64
  endif
  
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include

index c8156d61678c..1e13f502b42f 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -163,8 +163,8 @@ cc-ldoption = $(call try-run,\
$(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c /dev/null -o 
"$$TMP",$(1),$(2))
  
  # ld-option

-# Usage: LDFLAGS += $(call ld-option, -X)
-ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2))
+# Usage: LDFLAGS += $(call ld-option, -X, -Y)
+ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2),$(3))
  
  # ar-option

  # Usage: KBUILD_ARFLAGS := $(call ar-option,D)





[PATCH] arm64: build with baremetal linker target instead of Linux when available

2018-07-13 Thread Olof Johansson
Not all toolchains have the baremetal elf targets, RedHat/Fedora ones
in particular. So, probe for whether it's available and use the previous
(linux) targets if it isn't.

Reported-by: Laura Abbott 
Cc: Paul Kocialkowski 
Signed-off-by: Olof Johansson 
---
 arch/arm64/Makefile| 9 +
 scripts/Kbuild.include | 4 ++--
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index e7101b19d590..efe61a2e4b5e 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -60,15 +60,16 @@ ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
 KBUILD_CPPFLAGS+= -mbig-endian
 CHECKFLAGS += -D__AARCH64EB__
 AS += -EB
-# We must use the linux target here, since distributions don't tend to package
-# the ELF linker scripts with binutils, and this results in a build failure.
-LDFLAGS+= -EB -maarch64linuxb
+# Prefer the baremetal ELF build target, but not all toolchains include
+# it so fall back to the standard linux version if needed.
+LDFLAGS+= -EB $(call ld-option, -maarch64elfb, -maarch64linuxb)
 UTS_MACHINE:= aarch64_be
 else
 KBUILD_CPPFLAGS+= -mlittle-endian
 CHECKFLAGS += -D__AARCH64EL__
 AS += -EL
-LDFLAGS+= -EL -maarch64linux # See comment above
+# Same as above, prefer ELF but fall back to linux target if needed.
+LDFLAGS+= -EL $(call ld-option, -maarch64elf, -maarch64linux)
 UTS_MACHINE:= aarch64
 endif
 
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index c8156d61678c..1e13f502b42f 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -163,8 +163,8 @@ cc-ldoption = $(call try-run,\
$(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c 
/dev/null -o "$$TMP",$(1),$(2))
 
 # ld-option
-# Usage: LDFLAGS += $(call ld-option, -X)
-ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2))
+# Usage: LDFLAGS += $(call ld-option, -X, -Y)
+ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2),$(3))
 
 # ar-option
 # Usage: KBUILD_ARFLAGS := $(call ar-option,D)
-- 
2.11.0



[PATCH] arm64: build with baremetal linker target instead of Linux when available

2018-07-13 Thread Olof Johansson
Not all toolchains have the baremetal elf targets, RedHat/Fedora ones
in particular. So, probe for whether it's available and use the previous
(linux) targets if it isn't.

Reported-by: Laura Abbott 
Cc: Paul Kocialkowski 
Signed-off-by: Olof Johansson 
---
 arch/arm64/Makefile| 9 +
 scripts/Kbuild.include | 4 ++--
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile
index e7101b19d590..efe61a2e4b5e 100644
--- a/arch/arm64/Makefile
+++ b/arch/arm64/Makefile
@@ -60,15 +60,16 @@ ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
 KBUILD_CPPFLAGS+= -mbig-endian
 CHECKFLAGS += -D__AARCH64EB__
 AS += -EB
-# We must use the linux target here, since distributions don't tend to package
-# the ELF linker scripts with binutils, and this results in a build failure.
-LDFLAGS+= -EB -maarch64linuxb
+# Prefer the baremetal ELF build target, but not all toolchains include
+# it so fall back to the standard linux version if needed.
+LDFLAGS+= -EB $(call ld-option, -maarch64elfb, -maarch64linuxb)
 UTS_MACHINE:= aarch64_be
 else
 KBUILD_CPPFLAGS+= -mlittle-endian
 CHECKFLAGS += -D__AARCH64EL__
 AS += -EL
-LDFLAGS+= -EL -maarch64linux # See comment above
+# Same as above, prefer ELF but fall back to linux target if needed.
+LDFLAGS+= -EL $(call ld-option, -maarch64elf, -maarch64linux)
 UTS_MACHINE:= aarch64
 endif
 
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index c8156d61678c..1e13f502b42f 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -163,8 +163,8 @@ cc-ldoption = $(call try-run,\
$(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c 
/dev/null -o "$$TMP",$(1),$(2))
 
 # ld-option
-# Usage: LDFLAGS += $(call ld-option, -X)
-ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2))
+# Usage: LDFLAGS += $(call ld-option, -X, -Y)
+ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2),$(3))
 
 # ar-option
 # Usage: KBUILD_ARFLAGS := $(call ar-option,D)
-- 
2.11.0