llvm-cvtres.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include "llvm/BinaryFormat/Magic.h"
  14. #include "llvm/Object/Binary.h"
  15. #include "llvm/Object/WindowsMachineFlag.h"
  16. #include "llvm/Object/WindowsResource.h"
  17. #include "llvm/Option/Arg.h"
  18. #include "llvm/Option/ArgList.h"
  19. #include "llvm/Option/Option.h"
  20. #include "llvm/Support/BinaryStreamError.h"
  21. #include "llvm/Support/Error.h"
  22. #include "llvm/Support/InitLLVM.h"
  23. #include "llvm/Support/Path.h"
  24. #include "llvm/Support/PrettyStackTrace.h"
  25. #include "llvm/Support/Process.h"
  26. #include "llvm/Support/ScopedPrinter.h"
  27. #include "llvm/Support/Signals.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include <system_error>
  30. using namespace llvm;
  31. using namespace object;
  32. namespace {
  33. enum ID {
  34. OPT_INVALID = 0, // This is not an option ID.
  35. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  36. HELPTEXT, METAVAR, VALUES) \
  37. OPT_##ID,
  38. #include "Opts.inc"
  39. #undef OPTION
  40. };
  41. #define PREFIX(NAME, VALUE) \
  42. static constexpr StringLiteral NAME##_init[] = VALUE; \
  43. static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
  44. std::size(NAME##_init) - 1);
  45. #include "Opts.inc"
  46. #undef PREFIX
  47. static constexpr opt::OptTable::Info InfoTable[] = {
  48. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  49. HELPTEXT, METAVAR, VALUES) \
  50. { \
  51. PREFIX, NAME, HELPTEXT, \
  52. METAVAR, OPT_##ID, opt::Option::KIND##Class, \
  53. PARAM, FLAGS, OPT_##GROUP, \
  54. OPT_##ALIAS, ALIASARGS, VALUES},
  55. #include "Opts.inc"
  56. #undef OPTION
  57. };
  58. class CvtResOptTable : public opt::GenericOptTable {
  59. public:
  60. CvtResOptTable() : opt::GenericOptTable(InfoTable, true) {}
  61. };
  62. }
  63. [[noreturn]] static void reportError(Twine Msg) {
  64. errs() << Msg;
  65. exit(1);
  66. }
  67. static void reportError(StringRef Input, std::error_code EC) {
  68. reportError(Twine(Input) + ": " + EC.message() + ".\n");
  69. }
  70. static void error(StringRef Input, Error EC) {
  71. if (!EC)
  72. return;
  73. handleAllErrors(std::move(EC), [&](const ErrorInfoBase &EI) {
  74. reportError(Twine(Input) + ": " + EI.message() + ".\n");
  75. });
  76. }
  77. static void error(Error EC) {
  78. if (!EC)
  79. return;
  80. handleAllErrors(std::move(EC),
  81. [&](const ErrorInfoBase &EI) { reportError(EI.message()); });
  82. }
  83. static uint32_t getTime() {
  84. std::time_t Now = time(nullptr);
  85. if (Now < 0 || !isUInt<32>(Now))
  86. return UINT32_MAX;
  87. return static_cast<uint32_t>(Now);
  88. }
  89. template <typename T> T error(Expected<T> EC) {
  90. if (!EC)
  91. error(EC.takeError());
  92. return std::move(EC.get());
  93. }
  94. template <typename T> T error(StringRef Input, Expected<T> EC) {
  95. if (!EC)
  96. error(Input, EC.takeError());
  97. return std::move(EC.get());
  98. }
  99. template <typename T> T error(StringRef Input, ErrorOr<T> &&EC) {
  100. return error(Input, errorOrToExpected(std::move(EC)));
  101. }
  102. int main(int Argc, const char **Argv) {
  103. InitLLVM X(Argc, Argv);
  104. CvtResOptTable T;
  105. unsigned MAI, MAC;
  106. ArrayRef<const char *> ArgsArr = ArrayRef(Argv + 1, Argc - 1);
  107. opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
  108. if (InputArgs.hasArg(OPT_HELP)) {
  109. T.printHelp(outs(), "llvm-cvtres [options] file...", "Resource Converter");
  110. return 0;
  111. }
  112. bool Verbose = InputArgs.hasArg(OPT_VERBOSE);
  113. COFF::MachineTypes MachineType;
  114. if (opt::Arg *Arg = InputArgs.getLastArg(OPT_MACHINE)) {
  115. MachineType = getMachineType(Arg->getValue());
  116. if (MachineType == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
  117. reportError(Twine("Unsupported machine architecture ") + Arg->getValue() +
  118. "\n");
  119. }
  120. } else {
  121. if (Verbose)
  122. outs() << "Machine architecture not specified; assumed X64.\n";
  123. MachineType = COFF::IMAGE_FILE_MACHINE_AMD64;
  124. }
  125. std::vector<std::string> InputFiles = InputArgs.getAllArgValues(OPT_INPUT);
  126. if (InputFiles.size() == 0) {
  127. reportError("No input file specified.\n");
  128. }
  129. SmallString<128> OutputFile;
  130. if (opt::Arg *Arg = InputArgs.getLastArg(OPT_OUT)) {
  131. OutputFile = Arg->getValue();
  132. } else {
  133. OutputFile = sys::path::filename(StringRef(InputFiles[0]));
  134. sys::path::replace_extension(OutputFile, ".obj");
  135. }
  136. uint32_t DateTimeStamp;
  137. if (llvm::opt::Arg *Arg = InputArgs.getLastArg(OPT_TIMESTAMP)) {
  138. StringRef Value(Arg->getValue());
  139. if (Value.getAsInteger(0, DateTimeStamp))
  140. reportError(Twine("invalid timestamp: ") + Value +
  141. ". Expected 32-bit integer\n");
  142. } else {
  143. DateTimeStamp = getTime();
  144. }
  145. if (Verbose)
  146. outs() << "Machine: " << machineToStr(MachineType) << '\n';
  147. WindowsResourceParser Parser;
  148. for (const auto &File : InputFiles) {
  149. std::unique_ptr<MemoryBuffer> Buffer = error(
  150. File, MemoryBuffer::getFileOrSTDIN(File, /*IsText=*/false,
  151. /*RequiresNullTerminator=*/false));
  152. file_magic Type = identify_magic(Buffer->getMemBufferRef().getBuffer());
  153. if (Type != file_magic::windows_resource)
  154. reportError(File + ": unrecognized file format.\n");
  155. std::unique_ptr<WindowsResource> Binary = error(
  156. File,
  157. WindowsResource::createWindowsResource(Buffer->getMemBufferRef()));
  158. WindowsResource *RF = Binary.get();
  159. if (Verbose) {
  160. int EntryNumber = 0;
  161. ResourceEntryRef Entry = error(RF->getHeadEntry());
  162. bool End = false;
  163. while (!End) {
  164. error(Entry.moveNext(End));
  165. EntryNumber++;
  166. }
  167. outs() << "Number of resources: " << EntryNumber << "\n";
  168. }
  169. std::vector<std::string> Duplicates;
  170. error(Parser.parse(RF, Duplicates));
  171. for (const auto& DupeDiag : Duplicates)
  172. reportError(DupeDiag);
  173. }
  174. if (Verbose) {
  175. Parser.printTree(outs());
  176. }
  177. std::unique_ptr<MemoryBuffer> OutputBuffer =
  178. error(llvm::object::writeWindowsResourceCOFF(MachineType, Parser,
  179. DateTimeStamp));
  180. auto FileOrErr =
  181. FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
  182. if (!FileOrErr)
  183. reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
  184. std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
  185. std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
  186. FileBuffer->getBufferStart());
  187. error(FileBuffer->commit());
  188. if (Verbose) {
  189. std::unique_ptr<MemoryBuffer> Buffer =
  190. error(OutputFile,
  191. MemoryBuffer::getFileOrSTDIN(OutputFile, /*IsText=*/false,
  192. /*RequiresNullTerminator=*/false));
  193. ScopedPrinter W(errs());
  194. W.printBinaryBlock("Output File Raw Data",
  195. Buffer->getMemBufferRef().getBuffer());
  196. }
  197. return 0;
  198. }