This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/43380] New: Global constructors are called twice for code compiled with -fPIC and placed into executable
- From: "danielfriederich at gmx dot net" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 15 Mar 2010 18:15:21 -0000
- Subject: [Bug c++/43380] New: Global constructors are called twice for code compiled with -fPIC and placed into executable
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
I'm aware that code compiled with -fPIC is inefficient, but I did not read
anywhere that it is illegal to link -fPIC code into an executable.
We are compiling code with -fPIC and place the same code into the real
"product" so's as well as into unit test executables. With this setup
one problem I noticed is that if the test loads the shared object also,
the symbols of the test and the symbols of the so's get unified. This would not
be an actual problem, but unfortunately the C++ global objects get constructed
and destructed twice, causing memory corruption and ultimately a crash during
shutdown.
So the basic setup is:
global.cpp:
struct PrintThis {
PrintThis() { printf("PrintThis::PrintThis: %p\n", (void*)this);
} global;
This global.cpp gets compiled once with -fPIC and the resulting object file
gets linked twice, once into a shared object and once into the executable.
With this setup I get the following output:
PrintThis::PrintThis: 0x804a050
PrintThis::PrintThis: 0x804a050
So the constructor gets called twice for the same address.
When I compile the global.cpp without -fPIC for the executable, it prints
different addresses.
So my question is if this is a bug in gcc or in its runtime support?
I know that using -fPIC is inefficient, but I would think it should not
crash at runtime (with no warning ever, and memory corruptions are also
especially hard to diagnose).
--
Summary: Global constructors are called twice for code compiled
with -fPIC and placed into executable
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: danielfriederich at gmx dot net
GCC build triplet: gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9)
GCC host triplet: ubuntu
GCC target triplet: x86
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43380