[lld] r316370 - lld::COFF: better behavior when using as a library

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 23 13:03:32 PDT 2017


Author: ruiu
Date: Mon Oct 23 13:03:32 2017
New Revision: 316370

URL: http://llvm.org/viewvc/llvm-project?rev=316370&view=rev
Log:
lld::COFF: better behavior when using as a library

Previously, the COFF driver would call exit(0) when called
as a library.  Now it takes `ExitEarly` option, and if it
is false, it doesn't exit.  So it is now more library-friendly.

Furthermore, link() calls freeArena() before returning, to
clean up resources.

Based on an Andrew Kelley's patch.

Differential Revision: https://reviews.llvm.org/D39202

Modified:
    lld/trunk/COFF/Config.h
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/Error.cpp
    lld/trunk/COFF/Error.h
    lld/trunk/MinGW/Driver.cpp
    lld/trunk/include/lld/Common/Driver.h
    lld/trunk/tools/lld/lld.cpp

Modified: lld/trunk/COFF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Config.h?rev=316370&r1=316369&r2=316370&view=diff
==============================================================================
--- lld/trunk/COFF/Config.h (original)
+++ lld/trunk/COFF/Config.h Mon Oct 23 13:03:32 2017
@@ -166,6 +166,7 @@ struct Configuration {
   uint32_t MinorImageVersion = 0;
   uint32_t MajorOSVersion = 6;
   uint32_t MinorOSVersion = 0;
+  bool CanExitEarly = false;
   bool DynamicBase = true;
   bool AllowBind = true;
   bool NxCompat = true;

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=316370&r1=316369&r2=316370&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Mon Oct 23 13:03:32 2017
@@ -54,18 +54,25 @@ BumpPtrAllocator BAlloc;
 StringSaver Saver{BAlloc};
 std::vector<SpecificAllocBase *> SpecificAllocBase::Instances;
 
-bool link(ArrayRef<const char *> Args, raw_ostream &Diag) {
+bool link(ArrayRef<const char *> Args, bool CanExitEarly, raw_ostream &Diag) {
   ErrorCount = 0;
   ErrorOS = &Diag;
 
   Config = make<Configuration>();
   Config->Argv = {Args.begin(), Args.end()};
   Config->ColorDiagnostics = ErrorOS->has_colors();
+  Config->CanExitEarly = CanExitEarly;
 
   Symtab = make<SymbolTable>();
 
   Driver = make<LinkerDriver>();
   Driver->link(Args);
+
+  // Call exit() if we can to avoid calling destructors.
+  if (CanExitEarly)
+    exitLld(ErrorCount ? 1 : 0);
+
+  freeArena();
   return !ErrorCount;
 }
 
@@ -1087,7 +1094,7 @@ void LinkerDriver::link(ArrayRef<const c
   if (!Args.hasArg(OPT_INPUT)) {
     fixupExports();
     createImportLibrary(/*AsLib=*/true);
-    exit(0);
+    return;
   }
 
   // Handle /delayload
@@ -1179,7 +1186,7 @@ void LinkerDriver::link(ArrayRef<const c
   // This is useful because MSVC link.exe can generate complete PDBs.
   if (Args.hasArg(OPT_msvclto)) {
     invokeMSVC(Args);
-    exit(0);
+    return;
   }
 
   // Do LTO by compiling bitcode input files to a set of native COFF files then
@@ -1273,12 +1280,6 @@ void LinkerDriver::link(ArrayRef<const c
 
   // Write the result.
   writeResult();
-
-  if (ErrorCount)
-    return;
-
-  // Call exit to avoid calling destructors.
-  exit(0);
 }
 
 } // namespace coff

Modified: lld/trunk/COFF/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Error.cpp?rev=316370&r1=316369&r2=316370&view=diff
==============================================================================
--- lld/trunk/COFF/Error.cpp (original)
+++ lld/trunk/COFF/Error.cpp Mon Oct 23 13:03:32 2017
@@ -32,7 +32,7 @@ namespace coff {
 uint64_t ErrorCount;
 raw_ostream *ErrorOS;
 
-static LLVM_ATTRIBUTE_NORETURN void exitLld(int Val) {
+LLVM_ATTRIBUTE_NORETURN void exitLld(int Val) {
   // Dealloc/destroy ManagedStatic variables before calling
   // _exit(). In a non-LTO build, this is a nop. In an LTO
   // build allows us to get the output of -time-passes.
@@ -78,7 +78,8 @@ void error(const Twine &Msg) {
     print("error: ", raw_ostream::RED);
     *ErrorOS << "too many errors emitted, stopping now"
              << " (use /ERRORLIMIT:0 to see all errors)\n";
-    exitLld(1);
+    if (Config->CanExitEarly)
+      exitLld(1);
   }
 
   ++ErrorCount;

Modified: lld/trunk/COFF/Error.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Error.h?rev=316370&r1=316369&r2=316370&view=diff
==============================================================================
--- lld/trunk/COFF/Error.h (original)
+++ lld/trunk/COFF/Error.h Mon Oct 23 13:03:32 2017
@@ -27,6 +27,8 @@ LLVM_ATTRIBUTE_NORETURN void fatal(const
 LLVM_ATTRIBUTE_NORETURN void fatal(std::error_code EC, const Twine &Prefix);
 LLVM_ATTRIBUTE_NORETURN void fatal(llvm::Error &Err, const Twine &Prefix);
 
+LLVM_ATTRIBUTE_NORETURN void exitLld(int Val);
+
 template <class T> T check(ErrorOr<T> V, const Twine &Prefix) {
   if (auto EC = V.getError())
     fatal(EC, Prefix);

Modified: lld/trunk/MinGW/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/MinGW/Driver.cpp?rev=316370&r1=316369&r2=316370&view=diff
==============================================================================
--- lld/trunk/MinGW/Driver.cpp (original)
+++ lld/trunk/MinGW/Driver.cpp Mon Oct 23 13:03:32 2017
@@ -217,5 +217,5 @@ bool mingw::link(ArrayRef<const char *>
   std::vector<const char *> Vec;
   for (const std::string &S : LinkArgs)
     Vec.push_back(S.c_str());
-  return coff::link(Vec);
+  return coff::link(Vec, true);
 }

Modified: lld/trunk/include/lld/Common/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Common/Driver.h?rev=316370&r1=316369&r2=316370&view=diff
==============================================================================
--- lld/trunk/include/lld/Common/Driver.h (original)
+++ lld/trunk/include/lld/Common/Driver.h Mon Oct 23 13:03:32 2017
@@ -15,7 +15,7 @@
 
 namespace lld {
 namespace coff {
-bool link(llvm::ArrayRef<const char *> Args,
+bool link(llvm::ArrayRef<const char *> Args, bool CanExitEarly,
           llvm::raw_ostream &Diag = llvm::errs());
 }
 

Modified: lld/trunk/tools/lld/lld.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/tools/lld/lld.cpp?rev=316370&r1=316369&r2=316370&view=diff
==============================================================================
--- lld/trunk/tools/lld/lld.cpp (original)
+++ lld/trunk/tools/lld/lld.cpp Mon Oct 23 13:03:32 2017
@@ -115,7 +115,7 @@ int main(int Argc, const char **Argv) {
       return !mingw::link(Args);
     return !elf::link(Args, true);
   case WinLink:
-    return !coff::link(Args);
+    return !coff::link(Args, true);
   case Darwin:
     return !mach_o::link(Args);
   default:




More information about the llvm-commits mailing list