1# REQUIRES: x86
2
3# RUN: rm -rf %t; split-file %s %t
4# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %t/default.s -o %t/default.o
5# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %t/lazydef.s -o %t/lazydef.o
6# RUN: llvm-ar --format=darwin rcs %t/lazydef.a %t/lazydef.o
7
8## Check that mixing exported and unexported symbol options yields an error
9# RUN: not %lld -dylib %t/default.o -o /dev/null \
10# RUN:         -exported_symbol a -unexported_symbol b 2>&1 | \
11# RUN:     FileCheck --check-prefix=CONFLICT %s
12#
13# RUN: not %lld -dylib %t/default.o -o /dev/null \
14# RUN:         -exported_symbols_list /dev/null -unexported_symbol b 2>&1 | \
15# RUN:     FileCheck --check-prefix=CONFLICT %s
16#
17# RUN: not %lld -dylib %t/default.o -o /dev/null \
18# RUN:         -no_exported_symbols -unexported_symbol b 2>&1 | \
19# RUN:     FileCheck --check-prefix=CONFLICT %s
20#
21# RUN: not %lld -dylib %t/default.o -o /dev/null \
22# RUN:         -no_exported_symbols -exported_symbol b 2>&1 | \
23# RUN:     FileCheck --check-prefix=CONFLICT-NO-EXPORTS %s
24#
25# RUN: not %lld -dylib %t/default.o -o /dev/null \
26# RUN:         -no_exported_symbols -exported_symbols_list %t/literals.txt 2>&1 | \
27# RUN:     FileCheck --check-prefix=CONFLICT-NO-EXPORTS %s
28
29# CONFLICT: error: cannot use both -exported_symbol* and -unexported_symbol* options
30
31# CONFLICT-NO-EXPORTS: error: cannot use both -exported_symbol* and -no_exported_symbols options
32
33## Check that an exported literal name with no symbol definition yields an error
34## but that an exported glob-pattern with no matching symbol definition is OK
35# RUN: not %lld -dylib %t/default.o -o /dev/null \
36# RUN:         -exported_symbol absent_literal \
37# RUN:         -exported_symbol absent_gl?b 2>&1 | \
38# RUN:     FileCheck --check-prefix=UNDEF %s
39
40# UNDEF: error: undefined symbol: absent_literal
41# UNDEF-NEXT: >>> referenced by -exported_symbol(s_list)
42# UNDEF-NOT: error: {{.*}} absent_gl{{.}}b
43
44## Check that dynamic_lookup suppresses the error
45# RUN: %lld -dylib %t/default.o -undefined dynamic_lookup -o %t/dyn-lookup \
46# RUN:      -exported_symbol absent_literal
47# RUN: llvm-objdump --macho --syms %t/dyn-lookup | FileCheck %s --check-prefix=DYN
48# DYN: *UND* absent_literal
49
50## Check that exported literal symbols are present in output's
51## symbol table, even lazy symbols which would otherwise be omitted
52# RUN: %lld -dylib %t/default.o %t/lazydef.a -o %t/lazydef \
53# RUN:         -exported_symbol _keep_globl \
54# RUN:         -exported_symbol _keep_lazy
55# RUN: llvm-objdump --syms %t/lazydef | \
56# RUN:     FileCheck --check-prefix=EXPORT %s
57
58# EXPORT-DAG: g     F __TEXT,__text _keep_globl
59# EXPORT-DAG: g     F __TEXT,__text _keep_lazy
60
61## Check that exported symbol is global
62# RUN: %no-fatal-warnings-lld -dylib %t/default.o -o %t/hidden-export \
63# RUN:         -exported_symbol _private_extern 2>&1 | \
64# RUN:     FileCheck --check-prefix=PRIVATE %s
65
66# PRIVATE: warning: cannot export hidden symbol _private_extern
67
68## Check that we still hide the other symbols despite the warning
69# RUN: llvm-objdump --macho --exports-trie %t/hidden-export | \
70# RUN:     FileCheck --check-prefix=EMPTY-TRIE %s
71# EMPTY-TRIE:       Exports trie:
72# EMPTY-TRIE-EMPTY:
73
74## Check that the export trie is unaltered
75# RUN: %lld -dylib %t/default.o -o %t/default
76# RUN: llvm-objdump --macho --exports-trie %t/default | \
77# RUN:     FileCheck --check-prefix=DEFAULT %s
78
79# DEFAULT-LABEL: Exports trie:
80# DEFAULT-DAG:   _hide_globl
81# DEFAULT-DAG:   _keep_globl
82# DEFAULT-NOT:   _private_extern
83
84## Check that the export trie is shaped by an allow list and then
85## by a deny list. Both lists are designed to yield the same result.
86
87## Check the allow list
88# RUN: %lld -dylib %t/default.o -o %t/allowed \
89# RUN:     -exported_symbol _keep_globl
90# RUN: llvm-objdump --macho --exports-trie %t/allowed | \
91# RUN:     FileCheck --check-prefix=TRIE %s
92# RUN: llvm-nm -m %t/allowed | \
93# RUN:     FileCheck --check-prefix=NM %s
94
95## Check the deny list
96# RUN: %lld -dylib %t/default.o -o %t/denied \
97# RUN:     -unexported_symbol _hide_globl
98# RUN: llvm-objdump --macho --exports-trie %t/denied | \
99# RUN:     FileCheck --check-prefix=TRIE %s
100# RUN: llvm-nm -m %t/denied | \
101# RUN:     FileCheck --check-prefix=NM %s
102
103# TRIE-LABEL: Exports trie:
104# TRIE-DAG:   _keep_globl
105# TRIE-NOT:   _hide_globl
106# TRIE-NOT:   _private_extern
107
108# NM-DAG: external _keep_globl
109# NM-DAG: non-external (was a private external) _hide_globl
110# NM-DAG: non-external (was a private external) _private_extern
111
112# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
113# RUN:     %t/symdefs.s -o %t/symdefs.o
114
115## Check that only string-literal patterns match
116## Check that comments and blank lines are stripped from symbol list
117# RUN: %lld -dylib %t/symdefs.o -o %t/literal \
118# RUN:     -exported_symbols_list %t/literals.txt
119# RUN: llvm-objdump --macho --exports-trie %t/literal | \
120# RUN:     FileCheck --check-prefix=LITERAL %s
121
122# LITERAL-DAG: literal_only
123# LITERAL-DAG: literal_also
124# LITERAL-DAG: globby_also
125# LITERAL-NOT: globby_only
126
127## Check that only glob patterns match
128## Check that comments and blank lines are stripped from symbol list
129# RUN: %lld -dylib %t/symdefs.o -o %t/globby \
130# RUN:     -exported_symbols_list %t/globbys.txt
131# RUN: llvm-objdump --macho --exports-trie %t/globby | \
132# RUN:     FileCheck --check-prefix=GLOBBY %s
133
134# GLOBBY-DAG: literal_also
135# GLOBBY-DAG: globby_only
136# GLOBBY-DAG: globby_also
137# GLOBBY-NOT: literal_only
138
139# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
140# RUN:     %t/autohide.s -o %t/autohide.o
141# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
142# RUN:     %t/autohide-private-extern.s -o %t/autohide-private-extern.o
143# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
144# RUN:     %t/glob-private-extern.s -o %t/glob-private-extern.o
145# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
146# RUN:     %t/weak-private-extern.s -o %t/weak-private-extern.o
147## Test that we can export the autohide symbol but not when it's also
148## private-extern
149# RUN: %lld -dylib -exported_symbol "_foo" %t/autohide.o -o %t/exp-autohide.dylib
150# RUN: llvm-nm -g %t/exp-autohide.dylib | FileCheck %s --check-prefix=EXP-AUTOHIDE
151
152# RUN: not %lld -dylib -exported_symbol "_foo" %t/autohide-private-extern.o \
153# RUN:   -o /dev/null  2>&1 | FileCheck %s --check-prefix=AUTOHIDE-PRIVATE
154
155# RUN: not %lld -dylib -exported_symbol "_foo" %t/autohide.o \
156# RUN:   %t/glob-private-extern.o -o /dev/null 2>&1 | \
157# RUN:   FileCheck %s --check-prefix=AUTOHIDE-PRIVATE
158
159# RUN: not %lld -dylib -exported_symbol "_foo" %t/autohide.o \
160# RUN:   %t/weak-private-extern.o -o /dev/null 2>&1 | \
161# RUN:   FileCheck %s --check-prefix=AUTOHIDE-PRIVATE
162
163## Test that exported hidden symbols are still treated as a liveness root.
164## This previously used to crash when enabling -dead_strip since it's unconventional
165## to add treat private extern symbols as a liveness root.
166# RUN: %no-fatal-warnings-lld -dylib -exported_symbol "_foo" %t/autohide-private-extern.o \
167# RUN:   -dead_strip -o %t/exported-hidden
168# RUN: llvm-nm -m %t/exported-hidden | FileCheck %s --check-prefix=AUTOHIDE-PRIVATE-DEAD-STRIP
169
170# RUN: %no-fatal-warnings-lld -dylib -exported_symbol "_foo" %t/autohide.o \
171# RUN:   -dead_strip %t/glob-private-extern.o -o %t/exported-hidden
172# RUN: llvm-nm -m %t/exported-hidden | FileCheck %s --check-prefix=AUTOHIDE-PRIVATE-DEAD-STRIP
173
174# RUN: %no-fatal-warnings-lld -dylib -exported_symbol "_foo" %t/autohide.o \
175# RUN:   -dead_strip %t/weak-private-extern.o -o %t/exported-hidden
176# RUN: llvm-nm -m %t/exported-hidden | FileCheck %s --check-prefix=AUTOHIDE-PRIVATE-DEAD-STRIP
177
178# EXP-AUTOHIDE: T _foo
179# AUTOHIDE-PRIVATE: error: cannot export hidden symbol _foo
180# AUTOHIDE-PRIVATE-DEAD-STRIP: (__TEXT,__text) non-external (was a private external) _foo
181
182## Test not exporting any symbols
183# RUN: %lld -dylib %t/symdefs.o -o %t/noexports -exported_symbols_list /dev/null
184# RUN: llvm-objdump --macho --exports-trie %t/noexports | FileCheck --check-prefix=NOEXPORTS %s
185# RUN: %lld -dylib %t/symdefs.o -o %t/noexports -no_exported_symbols
186# RUN: llvm-objdump --macho --exports-trie %t/noexports | FileCheck --check-prefix=NOEXPORTS %s
187
188# NOEXPORTS-NOT: globby_also
189# NOEXPORTS-NOT: globby_only
190# NOEXPORTS-NOT: literal_also
191# NOEXPORTS-NOT: literal_only
192
193#--- default.s
194
195.globl _keep_globl, _hide_globl
196_keep_globl:
197  retq
198_hide_globl:
199  retq
200.private_extern _private_extern
201_private_extern:
202  retq
203_private:
204  retq
205
206#--- lazydef.s
207
208.globl _keep_lazy
209_keep_lazy:
210  retq
211
212#--- symdefs.s
213
214.globl literal_only, literal_also, globby_only, globby_also
215literal_only:
216  retq
217literal_also:
218  retq
219globby_only:
220  retq
221globby_also:
222  retq
223
224#--- literals.txt
225
226  literal_only # comment
227  literal_also
228
229# globby_only
230  globby_also
231
232#--- globbys.txt
233
234# literal_only
235  l?ter[aeiou]l_*[^y] # comment
236
237  *gl?bby_*
238
239#--- autohide.s
240.globl _foo
241.weak_def_can_be_hidden _foo
242_foo:
243  retq
244
245#--- autohide-private-extern.s
246.globl _foo
247.weak_def_can_be_hidden _foo
248.private_extern _foo
249_foo:
250  retq
251
252#--- glob-private-extern.s
253.global _foo
254.private_extern _foo
255_foo:
256  retq
257
258#--- weak-private-extern.s
259.global _foo
260.weak_definition _foo
261.private_extern _foo
262_foo:
263  retq
264