Bug 37651 - __sync_bool_compare_and_swap creates wrong code with -fPIC
Summary: __sync_bool_compare_and_swap creates wrong code with -fPIC
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.3.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-09-25 19:45 UTC by Frank Mehnert
Modified: 2012-01-06 10:17 UTC (History)
5 users (show)

See Also:
Host: i486-linux-gnu
Target: i486-linux-gnu
Build: i486-linux-gnu
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 Frank Mehnert 2008-09-25 19:45:06 UTC
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)
Comment 1 Andrew Pinski 2008-09-25 21:36:06 UTC
I get:
        xchgl   %ebx, %edi
        lock ; cmpxchg8b        (%esi)
        xchgl   %ebx, %edi

Which looks good.
Comment 2 brian 2008-09-25 22:29:05 UTC
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)
Comment 3 Andrew Haley 2012-01-06 10:17:52 UTC
Reproducible with with 4.2.4 but not with 4.4.6 or 4.6.2 or HEAD.