gcc - Stripping unused library functions / dead code from a static executable -


i'm compiling code arm cortex-m0 mcu gcc arm-none-eabi-g++ (4.8.3). fine, noticed when include , use function cstdlib, functions file included well. how rid of them?

i'm calling malloc() , free() only, resulting elf has system() , isatty() machine code well.

the mcu has 32kb flash, ~0.7kb ballast matters, if keeps happening other headers.

right use -ffunction-sections -fdata-sections compiling , -wl,--gc-sections -wl,--static while linking, follows:

arm-none-eabi-g++ -c --std=c++11 -os -i. -ilpc1xxx -idrivers -wall -mthumb \   -ffunction-sections -fdata-sections -fmessage-length=0 -mcpu=cortex-m0 \   -dtarget=lpc11xx -fno-builtin -flto -fno-exceptions -o main.o main.cpp arm-none-eabi-gcc -c --std=c11   -os -i. -ilpc1xxx -idrivers -wall -mthumb \   -ffunction-sections -fdata-sections -fmessage-length=0 -mcpu=cortex-m0 \   -dtarget=lpc11xx -fno-builtin -flto  -o core_cm0.o lpc1xxx/nxp/core_cm0.c arm-none-eabi-gcc -nostartfiles -mcpu=cortex-m0 -mthumb -wl,--gc-sections -flto \   -os -wl,--static -t lpc1xxx/memory.ld -o firmware.elf main.o core_cm0.o  \   libaeabi-cortexm0/libaeabi-cortexm0.a lpc11xx_handlers.o lpc1xxx_startup.o 

edit: warning: -flto flag in example wrong – somehow discards interrupt routines.

the result when arm-none-eabi-objdump -t firmware.elf, among others:

00000fbc g     f .text  0000002c _isatty 00001798 g     f .text  00000018 fclose 00000e4c g     f .text  00000030 _kill 00000e7c g     f .text  00000018 _exit 00000fe8 g     f .text  00000050 _system 

these functions redundant (and quite useless on mcu @ all), yet gcc keeps them in executable. there no calls them, these symbols not referenced anywhere. it's dead code.

how rid of them? compiler/linker flags?


edit:

minimal code reproduce problem:

#include <cstdlib> int main(){     [[gnu::unused]] volatile void * x = malloc(1);     return 0; } 

command used compile that:

arm-none-eabi-g++ --std=c++11 -os -wall -mthumb -ffunction-sections    -fdata-sections -fmessage-length=0 -mcpu=cortex-m0 -fno-builtin -flto   -fno-exceptions -wl,--static -wl,--gc-sections -o main.elf main.cpp 

and main.elf file still has stdlib bloat.


using -ffunction-sections right thing here, issue object file provides malloc , free built without (either lpc11xx_handlers.o, lpc1xxx_startup.o or of object files within libaeabi-cortexm0.a). in case, linker can include whole object file (or -wl,--gc-sections, whole section) contain functions need.

the layout of functions in object files , sections thing matters, not function defined in same header function.

so fix issue, rebuild standard library files -ffunction-sections -fdata-sections.


Comments