1# REQUIRES: x86
2# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
3# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/keep.s -o %t1.o
4
5## First check that section "keep" is garbage collected without using KEEP
6# RUN: echo "SECTIONS { \
7# RUN:  .text : { *(.text) } \
8# RUN:  .keep : { *(.keep) } \
9# RUN:  .temp : { *(.temp) }}" > %t.script
10# RUN: ld.lld --gc-sections -o %t --script %t.script %t.o
11# RUN: llvm-objdump --section-headers %t | \
12# RUN:   FileCheck -check-prefix=SECGC %s
13# SECGC:      Sections:
14# SECGC-NEXT: Idx Name          Size
15# SECGC-NEXT:   0               00000000
16# SECGC-NEXT:   1 .text         00000007
17# SECGC-NEXT:   2 .temp         00000004
18
19## Now apply KEEP command to preserve the section.
20# RUN: echo "SECTIONS { \
21# RUN:  .text : { *(.text) } \
22# RUN:  .keep : { KEEP(*(.keep)) } \
23# RUN:  .temp : { *(.temp) }}" > %t.script
24# RUN: ld.lld --gc-sections -o %t --script %t.script %t.o
25# RUN: llvm-objdump --section-headers %t | \
26# RUN:   FileCheck -check-prefix=SECNOGC %s
27# SECNOGC:      Sections:
28# SECNOGC-NEXT: Idx Name          Size
29# SECNOGC-NEXT:   0               00000000
30# SECNOGC-NEXT:   1 .text         00000007
31# SECNOGC-NEXT:   2 .keep         00000004
32# SECNOGC-NEXT:   3 .temp         00000004
33
34## A section name matches two entries in the SECTIONS directive. The
35## first one doesn't have KEEP, the second one does. If section that have
36## KEEP is the first in order then section is NOT collected.
37# RUN: echo "SECTIONS { \
38# RUN:  . = SIZEOF_HEADERS; \
39# RUN:  .keep : { KEEP(*(.keep)) } \
40# RUN:  .nokeep : { *(.keep) }}" > %t.script
41# RUN: ld.lld --gc-sections -o %t --script %t.script %t.o
42# RUN: llvm-objdump --section-headers %t | FileCheck --check-prefix=MIXED1 %s
43# MIXED1:      Sections:
44# MIXED1-NEXT: Idx Name          Size
45# MIXED1-NEXT:   0               00000000
46# MIXED1-NEXT:   1 .keep         00000004
47# MIXED1-NEXT:   2 .temp         00000004 0000000000000124
48# MIXED1-NEXT:   3 .text         00000007 0000000000000128
49# MIXED1-NEXT:   4 .comment      00000008 0000000000000000
50# MIXED1-NEXT:   5 .symtab       00000060 0000000000000000
51# MIXED1-NEXT:   6 .shstrtab     00000036 0000000000000000
52# MIXED1-NEXT:   7 .strtab       00000012 0000000000000000
53
54## The same, but now section without KEEP is at first place.
55## gold and bfd linkers disagree here. gold collects .keep while
56## bfd keeps it. Our current behavior is compatible with bfd although
57## we can choose either way.
58# RUN: echo "SECTIONS { \
59# RUN:  . = SIZEOF_HEADERS; \
60# RUN:  .nokeep : { *(.keep) } \
61# RUN:  .keep : { KEEP(*(.keep)) }}" > %t.script
62# RUN: ld.lld --gc-sections -o %t --script %t.script %t.o
63# RUN: llvm-objdump --section-headers %t | FileCheck --check-prefix=MIXED2 %s
64# MIXED2:      Sections:
65# MIXED2-NEXT: Idx Name          Size
66# MIXED2-NEXT:   0               00000000
67# MIXED2-NEXT:   1 .nokeep       00000004 0000000000000120
68# MIXED2-NEXT:   2 .temp         00000004 0000000000000124
69# MIXED2-NEXT:   3 .text         00000007 0000000000000128
70# MIXED2-NEXT:   4 .comment      00000008 0000000000000000
71# MIXED2-NEXT:   5 .symtab       00000060 0000000000000000
72# MIXED2-NEXT:   6 .shstrtab     00000038 0000000000000000
73# MIXED2-NEXT:   7 .strtab       00000012 0000000000000000
74
75# Check file pattern for kept sections.
76# RUN: echo "SECTIONS { \
77# RUN:  . = SIZEOF_HEADERS; \
78# RUN:  .keep : { KEEP(*1.o(.keep)) } \
79# RUN:  }" > %t.script
80# RUN: ld.lld --gc-sections -o %t --script %t.script %t1.o %t.o
81# RUN: llvm-objdump -s %t | FileCheck --check-prefix=FILEMATCH %s
82# FILEMATCH:        Contents of section .keep:
83# FILEMATCH-NEXT:   0120 41414141  AAAA
84
85.global _start
86_start:
87 mov temp, %eax
88
89.section .keep, "a"
90keep:
91 .long 1
92
93.section .temp, "a"
94temp:
95 .long 2
96