[PATCH] Implement semantic action for SEARCH_DIR linker script command

Davide Italiano davide at freebsd.org
Wed Jan 28 19:31:10 PST 2015


Attempt to write an unit test and address Ed's and Rui's comments.


http://reviews.llvm.org/D7220

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

Index: include/lld/ReaderWriter/ELFLinkingContext.h
===================================================================
--- include/lld/ReaderWriter/ELFLinkingContext.h
+++ include/lld/ReaderWriter/ELFLinkingContext.h
@@ -268,6 +268,9 @@
     return true;
   }
 
+  // Retrieve search path list.
+  virtual 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.
Index: lib/Driver/GnuLdDriver.cpp
===================================================================
--- lib/Driver/GnuLdDriver.cpp
+++ lib/Driver/GnuLdDriver.cpp
@@ -280,11 +280,16 @@
   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();
 }
 
Index: unittests/DriverTests/GnuLdDriverTest.cpp
===================================================================
--- unittests/DriverTests/GnuLdDriverTest.cpp
+++ unittests/DriverTests/GnuLdDriverTest.cpp
@@ -177,3 +177,17 @@
   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(\"/usr/lib\")", "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("/usr/lib", searchPaths[1]);
+}

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7220.18937.patch
Type: text/x-patch
Size: 2332 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150129/99a29109/attachment.bin>


More information about the llvm-commits mailing list