Index: lld/COFF/Driver.cpp =================================================================== --- lld/COFF/Driver.cpp +++ lld/COFF/Driver.cpp @@ -6,9 +6,9 @@ // //===----------------------------------------------------------------------===// -#include "DebugTypes.h" #include "Driver.h" #include "Config.h" +#include "DebugTypes.h" #include "ICF.h" #include "InputFiles.h" #include "MarkLive.h" @@ -30,6 +30,7 @@ #include "llvm/Object/ArchiveWriter.h" #include "llvm/Object/COFFImportFile.h" #include "llvm/Object/COFFModuleDefinition.h" +#include "llvm/Object/WindowsMachineFlag.h" #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" @@ -40,7 +41,6 @@ #include "llvm/Support/TarWriter.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/ToolDrivers/llvm-lib/LibDriver.h" #include #include #include Index: lld/COFF/SymbolTable.cpp =================================================================== --- lld/COFF/SymbolTable.cpp +++ lld/COFF/SymbolTable.cpp @@ -16,9 +16,9 @@ #include "lld/Common/Memory.h" #include "lld/Common/Timer.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/Object/WindowsMachineFlag.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/ToolDrivers/llvm-lib/LibDriver.h" #include using namespace llvm; Index: llvm/include/llvm/Object/WindowsMachineFlag.h =================================================================== --- /dev/null +++ llvm/include/llvm/Object/WindowsMachineFlag.h @@ -0,0 +1,33 @@ +//===- WindowsMachineFlag.h -------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Functions for implementing the /machine: flag. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLDRIVERS_MACHINEFLAG_MACHINEFLAG_H +#define LLVM_TOOLDRIVERS_MACHINEFLAG_MACHINEFLAG_H + +namespace llvm { + +class StringRef; +namespace COFF { +enum MachineTypes : unsigned; +} + +// Returns a user-readable string for ARMNT, ARM64, AMD64, I386. +// Other MachineTypes values must not be passed in. +StringRef machineToStr(COFF::MachineTypes MT); + +// Maps /machine: arguments to a MachineTypes value. +// Only returns ARMNT, ARM64, AMD64, I386, or IMAGE_FILE_MACHINE_UNKNOWN. +COFF::MachineTypes getMachineType(StringRef S); + +} + +#endif Index: llvm/include/llvm/ToolDrivers/llvm-lib/LibDriver.h =================================================================== --- llvm/include/llvm/ToolDrivers/llvm-lib/LibDriver.h +++ llvm/include/llvm/ToolDrivers/llvm-lib/LibDriver.h @@ -19,20 +19,6 @@ int libDriverMain(ArrayRef ARgs); - -class StringRef; -namespace COFF { -enum MachineTypes : unsigned; -} - -// Returns a user-readable string for ARMNT, ARM64, AMD64, I386. -// Other MachineTypes values must not be pased in. -StringRef machineToStr(COFF::MachineTypes MT); - -// Maps /machine: arguments to a MachineTypes value. -// Only returns ARMNT, ARM64, AMD64, I386, or IMAGE_FILE_MACHINE_UNKNOWN. -COFF::MachineTypes getMachineType(StringRef S); - } #endif Index: llvm/lib/Object/CMakeLists.txt =================================================================== --- llvm/lib/Object/CMakeLists.txt +++ llvm/lib/Object/CMakeLists.txt @@ -22,6 +22,7 @@ SymbolicFile.cpp SymbolSize.cpp WasmObjectFile.cpp + WindowsMachineFlag.cpp WindowsResource.cpp XCOFFObjectFile.cpp Index: llvm/lib/Object/WindowsMachineFlag.cpp =================================================================== --- /dev/null +++ llvm/lib/Object/WindowsMachineFlag.cpp @@ -0,0 +1,44 @@ +//===- WindowsMachineFlag.cpp ---------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Functions for implementing the /machine: flag. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Object/WindowsMachineFlag.h" + +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/BinaryFormat/COFF.h" + +using namespace llvm; + +// Returns /machine's value. +COFF::MachineTypes llvm::getMachineType(StringRef S) { + return StringSwitch(S.lower()) + .Cases("x64", "amd64", COFF::IMAGE_FILE_MACHINE_AMD64) + .Cases("x86", "i386", COFF::IMAGE_FILE_MACHINE_I386) + .Case("arm", COFF::IMAGE_FILE_MACHINE_ARMNT) + .Case("arm64", COFF::IMAGE_FILE_MACHINE_ARM64) + .Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN); +} + +StringRef llvm::machineToStr(COFF::MachineTypes MT) { + switch (MT) { + case COFF::IMAGE_FILE_MACHINE_ARMNT: + return "arm"; + case COFF::IMAGE_FILE_MACHINE_ARM64: + return "arm64"; + case COFF::IMAGE_FILE_MACHINE_AMD64: + return "x64"; + case COFF::IMAGE_FILE_MACHINE_I386: + return "x86"; + default: + llvm_unreachable("unknown machine type"); + } +} Index: llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp =================================================================== --- llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp +++ llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp @@ -26,6 +26,7 @@ #include "llvm/Support/Process.h" #include "llvm/Support/StringSaver.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/ToolDrivers/MachineFlag/MachineFlag.h" using namespace llvm; @@ -140,31 +141,6 @@ fatalOpenError(std::move(Err), B->getBufferIdentifier()); } -// Returns /machine's value. -COFF::MachineTypes llvm::getMachineType(StringRef S) { - return StringSwitch(S.lower()) - .Cases("x64", "amd64", COFF::IMAGE_FILE_MACHINE_AMD64) - .Cases("x86", "i386", COFF::IMAGE_FILE_MACHINE_I386) - .Case("arm", COFF::IMAGE_FILE_MACHINE_ARMNT) - .Case("arm64", COFF::IMAGE_FILE_MACHINE_ARM64) - .Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN); -} - -StringRef llvm::machineToStr(COFF::MachineTypes MT) { - switch (MT) { - case COFF::IMAGE_FILE_MACHINE_ARMNT: - return "arm"; - case COFF::IMAGE_FILE_MACHINE_ARM64: - return "arm64"; - case COFF::IMAGE_FILE_MACHINE_AMD64: - return "x64"; - case COFF::IMAGE_FILE_MACHINE_I386: - return "x86"; - default: - llvm_unreachable("unknown machine type"); - } -} - int llvm::libDriverMain(ArrayRef ArgsArr) { BumpPtrAllocator Alloc; StringSaver Saver(Alloc); Index: llvm/tools/llvm-cvtres/llvm-cvtres.cpp =================================================================== --- llvm/tools/llvm-cvtres/llvm-cvtres.cpp +++ llvm/tools/llvm-cvtres/llvm-cvtres.cpp @@ -13,6 +13,7 @@ #include "llvm/ADT/StringSwitch.h" #include "llvm/Object/Binary.h" +#include "llvm/Object/WindowsMachineFlag.h" #include "llvm/Object/WindowsResource.h" #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" @@ -118,16 +119,12 @@ COFF::MachineTypes MachineType; - if (InputArgs.hasArg(OPT_MACHINE)) { - std::string MachineString = InputArgs.getLastArgValue(OPT_MACHINE).upper(); - MachineType = StringSwitch(MachineString) - .Case("ARM", COFF::IMAGE_FILE_MACHINE_ARMNT) - .Case("ARM64", COFF::IMAGE_FILE_MACHINE_ARM64) - .Case("X64", COFF::IMAGE_FILE_MACHINE_AMD64) - .Case("X86", COFF::IMAGE_FILE_MACHINE_I386) - .Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN); - if (MachineType == COFF::IMAGE_FILE_MACHINE_UNKNOWN) - reportError("Unsupported machine architecture"); + if (opt::Arg *Arg = InputArgs.getLastArg(OPT_MACHINE)) { + MachineType = getMachineType(Arg->getValue()); + if (MachineType == COFF::IMAGE_FILE_MACHINE_UNKNOWN) { + reportError(Twine("Unsupported machine architecture ") + Arg->getValue() + + "\n"); + } } else { if (Verbose) outs() << "Machine architecture not specified; assumed X64.\n"; @@ -142,8 +139,8 @@ SmallString<128> OutputFile; - if (InputArgs.hasArg(OPT_OUT)) { - OutputFile = InputArgs.getLastArgValue(OPT_OUT); + if (opt::Arg *Arg = InputArgs.getLastArg(OPT_OUT)) { + OutputFile = Arg->getValue(); } else { OutputFile = sys::path::filename(StringRef(InputFiles[0])); sys::path::replace_extension(OutputFile, ".obj"); @@ -159,22 +156,8 @@ DateTimeStamp = getTime(); } - if (Verbose) { - outs() << "Machine: "; - switch (MachineType) { - case COFF::IMAGE_FILE_MACHINE_ARM64: - outs() << "ARM64\n"; - break; - case COFF::IMAGE_FILE_MACHINE_ARMNT: - outs() << "ARM\n"; - break; - case COFF::IMAGE_FILE_MACHINE_I386: - outs() << "X86\n"; - break; - default: - outs() << "X64\n"; - } - } + if (Verbose) + outs() << "Machine: " << machineToStr(MachineType) << '\n'; WindowsResourceParser Parser; Index: llvm/utils/gn/secondary/llvm/lib/Object/BUILD.gn =================================================================== --- llvm/utils/gn/secondary/llvm/lib/Object/BUILD.gn +++ llvm/utils/gn/secondary/llvm/lib/Object/BUILD.gn @@ -35,6 +35,7 @@ "SymbolSize.cpp", "SymbolicFile.cpp", "WasmObjectFile.cpp", + "WindowsMachineFlag.cpp", "WindowsResource.cpp", "XCOFFObjectFile.cpp", ]