Skip to content

Commit 38b10b2

Browse files
xen0nchenhuacai
authored andcommitted
LoongArch: Tweak CFLAGS for Clang compatibility
Now the arch code is mostly ready for LLVM/Clang consumption, it is time to re-organize the CFLAGS a little to actually enable the LLVM build. Namely, all -G0 switches from CFLAGS are removed, and -mexplicit-relocs and -mdirect-extern-access are now wrapped with cc-option (with the related asm/percpu.h definition guarded against toolchain combos that are known to not work). A build with !RELOCATABLE && !MODULE is confirmed working within a QEMU environment; support for the two features are currently blocked on LLVM/Clang, and will come later. Why -G0 can be removed: In GCC, -G stands for "small data threshold", that instructs the compiler to put data smaller than the specified threshold in a dedicated "small data" section (called .sdata on LoongArch and several other arches). However, benefiting from this would require ABI cooperation, which is not the case for LoongArch; and current GCC behave the same whether -G0 (equal to disabling this optimization) is given or not. So, remove -G0 from CFLAGS altogether for one less thing to care about. This also benefits LLVM/Clang compatibility where the -G switch is not supported. Why -mexplicit-relocs can now be conditionally applied without regressions: Originally -mexplicit-relocs is unconditionally added to CFLAGS in case of CONFIG_AS_HAS_EXPLICIT_RELOCS, because not having it (i.e. old GCC + new binutils) would not work: modules will have R_LARCH_ABS_* relocs inside, but given the rarity of such toolchain combo in the wild, it may not be worthwhile to support it, so support for such relocs in modules were not added back when explicit relocs support was upstreamed, and -mexplicit-relocs is unconditionally added to fail the build early. Now that Clang compatibility is desired, given Clang is behaving like -mexplicit-relocs from day one but without support for the CLI flag, we must ensure the flag is not passed in case of Clang. However, explicit compiler flavor checks can be more brittle than feature detection: in this case what actually matters is support for __attribute__((model)) when building modules. Given neither older GCC nor current Clang support this attribute, probing for the attribute support and #error'ing out would allow proper UX without checking for Clang, and also automatically work when Clang support for the attribute is to be added in the future. Why -mdirect-extern-access is now conditionally applied: This is actually a nice-to-have optimization that can reduce GOT accesses, but not having it is harmless either. Because Clang does not support the option currently, but might do so in the future, conditional application via cc-option ensures compatibility with both current and future Clang versions. Suggested-by: Xi Ruoyao <[email protected]> # cc-option changes Signed-off-by: WANG Xuerui <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent 83d8b38 commit 38b10b2

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

arch/loongarch/Makefile

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ ld-emul = $(64bit-emul)
4646
cflags-y += -mabi=lp64s
4747
endif
4848

49-
cflags-y += -G0 -pipe -msoft-float
50-
LDFLAGS_vmlinux += -G0 -static -n -nostdlib
49+
cflags-y += -pipe -msoft-float
50+
LDFLAGS_vmlinux += -static -n -nostdlib
5151

5252
# When the assembler supports explicit relocation hint, we must use it.
5353
# GCC may have -mexplicit-relocs off by default if it was built with an old
@@ -56,13 +56,18 @@ LDFLAGS_vmlinux += -G0 -static -n -nostdlib
5656
# When the assembler does not supports explicit relocation hint, we can't use
5757
# it. Disable it if the compiler supports it.
5858
#
59-
# If you've seen "unknown reloc hint" message building the kernel and you are
60-
# now wondering why "-mexplicit-relocs" is not wrapped with cc-option: the
61-
# combination of a "new" assembler and "old" compiler is not supported. Either
62-
# upgrade the compiler or downgrade the assembler.
59+
# The combination of a "new" assembler and "old" GCC is not supported, given
60+
# the rarity of this combo and the extra complexity needed to make it work.
61+
# Either upgrade the compiler or downgrade the assembler; the build will error
62+
# out if it is the case (by probing for the model attribute; all supported
63+
# compilers in this case would have support).
64+
#
65+
# Also, -mdirect-extern-access is useful in case of building with explicit
66+
# relocs, for avoiding unnecessary GOT accesses. It is harmless to not have
67+
# support though.
6368
ifdef CONFIG_AS_HAS_EXPLICIT_RELOCS
64-
cflags-y += -mexplicit-relocs
65-
KBUILD_CFLAGS_KERNEL += -mdirect-extern-access
69+
cflags-y += $(call cc-option,-mexplicit-relocs)
70+
KBUILD_CFLAGS_KERNEL += $(call cc-option,-mdirect-extern-access)
6671
else
6772
cflags-y += $(call cc-option,-mno-explicit-relocs)
6873
KBUILD_AFLAGS_KERNEL += -Wa,-mla-global-with-pcrel

arch/loongarch/include/asm/percpu.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
* loaded. Tell the compiler this fact when using explicit relocs.
1515
*/
1616
#if defined(MODULE) && defined(CONFIG_AS_HAS_EXPLICIT_RELOCS)
17-
#define PER_CPU_ATTRIBUTES __attribute__((model("extreme")))
17+
# if __has_attribute(model)
18+
# define PER_CPU_ATTRIBUTES __attribute__((model("extreme")))
19+
# else
20+
# error compiler support for the model attribute is necessary when a recent assembler is used
21+
# endif
1822
#endif
1923

2024
/* Use r21 for fast access */

arch/loongarch/vdso/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ endif
2121
cflags-vdso := $(ccflags-vdso) \
2222
-isystem $(shell $(CC) -print-file-name=include) \
2323
$(filter -W%,$(filter-out -Wa$(comma)%,$(KBUILD_CFLAGS))) \
24-
-O2 -g -fno-strict-aliasing -fno-common -fno-builtin -G0 \
24+
-O2 -g -fno-strict-aliasing -fno-common -fno-builtin \
2525
-fno-stack-protector -fno-jump-tables -DDISABLE_BRANCH_PROFILING \
2626
$(call cc-option, -fno-asynchronous-unwind-tables) \
2727
$(call cc-option, -fno-stack-protector)

0 commit comments

Comments
 (0)