Share /machine: handling code with llvm-cvtres too

r363016 let lld-link and llvm-lib share the /machine: parsing code.
This lets llvm-cvtres share it as well.

Making llvm-cvtres depend on llvm-lib seemed a bit strange (it doesn't
need llvm-lib's dependencies on BinaryFormat and BitReader) and I
couldn't find a good place to put this code. Since it's just a few
lines, put it in lib/Object for now.

Differential Revision: https://reviews.llvm.org/D63120

llvm-svn: 363144
This commit is contained in:
Nico Weber 2019-06-12 11:32:43 +00:00
parent ca9c3de17e
commit 1dc2123d64
9 changed files with 94 additions and 69 deletions

View File

@ -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"

View File

@ -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 <utility>
using namespace llvm;

View File

@ -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

View File

@ -19,20 +19,6 @@ template <typename T> class ArrayRef;
int libDriverMain(ArrayRef<const char *> 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

View File

@ -22,6 +22,7 @@ add_llvm_library(LLVMObject
SymbolicFile.cpp
SymbolSize.cpp
WasmObjectFile.cpp
WindowsMachineFlag.cpp
WindowsResource.cpp
XCOFFObjectFile.cpp

View File

@ -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<COFF::MachineTypes>(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");
}
}

View File

@ -18,6 +18,7 @@
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Object/ArchiveWriter.h"
#include "llvm/Object/COFF.h"
#include "llvm/Object/WindowsMachineFlag.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
@ -140,31 +141,6 @@ static void doList(opt::InputArgList& Args) {
fatalOpenError(std::move(Err), B->getBufferIdentifier());
}
// Returns /machine's value.
COFF::MachineTypes llvm::getMachineType(StringRef S) {
return StringSwitch<COFF::MachineTypes>(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<const char *> ArgsArr) {
BumpPtrAllocator Alloc;
StringSaver Saver(Alloc);

View File

@ -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 @@ int main(int Argc, const char **Argv) {
COFF::MachineTypes MachineType;
if (InputArgs.hasArg(OPT_MACHINE)) {
std::string MachineString = InputArgs.getLastArgValue(OPT_MACHINE).upper();
MachineType = StringSwitch<COFF::MachineTypes>(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 @@ int main(int Argc, const char **Argv) {
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 @@ int main(int Argc, const char **Argv) {
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;

View File

@ -35,6 +35,7 @@ static_library("Object") {
"SymbolSize.cpp",
"SymbolicFile.cpp",
"WasmObjectFile.cpp",
"WindowsMachineFlag.cpp",
"WindowsResource.cpp",
"XCOFFObjectFile.cpp",
]