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.
67 /// \brief PowerPC Machine Subtypes.
68 enum CPUSubtypePowerPC {
72 /// \brief SPARC Machine Subtypes.
73 enum CPUSubtypeSPARC {
77 /// \brief x86 Machine Subtypes.
84 } // end namespace mach
86 /// Format information for Mach object files.
88 /// \brief Constants for structure sizes.
92 SegmentLoadCommand32Size = 56,
93 SegmentLoadCommand64Size = 72,
96 SymtabLoadCommandSize = 24,
97 DysymtabLoadCommandSize = 80,
100 RelocationInfoSize = 8,
101 LinkeditLoadCommandSize = 16
104 /// \brief Constants for header magic field.
106 HM_Object32 = 0xFEEDFACE, ///< 32-bit mach object file
107 HM_Object64 = 0xFEEDFACF, ///< 64-bit mach object file
108 HM_Universal = 0xCAFEBABE ///< Universal object file
111 /// \brief Header common to all Mach object files.
117 uint32_t NumLoadCommands;
118 uint32_t SizeOfLoadCommands;
122 /// \brief Extended header for 64-bit object files.
127 // See <mach-o/loader.h>.
128 enum HeaderFileType {
133 HF_SubsectionsViaSymbols = 0x2000
136 enum LoadCommandType {
140 LCT_Segment64 = 0x19,
142 LCT_CodeSignature = 0x1d,
143 LCT_SegmentSplitInfo = 0x1e,
144 LCT_FunctionStarts = 0x26,
145 LCT_DataInCode = 0x29
148 /// \brief Load command structure.
154 /// @name Load Command Structures
157 struct SegmentLoadCommand {
165 uint32_t MaxVMProtection;
166 uint32_t InitialVMProtection;
167 uint32_t NumSections;
171 struct Segment64LoadCommand {
179 uint32_t MaxVMProtection;
180 uint32_t InitialVMProtection;
181 uint32_t NumSections;
185 struct SymtabLoadCommand {
188 uint32_t SymbolTableOffset;
189 uint32_t NumSymbolTableEntries;
190 uint32_t StringTableOffset;
191 uint32_t StringTableSize;
194 struct DysymtabLoadCommand {
198 uint32_t LocalSymbolsIndex;
199 uint32_t NumLocalSymbols;
201 uint32_t ExternalSymbolsIndex;
202 uint32_t NumExternalSymbols;
204 uint32_t UndefinedSymbolsIndex;
205 uint32_t NumUndefinedSymbols;
208 uint32_t NumTOCEntries;
210 uint32_t ModuleTableOffset;
211 uint32_t NumModuleTableEntries;
213 uint32_t ReferenceSymbolTableOffset;
214 uint32_t NumReferencedSymbolTableEntries;
216 uint32_t IndirectSymbolTableOffset;
217 uint32_t NumIndirectSymbolTableEntries;
219 uint32_t ExternalRelocationTableOffset;
220 uint32_t NumExternalRelocationTableEntries;
222 uint32_t LocalRelocationTableOffset;
223 uint32_t NumLocalRelocationTableEntries;
226 struct LinkeditDataLoadCommand {
234 /// @name Section Data
239 char SegmentName[16];
244 uint32_t RelocationTableOffset;
245 uint32_t NumRelocationTableEntries;
252 char SegmentName[16];
257 uint32_t RelocationTableOffset;
258 uint32_t NumRelocationTableEntries;
266 /// @name Symbol Table Entries
269 struct SymbolTableEntry {
270 uint32_t StringIndex;
272 uint8_t SectionIndex;
276 // Despite containing a uint64_t, this structure is only 4-byte aligned within
280 struct Symbol64TableEntry {
281 uint32_t StringIndex;
283 uint8_t SectionIndex;
290 /// @name Data-in-code Table Entry
293 // See <mach-o/loader.h>.
294 enum DataRegionType { Data = 1, JumpTable8, JumpTable16, JumpTable32 };
295 struct DataInCodeTableEntry {
296 uint32_t Offset; /* from mach_header to start of data region */
297 uint16_t Length; /* number of bytes in data region */
298 uint16_t Kind; /* a DataRegionType value */
302 /// @name Indirect Symbol Table
305 struct IndirectSymbolTableEntry {
310 /// @name Relocation Data
313 struct RelocationEntry {
320 // See <mach-o/nlist.h>.
321 enum SymbolTypeType {
322 STT_Undefined = 0x00,
327 enum SymbolTypeFlags {
328 // If any of these bits are set, then the entry is a stab entry number (see
329 // <mach-o/stab.h>. Otherwise the other masks apply.
330 STF_StabsEntryMask = 0xe0,
334 STF_PrivateExtern = 0x10
337 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
339 enum IndirectSymbolFlags {
340 ISF_Local = 0x80000000,
341 ISF_Absolute = 0x40000000
344 /// RelocationFlags - Special flags for addresses.
345 enum RelocationFlags {
346 RF_Scattered = 0x80000000
349 /// Common relocation info types.
350 enum RelocationInfoType {
356 /// Generic relocation info types, which are shared by some (but not all)
358 enum RelocationInfoType_Generic {
359 RIT_Generic_PreboundLazyPointer = 3,
360 RIT_Generic_LocalDifference = 4,
364 /// X86_64 uses its own relocation types.
365 enum RelocationInfoTypeX86_64 {
366 // Note that x86_64 doesn't even share the common relocation types.
367 RIT_X86_64_Unsigned = 0,
368 RIT_X86_64_Signed = 1,
369 RIT_X86_64_Branch = 2,
370 RIT_X86_64_GOTLoad = 3,
372 RIT_X86_64_Subtractor = 5,
373 RIT_X86_64_Signed1 = 6,
374 RIT_X86_64_Signed2 = 7,
375 RIT_X86_64_Signed4 = 8,
379 /// ARM uses its own relocation types.
380 enum RelocationInfoTypeARM {
381 RIT_ARM_LocalDifference = 3,
382 RIT_ARM_PreboundLazyPointer = 4,
383 RIT_ARM_Branch24Bit = 5,
384 RIT_ARM_ThumbBranch22Bit = 6,
385 RIT_ARM_ThumbBranch32Bit = 7,
387 RIT_ARM_HalfDifference = 9
391 } // end namespace macho
393 } // end namespace object
394 } // end namespace llvm