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,
152 LCT_LinkerOptions = 0x2D
155 /// \brief Load command structure.
161 /// @name Load Command Structures
164 struct SegmentLoadCommand {
172 uint32_t MaxVMProtection;
173 uint32_t InitialVMProtection;
174 uint32_t NumSections;
178 struct Segment64LoadCommand {
186 uint32_t MaxVMProtection;
187 uint32_t InitialVMProtection;
188 uint32_t NumSections;
192 struct SymtabLoadCommand {
195 uint32_t SymbolTableOffset;
196 uint32_t NumSymbolTableEntries;
197 uint32_t StringTableOffset;
198 uint32_t StringTableSize;
201 struct DysymtabLoadCommand {
205 uint32_t LocalSymbolsIndex;
206 uint32_t NumLocalSymbols;
208 uint32_t ExternalSymbolsIndex;
209 uint32_t NumExternalSymbols;
211 uint32_t UndefinedSymbolsIndex;
212 uint32_t NumUndefinedSymbols;
215 uint32_t NumTOCEntries;
217 uint32_t ModuleTableOffset;
218 uint32_t NumModuleTableEntries;
220 uint32_t ReferenceSymbolTableOffset;
221 uint32_t NumReferencedSymbolTableEntries;
223 uint32_t IndirectSymbolTableOffset;
224 uint32_t NumIndirectSymbolTableEntries;
226 uint32_t ExternalRelocationTableOffset;
227 uint32_t NumExternalRelocationTableEntries;
229 uint32_t LocalRelocationTableOffset;
230 uint32_t NumLocalRelocationTableEntries;
233 struct LinkeditDataLoadCommand {
240 struct LinkerOptionsLoadCommand {
244 // Load command is followed by Count number of zero-terminated UTF8 strings,
245 // and then zero-filled to be 4-byte aligned.
249 /// @name Section Data
253 SF_PureInstructions = 0x80000000
258 char SegmentName[16];
263 uint32_t RelocationTableOffset;
264 uint32_t NumRelocationTableEntries;
271 char SegmentName[16];
276 uint32_t RelocationTableOffset;
277 uint32_t NumRelocationTableEntries;
285 /// @name Symbol Table Entries
288 struct SymbolTableEntry {
289 uint32_t StringIndex;
291 uint8_t SectionIndex;
295 // Despite containing a uint64_t, this structure is only 4-byte aligned within
299 struct Symbol64TableEntry {
300 uint32_t StringIndex;
302 uint8_t SectionIndex;
309 /// @name Data-in-code Table Entry
312 // See <mach-o/loader.h>.
313 enum DataRegionType { Data = 1, JumpTable8, JumpTable16, JumpTable32 };
314 struct DataInCodeTableEntry {
315 uint32_t Offset; /* from mach_header to start of data region */
316 uint16_t Length; /* number of bytes in data region */
317 uint16_t Kind; /* a DataRegionType value */
321 /// @name Indirect Symbol Table
324 struct IndirectSymbolTableEntry {
329 /// @name Relocation Data
332 struct RelocationEntry {
339 // See <mach-o/nlist.h>.
340 enum SymbolTypeType {
341 STT_Undefined = 0x00,
346 enum SymbolTypeFlags {
347 // If any of these bits are set, then the entry is a stab entry number (see
348 // <mach-o/stab.h>. Otherwise the other masks apply.
349 STF_StabsEntryMask = 0xe0,
353 STF_PrivateExtern = 0x10
356 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
358 enum IndirectSymbolFlags {
359 ISF_Local = 0x80000000,
360 ISF_Absolute = 0x40000000
363 /// RelocationFlags - Special flags for addresses.
364 enum RelocationFlags {
365 RF_Scattered = 0x80000000
368 /// Common relocation info types.
369 enum RelocationInfoType {
375 /// Generic relocation info types, which are shared by some (but not all)
377 enum RelocationInfoType_Generic {
378 RIT_Generic_PreboundLazyPointer = 3,
379 RIT_Generic_LocalDifference = 4,
383 /// X86_64 uses its own relocation types.
384 enum RelocationInfoTypeX86_64 {
385 // Note that x86_64 doesn't even share the common relocation types.
386 RIT_X86_64_Unsigned = 0,
387 RIT_X86_64_Signed = 1,
388 RIT_X86_64_Branch = 2,
389 RIT_X86_64_GOTLoad = 3,
391 RIT_X86_64_Subtractor = 5,
392 RIT_X86_64_Signed1 = 6,
393 RIT_X86_64_Signed2 = 7,
394 RIT_X86_64_Signed4 = 8,
398 /// ARM uses its own relocation types.
399 enum RelocationInfoTypeARM {
400 RIT_ARM_LocalDifference = 3,
401 RIT_ARM_PreboundLazyPointer = 4,
402 RIT_ARM_Branch24Bit = 5,
403 RIT_ARM_ThumbBranch22Bit = 6,
404 RIT_ARM_ThumbBranch32Bit = 7,
406 RIT_ARM_HalfDifference = 9
410 } // end namespace macho
412 } // end namespace object
413 } // end namespace llvm