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.
70 /// \brief PowerPC Machine Subtypes.
71 enum CPUSubtypePowerPC {
75 /// \brief SPARC Machine Subtypes.
76 enum CPUSubtypeSPARC {
80 /// \brief x86 Machine Subtypes.
87 } // end namespace mach
89 /// Format information for Mach object files.
91 /// \brief Constants for structure sizes.
95 SegmentLoadCommand32Size = 56,
96 SegmentLoadCommand64Size = 72,
99 SymtabLoadCommandSize = 24,
100 DysymtabLoadCommandSize = 80,
103 RelocationInfoSize = 8,
104 LinkeditLoadCommandSize = 16
107 /// \brief Constants for header magic field.
109 HM_Object32 = 0xFEEDFACE, ///< 32-bit mach object file
110 HM_Object64 = 0xFEEDFACF, ///< 64-bit mach object file
111 HM_Universal = 0xCAFEBABE ///< Universal object file
114 /// \brief Header common to all Mach object files.
120 uint32_t NumLoadCommands;
121 uint32_t SizeOfLoadCommands;
125 /// \brief Extended header for 64-bit object files.
130 // See <mach-o/loader.h>.
131 enum HeaderFileType {
136 HF_SubsectionsViaSymbols = 0x2000
139 enum LoadCommandType {
143 LCT_Segment64 = 0x19,
145 LCT_CodeSignature = 0x1d,
146 LCT_SegmentSplitInfo = 0x1e,
147 LCT_FunctionStarts = 0x26,
148 LCT_DataInCode = 0x29
151 /// \brief Load command structure.
157 /// @name Load Command Structures
160 struct SegmentLoadCommand {
168 uint32_t MaxVMProtection;
169 uint32_t InitialVMProtection;
170 uint32_t NumSections;
174 struct Segment64LoadCommand {
182 uint32_t MaxVMProtection;
183 uint32_t InitialVMProtection;
184 uint32_t NumSections;
188 struct SymtabLoadCommand {
191 uint32_t SymbolTableOffset;
192 uint32_t NumSymbolTableEntries;
193 uint32_t StringTableOffset;
194 uint32_t StringTableSize;
197 struct DysymtabLoadCommand {
201 uint32_t LocalSymbolsIndex;
202 uint32_t NumLocalSymbols;
204 uint32_t ExternalSymbolsIndex;
205 uint32_t NumExternalSymbols;
207 uint32_t UndefinedSymbolsIndex;
208 uint32_t NumUndefinedSymbols;
211 uint32_t NumTOCEntries;
213 uint32_t ModuleTableOffset;
214 uint32_t NumModuleTableEntries;
216 uint32_t ReferenceSymbolTableOffset;
217 uint32_t NumReferencedSymbolTableEntries;
219 uint32_t IndirectSymbolTableOffset;
220 uint32_t NumIndirectSymbolTableEntries;
222 uint32_t ExternalRelocationTableOffset;
223 uint32_t NumExternalRelocationTableEntries;
225 uint32_t LocalRelocationTableOffset;
226 uint32_t NumLocalRelocationTableEntries;
229 struct LinkeditDataLoadCommand {
237 /// @name Section Data
242 char SegmentName[16];
247 uint32_t RelocationTableOffset;
248 uint32_t NumRelocationTableEntries;
255 char SegmentName[16];
260 uint32_t RelocationTableOffset;
261 uint32_t NumRelocationTableEntries;
269 /// @name Symbol Table Entries
272 struct SymbolTableEntry {
273 uint32_t StringIndex;
275 uint8_t SectionIndex;
279 // Despite containing a uint64_t, this structure is only 4-byte aligned within
283 struct Symbol64TableEntry {
284 uint32_t StringIndex;
286 uint8_t SectionIndex;
293 /// @name Data-in-code Table Entry
296 // See <mach-o/loader.h>.
297 enum DataRegionType { Data = 1, JumpTable8, JumpTable16, JumpTable32 };
298 struct DataInCodeTableEntry {
299 uint32_t Offset; /* from mach_header to start of data region */
300 uint16_t Length; /* number of bytes in data region */
301 uint16_t Kind; /* a DataRegionType value */
305 /// @name Indirect Symbol Table
308 struct IndirectSymbolTableEntry {
313 /// @name Relocation Data
316 struct RelocationEntry {
323 // See <mach-o/nlist.h>.
324 enum SymbolTypeType {
325 STT_Undefined = 0x00,
330 enum SymbolTypeFlags {
331 // If any of these bits are set, then the entry is a stab entry number (see
332 // <mach-o/stab.h>. Otherwise the other masks apply.
333 STF_StabsEntryMask = 0xe0,
337 STF_PrivateExtern = 0x10
340 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
342 enum IndirectSymbolFlags {
343 ISF_Local = 0x80000000,
344 ISF_Absolute = 0x40000000
347 /// RelocationFlags - Special flags for addresses.
348 enum RelocationFlags {
349 RF_Scattered = 0x80000000
352 /// Common relocation info types.
353 enum RelocationInfoType {
359 /// Generic relocation info types, which are shared by some (but not all)
361 enum RelocationInfoType_Generic {
362 RIT_Generic_PreboundLazyPointer = 3,
363 RIT_Generic_LocalDifference = 4,
367 /// X86_64 uses its own relocation types.
368 enum RelocationInfoTypeX86_64 {
369 // Note that x86_64 doesn't even share the common relocation types.
370 RIT_X86_64_Unsigned = 0,
371 RIT_X86_64_Signed = 1,
372 RIT_X86_64_Branch = 2,
373 RIT_X86_64_GOTLoad = 3,
375 RIT_X86_64_Subtractor = 5,
376 RIT_X86_64_Signed1 = 6,
377 RIT_X86_64_Signed2 = 7,
378 RIT_X86_64_Signed4 = 8,
382 /// ARM uses its own relocation types.
383 enum RelocationInfoTypeARM {
384 RIT_ARM_LocalDifference = 3,
385 RIT_ARM_PreboundLazyPointer = 4,
386 RIT_ARM_Branch24Bit = 5,
387 RIT_ARM_ThumbBranch22Bit = 6,
388 RIT_ARM_ThumbBranch32Bit = 7,
390 RIT_ARM_HalfDifference = 9
394 } // end namespace macho
396 } // end namespace object
397 } // end namespace llvm