Skip to contentSkip to conversation

Adding size relevant build options

PATCH Applied Sven Eckelmann 3 participants 19 messages May 7, 2013 – May 14, 2013 Kbuild

lore
Share & tools
lore
#0 lore raw

cover Adding size relevant build options

Hi,

I was talking with Marek and Simon about build options which may be size relevant. The first change is to allow the linker to check for unused code sections. The second change is to let the compiler use get the whole source code instead of only one source file during the final compilation step (ok, it is originally the link step).

These changes reduce the size on an amd64 (default Debian settings with -O2) by around 25%. The size reduction on MIPSel (default OpenWRT settings with -Os) were only by 6%-10%. Marek were still interested and asked me to submit the changes.

Kind regards,
Sven

#1 lore raw

PATCH 1/9 alfred: Add CFLAGS to the linker step

The GCC manual states for different parameters that the options for compilation must also be used when linking. The options for compilation are stored in CFLAGS and added to LINK.o to fix the behavior.

Option which need this are for example -fPIC/-fPIE or -flto.

---
 Makefile     | 2 +-
 vis/Makefile | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Makefile +1 -1
index 268a156..aeee2e8 100644
@@ -42,7 +42,7 @@ RM ?= rm -f
 INSTALL ?= install
 MKDIR ?= mkdir -p
 COMPILE.c = $(Q_CC)$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
-LINK.o = $(Q_LD)$(CC) $(LDFLAGS) $(TARGET_ARCH)
+LINK.o = $(Q_LD)$(CC) $(CFLAGS) $(LDFLAGS) $(TARGET_ARCH)
 
 # standard install paths
 PREFIX = /usr/local
vis/Makefile +1 -1
index 5ec0423..8585f9a 100644
@@ -42,7 +42,7 @@ RM ?= rm -f
 INSTALL ?= install
 MKDIR ?= mkdir -p
 COMPILE.c = $(Q_CC)$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
-LINK.o = $(Q_LD)$(CC) $(LDFLAGS) $(TARGET_ARCH)
+LINK.o = $(Q_LD)$(CC) $(CFLAGS) $(LDFLAGS) $(TARGET_ARCH)
 
 # standard install paths
 PREFIX = /usr/local
Signature · 1 line
1.8.2.1
#2 lore raw

Re: [B.A.T.M.A.N.] [PATCH 1/9] alfred: Add CFLAGS to the linker step

Quoted 11 lines · Sven Eckelmann

The GCC manual states for different parameters that the options for compilation must also be used when linking. The options for compilation are stored in CFLAGS and added to LINK.o to fix the behavior.

Option which need this are for example -fPIC/-fPIE or -flto.

---
 Makefile     | 2 +-
 vis/Makefile | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

Applied in revision c693d67.

Thanks,
Simon

#3 lore raw

PATCH 2/9 batctl: Add CFLAGS to the linker step

In reply to Sven Eckelmann

The GCC manual states for different parameters that the options for compilation must also be used when linking. The options for compilation are stored in CFLAGS and added to LINK.o to fix the behavior.

Option which need this are for example -fPIC/-fPIE or -flto.

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
index 61b24c6..1961298 100755
@@ -53,7 +53,7 @@ RM ?= rm -f
 INSTALL ?= install
 MKDIR ?= mkdir -p
 COMPILE.c = $(Q_CC)$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
-LINK.o = $(Q_LD)$(CC) $(LDFLAGS) $(TARGET_ARCH)
+LINK.o = $(Q_LD)$(CC) $(CFLAGS) $(LDFLAGS) $(TARGET_ARCH)
 
 # standard install paths
 PREFIX = /usr/local
Signature · 1 line
1.8.2.1
#4 lore raw

Re: [B.A.T.M.A.N.] [PATCH 2/9] batctl: Add CFLAGS to the linker step

Quoted 9 lines · On Tuesday, May 07, 2013 20:51:02 Sven Eckelmann wrote:

The GCC manual states for different parameters that the options for compilation must also be used when linking. The options for compilation are stored in CFLAGS and added to LINK.o to fix the behavior.

Option which need this are for example -fPIC/-fPIE or -flto.

---
 Makefile | 2 +-

Applied in revision b80d07f.

Thanks,
Marek

#5 lore raw

PATCH 3/9 batmand: Add CFLAGS to the linker step

In reply to Sven Eckelmann

The GCC manual states for different parameters that the options for compilation must also be used when linking. The options for compilation are stored in CFLAGS and added to LINK.o to fix the behavior.

Option which need this are for example -fPIC/-fPIE or -flto.

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
index ce1e198..721f20b 100755
@@ -57,7 +57,7 @@ RM ?= rm -f
 INSTALL ?= install
 MKDIR ?= mkdir -p
 COMPILE.c = $(Q_CC)$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
-LINK.o = $(Q_LD)$(CC) $(LDFLAGS) $(TARGET_ARCH)
+LINK.o = $(Q_LD)$(CC) $(CFLAGS) $(LDFLAGS) $(TARGET_ARCH)
 
 # standard install paths
 PREFIX = /usr/local
Signature · 1 line
1.8.2.1
#6 lore raw

PATCH 4/9 vis: Add CFLAGS to the linker step

In reply to Sven Eckelmann

The GCC manual states for different parameters that the options for compilation must also be used when linking. The options for compilation are stored in CFLAGS and added to LINK.o to fix the behavior.

Option which need this are for example -fPIC/-fPIE or -flto.

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
index c4220f5..0d49c7e 100755
@@ -43,7 +43,7 @@ RM ?= rm -f
 INSTALL ?= install
 MKDIR ?= mkdir -p
 COMPILE.c = $(Q_CC)$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
-LINK.o = $(Q_LD)$(CC) $(LDFLAGS) $(TARGET_ARCH)
+LINK.o = $(Q_LD)$(CC) $(CFLAGS) $(LDFLAGS) $(TARGET_ARCH)
 
 # standard install paths
 PREFIX = /usr/local
Signature · 1 line
1.8.2.1
#7 lore raw

PATCH 5/9 alfred: Use section garbage collection to reduce binary size

In reply to Sven Eckelmann

The linker can identify unused sections of a binary when each symbol is stored in a separate section. This mostly removes the unused debugfs function and reduces the size by ~5% on mipsel.

---
 alfred/Makefile | 3 +++
 1 file changed, 3 insertions(+)
alfred/Makefile +3 -0
index 4578c3b..ab19325 100644
@@ -44,6 +44,9 @@ endef
 MAKE_ALFRED_FLAGS=\
 	CONFIG_ALFRED_VIS=$(if $(CONFIG_PACKAGE_ALFRED_VIS),y,n)
 
+TARGET_CFLAGS  += -ffunction-sections -fdata-sections
+TARGET_LDFLAGS += -Wl,--gc-sections
+
 define Build/Compile
 	CFLAGS="$(TARGET_CPPFLAGS) $(TARGET_CFLAGS)" \
 	LDFLAGS="$(TARGET_LDFLAGS)" \
Signature · 1 line
1.8.2.1
#8 lore raw

Re: [B.A.T.M.A.N.] [PATCH 5/9] alfred: Use section garbage collection to reduce binary size

Quoted 8 lines · Sven Eckelmann

The linker can identify unused sections of a binary when each symbol is stored in a separate section. This mostly removes the unused debugfs function and reduces the size by ~5% on mipsel.

---
 alfred/Makefile | 3 +++
 1 file changed, 3 insertions(+)

Applied in revision fa23641 (alfred package feed).

Thanks,
Simon

#9 lore raw

PATCH 5/9 batman-adv-devel: Enable Link-time optimization

In reply to Sven Eckelmann

Link-time optimization allows to move parts of the optimization from the single source file to the global source view. This is done by emitting the GIMPLE representation in each object file and analyzing it again during the link step.

This reduces the binary size by around 6% on mipsel (this includes parts which were also removed by garbage collection).

---
 batman-adv-devel/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
batman-adv-devel/Makefile +2 -2
index 6dc66b8..cad95c6 100644
@@ -76,8 +76,8 @@ MAKE_BATCTL_ARGS += \
 	STRIP="/bin/true" \
 	batctl install
 
-TARGET_CFLAGS  += -ffunction-sections -fdata-sections
-TARGET_LDFLAGS += -Wl,--gc-sections
+TARGET_CFLAGS  += -ffunction-sections -fdata-sections -flto
+TARGET_LDFLAGS += -Wl,--gc-sections -fuse-linker-plugin
 
 ifneq ($(DEVELOPER)$(CONFIG_KMOD_BATMAN_ADV_DEVEL_BATCTL),)
 define Download/batctl
Signature · 1 line
1.8.2.1
#10 lore raw

Re: [B.A.T.M.A.N.] [PATCH 5/9] batman-adv-devel: Enable Link-time optimization

Quoted 5 lines · On Tuesday, May 07, 2013 20:51:09 Sven Eckelmann wrote:

Link-time optimization allows to move parts of the optimization from the single source file to the global source view. This is done by emitting the GIMPLE representation in each object file and analyzing it again during the link step.

Quoted 7 lines

This reduces the binary size by around 6% on mipsel (this includes parts which were also removed by garbage collection).

---
 batman-adv-devel/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Applied in revision e25de79.

Thanks,
Marek

#11 lore raw

PATCH 6/9 alfred: Enable Link-time optimization

In reply to Sven Eckelmann

Link-time optimization allows to move parts of the optimization from the single source file to the global source view. This is done by emitting the GIMPLE representation in each object file and analyzing it again during the link step.

This reduces the binary size by around 10% on mipsel (this includes parts which were also removed by garbage collection).

---
 alfred/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
alfred/Makefile +2 -2
index ab19325..2656d1c 100644
@@ -44,8 +44,8 @@ endef
 MAKE_ALFRED_FLAGS=\
 	CONFIG_ALFRED_VIS=$(if $(CONFIG_PACKAGE_ALFRED_VIS),y,n)
 
-TARGET_CFLAGS  += -ffunction-sections -fdata-sections
-TARGET_LDFLAGS += -Wl,--gc-sections
+TARGET_CFLAGS  += -ffunction-sections -fdata-sections -flto
+TARGET_LDFLAGS += -Wl,--gc-sections -fuse-linker-plugin
 
 define Build/Compile
 	CFLAGS="$(TARGET_CPPFLAGS) $(TARGET_CFLAGS)" \
Signature · 1 line
1.8.2.1
#12 lore raw

Re: [B.A.T.M.A.N.] [PATCH 6/9] alfred: Enable Link-time optimization

Quoted 4 lines · Sven Eckelmann

Link-time optimization allows to move parts of the optimization from the single source file to the global source view. This is done by emitting the GIMPLE representation in each object file and analyzing it again during the link step.

Quoted 8 lines

This reduces the binary size by around 10% on mipsel (this includes parts which were also removed by garbage collection).

---
 alfred/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Applied in revision bbc581c (alfred package feed).

Thanks,
Simon

#13 lore raw

PATCH 7/9 batman-adv-devel: Add missing TARGET_* flags to batctl compile

In reply to Sven Eckelmann

The compilation step of batctl completely dropped the TARGET_CFLAGS, TARGET_CPPFLAGS and TARGET_LDFLAGS and therefore weren't compiled and linked with the flags given by the OpenWRT build environment.

---
 batman-adv-devel/Makefile | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
batman-adv-devel/Makefile +3 -1
index 0958f38..b9e2650 100644
@@ -85,7 +85,9 @@ $(eval $(call Download,batctl))
 
 BATCTL_EXTRACT = tar xzf "$(DL_DIR)/$(PKG_BATCTL_FNAME)" -C "$(BUILD_DIR)/$(PKG_NAME)"
 BATCTL_PATCH = $(call Build/DoPatch,"$(PKG_BATCTL_BUILD_DIR)","$(PATCH_DIR)","*batctl*")
-BATCTL_BUILD = $(MAKE) -C $(PKG_BATCTL_BUILD_DIR) $(MAKE_BATCTL_ARGS)
+BATCTL_BUILD =	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" \
+		LDFLAGS="$(TARGET_LDFLAGS)" \
+		$(MAKE) -C $(PKG_BATCTL_BUILD_DIR) $(MAKE_BATCTL_ARGS)
 BATCTL_INSTALL = $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/local/sbin/batctl $(1)/usr/sbin/
 endif
 
Signature · 1 line
1.8.2.1
#14 lore raw

Re: [B.A.T.M.A.N.] [PATCH 7/9] batman-adv-devel: Add missing TARGET_* flags to batctl compile

Quoted 3 lines · On Tuesday, May 07, 2013 20:51:07 Sven Eckelmann wrote:

The compilation step of batctl completely dropped the TARGET_CFLAGS, TARGET_CPPFLAGS and TARGET_LDFLAGS and therefore weren't compiled and linked with the flags given by the OpenWRT build environment.

It seems you missed the meaning of MAKE_BATCTL_ARGS ? This variable contains TARGET_CFLAGS already. We apparently are missing TARGET_CPPFLAGS and TARGET_LDFLAGS. Do you want to resend and make the necessary changes to MAKE_BATCTL_ARGS ?

Cheers,
Marek

#15 lore raw

Re: [B.A.T.M.A.N.] [PATCH 7/9] batman-adv-devel: Add missing TARGET_* flags to batctl compile

Quoted 9 lines · On Wednesday 08 May 2013 13:11:57 Marek Lindner wrote:
Quoted 3 lines · On Tuesday, May 07, 2013 20:51:07 Sven Eckelmann wrote:

The compilation step of batctl completely dropped the TARGET_CFLAGS, TARGET_CPPFLAGS and TARGET_LDFLAGS and therefore weren't compiled and linked with the flags given by the OpenWRT build environment.

It seems you missed the meaning of MAKE_BATCTL_ARGS ? This variable contains TARGET_CFLAGS already. We apparently are missing TARGET_CPPFLAGS and TARGET_LDFLAGS. Do you want to resend and make the necessary changes to MAKE_BATCTL_ARGS ?

I didn't felt like fixing your other problems too, but sent two other patches resolving the MAKE_BATCTL_ARGS problem. See
[Bonus-PATCH 10/9] batman-adv-devel: Don't overwrite batctl's CFLAGS [Bonus-PATCH 11/9] batman-adv-devel: Remove unused parameters from MAKE_BATCTL_ARGS

Kind regards,
Sven

#16 lore raw

Re: [B.A.T.M.A.N.] [PATCH 7/9] batman-adv-devel: Add missing TARGET_* flags to batctl compile

Quoted 11 lines · On Wednesday, May 08, 2013 15:24:50 Sven Eckelmann wrote:
Quoted 9 lines · On Wednesday 08 May 2013 13:11:57 Marek Lindner wrote:
Quoted 3 lines · On Tuesday, May 07, 2013 20:51:07 Sven Eckelmann wrote:

The compilation step of batctl completely dropped the TARGET_CFLAGS, TARGET_CPPFLAGS and TARGET_LDFLAGS and therefore weren't compiled and linked with the flags given by the OpenWRT build environment.

It seems you missed the meaning of MAKE_BATCTL_ARGS ? This variable contains TARGET_CFLAGS already. We apparently are missing TARGET_CPPFLAGS and TARGET_LDFLAGS. Do you want to resend and make the necessary changes to MAKE_BATCTL_ARGS ?

Quoted 5 lines

I didn't felt like fixing your other problems too, but sent two other patches resolving the MAKE_BATCTL_ARGS problem. See [Bonus-PATCH 10/9] batman-adv-devel: Don't overwrite batctl's CFLAGS [Bonus-PATCH 11/9] batman-adv-devel: Remove unused parameters from MAKE_BATCTL_ARGS

Applied with minor modifications in revision f41a85c.

Thanks for the explanations as to why that is helpful.

Thanks,
Marek

#17 lore raw

PATCH 8/9 batman-adv-devel: Use section garbage collection to reduce binary size

In reply to Sven Eckelmann

The linker can identify unused sections of a binary when each symbol is stored in a separate section. This mostly removes unused linker sections and reduces the size by ~3% on mipsel.

---
 batman-adv-devel/Makefile | 3 +++
 1 file changed, 3 insertions(+)
batman-adv-devel/Makefile +3 -0
index b9e2650..6dc66b8 100644
@@ -76,6 +76,9 @@ MAKE_BATCTL_ARGS += \
 	STRIP="/bin/true" \
 	batctl install
 
+TARGET_CFLAGS  += -ffunction-sections -fdata-sections
+TARGET_LDFLAGS += -Wl,--gc-sections
+
 ifneq ($(DEVELOPER)$(CONFIG_KMOD_BATMAN_ADV_DEVEL_BATCTL),)
 define Download/batctl
   FILE:=$(PKG_BATCTL_FNAME)
Signature · 1 line
1.8.2.1
#18 lore raw

Re: [B.A.T.M.A.N.] [PATCH 8/9] batman-adv-devel: Use section garbage collection to reduce binary size

Quoted 8 lines · On Tuesday, May 07, 2013 20:51:08 Sven Eckelmann wrote:

The linker can identify unused sections of a binary when each symbol is stored in a separate section. This mostly removes unused linker sections and reduces the size by ~3% on mipsel.

---
 batman-adv-devel/Makefile | 3 +++
 1 file changed, 3 insertions(+)

Applied in revision 3e056f3.

Thanks,
Marek