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. Compiling works, but the linker is showing me the undefined reference errors. 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. My host machine is a 64-Bit Linux Mint. The program is build with CMake.
The needed functions are defined in the toolchain file 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. 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.
I also tried to manually link libgcc (that comes with the toolchain) but without success. 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
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?