From 3355670c60fc819a8c173a71335e3c7542346b0d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= <mati865@gmail.com>
Date: Tue, 8 Jun 2021 01:22:31 +0200
Subject: [PATCH] Dlltool: use default LLVM target unless specified

---
 .../llvm-dlltool/DlltoolDriver.cpp            | 25 +++++++++++++------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp b/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
index a4fd62e5557f..4a86f9a17306 100644
--- a/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
+++ b/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
@@ -17,6 +17,7 @@
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
 #include "llvm/Option/Option.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/Path.h"
 
 #include <vector>
@@ -74,6 +75,22 @@ static MachineTypes getEmulation(StringRef S) {
       .Default(IMAGE_FILE_MACHINE_UNKNOWN);
 }
 
+MachineTypes getDefaultEmulation() {
+  std::string TripleStr = sys::getDefaultTargetTriple();
+  switch (Triple(TripleStr).getArch()) {
+  case Triple::x86:
+    return COFF::IMAGE_FILE_MACHINE_I386;
+  case Triple::x86_64:
+    return COFF::IMAGE_FILE_MACHINE_AMD64;
+  case Triple::arm:
+    return COFF::IMAGE_FILE_MACHINE_ARMNT;
+  case Triple::aarch64:
+    return COFF::IMAGE_FILE_MACHINE_ARM64;
+  default:
+    return COFF::IMAGE_FILE_MACHINE_UNKNOWN;
+  }
+}
+
 int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
   DllOptTable Table;
   unsigned MissingIndex;
@@ -94,12 +111,6 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
     return 1;
   }
 
-  if (!Args.hasArgNoClaim(OPT_m) && Args.hasArgNoClaim(OPT_d)) {
-    llvm::errs() << "error: no target machine specified\n"
-                 << "supported targets: i386, i386:x86-64, arm, arm64\n";
-    return 1;
-  }
-
   for (auto *Arg : Args.filtered(OPT_UNKNOWN))
     llvm::errs() << "ignoring unknown argument: " << Arg->getAsString(Args)
                  << "\n";
@@ -119,7 +130,7 @@ int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
     return 1;
   }
 
-  COFF::MachineTypes Machine = IMAGE_FILE_MACHINE_UNKNOWN;
+  COFF::MachineTypes Machine = getDefaultEmulation();
   if (auto *Arg = Args.getLastArg(OPT_m))
     Machine = getEmulation(Arg->getValue());
 
-- 
2.31.1.windows.1

