xref: /llvm-project/lld/test/MachO/weak-definition-gc.s (revision ac20a3a5b530fc86cb85a4091e90be10bb28650b)
1# REQUIRES: x86
2# RUN: rm -rf %t; split-file %s %t
3
4## This tests that if two input files define the same weak symbol, we only
5## write it to the output once (...assuming both input files use
6## .subsections_via_symbols).
7
8# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/weak-sub.s -o %t/weak-sub.o
9# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/weak-nosub.s -o %t/weak-nosub.o
10
11## Test that weak symbols are emitted just once with .subsections_via_symbols
12# RUN: %lld -dylib -o %t/out.dylib %t/weak-sub.o %t/weak-sub.o
13# RUN: llvm-otool -jtV %t/out.dylib | FileCheck --check-prefix=SUB %s
14# RUN: %lld -dylib -o %t/out.dylib %t/weak-nosub.o %t/weak-sub.o
15# RUN: llvm-otool -jtV %t/out.dylib | FileCheck --check-prefix=SUB %s
16# RUN: %lld -dylib -o %t/out.dylib %t/weak-sub.o %t/weak-nosub.o
17# RUN: llvm-otool -jtV %t/out.dylib | FileCheck --check-prefix=SUB %s
18# SUB:      _foo
19# SUB-NEXT: retq
20# SUB-NOT:  retq
21# SUB:      _bar
22# SUB-NEXT: retq
23# SUB-NOT:  retq
24
25## We can even strip weak symbols without subsections_via_symbols as long
26## as none of the weak symbols in a section are needed.
27# RUN: %lld -dylib -o %t/out.dylib %t/weak-nosub.o %t/weak-nosub.o
28# RUN: llvm-otool -jtV %t/out.dylib | FileCheck --check-prefix=SUB %s
29
30## Test that omitted weak symbols don't add entries to the compact unwind table.
31# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 %t/weak-sub-lsda.s -o %t/weak-sub-lsda.o
32# RUN: %lld -dylib -lc++ -o %t/out.dylib %t/weak-sub-lsda.o %t/weak-sub-lsda.o
33# RUN: llvm-objdump --macho --unwind-info --syms %t/out.dylib | FileCheck %s --check-prefix=ONE-UNWIND
34# RUN: %lld -dylib -lc++ -o %t/out.dylib %t/weak-sub.o %t/weak-sub-lsda.o
35# RUN: llvm-objdump --macho --unwind-info --syms %t/out.dylib | FileCheck %s --check-prefix=NO-UNWIND
36# RUN: yaml2obj %t/weak-sub-lsda-r.yaml -o %t/weak-sub-lsda-r.o
37# RUN: %lld -dylib -lc++ -o %t/out.dylib %t/weak-sub.o %t/weak-sub-lsda-r.o
38# RUN: llvm-objdump --macho --unwind-info --syms %t/out.dylib | FileCheck %s --check-prefix=NO-UNWIND
39
40# ONE-UNWIND:      SYMBOL TABLE:
41# ONE-UNWIND-DAG:  [[#%x,FOO:]]       w  F __TEXT,__text _foo
42# ONE-UNWIND-NOT:                          __TEXT,__text _foo
43
44# ONE-UNWIND:      Contents of __unwind_info section:
45# ONE-UNWIND:        LSDA descriptors:
46# ONE-UNWIND:           [0]: function offset=0x[[#%.8x,FOO]]
47# ONE-UNWIND-NOT:       [1]:
48# ONE-UNWIND:        Second level indices:
49# ONE-UNWIND-DAG:      [0]: function offset=0x[[#%.8x,FOO]]
50# ONE-UNWIND-NOT:      [1]:
51
52# NO-UNWIND:      SYMBOL TABLE:
53# NO-UNWIND-DAG:  [[#%x,FOO:]]       w  F __TEXT,__text _foo
54# NO-UNWIND-NOT:                          __TEXT,__text _foo
55# NO-UNWIND-NOT:  Contents of __unwind_info section:
56
57## Test interaction with .alt_entry
58## FIXME: ld64 manages to strip both one copy of _foo and _bar each.
59##        We only manage this if we're lucky and the object files are in
60##        the right order. We're happy to not crash at link time for now.
61# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 %t/weak-sub-alt.s -o %t/weak-sub-alt.o
62# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 %t/weak-sub-alt2.s -o %t/weak-sub-alt2.o
63# RUN: %lld -dylib -o %t/out.dylib %t/weak-sub-alt.o %t/weak-sub-alt2.o
64# RUN: %lld -dylib -o %t/out.dylib %t/weak-sub-alt2.o %t/weak-sub-alt.o
65
66# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 %t/weak-aligned-1.s -o %t/weak-aligned-1.o
67# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 %t/weak-aligned-2.s -o %t/weak-aligned-2.o
68# RUN: %lld -o %t/out -lSystem %t/weak-aligned-1.o %t/weak-aligned-2.o
69# RUN: llvm-objdump --syms --section=__const --full-contents %t/out | FileCheck --check-prefixes=ALIGN,ALIGN2 %s
70# RUN: %lld -o %t/out -lSystem %t/weak-aligned-1.o %t/weak-aligned-2.o -dead_strip
71# RUN: llvm-objdump --syms --section=__const --full-contents %t/out | FileCheck --check-prefixes=ALIGN,ALIGN3 %s
72# ALIGN:       SYMBOL TABLE:
73# ALIGN-DAG:   [[#%x, ADDR:]]       l     O __DATA_CONST,__const .hidden _weak1
74# ALIGN2-DAG:  {{0*}}[[#ADDR+ 0x4]] l     O __DATA_CONST,__const .hidden _weak3
75# ALIGN3-DAG:  {{0*}}[[#ADDR+ 0x4]] l     O __DATA_CONST,__const .hidden _weak2
76# ALIGN2-DAG:  {{0*}}[[#ADDR+ 0x8]] l     O __DATA_CONST,__const .hidden _weak2
77# ALIGN-DAG:   {{0*}}[[#ADDR+0x10]] g     O __DATA_CONST,__const _aligned
78# ALIGN:       Contents of section __DATA_CONST,__const:
79# ALIGN2-NEXT: {{0*}}[[#ADDR]]      11111111 33333333 22222222 00000000
80# ALIGN3-NEXT: {{0*}}[[#ADDR]]      11111111 22222222 00000000 00000000
81# ALIGN-NEXT:  {{0*}}[[#ADDR+0x10]] 81818181 81818181 82828282 82828282
82
83# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 %t/weak-def.s -o %t/weak-def.o
84# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 %t/strong-def.s -o %t/strong-def.o
85# RUN: %lld -dylib -lc++ -o %t/weak-strong-mixed.dylib %t/weak-def.o %t/strong-def.o
86# RUN: %lld -dylib -lc++ -o %t/strong-weak-mixed.dylib %t/strong-def.o %t/weak-def.o
87## Check that omitted weak symbols are not adding their section and unwind stuff.
88
89# RUN: llvm-otool -jtV %t/weak-strong-mixed.dylib | FileCheck --check-prefix=MIXED %s
90# RUN: llvm-otool -jtV %t/strong-weak-mixed.dylib | FileCheck --check-prefix=MIXED %s
91# MIXED: (__TEXT,__text) section
92# MIXED-NEXT: _foo:
93# MIXED-NEXT: {{.+}} 	33 33            	xorl	(%rbx), %esi
94# MIXED-NEXT: {{.+}} 	33 33            	xorl	(%rbx), %esi
95# MIXED-NEXT: {{.+}}	c3              	retq
96
97# RUN: llvm-objdump --macho --syms --unwind-info %t/weak-strong-mixed.dylib | FileCheck --check-prefix=MIXED-UNWIND %s
98# RUN: llvm-objdump --macho --syms --unwind-info %t/strong-weak-mixed.dylib | FileCheck --check-prefix=MIXED-UNWIND %s
99# MIXED-UNWIND: g     F __TEXT,__text _foo
100# MIXED-UNWIND-NOT: Contents of __unwind_info section:
101
102#--- weak-sub.s
103.globl _foo, _bar
104.weak_definition _foo, _bar
105_foo:
106  retq
107_bar:
108  retq
109.subsections_via_symbols
110
111#--- weak-nosub.s
112.globl _foo, _bar
113.weak_definition _foo, _bar
114_foo:
115  retq
116_bar:
117  retq
118
119#--- weak-sub-lsda.s
120.section __TEXT,__text,regular,pure_instructions
121
122.globl _foo
123.weak_definition _foo
124_foo:
125  .cfi_startproc
126  .cfi_personality 155, ___gxx_personality_v0
127  .cfi_lsda 16, Lexception
128  pushq %rbp
129  .cfi_def_cfa_offset 128
130  .cfi_offset %rbp, 48
131  movq %rsp, %rbp
132  .cfi_def_cfa_register %rbp
133  popq %rbp
134  retq
135  .cfi_endproc
136
137.section __TEXT,__gcc_except_tab
138Lexception:
139    .space 0x10
140
141.subsections_via_symbols
142
143#--- weak-sub-alt.s
144.globl _foo, _bar
145.weak_definition _foo
146_foo:
147  retq
148
149# Alternative entry point to _foo (strong)
150.alt_entry _bar
151_bar:
152  retq
153
154.globl _main, _ref
155_main:
156  callq _ref
157  callq _bar
158
159.subsections_via_symbols
160
161#--- weak-sub-alt2.s
162.globl _foo, _bar
163.weak_definition _foo
164_foo:
165  retq
166
167# Alternative entry point to _foo (weak)
168.weak_definition _bar
169.alt_entry _bar
170_bar:
171  retq
172
173.globl _ref
174_ref:
175  callq _bar
176
177.subsections_via_symbols
178
179#--- weak-aligned-1.s
180.section __DATA,__const
181.p2align 3
182.globl _weak1
183.weak_def_can_be_hidden _weak1
184_weak1:
185  .4byte 0x11111111
186
187.globl _weak3
188.weak_def_can_be_hidden _weak3
189_weak3:
190  .4byte 0x33333333
191
192.subsections_via_symbols
193
194#--- weak-aligned-2.s
195# _weak1 and _weak3 are already in weak-aligned-1,
196# so from _weak1-3 in this file only _weak2 is used.
197# However, _aligned still has to stay aligned to a 16-byte boundary.
198.section __DATA,__const
199.p2align 3
200.globl _weak1
201.weak_def_can_be_hidden _weak1
202_weak1:
203  .4byte 0x11111111
204
205.globl _weak2
206.weak_def_can_be_hidden _weak2
207_weak2:
208  .4byte 0x22222222
209
210.globl _weak3
211.weak_def_can_be_hidden _weak3
212_weak3:
213  .4byte 0x33333333
214
215.section __DATA,__const
216.p2align 4
217.globl _aligned
218_aligned:
219  .8byte 0x8181818181818181
220  .8byte 0x8282828282828282
221
222.section __TEXT,__text
223.globl _main
224_main:
225  movl _weak1(%rip), %eax
226  movl _weak2(%rip), %ebx
227  movaps _aligned(%rip), %xmm0
228  retq
229
230.subsections_via_symbols
231
232#--- weak-def.s
233.section __TEXT,__text,regular,pure_instructions
234
235.globl _foo
236.weak_definition _foo
237_foo:
238  .cfi_startproc
239  .cfi_personality 155, ___gxx_personality_v0
240  .cfi_lsda 16, Lexception
241  pushq %rbp
242  .cfi_def_cfa_offset 128
243  .cfi_offset %rbp, 48
244  movq %rsp, %rbp
245  .cfi_def_cfa_register %rbp
246  popq %rbp
247  retq
248  .cfi_endproc
249
250.section __TEXT,__gcc_except_tab
251Lexception:
252    .space 0x10
253
254.subsections_via_symbols
255#--- strong-def.s
256.globl _foo, _bar
257
258_foo:
259  .4byte 0x33333333
260  retq
261
262.subsections_via_symbols
263
264#--- weak-sub-lsda-r.yaml
265## This was generated from compiling weak-sub-lsda.s above at rev a2404f11c77e
266## and then running it through `ld -r`. This converts a number of unwind-related
267## relocations from section- to symbol-based ones.
268--- !mach-o
269FileHeader:
270  magic:           0xFEEDFACF
271  cputype:         0x1000007
272  cpusubtype:      0x3
273  filetype:        0x1
274  ncmds:           2
275  sizeofcmds:      464
276  flags:           0x2000
277  reserved:        0x0
278LoadCommands:
279  - cmd:             LC_SEGMENT_64
280    cmdsize:         392
281    segname:         ''
282    vmaddr:          0
283    vmsize:          152
284    fileoff:         528
285    filesize:        152
286    maxprot:         7
287    initprot:        7
288    nsects:          3
289    flags:           0
290    Sections:
291      - sectname:        __text
292        segname:         __TEXT
293        addr:            0x0
294        size:            6
295        offset:          0x210
296        align:           0
297        reloff:          0x0
298        nreloc:          0
299        flags:           0x80000400
300        reserved1:       0x0
301        reserved2:       0x0
302        reserved3:       0x0
303        content:         554889E55DC3
304      - sectname:        __gcc_except_tab
305        segname:         __TEXT
306        addr:            0x6
307        size:            32
308        offset:          0x216
309        align:           0
310        reloff:          0x0
311        nreloc:          0
312        flags:           0x0
313        reserved1:       0x0
314        reserved2:       0x0
315        reserved3:       0x0
316        content:         '0000000000000000000000000000000000000000000000000000000000000000'
317      - sectname:        __eh_frame
318        segname:         __TEXT
319        addr:            0x28
320        size:            80
321        offset:          0x238
322        align:           3
323        reloff:          0x2A8
324        nreloc:          7
325        flags:           0x0
326        reserved1:       0x0
327        reserved2:       0x0
328        reserved3:       0x0
329        content:         1C00000000000000017A504C5200017810079B0400000010100C0708900100002C00000004000000F8FFFFFFFFFFFFFF060000000000000008E7FFFFFFFFFFFFFF410E800111067A430D060000000000
330        relocations:
331          - address:         0x13
332            symbolnum:       5
333            pcrel:           true
334            length:          2
335            extern:          true
336            type:            4
337            scattered:       false
338            value:           0
339          - address:         0x24
340            symbolnum:       2
341            pcrel:           false
342            length:          2
343            extern:          true
344            type:            5
345            scattered:       false
346            value:           0
347          - address:         0x24
348            symbolnum:       3
349            pcrel:           false
350            length:          2
351            extern:          true
352            type:            0
353            scattered:       false
354            value:           0
355          - address:         0x28
356            symbolnum:       3
357            pcrel:           false
358            length:          3
359            extern:          true
360            type:            5
361            scattered:       false
362            value:           0
363          - address:         0x28
364            symbolnum:       4
365            pcrel:           false
366            length:          3
367            extern:          true
368            type:            0
369            scattered:       false
370            value:           0
371          - address:         0x39
372            symbolnum:       3
373            pcrel:           false
374            length:          3
375            extern:          true
376            type:            5
377            scattered:       false
378            value:           0
379          - address:         0x39
380            symbolnum:       1
381            pcrel:           false
382            length:          3
383            extern:          true
384            type:            0
385            scattered:       false
386            value:           0
387      - sectname:        __compact_unwind
388        segname:         __LD
389        addr:            0x78
390        size:            32
391        offset:          0x288
392        align:           3
393        reloff:          0x2E0
394        nreloc:          4
395        flags:           0x2000000
396        reserved1:       0x0
397        reserved2:       0x0
398        reserved3:       0x0
399        content:         '0000000000000000060000000000004100000000000000000000000000000000'
400        relocations:
401          - address:         0x0
402            symbolnum:       4
403            pcrel:           false
404            length:          3
405            extern:          true
406            type:            0
407            scattered:       false
408            value:           0
409          - address:         0x18
410            symbolnum:       1
411            pcrel:           false
412            length:          3
413            extern:          true
414            type:            0
415            scattered:       false
416            value:           0
417          - address:         0x10
418            symbolnum:       5
419            pcrel:           false
420            length:          3
421            extern:          true
422            type:            0
423            scattered:       false
424            value:           0
425          - address:         0x18
426            symbolnum:       1
427            pcrel:           false
428            length:          3
429            extern:          true
430            type:            0
431            scattered:       false
432            value:           0
433  - cmd:             LC_SYMTAB
434    cmdsize:         24
435    symoff:          768
436    nsyms:           6
437    stroff:          864
438    strsize:         57
439LinkEditData:
440  NameList:
441    - n_strx:          29
442      n_type:          0xE
443      n_sect:          2
444      n_desc:          0
445      n_value:         6
446    - n_strx:          34
447      n_type:          0xE
448      n_sect:          2
449      n_desc:          0
450      n_value:         22
451    - n_strx:          39
452      n_type:          0xE
453      n_sect:          3
454      n_desc:          0
455      n_value:         40
456    - n_strx:          49
457      n_type:          0xE
458      n_sect:          3
459      n_desc:          0
460      n_value:         72
461    - n_strx:          2
462      n_type:          0xF
463      n_sect:          1
464      n_desc:          128
465      n_value:         0
466    - n_strx:          7
467      n_type:          0x1
468      n_sect:          0
469      n_desc:          0
470      n_value:         0
471  StringTable:
472    - ' '
473    - _foo
474    - ___gxx_personality_v0
475    - l001
476    - l002
477    - EH_Frame1
478    - func.eh
479...
480