[lld] r183141 - [lld][ELF] Add parser for linker script ENTRY command

Rui Ueyama ruiu at google.com
Mon Jun 3 11:05:22 PDT 2013


Author: ruiu
Date: Mon Jun  3 13:05:21 2013
New Revision: 183141

URL: http://llvm.org/viewvc/llvm-project?rev=183141&view=rev
Log:
[lld][ELF] Add parser for linker script ENTRY command

This is the parser for the ENTRY command. Note that because the parsing result
is currentlyd discarded, lld can parse but just ignore the command.

Reviewers: Bigcheese

CC: llvm-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D833

Modified:
    lld/trunk/include/lld/ReaderWriter/LinkerScript.h
    lld/trunk/lib/ReaderWriter/LinkerScript.cpp
    lld/trunk/test/linker-script.test

Modified: lld/trunk/include/lld/ReaderWriter/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/LinkerScript.h?rev=183141&r1=183140&r2=183141&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/LinkerScript.h (original)
+++ lld/trunk/include/lld/ReaderWriter/LinkerScript.h Mon Jun  3 13:05:21 2013
@@ -35,6 +35,7 @@ public:
     identifier,
     l_paren,
     r_paren,
+    kw_entry,
     kw_group,
     kw_output_format,
     kw_as_needed
@@ -75,6 +76,7 @@ private:
 class Command {
 public:
   enum class Kind {
+    Entry,
     OutputFormat,
     Group,
   };
@@ -154,6 +156,27 @@ private:
   std::vector<Path> _paths;
 };
 
+class Entry : public Command {
+public:
+  explicit Entry(StringRef entryName) :
+      Command(Kind::Entry), _entryName(entryName) { }
+
+  static bool classof(const Command *c) {
+    return c->getKind() == Kind::Entry;
+  }
+
+  virtual void dump(llvm::raw_ostream &os) const {
+    os << "ENTRY(" << _entryName << ")\n";
+  }
+
+  const StringRef getEntryName() const {
+    return _entryName;
+  }
+
+private:
+  StringRef _entryName;
+};
+
 class LinkerScript {
 public:
   void dump(llvm::raw_ostream &os) const {
@@ -189,10 +212,9 @@ private:
   }
 
   OutputFormat *parseOutputFormat();
-
   Group *parseGroup();
-
   bool parseAsNeeded(std::vector<Path> &paths);
+  Entry *parseEntry();
 
 private:
   llvm::BumpPtrAllocator _alloc;

Modified: lld/trunk/lib/ReaderWriter/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/LinkerScript.cpp?rev=183141&r1=183140&r2=183141&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/LinkerScript.cpp (original)
+++ lld/trunk/lib/ReaderWriter/LinkerScript.cpp Mon Jun  3 13:05:21 2013
@@ -18,30 +18,20 @@ namespace lld {
 namespace script {
 void Token::dump(llvm::raw_ostream &os) const {
   switch (_kind) {
-  case Token::eof:
-    os << "eof: ";
-    break;
-  case Token::identifier:
-    os << "identifier: ";
-    break;
-  case Token::kw_as_needed:
-    os << "kw_as_needed: ";
-    break;
-  case Token::kw_group:
-    os << "kw_group: ";
-    break;
-  case Token::kw_output_format:
-    os << "kw_output_format: ";
-    break;
-  case Token::l_paren:
-    os << "l_paren: ";
-    break;
-  case Token::r_paren:
-    os << "r_paren: ";
-    break;
-  case Token::unknown:
-    os << "unknown: ";
+#define CASE(name)                              \
+  case Token::name:                             \
+    os << #name ": ";                           \
     break;
+  CASE(eof)
+  CASE(identifier)
+  CASE(kw_as_needed)
+  CASE(kw_entry)
+  CASE(kw_group)
+  CASE(kw_output_format)
+  CASE(l_paren)
+  CASE(r_paren)
+  CASE(unknown)
+#undef CASE
   }
   os << _range << "\n";
 }
@@ -120,6 +110,7 @@ void Lexer::lex(Token &tok) {
       .Case("OUTPUT_FORMAT", Token::kw_output_format)
       .Case("GROUP", Token::kw_group)
       .Case("AS_NEEDED", Token::kw_as_needed)
+      .Case("ENTRY", Token::kw_entry)
       .Default(Token::identifier);
     tok = Token(word, kind);
     _buffer = _buffer.drop_front(end);
@@ -191,6 +182,13 @@ LinkerScript *Parser::parse() {
     case Token::kw_as_needed:
       // Not allowed at top level.
       return nullptr;
+    case Token::kw_entry: {
+      Entry *entry = parseEntry();
+      if (!entry)
+        return nullptr;
+      _script._commands.push_back(entry);
+      break;
+    }
     default:
       // Unexpected.
       return nullptr;
@@ -200,6 +198,7 @@ LinkerScript *Parser::parse() {
   return nullptr;
 }
 
+// Parse OUTPUT_FORMAT(ident)
 OutputFormat *Parser::parseOutputFormat() {
   assert(_tok._kind == Token::kw_output_format && "Expected OUTPUT_FORMAT!");
   consumeToken();
@@ -207,7 +206,7 @@ OutputFormat *Parser::parseOutputFormat(
     return nullptr;
 
   if (_tok._kind != Token::identifier) {
-    error(_tok, "Expected identifer in OUTPUT_FORMAT.");
+    error(_tok, "Expected identifier in OUTPUT_FORMAT.");
     return nullptr;
   }
 
@@ -220,6 +219,7 @@ OutputFormat *Parser::parseOutputFormat(
   return ret;
 }
 
+// Parse GROUP(file ...)
 Group *Parser::parseGroup() {
   assert(_tok._kind == Token::kw_group && "Expected GROUP!");
   consumeToken();
@@ -251,6 +251,7 @@ Group *Parser::parseGroup() {
   return ret;
 }
 
+// Parse AS_NEEDED(file ...)
 bool Parser::parseAsNeeded(std::vector<Path> &paths) {
   assert(_tok._kind == Token::kw_as_needed && "Expected AS_NEEDED!");
   consumeToken();
@@ -266,5 +267,23 @@ bool Parser::parseAsNeeded(std::vector<P
     return false;
   return true;
 }
+
+// Parse ENTRY(ident)
+Entry *Parser::parseEntry() {
+  assert(_tok._kind == Token::kw_entry && "Expected ENTRY!");
+  consumeToken();
+  if (!expectAndConsume(Token::l_paren, "expected ("))
+    return nullptr;
+  if (_tok._kind != Token::identifier) {
+    error(_tok, "expected identifier in ENTRY");
+    return nullptr;
+  }
+  StringRef entryName(_tok._range);
+  consumeToken();
+  if (!expectAndConsume(Token::r_paren, "expected )"))
+    return nullptr;
+  return new (_alloc) Entry(entryName);
+}
+
 } // end namespace script
 } // end namespace lld

Modified: lld/trunk/test/linker-script.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/linker-script.test?rev=183141&r1=183140&r2=183141&view=diff
==============================================================================
--- lld/trunk/test/linker-script.test (original)
+++ lld/trunk/test/linker-script.test Mon Jun  3 13:05:21 2013
@@ -3,6 +3,7 @@
 
 OUTPUT_FORMAT(elf64-x86-64)
 GROUP ( /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libc_nonshared.a  AS_NEEDED ( /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 ) )
+ENTRY(init)
 
 /*
 CHECK: kw_output_format: OUTPUT_FORMAT
@@ -18,7 +19,12 @@ CHECK: l_paren: (
 CHECK: identifier: /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
 CHECK: r_paren: )
 CHECK: r_paren: )
+CHECK: kw_entry: ENTRY
+CHECK: l_paren: (
+CHECK: identifier: init
+CHECK: r_paren: )
 CHECK: eof:
 CHECK: OUTPUT_FORMAT(elf64-x86-64)
 CHECK: GROUP(/lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libc_nonshared.a AS_NEEDED(/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2))
+CHECK: ENTRY(init)
 */





More information about the llvm-commits mailing list