[lld] r203892 - [PECOFF] Fix link order.

Rui Ueyama ruiu at google.com
Thu Mar 13 22:59:16 PDT 2014


Author: ruiu
Date: Fri Mar 14 00:59:16 2014
New Revision: 203892

URL: http://llvm.org/viewvc/llvm-project?rev=203892&view=rev
Log:
[PECOFF] Fix link order.

LLD fails to link symbol "_main" if the symbol is in a library file and
the library file is given as a bare argument (i.e. not with /defaultlib
option). It's because library files given as bare arguments are processed
before other libraries given with /defaultlib, so when Linker finds msvcrtd
needs a definition for "_main", the file providing the main function has
already been processed and skipped. Linker don't revisit libraries if it's
not given with /defaultlib.

To fix it this patch change the way of command line handling; files end with
".lib" are treated as if they are given with /defaultlib. I don't believe
it's 100% correct behavior but it's better than before.

Modified:
    lld/trunk/lib/Driver/WinLinkDriver.cpp
    lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp

Modified: lld/trunk/lib/Driver/WinLinkDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/WinLinkDriver.cpp?rev=203892&r1=203891&r2=203892&view=diff
==============================================================================
--- lld/trunk/lib/Driver/WinLinkDriver.cpp (original)
+++ lld/trunk/lib/Driver/WinLinkDriver.cpp Fri Mar 14 00:59:16 2014
@@ -1144,15 +1144,22 @@ bool WinLinkDriver::doParse(int argc, co
     }
   }
 
-  // Move files with ".lib" extension at the end of the input file list. Say
-  // foo.obj depends on bar.lib. The linker needs to accept both "bar.lib
-  // foo.obj" and "foo.obj bar.lib".
-  auto compfn = [](StringRef a, StringRef b) {
-    return !a.endswith_lower(".lib") && b.endswith_lower(".lib");
-  };
-  std::stable_sort(inputFiles.begin(), inputFiles.end(), compfn);
-  for (StringRef path : inputFiles)
-    files.push_back(std::unique_ptr<FileNode>(new PECOFFFileNode(ctx, path)));
+  // Arguments after "--" are interpreted as filenames even if they
+  // start with a hypen or a slash. This is not compatible with link.exe
+  // but useful for us to test lld on Unix.
+  if (llvm::opt::Arg *dashdash = parsedArgs->getLastArg(OPT_DASH_DASH))
+    for (const StringRef value : dashdash->getValues())
+      inputFiles.push_back(value);
+
+  // Prepare objects to add them to input graph.
+  for (StringRef path : inputFiles) {
+    path = ctx.allocate(path);
+    if (path.endswith_lower(".lib")) {
+      libraries.push_back(std::unique_ptr<FileNode>(new PECOFFLibraryNode(ctx, path)));
+    } else {
+      files.push_back(std::unique_ptr<FileNode>(new PECOFFFileNode(ctx, path)));
+    }
+  }
 
   // Use the default entry name if /entry option is not given.
   if (ctx.entrySymbolName().empty() && !parsedArgs->getLastArg(OPT_noentry))
@@ -1180,17 +1187,6 @@ bool WinLinkDriver::doParse(int argc, co
     for (const StringRef symbolName : ctx.initialUndefinedSymbols())
       ctx.addDeadStripRoot(symbolName);
 
-  // Arguments after "--" are interpreted as filenames even if they
-  // start with a hypen or a slash. This is not compatible with link.exe
-  // but useful for us to test lld on Unix.
-  if (llvm::opt::Arg *dashdash = parsedArgs->getLastArg(OPT_DASH_DASH)) {
-    for (const StringRef value : dashdash->getValues()) {
-      std::unique_ptr<FileNode> elem(
-          new PECOFFFileNode(ctx, ctx.allocate(value)));
-      files.push_back(std::move(elem));
-    }
-  }
-
   // Add the libraries specified by /defaultlib unless they are already added
   // nor blacklisted by /nodefaultlib.
   if (!ctx.getNoDefaultLibAll())

Modified: lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp?rev=203892&r1=203891&r2=203892&view=diff
==============================================================================
--- lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp (original)
+++ lld/trunk/unittests/DriverTests/WinLinkDriverTest.cpp Fri Mar 14 00:59:16 2014
@@ -137,14 +137,13 @@ TEST_F(WinLinkParserTest, Libpath) {
 //
 
 TEST_F(WinLinkParserTest, InputOrder) {
-  EXPECT_TRUE(parse("link.exe", "b.lib", "b.obj", "c.obj", "a.lib", "a.obj",
+  EXPECT_TRUE(parse("link.exe", "a.lib", "b.obj", "c.obj", "a.lib", "d.obj",
                     nullptr));
-  EXPECT_EQ(6, inputFileCount());
+  EXPECT_EQ(4, inputFileCount());
   EXPECT_EQ("b.obj", inputFile(0));
   EXPECT_EQ("c.obj", inputFile(1));
-  EXPECT_EQ("a.obj", inputFile(2));
-  EXPECT_EQ("b.lib", inputFile(3));
-  EXPECT_EQ("a.lib", inputFile(4));
+  EXPECT_EQ("d.obj", inputFile(2));
+  EXPECT_EQ("a.lib", inputFile(3, 0));
 }
 
 //





More information about the llvm-commits mailing list