2017-04-22 01:30:29 +08:00
|
|
|
//===- llvm-cvtres.cpp - Serialize .res files into .obj ---------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Serialize .res files into .obj files. This is intended to be a
|
|
|
|
// platform-independent port of Microsoft's cvtres.exe.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-05-20 09:49:19 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/Object/Binary.h"
|
|
|
|
#include "llvm/Object/WindowsResource.h"
|
2017-04-22 01:30:29 +08:00
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2017-05-20 09:49:19 +08:00
|
|
|
#include "llvm/Support/BinaryStreamError.h"
|
2017-04-22 01:30:29 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2017-05-20 09:49:19 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-04-22 01:30:29 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2017-06-17 06:00:42 +08:00
|
|
|
#include <system_error>
|
|
|
|
|
2017-04-22 01:30:29 +08:00
|
|
|
using namespace llvm;
|
2017-05-20 09:49:19 +08:00
|
|
|
using namespace object;
|
2017-04-22 01:30:29 +08: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-21 00:31:31 +08:00
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
2017-04-22 01:30:29 +08: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-21 00:31:31 +08:00
|
|
|
HELPTEXT, METAVAR, VALUES) \
|
2017-04-22 01:30:29 +08:00
|
|
|
{ \
|
2017-06-21 00:31:31 +08:00
|
|
|
PREFIX, NAME, HELPTEXT, \
|
|
|
|
METAVAR, OPT_##ID, opt::Option::KIND##Class, \
|
|
|
|
PARAM, FLAGS, OPT_##GROUP, \
|
|
|
|
OPT_##ALIAS, ALIASARGS, VALUES},
|
2017-04-22 01:30:29 +08:00
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
class CvtResOptTable : public opt::OptTable {
|
|
|
|
public:
|
|
|
|
CvtResOptTable() : OptTable(InfoTable, true) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
static ExitOnError ExitOnErr;
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:49:19 +08:00
|
|
|
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()); });
|
|
|
|
}
|
|
|
|
|
2017-06-20 02:49:05 +08:00
|
|
|
template <typename T> T error(Expected<T> EC) {
|
|
|
|
if (!EC)
|
|
|
|
error(EC.takeError());
|
|
|
|
return std::move(EC.get());
|
|
|
|
}
|
|
|
|
|
2017-04-22 01:30:29 +08:00
|
|
|
int main(int argc_, const char *argv_[]) {
|
|
|
|
sys::PrintStackTraceOnErrorSignal(argv_[0]);
|
|
|
|
PrettyStackTraceProgram X(argc_, argv_);
|
|
|
|
|
|
|
|
ExitOnErr.setBanner("llvm-cvtres: ");
|
|
|
|
|
|
|
|
SmallVector<const char *, 256> argv;
|
|
|
|
SpecificBumpPtrAllocator<char> ArgAllocator;
|
|
|
|
ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
|
|
|
|
argv, makeArrayRef(argv_, argc_), ArgAllocator)));
|
|
|
|
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
|
|
|
CvtResOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
2017-05-20 09:49:19 +08:00
|
|
|
ArrayRef<const char *> ArgsArr = makeArrayRef(argv_ + 1, argc_);
|
2017-04-22 01:30:29 +08:00
|
|
|
opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
|
|
|
|
|
2017-05-20 09:49:19 +08:00
|
|
|
if (InputArgs.hasArg(OPT_HELP)) {
|
2017-04-22 01:30:29 +08:00
|
|
|
T.PrintHelp(outs(), "cvtres", "Resource Converter", false);
|
2017-05-20 09:49:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-10 01:34:30 +08:00
|
|
|
bool Verbose = InputArgs.hasArg(OPT_VERBOSE);
|
|
|
|
|
2017-07-06 03:04:33 +08:00
|
|
|
COFF::MachineTypes MachineType;
|
2017-05-20 09:49:19 +08:00
|
|
|
|
|
|
|
if (InputArgs.hasArg(OPT_MACHINE)) {
|
|
|
|
std::string MachineString = InputArgs.getLastArgValue(OPT_MACHINE).upper();
|
2017-07-06 03:04:33 +08:00
|
|
|
MachineType = StringSwitch<COFF::MachineTypes>(MachineString)
|
|
|
|
.Case("ARM", COFF::IMAGE_FILE_MACHINE_ARMNT)
|
|
|
|
.Case("X64", COFF::IMAGE_FILE_MACHINE_AMD64)
|
|
|
|
.Case("X86", COFF::IMAGE_FILE_MACHINE_I386)
|
|
|
|
.Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
|
|
|
|
if (MachineType == COFF::IMAGE_FILE_MACHINE_UNKNOWN)
|
2017-05-20 09:49:19 +08:00
|
|
|
reportError("Unsupported machine architecture");
|
|
|
|
} else {
|
2017-06-10 01:34:30 +08:00
|
|
|
if (Verbose)
|
|
|
|
outs() << "Machine architecture not specified; assumed X64.\n";
|
2017-07-06 03:04:33 +08:00
|
|
|
MachineType = COFF::IMAGE_FILE_MACHINE_AMD64;
|
2017-05-20 09:49:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> InputFiles = InputArgs.getAllArgValues(OPT_INPUT);
|
|
|
|
|
|
|
|
if (InputFiles.size() == 0) {
|
2017-05-31 02:19:06 +08:00
|
|
|
reportError("No input file specified.\n");
|
2017-05-20 09:49:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallString<128> OutputFile;
|
|
|
|
|
|
|
|
if (InputArgs.hasArg(OPT_OUT)) {
|
|
|
|
OutputFile = InputArgs.getLastArgValue(OPT_OUT);
|
|
|
|
} else {
|
2017-06-17 06:00:42 +08:00
|
|
|
OutputFile = sys::path::filename(StringRef(InputFiles[0]));
|
|
|
|
sys::path::replace_extension(OutputFile, ".obj");
|
2017-05-20 09:49:19 +08:00
|
|
|
}
|
|
|
|
|
2017-06-10 01:34:30 +08:00
|
|
|
if (Verbose) {
|
|
|
|
outs() << "Machine: ";
|
|
|
|
switch (MachineType) {
|
2017-07-06 03:04:33 +08:00
|
|
|
case COFF::IMAGE_FILE_MACHINE_ARMNT:
|
2017-06-10 01:34:30 +08:00
|
|
|
outs() << "ARM\n";
|
|
|
|
break;
|
2017-07-06 03:04:33 +08:00
|
|
|
case COFF::IMAGE_FILE_MACHINE_I386:
|
2017-06-10 01:34:30 +08:00
|
|
|
outs() << "X86\n";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
outs() << "X64\n";
|
|
|
|
}
|
2017-05-31 02:19:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
WindowsResourceParser Parser;
|
|
|
|
|
2017-05-20 09:49:19 +08:00
|
|
|
for (const auto &File : InputFiles) {
|
2017-06-17 06:00:42 +08:00
|
|
|
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
|
2017-05-20 09:49:19 +08:00
|
|
|
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");
|
|
|
|
|
2017-06-10 01:34:30 +08:00
|
|
|
if (Verbose) {
|
|
|
|
int EntryNumber = 0;
|
2017-06-20 02:49:05 +08:00
|
|
|
ResourceEntryRef Entry = error(RF->getHeadEntry());
|
2017-06-10 01:34:30 +08:00
|
|
|
bool End = false;
|
|
|
|
while (!End) {
|
|
|
|
error(Entry.moveNext(End));
|
|
|
|
EntryNumber++;
|
|
|
|
}
|
|
|
|
outs() << "Number of resources: " << EntryNumber << "\n";
|
2017-05-20 09:49:19 +08:00
|
|
|
}
|
2017-05-31 02:19:06 +08:00
|
|
|
|
|
|
|
error(Parser.parse(RF));
|
2017-05-20 09:49:19 +08:00
|
|
|
}
|
2017-05-31 02:19:06 +08:00
|
|
|
|
2017-06-14 02:17:36 +08:00
|
|
|
if (Verbose) {
|
|
|
|
Parser.printTree(outs());
|
|
|
|
}
|
2017-06-10 01:34:30 +08:00
|
|
|
|
2017-06-20 02:49:05 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> OutputBuffer =
|
|
|
|
error(llvm::object::writeWindowsResourceCOFF(MachineType, Parser));
|
2017-06-17 05:13:24 +08:00
|
|
|
auto FileOrErr =
|
|
|
|
FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
|
|
|
|
if (!FileOrErr)
|
|
|
|
reportError(OutputFile, FileOrErr.getError());
|
|
|
|
std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
|
|
|
|
std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
|
|
|
|
FileBuffer->getBufferStart());
|
|
|
|
error(FileBuffer->commit());
|
2017-07-08 11:06:10 +08:00
|
|
|
|
2017-06-14 02:17:36 +08:00
|
|
|
if (Verbose) {
|
2017-06-17 06:00:42 +08:00
|
|
|
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(OutputFile);
|
2017-06-14 02:17:36 +08:00
|
|
|
if (!BinaryOrErr)
|
|
|
|
reportError(OutputFile, errorToErrorCode(BinaryOrErr.takeError()));
|
|
|
|
Binary &Binary = *BinaryOrErr.get().getBinary();
|
|
|
|
ScopedPrinter W(errs());
|
|
|
|
W.printBinaryBlock("Output File Raw Data", Binary.getData());
|
|
|
|
}
|
|
|
|
|
2017-04-22 01:30:29 +08:00
|
|
|
return 0;
|
|
|
|
}
|