Skip to contentSkip to conversation

Enable Dead Code Elimination

PATCH New Kele Zhang 3 participants 4 messages Oct 1, 2024 – Nov 6, 2024 Kbuild

lore
Share & tools
lore

Kele Zhang

author
#0 lore raw

v4 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
v2: https://lists.infradead.org/pipermail/opensbi/2024-August/007301.html
v3: https://lists.infradead.org/pipermail/opensbi/2024-September/007344.html
---
Changes in v2:
- Move --gc-sections to ELFFLAGS
- Remove redundant KEEP()

Changes in v3:
- Move --print-gc-sections to ifneq ($(DEBUG),)
- Move "ifneq ($(DEBUG),)" to the latter part of the Makefile, ensuring
that "ELFFLAGS += $(USE_LD_FLAG)" remains at the beginning.

Changes in v4:
- fix: replace spaces with tabs for consistent indentation
---

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

 Makefile             | 15 +++++++++------
 firmware/fw_base.ldS |  1 +
 2 files changed, 10 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v4 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             | 15 +++++++++------
 firmware/fw_base.ldS |  1 +
 2 files changed, 10 insertions(+), 6 deletions(-)
Makefile +9 -6
index f0012f6..dae282c 100644
@@ -355,12 +355,7 @@ GENFLAGS	+=	$(libsbiutils-genflags-y)
 GENFLAGS	+=	$(platform-genflags-y)
 GENFLAGS	+=	$(firmware-genflags-y)
 
-CFLAGS		=	-g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing
-ifneq ($(DEBUG),)
-CFLAGS		+=	-O0
-else
-CFLAGS		+=	-O2
-endif
+CFLAGS		=	-g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-sections
 CFLAGS		+=	-fno-omit-frame-pointer -fno-optimize-sibling-calls
 # Optionally supported flags
 ifeq ($(CC_SUPPORT_SAVE_RESTORE),y)
@@ -406,6 +401,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
@@ -424,6 +420,13 @@ MERGEFLAGS	+=	-m elf$(PLATFORM_RISCV_XLEN)lriscv
 
 DTSCPPFLAGS	=	$(CPPFLAGS) -nostdinc -nostdlib -fno-builtin -D__DTS__ -x assembler-with-cpp
 
+ifneq ($(DEBUG),)
+CFLAGS		+=	-O0
+ELFFLAGS	+=	-Wl,--print-gc-sections
+else
+CFLAGS		+=	-O2
+endif
+
 # Setup functions for compilation
 define dynamic_flags
 -I$(shell dirname $(2)) -D__OBJNAME__=$(subst -,_,$(shell basename $(1) .o))
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.43.0
#2 lore raw

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

? 2024-10-01?? 23:05 +0000?Kele Zhang???

Quoted 11 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 58 lines

---
?Makefile???????????? | 15 +++++++++------ ?firmware/fw_base.ldS |? 1 +
?2 files changed, 10 insertions(+), 6 deletions(-)

Makefile +0 -0
index f0012f6..dae282c 100644
@@ -355,12 +355,7 @@ GENFLAGS	+=	$(libsbiutils-genflags-y)
 ?GENFLAGS	+=	$(platform-genflags-y)
 ?GENFLAGS	+=	$(firmware-genflags-y)

?

Makefile +1 -6
-CFLAGS		=	-g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing
-ifneq ($(DEBUG),)
-CFLAGS		+=	-O0
-else
-CFLAGS		+=	-O2
-endif
+CFLAGS		=	-g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-

sections
?CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
?# Optionally supported flags
?ifeq ($(CC_SUPPORT_SAVE_RESTORE),y)

Makefile +0 -0
@@ -406,6 +401,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

Makefile +0 -0
@@ -424,6 +420,13 @@ MERGEFLAGS	+=	-m elf$(PLATFORM_RISCV_XLEN)lriscv

?
?DTSCPPFLAGS = $(CPPFLAGS) -nostdinc -nostdlib -fno-builtin -D__DTS__ -x assembler-with-cpp
?

Makefile +7 -0
+ifneq ($(DEBUG),)
+CFLAGS		+=	-O0
+ELFFLAGS	+=	-Wl,--print-gc-sections
+else
+CFLAGS		+=	-O2
+endif
+

?# Setup functions for compilation
?define dynamic_flags
?-I$(shell dirname $(2)) -D__OBJNAME__=$(subst -,_,$(shell basename $(1) .o))

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 = .);
 ?	}
Applied
#3 lore raw

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

In reply to Kele Zhang
Quoted 5 lines · On Wed, Oct 2, 2024 at 6:16?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.

Quoted 7 lines

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

LGTM.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 65 lines
---
 Makefile             | 15 +++++++++------
 firmware/fw_base.ldS |  1 +
 2 files changed, 10 insertions(+), 6 deletions(-)
Makefile +9 -6
index f0012f6..dae282c 100644
@@ -355,12 +355,7 @@ GENFLAGS   +=      $(libsbiutils-genflags-y)
 GENFLAGS       +=      $(platform-genflags-y)
 GENFLAGS       +=      $(firmware-genflags-y)
 
-CFLAGS         =       -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing
-ifneq ($(DEBUG),)
-CFLAGS         +=      -O0
-else
-CFLAGS         +=      -O2
-endif
+CFLAGS         =       -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-sections
 CFLAGS         +=      -fno-omit-frame-pointer -fno-optimize-sibling-calls
 # Optionally supported flags
 ifeq ($(CC_SUPPORT_SAVE_RESTORE),y)
@@ -406,6 +401,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
@@ -424,6 +420,13 @@ MERGEFLAGS +=      -m elf$(PLATFORM_RISCV_XLEN)lriscv
 
 DTSCPPFLAGS    =       $(CPPFLAGS) -nostdinc -nostdlib -fno-builtin -D__DTS__ -x assembler-with-cpp
 
+ifneq ($(DEBUG),)
+CFLAGS         +=      -O0
+ELFFLAGS       +=      -Wl,--print-gc-sections
+else
+CFLAGS         +=      -O2
+endif
+
 # Setup functions for compilation
 define dynamic_flags
 -I$(shell dirname $(2)) -D__OBJNAME__=$(subst -,_,$(shell basename $(1) .o))
firmware/fw_base.ldS +1 -1
index fb47984..a33746a 100644
@@ -20,6 +20,7 @@
                PROVIDE(_text_start = .);
                *(.entry)
                *(.text)
+               *(.text.*)
                . = ALIGN(8);
                PROVIDE(_text_end = .);
        }
--

2.43.0

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi