Skip to contentSkip to conversation

Enable Dead Code Elimination

PATCH Superseded Kele Zhang 3 participants 7 messages Aug 29, 2024 – Sep 1, 2024 Kbuild

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Kele Zhang

author
#0 lore raw

v2 cover Enable Dead Code Elimination

This patch enables --gc-sections. Dead Code Elimination (DCE) can reduce binary size and the attack surface without introducing side effects, and it has been used in the Linux kernel for many years.

After testing, enabling DCE does not affect the normal functionality of OpenSBI.

---
v1: https://lists.infradead.org/pipermail/opensbi/2024-August/007286.html

---
Changes in v2:
- Move --gc-sections to ELFFLAGS
- Remove redundant KEEP()

Kele Zhang (1):
Makefile: enable --gc-sections

 Makefile             | 5 +++--
 firmware/fw_base.ldS | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)

base-commit: c4940a9517486413cd676fc8032bb55f9d4e2778

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/1 Makefile: enable --gc-sections

The --gc-sections option enables the linker to perform garbage collection of unreferenced code and data, thereby reducing the binary size.

The -ffunction-sections option will place each function into a separate section, so it is necessary to add .text.* to the linker script.

---
 Makefile             | 5 +++--
 firmware/fw_base.ldS | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)
Makefile +3 -2
index e5a0f19..44db5d4 100644
@@ -355,9 +355,9 @@ GENFLAGS	+=	$(libsbiutils-genflags-y)
 GENFLAGS	+=	$(platform-genflags-y)
 GENFLAGS	+=	$(firmware-genflags-y)
 
-CFLAGS		=	-g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing
+CFLAGS		=	-g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-sections
 ifneq ($(DEBUG),)
-CFLAGS		+=	-O0
+CFLAGS		+=	-O0 -Wl,--print-gc-sections
 else
 CFLAGS		+=	-O2
 endif
@@ -406,6 +406,7 @@ ASFLAGS		+=	$(firmware-asflags-y)
 ARFLAGS		=	rcs
 
 ELFFLAGS	+=	$(USE_LD_FLAG)
+ELFFLAGS	+=	-Wl,--gc-sections
 ifeq ($(OPENSBI_LD_EXCLUDE_LIBS),y)
 ELFFLAGS	+=	-Wl,--exclude-libs,ALL
 endif
firmware/fw_base.ldS +1 -0
index fb47984..a33746a 100644
@@ -20,6 +20,7 @@
 		PROVIDE(_text_start = .);
 		*(.entry)
 		*(.text)
+		*(.text.*)
 		. = ALIGN(8);
 		PROVIDE(_text_end = .);
 	}
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 1/1 Makefile: enable --gc-sections

? 2024-08-29???? 20:12 +0000?Kele Zhang???

Quoted 10 lines

The --gc-sections option enables the linker to perform garbage collection of unreferenced code and data, thereby reducing the binary size.

The -ffunction-sections option will place each function into a separate section, so it is necessary to add .text.* to the linker script.

LGTM

Quoted 42 lines

---
?Makefile???????????? | 5 +++--
?firmware/fw_base.ldS | 1 +
?2 files changed, 4 insertions(+), 2 deletions(-)

Makefile +0 -0
index e5a0f19..44db5d4 100644
@@ -355,9 +355,9 @@ GENFLAGS	+=	$(libsbiutils-genflags-y)
 ?GENFLAGS	+=	$(platform-genflags-y)
 ?GENFLAGS	+=	$(firmware-genflags-y)

?

Makefile +1 -1
-CFLAGS		=	-g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing
+CFLAGS		=	-g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-

sections
?ifneq ($(DEBUG),)

Makefile +1 -1
-CFLAGS		+=	-O0
+CFLAGS		+=	-O0 -Wl,--print-gc-sections

?else
?CFLAGS += -O2
?endif

Makefile +0 -0
@@ -406,6 +406,7 @@ ASFLAGS		+=	$(firmware-asflags-y)

?ARFLAGS = rcs
?
?ELFFLAGS += $(USE_LD_FLAG)
+ELFFLAGS += -Wl,--gc-sections
?ifeq ($(OPENSBI_LD_EXCLUDE_LIBS),y)
?ELFFLAGS += -Wl,--exclude-libs,ALL
?endif

firmware/fw_base.ldS +1 -0
index fb47984..a33746a 100644
@@ -20,6 +20,7 @@
 ?		PROVIDE(_text_start = .);
 ?		*(.entry)
 ?		*(.text)
+		*(.text.*)
 ?		. = ALIGN(8);
 ?		PROVIDE(_text_end = .);
 ?	}
#3 lore raw

PATCH v2 1/1 Makefile: enable --gc-sections

In reply to Kele Zhang
Quoted 21 lines · On Fri, Aug 30, 2024 at 4:12?AM Kele Zhang <[email protected]> wrote:

The --gc-sections option enables the linker to perform garbage collection of unreferenced code and data, thereby reducing the binary size.

The -ffunction-sections option will place each function into a separate section, so it is necessary to add .text.* to the linker script.

---
 Makefile             | 5 +++--
 firmware/fw_base.ldS | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)
Makefile +0 -0
index e5a0f19..44db5d4 100644

Makefile GENFLAGS += $(libsbiutils-genflags-y) · line 360

Quoted 8 lines
Makefile +2 -2
@@ -355,9 +355,9 @@ GENFLAGS    +=      $(libsbiutils-genflags-y)
 GENFLAGS       +=      $(platform-genflags-y)
 GENFLAGS       +=      $(firmware-genflags-y)
 
-CFLAGS         =       -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing
+CFLAGS         =       -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-sections
 ifneq ($(DEBUG),)
-CFLAGS         +=      -O0
+CFLAGS         +=      -O0 -Wl,--print-gc-sections

This should be put into the ELF flags too.

Quoted 16 lines
Makefile +1 -0
 else
 CFLAGS         +=      -O2
 endif
@@ -406,6 +406,7 @@ ASFLAGS             +=      $(firmware-asflags-y)
 ARFLAGS                =       rcs
 
 ELFFLAGS       +=      $(USE_LD_FLAG)
+ELFFLAGS       +=      -Wl,--gc-sections
 ifeq ($(OPENSBI_LD_EXCLUDE_LIBS),y)
 ELFFLAGS       +=      -Wl,--exclude-libs,ALL
 endif
firmware/fw_base.ldS +0 -0
index fb47984..a33746a 100644

firmware/fw_base.ldS line 26

Quoted 7 lines
firmware/fw_base.ldS +1 -0
@@ -20,6 +20,7 @@
                PROVIDE(_text_start = .);
                *(.entry)
                *(.text)
+               *(.text.*)
                . = ALIGN(8);
                PROVIDE(_text_end = .);
        }

Regards,
Bin

author
#4 lore raw

PATCH v2 1/1 Makefile: enable --gc-sections

Bin Meng <[email protected]> ?2024?9?1??? 19:12???

Quoted 26 lines · On Fri, Aug 30, 2024 at 4:12?AM Kele Zhang <[email protected]> wrote:
Quoted 24 lines

The --gc-sections option enables the linker to perform garbage collection of unreferenced code and data, thereby reducing the binary size.

The -ffunction-sections option will place each function into a separate section, so it is necessary to add .text.* to the linker script.

---
 Makefile             | 5 +++--
 firmware/fw_base.ldS | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)
Makefile +0 -0
index e5a0f19..44db5d4 100644
@@ -355,9 +355,9 @@ GENFLAGS    +=      $(libsbiutils-genflags-y)
 GENFLAGS       +=      $(platform-genflags-y)
 GENFLAGS       +=      $(firmware-genflags-y)
 
Quoted 7 lines

Makefile GENFLAGS += $(libsbiutils-genflags-y) · line 360

Quoted 5 lines
Makefile +2 -2
-CFLAGS         =       -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing
+CFLAGS         =       -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-sections
 ifneq ($(DEBUG),)
-CFLAGS         +=      -O0
+CFLAGS         +=      -O0 -Wl,--print-gc-sections

This should be put into the ELF flags too.

Are ELFflags the flags that take effect during linking? -ffunction-sections -fdata-sections will take effect during compilation. Enabling them will generate different .s files and will not affect the behavior of the linker. So isn't it better to put them in cflags?

Quoted 27 lines
Quoted 17 lines
Makefile +1 -0
 else
 CFLAGS         +=      -O2
 endif
@@ -406,6 +406,7 @@ ASFLAGS             +=      $(firmware-asflags-y)
 ARFLAGS                =       rcs
 
 ELFFLAGS       +=      $(USE_LD_FLAG)
+ELFFLAGS       +=      -Wl,--gc-sections
 ifeq ($(OPENSBI_LD_EXCLUDE_LIBS),y)
 ELFFLAGS       +=      -Wl,--exclude-libs,ALL
 endif
firmware/fw_base.ldS +0 -0
index fb47984..a33746a 100644

firmware/fw_base.ldS line 26

Quoted 7 lines
firmware/fw_base.ldS +1 -0
@@ -20,6 +20,7 @@
                PROVIDE(_text_start = .);
                *(.entry)
                *(.text)
+               *(.text.*)
                . = ALIGN(8);
                PROVIDE(_text_end = .);
        }

Regards,
Bin

#5 lore raw

PATCH v2 1/1 Makefile: enable --gc-sections

? 2024-09-01???? 19:51 +0800?cola zhang???

Quoted 36 lines

Bin Meng <[email protected]> ?2024?9?1??? 19:12???

Quoted 26 lines · On Fri, Aug 30, 2024 at 4:12?AM Kele Zhang <[email protected]> wrote:
Quoted 24 lines

The --gc-sections option enables the linker to perform garbage collection of unreferenced code and data, thereby reducing the binary size.

The -ffunction-sections option will place each function into a separate section, so it is necessary to add .text.* to the linker script.

---
?Makefile???????????? | 5 +++--
?firmware/fw_base.ldS | 1 +
?2 files changed, 4 insertions(+), 2 deletions(-)

Makefile +0 -0
index e5a0f19..44db5d4 100644
@@ -355,9 +355,9 @@ GENFLAGS??? +=????? $(libsbiutils-genflags-y)
 ?GENFLAGS?????? +=????? $(platform-genflags-y)
 ?GENFLAGS?????? +=????? $(firmware-genflags-y)
 
Quoted 8 lines

Makefile

Quoted 6 lines
Makefile +1 -1
-CFLAGS???????? =?????? -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing
+CFLAGS???????? =?????? -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-

sections
?ifneq ($(DEBUG),)

Makefile +1 -1
-CFLAGS???????? +=????? -O0
+CFLAGS???????? +=????? -O0 -Wl,--print-gc-sections

This should be put into the ELF flags too.

Quoted 4 lines

Are ELFflags the flags that take effect during linking? -ffunction-sections -fdata-sections will take effect during compilation. Enabling them will generate different .s files and will not affect the behavior of the linker. So isn't it better to put them in cflags?

-Wl,--print-gc-sections move to ELFFLAGS

Regards,
Xiang W

Quoted 28 lines
Quoted 28 lines
Quoted 18 lines

?else
?CFLAGS???????? +=????? -O2
?endif

Makefile +0 -0
@@ -406,6 +406,7 @@ ASFLAGS???????????? +=????? $(firmware-asflags-y)

?ARFLAGS??????????????? =?????? rcs

?ELFFLAGS?????? +=????? $(USE_LD_FLAG)
+ELFFLAGS?????? +=????? -Wl,--gc-sections
?ifeq ($(OPENSBI_LD_EXCLUDE_LIBS),y)
?ELFFLAGS?????? +=????? -Wl,--exclude-libs,ALL
?endif

firmware/fw_base.ldS +0 -0
index fb47984..a33746a 100644

firmware/fw_base.ldS line 26

Quoted 7 lines
firmware/fw_base.ldS +1 -0
@@ -20,6 +20,7 @@
 ??????????????? PROVIDE(_text_start = .);
 ??????????????? *(.entry)
 ??????????????? *(.text)
+?????????????? *(.text.*)
 ??????????????? . = ALIGN(8);
 ??????????????? PROVIDE(_text_end = .);
 ??????? }

Regards,
Bin

author
#6 lore raw

PATCH v2 1/1 Makefile: enable --gc-sections

Xiang W <[email protected]> ?2024?9?1??? 20:15???

Quoted 38 lines

? 2024-09-01???? 19:51 +0800?cola zhang???

Quoted 36 lines

Bin Meng <[email protected]> ?2024?9?1??? 19:12???

Quoted 34 lines · On Fri, Aug 30, 2024 at 4:12?AM Kele Zhang <[email protected]> wrote:
Quoted 28 lines

The --gc-sections option enables the linker to perform garbage collection of unreferenced code and data, thereby reducing the binary size.

The -ffunction-sections option will place each function into a separate section, so it is necessary to add .text.* to the linker script.

---
 Makefile             | 5 +++--
 firmware/fw_base.ldS | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)
Makefile +1 -1
index e5a0f19..44db5d4 100644
@@ -355,9 +355,9 @@ GENFLAGS    +=      $(libsbiutils-genflags-y)
 GENFLAGS       +=      $(platform-genflags-y)
 GENFLAGS       +=      $(firmware-genflags-y)
 
-CFLAGS         =       -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing
+CFLAGS         =       -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-

sections
ifneq ($(DEBUG),)

Makefile GENFLAGS += $(libsbiutils-genflags-y) · line 360

Quoted 2 lines
Makefile +1 -1
-CFLAGS         +=      -O0
+CFLAGS         +=      -O0 -Wl,--print-gc-sections

This should be put into the ELF flags too.

Quoted 6 lines
Quoted 4 lines

Are ELFflags the flags that take effect during linking? -ffunction-sections -fdata-sections will take effect during compilation. Enabling them will generate different .s files and will not affect the behavior of the linker. So isn't it better to put them in cflags?

-Wl,--print-gc-sections move to ELFFLAGS

Sorry, I didn't see clearly. This indeed needs to be modified.

Quoted 32 lines

Regards,
Xiang W

Quoted 28 lines
Quoted 28 lines
Quoted 18 lines

else
CFLAGS += -O2
endif

Makefile +1 -0
@@ -406,6 +406,7 @@ ASFLAGS             +=      $(firmware-asflags-y)
 ARFLAGS                =       rcs
 
 ELFFLAGS       +=      $(USE_LD_FLAG)
+ELFFLAGS       +=      -Wl,--gc-sections
 ifeq ($(OPENSBI_LD_EXCLUDE_LIBS),y)
 ELFFLAGS       +=      -Wl,--exclude-libs,ALL
 endif
firmware/fw_base.ldS +0 -0
index fb47984..a33746a 100644

firmware/fw_base.ldS line 26

Quoted 7 lines
firmware/fw_base.ldS +1 -0
@@ -20,6 +20,7 @@
                PROVIDE(_text_start = .);
                *(.entry)
                *(.text)
+               *(.text.*)
                . = ALIGN(8);
                PROVIDE(_text_end = .);
        }

Regards,
Bin