Mach-O Programming Topics

Introduction

OS X supports a number of application environments, each with its own runtime rules, conventions, and file formats. In OS X, kernel extensions, command-line tools, applications, frameworks, and libraries (shared and static) are implemented using Mach-O (Mach object) files.

The OS X runtime architecture dictates how object files are laid out in the filesystem and how programs communicate with the kernel. The object file format used in OS X is Mach-O .

The following list describes other runtime environments supported in OS X:

Classic is a Mac app that runs Mac OS 9 within its address space and provides bridging services that allow OS X to interact with Mac OS 9 applications. Both classic 68K applications and PowerPC Code Fragment Manager (CFM) applications can run under Mac OS 9 in Classic. (Mac OS 9 does not support the 68K variant of Code Fragment Manager, so you cannot run CFM-68K applications in OS X.)

LaunchCFMApp is a command-line tool that runs programs created for the PowerPC Code Fragment Manager. The file format used by such programs is called Preferred Executable Format (PEF). Carbon provides bridging for Code Fragment Manager applications that allows them to link to Mach-O–based code, but—for ease of debugging if for no other reason—it’s generally a good idea to use Mach-O for Carbon applications.

The HotSpot Java virtual machine is a Mac app that executes Java bytecode applications and applets.

The OS X kernel supports kernel extensions (KEXTs), which are static Mach-O executable files that are loaded directly into the address space of the kernel. Because errant code can write directly to memory used by the kernel, kernel extensions have the potential to crash the operating system. You should generally avoid implementing functionality as kernel extensions if possible.

The Code Fragment Manager is documented in Mac OS Runtime Architectures, available from the Apple Developer Connection website.

This document discusses how you use the Mach-O file format. It describes what types of programs you can build, how programs are loaded and executed, how you can change the way programs are loaded and executed, how to load code at runtime, and how to load and link code at runtime. If you create or load bundles, shared libraries, or frameworks, you’ll probably want to read and understand everything in this document.

Who Should Read This Document

If you write development tools for OS X, you need to understand the information presented in this document.

This document is also useful for developers of shared libraries and frameworks, and for developers of applications that need to load code at runtime.

Organization of This Document

This document contains the following articles:

Building Mach-O Files describes how Mac apps are built and describes the types of programs you can develop.

If you are loading code at runtime but cannot or do not wish to use the CFBundle opaque type or the NSBundle class, you should refer to OS X ABI Dynamic Loader Reference.

The GCC C++ application binary interface—the specification of C++ class member layout, function/method name mangling, and related C++ issues. This information is documented for GCC 3.0 and later at http://www.codesourcery.com/cxx-abi/abi.html.

The GCC Objective-C data structures and dynamic runtime functions. For this information, see The Objective-C Programming Language.

You might also find the following books useful in conjunction with this document:

Mac OS Runtime Architectures, Apple Computer, Inc. Available at http://developer.apple.com/tools/mpw-tools/books.html. Documents the classic 68K segment loader architecture, as well as the Code Fragment Manager Preferred Executable executable format used with classic PowerPC applications and with many Carbon applications.

Linkers and Loaders, John R. Levine, Morgan Kaufmann, 2000, ISBN 1-55860-496-0. Describes the workings and operation of standard linkers from the earliest program loaders to the present dynamic link editors. Among the contents of this book are discussions of the classic BSD a.out format, the Executable and Linking Format (ELF) preferred by many current operating systems, the IBM System/360 linker output format, and the Microsoft Portable Executable (PE) format.

System V Application Binary Interface AMD64 Architecture Processor Supplement. Found at http://www.x86-64.org/documentation, this document describes the System V x86-64 environment, on which the OS X x86-64 environment is based.