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