]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - unittests/DriverTests/WinLinkDriverTest.cpp
Vendor import of lld trunk r233088:
[FreeBSD/FreeBSD.git] / unittests / DriverTests / WinLinkDriverTest.cpp
1 //===- lld/unittest/WinLinkDriverTest.cpp ---------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief Windows link.exe driver tests.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "DriverTest.h"
16 #include "lld/ReaderWriter/PECOFFLinkingContext.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/Support/COFF.h"
19 #include <set>
20 #include <vector>
21
22 using namespace llvm;
23 using namespace lld;
24
25 namespace {
26 class WinLinkParserTest
27     : public ParserTest<WinLinkDriver, PECOFFLinkingContext> {
28 protected:
29   const LinkingContext *linkingContext() override { return &_ctx; }
30 };
31 }
32
33 TEST_F(WinLinkParserTest, Basic) {
34   EXPECT_TRUE(parse("link.exe", "/subsystem:console", "/out:a.exe",
35                     "-entry:start", "a.obj", "b.obj", "c.obj", nullptr));
36   EXPECT_EQ(llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI, _ctx.getSubsystem());
37   EXPECT_EQ(llvm::COFF::IMAGE_FILE_MACHINE_I386, _ctx.getMachineType());
38   EXPECT_EQ("a.exe", _ctx.outputPath());
39   EXPECT_EQ("start", _ctx.getEntrySymbolName());
40   EXPECT_EQ(4, inputFileCount());
41   EXPECT_EQ("a.obj", inputFile(0));
42   EXPECT_EQ("b.obj", inputFile(1));
43   EXPECT_EQ("c.obj", inputFile(2));
44   EXPECT_TRUE(_ctx.getInputSearchPaths().empty());
45
46   // Unspecified flags will have default values.
47   EXPECT_FALSE(_ctx.isDll());
48   EXPECT_EQ(6, _ctx.getMinOSVersion().majorVersion);
49   EXPECT_EQ(0, _ctx.getMinOSVersion().minorVersion);
50   EXPECT_EQ(0x400000U, _ctx.getBaseAddress());
51   EXPECT_EQ(1024 * 1024U, _ctx.getStackReserve());
52   EXPECT_EQ(4096U, _ctx.getStackCommit());
53   EXPECT_EQ(4096U, _ctx.getSectionDefaultAlignment());
54   EXPECT_FALSE(_ctx.allowRemainingUndefines());
55   EXPECT_TRUE(_ctx.isNxCompat());
56   EXPECT_FALSE(_ctx.getLargeAddressAware());
57   EXPECT_TRUE(_ctx.getAllowBind());
58   EXPECT_TRUE(_ctx.getAllowIsolation());
59   EXPECT_FALSE(_ctx.getSwapRunFromCD());
60   EXPECT_FALSE(_ctx.getSwapRunFromNet());
61   EXPECT_TRUE(_ctx.getBaseRelocationEnabled());
62   EXPECT_TRUE(_ctx.isTerminalServerAware());
63   EXPECT_TRUE(_ctx.getDynamicBaseEnabled());
64   EXPECT_TRUE(_ctx.getCreateManifest());
65   EXPECT_EQ("", _ctx.getManifestDependency());
66   EXPECT_FALSE(_ctx.getEmbedManifest());
67   EXPECT_EQ(1, _ctx.getManifestId());
68   EXPECT_TRUE(_ctx.getManifestUAC());
69   EXPECT_EQ("'asInvoker'", _ctx.getManifestLevel());
70   EXPECT_EQ("'false'", _ctx.getManifestUiAccess());
71   EXPECT_TRUE(_ctx.deadStrip());
72   EXPECT_FALSE(_ctx.logInputFiles());
73 }
74
75 TEST_F(WinLinkParserTest, StartsWithHyphen) {
76   EXPECT_TRUE(
77       parse("link.exe", "-subsystem:console", "-out:a.exe", "a.obj", nullptr));
78   EXPECT_EQ(llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI, _ctx.getSubsystem());
79   EXPECT_EQ("a.exe", _ctx.outputPath());
80   EXPECT_EQ(2, inputFileCount());
81   EXPECT_EQ("a.obj", inputFile(0));
82 }
83
84 TEST_F(WinLinkParserTest, UppercaseOption) {
85   EXPECT_TRUE(
86       parse("link.exe", "/SUBSYSTEM:CONSOLE", "/OUT:a.exe", "a.obj", nullptr));
87   EXPECT_EQ(llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI, _ctx.getSubsystem());
88   EXPECT_EQ("a.exe", _ctx.outputPath());
89   EXPECT_EQ(2, inputFileCount());
90   EXPECT_EQ("a.obj", inputFile(0));
91 }
92
93 TEST_F(WinLinkParserTest, Mllvm) {
94   EXPECT_TRUE(parse("link.exe", "/mllvm:-debug", "a.obj", nullptr));
95   const std::vector<const char *> &options = _ctx.llvmOptions();
96   EXPECT_EQ(1U, options.size());
97   EXPECT_STREQ("-debug", options[0]);
98 }
99
100 TEST_F(WinLinkParserTest, NoInputFiles) {
101   EXPECT_FALSE(parse("link.exe", nullptr));
102   EXPECT_EQ("No input files\n", errorMessage());
103 }
104
105 //
106 // Tests for implicit file extension interpolation.
107 //
108
109 TEST_F(WinLinkParserTest, NoFileExtension) {
110   EXPECT_TRUE(parse("link.exe", "foo", "bar", nullptr));
111   EXPECT_EQ("foo.exe", _ctx.outputPath());
112   EXPECT_EQ(3, inputFileCount());
113   EXPECT_EQ("foo.obj", inputFile(0));
114   EXPECT_EQ("bar.obj", inputFile(1));
115 }
116
117 TEST_F(WinLinkParserTest, NonStandardFileExtension) {
118   EXPECT_TRUE(parse("link.exe", "foo.o", nullptr));
119   EXPECT_EQ("foo.exe", _ctx.outputPath());
120   EXPECT_EQ(2, inputFileCount());
121   EXPECT_EQ("foo.o", inputFile(0));
122 }
123
124 TEST_F(WinLinkParserTest, Libpath) {
125   EXPECT_TRUE(
126       parse("link.exe", "/libpath:dir1", "/libpath:dir2", "a.obj", nullptr));
127   const std::vector<StringRef> &paths = _ctx.getInputSearchPaths();
128   EXPECT_EQ(2U, paths.size());
129   EXPECT_EQ("dir1", paths[0]);
130   EXPECT_EQ("dir2", paths[1]);
131 }
132
133 //
134 // Tests for input file order
135 //
136
137 TEST_F(WinLinkParserTest, InputOrder) {
138   EXPECT_TRUE(parse("link.exe", "a.lib", "b.obj", "c.obj", "a.lib", "d.obj",
139                     nullptr));
140   EXPECT_EQ(5, inputFileCount());
141   EXPECT_EQ("b.obj", inputFile(0));
142   EXPECT_EQ("c.obj", inputFile(1));
143   EXPECT_EQ("d.obj", inputFile(2));
144   EXPECT_EQ("a.lib", inputFile(3));
145 }
146
147 //
148 // Tests for command line options that take values.
149 //
150
151 TEST_F(WinLinkParserTest, AlternateName) {
152   EXPECT_TRUE(parse("link.exe", "/alternatename:sym1=sym",
153                     "/alternatename:sym2=sym", "a.out", nullptr));
154   const std::set<std::string> &aliases = _ctx.getAlternateNames("sym");
155   EXPECT_EQ(2U, aliases.size());
156   auto it = aliases.begin();
157   EXPECT_EQ("sym1", *it++);
158   EXPECT_EQ("sym2", *it++);
159 }
160
161 TEST_F(WinLinkParserTest, Export) {
162   EXPECT_TRUE(parse("link.exe", "/export:foo", "a.out", nullptr));
163   const std::vector<PECOFFLinkingContext::ExportDesc> &exports =
164       _ctx.getDllExports();
165   EXPECT_EQ(1U, exports.size());
166   EXPECT_EQ("_foo", exports[0].name);
167   EXPECT_EQ(-1, exports[0].ordinal);
168   EXPECT_FALSE(exports[0].noname);
169   EXPECT_FALSE(exports[0].isData);
170 }
171
172 TEST_F(WinLinkParserTest, ExportWithOptions) {
173   EXPECT_TRUE(parse("link.exe", "/export:foo,@8,noname,data",
174                     "/export:bar,@10,data", "a.out", nullptr));
175   const std::vector<PECOFFLinkingContext::ExportDesc> &exports =
176       _ctx.getDllExports();
177   EXPECT_EQ(2U, exports.size());
178   EXPECT_EQ("_foo", exports[0].name);
179   EXPECT_EQ(8, exports[0].ordinal);
180   EXPECT_TRUE(exports[0].noname);
181   EXPECT_TRUE(exports[0].isData);
182   EXPECT_EQ("_bar", exports[1].name);
183   EXPECT_EQ(10, exports[1].ordinal);
184   EXPECT_FALSE(exports[1].noname);
185   EXPECT_TRUE(exports[1].isData);
186 }
187
188 TEST_F(WinLinkParserTest, ExportDuplicateExports) {
189   EXPECT_TRUE(
190       parse("link.exe", "/export:foo", "/export:foo,@2", "a.out", nullptr));
191   const std::vector<PECOFFLinkingContext::ExportDesc> &exports =
192       _ctx.getDllExports();
193   EXPECT_EQ(1U, exports.size());
194   EXPECT_EQ("_foo", exports[0].name);
195   EXPECT_EQ(-1, exports[0].ordinal);
196 }
197
198 TEST_F(WinLinkParserTest, ExportDuplicateOrdinals) {
199   EXPECT_FALSE(
200       parse("link.exe", "/export:foo,@1", "/export:bar,@1", "a.out", nullptr));
201 }
202
203 TEST_F(WinLinkParserTest, ExportInvalid1) {
204   EXPECT_FALSE(parse("link.exe", "/export:foo,@0", "a.out", nullptr));
205 }
206
207 TEST_F(WinLinkParserTest, ExportInvalid2) {
208   EXPECT_FALSE(parse("link.exe", "/export:foo,@65536", "a.out", nullptr));
209 }
210
211 TEST_F(WinLinkParserTest, MachineX86) {
212   EXPECT_TRUE(parse("link.exe", "/machine:x86", "a.obj", nullptr));
213   EXPECT_EQ(llvm::COFF::IMAGE_FILE_MACHINE_I386, _ctx.getMachineType());
214 }
215
216 TEST_F(WinLinkParserTest, MachineX64) {
217   EXPECT_TRUE(parse("link.exe", "/machine:x64", "a.obj", nullptr));
218   EXPECT_EQ(llvm::COFF::IMAGE_FILE_MACHINE_AMD64, _ctx.getMachineType());
219 }
220
221 TEST_F(WinLinkParserTest, MachineArm) {
222   EXPECT_TRUE(parse("link.exe", "/machine:arm", "a.obj", nullptr));
223   EXPECT_EQ(llvm::COFF::IMAGE_FILE_MACHINE_ARMNT, _ctx.getMachineType());
224 }
225
226 TEST_F(WinLinkParserTest, MachineUnknown) {
227   EXPECT_FALSE(parse("link.exe", "/machine:nosucharch", "a.obj", nullptr));
228   EXPECT_EQ("error: unknown machine type: nosucharch\n", errorMessage());
229 }
230
231 TEST_F(WinLinkParserTest, MajorImageVersion) {
232   EXPECT_TRUE(parse("link.exe", "/version:7", "foo.o", nullptr));
233   EXPECT_EQ(7, _ctx.getImageVersion().majorVersion);
234   EXPECT_EQ(0, _ctx.getImageVersion().minorVersion);
235 }
236
237 TEST_F(WinLinkParserTest, MajorMinorImageVersion) {
238   EXPECT_TRUE(parse("link.exe", "/version:72.35", "foo.o", nullptr));
239   EXPECT_EQ(72, _ctx.getImageVersion().majorVersion);
240   EXPECT_EQ(35, _ctx.getImageVersion().minorVersion);
241 }
242
243 TEST_F(WinLinkParserTest, MinMajorOSVersion) {
244   EXPECT_TRUE(parse("link.exe", "/subsystem:windows,3", "foo.o", nullptr));
245   EXPECT_EQ(llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI, _ctx.getSubsystem());
246   EXPECT_EQ(3, _ctx.getMinOSVersion().majorVersion);
247   EXPECT_EQ(0, _ctx.getMinOSVersion().minorVersion);
248 }
249
250 TEST_F(WinLinkParserTest, MinMajorMinorOSVersion) {
251   EXPECT_TRUE(parse("link.exe", "/subsystem:windows,3.1", "foo.o", nullptr));
252   EXPECT_EQ(llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_GUI, _ctx.getSubsystem());
253   EXPECT_EQ(3, _ctx.getMinOSVersion().majorVersion);
254   EXPECT_EQ(1, _ctx.getMinOSVersion().minorVersion);
255 }
256
257 TEST_F(WinLinkParserTest, Base) {
258   EXPECT_TRUE(parse("link.exe", "/base:8388608", "a.obj", nullptr));
259   EXPECT_EQ(0x800000U, _ctx.getBaseAddress());
260 }
261
262 TEST_F(WinLinkParserTest, InvalidBase) {
263   EXPECT_FALSE(parse("link.exe", "/base:1234", "a.obj", nullptr));
264   EXPECT_TRUE(StringRef(errorMessage())
265                   .startswith("Base address have to be multiple of 64K"));
266 }
267
268 TEST_F(WinLinkParserTest, StackReserve) {
269   EXPECT_TRUE(parse("link.exe", "/stack:8192", "a.obj", nullptr));
270   EXPECT_EQ(8192U, _ctx.getStackReserve());
271   EXPECT_EQ(4096U, _ctx.getStackCommit());
272 }
273
274 TEST_F(WinLinkParserTest, StackReserveAndCommit) {
275   EXPECT_TRUE(parse("link.exe", "/stack:16384,8192", "a.obj", nullptr));
276   EXPECT_EQ(16384U, _ctx.getStackReserve());
277   EXPECT_EQ(8192U, _ctx.getStackCommit());
278 }
279
280 TEST_F(WinLinkParserTest, InvalidStackSize) {
281   EXPECT_FALSE(parse("link.exe", "/stack:8192,16384", "a.obj", nullptr));
282   EXPECT_TRUE(StringRef(errorMessage()).startswith("Invalid stack size"));
283 }
284
285 TEST_F(WinLinkParserTest, HeapReserve) {
286   EXPECT_TRUE(parse("link.exe", "/heap:8192", "a.obj", nullptr));
287   EXPECT_EQ(8192U, _ctx.getHeapReserve());
288   EXPECT_EQ(4096U, _ctx.getHeapCommit());
289 }
290
291 TEST_F(WinLinkParserTest, HeapReserveAndCommit) {
292   EXPECT_TRUE(parse("link.exe", "/heap:16384,8192", "a.obj", nullptr));
293   EXPECT_EQ(16384U, _ctx.getHeapReserve());
294   EXPECT_EQ(8192U, _ctx.getHeapCommit());
295 }
296
297 TEST_F(WinLinkParserTest, InvalidHeapSize) {
298   EXPECT_FALSE(parse("link.exe", "/heap:8192,16384", "a.obj", nullptr));
299   EXPECT_TRUE(StringRef(errorMessage()).startswith("Invalid heap size"));
300 }
301
302 TEST_F(WinLinkParserTest, SectionAlignment) {
303   EXPECT_TRUE(parse("link.exe", "/align:8192", "a.obj", nullptr));
304   EXPECT_EQ(8192U, _ctx.getSectionDefaultAlignment());
305 }
306
307 TEST_F(WinLinkParserTest, InvalidAlignment) {
308   EXPECT_FALSE(parse("link.exe", "/align:1000", "a.obj", nullptr));
309   EXPECT_EQ("Section alignment must be a power of 2, but got 1000\n",
310             errorMessage());
311 }
312
313 TEST_F(WinLinkParserTest, Include) {
314   EXPECT_TRUE(parse("link.exe", "/include:foo", "a.out", nullptr));
315   auto symbols = _ctx.initialUndefinedSymbols();
316   EXPECT_FALSE(symbols.empty());
317   EXPECT_EQ("foo", symbols[0]);
318 }
319
320 TEST_F(WinLinkParserTest, Merge) {
321   EXPECT_TRUE(parse("link.exe", "/merge:.foo=.bar", "/merge:.bar=.baz",
322                     "a.out", nullptr));
323   EXPECT_EQ(".baz", _ctx.getOutputSectionName(".foo"));
324   EXPECT_EQ(".baz", _ctx.getOutputSectionName(".bar"));
325   EXPECT_EQ(".abc", _ctx.getOutputSectionName(".abc"));
326 }
327
328 TEST_F(WinLinkParserTest, Merge_Circular) {
329   EXPECT_FALSE(parse("link.exe", "/merge:.foo=.bar", "/merge:.bar=.foo",
330                      "a.out", nullptr));
331 }
332
333 TEST_F(WinLinkParserTest, Implib) {
334   EXPECT_TRUE(parse("link.exe", "/implib:foo.dll.lib", "a.out", nullptr));
335   EXPECT_EQ("foo.dll.lib", _ctx.getOutputImportLibraryPath());
336 }
337
338 TEST_F(WinLinkParserTest, ImplibDefault) {
339   EXPECT_TRUE(parse("link.exe", "/out:foobar.dll", "a.out", nullptr));
340   EXPECT_EQ("foobar.lib", _ctx.getOutputImportLibraryPath());
341 }
342
343 //
344 // Tests for /section
345 //
346
347 namespace {
348 const uint32_t discardable = llvm::COFF::IMAGE_SCN_MEM_DISCARDABLE;
349 const uint32_t not_cached = llvm::COFF::IMAGE_SCN_MEM_NOT_CACHED;
350 const uint32_t not_paged = llvm::COFF::IMAGE_SCN_MEM_NOT_PAGED;
351 const uint32_t shared = llvm::COFF::IMAGE_SCN_MEM_SHARED;
352 const uint32_t execute = llvm::COFF::IMAGE_SCN_MEM_EXECUTE;
353 const uint32_t read = llvm::COFF::IMAGE_SCN_MEM_READ;
354 const uint32_t write = llvm::COFF::IMAGE_SCN_MEM_WRITE;
355
356 #define TEST_SECTION(testname, arg, expect)                                    \
357   TEST_F(WinLinkParserTest, testname) {                                        \
358     EXPECT_TRUE(parse("link.exe", "/section:.text," arg, "a.obj", nullptr));   \
359     EXPECT_EQ(expect, _ctx.getSectionAttributes(".text", execute | read)); \
360   }
361
362 TEST_SECTION(SectionD, "d", execute | read | discardable)
363 TEST_SECTION(SectionE, "e", execute)
364 TEST_SECTION(SectionK, "k", execute | read | not_cached)
365 TEST_SECTION(SectionP, "p", execute | read | not_paged)
366 TEST_SECTION(SectionR, "r", read)
367 TEST_SECTION(SectionS, "s", execute | read | shared)
368 TEST_SECTION(SectionW, "w", write)
369
370 #undef TEST_SECTION
371
372 TEST_F(WinLinkParserTest, Section) {
373   EXPECT_TRUE(parse("link.exe", "/section:.text,dekprsw",
374                     "/section:.text,!dekprsw", "a.obj", nullptr));
375   EXPECT_EQ(0U, _ctx.getSectionAttributes(".text", execute | read));
376 }
377
378 TEST_F(WinLinkParserTest, SectionNegate) {
379   EXPECT_TRUE(parse("link.exe", "/section:.text,!e", "a.obj", nullptr));
380   EXPECT_EQ(read, _ctx.getSectionAttributes(".text", execute | read));
381 }
382
383 TEST_F(WinLinkParserTest, SectionMultiple) {
384   EXPECT_TRUE(parse("link.exe", "/section:.foo,e", "/section:.foo,rw",
385                     "/section:.foo,!d", "a.obj", nullptr));
386   uint32_t flags = execute | read | not_paged | discardable;
387   uint32_t expected = execute | read | write | not_paged;
388   EXPECT_EQ(expected, _ctx.getSectionAttributes(".foo", flags));
389 }
390
391 } // end anonymous namespace
392
393 //
394 // Tests for /defaultlib and /nodefaultlib.
395 //
396
397 TEST_F(WinLinkParserTest, DefaultLib) {
398   EXPECT_TRUE(parse("link.exe", "/defaultlib:user32.lib",
399                     "/defaultlib:kernel32", "a.obj", nullptr));
400   EXPECT_EQ(4, inputFileCount());
401   EXPECT_EQ("a.obj", inputFile(0));
402   EXPECT_EQ("user32.lib", inputFile(1));
403   EXPECT_EQ("kernel32.lib", inputFile(2));
404 }
405
406 TEST_F(WinLinkParserTest, DefaultLibDuplicates) {
407   EXPECT_TRUE(parse("link.exe", "/defaultlib:user32.lib",
408                     "/defaultlib:user32.lib", "a.obj", nullptr));
409   EXPECT_EQ(3, inputFileCount());
410   EXPECT_EQ("a.obj", inputFile(0));
411   EXPECT_EQ("user32.lib", inputFile(1));
412 }
413
414 TEST_F(WinLinkParserTest, NoDefaultLib) {
415   EXPECT_TRUE(parse("link.exe", "/defaultlib:user32.lib",
416                     "/defaultlib:kernel32", "/nodefaultlib:user32.lib", "a.obj",
417                     nullptr));
418   EXPECT_EQ(3, inputFileCount());
419   EXPECT_EQ("a.obj", inputFile(0));
420   EXPECT_EQ("kernel32.lib", inputFile(1));
421 }
422
423 TEST_F(WinLinkParserTest, NoDefaultLibCase) {
424   EXPECT_TRUE(parse("link.exe", "/defaultlib:user32",
425                     "/defaultlib:kernel32", "/nodefaultlib:USER32.LIB", "a.obj",
426                     nullptr));
427   EXPECT_EQ(3, inputFileCount());
428   EXPECT_EQ("a.obj", inputFile(0));
429   EXPECT_EQ("kernel32.lib", inputFile(1));
430 }
431
432 TEST_F(WinLinkParserTest, NoDefaultLibAll) {
433   EXPECT_TRUE(parse("link.exe", "/defaultlib:user32.lib",
434                     "/defaultlib:kernel32", "/nodefaultlib", "a.obj", nullptr));
435   EXPECT_EQ(2, inputFileCount());
436   EXPECT_EQ("a.obj", inputFile(0));
437 }
438
439 TEST_F(WinLinkParserTest, DisallowLib) {
440   EXPECT_TRUE(parse("link.exe", "/defaultlib:user32.lib",
441                     "/defaultlib:kernel32", "/disallowlib:user32.lib", "a.obj",
442                     nullptr));
443   EXPECT_EQ(3, inputFileCount());
444   EXPECT_EQ("a.obj", inputFile(0));
445   EXPECT_EQ("kernel32.lib", inputFile(1));
446 }
447
448 //
449 // Tests for DLL.
450 //
451
452 TEST_F(WinLinkParserTest, NoEntry) {
453   EXPECT_TRUE(parse("link.exe", "/noentry", "/dll", "a.obj", nullptr));
454   EXPECT_TRUE(_ctx.isDll());
455   EXPECT_EQ(0x10000000U, _ctx.getBaseAddress());
456   EXPECT_EQ("", _ctx.entrySymbolName());
457 }
458
459 TEST_F(WinLinkParserTest, NoEntryError) {
460   // /noentry without /dll is an error.
461   EXPECT_FALSE(parse("link.exe", "/noentry", "a.obj", nullptr));
462   EXPECT_EQ("/noentry must be specified with /dll\n", errorMessage());
463 }
464
465 //
466 // Tests for DELAYLOAD.
467 //
468
469 TEST_F(WinLinkParserTest, DelayLoad) {
470   EXPECT_TRUE(parse("link.exe", "/delayload:abc.dll", "/delayload:def.dll",
471                     "a.obj", nullptr));
472   EXPECT_TRUE(_ctx.isDelayLoadDLL("abc.dll"));
473   EXPECT_TRUE(_ctx.isDelayLoadDLL("DEF.DLL"));
474   EXPECT_FALSE(_ctx.isDelayLoadDLL("xyz.dll"));
475 }
476
477 //
478 // Tests for SEH.
479 //
480
481 TEST_F(WinLinkParserTest, SafeSEH) {
482   EXPECT_TRUE(parse("link.exe", "/safeseh", "a.obj", nullptr));
483   EXPECT_TRUE(_ctx.requireSEH());
484   EXPECT_FALSE(_ctx.noSEH());
485 }
486
487 TEST_F(WinLinkParserTest, NoSafeSEH) {
488   EXPECT_TRUE(parse("link.exe", "/safeseh:no", "a.obj", nullptr));
489   EXPECT_FALSE(_ctx.requireSEH());
490   EXPECT_TRUE(_ctx.noSEH());
491 }
492
493 //
494 // Tests for boolean flags.
495 //
496
497 TEST_F(WinLinkParserTest, Force) {
498   EXPECT_TRUE(parse("link.exe", "/force", "a.obj", nullptr));
499   EXPECT_TRUE(_ctx.allowRemainingUndefines());
500 }
501
502 TEST_F(WinLinkParserTest, ForceUnresolved) {
503   EXPECT_TRUE(parse("link.exe", "/force:unresolved", "a.obj", nullptr));
504   EXPECT_TRUE(_ctx.allowRemainingUndefines());
505 }
506
507 TEST_F(WinLinkParserTest, NoNxCompat) {
508   EXPECT_TRUE(parse("link.exe", "/nxcompat:no", "a.obj", nullptr));
509   EXPECT_FALSE(_ctx.isNxCompat());
510 }
511
512 TEST_F(WinLinkParserTest, LargeAddressAware) {
513   EXPECT_TRUE(parse("link.exe", "/largeaddressaware", "a.obj", nullptr));
514   EXPECT_TRUE(_ctx.getLargeAddressAware());
515 }
516
517 TEST_F(WinLinkParserTest, NoLargeAddressAware) {
518   EXPECT_TRUE(parse("link.exe", "/largeaddressaware:no", "a.obj", nullptr));
519   EXPECT_FALSE(_ctx.getLargeAddressAware());
520 }
521
522 TEST_F(WinLinkParserTest, AllowBind) {
523   EXPECT_TRUE(parse("link.exe", "/allowbind", "a.obj", nullptr));
524   EXPECT_TRUE(_ctx.getAllowBind());
525 }
526
527 TEST_F(WinLinkParserTest, NoAllowBind) {
528   EXPECT_TRUE(parse("link.exe", "/allowbind:no", "a.obj", nullptr));
529   EXPECT_FALSE(_ctx.getAllowBind());
530 }
531
532 TEST_F(WinLinkParserTest, AllowIsolation) {
533   EXPECT_TRUE(parse("link.exe", "/allowisolation", "a.obj", nullptr));
534   EXPECT_TRUE(_ctx.getAllowIsolation());
535 }
536
537 TEST_F(WinLinkParserTest, NoAllowIsolation) {
538   EXPECT_TRUE(parse("link.exe", "/allowisolation:no", "a.obj", nullptr));
539   EXPECT_FALSE(_ctx.getAllowIsolation());
540 }
541
542 TEST_F(WinLinkParserTest, SwapRunFromCD) {
543   EXPECT_TRUE(parse("link.exe", "/swaprun:cd", "a.obj", nullptr));
544   EXPECT_TRUE(_ctx.getSwapRunFromCD());
545 }
546
547 TEST_F(WinLinkParserTest, SwapRunFromNet) {
548   EXPECT_TRUE(parse("link.exe", "/swaprun:net", "a.obj", nullptr));
549   EXPECT_TRUE(_ctx.getSwapRunFromNet());
550 }
551
552 TEST_F(WinLinkParserTest, Debug) {
553   EXPECT_TRUE(parse("link.exe", "/debug", "a.obj", nullptr));
554   EXPECT_TRUE(_ctx.deadStrip());
555   EXPECT_TRUE(_ctx.getDebug());
556   EXPECT_EQ("a.pdb", _ctx.getPDBFilePath());
557 }
558
559 TEST_F(WinLinkParserTest, PDB) {
560   EXPECT_TRUE(parse("link.exe", "/debug", "/pdb:foo.pdb", "a.obj", nullptr));
561   EXPECT_TRUE(_ctx.getDebug());
562   EXPECT_EQ("foo.pdb", _ctx.getPDBFilePath());
563 }
564
565 TEST_F(WinLinkParserTest, Fixed) {
566   EXPECT_TRUE(parse("link.exe", "/fixed", "a.out", nullptr));
567   EXPECT_FALSE(_ctx.getBaseRelocationEnabled());
568   EXPECT_FALSE(_ctx.getDynamicBaseEnabled());
569 }
570
571 TEST_F(WinLinkParserTest, NoFixed) {
572   EXPECT_TRUE(parse("link.exe", "/fixed:no", "a.out", nullptr));
573   EXPECT_TRUE(_ctx.getBaseRelocationEnabled());
574 }
575
576 TEST_F(WinLinkParserTest, TerminalServerAware) {
577   EXPECT_TRUE(parse("link.exe", "/tsaware", "a.out", nullptr));
578   EXPECT_TRUE(_ctx.isTerminalServerAware());
579 }
580
581 TEST_F(WinLinkParserTest, NoTerminalServerAware) {
582   EXPECT_TRUE(parse("link.exe", "/tsaware:no", "a.out", nullptr));
583   EXPECT_FALSE(_ctx.isTerminalServerAware());
584 }
585
586 TEST_F(WinLinkParserTest, DynamicBase) {
587   EXPECT_TRUE(parse("link.exe", "/dynamicbase", "a.out", nullptr));
588   EXPECT_TRUE(_ctx.getDynamicBaseEnabled());
589 }
590
591 TEST_F(WinLinkParserTest, NoDynamicBase) {
592   EXPECT_TRUE(parse("link.exe", "/dynamicbase:no", "a.out", nullptr));
593   EXPECT_FALSE(_ctx.getDynamicBaseEnabled());
594 }
595
596 //
597 // Test for /failifmismatch
598 //
599
600 TEST_F(WinLinkParserTest, FailIfMismatch_Match) {
601   EXPECT_TRUE(parse("link.exe", "/failifmismatch:foo=bar",
602                     "/failifmismatch:foo=bar", "/failifmismatch:abc=def",
603                     "a.out", nullptr));
604 }
605
606 TEST_F(WinLinkParserTest, FailIfMismatch_Mismatch) {
607   EXPECT_FALSE(parse("link.exe", "/failifmismatch:foo=bar",
608                      "/failifmismatch:foo=baz", "a.out", nullptr));
609 }
610
611 //
612 // Tests for /manifest, /manifestuac, /manifestfile, and /manifestdependency.
613 //
614 TEST_F(WinLinkParserTest, Manifest_Default) {
615   EXPECT_TRUE(parse("link.exe", "/manifest", "a.out", nullptr));
616   EXPECT_TRUE(_ctx.getCreateManifest());
617   EXPECT_FALSE(_ctx.getEmbedManifest());
618   EXPECT_EQ(1, _ctx.getManifestId());
619   EXPECT_EQ("'asInvoker'", _ctx.getManifestLevel());
620   EXPECT_EQ("'false'", _ctx.getManifestUiAccess());
621 }
622
623 TEST_F(WinLinkParserTest, Manifest_No) {
624   EXPECT_TRUE(parse("link.exe", "/manifest:no", "a.out", nullptr));
625   EXPECT_FALSE(_ctx.getCreateManifest());
626 }
627
628 TEST_F(WinLinkParserTest, Manifestuac_no) {
629   EXPECT_TRUE(parse("link.exe", "/manifestuac:NO", "a.out", nullptr));
630   EXPECT_FALSE(_ctx.getManifestUAC());
631 }
632
633 TEST_F(WinLinkParserTest, Manifestuac_Level) {
634   EXPECT_TRUE(parse("link.exe", "/manifestuac:level='requireAdministrator'",
635                     "a.out", nullptr));
636   EXPECT_EQ("'requireAdministrator'", _ctx.getManifestLevel());
637   EXPECT_EQ("'false'", _ctx.getManifestUiAccess());
638 }
639
640 TEST_F(WinLinkParserTest, Manifestuac_UiAccess) {
641   EXPECT_TRUE(parse("link.exe", "/manifestuac:uiAccess='true'", "a.out", nullptr));
642   EXPECT_EQ("'asInvoker'", _ctx.getManifestLevel());
643   EXPECT_EQ("'true'", _ctx.getManifestUiAccess());
644 }
645
646 TEST_F(WinLinkParserTest, Manifestuac_LevelAndUiAccess) {
647   EXPECT_TRUE(parse("link.exe",
648                     "/manifestuac:level='requireAdministrator' uiAccess='true'",
649                     "a.out", nullptr));
650   EXPECT_EQ("'requireAdministrator'", _ctx.getManifestLevel());
651   EXPECT_EQ("'true'", _ctx.getManifestUiAccess());
652 }
653
654 TEST_F(WinLinkParserTest, Manifestfile) {
655   EXPECT_TRUE(parse("link.exe", "/manifestfile:bar.manifest",
656                     "a.out", nullptr));
657   EXPECT_EQ("bar.manifest", _ctx.getManifestOutputPath());
658 }
659
660 TEST_F(WinLinkParserTest, Manifestdependency) {
661   EXPECT_TRUE(parse("link.exe", "/manifestdependency:foo bar", "a.out",
662                     nullptr));
663   EXPECT_EQ("foo bar", _ctx.getManifestDependency());
664 }
665
666 //
667 // Test for /OPT
668 //
669
670 TEST_F(WinLinkParserTest, OptNoRef) {
671   EXPECT_TRUE(parse("link.exe", "/opt:noref", "a.obj", nullptr));
672   EXPECT_FALSE(_ctx.deadStrip());
673 }
674
675 TEST_F(WinLinkParserTest, OptIgnore) {
676   EXPECT_TRUE(parse("link.exe", "/opt:ref", "/opt:icf", "/opt:noicf",
677                     "/opt:icf=foo", "/opt:lbr", "/opt:nolbr", "a.obj",
678                     nullptr));
679 }
680
681 TEST_F(WinLinkParserTest, OptUnknown) {
682   EXPECT_FALSE(parse("link.exe", "/opt:foo", "a.obj", nullptr));
683 }
684
685 //
686 // Test for /PROFILE
687 //
688
689 TEST_F(WinLinkParserTest, Profile) {
690   EXPECT_TRUE(parse("link.exe", "/profile", "a.obj", nullptr));
691   EXPECT_TRUE(_ctx.deadStrip());
692   EXPECT_TRUE(_ctx.getBaseRelocationEnabled());
693   EXPECT_TRUE(_ctx.getDynamicBaseEnabled());
694 }
695
696 //
697 // Test for command line flags that are ignored.
698 //
699
700 TEST_F(WinLinkParserTest, Ignore) {
701   // There are some no-op command line options that are recognized for
702   // compatibility with link.exe.
703   EXPECT_TRUE(parse("link.exe", "/nologo", "/errorreport:prompt",
704                     "/incremental", "/incremental:no", "/delay:unload",
705                     "/disallowlib:foo", "/pdbaltpath:bar",
706                     "/wx", "/wx:no", "/tlbid:1", "/tlbout:foo", "/idlout:foo",
707                     "/ignore:4000", "/ignoreidl", "/implib:foo", "/safeseh",
708                     "/safeseh:no", "/functionpadmin", "/maxilksize:1024",
709                     "a.obj", nullptr));
710   EXPECT_EQ("", errorMessage());
711   EXPECT_EQ(2, inputFileCount());
712   EXPECT_EQ("a.obj", inputFile(0));
713 }
714
715 //
716 // Test for "--"
717 //
718
719 TEST_F(WinLinkParserTest, DashDash) {
720   EXPECT_TRUE(parse("link.exe", "/subsystem:console", "/out:a.exe", "a.obj",
721                     "--", "b.obj", "-c.obj", nullptr));
722   EXPECT_EQ(llvm::COFF::IMAGE_SUBSYSTEM_WINDOWS_CUI, _ctx.getSubsystem());
723   EXPECT_EQ("a.exe", _ctx.outputPath());
724   EXPECT_EQ(4, inputFileCount());
725   EXPECT_EQ("a.obj", inputFile(0));
726   EXPECT_EQ("b.obj", inputFile(1));
727   EXPECT_EQ("-c.obj", inputFile(2));
728 }