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 LCT_LinkerOptions = 0x2D 153 }; 154 155 /// \brief Load command structure. 156 struct LoadCommand { 157 uint32_t Type; 158 uint32_t Size; 159 }; 160 161 /// @name Load Command Structures 162 /// @{ 163 164 struct SegmentLoadCommand { 165 uint32_t Type; 166 uint32_t Size; 167 char Name[16]; 168 uint32_t VMAddress; 169 uint32_t VMSize; 170 uint32_t FileOffset; 171 uint32_t FileSize; 172 uint32_t MaxVMProtection; 173 uint32_t InitialVMProtection; 174 uint32_t NumSections; 175 uint32_t Flags; 176 }; 177 178 struct Segment64LoadCommand { 179 uint32_t Type; 180 uint32_t Size; 181 char Name[16]; 182 uint64_t VMAddress; 183 uint64_t VMSize; 184 uint64_t FileOffset; 185 uint64_t FileSize; 186 uint32_t MaxVMProtection; 187 uint32_t InitialVMProtection; 188 uint32_t NumSections; 189 uint32_t Flags; 190 }; 191 192 struct SymtabLoadCommand { 193 uint32_t Type; 194 uint32_t Size; 195 uint32_t SymbolTableOffset; 196 uint32_t NumSymbolTableEntries; 197 uint32_t StringTableOffset; 198 uint32_t StringTableSize; 199 }; 200 201 struct DysymtabLoadCommand { 202 uint32_t Type; 203 uint32_t Size; 204 205 uint32_t LocalSymbolsIndex; 206 uint32_t NumLocalSymbols; 207 208 uint32_t ExternalSymbolsIndex; 209 uint32_t NumExternalSymbols; 210 211 uint32_t UndefinedSymbolsIndex; 212 uint32_t NumUndefinedSymbols; 213 214 uint32_t TOCOffset; 215 uint32_t NumTOCEntries; 216 217 uint32_t ModuleTableOffset; 218 uint32_t NumModuleTableEntries; 219 220 uint32_t ReferenceSymbolTableOffset; 221 uint32_t NumReferencedSymbolTableEntries; 222 223 uint32_t IndirectSymbolTableOffset; 224 uint32_t NumIndirectSymbolTableEntries; 225 226 uint32_t ExternalRelocationTableOffset; 227 uint32_t NumExternalRelocationTableEntries; 228 229 uint32_t LocalRelocationTableOffset; 230 uint32_t NumLocalRelocationTableEntries; 231 }; 232 233 struct LinkeditDataLoadCommand { 234 uint32_t Type; 235 uint32_t Size; 236 uint32_t DataOffset; 237 uint32_t DataSize; 238 }; 239 240 struct LinkerOptionsLoadCommand { 241 uint32_t Type; 242 uint32_t Size; 243 uint32_t Count; 244 // Load command is followed by Count number of zero-terminated UTF8 strings, 245 // and then zero-filled to be 4-byte aligned. 246 }; 247 248 /// @} 249 /// @name Section Data 250 /// @{ 251 252 enum SectionFlags { 253 SF_PureInstructions = 0x80000000 254 }; 255 256 struct Section { 257 char Name[16]; 258 char SegmentName[16]; 259 uint32_t Address; 260 uint32_t Size; 261 uint32_t Offset; 262 uint32_t Align; 263 uint32_t RelocationTableOffset; 264 uint32_t NumRelocationTableEntries; 265 uint32_t Flags; 266 uint32_t Reserved1; 267 uint32_t Reserved2; 268 }; 269 struct Section64 { 270 char Name[16]; 271 char SegmentName[16]; 272 uint64_t Address; 273 uint64_t Size; 274 uint32_t Offset; 275 uint32_t Align; 276 uint32_t RelocationTableOffset; 277 uint32_t NumRelocationTableEntries; 278 uint32_t Flags; 279 uint32_t Reserved1; 280 uint32_t Reserved2; 281 uint32_t Reserved3; 282 }; 283 284 /// @} 285 /// @name Symbol Table Entries 286 /// @{ 287 288 struct SymbolTableEntry { 289 uint32_t StringIndex; 290 uint8_t Type; 291 uint8_t SectionIndex; 292 uint16_t Flags; 293 uint32_t Value; 294 }; 295 // Despite containing a uint64_t, this structure is only 4-byte aligned within 296 // a MachO file. 297 #pragma pack(push) 298 #pragma pack(4) 299 struct Symbol64TableEntry { 300 uint32_t StringIndex; 301 uint8_t Type; 302 uint8_t SectionIndex; 303 uint16_t Flags; 304 uint64_t Value; 305 }; 306 #pragma pack(pop) 307 308 /// @} 309 /// @name Data-in-code Table Entry 310 /// @{ 311 312 // See <mach-o/loader.h>. 313 enum DataRegionType { Data = 1, JumpTable8, JumpTable16, JumpTable32 }; 314 struct DataInCodeTableEntry { 315 uint32_t Offset; /* from mach_header to start of data region */ 316 uint16_t Length; /* number of bytes in data region */ 317 uint16_t Kind; /* a DataRegionType value */ 318 }; 319 320 /// @} 321 /// @name Indirect Symbol Table 322 /// @{ 323 324 struct IndirectSymbolTableEntry { 325 uint32_t Index; 326 }; 327 328 /// @} 329 /// @name Relocation Data 330 /// @{ 331 332 struct RelocationEntry { 333 uint32_t Word0; 334 uint32_t Word1; 335 }; 336 337 /// @} 338 339 // See <mach-o/nlist.h>. 340 enum SymbolTypeType { 341 STT_Undefined = 0x00, 342 STT_Absolute = 0x02, 343 STT_Section = 0x0e 344 }; 345 346 enum SymbolTypeFlags { 347 // If any of these bits are set, then the entry is a stab entry number (see 348 // <mach-o/stab.h>. Otherwise the other masks apply. 349 STF_StabsEntryMask = 0xe0, 350 351 STF_TypeMask = 0x0e, 352 STF_External = 0x01, 353 STF_PrivateExtern = 0x10 354 }; 355 356 /// IndirectSymbolFlags - Flags for encoding special values in the indirect 357 /// symbol entry. 358 enum IndirectSymbolFlags { 359 ISF_Local = 0x80000000, 360 ISF_Absolute = 0x40000000 361 }; 362 363 /// RelocationFlags - Special flags for addresses. 364 enum RelocationFlags { 365 RF_Scattered = 0x80000000 366 }; 367 368 /// Common relocation info types. 369 enum RelocationInfoType { 370 RIT_Vanilla = 0, 371 RIT_Pair = 1, 372 RIT_Difference = 2 373 }; 374 375 /// Generic relocation info types, which are shared by some (but not all) 376 /// platforms. 377 enum RelocationInfoType_Generic { 378 RIT_Generic_PreboundLazyPointer = 3, 379 RIT_Generic_LocalDifference = 4, 380 RIT_Generic_TLV = 5 381 }; 382 383 /// X86_64 uses its own relocation types. 384 enum RelocationInfoTypeX86_64 { 385 // Note that x86_64 doesn't even share the common relocation types. 386 RIT_X86_64_Unsigned = 0, 387 RIT_X86_64_Signed = 1, 388 RIT_X86_64_Branch = 2, 389 RIT_X86_64_GOTLoad = 3, 390 RIT_X86_64_GOT = 4, 391 RIT_X86_64_Subtractor = 5, 392 RIT_X86_64_Signed1 = 6, 393 RIT_X86_64_Signed2 = 7, 394 RIT_X86_64_Signed4 = 8, 395 RIT_X86_64_TLV = 9 396 }; 397 398 /// ARM uses its own relocation types. 399 enum RelocationInfoTypeARM { 400 RIT_ARM_LocalDifference = 3, 401 RIT_ARM_PreboundLazyPointer = 4, 402 RIT_ARM_Branch24Bit = 5, 403 RIT_ARM_ThumbBranch22Bit = 6, 404 RIT_ARM_ThumbBranch32Bit = 7, 405 RIT_ARM_Half = 8, 406 RIT_ARM_HalfDifference = 9 407 408 }; 409 410 } // end namespace macho 411 412 } // end namespace object 413 } // end namespace llvm 414 415 #endif 416