Skip to contentSkip to conversation

Makefile: enable --gc-sections

PATCH New Kele Zhang 4 participants 6 messages Aug 24, 2024 – Aug 28, 2024 1 message not in the archive Kbuild

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Kele Zhang

author
#1 lore raw

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
index 9c7fd4d..9abea9f 100644
@@ -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
index fb47984..a33746a 100644
@@ -20,6 +20,7 @@
 		PROVIDE(_text_start = .);
 		*(.entry)
 		*(.text)
+		*(.text.*)
 		. = ALIGN(8);
 		PROVIDE(_text_end = .);
 	}
firmware/fw_payload.elf.ldS +1 -1
index f1a544b..4e1f011 100644
@@ -23,7 +23,7 @@ SECTIONS
 	.payload :
 	{
 		PROVIDE(_payload_start = .);
-		*(.payload)
+		KEEP(*(.payload))
 		. = ALIGN(8);
 		PROVIDE(_payload_end = .);
 	}
Signature · 1 line
2.34.1
#2 lore raw

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.

---
 Makefile                    | 4 ++--
 firmware/fw_base.ldS        | 1 +
 firmware/fw_payload.elf.ldS | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
Makefile +0 -0
index 9c7fd4d..9abea9f 100644

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 -1
 ifneq ($(DEBUG),)
-CFLAGS         +=      -O0
+CFLAGS         +=      -O0 -Wl,--print-gc-sections
 else
 CFLAGS         +=      -O2
 endif
firmware/fw_base.ldS +1 -0
index fb47984..a33746a 100644
@@ -20,6 +20,7 @@
                PROVIDE(_text_start = .);
                *(.entry)
                *(.text)
+               *(.text.*)
                . = ALIGN(8);
                PROVIDE(_text_end = .);
        }
firmware/fw_payload.elf.ldS +0 -0
index f1a544b..4e1f011 100644

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

#3 lore raw

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.

---

?Makefile??????????????????? | 4 ++--
?firmware/fw_base.ldS??????? | 1 +
?firmware/fw_payload.elf.ldS | 2 +-

?3 files changed, 4 insertions(+), 3 deletions(-)

Makefile +0 -0
index 9c7fd4d..9abea9f 100644
@@ -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
?endif

firmware/fw_base.ldS +1 -0
index fb47984..a33746a 100644
@@ -20,6 +20,7 @@
 ?		PROVIDE(_text_start = .);
 ?		*(.entry)
 ?		*(.text)
+		*(.text.*)
 ?		. = ALIGN(8);
 ?		PROVIDE(_text_end = .);
 ?	}
firmware/fw_payload.elf.ldS +0 -0
index f1a544b..4e1f011 100644
@@ -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
author
#4 lore raw

PATCH 2/2 Makefile: Remove -g from default CFLAGS

In reply to a message not in the archive

Remove -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
index 9abea9f..bcec588 100644
@@ -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
#5 lore raw

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 -g from default CFLAGS as it should not be used in production builds. Adjust optimization level to -Og when 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
---
 Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Makefile +0 -0
index 9abea9f..bcec588 100644
@@ -346,9 +346,9 @@ GENFLAGS    +=      $(libsbiutils-genflags-y)
 GENFLAGS       +=      $(platform-genflags-y)

Makefile GENFLAGS += $(libsbiutils-genflags-y) · line 354

Quoted 10 lines
Makefile +2 -2
 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

Regards,
Bin

#6 lore raw

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 -g from default CFLAGS as it should not be used in production builds. Adjust optimization level to -Og when 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