Mac OS X Reference Library Apple Developer
Search

Major 64-Bit Changes

There are many differences between 32-bit and 64-bit environments in Mac OS X, including tool usage changes, changes to the size and alignment of data types, alignment pragmas, and I/O Kit drivers. This chapter describes the main changes developers should be aware of when porting code to 64-bit. You should read this chapter if you've decided to port your code to 64-bit or if you are writing a new code from scratch.

Tools Changes

You'll find a number of issues when porting code to a 64-bit executable. You can address most of these issues with subtle tweaks to your code. However, before you touch the first line of code, there are a few broad issues you should be aware of:

Data Type Changes

This section describes the changes to data type sizes and alignment in 64-bit executables, and explains how these changes will impact your code.

Data Type Size and Alignment

Mac OS X uses two data models: ILP32 (in which integers, long integers, and pointers are 32-bit quantities) and LP64 (in which integers are 32-bit quantities, and long integers and pointers are 64-bit quantities). Other types are equivalent to their 32-bit counterparts (except for size_t and a few others that are defined based on the size of long integers or pointers).

While almost all UNIX and Linux implementations use LP64, other operating systems use various data models. Windows, for example, uses LLP64, in which long long variables and pointers are 64-bit quantities, while long integers are 32-bit quantities. Cray, by contrast, uses ILP64, in which int variables are also 64-bit quantities.

In Mac OS X, the default alignment used for data structure layout is natural alignment (with a few exceptions noted below). Natural alignment means that data elements within a structure are aligned at intervals corresponding to the width of the underlying data type. For example, an int variable, which is 4 bytes wide, would be aligned on a 4-byte boundary.

Note: The data types fpos_t, off_t, and long long were historically exceptions to the natural alignment rules in the 32-bit PowerPC standard. Though they were all 8-byte (64-bit) types, they were aligned on 4-byte (32-bit) boundaries. In a 64-bit environment, these data types are naturally aligned on 8-byte (64-bit) boundaries.

Table 2-1 shows some common data types used in Mac OS X, along with their size and alignment. LP64 differences are highlighted in bold.

Table 2-1  Size and alignment of base data types in Mac OS X

Data type

ILP32 size

ILP32 alignment

LP64 size

LP64 alignment

char

1 byte

1 byte

1 byte

1 byte

short

2 bytes

2 bytes

2 bytes

2 bytes

int

4 bytes

4 bytes

4 bytes

4 bytes

long

4 bytes

4 bytes

8 bytes

8 bytes

pointer

4 bytes

4 bytes

8 bytes

8 bytes

size_t

4 bytes

4 bytes

8 bytes

8 bytes

long long

8 bytes

4 bytes

8 bytes

8 bytes

fpos_t

8 bytes

4 bytes

8 bytes

8 bytes

off_t

8 bytes

4 bytes

8 bytes

8 bytes

Note: Floating-point data type sizes are the same whether you are generating a 32-bit or 64-bit executable. However, the size of long double is 128 bits wide in GCC 4.0 and later (required for 64-bit compilation). Previous versions of the compiler (3.3 and earlier) used a 64-bit-wide long double type.

Because changes in size and alignment can significantly affect the data size produced by your code, you should generally pack structures so that the largest data types appear first, followed by progressively smaller data types. In this way, you maximize the use of space.

If, for compatibility, you need to support on-disk or network data structures containing 64-bit values aligned on 4-byte boundaries, you can override the default alignment using pragmas. See “Making Code 64-Bit Clean” for more information.

In addition to these changes to the base data types, various layers of Mac OS X have other data types that change size or underlying type in a 64-bit environment. These changes are described in 64-Bit Guide for Carbon Developers, 64-Bit Transition Guide for Cocoa, and “Kernel Extensions and Drivers.”

Data Type Impact on Code

Data type and alignment changes impact developers in several broad areas.

Security Changes

In 64-bit executables, executing code in data segments is not allowed. To achieve this, the NX (no execute) bit is set on the page table entries for the stack, heap, and initialized and uninitialized data segments.

In general, this should have no impact on developers unless you are doing something nonstandard such as writing self-modifying code. If you are writing code that requires execution on the stack or in the heap, you must explicitly mark the pages executable using mprotect or other similar calls.




Last updated: 2010-01-15

Did this document help you? Yes It's good, but... Not helpful...