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 // Despite containing a uint64_t, this structure is only 4-byte aligned within 277 // a MachO file. 278 #pragma pack(push) 279 #pragma pack(4) 280 struct Symbol64TableEntry { 281 uint32_t StringIndex; 282 uint8_t Type; 283 uint8_t SectionIndex; 284 uint16_t Flags; 285 uint64_t Value; 286 }; 287 #pragma pack(pop) 288 289 /// @} 290 /// @name Data-in-code Table Entry 291 /// @{ 292 293 // See <mach-o/loader.h>. 294 enum DataRegionType { Data = 1, JumpTable8, JumpTable16, JumpTable32 }; 295 struct DataInCodeTableEntry { 296 uint32_t Offset; /* from mach_header to start of data region */ 297 uint16_t Length; /* number of bytes in data region */ 298 uint16_t Kind; /* a DataRegionType value */ 299 }; 300 301 /// @} 302 /// @name Indirect Symbol Table 303 /// @{ 304 305 struct IndirectSymbolTableEntry { 306 uint32_t Index; 307 }; 308 309 /// @} 310 /// @name Relocation Data 311 /// @{ 312 313 struct RelocationEntry { 314 uint32_t Word0; 315 uint32_t Word1; 316 }; 317 318 /// @} 319 320 // See <mach-o/nlist.h>. 321 enum SymbolTypeType { 322 STT_Undefined = 0x00, 323 STT_Absolute = 0x02, 324 STT_Section = 0x0e 325 }; 326 327 enum SymbolTypeFlags { 328 // If any of these bits are set, then the entry is a stab entry number (see 329 // <mach-o/stab.h>. Otherwise the other masks apply. 330 STF_StabsEntryMask = 0xe0, 331 332 STF_TypeMask = 0x0e, 333 STF_External = 0x01, 334 STF_PrivateExtern = 0x10 335 }; 336 337 /// IndirectSymbolFlags - Flags for encoding special values in the indirect 338 /// symbol entry. 339 enum IndirectSymbolFlags { 340 ISF_Local = 0x80000000, 341 ISF_Absolute = 0x40000000 342 }; 343 344 /// RelocationFlags - Special flags for addresses. 345 enum RelocationFlags { 346 RF_Scattered = 0x80000000 347 }; 348 349 /// Common relocation info types. 350 enum RelocationInfoType { 351 RIT_Vanilla = 0, 352 RIT_Pair = 1, 353 RIT_Difference = 2 354 }; 355 356 /// Generic relocation info types, which are shared by some (but not all) 357 /// platforms. 358 enum RelocationInfoType_Generic { 359 RIT_Generic_PreboundLazyPointer = 3, 360 RIT_Generic_LocalDifference = 4, 361 RIT_Generic_TLV = 5 362 }; 363 364 /// X86_64 uses its own relocation types. 365 enum RelocationInfoTypeX86_64 { 366 // Note that x86_64 doesn't even share the common relocation types. 367 RIT_X86_64_Unsigned = 0, 368 RIT_X86_64_Signed = 1, 369 RIT_X86_64_Branch = 2, 370 RIT_X86_64_GOTLoad = 3, 371 RIT_X86_64_GOT = 4, 372 RIT_X86_64_Subtractor = 5, 373 RIT_X86_64_Signed1 = 6, 374 RIT_X86_64_Signed2 = 7, 375 RIT_X86_64_Signed4 = 8, 376 RIT_X86_64_TLV = 9 377 }; 378 379 /// ARM uses its own relocation types. 380 enum RelocationInfoTypeARM { 381 RIT_ARM_LocalDifference = 3, 382 RIT_ARM_PreboundLazyPointer = 4, 383 RIT_ARM_Branch24Bit = 5, 384 RIT_ARM_ThumbBranch22Bit = 6, 385 RIT_ARM_ThumbBranch32Bit = 7, 386 RIT_ARM_Half = 8, 387 RIT_ARM_HalfDifference = 9 388 389 }; 390 391 } // end namespace macho 392 393 } // end namespace object 394 } // end namespace llvm 395 396 #endif 397