Skip to content

Commit d40dd64

Browse files
author
Eric Beckmann
committed
Revert "Replace trivial use of external rc.exe by writing our own .res file."
This reverts commit d4c7e9fc63c10dbab0c30186ef8575474a704496. This is done in order to address the failure of CrWinClangLLD etc. bots. These throw an error of "side-by-side configuration is incorrect" during compilation, which sounds suspiciously related to these manifest changes. Revert "Switch external cvtres.exe for llvm's own resource library." This reverts commit 71fe8ef283a9dab9a3f21432c98466cbc23990d1. llvm-svn: 306618
1 parent 798a19a commit d40dd64

File tree

15 files changed

+128
-159
lines changed

15 files changed

+128
-159
lines changed

lld/COFF/DriverUtils.cpp

Lines changed: 79 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@
2020
#include "Symbols.h"
2121
#include "llvm/ADT/Optional.h"
2222
#include "llvm/ADT/StringSwitch.h"
23-
#include "llvm/BinaryFormat/COFF.h"
2423
#include "llvm/Object/COFF.h"
25-
#include "llvm/Object/WindowsResource.h"
2624
#include "llvm/Option/Arg.h"
2725
#include "llvm/Option/ArgList.h"
2826
#include "llvm/Option/Option.h"
2927
#include "llvm/Support/CommandLine.h"
3028
#include "llvm/Support/FileUtilities.h"
31-
#include "llvm/Support/MathExtras.h"
3229
#include "llvm/Support/Process.h"
3330
#include "llvm/Support/Program.h"
3431
#include "llvm/Support/raw_ostream.h"
@@ -44,9 +41,6 @@ namespace lld {
4441
namespace coff {
4542
namespace {
4643

47-
const uint16_t SUBLANG_ENGLISH_US = 0x0409;
48-
const uint16_t RT_MANIFEST = 24;
49-
5044
class Executor {
5145
public:
5246
explicit Executor(StringRef S) : Prog(Saver.save(S)) {}
@@ -263,6 +257,26 @@ void parseManifestUAC(StringRef Arg) {
263257
}
264258
}
265259

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+
266280
// An RAII temporary file class that automatically removes a temporary file.
267281
namespace {
268282
class TemporaryFile {
@@ -376,64 +390,38 @@ static std::string createManifestXml() {
376390
return readFile(File2.Path);
377391
}
378392

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-
423393
// Create a resource file containing a manifest XML.
424394
std::unique_ptr<MemoryBuffer> createManifestRes() {
425-
std::string Manifest = createManifestXml();
426-
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());
395+
// Create a temporary file for the resource script file.
396+
TemporaryFile RCFile("manifest", "rc");
433397

434-
// Copy the manifest data into the .res file.
435-
std::copy(Manifest.begin(), Manifest.end(), Buf);
436-
return Res;
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();
437425
}
438426

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

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());
609611
for (MemoryBufferRef MB : MBs) {
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");
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);
616625
}
617626

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());
627+
E.run();
628+
return File.getMemoryBuffer();
623629
}
624630

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

lld/test/COFF/combined-resources.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
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+
79
# RUN: yaml2obj < %p/Inputs/ret42.yaml > %t.obj
810
# RUN: lld-link /out:%t.exe /entry:main %t.obj %p/Inputs/resource.res \
911
# RUN: %p/Inputs/combined-resources.res %p/Inputs/combined-resources-2.res

lld/test/COFF/def-name.test

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

lld/test/COFF/dll.test

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

lld/test/COFF/dllimport-gc.test

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

lld/test/COFF/manifestinput.test

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

33
# RUN: yaml2obj %p/Inputs/ret42.yaml > %t.obj
44
# RUN: lld-link /out:%t.exe /entry:main \
@@ -8,28 +8,3 @@
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# REQUIRES: winres
2+
13
# RUN: yaml2obj < %p/Inputs/export.yaml > %t.obj
24
# RUN: lld-link /out:%t.dll /dll %t.obj
35
# RUN: llvm-readobj -file-headers %t.dll | FileCheck -check-prefix=ENTRY %s

lld/test/COFF/out.test

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

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

lld/test/COFF/resource.test

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

lld/test/lit.cfg

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ 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-
# 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')
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')

llvm/include/llvm/BinaryFormat/COFF.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ 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-
5549
// Sizes in bytes of various things in the COFF format.
5650
enum {
5751
Header16Size = 20,

llvm/include/llvm/Object/WindowsResource.h

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

4545
namespace llvm {
46+
4647
namespace object {
4748

4849
class WindowsResource;
4950

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-
};
51+
enum class Machine { UNKNOWN, ARM, X64, X86 };
8752

8853
class ResourceEntryRef {
8954
public:
@@ -108,14 +73,22 @@ class ResourceEntryRef {
10873

10974
Error loadNext();
11075

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+
11184
BinaryStreamReader Reader;
11285
bool IsStringType;
11386
ArrayRef<UTF16> Type;
11487
uint16_t TypeID;
11588
bool IsStringName;
11689
ArrayRef<UTF16> Name;
11790
uint16_t NameID;
118-
const WinResHeaderSuffix *Suffix = nullptr;
91+
const HeaderSuffix *Suffix = nullptr;
11992
ArrayRef<uint8_t> Data;
12093
const WindowsResource *OwningRes = nullptr;
12194
};

llvm/lib/BinaryFormat/Magic.cpp

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

0 commit comments

Comments
 (0)