public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments
@ 2004-04-02 20:53 gcaglar at hotmail dot com
  2004-04-02 20:56 ` [Bug c++/14826] " gcaglar at hotmail dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: gcaglar at hotmail dot com @ 2004-04-02 20:53 UTC (permalink / raw)
  To: gcc-bugs

Hi, this bug only happens when you compile the multiple objects and then link
them. I tried to send a tarball that has all the .h, .C, .ii, .s files and the
output file which is generated by the compiler to the gcc-bugs@gcc.gnu.org but
I've been blocked. So I'll paste here the required source code and the compiler
output.

g++ -v produces:
Reading specs from
/.../watson.ibm.com/fs/system/local/applications/gnu/aix/5.1/power/bin/../lib/gcc-lib/powerpc-ibm-aix5.1.0.0/3.3.3/specs
Configured with: /usr/gnu/src/gcc-3.3/configure --prefix=/usr/gnu
Thread model: aix
gcc version 3.3.3 20040215 (release)

The code is very simple, and runs ok with gcc-3.2.3. Basically, when you have a
static template variable which is a pointer, in different instantiations of the
template but with the same parameters. The static pointers are not the same. For
example, if you have a template Factory which has "static Factory* _factory"
and a method that returns this pointer, in file Bar.C Factory<int>::_factory is
different than the Factory<int>::_factory in Foo.C. The code is more clear:
Source files Foo.C, Bar.C, and main.C have to be compiled individually, the
problem does not appear if all the code is in one file.

Factory.h 
----
#ifndef FACTORYMAP_H
#define FACTORYMAP_H

template <class T>
class Factory
{
   public:
      Factory();
      inline static Factory<T>* getFactory();
      ~Factory();

      static Factory<T>* _factory;
      int _data;
};

template <class T>
Factory<T>::Factory()
{
}

template <class T>
Factory<T>* Factory<T>::getFactory()
{
   std::cout << "_factory " << &_factory << std::endl;
   if (_factory == 0) {
      _factory = new Factory<T>();
   }
   return _factory;
}

template <class T>
Factory<T>* Factory<T>::_factory = 0;

#endif
---

Foo.h
---
#ifndef FOO_H
#define FOO_H

class Foo
{
   public:
      Foo();
};

#endif
---

Foo.C
---
#include "Factory.h"
#include "Foo.h"
#include <iostream>

Foo::Foo() {
   Factory<int>* local = Factory<int>::getFactory();
   std::cout << "Foo: " << local << std::endl;
}
---

Bar.h
---
#ifndef BAR_H
#define BAR_H

class Bar
{
   public:
      Bar();
};

#endif
---

Bar.C
---
#include "Factory.h"
#include "Bar.h"
#include <iostream>

Bar::Bar() {
   Factory<int>* local = Factory<int>::getFactory();
   std::cout << "Bar: " << local << std::endl;
}
---

main.C
---
#include "Foo.h"
#include "Bar.h"

int main(int argc, char** argv)
{
   Foo f;
   Bar b;
   return 0;
}
---

-- 
           Summary: Static Template members having different values in
                    different instantiations of a template with the same
                    arguments
           Product: gcc
           Version: 3.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: gcaglar at hotmail dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14826


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/14826] Static Template members having different values in different instantiations of a template with the same arguments
  2004-04-02 20:53 [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments gcaglar at hotmail dot com
@ 2004-04-02 20:56 ` gcaglar at hotmail dot com
  2004-04-02 21:17 ` [Bug target/14826] [AIX] " bangerth at dealii dot org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: gcaglar at hotmail dot com @ 2004-04-02 20:56 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gcaglar at hotmail dot com  2004-04-02 20:56 -------
Created an attachment (id=6034)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=6034&action=view)
Tarball that has the .ii files and the output

Here is the package that has the required files.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14826


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/14826] [AIX] Static Template members having different values in different instantiations of a template with the same arguments
  2004-04-02 20:53 [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments gcaglar at hotmail dot com
  2004-04-02 20:56 ` [Bug c++/14826] " gcaglar at hotmail dot com
@ 2004-04-02 21:17 ` bangerth at dealii dot org
  2004-04-04  6:47 ` pinskia at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bangerth at dealii dot org @ 2004-04-02 21:17 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-04-02 21:17 -------
This works on x86 linux. However, since the report is about AIX, I 
assume it is about the usual lack of weak symbols on that system. In 
short, the linker should unify the two instances of the static variable, 
but doesn't. 
 
W. 

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c++                         |target
 GCC target triplet|                            |powerpc-ibm-aix5.1.0.0
            Summary|Static Template members     |[AIX] Static Template
                   |having different values in  |members having different
                   |different instantiations of |values in different
                   |a template with the same    |instantiations of a template
                   |arguments                   |with the same arguments


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14826


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/14826] [AIX] Static Template members having different values in different instantiations of a template with the same arguments
  2004-04-02 20:53 [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments gcaglar at hotmail dot com
  2004-04-02 20:56 ` [Bug c++/14826] " gcaglar at hotmail dot com
  2004-04-02 21:17 ` [Bug target/14826] [AIX] " bangerth at dealii dot org
@ 2004-04-04  6:47 ` pinskia at gcc dot gnu dot org
  2004-04-05  3:49 ` gcaglar at hotmail dot com
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-04-04  6:47 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-04-04 06:47 -------
Also this might be already fixed in 3.4.0, could you try a snapshot of 3.4.0?

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14826


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/14826] [AIX] Static Template members having different values in different instantiations of a template with the same arguments
  2004-04-02 20:53 [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments gcaglar at hotmail dot com
                   ` (2 preceding siblings ...)
  2004-04-04  6:47 ` pinskia at gcc dot gnu dot org
@ 2004-04-05  3:49 ` gcaglar at hotmail dot com
  2004-04-06 18:48 ` dje at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: gcaglar at hotmail dot com @ 2004-04-05  3:49 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gcaglar at hotmail dot com  2004-04-05 03:49 -------
(In reply to comment #3)
> Also this might be already fixed in 3.4.0, could you try a snapshot of 3.4.0?

No I have only tried with the releases, it works ok with the 3.2 series, but 
it doesn't work with the 3.3's. This is a pretty simple example could any of 
the developers try it with the 3.4's or confirm that they've fixed it, if so 
is there a patch?

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14826


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/14826] [AIX] Static Template members having different values in different instantiations of a template with the same arguments
  2004-04-02 20:53 [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments gcaglar at hotmail dot com
                   ` (3 preceding siblings ...)
  2004-04-05  3:49 ` gcaglar at hotmail dot com
@ 2004-04-06 18:48 ` dje at gcc dot gnu dot org
  2004-04-07  3:27 ` pinskia at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: dje at gcc dot gnu dot org @ 2004-04-06 18:48 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From dje at gcc dot gnu dot org  2004-04-06 18:48 -------
I can reproduce this problem on AIX 5.1.0.0 with GCC 3.3 and GCC 3.4 after
fixing an error in the example: <iostream> should be included before Factory.h.

This probably is either an AIX assembler error or AIX linker error.  The AIX
tools do not appear to be merging the two weak symbols in Foo.o and Bar.o. 
Either the linker is not merging them or the assembler is committing to the
local symbol too early and the linker cannot recover.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14826


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/14826] [AIX] Static Template members having different values in different instantiations of a template with the same arguments
  2004-04-02 20:53 [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments gcaglar at hotmail dot com
                   ` (4 preceding siblings ...)
  2004-04-06 18:48 ` dje at gcc dot gnu dot org
@ 2004-04-07  3:27 ` pinskia at gcc dot gnu dot org
  2004-04-16 13:45 ` gcc at pmade dot org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-04-07  3:27 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-04-07 03:26 -------
Confirmed by David.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|                            |1
   Last reconfirmed|0000-00-00 00:00:00         |2004-04-07 03:26:59
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14826


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/14826] [AIX] Static Template members having different values in different instantiations of a template with the same arguments
  2004-04-02 20:53 [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments gcaglar at hotmail dot com
                   ` (5 preceding siblings ...)
  2004-04-07  3:27 ` pinskia at gcc dot gnu dot org
@ 2004-04-16 13:45 ` gcc at pmade dot org
  2004-04-16 20:49 ` gcc at pmade dot org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: gcc at pmade dot org @ 2004-04-16 13:45 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gcc at pmade dot org  2004-04-16 13:05 -------
I have written a smaller test case that might be useful for tracking down
this problem. I have included the code below as well. I can confirm this
problem on AIX 5.2 with GCC 3.3.3 and 3.3.2.

This, IMO, is a major show stopper. Many common idioms use template static
members, including the Singleton.

I am willing to take a stab at this, if someone points me in the right
direction. I suspect, along with others, that this is a weak symbol problem.



// file a.h
#ifndef _a_h_
#define _a_h_

template <typename T> class Static {
public:
    static void* get_addr (void)
    { return &t_; }

private:
    static T *t_;
};

template <typename T> T* Static<T>::t_ = 0;

void print_from_b (void);
void print_from_c (void);

#endif


// file b.cxx
#include "a.h"
#include <iostream>

void print_from_b (void)
{
    std::cout << "From B: " << Static<int>::get_addr() << "\n";
}


// file c.cxx
#include "a.h"
#include <iostream>

void print_from_c (void)
{
    std::cout << "From C: " << Static<int>::get_addr() << "\n";
}


// file main.cxx
#include "a.h"

int main (void)
{
    Static<int> s;

    print_from_b();
    print_from_c();

    return 0;
}


On FreeBSD:
>From B: 0x8089010
>From C: 0x8089010


On AIX 5.2:
>From B: 0x200179a0
>From C: 0x20017ac8


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14826


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/14826] [AIX] Static Template members having different values in different instantiations of a template with the same arguments
  2004-04-02 20:53 [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments gcaglar at hotmail dot com
                   ` (6 preceding siblings ...)
  2004-04-16 13:45 ` gcc at pmade dot org
@ 2004-04-16 20:49 ` gcc at pmade dot org
  2004-05-05 19:51 ` gcc at pmade dot org
  2004-05-15 16:25 ` dje at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: gcc at pmade dot org @ 2004-04-16 20:49 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gcc at pmade dot org  2004-04-16 20:12 -------
I should also mention that I have a current AIX support contract. If I can
gather some specifics about this problem, and whether or not it is caused by a
linker or assembler bug, I can open a ticket with IBM.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14826


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/14826] [AIX] Static Template members having different values in different instantiations of a template with the same arguments
  2004-04-02 20:53 [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments gcaglar at hotmail dot com
                   ` (7 preceding siblings ...)
  2004-04-16 20:49 ` gcc at pmade dot org
@ 2004-05-05 19:51 ` gcc at pmade dot org
  2004-05-15 16:25 ` dje at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: gcc at pmade dot org @ 2004-05-05 19:51 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gcc at pmade dot org  2004-05-05 19:51 -------
I have attached a shell script that you can use instead of g++ to correctly
build my test case. Changes need to be made to each assembly file generated by
GCC. The assembly from b.cxx and c.cxx needs to be changed so that the static
symbol is placed into the table of contents with [RW]. The assembly from
main.cxx needs to be changed so that the static symbol is marked as .globl
instead of .weak.

As I understand from David, this is not an easy fix in GCC, and getting IBM to
fix it would not be any easier.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14826


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug target/14826] [AIX] Static Template members having different values in different instantiations of a template with the same arguments
  2004-04-02 20:53 [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments gcaglar at hotmail dot com
                   ` (8 preceding siblings ...)
  2004-05-05 19:51 ` gcc at pmade dot org
@ 2004-05-15 16:25 ` dje at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: dje at gcc dot gnu dot org @ 2004-05-15 16:25 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From dje at gcc dot gnu dot org  2004-05-14 21:37 -------
If there is no strong definition (.globl), the linker is suppose to choose
one of the weak definitions. The compiler cannot know to produce a strong
definition in one file.

Maybe you can explicitly instantiate the template in the main file
to force the compiler to generate the strong symbol.  You can try
compiling with -fno-implicit-templates and modifying your application
until that works.

Also, GCC marks the data symbol as weak, but not the TOC symbol.  This may be
contributing to each file receiving its own copy of the data symbol.  Because
GCC does not decide whether the symbol will be local or external when initially
referencing the symbol, it is difficult to emit the appropriate storage
allocation for the TOC symbol.

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14826


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2004-05-14 21:37 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-02 20:53 [Bug c++/14826] New: Static Template members having different values in different instantiations of a template with the same arguments gcaglar at hotmail dot com
2004-04-02 20:56 ` [Bug c++/14826] " gcaglar at hotmail dot com
2004-04-02 21:17 ` [Bug target/14826] [AIX] " bangerth at dealii dot org
2004-04-04  6:47 ` pinskia at gcc dot gnu dot org
2004-04-05  3:49 ` gcaglar at hotmail dot com
2004-04-06 18:48 ` dje at gcc dot gnu dot org
2004-04-07  3:27 ` pinskia at gcc dot gnu dot org
2004-04-16 13:45 ` gcc at pmade dot org
2004-04-16 20:49 ` gcc at pmade dot org
2004-05-05 19:51 ` gcc at pmade dot org
2004-05-15 16:25 ` dje at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).