r341107 - Refactor Addlibgcc to make the when and what logic more straightfoward.

Sterling Augustine via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 30 13:07:23 PDT 2018


Author: saugustine
Date: Thu Aug 30 13:07:23 2018
New Revision: 341107

URL: http://llvm.org/viewvc/llvm-project?rev=341107&view=rev
Log:
Refactor Addlibgcc to make the when and what logic more straightfoward.

No functional change. A forthcoming change will add support
for -shared-libgcc.


Modified:
    cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp

Modified: cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp?rev=341107&r1=341106&r2=341107&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp (original)
+++ cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp Thu Aug 30 13:07:23 2018
@@ -1126,23 +1126,41 @@ static void AddLibgcc(const llvm::Triple
   bool IsIAMCU = Triple.isOSIAMCU();
   bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) ||
                       Args.hasArg(options::OPT_static);
-  if (!D.CCCIsCXX())
+  // The driver ignores -shared-libgcc and therefore treats such cases as
+  // unspecified.  Breaking out the two variables as below makes the current
+  // behavior explicit.
+  bool UnspecifiedLibgcc = !StaticLibgcc;
+  bool SharedLibgcc = !StaticLibgcc;
+
+  // Gcc adds libgcc arguments in various ways:
+  //
+  // gcc <none>: -lgcc --as-needed -lgcc_s --no-as-needed
+  // g++ <none>:                   -lgcc_s               -lgcc
+  // gcc shared:                   -lgcc_s               -lgcc
+  // g++ shared:                   -lgcc_s               -lgcc
+  // gcc static: -lgcc             -lgcc_eh
+  // g++ static: -lgcc             -lgcc_eh
+  //
+  // Also, certain targets need additional adjustments.
+
+  bool LibGccFirst = (D.CCCIsCC() && UnspecifiedLibgcc) || StaticLibgcc;
+  if (LibGccFirst)
     CmdArgs.push_back("-lgcc");
 
-  if (StaticLibgcc || isAndroid) {
-    if (D.CCCIsCXX())
-      CmdArgs.push_back("-lgcc");
-  } else {
-    if (!D.CCCIsCXX() && !isCygMing)
-      CmdArgs.push_back("--as-needed");
+  bool AsNeeded = D.CCCIsCC() && !StaticLibgcc && !isCygMing;
+  if (AsNeeded)
+    CmdArgs.push_back("--as-needed");
+
+  if (UnspecifiedLibgcc || SharedLibgcc)
     CmdArgs.push_back("-lgcc_s");
-    if (!D.CCCIsCXX() && !isCygMing)
-      CmdArgs.push_back("--no-as-needed");
-  }
 
-  if (StaticLibgcc && !isAndroid && !IsIAMCU)
+  else if (StaticLibgcc && !isAndroid && !IsIAMCU)
     CmdArgs.push_back("-lgcc_eh");
-  else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX())
+
+  if (AsNeeded)
+    CmdArgs.push_back("--no-as-needed");
+
+  if (!LibGccFirst)
     CmdArgs.push_back("-lgcc");
 
   // According to Android ABI, we have to link with libdl if we are




More information about the cfe-commits mailing list