防止GCC LTO删除功能 [英] Prevent GCC LTO from deleting function

查看:442
本文介绍了防止GCC LTO删除功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GCC-ARM-Embedded和FreeRTOS. FreeRTOS具有功能vTaskSwitchContext(),仅在某些情况下使用 内联汇编代码.

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

问题是:当我使用LTO时,GCC不会考虑内联汇编代码,并认为未使用该函数,因此将其删除.然后,链接器将失败,因为无法解析内联汇编代码中的函数调用.

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.

我会应用__attribute__((used)),但是我不想触摸FreeRTOS代码(它是由STM32CubeMX生成的).

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

我尝试将其放入代码中,但实际上GCC足够聪明,无法执行此操作:

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

if(false)
    vTaskSwitchContext();

是否有某种方法可以在不同的源文件中或通过参数告诉GCC不应删除此功能?

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

示例

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

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

推荐答案

尝试从标记为used的单独函数中调用该函数.

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

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

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

这篇关于防止GCC LTO删除功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆