Skip to content

clang creates "weak symbol" whereas gcc produces "unique global sybol" symbol type for same source #22655

@llvmbot

Description

@llvmbot
Bugzilla Link 22281
Version 3.5
OS Linux
Reporter LLVM Bugzilla Contributor
CC @majnemer

Extended Description

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillaclang:codegenIR generation bugs: mangling, exceptions, etc.clang:frontendLanguage frontend issues, e.g. anything involving "Sema"wontfixIssue is real, but we can't or won't fix it. Not invalid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions