1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
| //===- llvm-cvtres.cpp - Serialize .res files into .obj ---------*- 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
//
//===----------------------------------------------------------------------===//
//
// Serialize .res files into .obj files. This is intended to be a
// platform-independent port of Microsoft's cvtres.exe.
//
//===----------------------------------------------------------------------===//
#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"
#include "llvm/Option/Option.h"
#include "llvm/Support/BinaryStreamError.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/raw_ostream.h"
#include <system_error>
using namespace llvm;
using namespace object;
namespace {
enum ID {
OPT_INVALID = 0, // This is not an option ID.
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR, VALUES) \
OPT_##ID,
#include "Opts.inc"
#undef OPTION
};
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
#include "Opts.inc"
#undef PREFIX
static const opt::OptTable::Info InfoTable[] = {
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR, VALUES) \
{ \
PREFIX, NAME, HELPTEXT, \
METAVAR, OPT_##ID, opt::Option::KIND##Class, \
PARAM, FLAGS, OPT_##GROUP, \
OPT_##ALIAS, ALIASARGS, VALUES},
#include "Opts.inc"
#undef OPTION
};
class CvtResOptTable : public opt::OptTable {
public:
CvtResOptTable() : OptTable(InfoTable, true) {}
};
}
LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
errs() << Msg;
exit(1);
}
static void reportError(StringRef Input, std::error_code EC) {
reportError(Twine(Input) + ": " + EC.message() + ".\n");
}
void error(std::error_code EC) {
if (!EC)
return;
reportError(EC.message() + ".\n");
}
void error(Error EC) {
if (!EC)
return;
handleAllErrors(std::move(EC),
[&](const ErrorInfoBase &EI) { reportError(EI.message()); });
}
static uint32_t getTime() {
std::time_t Now = time(nullptr);
if (Now < 0 || !isUInt<32>(Now))
return UINT32_MAX;
return static_cast<uint32_t>(Now);
}
template <typename T> T error(Expected<T> EC) {
if (!EC)
error(EC.takeError());
return std::move(EC.get());
}
int main(int Argc, const char **Argv) {
InitLLVM X(Argc, Argv);
CvtResOptTable T;
unsigned MAI, MAC;
ArrayRef<const char *> ArgsArr = makeArrayRef(Argv + 1, Argc - 1);
opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
if (InputArgs.hasArg(OPT_HELP)) {
T.PrintHelp(outs(), "llvm-cvtres [options] file...", "Resource Converter");
return 0;
}
bool Verbose = InputArgs.hasArg(OPT_VERBOSE);
COFF::MachineTypes MachineType;
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";
MachineType = COFF::IMAGE_FILE_MACHINE_AMD64;
}
std::vector<std::string> InputFiles = InputArgs.getAllArgValues(OPT_INPUT);
if (InputFiles.size() == 0) {
reportError("No input file specified.\n");
}
SmallString<128> OutputFile;
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");
}
uint32_t DateTimeStamp;
if (llvm::opt::Arg *Arg = InputArgs.getLastArg(OPT_TIMESTAMP)) {
StringRef Value(Arg->getValue());
if (Value.getAsInteger(0, DateTimeStamp))
reportError(Twine("invalid timestamp: ") + Value +
". Expected 32-bit integer\n");
} else {
DateTimeStamp = getTime();
}
if (Verbose)
outs() << "Machine: " << machineToStr(MachineType) << '\n';
WindowsResourceParser Parser;
for (const auto &File : InputFiles) {
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
if (!BinaryOrErr)
reportError(File, errorToErrorCode(BinaryOrErr.takeError()));
Binary &Binary = *BinaryOrErr.get().getBinary();
WindowsResource *RF = dyn_cast<WindowsResource>(&Binary);
if (!RF)
reportError(File + ": unrecognized file format.\n");
if (Verbose) {
int EntryNumber = 0;
ResourceEntryRef Entry = error(RF->getHeadEntry());
bool End = false;
while (!End) {
error(Entry.moveNext(End));
EntryNumber++;
}
outs() << "Number of resources: " << EntryNumber << "\n";
}
std::vector<std::string> Duplicates;
error(Parser.parse(RF, Duplicates));
for (const auto& DupeDiag : Duplicates)
reportError(DupeDiag);
}
if (Verbose) {
Parser.printTree(outs());
}
std::unique_ptr<MemoryBuffer> OutputBuffer =
error(llvm::object::writeWindowsResourceCOFF(MachineType, Parser,
DateTimeStamp));
auto FileOrErr =
FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
if (!FileOrErr)
reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
FileBuffer->getBufferStart());
error(FileBuffer->commit());
if (Verbose) {
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(OutputFile);
if (!BinaryOrErr)
reportError(OutputFile, errorToErrorCode(BinaryOrErr.takeError()));
Binary &Binary = *BinaryOrErr.get().getBinary();
ScopedPrinter W(errs());
W.printBinaryBlock("Output File Raw Data", Binary.getData());
}
return 0;
}
|