[lld] 30ec2bf - [ELF] PROVIDE: allow quoted names to be discarded

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 27 16:20:02 PDT 2024


Author: Fangrui Song
Date: 2024-07-27T16:19:57-07:00
New Revision: 30ec2bf58d1559b161254118e39271b829fd0867

URL: https://github.com/llvm/llvm-project/commit/30ec2bf58d1559b161254118e39271b829fd0867
DIFF: https://github.com/llvm/llvm-project/commit/30ec2bf58d1559b161254118e39271b829fd0867.diff

LOG: [ELF] PROVIDE: allow quoted names to be discarded

Extend commit ebb326a51fec37b5a47e5702e8ea157cd4f835cd for (#74771) to
support quoted names, e.g. `PROVIDE("f1" = f2 + f3);`.

Added: 
    

Modified: 
    lld/ELF/ScriptParser.cpp
    lld/test/ELF/gc-sections-with-provide.s
    lld/test/ELF/linkerscript/symbolreferenced.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index d9193c404ec67..fc168dfea32ad 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -89,6 +89,7 @@ class ScriptParser final : ScriptLexer {
   void readVersionScriptCommand();
   void readNoCrossRefs(bool to);
 
+  StringRef readName();
   SymbolAssignment *readSymbolAssignment(StringRef name);
   ByteCommand *readByteCommand(StringRef tok);
   std::array<uint8_t, 4> readFill();
@@ -1089,7 +1090,7 @@ std::array<uint8_t, 4> ScriptParser::readFill() {
 
 SymbolAssignment *ScriptParser::readProvideHidden(bool provide, bool hidden) {
   expect("(");
-  StringRef name = next(), eq = peek();
+  StringRef name = readName(), eq = peek();
   if (eq != "=") {
     setError("= expected, but got " + next());
     while (till(")"))
@@ -1130,10 +1131,10 @@ SymbolAssignment *ScriptParser::readAssignment(StringRef tok) {
     SaveAndRestore saved(inExpr, true);
     if (op.starts_with("=")) {
       // Support = followed by an expression without whitespace.
-      cmd = readSymbolAssignment(tok);
+      cmd = readSymbolAssignment(unquote(tok));
     } else if ((op.size() == 2 && op[1] == '=' && strchr("+-*/&^|", op[0])) ||
                op == "<<=" || op == ">>=") {
-      cmd = readSymbolAssignment(tok);
+      cmd = readSymbolAssignment(unquote(tok));
     } else if (tok == "PROVIDE") {
       cmd = readProvideHidden(true, false);
     } else if (tok == "HIDDEN") {
@@ -1152,8 +1153,9 @@ SymbolAssignment *ScriptParser::readAssignment(StringRef tok) {
   return cmd;
 }
 
+StringRef ScriptParser::readName() { return unquote(next()); }
+
 SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef name) {
-  name = unquote(name);
   StringRef op = next();
   assert(op == "=" || op == "*=" || op == "/=" || op == "+=" || op == "-=" ||
          op == "&=" || op == "^=" || op == "|=" || op == "<<=" || op == ">>=");

diff  --git a/lld/test/ELF/gc-sections-with-provide.s b/lld/test/ELF/gc-sections-with-provide.s
index 3e5b1b1efe6ca..268faa4354687 100644
--- a/lld/test/ELF/gc-sections-with-provide.s
+++ b/lld/test/ELF/gc-sections-with-provide.s
@@ -53,8 +53,8 @@ baz:
 
 
 #--- script.t
-PROVIDE(unused = bar + used);
-PROVIDE(used = another_used);
+PROVIDE(unused = bar + "used");
+PROVIDE("used" = another_used);
 PROVIDE(baz_ref = baz);
 PROVIDE(another_used = baz_ref);
 PROVIDE(another_unused = unused + bar + 0x1);

diff  --git a/lld/test/ELF/linkerscript/symbolreferenced.s b/lld/test/ELF/linkerscript/symbolreferenced.s
index 6848082690837..ad356d45fac4c 100644
--- a/lld/test/ELF/linkerscript/symbolreferenced.s
+++ b/lld/test/ELF/linkerscript/symbolreferenced.s
@@ -94,7 +94,7 @@ PROVIDE(unused = g1);
 PROVIDE_HIDDEN(another_unused = g1);
 
 #--- chain_with_cycle.t
-PROVIDE(f1 = f2 + f3);
+PROVIDE("f1" = f2 + f3);
 PROVIDE(f2 = f3 + f4);
 PROVIDE(f3 = f4);
 PROVIDE(f4 = f1);


        


More information about the llvm-commits mailing list