Skip to content

Commit 0826471

Browse files
committed
add mold linker support
Signed-off-by: sbwml <[email protected]>
1 parent 675620a commit 0826471

11 files changed

+480
-8
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ sudo apt-get install -y clang-15
5454
export USE_GLIBC=y
5555
```
5656

57-
### 启用 [GCC13](https://gcc.gnu.org/gcc-13/) 工具链编译
58-
##### 只需在构建固件前执行以下命令即可启用 GCC13 工具链
59-
60-
```
61-
export USE_GCC13=y
62-
```
63-
6457
### 启用 [eBPF](https://docs.kernel.org/bpf/) 支持
6558
##### 只需在构建固件前执行以下命令即可启用 eBPF 支持
6659

openwrt/build.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,14 @@ export platform=$2
8484
[ "$platform" = "x86_64" ] && export platform="x86_64" toolchain_arch="x86_64"
8585

8686
# gcc 13
87-
export USE_GCC13=$USE_GCC13
87+
USE_GCC13=y
8888

8989
# use glibc
9090
export USE_GLIBC=$USE_GLIBC
9191

92+
# use mold
93+
USE_MOLD=y
94+
9295
# lrng
9396
export ENABLE_LRNG=$ENABLE_LRNG
9497

@@ -122,6 +125,7 @@ fi
122125
echo -e "${GREEN_COLOR}Date: $CURRENT_DATE${RES}\r\n"
123126

124127
[ "$USE_GCC13" = "y" ] && echo -e "${GREEN_COLOR}GCC VERSION: 13${RES}" || echo -e "${GREEN_COLOR}GCC VERSION: 11${RES}"
128+
[ "$USE_MOLD" = "y" ] && echo -e "${GREEN_COLOR}USE_MOLD: true${RES}" || echo -e "${GREEN_COLOR}USE_MOLD: false${RES}"
125129
[ "$ENABLE_OTA" = "y" ] && echo -e "${GREEN_COLOR}ENABLE_OTA: true${RES}" || echo -e "${GREEN_COLOR}ENABLE_OTA: false${RES}"
126130
[ "$ENABLE_BPF" = "y" ] && echo -e "${GREEN_COLOR}ENABLE_BPF: true${RES}" || echo -e "${GREEN_COLOR}ENABLE_BPF: false${RES}"
127131
[ "$ENABLE_LTO" = "y" ] && echo -e "${GREEN_COLOR}ENABLE_LTO: true${RES}" || echo -e "${GREEN_COLOR}ENABLE_LTO: false${RES}"
@@ -249,6 +253,9 @@ export ENABLE_LTO=$ENABLE_LTO
249253
sed -i '/NaiveProxy/d' .config
250254
}
251255

256+
# mold
257+
[ "$USE_MOLD" = "y" ] && echo 'CONFIG_USE_MOLD=y' >> .config
258+
252259
# openwrt-23.05 gcc11/13
253260
if [ "$USE_GCC13" = "y" ]; then
254261
curl -s https://$mirror/openwrt/generic/config-gcc13 >> .config
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
From 18d4007ad731dfa625a6c03d1a946c453d7b92f2 Mon Sep 17 00:00:00 2001
2+
From: Andre Heider <[email protected]>
3+
Date: Fri, 27 Jan 2023 16:35:46 +0100
4+
Subject: [PATCH 1/8] build: add support to use the mold linker for packages
5+
6+
If CONFIG_USE_MOLD is set, all target packages will use the mold linker.
7+
Except the ones which opted-out via setting PKG_BUILD_FLAGS:=no-mold.
8+
9+
Signed-off-by: Andre Heider <[email protected]>
10+
---
11+
include/package.mk | 5 +++++
12+
1 file changed, 5 insertions(+)
13+
14+
diff --git a/include/package.mk b/include/package.mk
15+
index c391d320aa..1791d6d9d4 100644
16+
--- a/include/package.mk
17+
+++ b/include/package.mk
18+
@@ -55,6 +55,11 @@ ifeq ($(call pkg_build_flag,lto,$(if $(CONFIG_USE_LTO),1,0)),1)
19+
TARGET_CXXFLAGS+= -flto=auto -fno-fat-lto-objects
20+
TARGET_LDFLAGS+= -flto=auto -fuse-linker-plugin
21+
endif
22+
+ifdef CONFIG_USE_MOLD
23+
+ ifeq ($(call pkg_build_flag,mold,1),1)
24+
+ TARGET_LINKER:=mold
25+
+ endif
26+
+endif
27+
28+
include $(INCLUDE_DIR)/hardening.mk
29+
include $(INCLUDE_DIR)/prereq.mk
30+
--
31+
2.39.3
32+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
From b7619a40b1497d974fef2bcc07bfef6da93e049a Mon Sep 17 00:00:00 2001
2+
From: Andre Heider <[email protected]>
3+
Date: Fri, 27 Jan 2023 18:22:43 +0100
4+
Subject: [PATCH 2/8] treewide: opt-out of tree-wide mold usage
5+
6+
These use linker scripts, which mold doesn't support.
7+
8+
Signed-off-by: Andre Heider <[email protected]>
9+
---
10+
include/package.mk | 2 +-
11+
package/boot/grub2/Makefile | 2 +-
12+
package/kernel/lantiq/ltq-ifxos/Makefile | 1 +
13+
package/kernel/lantiq/ltq-vdsl-vr11-mei/Makefile | 1 +
14+
package/kernel/lantiq/ltq-vdsl-vr11/Makefile | 1 +
15+
5 files changed, 5 insertions(+), 2 deletions(-)
16+
17+
diff --git a/include/package.mk b/include/package.mk
18+
index 1791d6d9d4..61a26f0c43 100644
19+
--- a/include/package.mk
20+
+++ b/include/package.mk
21+
@@ -24,7 +24,7 @@ PKG_JOBS?=$(if $(PKG_BUILD_PARALLEL),$(MAKE_J),-j1)
22+
endif
23+
24+
PKG_BUILD_FLAGS?=
25+
-__unknown_flags=$(filter-out no-iremap no-mips16 gc-sections no-gc-sections lto no-lto,$(PKG_BUILD_FLAGS))
26+
+__unknown_flags=$(filter-out no-iremap no-mips16 gc-sections no-gc-sections lto no-lto no-mold,$(PKG_BUILD_FLAGS))
27+
ifneq ($(__unknown_flags),)
28+
$(error unknown PKG_BUILD_FLAGS: $(__unknown_flags))
29+
endif
30+
diff --git a/package/boot/grub2/Makefile b/package/boot/grub2/Makefile
31+
index 865feee9ad..44dafe1ef1 100644
32+
--- a/package/boot/grub2/Makefile
33+
+++ b/package/boot/grub2/Makefile
34+
@@ -25,7 +25,7 @@ ifneq ($(BUILD_VARIANT),none)
35+
endif
36+
37+
PKG_FLAGS:=nonshared
38+
-PKG_BUILD_FLAGS:=no-lto
39+
+PKG_BUILD_FLAGS:=no-lto no-mold
40+
41+
include $(INCLUDE_DIR)/host-build.mk
42+
include $(INCLUDE_DIR)/package.mk
43+
diff --git a/package/kernel/lantiq/ltq-ifxos/Makefile b/package/kernel/lantiq/ltq-ifxos/Makefile
44+
index d941a9d56f..97f7ca78ce 100644
45+
--- a/package/kernel/lantiq/ltq-ifxos/Makefile
46+
+++ b/package/kernel/lantiq/ltq-ifxos/Makefile
47+
@@ -23,6 +23,7 @@ PKG_LICENSE_FILES:=LICENSE
48+
PKG_EXTMOD_SUBDIRS:=src
49+
50+
PKG_FIXUP:=autoreconf
51+
+PKG_BUILD_FLAGS:=no-mold
52+
53+
include $(INCLUDE_DIR)/package.mk
54+
55+
diff --git a/package/kernel/lantiq/ltq-vdsl-vr11-mei/Makefile b/package/kernel/lantiq/ltq-vdsl-vr11-mei/Makefile
56+
index f2dcf8db84..7b8a948179 100644
57+
--- a/package/kernel/lantiq/ltq-vdsl-vr11-mei/Makefile
58+
+++ b/package/kernel/lantiq/ltq-vdsl-vr11-mei/Makefile
59+
@@ -25,6 +25,7 @@ PKG_EXTMOD_SUBDIRS:=src
60+
61+
PKG_FIXUP:=autoreconf
62+
PKG_FLAGS:=nonshared
63+
+PKG_BUILD_FLAGS:=no-mold
64+
65+
include $(INCLUDE_DIR)/package.mk
66+
67+
diff --git a/package/kernel/lantiq/ltq-vdsl-vr11/Makefile b/package/kernel/lantiq/ltq-vdsl-vr11/Makefile
68+
index 8284cba9a7..11f96d744a 100644
69+
--- a/package/kernel/lantiq/ltq-vdsl-vr11/Makefile
70+
+++ b/package/kernel/lantiq/ltq-vdsl-vr11/Makefile
71+
@@ -23,6 +23,7 @@ PKG_LICENSE:=GPL-2.0 BSD-2-Clause
72+
PKG_LICENSE_FILES:=LICENSE
73+
74+
PKG_FIXUP:=autoreconf
75+
+PKG_BUILD_FLAGS:=no-mold
76+
77+
include $(INCLUDE_DIR)/package.mk
78+
79+
--
80+
2.39.3
81+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
From a667acae2ed1d0a99a52e07916b01f42bc9f8f9d Mon Sep 17 00:00:00 2001
2+
From: Andre Heider <[email protected]>
3+
Date: Fri, 27 Jan 2023 17:53:02 +0100
4+
Subject: [PATCH 3/8] toolchain: add mold as additional linker
5+
6+
Install it as $tripple-ld.mold in order to use -fuse-ld=mold.
7+
8+
Signed-off-by: Andre Heider <[email protected]>
9+
---
10+
toolchain/Makefile | 2 +-
11+
toolchain/mold/Makefile | 22 ++++++++++++++++++++++
12+
2 files changed, 23 insertions(+), 1 deletion(-)
13+
create mode 100644 toolchain/mold/Makefile
14+
15+
diff --git a/toolchain/Makefile b/toolchain/Makefile
16+
index c0046293c9..09c16f72a7 100644
17+
--- a/toolchain/Makefile
18+
+++ b/toolchain/Makefile
19+
@@ -27,7 +27,7 @@
20+
curdir:=toolchain
21+
22+
# subdirectories to descend into
23+
-$(curdir)/builddirs := $(if $(CONFIG_GDB),gdb) $(if $(CONFIG_EXTERNAL_TOOLCHAIN),wrapper,kernel-headers binutils gcc/initial gcc/final $(LIBC) fortify-headers) $(if $(CONFIG_NASM),nasm)
24+
+$(curdir)/builddirs := $(if $(CONFIG_GDB),gdb) $(if $(CONFIG_EXTERNAL_TOOLCHAIN),wrapper,kernel-headers binutils gcc/initial gcc/final $(LIBC) fortify-headers) $(if $(CONFIG_NASM),nasm) $(if $(CONFIG_USE_MOLD),mold)
25+
26+
# builddir dependencies
27+
ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
28+
diff --git a/toolchain/mold/Makefile b/toolchain/mold/Makefile
29+
new file mode 100644
30+
index 0000000000..a2acba89d3
31+
--- /dev/null
32+
+++ b/toolchain/mold/Makefile
33+
@@ -0,0 +1,22 @@
34+
+#
35+
+# This is free software, licensed under the GNU General Public License v2.
36+
+# See /LICENSE for more information.
37+
+#
38+
+include $(TOPDIR)/rules.mk
39+
+include $(INCLUDE_DIR)/toolchain-build.mk
40+
+
41+
+define Host/Configure
42+
+endef
43+
+
44+
+define Host/Compile
45+
+endef
46+
+
47+
+define Host/Install
48+
+ $(INSTALL_DIR) $(TOOLCHAIN_DIR)/bin
49+
+ $(INSTALL_BIN) $(STAGING_DIR_HOST)/bin/mold $(TOOLCHAIN_DIR)/bin/$(REAL_GNU_TARGET_NAME)-ld.mold
50+
+endef
51+
+
52+
+define Host/Clean
53+
+endef
54+
+
55+
+$(eval $(call HostBuild))
56+
--
57+
2.39.3
58+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
From a500032a30152630e058c3ee6afbbe787325db43 Mon Sep 17 00:00:00 2001
2+
From: Andre Heider <[email protected]>
3+
Date: Fri, 27 Jan 2023 17:53:02 +0100
4+
Subject: [PATCH 4/8] tools: add mold, a modern linker
5+
6+
mold is a faster drop-in replacement for existing Unix linkers.
7+
8+
A single binary is able to link various targets, which is why this lives
9+
in tools/.
10+
11+
All toolchain builds then just need to copy the linker over, hence avoiding
12+
multiple builds with the same outcome.
13+
14+
Signed-off-by: Andre Heider <[email protected]>
15+
---
16+
tools/Makefile | 2 ++
17+
tools/mold/Makefile | 22 ++++++++++++++++++++++
18+
2 files changed, 24 insertions(+)
19+
create mode 100644 tools/mold/Makefile
20+
21+
diff --git a/tools/Makefile b/tools/Makefile
22+
index 40c3ec1ab0..bf525d34a0 100644
23+
--- a/tools/Makefile
24+
+++ b/tools/Makefile
25+
@@ -83,6 +83,7 @@ tools-$(if $(CONFIG_BUILD_ALL_HOST_TOOLS)$(CONFIG_TARGET_tegra),y) += cbootimage
26+
tools-$(if $(CONFIG_BUILD_ALL_HOST_TOOLS)$(CONFIG_USES_MINOR),y) += kernel2minor
27+
tools-$(if $(CONFIG_BUILD_ALL_HOST_TOOLS)$(CONFIG_USE_SPARSE),y) += sparse
28+
tools-$(if $(CONFIG_BUILD_ALL_HOST_TOOLS)$(CONFIG_USE_LLVM_BUILD),y) += llvm-bpf
29+
+tools-$(if $(CONFIG_BUILD_ALL_HOST_TOOLS)$(CONFIG_USE_MOLD),y) += mold
30+
31+
# builddir dependencies
32+
$(curdir)/autoconf/compile := $(curdir)/m4/compile
33+
@@ -114,6 +115,7 @@ $(curdir)/meson/compile := $(curdir)/ninja/compile
34+
$(curdir)/missing-macros/compile := $(curdir)/autoconf/compile
35+
$(curdir)/mkimage/compile += $(curdir)/bison/compile $(curdir)/libressl/compile
36+
$(curdir)/mklibs/compile := $(curdir)/libtool/compile
37+
+$(curdir)/mold/compile := $(curdir)/cmake/compile $(curdir)/zlib/compile $(curdir)/zstd/compile
38+
$(curdir)/mpc/compile := $(curdir)/mpfr/compile $(curdir)/gmp/compile
39+
$(curdir)/mpfr/compile := $(curdir)/gmp/compile
40+
$(curdir)/mtd-utils/compile := $(curdir)/libtool/compile $(curdir)/e2fsprogs/compile $(curdir)/zlib/compile
41+
diff --git a/tools/mold/Makefile b/tools/mold/Makefile
42+
new file mode 100644
43+
index 0000000000..e8fcecbfed
44+
--- /dev/null
45+
+++ b/tools/mold/Makefile
46+
@@ -0,0 +1,22 @@
47+
+# SPDX-License-Identifier: GPL-2.0-only
48+
+
49+
+include $(TOPDIR)/rules.mk
50+
+
51+
+PKG_NAME:=mold
52+
+PKG_VERSION:=1.11.0
53+
+
54+
+PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
55+
+PKG_SOURCE_URL_FILE:=v$(PKG_VERSION).tar.gz
56+
+PKG_SOURCE_URL:=https://github.com/rui314/mold/archive/refs/tags
57+
+PKG_HASH:=99318eced81b09a77e4c657011076cc8ec3d4b6867bd324b8677974545bc4d6f
58+
+
59+
+include $(INCLUDE_DIR)/host-build.mk
60+
+include $(INCLUDE_DIR)/cmake.mk
61+
+
62+
+CMAKE_HOST_OPTIONS += \
63+
+ -DMOLD_LTO=ON \
64+
+ -DMOLD_MOSTLY_STATIC=ON \
65+
+ -DMOLD_USE_SYSTEM_MIMALLOC=OFF \
66+
+ -DMOLD_USE_SYSTEM_TBB=OFF
67+
+
68+
+$(eval $(call HostBuild))
69+
--
70+
2.39.3
71+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
From 55d51626b8a3fd48a980e5d033ecdbef071137c4 Mon Sep 17 00:00:00 2001
2+
From: Andre Heider <[email protected]>
3+
Date: Sat, 28 Jan 2023 21:16:16 +0100
4+
Subject: [PATCH 5/8] build: replace SSTRIP_ARGS with
5+
SSTRIP_DISCARD_TRAILING_ZEROES
6+
7+
sstrip only has one functional arg. Make that a bool option, which can
8+
easily depend on other knobs then.
9+
10+
This is required to be disabled for the mold linker.
11+
12+
Signed-off-by: Andre Heider <[email protected]>
13+
---
14+
config/Config-build.in | 10 ++++------
15+
rules.mk | 2 +-
16+
2 files changed, 5 insertions(+), 7 deletions(-)
17+
18+
diff --git a/config/Config-build.in b/config/Config-build.in
19+
index fe16d81d36..a96ed2f63b 100644
20+
--- a/config/Config-build.in
21+
+++ b/config/Config-build.in
22+
@@ -190,7 +190,6 @@ menu "Global build settings"
23+
help
24+
This will install binaries stripped using strip from binutils.
25+
26+
-
27+
config USE_SSTRIP
28+
bool "sstrip"
29+
depends on !USE_GLIBC
30+
@@ -207,13 +206,12 @@ menu "Global build settings"
31+
help
32+
Specifies arguments passed to the strip command when stripping binaries.
33+
34+
- config SSTRIP_ARGS
35+
- string
36+
- prompt "Sstrip arguments"
37+
+ config SSTRIP_DISCARD_TRAILING_ZEROES
38+
+ bool "Strip trailing zero bytes"
39+
depends on USE_SSTRIP
40+
- default "-z"
41+
+ default y
42+
help
43+
- Specifies arguments passed to the sstrip command when stripping binaries.
44+
+ Use sstrip's -z option to discard trailing zero bytes
45+
46+
config STRIP_KERNEL_EXPORTS
47+
bool "Strip unnecessary exports from the kernel image"
48+
diff --git a/rules.mk b/rules.mk
49+
index 58c53705a2..c768ccc9b3 100644
50+
--- a/rules.mk
51+
+++ b/rules.mk
52+
@@ -325,7 +325,7 @@ else
53+
STRIP:=$(TARGET_CROSS)strip $(call qstrip,$(CONFIG_STRIP_ARGS))
54+
else
55+
ifneq ($(CONFIG_USE_SSTRIP),)
56+
- STRIP:=$(STAGING_DIR_HOST)/bin/sstrip $(call qstrip,$(CONFIG_SSTRIP_ARGS))
57+
+ STRIP:=$(STAGING_DIR_HOST)/bin/sstrip $(if $(CONFIG_SSTRIP_DISCARD_TRAILING_ZEROES),-z)
58+
endif
59+
endif
60+
RSTRIP= \
61+
--
62+
2.39.3
63+

0 commit comments

Comments
 (0)