1//===- MachOFormat.h - Mach-O Format Structures And Constants ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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
12// Mach object files.
13//
14// The definitions here are purposely chosen to match the LLVM style as opposed
15// to following the platform specific definition of the format.
16//
17// On a Mach system, see the <mach-o/...> includes for more information, in
18// particular <mach-o/loader.h>.
19//
20//===----------------------------------------------------------------------===//
21
22#ifndef LLVM_OBJECT_MACHOFORMAT_H
23#define LLVM_OBJECT_MACHOFORMAT_H
24
25#include "llvm/Support/DataTypes.h"
26
27namespace llvm {
28namespace object {
29
30/// General Mach platform information.
31namespace mach {
32  /// @name CPU Type and Subtype Information
33  /// {
34
35  /// \brief Capability bits used in CPU type encoding.
36  enum CPUTypeFlagsMask {
37    CTFM_ArchMask =  0xFF000000,
38    CTFM_ArchABI64 = 0x01000000
39  };
40
41  /// \brief Machine type IDs used in CPU type encoding.
42  enum CPUTypeMachine {
43    CTM_i386      = 7,
44    CTM_x86_64    = CTM_i386 | CTFM_ArchABI64,
45    CTM_ARM       = 12,
46    CTM_SPARC     = 14,
47    CTM_PowerPC   = 18,
48    CTM_PowerPC64 = CTM_PowerPC | CTFM_ArchABI64
49  };
50
51  /// \brief Capability bits used in CPU subtype encoding.
52  enum CPUSubtypeFlagsMask {
53    CSFM_SubtypeMask =  0xFF000000,
54    CSFM_SubtypeLib64 = 0x80000000
55  };
56
57  /// \brief ARM Machine Subtypes.
58  enum CPUSubtypeARM {
59    CSARM_ALL    = 0,
60    CSARM_V4T    = 5,
61    CSARM_V6     = 6,
62    CSARM_V5TEJ  = 7,
63    CSARM_XSCALE = 8,
64    CSARM_V7     = 9,
65    CSARM_V7F    = 10,
66    CSARM_V7S    = 11,
67    CSARM_V7K    = 12,
68    CSARM_V6M    = 14,
69    CSARM_V7M    = 15,
70    CSARM_V7EM   = 16
71  };
72
73  /// \brief PowerPC Machine Subtypes.
74  enum CPUSubtypePowerPC {
75    CSPPC_ALL = 0
76  };
77
78  /// \brief SPARC Machine Subtypes.
79  enum CPUSubtypeSPARC {
80    CSSPARC_ALL = 0
81  };
82
83  /// \brief x86 Machine Subtypes.
84  enum CPUSubtypeX86 {
85    CSX86_ALL = 3
86  };
87
88  /// @}
89
90} // end namespace mach
91
92/// Format information for Mach object files.
93namespace macho {
94  /// \brief Constants for structure sizes.
95  enum StructureSizes {
96    Header32Size = 28,
97    Header64Size = 32,
98    FatHeaderSize = 8,
99    FatArchHeaderSize = 20,
100    SegmentLoadCommand32Size = 56,
101    SegmentLoadCommand64Size = 72,
102    Section32Size = 68,
103    Section64Size = 80,
104    SymtabLoadCommandSize = 24,
105    DysymtabLoadCommandSize = 80,
106    Nlist32Size = 12,
107    Nlist64Size = 16,
108    RelocationInfoSize = 8,
109    LinkeditLoadCommandSize = 16
110  };
111
112  /// \brief Constants for header magic field.
113  enum HeaderMagic {
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
117  };
118
119  /// \brief Header common to all Mach object files.
120  struct Header {
121    uint32_t Magic;
122    uint32_t CPUType;
123    uint32_t CPUSubtype;
124    uint32_t FileType;
125    uint32_t NumLoadCommands;
126    uint32_t SizeOfLoadCommands;
127    uint32_t Flags;
128  };
129
130  /// \brief Extended header for 64-bit object files.
131  struct Header64Ext {
132    uint32_t Reserved;
133  };
134
135  /// \brief Header for universal object files.
136  struct FatHeader {
137    uint32_t Magic;
138    uint32_t NumFatArch;
139  };
140
141  /// \brief Header for a single-architecture object file in a
142  /// universal binary.
143  struct FatArchHeader {
144    uint32_t CPUType;
145    uint32_t CPUSubtype;
146    uint32_t Offset;
147    uint32_t Size;
148    uint32_t Align;
149  };
150
151  // See <mach-o/loader.h>.
152  enum HeaderFileType {
153    HFT_Object = 0x1
154  };
155
156  enum HeaderFlags {
157    HF_SubsectionsViaSymbols = 0x2000
158  };
159
160  enum LoadCommandType {
161    LCT_Segment = 0x1,
162    LCT_Symtab = 0x2,
163    LCT_Dysymtab = 0xb,
164    LCT_Segment64 = 0x19,
165    LCT_UUID = 0x1b,
166    LCT_CodeSignature = 0x1d,
167    LCT_SegmentSplitInfo = 0x1e,
168    LCT_FunctionStarts = 0x26,
169    LCT_DataInCode = 0x29,
170    LCT_LinkerOptions = 0x2D
171  };
172
173  /// \brief Load command structure.
174  struct LoadCommand {
175    uint32_t Type;
176    uint32_t Size;
177  };
178
179  /// @name Load Command Structures
180  /// @{
181
182  struct SegmentLoadCommand {
183    uint32_t Type;
184    uint32_t Size;
185    char Name[16];
186    uint32_t VMAddress;
187    uint32_t VMSize;
188    uint32_t FileOffset;
189    uint32_t FileSize;
190    uint32_t MaxVMProtection;
191    uint32_t InitialVMProtection;
192    uint32_t NumSections;
193    uint32_t Flags;
194  };
195
196  struct Segment64LoadCommand {
197    uint32_t Type;
198    uint32_t Size;
199    char Name[16];
200    uint64_t VMAddress;
201    uint64_t VMSize;
202    uint64_t FileOffset;
203    uint64_t FileSize;
204    uint32_t MaxVMProtection;
205    uint32_t InitialVMProtection;
206    uint32_t NumSections;
207    uint32_t Flags;
208  };
209
210  struct SymtabLoadCommand {
211    uint32_t Type;
212    uint32_t Size;
213    uint32_t SymbolTableOffset;
214    uint32_t NumSymbolTableEntries;
215    uint32_t StringTableOffset;
216    uint32_t StringTableSize;
217  };
218
219  struct DysymtabLoadCommand {
220    uint32_t Type;
221    uint32_t Size;
222
223    uint32_t LocalSymbolsIndex;
224    uint32_t NumLocalSymbols;
225
226    uint32_t ExternalSymbolsIndex;
227    uint32_t NumExternalSymbols;
228
229    uint32_t UndefinedSymbolsIndex;
230    uint32_t NumUndefinedSymbols;
231
232    uint32_t TOCOffset;
233    uint32_t NumTOCEntries;
234
235    uint32_t ModuleTableOffset;
236    uint32_t NumModuleTableEntries;
237
238    uint32_t ReferenceSymbolTableOffset;
239    uint32_t NumReferencedSymbolTableEntries;
240
241    uint32_t IndirectSymbolTableOffset;
242    uint32_t NumIndirectSymbolTableEntries;
243
244    uint32_t ExternalRelocationTableOffset;
245    uint32_t NumExternalRelocationTableEntries;
246
247    uint32_t LocalRelocationTableOffset;
248    uint32_t NumLocalRelocationTableEntries;
249  };
250
251  struct LinkeditDataLoadCommand {
252    uint32_t Type;
253    uint32_t Size;
254    uint32_t DataOffset;
255    uint32_t DataSize;
256  };
257
258  struct LinkerOptionsLoadCommand {
259    uint32_t Type;
260    uint32_t Size;
261    uint32_t Count;
262    // Load command is followed by Count number of zero-terminated UTF8 strings,
263    // and then zero-filled to be 4-byte aligned.
264  };
265
266  /// @}
267  /// @name Section Data
268  /// @{
269
270  enum SectionFlags {
271    SF_PureInstructions = 0x80000000
272  };
273
274  struct Section {
275    char Name[16];
276    char SegmentName[16];
277    uint32_t Address;
278    uint32_t Size;
279    uint32_t Offset;
280    uint32_t Align;
281    uint32_t RelocationTableOffset;
282    uint32_t NumRelocationTableEntries;
283    uint32_t Flags;
284    uint32_t Reserved1;
285    uint32_t Reserved2;
286  };
287  struct Section64 {
288    char Name[16];
289    char SegmentName[16];
290    uint64_t Address;
291    uint64_t Size;
292    uint32_t Offset;
293    uint32_t Align;
294    uint32_t RelocationTableOffset;
295    uint32_t NumRelocationTableEntries;
296    uint32_t Flags;
297    uint32_t Reserved1;
298    uint32_t Reserved2;
299    uint32_t Reserved3;
300  };
301
302  /// @}
303  /// @name Symbol Table Entries
304  /// @{
305
306  struct SymbolTableEntry {
307    uint32_t StringIndex;
308    uint8_t Type;
309    uint8_t SectionIndex;
310    uint16_t Flags;
311    uint32_t Value;
312  };
313  // Despite containing a uint64_t, this structure is only 4-byte aligned within
314  // a MachO file.
315#pragma pack(push)
316#pragma pack(4)
317  struct Symbol64TableEntry {
318    uint32_t StringIndex;
319    uint8_t Type;
320    uint8_t SectionIndex;
321    uint16_t Flags;
322    uint64_t Value;
323  };
324#pragma pack(pop)
325
326  /// @}
327  /// @name Data-in-code Table Entry
328  /// @{
329
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  */
336  };
337
338  /// @}
339  /// @name Indirect Symbol Table
340  /// @{
341
342  struct IndirectSymbolTableEntry {
343    uint32_t Index;
344  };
345
346  /// @}
347  /// @name Relocation Data
348  /// @{
349
350  struct RelocationEntry {
351    uint32_t Word0;
352    uint32_t Word1;
353  };
354
355  /// @}
356
357  // See <mach-o/nlist.h>.
358  enum SymbolTypeType {
359    STT_Undefined = 0x00,
360    STT_Absolute  = 0x02,
361    STT_Section   = 0x0e
362  };
363
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,
368
369    STF_TypeMask       = 0x0e,
370    STF_External       = 0x01,
371    STF_PrivateExtern  = 0x10
372  };
373
374  /// IndirectSymbolFlags - Flags for encoding special values in the indirect
375  /// symbol entry.
376  enum IndirectSymbolFlags {
377    ISF_Local    = 0x80000000,
378    ISF_Absolute = 0x40000000
379  };
380
381  /// RelocationFlags - Special flags for addresses.
382  enum RelocationFlags {
383    RF_Scattered = 0x80000000
384  };
385
386  /// Common relocation info types.
387  enum RelocationInfoType {
388    RIT_Vanilla             = 0,
389    RIT_Pair                = 1,
390    RIT_Difference          = 2
391  };
392
393  /// Generic relocation info types, which are shared by some (but not all)
394  /// platforms.
395  enum RelocationInfoType_Generic {
396    RIT_Generic_PreboundLazyPointer = 3,
397    RIT_Generic_LocalDifference     = 4,
398    RIT_Generic_TLV                 = 5
399  };
400
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,
408    RIT_X86_64_GOT        = 4,
409    RIT_X86_64_Subtractor = 5,
410    RIT_X86_64_Signed1    = 6,
411    RIT_X86_64_Signed2    = 7,
412    RIT_X86_64_Signed4    = 8,
413    RIT_X86_64_TLV        = 9
414  };
415
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,
423    RIT_ARM_Half = 8,
424    RIT_ARM_HalfDifference = 9
425
426  };
427
428} // end namespace macho
429
430} // end namespace object
431} // end namespace llvm
432
433#endif
434