1 //===- MachOFormat.h - Mach-O Format Structures And Constants ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares various structures and constants which are platform
11 // independent and can be shared by any client which wishes to interact with
14 // The definitions here are purposely chosen to match the LLVM style as opposed
15 // to following the platform specific definition of the format.
17 // On a Mach system, see the <mach-o/...> includes for more information, in
18 // particular <mach-o/loader.h>.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_OBJECT_MACHOFORMAT_H
23 #define LLVM_OBJECT_MACHOFORMAT_H
25 #include "llvm/Support/DataTypes.h"
30 /// General Mach platform information.
32 /// @name CPU Type and Subtype Information
35 /// \brief Capability bits used in CPU type encoding.
36 enum CPUTypeFlagsMask {
37 CTFM_ArchMask = 0xFF000000,
38 CTFM_ArchABI64 = 0x01000000
41 /// \brief Machine type IDs used in CPU type encoding.
44 CTM_x86_64 = CTM_i386 | CTFM_ArchABI64,
48 CTM_PowerPC64 = CTM_PowerPC | CTFM_ArchABI64
51 /// \brief Capability bits used in CPU subtype encoding.
52 enum CPUSubtypeFlagsMask {
53 CSFM_SubtypeMask = 0xFF000000,
54 CSFM_SubtypeLib64 = 0x80000000
57 /// \brief ARM Machine Subtypes.
73 /// \brief PowerPC Machine Subtypes.
74 enum CPUSubtypePowerPC {
78 /// \brief SPARC Machine Subtypes.
79 enum CPUSubtypeSPARC {
83 /// \brief x86 Machine Subtypes.
90 } // end namespace mach
92 /// Format information for Mach object files.
94 /// \brief Constants for structure sizes.
99 FatArchHeaderSize = 20,
100 SegmentLoadCommand32Size = 56,
101 SegmentLoadCommand64Size = 72,
104 SymtabLoadCommandSize = 24,
105 DysymtabLoadCommandSize = 80,
108 RelocationInfoSize = 8,
109 LinkeditLoadCommandSize = 16
112 /// \brief Constants for header magic field.
114 HM_Object32 = 0xFEEDFACE, ///< 32-bit mach object file
115 HM_Object64 = 0xFEEDFACF, ///< 64-bit mach object file
116 HM_Universal = 0xCAFEBABE ///< Universal object file
119 /// \brief Header common to all Mach object files.
125 uint32_t NumLoadCommands;
126 uint32_t SizeOfLoadCommands;
130 /// \brief Extended header for 64-bit object files.
135 /// \brief Header for universal object files.
141 /// \brief Header for a single-architecture object file in a
142 /// universal binary.
143 struct FatArchHeader {
151 // See <mach-o/loader.h>.
152 enum HeaderFileType {
157 HF_SubsectionsViaSymbols = 0x2000
160 enum LoadCommandType {
164 LCT_Segment64 = 0x19,
166 LCT_CodeSignature = 0x1d,
167 LCT_SegmentSplitInfo = 0x1e,
168 LCT_FunctionStarts = 0x26,
169 LCT_DataInCode = 0x29,
170 LCT_LinkerOptions = 0x2D
173 /// \brief Load command structure.
179 /// @name Load Command Structures
182 struct SegmentLoadCommand {
190 uint32_t MaxVMProtection;
191 uint32_t InitialVMProtection;
192 uint32_t NumSections;
196 struct Segment64LoadCommand {
204 uint32_t MaxVMProtection;
205 uint32_t InitialVMProtection;
206 uint32_t NumSections;
210 struct SymtabLoadCommand {
213 uint32_t SymbolTableOffset;
214 uint32_t NumSymbolTableEntries;
215 uint32_t StringTableOffset;
216 uint32_t StringTableSize;
219 struct DysymtabLoadCommand {
223 uint32_t LocalSymbolsIndex;
224 uint32_t NumLocalSymbols;
226 uint32_t ExternalSymbolsIndex;
227 uint32_t NumExternalSymbols;
229 uint32_t UndefinedSymbolsIndex;
230 uint32_t NumUndefinedSymbols;
233 uint32_t NumTOCEntries;
235 uint32_t ModuleTableOffset;
236 uint32_t NumModuleTableEntries;
238 uint32_t ReferenceSymbolTableOffset;
239 uint32_t NumReferencedSymbolTableEntries;
241 uint32_t IndirectSymbolTableOffset;
242 uint32_t NumIndirectSymbolTableEntries;
244 uint32_t ExternalRelocationTableOffset;
245 uint32_t NumExternalRelocationTableEntries;
247 uint32_t LocalRelocationTableOffset;
248 uint32_t NumLocalRelocationTableEntries;
251 struct LinkeditDataLoadCommand {
258 struct LinkerOptionsLoadCommand {
262 // Load command is followed by Count number of zero-terminated UTF8 strings,
263 // and then zero-filled to be 4-byte aligned.
267 /// @name Section Data
271 SF_PureInstructions = 0x80000000
276 char SegmentName[16];
281 uint32_t RelocationTableOffset;
282 uint32_t NumRelocationTableEntries;
289 char SegmentName[16];
294 uint32_t RelocationTableOffset;
295 uint32_t NumRelocationTableEntries;
303 /// @name Symbol Table Entries
306 struct SymbolTableEntry {
307 uint32_t StringIndex;
309 uint8_t SectionIndex;
313 // Despite containing a uint64_t, this structure is only 4-byte aligned within
317 struct Symbol64TableEntry {
318 uint32_t StringIndex;
320 uint8_t SectionIndex;
327 /// @name Data-in-code Table Entry
330 // See <mach-o/loader.h>.
331 enum DataRegionType { Data = 1, JumpTable8, JumpTable16, JumpTable32 };
332 struct DataInCodeTableEntry {
333 uint32_t Offset; /* from mach_header to start of data region */
334 uint16_t Length; /* number of bytes in data region */
335 uint16_t Kind; /* a DataRegionType value */
339 /// @name Indirect Symbol Table
342 struct IndirectSymbolTableEntry {
347 /// @name Relocation Data
350 struct RelocationEntry {
357 // See <mach-o/nlist.h>.
358 enum SymbolTypeType {
359 STT_Undefined = 0x00,
364 enum SymbolTypeFlags {
365 // If any of these bits are set, then the entry is a stab entry number (see
366 // <mach-o/stab.h>. Otherwise the other masks apply.
367 STF_StabsEntryMask = 0xe0,
371 STF_PrivateExtern = 0x10
374 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
376 enum IndirectSymbolFlags {
377 ISF_Local = 0x80000000,
378 ISF_Absolute = 0x40000000
381 /// RelocationFlags - Special flags for addresses.
382 enum RelocationFlags {
383 RF_Scattered = 0x80000000
386 /// Common relocation info types.
387 enum RelocationInfoType {
393 /// Generic relocation info types, which are shared by some (but not all)
395 enum RelocationInfoType_Generic {
396 RIT_Generic_PreboundLazyPointer = 3,
397 RIT_Generic_LocalDifference = 4,
401 /// X86_64 uses its own relocation types.
402 enum RelocationInfoTypeX86_64 {
403 // Note that x86_64 doesn't even share the common relocation types.
404 RIT_X86_64_Unsigned = 0,
405 RIT_X86_64_Signed = 1,
406 RIT_X86_64_Branch = 2,
407 RIT_X86_64_GOTLoad = 3,
409 RIT_X86_64_Subtractor = 5,
410 RIT_X86_64_Signed1 = 6,
411 RIT_X86_64_Signed2 = 7,
412 RIT_X86_64_Signed4 = 8,
416 /// ARM uses its own relocation types.
417 enum RelocationInfoTypeARM {
418 RIT_ARM_LocalDifference = 3,
419 RIT_ARM_PreboundLazyPointer = 4,
420 RIT_ARM_Branch24Bit = 5,
421 RIT_ARM_ThumbBranch22Bit = 6,
422 RIT_ARM_ThumbBranch32Bit = 7,
424 RIT_ARM_HalfDifference = 9
428 /// PPC relocation types from <mach-o/ppc/reloc.h>
429 enum RelocationInfoTypePPC {
430 RIT_PPC_BR14 = RIT_Pair +1,
438 RIT_PPC_HI16_SECTDIFF,
439 RIT_PPC_LO16_SECTDIFF,
440 RIT_PPC_HA16_SECTDIFF,
442 RIT_PPC_LO14_SECTDIFF,
443 RIT_PPC_LOCAL_SECTDIFF,
447 } // end namespace macho
449 } // end namespace object
450 } // end namespace llvm