1 //===- llvm-cvtres.cpp - Serialize .res files into .obj ---------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Serialize .res files into .obj files. This is intended to be a
10 // platform-independent port of Microsoft's cvtres.exe.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/BinaryFormat/Magic.h"
15 #include "llvm/Object/Binary.h"
16 #include "llvm/Object/WindowsMachineFlag.h"
17 #include "llvm/Object/WindowsResource.h"
18 #include "llvm/Option/Arg.h"
19 #include "llvm/Option/ArgList.h"
20 #include "llvm/Option/Option.h"
21 #include "llvm/Support/BinaryStreamError.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/InitLLVM.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/Process.h"
28 #include "llvm/Support/ScopedPrinter.h"
29 #include "llvm/Support/Signals.h"
30 #include "llvm/Support/raw_ostream.h"
32 #include <system_error>
35 using namespace object
;
40 OPT_INVALID
= 0, // This is not an option ID.
41 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
42 HELPTEXT, METAVAR, VALUES) \
48 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
52 const opt::OptTable::Info InfoTable
[] = {
53 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
54 HELPTEXT, METAVAR, VALUES) \
56 PREFIX, NAME, HELPTEXT, \
57 METAVAR, OPT_##ID, opt::Option::KIND##Class, \
58 PARAM, FLAGS, OPT_##GROUP, \
59 OPT_##ALIAS, ALIASARGS, VALUES},
64 class CvtResOptTable
: public opt::OptTable
{
66 CvtResOptTable() : OptTable(InfoTable
, true) {}
70 [[noreturn
]] static void reportError(Twine Msg
) {
75 static void reportError(StringRef Input
, std::error_code EC
) {
76 reportError(Twine(Input
) + ": " + EC
.message() + ".\n");
79 static void error(StringRef Input
, Error EC
) {
82 handleAllErrors(std::move(EC
), [&](const ErrorInfoBase
&EI
) {
83 reportError(Twine(Input
) + ": " + EI
.message() + ".\n");
87 static void error(Error EC
) {
90 handleAllErrors(std::move(EC
),
91 [&](const ErrorInfoBase
&EI
) { reportError(EI
.message()); });
94 static uint32_t getTime() {
95 std::time_t Now
= time(nullptr);
96 if (Now
< 0 || !isUInt
<32>(Now
))
98 return static_cast<uint32_t>(Now
);
101 template <typename T
> T
error(Expected
<T
> EC
) {
103 error(EC
.takeError());
104 return std::move(EC
.get());
107 template <typename T
> T
error(StringRef Input
, Expected
<T
> EC
) {
109 error(Input
, EC
.takeError());
110 return std::move(EC
.get());
113 template <typename T
> T
error(StringRef Input
, ErrorOr
<T
> &&EC
) {
114 return error(Input
, errorOrToExpected(std::move(EC
)));
117 int main(int Argc
, const char **Argv
) {
118 InitLLVM
X(Argc
, Argv
);
122 ArrayRef
<const char *> ArgsArr
= makeArrayRef(Argv
+ 1, Argc
- 1);
123 opt::InputArgList InputArgs
= T
.ParseArgs(ArgsArr
, MAI
, MAC
);
125 if (InputArgs
.hasArg(OPT_HELP
)) {
126 T
.printHelp(outs(), "llvm-cvtres [options] file...", "Resource Converter");
130 bool Verbose
= InputArgs
.hasArg(OPT_VERBOSE
);
132 COFF::MachineTypes MachineType
;
134 if (opt::Arg
*Arg
= InputArgs
.getLastArg(OPT_MACHINE
)) {
135 MachineType
= getMachineType(Arg
->getValue());
136 if (MachineType
== COFF::IMAGE_FILE_MACHINE_UNKNOWN
) {
137 reportError(Twine("Unsupported machine architecture ") + Arg
->getValue() +
142 outs() << "Machine architecture not specified; assumed X64.\n";
143 MachineType
= COFF::IMAGE_FILE_MACHINE_AMD64
;
146 std::vector
<std::string
> InputFiles
= InputArgs
.getAllArgValues(OPT_INPUT
);
148 if (InputFiles
.size() == 0) {
149 reportError("No input file specified.\n");
152 SmallString
<128> OutputFile
;
154 if (opt::Arg
*Arg
= InputArgs
.getLastArg(OPT_OUT
)) {
155 OutputFile
= Arg
->getValue();
157 OutputFile
= sys::path::filename(StringRef(InputFiles
[0]));
158 sys::path::replace_extension(OutputFile
, ".obj");
161 uint32_t DateTimeStamp
;
162 if (llvm::opt::Arg
*Arg
= InputArgs
.getLastArg(OPT_TIMESTAMP
)) {
163 StringRef
Value(Arg
->getValue());
164 if (Value
.getAsInteger(0, DateTimeStamp
))
165 reportError(Twine("invalid timestamp: ") + Value
+
166 ". Expected 32-bit integer\n");
168 DateTimeStamp
= getTime();
172 outs() << "Machine: " << machineToStr(MachineType
) << '\n';
174 WindowsResourceParser Parser
;
176 for (const auto &File
: InputFiles
) {
177 std::unique_ptr
<MemoryBuffer
> Buffer
= error(
178 File
, MemoryBuffer::getFileOrSTDIN(File
, /*IsText=*/false,
179 /*RequiresNullTerminator=*/false));
180 file_magic Type
= identify_magic(Buffer
->getMemBufferRef().getBuffer());
181 if (Type
!= file_magic::windows_resource
)
182 reportError(File
+ ": unrecognized file format.\n");
183 std::unique_ptr
<WindowsResource
> Binary
= error(
185 WindowsResource::createWindowsResource(Buffer
->getMemBufferRef()));
187 WindowsResource
*RF
= Binary
.get();
191 ResourceEntryRef Entry
= error(RF
->getHeadEntry());
194 error(Entry
.moveNext(End
));
197 outs() << "Number of resources: " << EntryNumber
<< "\n";
200 std::vector
<std::string
> Duplicates
;
201 error(Parser
.parse(RF
, Duplicates
));
202 for (const auto& DupeDiag
: Duplicates
)
203 reportError(DupeDiag
);
207 Parser
.printTree(outs());
210 std::unique_ptr
<MemoryBuffer
> OutputBuffer
=
211 error(llvm::object::writeWindowsResourceCOFF(MachineType
, Parser
,
214 FileOutputBuffer::create(OutputFile
, OutputBuffer
->getBufferSize());
216 reportError(OutputFile
, errorToErrorCode(FileOrErr
.takeError()));
217 std::unique_ptr
<FileOutputBuffer
> FileBuffer
= std::move(*FileOrErr
);
218 std::copy(OutputBuffer
->getBufferStart(), OutputBuffer
->getBufferEnd(),
219 FileBuffer
->getBufferStart());
220 error(FileBuffer
->commit());
223 std::unique_ptr
<MemoryBuffer
> Buffer
=
225 MemoryBuffer::getFileOrSTDIN(OutputFile
, /*IsText=*/false,
226 /*RequiresNullTerminator=*/false));
228 ScopedPrinter
W(errs());
229 W
.printBinaryBlock("Output File Raw Data",
230 Buffer
->getMemBufferRef().getBuffer());