简体   繁体   English

在为 arm 进行交叉编译时,是否有特定的编译器标志可以使用 gcc 同步内置函数?

[英]Are there specific compiler flags to use gcc sync-builtins when cross-compiling for arm?

I'm trying to cross-compile a program that uses gcc builtins, specific __sync_val_compare_and_swap, __sync_add_and_fetch and sync_sub_and_fetch.我正在尝试交叉编译一个使用 gcc 内置函数、特定 __sync_val_compare_and_swap、__sync_add_and_fetch 和 sync_sub_and_fetch 的程序。 Compiling works, but the linker is showing me the undefined reference errors.编译工作,但 linker 向我显示未定义的参考错误。 For example:例如:

memory_layout.c:(.text.memory_uniqueid+0x1c): undefined reference to '__sync_val_compare_and_swap_4'
memory_layout.c:(.text.ipc_counter+0x18): undefined reference to '__sync_add_and_fetch_4'

I'm using the st-gnu-arm-gcc-7-2018-q2-update_gdb-5_4-2016q3 toolchain that comes with eclipse and SW4STM32.我正在使用 eclipse 和 SW4STM32 附带的 st-gnu-arm-gcc-7-2018-q2-update_gdb-5_4-2016q3 工具链。 My host machine is a 64-Bit Linux Mint.我的主机是 64 位 Linux Mint。 The program is build with CMake.该程序使用 CMake 构建。

The needed functions are defined in the toolchain file lib/gcc/arm-none-eabi/7.3.1/plugin/include/sync-builtins.def.所需的函数在工具链文件 lib/gcc/arm-none-eabi/7.3.1/plugin/include/sync-builtins.def 中定义。

I've searched for similar errors but the provided solution (use -march=i486) but this didn't help.我搜索了类似的错误,但提供了解决方案(使用 -march=i486),但这没有帮助。 Another workaround I found is to compile the needed functions in an own library ( http://vincesoft.blogspot.com/2012/04/how-to-solve-undefined-reference-to.html ) but this seems to be for older gcc versions.我发现的另一个解决方法是在自己的库( http://vincesoft.blogspot.com/2012/04/how-to-solve-undefined-reference-to.html )中编译所需的函数,但这似乎是针对旧的gcc 版本。

I also tried to manually link libgcc (that comes with the toolchain) but without success.我还尝试手动链接 libgcc(工具链附带)但没有成功。 The used commands are使用的命令是

add_library(GCC_LIB STATIC IMPORTED /home/toolchains/st-gnu-arm-gcc-7-2018-q2-update_gdb-5_4-2016q3/lib/gcc/arm-none-eabi/7.3.1/libgcc.a)
target_link_libraries(${PROJECT_NAME} ${GCC_LIB})

the error is caused in the following function错误是在以下function中引起的

static inline unsigned int atomic_inc(unsigned int v)
{
    /* atomic load, modify, store */
    return __sync_add_and_fetch(v, 1);
}

Do I need to provide some special compiler flags or defines so the builtin functions can be linked?我是否需要提供一些特殊的编译器标志或定义以便可以链接内置函数?

As @KamiCuk pointed out I had to implement the needed functions by myself.正如@KamiCuk 指出的那样,我必须自己实现所需的功能。 The disable_intterrups() and enable_interrupts() function in his comment are os dependent.他评论中的 disable_intterrups() 和 enable_interrupts() function 依赖于操作系统。 As I was compiling for Freertos I used portENTER_CRITICAL and portEXIT_CRITICAL.在为 Freertos 编译时,我使用了 portENTER_CRITICAL 和 portEXIT_CRITICAL。 The finale solution for __sync_add_and_fetch_4 looked like this: __sync_add_and_fetch_4 的最终解决方案如下所示:

__sync_add_and_fetch_4(unsigned *v, unsigned add, ...) { 
portENTER_CRITICAL(); 
const unsigned ret = *v;
*v += add; 
portEXIT_CRITICAL(); 
return ret; }

Some of the atomic operations are already provided by FreeRTOS headers (atomic.h). FreeRTOS 标头 (atomic.h) 已经提供了一些原子操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM