Writing an Application that Runs in Mac OS System 1 Using CodeWarrior Gold 11

Yes. Weird indeed.

Hunk: Kind=HUNK_GLOBAL_CODE Name="foo"(1) Size=8
00000000: 3F3C 0001 MOVE.W #$0001,-(A7)
00000004: A9C8 _SysBeep
00000006: 4E75 RTS

Hunk: Kind=HUNK_GLOBAL_CODE Name="main_test"(2) Size=12
00000000: 4EAD 0000 JSR foo
00000004: 4EB9 0000 0000 JSR bar
0000000A: 4E75 RTS

XRef: Kind=HUNK_XREF_CODE32BIT Name="bar"(3) #Pairs=1
Offset=$00000006 Value=$00000000

XRef: Kind=HUNK_XREF_CODE16BIT Name="foo"(1) #Pairs=1
Offset=$00000002 Value=$00000000

Hunk: Kind=HUNK_GLOBAL_CODE Name="bar"(3) Size=8
00000000: 3F3C 0002 MOVE.W #$0002,-(A7)
00000004: A9C8 _SysBeep
00000006: 4E75 RTS
 
Fifth Crash: HOpenResFile and HCreateResFile

Don’t get fooled by the letter ‘H’ -- these are not HFS traps. That is, they are not part of the HD20 Init or MacPlus ROM that brought HFS to the Macintosh. You need to check for the existence of these traps specifically, not the existence of HFS. CodeWarrior has nice glue code that does this work for you, and falls back to OpenRFPerm and CreateResFile. However, the glue code fails on the 64K ROMs due to the condensed trap table, which is seems to be consistent source of crashes.
Hi David! I'm acutely curious where this glue code exists... I downloaded CW 11 Pro, but its not immediately clear where it is in the myriad of folders. Thanks!
 
I'm acutely curious where this glue code exists...

In MacOS.lib.

Add it to a project. Click MacOS.lib in the project to select it. Choose Disassemble from the Project menu. You'll get a list of all of the routines included in this library, including a bunch that have the same name as the Apple routines. This is the glue.

1769883967655.png

Now pick one of the names that you'd like to learn more about. Copy that and choose Find from the Search menu. It will take you to the specific function.

How does the compiler/linker know when to call a real MacOS trap versus the glue? Well, Apple includes the real trap address in the header files when you are supposed to call it directly. Otherwise, it just includes the C prototype. Let's look at the official Apple Resources.h file for HOpenResFile.

1769884526867.png

If you define SystemSevenOrLater as true (meaning your project only targets System 7 or later and doesn't intend to work in earlier systems), then you don't need the glue code, because that trap exists and is fully working in System 7. So, the compiler will insert the trap (0xA81A) directly into the compiled code. Otherwise it just provides the prototype for the function. The linker will then complain if you don't add the MacOS.lib or your own HOpenResFile function.

I have not located the source code for the glue code. I suspect it is Apple proprietary and not released.

- David
 
Back
Top