[LLVMbugs] [Bug 22281] New: clang creates "weak symbol" whereas gcc produces "unique global sybol" symbol type for same source

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Jan 21 08:19:31 PST 2015


http://llvm.org/bugs/show_bug.cgi?id=22281

            Bug ID: 22281
           Summary: clang creates "weak symbol" whereas gcc produces
                    "unique global sybol" symbol type for same source
           Product: clang
           Version: 3.5
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: aln at esi-group.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

The following code produces different symbol types in the object file when
compiled with clang and gcc. I do not have any error or crashes.

//singleton.cpp
class Singleton
{
public:
    static Singleton* getInstance()
    {
        static Singleton* mInstance=0;
        if (!mInstance)
        {
            mInstance=new Singleton();
        }
        return mInstance;
    }
};

void foo()
{
    Singleton* myBar = Singleton::getInstance();
}
//


Compile command:
% clang -o clang.o -c singleton.cpp && gcc -o gcc.o -c singleton.cpp


nm then shows the following for both object files:
% nm clang.o && echo && nm gcc.o
0000000000000000 T _Z3foov
0000000000000000 W _ZN9Singleton11getInstanceEv
                 U _Znwm
0000000000000000 V _ZZN9Singleton11getInstanceEvE9mInstance

0000000000000000 T _Z3foov
0000000000000000 W _ZN9Singleton11getInstanceEv
                 U _Znwm
0000000000000000 u _ZZN9Singleton11getInstanceEvE9mInstance


As can be seen _ZZN9Singleton11getInstanceEvE9mInstance is created as "V" =
weak object by clang and "u" = unique global symbol by gcc.

I think this difference is the underlying problem I experience in a larger code
base that uses the singleton pattern. The created executables and dynamic
libraries do not work correctly because the singletons are not unique in the
process. I observed multiple constructor calls while debugging. The singletons
are created in a library and are used across library boarders.

Observed on Fedora 21 with clang 3.5.0-6.fc21 and gcc 4.9.2-1.fc21.
I also observed this behaviour on Fedora 20 but do not recall the actual
compiler versions.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150121/d2d34305/attachment.html>


More information about the llvm-bugs mailing list