diff mbox series

[1/2] Makefile: enable --gc-sections

Message ID tencent_48AE88FE505E24B2609EACA57A9A2F35BD06@qq.com
State Superseded
Headers show
Series Enable Dead Code Elimination | expand

Commit Message

Kele Zhang Aug. 24, 2024, 8:16 a.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 <1952088712@qq.com>
Signed-off-by: Yuan Tan <tanyuan@tinylab.org>
Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
 Makefile                    | 4 ++--
 firmware/fw_base.ldS        | 1 +
 firmware/fw_payload.elf.ldS | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)

Comments

Bin Meng Aug. 28, 2024, 3:05 p.m. UTC | #1
On Sat, Aug 24, 2024 at 4:19 PM Kele Zhang <1952088712@qq.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 <1952088712@qq.com>
> Signed-off-by: Yuan Tan <tanyuan@tinylab.org>
> Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> ---
>  Makefile                    | 4 ++--
>  firmware/fw_base.ldS        | 1 +
>  firmware/fw_payload.elf.ldS | 2 +-
>  3 files changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 9c7fd4d..9abea9f 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -346,9 +346,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 -Wl,--gc-sections

--gc-sections should be put in the ELFFLAGS

>  ifneq ($(DEBUG),)
> -CFLAGS         +=      -O0
> +CFLAGS         +=      -O0 -Wl,--print-gc-sections
>  else
>  CFLAGS         +=      -O2
>  endif
> 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 --git a/firmware/fw_payload.elf.ldS b/firmware/fw_payload.elf.ldS
> index f1a544b..4e1f011 100644
> --- a/firmware/fw_payload.elf.ldS
> +++ b/firmware/fw_payload.elf.ldS
> @@ -23,7 +23,7 @@ SECTIONS
>         .payload :
>         {
>                 PROVIDE(_payload_start = .);
> -               *(.payload)
> +               KEEP(*(.payload))
>                 . = ALIGN(8);
>                 PROVIDE(_payload_end = .);
>         }
> --

Regards,
Bin
Xiang W Aug. 28, 2024, 4:23 p.m. UTC | #2
在 2024-08-24星期六的 16:16 +0800,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 <1952088712@qq.com>
> Signed-off-by: Yuan Tan <tanyuan@tinylab.org>
> Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> ---
>  Makefile                    | 4 ++--
>  firmware/fw_base.ldS        | 1 +
>  firmware/fw_payload.elf.ldS | 2 +-
>  3 files changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 9c7fd4d..9abea9f 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -346,9 +346,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 -Wl,--gc-sections
>  ifneq ($(DEBUG),)
> -CFLAGS		+=	-O0
> +CFLAGS		+=	-O0 -Wl,--print-gc-sections
>  else
>  CFLAGS		+=	-O2
>  endif
> 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 --git a/firmware/fw_payload.elf.ldS b/firmware/fw_payload.elf.ldS
> index f1a544b..4e1f011 100644
> --- a/firmware/fw_payload.elf.ldS
> +++ b/firmware/fw_payload.elf.ldS
> @@ -23,7 +23,7 @@ SECTIONS
>  	.payload :
>  	{
>  		PROVIDE(_payload_start = .);
> -		*(.payload)
> +		KEEP(*(.payload))
This modification is not necessary, this section will be accessed by
fw_next_addr in fw_payload.S

Regards,
Xiang W
>  		. = ALIGN(8);
>  		PROVIDE(_payload_end = .);
>  	}
> -- 
> 2.34.1
> 
>
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index 9c7fd4d..9abea9f 100644
--- a/Makefile
+++ b/Makefile
@@ -346,9 +346,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 -Wl,--gc-sections
 ifneq ($(DEBUG),)
-CFLAGS		+=	-O0
+CFLAGS		+=	-O0 -Wl,--print-gc-sections
 else
 CFLAGS		+=	-O2
 endif
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 --git a/firmware/fw_payload.elf.ldS b/firmware/fw_payload.elf.ldS
index f1a544b..4e1f011 100644
--- a/firmware/fw_payload.elf.ldS
+++ b/firmware/fw_payload.elf.ldS
@@ -23,7 +23,7 @@  SECTIONS
 	.payload :
 	{
 		PROVIDE(_payload_start = .);
-		*(.payload)
+		KEEP(*(.payload))
 		. = ALIGN(8);
 		PROVIDE(_payload_end = .);
 	}