linux-efi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] Fix cross compiling x86 with clang
@ 2021-03-26  0:04 Nathan Chancellor
  2021-03-26  0:04 ` [PATCH 1/3] x86: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS) Nathan Chancellor
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Nathan Chancellor @ 2021-03-26  0:04 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86, Ard Biesheuvel
  Cc: Nick Desaulniers, linux-efi, linux-kernel, clang-built-linux,
	Nathan Chancellor

Hi all,

This series fixes cross compiling x86 with clang, which was initially
brought up by John here:

https://lore.kernel.org/r/cceb074c-861c-d716-5e19-834a8492f245@gmail.com/

I have picked up that patch and added the same fix in a couple of other
places where it is needed.

I have tested this on an ARM based server with both defconfig and
allmodconfig, where the build now passes.

Please let me know if there are any problems.

Cheers,
Nathan

John Millikin (1):
  x86: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS)

Nathan Chancellor (2):
  x86/boot: Add $(CLANG_FLAGS) to compressed KBUILD_CFLAGS
  efi/libstub: Add $(CLANG_FLAGS) to x86 flags

 arch/x86/Makefile                     | 1 +
 arch/x86/boot/compressed/Makefile     | 1 +
 drivers/firmware/efi/libstub/Makefile | 3 ++-
 3 files changed, 4 insertions(+), 1 deletion(-)


base-commit: 0d02ec6b3136c73c09e7859f0d0e4e2c4c07b49b
-- 
2.31.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/3] x86: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS)
  2021-03-26  0:04 [PATCH 0/3] Fix cross compiling x86 with clang Nathan Chancellor
@ 2021-03-26  0:04 ` Nathan Chancellor
  2021-03-26  8:42   ` Sedat Dilek
  2021-03-26 13:38   ` John Millikin
  2021-03-26  0:04 ` [PATCH 2/3] x86/boot: Add $(CLANG_FLAGS) to compressed KBUILD_CFLAGS Nathan Chancellor
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 7+ messages in thread
From: Nathan Chancellor @ 2021-03-26  0:04 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86, Ard Biesheuvel
  Cc: Nick Desaulniers, linux-efi, linux-kernel, clang-built-linux,
	John Millikin, Sedat Dilek, Nathan Chancellor

From: John Millikin <john@john-millikin.com>

When cross-compiling with Clang, the `$(CLANG_FLAGS)' variable
contains additional flags needed to build C and assembly sources
for the target platform. Normally this variable is automatically
included in `$(KBUILD_CFLAGS)' by via the top-level Makefile.

The x86 real-mode makefile builds `$(REALMODE_CFLAGS)' from a
plain assignment and therefore drops the Clang flags. This causes
Clang to not recognize x86-specific assembler directives:

  arch/x86/realmode/rm/header.S:36:1: error: unknown directive
  .type real_mode_header STT_OBJECT ; .size real_mode_header, .-real_mode_header
  ^

Explicit propagation of `$(CLANG_FLAGS)' to `$(REALMODE_CFLAGS)',
which is inherited by real-mode make rules, fixes cross-compilation
with Clang for x86 targets.

Relevant flags:

* `--target' sets the target architecture when cross-compiling. This
  flag must be set for both compilation and assembly (`KBUILD_AFLAGS')
  to support architecture-specific assembler directives.

* `-no-integrated-as' tells clang to assemble with GNU Assembler
  instead of its built-in LLVM assembler. This flag is set by default
  unless `LLVM_IAS=1' is set, because the LLVM assembler can't yet
  parse certain GNU extensions.

Signed-off-by: John Millikin <john@john-millikin.com>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
 arch/x86/Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 2d6d5a28c3bf..9a73e0cea19c 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -33,6 +33,7 @@ REALMODE_CFLAGS += -ffreestanding
 REALMODE_CFLAGS += -fno-stack-protector
 REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), -Wno-address-of-packed-member)
 REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), $(cc_stack_align4))
+REALMODE_CFLAGS += $(CLANG_FLAGS)
 export REALMODE_CFLAGS
 
 # BITS is used as extension for files which are available in a 32 bit
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/3] x86/boot: Add $(CLANG_FLAGS) to compressed KBUILD_CFLAGS
  2021-03-26  0:04 [PATCH 0/3] Fix cross compiling x86 with clang Nathan Chancellor
  2021-03-26  0:04 ` [PATCH 1/3] x86: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS) Nathan Chancellor
@ 2021-03-26  0:04 ` Nathan Chancellor
  2021-03-26  0:04 ` [PATCH 3/3] efi/libstub: Add $(CLANG_FLAGS) to x86 flags Nathan Chancellor
  2021-03-26  8:43 ` [PATCH 0/3] Fix cross compiling x86 with clang Ard Biesheuvel
  3 siblings, 0 replies; 7+ messages in thread
From: Nathan Chancellor @ 2021-03-26  0:04 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86, Ard Biesheuvel
  Cc: Nick Desaulniers, linux-efi, linux-kernel, clang-built-linux,
	Nathan Chancellor

When cross compiling x86 on an ARM machine with clang, there are several
errors along the lines of:

arch/x86/include/asm/string_64.h:27:10: error: invalid output constraint
'=&c' in asm

This happens because the compressed boot Makefile reassigns KBUILD_CFLAGS
and drops the clang flags that set the target architecture ('--target=')
and the path to the GNU cross tools ('--prefix='), meaning that the host
architecture is targeted.

These flags are available as $(CLANG_FLAGS) from the main Makefile so
add them to the compressed boot folder's KBUILD_CFLAGS so that cross
compiling works as expected.

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
 arch/x86/boot/compressed/Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
index e0bc3988c3fa..6e5522aebbbd 100644
--- a/arch/x86/boot/compressed/Makefile
+++ b/arch/x86/boot/compressed/Makefile
@@ -46,6 +46,7 @@ KBUILD_CFLAGS += -D__DISABLE_EXPORTS
 # Disable relocation relaxation in case the link is not PIE.
 KBUILD_CFLAGS += $(call as-option,-Wa$(comma)-mrelax-relocations=no)
 KBUILD_CFLAGS += -include $(srctree)/include/linux/hidden.h
+KBUILD_CFLAGS += $(CLANG_FLAGS)
 
 # sev-es.c indirectly inludes inat-table.h which is generated during
 # compilation and stored in $(objtree). Add the directory to the includes so
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/3] efi/libstub: Add $(CLANG_FLAGS) to x86 flags
  2021-03-26  0:04 [PATCH 0/3] Fix cross compiling x86 with clang Nathan Chancellor
  2021-03-26  0:04 ` [PATCH 1/3] x86: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS) Nathan Chancellor
  2021-03-26  0:04 ` [PATCH 2/3] x86/boot: Add $(CLANG_FLAGS) to compressed KBUILD_CFLAGS Nathan Chancellor
@ 2021-03-26  0:04 ` Nathan Chancellor
  2021-03-26  8:43 ` [PATCH 0/3] Fix cross compiling x86 with clang Ard Biesheuvel
  3 siblings, 0 replies; 7+ messages in thread
From: Nathan Chancellor @ 2021-03-26  0:04 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86, Ard Biesheuvel
  Cc: Nick Desaulniers, linux-efi, linux-kernel, clang-built-linux,
	Nathan Chancellor

When cross compiling x86 on an ARM machine with clang, there are several
errors along the lines of:

arch/x86/include/asm/page_64.h:52:7: error: invalid output constraint
'=D' in asm

This happens because the x86 flags in the EFI stub are not derived from
KBUILD_CFLAGS like the other architectures are and the clang flags that
set the target architecture ('--target=') and the path to the GNU cross
tools ('--prefix=') are not present, meaning that the host architecture
is targeted.

These flags are available as $(CLANG_FLAGS) from the main Makefile so
add them to the cflags for x86 so that cross compiling works as
expected.

Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
 drivers/firmware/efi/libstub/Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile
index c23466e05e60..d0537573501e 100644
--- a/drivers/firmware/efi/libstub/Makefile
+++ b/drivers/firmware/efi/libstub/Makefile
@@ -13,7 +13,8 @@ cflags-$(CONFIG_X86)		+= -m$(BITS) -D__KERNEL__ \
 				   -Wno-pointer-sign \
 				   $(call cc-disable-warning, address-of-packed-member) \
 				   $(call cc-disable-warning, gnu) \
-				   -fno-asynchronous-unwind-tables
+				   -fno-asynchronous-unwind-tables \
+				   $(CLANG_FLAGS)
 
 # arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly
 # disable the stackleak plugin
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/3] x86: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS)
  2021-03-26  0:04 ` [PATCH 1/3] x86: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS) Nathan Chancellor
@ 2021-03-26  8:42   ` Sedat Dilek
  2021-03-26 13:38   ` John Millikin
  1 sibling, 0 replies; 7+ messages in thread
From: Sedat Dilek @ 2021-03-26  8:42 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	Ard Biesheuvel, Nick Desaulniers, linux-efi, linux-kernel,
	Clang-Built-Linux ML, John Millikin

On Fri, Mar 26, 2021 at 1:04 AM Nathan Chancellor <nathan@kernel.org> wrote:
>
> From: John Millikin <john@john-millikin.com>
>
> When cross-compiling with Clang, the `$(CLANG_FLAGS)' variable
> contains additional flags needed to build C and assembly sources
> for the target platform. Normally this variable is automatically
> included in `$(KBUILD_CFLAGS)' by via the top-level Makefile.
>
> The x86 real-mode makefile builds `$(REALMODE_CFLAGS)' from a
> plain assignment and therefore drops the Clang flags. This causes
> Clang to not recognize x86-specific assembler directives:
>
>   arch/x86/realmode/rm/header.S:36:1: error: unknown directive
>   .type real_mode_header STT_OBJECT ; .size real_mode_header, .-real_mode_header
>   ^
>
> Explicit propagation of `$(CLANG_FLAGS)' to `$(REALMODE_CFLAGS)',
> which is inherited by real-mode make rules, fixes cross-compilation
> with Clang for x86 targets.
>
> Relevant flags:
>
> * `--target' sets the target architecture when cross-compiling. This
>   flag must be set for both compilation and assembly (`KBUILD_AFLAGS')
>   to support architecture-specific assembler directives.
>
> * `-no-integrated-as' tells clang to assemble with GNU Assembler
>   instead of its built-in LLVM assembler. This flag is set by default
>   unless `LLVM_IAS=1' is set, because the LLVM assembler can't yet
>   parse certain GNU extensions.
>
> Signed-off-by: John Millikin <john@john-millikin.com>
> Tested-by: Sedat Dilek <sedat.dilek@gmail.com>
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>

I had John's v2 of this patch in my custom 5.11 patchset.
Thanks for bringing this up again, Nathan.

Later, I will test the triple patchset series.

Feel free to add for this one:

Tested-by: Sedat Dilek <sedat.dilek@gmail.com>

- Sedat -

> ---
>  arch/x86/Makefile | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/arch/x86/Makefile b/arch/x86/Makefile
> index 2d6d5a28c3bf..9a73e0cea19c 100644
> --- a/arch/x86/Makefile
> +++ b/arch/x86/Makefile
> @@ -33,6 +33,7 @@ REALMODE_CFLAGS += -ffreestanding
>  REALMODE_CFLAGS += -fno-stack-protector
>  REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), -Wno-address-of-packed-member)
>  REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), $(cc_stack_align4))
> +REALMODE_CFLAGS += $(CLANG_FLAGS)
>  export REALMODE_CFLAGS
>
>  # BITS is used as extension for files which are available in a 32 bit
> --
> 2.31.0
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/3] Fix cross compiling x86 with clang
  2021-03-26  0:04 [PATCH 0/3] Fix cross compiling x86 with clang Nathan Chancellor
                   ` (2 preceding siblings ...)
  2021-03-26  0:04 ` [PATCH 3/3] efi/libstub: Add $(CLANG_FLAGS) to x86 flags Nathan Chancellor
@ 2021-03-26  8:43 ` Ard Biesheuvel
  3 siblings, 0 replies; 7+ messages in thread
From: Ard Biesheuvel @ 2021-03-26  8:43 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, X86 ML,
	Nick Desaulniers, linux-efi, Linux Kernel Mailing List,
	clang-built-linux

On Fri, 26 Mar 2021 at 01:04, Nathan Chancellor <nathan@kernel.org> wrote:
>
> Hi all,
>
> This series fixes cross compiling x86 with clang, which was initially
> brought up by John here:
>
> https://lore.kernel.org/r/cceb074c-861c-d716-5e19-834a8492f245@gmail.com/
>
> I have picked up that patch and added the same fix in a couple of other
> places where it is needed.
>
> I have tested this on an ARM based server with both defconfig and
> allmodconfig, where the build now passes.
>
> Please let me know if there are any problems.
>
> Cheers,
> Nathan
>
> John Millikin (1):
>   x86: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS)
>
> Nathan Chancellor (2):
>   x86/boot: Add $(CLANG_FLAGS) to compressed KBUILD_CFLAGS
>   efi/libstub: Add $(CLANG_FLAGS) to x86 flags
>

Acked-by: Ard Biesheuvel <ardb@kernel.org>

>  arch/x86/Makefile                     | 1 +
>  arch/x86/boot/compressed/Makefile     | 1 +
>  drivers/firmware/efi/libstub/Makefile | 3 ++-
>  3 files changed, 4 insertions(+), 1 deletion(-)
>
>
> base-commit: 0d02ec6b3136c73c09e7859f0d0e4e2c4c07b49b
> --
> 2.31.0
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/3] x86: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS)
  2021-03-26  0:04 ` [PATCH 1/3] x86: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS) Nathan Chancellor
  2021-03-26  8:42   ` Sedat Dilek
@ 2021-03-26 13:38   ` John Millikin
  1 sibling, 0 replies; 7+ messages in thread
From: John Millikin @ 2021-03-26 13:38 UTC (permalink / raw)
  To: Nathan Chancellor, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	x86, Ard Biesheuvel
  Cc: Nick Desaulniers, linux-efi, linux-kernel, clang-built-linux,
	Sedat Dilek

On 3/26/21 09:04, Nathan Chancellor wrote:
> From: John Millikin <john@john-millikin.com>
> 
> When cross-compiling with Clang, the `$(CLANG_FLAGS)' variable
> contains additional flags needed to build C and assembly sources
> for the target platform. Normally this variable is automatically
> included in `$(KBUILD_CFLAGS)' by via the top-level Makefile.
> 
> The x86 real-mode makefile builds `$(REALMODE_CFLAGS)' from a
> plain assignment and therefore drops the Clang flags. This causes
> Clang to not recognize x86-specific assembler directives:
> 
>   arch/x86/realmode/rm/header.S:36:1: error: unknown directive
>   .type real_mode_header STT_OBJECT ; .size real_mode_header, .-real_mode_header
>   ^
> 
> Explicit propagation of `$(CLANG_FLAGS)' to `$(REALMODE_CFLAGS)',
> which is inherited by real-mode make rules, fixes cross-compilation
> with Clang for x86 targets.
> 
> Relevant flags:
> 
> * `--target' sets the target architecture when cross-compiling. This
>   flag must be set for both compilation and assembly (`KBUILD_AFLAGS')
>   to support architecture-specific assembler directives.
> 
> * `-no-integrated-as' tells clang to assemble with GNU Assembler
>   instead of its built-in LLVM assembler. This flag is set by default
>   unless `LLVM_IAS=1' is set, because the LLVM assembler can't yet
>   parse certain GNU extensions.
> 
> Signed-off-by: John Millikin <john@john-millikin.com>
> Tested-by: Sedat Dilek <sedat.dilek@gmail.com>
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>
> ---
>  arch/x86/Makefile | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/arch/x86/Makefile b/arch/x86/Makefile
> index 2d6d5a28c3bf..9a73e0cea19c 100644
> --- a/arch/x86/Makefile
> +++ b/arch/x86/Makefile
> @@ -33,6 +33,7 @@ REALMODE_CFLAGS += -ffreestanding
>  REALMODE_CFLAGS += -fno-stack-protector
>  REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), -Wno-address-of-packed-member)
>  REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), $(cc_stack_align4))
> +REALMODE_CFLAGS += $(CLANG_FLAGS)
>  export REALMODE_CFLAGS
>  
>  # BITS is used as extension for files which are available in a 32 bit
> 

Just wanted to say "thank you" for picking this up -- I ran into some real-life situations and didn't have time to debug why my patch was failing the Git validity check (presumed to be an email client misconfiguration).

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-03-26 13:39 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-26  0:04 [PATCH 0/3] Fix cross compiling x86 with clang Nathan Chancellor
2021-03-26  0:04 ` [PATCH 1/3] x86: Propagate $(CLANG_FLAGS) to $(REALMODE_FLAGS) Nathan Chancellor
2021-03-26  8:42   ` Sedat Dilek
2021-03-26 13:38   ` John Millikin
2021-03-26  0:04 ` [PATCH 2/3] x86/boot: Add $(CLANG_FLAGS) to compressed KBUILD_CFLAGS Nathan Chancellor
2021-03-26  0:04 ` [PATCH 3/3] efi/libstub: Add $(CLANG_FLAGS) to x86 flags Nathan Chancellor
2021-03-26  8:43 ` [PATCH 0/3] Fix cross compiling x86 with clang Ard Biesheuvel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).