Discussion:
Problem with C11 _Atomic
(too old to reply)
Pierre DAVID
2018-01-01 20:47:40 UTC
Permalink
Hi,

I'm on a recent current:
FreeBSD biceps.ma.maison 12.0-CURRENT FreeBSD 12.0-CURRENT #2 r327239: Wed Dec 27 18:25:46 CET 2017 ***@biceps.ma.maison:/usr/obj/usr/src/amd64.amd64/sys/BICEPS amd64

with clang 5.0.1:
FreeBSD clang version 5.0.1 (tags/RELEASE_501/final 320880) (based on LLVM 5.0.1)
Target: x86_64-unknown-freebsd12.0
Thread model: posix
InstalledDir: /usr/bin

I'm having a problem with the following source file:

------------------------------------------------------------------------------
#include <stdatomic.h>

struct foo
{
int f1 ;
char f2 ;
int f3 ;
} ;

_Atomic struct foo a ;
struct foo b ;

int main (int argc, char *argv [])
{
b = (struct foo) {.f1 = 5, .f2 = 7, .f3 = 9 } ;
// atomic_store (&a, b) ;
a = b ;
}
------------------------------------------------------------------------------

This code does not compile/link with:
% cc foo.c -lstdthreads
/tmp/foo-a0ef26.o: In function `main':
foo.c:(.text+0x63): undefined reference to `__sync_lock_test_and_set_16'
cc: error: linker command failed with exit code 1 (use -v to see invocation)

The gcc internal seems to be linked as "cc -v" told me:
% cc -v foo.c -lstdthreads
...
"/usr/bin/ld" --eh-frame-hdr -dynamic-linker /libexec/ld-elf.so.1 --hash-style=both --enable-new-dtags -o a.out /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtbegin.o -L/usr/lib /tmp/foo-d7a21b.o -lstdthreads -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/crtend.o /usr/lib/crtn.o

The problem occurs with "atomic_store(&a, b)" as well as with "a = b".

If I remove the f3 member, the struct foo is only 8 bytes and the code
compiles/links.

Did I missed something?

Thanks in advance,

Pierre
Konstantin Belousov
2018-01-01 21:09:07 UTC
Permalink
Post by Pierre DAVID
Hi,
FreeBSD clang version 5.0.1 (tags/RELEASE_501/final 320880) (based on LLVM 5.0.1)
Target: x86_64-unknown-freebsd12.0
Thread model: posix
InstalledDir: /usr/bin
------------------------------------------------------------------------------
#include <stdatomic.h>
struct foo
{
int f1 ;
char f2 ;
int f3 ;
} ;
_Atomic struct foo a ;
struct foo b ;
int main (int argc, char *argv [])
{
b = (struct foo) {.f1 = 5, .f2 = 7, .f3 = 9 } ;
// atomic_store (&a, b) ;
a = b ;
}
------------------------------------------------------------------------------
% cc foo.c -lstdthreads
foo.c:(.text+0x63): undefined reference to `__sync_lock_test_and_set_16'
cc: error: linker command failed with exit code 1 (use -v to see invocation)
% cc -v foo.c -lstdthreads
...
"/usr/bin/ld" --eh-frame-hdr -dynamic-linker /libexec/ld-elf.so.1 --hash-style=both --enable-new-dtags -o a.out /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtbegin.o -L/usr/lib /tmp/foo-d7a21b.o -lstdthreads -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/crtend.o /usr/lib/crtn.o
The problem occurs with "atomic_store(&a, b)" as well as with "a = b".
If I remove the f3 member, the struct foo is only 8 bytes and the code
compiles/links.
Did I missed something?
clang issues a calls to libatomic, which we do not provide.
As a workaround, use the following command to compile. The resulting
binary works on all practically usable machines.
$ cc -march=core2 source.c
You might want to turn off sse3/4.1 if you are concerned about older pentium4.
Pierre DAVID
2018-01-02 14:17:14 UTC
Permalink
Post by Konstantin Belousov
Post by Pierre DAVID
Hi,
FreeBSD clang version 5.0.1 (tags/RELEASE_501/final 320880) (based on LLVM 5.0.1)
Target: x86_64-unknown-freebsd12.0
Thread model: posix
InstalledDir: /usr/bin
------------------------------------------------------------------------------
#include <stdatomic.h>
struct foo
{
int f1 ;
char f2 ;
int f3 ;
} ;
_Atomic struct foo a ;
struct foo b ;
int main (int argc, char *argv [])
{
b = (struct foo) {.f1 = 5, .f2 = 7, .f3 = 9 } ;
// atomic_store (&a, b) ;
a = b ;
}
------------------------------------------------------------------------------
% cc foo.c -lstdthreads
foo.c:(.text+0x63): undefined reference to `__sync_lock_test_and_set_16'
cc: error: linker command failed with exit code 1 (use -v to see invocation)
% cc -v foo.c -lstdthreads
...
"/usr/bin/ld" --eh-frame-hdr -dynamic-linker /libexec/ld-elf.so.1 --hash-style=both --enable-new-dtags -o a.out /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtbegin.o -L/usr/lib /tmp/foo-d7a21b.o -lstdthreads -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/crtend.o /usr/lib/crtn.o
The problem occurs with "atomic_store(&a, b)" as well as with "a = b".
If I remove the f3 member, the struct foo is only 8 bytes and the code
compiles/links.
Did I missed something?
clang issues a calls to libatomic, which we do not provide.
As a workaround, use the following command to compile. The resulting
binary works on all practically usable machines.
$ cc -march=core2 source.c
You might want to turn off sse3/4.1 if you are concerned about older pentium4.
Thanks for your help. I wish that the C11 status of FreeBSD will soon
be complete out of the box, without the help of such a hack.

Pierre
Konstantin Belousov
2018-01-02 14:34:29 UTC
Permalink
Post by Pierre DAVID
Post by Konstantin Belousov
clang issues a calls to libatomic, which we do not provide.
As a workaround, use the following command to compile. The resulting
binary works on all practically usable machines.
$ cc -march=core2 source.c
You might want to turn off sse3/4.1 if you are concerned about older pentium4.
Thanks for your help. I wish that the C11 status of FreeBSD will soon
be complete out of the box, without the help of such a hack.
This is not FreeBSD but clang. Also I looked at the generated reference,
and the referenced symbol was absent in the gcc' 7.2.0 libatomic.

Same common problem with i386 and same cmpxchg8b is popular because the
default arch is i486.

This is a clang way of operations.

Continue reading on narkive:
Loading...