LLVM  14.0.0git
WindowsResource.cpp
Go to the documentation of this file.
1 //===-- WindowsResource.cpp -------------------------------------*- 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 implements the .res file class.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/Object/COFF.h"
19 #include <ctime>
20 #include <queue>
21 #include <system_error>
22 
23 using namespace llvm;
24 using namespace object;
25 
26 namespace llvm {
27 namespace object {
28 
29 #define RETURN_IF_ERROR(X) \
30  if (auto EC = X) \
31  return EC;
32 
33 #define UNWRAP_REF_OR_RETURN(Name, Expr) \
34  auto Name##OrErr = Expr; \
35  if (!Name##OrErr) \
36  return Name##OrErr.takeError(); \
37  const auto &Name = *Name##OrErr;
38 
39 #define UNWRAP_OR_RETURN(Name, Expr) \
40  auto Name##OrErr = Expr; \
41  if (!Name##OrErr) \
42  return Name##OrErr.takeError(); \
43  auto Name = *Name##OrErr;
44 
45 const uint32_t MIN_HEADER_SIZE = 7 * sizeof(uint32_t) + 2 * sizeof(uint16_t);
46 
47 // COFF files seem to be inconsistent with alignment between sections, just use
48 // 8-byte because it makes everyone happy.
50 
51 WindowsResource::WindowsResource(MemoryBufferRef Source)
52  : Binary(Binary::ID_WinRes, Source) {
53  size_t LeadingSize = WIN_RES_MAGIC_SIZE + WIN_RES_NULL_ENTRY_SIZE;
54  BBS = BinaryByteStream(Data.getBuffer().drop_front(LeadingSize),
56 }
57 
58 // static
60 WindowsResource::createWindowsResource(MemoryBufferRef Source) {
61  if (Source.getBufferSize() < WIN_RES_MAGIC_SIZE + WIN_RES_NULL_ENTRY_SIZE)
62  return make_error<GenericBinaryError>(
63  Source.getBufferIdentifier() + ": too small to be a resource file",
64  object_error::invalid_file_type);
65  std::unique_ptr<WindowsResource> Ret(new WindowsResource(Source));
66  return std::move(Ret);
67 }
68 
69 Expected<ResourceEntryRef> WindowsResource::getHeadEntry() {
70  if (BBS.getLength() < sizeof(WinResHeaderPrefix) + sizeof(WinResHeaderSuffix))
71  return make_error<EmptyResError>(getFileName() + " contains no entries",
72  object_error::unexpected_eof);
73  return ResourceEntryRef::create(BinaryStreamRef(BBS), this);
74 }
75 
76 ResourceEntryRef::ResourceEntryRef(BinaryStreamRef Ref,
77  const WindowsResource *Owner)
78  : Reader(Ref), Owner(Owner) {}
79 
81 ResourceEntryRef::create(BinaryStreamRef BSR, const WindowsResource *Owner) {
82  auto Ref = ResourceEntryRef(BSR, Owner);
83  if (auto E = Ref.loadNext())
84  return std::move(E);
85  return Ref;
86 }
87 
89  // Reached end of all the entries.
90  if (Reader.bytesRemaining() == 0) {
91  End = true;
92  return Error::success();
93  }
94  RETURN_IF_ERROR(loadNext());
95 
96  return Error::success();
97 }
98 
100  ArrayRef<UTF16> &Str, bool &IsString) {
101  uint16_t IDFlag;
102  RETURN_IF_ERROR(Reader.readInteger(IDFlag));
103  IsString = IDFlag != 0xffff;
104 
105  if (IsString) {
106  Reader.setOffset(
107  Reader.getOffset() -
108  sizeof(uint16_t)); // Re-read the bytes which we used to check the flag.
109  RETURN_IF_ERROR(Reader.readWideString(Str));
110  } else
111  RETURN_IF_ERROR(Reader.readInteger(ID));
112 
113  return Error::success();
114 }
115 
116 Error ResourceEntryRef::loadNext() {
117  const WinResHeaderPrefix *Prefix;
119 
120  if (Prefix->HeaderSize < MIN_HEADER_SIZE)
121  return make_error<GenericBinaryError>(Owner->getFileName() +
122  ": header size too small",
124 
125  RETURN_IF_ERROR(readStringOrId(Reader, TypeID, Type, IsStringType));
126 
127  RETURN_IF_ERROR(readStringOrId(Reader, NameID, Name, IsStringName));
128 
130 
131  RETURN_IF_ERROR(Reader.readObject(Suffix));
132 
133  RETURN_IF_ERROR(Reader.readArray(Data, Prefix->DataSize));
134 
136 
137  return Error::success();
138 }
139 
141  : Root(false), MinGW(MinGW) {}
142 
144  switch (TypeID) {
145  case 1: OS << "CURSOR (ID 1)"; break;
146  case 2: OS << "BITMAP (ID 2)"; break;
147  case 3: OS << "ICON (ID 3)"; break;
148  case 4: OS << "MENU (ID 4)"; break;
149  case 5: OS << "DIALOG (ID 5)"; break;
150  case 6: OS << "STRINGTABLE (ID 6)"; break;
151  case 7: OS << "FONTDIR (ID 7)"; break;
152  case 8: OS << "FONT (ID 8)"; break;
153  case 9: OS << "ACCELERATOR (ID 9)"; break;
154  case 10: OS << "RCDATA (ID 10)"; break;
155  case 11: OS << "MESSAGETABLE (ID 11)"; break;
156  case 12: OS << "GROUP_CURSOR (ID 12)"; break;
157  case 14: OS << "GROUP_ICON (ID 14)"; break;
158  case 16: OS << "VERSIONINFO (ID 16)"; break;
159  case 17: OS << "DLGINCLUDE (ID 17)"; break;
160  case 19: OS << "PLUGPLAY (ID 19)"; break;
161  case 20: OS << "VXD (ID 20)"; break;
162  case 21: OS << "ANICURSOR (ID 21)"; break;
163  case 22: OS << "ANIICON (ID 22)"; break;
164  case 23: OS << "HTML (ID 23)"; break;
165  case 24: OS << "MANIFEST (ID 24)"; break;
166  default: OS << "ID " << TypeID; break;
167  }
168 }
169 
170 static bool convertUTF16LEToUTF8String(ArrayRef<UTF16> Src, std::string &Out) {
172  return convertUTF16ToUTF8String(Src, Out);
173 
174  std::vector<UTF16> EndianCorrectedSrc;
175  EndianCorrectedSrc.resize(Src.size() + 1);
176  llvm::copy(Src, EndianCorrectedSrc.begin() + 1);
177  EndianCorrectedSrc[0] = UNI_UTF16_BYTE_ORDER_MARK_SWAPPED;
178  return convertUTF16ToUTF8String(makeArrayRef(EndianCorrectedSrc), Out);
179 }
180 
181 static std::string makeDuplicateResourceError(
182  const ResourceEntryRef &Entry, StringRef File1, StringRef File2) {
183  std::string Ret;
185 
186  OS << "duplicate resource:";
187 
188  OS << " type ";
189  if (Entry.checkTypeString()) {
190  std::string UTF8;
191  if (!convertUTF16LEToUTF8String(Entry.getTypeString(), UTF8))
192  UTF8 = "(failed conversion from UTF16)";
193  OS << '\"' << UTF8 << '\"';
194  } else
195  printResourceTypeName(Entry.getTypeID(), OS);
196 
197  OS << "/name ";
198  if (Entry.checkNameString()) {
199  std::string UTF8;
200  if (!convertUTF16LEToUTF8String(Entry.getNameString(), UTF8))
201  UTF8 = "(failed conversion from UTF16)";
202  OS << '\"' << UTF8 << '\"';
203  } else {
204  OS << "ID " << Entry.getNameID();
205  }
206 
207  OS << "/language " << Entry.getLanguage() << ", in " << File1 << " and in "
208  << File2;
209 
210  return OS.str();
211 }
212 
214  raw_string_ostream &OS, bool IsType, bool IsID) {
215  if (S.IsString) {
216  std::string UTF8;
217  if (!convertUTF16LEToUTF8String(S.String, UTF8))
218  UTF8 = "(failed conversion from UTF16)";
219  OS << '\"' << UTF8 << '\"';
220  } else if (IsType)
221  printResourceTypeName(S.ID, OS);
222  else if (IsID)
223  OS << "ID " << S.ID;
224  else
225  OS << S.ID;
226 }
227 
228 static std::string makeDuplicateResourceError(
229  const std::vector<WindowsResourceParser::StringOrID> &Context,
230  StringRef File1, StringRef File2) {
231  std::string Ret;
233 
234  OS << "duplicate resource:";
235 
236  if (Context.size() >= 1) {
237  OS << " type ";
238  printStringOrID(Context[0], OS, /* IsType */ true, /* IsID */ true);
239  }
240 
241  if (Context.size() >= 2) {
242  OS << "/name ";
243  printStringOrID(Context[1], OS, /* IsType */ false, /* IsID */ true);
244  }
245 
246  if (Context.size() >= 3) {
247  OS << "/language ";
248  printStringOrID(Context[2], OS, /* IsType */ false, /* IsID */ false);
249  }
250  OS << ", in " << File1 << " and in " << File2;
251 
252  return OS.str();
253 }
254 
255 // MinGW specific. Remove default manifests (with language zero) if there are
256 // other manifests present, and report an error if there are more than one
257 // manifest with a non-zero language code.
258 // GCC has the concept of a default manifest resource object, which gets
259 // linked in implicitly if present. This default manifest has got language
260 // id zero, and should be dropped silently if there's another manifest present.
261 // If the user resources surprisignly had a manifest with language id zero,
262 // we should also ignore the duplicate default manifest.
264  std::vector<std::string> &Duplicates) {
265  auto TypeIt = Root.IDChildren.find(/* RT_MANIFEST */ 24);
266  if (TypeIt == Root.IDChildren.end())
267  return;
268 
269  TreeNode *TypeNode = TypeIt->second.get();
270  auto NameIt =
271  TypeNode->IDChildren.find(/* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 1);
272  if (NameIt == TypeNode->IDChildren.end())
273  return;
274 
275  TreeNode *NameNode = NameIt->second.get();
276  if (NameNode->IDChildren.size() <= 1)
277  return; // None or one manifest present, all good.
278 
279  // If we have more than one manifest, drop the language zero one if present,
280  // and check again.
281  auto LangZeroIt = NameNode->IDChildren.find(0);
282  if (LangZeroIt != NameNode->IDChildren.end() &&
283  LangZeroIt->second->IsDataNode) {
284  uint32_t RemovedIndex = LangZeroIt->second->DataIndex;
285  NameNode->IDChildren.erase(LangZeroIt);
286  Data.erase(Data.begin() + RemovedIndex);
287  Root.shiftDataIndexDown(RemovedIndex);
288 
289  // If we're now down to one manifest, all is good.
290  if (NameNode->IDChildren.size() <= 1)
291  return;
292  }
293 
294  // More than one non-language-zero manifest
295  auto FirstIt = NameNode->IDChildren.begin();
296  uint32_t FirstLang = FirstIt->first;
297  TreeNode *FirstNode = FirstIt->second.get();
298  auto LastIt = NameNode->IDChildren.rbegin();
299  uint32_t LastLang = LastIt->first;
300  TreeNode *LastNode = LastIt->second.get();
301  Duplicates.push_back(
302  ("duplicate non-default manifests with languages " + Twine(FirstLang) +
303  " in " + InputFilenames[FirstNode->Origin] + " and " + Twine(LastLang) +
304  " in " + InputFilenames[LastNode->Origin])
305  .str());
306 }
307 
308 // Ignore duplicates of manifests with language zero (the default manifest),
309 // in case the user has provided a manifest with that language id. See
310 // the function comment above for context. Only returns true if MinGW is set
311 // to true.
312 bool WindowsResourceParser::shouldIgnoreDuplicate(
313  const ResourceEntryRef &Entry) const {
314  return MinGW && !Entry.checkTypeString() &&
315  Entry.getTypeID() == /* RT_MANIFEST */ 24 &&
316  !Entry.checkNameString() &&
317  Entry.getNameID() == /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 1 &&
318  Entry.getLanguage() == 0;
319 }
320 
321 bool WindowsResourceParser::shouldIgnoreDuplicate(
322  const std::vector<StringOrID> &Context) const {
323  return MinGW && Context.size() == 3 && !Context[0].IsString &&
324  Context[0].ID == /* RT_MANIFEST */ 24 && !Context[1].IsString &&
325  Context[1].ID == /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 1 &&
326  !Context[2].IsString && Context[2].ID == 0;
327 }
328 
330  std::vector<std::string> &Duplicates) {
331  auto EntryOrErr = WR->getHeadEntry();
332  if (!EntryOrErr) {
333  auto E = EntryOrErr.takeError();
334  if (E.isA<EmptyResError>()) {
335  // Check if the .res file contains no entries. In this case we don't have
336  // to throw an error but can rather just return without parsing anything.
337  // This applies for files which have a valid PE header magic and the
338  // mandatory empty null resource entry. Files which do not fit this
339  // criteria would have already been filtered out by
340  // WindowsResource::createWindowsResource().
342  return Error::success();
343  }
344  return E;
345  }
346 
347  ResourceEntryRef Entry = EntryOrErr.get();
348  uint32_t Origin = InputFilenames.size();
349  InputFilenames.push_back(std::string(WR->getFileName()));
350  bool End = false;
351  while (!End) {
352 
353  TreeNode *Node;
354  bool IsNewNode = Root.addEntry(Entry, Origin, Data, StringTable, Node);
355  if (!IsNewNode) {
356  if (!shouldIgnoreDuplicate(Entry))
357  Duplicates.push_back(makeDuplicateResourceError(
358  Entry, InputFilenames[Node->Origin], WR->getFileName()));
359  }
360 
361  RETURN_IF_ERROR(Entry.moveNext(End));
362  }
363 
364  return Error::success();
365 }
366 
368  std::vector<std::string> &Duplicates) {
369  UNWRAP_REF_OR_RETURN(BaseTable, RSR.getBaseTable());
370  uint32_t Origin = InputFilenames.size();
371  InputFilenames.push_back(std::string(Filename));
372  std::vector<StringOrID> Context;
373  return addChildren(Root, RSR, BaseTable, Origin, Context, Duplicates);
374 }
375 
377  ScopedPrinter Writer(OS);
378  Root.print(Writer, "Resource Tree");
379 }
380 
381 bool WindowsResourceParser::TreeNode::addEntry(
382  const ResourceEntryRef &Entry, uint32_t Origin,
383  std::vector<std::vector<uint8_t>> &Data,
384  std::vector<std::vector<UTF16>> &StringTable, TreeNode *&Result) {
385  TreeNode &TypeNode = addTypeNode(Entry, StringTable);
386  TreeNode &NameNode = TypeNode.addNameNode(Entry, StringTable);
387  return NameNode.addLanguageNode(Entry, Origin, Data, Result);
388 }
389 
390 Error WindowsResourceParser::addChildren(TreeNode &Node,
391  ResourceSectionRef &RSR,
392  const coff_resource_dir_table &Table,
393  uint32_t Origin,
394  std::vector<StringOrID> &Context,
395  std::vector<std::string> &Duplicates) {
396 
397  for (int i = 0; i < Table.NumberOfNameEntries + Table.NumberOfIDEntries;
398  i++) {
399  UNWRAP_REF_OR_RETURN(Entry, RSR.getTableEntry(Table, i));
400  TreeNode *Child;
401 
402  if (Entry.Offset.isSubDir()) {
403 
404  // Create a new subdirectory and recurse
405  if (i < Table.NumberOfNameEntries) {
406  UNWRAP_OR_RETURN(NameString, RSR.getEntryNameString(Entry));
407  Child = &Node.addNameChild(NameString, StringTable);
408  Context.push_back(StringOrID(NameString));
409  } else {
410  Child = &Node.addIDChild(Entry.Identifier.ID);
411  Context.push_back(StringOrID(Entry.Identifier.ID));
412  }
413 
414  UNWRAP_REF_OR_RETURN(NextTable, RSR.getEntrySubDir(Entry));
415  Error E =
416  addChildren(*Child, RSR, NextTable, Origin, Context, Duplicates);
417  if (E)
418  return E;
419  Context.pop_back();
420 
421  } else {
422 
423  // Data leaves are supposed to have a numeric ID as identifier (language).
424  if (Table.NumberOfNameEntries > 0)
426  "unexpected string key for data object");
427 
428  // Try adding a data leaf
429  UNWRAP_REF_OR_RETURN(DataEntry, RSR.getEntryData(Entry));
430  TreeNode *Child;
431  Context.push_back(StringOrID(Entry.Identifier.ID));
432  bool Added = Node.addDataChild(Entry.Identifier.ID, Table.MajorVersion,
433  Table.MinorVersion, Table.Characteristics,
434  Origin, Data.size(), Child);
435  if (Added) {
436  UNWRAP_OR_RETURN(Contents, RSR.getContents(DataEntry));
437  Data.push_back(ArrayRef<uint8_t>(
438  reinterpret_cast<const uint8_t *>(Contents.data()),
439  Contents.size()));
440  } else {
441  if (!shouldIgnoreDuplicate(Context))
442  Duplicates.push_back(makeDuplicateResourceError(
443  Context, InputFilenames[Child->Origin], InputFilenames.back()));
444  }
445  Context.pop_back();
446 
447  }
448  }
449  return Error::success();
450 }
451 
452 WindowsResourceParser::TreeNode::TreeNode(uint32_t StringIndex)
453  : StringIndex(StringIndex) {}
454 
455 WindowsResourceParser::TreeNode::TreeNode(uint16_t MajorVersion,
456  uint16_t MinorVersion,
458  uint32_t Origin, uint32_t DataIndex)
459  : IsDataNode(true), DataIndex(DataIndex), MajorVersion(MajorVersion),
460  MinorVersion(MinorVersion), Characteristics(Characteristics),
461  Origin(Origin) {}
462 
463 std::unique_ptr<WindowsResourceParser::TreeNode>
464 WindowsResourceParser::TreeNode::createStringNode(uint32_t Index) {
465  return std::unique_ptr<TreeNode>(new TreeNode(Index));
466 }
467 
468 std::unique_ptr<WindowsResourceParser::TreeNode>
469 WindowsResourceParser::TreeNode::createIDNode() {
470  return std::unique_ptr<TreeNode>(new TreeNode(0));
471 }
472 
473 std::unique_ptr<WindowsResourceParser::TreeNode>
474 WindowsResourceParser::TreeNode::createDataNode(uint16_t MajorVersion,
475  uint16_t MinorVersion,
477  uint32_t Origin,
478  uint32_t DataIndex) {
479  return std::unique_ptr<TreeNode>(new TreeNode(
480  MajorVersion, MinorVersion, Characteristics, Origin, DataIndex));
481 }
482 
483 WindowsResourceParser::TreeNode &WindowsResourceParser::TreeNode::addTypeNode(
484  const ResourceEntryRef &Entry,
485  std::vector<std::vector<UTF16>> &StringTable) {
486  if (Entry.checkTypeString())
487  return addNameChild(Entry.getTypeString(), StringTable);
488  else
489  return addIDChild(Entry.getTypeID());
490 }
491 
492 WindowsResourceParser::TreeNode &WindowsResourceParser::TreeNode::addNameNode(
493  const ResourceEntryRef &Entry,
494  std::vector<std::vector<UTF16>> &StringTable) {
495  if (Entry.checkNameString())
496  return addNameChild(Entry.getNameString(), StringTable);
497  else
498  return addIDChild(Entry.getNameID());
499 }
500 
501 bool WindowsResourceParser::TreeNode::addLanguageNode(
502  const ResourceEntryRef &Entry, uint32_t Origin,
503  std::vector<std::vector<uint8_t>> &Data, TreeNode *&Result) {
504  bool Added = addDataChild(Entry.getLanguage(), Entry.getMajorVersion(),
505  Entry.getMinorVersion(), Entry.getCharacteristics(),
506  Origin, Data.size(), Result);
507  if (Added)
508  Data.push_back(Entry.getData());
509  return Added;
510 }
511 
512 bool WindowsResourceParser::TreeNode::addDataChild(
513  uint32_t ID, uint16_t MajorVersion, uint16_t MinorVersion,
514  uint32_t Characteristics, uint32_t Origin, uint32_t DataIndex,
515  TreeNode *&Result) {
516  auto NewChild = createDataNode(MajorVersion, MinorVersion, Characteristics,
517  Origin, DataIndex);
518  auto ElementInserted = IDChildren.emplace(ID, std::move(NewChild));
519  Result = ElementInserted.first->second.get();
520  return ElementInserted.second;
521 }
522 
523 WindowsResourceParser::TreeNode &WindowsResourceParser::TreeNode::addIDChild(
524  uint32_t ID) {
525  auto Child = IDChildren.find(ID);
526  if (Child == IDChildren.end()) {
527  auto NewChild = createIDNode();
528  WindowsResourceParser::TreeNode &Node = *NewChild;
529  IDChildren.emplace(ID, std::move(NewChild));
530  return Node;
531  } else
532  return *(Child->second);
533 }
534 
535 WindowsResourceParser::TreeNode &WindowsResourceParser::TreeNode::addNameChild(
536  ArrayRef<UTF16> NameRef, std::vector<std::vector<UTF16>> &StringTable) {
537  std::string NameString;
538  convertUTF16LEToUTF8String(NameRef, NameString);
539 
540  auto Child = StringChildren.find(NameString);
541  if (Child == StringChildren.end()) {
542  auto NewChild = createStringNode(StringTable.size());
543  StringTable.push_back(NameRef);
544  WindowsResourceParser::TreeNode &Node = *NewChild;
545  StringChildren.emplace(NameString, std::move(NewChild));
546  return Node;
547  } else
548  return *(Child->second);
549 }
550 
552  StringRef Name) const {
553  ListScope NodeScope(Writer, Name);
554  for (auto const &Child : StringChildren) {
555  Child.second->print(Writer, Child.first);
556  }
557  for (auto const &Child : IDChildren) {
558  Child.second->print(Writer, to_string(Child.first));
559  }
560 }
561 
562 // This function returns the size of the entire resource tree, including
563 // directory tables, directory entries, and data entries. It does not include
564 // the directory strings or the relocations of the .rsrc section.
566  uint32_t Size = (IDChildren.size() + StringChildren.size()) *
567  sizeof(coff_resource_dir_entry);
568 
569  // Reached a node pointing to a data entry.
570  if (IsDataNode) {
571  Size += sizeof(coff_resource_data_entry);
572  return Size;
573  }
574 
575  // If the node does not point to data, it must have a directory table pointing
576  // to other nodes.
577  Size += sizeof(coff_resource_dir_table);
578 
579  for (auto const &Child : StringChildren) {
580  Size += Child.second->getTreeSize();
581  }
582  for (auto const &Child : IDChildren) {
583  Size += Child.second->getTreeSize();
584  }
585  return Size;
586 }
587 
588 // Shift DataIndex of all data children with an Index greater or equal to the
589 // given one, to fill a gap from removing an entry from the Data vector.
590 void WindowsResourceParser::TreeNode::shiftDataIndexDown(uint32_t Index) {
591  if (IsDataNode && DataIndex >= Index) {
592  DataIndex--;
593  } else {
594  for (auto &Child : IDChildren)
595  Child.second->shiftDataIndexDown(Index);
596  for (auto &Child : StringChildren)
597  Child.second->shiftDataIndexDown(Index);
598  }
599 }
600 
602 public:
605  std::unique_ptr<MemoryBuffer> write(uint32_t TimeDateStamp);
606 
607 private:
608  void performFileLayout();
609  void performSectionOneLayout();
610  void performSectionTwoLayout();
611  void writeCOFFHeader(uint32_t TimeDateStamp);
612  void writeFirstSectionHeader();
613  void writeSecondSectionHeader();
614  void writeFirstSection();
615  void writeSecondSection();
616  void writeSymbolTable();
617  void writeStringTable();
618  void writeDirectoryTree();
619  void writeDirectoryStringTable();
620  void writeFirstSectionRelocations();
621  std::unique_ptr<WritableMemoryBuffer> OutputBuffer;
622  char *BufferStart;
623  uint64_t CurrentOffset = 0;
624  COFF::MachineTypes MachineType;
625  const WindowsResourceParser::TreeNode &Resources;
627  uint64_t FileSize;
628  uint32_t SymbolTableOffset;
629  uint32_t SectionOneSize;
630  uint32_t SectionOneOffset;
631  uint32_t SectionOneRelocations;
632  uint32_t SectionTwoSize;
633  uint32_t SectionTwoOffset;
634  const ArrayRef<std::vector<UTF16>> StringTable;
635  std::vector<uint32_t> StringTableOffsets;
636  std::vector<uint32_t> DataOffsets;
637  std::vector<uint32_t> RelocationAddresses;
638 };
639 
641  COFF::MachineTypes MachineType, const WindowsResourceParser &Parser,
642  Error &E)
643  : MachineType(MachineType), Resources(Parser.getTree()),
644  Data(Parser.getData()), StringTable(Parser.getStringTable()) {
645  performFileLayout();
646 
648  FileSize, "internal .obj file created from .res files");
649 }
650 
651 void WindowsResourceCOFFWriter::performFileLayout() {
652  // Add size of COFF header.
653  FileSize = COFF::Header16Size;
654 
655  // one .rsrc section header for directory tree, another for resource data.
656  FileSize += 2 * COFF::SectionSize;
657 
658  performSectionOneLayout();
659  performSectionTwoLayout();
660 
661  // We have reached the address of the symbol table.
662  SymbolTableOffset = FileSize;
663 
664  FileSize += COFF::Symbol16Size; // size of the @feat.00 symbol.
665  FileSize += 4 * COFF::Symbol16Size; // symbol + aux for each section.
666  FileSize += Data.size() * COFF::Symbol16Size; // 1 symbol per resource.
667  FileSize += 4; // four null bytes for the string table.
668 }
669 
670 void WindowsResourceCOFFWriter::performSectionOneLayout() {
671  SectionOneOffset = FileSize;
672 
673  SectionOneSize = Resources.getTreeSize();
674  uint32_t CurrentStringOffset = SectionOneSize;
675  uint32_t TotalStringTableSize = 0;
676  for (auto const &String : StringTable) {
677  StringTableOffsets.push_back(CurrentStringOffset);
678  uint32_t StringSize = String.size() * sizeof(UTF16) + sizeof(uint16_t);
679  CurrentStringOffset += StringSize;
680  TotalStringTableSize += StringSize;
681  }
682  SectionOneSize += alignTo(TotalStringTableSize, sizeof(uint32_t));
683 
684  // account for the relocations of section one.
685  SectionOneRelocations = FileSize + SectionOneSize;
686  FileSize += SectionOneSize;
687  FileSize +=
688  Data.size() * COFF::RelocationSize; // one relocation for each resource.
689  FileSize = alignTo(FileSize, SECTION_ALIGNMENT);
690 }
691 
692 void WindowsResourceCOFFWriter::performSectionTwoLayout() {
693  // add size of .rsrc$2 section, which contains all resource data on 8-byte
694  // alignment.
695  SectionTwoOffset = FileSize;
696  SectionTwoSize = 0;
697  for (auto const &Entry : Data) {
698  DataOffsets.push_back(SectionTwoSize);
699  SectionTwoSize += alignTo(Entry.size(), sizeof(uint64_t));
700  }
701  FileSize += SectionTwoSize;
702  FileSize = alignTo(FileSize, SECTION_ALIGNMENT);
703 }
704 
705 std::unique_ptr<MemoryBuffer>
707  BufferStart = OutputBuffer->getBufferStart();
708 
709  writeCOFFHeader(TimeDateStamp);
710  writeFirstSectionHeader();
711  writeSecondSectionHeader();
712  writeFirstSection();
713  writeSecondSection();
714  writeSymbolTable();
715  writeStringTable();
716 
717  return std::move(OutputBuffer);
718 }
719 
720 // According to COFF specification, if the Src has a size equal to Dest,
721 // it's okay to *not* copy the trailing zero.
722 static void coffnamecpy(char (&Dest)[COFF::NameSize], StringRef Src) {
723  assert(Src.size() <= COFF::NameSize &&
724  "Src is larger than COFF::NameSize");
725  assert((Src.size() == COFF::NameSize || Dest[Src.size()] == '\0') &&
726  "Dest not zeroed upon initialization");
727  memcpy(Dest, Src.data(), Src.size());
728 }
729 
730 void WindowsResourceCOFFWriter::writeCOFFHeader(uint32_t TimeDateStamp) {
731  // Write the COFF header.
732  auto *Header = reinterpret_cast<coff_file_header *>(BufferStart);
733  Header->Machine = MachineType;
734  Header->NumberOfSections = 2;
735  Header->TimeDateStamp = TimeDateStamp;
736  Header->PointerToSymbolTable = SymbolTableOffset;
737  // One symbol for every resource plus 2 for each section and 1 for @feat.00
738  Header->NumberOfSymbols = Data.size() + 5;
739  Header->SizeOfOptionalHeader = 0;
740  // cvtres.exe sets 32BIT_MACHINE even for 64-bit machine types. Match it.
741  Header->Characteristics = COFF::IMAGE_FILE_32BIT_MACHINE;
742 }
743 
744 void WindowsResourceCOFFWriter::writeFirstSectionHeader() {
745  // Write the first section header.
746  CurrentOffset += sizeof(coff_file_header);
747  auto *SectionOneHeader =
748  reinterpret_cast<coff_section *>(BufferStart + CurrentOffset);
749  coffnamecpy(SectionOneHeader->Name, ".rsrc$01");
750  SectionOneHeader->VirtualSize = 0;
751  SectionOneHeader->VirtualAddress = 0;
752  SectionOneHeader->SizeOfRawData = SectionOneSize;
753  SectionOneHeader->PointerToRawData = SectionOneOffset;
754  SectionOneHeader->PointerToRelocations = SectionOneRelocations;
755  SectionOneHeader->PointerToLinenumbers = 0;
756  SectionOneHeader->NumberOfRelocations = Data.size();
757  SectionOneHeader->NumberOfLinenumbers = 0;
758  SectionOneHeader->Characteristics += COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
759  SectionOneHeader->Characteristics += COFF::IMAGE_SCN_MEM_READ;
760 }
761 
762 void WindowsResourceCOFFWriter::writeSecondSectionHeader() {
763  // Write the second section header.
764  CurrentOffset += sizeof(coff_section);
765  auto *SectionTwoHeader =
766  reinterpret_cast<coff_section *>(BufferStart + CurrentOffset);
767  coffnamecpy(SectionTwoHeader->Name, ".rsrc$02");
768  SectionTwoHeader->VirtualSize = 0;
769  SectionTwoHeader->VirtualAddress = 0;
770  SectionTwoHeader->SizeOfRawData = SectionTwoSize;
771  SectionTwoHeader->PointerToRawData = SectionTwoOffset;
772  SectionTwoHeader->PointerToRelocations = 0;
773  SectionTwoHeader->PointerToLinenumbers = 0;
774  SectionTwoHeader->NumberOfRelocations = 0;
775  SectionTwoHeader->NumberOfLinenumbers = 0;
776  SectionTwoHeader->Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
777  SectionTwoHeader->Characteristics += COFF::IMAGE_SCN_MEM_READ;
778 }
779 
780 void WindowsResourceCOFFWriter::writeFirstSection() {
781  // Write section one.
782  CurrentOffset += sizeof(coff_section);
783 
784  writeDirectoryTree();
785  writeDirectoryStringTable();
786  writeFirstSectionRelocations();
787 
788  CurrentOffset = alignTo(CurrentOffset, SECTION_ALIGNMENT);
789 }
790 
791 void WindowsResourceCOFFWriter::writeSecondSection() {
792  // Now write the .rsrc$02 section.
793  for (auto const &RawDataEntry : Data) {
794  llvm::copy(RawDataEntry, BufferStart + CurrentOffset);
795  CurrentOffset += alignTo(RawDataEntry.size(), sizeof(uint64_t));
796  }
797 
798  CurrentOffset = alignTo(CurrentOffset, SECTION_ALIGNMENT);
799 }
800 
801 void WindowsResourceCOFFWriter::writeSymbolTable() {
802  // Now write the symbol table.
803  // First, the feat symbol.
804  auto *Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset);
805  coffnamecpy(Symbol->Name.ShortName, "@feat.00");
806  Symbol->Value = 0x11;
807  Symbol->SectionNumber = 0xffff;
809  Symbol->StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
810  Symbol->NumberOfAuxSymbols = 0;
811  CurrentOffset += sizeof(coff_symbol16);
812 
813  // Now write the .rsrc1 symbol + aux.
814  Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset);
815  coffnamecpy(Symbol->Name.ShortName, ".rsrc$01");
816  Symbol->Value = 0;
817  Symbol->SectionNumber = 1;
819  Symbol->StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
820  Symbol->NumberOfAuxSymbols = 1;
821  CurrentOffset += sizeof(coff_symbol16);
822  auto *Aux = reinterpret_cast<coff_aux_section_definition *>(BufferStart +
823  CurrentOffset);
824  Aux->Length = SectionOneSize;
825  Aux->NumberOfRelocations = Data.size();
826  Aux->NumberOfLinenumbers = 0;
827  Aux->CheckSum = 0;
828  Aux->NumberLowPart = 0;
829  Aux->Selection = 0;
830  CurrentOffset += sizeof(coff_aux_section_definition);
831 
832  // Now write the .rsrc2 symbol + aux.
833  Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset);
834  coffnamecpy(Symbol->Name.ShortName, ".rsrc$02");
835  Symbol->Value = 0;
836  Symbol->SectionNumber = 2;
838  Symbol->StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
839  Symbol->NumberOfAuxSymbols = 1;
840  CurrentOffset += sizeof(coff_symbol16);
841  Aux = reinterpret_cast<coff_aux_section_definition *>(BufferStart +
842  CurrentOffset);
843  Aux->Length = SectionTwoSize;
844  Aux->NumberOfRelocations = 0;
845  Aux->NumberOfLinenumbers = 0;
846  Aux->CheckSum = 0;
847  Aux->NumberLowPart = 0;
848  Aux->Selection = 0;
849  CurrentOffset += sizeof(coff_aux_section_definition);
850 
851  // Now write a symbol for each relocation.
852  for (unsigned i = 0; i < Data.size(); i++) {
853  auto RelocationName = formatv("$R{0:X-6}", i & 0xffffff).sstr<COFF::NameSize>();
854  Symbol = reinterpret_cast<coff_symbol16 *>(BufferStart + CurrentOffset);
855  coffnamecpy(Symbol->Name.ShortName, RelocationName);
856  Symbol->Value = DataOffsets[i];
857  Symbol->SectionNumber = 2;
859  Symbol->StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
860  Symbol->NumberOfAuxSymbols = 0;
861  CurrentOffset += sizeof(coff_symbol16);
862  }
863 }
864 
865 void WindowsResourceCOFFWriter::writeStringTable() {
866  // Just 4 null bytes for the string table.
867  auto COFFStringTable = reinterpret_cast<void *>(BufferStart + CurrentOffset);
868  memset(COFFStringTable, 0, 4);
869 }
870 
871 void WindowsResourceCOFFWriter::writeDirectoryTree() {
872  // Traverse parsed resource tree breadth-first and write the corresponding
873  // COFF objects.
874  std::queue<const WindowsResourceParser::TreeNode *> Queue;
875  Queue.push(&Resources);
876  uint32_t NextLevelOffset =
877  sizeof(coff_resource_dir_table) + (Resources.getStringChildren().size() +
878  Resources.getIDChildren().size()) *
879  sizeof(coff_resource_dir_entry);
880  std::vector<const WindowsResourceParser::TreeNode *> DataEntriesTreeOrder;
881  uint32_t CurrentRelativeOffset = 0;
882 
883  while (!Queue.empty()) {
884  auto CurrentNode = Queue.front();
885  Queue.pop();
886  auto *Table = reinterpret_cast<coff_resource_dir_table *>(BufferStart +
887  CurrentOffset);
888  Table->Characteristics = CurrentNode->getCharacteristics();
889  Table->TimeDateStamp = 0;
890  Table->MajorVersion = CurrentNode->getMajorVersion();
891  Table->MinorVersion = CurrentNode->getMinorVersion();
892  auto &IDChildren = CurrentNode->getIDChildren();
893  auto &StringChildren = CurrentNode->getStringChildren();
894  Table->NumberOfNameEntries = StringChildren.size();
895  Table->NumberOfIDEntries = IDChildren.size();
896  CurrentOffset += sizeof(coff_resource_dir_table);
897  CurrentRelativeOffset += sizeof(coff_resource_dir_table);
898 
899  // Write the directory entries immediately following each directory table.
900  for (auto const &Child : StringChildren) {
901  auto *Entry = reinterpret_cast<coff_resource_dir_entry *>(BufferStart +
902  CurrentOffset);
903  Entry->Identifier.setNameOffset(
904  StringTableOffsets[Child.second->getStringIndex()]);
905  if (Child.second->checkIsDataNode()) {
906  Entry->Offset.DataEntryOffset = NextLevelOffset;
907  NextLevelOffset += sizeof(coff_resource_data_entry);
908  DataEntriesTreeOrder.push_back(Child.second.get());
909  } else {
910  Entry->Offset.SubdirOffset = NextLevelOffset + (1 << 31);
911  NextLevelOffset += sizeof(coff_resource_dir_table) +
912  (Child.second->getStringChildren().size() +
913  Child.second->getIDChildren().size()) *
914  sizeof(coff_resource_dir_entry);
915  Queue.push(Child.second.get());
916  }
917  CurrentOffset += sizeof(coff_resource_dir_entry);
918  CurrentRelativeOffset += sizeof(coff_resource_dir_entry);
919  }
920  for (auto const &Child : IDChildren) {
921  auto *Entry = reinterpret_cast<coff_resource_dir_entry *>(BufferStart +
922  CurrentOffset);
923  Entry->Identifier.ID = Child.first;
924  if (Child.second->checkIsDataNode()) {
925  Entry->Offset.DataEntryOffset = NextLevelOffset;
926  NextLevelOffset += sizeof(coff_resource_data_entry);
927  DataEntriesTreeOrder.push_back(Child.second.get());
928  } else {
929  Entry->Offset.SubdirOffset = NextLevelOffset + (1 << 31);
930  NextLevelOffset += sizeof(coff_resource_dir_table) +
931  (Child.second->getStringChildren().size() +
932  Child.second->getIDChildren().size()) *
933  sizeof(coff_resource_dir_entry);
934  Queue.push(Child.second.get());
935  }
936  CurrentOffset += sizeof(coff_resource_dir_entry);
937  CurrentRelativeOffset += sizeof(coff_resource_dir_entry);
938  }
939  }
940 
941  RelocationAddresses.resize(Data.size());
942  // Now write all the resource data entries.
943  for (auto DataNodes : DataEntriesTreeOrder) {
944  auto *Entry = reinterpret_cast<coff_resource_data_entry *>(BufferStart +
945  CurrentOffset);
946  RelocationAddresses[DataNodes->getDataIndex()] = CurrentRelativeOffset;
947  Entry->DataRVA = 0; // Set to zero because it is a relocation.
948  Entry->DataSize = Data[DataNodes->getDataIndex()].size();
949  Entry->Codepage = 0;
950  Entry->Reserved = 0;
951  CurrentOffset += sizeof(coff_resource_data_entry);
952  CurrentRelativeOffset += sizeof(coff_resource_data_entry);
953  }
954 }
955 
956 void WindowsResourceCOFFWriter::writeDirectoryStringTable() {
957  // Now write the directory string table for .rsrc$01
958  uint32_t TotalStringTableSize = 0;
959  for (auto &String : StringTable) {
960  uint16_t Length = String.size();
961  support::endian::write16le(BufferStart + CurrentOffset, Length);
962  CurrentOffset += sizeof(uint16_t);
963  auto *Start = reinterpret_cast<UTF16 *>(BufferStart + CurrentOffset);
964  llvm::copy(String, Start);
965  CurrentOffset += Length * sizeof(UTF16);
966  TotalStringTableSize += Length * sizeof(UTF16) + sizeof(uint16_t);
967  }
968  CurrentOffset +=
969  alignTo(TotalStringTableSize, sizeof(uint32_t)) - TotalStringTableSize;
970 }
971 
972 void WindowsResourceCOFFWriter::writeFirstSectionRelocations() {
973 
974  // Now write the relocations for .rsrc$01
975  // Five symbols already in table before we start, @feat.00 and 2 for each
976  // .rsrc section.
977  uint32_t NextSymbolIndex = 5;
978  for (unsigned i = 0; i < Data.size(); i++) {
979  auto *Reloc =
980  reinterpret_cast<coff_relocation *>(BufferStart + CurrentOffset);
981  Reloc->VirtualAddress = RelocationAddresses[i];
982  Reloc->SymbolTableIndex = NextSymbolIndex++;
983  switch (MachineType) {
985  Reloc->Type = COFF::IMAGE_REL_ARM_ADDR32NB;
986  break;
988  Reloc->Type = COFF::IMAGE_REL_AMD64_ADDR32NB;
989  break;
991  Reloc->Type = COFF::IMAGE_REL_I386_DIR32NB;
992  break;
994  Reloc->Type = COFF::IMAGE_REL_ARM64_ADDR32NB;
995  break;
996  default:
997  llvm_unreachable("unknown machine type");
998  }
999  CurrentOffset += sizeof(coff_relocation);
1000  }
1001 }
1002 
1006  uint32_t TimeDateStamp) {
1007  Error E = Error::success();
1008  WindowsResourceCOFFWriter Writer(MachineType, Parser, E);
1009  if (E)
1010  return std::move(E);
1011  return Writer.write(TimeDateStamp);
1012 }
1013 
1014 } // namespace object
1015 } // namespace llvm
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::object::WindowsResourceCOFFWriter::write
std::unique_ptr< MemoryBuffer > write(uint32_t TimeDateStamp)
Definition: WindowsResource.cpp:706
i
i
Definition: README.txt:29
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
llvm::BinaryStreamReader::setOffset
void setOffset(uint64_t Off)
Definition: BinaryStreamReader.h:254
llvm::object::WindowsResourceParser::printTree
void printTree(raw_ostream &OS) const
Definition: WindowsResource.cpp:376
MathExtras.h
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::object::SECTION_ALIGNMENT
const uint32_t SECTION_ALIGNMENT
Definition: WindowsResource.cpp:49
llvm::sys::IsBigEndianHost
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:98
llvm::COFF::IMAGE_REL_ARM64_ADDR32NB
@ IMAGE_REL_ARM64_ADDR32NB
Definition: COFF.h:388
llvm::COFF::IMAGE_SYM_CLASS_STATIC
@ IMAGE_SYM_CLASS_STATIC
Static.
Definition: COFF.h:210
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:164
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:625
llvm::write
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs)
Definition: DWP.cpp:535
llvm::object::WindowsResourceParser::TreeNode
Definition: WindowsResource.h:166
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::COFF::IMAGE_FILE_MACHINE_AMD64
@ IMAGE_FILE_MACHINE_AMD64
Definition: COFF.h:98
llvm::object::WinResHeaderPrefix
Definition: WindowsResource.h:60
llvm::COFF::NameSize
@ NameSize
Definition: COFF.h:58
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::COFF::IMAGE_REL_I386_DIR32NB
@ IMAGE_REL_I386_DIR32NB
Definition: COFF.h:336
llvm::object::WindowsResourceParser::TreeNode::getStringChildren
const Children< std::string > & getStringChildren() const
Definition: WindowsResource.h:180
llvm::copy
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1602
llvm::BinaryStreamReader::bytesRemaining
uint64_t bytesRemaining() const
Definition: BinaryStreamReader.h:257
llvm::COFF::IMAGE_FILE_MACHINE_ARMNT
@ IMAGE_FILE_MACHINE_ARMNT
Definition: COFF.h:100
llvm::support::endian::write16le
void write16le(void *P, uint16_t V)
Definition: Endian.h:415
FileOutputBuffer.h
llvm::object::WindowsResourceParser::TreeNode::getIDChildren
const Children< uint32_t > & getIDChildren() const
Definition: WindowsResource.h:179
llvm::COFF::Header16Size
@ Header16Size
Definition: COFF.h:56
llvm::X86ISD::BSR
@ BSR
Bit scan reverse.
Definition: X86ISelLowering.h:33
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:116
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
UNWRAP_OR_RETURN
#define UNWRAP_OR_RETURN(Name, Expr)
Definition: WindowsResource.cpp:39
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1035
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::object::WindowsResourceParser
Definition: WindowsResource.h:153
llvm::AMDGPU::HSAMD::ValueKind::Queue
@ Queue
WindowsResource.h
llvm::COFF::Symbol16Size
@ Symbol16Size
Definition: COFF.h:59
llvm::object::printStringOrID
static void printStringOrID(const WindowsResourceParser::StringOrID &S, raw_string_ostream &OS, bool IsType, bool IsID)
Definition: WindowsResource.cpp:213
llvm::object::WindowsResource
Definition: WindowsResource.h:136
llvm::object::Parser
Definition: COFFModuleDefinition.cpp:146
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::formatv
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Definition: FormatVariadic.h:250
llvm::COFF::IMAGE_FILE_MACHINE_I386
@ IMAGE_FILE_MACHINE_I386
Definition: COFF.h:103
llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
@ IMAGE_SCN_CNT_INITIALIZED_DATA
Definition: COFF.h:289
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::support::little
@ little
Definition: Endian.h:27
llvm::object::MIN_HEADER_SIZE
const uint32_t MIN_HEADER_SIZE
Definition: WindowsResource.cpp:45
llvm::BinaryStreamReader::getOffset
uint64_t getOffset() const
Definition: BinaryStreamReader.h:255
llvm::BinaryByteStream
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer.
Definition: BinaryByteStream.h:31
llvm::object::coff_resource_data_entry
Definition: COFF.h:777
llvm::COFF::IMAGE_SCN_MEM_READ
@ IMAGE_SCN_MEM_READ
Definition: COFF.h:321
llvm::object::convertUTF16LEToUTF8String
static bool convertUTF16LEToUTF8String(ArrayRef< UTF16 > Src, std::string &Out)
Definition: WindowsResource.cpp:170
llvm::object::WinResHeaderSuffix
Definition: WindowsResource.h:84
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
getFileName
static Expected< StringRef > getFileName(const DebugStringTableSubsectionRef &Strings, const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID)
Definition: CodeViewYAMLDebugSections.cpp:555
llvm::DelimitedScope
Definition: ScopedPrinter.h:353
false
Definition: StackSlotColoring.cpp:142
UNI_UTF16_BYTE_ORDER_MARK_SWAPPED
#define UNI_UTF16_BYTE_ORDER_MARK_SWAPPED
Definition: ConvertUTF.h:124
llvm::object::WIN_RES_DATA_ALIGNMENT
const uint32_t WIN_RES_DATA_ALIGNMENT
Definition: WindowsResource.h:57
llvm::COFF::IMAGE_REL_ARM_ADDR32NB
@ IMAGE_REL_ARM_ADDR32NB
Definition: COFF.h:368
llvm::BinaryStreamReader::readInteger
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream's offset.
Definition: BinaryStreamReader.h:75
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::writeStringTable
static void writeStringTable(std::vector< uint8_t > &B, ArrayRef< const std::string > Strings)
Definition: COFFImportFile.cpp:66
llvm::object::object_error::parse_failed
@ parse_failed
llvm::WritableMemoryBuffer::getNewMemBuffer
static std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
Definition: MemoryBuffer.cpp:315
llvm::convertUTF16ToUTF8String
bool convertUTF16ToUTF8String(ArrayRef< char > SrcBytes, std::string &Out)
Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
Definition: ConvertUTFWrapper.cpp:86
llvm::object::coff_symbol16
coff_symbol< support::ulittle16_t > coff_symbol16
Definition: COFF.h:265
FormatVariadic.h
llvm::BinaryStreamReader::padToAlignment
Error padToAlignment(uint32_t Align)
Definition: BinaryStreamReader.cpp:155
llvm::object::coff_resource_dir_entry
Definition: COFF.h:754
llvm::BinaryStreamReader
Provides read only access to a subclass of BinaryStream.
Definition: BinaryStreamReader.h:31
TypeID
Type::TypeID TypeID
Definition: Mips16HardFloat.cpp:102
llvm::COFF::IMAGE_FILE_32BIT_MACHINE
@ IMAGE_FILE_32BIT_MACHINE
Machine is based on a 32bit word architecture.
Definition: COFF.h:145
writeSymbolTable
static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, ArrayRef< MemberData > Members, StringRef StringTable)
Definition: ArchiveWriter.cpp:325
llvm::object::makeDuplicateResourceError
static std::string makeDuplicateResourceError(const ResourceEntryRef &Entry, StringRef File1, StringRef File2)
Definition: WindowsResource.cpp:181
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
uint64_t
llvm::object::WIN_RES_NULL_ENTRY_SIZE
const size_t WIN_RES_NULL_ENTRY_SIZE
Definition: WindowsResource.h:55
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::object::WindowsResourceParser::getStringTable
ArrayRef< std::vector< UTF16 > > getStringTable() const
Definition: WindowsResource.h:164
llvm::object::ResourceSectionRef::getBaseTable
Expected< const coff_resource_dir_table & > getBaseTable()
Definition: COFFObjectFile.cpp:1761
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::WindowsResourceCOFFWriter
Definition: WindowsResource.cpp:601
RETURN_IF_ERROR
#define RETURN_IF_ERROR(X)
Definition: WindowsResource.cpp:29
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::object::WindowsResourceParser::cleanUpManifests
void cleanUpManifests(std::vector< std::string > &Duplicates)
Definition: WindowsResource.cpp:263
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
llvm::object::EmptyResError
Definition: WindowsResource.h:92
llvm::BinaryStreamReader::readObject
Error readObject(const T *&Dest)
Get a pointer to an object of type T from the underlying stream, as if by memcpy, and store the resul...
Definition: BinaryStreamReader.h:169
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:100
llvm::ArrayRef< UTF16 >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::Binary
Definition: Binary.h:32
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
uint32_t
llvm::COFF::SectionSize
@ SectionSize
Definition: COFF.h:61
llvm::object::WindowsResourceParser::TreeNode::print
void print(ScopedPrinter &Writer, StringRef Name) const
Definition: WindowsResource.cpp:551
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::object::printResourceTypeName
void printResourceTypeName(uint16_t TypeID, raw_ostream &OS)
Definition: WindowsResource.cpp:143
llvm::object::readStringOrId
static Error readStringOrId(BinaryStreamReader &Reader, uint16_t &ID, ArrayRef< UTF16 > &Str, bool &IsString)
Definition: WindowsResource.cpp:99
llvm::COFF::IMAGE_FILE_MACHINE_ARM64
@ IMAGE_FILE_MACHINE_ARM64
Definition: COFF.h:101
llvm::BinaryStreamReader::readWideString
Error readWideString(ArrayRef< UTF16 > &Dest)
Similar to readCString, however read a null-terminated UTF16 string instead.
Definition: BinaryStreamReader.cpp:102
llvm::object::WindowsResourceParser::getData
ArrayRef< std::vector< uint8_t > > getData() const
Definition: WindowsResource.h:163
llvm::object::WindowsResourceParser::StringOrID
Definition: WindowsResource.h:234
llvm::COFF::RelocationSize
@ RelocationSize
Definition: COFF.h:62
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1231
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::object::coffnamecpy
static void coffnamecpy(char(&Dest)[COFF::NameSize], StringRef Src)
Definition: WindowsResource.cpp:722
llvm::COFF::IMAGE_REL_AMD64_ADDR32NB
@ IMAGE_REL_AMD64_ADDR32NB
Definition: COFF.h:349
llvm::ScopedPrinter
Definition: ScopedPrinter.h:70
llvm::object::WIN_RES_HEADER_ALIGNMENT
const uint32_t WIN_RES_HEADER_ALIGNMENT
Definition: WindowsResource.h:56
llvm::COFF::MachineTypes
MachineTypes
Definition: COFF.h:93
llvm::object::ResourceSectionRef
Definition: COFF.h:1239
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:476
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
UNWRAP_REF_OR_RETURN
#define UNWRAP_REF_OR_RETURN(Name, Expr)
Definition: WindowsResource.cpp:33
llvm::object::Binary::getFileName
StringRef getFileName() const
Definition: Binary.cpp:42
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
llvm::COFF::IMAGE_SYM_DTYPE_NULL
@ IMAGE_SYM_DTYPE_NULL
No complex type; simple scalar variable.
Definition: COFF.h:259
Characteristics
COFFYAML::WeakExternalCharacteristics Characteristics
Definition: COFFYAML.cpp:326
llvm::to_string
std::string to_string(const T &Value)
Definition: ScopedPrinter.h:63
llvm::object::coff_resource_dir_table
Definition: COFF.h:784
llvm::Type::TypeID
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
llvm::pdb::String
@ String
Definition: PDBTypes.h:407
llvm::UTF16
unsigned short UTF16
Definition: ConvertUTF.h:110
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1815
llvm::raw_string_ostream::str
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:643
llvm::object::WindowsResourceCOFFWriter::WindowsResourceCOFFWriter
WindowsResourceCOFFWriter(COFF::MachineTypes MachineType, const WindowsResourceParser &Parser, Error &E)
Definition: WindowsResource.cpp:640
llvm::UTF8
unsigned char UTF8
Definition: ConvertUTF.h:111
llvm::BinaryStreamReader::readArray
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
Definition: BinaryStreamReader.h:187
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
ScopedPrinter.h
llvm::object::ResourceEntryRef::moveNext
Error moveNext(bool &End)
Definition: WindowsResource.cpp:88