llvm-project
145 строк · 6.0 Кб
1# REQUIRES: x86
2
3# RUN: rm -rf %t; split-file %s %t
4
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
9
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
13
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
16
17# RUN: llvm-mc %t/main.s -triple=x86_64-apple-macos -filetype=obj -o %t/main.o
18
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
27
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
33# BAR: bar.dylib
34
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
39
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
48
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
56
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
64
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
72# NOLIBBAR: foo.dylib
73# NOLIBBAR-NOT: libbar.dylib
74
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
83
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
96
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
101
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
107
108## When linking a weak and a strong symbol from two dylibs, we should keep the
109## strong one.
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
119# NOWEAK: /foo.dylib
120# NOWEAK-NOT: weak-foo.dylib
121
122#--- main.s
123.globl _foo, _main
124_main:
125callq _foo
126retq
127
128#--- foo.s
129.globl _foo
130_foo:
131retq
132
133#--- bar.s
134.globl _bar
135_bar:
136retq
137
138#--- linkopt_bar.s
139.linker_option "-lbar"
140
141#--- weak-foo.s
142.globl _foo
143.weak_definition _foo
144_foo:
145retq
146