MachO.h 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400
  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. MH_FILESET = 0xCu,
  54. };
  55. enum {
  56. // Constant bits for the "flags" field in llvm::MachO::mach_header and
  57. // llvm::MachO::mach_header_64
  58. MH_NOUNDEFS = 0x00000001u,
  59. MH_INCRLINK = 0x00000002u,
  60. MH_DYLDLINK = 0x00000004u,
  61. MH_BINDATLOAD = 0x00000008u,
  62. MH_PREBOUND = 0x00000010u,
  63. MH_SPLIT_SEGS = 0x00000020u,
  64. MH_LAZY_INIT = 0x00000040u,
  65. MH_TWOLEVEL = 0x00000080u,
  66. MH_FORCE_FLAT = 0x00000100u,
  67. MH_NOMULTIDEFS = 0x00000200u,
  68. MH_NOFIXPREBINDING = 0x00000400u,
  69. MH_PREBINDABLE = 0x00000800u,
  70. MH_ALLMODSBOUND = 0x00001000u,
  71. MH_SUBSECTIONS_VIA_SYMBOLS = 0x00002000u,
  72. MH_CANONICAL = 0x00004000u,
  73. MH_WEAK_DEFINES = 0x00008000u,
  74. MH_BINDS_TO_WEAK = 0x00010000u,
  75. MH_ALLOW_STACK_EXECUTION = 0x00020000u,
  76. MH_ROOT_SAFE = 0x00040000u,
  77. MH_SETUID_SAFE = 0x00080000u,
  78. MH_NO_REEXPORTED_DYLIBS = 0x00100000u,
  79. MH_PIE = 0x00200000u,
  80. MH_DEAD_STRIPPABLE_DYLIB = 0x00400000u,
  81. MH_HAS_TLV_DESCRIPTORS = 0x00800000u,
  82. MH_NO_HEAP_EXECUTION = 0x01000000u,
  83. MH_APP_EXTENSION_SAFE = 0x02000000u,
  84. MH_NLIST_OUTOFSYNC_WITH_DYLDINFO = 0x04000000u,
  85. MH_SIM_SUPPORT = 0x08000000u,
  86. MH_DYLIB_IN_CACHE = 0x80000000u,
  87. };
  88. enum : uint32_t {
  89. // Flags for the "cmd" field in llvm::MachO::load_command
  90. LC_REQ_DYLD = 0x80000000u
  91. };
  92. #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) LCName = LCValue,
  93. enum LoadCommandType : uint32_t {
  94. #include "llvm/BinaryFormat/MachO.def"
  95. };
  96. #undef HANDLE_LOAD_COMMAND
  97. enum : uint32_t {
  98. // Constant bits for the "flags" field in llvm::MachO::segment_command
  99. SG_HIGHVM = 0x1u,
  100. SG_FVMLIB = 0x2u,
  101. SG_NORELOC = 0x4u,
  102. SG_PROTECTED_VERSION_1 = 0x8u,
  103. SG_READ_ONLY = 0x10u,
  104. // Constant masks for the "flags" field in llvm::MachO::section and
  105. // llvm::MachO::section_64
  106. SECTION_TYPE = 0x000000ffu, // SECTION_TYPE
  107. SECTION_ATTRIBUTES = 0xffffff00u, // SECTION_ATTRIBUTES
  108. SECTION_ATTRIBUTES_USR = 0xff000000u, // SECTION_ATTRIBUTES_USR
  109. SECTION_ATTRIBUTES_SYS = 0x00ffff00u // SECTION_ATTRIBUTES_SYS
  110. };
  111. /// These are the section type and attributes fields. A MachO section can
  112. /// have only one Type, but can have any of the attributes specified.
  113. enum SectionType : uint32_t {
  114. // Constant masks for the "flags[7:0]" field in llvm::MachO::section and
  115. // llvm::MachO::section_64 (mask "flags" with SECTION_TYPE)
  116. /// S_REGULAR - Regular section.
  117. S_REGULAR = 0x00u,
  118. /// S_ZEROFILL - Zero fill on demand section.
  119. S_ZEROFILL = 0x01u,
  120. /// S_CSTRING_LITERALS - Section with literal C strings.
  121. S_CSTRING_LITERALS = 0x02u,
  122. /// S_4BYTE_LITERALS - Section with 4 byte literals.
  123. S_4BYTE_LITERALS = 0x03u,
  124. /// S_8BYTE_LITERALS - Section with 8 byte literals.
  125. S_8BYTE_LITERALS = 0x04u,
  126. /// S_LITERAL_POINTERS - Section with pointers to literals.
  127. S_LITERAL_POINTERS = 0x05u,
  128. /// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
  129. S_NON_LAZY_SYMBOL_POINTERS = 0x06u,
  130. /// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
  131. S_LAZY_SYMBOL_POINTERS = 0x07u,
  132. /// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
  133. /// the Reserved2 field.
  134. S_SYMBOL_STUBS = 0x08u,
  135. /// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
  136. /// initialization.
  137. S_MOD_INIT_FUNC_POINTERS = 0x09u,
  138. /// S_MOD_TERM_FUNC_POINTERS - Section with only function pointers for
  139. /// termination.
  140. S_MOD_TERM_FUNC_POINTERS = 0x0au,
  141. /// S_COALESCED - Section contains symbols that are to be coalesced.
  142. S_COALESCED = 0x0bu,
  143. /// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
  144. /// gigabytes).
  145. S_GB_ZEROFILL = 0x0cu,
  146. /// S_INTERPOSING - Section with only pairs of function pointers for
  147. /// interposing.
  148. S_INTERPOSING = 0x0du,
  149. /// S_16BYTE_LITERALS - Section with only 16 byte literals.
  150. S_16BYTE_LITERALS = 0x0eu,
  151. /// S_DTRACE_DOF - Section contains DTrace Object Format.
  152. S_DTRACE_DOF = 0x0fu,
  153. /// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
  154. /// lazy loaded dylibs.
  155. S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10u,
  156. /// S_THREAD_LOCAL_REGULAR - Thread local data section.
  157. S_THREAD_LOCAL_REGULAR = 0x11u,
  158. /// S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
  159. S_THREAD_LOCAL_ZEROFILL = 0x12u,
  160. /// S_THREAD_LOCAL_VARIABLES - Section with thread local variable
  161. /// structure data.
  162. S_THREAD_LOCAL_VARIABLES = 0x13u,
  163. /// S_THREAD_LOCAL_VARIABLE_POINTERS - Section with pointers to thread
  164. /// local structures.
  165. S_THREAD_LOCAL_VARIABLE_POINTERS = 0x14u,
  166. /// S_THREAD_LOCAL_INIT_FUNCTION_POINTERS - Section with thread local
  167. /// variable initialization pointers to functions.
  168. S_THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
  169. /// S_INIT_FUNC_OFFSETS - Section with 32-bit offsets to initializer
  170. /// functions.
  171. S_INIT_FUNC_OFFSETS = 0x16u,
  172. LAST_KNOWN_SECTION_TYPE = S_INIT_FUNC_OFFSETS
  173. };
  174. enum : uint32_t {
  175. // Constant masks for the "flags[31:24]" field in llvm::MachO::section and
  176. // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
  177. /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
  178. /// instructions.
  179. S_ATTR_PURE_INSTRUCTIONS = 0x80000000u,
  180. /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
  181. /// in a ranlib table of contents.
  182. S_ATTR_NO_TOC = 0x40000000u,
  183. /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
  184. /// in files with the MY_DYLDLINK flag.
  185. S_ATTR_STRIP_STATIC_SYMS = 0x20000000u,
  186. /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
  187. S_ATTR_NO_DEAD_STRIP = 0x10000000u,
  188. /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
  189. S_ATTR_LIVE_SUPPORT = 0x08000000u,
  190. /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
  191. /// dyld.
  192. S_ATTR_SELF_MODIFYING_CODE = 0x04000000u,
  193. /// S_ATTR_DEBUG - A debug section.
  194. S_ATTR_DEBUG = 0x02000000u,
  195. // Constant masks for the "flags[23:8]" field in llvm::MachO::section and
  196. // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
  197. /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
  198. S_ATTR_SOME_INSTRUCTIONS = 0x00000400u,
  199. /// S_ATTR_EXT_RELOC - Section has external relocation entries.
  200. S_ATTR_EXT_RELOC = 0x00000200u,
  201. /// S_ATTR_LOC_RELOC - Section has local relocation entries.
  202. S_ATTR_LOC_RELOC = 0x00000100u,
  203. // Constant masks for the value of an indirect symbol in an indirect
  204. // symbol table
  205. INDIRECT_SYMBOL_LOCAL = 0x80000000u,
  206. INDIRECT_SYMBOL_ABS = 0x40000000u
  207. };
  208. enum DataRegionType {
  209. // Constants for the "kind" field in a data_in_code_entry structure
  210. DICE_KIND_DATA = 1u,
  211. DICE_KIND_JUMP_TABLE8 = 2u,
  212. DICE_KIND_JUMP_TABLE16 = 3u,
  213. DICE_KIND_JUMP_TABLE32 = 4u,
  214. DICE_KIND_ABS_JUMP_TABLE32 = 5u
  215. };
  216. enum RebaseType {
  217. REBASE_TYPE_POINTER = 1u,
  218. REBASE_TYPE_TEXT_ABSOLUTE32 = 2u,
  219. REBASE_TYPE_TEXT_PCREL32 = 3u
  220. };
  221. enum { REBASE_OPCODE_MASK = 0xF0u, REBASE_IMMEDIATE_MASK = 0x0Fu };
  222. enum RebaseOpcode {
  223. REBASE_OPCODE_DONE = 0x00u,
  224. REBASE_OPCODE_SET_TYPE_IMM = 0x10u,
  225. REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x20u,
  226. REBASE_OPCODE_ADD_ADDR_ULEB = 0x30u,
  227. REBASE_OPCODE_ADD_ADDR_IMM_SCALED = 0x40u,
  228. REBASE_OPCODE_DO_REBASE_IMM_TIMES = 0x50u,
  229. REBASE_OPCODE_DO_REBASE_ULEB_TIMES = 0x60u,
  230. REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB = 0x70u,
  231. REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u
  232. };
  233. enum BindType {
  234. BIND_TYPE_POINTER = 1u,
  235. BIND_TYPE_TEXT_ABSOLUTE32 = 2u,
  236. BIND_TYPE_TEXT_PCREL32 = 3u
  237. };
  238. enum BindSpecialDylib {
  239. BIND_SPECIAL_DYLIB_SELF = 0,
  240. BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE = -1,
  241. BIND_SPECIAL_DYLIB_FLAT_LOOKUP = -2,
  242. BIND_SPECIAL_DYLIB_WEAK_LOOKUP = -3
  243. };
  244. enum {
  245. BIND_SYMBOL_FLAGS_WEAK_IMPORT = 0x1u,
  246. BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION = 0x8u,
  247. BIND_OPCODE_MASK = 0xF0u,
  248. BIND_IMMEDIATE_MASK = 0x0Fu
  249. };
  250. enum BindOpcode {
  251. BIND_OPCODE_DONE = 0x00u,
  252. BIND_OPCODE_SET_DYLIB_ORDINAL_IMM = 0x10u,
  253. BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB = 0x20u,
  254. BIND_OPCODE_SET_DYLIB_SPECIAL_IMM = 0x30u,
  255. BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM = 0x40u,
  256. BIND_OPCODE_SET_TYPE_IMM = 0x50u,
  257. BIND_OPCODE_SET_ADDEND_SLEB = 0x60u,
  258. BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB = 0x70u,
  259. BIND_OPCODE_ADD_ADDR_ULEB = 0x80u,
  260. BIND_OPCODE_DO_BIND = 0x90u,
  261. BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB = 0xA0u,
  262. BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED = 0xB0u,
  263. BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u
  264. };
  265. enum {
  266. EXPORT_SYMBOL_FLAGS_KIND_MASK = 0x03u,
  267. EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION = 0x04u,
  268. EXPORT_SYMBOL_FLAGS_REEXPORT = 0x08u,
  269. EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER = 0x10u
  270. };
  271. enum ExportSymbolKind {
  272. EXPORT_SYMBOL_FLAGS_KIND_REGULAR = 0x00u,
  273. EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL = 0x01u,
  274. EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE = 0x02u
  275. };
  276. enum {
  277. // Constant masks for the "n_type" field in llvm::MachO::nlist and
  278. // llvm::MachO::nlist_64
  279. N_STAB = 0xe0,
  280. N_PEXT = 0x10,
  281. N_TYPE = 0x0e,
  282. N_EXT = 0x01
  283. };
  284. enum NListType : uint8_t {
  285. // Constants for the "n_type & N_TYPE" llvm::MachO::nlist and
  286. // llvm::MachO::nlist_64
  287. N_UNDF = 0x0u,
  288. N_ABS = 0x2u,
  289. N_SECT = 0xeu,
  290. N_PBUD = 0xcu,
  291. N_INDR = 0xau
  292. };
  293. enum SectionOrdinal {
  294. // Constants for the "n_sect" field in llvm::MachO::nlist and
  295. // llvm::MachO::nlist_64
  296. NO_SECT = 0u,
  297. MAX_SECT = 0xffu
  298. };
  299. enum {
  300. // Constant masks for the "n_desc" field in llvm::MachO::nlist and
  301. // llvm::MachO::nlist_64
  302. // The low 3 bits are the for the REFERENCE_TYPE.
  303. REFERENCE_TYPE = 0x7,
  304. REFERENCE_FLAG_UNDEFINED_NON_LAZY = 0,
  305. REFERENCE_FLAG_UNDEFINED_LAZY = 1,
  306. REFERENCE_FLAG_DEFINED = 2,
  307. REFERENCE_FLAG_PRIVATE_DEFINED = 3,
  308. REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4,
  309. REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY = 5,
  310. // Flag bits (some overlap with the library ordinal bits).
  311. N_ARM_THUMB_DEF = 0x0008u,
  312. REFERENCED_DYNAMICALLY = 0x0010u,
  313. N_NO_DEAD_STRIP = 0x0020u,
  314. N_WEAK_REF = 0x0040u,
  315. N_WEAK_DEF = 0x0080u,
  316. N_SYMBOL_RESOLVER = 0x0100u,
  317. N_ALT_ENTRY = 0x0200u,
  318. N_COLD_FUNC = 0x0400u,
  319. // For undefined symbols coming from libraries, see GET_LIBRARY_ORDINAL()
  320. // as these are in the top 8 bits.
  321. SELF_LIBRARY_ORDINAL = 0x0,
  322. MAX_LIBRARY_ORDINAL = 0xfd,
  323. DYNAMIC_LOOKUP_ORDINAL = 0xfe,
  324. EXECUTABLE_ORDINAL = 0xff
  325. };
  326. enum StabType {
  327. // Constant values for the "n_type" field in llvm::MachO::nlist and
  328. // llvm::MachO::nlist_64 when "(n_type & N_STAB) != 0"
  329. N_GSYM = 0x20u,
  330. N_FNAME = 0x22u,
  331. N_FUN = 0x24u,
  332. N_STSYM = 0x26u,
  333. N_LCSYM = 0x28u,
  334. N_BNSYM = 0x2Eu,
  335. N_PC = 0x30u,
  336. N_AST = 0x32u,
  337. N_OPT = 0x3Cu,
  338. N_RSYM = 0x40u,
  339. N_SLINE = 0x44u,
  340. N_ENSYM = 0x4Eu,
  341. N_SSYM = 0x60u,
  342. N_SO = 0x64u,
  343. N_OSO = 0x66u,
  344. N_LSYM = 0x80u,
  345. N_BINCL = 0x82u,
  346. N_SOL = 0x84u,
  347. N_PARAMS = 0x86u,
  348. N_VERSION = 0x88u,
  349. N_OLEVEL = 0x8Au,
  350. N_PSYM = 0xA0u,
  351. N_EINCL = 0xA2u,
  352. N_ENTRY = 0xA4u,
  353. N_LBRAC = 0xC0u,
  354. N_EXCL = 0xC2u,
  355. N_RBRAC = 0xE0u,
  356. N_BCOMM = 0xE2u,
  357. N_ECOMM = 0xE4u,
  358. N_ECOML = 0xE8u,
  359. N_LENG = 0xFEu
  360. };
  361. enum : uint32_t {
  362. // Constant values for the r_symbolnum field in an
  363. // llvm::MachO::relocation_info structure when r_extern is 0.
  364. R_ABS = 0,
  365. // Constant bits for the r_address field in an
  366. // llvm::MachO::relocation_info structure.
  367. R_SCATTERED = 0x80000000
  368. };
  369. enum RelocationInfoType {
  370. // Constant values for the r_type field in an
  371. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  372. // structure.
  373. GENERIC_RELOC_INVALID = 0xff,
  374. GENERIC_RELOC_VANILLA = 0,
  375. GENERIC_RELOC_PAIR = 1,
  376. GENERIC_RELOC_SECTDIFF = 2,
  377. GENERIC_RELOC_PB_LA_PTR = 3,
  378. GENERIC_RELOC_LOCAL_SECTDIFF = 4,
  379. GENERIC_RELOC_TLV = 5,
  380. // Constant values for the r_type field in a PowerPC architecture
  381. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  382. // structure.
  383. PPC_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
  384. PPC_RELOC_PAIR = GENERIC_RELOC_PAIR,
  385. PPC_RELOC_BR14 = 2,
  386. PPC_RELOC_BR24 = 3,
  387. PPC_RELOC_HI16 = 4,
  388. PPC_RELOC_LO16 = 5,
  389. PPC_RELOC_HA16 = 6,
  390. PPC_RELOC_LO14 = 7,
  391. PPC_RELOC_SECTDIFF = 8,
  392. PPC_RELOC_PB_LA_PTR = 9,
  393. PPC_RELOC_HI16_SECTDIFF = 10,
  394. PPC_RELOC_LO16_SECTDIFF = 11,
  395. PPC_RELOC_HA16_SECTDIFF = 12,
  396. PPC_RELOC_JBSR = 13,
  397. PPC_RELOC_LO14_SECTDIFF = 14,
  398. PPC_RELOC_LOCAL_SECTDIFF = 15,
  399. // Constant values for the r_type field in an ARM architecture
  400. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  401. // structure.
  402. ARM_RELOC_VANILLA = GENERIC_RELOC_VANILLA,
  403. ARM_RELOC_PAIR = GENERIC_RELOC_PAIR,
  404. ARM_RELOC_SECTDIFF = GENERIC_RELOC_SECTDIFF,
  405. ARM_RELOC_LOCAL_SECTDIFF = 3,
  406. ARM_RELOC_PB_LA_PTR = 4,
  407. ARM_RELOC_BR24 = 5,
  408. ARM_THUMB_RELOC_BR22 = 6,
  409. ARM_THUMB_32BIT_BRANCH = 7, // obsolete
  410. ARM_RELOC_HALF = 8,
  411. ARM_RELOC_HALF_SECTDIFF = 9,
  412. // Constant values for the r_type field in an ARM64 architecture
  413. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  414. // structure.
  415. // For pointers.
  416. ARM64_RELOC_UNSIGNED = 0,
  417. // Must be followed by an ARM64_RELOC_UNSIGNED
  418. ARM64_RELOC_SUBTRACTOR = 1,
  419. // A B/BL instruction with 26-bit displacement.
  420. ARM64_RELOC_BRANCH26 = 2,
  421. // PC-rel distance to page of target.
  422. ARM64_RELOC_PAGE21 = 3,
  423. // Offset within page, scaled by r_length.
  424. ARM64_RELOC_PAGEOFF12 = 4,
  425. // PC-rel distance to page of GOT slot.
  426. ARM64_RELOC_GOT_LOAD_PAGE21 = 5,
  427. // Offset within page of GOT slot, scaled by r_length.
  428. ARM64_RELOC_GOT_LOAD_PAGEOFF12 = 6,
  429. // For pointers to GOT slots.
  430. ARM64_RELOC_POINTER_TO_GOT = 7,
  431. // PC-rel distance to page of TLVP slot.
  432. ARM64_RELOC_TLVP_LOAD_PAGE21 = 8,
  433. // Offset within page of TLVP slot, scaled by r_length.
  434. ARM64_RELOC_TLVP_LOAD_PAGEOFF12 = 9,
  435. // Must be followed by ARM64_RELOC_PAGE21 or ARM64_RELOC_PAGEOFF12.
  436. ARM64_RELOC_ADDEND = 10,
  437. // Constant values for the r_type field in an x86_64 architecture
  438. // llvm::MachO::relocation_info or llvm::MachO::scattered_relocation_info
  439. // structure
  440. X86_64_RELOC_UNSIGNED = 0,
  441. X86_64_RELOC_SIGNED = 1,
  442. X86_64_RELOC_BRANCH = 2,
  443. X86_64_RELOC_GOT_LOAD = 3,
  444. X86_64_RELOC_GOT = 4,
  445. X86_64_RELOC_SUBTRACTOR = 5,
  446. X86_64_RELOC_SIGNED_1 = 6,
  447. X86_64_RELOC_SIGNED_2 = 7,
  448. X86_64_RELOC_SIGNED_4 = 8,
  449. X86_64_RELOC_TLV = 9
  450. };
  451. // Values for segment_command.initprot.
  452. // From <mach/vm_prot.h>
  453. enum { VM_PROT_READ = 0x1, VM_PROT_WRITE = 0x2, VM_PROT_EXECUTE = 0x4 };
  454. // Values for platform field in build_version_command.
  455. enum PlatformType {
  456. PLATFORM_UNKNOWN = 0,
  457. PLATFORM_MACOS = 1,
  458. PLATFORM_IOS = 2,
  459. PLATFORM_TVOS = 3,
  460. PLATFORM_WATCHOS = 4,
  461. PLATFORM_BRIDGEOS = 5,
  462. PLATFORM_MACCATALYST = 6,
  463. PLATFORM_IOSSIMULATOR = 7,
  464. PLATFORM_TVOSSIMULATOR = 8,
  465. PLATFORM_WATCHOSSIMULATOR = 9,
  466. PLATFORM_DRIVERKIT = 10,
  467. };
  468. // Values for tools enum in build_tool_version.
  469. enum { TOOL_CLANG = 1, TOOL_SWIFT = 2, TOOL_LD = 3 };
  470. // Structs from <mach-o/loader.h>
  471. struct mach_header {
  472. uint32_t magic;
  473. uint32_t cputype;
  474. uint32_t cpusubtype;
  475. uint32_t filetype;
  476. uint32_t ncmds;
  477. uint32_t sizeofcmds;
  478. uint32_t flags;
  479. };
  480. struct mach_header_64 {
  481. uint32_t magic;
  482. uint32_t cputype;
  483. uint32_t cpusubtype;
  484. uint32_t filetype;
  485. uint32_t ncmds;
  486. uint32_t sizeofcmds;
  487. uint32_t flags;
  488. uint32_t reserved;
  489. };
  490. struct load_command {
  491. uint32_t cmd;
  492. uint32_t cmdsize;
  493. };
  494. struct segment_command {
  495. uint32_t cmd;
  496. uint32_t cmdsize;
  497. char segname[16];
  498. uint32_t vmaddr;
  499. uint32_t vmsize;
  500. uint32_t fileoff;
  501. uint32_t filesize;
  502. uint32_t maxprot;
  503. uint32_t initprot;
  504. uint32_t nsects;
  505. uint32_t flags;
  506. };
  507. struct segment_command_64 {
  508. uint32_t cmd;
  509. uint32_t cmdsize;
  510. char segname[16];
  511. uint64_t vmaddr;
  512. uint64_t vmsize;
  513. uint64_t fileoff;
  514. uint64_t filesize;
  515. uint32_t maxprot;
  516. uint32_t initprot;
  517. uint32_t nsects;
  518. uint32_t flags;
  519. };
  520. struct section {
  521. char sectname[16];
  522. char segname[16];
  523. uint32_t addr;
  524. uint32_t size;
  525. uint32_t offset;
  526. uint32_t align;
  527. uint32_t reloff;
  528. uint32_t nreloc;
  529. uint32_t flags;
  530. uint32_t reserved1;
  531. uint32_t reserved2;
  532. };
  533. struct section_64 {
  534. char sectname[16];
  535. char segname[16];
  536. uint64_t addr;
  537. uint64_t size;
  538. uint32_t offset;
  539. uint32_t align;
  540. uint32_t reloff;
  541. uint32_t nreloc;
  542. uint32_t flags;
  543. uint32_t reserved1;
  544. uint32_t reserved2;
  545. uint32_t reserved3;
  546. };
  547. inline bool isVirtualSection(uint8_t type) {
  548. return (type == MachO::S_ZEROFILL || type == MachO::S_GB_ZEROFILL ||
  549. type == MachO::S_THREAD_LOCAL_ZEROFILL);
  550. }
  551. struct fvmlib {
  552. uint32_t name;
  553. uint32_t minor_version;
  554. uint32_t header_addr;
  555. };
  556. // The fvmlib_command is obsolete and no longer supported.
  557. struct fvmlib_command {
  558. uint32_t cmd;
  559. uint32_t cmdsize;
  560. struct fvmlib fvmlib;
  561. };
  562. struct dylib {
  563. uint32_t name;
  564. uint32_t timestamp;
  565. uint32_t current_version;
  566. uint32_t compatibility_version;
  567. };
  568. struct dylib_command {
  569. uint32_t cmd;
  570. uint32_t cmdsize;
  571. struct dylib dylib;
  572. };
  573. struct sub_framework_command {
  574. uint32_t cmd;
  575. uint32_t cmdsize;
  576. uint32_t umbrella;
  577. };
  578. struct sub_client_command {
  579. uint32_t cmd;
  580. uint32_t cmdsize;
  581. uint32_t client;
  582. };
  583. struct sub_umbrella_command {
  584. uint32_t cmd;
  585. uint32_t cmdsize;
  586. uint32_t sub_umbrella;
  587. };
  588. struct sub_library_command {
  589. uint32_t cmd;
  590. uint32_t cmdsize;
  591. uint32_t sub_library;
  592. };
  593. // The prebound_dylib_command is obsolete and no longer supported.
  594. struct prebound_dylib_command {
  595. uint32_t cmd;
  596. uint32_t cmdsize;
  597. uint32_t name;
  598. uint32_t nmodules;
  599. uint32_t linked_modules;
  600. };
  601. struct dylinker_command {
  602. uint32_t cmd;
  603. uint32_t cmdsize;
  604. uint32_t name;
  605. };
  606. struct thread_command {
  607. uint32_t cmd;
  608. uint32_t cmdsize;
  609. };
  610. struct routines_command {
  611. uint32_t cmd;
  612. uint32_t cmdsize;
  613. uint32_t init_address;
  614. uint32_t init_module;
  615. uint32_t reserved1;
  616. uint32_t reserved2;
  617. uint32_t reserved3;
  618. uint32_t reserved4;
  619. uint32_t reserved5;
  620. uint32_t reserved6;
  621. };
  622. struct routines_command_64 {
  623. uint32_t cmd;
  624. uint32_t cmdsize;
  625. uint64_t init_address;
  626. uint64_t init_module;
  627. uint64_t reserved1;
  628. uint64_t reserved2;
  629. uint64_t reserved3;
  630. uint64_t reserved4;
  631. uint64_t reserved5;
  632. uint64_t reserved6;
  633. };
  634. struct symtab_command {
  635. uint32_t cmd;
  636. uint32_t cmdsize;
  637. uint32_t symoff;
  638. uint32_t nsyms;
  639. uint32_t stroff;
  640. uint32_t strsize;
  641. };
  642. struct dysymtab_command {
  643. uint32_t cmd;
  644. uint32_t cmdsize;
  645. uint32_t ilocalsym;
  646. uint32_t nlocalsym;
  647. uint32_t iextdefsym;
  648. uint32_t nextdefsym;
  649. uint32_t iundefsym;
  650. uint32_t nundefsym;
  651. uint32_t tocoff;
  652. uint32_t ntoc;
  653. uint32_t modtaboff;
  654. uint32_t nmodtab;
  655. uint32_t extrefsymoff;
  656. uint32_t nextrefsyms;
  657. uint32_t indirectsymoff;
  658. uint32_t nindirectsyms;
  659. uint32_t extreloff;
  660. uint32_t nextrel;
  661. uint32_t locreloff;
  662. uint32_t nlocrel;
  663. };
  664. struct dylib_table_of_contents {
  665. uint32_t symbol_index;
  666. uint32_t module_index;
  667. };
  668. struct dylib_module {
  669. uint32_t module_name;
  670. uint32_t iextdefsym;
  671. uint32_t nextdefsym;
  672. uint32_t irefsym;
  673. uint32_t nrefsym;
  674. uint32_t ilocalsym;
  675. uint32_t nlocalsym;
  676. uint32_t iextrel;
  677. uint32_t nextrel;
  678. uint32_t iinit_iterm;
  679. uint32_t ninit_nterm;
  680. uint32_t objc_module_info_addr;
  681. uint32_t objc_module_info_size;
  682. };
  683. struct dylib_module_64 {
  684. uint32_t module_name;
  685. uint32_t iextdefsym;
  686. uint32_t nextdefsym;
  687. uint32_t irefsym;
  688. uint32_t nrefsym;
  689. uint32_t ilocalsym;
  690. uint32_t nlocalsym;
  691. uint32_t iextrel;
  692. uint32_t nextrel;
  693. uint32_t iinit_iterm;
  694. uint32_t ninit_nterm;
  695. uint32_t objc_module_info_size;
  696. uint64_t objc_module_info_addr;
  697. };
  698. struct dylib_reference {
  699. uint32_t isym : 24, flags : 8;
  700. };
  701. // The twolevel_hints_command is obsolete and no longer supported.
  702. struct twolevel_hints_command {
  703. uint32_t cmd;
  704. uint32_t cmdsize;
  705. uint32_t offset;
  706. uint32_t nhints;
  707. };
  708. // The twolevel_hints_command is obsolete and no longer supported.
  709. struct twolevel_hint {
  710. uint32_t isub_image : 8, itoc : 24;
  711. };
  712. // The prebind_cksum_command is obsolete and no longer supported.
  713. struct prebind_cksum_command {
  714. uint32_t cmd;
  715. uint32_t cmdsize;
  716. uint32_t cksum;
  717. };
  718. struct uuid_command {
  719. uint32_t cmd;
  720. uint32_t cmdsize;
  721. uint8_t uuid[16];
  722. };
  723. struct rpath_command {
  724. uint32_t cmd;
  725. uint32_t cmdsize;
  726. uint32_t path;
  727. };
  728. struct linkedit_data_command {
  729. uint32_t cmd;
  730. uint32_t cmdsize;
  731. uint32_t dataoff;
  732. uint32_t datasize;
  733. };
  734. struct data_in_code_entry {
  735. uint32_t offset;
  736. uint16_t length;
  737. uint16_t kind;
  738. };
  739. struct source_version_command {
  740. uint32_t cmd;
  741. uint32_t cmdsize;
  742. uint64_t version;
  743. };
  744. struct encryption_info_command {
  745. uint32_t cmd;
  746. uint32_t cmdsize;
  747. uint32_t cryptoff;
  748. uint32_t cryptsize;
  749. uint32_t cryptid;
  750. };
  751. struct encryption_info_command_64 {
  752. uint32_t cmd;
  753. uint32_t cmdsize;
  754. uint32_t cryptoff;
  755. uint32_t cryptsize;
  756. uint32_t cryptid;
  757. uint32_t pad;
  758. };
  759. struct version_min_command {
  760. uint32_t cmd; // LC_VERSION_MIN_MACOSX or
  761. // LC_VERSION_MIN_IPHONEOS
  762. uint32_t cmdsize; // sizeof(struct version_min_command)
  763. uint32_t version; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  764. uint32_t sdk; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  765. };
  766. struct note_command {
  767. uint32_t cmd; // LC_NOTE
  768. uint32_t cmdsize; // sizeof(struct note_command)
  769. char data_owner[16]; // owner name for this LC_NOTE
  770. uint64_t offset; // file offset of this data
  771. uint64_t size; // length of data region
  772. };
  773. struct build_tool_version {
  774. uint32_t tool; // enum for the tool
  775. uint32_t version; // version of the tool
  776. };
  777. struct build_version_command {
  778. uint32_t cmd; // LC_BUILD_VERSION
  779. uint32_t cmdsize; // sizeof(struct build_version_command) +
  780. // ntools * sizeof(struct build_tool_version)
  781. uint32_t platform; // platform
  782. uint32_t minos; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  783. uint32_t sdk; // X.Y.Z is encoded in nibbles xxxx.yy.zz
  784. uint32_t ntools; // number of tool entries following this
  785. };
  786. struct dyld_env_command {
  787. uint32_t cmd;
  788. uint32_t cmdsize;
  789. uint32_t name;
  790. };
  791. struct dyld_info_command {
  792. uint32_t cmd;
  793. uint32_t cmdsize;
  794. uint32_t rebase_off;
  795. uint32_t rebase_size;
  796. uint32_t bind_off;
  797. uint32_t bind_size;
  798. uint32_t weak_bind_off;
  799. uint32_t weak_bind_size;
  800. uint32_t lazy_bind_off;
  801. uint32_t lazy_bind_size;
  802. uint32_t export_off;
  803. uint32_t export_size;
  804. };
  805. struct linker_option_command {
  806. uint32_t cmd;
  807. uint32_t cmdsize;
  808. uint32_t count;
  809. };
  810. struct fileset_entry_command {
  811. uint32_t cmd;
  812. uint32_t cmdsize;
  813. uint64_t vmaddr;
  814. uint64_t fileoff;
  815. uint32_t entry_id;
  816. };
  817. // The symseg_command is obsolete and no longer supported.
  818. struct symseg_command {
  819. uint32_t cmd;
  820. uint32_t cmdsize;
  821. uint32_t offset;
  822. uint32_t size;
  823. };
  824. // The ident_command is obsolete and no longer supported.
  825. struct ident_command {
  826. uint32_t cmd;
  827. uint32_t cmdsize;
  828. };
  829. // The fvmfile_command is obsolete and no longer supported.
  830. struct fvmfile_command {
  831. uint32_t cmd;
  832. uint32_t cmdsize;
  833. uint32_t name;
  834. uint32_t header_addr;
  835. };
  836. struct tlv_descriptor_32 {
  837. uint32_t thunk;
  838. uint32_t key;
  839. uint32_t offset;
  840. };
  841. struct tlv_descriptor_64 {
  842. uint64_t thunk;
  843. uint64_t key;
  844. uint64_t offset;
  845. };
  846. struct tlv_descriptor {
  847. uintptr_t thunk;
  848. uintptr_t key;
  849. uintptr_t offset;
  850. };
  851. struct entry_point_command {
  852. uint32_t cmd;
  853. uint32_t cmdsize;
  854. uint64_t entryoff;
  855. uint64_t stacksize;
  856. };
  857. // Structs from <mach-o/fat.h>
  858. struct fat_header {
  859. uint32_t magic;
  860. uint32_t nfat_arch;
  861. };
  862. struct fat_arch {
  863. uint32_t cputype;
  864. uint32_t cpusubtype;
  865. uint32_t offset;
  866. uint32_t size;
  867. uint32_t align;
  868. };
  869. struct fat_arch_64 {
  870. uint32_t cputype;
  871. uint32_t cpusubtype;
  872. uint64_t offset;
  873. uint64_t size;
  874. uint32_t align;
  875. uint32_t reserved;
  876. };
  877. // Structs from <mach-o/reloc.h>
  878. struct relocation_info {
  879. int32_t r_address;
  880. uint32_t r_symbolnum : 24, r_pcrel : 1, r_length : 2, r_extern : 1,
  881. r_type : 4;
  882. };
  883. struct scattered_relocation_info {
  884. #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
  885. uint32_t r_scattered : 1, r_pcrel : 1, r_length : 2, r_type : 4,
  886. r_address : 24;
  887. #else
  888. uint32_t r_address : 24, r_type : 4, r_length : 2, r_pcrel : 1,
  889. r_scattered : 1;
  890. #endif
  891. int32_t r_value;
  892. };
  893. // Structs NOT from <mach-o/reloc.h>, but that make LLVM's life easier
  894. struct any_relocation_info {
  895. uint32_t r_word0, r_word1;
  896. };
  897. // Structs from <mach-o/nlist.h>
  898. struct nlist_base {
  899. uint32_t n_strx;
  900. uint8_t n_type;
  901. uint8_t n_sect;
  902. uint16_t n_desc;
  903. };
  904. struct nlist {
  905. uint32_t n_strx;
  906. uint8_t n_type;
  907. uint8_t n_sect;
  908. int16_t n_desc;
  909. uint32_t n_value;
  910. };
  911. struct nlist_64 {
  912. uint32_t n_strx;
  913. uint8_t n_type;
  914. uint8_t n_sect;
  915. uint16_t n_desc;
  916. uint64_t n_value;
  917. };
  918. // Values for dyld_chained_fixups_header::imports_format.
  919. enum ChainedImportFormat {
  920. DYLD_CHAINED_IMPORT = 1,
  921. DYLD_CHAINED_IMPORT_ADDEND = 2,
  922. DYLD_CHAINED_IMPORT_ADDEND64 = 3,
  923. };
  924. // Values for dyld_chained_fixups_header::symbols_format.
  925. enum {
  926. DYLD_CHAINED_SYMBOL_UNCOMPRESSED = 0,
  927. DYLD_CHAINED_SYMBOL_ZLIB = 1,
  928. };
  929. // Values for dyld_chained_starts_in_segment::page_start.
  930. enum {
  931. DYLD_CHAINED_PTR_START_NONE = 0xFFFF,
  932. DYLD_CHAINED_PTR_START_MULTI = 0x8000,
  933. DYLD_CHAINED_PTR_START_LAST = 0x8000,
  934. };
  935. // Values for dyld_chained_starts_in_segment::pointer_format.
  936. enum {
  937. DYLD_CHAINED_PTR_ARM64E = 1,
  938. DYLD_CHAINED_PTR_64 = 2,
  939. DYLD_CHAINED_PTR_32 = 3,
  940. DYLD_CHAINED_PTR_32_CACHE = 4,
  941. DYLD_CHAINED_PTR_32_FIRMWARE = 5,
  942. DYLD_CHAINED_PTR_64_OFFSET = 6,
  943. DYLD_CHAINED_PTR_ARM64E_KERNEL = 7,
  944. DYLD_CHAINED_PTR_64_KERNEL_CACHE = 8,
  945. DYLD_CHAINED_PTR_ARM64E_USERLAND = 9,
  946. DYLD_CHAINED_PTR_ARM64E_FIRMWARE = 10,
  947. DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE = 11,
  948. DYLD_CHAINED_PTR_ARM64E_USERLAND24 = 12,
  949. };
  950. /// Structs for dyld chained fixups.
  951. /// dyld_chained_fixups_header is the data pointed to by LC_DYLD_CHAINED_FIXUPS
  952. /// load command.
  953. struct dyld_chained_fixups_header {
  954. uint32_t fixups_version; ///< 0
  955. uint32_t starts_offset; ///< Offset of dyld_chained_starts_in_image.
  956. uint32_t imports_offset; ///< Offset of imports table in chain_data.
  957. uint32_t symbols_offset; ///< Offset of symbol strings in chain_data.
  958. uint32_t imports_count; ///< Number of imported symbol names.
  959. uint32_t imports_format; ///< DYLD_CHAINED_IMPORT*
  960. uint32_t symbols_format; ///< 0 => uncompressed, 1 => zlib compressed
  961. };
  962. /// dyld_chained_starts_in_image is embedded in LC_DYLD_CHAINED_FIXUPS payload.
  963. /// Each each seg_info_offset entry is the offset into this struct for that
  964. /// segment followed by pool of dyld_chain_starts_in_segment data.
  965. struct dyld_chained_starts_in_image {
  966. uint32_t seg_count;
  967. uint32_t seg_info_offset[1];
  968. };
  969. struct dyld_chained_starts_in_segment {
  970. uint32_t size; ///< Size of this, including chain_starts entries
  971. uint16_t page_size; ///< Page size in bytes (0x1000 or 0x4000)
  972. uint16_t pointer_format; ///< DYLD_CHAINED_PTR*
  973. uint64_t segment_offset; ///< VM offset from the __TEXT segment
  974. uint32_t max_valid_pointer; ///< Values beyond this are not pointers on 32-bit
  975. uint16_t page_count; ///< Length of the page_start array
  976. uint16_t page_start[1]; ///< Page offset of first fixup on each page, or
  977. ///< DYLD_CHAINED_PTR_START_NONE if no fixups
  978. };
  979. // DYLD_CHAINED_IMPORT
  980. struct dyld_chained_import {
  981. uint32_t lib_ordinal : 8;
  982. uint32_t weak_import : 1;
  983. uint32_t name_offset : 23;
  984. };
  985. // DYLD_CHAINED_IMPORT_ADDEND
  986. struct dyld_chained_import_addend {
  987. uint32_t lib_ordinal : 8;
  988. uint32_t weak_import : 1;
  989. uint32_t name_offset : 23;
  990. int32_t addend;
  991. };
  992. // DYLD_CHAINED_IMPORT_ADDEND64
  993. struct dyld_chained_import_addend64 {
  994. uint64_t lib_ordinal : 16;
  995. uint64_t weak_import : 1;
  996. uint64_t reserved : 15;
  997. uint64_t name_offset : 32;
  998. uint64_t addend;
  999. };
  1000. // The `bind` field (most significant bit) of the encoded fixup determines
  1001. // whether it is dyld_chained_ptr_64_bind or dyld_chained_ptr_64_rebase.
  1002. // DYLD_CHAINED_PTR_64/DYLD_CHAINED_PTR_64_OFFSET
  1003. struct dyld_chained_ptr_64_bind {
  1004. uint64_t ordinal : 24;
  1005. uint64_t addend : 8;
  1006. uint64_t reserved : 19;
  1007. uint64_t next : 12;
  1008. uint64_t bind : 1; // set to 1
  1009. };
  1010. // DYLD_CHAINED_PTR_64/DYLD_CHAINED_PTR_64_OFFSET
  1011. struct dyld_chained_ptr_64_rebase {
  1012. uint64_t target : 36;
  1013. uint64_t high8 : 8;
  1014. uint64_t reserved : 7;
  1015. uint64_t next : 12;
  1016. uint64_t bind : 1; // set to 0
  1017. };
  1018. // Byte order swapping functions for MachO structs
  1019. inline void swapStruct(fat_header &mh) {
  1020. sys::swapByteOrder(mh.magic);
  1021. sys::swapByteOrder(mh.nfat_arch);
  1022. }
  1023. inline void swapStruct(fat_arch &mh) {
  1024. sys::swapByteOrder(mh.cputype);
  1025. sys::swapByteOrder(mh.cpusubtype);
  1026. sys::swapByteOrder(mh.offset);
  1027. sys::swapByteOrder(mh.size);
  1028. sys::swapByteOrder(mh.align);
  1029. }
  1030. inline void swapStruct(fat_arch_64 &mh) {
  1031. sys::swapByteOrder(mh.cputype);
  1032. sys::swapByteOrder(mh.cpusubtype);
  1033. sys::swapByteOrder(mh.offset);
  1034. sys::swapByteOrder(mh.size);
  1035. sys::swapByteOrder(mh.align);
  1036. sys::swapByteOrder(mh.reserved);
  1037. }
  1038. inline void swapStruct(mach_header &mh) {
  1039. sys::swapByteOrder(mh.magic);
  1040. sys::swapByteOrder(mh.cputype);
  1041. sys::swapByteOrder(mh.cpusubtype);
  1042. sys::swapByteOrder(mh.filetype);
  1043. sys::swapByteOrder(mh.ncmds);
  1044. sys::swapByteOrder(mh.sizeofcmds);
  1045. sys::swapByteOrder(mh.flags);
  1046. }
  1047. inline void swapStruct(mach_header_64 &H) {
  1048. sys::swapByteOrder(H.magic);
  1049. sys::swapByteOrder(H.cputype);
  1050. sys::swapByteOrder(H.cpusubtype);
  1051. sys::swapByteOrder(H.filetype);
  1052. sys::swapByteOrder(H.ncmds);
  1053. sys::swapByteOrder(H.sizeofcmds);
  1054. sys::swapByteOrder(H.flags);
  1055. sys::swapByteOrder(H.reserved);
  1056. }
  1057. inline void swapStruct(load_command &lc) {
  1058. sys::swapByteOrder(lc.cmd);
  1059. sys::swapByteOrder(lc.cmdsize);
  1060. }
  1061. inline void swapStruct(symtab_command &lc) {
  1062. sys::swapByteOrder(lc.cmd);
  1063. sys::swapByteOrder(lc.cmdsize);
  1064. sys::swapByteOrder(lc.symoff);
  1065. sys::swapByteOrder(lc.nsyms);
  1066. sys::swapByteOrder(lc.stroff);
  1067. sys::swapByteOrder(lc.strsize);
  1068. }
  1069. inline void swapStruct(segment_command_64 &seg) {
  1070. sys::swapByteOrder(seg.cmd);
  1071. sys::swapByteOrder(seg.cmdsize);
  1072. sys::swapByteOrder(seg.vmaddr);
  1073. sys::swapByteOrder(seg.vmsize);
  1074. sys::swapByteOrder(seg.fileoff);
  1075. sys::swapByteOrder(seg.filesize);
  1076. sys::swapByteOrder(seg.maxprot);
  1077. sys::swapByteOrder(seg.initprot);
  1078. sys::swapByteOrder(seg.nsects);
  1079. sys::swapByteOrder(seg.flags);
  1080. }
  1081. inline void swapStruct(segment_command &seg) {
  1082. sys::swapByteOrder(seg.cmd);
  1083. sys::swapByteOrder(seg.cmdsize);
  1084. sys::swapByteOrder(seg.vmaddr);
  1085. sys::swapByteOrder(seg.vmsize);
  1086. sys::swapByteOrder(seg.fileoff);
  1087. sys::swapByteOrder(seg.filesize);
  1088. sys::swapByteOrder(seg.maxprot);
  1089. sys::swapByteOrder(seg.initprot);
  1090. sys::swapByteOrder(seg.nsects);
  1091. sys::swapByteOrder(seg.flags);
  1092. }
  1093. inline void swapStruct(section_64 &sect) {
  1094. sys::swapByteOrder(sect.addr);
  1095. sys::swapByteOrder(sect.size);
  1096. sys::swapByteOrder(sect.offset);
  1097. sys::swapByteOrder(sect.align);
  1098. sys::swapByteOrder(sect.reloff);
  1099. sys::swapByteOrder(sect.nreloc);
  1100. sys::swapByteOrder(sect.flags);
  1101. sys::swapByteOrder(sect.reserved1);
  1102. sys::swapByteOrder(sect.reserved2);
  1103. }
  1104. inline void swapStruct(section &sect) {
  1105. sys::swapByteOrder(sect.addr);
  1106. sys::swapByteOrder(sect.size);
  1107. sys::swapByteOrder(sect.offset);
  1108. sys::swapByteOrder(sect.align);
  1109. sys::swapByteOrder(sect.reloff);
  1110. sys::swapByteOrder(sect.nreloc);
  1111. sys::swapByteOrder(sect.flags);
  1112. sys::swapByteOrder(sect.reserved1);
  1113. sys::swapByteOrder(sect.reserved2);
  1114. }
  1115. inline void swapStruct(dyld_info_command &info) {
  1116. sys::swapByteOrder(info.cmd);
  1117. sys::swapByteOrder(info.cmdsize);
  1118. sys::swapByteOrder(info.rebase_off);
  1119. sys::swapByteOrder(info.rebase_size);
  1120. sys::swapByteOrder(info.bind_off);
  1121. sys::swapByteOrder(info.bind_size);
  1122. sys::swapByteOrder(info.weak_bind_off);
  1123. sys::swapByteOrder(info.weak_bind_size);
  1124. sys::swapByteOrder(info.lazy_bind_off);
  1125. sys::swapByteOrder(info.lazy_bind_size);
  1126. sys::swapByteOrder(info.export_off);
  1127. sys::swapByteOrder(info.export_size);
  1128. }
  1129. inline void swapStruct(dylib_command &d) {
  1130. sys::swapByteOrder(d.cmd);
  1131. sys::swapByteOrder(d.cmdsize);
  1132. sys::swapByteOrder(d.dylib.name);
  1133. sys::swapByteOrder(d.dylib.timestamp);
  1134. sys::swapByteOrder(d.dylib.current_version);
  1135. sys::swapByteOrder(d.dylib.compatibility_version);
  1136. }
  1137. inline void swapStruct(sub_framework_command &s) {
  1138. sys::swapByteOrder(s.cmd);
  1139. sys::swapByteOrder(s.cmdsize);
  1140. sys::swapByteOrder(s.umbrella);
  1141. }
  1142. inline void swapStruct(sub_umbrella_command &s) {
  1143. sys::swapByteOrder(s.cmd);
  1144. sys::swapByteOrder(s.cmdsize);
  1145. sys::swapByteOrder(s.sub_umbrella);
  1146. }
  1147. inline void swapStruct(sub_library_command &s) {
  1148. sys::swapByteOrder(s.cmd);
  1149. sys::swapByteOrder(s.cmdsize);
  1150. sys::swapByteOrder(s.sub_library);
  1151. }
  1152. inline void swapStruct(sub_client_command &s) {
  1153. sys::swapByteOrder(s.cmd);
  1154. sys::swapByteOrder(s.cmdsize);
  1155. sys::swapByteOrder(s.client);
  1156. }
  1157. inline void swapStruct(routines_command &r) {
  1158. sys::swapByteOrder(r.cmd);
  1159. sys::swapByteOrder(r.cmdsize);
  1160. sys::swapByteOrder(r.init_address);
  1161. sys::swapByteOrder(r.init_module);
  1162. sys::swapByteOrder(r.reserved1);
  1163. sys::swapByteOrder(r.reserved2);
  1164. sys::swapByteOrder(r.reserved3);
  1165. sys::swapByteOrder(r.reserved4);
  1166. sys::swapByteOrder(r.reserved5);
  1167. sys::swapByteOrder(r.reserved6);
  1168. }
  1169. inline void swapStruct(routines_command_64 &r) {
  1170. sys::swapByteOrder(r.cmd);
  1171. sys::swapByteOrder(r.cmdsize);
  1172. sys::swapByteOrder(r.init_address);
  1173. sys::swapByteOrder(r.init_module);
  1174. sys::swapByteOrder(r.reserved1);
  1175. sys::swapByteOrder(r.reserved2);
  1176. sys::swapByteOrder(r.reserved3);
  1177. sys::swapByteOrder(r.reserved4);
  1178. sys::swapByteOrder(r.reserved5);
  1179. sys::swapByteOrder(r.reserved6);
  1180. }
  1181. inline void swapStruct(thread_command &t) {
  1182. sys::swapByteOrder(t.cmd);
  1183. sys::swapByteOrder(t.cmdsize);
  1184. }
  1185. inline void swapStruct(dylinker_command &d) {
  1186. sys::swapByteOrder(d.cmd);
  1187. sys::swapByteOrder(d.cmdsize);
  1188. sys::swapByteOrder(d.name);
  1189. }
  1190. inline void swapStruct(uuid_command &u) {
  1191. sys::swapByteOrder(u.cmd);
  1192. sys::swapByteOrder(u.cmdsize);
  1193. }
  1194. inline void swapStruct(rpath_command &r) {
  1195. sys::swapByteOrder(r.cmd);
  1196. sys::swapByteOrder(r.cmdsize);
  1197. sys::swapByteOrder(r.path);
  1198. }
  1199. inline void swapStruct(source_version_command &s) {
  1200. sys::swapByteOrder(s.cmd);
  1201. sys::swapByteOrder(s.cmdsize);
  1202. sys::swapByteOrder(s.version);
  1203. }
  1204. inline void swapStruct(entry_point_command &e) {
  1205. sys::swapByteOrder(e.cmd);
  1206. sys::swapByteOrder(e.cmdsize);
  1207. sys::swapByteOrder(e.entryoff);
  1208. sys::swapByteOrder(e.stacksize);
  1209. }
  1210. inline void swapStruct(encryption_info_command &e) {
  1211. sys::swapByteOrder(e.cmd);
  1212. sys::swapByteOrder(e.cmdsize);
  1213. sys::swapByteOrder(e.cryptoff);
  1214. sys::swapByteOrder(e.cryptsize);
  1215. sys::swapByteOrder(e.cryptid);
  1216. }
  1217. inline void swapStruct(encryption_info_command_64 &e) {
  1218. sys::swapByteOrder(e.cmd);
  1219. sys::swapByteOrder(e.cmdsize);
  1220. sys::swapByteOrder(e.cryptoff);
  1221. sys::swapByteOrder(e.cryptsize);
  1222. sys::swapByteOrder(e.cryptid);
  1223. sys::swapByteOrder(e.pad);
  1224. }
  1225. inline void swapStruct(dysymtab_command &dst) {
  1226. sys::swapByteOrder(dst.cmd);
  1227. sys::swapByteOrder(dst.cmdsize);
  1228. sys::swapByteOrder(dst.ilocalsym);
  1229. sys::swapByteOrder(dst.nlocalsym);
  1230. sys::swapByteOrder(dst.iextdefsym);
  1231. sys::swapByteOrder(dst.nextdefsym);
  1232. sys::swapByteOrder(dst.iundefsym);
  1233. sys::swapByteOrder(dst.nundefsym);
  1234. sys::swapByteOrder(dst.tocoff);
  1235. sys::swapByteOrder(dst.ntoc);
  1236. sys::swapByteOrder(dst.modtaboff);
  1237. sys::swapByteOrder(dst.nmodtab);
  1238. sys::swapByteOrder(dst.extrefsymoff);
  1239. sys::swapByteOrder(dst.nextrefsyms);
  1240. sys::swapByteOrder(dst.indirectsymoff);
  1241. sys::swapByteOrder(dst.nindirectsyms);
  1242. sys::swapByteOrder(dst.extreloff);
  1243. sys::swapByteOrder(dst.nextrel);
  1244. sys::swapByteOrder(dst.locreloff);
  1245. sys::swapByteOrder(dst.nlocrel);
  1246. }
  1247. inline void swapStruct(any_relocation_info &reloc) {
  1248. sys::swapByteOrder(reloc.r_word0);
  1249. sys::swapByteOrder(reloc.r_word1);
  1250. }
  1251. inline void swapStruct(nlist_base &S) {
  1252. sys::swapByteOrder(S.n_strx);
  1253. sys::swapByteOrder(S.n_desc);
  1254. }
  1255. inline void swapStruct(nlist &sym) {
  1256. sys::swapByteOrder(sym.n_strx);
  1257. sys::swapByteOrder(sym.n_desc);
  1258. sys::swapByteOrder(sym.n_value);
  1259. }
  1260. inline void swapStruct(nlist_64 &sym) {
  1261. sys::swapByteOrder(sym.n_strx);
  1262. sys::swapByteOrder(sym.n_desc);
  1263. sys::swapByteOrder(sym.n_value);
  1264. }
  1265. inline void swapStruct(linkedit_data_command &C) {
  1266. sys::swapByteOrder(C.cmd);
  1267. sys::swapByteOrder(C.cmdsize);
  1268. sys::swapByteOrder(C.dataoff);
  1269. sys::swapByteOrder(C.datasize);
  1270. }
  1271. inline void swapStruct(linker_option_command &C) {
  1272. sys::swapByteOrder(C.cmd);
  1273. sys::swapByteOrder(C.cmdsize);
  1274. sys::swapByteOrder(C.count);
  1275. }
  1276. inline void swapStruct(fileset_entry_command &C) {
  1277. sys::swapByteOrder(C.cmd);
  1278. sys::swapByteOrder(C.cmdsize);
  1279. sys::swapByteOrder(C.vmaddr);
  1280. sys::swapByteOrder(C.fileoff);
  1281. sys::swapByteOrder(C.entry_id);
  1282. }
  1283. inline void swapStruct(version_min_command &C) {
  1284. sys::swapByteOrder(C.cmd);
  1285. sys::swapByteOrder(C.cmdsize);
  1286. sys::swapByteOrder(C.version);
  1287. sys::swapByteOrder(C.sdk);
  1288. }
  1289. inline void swapStruct(note_command &C) {
  1290. sys::swapByteOrder(C.cmd);
  1291. sys::swapByteOrder(C.cmdsize);
  1292. sys::swapByteOrder(C.offset);
  1293. sys::swapByteOrder(C.size);
  1294. }
  1295. inline void swapStruct(build_version_command &C) {
  1296. sys::swapByteOrder(C.cmd);
  1297. sys::swapByteOrder(C.cmdsize);
  1298. sys::swapByteOrder(C.platform);
  1299. sys::swapByteOrder(C.minos);
  1300. sys::swapByteOrder(C.sdk);
  1301. sys::swapByteOrder(C.ntools);
  1302. }
  1303. inline void swapStruct(build_tool_version &C) {
  1304. sys::swapByteOrder(C.tool);
  1305. sys::swapByteOrder(C.version);
  1306. }
  1307. inline void swapStruct(data_in_code_entry &C) {
  1308. sys::swapByteOrder(C.offset);
  1309. sys::swapByteOrder(C.length);
  1310. sys::swapByteOrder(C.kind);
  1311. }
  1312. inline void swapStruct(uint32_t &C) { sys::swapByteOrder(C); }
  1313. // The prebind_cksum_command is obsolete and no longer supported.
  1314. inline void swapStruct(prebind_cksum_command &C) {
  1315. sys::swapByteOrder(C.cmd);
  1316. sys::swapByteOrder(C.cmdsize);
  1317. sys::swapByteOrder(C.cksum);
  1318. }
  1319. // The twolevel_hints_command is obsolete and no longer supported.
  1320. inline void swapStruct(twolevel_hints_command &C) {
  1321. sys::swapByteOrder(C.cmd);
  1322. sys::swapByteOrder(C.cmdsize);
  1323. sys::swapByteOrder(C.offset);
  1324. sys::swapByteOrder(C.nhints);
  1325. }
  1326. // The prebound_dylib_command is obsolete and no longer supported.
  1327. inline void swapStruct(prebound_dylib_command &C) {
  1328. sys::swapByteOrder(C.cmd);
  1329. sys::swapByteOrder(C.cmdsize);
  1330. sys::swapByteOrder(C.name);
  1331. sys::swapByteOrder(C.nmodules);
  1332. sys::swapByteOrder(C.linked_modules);
  1333. }
  1334. // The fvmfile_command is obsolete and no longer supported.
  1335. inline void swapStruct(fvmfile_command &C) {
  1336. sys::swapByteOrder(C.cmd);
  1337. sys::swapByteOrder(C.cmdsize);
  1338. sys::swapByteOrder(C.name);
  1339. sys::swapByteOrder(C.header_addr);
  1340. }
  1341. // The symseg_command is obsolete and no longer supported.
  1342. inline void swapStruct(symseg_command &C) {
  1343. sys::swapByteOrder(C.cmd);
  1344. sys::swapByteOrder(C.cmdsize);
  1345. sys::swapByteOrder(C.offset);
  1346. sys::swapByteOrder(C.size);
  1347. }
  1348. // The ident_command is obsolete and no longer supported.
  1349. inline void swapStruct(ident_command &C) {
  1350. sys::swapByteOrder(C.cmd);
  1351. sys::swapByteOrder(C.cmdsize);
  1352. }
  1353. inline void swapStruct(fvmlib &C) {
  1354. sys::swapByteOrder(C.name);
  1355. sys::swapByteOrder(C.minor_version);
  1356. sys::swapByteOrder(C.header_addr);
  1357. }
  1358. // The fvmlib_command is obsolete and no longer supported.
  1359. inline void swapStruct(fvmlib_command &C) {
  1360. sys::swapByteOrder(C.cmd);
  1361. sys::swapByteOrder(C.cmdsize);
  1362. swapStruct(C.fvmlib);
  1363. }
  1364. // Get/Set functions from <mach-o/nlist.h>
  1365. inline uint16_t GET_LIBRARY_ORDINAL(uint16_t n_desc) {
  1366. return (((n_desc) >> 8u) & 0xffu);
  1367. }
  1368. inline void SET_LIBRARY_ORDINAL(uint16_t &n_desc, uint8_t ordinal) {
  1369. n_desc = (((n_desc)&0x00ff) | (((ordinal)&0xff) << 8));
  1370. }
  1371. inline uint8_t GET_COMM_ALIGN(uint16_t n_desc) {
  1372. return (n_desc >> 8u) & 0x0fu;
  1373. }
  1374. inline void SET_COMM_ALIGN(uint16_t &n_desc, uint8_t align) {
  1375. n_desc = ((n_desc & 0xf0ffu) | ((align & 0x0fu) << 8u));
  1376. }
  1377. // Enums from <mach/machine.h>
  1378. enum : uint32_t {
  1379. // Capability bits used in the definition of cpu_type.
  1380. CPU_ARCH_MASK = 0xff000000, // Mask for architecture bits
  1381. CPU_ARCH_ABI64 = 0x01000000, // 64 bit ABI
  1382. CPU_ARCH_ABI64_32 = 0x02000000, // ILP32 ABI on 64-bit hardware
  1383. };
  1384. // Constants for the cputype field.
  1385. enum CPUType {
  1386. CPU_TYPE_ANY = -1,
  1387. CPU_TYPE_X86 = 7,
  1388. CPU_TYPE_I386 = CPU_TYPE_X86,
  1389. CPU_TYPE_X86_64 = CPU_TYPE_X86 | CPU_ARCH_ABI64,
  1390. /* CPU_TYPE_MIPS = 8, */
  1391. CPU_TYPE_MC98000 = 10, // Old Motorola PowerPC
  1392. CPU_TYPE_ARM = 12,
  1393. CPU_TYPE_ARM64 = CPU_TYPE_ARM | CPU_ARCH_ABI64,
  1394. CPU_TYPE_ARM64_32 = CPU_TYPE_ARM | CPU_ARCH_ABI64_32,
  1395. CPU_TYPE_SPARC = 14,
  1396. CPU_TYPE_POWERPC = 18,
  1397. CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64
  1398. };
  1399. enum : uint32_t {
  1400. // Capability bits used in the definition of cpusubtype.
  1401. CPU_SUBTYPE_MASK = 0xff000000, // Mask for architecture bits
  1402. CPU_SUBTYPE_LIB64 = 0x80000000, // 64 bit libraries
  1403. // Special CPU subtype constants.
  1404. CPU_SUBTYPE_MULTIPLE = ~0u
  1405. };
  1406. // Constants for the cpusubtype field.
  1407. enum CPUSubTypeX86 {
  1408. CPU_SUBTYPE_I386_ALL = 3,
  1409. CPU_SUBTYPE_386 = 3,
  1410. CPU_SUBTYPE_486 = 4,
  1411. CPU_SUBTYPE_486SX = 0x84,
  1412. CPU_SUBTYPE_586 = 5,
  1413. CPU_SUBTYPE_PENT = CPU_SUBTYPE_586,
  1414. CPU_SUBTYPE_PENTPRO = 0x16,
  1415. CPU_SUBTYPE_PENTII_M3 = 0x36,
  1416. CPU_SUBTYPE_PENTII_M5 = 0x56,
  1417. CPU_SUBTYPE_CELERON = 0x67,
  1418. CPU_SUBTYPE_CELERON_MOBILE = 0x77,
  1419. CPU_SUBTYPE_PENTIUM_3 = 0x08,
  1420. CPU_SUBTYPE_PENTIUM_3_M = 0x18,
  1421. CPU_SUBTYPE_PENTIUM_3_XEON = 0x28,
  1422. CPU_SUBTYPE_PENTIUM_M = 0x09,
  1423. CPU_SUBTYPE_PENTIUM_4 = 0x0a,
  1424. CPU_SUBTYPE_PENTIUM_4_M = 0x1a,
  1425. CPU_SUBTYPE_ITANIUM = 0x0b,
  1426. CPU_SUBTYPE_ITANIUM_2 = 0x1b,
  1427. CPU_SUBTYPE_XEON = 0x0c,
  1428. CPU_SUBTYPE_XEON_MP = 0x1c,
  1429. CPU_SUBTYPE_X86_ALL = 3,
  1430. CPU_SUBTYPE_X86_64_ALL = 3,
  1431. CPU_SUBTYPE_X86_ARCH1 = 4,
  1432. CPU_SUBTYPE_X86_64_H = 8
  1433. };
  1434. inline int CPU_SUBTYPE_INTEL(int Family, int Model) {
  1435. return Family | (Model << 4);
  1436. }
  1437. inline int CPU_SUBTYPE_INTEL_FAMILY(CPUSubTypeX86 ST) {
  1438. return ((int)ST) & 0x0f;
  1439. }
  1440. inline int CPU_SUBTYPE_INTEL_MODEL(CPUSubTypeX86 ST) { return ((int)ST) >> 4; }
  1441. enum { CPU_SUBTYPE_INTEL_FAMILY_MAX = 15, CPU_SUBTYPE_INTEL_MODEL_ALL = 0 };
  1442. enum CPUSubTypeARM {
  1443. CPU_SUBTYPE_ARM_ALL = 0,
  1444. CPU_SUBTYPE_ARM_V4T = 5,
  1445. CPU_SUBTYPE_ARM_V6 = 6,
  1446. CPU_SUBTYPE_ARM_V5 = 7,
  1447. CPU_SUBTYPE_ARM_V5TEJ = 7,
  1448. CPU_SUBTYPE_ARM_XSCALE = 8,
  1449. CPU_SUBTYPE_ARM_V7 = 9,
  1450. // unused ARM_V7F = 10,
  1451. CPU_SUBTYPE_ARM_V7S = 11,
  1452. CPU_SUBTYPE_ARM_V7K = 12,
  1453. CPU_SUBTYPE_ARM_V6M = 14,
  1454. CPU_SUBTYPE_ARM_V7M = 15,
  1455. CPU_SUBTYPE_ARM_V7EM = 16
  1456. };
  1457. enum CPUSubTypeARM64 {
  1458. CPU_SUBTYPE_ARM64_ALL = 0,
  1459. CPU_SUBTYPE_ARM64_V8 = 1,
  1460. CPU_SUBTYPE_ARM64E = 2,
  1461. };
  1462. enum CPUSubTypeARM64_32 { CPU_SUBTYPE_ARM64_32_V8 = 1 };
  1463. enum CPUSubTypeSPARC { CPU_SUBTYPE_SPARC_ALL = 0 };
  1464. enum CPUSubTypePowerPC {
  1465. CPU_SUBTYPE_POWERPC_ALL = 0,
  1466. CPU_SUBTYPE_POWERPC_601 = 1,
  1467. CPU_SUBTYPE_POWERPC_602 = 2,
  1468. CPU_SUBTYPE_POWERPC_603 = 3,
  1469. CPU_SUBTYPE_POWERPC_603e = 4,
  1470. CPU_SUBTYPE_POWERPC_603ev = 5,
  1471. CPU_SUBTYPE_POWERPC_604 = 6,
  1472. CPU_SUBTYPE_POWERPC_604e = 7,
  1473. CPU_SUBTYPE_POWERPC_620 = 8,
  1474. CPU_SUBTYPE_POWERPC_750 = 9,
  1475. CPU_SUBTYPE_POWERPC_7400 = 10,
  1476. CPU_SUBTYPE_POWERPC_7450 = 11,
  1477. CPU_SUBTYPE_POWERPC_970 = 100,
  1478. CPU_SUBTYPE_MC980000_ALL = CPU_SUBTYPE_POWERPC_ALL,
  1479. CPU_SUBTYPE_MC98601 = CPU_SUBTYPE_POWERPC_601
  1480. };
  1481. Expected<uint32_t> getCPUType(const Triple &T);
  1482. Expected<uint32_t> getCPUSubType(const Triple &T);
  1483. struct x86_thread_state32_t {
  1484. uint32_t eax;
  1485. uint32_t ebx;
  1486. uint32_t ecx;
  1487. uint32_t edx;
  1488. uint32_t edi;
  1489. uint32_t esi;
  1490. uint32_t ebp;
  1491. uint32_t esp;
  1492. uint32_t ss;
  1493. uint32_t eflags;
  1494. uint32_t eip;
  1495. uint32_t cs;
  1496. uint32_t ds;
  1497. uint32_t es;
  1498. uint32_t fs;
  1499. uint32_t gs;
  1500. };
  1501. struct x86_thread_state64_t {
  1502. uint64_t rax;
  1503. uint64_t rbx;
  1504. uint64_t rcx;
  1505. uint64_t rdx;
  1506. uint64_t rdi;
  1507. uint64_t rsi;
  1508. uint64_t rbp;
  1509. uint64_t rsp;
  1510. uint64_t r8;
  1511. uint64_t r9;
  1512. uint64_t r10;
  1513. uint64_t r11;
  1514. uint64_t r12;
  1515. uint64_t r13;
  1516. uint64_t r14;
  1517. uint64_t r15;
  1518. uint64_t rip;
  1519. uint64_t rflags;
  1520. uint64_t cs;
  1521. uint64_t fs;
  1522. uint64_t gs;
  1523. };
  1524. enum x86_fp_control_precis {
  1525. x86_FP_PREC_24B = 0,
  1526. x86_FP_PREC_53B = 2,
  1527. x86_FP_PREC_64B = 3
  1528. };
  1529. enum x86_fp_control_rc {
  1530. x86_FP_RND_NEAR = 0,
  1531. x86_FP_RND_DOWN = 1,
  1532. x86_FP_RND_UP = 2,
  1533. x86_FP_CHOP = 3
  1534. };
  1535. struct fp_control_t {
  1536. unsigned short invalid : 1, denorm : 1, zdiv : 1, ovrfl : 1, undfl : 1,
  1537. precis : 1, : 2, pc : 2, rc : 2, : 1, : 3;
  1538. };
  1539. struct fp_status_t {
  1540. unsigned short invalid : 1, denorm : 1, zdiv : 1, ovrfl : 1, undfl : 1,
  1541. precis : 1, stkflt : 1, errsumm : 1, c0 : 1, c1 : 1, c2 : 1, tos : 3,
  1542. c3 : 1, busy : 1;
  1543. };
  1544. struct mmst_reg_t {
  1545. char mmst_reg[10];
  1546. char mmst_rsrv[6];
  1547. };
  1548. struct xmm_reg_t {
  1549. char xmm_reg[16];
  1550. };
  1551. struct x86_float_state64_t {
  1552. int32_t fpu_reserved[2];
  1553. fp_control_t fpu_fcw;
  1554. fp_status_t fpu_fsw;
  1555. uint8_t fpu_ftw;
  1556. uint8_t fpu_rsrv1;
  1557. uint16_t fpu_fop;
  1558. uint32_t fpu_ip;
  1559. uint16_t fpu_cs;
  1560. uint16_t fpu_rsrv2;
  1561. uint32_t fpu_dp;
  1562. uint16_t fpu_ds;
  1563. uint16_t fpu_rsrv3;
  1564. uint32_t fpu_mxcsr;
  1565. uint32_t fpu_mxcsrmask;
  1566. mmst_reg_t fpu_stmm0;
  1567. mmst_reg_t fpu_stmm1;
  1568. mmst_reg_t fpu_stmm2;
  1569. mmst_reg_t fpu_stmm3;
  1570. mmst_reg_t fpu_stmm4;
  1571. mmst_reg_t fpu_stmm5;
  1572. mmst_reg_t fpu_stmm6;
  1573. mmst_reg_t fpu_stmm7;
  1574. xmm_reg_t fpu_xmm0;
  1575. xmm_reg_t fpu_xmm1;
  1576. xmm_reg_t fpu_xmm2;
  1577. xmm_reg_t fpu_xmm3;
  1578. xmm_reg_t fpu_xmm4;
  1579. xmm_reg_t fpu_xmm5;
  1580. xmm_reg_t fpu_xmm6;
  1581. xmm_reg_t fpu_xmm7;
  1582. xmm_reg_t fpu_xmm8;
  1583. xmm_reg_t fpu_xmm9;
  1584. xmm_reg_t fpu_xmm10;
  1585. xmm_reg_t fpu_xmm11;
  1586. xmm_reg_t fpu_xmm12;
  1587. xmm_reg_t fpu_xmm13;
  1588. xmm_reg_t fpu_xmm14;
  1589. xmm_reg_t fpu_xmm15;
  1590. char fpu_rsrv4[6 * 16];
  1591. uint32_t fpu_reserved1;
  1592. };
  1593. struct x86_exception_state64_t {
  1594. uint16_t trapno;
  1595. uint16_t cpu;
  1596. uint32_t err;
  1597. uint64_t faultvaddr;
  1598. };
  1599. inline void swapStruct(x86_thread_state32_t &x) {
  1600. sys::swapByteOrder(x.eax);
  1601. sys::swapByteOrder(x.ebx);
  1602. sys::swapByteOrder(x.ecx);
  1603. sys::swapByteOrder(x.edx);
  1604. sys::swapByteOrder(x.edi);
  1605. sys::swapByteOrder(x.esi);
  1606. sys::swapByteOrder(x.ebp);
  1607. sys::swapByteOrder(x.esp);
  1608. sys::swapByteOrder(x.ss);
  1609. sys::swapByteOrder(x.eflags);
  1610. sys::swapByteOrder(x.eip);
  1611. sys::swapByteOrder(x.cs);
  1612. sys::swapByteOrder(x.ds);
  1613. sys::swapByteOrder(x.es);
  1614. sys::swapByteOrder(x.fs);
  1615. sys::swapByteOrder(x.gs);
  1616. }
  1617. inline void swapStruct(x86_thread_state64_t &x) {
  1618. sys::swapByteOrder(x.rax);
  1619. sys::swapByteOrder(x.rbx);
  1620. sys::swapByteOrder(x.rcx);
  1621. sys::swapByteOrder(x.rdx);
  1622. sys::swapByteOrder(x.rdi);
  1623. sys::swapByteOrder(x.rsi);
  1624. sys::swapByteOrder(x.rbp);
  1625. sys::swapByteOrder(x.rsp);
  1626. sys::swapByteOrder(x.r8);
  1627. sys::swapByteOrder(x.r9);
  1628. sys::swapByteOrder(x.r10);
  1629. sys::swapByteOrder(x.r11);
  1630. sys::swapByteOrder(x.r12);
  1631. sys::swapByteOrder(x.r13);
  1632. sys::swapByteOrder(x.r14);
  1633. sys::swapByteOrder(x.r15);
  1634. sys::swapByteOrder(x.rip);
  1635. sys::swapByteOrder(x.rflags);
  1636. sys::swapByteOrder(x.cs);
  1637. sys::swapByteOrder(x.fs);
  1638. sys::swapByteOrder(x.gs);
  1639. }
  1640. inline void swapStruct(x86_float_state64_t &x) {
  1641. sys::swapByteOrder(x.fpu_reserved[0]);
  1642. sys::swapByteOrder(x.fpu_reserved[1]);
  1643. // TODO swap: fp_control_t fpu_fcw;
  1644. // TODO swap: fp_status_t fpu_fsw;
  1645. sys::swapByteOrder(x.fpu_fop);
  1646. sys::swapByteOrder(x.fpu_ip);
  1647. sys::swapByteOrder(x.fpu_cs);
  1648. sys::swapByteOrder(x.fpu_rsrv2);
  1649. sys::swapByteOrder(x.fpu_dp);
  1650. sys::swapByteOrder(x.fpu_ds);
  1651. sys::swapByteOrder(x.fpu_rsrv3);
  1652. sys::swapByteOrder(x.fpu_mxcsr);
  1653. sys::swapByteOrder(x.fpu_mxcsrmask);
  1654. sys::swapByteOrder(x.fpu_reserved1);
  1655. }
  1656. inline void swapStruct(x86_exception_state64_t &x) {
  1657. sys::swapByteOrder(x.trapno);
  1658. sys::swapByteOrder(x.cpu);
  1659. sys::swapByteOrder(x.err);
  1660. sys::swapByteOrder(x.faultvaddr);
  1661. }
  1662. struct x86_state_hdr_t {
  1663. uint32_t flavor;
  1664. uint32_t count;
  1665. };
  1666. struct x86_thread_state_t {
  1667. x86_state_hdr_t tsh;
  1668. union {
  1669. x86_thread_state64_t ts64;
  1670. x86_thread_state32_t ts32;
  1671. } uts;
  1672. };
  1673. struct x86_float_state_t {
  1674. x86_state_hdr_t fsh;
  1675. union {
  1676. x86_float_state64_t fs64;
  1677. } ufs;
  1678. };
  1679. struct x86_exception_state_t {
  1680. x86_state_hdr_t esh;
  1681. union {
  1682. x86_exception_state64_t es64;
  1683. } ues;
  1684. };
  1685. inline void swapStruct(x86_state_hdr_t &x) {
  1686. sys::swapByteOrder(x.flavor);
  1687. sys::swapByteOrder(x.count);
  1688. }
  1689. enum X86ThreadFlavors {
  1690. x86_THREAD_STATE32 = 1,
  1691. x86_FLOAT_STATE32 = 2,
  1692. x86_EXCEPTION_STATE32 = 3,
  1693. x86_THREAD_STATE64 = 4,
  1694. x86_FLOAT_STATE64 = 5,
  1695. x86_EXCEPTION_STATE64 = 6,
  1696. x86_THREAD_STATE = 7,
  1697. x86_FLOAT_STATE = 8,
  1698. x86_EXCEPTION_STATE = 9,
  1699. x86_DEBUG_STATE32 = 10,
  1700. x86_DEBUG_STATE64 = 11,
  1701. x86_DEBUG_STATE = 12
  1702. };
  1703. inline void swapStruct(x86_thread_state_t &x) {
  1704. swapStruct(x.tsh);
  1705. if (x.tsh.flavor == x86_THREAD_STATE64)
  1706. swapStruct(x.uts.ts64);
  1707. }
  1708. inline void swapStruct(x86_float_state_t &x) {
  1709. swapStruct(x.fsh);
  1710. if (x.fsh.flavor == x86_FLOAT_STATE64)
  1711. swapStruct(x.ufs.fs64);
  1712. }
  1713. inline void swapStruct(x86_exception_state_t &x) {
  1714. swapStruct(x.esh);
  1715. if (x.esh.flavor == x86_EXCEPTION_STATE64)
  1716. swapStruct(x.ues.es64);
  1717. }
  1718. const uint32_t x86_THREAD_STATE32_COUNT =
  1719. sizeof(x86_thread_state32_t) / sizeof(uint32_t);
  1720. const uint32_t x86_THREAD_STATE64_COUNT =
  1721. sizeof(x86_thread_state64_t) / sizeof(uint32_t);
  1722. const uint32_t x86_FLOAT_STATE64_COUNT =
  1723. sizeof(x86_float_state64_t) / sizeof(uint32_t);
  1724. const uint32_t x86_EXCEPTION_STATE64_COUNT =
  1725. sizeof(x86_exception_state64_t) / sizeof(uint32_t);
  1726. const uint32_t x86_THREAD_STATE_COUNT =
  1727. sizeof(x86_thread_state_t) / sizeof(uint32_t);
  1728. const uint32_t x86_FLOAT_STATE_COUNT =
  1729. sizeof(x86_float_state_t) / sizeof(uint32_t);
  1730. const uint32_t x86_EXCEPTION_STATE_COUNT =
  1731. sizeof(x86_exception_state_t) / sizeof(uint32_t);
  1732. struct arm_thread_state32_t {
  1733. uint32_t r[13];
  1734. uint32_t sp;
  1735. uint32_t lr;
  1736. uint32_t pc;
  1737. uint32_t cpsr;
  1738. };
  1739. inline void swapStruct(arm_thread_state32_t &x) {
  1740. for (int i = 0; i < 13; i++)
  1741. sys::swapByteOrder(x.r[i]);
  1742. sys::swapByteOrder(x.sp);
  1743. sys::swapByteOrder(x.lr);
  1744. sys::swapByteOrder(x.pc);
  1745. sys::swapByteOrder(x.cpsr);
  1746. }
  1747. struct arm_thread_state64_t {
  1748. uint64_t x[29];
  1749. uint64_t fp;
  1750. uint64_t lr;
  1751. uint64_t sp;
  1752. uint64_t pc;
  1753. uint32_t cpsr;
  1754. uint32_t pad;
  1755. };
  1756. inline void swapStruct(arm_thread_state64_t &x) {
  1757. for (int i = 0; i < 29; i++)
  1758. sys::swapByteOrder(x.x[i]);
  1759. sys::swapByteOrder(x.fp);
  1760. sys::swapByteOrder(x.lr);
  1761. sys::swapByteOrder(x.sp);
  1762. sys::swapByteOrder(x.pc);
  1763. sys::swapByteOrder(x.cpsr);
  1764. }
  1765. struct arm_state_hdr_t {
  1766. uint32_t flavor;
  1767. uint32_t count;
  1768. };
  1769. struct arm_thread_state_t {
  1770. arm_state_hdr_t tsh;
  1771. union {
  1772. arm_thread_state32_t ts32;
  1773. } uts;
  1774. };
  1775. inline void swapStruct(arm_state_hdr_t &x) {
  1776. sys::swapByteOrder(x.flavor);
  1777. sys::swapByteOrder(x.count);
  1778. }
  1779. enum ARMThreadFlavors {
  1780. ARM_THREAD_STATE = 1,
  1781. ARM_VFP_STATE = 2,
  1782. ARM_EXCEPTION_STATE = 3,
  1783. ARM_DEBUG_STATE = 4,
  1784. ARN_THREAD_STATE_NONE = 5,
  1785. ARM_THREAD_STATE64 = 6,
  1786. ARM_EXCEPTION_STATE64 = 7
  1787. };
  1788. inline void swapStruct(arm_thread_state_t &x) {
  1789. swapStruct(x.tsh);
  1790. if (x.tsh.flavor == ARM_THREAD_STATE)
  1791. swapStruct(x.uts.ts32);
  1792. }
  1793. const uint32_t ARM_THREAD_STATE_COUNT =
  1794. sizeof(arm_thread_state32_t) / sizeof(uint32_t);
  1795. const uint32_t ARM_THREAD_STATE64_COUNT =
  1796. sizeof(arm_thread_state64_t) / sizeof(uint32_t);
  1797. struct ppc_thread_state32_t {
  1798. uint32_t srr0;
  1799. uint32_t srr1;
  1800. uint32_t r0;
  1801. uint32_t r1;
  1802. uint32_t r2;
  1803. uint32_t r3;
  1804. uint32_t r4;
  1805. uint32_t r5;
  1806. uint32_t r6;
  1807. uint32_t r7;
  1808. uint32_t r8;
  1809. uint32_t r9;
  1810. uint32_t r10;
  1811. uint32_t r11;
  1812. uint32_t r12;
  1813. uint32_t r13;
  1814. uint32_t r14;
  1815. uint32_t r15;
  1816. uint32_t r16;
  1817. uint32_t r17;
  1818. uint32_t r18;
  1819. uint32_t r19;
  1820. uint32_t r20;
  1821. uint32_t r21;
  1822. uint32_t r22;
  1823. uint32_t r23;
  1824. uint32_t r24;
  1825. uint32_t r25;
  1826. uint32_t r26;
  1827. uint32_t r27;
  1828. uint32_t r28;
  1829. uint32_t r29;
  1830. uint32_t r30;
  1831. uint32_t r31;
  1832. uint32_t ct;
  1833. uint32_t xer;
  1834. uint32_t lr;
  1835. uint32_t ctr;
  1836. uint32_t mq;
  1837. uint32_t vrsave;
  1838. };
  1839. inline void swapStruct(ppc_thread_state32_t &x) {
  1840. sys::swapByteOrder(x.srr0);
  1841. sys::swapByteOrder(x.srr1);
  1842. sys::swapByteOrder(x.r0);
  1843. sys::swapByteOrder(x.r1);
  1844. sys::swapByteOrder(x.r2);
  1845. sys::swapByteOrder(x.r3);
  1846. sys::swapByteOrder(x.r4);
  1847. sys::swapByteOrder(x.r5);
  1848. sys::swapByteOrder(x.r6);
  1849. sys::swapByteOrder(x.r7);
  1850. sys::swapByteOrder(x.r8);
  1851. sys::swapByteOrder(x.r9);
  1852. sys::swapByteOrder(x.r10);
  1853. sys::swapByteOrder(x.r11);
  1854. sys::swapByteOrder(x.r12);
  1855. sys::swapByteOrder(x.r13);
  1856. sys::swapByteOrder(x.r14);
  1857. sys::swapByteOrder(x.r15);
  1858. sys::swapByteOrder(x.r16);
  1859. sys::swapByteOrder(x.r17);
  1860. sys::swapByteOrder(x.r18);
  1861. sys::swapByteOrder(x.r19);
  1862. sys::swapByteOrder(x.r20);
  1863. sys::swapByteOrder(x.r21);
  1864. sys::swapByteOrder(x.r22);
  1865. sys::swapByteOrder(x.r23);
  1866. sys::swapByteOrder(x.r24);
  1867. sys::swapByteOrder(x.r25);
  1868. sys::swapByteOrder(x.r26);
  1869. sys::swapByteOrder(x.r27);
  1870. sys::swapByteOrder(x.r28);
  1871. sys::swapByteOrder(x.r29);
  1872. sys::swapByteOrder(x.r30);
  1873. sys::swapByteOrder(x.r31);
  1874. sys::swapByteOrder(x.ct);
  1875. sys::swapByteOrder(x.xer);
  1876. sys::swapByteOrder(x.lr);
  1877. sys::swapByteOrder(x.ctr);
  1878. sys::swapByteOrder(x.mq);
  1879. sys::swapByteOrder(x.vrsave);
  1880. }
  1881. struct ppc_state_hdr_t {
  1882. uint32_t flavor;
  1883. uint32_t count;
  1884. };
  1885. struct ppc_thread_state_t {
  1886. ppc_state_hdr_t tsh;
  1887. union {
  1888. ppc_thread_state32_t ts32;
  1889. } uts;
  1890. };
  1891. inline void swapStruct(ppc_state_hdr_t &x) {
  1892. sys::swapByteOrder(x.flavor);
  1893. sys::swapByteOrder(x.count);
  1894. }
  1895. enum PPCThreadFlavors {
  1896. PPC_THREAD_STATE = 1,
  1897. PPC_FLOAT_STATE = 2,
  1898. PPC_EXCEPTION_STATE = 3,
  1899. PPC_VECTOR_STATE = 4,
  1900. PPC_THREAD_STATE64 = 5,
  1901. PPC_EXCEPTION_STATE64 = 6,
  1902. PPC_THREAD_STATE_NONE = 7
  1903. };
  1904. inline void swapStruct(ppc_thread_state_t &x) {
  1905. swapStruct(x.tsh);
  1906. if (x.tsh.flavor == PPC_THREAD_STATE)
  1907. swapStruct(x.uts.ts32);
  1908. }
  1909. const uint32_t PPC_THREAD_STATE_COUNT =
  1910. sizeof(ppc_thread_state32_t) / sizeof(uint32_t);
  1911. // Define a union of all load command structs
  1912. #define LOAD_COMMAND_STRUCT(LCStruct) LCStruct LCStruct##_data;
  1913. LLVM_PACKED_START
  1914. union alignas(4) macho_load_command {
  1915. #include "llvm/BinaryFormat/MachO.def"
  1916. };
  1917. LLVM_PACKED_END
  1918. inline void swapStruct(dyld_chained_fixups_header &C) {
  1919. sys::swapByteOrder(C.fixups_version);
  1920. sys::swapByteOrder(C.starts_offset);
  1921. sys::swapByteOrder(C.imports_offset);
  1922. sys::swapByteOrder(C.symbols_offset);
  1923. sys::swapByteOrder(C.imports_count);
  1924. sys::swapByteOrder(C.imports_format);
  1925. sys::swapByteOrder(C.symbols_format);
  1926. }
  1927. inline void swapStruct(dyld_chained_starts_in_image &C) {
  1928. sys::swapByteOrder(C.seg_count);
  1929. // getStructOrErr() cannot copy the variable-length seg_info_offset array.
  1930. // Its elements must be byte swapped manually.
  1931. }
  1932. inline void swapStruct(dyld_chained_starts_in_segment &C) {
  1933. sys::swapByteOrder(C.size);
  1934. sys::swapByteOrder(C.page_size);
  1935. sys::swapByteOrder(C.pointer_format);
  1936. sys::swapByteOrder(C.segment_offset);
  1937. sys::swapByteOrder(C.max_valid_pointer);
  1938. sys::swapByteOrder(C.page_count);
  1939. // seg_info_offset entries must be byte swapped manually.
  1940. }
  1941. /* code signing attributes of a process */
  1942. enum CodeSignAttrs {
  1943. CS_VALID = 0x00000001, /* dynamically valid */
  1944. CS_ADHOC = 0x00000002, /* ad hoc signed */
  1945. CS_GET_TASK_ALLOW = 0x00000004, /* has get-task-allow entitlement */
  1946. CS_INSTALLER = 0x00000008, /* has installer entitlement */
  1947. CS_FORCED_LV =
  1948. 0x00000010, /* Library Validation required by Hardened System Policy */
  1949. CS_INVALID_ALLOWED = 0x00000020, /* (macOS Only) Page invalidation allowed by
  1950. task port policy */
  1951. CS_HARD = 0x00000100, /* don't load invalid pages */
  1952. CS_KILL = 0x00000200, /* kill process if it becomes invalid */
  1953. CS_CHECK_EXPIRATION = 0x00000400, /* force expiration checking */
  1954. CS_RESTRICT = 0x00000800, /* tell dyld to treat restricted */
  1955. CS_ENFORCEMENT = 0x00001000, /* require enforcement */
  1956. CS_REQUIRE_LV = 0x00002000, /* require library validation */
  1957. CS_ENTITLEMENTS_VALIDATED =
  1958. 0x00004000, /* code signature permits restricted entitlements */
  1959. CS_NVRAM_UNRESTRICTED =
  1960. 0x00008000, /* has com.apple.rootless.restricted-nvram-variables.heritable
  1961. entitlement */
  1962. CS_RUNTIME = 0x00010000, /* Apply hardened runtime policies */
  1963. CS_LINKER_SIGNED = 0x00020000, /* Automatically signed by the linker */
  1964. CS_ALLOWED_MACHO =
  1965. (CS_ADHOC | CS_HARD | CS_KILL | CS_CHECK_EXPIRATION | CS_RESTRICT |
  1966. CS_ENFORCEMENT | CS_REQUIRE_LV | CS_RUNTIME | CS_LINKER_SIGNED),
  1967. CS_EXEC_SET_HARD = 0x00100000, /* set CS_HARD on any exec'ed process */
  1968. CS_EXEC_SET_KILL = 0x00200000, /* set CS_KILL on any exec'ed process */
  1969. CS_EXEC_SET_ENFORCEMENT =
  1970. 0x00400000, /* set CS_ENFORCEMENT on any exec'ed process */
  1971. CS_EXEC_INHERIT_SIP =
  1972. 0x00800000, /* set CS_INSTALLER on any exec'ed process */
  1973. CS_KILLED = 0x01000000, /* was killed by kernel for invalidity */
  1974. CS_DYLD_PLATFORM =
  1975. 0x02000000, /* dyld used to load this is a platform binary */
  1976. CS_PLATFORM_BINARY = 0x04000000, /* this is a platform binary */
  1977. CS_PLATFORM_PATH =
  1978. 0x08000000, /* platform binary by the fact of path (osx only) */
  1979. CS_DEBUGGED = 0x10000000, /* process is currently or has previously been
  1980. debugged and allowed to run with invalid pages */
  1981. CS_SIGNED = 0x20000000, /* process has a signature (may have gone invalid) */
  1982. CS_DEV_CODE =
  1983. 0x40000000, /* code is dev signed, cannot be loaded into prod signed code
  1984. (will go away with rdar://problem/28322552) */
  1985. CS_DATAVAULT_CONTROLLER =
  1986. 0x80000000, /* has Data Vault controller entitlement */
  1987. CS_ENTITLEMENT_FLAGS = (CS_GET_TASK_ALLOW | CS_INSTALLER |
  1988. CS_DATAVAULT_CONTROLLER | CS_NVRAM_UNRESTRICTED),
  1989. };
  1990. /* executable segment flags */
  1991. enum CodeSignExecSegFlags {
  1992. CS_EXECSEG_MAIN_BINARY = 0x1, /* executable segment denotes main binary */
  1993. CS_EXECSEG_ALLOW_UNSIGNED = 0x10, /* allow unsigned pages (for debugging) */
  1994. CS_EXECSEG_DEBUGGER = 0x20, /* main binary is debugger */
  1995. CS_EXECSEG_JIT = 0x40, /* JIT enabled */
  1996. CS_EXECSEG_SKIP_LV = 0x80, /* OBSOLETE: skip library validation */
  1997. CS_EXECSEG_CAN_LOAD_CDHASH = 0x100, /* can bless cdhash for execution */
  1998. CS_EXECSEG_CAN_EXEC_CDHASH = 0x200, /* can execute blessed cdhash */
  1999. };
  2000. /* Magic numbers used by Code Signing */
  2001. enum CodeSignMagic {
  2002. CSMAGIC_REQUIREMENT = 0xfade0c00, /* single Requirement blob */
  2003. CSMAGIC_REQUIREMENTS =
  2004. 0xfade0c01, /* Requirements vector (internal requirements) */
  2005. CSMAGIC_CODEDIRECTORY = 0xfade0c02, /* CodeDirectory blob */
  2006. CSMAGIC_EMBEDDED_SIGNATURE = 0xfade0cc0, /* embedded form of signature data */
  2007. CSMAGIC_EMBEDDED_SIGNATURE_OLD = 0xfade0b02, /* XXX */
  2008. CSMAGIC_EMBEDDED_ENTITLEMENTS = 0xfade7171, /* embedded entitlements */
  2009. CSMAGIC_DETACHED_SIGNATURE =
  2010. 0xfade0cc1, /* multi-arch collection of embedded signatures */
  2011. CSMAGIC_BLOBWRAPPER = 0xfade0b01, /* CMS Signature, among other things */
  2012. CS_SUPPORTSSCATTER = 0x20100,
  2013. CS_SUPPORTSTEAMID = 0x20200,
  2014. CS_SUPPORTSCODELIMIT64 = 0x20300,
  2015. CS_SUPPORTSEXECSEG = 0x20400,
  2016. CS_SUPPORTSRUNTIME = 0x20500,
  2017. CS_SUPPORTSLINKAGE = 0x20600,
  2018. CSSLOT_CODEDIRECTORY = 0, /* slot index for CodeDirectory */
  2019. CSSLOT_INFOSLOT = 1,
  2020. CSSLOT_REQUIREMENTS = 2,
  2021. CSSLOT_RESOURCEDIR = 3,
  2022. CSSLOT_APPLICATION = 4,
  2023. CSSLOT_ENTITLEMENTS = 5,
  2024. CSSLOT_ALTERNATE_CODEDIRECTORIES =
  2025. 0x1000, /* first alternate CodeDirectory, if any */
  2026. CSSLOT_ALTERNATE_CODEDIRECTORY_MAX = 5, /* max number of alternate CD slots */
  2027. CSSLOT_ALTERNATE_CODEDIRECTORY_LIMIT =
  2028. CSSLOT_ALTERNATE_CODEDIRECTORIES +
  2029. CSSLOT_ALTERNATE_CODEDIRECTORY_MAX, /* one past the last */
  2030. CSSLOT_SIGNATURESLOT = 0x10000, /* CMS Signature */
  2031. CSSLOT_IDENTIFICATIONSLOT = 0x10001,
  2032. CSSLOT_TICKETSLOT = 0x10002,
  2033. CSTYPE_INDEX_REQUIREMENTS = 0x00000002, /* compat with amfi */
  2034. CSTYPE_INDEX_ENTITLEMENTS = 0x00000005, /* compat with amfi */
  2035. CS_HASHTYPE_SHA1 = 1,
  2036. CS_HASHTYPE_SHA256 = 2,
  2037. CS_HASHTYPE_SHA256_TRUNCATED = 3,
  2038. CS_HASHTYPE_SHA384 = 4,
  2039. CS_SHA1_LEN = 20,
  2040. CS_SHA256_LEN = 32,
  2041. CS_SHA256_TRUNCATED_LEN = 20,
  2042. CS_CDHASH_LEN = 20, /* always - larger hashes are truncated */
  2043. CS_HASH_MAX_SIZE = 48, /* max size of the hash we'll support */
  2044. /*
  2045. * Currently only to support Legacy VPN plugins, and Mac App Store
  2046. * but intended to replace all the various platform code, dev code etc. bits.
  2047. */
  2048. CS_SIGNER_TYPE_UNKNOWN = 0,
  2049. CS_SIGNER_TYPE_LEGACYVPN = 5,
  2050. CS_SIGNER_TYPE_MAC_APP_STORE = 6,
  2051. CS_SUPPL_SIGNER_TYPE_UNKNOWN = 0,
  2052. CS_SUPPL_SIGNER_TYPE_TRUSTCACHE = 7,
  2053. CS_SUPPL_SIGNER_TYPE_LOCAL = 8,
  2054. };
  2055. struct CS_CodeDirectory {
  2056. uint32_t magic; /* magic number (CSMAGIC_CODEDIRECTORY) */
  2057. uint32_t length; /* total length of CodeDirectory blob */
  2058. uint32_t version; /* compatibility version */
  2059. uint32_t flags; /* setup and mode flags */
  2060. uint32_t hashOffset; /* offset of hash slot element at index zero */
  2061. uint32_t identOffset; /* offset of identifier string */
  2062. uint32_t nSpecialSlots; /* number of special hash slots */
  2063. uint32_t nCodeSlots; /* number of ordinary (code) hash slots */
  2064. uint32_t codeLimit; /* limit to main image signature range */
  2065. uint8_t hashSize; /* size of each hash in bytes */
  2066. uint8_t hashType; /* type of hash (cdHashType* constants) */
  2067. uint8_t platform; /* platform identifier; zero if not platform binary */
  2068. uint8_t pageSize; /* log2(page size in bytes); 0 => infinite */
  2069. uint32_t spare2; /* unused (must be zero) */
  2070. /* Version 0x20100 */
  2071. uint32_t scatterOffset; /* offset of optional scatter vector */
  2072. /* Version 0x20200 */
  2073. uint32_t teamOffset; /* offset of optional team identifier */
  2074. /* Version 0x20300 */
  2075. uint32_t spare3; /* unused (must be zero) */
  2076. uint64_t codeLimit64; /* limit to main image signature range, 64 bits */
  2077. /* Version 0x20400 */
  2078. uint64_t execSegBase; /* offset of executable segment */
  2079. uint64_t execSegLimit; /* limit of executable segment */
  2080. uint64_t execSegFlags; /* executable segment flags */
  2081. };
  2082. static_assert(sizeof(CS_CodeDirectory) == 88);
  2083. struct CS_BlobIndex {
  2084. uint32_t type; /* type of entry */
  2085. uint32_t offset; /* offset of entry */
  2086. };
  2087. struct CS_SuperBlob {
  2088. uint32_t magic; /* magic number */
  2089. uint32_t length; /* total length of SuperBlob */
  2090. uint32_t count; /* number of index entries following */
  2091. /* followed by Blobs in no particular order as indicated by index offsets */
  2092. };
  2093. enum SecCSDigestAlgorithm {
  2094. kSecCodeSignatureNoHash = 0, /* null value */
  2095. kSecCodeSignatureHashSHA1 = 1, /* SHA-1 */
  2096. kSecCodeSignatureHashSHA256 = 2, /* SHA-256 */
  2097. kSecCodeSignatureHashSHA256Truncated =
  2098. 3, /* SHA-256 truncated to first 20 bytes */
  2099. kSecCodeSignatureHashSHA384 = 4, /* SHA-384 */
  2100. kSecCodeSignatureHashSHA512 = 5, /* SHA-512 */
  2101. };
  2102. enum LinkerOptimizationHintKind {
  2103. LOH_ARM64_ADRP_ADRP = 1,
  2104. LOH_ARM64_ADRP_LDR = 2,
  2105. LOH_ARM64_ADRP_ADD_LDR = 3,
  2106. LOH_ARM64_ADRP_LDR_GOT_LDR = 4,
  2107. LOH_ARM64_ADRP_ADD_STR = 5,
  2108. LOH_ARM64_ADRP_LDR_GOT_STR = 6,
  2109. LOH_ARM64_ADRP_ADD = 7,
  2110. LOH_ARM64_ADRP_LDR_GOT = 8,
  2111. };
  2112. } // end namespace MachO
  2113. } // end namespace llvm
  2114. #endif
  2115. #ifdef __GNUC__
  2116. #pragma GCC diagnostic pop
  2117. #endif