xref: /llvm-project-15.0.7/lld/test/MachO/objc.s (revision d64bad8f)
1# REQUIRES: x86, arm
2# RUN: rm -rf %t; split-file %s %t
3
4# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/has-objc-symbol.s -o %t/has-objc-symbol.o
5# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/has-objc-category.s -o %t/has-objc-category.o
6# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/has-objc-symbol-and-category.s -o %t/has-objc-symbol-and-category.o
7# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/has-swift.s -o %t/has-swift.o
8# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/has-swift-proto.s -o %t/has-swift-proto.o
9# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/no-objc.s -o %t/no-objc.o
10## Make sure we don't mis-parse a 32-bit file as 64-bit
11# RUN: llvm-mc -filetype=obj -triple=armv7-apple-watchos %t/no-objc.s -o %t/wrong-arch.o
12# RUN: llvm-ar rcs %t/libHasSomeObjC.a %t/no-objc.o %t/has-objc-symbol.o %t/has-objc-category.o %t/has-swift.o %t/has-swift-proto.o %t/wrong-arch.o
13# RUN: llvm-ar rcs %t/libHasSomeObjC2.a %t/no-objc.o %t/has-objc-symbol-and-category.o %t/has-swift.o %t/has-swift-proto.o %t/wrong-arch.o
14
15# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
16
17# RUN: %lld -lSystem %t/test.o -o %t/test -L%t -lHasSomeObjC -ObjC
18# RUN: llvm-objdump --section-headers --syms %t/test | FileCheck %s --check-prefix=OBJC
19
20# RUN: %lld -lSystem %t/test.o -o %t/test -L%t -lHasSomeObjC2 -ObjC
21# RUN: llvm-objdump --section-headers --syms %t/test | FileCheck %s --check-prefix=OBJC
22
23# RUN: %lld -lSystem %t/test.o -o %t/test --start-lib %t/no-objc.o %t/has-objc-symbol.o %t/has-objc-category.o %t/has-swift.o %t/has-swift-proto.o %t/wrong-arch.o --end-lib -ObjC
24# RUN: llvm-objdump --section-headers --syms %t/test | FileCheck %s --check-prefix=OBJC
25
26# OBJC:       Sections:
27# OBJC-NEXT:  Idx Name            Size   VMA  Type
28# OBJC-NEXT:    0 __text          {{.*}}      TEXT
29# OBJC-NEXT:    1 __swift         {{.*}}      DATA
30# OBJC-NEXT:    2 __swift5_fieldmd{{.*}}      DATA
31# OBJC-NEXT:    3 __objc_catlist  {{.*}}      DATA
32# OBJC-NEXT:    4 has_objc_symbol {{.*}}      DATA
33# OBJC-EMPTY:
34# OBJC-NEXT:  SYMBOL TABLE:
35# OBJC-DAG:   g     F __TEXT,__text _main
36# OBJC-DAG:   g     F __TEXT,__text _OBJC_CLASS_$_MyObject
37# OBJC-DAG:   g     O __TEXT,__swift5_fieldmd $s7somelib4Blah_pMF
38
39# RUN: %lld -lSystem %t/test.o -o %t/test -L%t -lHasSomeObjC
40# RUN: llvm-objdump --section-headers --syms %t/test | FileCheck %s --check-prefix=NO-OBJC
41
42# NO-OBJC:       Sections:
43# NO-OBJC-NEXT:  Idx Name           Size   VMA  Type
44# NO-OBJC-NEXT:    0 __text         {{.*}}      TEXT
45# NO-OBJC-EMPTY:
46# NO-OBJC-NEXT:  SYMBOL TABLE:
47# NO-OBJC-NEXT:  g     F __TEXT,__text _main
48# NO-OBJC-NEXT:  g     F __TEXT,__text __mh_execute_header
49# NO-OBJC-NEXT:          *UND* dyld_stub_binder
50# NO-OBJC-EMPTY:
51
52# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/refs-dup.s -o %t/refs-dup.o
53# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/refs-objc.s -o %t/refs-objc.o
54
55## Check that -ObjC causes has-objc-symbol.o to be loaded first, prior to symbol
56## resolution. This matches ld64's behavior.
57# RUN: %lld -dylib %t/refs-dup.o %t/refs-objc.o -o %t/refs-dup -L%t -lHasSomeObjC -ObjC
58# RUN: llvm-objdump --macho --syms %t/refs-dup | FileCheck %s --check-prefix=DUP-FROM-OBJC
59# DUP-FROM-OBJC: g     O __DATA,has_objc_symbol _has_dup
60
61## Without -ObjC, no-objc.o gets loaded first during symbol resolution, causing
62## a duplicate symbol error.
63# RUN: not %lld -dylib %t/refs-dup.o %t/refs-objc.o -o %t/refs-dup -L%t \
64# RUN:   -lHasSomeObjC 2>&1 | FileCheck %s --check-prefix=DUP-ERROR
65# DUP-ERROR: error: duplicate symbol: _has_dup
66
67## TODO: Load has-objc-symbol.o prior to symbol resolution to match the archive behavior.
68# RUN: not %lld -dylib %t/refs-dup.o %t/refs-objc.o -o %t/refs-dup --start-lib %t/no-objc.o \
69# RUN:   %t/has-objc-symbol.o %t/has-objc-category.o %t/has-swift.o %t/wrong-arch.o --end-lib \
70# RUN:   -ObjC  --check-prefix=DUP-FROM-OBJC
71
72#--- has-objc-symbol.s
73.globl _OBJC_CLASS_$_MyObject, _has_dup
74_OBJC_CLASS_$_MyObject:
75
76.section __DATA,has_objc_symbol
77_has_dup:
78
79#--- has-objc-category.s
80.section __DATA,__objc_catlist
81.quad 0x1234
82
83#--- has-objc-symbol-and-category.s
84## Make sure we load this archive member exactly once (i.e. no duplicate symbol
85## error).
86.globl _OBJC_CLASS_$_MyObject, _has_dup
87_OBJC_CLASS_$_MyObject:
88
89.section __DATA,has_objc_symbol
90_has_dup:
91
92.section __DATA,__objc_catlist
93.quad 0x1234
94
95#--- has-swift.s
96.section __TEXT,__swift
97.quad 0x1234
98
99#--- has-swift-proto.s
100.section __TEXT,__swift5_fieldmd
101.globl $s7somelib4Blah_pMF
102$s7somelib4Blah_pMF:
103
104#--- no-objc.s
105## This archive member should not be pulled in by -ObjC since it does not
106## contain any ObjC-related data.
107.globl _has_dup
108.section __DATA,foo
109.section __DATA,bar
110.section __DATA,no_objc
111_has_dup:
112
113#--- test.s
114.globl _main
115_main:
116  ret
117
118#--- refs-dup.s
119.data
120.quad _has_dup
121
122#--- refs-objc.s
123.data
124.quad _OBJC_CLASS_$_MyObject
125