xref: /llvm-project/lld/test/MachO/lc-linker-option.ll (revision 484c961ccdfaf1d72429560a08b2d44da9a292cd)
1; REQUIRES: x86
2; RUN: rm -rf %t; split-file %s %t
3
4; RUN: llvm-as %t/framework.ll -o %t/framework.o
5; RUN: %lld -lSystem %t/framework.o -o %t/frame
6; RUN: llvm-otool -l %t/frame | FileCheck --check-prefix=FRAME %s \
7; RUN:  --implicit-check-not LC_LOAD_DYLIB
8; FRAME:          cmd LC_LOAD_DYLIB
9; FRAME-NEXT: cmdsize
10; FRAME-NEXT:    name /usr/lib/libSystem.dylib
11; FRAME:          cmd LC_LOAD_DYLIB
12; FRAME-NEXT: cmdsize
13; FRAME-NEXT:    name /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
14
15; RUN: not %lld %t/framework.o -o %t/frame_no_autolink -ignore_auto_link 2>&1 | FileCheck --check-prefix=NO-AUTOLINK %s
16; RUN: not %lld %t/framework.o -o %t/frame_no_autolink --ignore-auto-link-option CoreFoundation 2>&1 | FileCheck --check-prefix=NO-AUTOLINK %s
17; RUN: not %lld %t/framework.o -o %t/frame_no_autolink --ignore-auto-link-option=CoreFoundation 2>&1 | FileCheck --check-prefix=NO-AUTOLINK %s
18; NO-AUTOLINK: error: undefined symbol: __CFBigNumGetInt128
19
20; RUN: llvm-as %t/l.ll -o %t/l.o
21;; The dynamic call to _CFBigNumGetInt128 uses dyld_stub_binder,
22;; which needs -lSystem from LC_LINKER_OPTION to get resolved.
23;; The reference to __cxa_allocate_exception will require -lc++ from
24;; LC_LINKER_OPTION to get resolved.
25; RUN: %no-lsystem-lld %t/l.o -o %t/l -framework CoreFoundation
26; RUN: llvm-otool -l %t/l | FileCheck --check-prefix=LIB %s \
27; RUN:  --implicit-check-not LC_LOAD_DYLIB
28; LIB:          cmd LC_LOAD_DYLIB
29; LIB-NEXT: cmdsize
30; LIB-NEXT:    name /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
31; LIB:          cmd LC_LOAD_DYLIB
32; LIB-NEXT: cmdsize
33; LIB-NEXT:    name /usr/lib/libSystem.dylib
34; LIB:          cmd LC_LOAD_DYLIB
35; LIB-NEXT: cmdsize
36; LIB-NEXT:    name /usr/lib/libc++abi.dylib
37
38;; Check that we don't create duplicate LC_LOAD_DYLIBs.
39; RUN: %no-lsystem-lld -lSystem %t/l.o -o %t/l -framework CoreFoundation
40; RUN: llvm-otool -l %t/l | FileCheck --check-prefix=LIB2 %s \
41; RUN:  --implicit-check-not LC_LOAD_DYLIB
42; LIB2:          cmd LC_LOAD_DYLIB
43; LIB2-NEXT: cmdsize
44; LIB2-NEXT:    name /usr/lib/libSystem.dylib
45; LIB2:          cmd LC_LOAD_DYLIB
46; LIB2-NEXT: cmdsize
47; LIB2-NEXT:    name /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
48; LIB2:          cmd LC_LOAD_DYLIB
49; LIB2-NEXT: cmdsize
50; LIB2-NEXT:    name /usr/lib/libc++abi.dylib
51
52; RUN: llvm-as %t/invalid.ll -o %t/invalid.o
53; RUN: not %lld %t/invalid.o -o /dev/null 2>&1 | FileCheck --check-prefix=INVALID %s
54; INVALID: error: -why_load is not allowed in LC_LINKER_OPTION
55
56;; This is a regression test for a dangling string reference issue that occurred
57;; when loading an archive-based framework via LC_LINKER_OPTION (see
58;; D111706). Prior to the fix, this would trigger a heap-use-after-free when run
59;; under ASAN.
60; RUN: llc %t/foo.ll -o %t/foo.o -filetype=obj
61; RUN: mkdir -p %t/Foo.framework
62;; In a proper framework, this is technically supposed to be a symlink to the
63;; actual archive at Foo.framework/Versions/Current, but we skip that here so
64;; that this test can run on Windows.
65; RUN: llvm-ar rcs %t/Foo.framework/Foo %t/foo.o
66; RUN: llc %t/load-framework-foo.ll -o %t/load-framework-foo.o -filetype=obj
67; RUN: llc %t/load-framework-undefined-symbol.ll -o %t/load-framework-undefined-symbol.o -filetype=obj
68; RUN: llc %t/load-missing.ll -o %t/load-missing.o -filetype=obj
69; RUN: llc %t/main.ll -o %t/main.o -filetype=obj
70; RUN: %lld %t/load-framework-foo.o %t/main.o -o %t/main -F%t
71; RUN: llvm-objdump --macho --syms %t/main | FileCheck %s --check-prefix=SYMS
72
73;; Make sure -all_load and -ObjC have no effect on libraries loaded via
74;; LC_LINKER_OPTION flags.
75; RUN: llc %t/load-library-foo.ll -o %t/load-library-foo.o -filetype=obj
76; RUN: llvm-ar rcs %t/libfoo.a %t/foo.o
77; RUN: %lld -all_load -ObjC %t/load-framework-foo.o %t/load-library-foo.o \
78; RUN:   %t/main.o -o %t/main -F%t -L%t
79; RUN: llvm-objdump --macho --syms %t/main | FileCheck %s --check-prefix=SYMS
80
81;; Note that _OBJC_CLASS_$_TestClass is *not* included here.
82; SYMS:       SYMBOL TABLE:
83; SYMS-NEXT:  g     F __TEXT,__text _main
84; SYMS-NEXT:  g     F __TEXT,__text __mh_execute_header
85; SYMS-NEXT:  *UND* dyld_stub_binder
86; SYMS-EMPTY:
87
88;; Make sure -all_load has effect when libraries are loaded via LC_LINKER_OPTION flags and explicitly passed as well
89; RUN: %lld -all_load %t/load-framework-foo.o %t/load-library-foo.o %t/main.o -o %t/main -F%t -L%t -lfoo
90; RUN: llvm-objdump --macho --syms %t/main | FileCheck %s --check-prefix=SYMS-ALL-LOAD
91
92;; Note that _OBJC_CLASS_$_TestClass is *included* here.
93; SYMS-ALL-LOAD:       SYMBOL TABLE:
94; SYMS-ALL-LOAD-NEXT:  g     F __TEXT,__text _main
95; SYMS-ALL-LOAD-NEXT:  g     O __DATA,__objc_data _OBJC_CLASS_$_TestClass
96; SYMS-ALL-LOAD-NEXT:  g     F __TEXT,__text __mh_execute_header
97; SYMS-ALL-LOAD-NEXT:  *UND* dyld_stub_binder
98; SYMS-ALL-LOAD-EMPTY:
99
100;; Make sure -force_load has effect when libraries are loaded via LC_LINKER_OPTION flags and explicitly passed as well
101; RUN: %lld %t/load-library-foo.o %t/main.o -o %t/main -F%t -L%t -force_load %t/libfoo.a
102; RUN: llvm-objdump --macho --syms %t/main | FileCheck %s --check-prefix=SYMS-FORCE-LOAD
103
104;; Note that _OBJC_CLASS_$_TestClass is *included* here.
105; SYMS-FORCE-LOAD:       SYMBOL TABLE:
106; SYMS-FORCE-LOAD-NEXT:  g     F __TEXT,__text _main
107; SYMS-FORCE-LOAD-NEXT:  g     O __DATA,__objc_data _OBJC_CLASS_$_TestClass
108; SYMS-FORCE-LOAD-NEXT:  g     F __TEXT,__text __mh_execute_header
109; SYMS-FORCE-LOAD-NEXT:  *UND* dyld_stub_binder
110; SYMS-FORCE-LOAD-EMPTY:
111
112;; Make sure -ObjC has effect when frameworks are loaded via LC_LINKER_OPTION flags and explicitly passed as well
113; RUN: %lld -ObjC %t/load-framework-foo.o %t/load-library-foo.o %t/main.o -o %t/main -F%t -L%t -framework Foo
114; RUN: llvm-objdump --macho --syms %t/main | FileCheck %s --check-prefix=SYMS-OBJC-LOAD
115
116;; Note that _OBJC_CLASS_$_TestClass is *included* here.
117; SYMS-OBJC-LOAD:       SYMBOL TABLE:
118; SYMS-OBJC-LOAD-NEXT:  g     F __TEXT,__text _main
119; SYMS-OBJC-LOAD-NEXT:  g     O __DATA,__objc_data _OBJC_CLASS_$_TestClass
120; SYMS-OBJC-LOAD-NEXT:  g     F __TEXT,__text __mh_execute_header
121; SYMS-OBJC-LOAD-NEXT:  *UND* dyld_stub_binder
122; SYMS-OBJC-LOAD-EMPTY:
123
124;; Make sure that frameworks containing object files or bitcode instead of
125;; dylibs or archives do not cause duplicate symbol errors
126; RUN: mkdir -p %t/Foo.framework
127; RUN: llc --filetype=obj %t/foo.ll -o %t/Foo.framework/Foo
128; RUN: llc --filetype=obj %t/load-framework-twice.ll -o %t/main
129;; Order of the object with the LC_LINKER_OPTION vs -framework arg is important.
130; RUN: %lld %t/main -F %t -framework Foo -framework Foo -o /dev/null
131; RUN: %lld -F %t -framework Foo -framework Foo %t/main -o /dev/null
132
133; RUN: llvm-as %t/foo.ll -o %t/Foo.framework/Foo
134; RUN: llvm-as %t/load-framework-twice.ll -o %t/main
135;; Order of the object with the LC_LINKER_OPTION vs -framework arg is important.
136; RUN: %lld %t/main -F %t -framework Foo -framework Foo -o /dev/null
137; RUN: %lld -F %t -framework Foo -framework Foo %t/main -o /dev/null
138
139;; Checks that "framework not found" errors from LC_LINKER_OPTIONS are not
140;; emitted unless the link fails or --strict-auto-link is passed.
141; RUN: %lld -ObjC %t/load-framework-foo.o %t/main.o -o %t/main-no-foo.out
142; RUN: llvm-objdump --macho --syms %t/main-no-foo.out | FileCheck %s --check-prefix=SYMS-NO-FOO
143; RUN: not %lld --strict-auto-link -ObjC %t/load-missing.o %t/main.o -o %t/main-no-foo.out 2>&1 \
144; RUN:   | FileCheck %s --check-prefix=MISSING-AUTO-LINK
145; RUN: %no-fatal-warnings-lld --strict-auto-link -ObjC %t/load-missing.o %t/main.o -o %t/main-no-foo.out 2>&1 \
146; RUN:   | FileCheck %s --check-prefix=MISSING-AUTO-LINK
147; RUN: not %lld -ObjC %t/load-framework-undefined-symbol.o %t/load-missing.o %t/main.o -o %t/main-no-foo.out 2>&1 \
148; RUN:   | FileCheck %s --check-prefixes=UNDEFINED-SYMBOL,MISSING-AUTO-LINK
149
150;; Verify that nothing from the framework is included.
151; SYMS-NO-FOO:       SYMBOL TABLE:
152; SYMS-NO-FOO-NEXT:  g     F __TEXT,__text _main
153; SYMS-NO-FOO-NOT:   g     O __DATA,__objc_data _OBJC_CLASS_$_TestClass
154
155; UNDEFINED-SYMBOL: undefined symbol: __SomeUndefinedSymbol
156; MISSING-AUTO-LINK: {{.+}}: auto-linked framework not found for -framework Foo
157; MISSING-AUTO-LINK: {{.+}}: auto-linked library not found for -lBar
158
159;--- framework.ll
160target triple = "x86_64-apple-macosx10.15.0"
161target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
162
163!0 = !{!"-framework", !"CoreFoundation"}
164!llvm.linker.options = !{!0}
165
166declare void @_CFBigNumGetInt128(...)
167
168define void @main() {
169  call void @_CFBigNumGetInt128()
170  ret void
171}
172
173;--- l.ll
174target triple = "x86_64-apple-macosx10.15.0"
175target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
176
177!0 = !{!"-lSystem"}
178!1 = !{!"-lc++"}
179!llvm.linker.options = !{!0, !0, !1}
180
181declare void @_CFBigNumGetInt128(...)
182declare ptr @__cxa_allocate_exception(i64)
183
184define void @main() {
185  call void @_CFBigNumGetInt128()
186  call ptr @__cxa_allocate_exception(i64 4)
187  ret void
188}
189
190;--- invalid.ll
191
192target triple = "x86_64-apple-macosx10.15.0"
193target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
194
195!0 = !{!"-why_load"}
196!llvm.linker.options = !{!0}
197
198define void @main() {
199  ret void
200}
201
202;--- load-framework-foo.ll
203target triple = "x86_64-apple-macosx10.15.0"
204target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
205
206!0 = !{!"-framework", !"Foo"}
207!llvm.linker.options = !{!0}
208
209;--- load-missing.ll
210target triple = "x86_64-apple-macosx10.15.0"
211target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
212
213!0 = !{!"-framework", !"Foo"}
214!1 = !{!"-lBar"}
215!llvm.linker.options = !{!0, !1}
216
217;--- load-framework-undefined-symbol.ll
218target triple = "x86_64-apple-macosx10.15.0"
219target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
220
221declare void @_SomeUndefinedSymbol(...)
222define void @foo() {
223  call void @_SomeUndefinedSymbol()
224  ret void
225}
226
227;--- load-framework-twice.ll
228target triple = "x86_64-apple-macosx10.15.0"
229target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
230
231!0 = !{!"-framework", !"Foo"}
232!llvm.linker.options = !{!0, !0}
233
234define void @main() {
235  ret void
236}
237
238;--- load-library-foo.ll
239target triple = "x86_64-apple-macosx10.15.0"
240target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
241
242!0 = !{!"-lfoo"}
243!llvm.linker.options = !{!0}
244
245;--- main.ll
246target triple = "x86_64-apple-macosx10.15.0"
247target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
248
249define void @main() {
250  ret void
251}
252
253!0 = !{!"-framework", !"Foo"}
254!llvm.linker.options = !{!0}
255
256;--- foo.ll
257target triple = "x86_64-apple-macosx10.15.0"
258target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
259
260%struct._class_t = type {}
261@"OBJC_CLASS_$_TestClass" = global %struct._class_t {}, section "__DATA, __objc_data", align 8
262