Size of *.elf file


In continuation to our discussion on the thread “Unable to Load Firmware to Cortex M4 core on iMX 7 using Rpmsg library on WinCE smart device application”, I am opening this question for the build output (*.elf) with cmake and KDS are different.
[Building freeRTOS gpio_imx example project for cortex M4 core of IMX7D]

As suggested by you i have build the code in release mode too but still the results are same. File size is too large.
I am attaching the build log file, here you can see the build options used.

Bipin Kumar

Dear @bipin7301

I verified the .elf file which you provided in your previous question:

In fact, this fail contains mainly debug information. By stripping away the debug information, the file size was reduced from 2’616kB to 115kB.
Use the following command to do this:

arm-none-eabi-strip -d -o "small.elf" "KDSTestNewRTOSLib.elf"

where KDSTestNewRTOSLib.elf is the name of your large .elf file, and small.elf the name of the optimized .elf file without debug symbols.

You can add the arm-none-eabi-strip command in the project’s CMakeList.txt file, in the section ADD_CUSTOM_COMMAND.

Regards, Andy

Build output from cmake is already small in size. I don’t require to add any option in CMaleList.txt file to reduce the file size, it does it automatically with its existing commands.

The problem is with build output from KDS, I have shared you the build log of KDS. Build output of KDS is large in size, the file you have cut down using commands was built from KDS.


Dear @bipin7301

I don’t know the KDS build system good enough to tell you where you can configure the settings. But I can explain you what needs to be changed.

Here is an extract of the relevant parts of the build.log you sent me with your question:

Building target: KDSTestNewRTOSLib- gpio_imx.elf
Invoking: Cross ARM C Linker
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Og -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -ffreestanding -fno-builtin  -g3 -T "MCIMX7D_M4_tcm.ld" -Xlinker --gc-sections -L"E:\Projects\KDS_Workspace\freertos-colibri-imx7_new\platform\devices\MCIMX7D\linker\gcc" -Xlinker -static -Xlinker -z -z max-page-size=4096 -Xlinker muldefs -Wl,-Map,"KDSTestNewRTOSLib-" --specs=nano.specs --specs=nosys.specs -o "KDSTestNewRTOSLib- gpio_imx.elf"  ./Sources/platform/utilities/src/debug_console_imx.o ./Sources/platform/utilities/src/print_scan.o  ./Sources/platform/drivers/src/ccm_analog_imx7d.o ./Sources/platform/drivers/src/ccm_imx7d.o ./Sources/platform/drivers/src/gpio_imx.o ./Sources/platform/drivers/src/lmem.o ./Sources/platform/drivers/src/rdc.o ./Sources/platform/drivers/src/uart_imx.o ./Sources/platform/drivers/src/wdog_imx.o  ./Sources/platform/devices/MCIMX7D/startup/gcc/startup_MCIMX7D_M4.o  ./Sources/platform/devices/MCIMX7D/startup/system_MCIMX7D_M4.o  ./Sources/imx7_colibri_m4/board.o ./Sources/imx7_colibri_m4/clock_freq.o ./Sources/imx7_colibri_m4/gpio_pins.o ./Sources/imx7_colibri_m4/pin_mux.o  ./Sources/gpio_imx/hardware_init.o ./Sources/gpio_imx/main.o

This is the command which links all the compiled object files together. If you can manage to add a parameter -s to this command, you will directly get an .elf file without debug symbols.

The message in the log file just following the previous one shows a post-build step:

arm-none-eabi-objcopy -O ihex "KDSTestNewRTOSLib- gpio_imx.elf"  "KDSTestNewRTOSLib- gpio_imx.hex"

If you can replace this with the arm-none-eabi-strip ... command (or add the command), this will minimize your .elf size, too.

Regards, Andy

I understand your point Andy and got to know how to downsize the .elf file using strip option but one question is still in my mind. I have gone through the FreeRTOS example projects for M4 and in CMakeLists.text file for any project no such option(arm-none-eabi-strip…) is used, but still build output file(.elf) size with cmake is very small. How does it happen with building project using cmake ?

Bipin Kumar

Dear @bipin7301

I can only explain how it works when you build the example through the batch files (build_all.bat, build_release.bat, build_debug.bat, which are part of the provided examples:

  1. The batch files refer to the file armgcc.cmake.
    In this file, there is already a part of the compiler/linker flags, such as


  2. Then Cmake uses the file CMakeLists.txt for further settings.
    The flags -g (also with additional suffixes) control the generation of the debug output. -g3 generates the largest debug information block.

The build.log you provided from your KDS environment looks somewhat different from the log output generated in my build.
In the log extract shown above, there is also a -g3 parameter, which is responsible for the >2MB .elf size. If you can’t find the appropriate configuration file which is responsible for this parameter, I recommend you ask Keil how to change this setting in the KDS environment.

Regards, Andy

Thanks Andy for your support in understanding this topic.