[PATCH] D98306: [ELF] Support . and $ in symbol names in expressions

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 11 09:34:47 PST 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rGe4f385d89448: [ELF] Support . and $ in symbol names in expressions (authored by MaskRay).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98306/new/

https://reviews.llvm.org/D98306

Files:
  lld/ELF/ScriptParser.cpp
  lld/test/ELF/linkerscript/diag3.test
  lld/test/ELF/linkerscript/diag4.test
  lld/test/ELF/linkerscript/diag5.test
  lld/test/ELF/linkerscript/symbol-name.test


Index: lld/test/ELF/linkerscript/symbol-name.test
===================================================================
--- /dev/null
+++ lld/test/ELF/linkerscript/symbol-name.test
@@ -0,0 +1,8 @@
+# REQUIRES: x86
+## Test that . and $ can be used by symbol names in expressions.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 /dev/null -o %t.o
+# RUN: ld.lld -T %s %t.o -o /dev/null
+
+a0 = DEFINED(.TOC.) ? .TOC. : 0;
+a1 = DEFINED(__global_pointer$) ? __global_pointer$ : 0;
Index: lld/test/ELF/linkerscript/diag5.test
===================================================================
--- lld/test/ELF/linkerscript/diag5.test
+++ lld/test/ELF/linkerscript/diag5.test
@@ -6,9 +6,9 @@
 SECTIONS {
   .text : { *(.text) }
   .keep : { *(.keep) }
-  boom .temp : { *(.temp) }
+  boom ^temp : { *(.temp) }
 }
 
-# CHECK:      9: malformed number: .temp
-# CHECK-NEXT: >>>   boom .temp : { *(.temp) }
+# CHECK:      9: malformed number: ^temp
+# CHECK-NEXT: >>>   boom ^temp : { *(.temp) }
 # CHECK-NEXT: >>>        ^
Index: lld/test/ELF/linkerscript/diag4.test
===================================================================
--- lld/test/ELF/linkerscript/diag4.test
+++ lld/test/ELF/linkerscript/diag4.test
@@ -6,9 +6,9 @@
 SECTIONS {
   .text : { *(.text) }
   .keep : { *(.keep) }
-  boom .temp : { *(.temp) }
+  boom ^temp : { *(.temp) }
 }
 
-# CHECK:      9: malformed number: .temp
-# CHECK-NEXT: >>>   boom .temp : { *(.temp) }
-# CHECK-NEXT: >>>        ^
\ No newline at end of file
+# CHECK:      9: malformed number: ^temp
+# CHECK-NEXT: >>>   boom ^temp : { *(.temp) }
+# CHECK-NEXT: >>>        ^
Index: lld/test/ELF/linkerscript/diag3.test
===================================================================
--- lld/test/ELF/linkerscript/diag3.test
+++ lld/test/ELF/linkerscript/diag3.test
@@ -5,9 +5,9 @@
 SECTIONS {
   .text : { *(.text) }
   .keep : { *(.keep) }
-  boom .temp : { *(.temp) }
+  boom ^temp : { *(.temp) }
 }
 
-# CHECK:      8: malformed number: .temp
-# CHECK-NEXT: >>>   boom .temp : { *(.temp) }
+# CHECK:      8: malformed number: ^temp
+# CHECK-NEXT: >>>   boom ^temp : { *(.temp) }
 # CHECK-NEXT: >>>        ^
Index: lld/ELF/ScriptParser.cpp
===================================================================
--- lld/ELF/ScriptParser.cpp
+++ lld/ELF/ScriptParser.cpp
@@ -1234,6 +1234,13 @@
     error(location + ": undefined section " + cmd->name);
 }
 
+static bool isValidSymbolName(StringRef s) {
+  auto valid = [](char c) {
+    return isAlnum(c) || c == '$' || c == '.' || c == '_';
+  };
+  return !s.empty() && !isDigit(s[0]) && llvm::all_of(s, valid);
+}
+
 Expr ScriptParser::readPrimary() {
   if (peek() == "(")
     return readParenExpr();
@@ -1408,7 +1415,7 @@
     return [=] { return *val; };
 
   // Tok is a symbol name.
-  if (!isValidCIdentifier(tok))
+  if (!isValidSymbolName(tok))
     setError("malformed number: " + tok);
   script->referencedSymbols.push_back(tok);
   return [=] { return script->getSymbolValue(tok, location); };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98306.329995.patch
Type: text/x-patch
Size: 2997 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210311/627165f2/attachment.bin>


More information about the llvm-commits mailing list