Game Development Community

Segfault with gcc 3.4.1 through 3.4.3

by Benoit Touchette · in Torque Game Engine · 11/15/2004 (4:51 pm) · 8 replies

After trying to figure out why i was segfaulting and getting sidetracked with this issue at www.garagegames.com/mg/forums/result.thread.php?qt=21521

I finally sat down with my trusted ddd and i seem to be actually segfaulting in Processor::init in x86UNIXCPUinfo.cc in the first section of assembly code starting at line 58. If i skip that part it actually works fine after the modifications suggested by James in the above thread. This seems to happen with gcc 3.4.1, 3.4.2, and 3.4.3 on my system here at home. At this point am not sure how to about fixing. Will keep digging.

Edit: Oh and using -O0 or -O1 seems to work, only issue seems to be using -O2 do i get the above problem.

#1
11/15/2004 (5:37 pm)
Found a solution that should work for both the standard torque and the RTS pack, hope this helps others with the same issue.

--- x86UNIXCPUInfo.old.cc	2004-11-15 22:11:32.000000000 -0500
+++ x86UNIXCPUInfo.cc	2004-11-15 22:13:07.000000000 -0500
@@ -29,6 +29,8 @@
 static char vendor[13] = {0,};
 static U32 properties = 0;
 static U32 processor  = 0;
+static U32 timeLo;
+static U32 timeHi;
 
 void Processor::init()
 {
@@ -55,6 +57,9 @@
        Platform::SystemInfo.processor.properties & CPU_PROP_FPU)
    {
       const U32 MS_INTERVAL = 750;
+#if defined(TORQUE_COMPILER_GCC) && ((__GNUC__ >= 3) && (__GNUC_MINOR__ >= 4))
+       asm ("rdtsc" : "=a" (timeLo), "=d" (timeHi));
+#else
       __asm__(
          "pushl  %eax\n"
          "pushl  %edx\n"
@@ -64,10 +69,23 @@
          "popl   %edx\n"
          "popl   %eax\n"
          );
+#endif
       U32 ms = Platform::getRealMilliseconds();
       while ( Platform::getRealMilliseconds() < ms+MS_INTERVAL )
       { /* empty */ }
       ms = Platform::getRealMilliseconds()-ms;
+#if defined(TORQUE_COMPILER_GCC) && ((__GNUC__ >= 3) && (__GNUC_MINOR__ >= 4))
+      asm(
+         "pushl  %eax\n"
+         "pushl  %edx\n"
+         "rdtsc\n"
+         "sub    (timeHi), %edx\n"
+         "sbb    (timeLo), %eax\n"
+         "mov    %eax, (clockticks)\n"
+         "popl   %edx\n"
+         "popl   %eax\n"
+         );
+#else
       asm(
          "pushl  %eax\n"
          "pushl  %edx\n"
@@ -78,6 +96,7 @@
          "popl   %edx\n"
          "popl   %eax\n"
          );
+#endif
       U32 mhz = static_cast<U32>(F32(clockticks) / F32(ms) / 1000.0f);
       
       // catch-22 the timing method used above to calc Mhz is generally
#2
11/16/2004 (6:08 am)
Exactly what I needed. Works fine with -O2 and -O3 now. Much thanks.
#3
11/16/2004 (6:37 am)
Glad it fixed your problem also :)
#4
11/16/2004 (3:19 pm)
This is on my todo list, thanks!
#5
11/19/2004 (11:10 am)
Ok, fixed.
#6
11/19/2004 (11:23 am)
Excellent :)
#7
08/10/2005 (8:16 am)
Works here too, just had to modify the GCC check to look for GCC 4 properly:

#if defined(TORQUE_COMPILER_GCC) && (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)) || (__GNUC__ == 4))
#8
08/10/2005 (8:15 pm)
Any idea what this should be for the AMD64 in native 64-bit mode? I'm attempting a compile of TGE 1.4rc1 on Fedora Core 4 x86_64 and have dealt with most of the C++ issues but I don't know what needs to happen in the asm parts of the code. This is one. The others are the CPU identification and the math acceleration code.