diff mbox series

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

Message ID 004ed61b6c6743ab87a369bc6202b4b1c50de89d.1724959010.git.zhangcola2003@gmail.com
State Superseded
Headers show
Series Enable Dead Code Elimination | expand

Commit Message

Kele Zhang Aug. 29, 2024, 8:12 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>
---
 Makefile             | 5 +++--
 firmware/fw_base.ldS | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)

Comments

Xiang W Aug. 30, 2024, 4:13 p.m. UTC | #1
在 2024-08-29星期四的 20:12 +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>
LGTM

Reviewed-by: Xiang W <wxjstz@126.com>
> ---
>  Makefile             | 5 +++--
>  firmware/fw_base.ldS | 1 +
>  2 files changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index e5a0f19..44db5d4 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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
> 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 = .);
>  	}
Bin Meng Sept. 1, 2024, 11:12 a.m. UTC | #2
On Fri, Aug 30, 2024 at 4:12 AM 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             | 5 +++--
>  firmware/fw_base.ldS | 1 +
>  2 files changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index e5a0f19..44db5d4 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -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.

>  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
> 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 = .);
>         }

Regards,
Bin
Kele Zhang Sept. 1, 2024, 11:51 a.m. UTC | #3
Bin Meng <bmeng.cn@gmail.com> 于2024年9月1日周日 19:12写道:
>
> On Fri, Aug 30, 2024 at 4:12 AM 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             | 5 +++--
> >  firmware/fw_base.ldS | 1 +
> >  2 files changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/Makefile b/Makefile
> > index e5a0f19..44db5d4 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -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.

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?

>
> >  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
> > 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 = .);
> >         }
>
> Regards,
> Bin
Xiang W Sept. 1, 2024, 12:14 p.m. UTC | #4
在 2024-09-01星期日的 19:51 +0800,cola zhang写道:
> Bin Meng <bmeng.cn@gmail.com> 于2024年9月1日周日 19:12写道:
> > 
> > On Fri, Aug 30, 2024 at 4:12 AM 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             | 5 +++--
> > >  firmware/fw_base.ldS | 1 +
> > >  2 files changed, 4 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/Makefile b/Makefile
> > > index e5a0f19..44db5d4 100644
> > > --- a/Makefile
> > > +++ b/Makefile
> > > @@ -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.
> 
> 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
> 
> > 
> > >  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
> > > 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 = .);
> > >         }
> > 
> > Regards,
> > Bin
Kele Zhang Sept. 1, 2024, 12:35 p.m. UTC | #5
Xiang W <wxjstz@126.com> 于2024年9月1日周日 20:15写道:
>
> 在 2024-09-01星期日的 19:51 +0800,cola zhang写道:
> > Bin Meng <bmeng.cn@gmail.com> 于2024年9月1日周日 19:12写道:
> > >
> > > On Fri, Aug 30, 2024 at 4:12 AM 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             | 5 +++--
> > > >  firmware/fw_base.ldS | 1 +
> > > >  2 files changed, 4 insertions(+), 2 deletions(-)
> > > >
> > > > diff --git a/Makefile b/Makefile
> > > > index e5a0f19..44db5d4 100644
> > > > --- a/Makefile
> > > > +++ b/Makefile
> > > > @@ -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.
> >
> > 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.

>
> Regards,
> Xiang W
> >
> > >
> > > >  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
> > > > 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 = .);
> > > >         }
> > >
> > > Regards,
> > > Bin
>
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index e5a0f19..44db5d4 100644
--- a/Makefile
+++ b/Makefile
@@ -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
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 = .);
 	}