3 # RUN: rm -rf %t; split-file %s %t
5 # RUN: llvm-mc %t/bar.s -triple=x86_64-apple-macos -filetype=obj -o %t/bar.o
6 # RUN: %lld -dylib %t/bar.o -o %t/bar.dylib
7 # RUN: %lld -dylib %t/bar.o -o %t/libbar.dylib
8 # RUN: %lld -dylib -mark_dead_strippable_dylib %t/bar.o -o %t/bar-strip.dylib
10 # RUN: llvm-mc %t/foo.s -triple=x86_64-apple-macos -filetype=obj -o %t/foo.o
11 # RUN: %lld -dylib %t/foo.o -o %t/foo_with_bar.dylib %t/bar.dylib -sub_library bar
12 # RUN: %lld -dylib %t/foo.o -o %t/foo.dylib
14 # RUN: llvm-mc %t/weak-foo.s -triple=x86_64-apple-macos -filetype=obj -o %t/weak-foo.o
15 # RUN: %lld -dylib %t/weak-foo.o -o %t/weak-foo.dylib
17 # RUN: llvm-mc %t/main.s -triple=x86_64-apple-macos -filetype=obj -o %t/main.o
19 ## foo_with_bar.dylib's reexport should be dropped since it's linked implicitly.
20 # RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib
21 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOBAR %s
22 # NOBAR-NOT: bar.dylib
23 # NOBAR: /usr/lib/libSystem.dylib
24 # NOBAR-NOT: bar.dylib
25 # NOBAR: foo_with_bar.dylib
26 # NOBAR-NOT: bar.dylib
28 ## If bar.dylib is linked explicitly, it should not be dropped.
29 # RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib %t/bar.dylib
30 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=BAR %s
31 # BAR: /usr/lib/libSystem.dylib
32 # BAR: foo_with_bar.dylib
35 ## ...except if -dead-strip_dylibs is passed...
36 # RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib %t/bar.dylib \
37 # RUN: -dead_strip_dylibs
38 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOBAR %s
40 ## ...or bar is explicitly marked dead-strippable.
41 # RUN: %lld -lSystem %t/main.o -o %t/main %t/foo.dylib %t/bar-strip.dylib
42 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOBARSTRIP %s
43 # NOBARSTRIP-NOT: bar-strip.dylib
44 # NOBARSTRIP: /usr/lib/libSystem.dylib
45 # NOBARSTRIP-NOT: bar-strip.dylib
46 # NOBARSTRIP: foo.dylib
47 # NOBARSTRIP-NOT: bar-strip.dylib
49 ## Even libraries explicitly reexported with -reexport_library are stripped
50 ## if they are not referenced.
51 # RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib \
52 # RUN: -reexport_library %t/bar.dylib -dead_strip_dylibs
53 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOBAR %s
54 # RUN: llvm-otool -l %t/main | FileCheck --check-prefix=NOREEXPORT %s
55 # NOREEXPORT-NOT: LC_REEXPORT_DYLIB
57 ## But -needed_library and -needed-l win over -dead_strip_dylibs again.
58 # RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib \
59 # RUN: -needed_library %t/bar.dylib -dead_strip_dylibs
60 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=BAR %s
61 # RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib \
62 # RUN: -L%t -needed-lbar -dead_strip_dylibs
63 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=BAR %s
65 ## LC_LINKER_OPTION does not count as an explicit reference.
66 # RUN: llvm-mc %t/linkopt_bar.s -triple=x86_64-apple-macos -filetype=obj -o %t/linkopt_bar.o
67 # RUN: %lld -lSystem %t/main.o %t/linkopt_bar.o -o %t/main -L %t %t/foo.dylib
68 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOLIBBAR %s
69 # NOLIBBAR-NOT: libbar.dylib
70 # NOLIBBAR: /usr/lib/libSystem.dylib
71 # NOLIBBAR-NOT: libbar.dylib
73 # NOLIBBAR-NOT: libbar.dylib
75 ## ...but with an additional explicit reference it's not stripped again.
76 # RUN: %lld -lSystem %t/main.o %t/linkopt_bar.o -o %t/main -L %t %t/foo.dylib -lbar
77 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=LIBBAR %s
78 # RUN: %lld -lSystem %t/main.o -o %t/main -L %t %t/foo.dylib -lbar %t/linkopt_bar.o
79 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=LIBBAR %s
80 # LIBBAR-DAG: /usr/lib/libSystem.dylib
81 # LIBBAR-DAG: libbar.dylib
82 # LIBBAR-DAG: foo.dylib
84 ## Test that a DylibSymbol being replaced by a DefinedSymbol marks the
85 ## dylib as unreferenced.
86 ## (Note: Since there's no dynamic linking in this example, libSystem is
87 ## stripped too. Since libSystem.dylib is needed to run an executable for
88 ## LC_MAIN, the output would crash when run. This matches ld64's behavior (!)
89 ## In practice, every executable uses dynamic linking, which uses
90 ## dyld_stub_binder, which keeps libSystem alive.)
91 ## Test all permutations of (Undefined, Defined, DylibSymbol).
92 # RUN: %lld -lSystem -dead_strip_dylibs %t/main.o %t/foo.o %t/foo.dylib -o %t/main
93 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s
94 # RUN: %lld -lSystem -dead_strip_dylibs %t/main.o %t/foo.dylib %t/foo.dylib %t/foo.o -o %t/main
95 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s
97 # RUN: %lld -lSystem -dead_strip_dylibs %t/foo.o %t/main.o %t/foo.dylib -o %t/main
98 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s
99 # RUN: %lld -lSystem -dead_strip_dylibs %t/foo.dylib %t/foo.dylib %t/main.o %t/foo.o -o %t/main
100 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s
102 # RUN: %lld -lSystem -dead_strip_dylibs %t/foo.o %t/foo.dylib %t/main.o -o %t/main
103 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s
104 # RUN: %lld -lSystem -dead_strip_dylibs %t/foo.dylib %t/foo.dylib %t/foo.o %t/main.o -o %t/main
105 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s
106 # NOFOO-NOT: foo.dylib
108 ## When linking a weak and a strong symbol from two dylibs, we should keep the
110 # RUN: %lld -lSystem -dead_strip_dylibs %t/main.o %t/foo.dylib %t/weak-foo.dylib -o %t/main
111 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOWEAK %s
112 # RUN: %lld -lSystem -dead_strip_dylibs %t/main.o %t/weak-foo.dylib %t/foo.dylib -o %t/main
113 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOWEAK %s
114 # RUN: %lld -lSystem -dead_strip_dylibs %t/foo.dylib %t/weak-foo.dylib %t/main.o -o %t/main
115 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOWEAK %s
116 # RUN: %lld -lSystem -dead_strip_dylibs %t/weak-foo.dylib %t/foo.dylib %t/main.o -o %t/main
117 # RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOWEAK %s
118 # NOWEAK-NOT: weak-foo.dylib
120 # NOWEAK-NOT: weak-foo.dylib
139 .linker_option "-lbar"
143 .weak_definition _foo