[lld] [ELF] PHDRS update while condition and phdrs.s unclose2.lds output (PR #100918)

Hongyu Chen via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 28 23:05:39 PDT 2024


https://github.com/yugier updated https://github.com/llvm/llvm-project/pull/100918

>From ae7e53bb951c96fdc9a37409d49495ee57758cad Mon Sep 17 00:00:00 2001
From: Hongyu Chen <hongyc4 at uci.edu>
Date: Sat, 27 Jul 2024 23:07:29 -0700
Subject: [PATCH 1/2] [ELF] PHDRS update while condition and phdrs.s
 unclose2.lds output

---
 lld/ELF/ScriptParser.cpp          | 12 ++++++------
 lld/test/ELF/linkerscript/phdrs.s |  2 +-
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index c033733877093..85143760f1540 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -524,17 +524,17 @@ void ScriptParser::readPhdrs() {
     cmd.name = tok;
     cmd.type = readPhdrType();
 
-    while (!errorCount() && !consume(";")) {
-      if (consume("FILEHDR"))
+    while (auto tok2 = till(";")) {
+      if (tok2 == "FILEHDR")
         cmd.hasFilehdr = true;
-      else if (consume("PHDRS"))
+      else if (tok2 == "PHDRS")
         cmd.hasPhdrs = true;
-      else if (consume("AT"))
+      else if (tok2 == "AT")
         cmd.lmaExpr = readParenExpr();
-      else if (consume("FLAGS"))
+      else if (tok2 == "FLAGS")
         cmd.flags = readParenExpr()().getValue();
       else
-        setError("unexpected header attribute: " + next());
+        setError("unexpected header attribute: " + tok2);
     }
 
     script->phdrsCommands.push_back(cmd);
diff --git a/lld/test/ELF/linkerscript/phdrs.s b/lld/test/ELF/linkerscript/phdrs.s
index 997f7e39972d2..e43688bcb62f5 100644
--- a/lld/test/ELF/linkerscript/phdrs.s
+++ b/lld/test/ELF/linkerscript/phdrs.s
@@ -108,7 +108,7 @@ PHDRS { text PT_LOAD ;
 PHDRS { text PT_LOAD
 
 # RUN: not ld.lld -T unclosed2.lds a.o 2>&1 | FileCheck --check-prefix=UNCLOSED2 %s
-# UNCLOSED2: error: unclosed2.lds:1: unexpected header attribute:
+# UNCLOSED2: error: unclosed2.lds:1: unexpected EOF
 
 #--- a.s
 .global _start

>From 38160d4ef5a1fbd25a66fbe5c431fe1e9def6b14 Mon Sep 17 00:00:00 2001
From: Hongyu Chen <hongyc4 at uci.edu>
Date: Sun, 28 Jul 2024 23:04:51 -0700
Subject: [PATCH 2/2] [ELF] Added readNameTill and replaced some unquote(tok)

---
 lld/ELF/ScriptLexer.cpp           | 17 +++++++++++++++++
 lld/ELF/ScriptLexer.h             |  1 +
 lld/ELF/ScriptParser.cpp          | 23 +++++++++++------------
 lld/test/ELF/linkerscript/phdrs.s |  6 ++++++
 4 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/lld/ELF/ScriptLexer.cpp b/lld/ELF/ScriptLexer.cpp
index 9d31b2b226517..20a76bb4b5dd2 100644
--- a/lld/ELF/ScriptLexer.cpp
+++ b/lld/ELF/ScriptLexer.cpp
@@ -231,6 +231,23 @@ ScriptLexer::Token ScriptLexer::till(StringRef tok) {
   return {};
 }
 
+ScriptLexer::Token ScriptLexer::readNameTill(StringRef tok) {
+  // this behaves like till but expects that token to be an
+  // identify of quoted string
+  StringRef str = next();
+  if (str.starts_with("\""))
+    str = str.substr(1, str.size() - 2);
+  if (str == tok)
+    return {};
+  if (str == "(" || str == ")" || str == "}")
+    setError(tok + " is missing before " + str);
+  if (!atEOF())
+    return {str};
+  prevTok = {};
+  setError("unexpected EOF");
+  return {};
+}
+
 // Returns true if S encloses T.
 static bool encloses(StringRef s, StringRef t) {
   return s.bytes_begin() <= t.bytes_begin() && t.bytes_end() <= s.bytes_end();
diff --git a/lld/ELF/ScriptLexer.h b/lld/ELF/ScriptLexer.h
index 0c12b984c9e1b..daee97e002b43 100644
--- a/lld/ELF/ScriptLexer.h
+++ b/lld/ELF/ScriptLexer.h
@@ -67,6 +67,7 @@ class ScriptLexer {
   bool consume(StringRef tok);
   void expect(StringRef expect);
   Token till(StringRef tok);
+  Token readNameTill(StringRef tok);
   std::string getCurrentLocation();
   MemoryBufferRef getCurrentMB();
 
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index 85143760f1540..7870688ce3db2 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -310,8 +310,8 @@ void ScriptParser::readDefsym(StringRef name) {
 void ScriptParser::readNoCrossRefs(bool to) {
   expect("(");
   NoCrossRefCommand cmd{{}, to};
-  while (auto tok = till(")"))
-    cmd.outputSections.push_back(unquote(tok));
+  while (auto tok = readNameTill(")"))
+    cmd.outputSections.push_back(tok);
   if (cmd.outputSections.size() < 2)
     warn(getCurrentLocation() + ": ignored with fewer than 2 output sections");
   else
@@ -371,8 +371,8 @@ void ScriptParser::readAsNeeded() {
   expect("(");
   bool orig = config->asNeeded;
   config->asNeeded = true;
-  while (auto tok = till(")"))
-    addFile(unquote(tok));
+  while (auto tok = readNameTill(")"))
+    addFile(tok);
   config->asNeeded = orig;
 }
 
@@ -387,8 +387,8 @@ void ScriptParser::readEntry() {
 
 void ScriptParser::readExtern() {
   expect("(");
-  while (auto tok = till(")"))
-    config->undefined.push_back(unquote(tok));
+  while (auto tok = readNameTill(")"))
+    config->undefined.push_back(tok);
 }
 
 void ScriptParser::readGroup() {
@@ -420,11 +420,11 @@ void ScriptParser::readInclude() {
 
 void ScriptParser::readInput() {
   expect("(");
-  while (auto tok = till(")")) {
+  while (auto tok = readNameTill(")")) {
     if (tok == "AS_NEEDED")
       readAsNeeded();
     else
-      addFile(unquote(tok));
+      addFile(tok);
   }
 }
 
@@ -524,7 +524,7 @@ void ScriptParser::readPhdrs() {
     cmd.name = tok;
     cmd.type = readPhdrType();
 
-    while (auto tok2 = till(";")) {
+    while (auto tok2 = readNameTill(";")) {
       if (tok2 == "FILEHDR")
         cmd.hasFilehdr = true;
       else if (tok2 == "PHDRS")
@@ -1391,9 +1391,8 @@ std::pair<uint64_t, uint64_t> ScriptParser::readInputSectionFlags() {
   uint64_t withFlags = 0;
   uint64_t withoutFlags = 0;
   expect("(");
-  while (!errorCount()) {
-    StringRef tok = readName();
-    bool without = tok.consume_front("!");
+  while (auto tok = readNameTill(")")) {
+    bool without = tok.str.consume_front("!");
     if (std::optional<uint64_t> flag = parseFlag(tok)) {
       if (without)
         withoutFlags |= *flag;
diff --git a/lld/test/ELF/linkerscript/phdrs.s b/lld/test/ELF/linkerscript/phdrs.s
index e43688bcb62f5..5559e55012a87 100644
--- a/lld/test/ELF/linkerscript/phdrs.s
+++ b/lld/test/ELF/linkerscript/phdrs.s
@@ -110,6 +110,12 @@ PHDRS { text PT_LOAD
 # RUN: not ld.lld -T unclosed2.lds a.o 2>&1 | FileCheck --check-prefix=UNCLOSED2 %s
 # UNCLOSED2: error: unclosed2.lds:1: unexpected EOF
 
+#--- unclosed3.lds
+PHDRS {all PT_LOAD FILEHDR PHDRS }
+
+# RUN: not ld.lld -T unclosed3.lds a.o 2>&1 | FileCheck --check-prefix=UNCLOSED3 %s
+# UNCLOSED3: error: unclosed3.lds:1: ; is missing before }
+
 #--- a.s
 .global _start
 _start:



More information about the llvm-commits mailing list