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:PEF (Preferred Executable Format)

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

The code fragment container of PowerPC (and CFM-68K) classic Mac OS: starts with "Joy!peff", a 40-byte container header, 28-byte section headers, then code, data, packed data and a loader section with imported libraries, exports and a relocation instruction stream. Located through the 'cfrg' resource.

Overview

PEF is the container used by the Code Fragment Manager (CFM) for PowerPC applications, shared libraries, drivers and extensions of Mac OS 7.5 to 9, and by native drivers in Mac ROMs. Big-endian. The fragment usually lives in the data fork of the file; the resource fork holds a 'cfrg' resource that says which fragments exist and where they are (see below). Carbon applications of Mac OS 9/X also use PEF. See Platform:Classic Mac OS and Format:Mac resource fork.

Container header

From elliotnunn patchpef pefbinary.py (struct '>4s4s4s5I2HI', 40 bytes) and tbxi pef_info.py (same format, MIT): bytes 'Joy!' and 'peff', a 4-byte architecture tag (pef2elf requires 'pwpc' for PowerPC; 'm68k' is the CFM-68K tag, Unverified), then five u32 (format version, timestamp, old definition version, old implementation version, current version), u16 section count, u16 instantiated section count, u32 reserved. The first 12 bytes of a PowerPC file are therefore 4A 6F 79 21 70 65 66 66 70 77 70 63.

Section headers

One 28-byte header per section follows the container header (struct '>i5I4B'): s32 name offset into the loader string table (-1 for none), u32 default address, u32 total size, u32 unpacked size, u32 packed size (the size in the container), u32 container offset, u8 section kind, u8 share kind, u8 alignment (power of two), u8 reserved. pef2elf reads them at 40+28*i and treats kinds 0 code, 1 data, 2 packed data (pidata), 3 constant data, 4 loader; the memory image of a section is its container bytes zero-extended to the total size.

Loader section (kind 4)

pef2elf.go reads these big-endian fields of the loader header: s32 main section and u32 main offset at +0/+4, init section and offset at +8/+12, term section and offset at +16/+20 (section index, offset within it; sections are numbered from 0 in the header order), u32 imported library count at +24, u32 total imported symbol count at +28, u32 relocation section count at +32, u32 offset of the relocation instructions at +36, u32 offset of the loader string table at +40, u32 export hash table offset at +44, u32 hash table power at +48, u32 exported symbol count at +52. The imported library table starts at +56, 24 bytes per library: u32 name offset, old and current version, u32 imported symbol count at +12, u32 index of its first imported symbol at +16, options. Imported symbols follow, 4 bytes each: low 24 bits are the name offset into the string table (the top byte is flags). Then the relocation headers, 12 bytes each: u16 section index, 2 bytes, u32 number of 16-bit relocation instructions at +4, u32 first instruction offset at +8. Exports: after the hash table (4 bytes per slot, 2^power slots) there is a key table (4 bytes per export, the high u16 being the name length) and then 10-byte export records: top byte kind and low 24 bits name offset, u32 offset, s16 section index (negative means an absolute or re-export).

Relocation opcodes

pef2elf relocation.go decodes the instruction stream (16-bit big-endian words, decided by the top bits): 00xxxxxx = RelocBySectDWithSkip (skip count in bits 6-13, run in the low 6 bits); 010 + 4-bit subopcode + 9-bit run-1: 0 BySectC, 1 BySectD, 2 TVector12, 3 TVector8, 4 VTable8, 5 ImportRun; 011 + subopcode: 0 SmByImport, 1 SmSetSectC, 2 SmSetSectD, 3 SmBySection (9-bit index); 1000 + 12 bits = IncrPosition (offset-1); 1001 = SmRepeat; 101000 = SetPosition (26-bit offset in two words); 101001 = LgByImport; 101100 = LgRepeat; 101101 = LgSetOrBySection. Words start with relocation address 0, section C = 0 and section D = 1.

Packed data (kind 2)

Pattern-initialised data sections use a small opcode stream (pidata.go in pef2elf); Unverified here: details were not read.

Locating the fragment: 'cfrg'

tbxi cfrg_rsrc.py (MIT) shows the old-style 'cfrg' layout: the u32 entry count is at offset 28 and the first entry at offset 32; each entry has a 42-byte fixed part followed by a Pascal-string name, and the next entry starts at the next multiple of 4. Inside an entry, the byte at +23 is the locator kind (1 = data fork, kDataForkCFragLocator), the u32 at +24 is the fragment's offset in the data fork and the u32 at +28 its length (0 means to end of fork). This is why tools that repack a data fork must patch the 'cfrg'.

Reversing

Import names plus the transition vector convention (a function pointer is a pair: code address and TOC base in r2) let you name calls: calls to imports go through glue that loads a TOC slot. pef2elf (no licence file found) converts a PowerPC PEF into a big-endian ELF with symbols, which Ghidra or IDA can load; it guesses the TOC by counting likely transition vectors.

Unverified

Unverified: PEF format version 1 is the only one in use; section kind 5 (export-only) and the separate 'pidata' opcode set were not read.

Code & repositories

  • https://github.com/elliotnunn/tbxi @ 69839ea3ad16697a161125408f9797765d66f2a9 · license: MIT
    Mac OS ROM mangler (Python, MIT) with PEF header parsing and 'cfrg' data fork offset handling.
    Tested on: source read, not run: read the source only (cfrg_rsrc.py, pef_info.py) · checked 2026-10-19; last activity 2025-12-01
  • https://github.com/elliotnunn/pef2elf @ 31326957317e6c8685755411755bc6cbd33e9aaf · license: none
    Go tool converting a PowerPC PEF to an ELF with symbols; its source decodes the loader section and relocation stream. No licence file was found in the repository.
    Tested on: source read, not run: read the source only · checked 2026-10-19; last activity 2025-03-01; no license: not reusable