* [PATCH v2 0/1] Enable Dead Code Elimination
@ 2024-08-29 20:11 Kele Zhang
2024-08-29 20:12 ` [PATCH v2 1/1] Makefile: enable --gc-sections Kele Zhang
0 siblings, 1 reply; 7+ messages in thread
From: Kele Zhang @ 2024-08-29 20:11 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
---
Changes in v2:
- Move --gc-sections to ELFFLAGS
- Remove redundant KEEP()
Kele Zhang (1):
Makefile: enable --gc-sections
Makefile | 5 +++--
firmware/fw_base.ldS | 1 +
2 files changed, 4 insertions(+), 2 deletions(-)
base-commit: c4940a9517486413cd676fc8032bb55f9d4e2778
--
2.34.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/1] Makefile: enable --gc-sections
2024-08-29 20:11 [PATCH v2 0/1] Enable Dead Code Elimination Kele Zhang
@ 2024-08-29 20:12 ` Kele Zhang
2024-08-30 16:13 ` Xiang W
2024-09-01 11:12 ` Bin Meng
0 siblings, 2 replies; 7+ messages in thread
From: Kele Zhang @ 2024-08-29 20:12 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 | 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 = .);
}
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 1/1] Makefile: enable --gc-sections
2024-08-29 20:12 ` [PATCH v2 1/1] Makefile: enable --gc-sections Kele Zhang
@ 2024-08-30 16:13 ` Xiang W
2024-09-01 11:12 ` Bin Meng
1 sibling, 0 replies; 7+ messages in thread
From: Xiang W @ 2024-08-30 16:13 UTC (permalink / raw)
To: opensbi
? 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 = .);
> ? }
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/1] Makefile: enable --gc-sections
2024-08-29 20:12 ` [PATCH v2 1/1] Makefile: enable --gc-sections Kele Zhang
2024-08-30 16:13 ` Xiang W
@ 2024-09-01 11:12 ` Bin Meng
2024-09-01 11:51 ` cola zhang
1 sibling, 1 reply; 7+ messages in thread
From: Bin Meng @ 2024-09-01 11:12 UTC (permalink / raw)
To: opensbi
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/1] Makefile: enable --gc-sections
2024-09-01 11:12 ` Bin Meng
@ 2024-09-01 11:51 ` cola zhang
2024-09-01 12:14 ` Xiang W
0 siblings, 1 reply; 7+ messages in thread
From: cola zhang @ 2024-09-01 11:51 UTC (permalink / raw)
To: opensbi
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/1] Makefile: enable --gc-sections
2024-09-01 11:51 ` cola zhang
@ 2024-09-01 12:14 ` Xiang W
2024-09-01 12:35 ` cola zhang
0 siblings, 1 reply; 7+ messages in thread
From: Xiang W @ 2024-09-01 12:14 UTC (permalink / raw)
To: opensbi
? 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
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/1] Makefile: enable --gc-sections
2024-09-01 12:14 ` Xiang W
@ 2024-09-01 12:35 ` cola zhang
0 siblings, 0 replies; 7+ messages in thread
From: cola zhang @ 2024-09-01 12:35 UTC (permalink / raw)
To: opensbi
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
>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2024-09-01 12:35 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-29 20:11 [PATCH v2 0/1] Enable Dead Code Elimination Kele Zhang
2024-08-29 20:12 ` [PATCH v2 1/1] Makefile: enable --gc-sections Kele Zhang
2024-08-30 16:13 ` Xiang W
2024-09-01 11:12 ` Bin Meng
2024-09-01 11:51 ` cola zhang
2024-09-01 12:14 ` Xiang W
2024-09-01 12:35 ` cola zhang
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.