Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent GCC LTO from deleting function

Tags:

c

gcc

ld

lto

freertos

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"
    ...
    );
}
like image 613
Jan Hieber Avatar asked Jul 15 '16 06:07

Jan Hieber


Video Answer


3 Answers

For some reason, the solution that Dietrich proposed didn't work for me. I'm using Infineon's DAVE 4 (basically eclipse with a fancy code generation plugin for their line of XMC microcontrollers), which may be the reason why it didn't work. For me, I had to call vTaskSwitchContext() after vTaskStartScheduler():

int main(){

    initializationCode();

    vTaskStartScheduler();

    //Code never reaches here
    vTaskSwitchContext();
}
like image 152
Chi Avatar answered Oct 02 '22 22:10

Chi


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

void dummyFunction(void) __attribute__((used));

// Never called.
void dummyFunction(void) {
    vTaskSwitchContext();
}
like image 36
Dietrich Epp Avatar answered Oct 02 '22 23:10

Dietrich Epp


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

like image 45
user1273684 Avatar answered Oct 02 '22 22:10

user1273684