[lld] [LLD] Add CLASS syntax to SECTIONS (PR #95323)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 22 14:31:35 PDT 2024
================
@@ -0,0 +1,432 @@
+# REQUIRES: x86
+
+# RUN: rm -rf %t && split-file %s %t && cd %t
+
+#--- matching.s
+.section .rodata.a,"a", at progbits
+.byte 1
+
+.section .rodata.b,"a", at progbits
+.byte 2
+
+.section .rodata.c,"ax", at progbits
+.byte 3
+
+.section .rodata.d,"a", at progbits
+.byte 4
+
+.section .rodata.e,"a", at progbits
+.byte 5
+
+.section .rodata.f,"a", at progbits
+.balign 2
+.byte 6
+
+.section .rodata.g,"a", at progbits
+.byte 7
+
+.section .rodata.h,"a", at progbits
+.byte 8
+
+# RUN: llvm-mc -n -filetype=obj -triple=x86_64 matching.s -o matching.o
+
+#--- matching.lds
+## CLASS definitions match sections in linker script order. The sections may be
+## placed in a different order. Classes may derive from one another. Class # #
+## references can be restricted by INPUT_SECTION_FLAGS. Classes can be referenced
+## in /DISCARD/ and INSERT.
+SECTIONS {
+ CLASS(a) { *(.rodata.a) }
+ CLASS(cd) { *(.rodata.c) *(.rodata.d) }
+ CLASS(ef) { *(SORT_BY_ALIGNMENT(.rodata.e .rodata.f)) }
+ CLASS(g) { *(.rodata.g) }
+ CLASS(h) { *(.rodata.h) }
+ .rodata : {
+ *(.rodata.*)
+ INPUT_SECTION_FLAGS(SHF_EXECINSTR) CLASS( cd)
+ CLASS(a)
+ CLASS(ef )
+ }
+ OVERLAY : { .rodata.d { INPUT_SECTION_FLAGS(!SHF_EXECINSTR) CLASS(cd) } }
+ /DISCARD/ : { CLASS(g) }
+}
+
+SECTIONS {
+ .rodata.h : { CLASS(h) }
+} INSERT AFTER .rodata;
+
+# RUN: ld.lld -T matching.lds matching.o -o matching
+# RUN: llvm-readobj -x .rodata -x .rodata.d -x .rodata.h matching |\
+# RUN: FileCheck %s --check-prefix=MATCHING
+# MATCHING: .rodata
+# MATCHING-NEXT: 020301cc 0605
+# MATCHING: .rodata.h
+# MATCHING-NEXT: 08
+# MATCHING: .rodata.d
+# MATCHING-NEXT: 04
+
+#--- already-defined.lds
+## A section class has more than one description.
+SECTIONS {
+ CLASS(a) { *(.rodata.a) }
+ CLASS(a) { *(.rodata.b) }
+ CLASS(b) { *(.rodata.c) }
+ CLASS(b) { *(.rodata.d) }
+}
+
+# RUN: not ld.lld -T already-defined.lds matching.o 2>&1 | \
+# RUN: FileCheck %s --check-prefix=ALREADY-DEFINED --implicit-check-not=error:
+
+# ALREADY-DEFINED: error: already-defined.lds:4: section class 'a' already defined
+
+#--- missing-filename-pattern.lds
+## A filename pattern is missing in a section class description.
+SECTIONS {
+ CLASS(a) { (.rodata.a) }
+}
+
+# RUN: not ld.lld -T missing-filename-pattern.lds matching.o 2>&1 | \
+# RUN: FileCheck %s --check-prefix=MISSING-FILENAME-PATTERN --implicit-check-not=error:
+
+# MISSING-FILENAME-PATTERN: error: missing-filename-pattern.lds:3: expected filename pattern
+
+#--- multiple-class-names.lds
+## More than one class is mentioned in a reference.
+SECTIONS {
+ CLASS(a) { *(.rodata.a) }
+ CLASS(b) { *(.rodata.b) }
+ .rodata : { CLASS(a b) }
+}
+
+# RUN: not ld.lld -T multiple-class-names.lds matching.o 2>&1 | \
+# RUN: FileCheck %s --check-prefix=MULTIPLE-CLASS-NAMES --implicit-check-not=error:
+
+# MULTIPLE-CLASS-NAMES: error: multiple-class-names.lds:5: ) expected, but got b
+
+#--- referenced-before-defined.lds
+## The content of section classes is demanded before its definition is processed.
+SECTIONS {
+ .rodata : { CLASS(a) }
+ CLASS(a) { *(.rodata.a) }
+}
+
+# RUN: not ld.lld -T referenced-before-defined.lds matching.o 2>&1 | \
+# RUN: FileCheck %s --check-prefix=REFERENCED-BEFORE-DEFINED
+# RUN: ld.lld -T referenced-before-defined.lds matching.o -o out --noinhibit-exec 2>&1 | \
+# RUN: FileCheck %s --check-prefix=REFERENCED-BEFORE-DEFINED-WARN
+
+# REFERENCED-BEFORE-DEFINED: error: section class 'a' referenced by '.rodata' before class definition
+# REFERENCED-BEFORE-DEFINED-WARN: warning: section class 'a' referenced by '.rodata' before class definition
+
+#--- unreferenced.lds
+## An input section is bound to a section class but is not referenced by at
----------------
MaskRay wrote:
`.. is not referenced.`
It's unclear what `at least one output section` means.
https://github.com/llvm/llvm-project/pull/95323
More information about the llvm-commits
mailing list