treewide: add support for "gc-sections" in PKG_BUILD_FLAGS
authorAndre Heider <a.heider@gmail.com>
Thu, 2 Feb 2023 20:16:21 +0000 (21:16 +0100)
committerChristian Marangi <ansuelsmth@gmail.com>
Tue, 21 Mar 2023 17:28:22 +0000 (18:28 +0100)
This reduces open coding and allows to easily add a knob to
enable it treewide, where chosen packages can still opt-out via
"no-gc-sections".

Note: libnl, mbedtls and opkg only used the CFLAGS part without the
LDFLAGS counterpart. That doesn't help at all if the goal is to produce
smaller binaries. I consider that an accident, and this fixes it.

Note: there are also packages using only the LDFLAGS part. I didn't
touch those, as gc might have been disabled via CFLAGS intentionally.

Signed-off-by: Andre Heider <a.heider@gmail.com>
20 files changed:
include/package.mk
package/boot/kexec-tools/Makefile
package/libs/libevent2/Makefile
package/libs/libnl/Makefile
package/libs/mbedtls/Makefile
package/libs/openssl/Makefile
package/network/config/firewall/Makefile
package/network/services/dropbear/Makefile
package/network/services/hostapd/Makefile
package/network/services/ppp/Makefile
package/network/utils/iproute2/Makefile
package/network/utils/iptables/Makefile
package/network/utils/iw/Makefile
package/network/utils/tcpdump/Makefile
package/network/utils/umbim/Makefile
package/network/utils/uqmi/Makefile
package/system/opkg/Makefile
package/utils/e2fsprogs/Makefile
package/utils/mdadm/Makefile
package/utils/mtd-utils/Makefile

index a1717a603d500a67fda46df79c88a01f9da88977..4d97cda00f0b5c66fba3398d0292166034d3ecef 100644 (file)
@@ -30,7 +30,7 @@ ifneq ($(strip $(PKG_USE_MIPS16)),1)
   PKG_BUILD_FLAGS+=no-mips16
 endif
 
-__unknown_flags=$(filter-out no-iremap no-mips16,$(PKG_BUILD_FLAGS))
+__unknown_flags=$(filter-out no-iremap no-mips16 gc-sections no-gc-sections,$(PKG_BUILD_FLAGS))
 ifneq ($(__unknown_flags),)
   $(error unknown PKG_BUILD_FLAGS: $(__unknown_flags))
 endif
@@ -51,6 +51,11 @@ ifdef CONFIG_USE_MIPS16
     TARGET_CXXFLAGS += -mips16 -minterlink-mips16
   endif
 endif
+ifeq ($(call pkg_build_flag,gc-sections,0),1)
+  TARGET_CFLAGS+= -ffunction-sections -fdata-sections
+  TARGET_CXXFLAGS+= -ffunction-sections -fdata-sections
+  TARGET_LDFLAGS+= -Wl,--gc-sections
+endif
 
 include $(INCLUDE_DIR)/hardening.mk
 include $(INCLUDE_DIR)/prereq.mk
index 0cb7688a24b392c8e0eae4a91d66091da1290379..7f06ec7d642f9f2c19b8ae2fc7a7813cd8572bd9 100644 (file)
@@ -17,6 +17,8 @@ PKG_HASH:=7fe36a064101cd5c515e41b2be393dce3ca88adce59d6ee668e0af7c0c4570cd
 
 PKG_CONFIG_DEPENDS := CONFIG_KEXEC_ZLIB CONFIG_KEXEC_LZMA
 
+PKG_BUILD_FLAGS:=gc-sections
+
 include $(INCLUDE_DIR)/package.mk
 
 define Package/kexec-tools/Default
@@ -82,9 +84,6 @@ CONFIGURE_ARGS = \
                $(if $(CONFIG_KEXEC_LZMA),--with,--without)-lzma \
                TARGET_LD="$(TARGET_CROSS)ld"
 
-TARGET_CFLAGS += -ffunction-sections -fdata-sections
-TARGET_LDFLAGS += -Wl,--gc-sections
-
 CONFIGURE_VARS += \
        BUILD_CC="$(HOSTCC)" \
        TARGET_CC="$(TARGET_CC)"
index 85c159c2a6975388d8b17d2cce1d734dcd3e8031..50dd19bf00054df5ff660a67f5a4f8e84613fc28 100644 (file)
@@ -26,6 +26,8 @@ PKG_CONFIG_DEPENDS:= \
        CONFIG_PACKAGE_libevent2-pthreads \
        CONFIG_PACKAGE_libevent2-mbedtls
 
+PKG_BUILD_FLAGS:=gc-sections
+
 include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/cmake.mk
 
@@ -112,8 +114,8 @@ define Package/libevent2-pthreads/description
        threading & locking.
 endef
 
-TARGET_CFLAGS += $(FPIC) -ffunction-sections -fdata-sections -flto
-TARGET_LDFLAGS += -Wl,--gc-sections,--as-needed -flto
+TARGET_CFLAGS += $(FPIC) -flto
+TARGET_LDFLAGS += -Wl,--as-needed -flto
 
 CMAKE_OPTIONS += \
        -DEVENT__DISABLE_BENCHMARK:BOOL=ON \
index 71b043a9e565073dba118b7c838d6c2a8e73c3d6..56549dcc0a24a2dea0b3e28418178ea4dbaa0b3d 100644 (file)
@@ -20,6 +20,8 @@ PKG_CPE_ID:=cpe:/a:libnl_project:libnl
 PKG_INSTALL:=1
 PKG_FIXUP:=autoreconf
 
+PKG_BUILD_FLAGS:=gc-sections
+
 include $(INCLUDE_DIR)/package.mk
 
 define Package/libnl/default
@@ -80,7 +82,7 @@ define Package/libnl/description
  message construction and parsing, object caching system, etc.
 endef
 
-TARGET_CFLAGS += -ffunction-sections -fdata-sections $(FPIC)
+TARGET_CFLAGS += $(FPIC)
 
 CONFIGURE_ARGS += \
        --disable-debug
index 7c167e2296d2a705290ba07a560d01017845ebb9..f49bb24de6357e14dd8806b4816250a2692ee985 100644 (file)
@@ -10,7 +10,7 @@ include $(TOPDIR)/rules.mk
 PKG_NAME:=mbedtls
 PKG_VERSION:=2.28.2
 PKG_RELEASE:=2
-PKG_BUILD_FLAGS:=no-mips16
+PKG_BUILD_FLAGS:=no-mips16 gc-sections
 
 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
 PKG_SOURCE_URL:=https://codeload.github.com/ARMmbed/mbedtls/tar.gz/v$(PKG_VERSION)?
@@ -121,7 +121,6 @@ This package contains mbedtls helper programs for private key and
 CSR generation (gen_key, cert_req)
 endef
 
-TARGET_CFLAGS += -ffunction-sections -fdata-sections
 TARGET_CFLAGS := $(filter-out -O%,$(TARGET_CFLAGS))
 
 CMAKE_OPTIONS += \
index 7918d40347b929174ef8e5271e890785f3080640..9fe5da9dd34a6c714e063a6e6ac93b90421b7423 100644 (file)
@@ -10,7 +10,7 @@ include $(TOPDIR)/rules.mk
 PKG_NAME:=openssl
 PKG_VERSION:=3.0.8
 PKG_RELEASE:=4
-PKG_BUILD_FLAGS:=no-mips16
+PKG_BUILD_FLAGS:=no-mips16 gc-sections
 
 PKG_BUILD_PARALLEL:=1
 
@@ -339,8 +339,7 @@ define Build/Configure
        )
 endef
 
-TARGET_CFLAGS += $(FPIC) -ffunction-sections -fdata-sections
-TARGET_LDFLAGS += -Wl,--gc-sections
+TARGET_CFLAGS += $(FPIC)
 
 define Build/Compile
        +$(MAKE) $(PKG_JOBS) -C $(PKG_BUILD_DIR) \
index e4a3ad97f776fc5560e857487f9828628ffbaa1e..95e2ae4b674dd313d8368ac917c29e9bf6efd4ec 100644 (file)
@@ -21,6 +21,8 @@ PKG_LICENSE:=ISC
 
 PKG_CONFIG_DEPENDS := CONFIG_IPV6
 
+PKG_BUILD_FLAGS:=gc-sections
+
 include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/cmake.mk
 
@@ -42,8 +44,8 @@ define Package/firewall/conffiles
 /etc/firewall.user
 endef
 
-TARGET_CFLAGS += -ffunction-sections -fdata-sections -flto
-TARGET_LDFLAGS += -Wl,--gc-sections -flto
+TARGET_CFLAGS += -flto
+TARGET_LDFLAGS += -flto
 CMAKE_OPTIONS += $(if $(CONFIG_IPV6),,-DDISABLE_IPV6=1)
 
 define Package/firewall/install
index ddd19bfa553eba5b7c54d3901989c24c8082d71b..1b3fdad067bc46ee71eecc96b8a77c6000a60c71 100644 (file)
@@ -23,7 +23,7 @@ PKG_CPE_ID:=cpe:/a:matt_johnston:dropbear_ssh_server
 
 PKG_BUILD_PARALLEL:=1
 PKG_ASLR_PIE_REGULAR:=1
-PKG_BUILD_FLAGS:=no-mips16
+PKG_BUILD_FLAGS:=no-mips16 gc-sections
 PKG_FIXUP:=autoreconf
 PKG_FLAGS:=nonshared
 
@@ -142,8 +142,8 @@ DB_OPT_CONFIG = \
        DROPBEAR_SVR_AGENTFWD|CONFIG_DROPBEAR_AGENTFORWARD|1|0 \
 
 
-TARGET_CFLAGS += -DARGTYPE=3 -ffunction-sections -fdata-sections -flto
-TARGET_LDFLAGS += -Wl,--gc-sections -flto=jobserver
+TARGET_CFLAGS += -DARGTYPE=3 -flto
+TARGET_LDFLAGS += -flto=jobserver
 
 db_opt_add     =echo '\#define $(1) $(2)' >> $(PKG_BUILD_DIR)/localoptions.h
 db_opt_replace =$(ESED) 's,^(\#define $(1)) .*$$$$,\1 $(2),g' $(PKG_BUILD_DIR)/sysoptions.h
index 889f1455047400671d13b541fae3f4974368e1c4..1b077530e13d9ec1f24892e2654eab8c6c6ca882 100644 (file)
@@ -32,6 +32,8 @@ PKG_CONFIG_DEPENDS:= \
        CONFIG_DRIVER_11AX_SUPPORT \
        CONFIG_WPA_ENABLE_WEP
 
+PKG_BUILD_FLAGS:=gc-sections
+
 EAPOL_TEST_PROVIDERS:=eapol-test eapol-test-openssl eapol-test-wolfssl
 
 SUPPLICANT_PROVIDERS:=
@@ -585,8 +587,8 @@ TARGET_CPPFLAGS := \
        -D_GNU_SOURCE \
        $(if $(CONFIG_WPA_MSG_MIN_PRIORITY),-DCONFIG_MSG_MIN_PRIORITY=$(CONFIG_WPA_MSG_MIN_PRIORITY))
 
-TARGET_CFLAGS += -ffunction-sections -fdata-sections -flto
-TARGET_LDFLAGS += -Wl,--gc-sections -flto=jobserver -fuse-linker-plugin -lubox -lubus
+TARGET_CFLAGS += -flto
+TARGET_LDFLAGS += -flto=jobserver -fuse-linker-plugin -lubox -lubus
 
 ifdef CONFIG_PACKAGE_kmod-cfg80211
   TARGET_LDFLAGS += -lm -lnl-tiny
index 8476204e14fcda827c14a45e167057d2424143f7..48f2091a1fce7fe1307b068fcc1174888b91da9c 100644 (file)
@@ -26,6 +26,7 @@ PKG_VERSION:=$(PKG_RELEASE_VERSION).git-$(PKG_SOURCE_DATE)
 PKG_BUILD_DEPENDS:=libpcap
 
 PKG_ASLR_PIE_REGULAR:=1
+PKG_BUILD_FLAGS:=gc-sections
 PKG_BUILD_PARALLEL:=1
 PKG_INSTALL:=1
 
@@ -192,8 +193,8 @@ $(call Build/Configure/Default,, \
                $(PKG_BUILD_DIR)/pppd/plugins/pppoatm/linux/
 endef
 
-TARGET_CFLAGS += -ffunction-sections -fdata-sections -flto
-TARGET_LDFLAGS += -Wl,--gc-sections -flto -fuse-linker-plugin
+TARGET_CFLAGS += -flto
+TARGET_LDFLAGS += -flto -fuse-linker-plugin
 
 MAKE_FLAGS += COPTS="$(TARGET_CFLAGS)" \
                PRECOMPILED_FILTER=1 \
index 302d57bf8b12b2e9ec0e81ef69b3ee2c42845f83..b3a809e0b57e880d30c4b614a5551fc7f4fe6c27 100644 (file)
@@ -19,6 +19,8 @@ PKG_BUILD_DEPENDS:=iptables
 PKG_LICENSE:=GPL-2.0
 PKG_CPE_ID:=cpe:/a:iproute2_project:iproute2
 
+PKG_BUILD_FLAGS:=gc-sections
+
 include $(INCLUDE_DIR)/kernel.mk
 include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/nls.mk
@@ -171,8 +173,8 @@ define Build/Configure
                > $(PKG_BUILD_DIR)/include/SNAPSHOT.h
 endef
 
-TARGET_CFLAGS += -ffunction-sections -fdata-sections -flto
-TARGET_LDFLAGS += -Wl,--gc-sections -Wl,--as-needed
+TARGET_CFLAGS += -flto
+TARGET_LDFLAGS += -Wl,--as-needed
 TARGET_CPPFLAGS += -I$(STAGING_DIR)/usr/include/libnl-tiny
 
 MAKE_FLAGS += \
index e96cfa0b16b6164fa358c3aa92b384a8e05d792f..0d6d76fc080fca17f8cca9243ae319a3c00a25bc 100644 (file)
@@ -20,6 +20,7 @@ PKG_FIXUP:=autoreconf
 PKG_FLAGS:=nonshared
 
 PKG_INSTALL:=1
+PKG_BUILD_FLAGS:=gc-sections
 PKG_BUILD_PARALLEL:=1
 PKG_LICENSE:=GPL-2.0
 PKG_CPE_ID:=cpe:/a:netfilter_core_team:iptables
@@ -591,12 +592,8 @@ TARGET_CPPFLAGS := \
 TARGET_CFLAGS += \
        -I$(PKG_BUILD_DIR)/include \
        -I$(LINUX_DIR)/user_headers/include \
-       -ffunction-sections -fdata-sections \
        -DNO_LEGACY
 
-TARGET_LDFLAGS += \
-       -Wl,--gc-sections
-
 CONFIGURE_ARGS += \
        --enable-shared \
        --enable-static \
index eb3a9ba091d2a2c3674316898f430a50b9c1bf1e..1eb3cd721a2de6ee39f137dfd257813773be5355 100644 (file)
@@ -18,6 +18,8 @@ PKG_HASH:=f167bbe947dd53bb9ebc0c1dcef5db6ad73ac1d6084f2c6f9376c5c360cc4d4e
 PKG_MAINTAINER:=Felix Fietkau <nbd@nbd.name>
 PKG_LICENSE:=GPL-2.0
 
+PKG_BUILD_FLAGS:=gc-sections
+
 include $(INCLUDE_DIR)/package.mk
 
 define Package/iw
@@ -55,8 +57,8 @@ ifeq ($(BUILD_VARIANT),full)
 endif
 
 MAKE_FLAGS += \
-       CFLAGS="$(TARGET_CPPFLAGS) $(TARGET_CFLAGS) -ffunction-sections -fdata-sections" \
-       LDFLAGS="$(TARGET_LDFLAGS) -Wl,--gc-sections -flto" \
+       CFLAGS="$(TARGET_CPPFLAGS) $(TARGET_CFLAGS)" \
+       LDFLAGS="$(TARGET_LDFLAGS) -flto" \
        NL1FOUND="" NL2FOUND=Y \
        NLLIBNAME="libnl-tiny" \
        LIBS="-lm -lnl-tiny" \
index 827a66e8604feab0cd4234b6b02f07be8f63eed0..e07e9feb6dba40ab856d6299e4eeee9b57fa39ff 100644 (file)
@@ -20,6 +20,7 @@ PKG_LICENSE:=BSD-3-Clause
 PKG_CPE_ID:=cpe:/a:tcpdump:tcpdump
 
 PKG_INSTALL:=1
+PKG_BUILD_FLAGS:=gc-sections
 PKG_BUILD_PARALLEL:=1
 
 include $(INCLUDE_DIR)/package.mk
@@ -48,9 +49,6 @@ CONFIGURE_ARGS += \
        --without-crypto \
        $(call autoconf_bool,CONFIG_IPV6,ipv6)
 
-TARGET_CFLAGS += -ffunction-sections -fdata-sections
-TARGET_LDFLAGS += -Wl,--gc-sections
-
 ifeq ($(BUILD_VARIANT),mini)
   TARGET_CFLAGS += -DTCPDUMP_MINI
   CONFIGURE_ARGS += --disable-smb
index 3563949c67c4ca41bf1d66ca4553ed5442bdbe09..1a09984a43ffa2b7938f9bf7420bea2de472c704 100644 (file)
@@ -14,6 +14,7 @@ PKG_LICENSE:=GPL-2.0
 PKG_LICENSE_FILES:=
 
 PKG_FLAGS:=nonshared
+PKG_BUILD_FLAGS:=gc-sections
 
 include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/cmake.mk
@@ -32,9 +33,7 @@ define Package/umbim/description
 endef
 
 TARGET_CFLAGS += \
-       -I$(STAGING_DIR)/usr/include -ffunction-sections -fdata-sections
-
-TARGET_LDFLAGS += -Wl,--gc-sections
+       -I$(STAGING_DIR)/usr/include
 
 define Package/umbim/install
        $(INSTALL_DIR) $(1)/sbin
index 02265d400c1db33cc020f8150e1c9a37945097c6..989e7c11b6efeb688cfb3e0bc4522db07e74860b 100644 (file)
@@ -14,6 +14,7 @@ PKG_LICENSE:=GPL-2.0
 PKG_LICENSE_FILES:=
 
 PKG_FLAGS:=nonshared
+PKG_BUILD_FLAGS:=gc-sections
 
 include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/cmake.mk
@@ -33,13 +34,9 @@ endef
 
 TARGET_CFLAGS += \
        -I$(STAGING_DIR)/usr/include \
-       -ffunction-sections \
-       -fdata-sections \
        -Wno-error=dangling-pointer \
        -Wno-error=maybe-uninitialized
 
-TARGET_LDFLAGS += -Wl,--gc-sections
-
 CMAKE_OPTIONS += \
        -DDEBUG=1
 
index 83c9dab1b020e8f8dd2d2a7feff1c4c222802a38..1841e190e614f9ebe500ef497d238529fdfa8d4a 100644 (file)
@@ -28,6 +28,8 @@ PKG_CONFIG_DEPENDS += \
 
 HOST_BUILD_DEPENDS:=libubox/host
 
+PKG_BUILD_FLAGS:=gc-sections
+
 include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/host-build.mk
 include $(INCLUDE_DIR)/cmake.mk
@@ -57,7 +59,6 @@ define Package/opkg/conffiles
 /etc/opkg/customfeeds.conf
 endef
 
-TARGET_CFLAGS += -ffunction-sections -fdata-sections
 EXTRA_CFLAGS += $(TARGET_CPPFLAGS)
 
 CMAKE_OPTIONS += \
index e6c5cfe75c86d39e83bd1fd37dfe50231b63e48a..cd9b10e17596a610e71c6d6248424134f19471cd 100644 (file)
@@ -23,6 +23,7 @@ PKG_BUILD_DEPENDS:=util-linux e2fsprogs/host
 PKG_INSTALL:=1
 
 PKG_BUILD_PARALLEL:=1
+PKG_BUILD_FLAGS:=gc-sections
 
 include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/host-build.mk
@@ -142,7 +143,7 @@ $(call Package/e2fsprogs)
   DEPENDS:= +e2fsprogs
 endef
 
-TARGET_CFLAGS += $(FPIC) -ffunction-sections -fdata-sections -flto
+TARGET_CFLAGS += $(FPIC) -flto
 
 TARGET_LDFLAGS += -flto
 
@@ -171,7 +172,6 @@ define Build/Compile
                V=$(if $(findstring c,$(OPENWRT_VERBOSE)),1,) \
                subst
        +$(MAKE) $(PKG_JOBS) -C $(PKG_BUILD_DIR) \
-               LDFLAGS=-Wl,--gc-sections \
                BUILDCC="$(HOSTCC)" \
                DESTDIR="$(PKG_INSTALL_DIR)" \
                ELF_OTHER_LIBS="$(TARGET_LDFLAGS) -luuid" \
index 553728194ec44ed957e47ad24eb3edf74618b26b..1828a88972820929e81227279b7eb51ae7542826 100644 (file)
@@ -19,6 +19,7 @@ PKG_MAINTAINER:=Felix Fietkau <nbd@nbd.name>
 PKG_CPE_ID:=cpe:/a:mdadm_project:mdadm
 
 PKG_BUILD_PARALLEL:=1
+PKG_BUILD_FLAGS:=gc-sections
 
 include $(INCLUDE_DIR)/package.mk
 
@@ -43,7 +44,6 @@ define Package/mdadm/conffiles
 endef
 
 TARGET_CFLAGS += \
-       -ffunction-sections -fdata-sections \
        -DHAVE_STDINT_H -DNO_COROSYNC -DNO_DLM -DUSE_PTHREADS \
        -DCONFFILE='\"/var/etc/mdadm.conf\"' \
        -DMAP_DIR='\"/var/run/mdadm\"' \
@@ -53,8 +53,6 @@ TARGET_CFLAGS += \
 
 TARGET_CXFLAGS = -DNO_LIBUDEV
 
-TARGET_LDFLAGS += -Wl,--gc-sections
-
 MAKE_FLAGS += \
                CHECK_RUN_DIR=0 \
                CXFLAGS="$(TARGET_CXFLAGS)"
index afbfbceff5480544ac360a02b484086e8d677bcb..bd53e071d5f6af44cf1955b6f534a28fcdeb8f79 100644 (file)
@@ -19,6 +19,7 @@ PKG_INSTALL:=1
 PKG_FIXUP:=autoreconf
 
 PKG_FLAGS:=nonshared
+PKG_BUILD_FLAGS:=gc-sections
 
 PKG_BUILD_DEPENDS:=util-linux
 
@@ -64,9 +65,6 @@ CONFIGURE_ARGS += \
        --without-zstd \
        --without-lzo
 
-TARGET_CFLAGS += -ffunction-sections -fdata-sections
-TARGET_LDFLAGS += -Wl,--gc-sections
-
 define Package/ubi-utils/install
        $(INSTALL_DIR) $(1)/usr/sbin
        $(INSTALL_BIN) \