1 //===- llvm-cvtres.cpp - Serialize .res files into .obj ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Serialize .res files into .obj files.  This is intended to be a
11 // platform-independent port of Microsoft's cvtres.exe.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/Object/Binary.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/ManagedStatic.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/Process.h"
28 #include "llvm/Support/Signals.h"
29 #include "llvm/Support/raw_ostream.h"
30 
31 #include <system_error>
32 
33 using namespace llvm;
34 using namespace object;
35 
36 namespace {
37 
38 enum ID {
39   OPT_INVALID = 0, // This is not an option ID.
40 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
41                HELPTEXT, METAVAR, VALUES)                                      \
42   OPT_##ID,
43 #include "Opts.inc"
44 #undef OPTION
45 };
46 
47 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
48 #include "Opts.inc"
49 #undef PREFIX
50 
51 static const opt::OptTable::Info InfoTable[] = {
52 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
53                HELPTEXT, METAVAR, VALUES)                                      \
54   {                                                                            \
55       PREFIX,      NAME,      HELPTEXT,                                        \
56       METAVAR,     OPT_##ID,  opt::Option::KIND##Class,                        \
57       PARAM,       FLAGS,     OPT_##GROUP,                                     \
58       OPT_##ALIAS, ALIASARGS, VALUES},
59 #include "Opts.inc"
60 #undef OPTION
61 };
62 
63 class CvtResOptTable : public opt::OptTable {
64 public:
CvtResOptTable()65   CvtResOptTable() : OptTable(InfoTable, true) {}
66 };
67 }
68 
reportError(Twine Msg)69 LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
70   errs() << Msg;
71   exit(1);
72 }
73 
reportError(StringRef Input,std::error_code EC)74 static void reportError(StringRef Input, std::error_code EC) {
75   reportError(Twine(Input) + ": " + EC.message() + ".\n");
76 }
77 
error(std::error_code EC)78 void error(std::error_code EC) {
79   if (!EC)
80     return;
81   reportError(EC.message() + ".\n");
82 }
83 
error(Error EC)84 void error(Error EC) {
85   if (!EC)
86     return;
87   handleAllErrors(std::move(EC),
88                   [&](const ErrorInfoBase &EI) { reportError(EI.message()); });
89 }
90 
error(Expected<T> EC)91 template <typename T> T error(Expected<T> EC) {
92   if (!EC)
93     error(EC.takeError());
94   return std::move(EC.get());
95 }
96 
main(int Argc,const char ** Argv)97 int main(int Argc, const char **Argv) {
98   InitLLVM X(Argc, Argv);
99 
100   CvtResOptTable T;
101   unsigned MAI, MAC;
102   ArrayRef<const char *> ArgsArr = makeArrayRef(Argv + 1, Argc - 1);
103   opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC);
104 
105   if (InputArgs.hasArg(OPT_HELP)) {
106     T.PrintHelp(outs(), "cvtres", "Resource Converter", false);
107     return 0;
108   }
109 
110   bool Verbose = InputArgs.hasArg(OPT_VERBOSE);
111 
112   COFF::MachineTypes MachineType;
113 
114   if (InputArgs.hasArg(OPT_MACHINE)) {
115     std::string MachineString = InputArgs.getLastArgValue(OPT_MACHINE).upper();
116     MachineType = StringSwitch<COFF::MachineTypes>(MachineString)
117                       .Case("ARM", COFF::IMAGE_FILE_MACHINE_ARMNT)
118                       .Case("ARM64", COFF::IMAGE_FILE_MACHINE_ARM64)
119                       .Case("X64", COFF::IMAGE_FILE_MACHINE_AMD64)
120                       .Case("X86", COFF::IMAGE_FILE_MACHINE_I386)
121                       .Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
122     if (MachineType == COFF::IMAGE_FILE_MACHINE_UNKNOWN)
123       reportError("Unsupported machine architecture");
124   } else {
125     if (Verbose)
126       outs() << "Machine architecture not specified; assumed X64.\n";
127     MachineType = COFF::IMAGE_FILE_MACHINE_AMD64;
128   }
129 
130   std::vector<std::string> InputFiles = InputArgs.getAllArgValues(OPT_INPUT);
131 
132   if (InputFiles.size() == 0) {
133     reportError("No input file specified.\n");
134   }
135 
136   SmallString<128> OutputFile;
137 
138   if (InputArgs.hasArg(OPT_OUT)) {
139     OutputFile = InputArgs.getLastArgValue(OPT_OUT);
140   } else {
141     OutputFile = sys::path::filename(StringRef(InputFiles[0]));
142     sys::path::replace_extension(OutputFile, ".obj");
143   }
144 
145   if (Verbose) {
146     outs() << "Machine: ";
147     switch (MachineType) {
148     case COFF::IMAGE_FILE_MACHINE_ARM64:
149       outs() << "ARM64\n";
150       break;
151     case COFF::IMAGE_FILE_MACHINE_ARMNT:
152       outs() << "ARM\n";
153       break;
154     case COFF::IMAGE_FILE_MACHINE_I386:
155       outs() << "X86\n";
156       break;
157     default:
158       outs() << "X64\n";
159     }
160   }
161 
162   WindowsResourceParser Parser;
163 
164   for (const auto &File : InputFiles) {
165     Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
166     if (!BinaryOrErr)
167       reportError(File, errorToErrorCode(BinaryOrErr.takeError()));
168 
169     Binary &Binary = *BinaryOrErr.get().getBinary();
170 
171     WindowsResource *RF = dyn_cast<WindowsResource>(&Binary);
172     if (!RF)
173       reportError(File + ": unrecognized file format.\n");
174 
175     if (Verbose) {
176       int EntryNumber = 0;
177       ResourceEntryRef Entry = error(RF->getHeadEntry());
178       bool End = false;
179       while (!End) {
180         error(Entry.moveNext(End));
181         EntryNumber++;
182       }
183       outs() << "Number of resources: " << EntryNumber << "\n";
184     }
185 
186     error(Parser.parse(RF));
187   }
188 
189   if (Verbose) {
190     Parser.printTree(outs());
191   }
192 
193   std::unique_ptr<MemoryBuffer> OutputBuffer =
194       error(llvm::object::writeWindowsResourceCOFF(MachineType, Parser));
195   auto FileOrErr =
196       FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
197   if (!FileOrErr)
198     reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
199   std::unique_ptr<FileOutputBuffer> FileBuffer = std::move(*FileOrErr);
200   std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
201             FileBuffer->getBufferStart());
202   error(FileBuffer->commit());
203 
204   if (Verbose) {
205     Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(OutputFile);
206     if (!BinaryOrErr)
207       reportError(OutputFile, errorToErrorCode(BinaryOrErr.takeError()));
208     Binary &Binary = *BinaryOrErr.get().getBinary();
209     ScopedPrinter W(errs());
210     W.printBinaryBlock("Output File Raw Data", Binary.getData());
211   }
212 
213   return 0;
214 }
215