File: dead-strip-dylibs.s

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (145 lines) | stat: -rw-r--r-- 6,189 bytes parent folder | download | duplicates (21)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# REQUIRES: x86

# RUN: rm -rf %t; split-file %s %t

# RUN: llvm-mc %t/bar.s -triple=x86_64-apple-macos -filetype=obj -o %t/bar.o
# RUN: %lld -dylib %t/bar.o -o %t/bar.dylib
# RUN: %lld -dylib %t/bar.o -o %t/libbar.dylib
# RUN: %lld -dylib -mark_dead_strippable_dylib %t/bar.o -o %t/bar-strip.dylib

# RUN: llvm-mc %t/foo.s -triple=x86_64-apple-macos -filetype=obj -o %t/foo.o
# RUN: %lld -dylib %t/foo.o -o %t/foo_with_bar.dylib %t/bar.dylib -sub_library bar
# RUN: %lld -dylib %t/foo.o -o %t/foo.dylib

# RUN: llvm-mc %t/weak-foo.s -triple=x86_64-apple-macos -filetype=obj -o %t/weak-foo.o
# RUN: %lld -dylib %t/weak-foo.o -o %t/weak-foo.dylib

# RUN: llvm-mc %t/main.s -triple=x86_64-apple-macos -filetype=obj -o %t/main.o

## foo_with_bar.dylib's reexport should be dropped since it's linked implicitly.
# RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOBAR %s
# NOBAR-NOT: bar.dylib
# NOBAR: /usr/lib/libSystem.dylib
# NOBAR-NOT: bar.dylib
# NOBAR: foo_with_bar.dylib
# NOBAR-NOT: bar.dylib

## If bar.dylib is linked explicitly, it should not be dropped.
# RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib %t/bar.dylib
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=BAR %s
# BAR: /usr/lib/libSystem.dylib
# BAR: foo_with_bar.dylib
# BAR: bar.dylib

## ...except if -dead-strip_dylibs is passed...
# RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib %t/bar.dylib \
# RUN:      -dead_strip_dylibs
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOBAR %s

## ...or bar is explicitly marked dead-strippable.
# RUN: %lld -lSystem %t/main.o -o %t/main %t/foo.dylib %t/bar-strip.dylib
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOBARSTRIP %s
# NOBARSTRIP-NOT: bar-strip.dylib
# NOBARSTRIP: /usr/lib/libSystem.dylib
# NOBARSTRIP-NOT: bar-strip.dylib
# NOBARSTRIP: foo.dylib
# NOBARSTRIP-NOT: bar-strip.dylib

## Even libraries explicitly reexported with -reexport_library are stripped
## if they are not referenced.
# RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib \
# RUN:     -reexport_library %t/bar.dylib -dead_strip_dylibs
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOBAR %s
# RUN: llvm-otool -l %t/main | FileCheck --check-prefix=NOREEXPORT %s
# NOREEXPORT-NOT: LC_REEXPORT_DYLIB

## But -needed_library and -needed-l win over -dead_strip_dylibs again.
# RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib \
# RUN:     -needed_library %t/bar.dylib -dead_strip_dylibs
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=BAR %s
# RUN: %lld -lSystem %t/main.o -o %t/main %t/foo_with_bar.dylib \
# RUN:     -L%t -needed-lbar -dead_strip_dylibs
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=BAR %s

## LC_LINKER_OPTION does not count as an explicit reference.
# RUN: llvm-mc %t/linkopt_bar.s -triple=x86_64-apple-macos -filetype=obj -o %t/linkopt_bar.o
# RUN: %lld -lSystem %t/main.o %t/linkopt_bar.o -o %t/main -L %t %t/foo.dylib
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOLIBBAR %s
# NOLIBBAR-NOT: libbar.dylib
# NOLIBBAR: /usr/lib/libSystem.dylib
# NOLIBBAR-NOT: libbar.dylib
# NOLIBBAR: foo.dylib
# NOLIBBAR-NOT: libbar.dylib

## ...but with an additional explicit reference it's not stripped again.
# RUN: %lld -lSystem %t/main.o %t/linkopt_bar.o -o %t/main -L %t %t/foo.dylib -lbar
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=LIBBAR %s
# RUN: %lld -lSystem %t/main.o -o %t/main -L %t %t/foo.dylib -lbar %t/linkopt_bar.o
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=LIBBAR %s
# LIBBAR-DAG: /usr/lib/libSystem.dylib
# LIBBAR-DAG: libbar.dylib
# LIBBAR-DAG: foo.dylib

## Test that a DylibSymbol being replaced by a DefinedSymbol marks the
## dylib as unreferenced.
## (Note: Since there's no dynamic linking in this example, libSystem is
## stripped too. Since libSystem.dylib is needed to run an executable for
## LC_MAIN, the output would crash when run. This matches ld64's behavior (!)
## In practice, every executable uses dynamic linking, which uses
## dyld_stub_binder, which keeps libSystem alive.)
## Test all permutations of (Undefined, Defined, DylibSymbol).
# RUN: %lld -lSystem -dead_strip_dylibs %t/main.o %t/foo.o %t/foo.dylib -o %t/main
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s
# RUN: %lld -lSystem -dead_strip_dylibs %t/main.o %t/foo.dylib %t/foo.dylib %t/foo.o -o %t/main
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s

# RUN: %lld -lSystem -dead_strip_dylibs %t/foo.o %t/main.o %t/foo.dylib -o %t/main
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s
# RUN: %lld -lSystem -dead_strip_dylibs %t/foo.dylib %t/foo.dylib %t/main.o %t/foo.o -o %t/main
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s

# RUN: %lld -lSystem -dead_strip_dylibs %t/foo.o %t/foo.dylib %t/main.o -o %t/main
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s
# RUN: %lld -lSystem -dead_strip_dylibs %t/foo.dylib %t/foo.dylib %t/foo.o %t/main.o -o %t/main
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOFOO %s
# NOFOO-NOT: foo.dylib

## When linking a weak and a strong symbol from two dylibs, we should keep the
## strong one.
# RUN: %lld -lSystem -dead_strip_dylibs %t/main.o %t/foo.dylib %t/weak-foo.dylib -o %t/main
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOWEAK %s
# RUN: %lld -lSystem -dead_strip_dylibs %t/main.o %t/weak-foo.dylib %t/foo.dylib -o %t/main
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOWEAK %s
# RUN: %lld -lSystem -dead_strip_dylibs %t/foo.dylib %t/weak-foo.dylib %t/main.o -o %t/main
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOWEAK %s
# RUN: %lld -lSystem -dead_strip_dylibs %t/weak-foo.dylib %t/foo.dylib %t/main.o -o %t/main
# RUN: llvm-otool -L %t/main | FileCheck --check-prefix=NOWEAK %s
# NOWEAK-NOT: weak-foo.dylib
# NOWEAK: /foo.dylib
# NOWEAK-NOT: weak-foo.dylib

#--- main.s
.globl _foo, _main
_main:
  callq _foo
  retq

#--- foo.s
.globl _foo
_foo:
  retq

#--- bar.s
.globl _bar
_bar:
  retq

#--- linkopt_bar.s
.linker_option "-lbar"

#--- weak-foo.s
.globl _foo
.weak_definition _foo
_foo:
  retq