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/Path.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/Process.h"
27 #include "llvm/Support/ScopedPrinter.h"
28 #include "llvm/Support/Signals.h"
29 #include "llvm/Support/raw_ostream.h"
31 #include <system_error>
34 using namespace object
;
39 OPT_INVALID
= 0, // This is not an option ID.
40 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
45 #define PREFIX(NAME, VALUE) \
46 static constexpr StringLiteral NAME##_init[] = VALUE; \
47 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
48 std::size(NAME##_init) - 1);
52 using namespace llvm::opt
;
53 static constexpr opt::OptTable::Info InfoTable
[] = {
54 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
59 class CvtResOptTable
: public opt::GenericOptTable
{
61 CvtResOptTable() : opt::GenericOptTable(InfoTable
, true) {}
65 [[noreturn
]] static void reportError(Twine Msg
) {
70 static void reportError(StringRef Input
, std::error_code EC
) {
71 reportError(Twine(Input
) + ": " + EC
.message() + ".\n");
74 static void error(StringRef Input
, Error EC
) {
77 handleAllErrors(std::move(EC
), [&](const ErrorInfoBase
&EI
) {
78 reportError(Twine(Input
) + ": " + EI
.message() + ".\n");
82 static void error(Error EC
) {
85 handleAllErrors(std::move(EC
),
86 [&](const ErrorInfoBase
&EI
) { reportError(EI
.message()); });
89 static uint32_t getTime() {
90 std::time_t Now
= time(nullptr);
91 if (Now
< 0 || !isUInt
<32>(Now
))
93 return static_cast<uint32_t>(Now
);
96 template <typename T
> T
error(Expected
<T
> EC
) {
98 error(EC
.takeError());
99 return std::move(EC
.get());
102 template <typename T
> T
error(StringRef Input
, Expected
<T
> EC
) {
104 error(Input
, EC
.takeError());
105 return std::move(EC
.get());
108 template <typename T
> T
error(StringRef Input
, ErrorOr
<T
> &&EC
) {
109 return error(Input
, errorOrToExpected(std::move(EC
)));
112 int main(int Argc
, const char **Argv
) {
113 InitLLVM
X(Argc
, Argv
);
117 ArrayRef
<const char *> ArgsArr
= ArrayRef(Argv
+ 1, Argc
- 1);
118 opt::InputArgList InputArgs
= T
.ParseArgs(ArgsArr
, MAI
, MAC
);
120 if (InputArgs
.hasArg(OPT_HELP
)) {
121 T
.printHelp(outs(), "llvm-cvtres [options] file...", "Resource Converter");
125 bool Verbose
= InputArgs
.hasArg(OPT_VERBOSE
);
127 COFF::MachineTypes MachineType
;
129 if (opt::Arg
*Arg
= InputArgs
.getLastArg(OPT_MACHINE
)) {
130 MachineType
= getMachineType(Arg
->getValue());
131 if (MachineType
== COFF::IMAGE_FILE_MACHINE_UNKNOWN
) {
132 reportError(Twine("Unsupported machine architecture ") + Arg
->getValue() +
137 outs() << "Machine architecture not specified; assumed X64.\n";
138 MachineType
= COFF::IMAGE_FILE_MACHINE_AMD64
;
141 std::vector
<std::string
> InputFiles
= InputArgs
.getAllArgValues(OPT_INPUT
);
143 if (InputFiles
.size() == 0) {
144 reportError("No input file specified.\n");
147 SmallString
<128> OutputFile
;
149 if (opt::Arg
*Arg
= InputArgs
.getLastArg(OPT_OUT
)) {
150 OutputFile
= Arg
->getValue();
152 OutputFile
= sys::path::filename(StringRef(InputFiles
[0]));
153 sys::path::replace_extension(OutputFile
, ".obj");
156 uint32_t DateTimeStamp
;
157 if (llvm::opt::Arg
*Arg
= InputArgs
.getLastArg(OPT_TIMESTAMP
)) {
158 StringRef
Value(Arg
->getValue());
159 if (Value
.getAsInteger(0, DateTimeStamp
))
160 reportError(Twine("invalid timestamp: ") + Value
+
161 ". Expected 32-bit integer\n");
163 DateTimeStamp
= getTime();
167 outs() << "Machine: " << machineToStr(MachineType
) << '\n';
169 WindowsResourceParser Parser
;
171 for (const auto &File
: InputFiles
) {
172 std::unique_ptr
<MemoryBuffer
> Buffer
= error(
173 File
, MemoryBuffer::getFileOrSTDIN(File
, /*IsText=*/false,
174 /*RequiresNullTerminator=*/false));
175 file_magic Type
= identify_magic(Buffer
->getMemBufferRef().getBuffer());
176 if (Type
!= file_magic::windows_resource
)
177 reportError(File
+ ": unrecognized file format.\n");
178 std::unique_ptr
<WindowsResource
> Binary
= error(
180 WindowsResource::createWindowsResource(Buffer
->getMemBufferRef()));
182 WindowsResource
*RF
= Binary
.get();
186 ResourceEntryRef Entry
= error(RF
->getHeadEntry());
189 error(Entry
.moveNext(End
));
192 outs() << "Number of resources: " << EntryNumber
<< "\n";
195 std::vector
<std::string
> Duplicates
;
196 error(Parser
.parse(RF
, Duplicates
));
197 for (const auto& DupeDiag
: Duplicates
)
198 reportError(DupeDiag
);
202 Parser
.printTree(outs());
205 std::unique_ptr
<MemoryBuffer
> OutputBuffer
=
206 error(llvm::object::writeWindowsResourceCOFF(MachineType
, Parser
,
209 FileOutputBuffer::create(OutputFile
, OutputBuffer
->getBufferSize());
211 reportError(OutputFile
, errorToErrorCode(FileOrErr
.takeError()));
212 std::unique_ptr
<FileOutputBuffer
> FileBuffer
= std::move(*FileOrErr
);
213 std::copy(OutputBuffer
->getBufferStart(), OutputBuffer
->getBufferEnd(),
214 FileBuffer
->getBufferStart());
215 error(FileBuffer
->commit());
218 std::unique_ptr
<MemoryBuffer
> Buffer
=
220 MemoryBuffer::getFileOrSTDIN(OutputFile
, /*IsText=*/false,
221 /*RequiresNullTerminator=*/false));
223 ScopedPrinter
W(errs());
224 W
.printBinaryBlock("Output File Raw Data",
225 Buffer
->getMemBufferRef().getBuffer());