OpenSBI Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/1] Enable Dead Code Elimination
@ 2024-09-05  9:31 Kele Zhang
  2024-09-05  9:31 ` [PATCH v3 1/1] Makefile: enable --gc-sections Kele Zhang
  0 siblings, 1 reply; 4+ messages in thread
From: Kele Zhang @ 2024-09-05  9:31 UTC (permalink / raw)
  To: opensbi

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
---
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.


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

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


base-commit: c4940a9517486413cd676fc8032bb55f9d4e2778
-- 
2.43.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 1/1] Makefile: enable --gc-sections
  2024-09-05  9:31 [PATCH v3 0/1] Enable Dead Code Elimination Kele Zhang
@ 2024-09-05  9:31 ` Kele Zhang
  2024-09-11 16:59   ` Bin Meng
  2024-09-11 17:03   ` Jessica Clarke
  0 siblings, 2 replies; 4+ messages in thread
From: Kele Zhang @ 2024-09-05  9:31 UTC (permalink / raw)
  To: opensbi

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.

Signed-off-by: Kele Zhang <zhangcola2003@gmail.com>
Signed-off-by: Yuan Tan <tanyuan@tinylab.org>
Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
 Makefile             | 15 +++++++++------
 firmware/fw_base.ldS |  1 +
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/Makefile b/Makefile
index e5a0f19..30ba3b6 100644
--- a/Makefile
+++ b/Makefile
@@ -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))
diff --git a/firmware/fw_base.ldS b/firmware/fw_base.ldS
index fb47984..a33746a 100644
--- a/firmware/fw_base.ldS
+++ b/firmware/fw_base.ldS
@@ -20,6 +20,7 @@
 		PROVIDE(_text_start = .);
 		*(.entry)
 		*(.text)
+		*(.text.*)
 		. = ALIGN(8);
 		PROVIDE(_text_end = .);
 	}
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v3 1/1] Makefile: enable --gc-sections
  2024-09-05  9:31 ` [PATCH v3 1/1] Makefile: enable --gc-sections Kele Zhang
@ 2024-09-11 16:59   ` Bin Meng
  2024-09-11 17:03   ` Jessica Clarke
  1 sibling, 0 replies; 4+ messages in thread
From: Bin Meng @ 2024-09-11 16:59 UTC (permalink / raw)
  To: opensbi

On Thu, Sep 5, 2024 at 5:31?PM Kele Zhang <zhangcola2003@gmail.com> 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.
>
> Signed-off-by: Kele Zhang <zhangcola2003@gmail.com>
> Signed-off-by: Yuan Tan <tanyuan@tinylab.org>
> Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> ---
>  Makefile             | 15 +++++++++------
>  firmware/fw_base.ldS |  1 +
>  2 files changed, 10 insertions(+), 6 deletions(-)
>

Reviewed-by: Bin Meng <bmeng.cn@gmail.com>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 1/1] Makefile: enable --gc-sections
  2024-09-05  9:31 ` [PATCH v3 1/1] Makefile: enable --gc-sections Kele Zhang
  2024-09-11 16:59   ` Bin Meng
@ 2024-09-11 17:03   ` Jessica Clarke
  1 sibling, 0 replies; 4+ messages in thread
From: Jessica Clarke @ 2024-09-11 17:03 UTC (permalink / raw)
  To: opensbi

On 5 Sep 2024, at 10:31, Kele Zhang <zhangcola2003@gmail.com> 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.
> 
> Signed-off-by: Kele Zhang <zhangcola2003@gmail.com>
> Signed-off-by: Yuan Tan <tanyuan@tinylab.org>
> Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> ?
> ...
> @@ -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

This line is using spaces not tabs.

Jess



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-09-11 17:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-05  9:31 [PATCH v3 0/1] Enable Dead Code Elimination Kele Zhang
2024-09-05  9:31 ` [PATCH v3 1/1] Makefile: enable --gc-sections Kele Zhang
2024-09-11 16:59   ` Bin Meng
2024-09-11 17:03   ` Jessica Clarke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox