Skip to content

Commit 1527cef

Browse files
committed
Enable LTO
Adds a shared `common/make/LdOps.mk` to centralize linker options, including grouped libs, GC/memory reporting, map output, and forced `memcpy`/`memset` symbol pulls. Updates GD32 firmware and library rules to use `gcc` as linker plus `gcc-ar`/`gcc-ranlib`/`gcc-nm`, and enables `-flto=auto` in both builds. The main firmware link/objcopy rules were also cleaned up and documented for clearer build flow.
1 parent 393380d commit 1527cef

3 files changed

Lines changed: 79 additions & 23 deletions

File tree

common/make/LdOps.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
LDLIBS:=-Wl,--start-group $(LDLIBS) -Wl,--end-group
2+
LDOPS = $(COPS)\
3+
-Wno-error=uninitialized \
4+
-Wl,--gc-sections \
5+
-Wl,--print-gc-sections \
6+
-Wl,--print-memory-usage \
7+
-Wl,-u,memcpy \
8+
-Wl,-u,memset \
9+
-Wl,-Map=$(MAP)

firmware-template-gd32/Rules.mk

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ $(info "Rules.mk")
22

33
PREFIX ?= arm-none-eabi-
44

5-
CC = $(PREFIX)gcc
6-
CPP = $(PREFIX)g++
7-
AS = $(CC)
8-
LD = $(PREFIX)ld
9-
AR = $(PREFIX)ar
5+
CC = $(PREFIX)gcc
6+
CPP = $(PREFIX)g++
7+
AS = $(CC)
8+
LD = $(PREFIX)gcc
9+
AR = $(PREFIX)gcc-ar
10+
RANLIB = $(PREFIX)gcc-ranlib
11+
NM = $(PREFIX)gcc-nm
1012

1113
BOARD?=BOARD_GD32F207RG
1214
ENET_PHY?=RTL8201F
@@ -62,14 +64,10 @@ COPS+=-fstack-usage
6264
COPS+=-ffunction-sections -fdata-sections
6365
COPS+=-Wall -Werror -Wpedantic -Wextra -Wunused -Wsign-conversion -Wconversion -Wduplicated-cond -Wlogical-op
6466
COPS+=--specs=nosys.specs
67+
COPS+=-flto=auto
6568

6669
include ../common/make/CppOps.mk
67-
68-
LDOPS=--gc-sections --print-gc-sections --print-memory-usage
69-
70-
PLATFORM_LIBGCC+= -L $(shell dirname `$(CC) $(COPS) -print-libgcc-file-name`)
71-
72-
$(info $$PLATFORM_LIBGCC [${PLATFORM_LIBGCC}])
70+
include ../common/make/LdOps.mk
7371

7472
C_OBJECTS=$(foreach sdir,$(SRCDIR),$(patsubst $(sdir)/%.c,$(BUILD)$(sdir)/%.o,$(wildcard $(sdir)/*.c)))
7573
CPP_OBJECTS+=$(foreach sdir,$(SRCDIR),$(patsubst $(sdir)/%.cpp,$(BUILD)$(sdir)/%.o,$(wildcard $(sdir)/*.cpp)))
@@ -117,24 +115,70 @@ $(LIBDEP):
117115
$(MAKE) -f Makefile.GD32 $(MAKECMDGOALS) 'PROJECT=${PROJECT}' 'FAMILY=${FAMILY}' 'MCU=${MCU}' 'BOARD=${BOARD}' 'ENET_PHY=${ENET_PHY}' 'MAKE_FLAGS=$(DEFINES)' -C $@
118116

119117
#
120-
# Build bin
118+
# Startup and support objects
121119
#
122120

121+
# Assemble the MCU startup code.
123122
$(BUILD)startup_$(LINE).o : $(FIRMWARE_DIR)/startup_$(LINE).S
124123
$(AS) $(COPS) -D__ASSEMBLY__ -c $(FIRMWARE_DIR)/startup_$(LINE).S -o $(BUILD)startup_$(LINE).o
125124

125+
# Compile the common HardFault handler.
126126
$(BUILD)hardfault_handler.o : $(FIRMWARE_DIR)/hardfault_handler.cpp
127127
$(CPP) $(COPS) $(CPPOPS) -c $(FIRMWARE_DIR)/hardfault_handler.cpp -o $(BUILD)hardfault_handler.o
128128

129+
# Compile the common debug Stack handler.
129130
$(BUILD)stack_debug_init.o : $(FIRMWARE_DIR)/stack_debug_init.cpp
130131
$(CPP) $(COPS) $(CPPOPS) -c $(FIRMWARE_DIR)/stack_debug_init.cpp -o $(BUILD)stack_debug_init.o
131132

132-
$(BUILD)main.elf: Makefile.GD32 $(LINKER) $(BUILD)startup_$(LINE).o $(BUILD)hardfault_handler.o $(BUILD)stack_debug_init.o $(OBJECTS) $(LIBDEP)
133-
$(LD) $(BUILD)startup_$(LINE).o $(BUILD)hardfault_handler.o $(BUILD)stack_debug_init.o $(OBJECTS) -Map $(MAP) -T $(LINKER) $(LDOPS) -o $(BUILD)main.elf $(LIBGD32) $(LDLIBS) $(PLATFORM_LIBGCC) -lgcc
134-
$(PREFIX)objdump -D $(BUILD)main.elf | $(PREFIX)c++filt > $(LIST)
135-
$(PREFIX)size -A -x $(BUILD)main.elf
133+
#
134+
# Link the ELF image
135+
#
136+
137+
# Link all object files together with the dependent libraries.
138+
# A linker map and a demangled disassembly listing are generated
139+
# for debugging and analysis.
140+
$(BUILD)main.elf: \
141+
Makefile.GD32 \
142+
$(LINKER) \
143+
$(BUILD)startup_$(LINE).o \
144+
$(BUILD)hardfault_handler.o \
145+
$(BUILD)stack_debug_init.o \
146+
$(OBJECTS) \
147+
$(LIBDEP) \
148+
| builddirs
149+
$(LD) \
150+
$(BUILD)startup_$(LINE).o \
151+
$(BUILD)hardfault_handler.o \
152+
$(BUILD)stack_debug_init.o \
153+
$(OBJECTS) \
154+
-T $(LINKER) \
155+
$(LDOPS) \
156+
-o $@ \
157+
$(LIBGD32) \
158+
$(LDLIBS) \
159+
-lgcc
160+
161+
# Generate a demangled disassembly listing.
162+
$(PREFIX)objdump -D $@ | $(PREFIX)c++filt > $(LIST)
163+
164+
# Display the memory usage by section.
165+
$(PREFIX)size -A -x $@
166+
167+
#
168+
# Create the binary firmware image
169+
#
136170

137-
$(TARGET) : $(BUILD)main.elf
138-
$(PREFIX)objcopy $(BUILD)main.elf -O binary $(TARGET) --remove-section=.tcmsram* --remove-section=.ram* --remove-section=.sram1* --remove-section=.sram2* --remove-section=.ramadd* --remove-section=.bkpsram*
171+
# Convert the ELF image into a binary image. RAM-only sections are
172+
# removed because they are initialized at runtime rather than stored
173+
# in flash.
174+
$(TARGET): $(BUILD)main.elf
175+
$(PREFIX)objcopy $< \
176+
-O binary \
177+
$@ \
178+
--remove-section=.tcmsram* \
179+
--remove-section=.sram1* \
180+
--remove-section=.sram2* \
181+
--remove-section=.ramadd* \
182+
--remove-section=.bkpsram*
139183

140184
$(foreach bdir,$(SRCDIR),$(eval $(call compile-objects,$(bdir))))

firmware-template-gd32/lib/Rules.mk

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ $(info $$MAKE_FLAGS [${MAKE_FLAGS}])
33

44
PREFIX ?= arm-none-eabi-
55

6-
CC = $(PREFIX)gcc
7-
CPP = $(PREFIX)g++
8-
AS = $(CC)
9-
LD = $(PREFIX)ld
10-
AR = $(PREFIX)ar
6+
CC = $(PREFIX)gcc
7+
CPP = $(PREFIX)g++
8+
AS = $(CC)
9+
LD = $(PREFIX)gcc
10+
AR = $(PREFIX)gcc-ar
11+
RANLIB = $(PREFIX)gcc-ranlib
12+
NM = $(PREFIX)gcc-nm
1113

1214
BOARD?=BOARD_GD32F207RG
1315
ENET_PHY?=RTL8201F
@@ -36,6 +38,7 @@ COPS+=-Os -nostartfiles -ffreestanding -nostdlib
3638
COPS+=-fstack-usage
3739
COPS+=-ffunction-sections -fdata-sections
3840
COPS+=-Wall -Werror -Wpedantic -Wextra -Wunused -Wsign-conversion -Wduplicated-cond -Wlogical-op
41+
COPS+=-flto=auto
3942

4043
include ../common/make/CppOps.mk
4144
include ../common/make/gd32/Gd32FirmwareOps.mk

0 commit comments

Comments
 (0)