xref: /llvm-project/lld/test/ELF/linkerscript/section-class.test (revision 4dac0dff086090d071fc3ef60d7458b3d6cfde60)
1# REQUIRES: x86
2
3# RUN: rm -rf %t && split-file %s %t && cd %t
4
5#--- matching.s
6.section .rodata.a,"a",@progbits
7.byte 1
8
9.section .rodata.b,"a",@progbits
10.byte 2
11
12.section .rodata.c,"ax",@progbits
13.byte 3
14
15.section .rodata.d,"a",@progbits
16.byte 4
17
18.section .rodata.e,"a",@progbits
19.byte 5
20
21.section .rodata.f,"a",@progbits
22.balign 2
23.byte 6
24
25.section .rodata.g,"a",@progbits
26.byte 7
27
28.section .rodata.h,"a",@progbits
29.byte 8
30
31# RUN: llvm-mc -n -filetype=obj -triple=x86_64 matching.s -o matching.o
32
33#--- matching.lds
34## CLASS definitions match sections in linker script order. The sections may be
35## placed in a different order. Classes may derive from one another. Class
36## references can be restricted by INPUT_SECTION_FLAGS. Classes can be referenced
37## in /DISCARD/ and INSERT.
38SECTIONS {
39  CLASS(a) { *(.rodata.a) }
40  CLASS(cd) { *(.rodata.c) *(.rodata.d) }
41  CLASS(ef) { *(SORT_BY_ALIGNMENT(.rodata.e .rodata.f)) }
42  CLASS(g) { *(.rodata.g) }
43  CLASS("h)") { *(.rodata.h) }
44  .rodata : {
45    *(.rodata.*)
46    INPUT_SECTION_FLAGS(SHF_EXECINSTR) CLASS( cd)
47    CLASS(a)CLASS(ef )
48  }
49  OVERLAY : { .rodata.d { INPUT_SECTION_FLAGS(!SHF_EXECINSTR) CLASS(cd) } }
50  /DISCARD/ : { CLASS(g) }
51}
52
53SECTIONS {
54  .rodata.h : { CLASS("h)") }
55} INSERT AFTER .rodata;
56
57# RUN: ld.lld -T matching.lds matching.o -o matching
58# RUN: llvm-objdump -s matching |\
59# RUN:   FileCheck %s --check-prefix=MATCHING
60# MATCHING:      .rodata
61# MATCHING-NEXT: 020301cc 0605 ......{{$}}
62# MATCHING:      .rodata.h
63# MATCHING-NEXT: 08 .{{$}}
64# MATCHING:      .rodata.d
65# MATCHING-NEXT: 04 .{{$}}
66
67#--- already-defined.lds
68## A section class has more than one description.
69SECTIONS {
70  CLASS(a) { *(.rodata.a) }
71  CLASS(a) { *(.rodata.b) }
72  CLASS(b) { *(.rodata.c) }
73  CLASS(b) { *(.rodata.d) }
74}
75
76# RUN: not ld.lld -T already-defined.lds matching.o 2>&1 | \
77# RUN:   FileCheck %s --check-prefix=ALREADY-DEFINED --implicit-check-not=error:
78
79# ALREADY-DEFINED: error: already-defined.lds:4: section class 'a' already defined
80
81#--- missing-filename-pattern-1.lds
82## A filename pattern is missing in a section class description.
83SECTIONS {
84  CLASS(a) { (.rodata.a) }
85}
86#--- missing-filename-pattern-2.lds
87## A filename pattern is missing in a section class description.
88SECTIONS {
89  CLASS(a) { .rodata.a) }
90}
91
92# RUN: not ld.lld -T missing-filename-pattern-1.lds matching.o 2>&1 | \
93# RUN:   FileCheck %s --check-prefix=MISSING-FILENAME-PATTERN --implicit-check-not=error:
94# RUN: not ld.lld -T missing-filename-pattern-2.lds matching.o 2>&1 | \
95# RUN:   FileCheck %s --check-prefix=MISSING-FILENAME-PATTERN --implicit-check-not=error:
96
97# MISSING-FILENAME-PATTERN: error: missing-filename-pattern-{{[1-2]}}.lds:3: expected filename pattern
98
99#--- multiple-class-names.lds
100## More than one class is mentioned in a reference.
101SECTIONS {
102  CLASS(a) { *(.rodata.a) }
103  CLASS(b) { *(.rodata.b) }
104  .rodata : { CLASS(a b) }
105}
106
107# RUN: not ld.lld -T multiple-class-names.lds matching.o 2>&1 | \
108# RUN:   FileCheck %s --check-prefix=MULTIPLE-CLASS-NAMES --implicit-check-not=error:
109
110# MULTIPLE-CLASS-NAMES: error: multiple-class-names.lds:5: ) expected, but got b
111
112#--- undefined.lds
113## A section class is referenced but never defined
114SECTIONS {
115  .rodata : { CLASS(a) }
116}
117
118# RUN: not ld.lld -T undefined.lds matching.o 2>&1 | \
119# RUN:   FileCheck %s --check-prefix=UNDEFINED --implicit-check-not=error:
120
121# UNDEFINED: error: undefined section class 'a'
122
123#--- referenced-before-defined.lds
124## The content of section classes is demanded before its definition is processed.
125SECTIONS {
126  .rodata : { CLASS(a) }
127  CLASS(a) { *(.rodata.a) }
128}
129
130# RUN: not ld.lld -T referenced-before-defined.lds matching.o 2>&1 | \
131# RUN:   FileCheck %s --check-prefix=REFERENCED-BEFORE-DEFINED
132# RUN: ld.lld -T referenced-before-defined.lds matching.o -o out --noinhibit-exec 2>&1 | \
133# RUN:   FileCheck %s --check-prefix=REFERENCED-BEFORE-DEFINED-WARN
134
135# REFERENCED-BEFORE-DEFINED: error: section class 'a' referenced by '.rodata' before class definition
136# REFERENCED-BEFORE-DEFINED-WARN: warning: section class 'a' referenced by '.rodata' before class definition
137
138#--- unreferenced.lds
139## An input section is bound to a section class but is not referenced.
140SECTIONS {
141  CLASS(a) { *(.rodata.*) }
142}
143
144# RUN: not ld.lld -T unreferenced.lds matching.o 2>&1 | \
145# RUN:   FileCheck %s --check-prefix=UNREFERENCED -implicit-check-not=error:
146# RUN: ld.lld -T unreferenced.lds matching.o -o out --noinhibit-exec 2>&1 | \
147# RUN:   FileCheck %s --check-prefix=UNREFERENCED-WARN -implicit-check-not=error:
148
149# UNREFERENCED: error: section class 'a' is unreferenced
150# UNREFERENCED-WARN: warning: section class 'a' is unreferenced
151
152#--- class-references-class.lds
153## One section class references another.
154SECTIONS {
155  CLASS(a) { *(.rodata.a) }
156  CLASS(b) { CLASS(a) }
157}
158
159# RUN: not ld.lld -T class-references-class.lds matching.o 2>&1 | \
160# RUN:   FileCheck %s --check-prefix=CLASS-REFERENCES-CLASS --implicit-check-not=error:
161
162# CLASS-REFERENCES-CLASS: error: class-references-class.lds:4: section class 'b' references class 'a'
163
164#--- spill.s
165.section .one_byte_section,"a",@progbits
166.fill 1
167
168.section .two_byte_section,"a",@progbits
169.fill 2
170
171# RUN: llvm-mc -n -filetype=obj -triple=x86_64 spill.s -o spill.o
172
173#--- spill.lds
174## An input section in a class spills to a later class ref when the region of
175## its first ref would overflow. The spill uses the alignment of the later ref.
176MEMORY {
177  a : ORIGIN = 0, LENGTH = 2
178  b : ORIGIN = 2, LENGTH = 16
179}
180
181SECTIONS {
182  CLASS(c) { *(.two_byte_section) }
183  .first_chance : SUBALIGN(1) { *(.one_byte_section) CLASS(c) } >a
184  .last_chance : SUBALIGN(8) { CLASS (c) } >b
185}
186
187# RUN: ld.lld -T spill.lds spill.o -o spill
188# RUN: llvm-readelf -S spill | FileCheck %s --check-prefix=SPILL
189
190# SPILL:      Name          Type     Address          Off    Size
191# SPILL:      .first_chance PROGBITS 0000000000000000 001000 000001
192# SPILL-NEXT: .last_chance  PROGBITS 0000000000000008 001008 000002
193
194#--- spill-fail.lds
195## A spill off the end still fails the link.
196MEMORY {
197  a : ORIGIN = 0, LENGTH = 1
198  b : ORIGIN = 2, LENGTH = 0
199}
200
201SECTIONS {
202  CLASS(c) { *(.two_byte_section) }
203  .first_chance : { *(.one_byte_section) CLASS(c) } >a
204  .last_chance : { CLASS(c) } >b
205}
206
207# RUN: not ld.lld -T spill-fail.lds spill.o 2>&1 |\
208# RUN:   FileCheck %s --check-prefix=SPILL-FAIL --implicit-check-not=error:
209
210# SPILL-FAIL: error: section '.last_chance' will not fit in region 'b': overflowed by 2 bytes
211
212#--- spill-lma.lds
213## The above spill still occurs when the LMA would overflow, even though the
214## VMA would fit.
215MEMORY {
216  vma_a : ORIGIN = 0, LENGTH = 3
217  vma_b : ORIGIN = 3, LENGTH = 3
218  lma_a : ORIGIN = 6, LENGTH = 2
219  lma_b : ORIGIN = 8, LENGTH = 2
220}
221
222SECTIONS {
223  CLASS(c) { *(.two_byte_section) }
224  .first_chance : { *(.one_byte_section) CLASS(c) } >vma_a AT>lma_a
225  .last_chance : { CLASS(c) } >vma_b AT>lma_b
226}
227
228# RUN: ld.lld -T spill-lma.lds spill.o -o spill-lma
229# RUN: llvm-readelf -S spill-lma | FileCheck %s --check-prefix=SPILL-LMA
230
231# SPILL-LMA:      Name          Type     Address          Off    Size
232# SPILL-LMA:      .first_chance PROGBITS 0000000000000000 001000 000001
233# SPILL-LMA-NEXT: .last_chance  PROGBITS 0000000000000003 001003 000002
234
235#--- spill-later.lds
236## A spill occurs to an additional class ref after the first.
237MEMORY {
238  a : ORIGIN = 0, LENGTH = 2
239  b : ORIGIN = 2, LENGTH = 1
240  c : ORIGIN = 3, LENGTH = 2
241}
242
243SECTIONS {
244  CLASS(c) { *(.two_byte_section) }
245  .first_chance : { *(.one_byte_section) CLASS(c) } >a
246  .second_chance : { CLASS(c) } >b
247  .last_chance : { CLASS(c) } >c
248}
249
250# RUN: ld.lld -T spill-later.lds spill.o -o spill-later
251# RUN: llvm-readelf -S spill-later | FileCheck %s --check-prefix=SPILL-LATER
252
253# SPILL-LATER:      Name            Type     Address          Off    Size
254# SPILL-LATER:      .first_chance   PROGBITS 0000000000000000 001000 000001
255# SPILL-LATER-NEXT: .second_chance  PROGBITS 0000000000000002 001001 000000
256# SPILL-LATER-NEXT: .last_chance    PROGBITS 0000000000000003 001003 000002
257
258#--- spill-earlier.lds
259## A later overflow causes an earlier section to spill.
260MEMORY {
261  a : ORIGIN = 0, LENGTH = 2
262  b : ORIGIN = 2, LENGTH = 1
263}
264
265SECTIONS {
266  CLASS(c) { *(.one_byte_section) }
267  .first_chance : { CLASS(c) *(.two_byte_section) } >a
268  .last_chance : { CLASS(c) } >b
269}
270
271# RUN: ld.lld -T spill-earlier.lds spill.o -o spill-earlier
272# RUN: llvm-readelf -S spill-earlier | FileCheck %s --check-prefix=SPILL-EARLIER
273
274# SPILL-EARLIER:      Name          Type     Address          Off    Size
275# SPILL-EARLIER:      .first_chance PROGBITS 0000000000000000 001000 000002
276# SPILL-EARLIER-NEXT: .last_chance  PROGBITS 0000000000000002 001002 000001
277
278#--- enable-non-contiguous-regions.lds
279## Class definitions do not preclude additional matches when used with
280## --enable-non-contiguous-regions, and additional matches in class
281## definitions become spills at class references.
282MEMORY {
283  a : ORIGIN = 0, LENGTH = 1
284  b : ORIGIN = 1, LENGTH = 2
285  c : ORIGIN = 3, LENGTH = 1
286}
287
288SECTIONS {
289  .first_chance : { *(.two_byte_section) } >a
290  /* An additional match in a class defers a spill. */
291  CLASS(two) { *(.two_byte_section) }
292  /* A class references actualizes deferred spills. */
293  .last_chance : { CLASS(two) } >b
294
295  /* Section classes do not preclude other matches. */
296  CLASS(one) { *(.one_byte_section) }
297  .one_byte_section : { *(.one_byte_section) } >c
298}
299
300# RUN: ld.lld -T enable-non-contiguous-regions.lds spill.o -o enable-non-contiguous-regions --enable-non-contiguous-regions
301# RUN: llvm-readelf -S enable-non-contiguous-regions | FileCheck %s --check-prefix=ENABLE-NON-CONTIGUOUS-REGIONS
302
303# ENABLE-NON-CONTIGUOUS-REGIONS:      Name          Type     Address          Off    Size
304# ENABLE-NON-CONTIGUOUS-REGIONS:      .first_chance     PROGBITS 0000000000000000 000190 000000
305# ENABLE-NON-CONTIGUOUS-REGIONS-NEXT: .last_chance      PROGBITS 0000000000000001 001001 000002
306# ENABLE-NON-CONTIGUOUS-REGIONS-NEXT: .one_byte_section PROGBITS 0000000000000003 001003 000001
307
308#--- merge.s
309.section .a,"aM",@progbits,1
310.byte 0x12, 0x34
311
312.section .b,"aM",@progbits,1
313.p2align 3
314.byte 0x12
315
316# RUN: llvm-mc -n -filetype=obj -triple=x86_64 merge.s -o merge.o
317
318#--- spill-merge.lds
319## SHF_MERGE sections are spilled according to the class refs of the first
320## merged input section (the one giving the resulting section its name).
321## Spills take into account increases in section alignment due to merging.
322MEMORY {
323  a : ORIGIN = 0, LENGTH = 1
324  b : ORIGIN = 1, LENGTH = 16
325  c : ORIGIN = 17, LENGTH = 16
326}
327
328SECTIONS {
329  CLASS(a) { *(.a) }
330  CLASS(b) { *(.b) }
331  .first : { CLASS(a) CLASS(b) } >a
332  .second : { CLASS(a) } >b
333  .third : { CLASS(b) } >c
334}
335
336# RUN: ld.lld -T spill-merge.lds merge.o -o spill-merge
337# RUN: llvm-readelf -S -x .second spill-merge | FileCheck %s --check-prefix=SPILL-MERGE
338
339# SPILL-MERGE:      Name    Type     Address          Off    Size
340# SPILL-MERGE:      .first  PROGBITS 0000000000000000 000190 000000
341# SPILL-MERGE-NEXT: .second PROGBITS 0000000000000008 001008 000009
342# SPILL-MERGE-NEXT: .third  PROGBITS 0000000000000018 001018 000000
343# SPILL-MERGE:      Hex dump of section '.second':
344# SPILL-MERGE-NEXT: 0x00000008 12000000 00000000 34 .
345
346#--- link-order.s
347.section .a,"a",@progbits
348.fill 1
349
350.section .b,"a",@progbits
351.fill 1
352
353.section .c,"a",@progbits
354.fill 1
355
356.section .link_order.a,"ao",@progbits,.a
357.byte 1
358
359.section .link_order.b,"ao",@progbits,.b
360.byte 2
361
362.section .link_order.c,"ao",@progbits,.c
363.byte 3
364
365# RUN: llvm-mc -n -filetype=obj -triple=x86_64 link-order.s -o link-order.o
366
367#--- link-order.lds
368## SHF_LINK_ORDER is reordered when spilling changes relative section order.
369MEMORY {
370  order : ORIGIN = 0, LENGTH = 3
371  potential_a : ORIGIN = 3, LENGTH = 0
372  bc : ORIGIN = 3, LENGTH = 2
373  actual_a : ORIGIN = 5, LENGTH = 1
374}
375
376SECTIONS {
377  CLASS(a) { *(.a) }
378  .order :  { *(.link_order.*) } > order
379  .potential_a : { CLASS(a) } >potential_a
380  .bc : { *(.b) *(.c) } >bc
381  .actual_a : { CLASS(a) } >actual_a
382}
383
384# RUN: ld.lld -T link-order.lds link-order.o -o link-order
385# RUN: llvm-objdump -s link-order | FileCheck %s --check-prefix=LINK-ORDER
386
387# LINK-ORDER: 020301 ...{{$}}
388
389#--- from-insert.lds
390## A section might spill from INSERT.
391SECTIONS {
392  CLASS(class) { *(.two_byte_section) }
393  .a : { *(.one_byte_section) }
394}
395SECTIONS { .b : { CLASS(class) } } INSERT AFTER .a;
396SECTIONS { .c : { CLASS(class) } }
397
398# RUN: not ld.lld -T from-insert.lds spill.o 2>&1 |\
399# RUN:   FileCheck %s --check-prefix=FROM-INSERT
400# RUN: ld.lld -T from-insert.lds spill.o -o out --noinhibit-exec 2>&1 |\
401# RUN:   FileCheck %s --check-prefix=FROM-INSERT-WARN
402
403# FROM-INSERT: error: section '.two_byte_section' cannot spill from/to INSERT section '.b'
404# FROM-INSERT-WARN: warning: section '.two_byte_section' cannot spill from/to INSERT section '.b'
405
406#--- to-insert.lds
407## A section might spill to INSERT.
408SECTIONS {
409  CLASS(class) { *(.two_byte_section) }
410  .a : { CLASS(class) *(.one_byte_section) }
411}
412SECTIONS { .b : { CLASS(class) } } INSERT AFTER .a;
413
414# RUN: not ld.lld -T to-insert.lds spill.o 2>&1 |\
415# RUN:   FileCheck %s --check-prefix=TO-INSERT
416# RUN:  ld.lld -T to-insert.lds spill.o -o out --noinhibit-exec 2>&1 |\
417# RUN:   FileCheck %s --check-prefix=TO-INSERT-WARN
418
419# TO-INSERT: error: section '.two_byte_section' cannot spill from/to INSERT section '.b'
420# TO-INSERT-WARN: warning: section '.two_byte_section' cannot spill from/to INSERT section '.b'
421
422#--- from-discard.lds
423## A section might spill from /DISCARD/.
424SECTIONS {
425  CLASS(class) { *(.two_byte_section) }
426  /DISCARD/ : { CLASS(class) }
427  .c : { CLASS(class) }
428}
429
430# RUN: not ld.lld -T from-discard.lds spill.o 2>&1 |\
431# RUN:   FileCheck %s --check-prefix=FROM-DISCARD
432# RUN: ld.lld -T from-discard.lds spill.o -o out --noinhibit-exec 2>&1 |\
433# RUN:   FileCheck %s --check-prefix=FROM-DISCARD-WARN
434
435# FROM-DISCARD: error: section '.two_byte_section' cannot spill from/to /DISCARD/
436# FROM-DISCARD-WARN: warning: section '.two_byte_section' cannot spill from/to /DISCARD/
437
438#--- to-discard.lds
439## A section might spill to /DISCARD/.
440SECTIONS {
441  CLASS(class) { *(.two_byte_section) }
442  .a : { CLASS(class) }
443  /DISCARD/ : { CLASS(class) }
444}
445
446# RUN: not ld.lld -T to-discard.lds spill.o 2>&1 |\
447# RUN:   FileCheck %s --check-prefix=TO-DISCARD
448# RUN: ld.lld -T to-discard.lds spill.o -o out --noinhibit-exec 2>&1 |\
449# RUN:   FileCheck %s --check-prefix=TO-DISCARD-WARN
450
451# TO-DISCARD: error: section '.two_byte_section' cannot spill from/to /DISCARD/
452# TO-DISCARD-WARN: warning: section '.two_byte_section' cannot spill from/to /DISCARD/
453