fhahn created this revision.
fhahn added a reviewer: rafael.
fhahn added a subscriber: cfe-commits.
Herald added a subscriber: dschuff.

This patch adds a check and an error message to gnutools::Linker::ConstructJob 
in case the architecture is not supported. For most other operating systems, 
the error message is created in lib/Basic/Targets.cpp:AllocateTarget, but when 
construction the linker arguments for the gnutools linker a supported 
architecture is required.

I am not sure if it would be better to validate target triples earlier.


https://reviews.llvm.org/D27066

Files:
  lib/Driver/Tools.cpp
  test/Driver/unsupported-target-arch.c


Index: test/Driver/unsupported-target-arch.c
===================================================================
--- /dev/null
+++ test/Driver/unsupported-target-arch.c
@@ -0,0 +1,25 @@
+// Tests that clang does not crash with invalid architectures in target 
triples.
+//
+// RUN: not %clang --target=noarch-unknown-linux -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-LINUX %s
+// CHECK-NOARCH-LINUX: error: unknown target triple 'noarch-unknown-linux', 
please use -triple or -arch
+//
+// RUN: not %clang --target=noarch-unknown-darwin -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-DARWIN %s
+// CHECK-NOARCH-DARWIN: error: unknown target triple 
'unknown-unknown-macosx{{.+}}', please use -triple or -arch
+//
+// RUN: not %clang --target=noarch-unknown-windows -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-WINDOWS %s
+// CHECK-NOARCH-WINDOWS: error: unknown target triple 
'noarch-unknown-windows-{{.+}}', please use -triple or -arch
+//
+// RUN: not %clang --target=noarch-unknown-freebsd -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-FREEBSD %s
+// CHECK-NOARCH-FREEBSD: error: unknown target triple 
'noarch-unknown-freebsd', please use -triple or -arch
+//
+// RUN: not %clang --target=noarch-unknown-netbsd -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-NETBSD %s
+// CHECK-NOARCH-NETBSD: error: unknown target triple 'noarch-unknown-netbsd', 
please use -triple or -arch
+//
+// RUN: not %clang --target=noarch-unknown-nacl -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-NACL %s
+// CHECK-NOARCH-NACL:  error: the target architecture 'noarch' is not 
supported by the target 'Native Client'
Index: lib/Driver/Tools.cpp
===================================================================
--- lib/Driver/Tools.cpp
+++ lib/Driver/Tools.cpp
@@ -9941,7 +9941,7 @@
       return "elf32_x86_64";
     return "elf_x86_64";
   default:
-    llvm_unreachable("Unexpected arch");
+    return nullptr;
   }
 }
 
@@ -10014,8 +10014,13 @@
     CmdArgs.push_back("--eh-frame-hdr");
   }
 
+  const char *LDMOption = getLDMOption(ToolChain.getTriple(), Args);
+  if (!LDMOption) {
+    D.Diag(diag::err_target_unknown_triple) << Triple.str();
+    return;
+  }
   CmdArgs.push_back("-m");
-  CmdArgs.push_back(getLDMOption(ToolChain.getTriple(), Args));
+  CmdArgs.push_back(LDMOption);
 
   if (Args.hasArg(options::OPT_static)) {
     if (Arch == llvm::Triple::arm || Arch == llvm::Triple::armeb ||


Index: test/Driver/unsupported-target-arch.c
===================================================================
--- /dev/null
+++ test/Driver/unsupported-target-arch.c
@@ -0,0 +1,25 @@
+// Tests that clang does not crash with invalid architectures in target triples.
+//
+// RUN: not %clang --target=noarch-unknown-linux -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-LINUX %s
+// CHECK-NOARCH-LINUX: error: unknown target triple 'noarch-unknown-linux', please use -triple or -arch
+//
+// RUN: not %clang --target=noarch-unknown-darwin -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-DARWIN %s
+// CHECK-NOARCH-DARWIN: error: unknown target triple 'unknown-unknown-macosx{{.+}}', please use -triple or -arch
+//
+// RUN: not %clang --target=noarch-unknown-windows -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-WINDOWS %s
+// CHECK-NOARCH-WINDOWS: error: unknown target triple 'noarch-unknown-windows-{{.+}}', please use -triple or -arch
+//
+// RUN: not %clang --target=noarch-unknown-freebsd -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-FREEBSD %s
+// CHECK-NOARCH-FREEBSD: error: unknown target triple 'noarch-unknown-freebsd', please use -triple or -arch
+//
+// RUN: not %clang --target=noarch-unknown-netbsd -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-NETBSD %s
+// CHECK-NOARCH-NETBSD: error: unknown target triple 'noarch-unknown-netbsd', please use -triple or -arch
+//
+// RUN: not %clang --target=noarch-unknown-nacl -o %t.o %s 2> %t.err
+// RUN: FileCheck --input-file=%t.err --check-prefix=CHECK-NOARCH-NACL %s
+// CHECK-NOARCH-NACL:  error: the target architecture 'noarch' is not supported by the target 'Native Client'
Index: lib/Driver/Tools.cpp
===================================================================
--- lib/Driver/Tools.cpp
+++ lib/Driver/Tools.cpp
@@ -9941,7 +9941,7 @@
       return "elf32_x86_64";
     return "elf_x86_64";
   default:
-    llvm_unreachable("Unexpected arch");
+    return nullptr;
   }
 }
 
@@ -10014,8 +10014,13 @@
     CmdArgs.push_back("--eh-frame-hdr");
   }
 
+  const char *LDMOption = getLDMOption(ToolChain.getTriple(), Args);
+  if (!LDMOption) {
+    D.Diag(diag::err_target_unknown_triple) << Triple.str();
+    return;
+  }
   CmdArgs.push_back("-m");
-  CmdArgs.push_back(getLDMOption(ToolChain.getTriple(), Args));
+  CmdArgs.push_back(LDMOption);
 
   if (Args.hasArg(options::OPT_static)) {
     if (Arch == llvm::Triple::arm || Arch == llvm::Triple::armeb ||
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to