Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

415 lines
9.5 KiB

  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. #ifndef LLVM_OBJECT_MACHOFORMAT_H
  22. #define LLVM_OBJECT_MACHOFORMAT_H
  23. #include "llvm/Support/DataTypes.h"
  24. namespace llvm {
  25. namespace object {
  26. /// General Mach platform information.
  27. namespace mach {
  28. /// @name CPU Type and Subtype Information
  29. /// {
  30. /// \brief Capability bits used in CPU type encoding.
  31. enum CPUTypeFlagsMask {
  32. CTFM_ArchMask = 0xFF000000,
  33. CTFM_ArchABI64 = 0x01000000
  34. };
  35. /// \brief Machine type IDs used in CPU type encoding.
  36. enum CPUTypeMachine {
  37. CTM_i386 = 7,
  38. CTM_x86_64 = CTM_i386 | CTFM_ArchABI64,
  39. CTM_ARM = 12,
  40. CTM_SPARC = 14,
  41. CTM_PowerPC = 18,
  42. CTM_PowerPC64 = CTM_PowerPC | CTFM_ArchABI64
  43. };
  44. /// \brief Capability bits used in CPU subtype encoding.
  45. enum CPUSubtypeFlagsMask {
  46. CSFM_SubtypeMask = 0xFF000000,
  47. CSFM_SubtypeLib64 = 0x80000000
  48. };
  49. /// \brief ARM Machine Subtypes.
  50. enum CPUSubtypeARM {
  51. CSARM_ALL = 0,
  52. CSARM_V4T = 5,
  53. CSARM_V6 = 6,
  54. CSARM_V5TEJ = 7,
  55. CSARM_XSCALE = 8,
  56. CSARM_V7 = 9,
  57. CSARM_V7F = 10,
  58. CSARM_V7S = 11,
  59. CSARM_V7K = 12,
  60. CSARM_V6M = 14,
  61. CSARM_V7M = 15,
  62. CSARM_V7EM = 16
  63. };
  64. /// \brief PowerPC Machine Subtypes.
  65. enum CPUSubtypePowerPC {
  66. CSPPC_ALL = 0
  67. };
  68. /// \brief SPARC Machine Subtypes.
  69. enum CPUSubtypeSPARC {
  70. CSSPARC_ALL = 0
  71. };
  72. /// \brief x86 Machine Subtypes.
  73. enum CPUSubtypeX86 {
  74. CSX86_ALL = 3
  75. };
  76. /// @}
  77. } // end namespace mach
  78. /// Format information for Mach object files.
  79. namespace macho {
  80. /// \brief Constants for structure sizes.
  81. enum StructureSizes {
  82. Header32Size = 28,
  83. Header64Size = 32,
  84. SegmentLoadCommand32Size = 56,
  85. SegmentLoadCommand64Size = 72,
  86. Section32Size = 68,
  87. Section64Size = 80,
  88. SymtabLoadCommandSize = 24,
  89. DysymtabLoadCommandSize = 80,
  90. Nlist32Size = 12,
  91. Nlist64Size = 16,
  92. RelocationInfoSize = 8,
  93. LinkeditLoadCommandSize = 16
  94. };
  95. /// \brief Constants for header magic field.
  96. enum HeaderMagic {
  97. HM_Object32 = 0xFEEDFACE, ///< 32-bit mach object file
  98. HM_Object64 = 0xFEEDFACF, ///< 64-bit mach object file
  99. HM_Universal = 0xCAFEBABE ///< Universal object file
  100. };
  101. /// \brief Header common to all Mach object files.
  102. struct Header {
  103. uint32_t Magic;
  104. uint32_t CPUType;
  105. uint32_t CPUSubtype;
  106. uint32_t FileType;
  107. uint32_t NumLoadCommands;
  108. uint32_t SizeOfLoadCommands;
  109. uint32_t Flags;
  110. };
  111. /// \brief Extended header for 64-bit object files.
  112. struct Header64Ext {
  113. uint32_t Reserved;
  114. };
  115. // See <mach-o/loader.h>.
  116. enum HeaderFileType {
  117. HFT_Object = 0x1
  118. };
  119. enum HeaderFlags {
  120. HF_SubsectionsViaSymbols = 0x2000
  121. };
  122. enum LoadCommandType {
  123. LCT_Segment = 0x1,
  124. LCT_Symtab = 0x2,
  125. LCT_Dysymtab = 0xb,
  126. LCT_Segment64 = 0x19,
  127. LCT_UUID = 0x1b,
  128. LCT_CodeSignature = 0x1d,
  129. LCT_SegmentSplitInfo = 0x1e,
  130. LCT_FunctionStarts = 0x26,
  131. LCT_DataInCode = 0x29,
  132. LCT_LinkerOptions = 0x2D
  133. };
  134. /// \brief Load command structure.
  135. struct LoadCommand {
  136. uint32_t Type;
  137. uint32_t Size;
  138. };
  139. /// @name Load Command Structures
  140. /// @{
  141. struct SegmentLoadCommand {
  142. uint32_t Type;
  143. uint32_t Size;
  144. char Name[16];
  145. uint32_t VMAddress;
  146. uint32_t VMSize;
  147. uint32_t FileOffset;
  148. uint32_t FileSize;
  149. uint32_t MaxVMProtection;
  150. uint32_t InitialVMProtection;
  151. uint32_t NumSections;
  152. uint32_t Flags;
  153. };
  154. struct Segment64LoadCommand {
  155. uint32_t Type;
  156. uint32_t Size;
  157. char Name[16];
  158. uint64_t VMAddress;
  159. uint64_t VMSize;
  160. uint64_t FileOffset;
  161. uint64_t FileSize;
  162. uint32_t MaxVMProtection;
  163. uint32_t InitialVMProtection;
  164. uint32_t NumSections;
  165. uint32_t Flags;
  166. };
  167. struct SymtabLoadCommand {
  168. uint32_t Type;
  169. uint32_t Size;
  170. uint32_t SymbolTableOffset;
  171. uint32_t NumSymbolTableEntries;
  172. uint32_t StringTableOffset;
  173. uint32_t StringTableSize;
  174. };
  175. struct DysymtabLoadCommand {
  176. uint32_t Type;
  177. uint32_t Size;
  178. uint32_t LocalSymbolsIndex;
  179. uint32_t NumLocalSymbols;
  180. uint32_t ExternalSymbolsIndex;
  181. uint32_t NumExternalSymbols;
  182. uint32_t UndefinedSymbolsIndex;
  183. uint32_t NumUndefinedSymbols;
  184. uint32_t TOCOffset;
  185. uint32_t NumTOCEntries;
  186. uint32_t ModuleTableOffset;
  187. uint32_t NumModuleTableEntries;
  188. uint32_t ReferenceSymbolTableOffset;
  189. uint32_t NumReferencedSymbolTableEntries;
  190. uint32_t IndirectSymbolTableOffset;
  191. uint32_t NumIndirectSymbolTableEntries;
  192. uint32_t ExternalRelocationTableOffset;
  193. uint32_t NumExternalRelocationTableEntries;
  194. uint32_t LocalRelocationTableOffset;
  195. uint32_t NumLocalRelocationTableEntries;
  196. };
  197. struct LinkeditDataLoadCommand {
  198. uint32_t Type;
  199. uint32_t Size;
  200. uint32_t DataOffset;
  201. uint32_t DataSize;
  202. };
  203. struct LinkerOptionsLoadCommand {
  204. uint32_t Type;
  205. uint32_t Size;
  206. uint32_t Count;
  207. // Load command is followed by Count number of zero-terminated UTF8 strings,
  208. // and then zero-filled to be 4-byte aligned.
  209. };
  210. /// @}
  211. /// @name Section Data
  212. /// @{
  213. enum SectionFlags {
  214. SF_PureInstructions = 0x80000000
  215. };
  216. struct Section {
  217. char Name[16];
  218. char SegmentName[16];
  219. uint32_t Address;
  220. uint32_t Size;
  221. uint32_t Offset;
  222. uint32_t Align;
  223. uint32_t RelocationTableOffset;
  224. uint32_t NumRelocationTableEntries;
  225. uint32_t Flags;
  226. uint32_t Reserved1;
  227. uint32_t Reserved2;
  228. };
  229. struct Section64 {
  230. char Name[16];
  231. char SegmentName[16];
  232. uint64_t Address;
  233. uint64_t Size;
  234. uint32_t Offset;
  235. uint32_t Align;
  236. uint32_t RelocationTableOffset;
  237. uint32_t NumRelocationTableEntries;
  238. uint32_t Flags;
  239. uint32_t Reserved1;
  240. uint32_t Reserved2;
  241. uint32_t Reserved3;
  242. };
  243. /// @}
  244. /// @name Symbol Table Entries
  245. /// @{
  246. struct SymbolTableEntry {
  247. uint32_t StringIndex;
  248. uint8_t Type;
  249. uint8_t SectionIndex;
  250. uint16_t Flags;
  251. uint32_t Value;
  252. };
  253. // Despite containing a uint64_t, this structure is only 4-byte aligned within
  254. // a MachO file.
  255. #pragma pack(push)
  256. #pragma pack(4)
  257. struct Symbol64TableEntry {
  258. uint32_t StringIndex;
  259. uint8_t Type;
  260. uint8_t SectionIndex;
  261. uint16_t Flags;
  262. uint64_t Value;
  263. };
  264. #pragma pack(pop)
  265. /// @}
  266. /// @name Data-in-code Table Entry
  267. /// @{
  268. // See <mach-o/loader.h>.
  269. enum DataRegionType { Data = 1, JumpTable8, JumpTable16, JumpTable32 };
  270. struct DataInCodeTableEntry {
  271. uint32_t Offset; /* from mach_header to start of data region */
  272. uint16_t Length; /* number of bytes in data region */
  273. uint16_t Kind; /* a DataRegionType value */
  274. };
  275. /// @}
  276. /// @name Indirect Symbol Table
  277. /// @{
  278. struct IndirectSymbolTableEntry {
  279. uint32_t Index;
  280. };
  281. /// @}
  282. /// @name Relocation Data
  283. /// @{
  284. struct RelocationEntry {
  285. uint32_t Word0;
  286. uint32_t Word1;
  287. };
  288. /// @}
  289. // See <mach-o/nlist.h>.
  290. enum SymbolTypeType {
  291. STT_Undefined = 0x00,
  292. STT_Absolute = 0x02,
  293. STT_Section = 0x0e
  294. };
  295. enum SymbolTypeFlags {
  296. // If any of these bits are set, then the entry is a stab entry number (see
  297. // <mach-o/stab.h>. Otherwise the other masks apply.
  298. STF_StabsEntryMask = 0xe0,
  299. STF_TypeMask = 0x0e,
  300. STF_External = 0x01,
  301. STF_PrivateExtern = 0x10
  302. };
  303. /// IndirectSymbolFlags - Flags for encoding special values in the indirect
  304. /// symbol entry.
  305. enum IndirectSymbolFlags {
  306. ISF_Local = 0x80000000,
  307. ISF_Absolute = 0x40000000
  308. };
  309. /// RelocationFlags - Special flags for addresses.
  310. enum RelocationFlags {
  311. RF_Scattered = 0x80000000
  312. };
  313. /// Common relocation info types.
  314. enum RelocationInfoType {
  315. RIT_Vanilla = 0,
  316. RIT_Pair = 1,
  317. RIT_Difference = 2
  318. };
  319. /// Generic relocation info types, which are shared by some (but not all)
  320. /// platforms.
  321. enum RelocationInfoType_Generic {
  322. RIT_Generic_PreboundLazyPointer = 3,
  323. RIT_Generic_LocalDifference = 4,
  324. RIT_Generic_TLV = 5
  325. };
  326. /// X86_64 uses its own relocation types.
  327. enum RelocationInfoTypeX86_64 {
  328. // Note that x86_64 doesn't even share the common relocation types.
  329. RIT_X86_64_Unsigned = 0,
  330. RIT_X86_64_Signed = 1,
  331. RIT_X86_64_Branch = 2,
  332. RIT_X86_64_GOTLoad = 3,
  333. RIT_X86_64_GOT = 4,
  334. RIT_X86_64_Subtractor = 5,
  335. RIT_X86_64_Signed1 = 6,
  336. RIT_X86_64_Signed2 = 7,
  337. RIT_X86_64_Signed4 = 8,
  338. RIT_X86_64_TLV = 9
  339. };
  340. /// ARM uses its own relocation types.
  341. enum RelocationInfoTypeARM {
  342. RIT_ARM_LocalDifference = 3,
  343. RIT_ARM_PreboundLazyPointer = 4,
  344. RIT_ARM_Branch24Bit = 5,
  345. RIT_ARM_ThumbBranch22Bit = 6,
  346. RIT_ARM_ThumbBranch32Bit = 7,
  347. RIT_ARM_Half = 8,
  348. RIT_ARM_HalfDifference = 9
  349. };
  350. } // end namespace macho
  351. } // end namespace object
  352. } // end namespace llvm
  353. #endif