1# REQUIRES: x86
2# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
3# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/keep.s -o %t2.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 %t1 --script %t.script %t
11# RUN: llvm-objdump -section-headers %t1 | \
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 %t1 --script %t.script %t
25# RUN: llvm-objdump -section-headers %t1 | \
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 %t1 --script %t.script %t
42# RUN: llvm-objdump -section-headers %t1 | 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 .text         00000007 00000000000000ec TEXT DATA
48# MIXED1-NEXT:   3 .temp         00000004 00000000000000f3 DATA
49# MIXED1-NEXT:   4 .symtab       00000060 0000000000000000
50# MIXED1-NEXT:   5 .shstrtab     0000002d 0000000000000000
51# MIXED1-NEXT:   6 .strtab       00000012 0000000000000000
52
53## The same, but now section without KEEP is at first place.
54## gold and bfd linkers disagree here. gold collects .keep while
55## bfd keeps it. Our current behavior is compatible with bfd although
56## we can choose either way.
57# RUN: echo "SECTIONS { \
58# RUN:  . = SIZEOF_HEADERS; \
59# RUN:  .nokeep : { *(.keep) } \
60# RUN:  .keep : { KEEP(*(.keep)) }}" > %t.script
61# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t
62# RUN: llvm-objdump -section-headers %t1 | FileCheck -check-prefix=MIXED2 %s
63# MIXED2:      Sections:
64# MIXED2-NEXT: Idx Name          Size
65# MIXED2-NEXT:   0               00000000
66# MIXED2-NEXT:   1 .nokeep       00000004 00000000000000e8 DATA
67# MIXED2-NEXT:   2 .text         00000007 00000000000000ec TEXT DATA
68# MIXED2-NEXT:   3 .temp         00000004 00000000000000f3 DATA
69# MIXED2-NEXT:   4 .symtab       00000060 0000000000000000
70# MIXED2-NEXT:   5 .shstrtab     0000002f 0000000000000000
71# MIXED2-NEXT:   6 .strtab       00000012 0000000000000000
72
73# Check file pattern for kept sections.
74# RUN: echo "SECTIONS { \
75# RUN:  . = SIZEOF_HEADERS; \
76# RUN:  .keep : { KEEP(*2.o(.keep)) } \
77# RUN:  }" > %t.script
78# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t2.o %t
79# RUN: llvm-objdump -s %t1 | FileCheck -check-prefix=FILEMATCH %s
80# FILEMATCH:        Contents of section .keep:
81# FILEMATCH-NEXT:   00e8 41414141  AAAA
82
83.global _start
84_start:
85 mov temp, %eax
86
87.section .keep, "a"
88keep:
89 .long 1
90
91.section .temp, "a"
92temp:
93 .long 2
94