Skip to content

Commit 5361c49

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 1d4e6ed commit 5361c49

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_GD32F450VI
1214
ENET_PHY?=RTL8201F
@@ -64,14 +66,10 @@ COPS+=-fstack-usage
6466
COPS+=-ffunction-sections -fdata-sections
6567
COPS+=-Wall -Werror -Wpedantic -Wextra -Wunused -Wsign-conversion -Wconversion -Wduplicated-cond -Wlogical-op
6668
COPS+=--specs=nosys.specs
69+
COPS+=-flto=auto
6770

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

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

121119
#
122-
# Build bin
120+
# Startup and support objects
123121
#
124122

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

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

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

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

139-
$(TARGET) : $(BUILD)main.elf
140-
$(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*
173+
# Convert the ELF image into a binary image. RAM-only sections are
174+
# removed because they are initialized at runtime rather than stored
175+
# in flash.
176+
$(TARGET): $(BUILD)main.elf
177+
$(PREFIX)objcopy $< \
178+
-O binary \
179+
$@ \
180+
--remove-section=.tcmsram* \
181+
--remove-section=.sram1* \
182+
--remove-section=.sram2* \
183+
--remove-section=.ramadd* \
184+
--remove-section=.bkpsram*
141185

142186
$(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_GD32F450VI
1315
ENET_PHY?=RTL8201F
@@ -40,6 +42,7 @@ COPS+=-Wall -Werror -Wpedantic -Wextra -Wunused -Wsign-conversion -Wduplicated-c
4042
ifndef FREE_RTOS_PORTABLE
4143
COPS+=-Wconversion
4244
endif
45+
COPS+=-flto=auto
4346

4447
include ../common/make/CppOps.mk
4548
include ../common/make/gd32/Gd32FirmwareOps.mk

0 commit comments

Comments
 (0)