MachO.h 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222
  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_INVALID = 0xff,
  368. GENERIC_RELOC_VANILLA = 0,
  369. GENERIC_RELOC_PAIR = 1,
  370. GENERIC_RELOC_SECTDIFF = 2,
  371. GENERIC_RELOC_PB_LA_PTR = 3,
  372. GENERIC_RELOC_LOCAL_SECTDIFF = 4,
  373. GENERIC_RELOC_TLV = 5,
  374. // Constant values for the r_type field in a PowerPC architecture
  375. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  376. // structure.
  377. PPC_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
  378. PPC_RELOC_PAIR = GENERIC_RELOC_PAIR,
  379. PPC_RELOC_BR14 = 2,
  380. PPC_RELOC_BR24 = 3,
  381. PPC_RELOC_HI16 = 4,
  382. PPC_RELOC_LO16 = 5,
  383. PPC_RELOC_HA16 = 6,
  384. PPC_RELOC_LO14 = 7,
  385. PPC_RELOC_SECTDIFF = 8,
  386. PPC_RELOC_PB_LA_PTR = 9,
  387. PPC_RELOC_HI16_SECTDIFF = 10,
  388. PPC_RELOC_LO16_SECTDIFF = 11,
  389. PPC_RELOC_HA16_SECTDIFF = 12,
  390. PPC_RELOC_JBSR = 13,
  391. PPC_RELOC_LO14_SECTDIFF = 14,
  392. PPC_RELOC_LOCAL_SECTDIFF = 15,
  393. // Constant values for the r_type field in an ARM architecture
  394. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  395. // structure.
  396. ARM_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
  397. ARM_RELOC_PAIR = GENERIC_RELOC_PAIR,
  398. ARM_RELOC_SECTDIFF = GENERIC_RELOC_SECTDIFF,
  399. ARM_RELOC_LOCAL_SECTDIFF = 3,
  400. ARM_RELOC_PB_LA_PTR = 4,
  401. ARM_RELOC_BR24 = 5,
  402. ARM_THUMB_RELOC_BR22 = 6,
  403. ARM_THUMB_32BIT_BRANCH = 7, // obsolete
  404. ARM_RELOC_HALF = 8,
  405. ARM_RELOC_HALF_SECTDIFF = 9,
  406. // Constant values for the r_type field in an ARM64 architecture
  407. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  408. // structure.
  409. // For pointers.
  410. ARM64_RELOC_UNSIGNED = 0,
  411. // Must be followed by an ARM64_RELOC_UNSIGNED
  412. ARM64_RELOC_SUBTRACTOR = 1,
  413. // A B/BL instruction with 26-bit displacement.
  414. ARM64_RELOC_BRANCH26 = 2,
  415. // PC-rel distance to page of target.
  416. ARM64_RELOC_PAGE21 = 3,
  417. // Offset within page, scaled by r_length.
  418. ARM64_RELOC_PAGEOFF12 = 4,
  419. // PC-rel distance to page of GOT slot.
  420. ARM64_RELOC_GOT_LOAD_PAGE21 = 5,
  421. // Offset within page of GOT slot, scaled by r_length.
  422. ARM64_RELOC_GOT_LOAD_PAGEOFF12 = 6,
  423. // For pointers to GOT slots.
  424. ARM64_RELOC_POINTER_TO_GOT = 7,
  425. // PC-rel distance to page of TLVP slot.
  426. ARM64_RELOC_TLVP_LOAD_PAGE21 = 8,
  427. // Offset within page of TLVP slot, scaled by r_length.
  428. ARM64_RELOC_TLVP_LOAD_PAGEOFF12 = 9,
  429. // Must be followed by ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12.
  430. ARM64_RELOC_ADDEND = 10,
  431. // Constant values for the r_type field in an x86_64 architecture
  432. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  433. // structure
  434. X86_64_RELOC_UNSIGNED = 0,
  435. X86_64_RELOC_SIGNED = 1,
  436. X86_64_RELOC_BRANCH = 2,
  437. X86_64_RELOC_GOT_LOAD = 3,
  438. X86_64_RELOC_GOT = 4,
  439. X86_64_RELOC_SUBTRACTOR = 5,
  440. X86_64_RELOC_SIGNED_1 = 6,
  441. X86_64_RELOC_SIGNED_2 = 7,
  442. X86_64_RELOC_SIGNED_4 = 8,
  443. X86_64_RELOC_TLV = 9
  444. };
  445. // Values for segment_command.initprot.
  446. // From <mach/vm_prot.h>
  447. enum { VM_PROT_READ = 0x1, VM_PROT_WRITE = 0x2, VM_PROT_EXECUTE = 0x4 };
  448. // Values for platform field in build_version_command.
  449. enum PlatformType {
  450. PLATFORM_UNKNOWN = 0,
  451. PLATFORM_MACOS = 1,
  452. PLATFORM_IOS = 2,
  453. PLATFORM_TVOS = 3,
  454. PLATFORM_WATCHOS = 4,
  455. PLATFORM_BRIDGEOS = 5,
  456. PLATFORM_MACCATALYST = 6,
  457. PLATFORM_IOSSIMULATOR = 7,
  458. PLATFORM_TVOSSIMULATOR = 8,
  459. PLATFORM_WATCHOSSIMULATOR = 9,
  460. PLATFORM_DRIVERKIT = 10,
  461. };
  462. // Values for tools enum in build_tool_version.
  463. enum { TOOL_CLANG = 1, TOOL_SWIFT = 2, TOOL_LD = 3 };
  464. // Structs from <mach-o/loader.h>
  465. struct mach_header {
  466. uint32_t magic;
  467. uint32_t cputype;
  468. uint32_t cpusubtype;
  469. uint32_t filetype;
  470. uint32_t ncmds;
  471. uint32_t sizeofcmds;
  472. uint32_t flags;
  473. };
  474. struct mach_header_64 {
  475. uint32_t magic;
  476. uint32_t cputype;
  477. uint32_t cpusubtype;
  478. uint32_t filetype;
  479. uint32_t ncmds;
  480. uint32_t sizeofcmds;
  481. uint32_t flags;
  482. uint32_t reserved;
  483. };
  484. struct load_command {
  485. uint32_t cmd;
  486. uint32_t cmdsize;
  487. };
  488. struct segment_command {
  489. uint32_t cmd;
  490. uint32_t cmdsize;
  491. char segname[16];
  492. uint32_t vmaddr;
  493. uint32_t vmsize;
  494. uint32_t fileoff;
  495. uint32_t filesize;
  496. uint32_t maxprot;
  497. uint32_t initprot;
  498. uint32_t nsects;
  499. uint32_t flags;
  500. };
  501. struct segment_command_64 {
  502. uint32_t cmd;
  503. uint32_t cmdsize;
  504. char segname[16];
  505. uint64_t vmaddr;
  506. uint64_t vmsize;
  507. uint64_t fileoff;
  508. uint64_t filesize;
  509. uint32_t maxprot;
  510. uint32_t initprot;
  511. uint32_t nsects;
  512. uint32_t flags;
  513. };
  514. struct section {
  515. char sectname[16];
  516. char segname[16];
  517. uint32_t addr;
  518. uint32_t size;
  519. uint32_t offset;
  520. uint32_t align;
  521. uint32_t reloff;
  522. uint32_t nreloc;
  523. uint32_t flags;
  524. uint32_t reserved1;
  525. uint32_t reserved2;
  526. };
  527. struct section_64 {
  528. char sectname[16];
  529. char segname[16];
  530. uint64_t addr;
  531. uint64_t size;
  532. uint32_t offset;
  533. uint32_t align;
  534. uint32_t reloff;
  535. uint32_t nreloc;
  536. uint32_t flags;
  537. uint32_t reserved1;
  538. uint32_t reserved2;
  539. uint32_t reserved3;
  540. };
  541. inline bool isVirtualSection(uint8_t type) {
  542. return (type == MachO::S_ZEROFILL || type == MachO::S_GB_ZEROFILL ||
  543. type == MachO::S_THREAD_LOCAL_ZEROFILL);
  544. }
  545. struct fvmlib {
  546. uint32_t name;
  547. uint32_t minor_version;
  548. uint32_t header_addr;
  549. };
  550. // The fvmlib_command is obsolete and no longer supported.
  551. struct fvmlib_command {
  552. uint32_t cmd;
  553. uint32_t cmdsize;
  554. struct fvmlib fvmlib;
  555. };
  556. struct dylib {
  557. uint32_t name;
  558. uint32_t timestamp;
  559. uint32_t current_version;
  560. uint32_t compatibility_version;
  561. };
  562. struct dylib_command {
  563. uint32_t cmd;
  564. uint32_t cmdsize;
  565. struct dylib dylib;
  566. };
  567. struct sub_framework_command {
  568. uint32_t cmd;
  569. uint32_t cmdsize;
  570. uint32_t umbrella;
  571. };
  572. struct sub_client_command {
  573. uint32_t cmd;
  574. uint32_t cmdsize;
  575. uint32_t client;
  576. };
  577. struct sub_umbrella_command {
  578. uint32_t cmd;
  579. uint32_t cmdsize;
  580. uint32_t sub_umbrella;
  581. };
  582. struct sub_library_command {
  583. uint32_t cmd;
  584. uint32_t cmdsize;
  585. uint32_t sub_library;
  586. };
  587. // The prebound_dylib_command is obsolete and no longer supported.
  588. struct prebound_dylib_command {
  589. uint32_t cmd;
  590. uint32_t cmdsize;
  591. uint32_t name;
  592. uint32_t nmodules;
  593. uint32_t linked_modules;
  594. };
  595. struct dylinker_command {
  596. uint32_t cmd;
  597. uint32_t cmdsize;
  598. uint32_t name;
  599. };
  600. struct thread_command {
  601. uint32_t cmd;
  602. uint32_t cmdsize;
  603. };
  604. struct routines_command {
  605. uint32_t cmd;
  606. uint32_t cmdsize;
  607. uint32_t init_address;
  608. uint32_t init_module;
  609. uint32_t reserved1;
  610. uint32_t reserved2;
  611. uint32_t reserved3;
  612. uint32_t reserved4;
  613. uint32_t reserved5;
  614. uint32_t reserved6;
  615. };
  616. struct routines_command_64 {
  617. uint32_t cmd;
  618. uint32_t cmdsize;
  619. uint64_t init_address;
  620. uint64_t init_module;
  621. uint64_t reserved1;
  622. uint64_t reserved2;
  623. uint64_t reserved3;
  624. uint64_t reserved4;
  625. uint64_t reserved5;
  626. uint64_t reserved6;
  627. };
  628. struct symtab_command {
  629. uint32_t cmd;
  630. uint32_t cmdsize;
  631. uint32_t symoff;
  632. uint32_t nsyms;
  633. uint32_t stroff;
  634. uint32_t strsize;
  635. };
  636. struct dysymtab_command {
  637. uint32_t cmd;
  638. uint32_t cmdsize;
  639. uint32_t ilocalsym;
  640. uint32_t nlocalsym;
  641. uint32_t iextdefsym;
  642. uint32_t nextdefsym;
  643. uint32_t iundefsym;
  644. uint32_t nundefsym;
  645. uint32_t tocoff;
  646. uint32_t ntoc;
  647. uint32_t modtaboff;
  648. uint32_t nmodtab;
  649. uint32_t extrefsymoff;
  650. uint32_t nextrefsyms;
  651. uint32_t indirectsymoff;
  652. uint32_t nindirectsyms;
  653. uint32_t extreloff;
  654. uint32_t nextrel;
  655. uint32_t locreloff;
  656. uint32_t nlocrel;
  657. };
  658. struct dylib_table_of_contents {
  659. uint32_t symbol_index;
  660. uint32_t module_index;
  661. };
  662. struct dylib_module {
  663. uint32_t module_name;
  664. uint32_t iextdefsym;
  665. uint32_t nextdefsym;
  666. uint32_t irefsym;
  667. uint32_t nrefsym;
  668. uint32_t ilocalsym;
  669. uint32_t nlocalsym;
  670. uint32_t iextrel;
  671. uint32_t nextrel;
  672. uint32_t iinit_iterm;
  673. uint32_t ninit_nterm;
  674. uint32_t objc_module_info_addr;
  675. uint32_t objc_module_info_size;
  676. };
  677. struct dylib_module_64 {
  678. uint32_t module_name;
  679. uint32_t iextdefsym;
  680. uint32_t nextdefsym;
  681. uint32_t irefsym;
  682. uint32_t nrefsym;
  683. uint32_t ilocalsym;
  684. uint32_t nlocalsym;
  685. uint32_t iextrel;
  686. uint32_t nextrel;
  687. uint32_t iinit_iterm;
  688. uint32_t ninit_nterm;
  689. uint32_t objc_module_info_size;
  690. uint64_t objc_module_info_addr;
  691. };
  692. struct dylib_reference {
  693. uint32_t isym : 24, flags : 8;
  694. };
  695. // The twolevel_hints_command is obsolete and no longer supported.
  696. struct twolevel_hints_command {
  697. uint32_t cmd;
  698. uint32_t cmdsize;
  699. uint32_t offset;
  700. uint32_t nhints;
  701. };
  702. // The twolevel_hints_command is obsolete and no longer supported.
  703. struct twolevel_hint {
  704. uint32_t isub_image : 8, itoc : 24;
  705. };
  706. // The prebind_cksum_command is obsolete and no longer supported.
  707. struct prebind_cksum_command {
  708. uint32_t cmd;
  709. uint32_t cmdsize;
  710. uint32_t cksum;
  711. };
  712. struct uuid_command {
  713. uint32_t cmd;
  714. uint32_t cmdsize;
  715. uint8_t uuid[16];
  716. };
  717. struct rpath_command {
  718. uint32_t cmd;
  719. uint32_t cmdsize;
  720. uint32_t path;
  721. };
  722. struct linkedit_data_command {
  723. uint32_t cmd;
  724. uint32_t cmdsize;
  725. uint32_t dataoff;
  726. uint32_t datasize;
  727. };
  728. struct data_in_code_entry {
  729. uint32_t offset;
  730. uint16_t length;
  731. uint16_t kind;
  732. };
  733. struct source_version_command {
  734. uint32_t cmd;
  735. uint32_t cmdsize;
  736. uint64_t version;
  737. };
  738. struct encryption_info_command {
  739. uint32_t cmd;
  740. uint32_t cmdsize;
  741. uint32_t cryptoff;
  742. uint32_t cryptsize;
  743. uint32_t cryptid;
  744. };
  745. struct encryption_info_command_64 {
  746. uint32_t cmd;
  747. uint32_t cmdsize;
  748. uint32_t cryptoff;
  749. uint32_t cryptsize;
  750. uint32_t cryptid;
  751. uint32_t pad;
  752. };
  753. struct version_min_command {
  754. uint32_t cmd; // LC_VERSION_MIN_MACOSX or
  755. // LC_VERSION_MIN_IPHONEOS
  756. uint32_t cmdsize; // sizeof(struct version_min_command)
  757. uint32_t version; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  758. uint32_t sdk; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  759. };
  760. struct note_command {
  761. uint32_t cmd; // LC_NOTE
  762. uint32_t cmdsize; // sizeof(struct note_command)
  763. char data_owner[16]; // owner name for this LC_NOTE
  764. uint64_t offset; // file offset of this data
  765. uint64_t size; // length of data region
  766. };
  767. struct build_tool_version {
  768. uint32_t tool; // enum for the tool
  769. uint32_t version; // version of the tool
  770. };
  771. struct build_version_command {
  772. uint32_t cmd; // LC_BUILD_VERSION
  773. uint32_t cmdsize; // sizeof(struct build_version_command) +
  774. // ntools * sizeof(struct build_tool_version)
  775. uint32_t platform; // platform
  776. uint32_t minos; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  777. uint32_t sdk; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  778. uint32_t ntools; // number of tool entries following this
  779. };
  780. struct dyld_info_command {
  781. uint32_t cmd;
  782. uint32_t cmdsize;
  783. uint32_t rebase_off;
  784. uint32_t rebase_size;
  785. uint32_t bind_off;
  786. uint32_t bind_size;
  787. uint32_t weak_bind_off;
  788. uint32_t weak_bind_size;
  789. uint32_t lazy_bind_off;
  790. uint32_t lazy_bind_size;
  791. uint32_t export_off;
  792. uint32_t export_size;
  793. };
  794. struct linker_option_command {
  795. uint32_t cmd;
  796. uint32_t cmdsize;
  797. uint32_t count;
  798. };
  799. // The symseg_command is obsolete and no longer supported.
  800. struct symseg_command {
  801. uint32_t cmd;
  802. uint32_t cmdsize;
  803. uint32_t offset;
  804. uint32_t size;
  805. };
  806. // The ident_command is obsolete and no longer supported.
  807. struct ident_command {
  808. uint32_t cmd;
  809. uint32_t cmdsize;
  810. };
  811. // The fvmfile_command is obsolete and no longer supported.
  812. struct fvmfile_command {
  813. uint32_t cmd;
  814. uint32_t cmdsize;
  815. uint32_t name;
  816. uint32_t header_addr;
  817. };
  818. struct tlv_descriptor_32 {
  819. uint32_t thunk;
  820. uint32_t key;
  821. uint32_t offset;
  822. };
  823. struct tlv_descriptor_64 {
  824. uint64_t thunk;
  825. uint64_t key;
  826. uint64_t offset;
  827. };
  828. struct tlv_descriptor {
  829. uintptr_t thunk;
  830. uintptr_t key;
  831. uintptr_t offset;
  832. };
  833. struct entry_point_command {
  834. uint32_t cmd;
  835. uint32_t cmdsize;
  836. uint64_t entryoff;
  837. uint64_t stacksize;
  838. };
  839. // Structs from <mach-o/fat.h>
  840. struct fat_header {
  841. uint32_t magic;
  842. uint32_t nfat_arch;
  843. };
  844. struct fat_arch {
  845. uint32_t cputype;
  846. uint32_t cpusubtype;
  847. uint32_t offset;
  848. uint32_t size;
  849. uint32_t align;
  850. };
  851. struct fat_arch_64 {
  852. uint32_t cputype;
  853. uint32_t cpusubtype;
  854. uint64_t offset;
  855. uint64_t size;
  856. uint32_t align;
  857. uint32_t reserved;
  858. };
  859. // Structs from <mach-o/reloc.h>
  860. struct relocation_info {
  861. int32_t r_address;
  862. uint32_t r_symbolnum : 24, r_pcrel : 1, r_length : 2, r_extern : 1,
  863. r_type : 4;
  864. };
  865. struct scattered_relocation_info {
  866. #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
  867. uint32_t r_scattered : 1, r_pcrel : 1, r_length : 2, r_type : 4,
  868. r_address : 24;
  869. #else
  870. uint32_t r_address : 24, r_type : 4, r_length : 2, r_pcrel : 1,
  871. r_scattered : 1;
  872. #endif
  873. int32_t r_value;
  874. };
  875. // Structs NOT from <mach-o/reloc.h>, but that make LLVM's life easier
  876. struct any_relocation_info {
  877. uint32_t r_word0, r_word1;
  878. };
  879. // Structs from <mach-o/nlist.h>
  880. struct nlist_base {
  881. uint32_t n_strx;
  882. uint8_t n_type;
  883. uint8_t n_sect;
  884. uint16_t n_desc;
  885. };
  886. struct nlist {
  887. uint32_t n_strx;
  888. uint8_t n_type;
  889. uint8_t n_sect;
  890. int16_t n_desc;
  891. uint32_t n_value;
  892. };
  893. struct nlist_64 {
  894. uint32_t n_strx;
  895. uint8_t n_type;
  896. uint8_t n_sect;
  897. uint16_t n_desc;
  898. uint64_t n_value;
  899. };
  900. // Byte order swapping functions for MachO structs
  901. inline void swapStruct(fat_header &mh) {
  902. sys::swapByteOrder(mh.magic);
  903. sys::swapByteOrder(mh.nfat_arch);
  904. }
  905. inline void swapStruct(fat_arch &mh) {
  906. sys::swapByteOrder(mh.cputype);
  907. sys::swapByteOrder(mh.cpusubtype);
  908. sys::swapByteOrder(mh.offset);
  909. sys::swapByteOrder(mh.size);
  910. sys::swapByteOrder(mh.align);
  911. }
  912. inline void swapStruct(fat_arch_64 &mh) {
  913. sys::swapByteOrder(mh.cputype);
  914. sys::swapByteOrder(mh.cpusubtype);
  915. sys::swapByteOrder(mh.offset);
  916. sys::swapByteOrder(mh.size);
  917. sys::swapByteOrder(mh.align);
  918. sys::swapByteOrder(mh.reserved);
  919. }
  920. inline void swapStruct(mach_header &mh) {
  921. sys::swapByteOrder(mh.magic);
  922. sys::swapByteOrder(mh.cputype);
  923. sys::swapByteOrder(mh.cpusubtype);
  924. sys::swapByteOrder(mh.filetype);
  925. sys::swapByteOrder(mh.ncmds);
  926. sys::swapByteOrder(mh.sizeofcmds);
  927. sys::swapByteOrder(mh.flags);
  928. }
  929. inline void swapStruct(mach_header_64 &H) {
  930. sys::swapByteOrder(H.magic);
  931. sys::swapByteOrder(H.cputype);
  932. sys::swapByteOrder(H.cpusubtype);
  933. sys::swapByteOrder(H.filetype);
  934. sys::swapByteOrder(H.ncmds);
  935. sys::swapByteOrder(H.sizeofcmds);
  936. sys::swapByteOrder(H.flags);
  937. sys::swapByteOrder(H.reserved);
  938. }
  939. inline void swapStruct(load_command &lc) {
  940. sys::swapByteOrder(lc.cmd);
  941. sys::swapByteOrder(lc.cmdsize);
  942. }
  943. inline void swapStruct(symtab_command &lc) {
  944. sys::swapByteOrder(lc.cmd);
  945. sys::swapByteOrder(lc.cmdsize);
  946. sys::swapByteOrder(lc.symoff);
  947. sys::swapByteOrder(lc.nsyms);
  948. sys::swapByteOrder(lc.stroff);
  949. sys::swapByteOrder(lc.strsize);
  950. }
  951. inline void swapStruct(segment_command_64 &seg) {
  952. sys::swapByteOrder(seg.cmd);
  953. sys::swapByteOrder(seg.cmdsize);
  954. sys::swapByteOrder(seg.vmaddr);
  955. sys::swapByteOrder(seg.vmsize);
  956. sys::swapByteOrder(seg.fileoff);
  957. sys::swapByteOrder(seg.filesize);
  958. sys::swapByteOrder(seg.maxprot);
  959. sys::swapByteOrder(seg.initprot);
  960. sys::swapByteOrder(seg.nsects);
  961. sys::swapByteOrder(seg.flags);
  962. }
  963. inline void swapStruct(segment_command &seg) {
  964. sys::swapByteOrder(seg.cmd);
  965. sys::swapByteOrder(seg.cmdsize);
  966. sys::swapByteOrder(seg.vmaddr);
  967. sys::swapByteOrder(seg.vmsize);
  968. sys::swapByteOrder(seg.fileoff);
  969. sys::swapByteOrder(seg.filesize);
  970. sys::swapByteOrder(seg.maxprot);
  971. sys::swapByteOrder(seg.initprot);
  972. sys::swapByteOrder(seg.nsects);
  973. sys::swapByteOrder(seg.flags);
  974. }
  975. inline void swapStruct(section_64 &sect) {
  976. sys::swapByteOrder(sect.addr);
  977. sys::swapByteOrder(sect.size);
  978. sys::swapByteOrder(sect.offset);
  979. sys::swapByteOrder(sect.align);
  980. sys::swapByteOrder(sect.reloff);
  981. sys::swapByteOrder(sect.nreloc);
  982. sys::swapByteOrder(sect.flags);
  983. sys::swapByteOrder(sect.reserved1);
  984. sys::swapByteOrder(sect.reserved2);
  985. }
  986. inline void swapStruct(section &sect) {
  987. sys::swapByteOrder(sect.addr);
  988. sys::swapByteOrder(sect.size);
  989. sys::swapByteOrder(sect.offset);
  990. sys::swapByteOrder(sect.align);
  991. sys::swapByteOrder(sect.reloff);
  992. sys::swapByteOrder(sect.nreloc);
  993. sys::swapByteOrder(sect.flags);
  994. sys::swapByteOrder(sect.reserved1);
  995. sys::swapByteOrder(sect.reserved2);
  996. }
  997. inline void swapStruct(dyld_info_command &info) {
  998. sys::swapByteOrder(info.cmd);
  999. sys::swapByteOrder(info.cmdsize);
  1000. sys::swapByteOrder(info.rebase_off);
  1001. sys::swapByteOrder(info.rebase_size);
  1002. sys::swapByteOrder(info.bind_off);
  1003. sys::swapByteOrder(info.bind_size);
  1004. sys::swapByteOrder(info.weak_bind_off);
  1005. sys::swapByteOrder(info.weak_bind_size);
  1006. sys::swapByteOrder(info.lazy_bind_off);
  1007. sys::swapByteOrder(info.lazy_bind_size);
  1008. sys::swapByteOrder(info.export_off);
  1009. sys::swapByteOrder(info.export_size);
  1010. }
  1011. inline void swapStruct(dylib_command &d) {
  1012. sys::swapByteOrder(d.cmd);
  1013. sys::swapByteOrder(d.cmdsize);
  1014. sys::swapByteOrder(d.dylib.name);
  1015. sys::swapByteOrder(d.dylib.timestamp);
  1016. sys::swapByteOrder(d.dylib.current_version);
  1017. sys::swapByteOrder(d.dylib.compatibility_version);
  1018. }
  1019. inline void swapStruct(sub_framework_command &s) {
  1020. sys::swapByteOrder(s.cmd);
  1021. sys::swapByteOrder(s.cmdsize);
  1022. sys::swapByteOrder(s.umbrella);
  1023. }
  1024. inline void swapStruct(sub_umbrella_command &s) {
  1025. sys::swapByteOrder(s.cmd);
  1026. sys::swapByteOrder(s.cmdsize);
  1027. sys::swapByteOrder(s.sub_umbrella);
  1028. }
  1029. inline void swapStruct(sub_library_command &s) {
  1030. sys::swapByteOrder(s.cmd);
  1031. sys::swapByteOrder(s.cmdsize);
  1032. sys::swapByteOrder(s.sub_library);
  1033. }
  1034. inline void swapStruct(sub_client_command &s) {
  1035. sys::swapByteOrder(s.cmd);
  1036. sys::swapByteOrder(s.cmdsize);
  1037. sys::swapByteOrder(s.client);
  1038. }
  1039. inline void swapStruct(routines_command &r) {
  1040. sys::swapByteOrder(r.cmd);
  1041. sys::swapByteOrder(r.cmdsize);
  1042. sys::swapByteOrder(r.init_address);
  1043. sys::swapByteOrder(r.init_module);
  1044. sys::swapByteOrder(r.reserved1);
  1045. sys::swapByteOrder(r.reserved2);
  1046. sys::swapByteOrder(r.reserved3);
  1047. sys::swapByteOrder(r.reserved4);
  1048. sys::swapByteOrder(r.reserved5);
  1049. sys::swapByteOrder(r.reserved6);
  1050. }
  1051. inline void swapStruct(routines_command_64 &r) {
  1052. sys::swapByteOrder(r.cmd);
  1053. sys::swapByteOrder(r.cmdsize);
  1054. sys::swapByteOrder(r.init_address);
  1055. sys::swapByteOrder(r.init_module);
  1056. sys::swapByteOrder(r.reserved1);
  1057. sys::swapByteOrder(r.reserved2);
  1058. sys::swapByteOrder(r.reserved3);
  1059. sys::swapByteOrder(r.reserved4);
  1060. sys::swapByteOrder(r.reserved5);
  1061. sys::swapByteOrder(r.reserved6);
  1062. }
  1063. inline void swapStruct(thread_command &t) {
  1064. sys::swapByteOrder(t.cmd);
  1065. sys::swapByteOrder(t.cmdsize);
  1066. }
  1067. inline void swapStruct(dylinker_command &d) {
  1068. sys::swapByteOrder(d.cmd);
  1069. sys::swapByteOrder(d.cmdsize);
  1070. sys::swapByteOrder(d.name);
  1071. }
  1072. inline void swapStruct(uuid_command &u) {
  1073. sys::swapByteOrder(u.cmd);
  1074. sys::swapByteOrder(u.cmdsize);
  1075. }
  1076. inline void swapStruct(rpath_command &r) {
  1077. sys::swapByteOrder(r.cmd);
  1078. sys::swapByteOrder(r.cmdsize);
  1079. sys::swapByteOrder(r.path);
  1080. }
  1081. inline void swapStruct(source_version_command &s) {
  1082. sys::swapByteOrder(s.cmd);
  1083. sys::swapByteOrder(s.cmdsize);
  1084. sys::swapByteOrder(s.version);
  1085. }
  1086. inline void swapStruct(entry_point_command &e) {
  1087. sys::swapByteOrder(e.cmd);
  1088. sys::swapByteOrder(e.cmdsize);
  1089. sys::swapByteOrder(e.entryoff);
  1090. sys::swapByteOrder(e.stacksize);
  1091. }
  1092. inline void swapStruct(encryption_info_command &e) {
  1093. sys::swapByteOrder(e.cmd);
  1094. sys::swapByteOrder(e.cmdsize);
  1095. sys::swapByteOrder(e.cryptoff);
  1096. sys::swapByteOrder(e.cryptsize);
  1097. sys::swapByteOrder(e.cryptid);
  1098. }
  1099. inline void swapStruct(encryption_info_command_64 &e) {
  1100. sys::swapByteOrder(e.cmd);
  1101. sys::swapByteOrder(e.cmdsize);
  1102. sys::swapByteOrder(e.cryptoff);
  1103. sys::swapByteOrder(e.cryptsize);
  1104. sys::swapByteOrder(e.cryptid);
  1105. sys::swapByteOrder(e.pad);
  1106. }
  1107. inline void swapStruct(dysymtab_command &dst) {
  1108. sys::swapByteOrder(dst.cmd);
  1109. sys::swapByteOrder(dst.cmdsize);
  1110. sys::swapByteOrder(dst.ilocalsym);
  1111. sys::swapByteOrder(dst.nlocalsym);
  1112. sys::swapByteOrder(dst.iextdefsym);
  1113. sys::swapByteOrder(dst.nextdefsym);
  1114. sys::swapByteOrder(dst.iundefsym);
  1115. sys::swapByteOrder(dst.nundefsym);
  1116. sys::swapByteOrder(dst.tocoff);
  1117. sys::swapByteOrder(dst.ntoc);
  1118. sys::swapByteOrder(dst.modtaboff);
  1119. sys::swapByteOrder(dst.nmodtab);
  1120. sys::swapByteOrder(dst.extrefsymoff);
  1121. sys::swapByteOrder(dst.nextrefsyms);
  1122. sys::swapByteOrder(dst.indirectsymoff);
  1123. sys::swapByteOrder(dst.nindirectsyms);
  1124. sys::swapByteOrder(dst.extreloff);
  1125. sys::swapByteOrder(dst.nextrel);
  1126. sys::swapByteOrder(dst.locreloff);
  1127. sys::swapByteOrder(dst.nlocrel);
  1128. }
  1129. inline void swapStruct(any_relocation_info &reloc) {
  1130. sys::swapByteOrder(reloc.r_word0);
  1131. sys::swapByteOrder(reloc.r_word1);
  1132. }
  1133. inline void swapStruct(nlist_base &S) {
  1134. sys::swapByteOrder(S.n_strx);
  1135. sys::swapByteOrder(S.n_desc);
  1136. }
  1137. inline void swapStruct(nlist &sym) {
  1138. sys::swapByteOrder(sym.n_strx);
  1139. sys::swapByteOrder(sym.n_desc);
  1140. sys::swapByteOrder(sym.n_value);
  1141. }
  1142. inline void swapStruct(nlist_64 &sym) {
  1143. sys::swapByteOrder(sym.n_strx);
  1144. sys::swapByteOrder(sym.n_desc);
  1145. sys::swapByteOrder(sym.n_value);
  1146. }
  1147. inline void swapStruct(linkedit_data_command &C) {
  1148. sys::swapByteOrder(C.cmd);
  1149. sys::swapByteOrder(C.cmdsize);
  1150. sys::swapByteOrder(C.dataoff);
  1151. sys::swapByteOrder(C.datasize);
  1152. }
  1153. inline void swapStruct(linker_option_command &C) {
  1154. sys::swapByteOrder(C.cmd);
  1155. sys::swapByteOrder(C.cmdsize);
  1156. sys::swapByteOrder(C.count);
  1157. }
  1158. inline void swapStruct(version_min_command &C) {
  1159. sys::swapByteOrder(C.cmd);
  1160. sys::swapByteOrder(C.cmdsize);
  1161. sys::swapByteOrder(C.version);
  1162. sys::swapByteOrder(C.sdk);
  1163. }
  1164. inline void swapStruct(note_command &C) {
  1165. sys::swapByteOrder(C.cmd);
  1166. sys::swapByteOrder(C.cmdsize);
  1167. sys::swapByteOrder(C.offset);
  1168. sys::swapByteOrder(C.size);
  1169. }
  1170. inline void swapStruct(build_version_command &C) {
  1171. sys::swapByteOrder(C.cmd);
  1172. sys::swapByteOrder(C.cmdsize);
  1173. sys::swapByteOrder(C.platform);
  1174. sys::swapByteOrder(C.minos);
  1175. sys::swapByteOrder(C.sdk);
  1176. sys::swapByteOrder(C.ntools);
  1177. }
  1178. inline void swapStruct(build_tool_version &C) {
  1179. sys::swapByteOrder(C.tool);
  1180. sys::swapByteOrder(C.version);
  1181. }
  1182. inline void swapStruct(data_in_code_entry &C) {
  1183. sys::swapByteOrder(C.offset);
  1184. sys::swapByteOrder(C.length);
  1185. sys::swapByteOrder(C.kind);
  1186. }
  1187. inline void swapStruct(uint32_t &C) { sys::swapByteOrder(C); }
  1188. // The prebind_cksum_command is obsolete and no longer supported.
  1189. inline void swapStruct(prebind_cksum_command &C) {
  1190. sys::swapByteOrder(C.cmd);
  1191. sys::swapByteOrder(C.cmdsize);
  1192. sys::swapByteOrder(C.cksum);
  1193. }
  1194. // The twolevel_hints_command is obsolete and no longer supported.
  1195. inline void swapStruct(twolevel_hints_command &C) {
  1196. sys::swapByteOrder(C.cmd);
  1197. sys::swapByteOrder(C.cmdsize);
  1198. sys::swapByteOrder(C.offset);
  1199. sys::swapByteOrder(C.nhints);
  1200. }
  1201. // The prebound_dylib_command is obsolete and no longer supported.
  1202. inline void swapStruct(prebound_dylib_command &C) {
  1203. sys::swapByteOrder(C.cmd);
  1204. sys::swapByteOrder(C.cmdsize);
  1205. sys::swapByteOrder(C.name);
  1206. sys::swapByteOrder(C.nmodules);
  1207. sys::swapByteOrder(C.linked_modules);
  1208. }
  1209. // The fvmfile_command is obsolete and no longer supported.
  1210. inline void swapStruct(fvmfile_command &C) {
  1211. sys::swapByteOrder(C.cmd);
  1212. sys::swapByteOrder(C.cmdsize);
  1213. sys::swapByteOrder(C.name);
  1214. sys::swapByteOrder(C.header_addr);
  1215. }
  1216. // The symseg_command is obsolete and no longer supported.
  1217. inline void swapStruct(symseg_command &C) {
  1218. sys::swapByteOrder(C.cmd);
  1219. sys::swapByteOrder(C.cmdsize);
  1220. sys::swapByteOrder(C.offset);
  1221. sys::swapByteOrder(C.size);
  1222. }
  1223. // The ident_command is obsolete and no longer supported.
  1224. inline void swapStruct(ident_command &C) {
  1225. sys::swapByteOrder(C.cmd);
  1226. sys::swapByteOrder(C.cmdsize);
  1227. }
  1228. inline void swapStruct(fvmlib &C) {
  1229. sys::swapByteOrder(C.name);
  1230. sys::swapByteOrder(C.minor_version);
  1231. sys::swapByteOrder(C.header_addr);
  1232. }
  1233. // The fvmlib_command is obsolete and no longer supported.
  1234. inline void swapStruct(fvmlib_command &C) {
  1235. sys::swapByteOrder(C.cmd);
  1236. sys::swapByteOrder(C.cmdsize);
  1237. swapStruct(C.fvmlib);
  1238. }
  1239. // Get/Set functions from <mach-o/nlist.h>
  1240. inline uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc) {
  1241. return (((n_desc) >> 8u) & 0xffu);
  1242. }
  1243. inline void SET_LIBRARY_ORDINAL(uint16_t &n_desc, uint8_t ordinal) {
  1244. n_desc = (((n_desc)&0x00ff) | (((ordinal)&0xff) << 8));
  1245. }
  1246. inline uint8_t GET_COMM_ALIGN(uint16_t n_desc) {
  1247. return (n_desc >> 8u) & 0x0fu;
  1248. }
  1249. inline void SET_COMM_ALIGN(uint16_t &n_desc, uint8_t align) {
  1250. n_desc = ((n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));
  1251. }
  1252. // Enums from <mach/machine.h>
  1253. enum : uint32_t {
  1254. // Capability bits used in the definition of cpu_type.
  1255. CPU_ARCH_MASK = 0xff000000, // Mask for architecture bits
  1256. CPU_ARCH_ABI64 = 0x01000000, // 64 bit ABI
  1257. CPU_ARCH_ABI64_32 = 0x02000000, // ILP32 ABI on 64-bit hardware
  1258. };
  1259. // Constants for the cputype field.
  1260. enum CPUType {
  1261. CPU_TYPE_ANY = -1,
  1262. CPU_TYPE_X86 = 7,
  1263. CPU_TYPE_I386 = CPU_TYPE_X86,
  1264. CPU_TYPE_X86_64 = CPU_TYPE_X86 | CPU_ARCH_ABI64,
  1265. /* CPU_TYPE_MIPS = 8, */
  1266. CPU_TYPE_MC98000 = 10, // Old Motorola PowerPC
  1267. CPU_TYPE_ARM = 12,
  1268. CPU_TYPE_ARM64 = CPU_TYPE_ARM | CPU_ARCH_ABI64,
  1269. CPU_TYPE_ARM64_32 = CPU_TYPE_ARM | CPU_ARCH_ABI64_32,
  1270. CPU_TYPE_SPARC = 14,
  1271. CPU_TYPE_POWERPC = 18,
  1272. CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
  1273. };
  1274. enum : uint32_t {
  1275. // Capability bits used in the definition of cpusubtype.
  1276. CPU_SUBTYPE_MASK = 0xff000000, // Mask for architecture bits
  1277. CPU_SUBTYPE_LIB64 = 0x80000000, // 64 bit libraries
  1278. // Special CPU subtype constants.
  1279. CPU_SUBTYPE_MULTIPLE = ~0u
  1280. };
  1281. // Constants for the cpusubtype field.
  1282. enum CPUSubTypeX86 {
  1283. CPU_SUBTYPE_I386_ALL = 3,
  1284. CPU_SUBTYPE_386 = 3,
  1285. CPU_SUBTYPE_486 = 4,
  1286. CPU_SUBTYPE_486SX = 0x84,
  1287. CPU_SUBTYPE_586 = 5,
  1288. CPU_SUBTYPE_PENT = CPU_SUBTYPE_586,
  1289. CPU_SUBTYPE_PENTPRO = 0x16,
  1290. CPU_SUBTYPE_PENTII_M3 = 0x36,
  1291. CPU_SUBTYPE_PENTII_M5 = 0x56,
  1292. CPU_SUBTYPE_CELERON = 0x67,
  1293. CPU_SUBTYPE_CELERON_MOBILE = 0x77,
  1294. CPU_SUBTYPE_PENTIUM_3 = 0x08,
  1295. CPU_SUBTYPE_PENTIUM_3_M = 0x18,
  1296. CPU_SUBTYPE_PENTIUM_3_XEON = 0x28,
  1297. CPU_SUBTYPE_PENTIUM_M = 0x09,
  1298. CPU_SUBTYPE_PENTIUM_4 = 0x0a,
  1299. CPU_SUBTYPE_PENTIUM_4_M = 0x1a,
  1300. CPU_SUBTYPE_ITANIUM = 0x0b,
  1301. CPU_SUBTYPE_ITANIUM_2 = 0x1b,
  1302. CPU_SUBTYPE_XEON = 0x0c,
  1303. CPU_SUBTYPE_XEON_MP = 0x1c,
  1304. CPU_SUBTYPE_X86_ALL = 3,
  1305. CPU_SUBTYPE_X86_64_ALL = 3,
  1306. CPU_SUBTYPE_X86_ARCH1 = 4,
  1307. CPU_SUBTYPE_X86_64_H = 8
  1308. };
  1309. inline int CPU_SUBTYPE_INTEL(int Family, int Model) {
  1310. return Family | (Model << 4);
  1311. }
  1312. inline int CPU_SUBTYPE_INTEL_FAMILY(CPUSubTypeX86 ST) {
  1313. return ((int)ST) & 0x0f;
  1314. }
  1315. inline int CPU_SUBTYPE_INTEL_MODEL(CPUSubTypeX86 ST) { return ((int)ST) >> 4; }
  1316. enum { CPU_SUBTYPE_INTEL_FAMILY_MAX = 15, CPU_SUBTYPE_INTEL_MODEL_ALL = 0 };
  1317. enum CPUSubTypeARM {
  1318. CPU_SUBTYPE_ARM_ALL = 0,
  1319. CPU_SUBTYPE_ARM_V4T = 5,
  1320. CPU_SUBTYPE_ARM_V6 = 6,
  1321. CPU_SUBTYPE_ARM_V5 = 7,
  1322. CPU_SUBTYPE_ARM_V5TEJ = 7,
  1323. CPU_SUBTYPE_ARM_XSCALE = 8,
  1324. CPU_SUBTYPE_ARM_V7 = 9,
  1325. // unused ARM_V7F = 10,
  1326. CPU_SUBTYPE_ARM_V7S = 11,
  1327. CPU_SUBTYPE_ARM_V7K = 12,
  1328. CPU_SUBTYPE_ARM_V6M = 14,
  1329. CPU_SUBTYPE_ARM_V7M = 15,
  1330. CPU_SUBTYPE_ARM_V7EM = 16
  1331. };
  1332. enum CPUSubTypeARM64 {
  1333. CPU_SUBTYPE_ARM64_ALL = 0,
  1334. CPU_SUBTYPE_ARM64_V8 = 1,
  1335. CPU_SUBTYPE_ARM64E = 2,
  1336. };
  1337. enum CPUSubTypeARM64_32 { CPU_SUBTYPE_ARM64_32_V8 = 1 };
  1338. enum CPUSubTypeSPARC { CPU_SUBTYPE_SPARC_ALL = 0 };
  1339. enum CPUSubTypePowerPC {
  1340. CPU_SUBTYPE_POWERPC_ALL = 0,
  1341. CPU_SUBTYPE_POWERPC_601 = 1,
  1342. CPU_SUBTYPE_POWERPC_602 = 2,
  1343. CPU_SUBTYPE_POWERPC_603 = 3,
  1344. CPU_SUBTYPE_POWERPC_603e = 4,
  1345. CPU_SUBTYPE_POWERPC_603ev = 5,
  1346. CPU_SUBTYPE_POWERPC_604 = 6,
  1347. CPU_SUBTYPE_POWERPC_604e = 7,
  1348. CPU_SUBTYPE_POWERPC_620 = 8,
  1349. CPU_SUBTYPE_POWERPC_750 = 9,
  1350. CPU_SUBTYPE_POWERPC_7400 = 10,
  1351. CPU_SUBTYPE_POWERPC_7450 = 11,
  1352. CPU_SUBTYPE_POWERPC_970 = 100,
  1353. CPU_SUBTYPE_MC980000_ALL = CPU_SUBTYPE_POWERPC_ALL,
  1354. CPU_SUBTYPE_MC98601 = CPU_SUBTYPE_POWERPC_601
  1355. };
  1356. Expected<uint32_t> getCPUType(const Triple &T);
  1357. Expected<uint32_t> getCPUSubType(const Triple &T);
  1358. struct x86_thread_state32_t {
  1359. uint32_t eax;
  1360. uint32_t ebx;
  1361. uint32_t ecx;
  1362. uint32_t edx;
  1363. uint32_t edi;
  1364. uint32_t esi;
  1365. uint32_t ebp;
  1366. uint32_t esp;
  1367. uint32_t ss;
  1368. uint32_t eflags;
  1369. uint32_t eip;
  1370. uint32_t cs;
  1371. uint32_t ds;
  1372. uint32_t es;
  1373. uint32_t fs;
  1374. uint32_t gs;
  1375. };
  1376. struct x86_thread_state64_t {
  1377. uint64_t rax;
  1378. uint64_t rbx;
  1379. uint64_t rcx;
  1380. uint64_t rdx;
  1381. uint64_t rdi;
  1382. uint64_t rsi;
  1383. uint64_t rbp;
  1384. uint64_t rsp;
  1385. uint64_t r8;
  1386. uint64_t r9;
  1387. uint64_t r10;
  1388. uint64_t r11;
  1389. uint64_t r12;
  1390. uint64_t r13;
  1391. uint64_t r14;
  1392. uint64_t r15;
  1393. uint64_t rip;
  1394. uint64_t rflags;
  1395. uint64_t cs;
  1396. uint64_t fs;
  1397. uint64_t gs;
  1398. };
  1399. enum x86_fp_control_precis {
  1400. x86_FP_PREC_24B = 0,
  1401. x86_FP_PREC_53B = 2,
  1402. x86_FP_PREC_64B = 3
  1403. };
  1404. enum x86_fp_control_rc {
  1405. x86_FP_RND_NEAR = 0,
  1406. x86_FP_RND_DOWN = 1,
  1407. x86_FP_RND_UP = 2,
  1408. x86_FP_CHOP = 3
  1409. };
  1410. struct fp_control_t {
  1411. unsigned short invalid : 1, denorm : 1, zdiv : 1, ovrfl : 1, undfl : 1,
  1412. precis : 1, : 2, pc : 2, rc : 2, : 1, : 3;
  1413. };
  1414. struct fp_status_t {
  1415. unsigned short invalid : 1, denorm : 1, zdiv : 1, ovrfl : 1, undfl : 1,
  1416. precis : 1, stkflt : 1, errsumm : 1, c0 : 1, c1 : 1, c2 : 1, tos : 3,
  1417. c3 : 1, busy : 1;
  1418. };
  1419. struct mmst_reg_t {
  1420. char mmst_reg[10];
  1421. char mmst_rsrv[6];
  1422. };
  1423. struct xmm_reg_t {
  1424. char xmm_reg[16];
  1425. };
  1426. struct x86_float_state64_t {
  1427. int32_t fpu_reserved[2];
  1428. fp_control_t fpu_fcw;
  1429. fp_status_t fpu_fsw;
  1430. uint8_t fpu_ftw;
  1431. uint8_t fpu_rsrv1;
  1432. uint16_t fpu_fop;
  1433. uint32_t fpu_ip;
  1434. uint16_t fpu_cs;
  1435. uint16_t fpu_rsrv2;
  1436. uint32_t fpu_dp;
  1437. uint16_t fpu_ds;
  1438. uint16_t fpu_rsrv3;
  1439. uint32_t fpu_mxcsr;
  1440. uint32_t fpu_mxcsrmask;
  1441. mmst_reg_t fpu_stmm0;
  1442. mmst_reg_t fpu_stmm1;
  1443. mmst_reg_t fpu_stmm2;
  1444. mmst_reg_t fpu_stmm3;
  1445. mmst_reg_t fpu_stmm4;
  1446. mmst_reg_t fpu_stmm5;
  1447. mmst_reg_t fpu_stmm6;
  1448. mmst_reg_t fpu_stmm7;
  1449. xmm_reg_t fpu_xmm0;
  1450. xmm_reg_t fpu_xmm1;
  1451. xmm_reg_t fpu_xmm2;
  1452. xmm_reg_t fpu_xmm3;
  1453. xmm_reg_t fpu_xmm4;
  1454. xmm_reg_t fpu_xmm5;
  1455. xmm_reg_t fpu_xmm6;
  1456. xmm_reg_t fpu_xmm7;
  1457. xmm_reg_t fpu_xmm8;
  1458. xmm_reg_t fpu_xmm9;
  1459. xmm_reg_t fpu_xmm10;
  1460. xmm_reg_t fpu_xmm11;
  1461. xmm_reg_t fpu_xmm12;
  1462. xmm_reg_t fpu_xmm13;
  1463. xmm_reg_t fpu_xmm14;
  1464. xmm_reg_t fpu_xmm15;
  1465. char fpu_rsrv4[6 * 16];
  1466. uint32_t fpu_reserved1;
  1467. };
  1468. struct x86_exception_state64_t {
  1469. uint16_t trapno;
  1470. uint16_t cpu;
  1471. uint32_t err;
  1472. uint64_t faultvaddr;
  1473. };
  1474. inline void swapStruct(x86_thread_state32_t &x) {
  1475. sys::swapByteOrder(x.eax);
  1476. sys::swapByteOrder(x.ebx);
  1477. sys::swapByteOrder(x.ecx);
  1478. sys::swapByteOrder(x.edx);
  1479. sys::swapByteOrder(x.edi);
  1480. sys::swapByteOrder(x.esi);
  1481. sys::swapByteOrder(x.ebp);
  1482. sys::swapByteOrder(x.esp);
  1483. sys::swapByteOrder(x.ss);
  1484. sys::swapByteOrder(x.eflags);
  1485. sys::swapByteOrder(x.eip);
  1486. sys::swapByteOrder(x.cs);
  1487. sys::swapByteOrder(x.ds);
  1488. sys::swapByteOrder(x.es);
  1489. sys::swapByteOrder(x.fs);
  1490. sys::swapByteOrder(x.gs);
  1491. }
  1492. inline void swapStruct(x86_thread_state64_t &x) {
  1493. sys::swapByteOrder(x.rax);
  1494. sys::swapByteOrder(x.rbx);
  1495. sys::swapByteOrder(x.rcx);
  1496. sys::swapByteOrder(x.rdx);
  1497. sys::swapByteOrder(x.rdi);
  1498. sys::swapByteOrder(x.rsi);
  1499. sys::swapByteOrder(x.rbp);
  1500. sys::swapByteOrder(x.rsp);
  1501. sys::swapByteOrder(x.r8);
  1502. sys::swapByteOrder(x.r9);
  1503. sys::swapByteOrder(x.r10);
  1504. sys::swapByteOrder(x.r11);
  1505. sys::swapByteOrder(x.r12);
  1506. sys::swapByteOrder(x.r13);
  1507. sys::swapByteOrder(x.r14);
  1508. sys::swapByteOrder(x.r15);
  1509. sys::swapByteOrder(x.rip);
  1510. sys::swapByteOrder(x.rflags);
  1511. sys::swapByteOrder(x.cs);
  1512. sys::swapByteOrder(x.fs);
  1513. sys::swapByteOrder(x.gs);
  1514. }
  1515. inline void swapStruct(x86_float_state64_t &x) {
  1516. sys::swapByteOrder(x.fpu_reserved[0]);
  1517. sys::swapByteOrder(x.fpu_reserved[1]);
  1518. // TODO swap: fp_control_t fpu_fcw;
  1519. // TODO swap: fp_status_t fpu_fsw;
  1520. sys::swapByteOrder(x.fpu_fop);
  1521. sys::swapByteOrder(x.fpu_ip);
  1522. sys::swapByteOrder(x.fpu_cs);
  1523. sys::swapByteOrder(x.fpu_rsrv2);
  1524. sys::swapByteOrder(x.fpu_dp);
  1525. sys::swapByteOrder(x.fpu_ds);
  1526. sys::swapByteOrder(x.fpu_rsrv3);
  1527. sys::swapByteOrder(x.fpu_mxcsr);
  1528. sys::swapByteOrder(x.fpu_mxcsrmask);
  1529. sys::swapByteOrder(x.fpu_reserved1);
  1530. }
  1531. inline void swapStruct(x86_exception_state64_t &x) {
  1532. sys::swapByteOrder(x.trapno);
  1533. sys::swapByteOrder(x.cpu);
  1534. sys::swapByteOrder(x.err);
  1535. sys::swapByteOrder(x.faultvaddr);
  1536. }
  1537. struct x86_state_hdr_t {
  1538. uint32_t flavor;
  1539. uint32_t count;
  1540. };
  1541. struct x86_thread_state_t {
  1542. x86_state_hdr_t tsh;
  1543. union {
  1544. x86_thread_state64_t ts64;
  1545. x86_thread_state32_t ts32;
  1546. } uts;
  1547. };
  1548. struct x86_float_state_t {
  1549. x86_state_hdr_t fsh;
  1550. union {
  1551. x86_float_state64_t fs64;
  1552. } ufs;
  1553. };
  1554. struct x86_exception_state_t {
  1555. x86_state_hdr_t esh;
  1556. union {
  1557. x86_exception_state64_t es64;
  1558. } ues;
  1559. };
  1560. inline void swapStruct(x86_state_hdr_t &x) {
  1561. sys::swapByteOrder(x.flavor);
  1562. sys::swapByteOrder(x.count);
  1563. }
  1564. enum X86ThreadFlavors {
  1565. x86_THREAD_STATE32 = 1,
  1566. x86_FLOAT_STATE32 = 2,
  1567. x86_EXCEPTION_STATE32 = 3,
  1568. x86_THREAD_STATE64 = 4,
  1569. x86_FLOAT_STATE64 = 5,
  1570. x86_EXCEPTION_STATE64 = 6,
  1571. x86_THREAD_STATE = 7,
  1572. x86_FLOAT_STATE = 8,
  1573. x86_EXCEPTION_STATE = 9,
  1574. x86_DEBUG_STATE32 = 10,
  1575. x86_DEBUG_STATE64 = 11,
  1576. x86_DEBUG_STATE = 12
  1577. };
  1578. inline void swapStruct(x86_thread_state_t &x) {
  1579. swapStruct(x.tsh);
  1580. if (x.tsh.flavor == x86_THREAD_STATE64)
  1581. swapStruct(x.uts.ts64);
  1582. }
  1583. inline void swapStruct(x86_float_state_t &x) {
  1584. swapStruct(x.fsh);
  1585. if (x.fsh.flavor == x86_FLOAT_STATE64)
  1586. swapStruct(x.ufs.fs64);
  1587. }
  1588. inline void swapStruct(x86_exception_state_t &x) {
  1589. swapStruct(x.esh);
  1590. if (x.esh.flavor == x86_EXCEPTION_STATE64)
  1591. swapStruct(x.ues.es64);
  1592. }
  1593. const uint32_t x86_THREAD_STATE32_COUNT =
  1594. sizeof(x86_thread_state32_t) / sizeof(uint32_t);
  1595. const uint32_t x86_THREAD_STATE64_COUNT =
  1596. sizeof(x86_thread_state64_t) / sizeof(uint32_t);
  1597. const uint32_t x86_FLOAT_STATE64_COUNT =
  1598. sizeof(x86_float_state64_t) / sizeof(uint32_t);
  1599. const uint32_t x86_EXCEPTION_STATE64_COUNT =
  1600. sizeof(x86_exception_state64_t) / sizeof(uint32_t);
  1601. const uint32_t x86_THREAD_STATE_COUNT =
  1602. sizeof(x86_thread_state_t) / sizeof(uint32_t);
  1603. const uint32_t x86_FLOAT_STATE_COUNT =
  1604. sizeof(x86_float_state_t) / sizeof(uint32_t);
  1605. const uint32_t x86_EXCEPTION_STATE_COUNT =
  1606. sizeof(x86_exception_state_t) / sizeof(uint32_t);
  1607. struct arm_thread_state32_t {
  1608. uint32_t r[13];
  1609. uint32_t sp;
  1610. uint32_t lr;
  1611. uint32_t pc;
  1612. uint32_t cpsr;
  1613. };
  1614. inline void swapStruct(arm_thread_state32_t &x) {
  1615. for (int i = 0; i < 13; i++)
  1616. sys::swapByteOrder(x.r[i]);
  1617. sys::swapByteOrder(x.sp);
  1618. sys::swapByteOrder(x.lr);
  1619. sys::swapByteOrder(x.pc);
  1620. sys::swapByteOrder(x.cpsr);
  1621. }
  1622. struct arm_thread_state64_t {
  1623. uint64_t x[29];
  1624. uint64_t fp;
  1625. uint64_t lr;
  1626. uint64_t sp;
  1627. uint64_t pc;
  1628. uint32_t cpsr;
  1629. uint32_t pad;
  1630. };
  1631. inline void swapStruct(arm_thread_state64_t &x) {
  1632. for (int i = 0; i < 29; i++)
  1633. sys::swapByteOrder(x.x[i]);
  1634. sys::swapByteOrder(x.fp);
  1635. sys::swapByteOrder(x.lr);
  1636. sys::swapByteOrder(x.sp);
  1637. sys::swapByteOrder(x.pc);
  1638. sys::swapByteOrder(x.cpsr);
  1639. }
  1640. struct arm_state_hdr_t {
  1641. uint32_t flavor;
  1642. uint32_t count;
  1643. };
  1644. struct arm_thread_state_t {
  1645. arm_state_hdr_t tsh;
  1646. union {
  1647. arm_thread_state32_t ts32;
  1648. } uts;
  1649. };
  1650. inline void swapStruct(arm_state_hdr_t &x) {
  1651. sys::swapByteOrder(x.flavor);
  1652. sys::swapByteOrder(x.count);
  1653. }
  1654. enum ARMThreadFlavors {
  1655. ARM_THREAD_STATE = 1,
  1656. ARM_VFP_STATE = 2,
  1657. ARM_EXCEPTION_STATE = 3,
  1658. ARM_DEBUG_STATE = 4,
  1659. ARN_THREAD_STATE_NONE = 5,
  1660. ARM_THREAD_STATE64 = 6,
  1661. ARM_EXCEPTION_STATE64 = 7
  1662. };
  1663. inline void swapStruct(arm_thread_state_t &x) {
  1664. swapStruct(x.tsh);
  1665. if (x.tsh.flavor == ARM_THREAD_STATE)
  1666. swapStruct(x.uts.ts32);
  1667. }
  1668. const uint32_t ARM_THREAD_STATE_COUNT =
  1669. sizeof(arm_thread_state32_t) / sizeof(uint32_t);
  1670. const uint32_t ARM_THREAD_STATE64_COUNT =
  1671. sizeof(arm_thread_state64_t) / sizeof(uint32_t);
  1672. struct ppc_thread_state32_t {
  1673. uint32_t srr0;
  1674. uint32_t srr1;
  1675. uint32_t r0;
  1676. uint32_t r1;
  1677. uint32_t r2;
  1678. uint32_t r3;
  1679. uint32_t r4;
  1680. uint32_t r5;
  1681. uint32_t r6;
  1682. uint32_t r7;
  1683. uint32_t r8;
  1684. uint32_t r9;
  1685. uint32_t r10;
  1686. uint32_t r11;
  1687. uint32_t r12;
  1688. uint32_t r13;
  1689. uint32_t r14;
  1690. uint32_t r15;
  1691. uint32_t r16;
  1692. uint32_t r17;
  1693. uint32_t r18;
  1694. uint32_t r19;
  1695. uint32_t r20;
  1696. uint32_t r21;
  1697. uint32_t r22;
  1698. uint32_t r23;
  1699. uint32_t r24;
  1700. uint32_t r25;
  1701. uint32_t r26;
  1702. uint32_t r27;
  1703. uint32_t r28;
  1704. uint32_t r29;
  1705. uint32_t r30;
  1706. uint32_t r31;
  1707. uint32_t ct;
  1708. uint32_t xer;
  1709. uint32_t lr;
  1710. uint32_t ctr;
  1711. uint32_t mq;
  1712. uint32_t vrsave;
  1713. };
  1714. inline void swapStruct(ppc_thread_state32_t &x) {
  1715. sys::swapByteOrder(x.srr0);
  1716. sys::swapByteOrder(x.srr1);
  1717. sys::swapByteOrder(x.r0);
  1718. sys::swapByteOrder(x.r1);
  1719. sys::swapByteOrder(x.r2);
  1720. sys::swapByteOrder(x.r3);
  1721. sys::swapByteOrder(x.r4);
  1722. sys::swapByteOrder(x.r5);
  1723. sys::swapByteOrder(x.r6);
  1724. sys::swapByteOrder(x.r7);
  1725. sys::swapByteOrder(x.r8);
  1726. sys::swapByteOrder(x.r9);
  1727. sys::swapByteOrder(x.r10);
  1728. sys::swapByteOrder(x.r11);
  1729. sys::swapByteOrder(x.r12);
  1730. sys::swapByteOrder(x.r13);
  1731. sys::swapByteOrder(x.r14);
  1732. sys::swapByteOrder(x.r15);
  1733. sys::swapByteOrder(x.r16);
  1734. sys::swapByteOrder(x.r17);
  1735. sys::swapByteOrder(x.r18);
  1736. sys::swapByteOrder(x.r19);
  1737. sys::swapByteOrder(x.r20);
  1738. sys::swapByteOrder(x.r21);
  1739. sys::swapByteOrder(x.r22);
  1740. sys::swapByteOrder(x.r23);
  1741. sys::swapByteOrder(x.r24);
  1742. sys::swapByteOrder(x.r25);
  1743. sys::swapByteOrder(x.r26);
  1744. sys::swapByteOrder(x.r27);
  1745. sys::swapByteOrder(x.r28);
  1746. sys::swapByteOrder(x.r29);
  1747. sys::swapByteOrder(x.r30);
  1748. sys::swapByteOrder(x.r31);
  1749. sys::swapByteOrder(x.ct);
  1750. sys::swapByteOrder(x.xer);
  1751. sys::swapByteOrder(x.lr);
  1752. sys::swapByteOrder(x.ctr);
  1753. sys::swapByteOrder(x.mq);
  1754. sys::swapByteOrder(x.vrsave);
  1755. }
  1756. struct ppc_state_hdr_t {
  1757. uint32_t flavor;
  1758. uint32_t count;
  1759. };
  1760. struct ppc_thread_state_t {
  1761. ppc_state_hdr_t tsh;
  1762. union {
  1763. ppc_thread_state32_t ts32;
  1764. } uts;
  1765. };
  1766. inline void swapStruct(ppc_state_hdr_t &x) {
  1767. sys::swapByteOrder(x.flavor);
  1768. sys::swapByteOrder(x.count);
  1769. }
  1770. enum PPCThreadFlavors {
  1771. PPC_THREAD_STATE = 1,
  1772. PPC_FLOAT_STATE = 2,
  1773. PPC_EXCEPTION_STATE = 3,
  1774. PPC_VECTOR_STATE = 4,
  1775. PPC_THREAD_STATE64 = 5,
  1776. PPC_EXCEPTION_STATE64 = 6,
  1777. PPC_THREAD_STATE_NONE = 7
  1778. };
  1779. inline void swapStruct(ppc_thread_state_t &x) {
  1780. swapStruct(x.tsh);
  1781. if (x.tsh.flavor == PPC_THREAD_STATE)
  1782. swapStruct(x.uts.ts32);
  1783. }
  1784. const uint32_t PPC_THREAD_STATE_COUNT =
  1785. sizeof(ppc_thread_state32_t) / sizeof(uint32_t);
  1786. // Define a union of all load command structs
  1787. #define LOAD_COMMAND_STRUCT(LCStruct) LCStruct LCStruct##_data;
  1788. LLVM_PACKED_START
  1789. union alignas(4) macho_load_command {
  1790. #include "llvm/BinaryFormat/MachO.def"
  1791. };
  1792. LLVM_PACKED_END
  1793. /* code signing attributes of a process */
  1794. enum CodeSignAttrs {
  1795. CS_VALID = 0x00000001, /* dynamically valid */
  1796. CS_ADHOC = 0x00000002, /* ad hoc signed */
  1797. CS_GET_TASK_ALLOW = 0x00000004, /* has get-task-allow entitlement */
  1798. CS_INSTALLER = 0x00000008, /* has installer entitlement */
  1799. CS_FORCED_LV =
  1800. 0x00000010, /* Library Validation required by Hardened System Policy */
  1801. CS_INVALID_ALLOWED = 0x00000020, /* (macOS Only) Page invalidation allowed by
  1802. task port policy */
  1803. CS_HARD = 0x00000100, /* don't load invalid pages */
  1804. CS_KILL = 0x00000200, /* kill process if it becomes invalid */
  1805. CS_CHECK_EXPIRATION = 0x00000400, /* force expiration checking */
  1806. CS_RESTRICT = 0x00000800, /* tell dyld to treat restricted */
  1807. CS_ENFORCEMENT = 0x00001000, /* require enforcement */
  1808. CS_REQUIRE_LV = 0x00002000, /* require library validation */
  1809. CS_ENTITLEMENTS_VALIDATED =
  1810. 0x00004000, /* code signature permits restricted entitlements */
  1811. CS_NVRAM_UNRESTRICTED =
  1812. 0x00008000, /* has com.apple.rootless.restricted-nvram-variables.heritable
  1813. entitlement */
  1814. CS_RUNTIME = 0x00010000, /* Apply hardened runtime policies */
  1815. CS_LINKER_SIGNED = 0x00020000, /* Automatically signed by the linker */
  1816. CS_ALLOWED_MACHO =
  1817. (CS_ADHOC | CS_HARD | CS_KILL | CS_CHECK_EXPIRATION | CS_RESTRICT |
  1818. CS_ENFORCEMENT | CS_REQUIRE_LV | CS_RUNTIME | CS_LINKER_SIGNED),
  1819. CS_EXEC_SET_HARD = 0x00100000, /* set CS_HARD on any exec'ed process */
  1820. CS_EXEC_SET_KILL = 0x00200000, /* set CS_KILL on any exec'ed process */
  1821. CS_EXEC_SET_ENFORCEMENT =
  1822. 0x00400000, /* set CS_ENFORCEMENT on any exec'ed process */
  1823. CS_EXEC_INHERIT_SIP =
  1824. 0x00800000, /* set CS_INSTALLER on any exec'ed process */
  1825. CS_KILLED = 0x01000000, /* was killed by kernel for invalidity */
  1826. CS_DYLD_PLATFORM =
  1827. 0x02000000, /* dyld used to load this is a platform binary */
  1828. CS_PLATFORM_BINARY = 0x04000000, /* this is a platform binary */
  1829. CS_PLATFORM_PATH =
  1830. 0x08000000, /* platform binary by the fact of path (osx only) */
  1831. CS_DEBUGGED = 0x10000000, /* process is currently or has previously been
  1832. debugged and allowed to run with invalid pages */
  1833. CS_SIGNED = 0x20000000, /* process has a signature (may have gone invalid) */
  1834. CS_DEV_CODE =
  1835. 0x40000000, /* code is dev signed, cannot be loaded into prod signed code
  1836. (will go away with rdar://problem/28322552) */
  1837. CS_DATAVAULT_CONTROLLER =
  1838. 0x80000000, /* has Data Vault controller entitlement */
  1839. CS_ENTITLEMENT_FLAGS = (CS_GET_TASK_ALLOW | CS_INSTALLER |
  1840. CS_DATAVAULT_CONTROLLER | CS_NVRAM_UNRESTRICTED),
  1841. };
  1842. /* executable segment flags */
  1843. enum CodeSignExecSegFlags {
  1844. CS_EXECSEG_MAIN_BINARY = 0x1, /* executable segment denotes main binary */
  1845. CS_EXECSEG_ALLOW_UNSIGNED = 0x10, /* allow unsigned pages (for debugging) */
  1846. CS_EXECSEG_DEBUGGER = 0x20, /* main binary is debugger */
  1847. CS_EXECSEG_JIT = 0x40, /* JIT enabled */
  1848. CS_EXECSEG_SKIP_LV = 0x80, /* OBSOLETE: skip library validation */
  1849. CS_EXECSEG_CAN_LOAD_CDHASH = 0x100, /* can bless cdhash for execution */
  1850. CS_EXECSEG_CAN_EXEC_CDHASH = 0x200, /* can execute blessed cdhash */
  1851. };
  1852. /* Magic numbers used by Code Signing */
  1853. enum CodeSignMagic {
  1854. CSMAGIC_REQUIREMENT = 0xfade0c00, /* single Requirement blob */
  1855. CSMAGIC_REQUIREMENTS =
  1856. 0xfade0c01, /* Requirements vector (internal requirements) */
  1857. CSMAGIC_CODEDIRECTORY = 0xfade0c02, /* CodeDirectory blob */
  1858. CSMAGIC_EMBEDDED_SIGNATURE = 0xfade0cc0, /* embedded form of signature data */
  1859. CSMAGIC_EMBEDDED_SIGNATURE_OLD = 0xfade0b02, /* XXX */
  1860. CSMAGIC_EMBEDDED_ENTITLEMENTS = 0xfade7171, /* embedded entitlements */
  1861. CSMAGIC_DETACHED_SIGNATURE =
  1862. 0xfade0cc1, /* multi-arch collection of embedded signatures */
  1863. CSMAGIC_BLOBWRAPPER = 0xfade0b01, /* CMS Signature, among other things */
  1864. CS_SUPPORTSSCATTER = 0x20100,
  1865. CS_SUPPORTSTEAMID = 0x20200,
  1866. CS_SUPPORTSCODELIMIT64 = 0x20300,
  1867. CS_SUPPORTSEXECSEG = 0x20400,
  1868. CS_SUPPORTSRUNTIME = 0x20500,
  1869. CS_SUPPORTSLINKAGE = 0x20600,
  1870. CSSLOT_CODEDIRECTORY = 0, /* slot index for CodeDirectory */
  1871. CSSLOT_INFOSLOT = 1,
  1872. CSSLOT_REQUIREMENTS = 2,
  1873. CSSLOT_RESOURCEDIR = 3,
  1874. CSSLOT_APPLICATION = 4,
  1875. CSSLOT_ENTITLEMENTS = 5,
  1876. CSSLOT_ALTERNATE_CODEDIRECTORIES =
  1877. 0x1000, /* first alternate CodeDirectory, if any */
  1878. CSSLOT_ALTERNATE_CODEDIRECTORY_MAX = 5, /* max number of alternate CD slots */
  1879. CSSLOT_ALTERNATE_CODEDIRECTORY_LIMIT =
  1880. CSSLOT_ALTERNATE_CODEDIRECTORIES +
  1881. CSSLOT_ALTERNATE_CODEDIRECTORY_MAX, /* one past the last */
  1882. CSSLOT_SIGNATURESLOT = 0x10000, /* CMS Signature */
  1883. CSSLOT_IDENTIFICATIONSLOT = 0x10001,
  1884. CSSLOT_TICKETSLOT = 0x10002,
  1885. CSTYPE_INDEX_REQUIREMENTS = 0x00000002, /* compat with amfi */
  1886. CSTYPE_INDEX_ENTITLEMENTS = 0x00000005, /* compat with amfi */
  1887. CS_HASHTYPE_SHA1 = 1,
  1888. CS_HASHTYPE_SHA256 = 2,
  1889. CS_HASHTYPE_SHA256_TRUNCATED = 3,
  1890. CS_HASHTYPE_SHA384 = 4,
  1891. CS_SHA1_LEN = 20,
  1892. CS_SHA256_LEN = 32,
  1893. CS_SHA256_TRUNCATED_LEN = 20,
  1894. CS_CDHASH_LEN = 20, /* always - larger hashes are truncated */
  1895. CS_HASH_MAX_SIZE = 48, /* max size of the hash we'll support */
  1896. /*
  1897. * Currently only to support Legacy VPN plugins, and Mac App Store
  1898. * but intended to replace all the various platform code, dev code etc. bits.
  1899. */
  1900. CS_SIGNER_TYPE_UNKNOWN = 0,
  1901. CS_SIGNER_TYPE_LEGACYVPN = 5,
  1902. CS_SIGNER_TYPE_MAC_APP_STORE = 6,
  1903. CS_SUPPL_SIGNER_TYPE_UNKNOWN = 0,
  1904. CS_SUPPL_SIGNER_TYPE_TRUSTCACHE = 7,
  1905. CS_SUPPL_SIGNER_TYPE_LOCAL = 8,
  1906. };
  1907. struct CS_CodeDirectory {
  1908. uint32_t magic; /* magic number (CSMAGIC_CODEDIRECTORY) */
  1909. uint32_t length; /* total length of CodeDirectory blob */
  1910. uint32_t version; /* compatibility version */
  1911. uint32_t flags; /* setup and mode flags */
  1912. uint32_t hashOffset; /* offset of hash slot element at index zero */
  1913. uint32_t identOffset; /* offset of identifier string */
  1914. uint32_t nSpecialSlots; /* number of special hash slots */
  1915. uint32_t nCodeSlots; /* number of ordinary (code) hash slots */
  1916. uint32_t codeLimit; /* limit to main image signature range */
  1917. uint8_t hashSize; /* size of each hash in bytes */
  1918. uint8_t hashType; /* type of hash (cdHashType* constants) */
  1919. uint8_t platform; /* platform identifier; zero if not platform binary */
  1920. uint8_t pageSize; /* log2(page size in bytes); 0 => infinite */
  1921. uint32_t spare2; /* unused (must be zero) */
  1922. /* Version 0x20100 */
  1923. uint32_t scatterOffset; /* offset of optional scatter vector */
  1924. /* Version 0x20200 */
  1925. uint32_t teamOffset; /* offset of optional team identifier */
  1926. /* Version 0x20300 */
  1927. uint32_t spare3; /* unused (must be zero) */
  1928. uint64_t codeLimit64; /* limit to main image signature range, 64 bits */
  1929. /* Version 0x20400 */
  1930. uint64_t execSegBase; /* offset of executable segment */
  1931. uint64_t execSegLimit; /* limit of executable segment */
  1932. uint64_t execSegFlags; /* executable segment flags */
  1933. };
  1934. static_assert(sizeof(CS_CodeDirectory) == 88, "");
  1935. struct CS_BlobIndex {
  1936. uint32_t type; /* type of entry */
  1937. uint32_t offset; /* offset of entry */
  1938. };
  1939. struct CS_SuperBlob {
  1940. uint32_t magic; /* magic number */
  1941. uint32_t length; /* total length of SuperBlob */
  1942. uint32_t count; /* number of index entries following */
  1943. /* followed by Blobs in no particular order as indicated by index offsets */
  1944. };
  1945. enum SecCSDigestAlgorithm {
  1946. kSecCodeSignatureNoHash = 0, /* null value */
  1947. kSecCodeSignatureHashSHA1 = 1, /* SHA-1 */
  1948. kSecCodeSignatureHashSHA256 = 2, /* SHA-256 */
  1949. kSecCodeSignatureHashSHA256Truncated =
  1950. 3, /* SHA-256 truncated to first 20 bytes */
  1951. kSecCodeSignatureHashSHA384 = 4, /* SHA-384 */
  1952. kSecCodeSignatureHashSHA512 = 5, /* SHA-512 */
  1953. };
  1954. } // end namespace MachO
  1955. } // end namespace llvm
  1956. #endif
  1957. #ifdef __GNUC__
  1958. #pragma GCC diagnostic pop
  1959. #endif