The following example dumps a core if compiled with -march=i586 -fPIC: #include <stdint.h> int main(void) { static uint64_t volatile s_u64; __sync_bool_compare_and_swap(&s_u64, 0, 0); } The reason is that %ebx will be used as pointer for the memory variable. I can reproduce this bug so far with gcc-4.2 and gcc-4.3.2 (both Debian Sid). $ gcc -v Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-cld --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu Thread model: posix gcc version 4.3.2 (Debian 4.3.2-1)
I get: xchgl %ebx, %edi lock ; cmpxchg8b (%esi) xchgl %ebx, %edi Which looks good.
Subject: Re: __sync_bool_compare_and_swap creates wrong code with -fPIC You get that if the variable is auto, but if it's static the reference is with a GOTOFF reloc: xorl %edi, %edi pushl %ebx call .L6 .L6: popl %ebx addl $_GLOBAL_OFFSET_TABLE_+[.-.L6], %ebx pushl %ecx movl %edi, %ecx xchgl %ebx, %edi lock cmpxchg8b s_u64.1229@GOTOFF(%ebx)
Reproducible with with 4.2.4 but not with 4.4.6 or 4.6.2 or HEAD.