User account creation filtered due to spam.

Bug 12484

Summary: virtual function yields bogus value when returning float constant (-O3 -fPIC)
Product: gcc Reporter: jonathan
Component: rtl-optimizationAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: gcc-bugs
Priority: P2    
Version: 3.3   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Attachments: source code reproducing bug
output from command g++ -v -save-temps
preprocessed file

Description jonathan 2003-10-01 19:00:14 UTC
here's the code to reproduce the error...

//----
#include <iostream>

class Foo
{
public:
  Foo() : _value(1) {}
  virtual ~Foo() {}

  virtual float getValue() const { return float(1); }
  virtual float getMemberValue() const { return _value; }

private:
  float const _value;
};


void
useFoo(Foo const &f)
{

  std::cout << std::endl
	    << "Foo const &f; " 
	    << std::endl;

  std::cout << " f.getValue() = " 
	    << f.getValue()
	    << std::endl;

  std::cout << " f.getMemberValue() = " 
	    << f.getMemberValue()
	    << std::endl;
}



int
main()
{
  Foo foo;

  std::cout << std::endl
	    << "Foo foo; " 
	    << std::endl;

  std::cout << " foo.getValue() = " 
	    << foo.getValue()
	    << std::endl;

  std::cout << " foo.getMemberValue() = " 
	    << foo.getMemberValue()
	    << std::endl;

  useFoo(foo);

}
//---

note:

1) problem occurs with -O3 and -fPI; dropping to -O2 and/or omitting -fPIC
eliminates problem

2) bogus results occur only when returning constant (e.g. return float(1); );
returning member data is o.k. (e.g. return _value)

3) bogus results occur only when given const reference to object having virtual
member; seems to work o.k. given the object itself

4) g++ 3.3.1 gives same behavior
Comment 1 jonathan 2003-10-01 19:02:45 UTC
Created attachment 4877 [details]
source code reproducing bug
Comment 2 jonathan 2003-10-01 19:03:48 UTC
Created attachment 4878 [details]
output from command g++ -v -save-temps
Comment 3 jonathan 2003-10-01 19:05:31 UTC
Created attachment 4879 [details]
preprocessed file
Comment 4 Andrew Pinski 2003-10-01 19:06:28 UTC
What version of binutils are you using?
Also what target are you using, i686-pc-linux?
If so read < http://gcc.gnu.org/install/specific.html#ix86-*-linux* >: As of GCC 3.3, binutils 
2.13.1 or later is required for this platform.  See bug 10877 for more information.
Comment 5 jonathan 2003-10-01 19:56:43 UTC
yes, i'm using target i686-pc-linux.

the box where i first detected the problem uses binutils 2.11.93.0.2 20020207
jmw@vulcan:115>ld --version
GNU ld version 2.11.93.0.2 20020207

we have another machine using binutils 2.13.90.0.2 20020802
jmw@eric:16>ld --version
GNU ld version 2.13.90.0.2 20020802

this problem appears to be resolved when using the latter version of binutils.

thanks for your help.


*** This bug has been marked as a duplicate of 10877 ***