diff mbox series

[v4,1/1] Makefile: enable --gc-sections

Message ID 09f89be252e5ea08820909ad6a1bb93866b438c8.1727823614.git.zhangcola2003@gmail.com
State New
Headers show
Series Enable Dead Code Elimination | expand

Commit Message

Kele Zhang Oct. 1, 2024, 11:05 p.m. UTC
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>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
---
 Makefile             | 15 +++++++++------
 firmware/fw_base.ldS |  1 +
 2 files changed, 10 insertions(+), 6 deletions(-)

Comments

Xiang W Oct. 4, 2024, 12:55 a.m. UTC | #1
在 2024-10-01二的 23:05 +0000,Kele Zhang写道:
> 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>
> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
LGTM

Reviewed-by: Xiang W <wxjstz@126.com>
> ---
>  Makefile             | 15 +++++++++------
>  firmware/fw_base.ldS |  1 +
>  2 files changed, 10 insertions(+), 6 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index f0012f6..dae282c 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 = .);
>  	}
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index f0012f6..dae282c 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 = .);
 	}