1//===- llvm-cvtres.cpp - Serialize .res files into .obj ---------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Serialize .res files into .obj files. This is intended to be a
10// platform-independent port of Microsoft's cvtres.exe.
11//
12//===----------------------------------------------------------------------===//
13
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"
30
31#include <system_error>
32
33using namespace llvm;
34using namespace object;
35
36namespace {
37
38enum ID {
39 OPT_INVALID = 0, // This is not an option ID.
40#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
41#include "Opts.inc"
42#undef OPTION
43};
44
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);
49#include "Opts.inc"
50#undef PREFIX
51
52using namespace llvm::opt;
53static constexpr opt::OptTable::Info InfoTable[] = {
54#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
55#include "Opts.inc"
56#undef OPTION
57};
58
59class CvtResOptTable : public opt::GenericOptTable {
60public:
61 CvtResOptTable() : opt::GenericOptTable(InfoTable, true) {}
62};
63}
64
65[[noreturn]] static void reportError(Twine Msg) {
66 errs() << Msg;
67 exit(status: 1);
68}
69
70static void reportError(StringRef Input, std::error_code EC) {
71 reportError(Msg: Twine(Input) + ": " + EC.message() + ".\n");
72}
73
74static void error(StringRef Input, Error EC) {
75 if (!EC)
76 return;
77 handleAllErrors(E: std::move(EC), Handlers: [&](const ErrorInfoBase &EI) {
78 reportError(Msg: Twine(Input) + ": " + EI.message() + ".\n");
79 });
80}
81
82static void error(Error EC) {
83 if (!EC)
84 return;
85 handleAllErrors(E: std::move(EC),
86 Handlers: [&](const ErrorInfoBase &EI) { reportError(Msg: EI.message()); });
87}
88
89static uint32_t getTime() {
90 std::time_t Now = time(timer: nullptr);
91 if (Now < 0 || !isUInt<32>(x: Now))
92 return UINT32_MAX;
93 return static_cast<uint32_t>(Now);
94}
95
96template <typename T> T error(Expected<T> EC) {
97 if (!EC)
98 error(EC.takeError());
99 return std::move(EC.get());
100}
101
102template <typename T> T error(StringRef Input, Expected<T> EC) {
103 if (!EC)
104 error(Input, EC.takeError());
105 return std::move(EC.get());
106}
107
108template <typename T> T error(StringRef Input, ErrorOr<T> &&EC) {
109 return error(Input, errorOrToExpected(std::move(EC)));
110}
111
112int main(int Argc, const char **Argv) {
113 InitLLVM X(Argc, Argv);
114
115 CvtResOptTable T;
116 unsigned MAI, MAC;
117 ArrayRef<const char *> ArgsArr = ArrayRef(Argv + 1, Argc - 1);
118 opt::InputArgList InputArgs = T.ParseArgs(Args: ArgsArr, MissingArgIndex&: MAI, MissingArgCount&: MAC);
119
120 if (InputArgs.hasArg(OPT_HELP)) {
121 T.printHelp(OS&: outs(), Usage: "llvm-cvtres [options] file...", Title: "Resource Converter");
122 return 0;
123 }
124
125 bool Verbose = InputArgs.hasArg(OPT_VERBOSE);
126
127 COFF::MachineTypes MachineType;
128
129 if (opt::Arg *Arg = InputArgs.getLastArg(OPT_MACHINE)) {
130 MachineType = getMachineType(S: Arg->getValue());
131 if (MachineType == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
132 reportError(Msg: Twine("Unsupported machine architecture ") + Arg->getValue() +
133 "\n");
134 }
135 } else {
136 if (Verbose)
137 outs() << "Machine architecture not specified; assumed X64.\n";
138 MachineType = COFF::IMAGE_FILE_MACHINE_AMD64;
139 }
140
141 std::vector<std::string> InputFiles = InputArgs.getAllArgValues(Id: OPT_INPUT);
142
143 if (InputFiles.size() == 0) {
144 reportError(Msg: "No input file specified.\n");
145 }
146
147 SmallString<128> OutputFile;
148
149 if (opt::Arg *Arg = InputArgs.getLastArg(OPT_OUT)) {
150 OutputFile = Arg->getValue();
151 } else {
152 OutputFile = sys::path::filename(path: StringRef(InputFiles[0]));
153 sys::path::replace_extension(path&: OutputFile, extension: ".obj");
154 }
155
156 uint32_t DateTimeStamp;
157 if (llvm::opt::Arg *Arg = InputArgs.getLastArg(OPT_TIMESTAMP)) {
158 StringRef Value(Arg->getValue());
159 if (Value.getAsInteger(Radix: 0, Result&: DateTimeStamp))
160 reportError(Msg: Twine("invalid timestamp: ") + Value +
161 ". Expected 32-bit integer\n");
162 } else {
163 DateTimeStamp = getTime();
164 }
165
166 if (Verbose)
167 outs() << "Machine: " << machineToStr(MT: MachineType) << '\n';
168
169 WindowsResourceParser Parser;
170
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(
179 File,
180 WindowsResource::createWindowsResource(Buffer->getMemBufferRef()));
181
182 WindowsResource *RF = Binary.get();
183
184 if (Verbose) {
185 int EntryNumber = 0;
186 ResourceEntryRef Entry = error(RF->getHeadEntry());
187 bool End = false;
188 while (!End) {
189 error(Entry.moveNext(End));
190 EntryNumber++;
191 }
192 outs() << "Number of resources: " << EntryNumber << "\n";
193 }
194
195 std::vector<std::string> Duplicates;
196 error(Parser.parse(RF, Duplicates));
197 for (const auto& DupeDiag : Duplicates)
198 reportError(DupeDiag);
199 }
200
201 if (Verbose) {
202 Parser.printTree(OS&: outs());
203 }
204
205 std::unique_ptr<MemoryBuffer> OutputBuffer =
206 error(EC: llvm::object::writeWindowsResourceCOFF(MachineType, Parser,
207 TimeDateStamp: DateTimeStamp));
208 auto FileOrErr =
209 FileOutputBuffer::create(FilePath: OutputFile, Size: OutputBuffer->getBufferSize());
210 if (!FileOrErr)
211 reportError(Input: OutputFile, EC: errorToErrorCode(Err: FileOrErr.takeError()));
212 std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
213 std::copy(first: OutputBuffer->getBufferStart(), last: OutputBuffer->getBufferEnd(),
214 result: FileBuffer->getBufferStart());
215 error(EC: FileBuffer->commit());
216
217 if (Verbose) {
218 std::unique_ptr<MemoryBuffer> Buffer =
219 error(Input: OutputFile,
220 EC: MemoryBuffer::getFileOrSTDIN(Filename: OutputFile, /*IsText=*/false,
221 /*RequiresNullTerminator=*/false));
222
223 ScopedPrinter W(errs());
224 W.printBinaryBlock(Label: "Output File Raw Data",
225 Value: Buffer->getMemBufferRef().getBuffer());
226 }
227
228 return 0;
229}
230

source code of llvm/tools/llvm-cvtres/llvm-cvtres.cpp