LLVM  9.0.0svn
WindowsResource.h
Go to the documentation of this file.
1 //===-- WindowsResource.h ---------------------------------------*- 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 // This file declares the .res file class. .res files are intermediate
10 // products of the typical resource-compilation process on Windows. This
11 // process is as follows:
12 //
13 // .rc file(s) ---(rc.exe)---> .res file(s) ---(cvtres.exe)---> COFF file
14 //
15 // .rc files are human-readable scripts that list all resources a program uses.
16 //
17 // They are compiled into .res files, which are a list of the resources in
18 // binary form.
19 //
20 // Finally the data stored in the .res is compiled into a COFF file, where it
21 // is organized in a directory tree structure for optimized access by the
22 // program during runtime.
23 //
24 // Ref: msdn.microsoft.com/en-us/library/windows/desktop/ms648007(v=vs.85).aspx
25 //
26 //===---------------------------------------------------------------------===//
27 
28 #ifndef LLVM_INCLUDE_LLVM_OBJECT_RESFILE_H
29 #define LLVM_INCLUDE_LLVM_OBJECT_RESFILE_H
30 
31 #include "llvm/ADT/ArrayRef.h"
32 #include "llvm/BinaryFormat/COFF.h"
33 #include "llvm/Object/Binary.h"
34 #include "llvm/Object/Error.h"
38 #include "llvm/Support/Endian.h"
39 #include "llvm/Support/Error.h"
40 
41 #include <map>
42 
43 namespace llvm {
44 
45 class raw_ostream;
46 class ScopedPrinter;
47 
48 namespace object {
49 
50 class WindowsResource;
51 
52 const size_t WIN_RES_MAGIC_SIZE = 16;
53 const size_t WIN_RES_NULL_ENTRY_SIZE = 16;
56 const uint16_t WIN_RES_PURE_MOVEABLE = 0x0030;
57 
61 };
62 
63 // Type and Name may each either be an integer ID or a string. This struct is
64 // only used in the case where they are both IDs.
65 struct WinResIDs {
66  uint16_t TypeFlag;
68  uint16_t NameFlag;
70 
71  void setType(uint16_t ID) {
72  TypeFlag = 0xffff;
73  TypeID = ID;
74  }
75 
76  void setName(uint16_t ID) {
77  NameFlag = 0xffff;
78  NameID = ID;
79  }
80 };
81 
88 };
89 
91 public:
92  EmptyResError(Twine Msg, object_error ECOverride)
93  : GenericBinaryError(Msg, ECOverride) {}
94 };
95 
97 public:
98  Error moveNext(bool &End);
99  bool checkTypeString() const { return IsStringType; }
100  ArrayRef<UTF16> getTypeString() const { return Type; }
101  uint16_t getTypeID() const { return TypeID; }
102  bool checkNameString() const { return IsStringName; }
103  ArrayRef<UTF16> getNameString() const { return Name; }
104  uint16_t getNameID() const { return NameID; }
105  uint16_t getDataVersion() const { return Suffix->DataVersion; }
106  uint16_t getLanguage() const { return Suffix->Language; }
107  uint16_t getMemoryFlags() const { return Suffix->MemoryFlags; }
108  uint16_t getMajorVersion() const { return Suffix->Version >> 16; }
109  uint16_t getMinorVersion() const { return Suffix->Version; }
110  uint32_t getCharacteristics() const { return Suffix->Characteristics; }
111  ArrayRef<uint8_t> getData() const { return Data; }
112 
113 private:
114  friend class WindowsResource;
115 
117  Error loadNext();
118 
120  const WindowsResource *Owner);
121 
122  BinaryStreamReader Reader;
123  const WindowsResource *Owner;
124  bool IsStringType;
126  uint16_t TypeID;
127  bool IsStringName;
129  uint16_t NameID;
130  const WinResHeaderSuffix *Suffix = nullptr;
132 };
133 
134 class WindowsResource : public Binary {
135 public:
136  Expected<ResourceEntryRef> getHeadEntry();
137 
138  static bool classof(const Binary *V) { return V->isWinRes(); }
139 
141  createWindowsResource(MemoryBufferRef Source);
142 
143 private:
144  friend class ResourceEntryRef;
145 
147 
148  BinaryByteStream BBS;
149 };
150 
152 public:
153  class TreeNode;
155  Error parse(WindowsResource *WR, std::vector<std::string> &Duplicates);
156  void printTree(raw_ostream &OS) const;
157  const TreeNode &getTree() const { return Root; }
158  const ArrayRef<std::vector<uint8_t>> getData() const { return Data; }
160  return StringTable;
161  }
162 
163  class TreeNode {
164  public:
165  template <typename T>
166  using Children = std::map<T, std::unique_ptr<TreeNode>>;
167 
168  void print(ScopedPrinter &Writer, StringRef Name) const;
169  uint32_t getTreeSize() const;
170  uint32_t getStringIndex() const { return StringIndex; }
171  uint32_t getDataIndex() const { return DataIndex; }
172  uint16_t getMajorVersion() const { return MajorVersion; }
173  uint16_t getMinorVersion() const { return MinorVersion; }
175  bool checkIsDataNode() const { return IsDataNode; }
176  const Children<uint32_t> &getIDChildren() const { return IDChildren; }
178  return StringChildren;
179  }
180 
181  private:
182  friend class WindowsResourceParser;
183 
184  static uint32_t StringCount;
185  static uint32_t DataCount;
186 
187  static std::unique_ptr<TreeNode> createStringNode();
188  static std::unique_ptr<TreeNode> createIDNode();
189  static std::unique_ptr<TreeNode> createDataNode(uint16_t MajorVersion,
190  uint16_t MinorVersion,
192  uint32_t Origin);
193 
194  explicit TreeNode(bool IsStringNode);
195  TreeNode(uint16_t MajorVersion, uint16_t MinorVersion,
197 
198  bool addEntry(const ResourceEntryRef &Entry, uint32_t Origin,
199  bool &IsNewTypeString, bool &IsNewNameString,
200  TreeNode *&Result);
201  TreeNode &addTypeNode(const ResourceEntryRef &Entry, bool &IsNewTypeString);
202  TreeNode &addNameNode(const ResourceEntryRef &Entry, bool &IsNewNameString);
203  bool addLanguageNode(const ResourceEntryRef &Entry, uint32_t Origin,
204  TreeNode *&Result);
205  bool addDataChild(uint32_t ID, uint16_t MajorVersion, uint16_t MinorVersion,
207  TreeNode *&Result);
208  TreeNode &addIDChild(uint32_t ID);
209  TreeNode &addNameChild(ArrayRef<UTF16> NameRef, bool &IsNewString);
210 
211  bool IsDataNode = false;
212  uint32_t StringIndex;
213  uint32_t DataIndex;
214  Children<uint32_t> IDChildren;
215  Children<std::string> StringChildren;
216  uint16_t MajorVersion = 0;
217  uint16_t MinorVersion = 0;
219 
220  // The .res file that defined this TreeNode, for diagnostics.
221  // Index into InputFilenames.
222  uint32_t Origin;
223  };
224 
225 private:
226  TreeNode Root;
227  std::vector<std::vector<uint8_t>> Data;
228  std::vector<std::vector<UTF16>> StringTable;
229 
230  std::vector<std::string> InputFilenames;
231 };
232 
236  uint32_t TimeDateStamp);
237 
238 void printResourceTypeName(uint16_t TypeID, raw_ostream &OS);
239 } // namespace object
240 } // namespace llvm
241 
242 #endif
const size_t WIN_RES_NULL_ENTRY_SIZE
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:48
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Expected< std::unique_ptr< MemoryBuffer > > writeWindowsResourceCOFF(llvm::COFF::MachineTypes MachineType, const WindowsResourceParser &Parser, uint32_t TimeDateStamp)
support::ulittle32_t HeaderSize
support::ulittle16_t NameID
void setName(uint16_t ID)
C::iterator addEntry(C &Container, StringRef InstallName)
support::ulittle16_t TypeID
MachineTypes
Definition: COFF.h:93
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
std::map< T, std::unique_ptr< TreeNode > > Children
const Children< uint32_t > & getIDChildren() const
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
static bool classof(const Binary *V)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
object_error
Definition: Error.h:27
ArrayRef< uint8_t > getData() const
Type::TypeID TypeID
ArrayRef< UTF16 > getTypeString() const
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
const uint16_t WIN_RES_PURE_MOVEABLE
llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
Definition: JSON.cpp:510
bool isWinRes() const
Definition: Binary.h:143
const ArrayRef< std::vector< UTF16 > > getStringTable() const
const uint32_t WIN_RES_HEADER_ALIGNMENT
const ArrayRef< std::vector< uint8_t > > getData() const
void setType(uint16_t ID)
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
support::ulittle32_t DataVersion
support::ulittle16_t MemoryFlags
const size_t WIN_RES_MAGIC_SIZE
EmptyResError(Twine Msg, object_error ECOverride)
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:325
ArrayRef< UTF16 > getNameString() const
Generic binary error.
Definition: Error.h:65
void printResourceTypeName(uint16_t TypeID, raw_ostream &OS)
const uint32_t WIN_RES_DATA_ALIGNMENT
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
const TreeNode & getTree() const
support::ulittle32_t Characteristics
const Children< std::string > & getStringChildren() const