Dear all,
I would like to propose a modification of the code generation of the GNU C/C++
compiler when ELF object files are generated.
Consider the following two C++ modules:
// ========= module A.CPP ============
class XXX
{
public:
XXX () { i = 1; }
virtual ~XXX () { i = -1; }
virtual int e (int);
virtual int f (int);
private:
int i;
};
int XXX::e (int) { return 1000; }
int XXX::f (int) { return 500; }
XXX* newXXX1 ()
{
return new XXX;
}
// ========= module B.CPP ============
class XXX
{
public:
XXX () { i = -1; }
virtual ~XXX () {};
virtual int e (int*);
private:
int i;
};
int XXX::e (int*) { return 100; }
XXX* newXXX2 ()
{
return new XXX;
}
If both modules are linked together we have a clear violation of the C++ one
definition rule since class XXX is defined twice. If we activate the compiler
option -fmerge-templates (which is possible if one uses ELF object files) we
get unfortunately NO linker warning ("multiple definitions of ...") because
- the constructors XXX::XXX() are generated inline
- the destructors XXX::~XXX() are located in the two sections
.gnu.linkonce.t._ZN3XXXD1Ev and .gnu.linkonce.t._ZN3XXXD0Ev
- the VTBLs are located in the sections .gnu.linkonce.r._ZTV3XXX.
The linker retains only one instance of the equally named sections and so we
don't get a linker warning and the ODR violation goes undetected.
I would now propose to locate the VTBLs in the .rodata section even if the
option -fmerge-templates is activated. (VTBLs are located by default in the
.rodata section.)
I think this is feasible since we would get a linker warning ("multiple
definition of `vtable for XXX'") and VTBLs are not subject to code bloat.
By the way for my tests I used the GNU C/C++ compiler 3.3-e500 which was
invoked as follows:
ccppc -c -x c++ -ansi -Wall -Werror -mcpu=8540 -fverbose-asm -mbig
-fmerge-templates -mmultiple -mno-string -mstrict-align -O3
-fno-exceptions -fno-rtti -fno-builtin-printf
-I<different include paths>
-D<differen #define's>
A.CPP -oA.O
With kind regards
W. Roehrl
--
Summary: ODR and option -fmerge-templates
Product: gcc
Version: 3.3
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: wolfgang dot roehrl at de dot gi-de dot com
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: sparc-sun-solaris2.5.1
GCC host triplet: i386-pc-mingw32
GCC target triplet: powerpc-wrs-vxworks
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18717