[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / lld / test / MachO / dead-strip.s
blobf60305a4ec494dacc25355f08c63c11219a67935
1 # REQUIRES: x86, llvm-64-bits
3 # RUN: rm -rf %t; split-file %s %t
5 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
6 # RUN: %t/basics.s -o %t/basics.o
8 ## Check that .private_extern symbols are marked as local in the symbol table
9 ## and aren't in the export trie.
10 # RUN: %lld -lSystem -dead_strip -map %t/map -u _ref_private_extern_u \
11 # RUN: %t/basics.o -o %t/basics
12 # RUN: llvm-objdump --syms --section-headers %t/basics | \
13 # RUN: FileCheck --check-prefix=EXEC --implicit-check-not _unref %s
14 # RUN: llvm-objdump --macho --section=__DATA,__ref_section \
15 # RUN: --exports-trie --indirect-symbols %t/basics | \
16 # RUN: FileCheck --check-prefix=EXECDATA --implicit-check-not _unref %s
17 # RUN: llvm-otool -l %t/basics | grep -q 'segname __PAGEZERO'
18 # EXEC-LABEL: Sections:
19 # EXEC-LABEL: Name
20 # EXEC-NEXT: __text
21 # EXEC-NEXT: __got
22 # EXEC-NEXT: __ref_section
23 # EXEC-NEXT: __common
24 # EXEC-LABEL: SYMBOL TABLE:
25 # EXEC-DAG: l {{.*}} _ref_data
26 # EXEC-DAG: l {{.*}} _ref_local
27 # EXEC-DAG: l {{.*}} _ref_from_no_dead_strip_globl
28 # EXEC-DAG: l {{.*}} _no_dead_strip_local
29 # EXEC-DAG: l {{.*}} _ref_from_no_dead_strip_local
30 # EXEC-DAG: l {{.*}} _ref_private_extern_u
31 # EXEC-DAG: l {{.*}} _main
32 # EXEC-DAG: l {{.*}} _ref_private_extern
33 # EXEC-DAG: g {{.*}} _no_dead_strip_globl
34 # EXEC-DAG: g {{.*}} _ref_com
35 # EXEC-DAG: g {{.*}} __mh_execute_header
36 # EXECDATA-LABEL: Indirect symbols
37 # EXECDATA-NEXT: name
38 # EXECDATA-NEXT: _ref_com
39 # EXECDATA-LABEL: Contents of (__DATA,__ref_section) section
40 # EXECDATA-NEXT: 04 00 00 00 00 00 00 00 05 00 00 00 00 00 00 00
41 # EXECDATA-LABEL: Exports trie:
42 # EXECDATA-DAG: _ref_com
43 # EXECDATA-DAG: _no_dead_strip_globl
44 # EXECDATA-DAG: __mh_execute_header
46 ## Check that dead stripped symbols get listed properly.
47 # RUN: FileCheck --check-prefix=MAP %s < %t/map
49 # MAP: _main
50 # MAP-LABEL: Dead Stripped Symbols
51 # MAP-DAG: <<dead>> 0x00000001 [ 2] _unref_com
52 # MAP-DAG: <<dead>> 0x00000008 [ 2] _unref_data
53 # MAP-DAG: <<dead>> 0x00000006 [ 2] _unref_extern
54 # MAP-DAG: <<dead>> 0x00000001 [ 2] _unref_local
55 # MAP-DAG: <<dead>> 0x00000007 [ 2] _unref_private_extern
56 # MAP-DAG: <<dead>> 0x00000008 [ 2] l_unref_data
58 ## Run dead stripping on code without any dead symbols.
59 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
60 # RUN: %t/no-dead-symbols.s -o %t/no-dead-symbols.o
61 # RUN: %lld -lSystem -dead_strip -map %t/no-dead-symbols-map \
62 # RUN: %t/no-dead-symbols.o -o %t/no-dead-symbols
63 ## Mark the end of the file with a string.
64 # RUN: FileCheck --check-prefix=NODEADSYMBOLS %s < %t/no-dead-symbols-map
66 # NODEADSYMBOLS-LABEL: # Symbols:
67 # NODEADSYMBOLS-NEXT: # Address Size File Name
68 # NODEADSYMBOLS-NEXT: _main
69 # NODEADSYMBOLS-LABEL: # Dead Stripped Symbols:
70 # NODEADSYMBOLS-NEXT: # Size File Name
71 # NODEADSYMBOLS-EMPTY:
73 # RUN: %lld -dylib -dead_strip -u _ref_private_extern_u %t/basics.o -o %t/basics.dylib
74 # RUN: llvm-objdump --syms %t/basics.dylib | \
75 # RUN: FileCheck --check-prefix=DYLIB --implicit-check-not _unref %s
76 # RUN: %lld -bundle -dead_strip -u _ref_private_extern_u %t/basics.o -o %t/basics.dylib
77 # RUN: llvm-objdump --syms %t/basics.dylib | \
78 # RUN: FileCheck --check-prefix=DYLIB --implicit-check-not _unref %s
79 # DYLIB-LABEL: SYMBOL TABLE:
80 # DYLIB-DAG: l {{.*}} _ref_data
81 # DYLIB-DAG: l {{.*}} _ref_local
82 # DYLIB-DAG: l {{.*}} _ref_from_no_dead_strip_globl
83 # DYLIB-DAG: l {{.*}} _no_dead_strip_local
84 # DYLIB-DAG: l {{.*}} _ref_from_no_dead_strip_local
85 # DYLIB-DAG: l {{.*}} _ref_private_extern_u
86 # DYLIB-DAG: l {{.*}} _ref_private_extern
87 # DYLIB-DAG: g {{.*}} _ref_com
88 # DYLIB-DAG: g {{.*}} _unref_com
89 # DYLIB-DAG: g {{.*}} _unref_extern
90 # DYLIB-DAG: g {{.*}} _no_dead_strip_globl
92 ## Extern symbols aren't stripped from executables with -export_dynamic
93 # RUN: %lld -lSystem -dead_strip -export_dynamic -u _ref_private_extern_u \
94 # RUN: %t/basics.o -o %t/basics-export-dyn
95 # RUN: llvm-objdump --syms --section-headers %t/basics-export-dyn | \
96 # RUN: FileCheck --check-prefix=EXECDYN %s
97 # EXECDYN-LABEL: Sections:
98 # EXECDYN-LABEL: Name
99 # EXECDYN-NEXT: __text
100 # EXECDYN-NEXT: __got
101 # EXECDYN-NEXT: __ref_section
102 # EXECDYN-NEXT: __common
103 # EXECDYN-LABEL: SYMBOL TABLE:
104 # EXECDYN-DAG: l {{.*}} _ref_data
105 # EXECDYN-DAG: l {{.*}} _ref_local
106 # EXECDYN-DAG: l {{.*}} _ref_from_no_dead_strip_globl
107 # EXECDYN-DAG: l {{.*}} _no_dead_strip_local
108 # EXECDYN-DAG: l {{.*}} _ref_from_no_dead_strip_local
109 # EXECDYN-DAG: l {{.*}} _ref_private_extern_u
110 # EXECDYN-DAG: l {{.*}} _main
111 # EXECDYN-DAG: l {{.*}} _ref_private_extern
112 # EXECDYN-DAG: g {{.*}} _ref_com
113 # EXECDYN-DAG: g {{.*}} _unref_com
114 # EXECDYN-DAG: g {{.*}} _unref_extern
115 # EXECDYN-DAG: g {{.*}} _no_dead_strip_globl
116 # EXECDYN-DAG: g {{.*}} __mh_execute_header
118 ## Absolute symbol handling.
119 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
120 # RUN: %t/abs.s -o %t/abs.o
121 # RUN: %lld -lSystem -dead_strip %t/abs.o -o %t/abs
122 # RUN: llvm-objdump --macho --syms --exports-trie %t/abs | \
123 # RUN: FileCheck --check-prefix=ABS %s
124 #ABS-LABEL: SYMBOL TABLE:
125 #ABS-NEXT: g {{.*}} _main
126 #ABS-NEXT: g *ABS* _abs1
127 #ABS-NEXT: g {{.*}} __mh_execute_header
128 #ABS-LABEL: Exports trie:
129 #ABS-NEXT: __mh_execute_header
130 #ABS-NEXT: _main
131 #ABS-NEXT: _abs1 [absolute]
133 ## Check that symbols from -exported_symbol(s_list) are preserved.
134 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
135 # RUN: %t/exported-symbol.s -o %t/exported-symbol.o
136 # RUN: %lld -lSystem -dead_strip -exported_symbol _my_exported_symbol \
137 # RUN: %t/exported-symbol.o -o %t/exported-symbol
138 # RUN: llvm-objdump --syms %t/exported-symbol | \
139 # RUN: FileCheck --check-prefix=EXPORTEDSYMBOL --implicit-check-not _unref %s
140 # EXPORTEDSYMBOL-LABEL: SYMBOL TABLE:
141 # EXPORTEDSYMBOL-NEXT: l {{.*}} _main
142 # EXPORTEDSYMBOL-NEXT: l {{.*}} __mh_execute_header
143 # EXPORTEDSYMBOL-NEXT: g {{.*}} _my_exported_symbol
145 ## Check that mod_init_funcs and mod_term_funcs are not stripped.
146 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
147 # RUN: %t/mod-funcs.s -o %t/mod-funcs.o
148 # RUN: %lld -lSystem -dead_strip %t/mod-funcs.o -o %t/mod-funcs
149 # RUN: llvm-objdump --syms %t/mod-funcs | \
150 # RUN: FileCheck --check-prefix=MODFUNCS --implicit-check-not _unref %s
151 # MODFUNCS-LABEL: SYMBOL TABLE:
152 # MODFUNCS-NEXT: l {{.*}} _ref_from_init
153 # MODFUNCS-NEXT: l {{.*}} _ref_init
154 # MODFUNCS-NEXT: l {{.*}} _ref_from_term
155 # MODFUNCS-NEXT: l {{.*}} _ref_term
156 # MODFUNCS-NEXT: g {{.*}} _main
157 # MODFUNCS-NEXT: g {{.*}} __mh_execute_header
159 ## Check that DylibSymbols in dead subsections are stripped: They should
160 ## not be in the import table and should have no import stubs.
161 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
162 # RUN: %t/dylib.s -o %t/dylib.o
163 # RUN: %lld -dylib -dead_strip %t/dylib.o -o %t/dylib.dylib
164 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
165 # RUN: %t/strip-dylib-ref.s -o %t/strip-dylib-ref.o
166 # RUN: %lld -lSystem -dead_strip %t/strip-dylib-ref.o %t/dylib.dylib \
167 # RUN: -o %t/strip-dylib-ref -U _ref_undef_fun -U _unref_undef_fun
168 # RUN: llvm-objdump --syms --bind --lazy-bind --weak-bind %t/strip-dylib-ref | \
169 # RUN: FileCheck --check-prefix=STRIPDYLIB --implicit-check-not _unref %s
170 # STRIPDYLIB: SYMBOL TABLE:
171 # STRIPDYLIB-NEXT: l {{.*}} __dyld_private
172 # STRIPDYLIB-NEXT: g {{.*}} _main
173 # STRIPDYLIB-NEXT: g {{.*}} __mh_execute_header
174 # STRIPDYLIB-NEXT: *UND* dyld_stub_binder
175 # STRIPDYLIB-NEXT: *UND* _ref_dylib_fun
176 # STRIPDYLIB-NEXT: *UND* _ref_undef_fun
177 # STRIPDYLIB: Bind table:
178 # STRIPDYLIB: Lazy bind table:
179 # STRIPDYLIB: __DATA __la_symbol_ptr {{.*}} flat-namespace _ref_undef_fun
180 # STRIPDYLIB: __DATA __la_symbol_ptr {{.*}} dylib _ref_dylib_fun
181 # STRIPDYLIB: Weak bind table:
182 ## Stubs smoke check: There should be two stubs entries, not four, but we
183 ## don't verify that they belong to _ref_undef_fun and _ref_dylib_fun.
184 # RUN: llvm-objdump -d --section=__stubs --section=__stub_helper \
185 # RUN: %t/strip-dylib-ref |FileCheck --check-prefix=STUBS %s
186 # STUBS-LABEL: <__stubs>:
187 # STUBS-NEXT: jmpq
188 # STUBS-NEXT: jmpq
189 # STUBS-NOT: jmpq
190 # STUBS-LABEL: <__stub_helper>:
191 # STUBS: pushq $0
192 # STUBS: jmp
193 # STUBS: jmp
194 # STUBS-NOT: jmp
195 ## An undefined symbol referenced from a dead-stripped function shouldn't
196 ## produce a diagnostic:
197 # RUN: %lld -lSystem -dead_strip %t/strip-dylib-ref.o %t/dylib.dylib \
198 # RUN: -o %t/strip-dylib-ref -U _ref_undef_fun
200 ## Check that referenced undefs are kept with -undefined dynamic_lookup.
201 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
202 # RUN: %t/ref-undef.s -o %t/ref-undef.o
203 # RUN: %lld -lSystem -dead_strip %t/ref-undef.o \
204 # RUN: -o %t/ref-undef -undefined dynamic_lookup
205 # RUN: llvm-objdump --syms --lazy-bind %t/ref-undef | \
206 # RUN: FileCheck --check-prefix=STRIPDYNLOOKUP %s
207 # STRIPDYNLOOKUP: SYMBOL TABLE:
208 # STRIPDYNLOOKUP: *UND* _ref_undef_fun
209 # STRIPDYNLOOKUP: Lazy bind table:
210 # STRIPDYNLOOKUP: __DATA __la_symbol_ptr {{.*}} flat-namespace _ref_undef_fun
212 ## S_ATTR_LIVE_SUPPORT tests.
213 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
214 # RUN: %t/live-support.s -o %t/live-support.o
215 # RUN: %lld -lSystem -dead_strip %t/live-support.o %t/dylib.dylib \
216 # RUN: -U _ref_undef_fun -U _unref_undef_fun -o %t/live-support
217 # RUN: llvm-objdump --syms %t/live-support | \
218 # RUN: FileCheck --check-prefix=LIVESUPP --implicit-check-not _unref %s
219 # LIVESUPP-LABEL: SYMBOL TABLE:
220 # LIVESUPP-NEXT: l {{.*}} _ref_ls_fun_fw
221 # LIVESUPP-NEXT: l {{.*}} _ref_ls_fun_bw
222 # LIVESUPP-NEXT: l {{.*}} _ref_ls_dylib_fun
223 # LIVESUPP-NEXT: l {{.*}} _ref_ls_undef_fun
224 # LIVESUPP-NEXT: l {{.*}} __dyld_private
225 # LIVESUPP-NEXT: g {{.*}} _main
226 # LIVESUPP-NEXT: g {{.*}} _bar
227 # LIVESUPP-NEXT: g {{.*}} _foo
228 # LIVESUPP-NEXT: g {{.*}} __mh_execute_header
229 # LIVESUPP-NEXT: *UND* dyld_stub_binder
230 # LIVESUPP-NEXT: *UND* _ref_dylib_fun
231 # LIVESUPP-NEXT: *UND* _ref_undef_fun
233 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
234 # RUN: %t/live-support-iterations.s -o %t/live-support-iterations.o
235 # RUN: %lld -lSystem -dead_strip %t/live-support-iterations.o \
236 # RUN: -o %t/live-support-iterations
237 # RUN: llvm-objdump --syms %t/live-support-iterations | \
238 # RUN: FileCheck --check-prefix=LIVESUPP2 --implicit-check-not _unref %s
239 # LIVESUPP2-LABEL: SYMBOL TABLE:
240 # LIVESUPP2-NEXT: l {{.*}} _bar
241 # LIVESUPP2-NEXT: l {{.*}} _foo_refd
242 # LIVESUPP2-NEXT: l {{.*}} _bar_refd
243 # LIVESUPP2-NEXT: l {{.*}} _baz
244 # LIVESUPP2-NEXT: l {{.*}} _baz_refd
245 # LIVESUPP2-NEXT: l {{.*}} _foo
246 # LIVESUPP2-NEXT: g {{.*}} _main
247 # LIVESUPP2-NEXT: g {{.*}} __mh_execute_header
249 ## Dead stripping should not remove the __TEXT,__unwind_info
250 ## and __TEXT,__gcc_except_tab functions, but it should still
251 ## remove the unreferenced function __Z5unref.
252 ## The reference to ___gxx_personality_v0 should also not be
253 ## stripped.
254 ## (Need to use darwin19.0.0 to make -mc emit __LD,__compact_unwind.)
255 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 \
256 # RUN: %t/unwind.s -o %t/unwind.o
257 # RUN: %lld -lc++ -lSystem -dead_strip %t/unwind.o -o %t/unwind
258 # RUN: llvm-objdump --syms %t/unwind | \
259 # RUN: FileCheck --check-prefix=UNWIND --implicit-check-not unref %s
260 # RUN: llvm-otool -l %t/unwind | FileCheck --check-prefix=UNWINDSECT %s
261 # UNWINDSECT-DAG: sectname __unwind_info
262 # UNWINDSECT-DAG: sectname __gcc_except_tab
263 # UNWIND-LABEL: SYMBOL TABLE:
264 # UNWIND-NEXT: l O __TEXT,__gcc_except_tab GCC_except_table1
265 # UNWIND-NEXT: l O __DATA,__data __dyld_private
266 # UNWIND-NEXT: g F __TEXT,__text _main
267 # UNWIND-NEXT: g F __TEXT,__text __mh_execute_header
268 # UNWIND-NEXT: *UND* dyld_stub_binder
269 # UNWIND-NEXT: *UND* ___cxa_allocate_exception
270 # UNWIND-NEXT: *UND* ___cxa_end_catch
271 # UNWIND-NEXT: *UND* __ZTIi
272 # UNWIND-NEXT: *UND* ___cxa_throw
273 # UNWIND-NEXT: *UND* ___gxx_personality_v0
274 # UNWIND-NEXT: *UND* ___cxa_begin_catch
275 # UNWIND-NOT: GCC_except_table0
277 ## If a dead stripped function has a strong ref to a dylib symbol but
278 ## a live function only a weak ref, the dylib is still not a WEAK_DYLIB.
279 ## This matches ld64.
280 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
281 # RUN: %t/weak-ref.s -o %t/weak-ref.o
282 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
283 # RUN: %t/strong-dead-ref.s -o %t/strong-dead-ref.o
284 # RUN: %lld -lSystem -dead_strip %t/weak-ref.o %t/strong-dead-ref.o \
285 # RUN: %t/dylib.dylib -o %t/weak-ref
286 # RUN: llvm-otool -l %t/weak-ref | FileCheck -DDIR=%t --check-prefix=WEAK %s
287 # WEAK: cmd LC_LOAD_DYLIB
288 # WEAK-NEXT: cmdsize
289 # WEAK-NEXT: name /usr/lib/libSystem.dylib
290 # WEAK: cmd LC_LOAD_DYLIB
291 # WEAK-NEXT: cmdsize
292 # WEAK-NEXT: name [[DIR]]/dylib.dylib
294 ## A strong symbol that would override a weak import does not emit the
295 ## "this overrides a weak import" opcode if it is dead-stripped.
296 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
297 # RUN: %t/weak-dylib.s -o %t/weak-dylib.o
298 # RUN: %lld -dylib -dead_strip %t/weak-dylib.o -o %t/weak-dylib.dylib
299 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
300 # RUN: %t/dead-weak-override.s -o %t/dead-weak-override.o
301 # RUN: %lld -dead_strip %t/dead-weak-override.o %t/weak-dylib.dylib \
302 # RUN: -o %t/dead-weak-override
303 # RUN: llvm-objdump --macho --weak-bind --private-header \
304 # RUN: %t/dead-weak-override | FileCheck --check-prefix=DEADWEAK %s
305 # DEADWEAK-NOT: WEAK_DEFINES
306 # DEADWEAK: Weak bind table:
307 # DEADWEAK: segment section address type addend symbol
308 # DEADWEAK-NOT: strong _weak_in_dylib
310 ## Stripped symbols should not be in the debug info stabs entries.
311 # RUN: llvm-mc -g -filetype=obj -triple=x86_64-apple-macos \
312 # RUN: %t/debug.s -o %t/debug.o
313 # RUN: %lld -lSystem -dead_strip %t/debug.o -o %t/debug
314 # RUN: dsymutil -s %t/debug | FileCheck --check-prefix=EXECSTABS %s
315 # EXECSTABS-NOT: N_FUN {{.*}} '_unref'
316 # EXECSTABS: N_FUN {{.*}} '_main'
317 # EXECSTABS-NOT: N_FUN {{.*}} '_unref'
319 # RUN: llvm-mc -g -filetype=obj -triple=x86_64-apple-macos \
320 # RUN: %t/literals.s -o %t/literals.o
321 # RUN: %lld -dylib -dead_strip %t/literals.o -o %t/literals
322 # RUN: llvm-objdump --macho --section="__TEXT,__cstring" --section="__DATA,str_ptrs" \
323 # RUN: --section="__TEXT,__literals" %t/literals | FileCheck %s --check-prefix=LIT
324 # LIT: Contents of (__TEXT,__cstring) section
325 # LIT-NEXT: foobar
326 # LIT-NEXT: Contents of (__DATA,str_ptrs) section
327 # LIT-NEXT: __TEXT:__cstring:bar
328 # LIT-NEXT: __TEXT:__cstring:bar
329 # LIT-NEXT: Contents of (__TEXT,__literals) section
330 # LIT-NEXT: ef be ad de {{$}}
332 ## Duplicate symbols that will be dead stripped later should not fail when using
333 ## the --dead-stripped-duplicates flag
334 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
335 # RUN: %t/duplicate1.s -o %t/duplicate1.o
336 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
337 # RUN: %t/duplicate2.s -o %t/duplicate2.o
338 # RUN: %lld -lSystem -dead_strip --dead-strip-duplicates -map %t/stripped-duplicate-map \
339 # RUN: %t/duplicate1.o %t/duplicate2.o -o %t/duplicate
340 # RUN: llvm-objdump --syms %t/duplicate | FileCheck %s --check-prefix=DUP
341 # DUP-LABEL: SYMBOL TABLE:
342 # DUP-NEXT: g F __TEXT,__text _main
343 # DUP-NEXT: g F __TEXT,__text __mh_execute_header
344 # DUP-NEXT: *UND* dyld_stub_binder
346 ## Check that the duplicate dead stripped symbols get listed properly.
347 # RUN: FileCheck --check-prefix=DUPMAP %s < %t/stripped-duplicate-map
348 # DUPMAP: _main
349 # DUPMAP-LABEL: Dead Stripped Symbols
350 # DUPMAP: <<dead>> 0x00000001 [ 3] _foo
352 #--- duplicate1.s
353 .text
354 .globl _main, _foo
355 _foo:
356 retq
358 _main:
359 retq
361 .subsections_via_symbols
363 #--- duplicate2.s
364 .text
365 .globl _foo
366 _foo:
367 retq
369 .subsections_via_symbols
371 #--- basics.s
372 .comm _ref_com, 1
373 .comm _unref_com, 1
375 .section __DATA,__unref_section
376 _unref_data:
377 .quad 4
379 l_unref_data:
380 .quad 5
382 ## Referenced by no_dead_strip == S_ATTR_NO_DEAD_STRIP
383 .section __DATA,__ref_section,regular,no_dead_strip
385 ## Referenced because in no_dead_strip section.
386 _ref_data:
387 .quad 4
389 ## This is a local symbol so it's not in the symbol table, but
390 ## it is still in the section data.
391 l_ref_data:
392 .quad 5
394 .text
396 # Exported symbols should not be stripped from dylibs
397 # or bundles, but they should be stripped from executables.
398 .globl _unref_extern
399 _unref_extern:
400 callq _ref_local
401 retq
403 # Unreferenced local symbols should be stripped.
404 _unref_local:
405 retq
407 # Same for unreferenced private externs.
408 .globl _unref_private_extern
409 .private_extern _unref_private_extern
410 _unref_private_extern:
411 # This shouldn't create an indirect symbol since it's
412 # a reference from a dead function.
413 movb _unref_com@GOTPCREL(%rip), %al
414 retq
416 # Referenced local symbols should not be stripped.
417 _ref_local:
418 callq _ref_private_extern
419 retq
421 # Same for referenced private externs.
422 # This one is referenced by a relocation.
423 .globl _ref_private_extern
424 .private_extern _ref_private_extern
425 _ref_private_extern:
426 retq
428 # This one is referenced by a -u flag.
429 .globl _ref_private_extern_u
430 .private_extern _ref_private_extern_u
431 _ref_private_extern_u:
432 retq
434 # Entry point should not be stripped for executables, even if hidden.
435 # For shared libraries this is stripped since it's just a regular hidden
436 # symbol there.
437 .globl _main
438 .private_extern _main
439 _main:
440 movb _ref_com@GOTPCREL(%rip), %al
441 callq _ref_local
442 retq
444 # Things marked no_dead_strip should not be stripped either.
445 # (clang emits this e.g. for `__attribute__((used))` globals.)
446 # Both for .globl symbols...
447 .globl _no_dead_strip_globl
448 .no_dead_strip _no_dead_strip_globl
449 _no_dead_strip_globl:
450 callq _ref_from_no_dead_strip_globl
451 retq
452 _ref_from_no_dead_strip_globl:
453 retq
455 # ...and for locals.
456 .no_dead_strip _no_dead_strip_local
457 _no_dead_strip_local:
458 callq _ref_from_no_dead_strip_local
459 retq
460 _ref_from_no_dead_strip_local:
461 retq
463 .subsections_via_symbols
465 #--- exported-symbol.s
466 .text
468 .globl _unref_symbol
469 _unref_symbol:
470 retq
472 .globl _my_exported_symbol
473 _my_exported_symbol:
474 retq
476 .globl _main
477 _main:
478 retq
480 .subsections_via_symbols
482 #--- abs.s
483 .globl _abs1, _abs2, _abs3
485 .no_dead_strip _abs1
486 _abs1 = 1
487 _abs2 = 2
488 _abs3 = 3
490 .section __DATA,__foo,regular,no_dead_strip
491 # Absolute symbols are not in a section, so the no_dead_strip
492 # on the section above has no effect.
493 .globl _abs4
494 _abs4 = 4
496 .text
497 .globl _main
498 _main:
499 # This is relaxed away, so there's no relocation here and
500 # _abs3 isn't in the exported symbol table.
501 mov _abs3, %rax
502 retq
504 .subsections_via_symbols
506 #--- mod-funcs.s
507 ## Roughly based on `clang -O2 -S` output for `struct A { A(); ~A(); }; A a;`
508 ## for mod_init_funcs. mod_term_funcs then similar to that.
509 .section __TEXT,__StaticInit,regular,pure_instructions
511 __unref:
512 retq
514 _ref_from_init:
515 retq
517 _ref_init:
518 callq _ref_from_init
519 retq
521 _ref_from_term:
522 retq
524 _ref_term:
525 callq _ref_from_term
526 retq
528 .globl _main
529 _main:
530 retq
532 .section __DATA,__mod_init_func,mod_init_funcs
533 .quad _ref_init
535 .section __DATA,__mod_term_func,mod_term_funcs
536 .quad _ref_term
538 .subsections_via_symbols
540 #--- dylib.s
541 .text
543 .globl _ref_dylib_fun
544 _ref_dylib_fun:
545 retq
547 .globl _unref_dylib_fun
548 _unref_dylib_fun:
549 retq
551 .subsections_via_symbols
553 #--- strip-dylib-ref.s
554 .text
556 _unref:
557 callq _ref_dylib_fun
558 callq _unref_dylib_fun
559 callq _ref_undef_fun
560 callq _unref_undef_fun
561 retq
563 .globl _main
564 _main:
565 callq _ref_dylib_fun
566 callq _ref_undef_fun
567 retq
569 .subsections_via_symbols
571 #--- live-support.s
572 ## In practice, live_support is used for instruction profiling
573 ## data and asan. (Also for __eh_frame, but that needs special handling
574 ## in the linker anyways.)
575 ## This test isn't based on anything happening in real code though.
576 .section __TEXT,__ref_ls_fw,regular,live_support
577 _ref_ls_fun_fw:
578 # This is called by _main and is kept alive by normal
579 # forward liveness propagation, The live_support attribute
580 # does nothing in this case.
581 retq
583 .section __TEXT,__unref_ls_fw,regular,live_support
584 _unref_ls_fun_fw:
585 retq
587 .section __TEXT,__ref_ls_bw,regular,live_support
588 _ref_ls_fun_bw:
589 # This _calls_ something that's alive but isn't referenced itself. This is
590 # kept alive only due to this being in a live_support section.
591 callq _foo
593 # _bar on the other hand is kept alive since it's called from here.
594 callq _bar
595 retq
597 ## Kept alive by a live symbol form a dynamic library.
598 _ref_ls_dylib_fun:
599 callq _ref_dylib_fun
600 retq
602 ## Kept alive by a live undefined symbol.
603 _ref_ls_undef_fun:
604 callq _ref_undef_fun
605 retq
607 ## All symbols in this live_support section reference dead symbols
608 ## and are hence dead themselves.
609 .section __TEXT,__unref_ls_bw,regular,live_support
610 _unref_ls_fun_bw:
611 callq _unref
612 retq
614 _unref_ls_dylib_fun_bw:
615 callq _unref_dylib_fun
616 retq
618 _unref_ls_undef_fun_bw:
619 callq _unref_undef_fun
620 retq
622 .text
623 .globl _unref
624 _unref:
625 retq
627 .globl _bar
628 _bar:
629 retq
631 .globl _foo
632 _foo:
633 callq _ref_ls_fun_fw
634 retq
636 .globl _main
637 _main:
638 callq _ref_ls_fun_fw
639 callq _foo
640 callq _ref_dylib_fun
641 callq _ref_undef_fun
642 retq
644 .subsections_via_symbols
646 #--- live-support-iterations.s
647 .section __TEXT,_ls,regular,live_support
649 ## This is a live_support subsection that only becomes
650 ## live after _foo below is processed. This means the algorithm of
651 ## 1. mark things reachable from gc roots live
652 ## 2. go through live sections and mark the ones live pointing to
653 ## live symbols or sections
654 ## needs more than one iteration, since _bar won't be live when step 2
655 ## runs for the first time.
656 ## (ld64 gets this wrong -- it has different output based on if _bar is
657 ## before _foo or after it.)
658 _bar:
659 callq _foo_refd
660 callq _bar_refd
661 retq
663 ## Same here. This is maybe more interesting since it references a live_support
664 ## symbol instead of a "normal" symbol.
665 _baz:
666 callq _foo_refd
667 callq _baz_refd
668 retq
670 _foo:
671 callq _main
672 callq _foo_refd
673 retq
675 ## Test no_dead_strip on a symbol in a live_support section.
676 ## ld64 ignores this, but that doesn't look intentional. So lld honors it.
677 .no_dead_strip
678 _quux:
679 retq
682 .text
683 .globl _main
684 _main:
685 movq $0, %rax
686 retq
688 _foo_refd:
689 retq
691 _bar_refd:
692 retq
694 _baz_refd:
695 retq
697 .subsections_via_symbols
699 #--- unwind.s
700 ## This is the output of `clang -O2 -S throw.cc` where throw.cc
701 ## looks like this:
702 ## int unref() {
703 ## try {
704 ## throw 0;
705 ## } catch (int i) {
706 ## return i + 1;
707 ## }
708 ## }
709 ## int main() {
710 ## try {
711 ## throw 0;
712 ## } catch (int i) {
713 ## return i;
714 ## }
715 ## }
716 .section __TEXT,__text,regular,pure_instructions
717 .globl __Z5unrefv ## -- Begin function _Z5unrefv
718 .p2align 4, 0x90
719 __Z5unrefv: ## @_Z5unrefv
720 Lfunc_begin0:
721 .cfi_startproc
722 .cfi_personality 155, ___gxx_personality_v0
723 .cfi_lsda 16, Lexception0
724 ## %bb.0:
725 pushq %rbp
726 .cfi_def_cfa_offset 16
727 .cfi_offset %rbp, -16
728 movq %rsp, %rbp
729 .cfi_def_cfa_register %rbp
730 subq $16, %rsp
731 movl $4, %edi
732 callq ___cxa_allocate_exception
733 movl $0, (%rax)
734 Ltmp0:
735 movq __ZTIi@GOTPCREL(%rip), %rsi
736 movq %rax, %rdi
737 xorl %edx, %edx
738 callq ___cxa_throw
739 Ltmp1:
740 ## %bb.1:
742 LBB0_2:
743 Ltmp2:
744 leaq -4(%rbp), %rcx
745 movq %rax, %rdi
746 movl %edx, %esi
747 movq %rcx, %rdx
748 callq __Z5unrefv.cold.1
749 movl -4(%rbp), %eax
750 addq $16, %rsp
751 popq %rbp
752 retq
753 Lfunc_end0:
754 .cfi_endproc
755 .section __TEXT,__gcc_except_tab
756 .p2align 2
757 GCC_except_table0:
758 Lexception0:
759 .byte 255 ## @LPStart Encoding = omit
760 .byte 155 ## @TType Encoding = indirect pcrel sdata4
761 .uleb128 Lttbase0-Lttbaseref0
762 Lttbaseref0:
763 .byte 1 ## Call site Encoding = uleb128
764 .uleb128 Lcst_end0-Lcst_begin0
765 Lcst_begin0:
766 .uleb128 Lfunc_begin0-Lfunc_begin0 ## >> Call Site 1 <<
767 .uleb128 Ltmp0-Lfunc_begin0 ## Call between Lfunc_begin0 and Ltmp0
768 .byte 0 ## has no landing pad
769 .byte 0 ## On action: cleanup
770 .uleb128 Ltmp0-Lfunc_begin0 ## >> Call Site 2 <<
771 .uleb128 Ltmp1-Ltmp0 ## Call between Ltmp0 and Ltmp1
772 .uleb128 Ltmp2-Lfunc_begin0 ## jumps to Ltmp2
773 .byte 1 ## On action: 1
774 .uleb128 Ltmp1-Lfunc_begin0 ## >> Call Site 3 <<
775 .uleb128 Lfunc_end0-Ltmp1 ## Call between Ltmp1 and Lfunc_end0
776 .byte 0 ## has no landing pad
777 .byte 0 ## On action: cleanup
778 Lcst_end0:
779 .byte 1 ## >> Action Record 1 <<
780 ## Catch TypeInfo 1
781 .byte 0 ## No further actions
782 .p2align 2
783 ## >> Catch TypeInfos <<
784 .long __ZTIi@GOTPCREL+4 ## TypeInfo 1
785 Lttbase0:
786 .p2align 2
787 ## -- End function
788 .section __TEXT,__text,regular,pure_instructions
789 .globl _main ## -- Begin function main
790 .p2align 4, 0x90
791 _main: ## @main
792 Lfunc_begin1:
793 .cfi_startproc
794 .cfi_personality 155, ___gxx_personality_v0
795 .cfi_lsda 16, Lexception1
796 ## %bb.0:
797 pushq %rbp
798 .cfi_def_cfa_offset 16
799 .cfi_offset %rbp, -16
800 movq %rsp, %rbp
801 .cfi_def_cfa_register %rbp
802 pushq %rbx
803 pushq %rax
804 .cfi_offset %rbx, -24
805 movl $4, %edi
806 callq ___cxa_allocate_exception
807 movl $0, (%rax)
808 Ltmp3:
809 movq __ZTIi@GOTPCREL(%rip), %rsi
810 movq %rax, %rdi
811 xorl %edx, %edx
812 callq ___cxa_throw
813 Ltmp4:
814 ## %bb.1:
816 LBB1_2:
817 Ltmp5:
818 movq %rax, %rdi
819 callq ___cxa_begin_catch
820 movl (%rax), %ebx
821 callq ___cxa_end_catch
822 movl %ebx, %eax
823 addq $8, %rsp
824 popq %rbx
825 popq %rbp
826 retq
827 Lfunc_end1:
828 .cfi_endproc
829 .section __TEXT,__gcc_except_tab
830 .p2align 2
831 GCC_except_table1:
832 Lexception1:
833 .byte 255 ## @LPStart Encoding = omit
834 .byte 155 ## @TType Encoding = indirect pcrel sdata4
835 .uleb128 Lttbase1-Lttbaseref1
836 Lttbaseref1:
837 .byte 1 ## Call site Encoding = uleb128
838 .uleb128 Lcst_end1-Lcst_begin1
839 Lcst_begin1:
840 .uleb128 Lfunc_begin1-Lfunc_begin1 ## >> Call Site 1 <<
841 .uleb128 Ltmp3-Lfunc_begin1 ## Call between Lfunc_begin1 and Ltmp3
842 .byte 0 ## has no landing pad
843 .byte 0 ## On action: cleanup
844 .uleb128 Ltmp3-Lfunc_begin1 ## >> Call Site 2 <<
845 .uleb128 Ltmp4-Ltmp3 ## Call between Ltmp3 and Ltmp4
846 .uleb128 Ltmp5-Lfunc_begin1 ## jumps to Ltmp5
847 .byte 1 ## On action: 1
848 .uleb128 Ltmp4-Lfunc_begin1 ## >> Call Site 3 <<
849 .uleb128 Lfunc_end1-Ltmp4 ## Call between Ltmp4 and Lfunc_end1
850 .byte 0 ## has no landing pad
851 .byte 0 ## On action: cleanup
852 Lcst_end1:
853 .byte 1 ## >> Action Record 1 <<
854 ## Catch TypeInfo 1
855 .byte 0 ## No further actions
856 .p2align 2
857 ## >> Catch TypeInfos <<
858 .long __ZTIi@GOTPCREL+4 ## TypeInfo 1
859 Lttbase1:
860 .p2align 2
861 ## -- End function
862 .section __TEXT,__text,regular,pure_instructions
863 .p2align 4, 0x90 ## -- Begin function _Z5unrefv.cold.1
864 __Z5unrefv.cold.1: ## @_Z5unrefv.cold.1
865 .cfi_startproc
866 ## %bb.0:
867 pushq %rbp
868 .cfi_def_cfa_offset 16
869 .cfi_offset %rbp, -16
870 movq %rsp, %rbp
871 .cfi_def_cfa_register %rbp
872 pushq %rbx
873 pushq %rax
874 .cfi_offset %rbx, -24
875 movq %rdx, %rbx
876 callq ___cxa_begin_catch
877 movl (%rax), %eax
878 incl %eax
879 movl %eax, (%rbx)
880 addq $8, %rsp
881 popq %rbx
882 popq %rbp
883 jmp ___cxa_end_catch ## TAILCALL
884 .cfi_endproc
885 ## -- End function
886 .subsections_via_symbols
888 #--- weak-ref.s
889 .text
890 .weak_reference _ref_dylib_fun
891 .globl _main
892 _main:
893 callq _ref_dylib_fun
894 retq
896 .subsections_via_symbols
898 #--- strong-dead-ref.s
899 .text
900 .globl _unref_dylib_fun
901 _unref:
902 callq _unref_dylib_fun
903 retq
905 .subsections_via_symbols
907 #--- weak-dylib.s
908 .text
909 .globl _weak_in_dylib
910 .weak_definition _weak_in_dylib
911 _weak_in_dylib:
912 retq
914 .subsections_via_symbols
916 #--- dead-weak-override.s
918 ## Overrides the _weak_in_dylib symbol in weak-dylib, but is dead stripped.
919 .text
921 #.no_dead_strip _weak_in_dylib
922 .globl _weak_in_dylib
923 _weak_in_dylib:
924 retq
926 .globl _main
927 _main:
928 retq
930 .subsections_via_symbols
932 #--- debug.s
933 .text
934 .globl _unref
935 _unref:
936 retq
938 .globl _main
939 _main:
940 retq
942 .subsections_via_symbols
944 #--- no-dead-symbols.s
945 .text
946 .globl _main
947 _main:
948 retq
950 #--- literals.s
951 .cstring
952 _unref_foo:
953 .ascii "foo"
954 _bar:
955 Lbar:
956 .asciz "bar"
957 _unref_baz:
958 .asciz "baz"
960 .literal4
961 .p2align 2
962 L._foo4:
963 .long 0xdeadbeef
964 L._bar4:
965 .long 0xdeadbeef
966 L._unref:
967 .long 0xfeedface
969 .section __DATA,str_ptrs,literal_pointers
970 .globl _data
971 _data:
972 .quad _bar
973 .quad Lbar
975 ## The output binary has these integer literals put into a section that isn't
976 ## marked with a S_*BYTE_LITERALS flag, so we don't mark word_ptrs with the
977 ## S_LITERAL_POINTERS flag in order not to confuse llvm-objdump.
978 .section __DATA,word_ptrs
979 .globl _more_data
980 _more_data:
981 .quad L._foo4
982 .quad L._bar4
984 .subsections_via_symbols
986 #--- ref-undef.s
987 .globl _main
988 _main:
989 callq _ref_undef_fun
990 .subsections_via_symbols