Remove edis remnant.
[oota-llvm.git] / include / llvm / Object / MachOFormat.h
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
27 namespace llvm {
28 namespace object {
29
30 /// General Mach platform information.
31 namespace 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   };
69
70   /// \brief PowerPC Machine Subtypes.
71   enum CPUSubtypePowerPC {
72     CSPPC_ALL = 0
73   };
74
75   /// \brief SPARC Machine Subtypes.
76   enum CPUSubtypeSPARC {
77     CSSPARC_ALL = 0
78   };
79
80   /// \brief x86 Machine Subtypes.
81   enum CPUSubtypeX86 {
82     CSX86_ALL = 3
83   };
84
85   /// @}
86
87 } // end namespace mach
88
89 /// Format information for Mach object files.
90 namespace macho {
91   /// \brief Constants for structure sizes.
92   enum StructureSizes {
93     Header32Size = 28,
94     Header64Size = 32,
95     SegmentLoadCommand32Size = 56,
96     SegmentLoadCommand64Size = 72,
97     Section32Size = 68,
98     Section64Size = 80,
99     SymtabLoadCommandSize = 24,
100     DysymtabLoadCommandSize = 80,
101     Nlist32Size = 12,
102     Nlist64Size = 16,
103     RelocationInfoSize = 8,
104     LinkeditLoadCommandSize = 16
105   };
106
107   /// \brief Constants for header magic field.
108   enum HeaderMagic {
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
112   };
113
114   /// \brief Header common to all Mach object files.
115   struct Header {
116     uint32_t Magic;
117     uint32_t CPUType;
118     uint32_t CPUSubtype;
119     uint32_t FileType;
120     uint32_t NumLoadCommands;
121     uint32_t SizeOfLoadCommands;
122     uint32_t Flags;
123   };
124
125   /// \brief Extended header for 64-bit object files.
126   struct Header64Ext {
127     uint32_t Reserved;
128   };
129
130   // See <mach-o/loader.h>.
131   enum HeaderFileType {
132     HFT_Object = 0x1
133   };
134
135   enum HeaderFlags {
136     HF_SubsectionsViaSymbols = 0x2000
137   };
138
139   enum LoadCommandType {
140     LCT_Segment = 0x1,
141     LCT_Symtab = 0x2,
142     LCT_Dysymtab = 0xb,
143     LCT_Segment64 = 0x19,
144     LCT_UUID = 0x1b,
145     LCT_CodeSignature = 0x1d,
146     LCT_SegmentSplitInfo = 0x1e,
147     LCT_FunctionStarts = 0x26,
148     LCT_DataInCode = 0x29
149   };
150
151   /// \brief Load command structure.
152   struct LoadCommand {
153     uint32_t Type;
154     uint32_t Size;
155   };
156
157   /// @name Load Command Structures
158   /// @{
159
160   struct SegmentLoadCommand {
161     uint32_t Type;
162     uint32_t Size;
163     char Name[16];
164     uint32_t VMAddress;
165     uint32_t VMSize;
166     uint32_t FileOffset;
167     uint32_t FileSize;
168     uint32_t MaxVMProtection;
169     uint32_t InitialVMProtection;
170     uint32_t NumSections;
171     uint32_t Flags;
172   };
173
174   struct Segment64LoadCommand {
175     uint32_t Type;
176     uint32_t Size;
177     char Name[16];
178     uint64_t VMAddress;
179     uint64_t VMSize;
180     uint64_t FileOffset;
181     uint64_t FileSize;
182     uint32_t MaxVMProtection;
183     uint32_t InitialVMProtection;
184     uint32_t NumSections;
185     uint32_t Flags;
186   };
187
188   struct SymtabLoadCommand {
189     uint32_t Type;
190     uint32_t Size;
191     uint32_t SymbolTableOffset;
192     uint32_t NumSymbolTableEntries;
193     uint32_t StringTableOffset;
194     uint32_t StringTableSize;
195   };
196
197   struct DysymtabLoadCommand {
198     uint32_t Type;
199     uint32_t Size;
200
201     uint32_t LocalSymbolsIndex;
202     uint32_t NumLocalSymbols;
203
204     uint32_t ExternalSymbolsIndex;
205     uint32_t NumExternalSymbols;
206
207     uint32_t UndefinedSymbolsIndex;
208     uint32_t NumUndefinedSymbols;
209
210     uint32_t TOCOffset;
211     uint32_t NumTOCEntries;
212
213     uint32_t ModuleTableOffset;
214     uint32_t NumModuleTableEntries;
215
216     uint32_t ReferenceSymbolTableOffset;
217     uint32_t NumReferencedSymbolTableEntries;
218
219     uint32_t IndirectSymbolTableOffset;
220     uint32_t NumIndirectSymbolTableEntries;
221
222     uint32_t ExternalRelocationTableOffset;
223     uint32_t NumExternalRelocationTableEntries;
224
225     uint32_t LocalRelocationTableOffset;
226     uint32_t NumLocalRelocationTableEntries;
227   };
228
229   struct LinkeditDataLoadCommand {
230     uint32_t Type;
231     uint32_t Size;
232     uint32_t DataOffset;
233     uint32_t DataSize;
234   };
235
236   /// @}
237   /// @name Section Data
238   /// @{
239
240   enum SectionFlags {
241     SF_PureInstructions = 0x80000000
242   };
243
244   struct Section {
245     char Name[16];
246     char SegmentName[16];
247     uint32_t Address;
248     uint32_t Size;
249     uint32_t Offset;
250     uint32_t Align;
251     uint32_t RelocationTableOffset;
252     uint32_t NumRelocationTableEntries;
253     uint32_t Flags;
254     uint32_t Reserved1;
255     uint32_t Reserved2;
256   };
257   struct Section64 {
258     char Name[16];
259     char SegmentName[16];
260     uint64_t Address;
261     uint64_t Size;
262     uint32_t Offset;
263     uint32_t Align;
264     uint32_t RelocationTableOffset;
265     uint32_t NumRelocationTableEntries;
266     uint32_t Flags;
267     uint32_t Reserved1;
268     uint32_t Reserved2;
269     uint32_t Reserved3;
270   };
271
272   /// @}
273   /// @name Symbol Table Entries
274   /// @{
275
276   struct SymbolTableEntry {
277     uint32_t StringIndex;
278     uint8_t Type;
279     uint8_t SectionIndex;
280     uint16_t Flags;
281     uint32_t Value;
282   };
283   // Despite containing a uint64_t, this structure is only 4-byte aligned within
284   // a MachO file.
285 #pragma pack(push)
286 #pragma pack(4)
287   struct Symbol64TableEntry {
288     uint32_t StringIndex;
289     uint8_t Type;
290     uint8_t SectionIndex;
291     uint16_t Flags;
292     uint64_t Value;
293   };
294 #pragma pack(pop)
295
296   /// @}
297   /// @name Data-in-code Table Entry
298   /// @{
299
300   // See <mach-o/loader.h>.
301   enum DataRegionType { Data = 1, JumpTable8, JumpTable16, JumpTable32 };
302   struct DataInCodeTableEntry {
303     uint32_t Offset;  /* from mach_header to start of data region */
304     uint16_t Length;  /* number of bytes in data region */
305     uint16_t Kind;    /* a DataRegionType value  */
306   };
307
308   /// @}
309   /// @name Indirect Symbol Table
310   /// @{
311
312   struct IndirectSymbolTableEntry {
313     uint32_t Index;
314   };
315
316   /// @}
317   /// @name Relocation Data
318   /// @{
319
320   struct RelocationEntry {
321     uint32_t Word0;
322     uint32_t Word1;
323   };
324
325   /// @}
326
327   // See <mach-o/nlist.h>.
328   enum SymbolTypeType {
329     STT_Undefined = 0x00,
330     STT_Absolute  = 0x02,
331     STT_Section   = 0x0e
332   };
333
334   enum SymbolTypeFlags {
335     // If any of these bits are set, then the entry is a stab entry number (see
336     // <mach-o/stab.h>. Otherwise the other masks apply.
337     STF_StabsEntryMask = 0xe0,
338
339     STF_TypeMask       = 0x0e,
340     STF_External       = 0x01,
341     STF_PrivateExtern  = 0x10
342   };
343
344   /// IndirectSymbolFlags - Flags for encoding special values in the indirect
345   /// symbol entry.
346   enum IndirectSymbolFlags {
347     ISF_Local    = 0x80000000,
348     ISF_Absolute = 0x40000000
349   };
350
351   /// RelocationFlags - Special flags for addresses.
352   enum RelocationFlags {
353     RF_Scattered = 0x80000000
354   };
355
356   /// Common relocation info types.
357   enum RelocationInfoType {
358     RIT_Vanilla             = 0,
359     RIT_Pair                = 1,
360     RIT_Difference          = 2
361   };
362
363   /// Generic relocation info types, which are shared by some (but not all)
364   /// platforms.
365   enum RelocationInfoType_Generic {
366     RIT_Generic_PreboundLazyPointer = 3,
367     RIT_Generic_LocalDifference     = 4,
368     RIT_Generic_TLV                 = 5
369   };
370
371   /// X86_64 uses its own relocation types.
372   enum RelocationInfoTypeX86_64 {
373     // Note that x86_64 doesn't even share the common relocation types.
374     RIT_X86_64_Unsigned   = 0,
375     RIT_X86_64_Signed     = 1,
376     RIT_X86_64_Branch     = 2,
377     RIT_X86_64_GOTLoad    = 3,
378     RIT_X86_64_GOT        = 4,
379     RIT_X86_64_Subtractor = 5,
380     RIT_X86_64_Signed1    = 6,
381     RIT_X86_64_Signed2    = 7,
382     RIT_X86_64_Signed4    = 8,
383     RIT_X86_64_TLV        = 9
384   };
385
386   /// ARM uses its own relocation types.
387   enum RelocationInfoTypeARM {
388     RIT_ARM_LocalDifference = 3,
389     RIT_ARM_PreboundLazyPointer = 4,
390     RIT_ARM_Branch24Bit = 5,
391     RIT_ARM_ThumbBranch22Bit = 6,
392     RIT_ARM_ThumbBranch32Bit = 7,
393     RIT_ARM_Half = 8,
394     RIT_ARM_HalfDifference = 9
395
396   };
397
398 } // end namespace macho
399
400 } // end namespace object
401 } // end namespace llvm
402
403 #endif