Preventing LTO GCC Function Cancellation

I work with GCC-ARM-Embedded and FreeRTOS. FreeRTOS has a function vTaskSwitchContext() , which is used only in some built-in assembler code.

The problem is this: when I use LTO, GCC does not take into account the built-in assembler code and considers that the function is not used, thereby deleting it. The linker then fails because the function call in the inline assembler code cannot be resolved.

I would apply __attribute__((used)) , but I do not want to touch the FreeRTOS code (it is generated by STM32CubeMX).

I tried putting this in my code, but actually GCC is smart enough not to let this work:

 if(false) vTaskSwitchContext(); 

Is it possible to specify GCC in another source file or through a parameter so that this function is not deleted?

Example

 // file1.c void vTaskSwitchContext( void ) { ... } // file2.c void xPortPendSVHandler( void ) { __asm volatile ( ... " isb \n" " bl vTaskSwitchContext \n" " mov r0, #0 \n" ... ); } 
+7
source share
3 answers

Try calling the function from a separate function marked used .

 void dummyFunction(void) __attribute__((used)); // Never called. void dummyFunction(void) { vTaskSwitchContext(); } 
+8
source

You can add -Wl,--undefined=vTaskSwitchContext to your LDFLAGS .

+4
source

For some reason, the solution proposed by Dietrich did not work for me. I use Infineon DAVE 4 (in fact, an eclipse with an unusual plug-in for generating code for their XMC microcontroller line), which may be the reason that it did not work. For me, I had to call vTaskSwitchContext() after vTaskStartScheduler() :

 int main(){ initializationCode(); vTaskStartScheduler(); //Code never reaches here vTaskSwitchContext(); } 
0
source

All Articles