I'm trying to use clang compiler and GNU Arm Embedded Toolchain assembler+linker+libraries to build a LED blinker for STM32F103. My host system is Debian x86_64. I use a Makefile project generated in STM32CubeMX (which is based on GCC) with some changes in makefile. However, the size of result binary file is 16KB, when the bin created using original Cube project is 5KB. What's more, I noticed that using sprintf() (for UART), increases the size to 51KB. I understand that dynamic memory allocation may consume a lot of space, but for the same code the size of bin created in Cube project is only 7KB.
How to decrease the binary size of clang+GNU toolchain configuration?
Compilation:
clang -c --target=armv7m-none-eabi -mcpu=cortex-m3 -mthumb $(INCLUDES) -O2 -fdata-sections -ffunction-sections file.c -o file.o
arm-none-eabi-as startup_stm32f103xb.S -o startup_stm32f103xb.o
Linking:
clang -v -nostdlib --target=armv7m-none-eabi -mcpu=cortex-m3 \
--ld-path=/path/to/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-gcc \
-TSTM32F103C8Tx_FLASH.ld \
-L"/path/to/gcc-arm-none-eabi-9-2020-q2-update/lib/gcc/arm-none-eabi/9.3.1/thumb/v7-m/nofp" \
-L"/path/to/gcc-arm-none-eabi-9-2020-q2-update/arm-none-eabi/lib/thumb/v7-m/nofp/" \
$(OBJECTS) startup_stm32f103xb.o \
-o out
Getting binary:
arm-none-eabi-objcopy -O binary -S out out.bin
Some of the functions and objects are really space-consuming, mainly those related to dynamic memory management, e.g.:
$readelf -a out
...
775: 080049b9 6992 FUNC GLOBAL DEFAULT 2 _svfprintf_r
964: 20000444 1032 OBJECT GLOBAL DEFAULT 7 __malloc_av_
993: 08006709 3784 FUNC GLOBAL DEFAULT 2 _dtoa_r
994: 080078e5 1372 FUNC GLOBAL DEFAULT 2 _malloc_r
1032: 08008b29 2852 FUNC GLOBAL DEFAULT 2 _svfiprintf_r
1073: 08009e41 3372 FUNC GLOBAL DEFAULT 2 _vfiprintf_r
...