xref: /llvm-project/lld/test/ELF/relocatable-gc.s (revision 6d2d3bd0a61f5fc7fd9f61f48bc30e9ca77cc619)
1# REQUIRES: x86
2## Test -r --gc-sections.
3
4# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
5
6## By default all regular sections are discarded. We currently don't track
7## usage of group signature symbols and will retain them and their associated
8## STT_SECTION symbols.
9# RUN: ld.lld -r --gc-sections --print-gc-sections %t.o -o %t
10# RUN: llvm-readelf -S -s %t | FileCheck %s
11
12# CHECK:      [ 1] .group
13# CHECK-NEXT: [ 2] .note.GNU-stack
14
15# CHECK:      Symbol table '.symtab' contains 3 entries:
16# CHECK-NEXT: Num:
17# CHECK-NEXT: 0:
18# CHECK-NEXT: 1: {{.*}} NOTYPE  LOCAL DEFAULT  1 group
19# CHECK-NEXT: 2: {{.*}} SECTION LOCAL DEFAULT  1
20
21## -u keeps .text.bar alive. Other group members are kept alive as well.
22# RUN: ld.lld -r --gc-sections -u bar %t.o -o - | llvm-readelf -Ss - | \
23# RUN:   FileCheck %s --check-prefix=KEEP_GROUP
24## -e, --init and --fini are similar.
25# RUN: ld.lld -r --gc-sections -e bar %t.o -o - | llvm-readelf -Ss - | \
26# RUN:   FileCheck %s --check-prefix=KEEP_GROUP
27# RUN: ld.lld -r --gc-sections --init=bar %t.o -o - | llvm-readelf -Ss - | \
28# RUN:   FileCheck %s --check-prefix=KEEP_GROUP
29# RUN: ld.lld -r --gc-sections --fini=bar %t.o -o - | llvm-readelf -Ss - | \
30# RUN:   FileCheck %s --check-prefix=KEEP_GROUP
31
32# KEEP_GROUP:      [ 1] .group
33# KEEP_GROUP-NEXT: [ 2] .text.bar
34# KEEP_GROUP-NEXT: [ 3] .text.foo
35# KEEP_GROUP-NEXT: [ 4] .note.GNU-stack
36
37# KEEP_GROUP:      Symbol table '.symtab' contains 7 entries:
38# KEEP_GROUP:      4: {{.*}} SECTION
39# KEEP_GROUP-NEXT: 5: {{.*}}   2 bar
40# KEEP_GROUP-NEXT: 6: {{.*}}   3 foo
41
42## If .text is retained, its referenced qux and .fred are retained as well.
43## fred_und is used (by .fred) and thus emitted.
44## Note, GNU ld does not retain qux.
45# RUN: ld.lld -r --gc-sections -z nostart-stop-gc -e _start %t.o -o %tstart.ro
46# RUN: llvm-readelf -Ss %tstart.ro | FileCheck %s --check-prefix=KEEP_START
47
48# KEEP_START:      [ 1] .text
49# KEEP_START-NEXT: [ 2] .rela.text
50# KEEP_START-NEXT: [ 3] qux
51# KEEP_START-NEXT: [ 4] .group
52# KEEP_START-NEXT: [ 5] .fred
53# KEEP_START-NEXT: [ 6] .rela.fred
54# KEEP_START-NEXT: [ 7] .note.GNU-stack
55
56# KEEP_START:      Symbol table '.symtab' contains 10 entries:
57# KEEP_START:      5: {{.*}} SECTION
58# KEEP_START-NEXT: 6: {{.*}}   1 _start
59# KEEP_START-NEXT: 7: {{.*}}   5 fred
60# KEEP_START-NEXT: 8: {{.*}} UND __start_qux
61# KEEP_START-NEXT: 9: {{.*}} UND fred_und
62
63.section qux,"a",@progbits
64  .byte 0
65
66.text
67.globl _start, bar, foo, fred
68_start:
69  call fred
70  .quad __start_qux
71
72.section .text.bar,"axG",@progbits,group,comdat
73bar:
74  .byte 1
75.section .text.foo,"axG",@progbits,group,comdat
76foo:
77  .byte 2
78.section .fred,"ax",@progbits
79fred:
80  call fred_und
81