Skip to content

Commit 602afcf

Browse files
author
Eric Beckmann
committed
Revert "Revert "Replace trivial use of external rc.exe by writing our own .res file.""
Summary: This reverts commit 5193107. This revert was originally done because the integrations of the new WindowsResource library into LLD was causing error in chromium, due to bugs in how resource sections were handled. These bugs were fixed, meaning that the features may be reintegrated. Subscribers: hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D34922 llvm-svn: 306941
1 parent d29d3da commit 602afcf

File tree

15 files changed

+159
-128
lines changed

15 files changed

+159
-128
lines changed

lld/COFF/DriverUtils.cpp

Lines changed: 73 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
#include "Symbols.h"
2121
#include "llvm/ADT/Optional.h"
2222
#include "llvm/ADT/StringSwitch.h"
23+
#include "llvm/BinaryFormat/COFF.h"
2324
#include "llvm/Object/COFF.h"
25+
#include "llvm/Object/WindowsResource.h"
2426
#include "llvm/Option/Arg.h"
2527
#include "llvm/Option/ArgList.h"
2628
#include "llvm/Option/Option.h"
2729
#include "llvm/Support/CommandLine.h"
2830
#include "llvm/Support/FileUtilities.h"
31+
#include "llvm/Support/MathExtras.h"
2932
#include "llvm/Support/Process.h"
3033
#include "llvm/Support/Program.h"
3134
#include "llvm/Support/raw_ostream.h"
@@ -41,6 +44,9 @@ namespace lld {
4144
namespace coff {
4245
namespace {
4346

47+
const uint16_t SUBLANG_ENGLISH_US = 0x0409;
48+
const uint16_t RT_MANIFEST = 24;
49+
4450
class Executor {
4551
public:
4652
explicit Executor(StringRef S) : Prog(Saver.save(S)) {}
@@ -257,26 +263,6 @@ void parseManifestUAC(StringRef Arg) {
257263
}
258264
}
259265

260-
// Quote each line with "". Existing double-quote is converted
261-
// to two double-quotes.
262-
static void quoteAndPrint(raw_ostream &Out, StringRef S) {
263-
while (!S.empty()) {
264-
StringRef Line;
265-
std::tie(Line, S) = S.split("\n");
266-
if (Line.empty())
267-
continue;
268-
Out << '\"';
269-
for (int I = 0, E = Line.size(); I != E; ++I) {
270-
if (Line[I] == '\"') {
271-
Out << "\"\"";
272-
} else {
273-
Out << Line[I];
274-
}
275-
}
276-
Out << "\"\n";
277-
}
278-
}
279-
280266
// An RAII temporary file class that automatically removes a temporary file.
281267
namespace {
282268
class TemporaryFile {
@@ -390,38 +376,64 @@ static std::string createManifestXml() {
390376
return readFile(File2.Path);
391377
}
392378

379+
static std::unique_ptr<MemoryBuffer>
380+
createMemoryBufferForManifestRes(size_t ManifestSize) {
381+
size_t ResSize = alignTo(object::WIN_RES_MAGIC_SIZE +
382+
object::WIN_RES_NULL_ENTRY_SIZE +
383+
sizeof(object::WinResHeaderPrefix) +
384+
sizeof(object::WinResIDs) +
385+
sizeof(object::WinResHeaderSuffix) +
386+
ManifestSize,
387+
object::WIN_RES_DATA_ALIGNMENT);
388+
return MemoryBuffer::getNewMemBuffer(ResSize);
389+
}
390+
391+
static void writeResFileHeader(char *&Buf) {
392+
memcpy(Buf, COFF::WinResMagic, sizeof(COFF::WinResMagic));
393+
Buf += sizeof(COFF::WinResMagic);
394+
memset(Buf, 0, object::WIN_RES_NULL_ENTRY_SIZE);
395+
Buf += object::WIN_RES_NULL_ENTRY_SIZE;
396+
}
397+
398+
static void writeResEntryHeader(char *&Buf, size_t ManifestSize) {
399+
// Write the prefix.
400+
auto *Prefix = reinterpret_cast<object::WinResHeaderPrefix *>(Buf);
401+
Prefix->DataSize = ManifestSize;
402+
Prefix->HeaderSize = sizeof(object::WinResHeaderPrefix) +
403+
sizeof(object::WinResIDs) +
404+
sizeof(object::WinResHeaderSuffix);
405+
Buf += sizeof(object::WinResHeaderPrefix);
406+
407+
// Write the Type/Name IDs.
408+
auto *IDs = reinterpret_cast<object::WinResIDs *>(Buf);
409+
IDs->setType(RT_MANIFEST);
410+
IDs->setName(Config->ManifestID);
411+
Buf += sizeof(object::WinResIDs);
412+
413+
// Write the suffix.
414+
auto *Suffix = reinterpret_cast<object::WinResHeaderSuffix *>(Buf);
415+
Suffix->DataVersion = 0;
416+
Suffix->MemoryFlags = object::WIN_RES_PURE_MOVEABLE;
417+
Suffix->Language = SUBLANG_ENGLISH_US;
418+
Suffix->Version = 0;
419+
Suffix->Characteristics = 0;
420+
Buf += sizeof(object::WinResHeaderSuffix);
421+
}
422+
393423
// Create a resource file containing a manifest XML.
394424
std::unique_ptr<MemoryBuffer> createManifestRes() {
395-
// Create a temporary file for the resource script file.
396-
TemporaryFile RCFile("manifest", "rc");
425+
std::string Manifest = createManifestXml();
397426

398-
// Open the temporary file for writing.
399-
std::error_code EC;
400-
raw_fd_ostream Out(RCFile.Path, EC, sys::fs::F_Text);
401-
if (EC)
402-
fatal(EC, "failed to open " + RCFile.Path);
403-
404-
// Write resource script to the RC file.
405-
Out << "#define LANG_ENGLISH 9\n"
406-
<< "#define SUBLANG_DEFAULT 1\n"
407-
<< "#define APP_MANIFEST " << Config->ManifestID << "\n"
408-
<< "#define RT_MANIFEST 24\n"
409-
<< "LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT\n"
410-
<< "APP_MANIFEST RT_MANIFEST {\n";
411-
quoteAndPrint(Out, createManifestXml());
412-
Out << "}\n";
413-
Out.close();
414-
415-
// Create output resource file.
416-
TemporaryFile ResFile("output-resource", "res");
417-
418-
Executor E("rc.exe");
419-
E.add("/fo");
420-
E.add(ResFile.Path);
421-
E.add("/nologo");
422-
E.add(RCFile.Path);
423-
E.run();
424-
return ResFile.getMemoryBuffer();
427+
std::unique_ptr<MemoryBuffer> Res =
428+
createMemoryBufferForManifestRes(Manifest.size());
429+
430+
char *Buf = const_cast<char *>(Res->getBufferStart());
431+
writeResFileHeader(Buf);
432+
writeResEntryHeader(Buf, Manifest.size());
433+
434+
// Copy the manifest data into the .res file.
435+
std::copy(Manifest.begin(), Manifest.end(), Buf);
436+
return Res;
425437
}
426438

427439
void createSideBySideManifest() {
@@ -592,40 +604,22 @@ void checkFailIfMismatch(StringRef Arg) {
592604
// using cvtres.exe.
593605
std::unique_ptr<MemoryBuffer>
594606
convertResToCOFF(const std::vector<MemoryBufferRef> &MBs) {
595-
// Create an output file path.
596-
TemporaryFile File("resource-file", "obj");
607+
object::WindowsResourceParser Parser;
597608

598-
// Execute cvtres.exe.
599-
Executor E("cvtres.exe");
600-
E.add("/machine:" + machineToStr(Config->Machine));
601-
E.add("/readonly");
602-
E.add("/nologo");
603-
E.add("/out:" + Twine(File.Path));
604-
605-
// We must create new files because the memory buffers we have may have no
606-
// underlying file still existing on the disk.
607-
// It happens if it was created from a TemporaryFile, which usually delete
608-
// the file just after creating the MemoryBuffer.
609-
std::vector<TemporaryFile> ResFiles;
610-
ResFiles.reserve(MBs.size());
611609
for (MemoryBufferRef MB : MBs) {
612-
// We store the temporary file in a vector to avoid deletion
613-
// before running cvtres
614-
ResFiles.emplace_back("resource-file", "res");
615-
TemporaryFile& ResFile = ResFiles.back();
616-
// Write the content of the resource in a temporary file
617-
std::error_code EC;
618-
raw_fd_ostream OS(ResFile.Path, EC, sys::fs::F_None);
619-
if (EC)
620-
fatal(EC, "failed to open " + ResFile.Path);
621-
OS << MB.getBuffer();
622-
OS.close();
623-
624-
E.add(ResFile.Path);
610+
std::unique_ptr<object::Binary> Bin = check(object::createBinary(MB));
611+
object::WindowsResource *RF = dyn_cast<object::WindowsResource>(Bin.get());
612+
if (!RF)
613+
fatal("cannot compile non-resource file as resource");
614+
if (auto EC = Parser.parse(RF))
615+
fatal(EC, "failed to parse .res file");
625616
}
626617

627-
E.run();
628-
return File.getMemoryBuffer();
618+
Expected<std::unique_ptr<MemoryBuffer>> E =
619+
llvm::object::writeWindowsResourceCOFF(Config->Machine, Parser);
620+
if (!E)
621+
fatal(errorToErrorCode(E.takeError()), "failed to write .res to COFF");
622+
return std::move(E.get());
629623
}
630624

631625
// Run MSVC link.exe for given in-memory object files.

lld/test/COFF/combined-resources.test

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
// > rc /fo combined-resources.res /nologo combined-resources.rc
55
// > rc /fo combined-resources-2.res /nologo combined-resources-2.rc
66

7-
# REQUIRES: winres
8-
97
# RUN: yaml2obj < %p/Inputs/ret42.yaml > %t.obj
108
# RUN: lld-link /out:%t.exe /entry:main %t.obj %p/Inputs/resource.res \
119
# RUN: %p/Inputs/combined-resources.res %p/Inputs/combined-resources-2.res

lld/test/COFF/def-name.test

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# REQUIRES: winres
2-
31
# RUN: rm -rf %t
42
# RUN: mkdir -p %t
53
# RUN: cd %t

lld/test/COFF/dll.test

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# REQUIRES: winres
2-
31
# RUN: yaml2obj < %p/Inputs/export.yaml > %t.obj
42
# RUN: lld-link /out:%t.dll /dll %t.obj /export:exportfn1 /export:exportfn2 \
53
# RUN: /export:mangled

lld/test/COFF/dllimport-gc.test

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# REQUIRES: winres
2-
31
# RUN: yaml2obj < %p/Inputs/export.yaml > %t-lib.obj
42
# RUN: lld-link /out:%t.dll /dll %t-lib.obj /implib:%t.lib /export:exportfn1
53

lld/test/COFF/manifestinput.test

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# REQUIRES: winres
1+
# REQUIRES: win_mt
22

33
# RUN: yaml2obj %p/Inputs/ret42.yaml > %t.obj
44
# RUN: lld-link /out:%t.exe /entry:main \
@@ -8,3 +8,28 @@
88

99
CHECK: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
1010
CHECK: <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"><dependency><dependentAssembly><assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"></assemblyIdentity></dependentAssembly></dependency><trustInfo><security><requestedPrivileges><requestedExecutionLevel level="requireAdministrator" uiAccess="false"></requestedExecutionLevel></requestedPrivileges></security></trustInfo></assembly>
11+
12+
# RUN: yaml2obj %p/Inputs/ret42.yaml > %t.obj
13+
# RUN: lld-link /out:%t.exe /entry:main \
14+
# RUN: /manifest:embed \
15+
# RUN: /manifestuac:"level='requireAdministrator'" \
16+
# RUN: /manifestinput:%p/Inputs/manifestinput.test %t.obj
17+
# RUN: llvm-readobj -coff-resources -file-headers %t.exe | FileCheck %s \
18+
# RUN: -check-prefix TEST_EMBED
19+
20+
TEST_EMBED: ResourceTableRVA: 0x1000
21+
TEST_EMBED-NEXT: ResourceTableSize: 0x298
22+
TEST_EMBED-DAG: Resources [
23+
TEST_EMBED-NEXT: Total Number of Resources: 1
24+
TEST_EMBED-DAG: Number of String Entries: 0
25+
TEST_EMBED-NEXT: Number of ID Entries: 1
26+
TEST_EMBED-NEXT: Type: kRT_MANIFEST (ID 24) [
27+
TEST_EMBED-NEXT: Table Offset: 0x18
28+
TEST_EMBED-NEXT: Number of String Entries: 0
29+
TEST_EMBED-NEXT: Number of ID Entries: 1
30+
TEST_EMBED-NEXT: Name: (ID 1) [
31+
TEST_EMBED-NEXT: Table Offset: 0x30
32+
TEST_EMBED-NEXT: Number of String Entries: 0
33+
TEST_EMBED-NEXT: Number of ID Entries: 1
34+
TEST_EMBED-NEXT: Language: (ID 1033) [
35+
TEST_EMBED-NEXT: Entry Offset: 0x48

lld/test/COFF/noentry.test

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# REQUIRES: winres
2-
31
# RUN: yaml2obj < %p/Inputs/export.yaml > %t.obj
42
# RUN: lld-link /out:%t.dll /dll %t.obj
53
# RUN: llvm-readobj -file-headers %t.dll | FileCheck -check-prefix=ENTRY %s

lld/test/COFF/out.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# REQUIRES: winres
21
# RUN: yaml2obj < %p/Inputs/ret42.yaml > %t.obj
32

43
# RUN: mkdir -p %T/out/tmp

lld/test/COFF/resource.test

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# REQUIRES: winres
2-
31
# RUN: yaml2obj < %p/Inputs/ret42.yaml > %t.obj
42
# RUN: lld-link /out:%t.exe /entry:main %t.obj %p/Inputs/resource.res
53

lld/test/lit.cfg

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,7 @@ llvm_config_cmd.wait()
264264
# Set a fake constant version so that we get consitent output.
265265
config.environment['LLD_VERSION'] = 'LLD 1.0'
266266

267-
# Check if Windows resource file compiler exists.
268-
cvtres = lit.util.which('cvtres', config.environment['PATH'])
269-
rc = lit.util.which('rc', config.environment['PATH'])
270-
if cvtres and rc:
271-
config.available_features.add('winres')
267+
# Indirectly check if the mt.exe Microsoft utility exists by searching for
268+
# cvtres, which always accompanies it.
269+
if lit.util.which('cvtres', config.environment['PATH']):
270+
config.available_features.add('win_mt')

llvm/include/llvm/BinaryFormat/COFF.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ static const char ClGlObjMagic[] = {
4646
'\xac', '\x9b', '\xd6', '\xb6', '\x22', '\x26', '\x53', '\xc2',
4747
};
4848

49+
// The signature bytes that start a .res file.
50+
static const char WinResMagic[] = {
51+
'\x00', '\x00', '\x00', '\x00', '\x20', '\x00', '\x00', '\x00',
52+
'\xff', '\xff', '\x00', '\x00', '\xff', '\xff', '\x00', '\x00',
53+
};
54+
4955
// Sizes in bytes of various things in the COFF format.
5056
enum {
5157
Header16Size = 20,

llvm/include/llvm/Object/WindowsResource.h

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,47 @@
4343
#include <map>
4444

4545
namespace llvm {
46-
4746
namespace object {
4847

4948
class WindowsResource;
5049

51-
enum class Machine { UNKNOWN, ARM, X64, X86 };
50+
const size_t WIN_RES_MAGIC_SIZE = 16;
51+
const size_t WIN_RES_NULL_ENTRY_SIZE = 16;
52+
const uint32_t WIN_RES_HEADER_ALIGNMENT = 4;
53+
const uint32_t WIN_RES_DATA_ALIGNMENT = 4;
54+
const uint16_t WIN_RES_PURE_MOVEABLE = 0x0030;
55+
56+
struct WinResHeaderPrefix {
57+
support::ulittle32_t DataSize;
58+
support::ulittle32_t HeaderSize;
59+
};
60+
61+
// Type and Name may each either be an integer ID or a string. This struct is
62+
// only used in the case where they are both IDs.
63+
struct WinResIDs {
64+
uint16_t TypeFlag;
65+
support::ulittle16_t TypeID;
66+
uint16_t NameFlag;
67+
support::ulittle16_t NameID;
68+
69+
void setType(uint16_t ID) {
70+
TypeFlag = 0xffff;
71+
TypeID = ID;
72+
}
73+
74+
void setName(uint16_t ID) {
75+
NameFlag = 0xffff;
76+
NameID = ID;
77+
}
78+
};
79+
80+
struct WinResHeaderSuffix {
81+
support::ulittle32_t DataVersion;
82+
support::ulittle16_t MemoryFlags;
83+
support::ulittle16_t Language;
84+
support::ulittle32_t Version;
85+
support::ulittle32_t Characteristics;
86+
};
5287

5388
class ResourceEntryRef {
5489
public:
@@ -73,22 +108,14 @@ class ResourceEntryRef {
73108

74109
Error loadNext();
75110

76-
struct HeaderSuffix {
77-
support::ulittle32_t DataVersion;
78-
support::ulittle16_t MemoryFlags;
79-
support::ulittle16_t Language;
80-
support::ulittle32_t Version;
81-
support::ulittle32_t Characteristics;
82-
};
83-
84111
BinaryStreamReader Reader;
85112
bool IsStringType;
86113
ArrayRef<UTF16> Type;
87114
uint16_t TypeID;
88115
bool IsStringName;
89116
ArrayRef<UTF16> Name;
90117
uint16_t NameID;
91-
const HeaderSuffix *Suffix = nullptr;
118+
const WinResHeaderSuffix *Suffix = nullptr;
92119
ArrayRef<uint8_t> Data;
93120
const WindowsResource *OwningRes = nullptr;
94121
};

llvm/lib/BinaryFormat/Magic.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ file_magic llvm::identify_magic(StringRef Magic) {
5151
return file_magic::coff_import_library;
5252
}
5353
// Windows resource file
54-
if (startswith(Magic, "\0\0\0\0\x20\0\0\0\xFF"))
54+
if (Magic.size() >= sizeof(COFF::WinResMagic) &&
55+
memcmp(Magic.data(), COFF::WinResMagic, sizeof(COFF::WinResMagic)) == 0)
5556
return file_magic::windows_resource;
5657
// 0x0000 = COFF unknown machine type
5758
if (Magic[1] == 0)

0 commit comments

Comments
 (0)