Bug 12079

Summary: Regression from gcc 3.2.2: incorrect code generation when -O3 optimization is used
Product: gcc Reporter: Andrew A. Peristiy <amelkor>
Component: rtl-optimizationAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: critical CC: gcc-bugs
Priority: P2    
Version: 3.3.1   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Andrew A. Peristiy 2003-08-27 13:00:00 UTC
Configuration:
andr@asp:~/test/gcc331\%uname -a
Linux asp 2.4.18-27.7.x #1 Fri Mar 14 06:44:53 EST 2003 i686 unknown

andr@asp:~/test/gcc331\%gcc -v
Reading specs from /home/dev/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/specs
Configured with: /home/dev/src/gcc-3.3.1/configure --prefix=/home/dev
--enable-threads --enable-languages=c,c++
Thread model: posix
gcc version 3.3.1
--------

Consider program with three simple files Test.h, Test.cpp and main.cpp:
File Test.h :
---
#ifndef ___TEST_
#define ___TEST_

class Test
{
    static const char* str;

public:

    static const char* get();

};

#endif
-----

File Test.cpp :
---
#include "Test.h"

const char* Test::str = "SomeString";

const char* Test::get()
{
    return str;
}
----

File main.cpp :
---
#include "Test.h"
#include <iostream>

int main(  int, char** )
{
  const char* x = Test::get();

  std::cout << x << "\n";
  return 0;
}
-----

Compile class Test from Test.cpp into shared library with -O3 optimization:

andr@asp:~/test/gcc331\%g++ -g -O3 -fPIC -shared Test.cpp -o test.so
andr@asp:~/test/gcc331\%

Then compile code from the file main.cpp that will use it:

andr@asp:~/test/gcc331\%g++ -g main.cpp test.so -o main
andr@asp:~/test/gcc331\%

Now run it:
andr@asp:~/test/gcc331\%./main
Segmentation fault (core dumped)
andr@asp:~/test/gcc331\%

Recompile shared library with -O2 optimization and run test again:

andr@asp:~/test/gcc331\%g++ -g -O2 -fPIC -shared Test.cpp -o test.so
andr@asp:~/test/gcc331\%./main
SomeString
andr@asp:~/test/gcc331\%

It works fine in this case !
With gcc 3.2.2 it works fine in both cases with -O2 and -O3 options.
Comment 1 Andrew Pinski 2003-08-27 13:37:37 UTC
What binutils version are you using?
If it is less than 2.13.1 then please upgrade as any version less than that, misassembles code.
From <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.

If that is your problem then this is a dup of bug 10877.
Comment 2 Andrew A. Peristiy 2003-08-28 12:07:48 UTC
I have upgraded binutils to 2.13.90 now it works! I confirm that my report
describes the same problem as in 10877.
 


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