From 6b433aede18a9275241653b721ea16ffae117fe2 Mon Sep 17 00:00:00 2001 From: Andre Guedes Date: Tue, 29 Sep 2015 17:06:21 -0300 Subject: [PATCH] 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. --- cpu/x86/Makefile.x86_common | 6 ++++-- cpu/x86/quarkX1000.ld | 2 +- platform/galileo/bsp/libc/build_newlib.sh | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cpu/x86/Makefile.x86_common b/cpu/x86/Makefile.x86_common index 0656dd0d6..d920327cf 100644 --- a/cpu/x86/Makefile.x86_common +++ b/cpu/x86/Makefile.x86_common @@ -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 diff --git a/cpu/x86/quarkX1000.ld b/cpu/x86/quarkX1000.ld index f9ff105c2..8beb425cf 100644 --- a/cpu/x86/quarkX1000.ld +++ b/cpu/x86/quarkX1000.ld @@ -41,7 +41,7 @@ SECTIONS { .text ALIGN (4K) : { - *(.multiboot) + KEEP(*(.multiboot)) *(.text*) } diff --git a/platform/galileo/bsp/libc/build_newlib.sh b/platform/galileo/bsp/libc/build_newlib.sh index a48cb2e4c..cee34f3ff 100755 --- a/platform/galileo/bsp/libc/build_newlib.sh +++ b/platform/galileo/bsp/libc/build_newlib.sh @@ -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} \