[lld] r227694 - Implement semantic action for SEARCH_DIR linker script command

Rafael Auler rafaelauler at gmail.com
Sat Jan 31 14:42:20 PST 2015


Author: rafauler
Date: Sat Jan 31 16:42:19 2015
New Revision: 227694

URL: http://llvm.org/viewvc/llvm-project?rev=227694&view=rev
Log:
Implement semantic action for SEARCH_DIR linker script command

This is needed, among others by the FreeBSD kernel linker script.

Patch by Davide Italiano!

Reviewers: ruiu, rafaelauler

Differential Revision: http://reviews.llvm.org/D7220


Modified:
    lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h
    lld/trunk/lib/Driver/GnuLdDriver.cpp
    lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp

Modified: lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h?rev=227694&r1=227693&r2=227694&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h Sat Jan 31 16:42:19 2015
@@ -268,6 +268,9 @@ public:
     return true;
   }
 
+  // Retrieve search path list.
+  StringRefVector getSearchPaths() { return _inputSearchPaths; };
+
   // By default, the linker would merge sections that are read only with
   // segments that have read and execute permissions. When the user specifies a
   // flag --rosegment, a separate segment needs to be created.

Modified: lld/trunk/lib/Driver/GnuLdDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/GnuLdDriver.cpp?rev=227694&r1=227693&r2=227694&view=diff
==============================================================================
--- lld/trunk/lib/Driver/GnuLdDriver.cpp (original)
+++ lld/trunk/lib/Driver/GnuLdDriver.cpp Sat Jan 31 16:42:19 2015
@@ -280,11 +280,16 @@ GnuLdDriver::evalLinkerScript(ELFLinking
   if (!script)
     return LinkerScriptReaderError::parse_error;
   // Evaluate script commands.
-  // Currently we only recognize GROUP() command.
-  for (const script::Command *c : script->_commands)
+  // Currently we only recognize this subset of linker script commands:
+  // - GROUP()
+  // - SEARCH_DIR()
+  for (const script::Command *c : script->_commands) {
     if (auto *group = dyn_cast<script::Group>(c))
       if (std::error_code ec = evaluateLinkerScriptGroup(ctx, path, group, diag))
         return ec;
+    if (auto *searchDir = dyn_cast<script::SearchDir>(c))
+      ctx.addSearchPath(searchDir->getSearchPath());
+  }
   return std::error_code();
 }
 

Modified: lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp?rev=227694&r1=227693&r2=227694&view=diff
==============================================================================
--- lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp (original)
+++ lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp Sat Jan 31 16:42:19 2015
@@ -177,3 +177,17 @@ TEST_F(GnuLdParserTest, LinkerScriptGrou
   EXPECT_EQ("/y", cast<FileNode>(nodes[2].get())->getFile()->path());
   EXPECT_EQ(2, cast<GroupEnd>(nodes[3].get())->getSize());
 }
+
+TEST_F(GnuLdParserTest, LinkerScriptSearchDir) {
+  parse("ld", "a.o", nullptr);
+  std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
+    "SEARCH_DIR(\"/foo/bar\")", "foo.so");
+  std::string s;
+  raw_string_ostream out(s);
+  std::error_code ec = GnuLdDriver::evalLinkerScript(
+    *_context, std::move(mb), out);
+  EXPECT_FALSE(ec);
+  std::vector<StringRef> searchPaths = _context->getSearchPaths();
+  EXPECT_EQ((size_t)2, searchPaths.size());
+  EXPECT_EQ("/foo/bar", searchPaths[1]);
+}





More information about the llvm-commits mailing list