Commit 4380f79c authored by Hugo Grostabussiat's avatar Hugo Grostabussiat

Drastically reduce the size of m4.bin

This commit reduces the size of the output binary by the following
means:
1) enabling compiler optimization and requesting it to optimize for size
2) using the -ffunction-sections and -fdata-sections compiler options in
   addition to the --gc-sections linker option.

The -ffunction-sections and -fdata-sections compiler options causes the
compiler to generate a new section for every function and object.
Doing this enables the linker to move around individual function instead
of full compilation units.

The --gc-sections option instructs the linker to discard input sections
which are not referenced by other sections in the output.

Those two features combined allow the linker to discard all the
functions which are unused, which can save a lot of space in the
resulting binary.

To know which sections are unused, the linker needs to be told at least
one section it should keep, otherwise it would discard all the sections
since the firmware does not have a standard entry point.

In our case, the ldscript orders the linker to keep the m4_start.o
.text section, as it is the one that contains the vectors table, which
is the root from which all the useful code will be called.

The size of the resulting m4.bin after thoses optimizations is about
4.4 KiB vs. 11 KiB before.
parent ab83de31
...@@ -5,8 +5,10 @@ OBJCOPY = arm-none-eabi-objcopy ...@@ -5,8 +5,10 @@ OBJCOPY = arm-none-eabi-objcopy
CFLAGS += -g CFLAGS += -g
CFLAGS += -march=armv7e-m -mthumb -mcpu=cortex-m4 \ CFLAGS += -march=armv7e-m -mthumb -mcpu=cortex-m4 \
-mlittle-endian -mfloat-abi=softfp \ -mlittle-endian -mfloat-abi=softfp \
-fno-zero-initialized-in-bss -fno-zero-initialized-in-bss \
LDFLAGS := -T ldscript -nostartfiles -Wl,-Map,m4.map,--cref,-n -O2 -Os -ffunction-sections -fdata-sections
LDFLAGS := -Wl,--gc-sections -T ldscript -nostartfiles \
-Wl,-Map,m4.map,--cref,-n
OBJS := m4_start.o \ OBJS := m4_start.o \
m4_main.o \ m4_main.o \
......
...@@ -3,7 +3,7 @@ SECTIONS ...@@ -3,7 +3,7 @@ SECTIONS
{ {
.text : .text :
{ {
*m4_start.o(.text) KEEP(*m4_start.o(.text))
*(.text .text.*) *(.text .text.*)
} }
.data : .data :
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment