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