swift-class-def.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include <sys/cdefs.h>
  2. #if __LP64__
  3. # define PTR " .quad "
  4. # define PTRSIZE "8"
  5. # define LOGPTRSIZE "3"
  6. # define ONLY_LP64(x) x
  7. #else
  8. # define PTR " .long "
  9. # define PTRSIZE "4"
  10. # define LOGPTRSIZE "2"
  11. # define ONLY_LP64(x)
  12. #endif
  13. #if __has_feature(ptrauth_calls)
  14. # define SIGNED_METHOD_LIST_IMP "@AUTH(ia,0,addr) "
  15. # define SIGNED_STUB_INITIALIZER "@AUTH(ia,0xc671,addr) "
  16. #else
  17. # define SIGNED_METHOD_LIST_IMP
  18. # define SIGNED_STUB_INITIALIZER
  19. #endif
  20. #define str(x) #x
  21. #define str2(x) str(x)
  22. // Swift metadata initializers. Define these in the test.
  23. EXTERN_C Class initSuper(Class cls, void *arg);
  24. EXTERN_C Class initSub(Class cls, void *arg);
  25. @interface SwiftSuper : NSObject @end
  26. @interface SwiftSub : SwiftSuper @end
  27. __BEGIN_DECLS
  28. // not id to avoid ARC operations because the class doesn't implement RR methods
  29. void* nop(void* self) { return self; }
  30. __END_DECLS
  31. #define SWIFT_CLASS(name, superclass, swiftInit) \
  32. asm( \
  33. ".globl _OBJC_CLASS_$_" #name "\n" \
  34. ".section __DATA,__objc_data \n" \
  35. ".align 3 \n" \
  36. "_OBJC_CLASS_$_" #name ": \n" \
  37. PTR "_OBJC_METACLASS_$_" #name "\n" \
  38. PTR "_OBJC_CLASS_$_" #superclass "\n" \
  39. PTR "__objc_empty_cache \n" \
  40. PTR "0 \n" \
  41. PTR "L_" #name "_ro + 2 \n" \
  42. /* Swift class fields. */ \
  43. ".long 0 \n" /* flags */ \
  44. ".long 0 \n" /* instanceAddressOffset */ \
  45. ".long 16 \n" /* instanceSize */ \
  46. ".short 15 \n" /* instanceAlignMask */ \
  47. ".short 0 \n" /* reserved */ \
  48. ".long 256 \n" /* classSize */ \
  49. ".long 0 \n" /* classAddressOffset */ \
  50. PTR "0 \n" /* description */ \
  51. /* pad to OBJC_MAX_CLASS_SIZE */ \
  52. PTR "0 \n" \
  53. PTR "0 \n" \
  54. PTR "0 \n" \
  55. PTR "0 \n" \
  56. PTR "0 \n" \
  57. PTR "0 \n" \
  58. PTR "0 \n" \
  59. PTR "0 \n" \
  60. PTR "0 \n" \
  61. PTR "0 \n" \
  62. PTR "0 \n" \
  63. PTR "0 \n" \
  64. PTR "0 \n" \
  65. PTR "0 \n" \
  66. PTR "0 \n" \
  67. PTR "0 \n" \
  68. PTR "0 \n" \
  69. PTR "0 \n" \
  70. PTR "0 \n" \
  71. PTR "0 \n" \
  72. PTR "0 \n" \
  73. PTR "0 \n" \
  74. PTR "0 \n" \
  75. PTR "0 \n" \
  76. \
  77. "_OBJC_METACLASS_$_" #name ": \n" \
  78. PTR "_OBJC_METACLASS_$_" #superclass "\n" \
  79. PTR "_OBJC_METACLASS_$_" #superclass "\n" \
  80. PTR "__objc_empty_cache \n" \
  81. PTR "0 \n" \
  82. PTR "L_" #name "_meta_ro \n" \
  83. /* pad to OBJC_MAX_CLASS_SIZE */ \
  84. PTR "0 \n" \
  85. PTR "0 \n" \
  86. PTR "0 \n" \
  87. PTR "0 \n" \
  88. PTR "0 \n" \
  89. PTR "0 \n" \
  90. PTR "0 \n" \
  91. PTR "0 \n" \
  92. PTR "0 \n" \
  93. PTR "0 \n" \
  94. PTR "0 \n" \
  95. PTR "0 \n" \
  96. PTR "0 \n" \
  97. PTR "0 \n" \
  98. PTR "0 \n" \
  99. PTR "0 \n" \
  100. PTR "0 \n" \
  101. PTR "0 \n" \
  102. PTR "0 \n" \
  103. PTR "0 \n" \
  104. PTR "0 \n" \
  105. PTR "0 \n" \
  106. PTR "0 \n" \
  107. PTR "0 \n" \
  108. PTR "0 \n" \
  109. PTR "0 \n" \
  110. PTR "0 \n" \
  111. \
  112. "L_" #name "_ro: \n" \
  113. ".long (1<<6) \n" \
  114. ".long 0 \n" \
  115. ".long " PTRSIZE " \n" \
  116. ONLY_LP64(".long 0 \n") \
  117. PTR "0 \n" \
  118. PTR "L_" #name "_name \n" \
  119. PTR "L_" #name "_methods \n" \
  120. PTR "0 \n" \
  121. PTR "L_" #name "_ivars \n" \
  122. PTR "0 \n" \
  123. PTR "0 \n" \
  124. PTR "_" #swiftInit SIGNED_METHOD_LIST_IMP "\n" \
  125. \
  126. "L_" #name "_meta_ro: \n" \
  127. ".long 1 \n" \
  128. ".long 40 \n" \
  129. ".long 40 \n" \
  130. ONLY_LP64(".long 0 \n") \
  131. PTR "0 \n" \
  132. PTR "L_" #name "_name \n" \
  133. PTR "L_" #name "_meta_methods \n" \
  134. PTR "0 \n" \
  135. PTR "0 \n" \
  136. PTR "0 \n" \
  137. PTR "0 \n" \
  138. \
  139. "L_" #name "_methods: \n" \
  140. "L_" #name "_meta_methods: \n" \
  141. ".long 3*" PTRSIZE "\n" \
  142. ".long 1 \n" \
  143. PTR "L_" #name "_self \n" \
  144. PTR "L_" #name "_self \n" \
  145. PTR "_nop" SIGNED_METHOD_LIST_IMP "\n" \
  146. \
  147. "L_" #name "_ivars: \n" \
  148. ".long 4*" PTRSIZE " \n" \
  149. ".long 1 \n" \
  150. PTR "L_" #name "_ivar_offset \n" \
  151. PTR "L_" #name "_ivar_name \n" \
  152. PTR "L_" #name "_ivar_type \n" \
  153. ".long " LOGPTRSIZE "\n" \
  154. ".long " PTRSIZE "\n" \
  155. \
  156. "L_" #name "_ivar_offset: \n" \
  157. ".long 0 \n" \
  158. \
  159. ".cstring \n" \
  160. "L_" #name "_name: .ascii \"" #name "\\0\" \n" \
  161. "L_" #name "_self: .ascii \"self\\0\" \n" \
  162. "L_" #name "_ivar_name: " \
  163. " .ascii \"" #name "_ivar\\0\" \n" \
  164. "L_" #name "_ivar_type: .ascii \"c\\0\" \n" \
  165. \
  166. \
  167. ".text \n" \
  168. ); \
  169. extern char OBJC_CLASS_$_ ## name; \
  170. Class Raw ## name = (Class)&OBJC_CLASS_$_ ## name
  171. #define SWIFT_STUB_CLASSREF(name) \
  172. extern char OBJC_CLASS_$_ ## name; \
  173. static Class name ## Classref = (Class)(&OBJC_CLASS_$_ ## name + 1)
  174. #define SWIFT_STUB_CLASS(name, initializer) \
  175. asm( \
  176. ".globl _OBJC_CLASS_$_" #name "\n" \
  177. ".section __DATA,__objc_data \n" \
  178. ".align 3 \n" \
  179. "_dummy" #name ": \n" \
  180. PTR "0 \n" \
  181. ".alt_entry _OBJC_CLASS_$_" #name "\n" \
  182. "_OBJC_CLASS_$_" #name ": \n" \
  183. PTR "1 \n" \
  184. PTR "_" #initializer SIGNED_STUB_INITIALIZER "\n" \
  185. ".text" \
  186. ); \
  187. extern char OBJC_CLASS_$_ ## name; \
  188. Class Raw ## name = (Class)&OBJC_CLASS_$_ ## name; \
  189. SWIFT_STUB_CLASSREF(name)
  190. void fn(void) { }