Makefile: enable --gc-sections
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/2 Makefile: enable --gc-sections
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.
--- Makefile | 4 ++-- firmware/fw_base.ldS | 1 + firmware/fw_payload.elf.ldS | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-)
Makefile +2 -2
@@ -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
firmware/fw_base.ldS +1 -0
@@ -20,6 +20,7 @@ PROVIDE(_text_start = .); *(.entry) *(.text) + *(.text.*) . = ALIGN(8); PROVIDE(_text_end = .); }
firmware/fw_payload.elf.ldS +1 -1
@@ -23,7 +23,7 @@ SECTIONS .payload : { PROVIDE(_payload_start = .); - *(.payload) + KEEP(*(.payload)) . = ALIGN(8); PROVIDE(_payload_end = .); }
Signature · 1 line
2.34.1
PATCH 1/2 Makefile: enable --gc-sections
Quoted 22 lines · On Sat, Aug 24, 2024 at 4:19?PM Kele Zhang <[email protected]> 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 <[email protected]>Signed-off-by Yuan Tan <[email protected]>Signed-off-by Zhangjin Wu <[email protected]>--- Makefile | 4 ++-- firmware/fw_base.ldS | 1 + firmware/fw_payload.elf.ldS | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-)Makefile +0
-0
Makefile GENFLAGS += $(libsbiutils-genflags-y) · line 349
Quoted 5 lines
Makefile +1
-1@@ -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
Quoted 23 lines
Makefile +1
-1ifneq ($(DEBUG),) -CFLAGS += -O0 +CFLAGS += -O0 -Wl,--print-gc-sections else CFLAGS += -O2 endiffirmware/fw_base.ldS +1
-0@@ -20,6 +20,7 @@ PROVIDE(_text_start = .); *(.entry) *(.text) + *(.text.*) . = ALIGN(8); PROVIDE(_text_end = .); }firmware/fw_payload.elf.ldS +0
-0
firmware/fw_payload.elf.ldS SECTIONS
Quoted 9 lines
firmware/fw_payload.elf.ldS +1
-2@@ -23,7 +23,7 @@ SECTIONS .payload : { PROVIDE(_payload_start = .); - *(.payload) + KEEP(*(.payload)) . = ALIGN(8); PROVIDE(_payload_end = .); } --
Regards,
Bin
PATCH 1/2 Makefile: enable --gc-sections
In reply to Kele Zhang? 2024-08-24???? 16:16 +0800?Kele Zhang???
Quoted 53 lines
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 <[email protected]>Signed-off-by Yuan Tan <[email protected]>Signed-off-by Zhangjin Wu <[email protected]>---
?Makefile??????????????????? | 4 ++-- ?firmware/fw_base.ldS??????? | 1 + ?firmware/fw_payload.elf.ldS | 2 +-?3 files changed, 4 insertions(+), 3 deletions(-)
Makefile +0
-0@@ -346,9 +346,9 @@ GENFLAGS += $(libsbiutils-genflags-y) ?GENFLAGS += $(platform-genflags-y) ?GENFLAGS += $(firmware-genflags-y)?
Makefile +1
-1-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),)Makefile +1
-1-CFLAGS += -O0 +CFLAGS += -O0 -Wl,--print-gc-sections?else
?CFLAGS += -O2
?endiffirmware/fw_base.ldS +1
-0@@ -20,6 +20,7 @@ ? PROVIDE(_text_start = .); ? *(.entry) ? *(.text) + *(.text.*) ? . = ALIGN(8); ? PROVIDE(_text_end = .); ? }firmware/fw_payload.elf.ldS +0
-0@@ -23,7 +23,7 @@ SECTIONS? .payload :
? {
?PROVIDE(_payload_start = .);
firmware/fw_payload.elf.ldS SECTIONS · line 26
Quoted 2 lines
firmware/fw_payload.elf.ldS +1
-1- *(.payload) + KEEP(*(.payload))
This modification is not necessary, this section will be accessed by fw_next_addr in fw_payload.S
Regards,
Xiang W
Quoted 7 lines
? . = ALIGN(8);
?PROVIDE(_payload_end = .);
? }Signature · 1 line
2.34.1
PATCH 2/2 Makefile: Remove -g from default CFLAGS
In reply to a message not in the archiveRemove -g from default CFLAGS as it should not be used in production builds. Adjust optimization level to -Og when DEBUG is enabled.
--- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Makefile +2 -2
@@ -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 -ffunction-sections -fdata-sections -Wl,--gc-sections +CFLAGS = -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-sections -Wl,--gc-sections ifneq ($(DEBUG),) -CFLAGS += -O0 -Wl,--print-gc-sections +CFLAGS += -Og -Wl,--print-gc-sections else CFLAGS += -O2 endif
Signature · 1 line
2.34.1
PATCH 2/2 Makefile: Remove -g from default CFLAGS
Quoted 3 lines · On Sat, Aug 24, 2024 at 4:22?PM Kele Zhang <[email protected]> wrote:
Remove
-gfrom default CFLAGS as it should not be used in production builds. Adjust optimization level to-Ogwhen DEBUG is enabled.
The production build does not use ELF images to program onto the board but the plain binary image instead. So -gshould be fine. As for -Og vs. -O0 I prefer using -O0 as -Og sometimes breaks debugging.
Quoted 13 lines
Signed-off-by Kele Zhang <[email protected]>Signed-off-by Yuan Tan <[email protected]>--- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Makefile +0
-0@@ -346,9 +346,9 @@ GENFLAGS += $(libsbiutils-genflags-y) GENFLAGS += $(platform-genflags-y)
Makefile GENFLAGS += $(libsbiutils-genflags-y) · line 354
Quoted 10 lines
Makefile +2
-2GENFLAGS += $(firmware-genflags-y) -CFLAGS = -g -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-sections -Wl,--gc-sections +CFLAGS = -Wall -Werror -ffreestanding -nostdlib -fno-stack-protector -fno-strict-aliasing -ffunction-sections -fdata-sections -Wl,--gc-sections ifneq ($(DEBUG),) -CFLAGS += -O0 -Wl,--print-gc-sections +CFLAGS += -Og -Wl,--print-gc-sections else CFLAGS += -O2 endif
Regards,
Bin
PATCH 2/2 Makefile: Remove -g from default CFLAGS
Quoted 8 lines · Bin Meng
Quoted 3 lines · On Sat, Aug 24, 2024 at 4:22?PM Kele Zhang <[email protected]> wrote:
Remove
-gfrom default CFLAGS as it should not be used in production builds. Adjust optimization level to-Ogwhen DEBUG is enabled.The production build does not use ELF images to program onto the board but the plain binary image instead. So
-gshould be fine.
And even for boards that do, -g is the right thing to do. If you don?t want debugging symbols, strip the binary, but it?s useful to default to including them; distributions generally do exactly that, stripping the installed binary but keeping the debug symbols around in a separate .debug file.
Debug symbols also aren?t loaded into memory, they only exist on disk, so the only cost is a bit of disk space.
Jess