libnds icon indicating copy to clipboard operation
libnds copied to clipboard

Undefined reference when using `std::atomic<unsigned>`

Open Chi-Iroh opened this issue 1 year ago • 2 comments

Hi, first of all thank you a lot for your amazing work !

Bug Report

I get linker errors when using std::atomic<unsigned> in my code :

/opt/devkitpro/devkitARM/bin/../lib/gcc/arm-none-eabi/14.1.0/../../../../arm-none-eabi/bin/ld: main.o: in function `std::__atomic_base<unsigned int>::fetch_add(unsigned int, std::memory_order)':
/opt/devkitpro/devkitARM/arm-none-eabi/include/c++/14.1.0/bits/atomic_base.h:631:(.text+0x74): undefined reference to `__atomic_fetch_add_4'
/opt/devkitpro/devkitARM/bin/../lib/gcc/arm-none-eabi/14.1.0/../../../../arm-none-eabi/bin/ld: /opt/devkitpro/devkitARM/arm-none-eabi/include/c++/14.1.0/bits/atomic_base.h:631:(.text+0xc8): undefined reference to `__atomic_fetch_add_4'

I'm compiling with -std=gnu++23 flag, but I also got the error with -std=gnu++20 and -std=gnu++17. I use Arch Linux and I installed devkitpro using pacman. I haven't modified devkitpro. My environment variables :

DEVKITARM=/opt/devkitpro/devkitARM
DEVKITPPC=/opt/devkitpro/devkitPPC
DEVKITPRO=/opt/devkitpro
PATH=/opt/devkitpro/tools/bin:/opt/devkitpro/portlibs/nds/bin:/opt/devkitpro/devkitARM/arm-none-eabi/bin:/opt/devkitpro/devkitARM/bin:/opt/devkitpro/tools/bin

Output of arm-none-eabi-g++ -v :

Using built-in specs.
COLLECT_GCC=arm-none-eabi-g++
COLLECT_LTO_WRAPPER=/opt/devkitpro/devkitARM/bin/../libexec/gcc/arm-none-eabi/14.1.0/lto-wrapper
Target: arm-none-eabi
Configured with: ../../gcc-14.1.0/configure --enable-languages=c,c++,objc,lto --with-gnu-as --with-gnu-ld --with-gcc --with-march=armv4t --enable-cxx-flags=-ffunction-sections --disable-libstdcxx-verbose --enable-poison-system-directories --enable-interwork --enable-multilib --enable-threads --disable-win32-registry --disable-nls --disable-debug --disable-libmudflap --disable-libssp --disable-libgomp --disable-libstdcxx-pch --enable-libstdcxx-time=yes --enable-libstdcxx-filesystem-ts --target=arm-none-eabi --with-newlib --with-headers=../../newlib-4.4.0.20231231/newlib/libc/include --prefix=/home/davem/projects/devkitpro/tool-packages/devkitARM/src/build/x86_64-linux-gnu/devkitARM --enable-lto --disable-tm-clone-registry --disable-__cxa_atexit --with-bugurl=http://wiki.devkitpro.org/index.php/Bug_Reports --with-pkgversion='devkitARM release 64' --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --with-gmp= --with-mpfr= --with-mpc= --with-isl= --with-zstd= --with-gmp=/opt/devkitpro/crosstools/x86_64-linux-gnu --with-mpfr=/opt/devkitpro/crosstools/x86_64-linux-gnu --with-mpc=/opt/devkitpro/crosstools/x86_64-linux-gnu --with-isl=/opt/devkitpro/crosstools/x86_64-linux-gnu --with-zstd=/opt/devkitpro/crosstools/x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.1.0 (devkitARM release 64)

I only use libnds in my code, no other dependency.

How to fix it

After googling the undefined reference message, I found this github issue and adding this function solves the problem for me :

extern "C" unsigned __atomic_fetch_add_4(volatile void *ptr, unsigned val, int memmodel) {
	(void)memmodel;
	unsigned tmp = *(unsigned*)ptr;
	*(unsigned*)ptr = tmp + val;
	return tmp;
}

Chi-Iroh avatar Jun 23 '24 10:06 Chi-Iroh

__atomic_fetch_sub_4 and __atomic_compare_exchange_1 are also missing.

R-YaTian avatar Jul 25 '25 08:07 R-YaTian

The armv5 instruction set lacks atomic instructions. Usually they need to be supplied or emulated by the operating system or compiler support libraries. GCC inserts calls to libatomic, which we presently do not ship at all. However, we believe it may be possible to provide our own (more efficient) implementations. The example implementation in the OP is incorrect, as it is not thread/irq/preemption safe.

fincs avatar Aug 01 '25 17:08 fincs