Skip to content

Commit d7bddb1

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 4835f51 commit d7bddb1

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_GD32F103RC
1214
MCU?=GD32F103RC
@@ -58,14 +60,10 @@ COPS+=-fstack-usage
5860
COPS+=-ffunction-sections -fdata-sections
5961
COPS+=-Wall -Werror -Wpedantic -Wextra -Wunused -Wsign-conversion -Wconversion -Wduplicated-cond -Wlogical-op
6062
COPS+=--specs=nosys.specs
63+
COPS+=-flto=auto
6164

6265
include ../common/make/CppOps.mk
63-
64-
LDOPS=--gc-sections --print-gc-sections --print-memory-usage
65-
66-
PLATFORM_LIBGCC+= -L $(shell dirname `$(CC) $(COPS) -print-libgcc-file-name`)
67-
68-
$(info $$PLATFORM_LIBGCC [${PLATFORM_LIBGCC}])
66+
include ../common/make/LdOps.mk
6967

7068
C_OBJECTS=$(foreach sdir,$(SRCDIR),$(patsubst $(sdir)/%.c,$(BUILD)$(sdir)/%.o,$(wildcard $(sdir)/*.c)))
7169
CPP_OBJECTS+=$(foreach sdir,$(SRCDIR),$(patsubst $(sdir)/%.cpp,$(BUILD)$(sdir)/%.o,$(wildcard $(sdir)/*.cpp)))
@@ -113,24 +111,70 @@ $(LIBDEP):
113111
$(MAKE) -f Makefile.GD32 $(MAKECMDGOALS) 'PROJECT=${PROJECT}' 'FAMILY=${FAMILY}' 'MCU=${MCU}' 'BOARD=${BOARD}' 'MAKE_FLAGS=$(DEFINES)' -C $@
114112

115113
#
116-
# Build bin
114+
# Startup and support objects
117115
#
118116

117+
# Assemble the MCU startup code.
119118
$(BUILD)startup_$(LINE).o : $(FIRMWARE_DIR)/startup_$(LINE).S
120119
$(AS) $(COPS) -D__ASSEMBLY__ -c $(FIRMWARE_DIR)/startup_$(LINE).S -o $(BUILD)startup_$(LINE).o
121120

121+
# Compile the common HardFault handler.
122122
$(BUILD)hardfault_handler.o : $(FIRMWARE_DIR)/hardfault_handler.cpp
123123
$(CPP) $(COPS) $(CPPOPS) -c $(FIRMWARE_DIR)/hardfault_handler.cpp -o $(BUILD)hardfault_handler.o
124124

125+
# Compile the common debug Stack handler.
125126
$(BUILD)stack_debug_init.o : $(FIRMWARE_DIR)/stack_debug_init.cpp
126127
$(CPP) $(COPS) $(CPPOPS) -c $(FIRMWARE_DIR)/stack_debug_init.cpp -o $(BUILD)stack_debug_init.o
127128

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

133-
$(TARGET) : $(BUILD)main.elf
134-
$(PREFIX)objcopy $(BUILD)main.elf -O binary $(TARGET) --remove-section=.tcmsram* --remove-section=.sram1* --remove-section=.sram2* --remove-section=.ramadd* --remove-section=.bkpsram*
167+
# Convert the ELF image into a binary image. RAM-only sections are
168+
# removed because they are initialized at runtime rather than stored
169+
# in flash.
170+
$(TARGET): $(BUILD)main.elf
171+
$(PREFIX)objcopy $< \
172+
-O binary \
173+
$@ \
174+
--remove-section=.tcmsram* \
175+
--remove-section=.sram1* \
176+
--remove-section=.sram2* \
177+
--remove-section=.ramadd* \
178+
--remove-section=.bkpsram*
135179

136180
$(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_GD32F103RC
1315
MCU?=GD32F103RC
@@ -37,6 +39,7 @@ COPS+=-Wall -Werror -Wpedantic -Wextra -Wunused -Wsign-conversion -Wduplicated-c
3739
ifndef FREE_RTOS_PORTABLE
3840
COPS+=-Wconversion
3941
endif
42+
COPS+=-flto=auto
4043

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

0 commit comments

Comments
 (0)