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.
98 SegmentLoadCommand32Size = 56,
99 SegmentLoadCommand64Size = 72,
102 SymtabLoadCommandSize = 24,
103 DysymtabLoadCommandSize = 80,
106 RelocationInfoSize = 8,
107 LinkeditLoadCommandSize = 16
110 /// \brief Constants for header magic field.
112 HM_Object32 = 0xFEEDFACE, ///< 32-bit mach object file
113 HM_Object64 = 0xFEEDFACF, ///< 64-bit mach object file
114 HM_Universal = 0xCAFEBABE ///< Universal object file
117 /// \brief Header common to all Mach object files.
123 uint32_t NumLoadCommands;
124 uint32_t SizeOfLoadCommands;
128 /// \brief Extended header for 64-bit object files.
133 // See <mach-o/loader.h>.
134 enum HeaderFileType {
139 HF_SubsectionsViaSymbols = 0x2000
142 enum LoadCommandType {
146 LCT_Segment64 = 0x19,
148 LCT_CodeSignature = 0x1d,
149 LCT_SegmentSplitInfo = 0x1e,
150 LCT_FunctionStarts = 0x26,
151 LCT_DataInCode = 0x29
154 /// \brief Load command structure.
160 /// @name Load Command Structures
163 struct SegmentLoadCommand {
171 uint32_t MaxVMProtection;
172 uint32_t InitialVMProtection;
173 uint32_t NumSections;
177 struct Segment64LoadCommand {
185 uint32_t MaxVMProtection;
186 uint32_t InitialVMProtection;
187 uint32_t NumSections;
191 struct SymtabLoadCommand {
194 uint32_t SymbolTableOffset;
195 uint32_t NumSymbolTableEntries;
196 uint32_t StringTableOffset;
197 uint32_t StringTableSize;
200 struct DysymtabLoadCommand {
204 uint32_t LocalSymbolsIndex;
205 uint32_t NumLocalSymbols;
207 uint32_t ExternalSymbolsIndex;
208 uint32_t NumExternalSymbols;
210 uint32_t UndefinedSymbolsIndex;
211 uint32_t NumUndefinedSymbols;
214 uint32_t NumTOCEntries;
216 uint32_t ModuleTableOffset;
217 uint32_t NumModuleTableEntries;
219 uint32_t ReferenceSymbolTableOffset;
220 uint32_t NumReferencedSymbolTableEntries;
222 uint32_t IndirectSymbolTableOffset;
223 uint32_t NumIndirectSymbolTableEntries;
225 uint32_t ExternalRelocationTableOffset;
226 uint32_t NumExternalRelocationTableEntries;
228 uint32_t LocalRelocationTableOffset;
229 uint32_t NumLocalRelocationTableEntries;
232 struct LinkeditDataLoadCommand {
240 /// @name Section Data
244 SF_PureInstructions = 0x80000000
249 char SegmentName[16];
254 uint32_t RelocationTableOffset;
255 uint32_t NumRelocationTableEntries;
262 char SegmentName[16];
267 uint32_t RelocationTableOffset;
268 uint32_t NumRelocationTableEntries;
276 /// @name Symbol Table Entries
279 struct SymbolTableEntry {
280 uint32_t StringIndex;
282 uint8_t SectionIndex;
286 // Despite containing a uint64_t, this structure is only 4-byte aligned within
290 struct Symbol64TableEntry {
291 uint32_t StringIndex;
293 uint8_t SectionIndex;
300 /// @name Data-in-code Table Entry
303 // See <mach-o/loader.h>.
304 enum DataRegionType { Data = 1, JumpTable8, JumpTable16, JumpTable32 };
305 struct DataInCodeTableEntry {
306 uint32_t Offset; /* from mach_header to start of data region */
307 uint16_t Length; /* number of bytes in data region */
308 uint16_t Kind; /* a DataRegionType value */
312 /// @name Indirect Symbol Table
315 struct IndirectSymbolTableEntry {
320 /// @name Relocation Data
323 struct RelocationEntry {
330 // See <mach-o/nlist.h>.
331 enum SymbolTypeType {
332 STT_Undefined = 0x00,
337 enum SymbolTypeFlags {
338 // If any of these bits are set, then the entry is a stab entry number (see
339 // <mach-o/stab.h>. Otherwise the other masks apply.
340 STF_StabsEntryMask = 0xe0,
344 STF_PrivateExtern = 0x10
347 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
349 enum IndirectSymbolFlags {
350 ISF_Local = 0x80000000,
351 ISF_Absolute = 0x40000000
354 /// RelocationFlags - Special flags for addresses.
355 enum RelocationFlags {
356 RF_Scattered = 0x80000000
359 /// Common relocation info types.
360 enum RelocationInfoType {
366 /// Generic relocation info types, which are shared by some (but not all)
368 enum RelocationInfoType_Generic {
369 RIT_Generic_PreboundLazyPointer = 3,
370 RIT_Generic_LocalDifference = 4,
374 /// X86_64 uses its own relocation types.
375 enum RelocationInfoTypeX86_64 {
376 // Note that x86_64 doesn't even share the common relocation types.
377 RIT_X86_64_Unsigned = 0,
378 RIT_X86_64_Signed = 1,
379 RIT_X86_64_Branch = 2,
380 RIT_X86_64_GOTLoad = 3,
382 RIT_X86_64_Subtractor = 5,
383 RIT_X86_64_Signed1 = 6,
384 RIT_X86_64_Signed2 = 7,
385 RIT_X86_64_Signed4 = 8,
389 /// ARM uses its own relocation types.
390 enum RelocationInfoTypeARM {
391 RIT_ARM_LocalDifference = 3,
392 RIT_ARM_PreboundLazyPointer = 4,
393 RIT_ARM_Branch24Bit = 5,
394 RIT_ARM_ThumbBranch22Bit = 6,
395 RIT_ARM_ThumbBranch32Bit = 7,
397 RIT_ARM_HalfDifference = 9
401 } // end namespace macho
403 } // end namespace object
404 } // end namespace llvm