cgccldfreertoslto

Prevent GCC LTO from deleting function


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

The problem is: When I use LTO, GCC does not consider the inline assembler code and thinks the function is not used, thus removes it. The linker then fails because the function call in the inline assembler code cannot be resolved.

I would apply __attribute__((used)) but I don't want to touch the FreeRTOS code (it's generated by STM32CubeMX).

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

if(false)
    vTaskSwitchContext();

Is there some way to tell GCC in a different source file, or via parameter, that this function should not be removed?

Example

// file1.c
void vTaskSwitchContext( void )
{
    ...
}

// file2.c
void xPortPendSVHandler( void )
{
    __asm volatile
    (
    ...
    "   isb                                 \n"
    "   bl vTaskSwitchContext               \n"
    "   mov r0, #0                          \n"
    ...
    );
}

Solution

  • Try calling the function from a separate function which is marked used.

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