How to remove unused FreeRTOS Functions from Executable


I am using the STM32F103 with Freertos and I cannot remove some unused function from my executable, it costs some extra Kbytes of Flash.

I am using the compiler options -fdata-sections -ffunction-sections -Wl,–gc-sections -Os
to remove all the unused symbols but some Freertos functions like xQueueGenericSend are still there in my executable. That happens only for the FreeRTOS functions.

I am not using Queues in my software and I also changed the FreertosConfig.h file to deactivate some functionality but the unused FreeRTOS functions are still there, in my program.

Could you explain me how can I remove this unused functions?

Your sincerely,

The compiler options will place functions in separate sections. Are you then also using the linker options to remove unused sections? Note if you have configUSE_TIMERS set or if you are using semaphores then queues will be in use by the kernel.

ok, thank you for your reply. I am using the linker option -Wl,-gc-sections to remove the unused code. As you write, I think that the reason is that the functions are used in Semaphore.

Your sincerely,

As for the linker, I added the option -Wl,--gc-sections, note that is has a double-dash before --gc. It is passed to the linker. This is the complete command:

arm-none-eabi-gcc -T my_project.ld -Xlinker
-Map="" -mcpu=cortex-m4
-Wl,--gc-sections -mthumb ...object files...
-o my_project.elf

Another trick: can you temporarily disable compilation of the function xQueueGenericSend(), with an #if 0? The linker may then tell you which module is calling mentioned function.

I made the trick with the #if 0, The xTimerGenericCommand is using the xQueueGenericSend().

Yes, FreeRTOS uses Queue functionality for a LOT of different purposes (Queue, Semaphores, and Mutexes, and Timers use a Queue) so you normally have the basic queue code loaded, but that code also supports a lot of functionality so you don’t need other code to support it.