autc04 / Retro68

a gcc-based cross-compiler for classic 68K and PPC Macintoshes
GNU General Public License v3.0
537 stars 51 forks source link

powerpc-apple-macos-gcc "-g" flag makes app crash #205

Open ryandesign opened 1 year ago

ryandesign commented 1 year ago

My app crashes or behaves incorrectly when compiled for PowerPC with the CMake "Debug" build type. I originally reported this in #184 but didn't realize at the time that crashes were happening for different reasons on different systems.

For example, my app may launch fine but crash later when I try to invoke a function from a third-party library. I flipped a build switch to completely exclude that third-party library from the build, and then the app failed its initialization function, which checks whether the Appearance Manager is installed. The app determined that Appearance was not installed, even though it is. My function that determines if Appearance is installed stores the result in a static variable, and I think something about static variables is not working correctly when the -g flag is used.

I've reduced this crash to the following simplest test case:

main.c:

void main(void)
{
    static int s = -1;
    ++s;
}

Build commands (simplified from what CMake generated):

/opt/local/libexec/Retro68/bin/powerpc-apple-macos-gcc -g -o main.o -c main.c   
/opt/local/libexec/Retro68/bin/powerpc-apple-macos-gcc -g -o app.xcoff main.o
/opt/local/libexec/Retro68/bin/MakePEF -o app.pef app.xcoff 
/opt/local/libexec/Retro68/bin/Rez /opt/local/libexec/Retro68/powerpc-apple-macos/RIncludes/RetroPPCAPPL.r -I/opt/local/libexec/Retro68/powerpc-apple-macos/RIncludes -DCFRAG_NAME="\"app\"" -o app.bin -t APPL -c '????' --data app.pef
LaunchAPPL app.bin

The crash is reported in MacsBug variously as "PowerPC unmapped memory exception" or "PowerPC read-only memory exception" or "PowerPC excluded memory exception". This is on a beige Power Macintosh G3 running Mac OS 9.2.2.

The problem disappears if I remove the -g flag from the first line when compiling main.c into main.o.