Skip to content

Commit

Permalink
add mold linker support
Browse files Browse the repository at this point in the history
Signed-off-by: sbwml <admin@cooluc.com>
  • Loading branch information
sbwml committed Jan 17, 2024
1 parent 675620a commit 0826471
Show file tree
Hide file tree
Showing 11 changed files with 480 additions and 8 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ sudo apt-get install -y clang-15
export USE_GLIBC=y
```

### 启用 [GCC13](https://gcc.gnu.org/gcc-13/) 工具链编译
##### 只需在构建固件前执行以下命令即可启用 GCC13 工具链

```
export USE_GCC13=y
```

### 启用 [eBPF](https://docs.kernel.org/bpf/) 支持
##### 只需在构建固件前执行以下命令即可启用 eBPF 支持

Expand Down
9 changes: 8 additions & 1 deletion openwrt/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ export platform=$2
[ "$platform" = "x86_64" ] && export platform="x86_64" toolchain_arch="x86_64"

# gcc 13
export USE_GCC13=$USE_GCC13
USE_GCC13=y

# use glibc
export USE_GLIBC=$USE_GLIBC

# use mold
USE_MOLD=y

# lrng
export ENABLE_LRNG=$ENABLE_LRNG

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

[ "$USE_GCC13" = "y" ] && echo -e "${GREEN_COLOR}GCC VERSION: 13${RES}" || echo -e "${GREEN_COLOR}GCC VERSION: 11${RES}"
[ "$USE_MOLD" = "y" ] && echo -e "${GREEN_COLOR}USE_MOLD: true${RES}" || echo -e "${GREEN_COLOR}USE_MOLD: false${RES}"
[ "$ENABLE_OTA" = "y" ] && echo -e "${GREEN_COLOR}ENABLE_OTA: true${RES}" || echo -e "${GREEN_COLOR}ENABLE_OTA: false${RES}"
[ "$ENABLE_BPF" = "y" ] && echo -e "${GREEN_COLOR}ENABLE_BPF: true${RES}" || echo -e "${GREEN_COLOR}ENABLE_BPF: false${RES}"
[ "$ENABLE_LTO" = "y" ] && echo -e "${GREEN_COLOR}ENABLE_LTO: true${RES}" || echo -e "${GREEN_COLOR}ENABLE_LTO: false${RES}"
Expand Down Expand Up @@ -249,6 +253,9 @@ export ENABLE_LTO=$ENABLE_LTO
sed -i '/NaiveProxy/d' .config
}

# mold
[ "$USE_MOLD" = "y" ] && echo 'CONFIG_USE_MOLD=y' >> .config

# openwrt-23.05 gcc11/13
if [ "$USE_GCC13" = "y" ]; then
curl -s https://$mirror/openwrt/generic/config-gcc13 >> .config
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
From 18d4007ad731dfa625a6c03d1a946c453d7b92f2 Mon Sep 17 00:00:00 2001
From: Andre Heider <a.heider@gmail.com>
Date: Fri, 27 Jan 2023 16:35:46 +0100
Subject: [PATCH 1/8] build: add support to use the mold linker for packages

If CONFIG_USE_MOLD is set, all target packages will use the mold linker.
Except the ones which opted-out via setting PKG_BUILD_FLAGS:=no-mold.

Signed-off-by: Andre Heider <a.heider@gmail.com>
---
include/package.mk | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/include/package.mk b/include/package.mk
index c391d320aa..1791d6d9d4 100644
--- a/include/package.mk
+++ b/include/package.mk
@@ -55,6 +55,11 @@ ifeq ($(call pkg_build_flag,lto,$(if $(CONFIG_USE_LTO),1,0)),1)
TARGET_CXXFLAGS+= -flto=auto -fno-fat-lto-objects
TARGET_LDFLAGS+= -flto=auto -fuse-linker-plugin
endif
+ifdef CONFIG_USE_MOLD
+ ifeq ($(call pkg_build_flag,mold,1),1)
+ TARGET_LINKER:=mold
+ endif
+endif

include $(INCLUDE_DIR)/hardening.mk
include $(INCLUDE_DIR)/prereq.mk
--
2.39.3

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
From b7619a40b1497d974fef2bcc07bfef6da93e049a Mon Sep 17 00:00:00 2001
From: Andre Heider <a.heider@gmail.com>
Date: Fri, 27 Jan 2023 18:22:43 +0100
Subject: [PATCH 2/8] treewide: opt-out of tree-wide mold usage

These use linker scripts, which mold doesn't support.

Signed-off-by: Andre Heider <a.heider@gmail.com>
---
include/package.mk | 2 +-
package/boot/grub2/Makefile | 2 +-
package/kernel/lantiq/ltq-ifxos/Makefile | 1 +
package/kernel/lantiq/ltq-vdsl-vr11-mei/Makefile | 1 +
package/kernel/lantiq/ltq-vdsl-vr11/Makefile | 1 +
5 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/include/package.mk b/include/package.mk
index 1791d6d9d4..61a26f0c43 100644
--- a/include/package.mk
+++ b/include/package.mk
@@ -24,7 +24,7 @@ PKG_JOBS?=$(if $(PKG_BUILD_PARALLEL),$(MAKE_J),-j1)
endif

PKG_BUILD_FLAGS?=
-__unknown_flags=$(filter-out no-iremap no-mips16 gc-sections no-gc-sections lto no-lto,$(PKG_BUILD_FLAGS))
+__unknown_flags=$(filter-out no-iremap no-mips16 gc-sections no-gc-sections lto no-lto no-mold,$(PKG_BUILD_FLAGS))
ifneq ($(__unknown_flags),)
$(error unknown PKG_BUILD_FLAGS: $(__unknown_flags))
endif
diff --git a/package/boot/grub2/Makefile b/package/boot/grub2/Makefile
index 865feee9ad..44dafe1ef1 100644
--- a/package/boot/grub2/Makefile
+++ b/package/boot/grub2/Makefile
@@ -25,7 +25,7 @@ ifneq ($(BUILD_VARIANT),none)
endif

PKG_FLAGS:=nonshared
-PKG_BUILD_FLAGS:=no-lto
+PKG_BUILD_FLAGS:=no-lto no-mold

include $(INCLUDE_DIR)/host-build.mk
include $(INCLUDE_DIR)/package.mk
diff --git a/package/kernel/lantiq/ltq-ifxos/Makefile b/package/kernel/lantiq/ltq-ifxos/Makefile
index d941a9d56f..97f7ca78ce 100644
--- a/package/kernel/lantiq/ltq-ifxos/Makefile
+++ b/package/kernel/lantiq/ltq-ifxos/Makefile
@@ -23,6 +23,7 @@ PKG_LICENSE_FILES:=LICENSE
PKG_EXTMOD_SUBDIRS:=src

PKG_FIXUP:=autoreconf
+PKG_BUILD_FLAGS:=no-mold

include $(INCLUDE_DIR)/package.mk

diff --git a/package/kernel/lantiq/ltq-vdsl-vr11-mei/Makefile b/package/kernel/lantiq/ltq-vdsl-vr11-mei/Makefile
index f2dcf8db84..7b8a948179 100644
--- a/package/kernel/lantiq/ltq-vdsl-vr11-mei/Makefile
+++ b/package/kernel/lantiq/ltq-vdsl-vr11-mei/Makefile
@@ -25,6 +25,7 @@ PKG_EXTMOD_SUBDIRS:=src

PKG_FIXUP:=autoreconf
PKG_FLAGS:=nonshared
+PKG_BUILD_FLAGS:=no-mold

include $(INCLUDE_DIR)/package.mk

diff --git a/package/kernel/lantiq/ltq-vdsl-vr11/Makefile b/package/kernel/lantiq/ltq-vdsl-vr11/Makefile
index 8284cba9a7..11f96d744a 100644
--- a/package/kernel/lantiq/ltq-vdsl-vr11/Makefile
+++ b/package/kernel/lantiq/ltq-vdsl-vr11/Makefile
@@ -23,6 +23,7 @@ PKG_LICENSE:=GPL-2.0 BSD-2-Clause
PKG_LICENSE_FILES:=LICENSE

PKG_FIXUP:=autoreconf
+PKG_BUILD_FLAGS:=no-mold

include $(INCLUDE_DIR)/package.mk

--
2.39.3

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
From a667acae2ed1d0a99a52e07916b01f42bc9f8f9d Mon Sep 17 00:00:00 2001
From: Andre Heider <a.heider@gmail.com>
Date: Fri, 27 Jan 2023 17:53:02 +0100
Subject: [PATCH 3/8] toolchain: add mold as additional linker

Install it as $tripple-ld.mold in order to use -fuse-ld=mold.

Signed-off-by: Andre Heider <a.heider@gmail.com>
---
toolchain/Makefile | 2 +-
toolchain/mold/Makefile | 22 ++++++++++++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)
create mode 100644 toolchain/mold/Makefile

diff --git a/toolchain/Makefile b/toolchain/Makefile
index c0046293c9..09c16f72a7 100644
--- a/toolchain/Makefile
+++ b/toolchain/Makefile
@@ -27,7 +27,7 @@
curdir:=toolchain

# subdirectories to descend into
-$(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)
+$(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)

# builddir dependencies
ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
diff --git a/toolchain/mold/Makefile b/toolchain/mold/Makefile
new file mode 100644
index 0000000000..a2acba89d3
--- /dev/null
+++ b/toolchain/mold/Makefile
@@ -0,0 +1,22 @@
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+include $(TOPDIR)/rules.mk
+include $(INCLUDE_DIR)/toolchain-build.mk
+
+define Host/Configure
+endef
+
+define Host/Compile
+endef
+
+define Host/Install
+ $(INSTALL_DIR) $(TOOLCHAIN_DIR)/bin
+ $(INSTALL_BIN) $(STAGING_DIR_HOST)/bin/mold $(TOOLCHAIN_DIR)/bin/$(REAL_GNU_TARGET_NAME)-ld.mold
+endef
+
+define Host/Clean
+endef
+
+$(eval $(call HostBuild))
--
2.39.3

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
From a500032a30152630e058c3ee6afbbe787325db43 Mon Sep 17 00:00:00 2001
From: Andre Heider <a.heider@gmail.com>
Date: Fri, 27 Jan 2023 17:53:02 +0100
Subject: [PATCH 4/8] tools: add mold, a modern linker

mold is a faster drop-in replacement for existing Unix linkers.

A single binary is able to link various targets, which is why this lives
in tools/.

All toolchain builds then just need to copy the linker over, hence avoiding
multiple builds with the same outcome.

Signed-off-by: Andre Heider <a.heider@gmail.com>
---
tools/Makefile | 2 ++
tools/mold/Makefile | 22 ++++++++++++++++++++++
2 files changed, 24 insertions(+)
create mode 100644 tools/mold/Makefile

diff --git a/tools/Makefile b/tools/Makefile
index 40c3ec1ab0..bf525d34a0 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -83,6 +83,7 @@ tools-$(if $(CONFIG_BUILD_ALL_HOST_TOOLS)$(CONFIG_TARGET_tegra),y) += cbootimage
tools-$(if $(CONFIG_BUILD_ALL_HOST_TOOLS)$(CONFIG_USES_MINOR),y) += kernel2minor
tools-$(if $(CONFIG_BUILD_ALL_HOST_TOOLS)$(CONFIG_USE_SPARSE),y) += sparse
tools-$(if $(CONFIG_BUILD_ALL_HOST_TOOLS)$(CONFIG_USE_LLVM_BUILD),y) += llvm-bpf
+tools-$(if $(CONFIG_BUILD_ALL_HOST_TOOLS)$(CONFIG_USE_MOLD),y) += mold

# builddir dependencies
$(curdir)/autoconf/compile := $(curdir)/m4/compile
@@ -114,6 +115,7 @@ $(curdir)/meson/compile := $(curdir)/ninja/compile
$(curdir)/missing-macros/compile := $(curdir)/autoconf/compile
$(curdir)/mkimage/compile += $(curdir)/bison/compile $(curdir)/libressl/compile
$(curdir)/mklibs/compile := $(curdir)/libtool/compile
+$(curdir)/mold/compile := $(curdir)/cmake/compile $(curdir)/zlib/compile $(curdir)/zstd/compile
$(curdir)/mpc/compile := $(curdir)/mpfr/compile $(curdir)/gmp/compile
$(curdir)/mpfr/compile := $(curdir)/gmp/compile
$(curdir)/mtd-utils/compile := $(curdir)/libtool/compile $(curdir)/e2fsprogs/compile $(curdir)/zlib/compile
diff --git a/tools/mold/Makefile b/tools/mold/Makefile
new file mode 100644
index 0000000000..e8fcecbfed
--- /dev/null
+++ b/tools/mold/Makefile
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=mold
+PKG_VERSION:=1.11.0
+
+PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
+PKG_SOURCE_URL_FILE:=v$(PKG_VERSION).tar.gz
+PKG_SOURCE_URL:=https://github.com/rui314/mold/archive/refs/tags
+PKG_HASH:=99318eced81b09a77e4c657011076cc8ec3d4b6867bd324b8677974545bc4d6f
+
+include $(INCLUDE_DIR)/host-build.mk
+include $(INCLUDE_DIR)/cmake.mk
+
+CMAKE_HOST_OPTIONS += \
+ -DMOLD_LTO=ON \
+ -DMOLD_MOSTLY_STATIC=ON \
+ -DMOLD_USE_SYSTEM_MIMALLOC=OFF \
+ -DMOLD_USE_SYSTEM_TBB=OFF
+
+$(eval $(call HostBuild))
--
2.39.3

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
From 55d51626b8a3fd48a980e5d033ecdbef071137c4 Mon Sep 17 00:00:00 2001
From: Andre Heider <a.heider@gmail.com>
Date: Sat, 28 Jan 2023 21:16:16 +0100
Subject: [PATCH 5/8] build: replace SSTRIP_ARGS with
SSTRIP_DISCARD_TRAILING_ZEROES

sstrip only has one functional arg. Make that a bool option, which can
easily depend on other knobs then.

This is required to be disabled for the mold linker.

Signed-off-by: Andre Heider <a.heider@gmail.com>
---
config/Config-build.in | 10 ++++------
rules.mk | 2 +-
2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/config/Config-build.in b/config/Config-build.in
index fe16d81d36..a96ed2f63b 100644
--- a/config/Config-build.in
+++ b/config/Config-build.in
@@ -190,7 +190,6 @@ menu "Global build settings"
help
This will install binaries stripped using strip from binutils.

-
config USE_SSTRIP
bool "sstrip"
depends on !USE_GLIBC
@@ -207,13 +206,12 @@ menu "Global build settings"
help
Specifies arguments passed to the strip command when stripping binaries.

- config SSTRIP_ARGS
- string
- prompt "Sstrip arguments"
+ config SSTRIP_DISCARD_TRAILING_ZEROES
+ bool "Strip trailing zero bytes"
depends on USE_SSTRIP
- default "-z"
+ default y
help
- Specifies arguments passed to the sstrip command when stripping binaries.
+ Use sstrip's -z option to discard trailing zero bytes

config STRIP_KERNEL_EXPORTS
bool "Strip unnecessary exports from the kernel image"
diff --git a/rules.mk b/rules.mk
index 58c53705a2..c768ccc9b3 100644
--- a/rules.mk
+++ b/rules.mk
@@ -325,7 +325,7 @@ else
STRIP:=$(TARGET_CROSS)strip $(call qstrip,$(CONFIG_STRIP_ARGS))
else
ifneq ($(CONFIG_USE_SSTRIP),)
- STRIP:=$(STAGING_DIR_HOST)/bin/sstrip $(call qstrip,$(CONFIG_SSTRIP_ARGS))
+ STRIP:=$(STAGING_DIR_HOST)/bin/sstrip $(if $(CONFIG_SSTRIP_DISCARD_TRAILING_ZEROES),-z)
endif
endif
RSTRIP= \
--
2.39.3

Loading

0 comments on commit 0826471

Please sign in to comment.