[clang] f861e33 - [Driver] Reject -Wa, -mrelax-relocations= for non-ELF

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 15 18:01:16 PDT 2024


Author: Fangrui Song
Date: 2024-08-15T18:01:10-07:00
New Revision: f861e33912732d3de05369cdff852968fcdcb3a0

URL: https://github.com/llvm/llvm-project/commit/f861e33912732d3de05369cdff852968fcdcb3a0
DIFF: https://github.com/llvm/llvm-project/commit/f861e33912732d3de05369cdff852968fcdcb3a0.diff

LOG: [Driver] Reject -Wa,-mrelax-relocations= for non-ELF

Added: 
    

Modified: 
    clang/lib/Driver/ToolChains/Clang.cpp
    clang/test/Driver/relax.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 2b7241964a345d..daffa7cf96978a 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -2582,6 +2582,7 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
   bool TakeNextArg = false;
 
   const llvm::Triple &Triple = C.getDefaultToolChain().getTriple();
+  bool IsELF = Triple.isOSBinFormatELF();
   bool Crel = false, ExperimentalCrel = false;
   bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations();
   bool UseNoExecStack = false;
@@ -2621,10 +2622,16 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
         continue; // LLVM handles bigobj automatically
 
       auto Equal = Value.split('=');
-      auto checkArg = [&](std::initializer_list<const char *> Set) {
-        if (!llvm::is_contained(Set, Equal.second))
+      auto checkArg = [&](bool ValidTarget,
+                          std::initializer_list<const char *> Set) {
+        if (!ValidTarget) {
+          D.Diag(diag::err_drv_unsupported_opt_for_target)
+              << (Twine("-Wa,") + Equal.first + "=").str()
+              << Triple.getTriple();
+        } else if (!llvm::is_contained(Set, Equal.second)) {
           D.Diag(diag::err_drv_unsupported_option_argument)
               << (Twine("-Wa,") + Equal.first + "=").str() << Equal.second;
+        }
       };
       switch (C.getDefaultToolChain().getArch()) {
       default:
@@ -2634,7 +2641,7 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
         if (Equal.first == "-mrelax-relocations" ||
             Equal.first == "--mrelax-relocations") {
           UseRelaxRelocations = Equal.second == "yes";
-          checkArg({"yes", "no"});
+          checkArg(IsELF, {"yes", "no"});
           continue;
         }
         if (Value == "-msse2avx") {
@@ -2656,7 +2663,7 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
         if (Equal.first == "-mimplicit-it") {
           // Only store the value; the last value set takes effect.
           ImplicitIt = Equal.second;
-          checkArg({"always", "never", "arm", "thumb"});
+          checkArg(true, {"always", "never", "arm", "thumb"});
           continue;
         }
         if (Value == "-mthumb")

diff  --git a/clang/test/Driver/relax.c b/clang/test/Driver/relax.c
index 9315a0045f1f43..93249cac7f40f8 100644
--- a/clang/test/Driver/relax.c
+++ b/clang/test/Driver/relax.c
@@ -8,3 +8,6 @@
 
 // RUN: not %clang -### --target=aarch64 -c -Wa,-mrelax-relocations=no %s 2>&1 | FileCheck %s --check-prefix=ERR2
 // ERR2: error: unsupported argument '-mrelax-relocations=no' to option '-Wa,'
+
+// RUN: not %clang -### --target=x86_64-apple-darwin -c -Wa,-mrelax-relocations=no %s 2>&1 | FileCheck %s --check-prefix=ERR3
+// ERR3: error: unsupported option '-Wa,-mrelax-relocations=' for target 'x86_64-apple-darwin'


        


More information about the cfe-commits mailing list