Skip to content

Commit 66c951f

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 033c865 commit 66c951f

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_GD32F407RE
1214
ENET_PHY?=DP83848
@@ -54,14 +56,10 @@ COPS+=-fstack-usage
5456
COPS+=-ffunction-sections -fdata-sections
5557
COPS+=-Wall -Werror -Wpedantic -Wextra -Wunused -Wsign-conversion -Wconversion -Wduplicated-cond -Wlogical-op
5658
COPS+=--specs=nosys.specs
59+
COPS+=-flto=auto
5760

5861
include ../common/make/CppOps.mk
59-
60-
LDOPS=--gc-sections --print-gc-sections --print-memory-usage
61-
62-
PLATFORM_LIBGCC+= -L $(shell dirname `$(CC) $(COPS) -print-libgcc-file-name`)
63-
64-
$(info $$PLATFORM_LIBGCC [${PLATFORM_LIBGCC}])
62+
include ../common/make/LdOps.mk
6563

6664
C_OBJECTS=$(foreach sdir,$(SRCDIR),$(patsubst $(sdir)/%.c,$(BUILD)$(sdir)/%.o,$(wildcard $(sdir)/*.c)))
6765
CPP_OBJECTS+=$(foreach sdir,$(SRCDIR),$(patsubst $(sdir)/%.cpp,$(BUILD)$(sdir)/%.o,$(wildcard $(sdir)/*.cpp)))
@@ -109,24 +107,70 @@ $(LIBDEP):
109107
$(MAKE) -f Makefile.GD32 $(MAKECMDGOALS) 'PROJECT=${PROJECT}' 'FAMILY=${FAMILY}' 'MCU=${MCU}' 'BOARD=${BOARD}' 'ENET_PHY=${ENET_PHY}' 'MAKE_FLAGS=$(DEFINES)' -C $@
110108

111109
#
112-
# Build bin
110+
# Startup and support objects
113111
#
114112

113+
# Assemble the MCU startup code.
115114
$(BUILD)startup_$(LINE).o : $(FIRMWARE_DIR)/startup_$(LINE).S
116115
$(AS) $(COPS) -D__ASSEMBLY__ -c $(FIRMWARE_DIR)/startup_$(LINE).S -o $(BUILD)startup_$(LINE).o
117116

117+
# Compile the common HardFault handler.
118118
$(BUILD)hardfault_handler.o : $(FIRMWARE_DIR)/hardfault_handler.cpp
119119
$(CPP) $(COPS) $(CPPOPS) -c $(FIRMWARE_DIR)/hardfault_handler.cpp -o $(BUILD)hardfault_handler.o
120120

121+
# Compile the common debug Stack handler.
121122
$(BUILD)stack_debug_init.o : $(FIRMWARE_DIR)/stack_debug_init.cpp
122123
$(CPP) $(COPS) $(CPPOPS) -c $(FIRMWARE_DIR)/stack_debug_init.cpp -o $(BUILD)stack_debug_init.o
123124

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

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

132176
$(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_GD32F407RE
1315
ENET_PHY?=DP83848
@@ -34,6 +36,7 @@ COPS+=-Os -nostartfiles -ffreestanding -nostdlib
3436
COPS+=-fstack-usage
3537
COPS+=-ffunction-sections -fdata-sections
3638
COPS+=-Wall -Werror -Wpedantic -Wextra -Wunused -Wsign-conversion -Wduplicated-cond -Wlogical-op
39+
COPS+=-flto=auto
3740

3841
include ../common/make/CppOps.mk
3942
include ../common/make/gd32/Gd32FirmwareOps.mk

0 commit comments

Comments
 (0)