2017-04-21 19:30:29 +02:00
|
|
|
//===- llvm-cvtres.cpp - Serialize .res files into .obj ---------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// 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
|
2017-04-21 19:30:29 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Serialize .res files into .obj files. This is intended to be a
|
|
|
|
// platform-independent port of Microsoft's cvtres.exe.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-04-15 13:01:06 +02:00
|
|
|
#include "llvm/BinaryFormat/Magic.h"
|
2017-05-20 03:49:19 +02:00
|
|
|
#include "llvm/Object/Binary.h"
|
2019-06-12 13:32:43 +02:00
|
|
|
#include "llvm/Object/WindowsMachineFlag.h"
|
2017-05-20 03:49:19 +02:00
|
|
|
#include "llvm/Object/WindowsResource.h"
|
2017-04-21 19:30:29 +02:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2017-05-20 03:49:19 +02:00
|
|
|
#include "llvm/Support/BinaryStreamError.h"
|
2017-04-21 19:30:29 +02:00
|
|
|
#include "llvm/Support/Error.h"
|
2018-04-13 20:26:06 +02:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2017-04-21 19:30:29 +02:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2017-05-20 03:49:19 +02:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-04-21 19:30:29 +02:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
2019-04-25 01:26:30 +02:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2017-04-21 19:30:29 +02:00
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2017-06-17 00:00:42 +02:00
|
|
|
#include <system_error>
|
|
|
|
|
2017-04-21 19:30:29 +02:00
|
|
|
using namespace llvm;
|
2017-05-20 03:49:19 +02:00
|
|
|
using namespace object;
|
2017-04-21 19:30:29 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum ID {
|
|
|
|
OPT_INVALID = 0, // This is not an option ID.
|
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
2017-06-20 18:31:31 +02:00
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
2017-04-21 19:30:29 +02:00
|
|
|
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, \
|
2017-06-20 18:31:31 +02:00
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
2017-04-21 19:30:29 +02:00
|
|
|
{ \
|
2017-06-20 18:31:31 +02:00
|
|
|
PREFIX, NAME, HELPTEXT, \
|
|
|
|
METAVAR, OPT_##ID, opt::Option::KIND##Class, \
|
|
|
|
PARAM, FLAGS, OPT_##GROUP, \
|
|
|
|
OPT_##ALIAS, ALIASARGS, VALUES},
|
2017-04-21 19:30:29 +02:00
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
class CvtResOptTable : public opt::OptTable {
|
|
|
|
public:
|
|
|
|
CvtResOptTable() : OptTable(InfoTable, true) {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-27 00:57:09 +02:00
|
|
|
static LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
|
2017-05-20 03:49:19 +02:00
|
|
|
errs() << Msg;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reportError(StringRef Input, std::error_code EC) {
|
|
|
|
reportError(Twine(Input) + ": " + EC.message() + ".\n");
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:01:06 +02:00
|
|
|
static void error(StringRef Input, Error EC) {
|
|
|
|
if (!EC)
|
|
|
|
return;
|
|
|
|
handleAllErrors(std::move(EC), [&](const ErrorInfoBase &EI) {
|
|
|
|
reportError(Twine(Input) + ": " + EI.message() + ".\n");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-27 00:57:09 +02:00
|
|
|
static void error(Error EC) {
|
2017-05-20 03:49:19 +02:00
|
|
|
if (!EC)
|
|
|
|
return;
|
|
|
|
handleAllErrors(std::move(EC),
|
|
|
|
[&](const ErrorInfoBase &EI) { reportError(EI.message()); });
|
|
|
|
}
|
|
|
|
|
2019-06-11 13:26:50 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-06-19 20:49:05 +02:00
|
|
|
template <typename T> T error(Expected<T> EC) {
|
|
|
|
if (!EC)
|
|
|
|
error(EC.takeError());
|
|
|
|
return std::move(EC.get());
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:01:06 +02:00
|
|
|
template <typename T> T error(StringRef Input, Expected<T> EC) {
|
|
|
|
if (!EC)
|
|
|
|
error(Input, EC.takeError());
|
|
|
|
return std::move(EC.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> T error(StringRef Input, ErrorOr<T> &&EC) {
|
|
|
|
return error(Input, errorOrToExpected(std::move(EC)));
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:26:06 +02:00
|
|
|
int main(int Argc, const char **Argv) {
|
|
|
|
InitLLVM X(Argc, Argv);
|
2017-04-21 19:30:29 +02:00
|
|
|
|
|
|
|
CvtResOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
2018-04-13 20:26:06 +02:00
|
|
|
ArrayRef<const char *> ArgsArr = makeArrayRef(Argv + 1, Argc - 1);
|
2017-04-21 19:30:29 +02:00
|
|
|
opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
|
|
|
|
|
2017-05-20 03:49:19 +02:00
|
|
|
if (InputArgs.hasArg(OPT_HELP)) {
|
2019-04-24 21:13:38 +02:00
|
|
|
T.PrintHelp(outs(), "llvm-cvtres [options] file...", "Resource Converter");
|
2017-05-20 03:49:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-09 19:34:30 +02:00
|
|
|
bool Verbose = InputArgs.hasArg(OPT_VERBOSE);
|
|
|
|
|
2017-07-05 21:04:33 +02:00
|
|
|
COFF::MachineTypes MachineType;
|
2017-05-20 03:49:19 +02:00
|
|
|
|
2019-06-12 13:32:43 +02:00
|
|
|
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");
|
|
|
|
}
|
2017-05-20 03:49:19 +02:00
|
|
|
} else {
|
2017-06-09 19:34:30 +02:00
|
|
|
if (Verbose)
|
|
|
|
outs() << "Machine architecture not specified; assumed X64.\n";
|
2017-07-05 21:04:33 +02:00
|
|
|
MachineType = COFF::IMAGE_FILE_MACHINE_AMD64;
|
2017-05-20 03:49:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> InputFiles = InputArgs.getAllArgValues(OPT_INPUT);
|
|
|
|
|
|
|
|
if (InputFiles.size() == 0) {
|
2017-05-30 20:19:06 +02:00
|
|
|
reportError("No input file specified.\n");
|
2017-05-20 03:49:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallString<128> OutputFile;
|
|
|
|
|
2019-06-12 13:32:43 +02:00
|
|
|
if (opt::Arg *Arg = InputArgs.getLastArg(OPT_OUT)) {
|
|
|
|
OutputFile = Arg->getValue();
|
2017-05-20 03:49:19 +02:00
|
|
|
} else {
|
2017-06-17 00:00:42 +02:00
|
|
|
OutputFile = sys::path::filename(StringRef(InputFiles[0]));
|
|
|
|
sys::path::replace_extension(OutputFile, ".obj");
|
2017-05-20 03:49:19 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 13:26:50 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-06-12 13:32:43 +02:00
|
|
|
if (Verbose)
|
|
|
|
outs() << "Machine: " << machineToStr(MachineType) << '\n';
|
2017-05-30 20:19:06 +02:00
|
|
|
|
|
|
|
WindowsResourceParser Parser;
|
|
|
|
|
2017-05-20 03:49:19 +02:00
|
|
|
for (const auto &File : InputFiles) {
|
2021-04-15 13:01:06 +02:00
|
|
|
std::unique_ptr<MemoryBuffer> Buffer = error(
|
|
|
|
File, MemoryBuffer::getFileOrSTDIN(File, /*IsText=*/false,
|
|
|
|
/*RequiresNullTerminator=*/false));
|
|
|
|
file_magic Type = identify_magic(Buffer->getMemBufferRef().getBuffer());
|
|
|
|
if (Type != file_magic::windows_resource)
|
2017-05-20 03:49:19 +02:00
|
|
|
reportError(File + ": unrecognized file format.\n");
|
2021-04-15 13:01:06 +02:00
|
|
|
std::unique_ptr<WindowsResource> Binary = error(
|
|
|
|
File,
|
|
|
|
WindowsResource::createWindowsResource(Buffer->getMemBufferRef()));
|
|
|
|
|
|
|
|
WindowsResource *RF = Binary.get();
|
2017-05-20 03:49:19 +02:00
|
|
|
|
2017-06-09 19:34:30 +02:00
|
|
|
if (Verbose) {
|
|
|
|
int EntryNumber = 0;
|
2017-06-19 20:49:05 +02:00
|
|
|
ResourceEntryRef Entry = error(RF->getHeadEntry());
|
2017-06-09 19:34:30 +02:00
|
|
|
bool End = false;
|
|
|
|
while (!End) {
|
|
|
|
error(Entry.moveNext(End));
|
|
|
|
EntryNumber++;
|
|
|
|
}
|
|
|
|
outs() << "Number of resources: " << EntryNumber << "\n";
|
2017-05-20 03:49:19 +02:00
|
|
|
}
|
2017-05-30 20:19:06 +02:00
|
|
|
|
2019-05-02 23:21:55 +02:00
|
|
|
std::vector<std::string> Duplicates;
|
|
|
|
error(Parser.parse(RF, Duplicates));
|
|
|
|
for (const auto& DupeDiag : Duplicates)
|
|
|
|
reportError(DupeDiag);
|
2017-05-20 03:49:19 +02:00
|
|
|
}
|
2017-05-30 20:19:06 +02:00
|
|
|
|
2017-06-13 20:17:36 +02:00
|
|
|
if (Verbose) {
|
|
|
|
Parser.printTree(outs());
|
|
|
|
}
|
2017-06-09 19:34:30 +02:00
|
|
|
|
2017-06-19 20:49:05 +02:00
|
|
|
std::unique_ptr<MemoryBuffer> OutputBuffer =
|
2019-06-11 13:26:50 +02:00
|
|
|
error(llvm::object::writeWindowsResourceCOFF(MachineType, Parser,
|
|
|
|
DateTimeStamp));
|
2017-06-16 23:13:24 +02:00
|
|
|
auto FileOrErr =
|
|
|
|
FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
|
|
|
|
if (!FileOrErr)
|
2017-11-08 02:05:44 +01:00
|
|
|
reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
|
2017-06-16 23:13:24 +02:00
|
|
|
std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
|
|
|
|
std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
|
|
|
|
FileBuffer->getBufferStart());
|
|
|
|
error(FileBuffer->commit());
|
2017-07-08 05:06:10 +02:00
|
|
|
|
2017-06-13 20:17:36 +02:00
|
|
|
if (Verbose) {
|
2021-04-15 13:01:06 +02:00
|
|
|
std::unique_ptr<MemoryBuffer> Buffer =
|
|
|
|
error(OutputFile,
|
|
|
|
MemoryBuffer::getFileOrSTDIN(OutputFile, /*IsText=*/false,
|
|
|
|
/*RequiresNullTerminator=*/false));
|
|
|
|
|
2017-06-13 20:17:36 +02:00
|
|
|
ScopedPrinter W(errs());
|
2021-04-15 13:01:06 +02:00
|
|
|
W.printBinaryBlock("Output File Raw Data",
|
|
|
|
Buffer->getMemBufferRef().getBuffer());
|
2017-06-13 20:17:36 +02:00
|
|
|
}
|
|
|
|
|
2017-04-21 19:30:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|