x86: Improve release binary size

This patch adds -ffunction-sections and -fdata-sections to the
'release' CFLAGS so each function and data is place into its
own section in the output file. It also adds --gc-section to
the 'release' LDFLAGS so the linker removes the sections which
are not referenced.

This patch also adds -ffunction-sections and -fdata-sections
options to CFLAGS from build_newlib.sh. This increases newlib
static libraries size, however, the Contiki image shrinks even
more since --gc-section removes "dead code" from newlib.

As a practical effect, all unused function and data (as well as
sections such as .eh_frame) are striped out from the final elf
binary. This shrinks our release binary drastically.

Finally, to prevent --gc-section from removing .multiboot section,
this patch adds KEEP(*(.multiboot)) to quarkX1000.ld.
This commit is contained in:
Andre Guedes 2015-09-29 17:06:21 -03:00 committed by Jesus Sanchez-Palencia
parent c796e270bf
commit 6b433aede1
3 changed files with 7 additions and 5 deletions

View File

@ -13,8 +13,10 @@ CFLAGS += -Wall -fno-asynchronous-unwind-tables
LDFLAGS += -Wl,-Map=contiki-$(TARGET).map,--build-id=none
ifeq ($(BUILD_RELEASE),1)
CFLAGS += -Os -fno-strict-aliasing
LDFLAGS += -Wl,--strip-all
CFLAGS += -Os -fno-strict-aliasing -ffunction-sections -fdata-sections
# XXX: --gc-sections can be very tricky sometimes. If somehow the release
# binary seems to be broken, check if removing this option fixes the issue.
LDFLAGS += -Wl,--strip-all,--gc-sections
else
ifeq ($(findstring clang,$(CC)),clang)
CFLAGS += -O0 -g

View File

@ -41,7 +41,7 @@ SECTIONS {
.text ALIGN (4K) :
{
*(.multiboot)
KEEP(*(.multiboot))
*(.text*)
}

View File

@ -65,8 +65,8 @@ build() {
export COMPILER_AS_FOR_TARGET=as
export COMPILER_LD_FOR_TARGET=ld
export COMPILER_NM_FOR_TARGET=nm
export CFLAGS_FOR_TARGET="-Os -m32 -march=i586 -mtune=i586 -fno-stack-protector -DPREFER_SIZE_OVER_SPEED"
export CXXFLAGS_FOR_TARGET="-Os -m32 -march=i586 -mtune=i586 -fno-stack-protector -DPREFER_SIZE_OVER_SPEED"
export CFLAGS_FOR_TARGET="-Os -m32 -march=i586 -mtune=i586 -fno-stack-protector -DPREFER_SIZE_OVER_SPEED -ffunction-sections -fdata-sections"
export CXXFLAGS_FOR_TARGET="-Os -m32 -march=i586 -mtune=i586 -fno-stack-protector -DPREFER_SIZE_OVER_SPEED -ffunction-sections -fdata-sections"
mkdir -p install
./configure --target=${TARGET} \