MachO.h 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/BinaryFormat/MachO.h - The MachO file format -------*- C++/-*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines manifest constants for the MachO object file format.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_BINARYFORMAT_MACHO_H
  18. #define LLVM_BINARYFORMAT_MACHO_H
  19. #include "llvm/Support/Compiler.h"
  20. #include "llvm/Support/DataTypes.h"
  21. #include "llvm/Support/Error.h"
  22. #include "llvm/Support/SwapByteOrder.h"
  23. namespace llvm {
  24. class Triple;
  25. namespace MachO {
  26. // Enums from <mach-o/loader.h>
  27. enum : uint32_t {
  28. // Constants for the "magic" field in llvm::MachO::mach_header and
  29. // llvm::MachO::mach_header_64
  30. MH_MAGIC = 0xFEEDFACEu,
  31. MH_CIGAM = 0xCEFAEDFEu,
  32. MH_MAGIC_64 = 0xFEEDFACFu,
  33. MH_CIGAM_64 = 0xCFFAEDFEu,
  34. FAT_MAGIC = 0xCAFEBABEu,
  35. FAT_CIGAM = 0xBEBAFECAu,
  36. FAT_MAGIC_64 = 0xCAFEBABFu,
  37. FAT_CIGAM_64 = 0xBFBAFECAu
  38. };
  39. enum HeaderFileType {
  40. // Constants for the "filetype" field in llvm::MachO::mach_header and
  41. // llvm::MachO::mach_header_64
  42. MH_OBJECT = 0x1u,
  43. MH_EXECUTE = 0x2u,
  44. MH_FVMLIB = 0x3u,
  45. MH_CORE = 0x4u,
  46. MH_PRELOAD = 0x5u,
  47. MH_DYLIB = 0x6u,
  48. MH_DYLINKER = 0x7u,
  49. MH_BUNDLE = 0x8u,
  50. MH_DYLIB_STUB = 0x9u,
  51. MH_DSYM = 0xAu,
  52. MH_KEXT_BUNDLE = 0xBu
  53. };
  54. enum {
  55. // Constant bits for the "flags" field in llvm::MachO::mach_header and
  56. // llvm::MachO::mach_header_64
  57. MH_NOUNDEFS = 0x00000001u,
  58. MH_INCRLINK = 0x00000002u,
  59. MH_DYLDLINK = 0x00000004u,
  60. MH_BINDATLOAD = 0x00000008u,
  61. MH_PREBOUND = 0x00000010u,
  62. MH_SPLIT_SEGS = 0x00000020u,
  63. MH_LAZY_INIT = 0x00000040u,
  64. MH_TWOLEVEL = 0x00000080u,
  65. MH_FORCE_FLAT = 0x00000100u,
  66. MH_NOMULTIDEFS = 0x00000200u,
  67. MH_NOFIXPREBINDING = 0x00000400u,
  68. MH_PREBINDABLE = 0x00000800u,
  69. MH_ALLMODSBOUND = 0x00001000u,
  70. MH_SUBSECTIONS_VIA_SYMBOLS = 0x00002000u,
  71. MH_CANONICAL = 0x00004000u,
  72. MH_WEAK_DEFINES = 0x00008000u,
  73. MH_BINDS_TO_WEAK = 0x00010000u,
  74. MH_ALLOW_STACK_EXECUTION = 0x00020000u,
  75. MH_ROOT_SAFE = 0x00040000u,
  76. MH_SETUID_SAFE = 0x00080000u,
  77. MH_NO_REEXPORTED_DYLIBS = 0x00100000u,
  78. MH_PIE = 0x00200000u,
  79. MH_DEAD_STRIPPABLE_DYLIB = 0x00400000u,
  80. MH_HAS_TLV_DESCRIPTORS = 0x00800000u,
  81. MH_NO_HEAP_EXECUTION = 0x01000000u,
  82. MH_APP_EXTENSION_SAFE = 0x02000000u,
  83. MH_NLIST_OUTOFSYNC_WITH_DYLDINFO = 0x04000000u,
  84. MH_SIM_SUPPORT = 0x08000000u,
  85. MH_DYLIB_IN_CACHE = 0x80000000u,
  86. };
  87. enum : uint32_t {
  88. // Flags for the "cmd" field in llvm::MachO::load_command
  89. LC_REQ_DYLD = 0x80000000u
  90. };
  91. #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) LCName = LCValue,
  92. enum LoadCommandType : uint32_t {
  93. #include "llvm/BinaryFormat/MachO.def"
  94. };
  95. #undef HANDLE_LOAD_COMMAND
  96. enum : uint32_t {
  97. // Constant bits for the "flags" field in llvm::MachO::segment_command
  98. SG_HIGHVM = 0x1u,
  99. SG_FVMLIB = 0x2u,
  100. SG_NORELOC = 0x4u,
  101. SG_PROTECTED_VERSION_1 = 0x8u,
  102. // Constant masks for the "flags" field in llvm::MachO::section and
  103. // llvm::MachO::section_64
  104. SECTION_TYPE = 0x000000ffu, // SECTION_TYPE
  105. SECTION_ATTRIBUTES = 0xffffff00u, // SECTION_ATTRIBUTES
  106. SECTION_ATTRIBUTES_USR = 0xff000000u, // SECTION_ATTRIBUTES_USR
  107. SECTION_ATTRIBUTES_SYS = 0x00ffff00u // SECTION_ATTRIBUTES_SYS
  108. };
  109. /// These are the section type and attributes fields. A MachO section can
  110. /// have only one Type, but can have any of the attributes specified.
  111. enum SectionType : uint32_t {
  112. // Constant masks for the "flags[7:0]" field in llvm::MachO::section and
  113. // llvm::MachO::section_64 (mask "flags" with SECTION_TYPE)
  114. /// S_REGULAR - Regular section.
  115. S_REGULAR = 0x00u,
  116. /// S_ZEROFILL - Zero fill on demand section.
  117. S_ZEROFILL = 0x01u,
  118. /// S_CSTRING_LITERALS - Section with literal C strings.
  119. S_CSTRING_LITERALS = 0x02u,
  120. /// S_4BYTE_LITERALS - Section with 4 byte literals.
  121. S_4BYTE_LITERALS = 0x03u,
  122. /// S_8BYTE_LITERALS - Section with 8 byte literals.
  123. S_8BYTE_LITERALS = 0x04u,
  124. /// S_LITERAL_POINTERS - Section with pointers to literals.
  125. S_LITERAL_POINTERS = 0x05u,
  126. /// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
  127. S_NON_LAZY_SYMBOL_POINTERS = 0x06u,
  128. /// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
  129. S_LAZY_SYMBOL_POINTERS = 0x07u,
  130. /// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
  131. /// the Reserved2 field.
  132. S_SYMBOL_STUBS = 0x08u,
  133. /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
  134. /// initialization.
  135. S_MOD_INIT_FUNC_POINTERS = 0x09u,
  136. /// S_MOD_TERM_FUNC_POINTERS - Section with only function pointers for
  137. /// termination.
  138. S_MOD_TERM_FUNC_POINTERS = 0x0au,
  139. /// S_COALESCED - Section contains symbols that are to be coalesced.
  140. S_COALESCED = 0x0bu,
  141. /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
  142. /// gigabytes).
  143. S_GB_ZEROFILL = 0x0cu,
  144. /// S_INTERPOSING - Section with only pairs of function pointers for
  145. /// interposing.
  146. S_INTERPOSING = 0x0du,
  147. /// S_16BYTE_LITERALS - Section with only 16 byte literals.
  148. S_16BYTE_LITERALS = 0x0eu,
  149. /// S_DTRACE_DOF - Section contains DTrace Object Format.
  150. S_DTRACE_DOF = 0x0fu,
  151. /// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
  152. /// lazy loaded dylibs.
  153. S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10u,
  154. /// S_THREAD_LOCAL_REGULAR - Thread local data section.
  155. S_THREAD_LOCAL_REGULAR = 0x11u,
  156. /// S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
  157. S_THREAD_LOCAL_ZEROFILL = 0x12u,
  158. /// S_THREAD_LOCAL_VARIABLES - Section with thread local variable
  159. /// structure data.
  160. S_THREAD_LOCAL_VARIABLES = 0x13u,
  161. /// S_THREAD_LOCAL_VARIABLE_POINTERS - Section with pointers to thread
  162. /// local structures.
  163. S_THREAD_LOCAL_VARIABLE_POINTERS = 0x14u,
  164. /// S_THREAD_LOCAL_INIT_FUNCTION_POINTERS - Section with thread local
  165. /// variable initialization pointers to functions.
  166. S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
  167. LAST_KNOWN_SECTION_TYPE = S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
  168. };
  169. enum : uint32_t {
  170. // Constant masks for the "flags[31:24]" field in llvm::MachO::section and
  171. // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
  172. /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
  173. /// instructions.
  174. S_ATTR_PURE_INSTRUCTIONS = 0x80000000u,
  175. /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
  176. /// in a ranlib table of contents.
  177. S_ATTR_NO_TOC = 0x40000000u,
  178. /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
  179. /// in files with the MY_DYLDLINK flag.
  180. S_ATTR_STRIP_STATIC_SYMS = 0x20000000u,
  181. /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
  182. S_ATTR_NO_DEAD_STRIP = 0x10000000u,
  183. /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
  184. S_ATTR_LIVE_SUPPORT = 0x08000000u,
  185. /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
  186. /// dyld.
  187. S_ATTR_SELF_MODIFYING_CODE = 0x04000000u,
  188. /// S_ATTR_DEBUG - A debug section.
  189. S_ATTR_DEBUG = 0x02000000u,
  190. // Constant masks for the "flags[23:8]" field in llvm::MachO::section and
  191. // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
  192. /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
  193. S_ATTR_SOME_INSTRUCTIONS = 0x00000400u,
  194. /// S_ATTR_EXT_RELOC - Section has external relocation entries.
  195. S_ATTR_EXT_RELOC = 0x00000200u,
  196. /// S_ATTR_LOC_RELOC - Section has local relocation entries.
  197. S_ATTR_LOC_RELOC = 0x00000100u,
  198. // Constant masks for the value of an indirect symbol in an indirect
  199. // symbol table
  200. INDIRECT_SYMBOL_LOCAL = 0x80000000u,
  201. INDIRECT_SYMBOL_ABS = 0x40000000u
  202. };
  203. enum DataRegionType {
  204. // Constants for the "kind" field in a data_in_code_entry structure
  205. DICE_KIND_DATA = 1u,
  206. DICE_KIND_JUMP_TABLE8 = 2u,
  207. DICE_KIND_JUMP_TABLE16 = 3u,
  208. DICE_KIND_JUMP_TABLE32 = 4u,
  209. DICE_KIND_ABS_JUMP_TABLE32 = 5u
  210. };
  211. enum RebaseType {
  212. REBASE_TYPE_POINTER = 1u,
  213. REBASE_TYPE_TEXT_ABSOLUTE32 = 2u,
  214. REBASE_TYPE_TEXT_PCREL32 = 3u
  215. };
  216. enum { REBASE_OPCODE_MASK = 0xF0u, REBASE_IMMEDIATE_MASK = 0x0Fu };
  217. enum RebaseOpcode {
  218. REBASE_OPCODE_DONE = 0x00u,
  219. REBASE_OPCODE_SET_TYPE_IMM = 0x10u,
  220. REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x20u,
  221. REBASE_OPCODE_ADD_ADDR_ULEB = 0x30u,
  222. REBASE_OPCODE_ADD_ADDR_IMM_SCALED = 0x40u,
  223. REBASE_OPCODE_DO_REBASE_IMM_TIMES = 0x50u,
  224. REBASE_OPCODE_DO_REBASE_ULEB_TIMES = 0x60u,
  225. REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB = 0x70u,
  226. REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u
  227. };
  228. enum BindType {
  229. BIND_TYPE_POINTER = 1u,
  230. BIND_TYPE_TEXT_ABSOLUTE32 = 2u,
  231. BIND_TYPE_TEXT_PCREL32 = 3u
  232. };
  233. enum BindSpecialDylib {
  234. BIND_SPECIAL_DYLIB_SELF = 0,
  235. BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE = -1,
  236. BIND_SPECIAL_DYLIB_FLAT_LOOKUP = -2
  237. };
  238. enum {
  239. BIND_SYMBOL_FLAGS_WEAK_IMPORT = 0x1u,
  240. BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION = 0x8u,
  241. BIND_OPCODE_MASK = 0xF0u,
  242. BIND_IMMEDIATE_MASK = 0x0Fu
  243. };
  244. enum BindOpcode {
  245. BIND_OPCODE_DONE = 0x00u,
  246. BIND_OPCODE_SET_DYLIB_ORDINAL_IMM = 0x10u,
  247. BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB = 0x20u,
  248. BIND_OPCODE_SET_DYLIB_SPECIAL_IMM = 0x30u,
  249. BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM = 0x40u,
  250. BIND_OPCODE_SET_TYPE_IMM = 0x50u,
  251. BIND_OPCODE_SET_ADDEND_SLEB = 0x60u,
  252. BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x70u,
  253. BIND_OPCODE_ADD_ADDR_ULEB = 0x80u,
  254. BIND_OPCODE_DO_BIND = 0x90u,
  255. BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB = 0xA0u,
  256. BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED = 0xB0u,
  257. BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u
  258. };
  259. enum {
  260. EXPORT_SYMBOL_FLAGS_KIND_MASK = 0x03u,
  261. EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION = 0x04u,
  262. EXPORT_SYMBOL_FLAGS_REEXPORT = 0x08u,
  263. EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER = 0x10u
  264. };
  265. enum ExportSymbolKind {
  266. EXPORT_SYMBOL_FLAGS_KIND_REGULAR = 0x00u,
  267. EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL = 0x01u,
  268. EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE = 0x02u
  269. };
  270. enum {
  271. // Constant masks for the "n_type" field in llvm::MachO::nlist and
  272. // llvm::MachO::nlist_64
  273. N_STAB = 0xe0,
  274. N_PEXT = 0x10,
  275. N_TYPE = 0x0e,
  276. N_EXT = 0x01
  277. };
  278. enum NListType : uint8_t {
  279. // Constants for the "n_type & N_TYPE" llvm::MachO::nlist and
  280. // llvm::MachO::nlist_64
  281. N_UNDF = 0x0u,
  282. N_ABS = 0x2u,
  283. N_SECT = 0xeu,
  284. N_PBUD = 0xcu,
  285. N_INDR = 0xau
  286. };
  287. enum SectionOrdinal {
  288. // Constants for the "n_sect" field in llvm::MachO::nlist and
  289. // llvm::MachO::nlist_64
  290. NO_SECT = 0u,
  291. MAX_SECT = 0xffu
  292. };
  293. enum {
  294. // Constant masks for the "n_desc" field in llvm::MachO::nlist and
  295. // llvm::MachO::nlist_64
  296. // The low 3 bits are the for the REFERENCE_TYPE.
  297. REFERENCE_TYPE = 0x7,
  298. REFERENCE_FLAG_UNDEFINED_NON_LAZY = 0,
  299. REFERENCE_FLAG_UNDEFINED_LAZY = 1,
  300. REFERENCE_FLAG_DEFINED = 2,
  301. REFERENCE_FLAG_PRIVATE_DEFINED = 3,
  302. REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4,
  303. REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY = 5,
  304. // Flag bits (some overlap with the library ordinal bits).
  305. N_ARM_THUMB_DEF = 0x0008u,
  306. REFERENCED_DYNAMICALLY = 0x0010u,
  307. N_NO_DEAD_STRIP = 0x0020u,
  308. N_WEAK_REF = 0x0040u,
  309. N_WEAK_DEF = 0x0080u,
  310. N_SYMBOL_RESOLVER = 0x0100u,
  311. N_ALT_ENTRY = 0x0200u,
  312. N_COLD_FUNC = 0x0400u,
  313. // For undefined symbols coming from libraries, see GET_LIBRARY_ORDINAL()
  314. // as these are in the top 8 bits.
  315. SELF_LIBRARY_ORDINAL = 0x0,
  316. MAX_LIBRARY_ORDINAL = 0xfd,
  317. DYNAMIC_LOOKUP_ORDINAL = 0xfe,
  318. EXECUTABLE_ORDINAL = 0xff
  319. };
  320. enum StabType {
  321. // Constant values for the "n_type" field in llvm::MachO::nlist and
  322. // llvm::MachO::nlist_64 when "(n_type & N_STAB) != 0"
  323. N_GSYM = 0x20u,
  324. N_FNAME = 0x22u,
  325. N_FUN = 0x24u,
  326. N_STSYM = 0x26u,
  327. N_LCSYM = 0x28u,
  328. N_BNSYM = 0x2Eu,
  329. N_PC = 0x30u,
  330. N_AST = 0x32u,
  331. N_OPT = 0x3Cu,
  332. N_RSYM = 0x40u,
  333. N_SLINE = 0x44u,
  334. N_ENSYM = 0x4Eu,
  335. N_SSYM = 0x60u,
  336. N_SO = 0x64u,
  337. N_OSO = 0x66u,
  338. N_LSYM = 0x80u,
  339. N_BINCL = 0x82u,
  340. N_SOL = 0x84u,
  341. N_PARAMS = 0x86u,
  342. N_VERSION = 0x88u,
  343. N_OLEVEL = 0x8Au,
  344. N_PSYM = 0xA0u,
  345. N_EINCL = 0xA2u,
  346. N_ENTRY = 0xA4u,
  347. N_LBRAC = 0xC0u,
  348. N_EXCL = 0xC2u,
  349. N_RBRAC = 0xE0u,
  350. N_BCOMM = 0xE2u,
  351. N_ECOMM = 0xE4u,
  352. N_ECOML = 0xE8u,
  353. N_LENG = 0xFEu
  354. };
  355. enum : uint32_t {
  356. // Constant values for the r_symbolnum field in an
  357. // llvm::MachO::relocation_info structure when r_extern is 0.
  358. R_ABS = 0,
  359. // Constant bits for the r_address field in an
  360. // llvm::MachO::relocation_info structure.
  361. R_SCATTERED = 0x80000000
  362. };
  363. enum RelocationInfoType {
  364. // Constant values for the r_type field in an
  365. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  366. // structure.
  367. GENERIC_RELOC_VANILLA = 0,
  368. GENERIC_RELOC_PAIR = 1,
  369. GENERIC_RELOC_SECTDIFF = 2,
  370. GENERIC_RELOC_PB_LA_PTR = 3,
  371. GENERIC_RELOC_LOCAL_SECTDIFF = 4,
  372. GENERIC_RELOC_TLV = 5,
  373. // Constant values for the r_type field in a PowerPC architecture
  374. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  375. // structure.
  376. PPC_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
  377. PPC_RELOC_PAIR = GENERIC_RELOC_PAIR,
  378. PPC_RELOC_BR14 = 2,
  379. PPC_RELOC_BR24 = 3,
  380. PPC_RELOC_HI16 = 4,
  381. PPC_RELOC_LO16 = 5,
  382. PPC_RELOC_HA16 = 6,
  383. PPC_RELOC_LO14 = 7,
  384. PPC_RELOC_SECTDIFF = 8,
  385. PPC_RELOC_PB_LA_PTR = 9,
  386. PPC_RELOC_HI16_SECTDIFF = 10,
  387. PPC_RELOC_LO16_SECTDIFF = 11,
  388. PPC_RELOC_HA16_SECTDIFF = 12,
  389. PPC_RELOC_JBSR = 13,
  390. PPC_RELOC_LO14_SECTDIFF = 14,
  391. PPC_RELOC_LOCAL_SECTDIFF = 15,
  392. // Constant values for the r_type field in an ARM architecture
  393. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  394. // structure.
  395. ARM_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
  396. ARM_RELOC_PAIR = GENERIC_RELOC_PAIR,
  397. ARM_RELOC_SECTDIFF = GENERIC_RELOC_SECTDIFF,
  398. ARM_RELOC_LOCAL_SECTDIFF = 3,
  399. ARM_RELOC_PB_LA_PTR = 4,
  400. ARM_RELOC_BR24 = 5,
  401. ARM_THUMB_RELOC_BR22 = 6,
  402. ARM_THUMB_32BIT_BRANCH = 7, // obsolete
  403. ARM_RELOC_HALF = 8,
  404. ARM_RELOC_HALF_SECTDIFF = 9,
  405. // Constant values for the r_type field in an ARM64 architecture
  406. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  407. // structure.
  408. // For pointers.
  409. ARM64_RELOC_UNSIGNED = 0,
  410. // Must be followed by an ARM64_RELOC_UNSIGNED
  411. ARM64_RELOC_SUBTRACTOR = 1,
  412. // A B/BL instruction with 26-bit displacement.
  413. ARM64_RELOC_BRANCH26 = 2,
  414. // PC-rel distance to page of target.
  415. ARM64_RELOC_PAGE21 = 3,
  416. // Offset within page, scaled by r_length.
  417. ARM64_RELOC_PAGEOFF12 = 4,
  418. // PC-rel distance to page of GOT slot.
  419. ARM64_RELOC_GOT_LOAD_PAGE21 = 5,
  420. // Offset within page of GOT slot, scaled by r_length.
  421. ARM64_RELOC_GOT_LOAD_PAGEOFF12 = 6,
  422. // For pointers to GOT slots.
  423. ARM64_RELOC_POINTER_TO_GOT = 7,
  424. // PC-rel distance to page of TLVP slot.
  425. ARM64_RELOC_TLVP_LOAD_PAGE21 = 8,
  426. // Offset within page of TLVP slot, scaled by r_length.
  427. ARM64_RELOC_TLVP_LOAD_PAGEOFF12 = 9,
  428. // Must be followed by ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12.
  429. ARM64_RELOC_ADDEND = 10,
  430. // Constant values for the r_type field in an x86_64 architecture
  431. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  432. // structure
  433. X86_64_RELOC_UNSIGNED = 0,
  434. X86_64_RELOC_SIGNED = 1,
  435. X86_64_RELOC_BRANCH = 2,
  436. X86_64_RELOC_GOT_LOAD = 3,
  437. X86_64_RELOC_GOT = 4,
  438. X86_64_RELOC_SUBTRACTOR = 5,
  439. X86_64_RELOC_SIGNED_1 = 6,
  440. X86_64_RELOC_SIGNED_2 = 7,
  441. X86_64_RELOC_SIGNED_4 = 8,
  442. X86_64_RELOC_TLV = 9
  443. };
  444. // Values for segment_command.initprot.
  445. // From <mach/vm_prot.h>
  446. enum { VM_PROT_READ = 0x1, VM_PROT_WRITE = 0x2, VM_PROT_EXECUTE = 0x4 };
  447. // Values for platform field in build_version_command.
  448. enum PlatformType {
  449. PLATFORM_MACOS = 1,
  450. PLATFORM_IOS = 2,
  451. PLATFORM_TVOS = 3,
  452. PLATFORM_WATCHOS = 4,
  453. PLATFORM_BRIDGEOS = 5,
  454. PLATFORM_MACCATALYST = 6,
  455. PLATFORM_IOSSIMULATOR = 7,
  456. PLATFORM_TVOSSIMULATOR = 8,
  457. PLATFORM_WATCHOSSIMULATOR = 9,
  458. PLATFORM_DRIVERKIT = 10,
  459. };
  460. // Values for tools enum in build_tool_version.
  461. enum { TOOL_CLANG = 1, TOOL_SWIFT = 2, TOOL_LD = 3 };
  462. // Structs from <mach-o/loader.h>
  463. struct mach_header {
  464. uint32_t magic;
  465. uint32_t cputype;
  466. uint32_t cpusubtype;
  467. uint32_t filetype;
  468. uint32_t ncmds;
  469. uint32_t sizeofcmds;
  470. uint32_t flags;
  471. };
  472. struct mach_header_64 {
  473. uint32_t magic;
  474. uint32_t cputype;
  475. uint32_t cpusubtype;
  476. uint32_t filetype;
  477. uint32_t ncmds;
  478. uint32_t sizeofcmds;
  479. uint32_t flags;
  480. uint32_t reserved;
  481. };
  482. struct load_command {
  483. uint32_t cmd;
  484. uint32_t cmdsize;
  485. };
  486. struct segment_command {
  487. uint32_t cmd;
  488. uint32_t cmdsize;
  489. char segname[16];
  490. uint32_t vmaddr;
  491. uint32_t vmsize;
  492. uint32_t fileoff;
  493. uint32_t filesize;
  494. uint32_t maxprot;
  495. uint32_t initprot;
  496. uint32_t nsects;
  497. uint32_t flags;
  498. };
  499. struct segment_command_64 {
  500. uint32_t cmd;
  501. uint32_t cmdsize;
  502. char segname[16];
  503. uint64_t vmaddr;
  504. uint64_t vmsize;
  505. uint64_t fileoff;
  506. uint64_t filesize;
  507. uint32_t maxprot;
  508. uint32_t initprot;
  509. uint32_t nsects;
  510. uint32_t flags;
  511. };
  512. struct section {
  513. char sectname[16];
  514. char segname[16];
  515. uint32_t addr;
  516. uint32_t size;
  517. uint32_t offset;
  518. uint32_t align;
  519. uint32_t reloff;
  520. uint32_t nreloc;
  521. uint32_t flags;
  522. uint32_t reserved1;
  523. uint32_t reserved2;
  524. };
  525. struct section_64 {
  526. char sectname[16];
  527. char segname[16];
  528. uint64_t addr;
  529. uint64_t size;
  530. uint32_t offset;
  531. uint32_t align;
  532. uint32_t reloff;
  533. uint32_t nreloc;
  534. uint32_t flags;
  535. uint32_t reserved1;
  536. uint32_t reserved2;
  537. uint32_t reserved3;
  538. };
  539. inline bool isVirtualSection(uint8_t type) {
  540. return (type == MachO::S_ZEROFILL || type == MachO::S_GB_ZEROFILL ||
  541. type == MachO::S_THREAD_LOCAL_ZEROFILL);
  542. }
  543. struct fvmlib {
  544. uint32_t name;
  545. uint32_t minor_version;
  546. uint32_t header_addr;
  547. };
  548. // The fvmlib_command is obsolete and no longer supported.
  549. struct fvmlib_command {
  550. uint32_t cmd;
  551. uint32_t cmdsize;
  552. struct fvmlib fvmlib;
  553. };
  554. struct dylib {
  555. uint32_t name;
  556. uint32_t timestamp;
  557. uint32_t current_version;
  558. uint32_t compatibility_version;
  559. };
  560. struct dylib_command {
  561. uint32_t cmd;
  562. uint32_t cmdsize;
  563. struct dylib dylib;
  564. };
  565. struct sub_framework_command {
  566. uint32_t cmd;
  567. uint32_t cmdsize;
  568. uint32_t umbrella;
  569. };
  570. struct sub_client_command {
  571. uint32_t cmd;
  572. uint32_t cmdsize;
  573. uint32_t client;
  574. };
  575. struct sub_umbrella_command {
  576. uint32_t cmd;
  577. uint32_t cmdsize;
  578. uint32_t sub_umbrella;
  579. };
  580. struct sub_library_command {
  581. uint32_t cmd;
  582. uint32_t cmdsize;
  583. uint32_t sub_library;
  584. };
  585. // The prebound_dylib_command is obsolete and no longer supported.
  586. struct prebound_dylib_command {
  587. uint32_t cmd;
  588. uint32_t cmdsize;
  589. uint32_t name;
  590. uint32_t nmodules;
  591. uint32_t linked_modules;
  592. };
  593. struct dylinker_command {
  594. uint32_t cmd;
  595. uint32_t cmdsize;
  596. uint32_t name;
  597. };
  598. struct thread_command {
  599. uint32_t cmd;
  600. uint32_t cmdsize;
  601. };
  602. struct routines_command {
  603. uint32_t cmd;
  604. uint32_t cmdsize;
  605. uint32_t init_address;
  606. uint32_t init_module;
  607. uint32_t reserved1;
  608. uint32_t reserved2;
  609. uint32_t reserved3;
  610. uint32_t reserved4;
  611. uint32_t reserved5;
  612. uint32_t reserved6;
  613. };
  614. struct routines_command_64 {
  615. uint32_t cmd;
  616. uint32_t cmdsize;
  617. uint64_t init_address;
  618. uint64_t init_module;
  619. uint64_t reserved1;
  620. uint64_t reserved2;
  621. uint64_t reserved3;
  622. uint64_t reserved4;
  623. uint64_t reserved5;
  624. uint64_t reserved6;
  625. };
  626. struct symtab_command {
  627. uint32_t cmd;
  628. uint32_t cmdsize;
  629. uint32_t symoff;
  630. uint32_t nsyms;
  631. uint32_t stroff;
  632. uint32_t strsize;
  633. };
  634. struct dysymtab_command {
  635. uint32_t cmd;
  636. uint32_t cmdsize;
  637. uint32_t ilocalsym;
  638. uint32_t nlocalsym;
  639. uint32_t iextdefsym;
  640. uint32_t nextdefsym;
  641. uint32_t iundefsym;
  642. uint32_t nundefsym;
  643. uint32_t tocoff;
  644. uint32_t ntoc;
  645. uint32_t modtaboff;
  646. uint32_t nmodtab;
  647. uint32_t extrefsymoff;
  648. uint32_t nextrefsyms;
  649. uint32_t indirectsymoff;
  650. uint32_t nindirectsyms;
  651. uint32_t extreloff;
  652. uint32_t nextrel;
  653. uint32_t locreloff;
  654. uint32_t nlocrel;
  655. };
  656. struct dylib_table_of_contents {
  657. uint32_t symbol_index;
  658. uint32_t module_index;
  659. };
  660. struct dylib_module {
  661. uint32_t module_name;
  662. uint32_t iextdefsym;
  663. uint32_t nextdefsym;
  664. uint32_t irefsym;
  665. uint32_t nrefsym;
  666. uint32_t ilocalsym;
  667. uint32_t nlocalsym;
  668. uint32_t iextrel;
  669. uint32_t nextrel;
  670. uint32_t iinit_iterm;
  671. uint32_t ninit_nterm;
  672. uint32_t objc_module_info_addr;
  673. uint32_t objc_module_info_size;
  674. };
  675. struct dylib_module_64 {
  676. uint32_t module_name;
  677. uint32_t iextdefsym;
  678. uint32_t nextdefsym;
  679. uint32_t irefsym;
  680. uint32_t nrefsym;
  681. uint32_t ilocalsym;
  682. uint32_t nlocalsym;
  683. uint32_t iextrel;
  684. uint32_t nextrel;
  685. uint32_t iinit_iterm;
  686. uint32_t ninit_nterm;
  687. uint32_t objc_module_info_size;
  688. uint64_t objc_module_info_addr;
  689. };
  690. struct dylib_reference {
  691. uint32_t isym : 24, flags : 8;
  692. };
  693. // The twolevel_hints_command is obsolete and no longer supported.
  694. struct twolevel_hints_command {
  695. uint32_t cmd;
  696. uint32_t cmdsize;
  697. uint32_t offset;
  698. uint32_t nhints;
  699. };
  700. // The twolevel_hints_command is obsolete and no longer supported.
  701. struct twolevel_hint {
  702. uint32_t isub_image : 8, itoc : 24;
  703. };
  704. // The prebind_cksum_command is obsolete and no longer supported.
  705. struct prebind_cksum_command {
  706. uint32_t cmd;
  707. uint32_t cmdsize;
  708. uint32_t cksum;
  709. };
  710. struct uuid_command {
  711. uint32_t cmd;
  712. uint32_t cmdsize;
  713. uint8_t uuid[16];
  714. };
  715. struct rpath_command {
  716. uint32_t cmd;
  717. uint32_t cmdsize;
  718. uint32_t path;
  719. };
  720. struct linkedit_data_command {
  721. uint32_t cmd;
  722. uint32_t cmdsize;
  723. uint32_t dataoff;
  724. uint32_t datasize;
  725. };
  726. struct data_in_code_entry {
  727. uint32_t offset;
  728. uint16_t length;
  729. uint16_t kind;
  730. };
  731. struct source_version_command {
  732. uint32_t cmd;
  733. uint32_t cmdsize;
  734. uint64_t version;
  735. };
  736. struct encryption_info_command {
  737. uint32_t cmd;
  738. uint32_t cmdsize;
  739. uint32_t cryptoff;
  740. uint32_t cryptsize;
  741. uint32_t cryptid;
  742. };
  743. struct encryption_info_command_64 {
  744. uint32_t cmd;
  745. uint32_t cmdsize;
  746. uint32_t cryptoff;
  747. uint32_t cryptsize;
  748. uint32_t cryptid;
  749. uint32_t pad;
  750. };
  751. struct version_min_command {
  752. uint32_t cmd; // LC_VERSION_MIN_MACOSX or
  753. // LC_VERSION_MIN_IPHONEOS
  754. uint32_t cmdsize; // sizeof(struct version_min_command)
  755. uint32_t version; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  756. uint32_t sdk; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  757. };
  758. struct note_command {
  759. uint32_t cmd; // LC_NOTE
  760. uint32_t cmdsize; // sizeof(struct note_command)
  761. char data_owner[16]; // owner name for this LC_NOTE
  762. uint64_t offset; // file offset of this data
  763. uint64_t size; // length of data region
  764. };
  765. struct build_tool_version {
  766. uint32_t tool; // enum for the tool
  767. uint32_t version; // version of the tool
  768. };
  769. struct build_version_command {
  770. uint32_t cmd; // LC_BUILD_VERSION
  771. uint32_t cmdsize; // sizeof(struct build_version_command) +
  772. // ntools * sizeof(struct build_tool_version)
  773. uint32_t platform; // platform
  774. uint32_t minos; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  775. uint32_t sdk; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  776. uint32_t ntools; // number of tool entries following this
  777. };
  778. struct dyld_info_command {
  779. uint32_t cmd;
  780. uint32_t cmdsize;
  781. uint32_t rebase_off;
  782. uint32_t rebase_size;
  783. uint32_t bind_off;
  784. uint32_t bind_size;
  785. uint32_t weak_bind_off;
  786. uint32_t weak_bind_size;
  787. uint32_t lazy_bind_off;
  788. uint32_t lazy_bind_size;
  789. uint32_t export_off;
  790. uint32_t export_size;
  791. };
  792. struct linker_option_command {
  793. uint32_t cmd;
  794. uint32_t cmdsize;
  795. uint32_t count;
  796. };
  797. // The symseg_command is obsolete and no longer supported.
  798. struct symseg_command {
  799. uint32_t cmd;
  800. uint32_t cmdsize;
  801. uint32_t offset;
  802. uint32_t size;
  803. };
  804. // The ident_command is obsolete and no longer supported.
  805. struct ident_command {
  806. uint32_t cmd;
  807. uint32_t cmdsize;
  808. };
  809. // The fvmfile_command is obsolete and no longer supported.
  810. struct fvmfile_command {
  811. uint32_t cmd;
  812. uint32_t cmdsize;
  813. uint32_t name;
  814. uint32_t header_addr;
  815. };
  816. struct tlv_descriptor_32 {
  817. uint32_t thunk;
  818. uint32_t key;
  819. uint32_t offset;
  820. };
  821. struct tlv_descriptor_64 {
  822. uint64_t thunk;
  823. uint64_t key;
  824. uint64_t offset;
  825. };
  826. struct tlv_descriptor {
  827. uintptr_t thunk;
  828. uintptr_t key;
  829. uintptr_t offset;
  830. };
  831. struct entry_point_command {
  832. uint32_t cmd;
  833. uint32_t cmdsize;
  834. uint64_t entryoff;
  835. uint64_t stacksize;
  836. };
  837. // Structs from <mach-o/fat.h>
  838. struct fat_header {
  839. uint32_t magic;
  840. uint32_t nfat_arch;
  841. };
  842. struct fat_arch {
  843. uint32_t cputype;
  844. uint32_t cpusubtype;
  845. uint32_t offset;
  846. uint32_t size;
  847. uint32_t align;
  848. };
  849. struct fat_arch_64 {
  850. uint32_t cputype;
  851. uint32_t cpusubtype;
  852. uint64_t offset;
  853. uint64_t size;
  854. uint32_t align;
  855. uint32_t reserved;
  856. };
  857. // Structs from <mach-o/reloc.h>
  858. struct relocation_info {
  859. int32_t r_address;
  860. uint32_t r_symbolnum : 24, r_pcrel : 1, r_length : 2, r_extern : 1,
  861. r_type : 4;
  862. };
  863. struct scattered_relocation_info {
  864. #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
  865. uint32_t r_scattered : 1, r_pcrel : 1, r_length : 2, r_type : 4,
  866. r_address : 24;
  867. #else
  868. uint32_t r_address : 24, r_type : 4, r_length : 2, r_pcrel : 1,
  869. r_scattered : 1;
  870. #endif
  871. int32_t r_value;
  872. };
  873. // Structs NOT from <mach-o/reloc.h>, but that make LLVM's life easier
  874. struct any_relocation_info {
  875. uint32_t r_word0, r_word1;
  876. };
  877. // Structs from <mach-o/nlist.h>
  878. struct nlist_base {
  879. uint32_t n_strx;
  880. uint8_t n_type;
  881. uint8_t n_sect;
  882. uint16_t n_desc;
  883. };
  884. struct nlist {
  885. uint32_t n_strx;
  886. uint8_t n_type;
  887. uint8_t n_sect;
  888. int16_t n_desc;
  889. uint32_t n_value;
  890. };
  891. struct nlist_64 {
  892. uint32_t n_strx;
  893. uint8_t n_type;
  894. uint8_t n_sect;
  895. uint16_t n_desc;
  896. uint64_t n_value;
  897. };
  898. // Byte order swapping functions for MachO structs
  899. inline void swapStruct(fat_header &mh) {
  900. sys::swapByteOrder(mh.magic);
  901. sys::swapByteOrder(mh.nfat_arch);
  902. }
  903. inline void swapStruct(fat_arch &mh) {
  904. sys::swapByteOrder(mh.cputype);
  905. sys::swapByteOrder(mh.cpusubtype);
  906. sys::swapByteOrder(mh.offset);
  907. sys::swapByteOrder(mh.size);
  908. sys::swapByteOrder(mh.align);
  909. }
  910. inline void swapStruct(fat_arch_64 &mh) {
  911. sys::swapByteOrder(mh.cputype);
  912. sys::swapByteOrder(mh.cpusubtype);
  913. sys::swapByteOrder(mh.offset);
  914. sys::swapByteOrder(mh.size);
  915. sys::swapByteOrder(mh.align);
  916. sys::swapByteOrder(mh.reserved);
  917. }
  918. inline void swapStruct(mach_header &mh) {
  919. sys::swapByteOrder(mh.magic);
  920. sys::swapByteOrder(mh.cputype);
  921. sys::swapByteOrder(mh.cpusubtype);
  922. sys::swapByteOrder(mh.filetype);
  923. sys::swapByteOrder(mh.ncmds);
  924. sys::swapByteOrder(mh.sizeofcmds);
  925. sys::swapByteOrder(mh.flags);
  926. }
  927. inline void swapStruct(mach_header_64 &H) {
  928. sys::swapByteOrder(H.magic);
  929. sys::swapByteOrder(H.cputype);
  930. sys::swapByteOrder(H.cpusubtype);
  931. sys::swapByteOrder(H.filetype);
  932. sys::swapByteOrder(H.ncmds);
  933. sys::swapByteOrder(H.sizeofcmds);
  934. sys::swapByteOrder(H.flags);
  935. sys::swapByteOrder(H.reserved);
  936. }
  937. inline void swapStruct(load_command &lc) {
  938. sys::swapByteOrder(lc.cmd);
  939. sys::swapByteOrder(lc.cmdsize);
  940. }
  941. inline void swapStruct(symtab_command &lc) {
  942. sys::swapByteOrder(lc.cmd);
  943. sys::swapByteOrder(lc.cmdsize);
  944. sys::swapByteOrder(lc.symoff);
  945. sys::swapByteOrder(lc.nsyms);
  946. sys::swapByteOrder(lc.stroff);
  947. sys::swapByteOrder(lc.strsize);
  948. }
  949. inline void swapStruct(segment_command_64 &seg) {
  950. sys::swapByteOrder(seg.cmd);
  951. sys::swapByteOrder(seg.cmdsize);
  952. sys::swapByteOrder(seg.vmaddr);
  953. sys::swapByteOrder(seg.vmsize);
  954. sys::swapByteOrder(seg.fileoff);
  955. sys::swapByteOrder(seg.filesize);
  956. sys::swapByteOrder(seg.maxprot);
  957. sys::swapByteOrder(seg.initprot);
  958. sys::swapByteOrder(seg.nsects);
  959. sys::swapByteOrder(seg.flags);
  960. }
  961. inline void swapStruct(segment_command &seg) {
  962. sys::swapByteOrder(seg.cmd);
  963. sys::swapByteOrder(seg.cmdsize);
  964. sys::swapByteOrder(seg.vmaddr);
  965. sys::swapByteOrder(seg.vmsize);
  966. sys::swapByteOrder(seg.fileoff);
  967. sys::swapByteOrder(seg.filesize);
  968. sys::swapByteOrder(seg.maxprot);
  969. sys::swapByteOrder(seg.initprot);
  970. sys::swapByteOrder(seg.nsects);
  971. sys::swapByteOrder(seg.flags);
  972. }
  973. inline void swapStruct(section_64 &sect) {
  974. sys::swapByteOrder(sect.addr);
  975. sys::swapByteOrder(sect.size);
  976. sys::swapByteOrder(sect.offset);
  977. sys::swapByteOrder(sect.align);
  978. sys::swapByteOrder(sect.reloff);
  979. sys::swapByteOrder(sect.nreloc);
  980. sys::swapByteOrder(sect.flags);
  981. sys::swapByteOrder(sect.reserved1);
  982. sys::swapByteOrder(sect.reserved2);
  983. }
  984. inline void swapStruct(section &sect) {
  985. sys::swapByteOrder(sect.addr);
  986. sys::swapByteOrder(sect.size);
  987. sys::swapByteOrder(sect.offset);
  988. sys::swapByteOrder(sect.align);
  989. sys::swapByteOrder(sect.reloff);
  990. sys::swapByteOrder(sect.nreloc);
  991. sys::swapByteOrder(sect.flags);
  992. sys::swapByteOrder(sect.reserved1);
  993. sys::swapByteOrder(sect.reserved2);
  994. }
  995. inline void swapStruct(dyld_info_command &info) {
  996. sys::swapByteOrder(info.cmd);
  997. sys::swapByteOrder(info.cmdsize);
  998. sys::swapByteOrder(info.rebase_off);
  999. sys::swapByteOrder(info.rebase_size);
  1000. sys::swapByteOrder(info.bind_off);
  1001. sys::swapByteOrder(info.bind_size);
  1002. sys::swapByteOrder(info.weak_bind_off);
  1003. sys::swapByteOrder(info.weak_bind_size);
  1004. sys::swapByteOrder(info.lazy_bind_off);
  1005. sys::swapByteOrder(info.lazy_bind_size);
  1006. sys::swapByteOrder(info.export_off);
  1007. sys::swapByteOrder(info.export_size);
  1008. }
  1009. inline void swapStruct(dylib_command &d) {
  1010. sys::swapByteOrder(d.cmd);
  1011. sys::swapByteOrder(d.cmdsize);
  1012. sys::swapByteOrder(d.dylib.name);
  1013. sys::swapByteOrder(d.dylib.timestamp);
  1014. sys::swapByteOrder(d.dylib.current_version);
  1015. sys::swapByteOrder(d.dylib.compatibility_version);
  1016. }
  1017. inline void swapStruct(sub_framework_command &s) {
  1018. sys::swapByteOrder(s.cmd);
  1019. sys::swapByteOrder(s.cmdsize);
  1020. sys::swapByteOrder(s.umbrella);
  1021. }
  1022. inline void swapStruct(sub_umbrella_command &s) {
  1023. sys::swapByteOrder(s.cmd);
  1024. sys::swapByteOrder(s.cmdsize);
  1025. sys::swapByteOrder(s.sub_umbrella);
  1026. }
  1027. inline void swapStruct(sub_library_command &s) {
  1028. sys::swapByteOrder(s.cmd);
  1029. sys::swapByteOrder(s.cmdsize);
  1030. sys::swapByteOrder(s.sub_library);
  1031. }
  1032. inline void swapStruct(sub_client_command &s) {
  1033. sys::swapByteOrder(s.cmd);
  1034. sys::swapByteOrder(s.cmdsize);
  1035. sys::swapByteOrder(s.client);
  1036. }
  1037. inline void swapStruct(routines_command &r) {
  1038. sys::swapByteOrder(r.cmd);
  1039. sys::swapByteOrder(r.cmdsize);
  1040. sys::swapByteOrder(r.init_address);
  1041. sys::swapByteOrder(r.init_module);
  1042. sys::swapByteOrder(r.reserved1);
  1043. sys::swapByteOrder(r.reserved2);
  1044. sys::swapByteOrder(r.reserved3);
  1045. sys::swapByteOrder(r.reserved4);
  1046. sys::swapByteOrder(r.reserved5);
  1047. sys::swapByteOrder(r.reserved6);
  1048. }
  1049. inline void swapStruct(routines_command_64 &r) {
  1050. sys::swapByteOrder(r.cmd);
  1051. sys::swapByteOrder(r.cmdsize);
  1052. sys::swapByteOrder(r.init_address);
  1053. sys::swapByteOrder(r.init_module);
  1054. sys::swapByteOrder(r.reserved1);
  1055. sys::swapByteOrder(r.reserved2);
  1056. sys::swapByteOrder(r.reserved3);
  1057. sys::swapByteOrder(r.reserved4);
  1058. sys::swapByteOrder(r.reserved5);
  1059. sys::swapByteOrder(r.reserved6);
  1060. }
  1061. inline void swapStruct(thread_command &t) {
  1062. sys::swapByteOrder(t.cmd);
  1063. sys::swapByteOrder(t.cmdsize);
  1064. }
  1065. inline void swapStruct(dylinker_command &d) {
  1066. sys::swapByteOrder(d.cmd);
  1067. sys::swapByteOrder(d.cmdsize);
  1068. sys::swapByteOrder(d.name);
  1069. }
  1070. inline void swapStruct(uuid_command &u) {
  1071. sys::swapByteOrder(u.cmd);
  1072. sys::swapByteOrder(u.cmdsize);
  1073. }
  1074. inline void swapStruct(rpath_command &r) {
  1075. sys::swapByteOrder(r.cmd);
  1076. sys::swapByteOrder(r.cmdsize);
  1077. sys::swapByteOrder(r.path);
  1078. }
  1079. inline void swapStruct(source_version_command &s) {
  1080. sys::swapByteOrder(s.cmd);
  1081. sys::swapByteOrder(s.cmdsize);
  1082. sys::swapByteOrder(s.version);
  1083. }
  1084. inline void swapStruct(entry_point_command &e) {
  1085. sys::swapByteOrder(e.cmd);
  1086. sys::swapByteOrder(e.cmdsize);
  1087. sys::swapByteOrder(e.entryoff);
  1088. sys::swapByteOrder(e.stacksize);
  1089. }
  1090. inline void swapStruct(encryption_info_command &e) {
  1091. sys::swapByteOrder(e.cmd);
  1092. sys::swapByteOrder(e.cmdsize);
  1093. sys::swapByteOrder(e.cryptoff);
  1094. sys::swapByteOrder(e.cryptsize);
  1095. sys::swapByteOrder(e.cryptid);
  1096. }
  1097. inline void swapStruct(encryption_info_command_64 &e) {
  1098. sys::swapByteOrder(e.cmd);
  1099. sys::swapByteOrder(e.cmdsize);
  1100. sys::swapByteOrder(e.cryptoff);
  1101. sys::swapByteOrder(e.cryptsize);
  1102. sys::swapByteOrder(e.cryptid);
  1103. sys::swapByteOrder(e.pad);
  1104. }
  1105. inline void swapStruct(dysymtab_command &dst) {
  1106. sys::swapByteOrder(dst.cmd);
  1107. sys::swapByteOrder(dst.cmdsize);
  1108. sys::swapByteOrder(dst.ilocalsym);
  1109. sys::swapByteOrder(dst.nlocalsym);
  1110. sys::swapByteOrder(dst.iextdefsym);
  1111. sys::swapByteOrder(dst.nextdefsym);
  1112. sys::swapByteOrder(dst.iundefsym);
  1113. sys::swapByteOrder(dst.nundefsym);
  1114. sys::swapByteOrder(dst.tocoff);
  1115. sys::swapByteOrder(dst.ntoc);
  1116. sys::swapByteOrder(dst.modtaboff);
  1117. sys::swapByteOrder(dst.nmodtab);
  1118. sys::swapByteOrder(dst.extrefsymoff);
  1119. sys::swapByteOrder(dst.nextrefsyms);
  1120. sys::swapByteOrder(dst.indirectsymoff);
  1121. sys::swapByteOrder(dst.nindirectsyms);
  1122. sys::swapByteOrder(dst.extreloff);
  1123. sys::swapByteOrder(dst.nextrel);
  1124. sys::swapByteOrder(dst.locreloff);
  1125. sys::swapByteOrder(dst.nlocrel);
  1126. }
  1127. inline void swapStruct(any_relocation_info &reloc) {
  1128. sys::swapByteOrder(reloc.r_word0);
  1129. sys::swapByteOrder(reloc.r_word1);
  1130. }
  1131. inline void swapStruct(nlist_base &S) {
  1132. sys::swapByteOrder(S.n_strx);
  1133. sys::swapByteOrder(S.n_desc);
  1134. }
  1135. inline void swapStruct(nlist &sym) {
  1136. sys::swapByteOrder(sym.n_strx);
  1137. sys::swapByteOrder(sym.n_desc);
  1138. sys::swapByteOrder(sym.n_value);
  1139. }
  1140. inline void swapStruct(nlist_64 &sym) {
  1141. sys::swapByteOrder(sym.n_strx);
  1142. sys::swapByteOrder(sym.n_desc);
  1143. sys::swapByteOrder(sym.n_value);
  1144. }
  1145. inline void swapStruct(linkedit_data_command &C) {
  1146. sys::swapByteOrder(C.cmd);
  1147. sys::swapByteOrder(C.cmdsize);
  1148. sys::swapByteOrder(C.dataoff);
  1149. sys::swapByteOrder(C.datasize);
  1150. }
  1151. inline void swapStruct(linker_option_command &C) {
  1152. sys::swapByteOrder(C.cmd);
  1153. sys::swapByteOrder(C.cmdsize);
  1154. sys::swapByteOrder(C.count);
  1155. }
  1156. inline void swapStruct(version_min_command &C) {
  1157. sys::swapByteOrder(C.cmd);
  1158. sys::swapByteOrder(C.cmdsize);
  1159. sys::swapByteOrder(C.version);
  1160. sys::swapByteOrder(C.sdk);
  1161. }
  1162. inline void swapStruct(note_command &C) {
  1163. sys::swapByteOrder(C.cmd);
  1164. sys::swapByteOrder(C.cmdsize);
  1165. sys::swapByteOrder(C.offset);
  1166. sys::swapByteOrder(C.size);
  1167. }
  1168. inline void swapStruct(build_version_command &C) {
  1169. sys::swapByteOrder(C.cmd);
  1170. sys::swapByteOrder(C.cmdsize);
  1171. sys::swapByteOrder(C.platform);
  1172. sys::swapByteOrder(C.minos);
  1173. sys::swapByteOrder(C.sdk);
  1174. sys::swapByteOrder(C.ntools);
  1175. }
  1176. inline void swapStruct(build_tool_version &C) {
  1177. sys::swapByteOrder(C.tool);
  1178. sys::swapByteOrder(C.version);
  1179. }
  1180. inline void swapStruct(data_in_code_entry &C) {
  1181. sys::swapByteOrder(C.offset);
  1182. sys::swapByteOrder(C.length);
  1183. sys::swapByteOrder(C.kind);
  1184. }
  1185. inline void swapStruct(uint32_t &C) { sys::swapByteOrder(C); }
  1186. // The prebind_cksum_command is obsolete and no longer supported.
  1187. inline void swapStruct(prebind_cksum_command &C) {
  1188. sys::swapByteOrder(C.cmd);
  1189. sys::swapByteOrder(C.cmdsize);
  1190. sys::swapByteOrder(C.cksum);
  1191. }
  1192. // The twolevel_hints_command is obsolete and no longer supported.
  1193. inline void swapStruct(twolevel_hints_command &C) {
  1194. sys::swapByteOrder(C.cmd);
  1195. sys::swapByteOrder(C.cmdsize);
  1196. sys::swapByteOrder(C.offset);
  1197. sys::swapByteOrder(C.nhints);
  1198. }
  1199. // The prebound_dylib_command is obsolete and no longer supported.
  1200. inline void swapStruct(prebound_dylib_command &C) {
  1201. sys::swapByteOrder(C.cmd);
  1202. sys::swapByteOrder(C.cmdsize);
  1203. sys::swapByteOrder(C.name);
  1204. sys::swapByteOrder(C.nmodules);
  1205. sys::swapByteOrder(C.linked_modules);
  1206. }
  1207. // The fvmfile_command is obsolete and no longer supported.
  1208. inline void swapStruct(fvmfile_command &C) {
  1209. sys::swapByteOrder(C.cmd);
  1210. sys::swapByteOrder(C.cmdsize);
  1211. sys::swapByteOrder(C.name);
  1212. sys::swapByteOrder(C.header_addr);
  1213. }
  1214. // The symseg_command is obsolete and no longer supported.
  1215. inline void swapStruct(symseg_command &C) {
  1216. sys::swapByteOrder(C.cmd);
  1217. sys::swapByteOrder(C.cmdsize);
  1218. sys::swapByteOrder(C.offset);
  1219. sys::swapByteOrder(C.size);
  1220. }
  1221. // The ident_command is obsolete and no longer supported.
  1222. inline void swapStruct(ident_command &C) {
  1223. sys::swapByteOrder(C.cmd);
  1224. sys::swapByteOrder(C.cmdsize);
  1225. }
  1226. inline void swapStruct(fvmlib &C) {
  1227. sys::swapByteOrder(C.name);
  1228. sys::swapByteOrder(C.minor_version);
  1229. sys::swapByteOrder(C.header_addr);
  1230. }
  1231. // The fvmlib_command is obsolete and no longer supported.
  1232. inline void swapStruct(fvmlib_command &C) {
  1233. sys::swapByteOrder(C.cmd);
  1234. sys::swapByteOrder(C.cmdsize);
  1235. swapStruct(C.fvmlib);
  1236. }
  1237. // Get/Set functions from <mach-o/nlist.h>
  1238. inline uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc) {
  1239. return (((n_desc) >> 8u) & 0xffu);
  1240. }
  1241. inline void SET_LIBRARY_ORDINAL(uint16_t &n_desc, uint8_t ordinal) {
  1242. n_desc = (((n_desc)&0x00ff) | (((ordinal)&0xff) << 8));
  1243. }
  1244. inline uint8_t GET_COMM_ALIGN(uint16_t n_desc) {
  1245. return (n_desc >> 8u) & 0x0fu;
  1246. }
  1247. inline void SET_COMM_ALIGN(uint16_t &n_desc, uint8_t align) {
  1248. n_desc = ((n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));
  1249. }
  1250. // Enums from <mach/machine.h>
  1251. enum : uint32_t {
  1252. // Capability bits used in the definition of cpu_type.
  1253. CPU_ARCH_MASK = 0xff000000, // Mask for architecture bits
  1254. CPU_ARCH_ABI64 = 0x01000000, // 64 bit ABI
  1255. CPU_ARCH_ABI64_32 = 0x02000000, // ILP32 ABI on 64-bit hardware
  1256. };
  1257. // Constants for the cputype field.
  1258. enum CPUType {
  1259. CPU_TYPE_ANY = -1,
  1260. CPU_TYPE_X86 = 7,
  1261. CPU_TYPE_I386 = CPU_TYPE_X86,
  1262. CPU_TYPE_X86_64 = CPU_TYPE_X86 | CPU_ARCH_ABI64,
  1263. /* CPU_TYPE_MIPS = 8, */
  1264. CPU_TYPE_MC98000 = 10, // Old Motorola PowerPC
  1265. CPU_TYPE_ARM = 12,
  1266. CPU_TYPE_ARM64 = CPU_TYPE_ARM | CPU_ARCH_ABI64,
  1267. CPU_TYPE_ARM64_32 = CPU_TYPE_ARM | CPU_ARCH_ABI64_32,
  1268. CPU_TYPE_SPARC = 14,
  1269. CPU_TYPE_POWERPC = 18,
  1270. CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
  1271. };
  1272. enum : uint32_t {
  1273. // Capability bits used in the definition of cpusubtype.
  1274. CPU_SUBTYPE_MASK = 0xff000000, // Mask for architecture bits
  1275. CPU_SUBTYPE_LIB64 = 0x80000000, // 64 bit libraries
  1276. // Special CPU subtype constants.
  1277. CPU_SUBTYPE_MULTIPLE = ~0u
  1278. };
  1279. // Constants for the cpusubtype field.
  1280. enum CPUSubTypeX86 {
  1281. CPU_SUBTYPE_I386_ALL = 3,
  1282. CPU_SUBTYPE_386 = 3,
  1283. CPU_SUBTYPE_486 = 4,
  1284. CPU_SUBTYPE_486SX = 0x84,
  1285. CPU_SUBTYPE_586 = 5,
  1286. CPU_SUBTYPE_PENT = CPU_SUBTYPE_586,
  1287. CPU_SUBTYPE_PENTPRO = 0x16,
  1288. CPU_SUBTYPE_PENTII_M3 = 0x36,
  1289. CPU_SUBTYPE_PENTII_M5 = 0x56,
  1290. CPU_SUBTYPE_CELERON = 0x67,
  1291. CPU_SUBTYPE_CELERON_MOBILE = 0x77,
  1292. CPU_SUBTYPE_PENTIUM_3 = 0x08,
  1293. CPU_SUBTYPE_PENTIUM_3_M = 0x18,
  1294. CPU_SUBTYPE_PENTIUM_3_XEON = 0x28,
  1295. CPU_SUBTYPE_PENTIUM_M = 0x09,
  1296. CPU_SUBTYPE_PENTIUM_4 = 0x0a,
  1297. CPU_SUBTYPE_PENTIUM_4_M = 0x1a,
  1298. CPU_SUBTYPE_ITANIUM = 0x0b,
  1299. CPU_SUBTYPE_ITANIUM_2 = 0x1b,
  1300. CPU_SUBTYPE_XEON = 0x0c,
  1301. CPU_SUBTYPE_XEON_MP = 0x1c,
  1302. CPU_SUBTYPE_X86_ALL = 3,
  1303. CPU_SUBTYPE_X86_64_ALL = 3,
  1304. CPU_SUBTYPE_X86_ARCH1 = 4,
  1305. CPU_SUBTYPE_X86_64_H = 8
  1306. };
  1307. inline int CPU_SUBTYPE_INTEL(int Family, int Model) {
  1308. return Family | (Model << 4);
  1309. }
  1310. inline int CPU_SUBTYPE_INTEL_FAMILY(CPUSubTypeX86 ST) {
  1311. return ((int)ST) & 0x0f;
  1312. }
  1313. inline int CPU_SUBTYPE_INTEL_MODEL(CPUSubTypeX86 ST) { return ((int)ST) >> 4; }
  1314. enum { CPU_SUBTYPE_INTEL_FAMILY_MAX = 15, CPU_SUBTYPE_INTEL_MODEL_ALL = 0 };
  1315. enum CPUSubTypeARM {
  1316. CPU_SUBTYPE_ARM_ALL = 0,
  1317. CPU_SUBTYPE_ARM_V4T = 5,
  1318. CPU_SUBTYPE_ARM_V6 = 6,
  1319. CPU_SUBTYPE_ARM_V5 = 7,
  1320. CPU_SUBTYPE_ARM_V5TEJ = 7,
  1321. CPU_SUBTYPE_ARM_XSCALE = 8,
  1322. CPU_SUBTYPE_ARM_V7 = 9,
  1323. // unused ARM_V7F = 10,
  1324. CPU_SUBTYPE_ARM_V7S = 11,
  1325. CPU_SUBTYPE_ARM_V7K = 12,
  1326. CPU_SUBTYPE_ARM_V6M = 14,
  1327. CPU_SUBTYPE_ARM_V7M = 15,
  1328. CPU_SUBTYPE_ARM_V7EM = 16
  1329. };
  1330. enum CPUSubTypeARM64 {
  1331. CPU_SUBTYPE_ARM64_ALL = 0,
  1332. CPU_SUBTYPE_ARM64_V8 = 1,
  1333. CPU_SUBTYPE_ARM64E = 2,
  1334. };
  1335. enum CPUSubTypeARM64_32 { CPU_SUBTYPE_ARM64_32_V8 = 1 };
  1336. enum CPUSubTypeSPARC { CPU_SUBTYPE_SPARC_ALL = 0 };
  1337. enum CPUSubTypePowerPC {
  1338. CPU_SUBTYPE_POWERPC_ALL = 0,
  1339. CPU_SUBTYPE_POWERPC_601 = 1,
  1340. CPU_SUBTYPE_POWERPC_602 = 2,
  1341. CPU_SUBTYPE_POWERPC_603 = 3,
  1342. CPU_SUBTYPE_POWERPC_603e = 4,
  1343. CPU_SUBTYPE_POWERPC_603ev = 5,
  1344. CPU_SUBTYPE_POWERPC_604 = 6,
  1345. CPU_SUBTYPE_POWERPC_604e = 7,
  1346. CPU_SUBTYPE_POWERPC_620 = 8,
  1347. CPU_SUBTYPE_POWERPC_750 = 9,
  1348. CPU_SUBTYPE_POWERPC_7400 = 10,
  1349. CPU_SUBTYPE_POWERPC_7450 = 11,
  1350. CPU_SUBTYPE_POWERPC_970 = 100,
  1351. CPU_SUBTYPE_MC980000_ALL = CPU_SUBTYPE_POWERPC_ALL,
  1352. CPU_SUBTYPE_MC98601 = CPU_SUBTYPE_POWERPC_601
  1353. };
  1354. Expected<uint32_t> getCPUType(const Triple &T);
  1355. Expected<uint32_t> getCPUSubType(const Triple &T);
  1356. struct x86_thread_state32_t {
  1357. uint32_t eax;
  1358. uint32_t ebx;
  1359. uint32_t ecx;
  1360. uint32_t edx;
  1361. uint32_t edi;
  1362. uint32_t esi;
  1363. uint32_t ebp;
  1364. uint32_t esp;
  1365. uint32_t ss;
  1366. uint32_t eflags;
  1367. uint32_t eip;
  1368. uint32_t cs;
  1369. uint32_t ds;
  1370. uint32_t es;
  1371. uint32_t fs;
  1372. uint32_t gs;
  1373. };
  1374. struct x86_thread_state64_t {
  1375. uint64_t rax;
  1376. uint64_t rbx;
  1377. uint64_t rcx;
  1378. uint64_t rdx;
  1379. uint64_t rdi;
  1380. uint64_t rsi;
  1381. uint64_t rbp;
  1382. uint64_t rsp;
  1383. uint64_t r8;
  1384. uint64_t r9;
  1385. uint64_t r10;
  1386. uint64_t r11;
  1387. uint64_t r12;
  1388. uint64_t r13;
  1389. uint64_t r14;
  1390. uint64_t r15;
  1391. uint64_t rip;
  1392. uint64_t rflags;
  1393. uint64_t cs;
  1394. uint64_t fs;
  1395. uint64_t gs;
  1396. };
  1397. enum x86_fp_control_precis {
  1398. x86_FP_PREC_24B = 0,
  1399. x86_FP_PREC_53B = 2,
  1400. x86_FP_PREC_64B = 3
  1401. };
  1402. enum x86_fp_control_rc {
  1403. x86_FP_RND_NEAR = 0,
  1404. x86_FP_RND_DOWN = 1,
  1405. x86_FP_RND_UP = 2,
  1406. x86_FP_CHOP = 3
  1407. };
  1408. struct fp_control_t {
  1409. unsigned short invalid : 1, denorm : 1, zdiv : 1, ovrfl : 1, undfl : 1,
  1410. precis : 1, : 2, pc : 2, rc : 2, : 1, : 3;
  1411. };
  1412. struct fp_status_t {
  1413. unsigned short invalid : 1, denorm : 1, zdiv : 1, ovrfl : 1, undfl : 1,
  1414. precis : 1, stkflt : 1, errsumm : 1, c0 : 1, c1 : 1, c2 : 1, tos : 3,
  1415. c3 : 1, busy : 1;
  1416. };
  1417. struct mmst_reg_t {
  1418. char mmst_reg[10];
  1419. char mmst_rsrv[6];
  1420. };
  1421. struct xmm_reg_t {
  1422. char xmm_reg[16];
  1423. };
  1424. struct x86_float_state64_t {
  1425. int32_t fpu_reserved[2];
  1426. fp_control_t fpu_fcw;
  1427. fp_status_t fpu_fsw;
  1428. uint8_t fpu_ftw;
  1429. uint8_t fpu_rsrv1;
  1430. uint16_t fpu_fop;
  1431. uint32_t fpu_ip;
  1432. uint16_t fpu_cs;
  1433. uint16_t fpu_rsrv2;
  1434. uint32_t fpu_dp;
  1435. uint16_t fpu_ds;
  1436. uint16_t fpu_rsrv3;
  1437. uint32_t fpu_mxcsr;
  1438. uint32_t fpu_mxcsrmask;
  1439. mmst_reg_t fpu_stmm0;
  1440. mmst_reg_t fpu_stmm1;
  1441. mmst_reg_t fpu_stmm2;
  1442. mmst_reg_t fpu_stmm3;
  1443. mmst_reg_t fpu_stmm4;
  1444. mmst_reg_t fpu_stmm5;
  1445. mmst_reg_t fpu_stmm6;
  1446. mmst_reg_t fpu_stmm7;
  1447. xmm_reg_t fpu_xmm0;
  1448. xmm_reg_t fpu_xmm1;
  1449. xmm_reg_t fpu_xmm2;
  1450. xmm_reg_t fpu_xmm3;
  1451. xmm_reg_t fpu_xmm4;
  1452. xmm_reg_t fpu_xmm5;
  1453. xmm_reg_t fpu_xmm6;
  1454. xmm_reg_t fpu_xmm7;
  1455. xmm_reg_t fpu_xmm8;
  1456. xmm_reg_t fpu_xmm9;
  1457. xmm_reg_t fpu_xmm10;
  1458. xmm_reg_t fpu_xmm11;
  1459. xmm_reg_t fpu_xmm12;
  1460. xmm_reg_t fpu_xmm13;
  1461. xmm_reg_t fpu_xmm14;
  1462. xmm_reg_t fpu_xmm15;
  1463. char fpu_rsrv4[6 * 16];
  1464. uint32_t fpu_reserved1;
  1465. };
  1466. struct x86_exception_state64_t {
  1467. uint16_t trapno;
  1468. uint16_t cpu;
  1469. uint32_t err;
  1470. uint64_t faultvaddr;
  1471. };
  1472. inline void swapStruct(x86_thread_state32_t &x) {
  1473. sys::swapByteOrder(x.eax);
  1474. sys::swapByteOrder(x.ebx);
  1475. sys::swapByteOrder(x.ecx);
  1476. sys::swapByteOrder(x.edx);
  1477. sys::swapByteOrder(x.edi);
  1478. sys::swapByteOrder(x.esi);
  1479. sys::swapByteOrder(x.ebp);
  1480. sys::swapByteOrder(x.esp);
  1481. sys::swapByteOrder(x.ss);
  1482. sys::swapByteOrder(x.eflags);
  1483. sys::swapByteOrder(x.eip);
  1484. sys::swapByteOrder(x.cs);
  1485. sys::swapByteOrder(x.ds);
  1486. sys::swapByteOrder(x.es);
  1487. sys::swapByteOrder(x.fs);
  1488. sys::swapByteOrder(x.gs);
  1489. }
  1490. inline void swapStruct(x86_thread_state64_t &x) {
  1491. sys::swapByteOrder(x.rax);
  1492. sys::swapByteOrder(x.rbx);
  1493. sys::swapByteOrder(x.rcx);
  1494. sys::swapByteOrder(x.rdx);
  1495. sys::swapByteOrder(x.rdi);
  1496. sys::swapByteOrder(x.rsi);
  1497. sys::swapByteOrder(x.rbp);
  1498. sys::swapByteOrder(x.rsp);
  1499. sys::swapByteOrder(x.r8);
  1500. sys::swapByteOrder(x.r9);
  1501. sys::swapByteOrder(x.r10);
  1502. sys::swapByteOrder(x.r11);
  1503. sys::swapByteOrder(x.r12);
  1504. sys::swapByteOrder(x.r13);
  1505. sys::swapByteOrder(x.r14);
  1506. sys::swapByteOrder(x.r15);
  1507. sys::swapByteOrder(x.rip);
  1508. sys::swapByteOrder(x.rflags);
  1509. sys::swapByteOrder(x.cs);
  1510. sys::swapByteOrder(x.fs);
  1511. sys::swapByteOrder(x.gs);
  1512. }
  1513. inline void swapStruct(x86_float_state64_t &x) {
  1514. sys::swapByteOrder(x.fpu_reserved[0]);
  1515. sys::swapByteOrder(x.fpu_reserved[1]);
  1516. // TODO swap: fp_control_t fpu_fcw;
  1517. // TODO swap: fp_status_t fpu_fsw;
  1518. sys::swapByteOrder(x.fpu_fop);
  1519. sys::swapByteOrder(x.fpu_ip);
  1520. sys::swapByteOrder(x.fpu_cs);
  1521. sys::swapByteOrder(x.fpu_rsrv2);
  1522. sys::swapByteOrder(x.fpu_dp);
  1523. sys::swapByteOrder(x.fpu_ds);
  1524. sys::swapByteOrder(x.fpu_rsrv3);
  1525. sys::swapByteOrder(x.fpu_mxcsr);
  1526. sys::swapByteOrder(x.fpu_mxcsrmask);
  1527. sys::swapByteOrder(x.fpu_reserved1);
  1528. }
  1529. inline void swapStruct(x86_exception_state64_t &x) {
  1530. sys::swapByteOrder(x.trapno);
  1531. sys::swapByteOrder(x.cpu);
  1532. sys::swapByteOrder(x.err);
  1533. sys::swapByteOrder(x.faultvaddr);
  1534. }
  1535. struct x86_state_hdr_t {
  1536. uint32_t flavor;
  1537. uint32_t count;
  1538. };
  1539. struct x86_thread_state_t {
  1540. x86_state_hdr_t tsh;
  1541. union {
  1542. x86_thread_state64_t ts64;
  1543. x86_thread_state32_t ts32;
  1544. } uts;
  1545. };
  1546. struct x86_float_state_t {
  1547. x86_state_hdr_t fsh;
  1548. union {
  1549. x86_float_state64_t fs64;
  1550. } ufs;
  1551. };
  1552. struct x86_exception_state_t {
  1553. x86_state_hdr_t esh;
  1554. union {
  1555. x86_exception_state64_t es64;
  1556. } ues;
  1557. };
  1558. inline void swapStruct(x86_state_hdr_t &x) {
  1559. sys::swapByteOrder(x.flavor);
  1560. sys::swapByteOrder(x.count);
  1561. }
  1562. enum X86ThreadFlavors {
  1563. x86_THREAD_STATE32 = 1,
  1564. x86_FLOAT_STATE32 = 2,
  1565. x86_EXCEPTION_STATE32 = 3,
  1566. x86_THREAD_STATE64 = 4,
  1567. x86_FLOAT_STATE64 = 5,
  1568. x86_EXCEPTION_STATE64 = 6,
  1569. x86_THREAD_STATE = 7,
  1570. x86_FLOAT_STATE = 8,
  1571. x86_EXCEPTION_STATE = 9,
  1572. x86_DEBUG_STATE32 = 10,
  1573. x86_DEBUG_STATE64 = 11,
  1574. x86_DEBUG_STATE = 12
  1575. };
  1576. inline void swapStruct(x86_thread_state_t &x) {
  1577. swapStruct(x.tsh);
  1578. if (x.tsh.flavor == x86_THREAD_STATE64)
  1579. swapStruct(x.uts.ts64);
  1580. }
  1581. inline void swapStruct(x86_float_state_t &x) {
  1582. swapStruct(x.fsh);
  1583. if (x.fsh.flavor == x86_FLOAT_STATE64)
  1584. swapStruct(x.ufs.fs64);
  1585. }
  1586. inline void swapStruct(x86_exception_state_t &x) {
  1587. swapStruct(x.esh);
  1588. if (x.esh.flavor == x86_EXCEPTION_STATE64)
  1589. swapStruct(x.ues.es64);
  1590. }
  1591. const uint32_t x86_THREAD_STATE32_COUNT =
  1592. sizeof(x86_thread_state32_t) / sizeof(uint32_t);
  1593. const uint32_t x86_THREAD_STATE64_COUNT =
  1594. sizeof(x86_thread_state64_t) / sizeof(uint32_t);
  1595. const uint32_t x86_FLOAT_STATE64_COUNT =
  1596. sizeof(x86_float_state64_t) / sizeof(uint32_t);
  1597. const uint32_t x86_EXCEPTION_STATE64_COUNT =
  1598. sizeof(x86_exception_state64_t) / sizeof(uint32_t);
  1599. const uint32_t x86_THREAD_STATE_COUNT =
  1600. sizeof(x86_thread_state_t) / sizeof(uint32_t);
  1601. const uint32_t x86_FLOAT_STATE_COUNT =
  1602. sizeof(x86_float_state_t) / sizeof(uint32_t);
  1603. const uint32_t x86_EXCEPTION_STATE_COUNT =
  1604. sizeof(x86_exception_state_t) / sizeof(uint32_t);
  1605. struct arm_thread_state32_t {
  1606. uint32_t r[13];
  1607. uint32_t sp;
  1608. uint32_t lr;
  1609. uint32_t pc;
  1610. uint32_t cpsr;
  1611. };
  1612. inline void swapStruct(arm_thread_state32_t &x) {
  1613. for (int i = 0; i < 13; i++)
  1614. sys::swapByteOrder(x.r[i]);
  1615. sys::swapByteOrder(x.sp);
  1616. sys::swapByteOrder(x.lr);
  1617. sys::swapByteOrder(x.pc);
  1618. sys::swapByteOrder(x.cpsr);
  1619. }
  1620. struct arm_thread_state64_t {
  1621. uint64_t x[29];
  1622. uint64_t fp;
  1623. uint64_t lr;
  1624. uint64_t sp;
  1625. uint64_t pc;
  1626. uint32_t cpsr;
  1627. uint32_t pad;
  1628. };
  1629. inline void swapStruct(arm_thread_state64_t &x) {
  1630. for (int i = 0; i < 29; i++)
  1631. sys::swapByteOrder(x.x[i]);
  1632. sys::swapByteOrder(x.fp);
  1633. sys::swapByteOrder(x.lr);
  1634. sys::swapByteOrder(x.sp);
  1635. sys::swapByteOrder(x.pc);
  1636. sys::swapByteOrder(x.cpsr);
  1637. }
  1638. struct arm_state_hdr_t {
  1639. uint32_t flavor;
  1640. uint32_t count;
  1641. };
  1642. struct arm_thread_state_t {
  1643. arm_state_hdr_t tsh;
  1644. union {
  1645. arm_thread_state32_t ts32;
  1646. } uts;
  1647. };
  1648. inline void swapStruct(arm_state_hdr_t &x) {
  1649. sys::swapByteOrder(x.flavor);
  1650. sys::swapByteOrder(x.count);
  1651. }
  1652. enum ARMThreadFlavors {
  1653. ARM_THREAD_STATE = 1,
  1654. ARM_VFP_STATE = 2,
  1655. ARM_EXCEPTION_STATE = 3,
  1656. ARM_DEBUG_STATE = 4,
  1657. ARN_THREAD_STATE_NONE = 5,
  1658. ARM_THREAD_STATE64 = 6,
  1659. ARM_EXCEPTION_STATE64 = 7
  1660. };
  1661. inline void swapStruct(arm_thread_state_t &x) {
  1662. swapStruct(x.tsh);
  1663. if (x.tsh.flavor == ARM_THREAD_STATE)
  1664. swapStruct(x.uts.ts32);
  1665. }
  1666. const uint32_t ARM_THREAD_STATE_COUNT =
  1667. sizeof(arm_thread_state32_t) / sizeof(uint32_t);
  1668. const uint32_t ARM_THREAD_STATE64_COUNT =
  1669. sizeof(arm_thread_state64_t) / sizeof(uint32_t);
  1670. struct ppc_thread_state32_t {
  1671. uint32_t srr0;
  1672. uint32_t srr1;
  1673. uint32_t r0;
  1674. uint32_t r1;
  1675. uint32_t r2;
  1676. uint32_t r3;
  1677. uint32_t r4;
  1678. uint32_t r5;
  1679. uint32_t r6;
  1680. uint32_t r7;
  1681. uint32_t r8;
  1682. uint32_t r9;
  1683. uint32_t r10;
  1684. uint32_t r11;
  1685. uint32_t r12;
  1686. uint32_t r13;
  1687. uint32_t r14;
  1688. uint32_t r15;
  1689. uint32_t r16;
  1690. uint32_t r17;
  1691. uint32_t r18;
  1692. uint32_t r19;
  1693. uint32_t r20;
  1694. uint32_t r21;
  1695. uint32_t r22;
  1696. uint32_t r23;
  1697. uint32_t r24;
  1698. uint32_t r25;
  1699. uint32_t r26;
  1700. uint32_t r27;
  1701. uint32_t r28;
  1702. uint32_t r29;
  1703. uint32_t r30;
  1704. uint32_t r31;
  1705. uint32_t ct;
  1706. uint32_t xer;
  1707. uint32_t lr;
  1708. uint32_t ctr;
  1709. uint32_t mq;
  1710. uint32_t vrsave;
  1711. };
  1712. inline void swapStruct(ppc_thread_state32_t &x) {
  1713. sys::swapByteOrder(x.srr0);
  1714. sys::swapByteOrder(x.srr1);
  1715. sys::swapByteOrder(x.r0);
  1716. sys::swapByteOrder(x.r1);
  1717. sys::swapByteOrder(x.r2);
  1718. sys::swapByteOrder(x.r3);
  1719. sys::swapByteOrder(x.r4);
  1720. sys::swapByteOrder(x.r5);
  1721. sys::swapByteOrder(x.r6);
  1722. sys::swapByteOrder(x.r7);
  1723. sys::swapByteOrder(x.r8);
  1724. sys::swapByteOrder(x.r9);
  1725. sys::swapByteOrder(x.r10);
  1726. sys::swapByteOrder(x.r11);
  1727. sys::swapByteOrder(x.r12);
  1728. sys::swapByteOrder(x.r13);
  1729. sys::swapByteOrder(x.r14);
  1730. sys::swapByteOrder(x.r15);
  1731. sys::swapByteOrder(x.r16);
  1732. sys::swapByteOrder(x.r17);
  1733. sys::swapByteOrder(x.r18);
  1734. sys::swapByteOrder(x.r19);
  1735. sys::swapByteOrder(x.r20);
  1736. sys::swapByteOrder(x.r21);
  1737. sys::swapByteOrder(x.r22);
  1738. sys::swapByteOrder(x.r23);
  1739. sys::swapByteOrder(x.r24);
  1740. sys::swapByteOrder(x.r25);
  1741. sys::swapByteOrder(x.r26);
  1742. sys::swapByteOrder(x.r27);
  1743. sys::swapByteOrder(x.r28);
  1744. sys::swapByteOrder(x.r29);
  1745. sys::swapByteOrder(x.r30);
  1746. sys::swapByteOrder(x.r31);
  1747. sys::swapByteOrder(x.ct);
  1748. sys::swapByteOrder(x.xer);
  1749. sys::swapByteOrder(x.lr);
  1750. sys::swapByteOrder(x.ctr);
  1751. sys::swapByteOrder(x.mq);
  1752. sys::swapByteOrder(x.vrsave);
  1753. }
  1754. struct ppc_state_hdr_t {
  1755. uint32_t flavor;
  1756. uint32_t count;
  1757. };
  1758. struct ppc_thread_state_t {
  1759. ppc_state_hdr_t tsh;
  1760. union {
  1761. ppc_thread_state32_t ts32;
  1762. } uts;
  1763. };
  1764. inline void swapStruct(ppc_state_hdr_t &x) {
  1765. sys::swapByteOrder(x.flavor);
  1766. sys::swapByteOrder(x.count);
  1767. }
  1768. enum PPCThreadFlavors {
  1769. PPC_THREAD_STATE = 1,
  1770. PPC_FLOAT_STATE = 2,
  1771. PPC_EXCEPTION_STATE = 3,
  1772. PPC_VECTOR_STATE = 4,
  1773. PPC_THREAD_STATE64 = 5,
  1774. PPC_EXCEPTION_STATE64 = 6,
  1775. PPC_THREAD_STATE_NONE = 7
  1776. };
  1777. inline void swapStruct(ppc_thread_state_t &x) {
  1778. swapStruct(x.tsh);
  1779. if (x.tsh.flavor == PPC_THREAD_STATE)
  1780. swapStruct(x.uts.ts32);
  1781. }
  1782. const uint32_t PPC_THREAD_STATE_COUNT =
  1783. sizeof(ppc_thread_state32_t) / sizeof(uint32_t);
  1784. // Define a union of all load command structs
  1785. #define LOAD_COMMAND_STRUCT(LCStruct) LCStruct LCStruct##_data;
  1786. LLVM_PACKED_START
  1787. union alignas(4) macho_load_command {
  1788. #include "llvm/BinaryFormat/MachO.def"
  1789. };
  1790. LLVM_PACKED_END
  1791. } // end namespace MachO
  1792. } // end namespace llvm
  1793. #endif
  1794. #ifdef __GNUC__
  1795. #pragma GCC diagnostic pop
  1796. #endif