Skip to contentSkip to conversation

init/Kconfig: add config support for detecting linker

PATCH In review [email protected] 6 participants 23 messages Feb 11, 2019 – Apr 7, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 init/Kconfig: add config support for detecting linker

Similar to how we differentiate between CONFIG_CC_IS_GCC and CONFIG_CC_IS_CLANG, add CONFIG_LD_IS_BFD, CONFIG_LD_IS_GOLD, and CONFIG_LD_IS_LLD.

This simiplifies patches to Makefiles that need to do different things for different linkers.

---
Changes V1->V2:
* Add suggested, reviewed, and tested by tags.

 init/Kconfig | 9 +++++++++
 1 file changed, 9 insertions(+)
init/Kconfig +9 -0
index c9386a365eea..b6046dcf7794 100644
@@ -26,6 +26,15 @@ config CLANG_VERSION
 config CC_HAS_ASM_GOTO
 	def_bool $(success,$(srctree)/scripts/gcc-goto.sh $(CC))
 
+config LD_IS_BFD
+	def_bool $(success,$(LD) --version | head -n 1 | grep -q 'GNU ld')
+
+config LD_IS_GOLD
+	def_bool $(success,$(LD) --version | head -n 1 | grep -q 'GNU gold')
+
+config LD_IS_LLD
+	def_bool $(success,$(LD) --version | head -n 1 | grep -q 'LLD')
+
 config CONSTRUCTORS
 	bool
 	depends on !UML
Signature · 1 line
2.20.1.791.gb4d0f1c61a-goog
#1 lore raw

PATCH v2 2/4 Makefile: clang: choose GCC_TOOLCHAIN_DIR not on LD

This causes an issue when trying to build with make LD=ld.lld if ld.lld and the rest of your cross tools aren't in the same directory (ex. /usr/local/bin) (as is the case for Android's build system), as the GCC_TOOLCHAIN_DIR then gets set based on which $(LD) which will point where LLVM tools are, not GCC/binutils tools are located.

Instead, select the GCC_TOOLCHAIN_DIR based on another tool provided by binutils for which LLVM does not provide a substitute for, such as elfedit.

Fixes commit 785f11aa595b ("kbuild: Add better clang cross build support")

---
Changes V1->V2:
* add reviewed and tested tags.

 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
index 86cf35d1d79d..d3b65e96d183 100644
@@ -492,7 +492,7 @@ endif
 ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),)
 ifneq ($(CROSS_COMPILE),)
 CLANG_FLAGS	:= --target=$(notdir $(CROSS_COMPILE:%-=%))
-GCC_TOOLCHAIN_DIR := $(dir $(shell which $(LD)))
+GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)elfedit))
 CLANG_FLAGS	+= --prefix=$(GCC_TOOLCHAIN_DIR)
 GCC_TOOLCHAIN	:= $(realpath $(GCC_TOOLCHAIN_DIR)/..)
 endif
Signature · 1 line
2.20.1.791.gb4d0f1c61a-goog
#2 lore raw

Re: [PATCH v2 2/4] Makefile: clang: choose GCC_TOOLCHAIN_DIR not on LD

Quoted 11 lines · On Tue, Feb 12, 2019 at 5:42 AM <[email protected]> wrote:

This causes an issue when trying to build with make LD=ld.lld if ld.lld and the rest of your cross tools aren't in the same directory (ex. /usr/local/bin) (as is the case for Android's build system), as the GCC_TOOLCHAIN_DIR then gets set based on which $(LD) which will point where LLVM tools are, not GCC/binutils tools are located.

Instead, select the GCC_TOOLCHAIN_DIR based on another tool provided by binutils for which LLVM does not provide a substitute for, such as elfedit.

Quoted 8 lines

Fixes commit 785f11aa595b ("kbuild: Add better clang cross build support")

---

This one looks correct to me.

Applied to linux-kbuild.

Quoted 20 lines

Changes V1->V2:
* add reviewed and tested tags.

 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -2
index 86cf35d1d79d..d3b65e96d183 100644
@@ -492,7 +492,7 @@ endif
 ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),)
 ifneq ($(CROSS_COMPILE),)
 CLANG_FLAGS    := --target=$(notdir $(CROSS_COMPILE:%-=%))
-GCC_TOOLCHAIN_DIR := $(dir $(shell which $(LD)))
+GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)elfedit))
 CLANG_FLAGS    += --prefix=$(GCC_TOOLCHAIN_DIR)
 GCC_TOOLCHAIN  := $(realpath $(GCC_TOOLCHAIN_DIR)/..)
 endif
--
Quoted 2 lines

2.20.1.791.gb4d0f1c61a-goog

--
Best Regards
Masahiro Yamada

#3 lore raw

PATCH v2 3/4 Makefile: lld: tell clang to use lld

In reply to [email protected]

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld=lld. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

---
Changes V1->V2:
* add reviewed and tested by tags.
* move this addition up 2 statments so that it's properly added to KBUILD_*FLAGS as per Nathan.

 Makefile | 3 +++
 1 file changed, 3 insertions(+)
Makefile +3 -0
index d3b65e96d183..00e8e01d23fc 100644
@@ -500,6 +500,9 @@ ifneq ($(GCC_TOOLCHAIN),)
 CLANG_FLAGS	+= --gcc-toolchain=$(GCC_TOOLCHAIN)
 endif
 CLANG_FLAGS	+= -no-integrated-as
+ifneq ($(shell $(LD) --version 2>&1 | head -n 1 | grep LLD),)
+CLANG_FLAGS	+= -fuse-ld=lld
+endif
 KBUILD_CFLAGS	+= $(CLANG_FLAGS)
 KBUILD_AFLAGS	+= $(CLANG_FLAGS)
 export CLANG_FLAGS
Signature · 1 line
2.20.1.791.gb4d0f1c61a-goog
#4 lore raw

Re: [PATCH v2 3/4] Makefile: lld: tell clang to use lld

Quoted 5 lines · On Tue, Feb 12, 2019 at 5:42 AM <[email protected]> wrote:

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld=lld. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Sorry, please explain what kind of problem
this patch solves.

[1] $(LD) is used to link vmlinux, modules, etc.

[2] $(CC) is used to link vdso, etc.

    and -fuse-ld= selects which linker is invoked from $(CC)

Is it a problem to use a different
type of linker for [1] and [2] ?

Thanks.

Quoted 29 lines

---
Changes V1->V2:
* add reviewed and tested by tags.
* move this addition up 2 statments so that it's properly added to KBUILD_*FLAGS as per Nathan.

 Makefile | 3 +++
 1 file changed, 3 insertions(+)
Makefile +3 -1
index d3b65e96d183..00e8e01d23fc 100644
@@ -500,6 +500,9 @@ ifneq ($(GCC_TOOLCHAIN),)
 CLANG_FLAGS    += --gcc-toolchain=$(GCC_TOOLCHAIN)
 endif
 CLANG_FLAGS    += -no-integrated-as
+ifneq ($(shell $(LD) --version 2>&1 | head -n 1 | grep LLD),)
+CLANG_FLAGS    += -fuse-ld=lld
+endif
 KBUILD_CFLAGS  += $(CLANG_FLAGS)
 KBUILD_AFLAGS  += $(CLANG_FLAGS)
 export CLANG_FLAGS
--
Quoted 2 lines

2.20.1.791.gb4d0f1c61a-goog

--
Best Regards
Masahiro Yamada

#5 lore raw

Re: [PATCH v2 3/4] Makefile: lld: tell clang to use lld

Quoted 14 lines · Masahiro Yamada
Quoted 5 lines · On Tue, Feb 12, 2019 at 5:42 AM <[email protected]> wrote:

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld=lld. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Sorry, please explain what kind of problem
this patch solves.

Quoted 4 lines

[1] $(LD) is used to link vmlinux, modules, etc.

[2] $(CC) is used to link vdso, etc.

    and -fuse-ld= selects which linker is invoked from $(CC)

It solves case 2.

Quoted 4 lines

Is it a problem to use a different
type of linker for [1] and [2] ?

Ideally, no, but I can think of at least one case where it might be problematic to mix linkers like that:
You might be mixing linker flags added via ld-option from one linker that aren't actually supported by the other linker.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#6 lore raw

Re: [PATCH v2 3/4] Makefile: lld: tell clang to use lld

Quoted 26 lines · Nick Desaulniers
Quoted 14 lines · Masahiro Yamada
Quoted 5 lines · On Tue, Feb 12, 2019 at 5:42 AM <[email protected]> wrote:

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld=lld. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Sorry, please explain what kind of problem
this patch solves.

Quoted 4 lines

[1] $(LD) is used to link vmlinux, modules, etc.

[2] $(CC) is used to link vdso, etc.

    and -fuse-ld= selects which linker is invoked from $(CC)

It solves case 2.

Quoted 2 lines
Quoted 7 lines
Quoted 2 lines

Is it a problem to use a different
type of linker for [1] and [2] ?

Ideally, no, but I can think of at least one case where it might be problematic to mix linkers like that:
You might be mixing linker flags added via ld-option from one linker that aren't actually supported by the other linker.

You can do this only when you are sure
that the _exactly_ same linker is used.

In my understanding, -fuse-ld=lld does not guarantee it.

Signature · 2 lines
Best Regards
Masahiro Yamada
#7 lore raw

Re: [PATCH v2 3/4] Makefile: lld: tell clang to use lld

Quoted 37 lines · Masahiro Yamada
Quoted 26 lines · Nick Desaulniers
Quoted 14 lines · Masahiro Yamada
Quoted 5 lines · On Tue, Feb 12, 2019 at 5:42 AM <[email protected]> wrote:

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld=lld. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Sorry, please explain what kind of problem
this patch solves.

Quoted 4 lines

[1] $(LD) is used to link vmlinux, modules, etc.

[2] $(CC) is used to link vdso, etc.

    and -fuse-ld= selects which linker is invoked from $(CC)

It solves case 2.

Quoted 2 lines
Quoted 7 lines
Quoted 2 lines

Is it a problem to use a different
type of linker for [1] and [2] ?

Ideally, no, but I can think of at least one case where it might be problematic to mix linkers like that:
You might be mixing linker flags added via ld-option from one linker that aren't actually supported by the other linker.

Quoted 4 lines

You can do this only when you are sure
that the _exactly_ same linker is used.

In my understanding, -fuse-ld=lld does not guarantee it.

I really don't think we should be mixing and matching linkers during a kernel build. When we compile with clang, we don't have escape hatches that allow for some object files to be compiled with GCC (mixing clang and GCC compiled object files into one build). Following the same logic, I think mixing linkers during kernel build should similarly be dissuaded. This patch AVOIDS clang using a different linker than what was specified via $LD, which is CRITICAL for cc-ldoption kbuild macro. Masahiro, I hope this patch can be re-evaluated, or if I'm not understanding your point, that you can provide additional clarification.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#8 lore raw

Re: [PATCH v2 3/4] Makefile: lld: tell clang to use lld

Hi Nick,

Quoted 45 lines · Nick Desaulniers
Quoted 37 lines · Masahiro Yamada
Quoted 26 lines · Nick Desaulniers
Quoted 14 lines · Masahiro Yamada
Quoted 5 lines · On Tue, Feb 12, 2019 at 5:42 AM <[email protected]> wrote:

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld=lld. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Sorry, please explain what kind of problem
this patch solves.

Quoted 4 lines

[1] $(LD) is used to link vmlinux, modules, etc.

[2] $(CC) is used to link vdso, etc.

    and -fuse-ld= selects which linker is invoked from $(CC)

It solves case 2.

Quoted 2 lines
Quoted 7 lines
Quoted 2 lines

Is it a problem to use a different
type of linker for [1] and [2] ?

Ideally, no, but I can think of at least one case where it might be problematic to mix linkers like that:
You might be mixing linker flags added via ld-option from one linker that aren't actually supported by the other linker.

Quoted 4 lines

You can do this only when you are sure
that the _exactly_ same linker is used.

In my understanding, -fuse-ld=lld does not guarantee it.

Quoted 11 lines

I really don't think we should be mixing and matching linkers during a kernel build. When we compile with clang, we don't have escape hatches that allow for some object files to be compiled with GCC (mixing clang and GCC compiled object files into one build). Following the same logic, I think mixing linkers during kernel build should similarly be dissuaded. This patch AVOIDS clang using a different linker than what was specified via $LD, which is CRITICAL for cc-ldoption kbuild macro. Masahiro, I hope this patch can be re-evaluated, or if I'm not understanding your point, that you can provide additional clarification.

You can pass an absolute pass to LD, like

make LD=/path/to/my/llvm/install/dir/bin/ld.lld

This clarifies which linker is being used even when multiple versions of llvm are installed on the machine.

However, -fuse-ld=lld is ambiguous.
Will it use the first ld.lld found in PATH?

So, you cannot avoid mixing linkers by this means.

If we could do -fuse=$(LD), I would agree with it. Clang accepts -fuse=<absolute-path>, GCC does not.

Is there a way to control the linker search path?

Signature · 2 lines
Best Regards
Masahiro Yamada
#9 lore raw

PATCH v3 1/1 Makefile: lld: tell clang to use lld

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld={bfd,gold,lld,<absolute path to linker>}. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Select the linker via absolute path from $PATH via which. This allows you to build with:

$ make LD=ld.lld
$ make LD=ld.lld-8
$ make LD=/path/to/ld.lld

Add -Qunused-arguments to KBUILD_CPPFLAGS when using LLD, as otherwise Clang likes to complain about -fuse-lld= being unused when compiling but not linking (-c) such as when cc-option is used.

---
Changes V2->V3:
* Use absolute path based on which $LD as per Masahiro.
* Add -Qunused-arguments.
* Drop tested-by/reviewed-by tags, since this patched has changed enough to warrant re-testing/re-review, IMO.
* Add more info to the commit message.
Changes V1->V2:
* add reviewed and tested by tags.
* move this addition up 2 statments so that it's properly added to KBUILD_*FLAGS as per Nathan.

 Makefile | 4 ++++
 1 file changed, 4 insertions(+)
Makefile +4 -0
index 026fbc450906..895c076b6305 100644
@@ -514,6 +514,10 @@ ifneq ($(GCC_TOOLCHAIN),)
 CLANG_FLAGS	+= --gcc-toolchain=$(GCC_TOOLCHAIN)
 endif
 CLANG_FLAGS	+= -no-integrated-as
+ifneq ($(shell $(LD) --version 2>&1 | head -n 1 | grep LLD),)
+CLANG_FLAGS	+= -fuse-ld=$(shell which $(LD))
+KBUILD_CPPFLAGS += -Qunused-arguments
+endif
 KBUILD_CFLAGS	+= $(CLANG_FLAGS)
 KBUILD_AFLAGS	+= $(CLANG_FLAGS)
 export CLANG_FLAGS
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
Reviewed-by Tested-by on v3 1/1
#10 lore raw

Re: [PATCH v3] Makefile: lld: tell clang to use lld

Quoted 42 lines · Nick Desaulniers

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld={bfd,gold,lld,<absolute path to linker>}. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Select the linker via absolute path from $PATH via which. This allows you to build with:

$ make LD=ld.lld
$ make LD=ld.lld-8
$ make LD=/path/to/ld.lld

Add -Qunused-arguments to KBUILD_CPPFLAGS when using LLD, as otherwise Clang likes to complain about -fuse-lld= being unused when compiling but not linking (-c) such as when cc-option is used.

---
Changes V2->V3:
* Use absolute path based on which $LD as per Masahiro.
* Add -Qunused-arguments.
* Drop tested-by/reviewed-by tags, since this patched has changed enough to warrant re-testing/re-review, IMO.
* Add more info to the commit message.
Changes V1->V2:
* add reviewed and tested by tags.
* move this addition up 2 statments so that it's properly added to KBUILD_*FLAGS as per Nathan.

 Makefile | 4 ++++
 1 file changed, 4 insertions(+)
Makefile +0 -0
index 026fbc450906..895c076b6305 100644

Makefile ifneq ($(GCC_TOOLCHAIN),) · line 519

Quoted 6 lines
Makefile +3 -0
@@ -514,6 +514,10 @@ ifneq ($(GCC_TOOLCHAIN),)
 CLANG_FLAGS	+= --gcc-toolchain=$(GCC_TOOLCHAIN)
 endif
 CLANG_FLAGS	+= -no-integrated-as
+ifneq ($(shell $(LD) --version 2>&1 | head -n 1 | grep LLD),)
+CLANG_FLAGS	+= -fuse-ld=$(shell which $(LD))
+KBUILD_CPPFLAGS += -Qunused-arguments

We should remove the -Qunused-arguments call in the second clang block at the same time. With that change:

Quoted 7 lines
Makefile +1 -0
+endif
 KBUILD_CFLAGS	+= $(CLANG_FLAGS)
 KBUILD_AFLAGS	+= $(CLANG_FLAGS)
 export CLANG_FLAGS
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
#11 lore raw

PATCH v4 1/1 Makefile: lld: tell clang to use lld

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld={bfd,gold,lld,<absolute path to linker>}. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Select the linker via absolute path from $PATH via which. This allows you to build with:

$ make LD=ld.lld
$ make LD=ld.lld-8
$ make LD=/path/to/ld.lld

Add -Qunused-arguments to KBUILD_CPPFLAGS sooner, as otherwise Clang likes to complain about -fuse-lld= being unused when compiling but not linking (-c) such as when cc-option is used. There's no need to guard with cc-option.

---
Changes V3->V4:
* Unconditionally add -Qunused-arguments sooners, as per Nathan.
* Slight modification to commit message for that point. Changes V2->V3:
* Use absolute path based on which $LD as per Masahiro.
* Add -Qunused-arguments.
* Drop tested-by/reviewed-by tags, since this patched has changed enough to warrant re-testing/re-review, IMO.
* Add more info to the commit message.
Changes V1->V2:
* add reviewed and tested by tags.
* move this addition up 2 statments so that it's properly added to KBUILD_*FLAGS as per Nathan.

 Makefile | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Makefile +4 -1
index 026fbc450906..b290e76e1ca5 100644
@@ -514,6 +514,10 @@ ifneq ($(GCC_TOOLCHAIN),)
 CLANG_FLAGS	+= --gcc-toolchain=$(GCC_TOOLCHAIN)
 endif
 CLANG_FLAGS	+= -no-integrated-as
+ifneq ($(shell $(LD) --version 2>&1 | head -n 1 | grep LLD),)
+CLANG_FLAGS	+= -fuse-ld=$(shell which $(LD))
+endif
+KBUILD_CPPFLAGS	+= -Qunused-arguments
 KBUILD_CFLAGS	+= $(CLANG_FLAGS)
 KBUILD_AFLAGS	+= $(CLANG_FLAGS)
 export CLANG_FLAGS
@@ -716,7 +720,6 @@ stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
 KBUILD_CFLAGS += $(stackp-flags-y)
 
 ifdef CONFIG_CC_IS_CLANG
-KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-invalid-specifier)
 KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
 KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
Reviewed-by Tested-by on v4 1/1
#12 lore raw

Re: [PATCH v4] Makefile: lld: tell clang to use lld

Quoted 17 lines · Nick Desaulniers

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld={bfd,gold,lld,<absolute path to linker>}. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Select the linker via absolute path from $PATH via which. This allows you to build with:

$ make LD=ld.lld
$ make LD=ld.lld-8
$ make LD=/path/to/ld.lld

Add -Qunused-arguments to KBUILD_CPPFLAGS sooner, as otherwise Clang likes to complain about -fuse-lld= being unused when compiling but not linking (-c) such as when cc-option is used. There's no need to guard with cc-option.

Quoted 6 lines
Quoted 44 lines

---
Changes V3->V4:
* Unconditionally add -Qunused-arguments sooners, as per Nathan.
* Slight modification to commit message for that point. Changes V2->V3:
* Use absolute path based on which $LD as per Masahiro.
* Add -Qunused-arguments.
* Drop tested-by/reviewed-by tags, since this patched has changed enough to warrant re-testing/re-review, IMO.
* Add more info to the commit message.
Changes V1->V2:
* add reviewed and tested by tags.
* move this addition up 2 statments so that it's properly added to KBUILD_*FLAGS as per Nathan.

 Makefile | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Makefile +4 -1
index 026fbc450906..b290e76e1ca5 100644
@@ -514,6 +514,10 @@ ifneq ($(GCC_TOOLCHAIN),)
 CLANG_FLAGS	+= --gcc-toolchain=$(GCC_TOOLCHAIN)
 endif
 CLANG_FLAGS	+= -no-integrated-as
+ifneq ($(shell $(LD) --version 2>&1 | head -n 1 | grep LLD),)
+CLANG_FLAGS	+= -fuse-ld=$(shell which $(LD))
+endif
+KBUILD_CPPFLAGS	+= -Qunused-arguments
 KBUILD_CFLAGS	+= $(CLANG_FLAGS)
 KBUILD_AFLAGS	+= $(CLANG_FLAGS)
 export CLANG_FLAGS
@@ -716,7 +720,6 @@ stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
 KBUILD_CFLAGS += $(stackp-flags-y)
 
 ifdef CONFIG_CC_IS_CLANG
-KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-invalid-specifier)
 KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
 KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
Reviewed-by on v3 1/1
#13 lore raw

Re: [PATCH v3] Makefile: lld: tell clang to use lld

In reply to Nick Desaulniers
Quoted 17 lines · Nick Desaulniers' via Clang Built Linux

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld={bfd,gold,lld,<absolute path to linker>}. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Select the linker via absolute path from $PATH via which. This allows you to build with:

$ make LD=ld.lld
$ make LD=ld.lld-8
$ make LD=/path/to/ld.lld

Add -Qunused-arguments to KBUILD_CPPFLAGS when using LLD, as otherwise Clang likes to complain about -fuse-lld= being unused when compiling but not linking (-c) such as when cc-option is used.

Quoted 6 lines

Thanks for bringing this up again, Nick.

I suggest to do a separate patch on the move of "KBUILD_CPPFLAGS += -Qunused-arguments".
( Patch was attached in [2],[3]. )

As pointed by Nathan this needs the removal in the original code-block.

Other than that:

[1] https://github.com/ClangBuiltLinux/linux/issues/366#issuecomment-466836735
[2] https://github.com/ClangBuiltLinux/linux/issues/366#issuecomment-467118373
[3] https://github.com/ClangBuiltLinux/linux/files/2901865/kbuild-clang-lld-Move-Qunused-arguments-CPPFLAGS-patch.txt

Quoted 39 lines

---
Changes V2->V3:
* Use absolute path based on which $LD as per Masahiro.
* Add -Qunused-arguments.
* Drop tested-by/reviewed-by tags, since this patched has changed enough to warrant re-testing/re-review, IMO.
* Add more info to the commit message.
Changes V1->V2:
* add reviewed and tested by tags.
* move this addition up 2 statments so that it's properly added to KBUILD_*FLAGS as per Nathan.

 Makefile | 4 ++++
 1 file changed, 4 insertions(+)
Makefile +4 -1
index 026fbc450906..895c076b6305 100644
@@ -514,6 +514,10 @@ ifneq ($(GCC_TOOLCHAIN),)
 CLANG_FLAGS    += --gcc-toolchain=$(GCC_TOOLCHAIN)
 endif
 CLANG_FLAGS    += -no-integrated-as
+ifneq ($(shell $(LD) --version 2>&1 | head -n 1 | grep LLD),)
+CLANG_FLAGS    += -fuse-ld=$(shell which $(LD))
+KBUILD_CPPFLAGS += -Qunused-arguments
+endif
 KBUILD_CFLAGS  += $(CLANG_FLAGS)
 KBUILD_AFLAGS  += $(CLANG_FLAGS)
 export CLANG_FLAGS
--

2.21.0.392.gf8f6787159e-goog

--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20190402070858.49597-1-ndesaulniers%40google.com.
For more options, visit https://groups.google.com/d/optout.

#14 lore raw

Re: [PATCH v3] Makefile: lld: tell clang to use lld

Quoted 26 lines · Sedat Dilek
Quoted 17 lines · Nick Desaulniers' via Clang Built Linux

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld={bfd,gold,lld,<absolute path to linker>}. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Select the linker via absolute path from $PATH via which. This allows you to build with:

$ make LD=ld.lld
$ make LD=ld.lld-8
$ make LD=/path/to/ld.lld

Add -Qunused-arguments to KBUILD_CPPFLAGS when using LLD, as otherwise Clang likes to complain about -fuse-lld= being unused when compiling but not linking (-c) such as when cc-option is used.

Quoted 6 lines
Quoted 6 lines

Thanks for bringing this up again, Nick.

I suggest to do a separate patch on the move of "KBUILD_CPPFLAGS += -Qunused-arguments".

I think it makes sense to do it all in one patch as it doesn't make much sense to do the move without this change. Nick already sent a v4 doing this.

Cheers,
Nathan

Quoted 51 lines

( Patch was attached in [2],[3]. )

As pointed by Nathan this needs the removal in the original code-block.

Other than that:

[1] https://github.com/ClangBuiltLinux/linux/issues/366#issuecomment-466836735
[2] https://github.com/ClangBuiltLinux/linux/issues/366#issuecomment-467118373
[3] https://github.com/ClangBuiltLinux/linux/files/2901865/kbuild-clang-lld-Move-Qunused-arguments-CPPFLAGS-patch.txt

Quoted 39 lines

---
Changes V2->V3:
* Use absolute path based on which $LD as per Masahiro.
* Add -Qunused-arguments.
* Drop tested-by/reviewed-by tags, since this patched has changed enough to warrant re-testing/re-review, IMO.
* Add more info to the commit message.
Changes V1->V2:
* add reviewed and tested by tags.
* move this addition up 2 statments so that it's properly added to KBUILD_*FLAGS as per Nathan.

 Makefile | 4 ++++
 1 file changed, 4 insertions(+)
Makefile +4 -1
index 026fbc450906..895c076b6305 100644
@@ -514,6 +514,10 @@ ifneq ($(GCC_TOOLCHAIN),)
 CLANG_FLAGS    += --gcc-toolchain=$(GCC_TOOLCHAIN)
 endif
 CLANG_FLAGS    += -no-integrated-as
+ifneq ($(shell $(LD) --version 2>&1 | head -n 1 | grep LLD),)
+CLANG_FLAGS    += -fuse-ld=$(shell which $(LD))
+KBUILD_CPPFLAGS += -Qunused-arguments
+endif
 KBUILD_CFLAGS  += $(CLANG_FLAGS)
 KBUILD_AFLAGS  += $(CLANG_FLAGS)
 export CLANG_FLAGS
--

2.21.0.392.gf8f6787159e-goog

--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20190402070858.49597-1-ndesaulniers%40google.com.
For more options, visit https://groups.google.com/d/optout.

#15 lore raw

Re: [PATCH v3] Makefile: lld: tell clang to use lld

In reply to Nick Desaulniers
Quoted 23 lines · Nick Desaulniers

This is needed because clang doesn't select which linker to use based on $LD but rather -fuse-ld={bfd,gold,lld,<absolute path to linker>}. This is problematic especially for cc-ldoption, which checks for linker flag support via invoking the compiler, rather than the linker.

Select the linker via absolute path from $PATH via which. This allows you to build with:

$ make LD=ld.lld
$ make LD=ld.lld-8
$ make LD=/path/to/ld.lld

Add -Qunused-arguments to KBUILD_CPPFLAGS when using LLD, as otherwise Clang likes to complain about -fuse-lld= being unused when compiling but not linking (-c) such as when cc-option is used.

Quoted 3 lines

---
Changes V2->V3:
* Use absolute path based on which $LD as per Masahiro.

This is not what I suggested. I wanted to say:
"You cannot do this for GCC, so do not do it at all".

I want to propose alternative solution.
Please check the attached patches.

To apply 0002, you need the following as a prerequisite:

https://lore.kernel.org/patchwork/patch/1057685/

--
Best Regards
Masahiro Yamada

From 7fcb7806991d9b644173879c3c1fcbbded532435 Mon Sep 17 00:00:00 2001
From: Masahiro Yamada <[email protected]>
Date: Fri, 5 Apr 2019 17:46:14 +0900
Subject: [PATCH 1/2] ARM: vdso: use $(LD) instead of $(CC) to link VDSO

We use $(LD) to link vmlinux, modules, decompressors, etc.

VDSO is the only exceptional case where $(CC) is used as the frontend of linking, but I do not know why. VDSO uses a special linker script, and does not link standard libraries at all.

I changed the Makefile to use $(LD) rahter than $(CC). I confirmed the same vdso.so.raw was still produced.

Users will be able to use their favorite linker (e.g. lld instead of of bfd) by passing LD= from the command line.

My plan is to rewrite all VDSO Makefiles to use $(LD), then delete cc-ldoption.

---
 arch/arm/vdso/Makefile | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)
arch/arm/vdso/Makefile +8 -13
index f4efff9d3afb..fadf554d9391 100644
@@ -10,12 +10,12 @@ obj-vdso := $(addprefix $(obj)/, $(obj-vdso))
 ccflags-y := -fPIC -fno-common -fno-builtin -fno-stack-protector
 ccflags-y += -DDISABLE_BRANCH_PROFILING
 
-VDSO_LDFLAGS := -Wl,-Bsymbolic -Wl,--no-undefined -Wl,-soname=linux-vdso.so.1
-VDSO_LDFLAGS += -Wl,-z,max-page-size=4096 -Wl,-z,common-page-size=4096
-VDSO_LDFLAGS += -nostdlib -shared
-VDSO_LDFLAGS += $(call cc-ldoption, -Wl$(comma)--hash-style=sysv)
-VDSO_LDFLAGS += $(call cc-ldoption, -Wl$(comma)--build-id)
-VDSO_LDFLAGS += $(call cc-ldoption, -fuse-ld=bfd)
+ldflags-y = -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \
+	    -z max-page-size=4096 -z common-page-size=4096 \
+	    -nostdlib -shared \
+	    $(call ld-option, --hash-style=sysv) \
+	    $(call ld-option, --build-id) \
+	    -T
 
 obj-$(CONFIG_VDSO) += vdso.o
 extra-$(CONFIG_VDSO) += vdso.lds
@@ -37,8 +37,8 @@ KCOV_INSTRUMENT := n
 $(obj)/vdso.o : $(obj)/vdso.so
 
 # Link rule for the .so file
-$(obj)/vdso.so.raw: $(src)/vdso.lds $(obj-vdso) FORCE
-	$(call if_changed,vdsold)
+$(obj)/vdso.so.raw: $(obj)/vdso.lds $(obj-vdso) FORCE
+	$(call if_changed,ld)
 
 $(obj)/vdso.so.dbg: $(obj)/vdso.so.raw $(obj)/vdsomunge FORCE
 	$(call if_changed,vdsomunge)
@@ -48,11 +48,6 @@ $(obj)/%.so: OBJCOPYFLAGS := -S
 $(obj)/%.so: $(obj)/%.so.dbg FORCE
 	$(call if_changed,objcopy)
 
-# Actual build commands
-quiet_cmd_vdsold = VDSO    $@
-      cmd_vdsold = $(CC) $(c_flags) $(VDSO_LDFLAGS) \
-                   -Wl,-T $(filter %.lds,$^) $(filter %.o,$^) -o $@
-
 quiet_cmd_vdsomunge = MUNGE   $@
       cmd_vdsomunge = $(objtree)/$(obj)/vdsomunge $< $@
 
Signature · 1 line
2.17.1

From db93aba5086ebc0558c193101d56c2e5a5878dda Mon Sep 17 00:00:00 2001
From: Masahiro Yamada <[email protected]>
Date: Thu, 4 Apr 2019 23:40:18 +0900
Subject: [PATCH 2/2] arm64: vdso: use $(LD) instead of $(CC) to link VDSO

We use $(LD) to link vmlinux, modules, decompressors, etc.

VDSO is the only exceptional case where $(CC) is used as the frontend of linking, but I do not know why. VDSO uses a special linker script, and does not link standard libraries at all.

I changed the Makefile to use $(LD) rahter than $(CC). I tested, and VDSO still worked.

Users will be able to use their favorite linker (e.g. lld instead of of bfd) by passing LD= from the command line.

My plan is to rewrite all VDSO Makefiles to use $(LD), then delete cc-ldoption.

---
 arch/arm64/kernel/vdso/Makefile | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
arch/arm64/kernel/vdso/Makefile +3 -10
index a0af6bf6c11b..744b9dbaba03 100644
@@ -12,17 +12,12 @@ obj-vdso := gettimeofday.o note.o sigreturn.o
 targets := $(obj-vdso) vdso.so vdso.so.dbg
 obj-vdso := $(addprefix $(obj)/, $(obj-vdso))
 
-ccflags-y := -shared -fno-common -fno-builtin
-ccflags-y += -nostdlib -Wl,-soname=linux-vdso.so.1 \
-		$(call cc-ldoption, -Wl$(comma)--hash-style=sysv)
+ldflags-y := -shared -nostdlib -soname=linux-vdso.so.1 \
+		$(call ld-option, --hash-style=sysv) -n -T
 
 # Disable gcov profiling for VDSO code
 GCOV_PROFILE := n
 
-# Workaround for bare-metal (ELF) toolchains that neglect to pass -shared
-# down to collect2, resulting in silent corruption of the vDSO image.
-ccflags-y += -Wl,-shared
-
 obj-y += vdso.o
 extra-y += vdso.lds
 CPPFLAGS_vdso.lds += -P -C -U$(ARCH)
@@ -32,7 +27,7 @@ $(obj)/vdso.o : $(obj)/vdso.so
 
 # Link rule for the .so file, .lds has to be first
 $(obj)/vdso.so.dbg: $(obj)/vdso.lds $(obj-vdso) FORCE
-	$(call if_changed,vdsold)
+	$(call if_changed,ld)
 
 # Strip rule for the .so file
 $(obj)/%.so: OBJCOPYFLAGS := -S
@@ -52,8 +47,6 @@ $(obj-vdso): %.o: %.S FORCE
 	$(call if_changed_dep,vdsoas)
 
 # Actual build commands
-quiet_cmd_vdsold = VDSOL   $@
-      cmd_vdsold = $(CC) $(c_flags) -Wl,-n -Wl,-T $(real-prereqs) -o $@
 quiet_cmd_vdsoas = VDSOA   $@
       cmd_vdsoas = $(CC) $(a_flags) -c -o $@ $<
 
Signature · 1 line
2.17.1
#16 lore raw

Re: [PATCH v3] Makefile: lld: tell clang to use lld

Quoted 2 lines · Masahiro Yamada

I want to propose alternative solution.
Please check the attached patches.

In both patches:

Quoted 2 lines

I changed the Makefile to use $(LD) rahter than $(CC). I confirmed the same vdso.so.raw was still produced.

typo: rahter -> rather

In the 0001 patch of arch/arm/vdso/Makefile:

arch/arm/vdso/Makefile obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) · line 18

Quoted 8 lines

...

arch/arm/vdso/Makefile +6 -1
-VDSO_LDFLAGS += $(call cc-ldoption, -fuse-ld=bfd)
+ldflags-y = -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \
+           -z max-page-size=4096 -z common-page-size=4096 \
+           -nostdlib -shared \
+           $(call ld-option, --hash-style=sysv) \
+           $(call ld-option, --build-id) \
+           -T

I see that "-fuse-ld=bfd" is explicitly dropped here, which could reintroduce the problem fixed in d2b30cd4b722 ("ARM: 8384/1: VDSO:
force use of BFD linker") (and 3473f26592c1c). Does ld.gold still exhibit this problem? If so, we'll need to detect gold and force bfd still maybe?

Signature · 1 line
Kees Cook
#17 lore raw

Re: [PATCH v3] Makefile: lld: tell clang to use lld

Quoted 5 lines · Kees Cook
Quoted 2 lines · Masahiro Yamada

I want to propose alternative solution.
Please check the attached patches.

```
My plan is to rewrite all VDSO Makefiles to use $(LD), then delete cc-ldoption.
```

I agree with that as a possible solution as well; I think it's best to just invoke the linker directly rather than use the compiler as the driver. Just small nits with the 2 attached patches, but I think they will also work for us.

Looks like there's 15 call sites across 12 files, plus Documentation/ and scripts/Kbuild.include; removing it seems feasible and I'd be happy to help if you'd welcome the patches?

Let's start with that series of changes, but I suspect we will ultimately need to revisit -fuse-ld=. I was just thinking that even my proposed patch doesn't do anything for KBUILD_HOSTCFLAGS, for example. I worry that bfd may still be invoked by Clang at some point in the build. I will try to test in an environment with no GNU binutils.

Quoted 7 lines

In the 0001 patch of arch/arm/vdso/Makefile:

arch/arm/vdso/Makefile obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) · line 16

Quoted 6 lines

...

arch/arm/vdso/Makefile +4 -1
-VDSO_LDFLAGS += $(call cc-ldoption, -fuse-ld=bfd)
+ldflags-y = -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \
+           -z max-page-size=4096 -z common-page-size=4096 \
+           -nostdlib -shared \
+           $(call ld-option, --hash-style=sysv) \

I think the vdso's should all REQUIRE --hash-style=sysv; the kselftest for vdso's will fail otherwise. I could always follow up with patches for that if we didn't want to conflate changes.

Quoted 2 lines

arch/arm/vdso/Makefile obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) · line 18

Quoted 2 lines
arch/arm/vdso/Makefile +2 -0
+           $(call ld-option, --build-id) \
+           -T

Was it intentional to add -T standalone? The man page for ld says it needs an additional filename to follow -T. Or rather is optional if a -L flag is specified?

Quoted 6 lines

I see that "-fuse-ld=bfd" is explicitly dropped here, which could reintroduce the problem fixed in d2b30cd4b722 ("ARM: 8384/1: VDSO:
force use of BFD linker") (and 3473f26592c1c). Does ld.gold still exhibit this problem? If so, we'll need to detect gold and force bfd still maybe?

The arm32 vdso Makefile is an oddity in this regard. I think what Kees described is best. Rather than always set the linker to bfd (which harms linkers like lld), detect if gold is being used and if so set the linker back to bfd.

For arm64, lld has this issue with -n,
https://github.com/ClangBuiltLinux/linux/issues/340, but I think we can fix that in a follow up patch. Same thoughts on --hash-style and -T.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#18 lore raw

Re: [PATCH v3] Makefile: lld: tell clang to use lld

Hi Kees, Nick,

Quoted 12 lines · Nick Desaulniers
Quoted 5 lines · Kees Cook
Quoted 2 lines · Masahiro Yamada

I want to propose alternative solution.
Please check the attached patches.

```
My plan is to rewrite all VDSO Makefiles to use $(LD), then delete cc-ldoption.
```
Quoted 8 lines

I agree with that as a possible solution as well; I think it's best to just invoke the linker directly rather than use the compiler as the driver. Just small nits with the 2 attached patches, but I think they will also work for us.

Looks like there's 15 call sites across 12 files, plus Documentation/ and scripts/Kbuild.include; removing it seems feasible and I'd be happy to help if you'd welcome the patches?

Yeah, your help is appreciated.

I just worked on two Makefiles
because I wanted to hear opinions
before investing more efforts.

Basically, you are positive to this approach, so let's go this way.

Quoted 4 lines

Let's start with that series of changes, but I suspect we will ultimately need to revisit -fuse-ld=. I was just thinking that even my proposed patch doesn't do anything for KBUILD_HOSTCFLAGS, for example.

Host tools are not so important, but I agree.

BTW, why does clang invoke bfd linker instead of lld by default?

If we want to make llvm self-contained, I believe clang should use ld.lld by default.

Quoted 16 lines

I worry that bfd may still be invoked by Clang at some point
in the build. I will try to test in an environment with no GNU binutils.

Quoted 7 lines

In the 0001 patch of arch/arm/vdso/Makefile:

arch/arm/vdso/Makefile obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) · line 16

Quoted 6 lines

...

arch/arm/vdso/Makefile +4 -1
-VDSO_LDFLAGS += $(call cc-ldoption, -fuse-ld=bfd)
+ldflags-y = -Bsymbolic --no-undefined -soname=linux-vdso.so.1 \
+           -z max-page-size=4096 -z common-page-size=4096 \
+           -nostdlib -shared \
+           $(call ld-option, --hash-style=sysv) \

I think the vdso's should all REQUIRE --hash-style=sysv; the kselftest for vdso's will fail otherwise. I could always follow up with patches for that if we didn't want to conflate changes.

Quoted 5 lines
Quoted 2 lines

arch/arm/vdso/Makefile obj-vdso := $(addprefix $(obj)/, $(obj-vdso)) · line 18

Quoted 2 lines
arch/arm/vdso/Makefile +2 -0
+           $(call ld-option, --build-id) \
+           -T

Was it intentional to add -T standalone? The man page for ld says it needs an additional filename to follow -T.

This is because I wanted to re-use cmd_ld defined in scripts/Makefile.lib

$(real-prereqs) contains the linker scripts and objects.

Quoted 9 lines

Or rather is optional
if a -L flag is specified?

Quoted 6 lines

I see that "-fuse-ld=bfd" is explicitly dropped here, which could reintroduce the problem fixed in d2b30cd4b722 ("ARM: 8384/1: VDSO:
force use of BFD linker") (and 3473f26592c1c). Does ld.gold still exhibit this problem? If so, we'll need to detect gold and force bfd still maybe?

I intentionally dropped -fuse-ld=bfd.

With my patch applied, users have full control of the linker by passing LD=.

Since we cannot link vmlinux with gold, users should definitely pass a bfd linker.

Quoted 9 lines

The arm32 vdso Makefile is an oddity in this regard. I think what Kees described is best. Rather than always set the linker to bfd (which harms linkers like lld), detect if gold is being used and if so set the linker back to bfd.

For arm64, lld has this issue with -n,
https://github.com/ClangBuiltLinux/linux/issues/340, but I think we can fix that in a follow up patch. Same thoughts on --hash-style and -T.

If we need to add something depending on the linker type,

I do not mind doing like this.

ldflags-$(CONFIG_LD_IS_GOLD) += ... ldflags-$(CONFIG_LD_IS_LLD) += ...

But, it is a different issue.

--
Best Regards
Masahiro Yamada

#19 lore raw

PATCH v2 4/4 Makefile: lld: set -O2 linker flag when linking with LLD

In reply to [email protected]

For arm64:
0.34% size improvement with lld -O2 over lld for vmlinux. 3.3% size improvement with lld -O2 over lld for Image.lz4-dtb.

---
Changes V1->V2:
* add tested and reviewed by tags.

 Makefile | 4 ++++
 1 file changed, 4 insertions(+)
Makefile +4 -0
index 00e8e01d23fc..8011555745aa 100644
@@ -718,6 +718,10 @@ else
 KBUILD_CFLAGS += -Wno-unused-but-set-variable
 endif
 
+ifdef CONFIG_LD_IS_LLD
+KBUILD_LDFLAGS += -O2
+endif
+
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
 ifdef CONFIG_FRAME_POINTER
 KBUILD_CFLAGS	+= -fno-omit-frame-pointer -fno-optimize-sibling-calls
Signature · 1 line
2.20.1.791.gb4d0f1c61a-goog
#20 lore raw

Re: [PATCH v2 4/4] Makefile: lld: set -O2 linker flag when linking with LLD

Quoted 3 lines · On Mon, Feb 11, 2019 at 11:30:06AM -0800, [email protected] wrote:

For arm64:
0.34% size improvement with lld -O2 over lld for vmlinux. 3.3% size improvement with lld -O2 over lld for Image.lz4-dtb.

The Changelog is utterly failing to explain what it does.

Quoted 1 line

That is irrelevant crap.

Quoted 30 lines

---
Changes V1->V2:
* add tested and reviewed by tags.

 Makefile | 4 ++++
 1 file changed, 4 insertions(+)
Makefile +4 -0
index 00e8e01d23fc..8011555745aa 100644
@@ -718,6 +718,10 @@ else
 KBUILD_CFLAGS += -Wno-unused-but-set-variable
 endif
 
+ifdef CONFIG_LD_IS_LLD
+KBUILD_LDFLAGS += -O2
+endif
+
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
 ifdef CONFIG_FRAME_POINTER
 KBUILD_CFLAGS	+= -fno-omit-frame-pointer -fno-optimize-sibling-calls
Signature · 1 line
2.20.1.791.gb4d0f1c61a-goog
#21 lore raw

Re: [PATCH v2 4/4] Makefile: lld: set -O2 linker flag when linking with LLD

In reply to [email protected]
Quoted 23 lines · On Tue, Feb 12, 2019 at 5:42 AM <[email protected]> wrote:

For arm64:
0.34% size improvement with lld -O2 over lld for vmlinux. 3.3% size improvement with lld -O2 over lld for Image.lz4-dtb.

---
Changes V1->V2:
* add tested and reviewed by tags.

 Makefile | 4 ++++
 1 file changed, 4 insertions(+)
Makefile +0 -0
index 00e8e01d23fc..8011555745aa 100644

Makefile else · line 724

Quoted 7 lines
Makefile +4 -0
@@ -718,6 +718,10 @@ else
 KBUILD_CFLAGS += -Wno-unused-but-set-variable
 endif
 
+ifdef CONFIG_LD_IS_LLD
+KBUILD_LDFLAGS += -O2
+endif
+

I wondered why this option should be added only for lld, and I found the answer in the "Link:"

"for bfd, didn't seem to change the size at all (I can't recall if this was arm64 or x86 I had tested with though)."

Hmm, I see.
My 'man ld' says it is currently effective only for shared libraries.

-O level
If level is a numeric values greater than zero ld optimizes the output. This might take significantly longer and therefore probably should only be enabled for the final binary. At the moment this option only affects ELF shared library generation. Future releases of the linker may make more use of this option. Also currently there is no difference in the linker's behaviour for different non-zero values of this option. Again this may change with future releases.

I am fine with adding it unconditionally
as long as it is harmless for bfd, gold.

Quoted 6 lines
Makefile +0 -1
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
 ifdef CONFIG_FRAME_POINTER
 KBUILD_CFLAGS  += -fno-omit-frame-pointer -fno-optimize-sibling-calls
--

2.20.1.791.gb4d0f1c61a-goog

Signature · 2 lines
Best Regards
Masahiro Yamada
#22 lore raw

v2 cover Improve kernel LLD support

In reply to [email protected]

This patch cleans up a few places in the Makefile to make way for landing LLD support in the kernel. There are still a few arch specific fixes that need to get upstreamed, but this core set of patches should be arch independent.

Some very rough numbers: LLD is improving the overall build time of Google Pixel kernels by about 9%. The build is mostly dominated by compilation, but LLD is able to shave off quite a bit of time.

One of the included patches helps get LLD linked kernels to competitive binary size, but there still some bugs in LLD causing it not to discard certain sections. Once that's fixed, I estimate an LLD linked kernel to be just barely smaller than a BFD linked kernel (less than 1%).

Folks looking to test should use LLD built from source, as I recently fixed a bug in LLD that prevented LLD linked kernel modules from being loadable.

$ make LD=ld.lld

Nick Desaulniers (4):
init/Kconfig: add config support for detecting linker
Makefile: clang: choose GCC_TOOLCHAIN_DIR not on LD
Makefile: lld: tell clang to use lld
Makefile: lld: set -O2 linker flag when linking with LLD

Changes since v1:
* collect reviewed-by, sugguested-by, tested-by tags.
* move where -fuse-ld=lld is added to CFLAGS.

 Makefile     | 9 ++++++++-
 init/Kconfig | 9 +++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
Signature · 1 line
2.20.1.791.gb4d0f1c61a-goog