objc-abi.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (c) 2009 Apple Inc. All Rights Reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://www.opensource.apple.com/apsl/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPLE_LICENSE_HEADER_END@
  22. */
  23. #ifndef _OBJC_ABI_H
  24. #define _OBJC_ABI_H
  25. /*
  26. * WARNING DANGER HAZARD BEWARE EEK
  27. *
  28. * Everything in this file is for Apple Internal use only.
  29. * These will change in arbitrary OS updates and in unpredictable ways.
  30. * When your program breaks, you get to keep both pieces.
  31. */
  32. /*
  33. * objc-abi.h: Declarations for functions used by compiler codegen.
  34. */
  35. #include <malloc/malloc.h>
  36. #include <TargetConditionals.h>
  37. #include <objc/objc.h>
  38. #include <objc/runtime.h>
  39. #include <objc/message.h>
  40. /* Linker metadata symbols */
  41. // NSObject was in Foundation/CF on macOS < 10.8.
  42. #if TARGET_OS_OSX
  43. #if __OBJC2__
  44. OBJC_EXPORT const char __objc_nsobject_class_10_5
  45. __asm__("$ld$hide$os10.5$_OBJC_CLASS_$_NSObject");
  46. OBJC_EXPORT const char __objc_nsobject_class_10_6
  47. __asm__("$ld$hide$os10.6$_OBJC_CLASS_$_NSObject");
  48. OBJC_EXPORT const char __objc_nsobject_class_10_7
  49. __asm__("$ld$hide$os10.7$_OBJC_CLASS_$_NSObject");
  50. OBJC_EXPORT const char __objc_nsobject_metaclass_10_5
  51. __asm__("$ld$hide$os10.5$_OBJC_METACLASS_$_NSObject");
  52. OBJC_EXPORT const char __objc_nsobject_metaclass_10_6
  53. __asm__("$ld$hide$os10.6$_OBJC_METACLASS_$_NSObject");
  54. OBJC_EXPORT const char __objc_nsobject_metaclass_10_7
  55. __asm__("$ld$hide$os10.7$_OBJC_METACLASS_$_NSObject");
  56. OBJC_EXPORT const char __objc_nsobject_isa_10_5
  57. __asm__("$ld$hide$os10.5$_OBJC_IVAR_$_NSObject.isa");
  58. OBJC_EXPORT const char __objc_nsobject_isa_10_6
  59. __asm__("$ld$hide$os10.6$_OBJC_IVAR_$_NSObject.isa");
  60. OBJC_EXPORT const char __objc_nsobject_isa_10_7
  61. __asm__("$ld$hide$os10.7$_OBJC_IVAR_$_NSObject.isa");
  62. #else
  63. OBJC_EXPORT const char __objc_nsobject_class_10_5
  64. __asm__("$ld$hide$os10.5$.objc_class_name_NSObject");
  65. OBJC_EXPORT const char __objc_nsobject_class_10_6
  66. __asm__("$ld$hide$os10.6$.objc_class_name_NSObject");
  67. OBJC_EXPORT const char __objc_nsobject_class_10_7
  68. __asm__("$ld$hide$os10.7$.objc_class_name_NSObject");
  69. #endif
  70. #endif
  71. /* Runtime startup. */
  72. // Old static initializer. Used by old crt1.o and old bug workarounds.
  73. OBJC_EXPORT void
  74. _objcInit(void)
  75. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  76. /* Images */
  77. // Description of an Objective-C image.
  78. // __DATA,__objc_imageinfo stores one of these.
  79. typedef struct objc_image_info {
  80. uint32_t version; // currently 0
  81. uint32_t flags;
  82. #if __cplusplus >= 201103L
  83. private:
  84. enum : uint32_t {
  85. IsReplacement = 1<<0, // used for Fix&Continue, now ignored
  86. SupportsGC = 1<<1, // image supports GC
  87. RequiresGC = 1<<2, // image requires GC
  88. OptimizedByDyld = 1<<3, // image is from an optimized shared cache
  89. CorrectedSynthesize = 1<<4, // used for an old workaround, now ignored
  90. IsSimulated = 1<<5, // image compiled for a simulator platform
  91. HasCategoryClassProperties = 1<<6, // class properties in category_t
  92. SwiftVersionMaskShift = 8,
  93. SwiftVersionMask = 0xff << SwiftVersionMaskShift // Swift ABI version
  94. };
  95. public:
  96. enum : uint32_t {
  97. SwiftVersion1 = 1,
  98. SwiftVersion1_2 = 2,
  99. SwiftVersion2 = 3,
  100. SwiftVersion3 = 4
  101. };
  102. public:
  103. bool isReplacement() const { return flags & IsReplacement; }
  104. bool supportsGC() const { return flags & SupportsGC; }
  105. bool requiresGC() const { return flags & RequiresGC; }
  106. bool optimizedByDyld() const { return flags & OptimizedByDyld; }
  107. bool hasCategoryClassProperties() const { return flags & HasCategoryClassProperties; }
  108. bool containsSwift() const { return (flags & SwiftVersionMask) != 0; }
  109. uint32_t swiftVersion() const { return (flags & SwiftVersionMask) >> SwiftVersionMaskShift; }
  110. #endif
  111. } objc_image_info;
  112. /*
  113. IsReplacement:
  114. Once used for Fix&Continue in old OS X object files (not final linked images)
  115. Not currently used.
  116. SupportsGC:
  117. App: GC is required. Framework: GC is supported but not required.
  118. RequiresGC:
  119. Framework: GC is required.
  120. OptimizedByDyld:
  121. Assorted metadata precooked in the dyld shared cache.
  122. Never set for images outside the shared cache file itself.
  123. CorrectedSynthesize:
  124. Once used on old iOS to mark images that did not have a particular
  125. miscompile. Not used by the runtime.
  126. IsSimulated:
  127. Image was compiled for a simulator platform. Not used by the runtime.
  128. HasClassProperties:
  129. New ABI: category_t.classProperties fields are present.
  130. Old ABI: Set by some compilers. Not used by the runtime.
  131. */
  132. /* Properties */
  133. // Read or write an object property. Not all object properties use these.
  134. OBJC_EXPORT id _Nullable
  135. objc_getProperty(id _Nullable self, SEL _Nonnull _cmd,
  136. ptrdiff_t offset, BOOL atomic)
  137. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  138. OBJC_EXPORT void
  139. objc_setProperty(id _Nullable self, SEL _Nonnull _cmd, ptrdiff_t offset,
  140. id _Nullable newValue, BOOL atomic, signed char shouldCopy)
  141. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  142. OBJC_EXPORT void
  143. objc_setProperty_atomic(id _Nullable self, SEL _Nonnull _cmd,
  144. id _Nullable newValue, ptrdiff_t offset)
  145. OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0);
  146. OBJC_EXPORT void
  147. objc_setProperty_nonatomic(id _Nullable self, SEL _Nonnull _cmd,
  148. id _Nullable newValue, ptrdiff_t offset)
  149. OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0);
  150. OBJC_EXPORT void
  151. objc_setProperty_atomic_copy(id _Nullable self, SEL _Nonnull _cmd,
  152. id _Nullable newValue, ptrdiff_t offset)
  153. OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0);
  154. OBJC_EXPORT void
  155. objc_setProperty_nonatomic_copy(id _Nullable self, SEL _Nonnull _cmd,
  156. id _Nullable newValue, ptrdiff_t offset)
  157. OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0);
  158. // Read or write a non-object property. Not all uses are C structs,
  159. // and not all C struct properties use this.
  160. OBJC_EXPORT void
  161. objc_copyStruct(void * _Nonnull dest, const void * _Nonnull src,
  162. ptrdiff_t size, BOOL atomic, BOOL hasStrong)
  163. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  164. // Perform a copy of a C++ object using striped locks. Used by non-POD C++ typed atomic properties.
  165. OBJC_EXPORT void
  166. objc_copyCppObjectAtomic(void * _Nonnull dest, const void * _Nonnull src,
  167. void (* _Nonnull copyHelper)
  168. (void * _Nonnull dest, const void * _Nonnull source))
  169. OBJC_AVAILABLE(10.8, 6.0, 9.0, 1.0, 2.0);
  170. /* Classes. */
  171. #if __OBJC2__
  172. OBJC_EXPORT IMP _Nonnull _objc_empty_vtable
  173. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  174. #endif
  175. OBJC_EXPORT struct objc_cache _objc_empty_cache
  176. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  177. /* Messages */
  178. #if __OBJC2__
  179. // objc_msgSendSuper2() takes the current search class, not its superclass.
  180. OBJC_EXPORT id _Nullable
  181. objc_msgSendSuper2(struct objc_super * _Nonnull super, SEL _Nonnull op, ...)
  182. OBJC_AVAILABLE(10.6, 2.0, 9.0, 1.0, 2.0);
  183. OBJC_EXPORT void
  184. objc_msgSendSuper2_stret(struct objc_super * _Nonnull super,
  185. SEL _Nonnull op,...)
  186. OBJC_AVAILABLE(10.6, 2.0, 9.0, 1.0, 2.0)
  187. OBJC_ARM64_UNAVAILABLE;
  188. // objc_msgSend_noarg() may be faster for methods with no additional arguments.
  189. OBJC_EXPORT id _Nullable
  190. objc_msgSend_noarg(id _Nullable self, SEL _Nonnull _cmd)
  191. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
  192. #endif
  193. #if __OBJC2__
  194. // Debug messengers. Messengers used by the compiler have a debug flavor that
  195. // may perform extra sanity checking.
  196. // Old objc_msgSendSuper() does not have a debug version; this is OBJC2 only.
  197. // *_fixup() do not have debug versions; use non-fixup only for debug mode.
  198. OBJC_EXPORT id _Nullable
  199. objc_msgSend_debug(id _Nullable self, SEL _Nonnull op, ...)
  200. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
  201. OBJC_EXPORT id _Nullable
  202. objc_msgSendSuper2_debug(struct objc_super * _Nonnull super,
  203. SEL _Nonnull op, ...)
  204. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
  205. OBJC_EXPORT void
  206. objc_msgSend_stret_debug(id _Nullable self, SEL _Nonnull op, ...)
  207. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0)
  208. OBJC_ARM64_UNAVAILABLE;
  209. OBJC_EXPORT void
  210. objc_msgSendSuper2_stret_debug(struct objc_super * _Nonnull super,
  211. SEL _Nonnull op,...)
  212. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0)
  213. OBJC_ARM64_UNAVAILABLE;
  214. # if defined(__i386__)
  215. OBJC_EXPORT double
  216. objc_msgSend_fpret_debug(id _Nullable self, SEL _Nonnull op, ...)
  217. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
  218. # elif defined(__x86_64__)
  219. OBJC_EXPORT long double
  220. objc_msgSend_fpret_debug(id _Nullable self, SEL _Nonnull op, ...)
  221. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
  222. # if __STDC_VERSION__ >= 199901L
  223. OBJC_EXPORT _Complex long double
  224. objc_msgSend_fp2ret_debug(id _Nullable self, SEL _Nonnull op, ...)
  225. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
  226. # else
  227. OBJC_EXPORT void
  228. objc_msgSend_fp2ret_debug(id _Nullable self, SEL _Nonnull op, ...)
  229. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
  230. # endif
  231. # endif
  232. #endif
  233. #if __OBJC2__
  234. // Lookup messengers.
  235. // These are not callable C functions. Do not call them directly.
  236. // The caller should set the method parameters, call objc_msgLookup(),
  237. // then immediately call the returned IMP.
  238. //
  239. // Generic ABI:
  240. // - Callee-saved registers are preserved.
  241. // - Receiver and selector registers may be modified. These values must
  242. // be passed to the called IMP. Other parameter registers are preserved.
  243. // - Caller-saved non-parameter registers are not preserved. Some of
  244. // these registers are used to pass data from objc_msgLookup() to
  245. // the called IMP and must not be disturbed by the caller.
  246. // - Red zone is not preserved.
  247. // See each architecture's implementation for details.
  248. OBJC_EXPORT void
  249. objc_msgLookup(void)
  250. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  251. OBJC_EXPORT void
  252. objc_msgLookupSuper2(void)
  253. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  254. OBJC_EXPORT void
  255. objc_msgLookup_stret(void)
  256. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0)
  257. OBJC_ARM64_UNAVAILABLE;
  258. OBJC_EXPORT void
  259. objc_msgLookupSuper2_stret(void)
  260. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0)
  261. OBJC_ARM64_UNAVAILABLE;
  262. # if defined(__i386__)
  263. OBJC_EXPORT void
  264. objc_msgLookup_fpret(void)
  265. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  266. # elif defined(__x86_64__)
  267. OBJC_EXPORT void
  268. objc_msgLookup_fpret(void)
  269. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  270. OBJC_EXPORT void
  271. objc_msgLookup_fp2ret(void)
  272. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  273. # endif
  274. #endif
  275. #if (TARGET_OS_OSX || TARGET_OS_SIMULATOR) && defined(__x86_64__)
  276. // objc_msgSend_fixup() was used for vtable-dispatchable call sites.
  277. // The symbols remain exported on macOS for binary compatibility.
  278. // The symbols can probably be removed from iOS simulator but we haven't tried.
  279. OBJC_EXPORT void
  280. objc_msgSend_fixup(void)
  281. __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized");
  282. OBJC_EXPORT void
  283. objc_msgSend_stret_fixup(void)
  284. __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized");
  285. OBJC_EXPORT void
  286. objc_msgSendSuper2_fixup(void)
  287. __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized");
  288. OBJC_EXPORT void
  289. objc_msgSendSuper2_stret_fixup(void)
  290. __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized");
  291. OBJC_EXPORT void
  292. objc_msgSend_fpret_fixup(void)
  293. __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized");
  294. OBJC_EXPORT void
  295. objc_msgSend_fp2ret_fixup(void)
  296. __OSX_DEPRECATED(10.5, 10.8, "fixup dispatch is no longer optimized");
  297. #endif
  298. /* C++-compatible exception handling. */
  299. #if __OBJC2__
  300. // Vtable for C++ exception typeinfo for Objective-C types.
  301. OBJC_EXPORT const void * _Nullable objc_ehtype_vtable[]
  302. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  303. // C++ exception typeinfo for type `id`.
  304. OBJC_EXPORT struct objc_typeinfo OBJC_EHTYPE_id
  305. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  306. // Exception personality function for Objective-C and Objective-C++ code.
  307. struct _Unwind_Exception;
  308. struct _Unwind_Context;
  309. OBJC_EXPORT int
  310. __objc_personality_v0(int version,
  311. int actions,
  312. uint64_t exceptionClass,
  313. struct _Unwind_Exception * _Nonnull exceptionObject,
  314. struct _Unwind_Context * _Nonnull context)
  315. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  316. #endif
  317. /* ARC */
  318. OBJC_EXPORT id _Nullable
  319. objc_retainBlock(id _Nullable)
  320. OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
  321. /* Non-pointer isa */
  322. #if __OBJC2__
  323. // Extract class pointer from an isa field.
  324. #if TARGET_OS_SIMULATOR && !TARGET_OS_IOSMAC
  325. // No simulators use nonpointer isa yet.
  326. #elif __LP64__
  327. # define OBJC_HAVE_NONPOINTER_ISA 1
  328. # define OBJC_HAVE_PACKED_NONPOINTER_ISA 1
  329. // Packed-isa version. This one is used directly by Swift code.
  330. // (Class)(isa & (uintptr_t)&objc_absolute_packed_isa_class_mask) == class ptr
  331. OBJC_EXPORT const struct { char c; } objc_absolute_packed_isa_class_mask
  332. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  333. #elif (__ARM_ARCH_7K__ >= 2 || (__arm64__ && !__LP64__))
  334. # define OBJC_HAVE_NONPOINTER_ISA 1
  335. # define OBJC_HAVE_INDEXED_NONPOINTER_ISA 1
  336. // Indexed-isa version.
  337. // if (isa & (uintptr_t)&objc_absolute_indexed_isa_magic_mask == (uintptr_t)&objc_absolute_indexed_isa_magic_value) {
  338. // uintptr_t index = (isa & (uintptr_t)&objc_absolute_indexed_isa_index_mask) >> (uintptr_t)&objc_absolute_indexed_isa_index_shift;
  339. // cls = objc_indexed_classes[index];
  340. // } else
  341. // cls = (Class)isa;
  342. // }
  343. OBJC_EXPORT const struct { char c; } objc_absolute_indexed_isa_magic_mask
  344. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  345. OBJC_EXPORT const struct { char c; } objc_absolute_indexed_isa_magic_value
  346. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  347. OBJC_EXPORT const struct { char c; } objc_absolute_indexed_isa_index_mask
  348. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  349. OBJC_EXPORT const struct { char c; } objc_absolute_indexed_isa_index_shift
  350. OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
  351. #endif
  352. #endif
  353. /* Object class */
  354. // This symbol might be required for binary compatibility, so we
  355. // declare it here where TAPI will see it.
  356. #if __OBJC__ && __OBJC2__
  357. #pragma clang diagnostic push
  358. #pragma clang diagnostic ignored "-Wobjc-interface-ivars"
  359. #if !defined(OBJC_DECLARE_SYMBOLS)
  360. __OSX_AVAILABLE(10.0)
  361. __IOS_UNAVAILABLE __TVOS_UNAVAILABLE
  362. __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
  363. #endif
  364. OBJC_ROOT_CLASS
  365. @interface Object {
  366. Class isa;
  367. }
  368. @end
  369. #pragma clang diagnostic pop
  370. #endif
  371. // _OBJC_ABI_H
  372. #endif