LLVM 20.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"
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
43namespace llvm {
44
45class raw_ostream;
46class ScopedPrinter;
47
48namespace object {
49
50class WindowsResource;
51class ResourceSectionRef;
52struct coff_resource_dir_table;
53
54const size_t WIN_RES_MAGIC_SIZE = 16;
55const 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.
67struct WinResIDs {
72
74 TypeFlag = 0xffff;
75 TypeID = ID;
76 }
77
79 NameFlag = 0xffff;
80 NameID = ID;
81 }
82};
83
90};
91
93public:
95 : GenericBinaryError(Msg, ECOverride) {}
96};
97
99public:
100 Error moveNext(bool &End);
101 bool checkTypeString() const { return IsStringType; }
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
115private:
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
136class WindowsResource : public Binary {
137public:
139
140 static bool classof(const Binary *V) { return V->isWinRes(); }
141
144
145private:
146 friend class ResourceEntryRef;
147
149
151};
152
154public:
155 class TreeNode;
156 WindowsResourceParser(bool MinGW = false);
157 Error parse(WindowsResource *WR, std::vector<std::string> &Duplicates);
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; }
163 ArrayRef<std::vector<uint8_t>> getData() const { return Data; }
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:
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 {
238
241 };
242
243private:
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
basic Basic Alias true
RelocType Type
Definition: COFFYAML.cpp:391
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:331
std::string Name
bool End
Definition: ELF_riscv.cpp:480
Type::TypeID TypeID
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer.
Provides read only access to a subclass of BinaryStream.
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
EmptyResError(Twine Msg, object_error ECOverride)
Generic binary error.
Definition: Error.h:66
ArrayRef< UTF16 > getTypeString() const
ArrayRef< UTF16 > getNameString() const
ArrayRef< uint8_t > getData() const
const Children< std::string > & getStringChildren() const
const Children< uint32_t > & getIDChildren() const
void print(ScopedPrinter &Writer, StringRef Name) const
std::map< T, std::unique_ptr< TreeNode > > Children
void cleanUpManifests(std::vector< std::string > &Duplicates)
const TreeNode & getTree() const
void printTree(raw_ostream &OS) const
ArrayRef< std::vector< uint8_t > > getData() const
ArrayRef< std::vector< UTF16 > > getStringTable() const
Expected< ResourceEntryRef > getHeadEntry()
static bool classof(const Binary *V)
static Expected< std::unique_ptr< WindowsResource > > createWindowsResource(MemoryBufferRef Source)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
MachineTypes
Definition: COFF.h:92
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Expected< std::unique_ptr< MemoryBuffer > > writeWindowsResourceCOFF(llvm::COFF::MachineTypes MachineType, const WindowsResourceParser &Parser, uint32_t TimeDateStamp)
const size_t WIN_RES_NULL_ENTRY_SIZE
const uint32_t WIN_RES_DATA_ALIGNMENT
void printResourceTypeName(uint16_t TypeID, raw_ostream &OS)
const uint32_t WIN_RES_HEADER_ALIGNMENT
object_error
Definition: Error.h:27
const size_t WIN_RES_MAGIC_SIZE
const uint16_t WIN_RES_PURE_MOVEABLE
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Ref
The access may reference the value stored in memory.
support::ulittle32_t HeaderSize
support::ulittle16_t MemoryFlags
support::ulittle32_t Characteristics
support::ulittle32_t DataVersion
support::ulittle16_t TypeID
support::ulittle16_t NameID
void setType(uint16_t ID)
void setName(uint16_t ID)
Definition: regcomp.c:192