[Driver] Pass -O0 to gold plugin when -Og is specified on the command-line

The gold plugin understands -O0..-O3, so when -Og is passed to it, the link
fails with:
> /bin/ld.gold: fatal error: Optimization level must be between 0 and 3
>
> clang-5.0: error: linker command failed with exit code 1 (use -v to see
> invocation)

Pass -O0 instead of -Og to the gold plugin when that optimization level
is specified as a command-line argument.

-- 
Joey Pabalinas
Index: lib/Driver/ToolChains/CommonArgs.cpp
===================================================================
--- lib/Driver/ToolChains/CommonArgs.cpp        (revision 324803)
+++ lib/Driver/ToolChains/CommonArgs.cpp        (working copy)
@@ -405,7 +405,9 @@
         A->getOption().matches(options::OPT_Ofast))
       OOpt = "3";
     else if (A->getOption().matches(options::OPT_O))
-      OOpt = A->getValue();
+      // The ld.gold linker needs an optimization level between 0 and 3.
+      // If optimization level is`-Og`, pass `-O0` to avoid linker errors.
+      OOpt = A->getValue()[0] == 'g' ? "0" : A->getValue();
     else if (A->getOption().matches(options::OPT_O0))
       OOpt = "0";
     if (!OOpt.empty())
Index: test/Driver/gold-lto.c
===================================================================
--- test/Driver/gold-lto.c      (revision 324803)
+++ test/Driver/gold-lto.c      (working copy)
@@ -8,6 +8,14 @@
 // CHECK-X86-64-BASIC: "-plugin-opt=foo"
 //
 // RUN: %clang -target x86_64-unknown-linux -### %t.o -flto 2>&1 \
+// RUN:     -Wl,-plugin-opt=foo -Og \
+// RUN:     | FileCheck %s --check-prefix=CHECK-X86-64-CORE2
+// CHECK-X86-64-BASIC: "-plugin" "{{.*}}{{[/\\]}}LLVMgold.{{dll|dylib|so}}"
+// CHECK-X86-64-CORE2: "-plugin-opt=mcpu=core2"
+// CHECK-X86-64-BASIC: "-plugin-opt=Og"
+// CHECK-X86-64-BASIC: "-plugin-opt=foo"
+//
+// RUN: %clang -target x86_64-unknown-linux -### %t.o -flto 2>&1 \
 // RUN:     -march=corei7 -Wl,-plugin-opt=foo -Ofast \
 // RUN:     | FileCheck %s --check-prefix=CHECK-X86-64-COREI7
 // CHECK-X86-64-COREI7: "-plugin" "{{.*}}{{[/\\]}}LLVMgold.{{dll|dylib|so}}"

Attachment: signature.asc
Description: PGP signature

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to