1# REQUIRES: x86
2# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
3# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
4# RUN:   %p/Inputs/sort.s -o %t2.o
5
6# RUN: echo "SECTIONS { .aaa : { *(.aaa.*) } }" > %t1.script
7# RUN: ld.lld -o %t1 --script %t1.script %t2.o %t1.o
8# RUN: llvm-objdump -s %t1 | FileCheck -check-prefix=UNSORTED %s
9# UNSORTED:       Contents of section .aaa:
10# UNSORTED-NEXT:   55000000 00000000 00000000 00000000
11# UNSORTED-NEXT:   00000000 00000000 00000000 00000000
12# UNSORTED-NEXT:   11000000 00000000 33000000 00000000
13# UNSORTED-NEXT:   22000000 00000000 44000000 00000000
14# UNSORTED-NEXT:   05000000 00000000 01000000 00000000
15# UNSORTED-NEXT:   03000000 00000000 02000000 00000000
16# UNSORTED-NEXT:   04000000 00000000
17
18## Check that SORT works (sorted by name of section).
19# RUN: echo "SECTIONS { .aaa : { *(SORT(.aaa.*)) } }" > %t2.script
20# RUN: ld.lld -o %t2 --script %t2.script %t2.o %t1.o
21# RUN: llvm-objdump -s %t2 | FileCheck -check-prefix=SORTED_A %s
22# SORTED_A:      Contents of section .aaa:
23# SORTED_A-NEXT:  11000000 00000000 01000000 00000000
24# SORTED_A-NEXT:  22000000 00000000 02000000 00000000
25# SORTED_A-NEXT:  33000000 00000000 03000000 00000000
26# SORTED_A-NEXT:  44000000 00000000 00000000 00000000
27# SORTED_A-NEXT:  04000000 00000000 55000000 00000000
28# SORTED_A-NEXT:  00000000 00000000 00000000 00000000
29# SORTED_A-NEXT:  05000000 00000000
30
31## When we switch the order of files, check that sorting by
32## section names is stable.
33# RUN: echo "SECTIONS { .aaa : { *(SORT(.aaa.*)) } }" > %t3.script
34# RUN: ld.lld -o %t3 --script %t3.script %t1.o %t2.o
35# RUN: llvm-objdump -s %t3 | FileCheck -check-prefix=SORTED_B %s
36# SORTED_B:      Contents of section .aaa:
37# SORTED_B-NEXT:  01000000 00000000 00000000 00000000
38# SORTED_B-NEXT:  00000000 00000000 00000000 00000000
39# SORTED_B-NEXT:  11000000 00000000 02000000 00000000
40# SORTED_B-NEXT:  22000000 00000000 03000000 00000000
41# SORTED_B-NEXT:  33000000 00000000 00000000 00000000
42# SORTED_B-NEXT:  04000000 00000000 44000000 00000000
43# SORTED_B-NEXT:  05000000 00000000 55000000 00000000
44
45## Check that SORT surrounded with KEEP also works.
46# RUN: echo "SECTIONS { .aaa : { KEEP (*(SORT(.aaa.*))) } }" > %t3.script
47# RUN: ld.lld -o %t3 --script %t3.script %t2.o %t1.o
48# RUN: llvm-objdump -s %t3 | FileCheck -check-prefix=SORTED_A %s
49
50## Check that SORT_BY_NAME works (SORT is alias).
51# RUN: echo "SECTIONS { .aaa : { *(SORT_BY_NAME(.aaa.*)) } }" > %t4.script
52# RUN: ld.lld -o %t4 --script %t4.script %t2.o %t1.o
53# RUN: llvm-objdump -s %t4 | FileCheck -check-prefix=SORTED_A %s
54
55## Check that sections ordered by alignment.
56# RUN: echo "SECTIONS { .aaa : { *(SORT_BY_ALIGNMENT(.aaa.*)) } }" > %t5.script
57# RUN: ld.lld -o %t5 --script %t5.script %t1.o %t2.o
58# RUN: llvm-objdump -s %t5 | FileCheck -check-prefix=SORTED_ALIGNMENT %s
59# SORTED_ALIGNMENT:      Contents of section .aaa:
60# SORTED_ALIGNMENT-NEXT:  05000000 00000000 00000000 00000000
61# SORTED_ALIGNMENT-NEXT:  00000000 00000000 00000000 00000000
62# SORTED_ALIGNMENT-NEXT:  11000000 00000000 00000000 00000000
63# SORTED_ALIGNMENT-NEXT:  04000000 00000000 00000000 00000000
64# SORTED_ALIGNMENT-NEXT:  22000000 00000000 03000000 00000000
65# SORTED_ALIGNMENT-NEXT:  33000000 00000000 02000000 00000000
66# SORTED_ALIGNMENT-NEXT:  44000000 00000000 01000000 00000000
67# SORTED_ALIGNMENT-NEXT:  55000000 00000000
68
69## SORT_NONE itself does not sort anything.
70# RUN: echo "SECTIONS { .aaa : { *(SORT_NONE(.aaa.*)) } }" > %t6.script
71# RUN: ld.lld -o %t7 --script %t6.script %t2.o %t1.o
72# RUN: llvm-objdump -s %t7 | FileCheck -check-prefix=UNSORTED %s
73
74## Check --sort-section alignment option.
75# RUN: echo "SECTIONS { .aaa : { *(.aaa.*) } }" > %t7.script
76# RUN: ld.lld --sort-section alignment -o %t8 --script %t7.script %t1.o %t2.o
77# RUN: llvm-objdump -s %t8 | FileCheck -check-prefix=SORTED_ALIGNMENT %s
78
79## Check --sort-section= form.
80# RUN: ld.lld --sort-section=alignment -o %t8_1 --script %t7.script %t1.o %t2.o
81# RUN: llvm-objdump -s %t8_1 | FileCheck -check-prefix=SORTED_ALIGNMENT %s
82
83## Check --sort-section name option.
84# RUN: echo "SECTIONS { .aaa : { *(.aaa.*) } }" > %t8.script
85# RUN: ld.lld --sort-section name -o %t9 --script %t8.script %t1.o %t2.o
86# RUN: llvm-objdump -s %t9 | FileCheck -check-prefix=SORTED_B %s
87
88## SORT_NONE disables the --sort-section.
89# RUN: echo "SECTIONS { .aaa : { *(SORT_NONE(.aaa.*)) } }" > %t9.script
90# RUN: ld.lld --sort-section name -o %t10 --script %t9.script %t2.o %t1.o
91# RUN: llvm-objdump -s %t10 | FileCheck -check-prefix=UNSORTED %s
92
93## SORT_NONE as a inner sort directive.
94# RUN: echo "SECTIONS { .aaa : { *(SORT_BY_NAME(SORT_NONE(.aaa.*))) } }" > %t10.script
95# RUN: ld.lld -o %t11 --script %t10.script %t2.o %t1.o
96# RUN: llvm-objdump -s %t11 | FileCheck -check-prefix=SORTED_A %s
97
98.global _start
99_start:
100 nop
101
102.section .aaa.5, "a"
103.align 32
104.quad 5
105
106.section .aaa.1, "a"
107.align 2
108.quad 1
109
110.section .aaa.3, "a"
111.align 8
112.quad 3
113
114.section .aaa.2, "a"
115.align 4
116.quad 2
117
118.section .aaa.4, "a"
119.align 16
120.quad 4
121