Bug 65756 - undefined reference to __atomic_store for odd-sized std::atomic<T>
Summary: undefined reference to __atomic_store for odd-sized std::atomic<T>
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-04-13 23:31 UTC by Martin Sebor
Modified: 2015-04-14 07:52 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2015-04-13 23:31:04 UTC
The following C++ 11 program fails to link with GCC (both 4.9 and 5.0) because the primary std::atomic template allocates 3 bytes for both a and aa and calls __atomic_store(&aa.a, &tmp, __ATOMIC_SEQ_CST).  GCC only supports calls to __atomic_store with power of 2 arguments of at most 8.  Assuming the code is well-defined (I couldn't find anything to suggest otherwise -- the type meets the trivially-copyable requirement) it seems that specializations of the template on types of such sizes should add padding up to the nearest greater power of 2 and increase their own alignment so as to make it possible to manipulate them atomically.  Larger specializations will need to make use of a mutex.

$ cat a.cpp && g++ -Wall -Wpedantic -std=c++11 a.cpp
#include <atomic>

int main (void) {
    struct A { char a [3]; } a = { { 'a', 'b', 'c' } };
    std::atomic<A> aa;
    aa.store (a);
}
/tmp/ccGQyZte.o: In function `std::atomic<main::A>::store(main::A, std::memory_order)':
a.cpp:(.text+0xf0): undefined reference to `__atomic_store'
collect2: error: ld returned 1 exit status
Comment 1 Jonathan Wakely 2015-04-13 23:32:34 UTC
You need to link to libatomic for objects with sizes that aren't supported natively by the CPU.
Comment 2 Martin Sebor 2015-04-14 00:20:18 UTC
I see.  Thanks.  With that fixed, the program links successfully.  It would be nice if GCC gave a better message for this sort of a user error.  I recall a compiler (HP aCC maybe, or MSVC) that suggested a library to link with when the linker failed with an unsatisfied reference.
Comment 3 Martin Sebor 2015-04-14 03:41:46 UTC
Interesting.  For some programs and libraries, GCC too gives a diagnostic that's more helpful than just: undefined reference to `foo'

/usr/bin/ld: /tmp/cccCqEak.o: undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'
/lib64/libpthread.so.0: error adding symbols: DSO missing from command line
Comment 4 Jakub Jelinek 2015-04-14 07:52:02 UTC
That is only for libraries visible to the linker indirectly (dependencies of other libraries).