[lld] r239073 - COFF: Add /failifmismatch option.

Rui Ueyama ruiu at google.com
Thu Jun 4 12:21:25 PDT 2015


Author: ruiu
Date: Thu Jun  4 14:21:24 2015
New Revision: 239073

URL: http://llvm.org/viewvc/llvm-project?rev=239073&view=rev
Log:
COFF: Add /failifmismatch option.

Added:
    lld/trunk/test/COFF/failifmismatch.test
Modified:
    lld/trunk/COFF/Config.h
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/Driver.h
    lld/trunk/COFF/DriverUtils.cpp

Modified: lld/trunk/COFF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Config.h?rev=239073&r1=239072&r2=239073&view=diff
==============================================================================
--- lld/trunk/COFF/Config.h (original)
+++ lld/trunk/COFF/Config.h Thu Jun  4 14:21:24 2015
@@ -13,6 +13,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Object/COFF.h"
 #include <cstdint>
+#include <map>
 #include <set>
 #include <string>
 
@@ -35,6 +36,9 @@ public:
   std::set<StringRef> NoDefaultLibs;
   bool NoDefaultLibAll = false;
 
+  // Used by /failifmismatch option.
+  std::map<StringRef, StringRef> MustMatch;
+
   uint64_t ImageBase = 0x140000000;
   uint64_t StackReserve = 1024 * 1024;
   uint64_t StackCommit = 4096;

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=239073&r1=239072&r2=239073&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Thu Jun  4 14:21:24 2015
@@ -110,6 +110,11 @@ LinkerDriver::parseDirectives(StringRef
     return EC;
   std::unique_ptr<llvm::opt::InputArgList> Args = std::move(ArgsOrErr.get());
 
+  // Handle /failifmismatch
+  if (auto EC = checkFailIfMismatch(Args.get()))
+    return EC;
+
+  // Handle /defaultlib
   for (auto *Arg : Args->filtered(OPT_defaultlib)) {
     if (Optional<StringRef> Path = findLib(Arg->getValue())) {
       auto FileOrErr = openFile(*Path);
@@ -297,6 +302,12 @@ bool LinkerDriver::link(int Argc, const
     }
   }
 
+  // Handle /failifmismatch
+  if (auto EC = checkFailIfMismatch(Args.get())) {
+    llvm::errs() << "/failifmismatch: " << EC.message() << "\n";
+    return false;
+  }
+
   // Create a list of input files. Files can be given as arguments
   // for /defaultlib option.
   std::vector<StringRef> Inputs;

Modified: lld/trunk/COFF/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.h?rev=239073&r1=239072&r2=239073&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.h (original)
+++ lld/trunk/COFF/Driver.h Thu Jun  4 14:21:24 2015
@@ -90,6 +90,12 @@ std::error_code parseVersion(StringRef A
 std::error_code parseSubsystem(StringRef Arg, WindowsSubsystem *Sys,
                                uint32_t *Major, uint32_t *Minor);
 
+// Parses a string in the form of "key=value" and check
+// if value matches previous values for the key.
+// This feature used in the directive section to reject
+// incompatible objects.
+std::error_code checkFailIfMismatch(llvm::opt::InputArgList *Args);
+
 // Create enum with OPT_xxx values for each option in Options.td
 enum {
   OPT_INVALID = 0,

Modified: lld/trunk/COFF/DriverUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/DriverUtils.cpp?rev=239073&r1=239072&r2=239073&view=diff
==============================================================================
--- lld/trunk/COFF/DriverUtils.cpp (original)
+++ lld/trunk/COFF/DriverUtils.cpp Thu Jun  4 14:21:24 2015
@@ -13,6 +13,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "Config.h"
 #include "Driver.h"
 #include "Error.h"
 #include "Memory.h"
@@ -112,6 +113,29 @@ std::error_code parseSubsystem(StringRef
   return std::error_code();
 }
 
+// Parses a string in the form of "key=value" and check
+// if value matches previous values for the same key.
+std::error_code checkFailIfMismatch(llvm::opt::InputArgList *Args) {
+  for (auto *Arg : Args->filtered(OPT_failifmismatch)) {
+    StringRef K, V;
+    std::tie(K, V) = StringRef(Arg->getValue()).split('=');
+    if (K.empty() || V.empty()) {
+      llvm::errs() << "/failifmismatch: invalid argument: "
+                   << Arg->getValue() << "\n";
+      return make_error_code(LLDError::InvalidOption);
+    }
+    StringRef Existing = Config->MustMatch[K];
+    if (!Existing.empty() && V != Existing) {
+      llvm::errs() << "/failifmismatch: mismatch detected: "
+                   << Existing << " and " << V
+                   << " for key " << K << "\n";
+      return make_error_code(LLDError::InvalidOption);
+    }
+    Config->MustMatch[K] = V;
+  }
+  return std::error_code();
+}
+
 // Create OptTable
 
 // Create prefix string literals used in Options.td

Added: lld/trunk/test/COFF/failifmismatch.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/failifmismatch.test?rev=239073&view=auto
==============================================================================
--- lld/trunk/test/COFF/failifmismatch.test (added)
+++ lld/trunk/test/COFF/failifmismatch.test Thu Jun  4 14:21:24 2015
@@ -0,0 +1,11 @@
+# RUN: lld -flavor link2 /entry:main /subsystem:console /out:%t.exe \
+# RUN:   %p/Inputs/ret42.obj
+
+# RUN: lld -flavor link2 /entry:main /subsystem:console /out:%t.exe \
+# RUN:   %p/Inputs/ret42.obj /failifmismatch:k1=v1 /failifmismatch:k2=v1
+
+# RUN: lld -flavor link2 /entry:main /subsystem:console /out:%t.exe \
+# RUN:   %p/Inputs/ret42.obj /failifmismatch:k1=v1 /failifmismatch:k1=v1
+
+# RUN: not lld -flavor link2 /entry:main /subsystem:console /out:%t.exe \
+# RUN:   %p/Inputs/ret42.obj /failifmismatch:k1=v1 /failifmismatch:k1=v2





More information about the llvm-commits mailing list