Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:Classic Mac 68k CODE segments

From ReverseEngineering.wiki
Format
On this wiki Facts · Code · Notes · Topics

How a 68k classic Mac application stores its code: 'CODE' resources in the resource fork, where CODE 0 holds the jump table and CODE 1 and up are segments loaded on demand through `_LoadSeg` (A9F0) via jump table entries.

Overview

A 68k Mac application (or driver, or 68k code resource) keeps its program in 'CODE' resources of its Format:Mac resource fork. Resource ID 0 is not code: it holds the application's jump table. IDs 1 and up are code segments, loaded by the Segment Loader when first called. See Platform:Classic Mac OS. PowerPC programs instead use Format:PEF (Preferred Executable Format).

CODE 0 and the jump table

From elliotnunn ToolboxToolbox FlattenSegmentedMacAppForDisassembler.py (MIT, lines 35-61): CODE 0 has two big-endian u32 at offsets 8 and 12 (the size of the jump table and the offset of the jump table from register A5), and the jump table entries start at offset 16, 8 bytes each. The script reads A5-relative address of an entry as (entry offset - 16) + the A5 offset + 2.

  • 16-bit table (classic): each entry is u16 offset within the target segment, then 3F3C (MOVE.W #n,-(SP)), u16 segment number, then A9F0 (the _LoadSeg trap). The script raises an error if 3F3C or A9F0 are missing.
  • 32-bit table: an entry 00 00 FF FF 00 00 00 00 switches to the 32-bit format; after it each entry is u16 segment number, A9F0, u32 offset.

So a call to another segment appears in code as a JSR or PEA with a 16-bit A5-relative displacement into the jump table; the first call goes through the _LoadSeg stub and later calls jump directly. Unverified: the 4-byte header at the start of each CODE n resource (offset of the first jump table entry, number of entries) is from Inside Macintosh memory and was not read in a source.

32-bit (far model) segments

The same script treats a CODE n resource starting with FF FF as a 32-bit segment with two relocation lists whose offsets are at 0x14 (A5 relocations) and 0x1C (PC relocations), read as big-endian u32 (struct '>LxxxxL' at 0x14). Each list is a byte stream decoded as deltas: a first byte of 00 with bit 0x80 of the second byte set introduces a 5-byte value (a 32-bit delta shifted left by one), a first byte with bit 0x80 set introduces a 2-byte value (15 bits, shifted left by one), otherwise a single byte (shifted left by one); a 00 00 pair ends the list. The running sum is the offset of the longword to relocate.

MacsBug procedure names

The same script finds function names left by compilers after the function's last instruction (RTS 4E75, JMP (A0) 4ED0, or RTD 4E74): either 8 bytes of fixed-length name, or a length byte 0x80 and a second length byte then the name, or a byte 0x81-0x9F whose low 7 bits are the length followed by the name. A function start is found by walking back to a jump table target or to the opcode word 'NV' (4E56, LINK A6, written as bytes 4E 56 in memory, the script tests for the two ASCII letters). Characters accepted in names are letters, digits, space, % and _.

Reversing

Flatten the segments into one image (the script places segment n at 0x100000*n and rewrites the JMP targets so a disassembler sees direct calls), and name functions from the MacsBug strings. Stack-based Pascal calling convention and trap words (A-line opcodes, A9F0 _LoadSeg) are normal in this code.

Code & repositories

  • https://github.com/elliotnunn/ToolboxToolbox @ f67194a8893ec4277fa189412bcda5a62160ff99 · license: MIT
    Scripts to take Toolbox ROMs and classic apps apart; FlattenSegmentedMacAppForDisassembler.py rebuilds a flat image from CODE segments with MacsBug names.
    Tested on: source read, not run: read the source only · checked 2026-10-19; last activity 2022-03-31