LLVM  14.0.0git
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_OBJECT_WINDOWSRESOURCE_H
29 #define LLVM_OBJECT_WINDOWSRESOURCE_H
30 
31 #include "llvm/ADT/ArrayRef.h"
32 #include "llvm/BinaryFormat/COFF.h"
33 #include "llvm/Object/Binary.h"
34 #include "llvm/Object/COFF.h"
35 #include "llvm/Object/Error.h"
39 #include "llvm/Support/Endian.h"
40 #include "llvm/Support/Error.h"
41 
42 #include <map>
43 
44 namespace llvm {
45 
46 class raw_ostream;
47 class ScopedPrinter;
48 
49 namespace object {
50 
51 class WindowsResource;
52 class ResourceSectionRef;
53 
54 const size_t WIN_RES_MAGIC_SIZE = 16;
55 const size_t WIN_RES_NULL_ENTRY_SIZE = 16;
59 
63 };
64 
65 // Type and Name may each either be an integer ID or a string. This struct is
66 // only used in the case where they are both IDs.
67 struct WinResIDs {
72 
74  TypeFlag = 0xffff;
75  TypeID = ID;
76  }
77 
79  NameFlag = 0xffff;
80  NameID = ID;
81  }
82 };
83 
90 };
91 
93 public:
94  EmptyResError(Twine Msg, object_error ECOverride)
95  : GenericBinaryError(Msg, ECOverride) {}
96 };
97 
99 public:
100  Error moveNext(bool &End);
101  bool checkTypeString() const { return IsStringType; }
102  ArrayRef<UTF16> getTypeString() const { return Type; }
103  uint16_t getTypeID() const { return TypeID; }
104  bool checkNameString() const { return IsStringName; }
105  ArrayRef<UTF16> getNameString() const { return Name; }
106  uint16_t getNameID() const { return NameID; }
107  uint16_t getDataVersion() const { return Suffix->DataVersion; }
108  uint16_t getLanguage() const { return Suffix->Language; }
109  uint16_t getMemoryFlags() const { return Suffix->MemoryFlags; }
110  uint16_t getMajorVersion() const { return Suffix->Version >> 16; }
111  uint16_t getMinorVersion() const { return Suffix->Version; }
112  uint32_t getCharacteristics() const { return Suffix->Characteristics; }
113  ArrayRef<uint8_t> getData() const { return Data; }
114 
115 private:
116  friend class WindowsResource;
117 
119  Error loadNext();
120 
122  const WindowsResource *Owner);
123 
124  BinaryStreamReader Reader;
125  const WindowsResource *Owner;
126  bool IsStringType;
129  bool IsStringName;
131  uint16_t NameID;
132  const WinResHeaderSuffix *Suffix = nullptr;
134 };
135 
136 class WindowsResource : public Binary {
137 public:
139 
140  static bool classof(const Binary *V) { return V->isWinRes(); }
141 
144 
145 private:
146  friend class ResourceEntryRef;
147 
149 
150  BinaryByteStream BBS;
151 };
152 
154 public:
155  class TreeNode;
156  WindowsResourceParser(bool MinGW = false);
157  Error parse(WindowsResource *WR, std::vector<std::string> &Duplicates);
158  Error parse(ResourceSectionRef &RSR, StringRef Filename,
159  std::vector<std::string> &Duplicates);
160  void cleanUpManifests(std::vector<std::string> &Duplicates);
161  void printTree(raw_ostream &OS) const;
162  const TreeNode &getTree() const { return Root; }
164  ArrayRef<std::vector<UTF16>> getStringTable() const { return StringTable; }
165 
166  class TreeNode {
167  public:
168  template <typename T>
169  using Children = std::map<T, std::unique_ptr<TreeNode>>;
170 
171  void print(ScopedPrinter &Writer, StringRef Name) const;
172  uint32_t getTreeSize() const;
173  uint32_t getStringIndex() const { return StringIndex; }
174  uint32_t getDataIndex() const { return DataIndex; }
175  uint16_t getMajorVersion() const { return MajorVersion; }
176  uint16_t getMinorVersion() const { return MinorVersion; }
177  uint32_t getCharacteristics() const { return Characteristics; }
178  bool checkIsDataNode() const { return IsDataNode; }
179  const Children<uint32_t> &getIDChildren() const { return IDChildren; }
181  return StringChildren;
182  }
183 
184  private:
185  friend class WindowsResourceParser;
186 
187  // Index is the StringTable vector index for this node's name.
188  static std::unique_ptr<TreeNode> createStringNode(uint32_t Index);
189  static std::unique_ptr<TreeNode> createIDNode();
190  // DataIndex is the Data vector index that the data node points at.
191  static std::unique_ptr<TreeNode> createDataNode(uint16_t MajorVersion,
192  uint16_t MinorVersion,
194  uint32_t Origin,
195  uint32_t DataIndex);
196 
197  explicit TreeNode(uint32_t StringIndex);
198  TreeNode(uint16_t MajorVersion, uint16_t MinorVersion,
199  uint32_t Characteristics, uint32_t Origin, uint32_t DataIndex);
200 
201  bool addEntry(const ResourceEntryRef &Entry, uint32_t Origin,
202  std::vector<std::vector<uint8_t>> &Data,
203  std::vector<std::vector<UTF16>> &StringTable,
204  TreeNode *&Result);
205  TreeNode &addTypeNode(const ResourceEntryRef &Entry,
206  std::vector<std::vector<UTF16>> &StringTable);
207  TreeNode &addNameNode(const ResourceEntryRef &Entry,
208  std::vector<std::vector<UTF16>> &StringTable);
209  bool addLanguageNode(const ResourceEntryRef &Entry, uint32_t Origin,
210  std::vector<std::vector<uint8_t>> &Data,
211  TreeNode *&Result);
212  bool addDataChild(uint32_t ID, uint16_t MajorVersion, uint16_t MinorVersion,
214  uint32_t DataIndex, TreeNode *&Result);
215  TreeNode &addIDChild(uint32_t ID);
216  TreeNode &addNameChild(ArrayRef<UTF16> NameRef,
217  std::vector<std::vector<UTF16>> &StringTable);
218  void shiftDataIndexDown(uint32_t Index);
219 
220  bool IsDataNode = false;
221  uint32_t StringIndex;
222  uint32_t DataIndex;
223  Children<uint32_t> IDChildren;
224  Children<std::string> StringChildren;
225  uint16_t MajorVersion = 0;
226  uint16_t MinorVersion = 0;
228 
229  // The .res file that defined this TreeNode, for diagnostics.
230  // Index into InputFilenames.
231  uint32_t Origin;
232  };
233 
234  struct StringOrID {
235  bool IsString;
238 
241  };
242 
243 private:
244  Error addChildren(TreeNode &Node, ResourceSectionRef &RSR,
245  const coff_resource_dir_table &Table, uint32_t Origin,
246  std::vector<StringOrID> &Context,
247  std::vector<std::string> &Duplicates);
248  bool shouldIgnoreDuplicate(const ResourceEntryRef &Entry) const;
249  bool shouldIgnoreDuplicate(const std::vector<StringOrID> &Context) const;
250 
251  TreeNode Root;
252  std::vector<std::vector<uint8_t>> Data;
253  std::vector<std::vector<UTF16>> StringTable;
254 
255  std::vector<std::string> InputFilenames;
256 
257  bool MinGW;
258 };
259 
262  const WindowsResourceParser &Parser,
263  uint32_t TimeDateStamp);
264 
266 } // namespace object
267 } // namespace llvm
268 
269 #endif
llvm::object::WindowsResourceParser::TreeNode::getStringIndex
uint32_t getStringIndex() const
Definition: WindowsResource.h:173
BinaryStreamReader.h
llvm::object::ResourceEntryRef::getMajorVersion
uint16_t getMajorVersion() const
Definition: WindowsResource.h:110
llvm::object::WindowsResourceParser::TreeNode::Children
std::map< T, std::unique_ptr< TreeNode > > Children
Definition: WindowsResource.h:169
llvm::object::WindowsResourceParser::printTree
void printTree(raw_ostream &OS) const
Definition: WindowsResource.cpp:376
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::object::WindowsResource::createWindowsResource
static Expected< std::unique_ptr< WindowsResource > > createWindowsResource(MemoryBufferRef Source)
Definition: WindowsResource.cpp:60
llvm::object::ResourceEntryRef::getNameString
ArrayRef< UTF16 > getNameString() const
Definition: WindowsResource.h:105
llvm::object::ResourceEntryRef::getCharacteristics
uint32_t getCharacteristics() const
Definition: WindowsResource.h:112
llvm::object::ResourceEntryRef::getMemoryFlags
uint16_t getMemoryFlags() const
Definition: WindowsResource.h:109
llvm::support::detail::packed_endian_specific_integral
Definition: Endian.h:206
llvm::object::WindowsResourceParser::StringOrID::ID
uint32_t ID
Definition: WindowsResource.h:237
BinaryByteStream.h
llvm::object::WinResHeaderSuffix::Characteristics
support::ulittle32_t Characteristics
Definition: WindowsResource.h:89
llvm::object::ResourceEntryRef::getData
ArrayRef< uint8_t > getData() const
Definition: WindowsResource.h:113
llvm::object::WindowsResourceParser::TreeNode::getDataIndex
uint32_t getDataIndex() const
Definition: WindowsResource.h:174
llvm::object::WindowsResourceParser::TreeNode
Definition: WindowsResource.h:166
llvm::object::WinResIDs
Definition: WindowsResource.h:67
llvm::object::WIN_RES_PURE_MOVEABLE
const uint16_t WIN_RES_PURE_MOVEABLE
Definition: WindowsResource.h:58
llvm::object::ResourceEntryRef::getMinorVersion
uint16_t getMinorVersion() const
Definition: WindowsResource.h:111
Error.h
llvm::object::WinResHeaderPrefix
Definition: WindowsResource.h:60
COFF.h
llvm::object::WindowsResourceParser::StringOrID::IsString
bool IsString
Definition: WindowsResource.h:235
llvm::object::ResourceEntryRef::getTypeID
uint16_t getTypeID() const
Definition: WindowsResource.h:103
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::object::WindowsResourceParser::TreeNode::getStringChildren
const Children< std::string > & getStringChildren() const
Definition: WindowsResource.h:180
llvm::object::WindowsResourceParser::TreeNode::getIDChildren
const Children< uint32_t > & getIDChildren() const
Definition: WindowsResource.h:179
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::object::ResourceEntryRef::getTypeString
ArrayRef< UTF16 > getTypeString() const
Definition: WindowsResource.h:102
llvm::object::WinResIDs::setName
void setName(uint16_t ID)
Definition: WindowsResource.h:78
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::object::WindowsResourceParser
Definition: WindowsResource.h:153
llvm::object::WindowsResourceParser::StringOrID::String
ArrayRef< UTF16 > String
Definition: WindowsResource.h:236
llvm::object::WindowsResource
Definition: WindowsResource.h:136
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::object::WinResHeaderSuffix::DataVersion
support::ulittle32_t DataVersion
Definition: WindowsResource.h:85
llvm::object::WindowsResourceParser::TreeNode::checkIsDataNode
bool checkIsDataNode() const
Definition: WindowsResource.h:178
llvm::object::ResourceEntryRef::checkTypeString
bool checkTypeString() const
Definition: WindowsResource.h:101
llvm::object::WindowsResourceParser::TreeNode::getCharacteristics
uint32_t getCharacteristics() const
Definition: WindowsResource.h:177
Error.h
llvm::object::Binary::isWinRes
bool isWinRes() const
Definition: Binary.h:154
llvm::object::object_error
object_error
Definition: Error.h:29
llvm::BinaryByteStream
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer.
Definition: BinaryByteStream.h:31
llvm::object::WindowsResourceParser::TreeNode::getMinorVersion
uint16_t getMinorVersion() const
Definition: WindowsResource.h:176
llvm::object::WinResHeaderSuffix
Definition: WindowsResource.h:84
llvm::object::WindowsResourceParser::TreeNode::getMajorVersion
uint16_t getMajorVersion() const
Definition: WindowsResource.h:175
false
Definition: StackSlotColoring.cpp:142
llvm::object::WIN_RES_DATA_ALIGNMENT
const uint32_t WIN_RES_DATA_ALIGNMENT
Definition: WindowsResource.h:57
llvm::object::WindowsResourceParser::StringOrID::StringOrID
StringOrID(ArrayRef< UTF16 > String)
Definition: WindowsResource.h:240
llvm::object::WindowsResourceParser::getTree
const TreeNode & getTree() const
Definition: WindowsResource.h:162
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::object::WinResIDs::NameFlag
uint16_t NameFlag
Definition: WindowsResource.h:70
llvm::object::GenericBinaryError
Generic binary error.
Definition: Error.h:67
llvm::BinaryStreamReader
Provides read only access to a subclass of BinaryStream.
Definition: BinaryStreamReader.h:31
TypeID
Type::TypeID TypeID
Definition: Mips16HardFloat.cpp:102
llvm::object::EmptyResError::EmptyResError
EmptyResError(Twine Msg, object_error ECOverride)
Definition: WindowsResource.h:94
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::object::WindowsResourceParser::parse
Error parse(WindowsResource *WR, std::vector< std::string > &Duplicates)
Definition: WindowsResource.cpp:329
llvm::object::WindowsResourceParser::WindowsResourceParser
WindowsResourceParser(bool MinGW=false)
Definition: WindowsResource.cpp:140
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::object::WinResHeaderSuffix::MemoryFlags
support::ulittle16_t MemoryFlags
Definition: WindowsResource.h:86
llvm::object::WinResIDs::TypeFlag
uint16_t TypeFlag
Definition: WindowsResource.h:68
llvm::object::WIN_RES_NULL_ENTRY_SIZE
const size_t WIN_RES_NULL_ENTRY_SIZE
Definition: WindowsResource.h:55
llvm::object::WindowsResourceParser::getStringTable
ArrayRef< std::vector< UTF16 > > getStringTable() const
Definition: WindowsResource.h:164
llvm::object::writeWindowsResourceCOFF
Expected< std::unique_ptr< MemoryBuffer > > writeWindowsResourceCOFF(llvm::COFF::MachineTypes MachineType, const WindowsResourceParser &Parser, uint32_t TimeDateStamp)
Definition: WindowsResource.cpp:1004
llvm::object::WindowsResourceParser::StringOrID::StringOrID
StringOrID(uint32_t ID)
Definition: WindowsResource.h:239
ArrayRef.h
TemplateParamKind::Type
@ Type
llvm::object::WindowsResourceParser::cleanUpManifests
void cleanUpManifests(std::vector< std::string > &Duplicates)
Definition: WindowsResource.cpp:263
llvm::object::EmptyResError
Definition: WindowsResource.h:92
llvm::object::ResourceEntryRef::getDataVersion
uint16_t getDataVersion() const
Definition: WindowsResource.h:107
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:100
llvm::ArrayRef< UTF16 >
llvm::object::ResourceEntryRef::getLanguage
uint16_t getLanguage() const
Definition: WindowsResource.h:108
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::Binary
Definition: Binary.h:32
llvm::object::WinResIDs::setType
void setType(uint16_t ID)
Definition: WindowsResource.h:73
uint32_t
llvm::object::ResourceEntryRef::getNameID
uint16_t getNameID() const
Definition: WindowsResource.h:106
llvm::object::WindowsResourceParser::TreeNode::print
void print(ScopedPrinter &Writer, StringRef Name) const
Definition: WindowsResource.cpp:551
llvm::object::printResourceTypeName
void printResourceTypeName(uint16_t TypeID, raw_ostream &OS)
Definition: WindowsResource.cpp:143
Node
Definition: ItaniumDemangle.h:234
llvm::object::WindowsResourceParser::getData
ArrayRef< std::vector< uint8_t > > getData() const
Definition: WindowsResource.h:163
llvm::object::WindowsResource::classof
static bool classof(const Binary *V)
Definition: WindowsResource.h:140
ConvertUTF.h
llvm::object::WinResHeaderPrefix::HeaderSize
support::ulittle32_t HeaderSize
Definition: WindowsResource.h:62
llvm::object::WindowsResourceParser::StringOrID
Definition: WindowsResource.h:234
llvm::object::WinResHeaderPrefix::DataSize
support::ulittle32_t DataSize
Definition: WindowsResource.h:61
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::object::WindowsResourceParser::TreeNode::getTreeSize
uint32_t getTreeSize() const
Definition: WindowsResource.cpp:565
uint16_t
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::object::WindowsResource::getHeadEntry
Expected< ResourceEntryRef > getHeadEntry()
Definition: WindowsResource.cpp:69
llvm::ScopedPrinter
Definition: ScopedPrinter.h:70
llvm::object::WIN_RES_HEADER_ALIGNMENT
const uint32_t WIN_RES_HEADER_ALIGNMENT
Definition: WindowsResource.h:56
llvm::object::WinResHeaderSuffix::Version
support::ulittle32_t Version
Definition: WindowsResource.h:88
llvm::COFF::MachineTypes
MachineTypes
Definition: COFF.h:93
llvm::object::ResourceSectionRef
Definition: COFF.h:1239
llvm::object::ResourceEntryRef::checkNameString
bool checkNameString() const
Definition: WindowsResource.h:104
llvm::object::WinResIDs::NameID
support::ulittle16_t NameID
Definition: WindowsResource.h:71
Binary.h
COFF.h
llvm::object::ResourceEntryRef
Definition: WindowsResource.h:98
llvm::object::WIN_RES_MAGIC_SIZE
const size_t WIN_RES_MAGIC_SIZE
Definition: WindowsResource.h:54
Characteristics
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:326
llvm::object::coff_resource_dir_table
Definition: COFF.h:784
llvm::object::WinResHeaderSuffix::Language
support::ulittle16_t Language
Definition: WindowsResource.h:87
Endian.h
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1815
llvm::object::WinResIDs::TypeID
support::ulittle16_t TypeID
Definition: WindowsResource.h:69
llvm::BinaryStreamRef
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Definition: BinaryStreamRef.h:156
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37
llvm::object::ResourceEntryRef::moveNext
Error moveNext(bool &End)
Definition: WindowsResource.cpp:88