[lld] [ELF] Reject error-prone meta characters in input section description (PR #84130)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 6 17:18:23 PST 2024


https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/84130

>From 080332916e4be95f012a89a54a04d0a85bb61d92 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Tue, 5 Mar 2024 23:54:31 -0800
Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.5-bogner
---
 lld/ELF/ScriptParser.cpp              | 14 ++++++++++++--
 lld/test/ELF/linkerscript/wildcards.s | 21 ++++++++++++++-------
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index f0ede1f43bbdb3..282f95bd04b085 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -717,9 +717,19 @@ SmallVector<SectionPattern, 0> ScriptParser::readInputSectionsList() {
 
     StringMatcher SectionMatcher;
     // Break if the next token is ), EXCLUDE_FILE, or SORT*.
-    while (!errorCount() && peek() != ")" && peek() != "EXCLUDE_FILE" &&
-           peekSortKind() == SortSectionPolicy::Default)
+    while (!errorCount() && peekSortKind() == SortSectionPolicy::Default) {
+      StringRef s = peek();
+      if (s == ")" || s == "EXCLUDE_FILE")
+        break;
+      // Detect common mistakes that certain non-wildcard meta characters used
+      // without a closing ')'.
+      if (s.size() == 1 && strchr("(){}", s[0])) {
+        skip();
+        setError("section pattern is expected");
+        break;
+      }
       SectionMatcher.addPattern(unquote(next()));
+    }
 
     if (!SectionMatcher.empty())
       ret.push_back({std::move(excludeFilePat), std::move(SectionMatcher)});
diff --git a/lld/test/ELF/linkerscript/wildcards.s b/lld/test/ELF/linkerscript/wildcards.s
index 1eea27891dfc2c..24d4102559c95e 100644
--- a/lld/test/ELF/linkerscript/wildcards.s
+++ b/lld/test/ELF/linkerscript/wildcards.s
@@ -91,24 +91,31 @@ SECTIONS {
   .text : { *([.]abc .ab[v-y] ) }
 }
 
-## Test a few non-wildcard meta characters rejected by GNU ld.
+## Test a few non-wildcard characters rejected by GNU ld.
 
 #--- lbrace.lds
-# RUN: ld.lld -T lbrace.lds a.o -o out
+# RUN: not ld.lld -T lbrace.lds a.o 2>&1 | FileCheck %s --check-prefix=ERR-LBRACE --match-full-lines --strict-whitespace
+#      ERR-LBRACE:{{.*}}: section pattern is expected
+# ERR-LBRACE-NEXT:>>>   .text : { *(.a* { ) }
+# ERR-LBRACE-NEXT:>>>                   ^
 SECTIONS {
   .text : { *(.a* { ) }
 }
 
 #--- lparen.lds
-## ( is recognized as a section name pattern. Note, ( is rejected by GNU ld.
-# RUN: ld.lld -T lparen.lds a.o -o out
-# RUN: llvm-objdump --section-headers out | FileCheck --check-prefix=SEC-NO %s
+# RUN: not ld.lld -T lparen.lds a.o 2>&1 | FileCheck %s --check-prefix=ERR-LPAREN --match-full-lines --strict-whitespace
+#      ERR-LPAREN:{{.*}}: section pattern is expected
+# ERR-LPAREN-NEXT:>>>   .text : { *(.a* ( ) }
+# ERR-LPAREN-NEXT:>>>                   ^
 SECTIONS {
- .text : { *(.a* ( ) }
+  .text : { *(.a* ( ) }
 }
 
 #--- rbrace.lds
-# RUN: ld.lld -T rbrace.lds a.o -o out
+# RUN: not ld.lld -T rbrace.lds a.o 2>&1 | FileCheck %s --check-prefix=ERR-RBRACE --match-full-lines --strict-whitespace
+#      ERR-RBRACE:{{.*}}: section pattern is expected
+# ERR-RBRACE-NEXT:>>>   .text : { *(.a* } ) }
+# ERR-RBRACE-NEXT:>>>                   ^
 SECTIONS {
   .text : { *(.a* } ) }
 }

>From 5aca4c05b840184922f767d5faedcc9c8172bd09 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Wed, 6 Mar 2024 09:22:51 -0800
Subject: [PATCH 2/3] improve comment and tests

Created using spr 1.3.5-bogner
---
 lld/ELF/ScriptParser.cpp              | 4 ++--
 lld/test/ELF/linkerscript/wildcards.s | 9 +++++++++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index 282f95bd04b085..d8660f7a2f56f8 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -721,9 +721,9 @@ SmallVector<SectionPattern, 0> ScriptParser::readInputSectionsList() {
       StringRef s = peek();
       if (s == ")" || s == "EXCLUDE_FILE")
         break;
-      // Detect common mistakes that certain non-wildcard meta characters used
+      // Detect common mistakes when certain non-wildcard meta characters used
       // without a closing ')'.
-      if (s.size() == 1 && strchr("(){}", s[0])) {
+      if (!s.empty() && strchr("(){}", s[0])) {
         skip();
         setError("section pattern is expected");
         break;
diff --git a/lld/test/ELF/linkerscript/wildcards.s b/lld/test/ELF/linkerscript/wildcards.s
index 24d4102559c95e..5256dcba78240c 100644
--- a/lld/test/ELF/linkerscript/wildcards.s
+++ b/lld/test/ELF/linkerscript/wildcards.s
@@ -102,6 +102,15 @@ SECTIONS {
   .text : { *(.a* { ) }
 }
 
+#--- lbrace2.lds
+# RUN: not ld.lld -T lbrace2.lds a.o 2>&1 | FileCheck %s --check-prefix=ERR-LBRACE2 --match-full-lines --strict-whitespace
+#      ERR-LBRACE2:{{.*}}: section pattern is expected
+# ERR-LBRACE2-NEXT:>>>   .text : { *(.a*{) }
+# ERR-LBRACE2-NEXT:>>>                  ^
+SECTIONS {
+  .text : { *(.a*{) }
+}
+
 #--- lparen.lds
 # RUN: not ld.lld -T lparen.lds a.o 2>&1 | FileCheck %s --check-prefix=ERR-LPAREN --match-full-lines --strict-whitespace
 #      ERR-LPAREN:{{.*}}: section pattern is expected

>From ac23c0a007536fa78957f76b499744dc7fcc0d90 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Wed, 6 Mar 2024 17:18:14 -0800
Subject: [PATCH 3/3] improve a test

Created using spr 1.3.5-bogner
---
 lld/test/ELF/linkerscript/wildcards.s | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lld/test/ELF/linkerscript/wildcards.s b/lld/test/ELF/linkerscript/wildcards.s
index 5256dcba78240c..e9ae722e448927 100644
--- a/lld/test/ELF/linkerscript/wildcards.s
+++ b/lld/test/ELF/linkerscript/wildcards.s
@@ -123,10 +123,10 @@ SECTIONS {
 #--- rbrace.lds
 # RUN: not ld.lld -T rbrace.lds a.o 2>&1 | FileCheck %s --check-prefix=ERR-RBRACE --match-full-lines --strict-whitespace
 #      ERR-RBRACE:{{.*}}: section pattern is expected
-# ERR-RBRACE-NEXT:>>>   .text : { *(.a* } ) }
-# ERR-RBRACE-NEXT:>>>                   ^
+# ERR-RBRACE-NEXT:>>>   .text : { *(.a* x = 3; } ) }
+# ERR-RBRACE-NEXT:>>>                          ^
 SECTIONS {
-  .text : { *(.a* } ) }
+  .text : { *(.a* x = 3; } ) }
 }
 
 #--- rparen.lds



More information about the llvm-commits mailing list