llvm.org GIT mirror llvm / 2ecbbba
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363144 91177308-0d34-0410-b5e6-96231b3b80d8 Nico Weber 2 months ago
7 changed file(s) with 91 addition(s) and 67 deletion(s). Raw diff Collapse all Expand all
0 //===- WindowsMachineFlag.h -------------------------------------*- C++ -*-===//
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Functions for implementing the /machine: flag.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #ifndef LLVM_TOOLDRIVERS_MACHINEFLAG_MACHINEFLAG_H
13 #define LLVM_TOOLDRIVERS_MACHINEFLAG_MACHINEFLAG_H
14
15 namespace llvm {
16
17 class StringRef;
18 namespace COFF {
19 enum MachineTypes : unsigned;
20 }
21
22 // Returns a user-readable string for ARMNT, ARM64, AMD64, I386.
23 // Other MachineTypes values must not be passed in.
24 StringRef machineToStr(COFF::MachineTypes MT);
25
26 // Maps /machine: arguments to a MachineTypes value.
27 // Only returns ARMNT, ARM64, AMD64, I386, or IMAGE_FILE_MACHINE_UNKNOWN.
28 COFF::MachineTypes getMachineType(StringRef S);
29
30 }
31
32 #endif
1818
1919 int libDriverMain(ArrayRef ARgs);
2020
21
22 class StringRef;
23 namespace COFF {
24 enum MachineTypes : unsigned;
25 }
26
27 // Returns a user-readable string for ARMNT, ARM64, AMD64, I386.
28 // Other MachineTypes values must not be pased in.
29 StringRef machineToStr(COFF::MachineTypes MT);
30
31 // Maps /machine: arguments to a MachineTypes value.
32 // Only returns ARMNT, ARM64, AMD64, I386, or IMAGE_FILE_MACHINE_UNKNOWN.
33 COFF::MachineTypes getMachineType(StringRef S);
34
3521 }
3622
3723 #endif
2121 SymbolicFile.cpp
2222 SymbolSize.cpp
2323 WasmObjectFile.cpp
24 WindowsMachineFlag.cpp
2425 WindowsResource.cpp
2526 XCOFFObjectFile.cpp
2627
0 //===- WindowsMachineFlag.cpp ---------------------------------------------===//
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Functions for implementing the /machine: flag.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "llvm/Object/WindowsMachineFlag.h"
13
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/BinaryFormat/COFF.h"
17
18 using namespace llvm;
19
20 // Returns /machine's value.
21 COFF::MachineTypes llvm::getMachineType(StringRef S) {
22 return StringSwitch(S.lower())
23 .Cases("x64", "amd64", COFF::IMAGE_FILE_MACHINE_AMD64)
24 .Cases("x86", "i386", COFF::IMAGE_FILE_MACHINE_I386)
25 .Case("arm", COFF::IMAGE_FILE_MACHINE_ARMNT)
26 .Case("arm64", COFF::IMAGE_FILE_MACHINE_ARM64)
27 .Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
28 }
29
30 StringRef llvm::machineToStr(COFF::MachineTypes MT) {
31 switch (MT) {
32 case COFF::IMAGE_FILE_MACHINE_ARMNT:
33 return "arm";
34 case COFF::IMAGE_FILE_MACHINE_ARM64:
35 return "arm64";
36 case COFF::IMAGE_FILE_MACHINE_AMD64:
37 return "x64";
38 case COFF::IMAGE_FILE_MACHINE_I386:
39 return "x86";
40 default:
41 llvm_unreachable("unknown machine type");
42 }
43 }
1717 #include "llvm/Bitcode/BitcodeReader.h"
1818 #include "llvm/Object/ArchiveWriter.h"
1919 #include "llvm/Object/COFF.h"
20 #include "llvm/Object/WindowsMachineFlag.h"
2021 #include "llvm/Option/Arg.h"
2122 #include "llvm/Option/ArgList.h"
2223 #include "llvm/Option/Option.h"
137138 llvm::outs() << Name << '\n';
138139 }
139140 fatalOpenError(std::move(Err), B->getBufferIdentifier());
140 }
141
142 // Returns /machine's value.
143 COFF::MachineTypes llvm::getMachineType(StringRef S) {
144 return StringSwitch(S.lower())
145 .Cases("x64", "amd64", COFF::IMAGE_FILE_MACHINE_AMD64)
146 .Cases("x86", "i386", COFF::IMAGE_FILE_MACHINE_I386)
147 .Case("arm", COFF::IMAGE_FILE_MACHINE_ARMNT)
148 .Case("arm64", COFF::IMAGE_FILE_MACHINE_ARM64)
149 .Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
150 }
151
152 StringRef llvm::machineToStr(COFF::MachineTypes MT) {
153 switch (MT) {
154 case COFF::IMAGE_FILE_MACHINE_ARMNT:
155 return "arm";
156 case COFF::IMAGE_FILE_MACHINE_ARM64:
157 return "arm64";
158 case COFF::IMAGE_FILE_MACHINE_AMD64:
159 return "x64";
160 case COFF::IMAGE_FILE_MACHINE_I386:
161 return "x86";
162 default:
163 llvm_unreachable("unknown machine type");
164 }
165141 }
166142
167143 int llvm::libDriverMain(ArrayRef ArgsArr) {
1212
1313 #include "llvm/ADT/StringSwitch.h"
1414 #include "llvm/Object/Binary.h"
15 #include "llvm/Object/WindowsMachineFlag.h"
1516 #include "llvm/Object/WindowsResource.h"
1617 #include "llvm/Option/Arg.h"
1718 #include "llvm/Option/ArgList.h"
117118
118119 COFF::MachineTypes MachineType;
119120
120 if (InputArgs.hasArg(OPT_MACHINE)) {
121 std::string MachineString = InputArgs.getLastArgValue(OPT_MACHINE).upper();
122 MachineType = StringSwitch(MachineString)
123 .Case("ARM", COFF::IMAGE_FILE_MACHINE_ARMNT)
124 .Case("ARM64", COFF::IMAGE_FILE_MACHINE_ARM64)
125 .Case("X64", COFF::IMAGE_FILE_MACHINE_AMD64)
126 .Case("X86", COFF::IMAGE_FILE_MACHINE_I386)
127 .Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
128 if (MachineType == COFF::IMAGE_FILE_MACHINE_UNKNOWN)
129 reportError("Unsupported machine architecture");
121 if (opt::Arg *Arg = InputArgs.getLastArg(OPT_MACHINE)) {
122 MachineType = getMachineType(Arg->getValue());
123 if (MachineType == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
124 reportError(Twine("Unsupported machine architecture ") + Arg->getValue() +
125 "\n");
126 }
130127 } else {
131128 if (Verbose)
132129 outs() << "Machine architecture not specified; assumed X64.\n";
141138
142139 SmallString<128> OutputFile;
143140
144 if (InputArgs.hasArg(OPT_OUT)) {
145 OutputFile = InputArgs.getLastArgValue(OPT_OUT);
141 if (opt::Arg *Arg = InputArgs.getLastArg(OPT_OUT)) {
142 OutputFile = Arg->getValue();
146143 } else {
147144 OutputFile = sys::path::filename(StringRef(InputFiles[0]));
148145 sys::path::replace_extension(OutputFile, ".obj");
158155 DateTimeStamp = getTime();
159156 }
160157
161 if (Verbose) {
162 outs() << "Machine: ";
163 switch (MachineType) {
164 case COFF::IMAGE_FILE_MACHINE_ARM64:
165 outs() << "ARM64\n";
166 break;
167 case COFF::IMAGE_FILE_MACHINE_ARMNT:
168 outs() << "ARM\n";
169 break;
170 case COFF::IMAGE_FILE_MACHINE_I386:
171 outs() << "X86\n";
172 break;
173 default:
174 outs() << "X64\n";
175 }
176 }
158 if (Verbose)
159 outs() << "Machine: " << machineToStr(MachineType) << '\n';
177160
178161 WindowsResourceParser Parser;
179162
3434 "SymbolSize.cpp",
3535 "SymbolicFile.cpp",
3636 "WasmObjectFile.cpp",
37 "WindowsMachineFlag.cpp",
3738 "WindowsResource.cpp",
3839 "XCOFFObjectFile.cpp",
3940 ]