[lld] r228375 - [ELF] Fix -nostdlib option.

Shankar Easwaran shankare at codeaurora.org
Thu Feb 5 20:15:00 PST 2015


Author: shankare
Date: Thu Feb  5 22:15:00 2015
New Revision: 228375

URL: http://llvm.org/viewvc/llvm-project?rev=228375&view=rev
Log:
[ELF] Fix -nostdlib option.

Only search library directories explicitly specified
on the command line. Library directories specified in linker
scripts (including linker scripts specified on the command
line) are ignored.

Modified:
    lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h
    lld/trunk/lib/Driver/GnuLdDriver.cpp
    lld/trunk/lib/ReaderWriter/ELF/ELFLinkingContext.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=228375&r1=228374&r2=228375&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/ELFLinkingContext.h Thu Feb  5 22:15:00 2015
@@ -303,6 +303,10 @@ public:
     _scripts.push_back(std::move(script));
   }
 
+  /// \brief nostdlib support.
+  bool nostdlib() const { return _nostdlib; }
+  void setNoStdLib(bool nostdlib) { _nostdlib = nostdlib; }
+
 private:
   ELFLinkingContext() LLVM_DELETED_FUNCTION;
 
@@ -328,6 +332,7 @@ protected:
   bool _mergeRODataToTextSegment;
   bool _demangle;
   bool _alignSegments;
+  bool _nostdlib;
   llvm::Optional<uint64_t> _maxPageSize;
 
   OutputMagic _outputMagic;

Modified: lld/trunk/lib/Driver/GnuLdDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/GnuLdDriver.cpp?rev=228375&r1=228374&r2=228375&view=diff
==============================================================================
--- lld/trunk/lib/Driver/GnuLdDriver.cpp (original)
+++ lld/trunk/lib/Driver/GnuLdDriver.cpp Thu Feb  5 22:15:00 2015
@@ -293,7 +293,8 @@ GnuLdDriver::evalLinkerScript(ELFLinking
       ctx.getNodes().push_back(llvm::make_unique<GroupEnd>(groupSize));
     }
     if (auto *searchDir = dyn_cast<script::SearchDir>(c))
-      ctx.addSearchPath(searchDir->getSearchPath());
+      if (!ctx.nostdlib())
+        ctx.addSearchPath(searchDir->getSearchPath());
     if (auto *entry = dyn_cast<script::Entry>(c))
       ctx.setEntrySymbolName(entry->getEntryName());
     if (auto *output = dyn_cast<script::Output>(c))
@@ -386,6 +387,8 @@ bool GnuLdDriver::parse(int argc, const
 
   bool _outputOptionSet = false;
 
+  bool hasNoStdLib = false;
+
   // Ignore unknown arguments.
   for (auto unknownArg : parsedArgs->filtered(OPT_UNKNOWN))
     diag << "warning: ignoring unknown argument: "
@@ -400,9 +403,12 @@ bool GnuLdDriver::parse(int argc, const
     ctx->addSearchPath(libDir->getValue());
 
   // Add the default search directory specific to the target.
-  if (!parsedArgs->hasArg(OPT_nostdlib))
+  if (!(hasNoStdLib = parsedArgs->hasArg(OPT_nostdlib)))
     ctx->addDefaultSearchDirs(baseTriple);
 
+  // -nostdlib support.
+  ctx->setNoStdLib(hasNoStdLib);
+
   // Handle --demangle option(For compatibility)
   if (parsedArgs->getLastArg(OPT_demangle))
     ctx->setDemangleSymbols(true);

Modified: lld/trunk/lib/ReaderWriter/ELF/ELFLinkingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/ELFLinkingContext.cpp?rev=228375&r1=228374&r2=228375&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/ELFLinkingContext.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/ELFLinkingContext.cpp Thu Feb  5 22:15:00 2015
@@ -61,8 +61,8 @@ ELFLinkingContext::ELFLinkingContext(
       _mergeCommonStrings(false), _useShlibUndefines(true),
       _dynamicLinkerArg(false), _noAllowDynamicLibraries(false),
       _mergeRODataToTextSegment(true), _demangle(true), _alignSegments(true),
-      _outputMagic(OutputMagic::DEFAULT), _initFunction("_init"),
-      _finiFunction("_fini"), _sysrootPath("") {}
+      _nostdlib(false), _outputMagic(OutputMagic::DEFAULT),
+      _initFunction("_init"), _finiFunction("_fini"), _sysrootPath("") {}
 
 void ELFLinkingContext::addPasses(PassManager &pm) {
   pm.add(std::unique_ptr<Pass>(new elf::OrderPass()));

Modified: lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp?rev=228375&r1=228374&r2=228375&view=diff
==============================================================================
--- lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp (original)
+++ lld/trunk/unittests/DriverTests/GnuLdDriverTest.cpp Thu Feb  5 22:15:00 2015
@@ -29,9 +29,12 @@ protected:
 
 class LinkerScriptTest : public testing::Test {
 protected:
-  void parse(StringRef script) {
+  virtual void SetUp() {
     llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
     _ctx = std::move(GnuLdDriver::createELFLinkingContext(triple));
+  }
+
+  void parse(StringRef script) {
     std::unique_ptr<MemoryBuffer> mb = MemoryBuffer::getMemBuffer(
       script, "foo.so");
     std::string s;
@@ -214,3 +217,11 @@ TEST_F(LinkerScriptTest, Output) {
   parse("OUTPUT(\"/path/to/output\")");
   EXPECT_EQ("/path/to/output", _ctx->outputPath());
 }
+
+// Test that search paths are ignored when nostdlib is set.
+TEST_F(LinkerScriptTest, IgnoreSearchDirNoStdLib) {
+  _ctx->setNoStdLib(true);
+  parse("SEARCH_DIR(\"/foo/bar\")");
+  std::vector<StringRef> paths = _ctx->getSearchPaths();
+  EXPECT_EQ((size_t)0, paths.size());
+}





More information about the llvm-commits mailing list