Complete the existing support of ARM v6m, v7m, and v7em, i.e., respectively cortex...
[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     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.
93 namespace macho {
94   /// \brief Constants for structure sizes.
95   enum StructureSizes {
96     Header32Size = 28,
97     Header64Size = 32,
98     SegmentLoadCommand32Size = 56,
99     SegmentLoadCommand64Size = 72,
100     Section32Size = 68,
101     Section64Size = 80,
102     SymtabLoadCommandSize = 24,
103     DysymtabLoadCommandSize = 80,
104     Nlist32Size = 12,
105     Nlist64Size = 16,
106     RelocationInfoSize = 8,
107     LinkeditLoadCommandSize = 16
108   };
109
110   /// \brief Constants for header magic field.
111   enum HeaderMagic {
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
115   };
116
117   /// \brief Header common to all Mach object files.
118   struct Header {
119     uint32_t Magic;
120     uint32_t CPUType;
121     uint32_t CPUSubtype;
122     uint32_t FileType;
123     uint32_t NumLoadCommands;
124     uint32_t SizeOfLoadCommands;
125     uint32_t Flags;
126   };
127
128   /// \brief Extended header for 64-bit object files.
129   struct Header64Ext {
130     uint32_t Reserved;
131   };
132
133   // See <mach-o/loader.h>.
134   enum HeaderFileType {
135     HFT_Object = 0x1
136   };
137
138   enum HeaderFlags {
139     HF_SubsectionsViaSymbols = 0x2000
140   };
141
142   enum LoadCommandType {
143     LCT_Segment = 0x1,
144     LCT_Symtab = 0x2,
145     LCT_Dysymtab = 0xb,
146     LCT_Segment64 = 0x19,
147     LCT_UUID = 0x1b,
148     LCT_CodeSignature = 0x1d,
149     LCT_SegmentSplitInfo = 0x1e,
150     LCT_FunctionStarts = 0x26,
151     LCT_DataInCode = 0x29
152   };
153
154   /// \brief Load command structure.
155   struct LoadCommand {
156     uint32_t Type;
157     uint32_t Size;
158   };
159
160   /// @name Load Command Structures
161   /// @{
162
163   struct SegmentLoadCommand {
164     uint32_t Type;
165     uint32_t Size;
166     char Name[16];
167     uint32_t VMAddress;
168     uint32_t VMSize;
169     uint32_t FileOffset;
170     uint32_t FileSize;
171     uint32_t MaxVMProtection;
172     uint32_t InitialVMProtection;
173     uint32_t NumSections;
174     uint32_t Flags;
175   };
176
177   struct Segment64LoadCommand {
178     uint32_t Type;
179     uint32_t Size;
180     char Name[16];
181     uint64_t VMAddress;
182     uint64_t VMSize;
183     uint64_t FileOffset;
184     uint64_t FileSize;
185     uint32_t MaxVMProtection;
186     uint32_t InitialVMProtection;
187     uint32_t NumSections;
188     uint32_t Flags;
189   };
190
191   struct SymtabLoadCommand {
192     uint32_t Type;
193     uint32_t Size;
194     uint32_t SymbolTableOffset;
195     uint32_t NumSymbolTableEntries;
196     uint32_t StringTableOffset;
197     uint32_t StringTableSize;
198   };
199
200   struct DysymtabLoadCommand {
201     uint32_t Type;
202     uint32_t Size;
203
204     uint32_t LocalSymbolsIndex;
205     uint32_t NumLocalSymbols;
206
207     uint32_t ExternalSymbolsIndex;
208     uint32_t NumExternalSymbols;
209
210     uint32_t UndefinedSymbolsIndex;
211     uint32_t NumUndefinedSymbols;
212
213     uint32_t TOCOffset;
214     uint32_t NumTOCEntries;
215
216     uint32_t ModuleTableOffset;
217     uint32_t NumModuleTableEntries;
218
219     uint32_t ReferenceSymbolTableOffset;
220     uint32_t NumReferencedSymbolTableEntries;
221
222     uint32_t IndirectSymbolTableOffset;
223     uint32_t NumIndirectSymbolTableEntries;
224
225     uint32_t ExternalRelocationTableOffset;
226     uint32_t NumExternalRelocationTableEntries;
227
228     uint32_t LocalRelocationTableOffset;
229     uint32_t NumLocalRelocationTableEntries;
230   };
231
232   struct LinkeditDataLoadCommand {
233     uint32_t Type;
234     uint32_t Size;
235     uint32_t DataOffset;
236     uint32_t DataSize;
237   };
238
239   /// @}
240   /// @name Section Data
241   /// @{
242
243   enum SectionFlags {
244     SF_PureInstructions = 0x80000000
245   };
246
247   struct Section {
248     char Name[16];
249     char SegmentName[16];
250     uint32_t Address;
251     uint32_t Size;
252     uint32_t Offset;
253     uint32_t Align;
254     uint32_t RelocationTableOffset;
255     uint32_t NumRelocationTableEntries;
256     uint32_t Flags;
257     uint32_t Reserved1;
258     uint32_t Reserved2;
259   };
260   struct Section64 {
261     char Name[16];
262     char SegmentName[16];
263     uint64_t Address;
264     uint64_t Size;
265     uint32_t Offset;
266     uint32_t Align;
267     uint32_t RelocationTableOffset;
268     uint32_t NumRelocationTableEntries;
269     uint32_t Flags;
270     uint32_t Reserved1;
271     uint32_t Reserved2;
272     uint32_t Reserved3;
273   };
274
275   /// @}
276   /// @name Symbol Table Entries
277   /// @{
278
279   struct SymbolTableEntry {
280     uint32_t StringIndex;
281     uint8_t Type;
282     uint8_t SectionIndex;
283     uint16_t Flags;
284     uint32_t Value;
285   };
286   // Despite containing a uint64_t, this structure is only 4-byte aligned within
287   // a MachO file.
288 #pragma pack(push)
289 #pragma pack(4)
290   struct Symbol64TableEntry {
291     uint32_t StringIndex;
292     uint8_t Type;
293     uint8_t SectionIndex;
294     uint16_t Flags;
295     uint64_t Value;
296   };
297 #pragma pack(pop)
298
299   /// @}
300   /// @name Data-in-code Table Entry
301   /// @{
302
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  */
309   };
310
311   /// @}
312   /// @name Indirect Symbol Table
313   /// @{
314
315   struct IndirectSymbolTableEntry {
316     uint32_t Index;
317   };
318
319   /// @}
320   /// @name Relocation Data
321   /// @{
322
323   struct RelocationEntry {
324     uint32_t Word0;
325     uint32_t Word1;
326   };
327
328   /// @}
329
330   // See <mach-o/nlist.h>.
331   enum SymbolTypeType {
332     STT_Undefined = 0x00,
333     STT_Absolute  = 0x02,
334     STT_Section   = 0x0e
335   };
336
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,
341
342     STF_TypeMask       = 0x0e,
343     STF_External       = 0x01,
344     STF_PrivateExtern  = 0x10
345   };
346
347   /// IndirectSymbolFlags - Flags for encoding special values in the indirect
348   /// symbol entry.
349   enum IndirectSymbolFlags {
350     ISF_Local    = 0x80000000,
351     ISF_Absolute = 0x40000000
352   };
353
354   /// RelocationFlags - Special flags for addresses.
355   enum RelocationFlags {
356     RF_Scattered = 0x80000000
357   };
358
359   /// Common relocation info types.
360   enum RelocationInfoType {
361     RIT_Vanilla             = 0,
362     RIT_Pair                = 1,
363     RIT_Difference          = 2
364   };
365
366   /// Generic relocation info types, which are shared by some (but not all)
367   /// platforms.
368   enum RelocationInfoType_Generic {
369     RIT_Generic_PreboundLazyPointer = 3,
370     RIT_Generic_LocalDifference     = 4,
371     RIT_Generic_TLV                 = 5
372   };
373
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,
381     RIT_X86_64_GOT        = 4,
382     RIT_X86_64_Subtractor = 5,
383     RIT_X86_64_Signed1    = 6,
384     RIT_X86_64_Signed2    = 7,
385     RIT_X86_64_Signed4    = 8,
386     RIT_X86_64_TLV        = 9
387   };
388
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,
396     RIT_ARM_Half = 8,
397     RIT_ARM_HalfDifference = 9
398
399   };
400
401 } // end namespace macho
402
403 } // end namespace object
404 } // end namespace llvm
405
406 #endif