Coverage Report

Created: 2018-01-17 17:22

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/COFF/DriverUtils.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DriverUtils.cpp ----------------------------------------------------===//
2
//
3
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file contains utility functions for the driver. Because there
11
// are so many small functions, we created this separate file to make
12
// Driver.cpp less cluttered.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#include "Config.h"
17
#include "Driver.h"
18
#include "Symbols.h"
19
#include "lld/Common/ErrorHandler.h"
20
#include "lld/Common/Memory.h"
21
#include "llvm/ADT/Optional.h"
22
#include "llvm/ADT/StringSwitch.h"
23
#include "llvm/BinaryFormat/COFF.h"
24
#include "llvm/Object/COFF.h"
25
#include "llvm/Object/WindowsResource.h"
26
#include "llvm/Option/Arg.h"
27
#include "llvm/Option/ArgList.h"
28
#include "llvm/Option/Option.h"
29
#include "llvm/Support/CommandLine.h"
30
#include "llvm/Support/FileUtilities.h"
31
#include "llvm/Support/MathExtras.h"
32
#include "llvm/Support/Process.h"
33
#include "llvm/Support/Program.h"
34
#include "llvm/Support/raw_ostream.h"
35
#include "llvm/WindowsManifest/WindowsManifestMerger.h"
36
#include <memory>
37
38
using namespace llvm::COFF;
39
using namespace llvm;
40
using llvm::sys::Process;
41
42
namespace lld {
43
namespace coff {
44
namespace {
45
46
const uint16_t SUBLANG_ENGLISH_US = 0x0409;
47
const uint16_t RT_MANIFEST = 24;
48
49
class Executor {
50
public:
51
0
  explicit Executor(StringRef S) : Prog(Saver.save(S)) {}
52
0
  void add(StringRef S) { Args.push_back(Saver.save(S)); }
53
0
  void add(std::string &S) { Args.push_back(Saver.save(S)); }
54
0
  void add(Twine S) { Args.push_back(Saver.save(S)); }
55
0
  void add(const char *S) { Args.push_back(Saver.save(S)); }
56
57
0
  void run() {
58
0
    ErrorOr<std::string> ExeOrErr = sys::findProgramByName(Prog);
59
0
    if (auto EC = ExeOrErr.getError())
60
0
      fatal("unable to find " + Prog + " in PATH: " + EC.message());
61
0
    StringRef Exe = Saver.save(*ExeOrErr);
62
0
    Args.insert(Args.begin(), Exe);
63
0
64
0
    std::vector<const char *> Vec;
65
0
    for (StringRef S : Args)
66
0
      Vec.push_back(S.data());
67
0
    Vec.push_back(nullptr);
68
0
69
0
    if (sys::ExecuteAndWait(Args[0], Vec.data()) != 0)
70
0
      fatal("ExecuteAndWait failed: " +
71
0
            llvm::join(Args.begin(), Args.end(), " "));
72
0
  }
73
74
private:
75
  StringRef Prog;
76
  std::vector<StringRef> Args;
77
};
78
79
} // anonymous namespace
80
81
// Returns /machine's value.
82
0
MachineTypes getMachineType(StringRef S) {
83
0
  MachineTypes MT = StringSwitch<MachineTypes>(S.lower())
84
0
                        .Cases("x64", "amd64", AMD64)
85
0
                        .Cases("x86", "i386", I386)
86
0
                        .Case("arm", ARMNT)
87
0
                        .Case("arm64", ARM64)
88
0
                        .Default(IMAGE_FILE_MACHINE_UNKNOWN);
89
0
  if (MT != IMAGE_FILE_MACHINE_UNKNOWN)
90
0
    return MT;
91
0
  fatal("unknown /machine argument: " + S);
92
0
}
93
94
0
StringRef machineToStr(MachineTypes MT) {
95
0
  switch (MT) {
96
0
  case ARMNT:
97
0
    return "arm";
98
0
  case ARM64:
99
0
    return "arm64";
100
0
  case AMD64:
101
0
    return "x64";
102
0
  case I386:
103
0
    return "x86";
104
0
  default:
105
0
    llvm_unreachable("unknown machine type");
106
0
  }
107
0
}
108
109
// Parses a string in the form of "<integer>[,<integer>]".
110
0
void parseNumbers(StringRef Arg, uint64_t *Addr, uint64_t *Size) {
111
0
  StringRef S1, S2;
112
0
  std::tie(S1, S2) = Arg.split(',');
113
0
  if (S1.getAsInteger(0, *Addr))
114
0
    fatal("invalid number: " + S1);
115
0
  if (Size && !S2.empty() && S2.getAsInteger(0, *Size))
116
0
    fatal("invalid number: " + S2);
117
0
}
118
119
// Parses a string in the form of "<integer>[.<integer>]".
120
// If second number is not present, Minor is set to 0.
121
0
void parseVersion(StringRef Arg, uint32_t *Major, uint32_t *Minor) {
122
0
  StringRef S1, S2;
123
0
  std::tie(S1, S2) = Arg.split('.');
124
0
  if (S1.getAsInteger(0, *Major))
125
0
    fatal("invalid number: " + S1);
126
0
  *Minor = 0;
127
0
  if (!S2.empty() && S2.getAsInteger(0, *Minor))
128
0
    fatal("invalid number: " + S2);
129
0
}
130
131
// Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
132
void parseSubsystem(StringRef Arg, WindowsSubsystem *Sys, uint32_t *Major,
133
0
                    uint32_t *Minor) {
134
0
  StringRef SysStr, Ver;
135
0
  std::tie(SysStr, Ver) = Arg.split(',');
136
0
  *Sys = StringSwitch<WindowsSubsystem>(SysStr.lower())
137
0
    .Case("boot_application", IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION)
138
0
    .Case("console", IMAGE_SUBSYSTEM_WINDOWS_CUI)
139
0
    .Case("efi_application", IMAGE_SUBSYSTEM_EFI_APPLICATION)
140
0
    .Case("efi_boot_service_driver", IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER)
141
0
    .Case("efi_rom", IMAGE_SUBSYSTEM_EFI_ROM)
142
0
    .Case("efi_runtime_driver", IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)
143
0
    .Case("native", IMAGE_SUBSYSTEM_NATIVE)
144
0
    .Case("posix", IMAGE_SUBSYSTEM_POSIX_CUI)
145
0
    .Case("windows", IMAGE_SUBSYSTEM_WINDOWS_GUI)
146
0
    .Default(IMAGE_SUBSYSTEM_UNKNOWN);
147
0
  if (*Sys == IMAGE_SUBSYSTEM_UNKNOWN)
148
0
    fatal("unknown subsystem: " + SysStr);
149
0
  if (!Ver.empty())
150
0
    parseVersion(Ver, Major, Minor);
151
0
}
152
153
// Parse a string of the form of "<from>=<to>".
154
// Results are directly written to Config.
155
0
void parseAlternateName(StringRef S) {
156
0
  StringRef From, To;
157
0
  std::tie(From, To) = S.split('=');
158
0
  if (From.empty() || To.empty())
159
0
    fatal("/alternatename: invalid argument: " + S);
160
0
  auto It = Config->AlternateNames.find(From);
161
0
  if (It != Config->AlternateNames.end() && It->second != To)
162
0
    fatal("/alternatename: conflicts: " + S);
163
0
  Config->AlternateNames.insert(It, std::make_pair(From, To));
164
0
}
165
166
// Parse a string of the form of "<from>=<to>".
167
// Results are directly written to Config.
168
0
void parseMerge(StringRef S) {
169
0
  StringRef From, To;
170
0
  std::tie(From, To) = S.split('=');
171
0
  if (From.empty() || To.empty())
172
0
    fatal("/merge: invalid argument: " + S);
173
0
  auto Pair = Config->Merge.insert(std::make_pair(From, To));
174
0
  bool Inserted = Pair.second;
175
0
  if (!Inserted) {
176
0
    StringRef Existing = Pair.first->second;
177
0
    if (Existing != To)
178
0
      warn(S + ": already merged into " + Existing);
179
0
  }
180
0
}
181
182
0
static uint32_t parseSectionAttributes(StringRef S) {
183
0
  uint32_t Ret = 0;
184
0
  for (char C : S.lower()) {
185
0
    switch (C) {
186
0
    case 'd':
187
0
      Ret |= IMAGE_SCN_MEM_DISCARDABLE;
188
0
      break;
189
0
    case 'e':
190
0
      Ret |= IMAGE_SCN_MEM_EXECUTE;
191
0
      break;
192
0
    case 'k':
193
0
      Ret |= IMAGE_SCN_MEM_NOT_CACHED;
194
0
      break;
195
0
    case 'p':
196
0
      Ret |= IMAGE_SCN_MEM_NOT_PAGED;
197
0
      break;
198
0
    case 'r':
199
0
      Ret |= IMAGE_SCN_MEM_READ;
200
0
      break;
201
0
    case 's':
202
0
      Ret |= IMAGE_SCN_MEM_SHARED;
203
0
      break;
204
0
    case 'w':
205
0
      Ret |= IMAGE_SCN_MEM_WRITE;
206
0
      break;
207
0
    default:
208
0
      fatal("/section: invalid argument: " + S);
209
0
    }
210
0
  }
211
0
  return Ret;
212
0
}
213
214
// Parses /section option argument.
215
0
void parseSection(StringRef S) {
216
0
  StringRef Name, Attrs;
217
0
  std::tie(Name, Attrs) = S.split(',');
218
0
  if (Name.empty() || Attrs.empty())
219
0
    fatal("/section: invalid argument: " + S);
220
0
  Config->Section[Name] = parseSectionAttributes(Attrs);
221
0
}
222
223
// Parses /aligncomm option argument.
224
0
void parseAligncomm(StringRef S) {
225
0
  StringRef Name, Align;
226
0
  std::tie(Name, Align) = S.split(',');
227
0
  if (Name.empty() || Align.empty()) {
228
0
    error("/aligncomm: invalid argument: " + S);
229
0
    return;
230
0
  }
231
0
  int V;
232
0
  if (Align.getAsInteger(0, V)) {
233
0
    error("/aligncomm: invalid argument: " + S);
234
0
    return;
235
0
  }
236
0
  Config->AlignComm[Name] = std::max(Config->AlignComm[Name], 1 << V);
237
0
}
238
239
// Parses a string in the form of "EMBED[,=<integer>]|NO".
240
// Results are directly written to Config.
241
0
void parseManifest(StringRef Arg) {
242
0
  if (Arg.equals_lower("no")) {
243
0
    Config->Manifest = Configuration::No;
244
0
    return;
245
0
  }
246
0
  if (!Arg.startswith_lower("embed"))
247
0
    fatal("invalid option " + Arg);
248
0
  Config->Manifest = Configuration::Embed;
249
0
  Arg = Arg.substr(strlen("embed"));
250
0
  if (Arg.empty())
251
0
    return;
252
0
  if (!Arg.startswith_lower(",id="))
253
0
    fatal("invalid option " + Arg);
254
0
  Arg = Arg.substr(strlen(",id="));
255
0
  if (Arg.getAsInteger(0, Config->ManifestID))
256
0
    fatal("invalid option " + Arg);
257
0
}
258
259
// Parses a string in the form of "level=<string>|uiAccess=<string>|NO".
260
// Results are directly written to Config.
261
0
void parseManifestUAC(StringRef Arg) {
262
0
  if (Arg.equals_lower("no")) {
263
0
    Config->ManifestUAC = false;
264
0
    return;
265
0
  }
266
0
  for (;;) {
267
0
    Arg = Arg.ltrim();
268
0
    if (Arg.empty())
269
0
      return;
270
0
    if (Arg.startswith_lower("level=")) {
271
0
      Arg = Arg.substr(strlen("level="));
272
0
      std::tie(Config->ManifestLevel, Arg) = Arg.split(" ");
273
0
      continue;
274
0
    }
275
0
    if (Arg.startswith_lower("uiaccess=")) {
276
0
      Arg = Arg.substr(strlen("uiaccess="));
277
0
      std::tie(Config->ManifestUIAccess, Arg) = Arg.split(" ");
278
0
      continue;
279
0
    }
280
0
    fatal("invalid option " + Arg);
281
0
  }
282
0
}
283
284
// An RAII temporary file class that automatically removes a temporary file.
285
namespace {
286
class TemporaryFile {
287
public:
288
0
  TemporaryFile(StringRef Prefix, StringRef Extn, StringRef Contents = "") {
289
0
    SmallString<128> S;
290
0
    if (auto EC = sys::fs::createTemporaryFile("lld-" + Prefix, Extn, S))
291
0
      fatal("cannot create a temporary file: " + EC.message());
292
0
    Path = S.str();
293
0
294
0
    if (!Contents.empty()) {
295
0
      std::error_code EC;
296
0
      raw_fd_ostream OS(Path, EC, sys::fs::F_None);
297
0
      if (EC)
298
0
        fatal("failed to open " + Path + ": " + EC.message());
299
0
      OS << Contents;
300
0
    }
301
0
  }
302
303
0
  TemporaryFile(TemporaryFile &&Obj) {
304
0
    std::swap(Path, Obj.Path);
305
0
  }
306
307
0
  ~TemporaryFile() {
308
0
    if (Path.empty())
309
0
      return;
310
0
    if (sys::fs::remove(Path))
311
0
      fatal("failed to remove " + Path);
312
0
  }
313
314
  // Returns a memory buffer of this temporary file.
315
  // Note that this function does not leave the file open,
316
  // so it is safe to remove the file immediately after this function
317
  // is called (you cannot remove an opened file on Windows.)
318
0
  std::unique_ptr<MemoryBuffer> getMemoryBuffer() {
319
0
    // IsVolatileSize=true forces MemoryBuffer to not use mmap().
320
0
    return CHECK(MemoryBuffer::getFile(Path, /*FileSize=*/-1,
321
0
                                       /*RequiresNullTerminator=*/false,
322
0
                                       /*IsVolatileSize=*/true),
323
0
                 "could not open " + Path);
324
0
  }
325
326
  std::string Path;
327
};
328
}
329
330
0
static std::string createDefaultXml() {
331
0
  std::string Ret;
332
0
  raw_string_ostream OS(Ret);
333
0
334
0
  // Emit the XML. Note that we do *not* verify that the XML attributes are
335
0
  // syntactically correct. This is intentional for link.exe compatibility.
336
0
  OS << "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
337
0
     << "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n"
338
0
     << "          manifestVersion=\"1.0\">\n";
339
0
  if (Config->ManifestUAC) {
340
0
    OS << "  <trustInfo>\n"
341
0
       << "    <security>\n"
342
0
       << "      <requestedPrivileges>\n"
343
0
       << "         <requestedExecutionLevel level=" << Config->ManifestLevel
344
0
       << " uiAccess=" << Config->ManifestUIAccess << "/>\n"
345
0
       << "      </requestedPrivileges>\n"
346
0
       << "    </security>\n"
347
0
       << "  </trustInfo>\n";
348
0
  }
349
0
  if (!Config->ManifestDependency.empty()) {
350
0
    OS << "  <dependency>\n"
351
0
       << "    <dependentAssembly>\n"
352
0
       << "      <assemblyIdentity " << Config->ManifestDependency << " />\n"
353
0
       << "    </dependentAssembly>\n"
354
0
       << "  </dependency>\n";
355
0
  }
356
0
  OS << "</assembly>\n";
357
0
  return OS.str();
358
0
}
359
360
0
static std::string createManifestXmlWithInternalMt(StringRef DefaultXml) {
361
0
  std::unique_ptr<MemoryBuffer> DefaultXmlCopy =
362
0
      MemoryBuffer::getMemBufferCopy(DefaultXml);
363
0
364
0
  windows_manifest::WindowsManifestMerger Merger;
365
0
  if (auto E = Merger.merge(*DefaultXmlCopy.get()))
366
0
    fatal("internal manifest tool failed on default xml: " +
367
0
          toString(std::move(E)));
368
0
369
0
  for (StringRef Filename : Config->ManifestInput) {
370
0
    std::unique_ptr<MemoryBuffer> Manifest =
371
0
        check(MemoryBuffer::getFile(Filename));
372
0
    if (auto E = Merger.merge(*Manifest.get()))
373
0
      fatal("internal manifest tool failed on file " + Filename + ": " +
374
0
            toString(std::move(E)));
375
0
  }
376
0
377
0
  return Merger.getMergedManifest().get()->getBuffer();
378
0
}
379
380
0
static std::string createManifestXmlWithExternalMt(StringRef DefaultXml) {
381
0
  // Create the default manifest file as a temporary file.
382
0
  TemporaryFile Default("defaultxml", "manifest");
383
0
  std::error_code EC;
384
0
  raw_fd_ostream OS(Default.Path, EC, sys::fs::F_Text);
385
0
  if (EC)
386
0
    fatal("failed to open " + Default.Path + ": " + EC.message());
387
0
  OS << DefaultXml;
388
0
  OS.close();
389
0
390
0
  // Merge user-supplied manifests if they are given.  Since libxml2 is not
391
0
  // enabled, we must shell out to Microsoft's mt.exe tool.
392
0
  TemporaryFile User("user", "manifest");
393
0
394
0
  Executor E("mt.exe");
395
0
  E.add("/manifest");
396
0
  E.add(Default.Path);
397
0
  for (StringRef Filename : Config->ManifestInput) {
398
0
    E.add("/manifest");
399
0
    E.add(Filename);
400
0
  }
401
0
  E.add("/nologo");
402
0
  E.add("/out:" + StringRef(User.Path));
403
0
  E.run();
404
0
405
0
  return CHECK(MemoryBuffer::getFile(User.Path), "could not open " + User.Path)
406
0
      .get()
407
0
      ->getBuffer();
408
0
}
409
410
0
static std::string createManifestXml() {
411
0
  std::string DefaultXml = createDefaultXml();
412
0
  if (Config->ManifestInput.empty())
413
0
    return DefaultXml;
414
0
415
0
  if (windows_manifest::isAvailable())
416
0
    return createManifestXmlWithInternalMt(DefaultXml);
417
0
418
0
  return createManifestXmlWithExternalMt(DefaultXml);
419
0
}
420
421
static std::unique_ptr<WritableMemoryBuffer>
422
0
createMemoryBufferForManifestRes(size_t ManifestSize) {
423
0
  size_t ResSize = alignTo(
424
0
      object::WIN_RES_MAGIC_SIZE + object::WIN_RES_NULL_ENTRY_SIZE +
425
0
          sizeof(object::WinResHeaderPrefix) + sizeof(object::WinResIDs) +
426
0
          sizeof(object::WinResHeaderSuffix) + ManifestSize,
427
0
      object::WIN_RES_DATA_ALIGNMENT);
428
0
  return WritableMemoryBuffer::getNewMemBuffer(ResSize, Config->OutputFile +
429
0
                                                            ".manifest.res");
430
0
}
431
432
0
static void writeResFileHeader(char *&Buf) {
433
0
  memcpy(Buf, COFF::WinResMagic, sizeof(COFF::WinResMagic));
434
0
  Buf += sizeof(COFF::WinResMagic);
435
0
  memset(Buf, 0, object::WIN_RES_NULL_ENTRY_SIZE);
436
0
  Buf += object::WIN_RES_NULL_ENTRY_SIZE;
437
0
}
438
439
0
static void writeResEntryHeader(char *&Buf, size_t ManifestSize) {
440
0
  // Write the prefix.
441
0
  auto *Prefix = reinterpret_cast<object::WinResHeaderPrefix *>(Buf);
442
0
  Prefix->DataSize = ManifestSize;
443
0
  Prefix->HeaderSize = sizeof(object::WinResHeaderPrefix) +
444
0
                       sizeof(object::WinResIDs) +
445
0
                       sizeof(object::WinResHeaderSuffix);
446
0
  Buf += sizeof(object::WinResHeaderPrefix);
447
0
448
0
  // Write the Type/Name IDs.
449
0
  auto *IDs = reinterpret_cast<object::WinResIDs *>(Buf);
450
0
  IDs->setType(RT_MANIFEST);
451
0
  IDs->setName(Config->ManifestID);
452
0
  Buf += sizeof(object::WinResIDs);
453
0
454
0
  // Write the suffix.
455
0
  auto *Suffix = reinterpret_cast<object::WinResHeaderSuffix *>(Buf);
456
0
  Suffix->DataVersion = 0;
457
0
  Suffix->MemoryFlags = object::WIN_RES_PURE_MOVEABLE;
458
0
  Suffix->Language = SUBLANG_ENGLISH_US;
459
0
  Suffix->Version = 0;
460
0
  Suffix->Characteristics = 0;
461
0
  Buf += sizeof(object::WinResHeaderSuffix);
462
0
}
463
464
// Create a resource file containing a manifest XML.
465
0
std::unique_ptr<MemoryBuffer> createManifestRes() {
466
0
  std::string Manifest = createManifestXml();
467
0
468
0
  std::unique_ptr<WritableMemoryBuffer> Res =
469
0
      createMemoryBufferForManifestRes(Manifest.size());
470
0
471
0
  char *Buf = Res->getBufferStart();
472
0
  writeResFileHeader(Buf);
473
0
  writeResEntryHeader(Buf, Manifest.size());
474
0
475
0
  // Copy the manifest data into the .res file.
476
0
  std::copy(Manifest.begin(), Manifest.end(), Buf);
477
0
  return std::move(Res);
478
0
}
479
480
0
void createSideBySideManifest() {
481
0
  std::string Path = Config->ManifestFile;
482
0
  if (Path == "")
483
0
    Path = Config->OutputFile + ".manifest";
484
0
  std::error_code EC;
485
0
  raw_fd_ostream Out(Path, EC, sys::fs::F_Text);
486
0
  if (EC)
487
0
    fatal("failed to create manifest: " + EC.message());
488
0
  Out << createManifestXml();
489
0
}
490
491
// Parse a string in the form of
492
// "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]"
493
// or "<name>=<dllname>.<name>".
494
// Used for parsing /export arguments.
495
0
Export parseExport(StringRef Arg) {
496
0
  Export E;
497
0
  StringRef Rest;
498
0
  std::tie(E.Name, Rest) = Arg.split(",");
499
0
  if (E.Name.empty())
500
0
    goto err;
501
0
502
0
  if (E.Name.contains('=')) {
503
0
    StringRef X, Y;
504
0
    std::tie(X, Y) = E.Name.split("=");
505
0
506
0
    // If "<name>=<dllname>.<name>".
507
0
    if (Y.contains(".")) {
508
0
      E.Name = X;
509
0
      E.ForwardTo = Y;
510
0
      return E;
511
0
    }
512
0
513
0
    E.ExtName = X;
514
0
    E.Name = Y;
515
0
    if (E.Name.empty())
516
0
      goto err;
517
0
  }
518
0
519
0
  // If "<name>=<internalname>[,@ordinal[,NONAME]][,DATA][,PRIVATE]"
520
0
  while (!Rest.empty()) {
521
0
    StringRef Tok;
522
0
    std::tie(Tok, Rest) = Rest.split(",");
523
0
    if (Tok.equals_lower("noname")) {
524
0
      if (E.Ordinal == 0)
525
0
        goto err;
526
0
      E.Noname = true;
527
0
      continue;
528
0
    }
529
0
    if (Tok.equals_lower("data")) {
530
0
      E.Data = true;
531
0
      continue;
532
0
    }
533
0
    if (Tok.equals_lower("constant")) {
534
0
      E.Constant = true;
535
0
      continue;
536
0
    }
537
0
    if (Tok.equals_lower("private")) {
538
0
      E.Private = true;
539
0
      continue;
540
0
    }
541
0
    if (Tok.startswith("@")) {
542
0
      int32_t Ord;
543
0
      if (Tok.substr(1).getAsInteger(0, Ord))
544
0
        goto err;
545
0
      if (Ord <= 0 || 65535 < Ord)
546
0
        goto err;
547
0
      E.Ordinal = Ord;
548
0
      continue;
549
0
    }
550
0
    goto err;
551
0
  }
552
0
  return E;
553
0
554
0
err:
555
0
  fatal("invalid /export: " + Arg);
556
0
}
557
558
0
static StringRef undecorate(StringRef Sym) {
559
0
  if (Config->Machine != I386)
560
0
    return Sym;
561
0
  return Sym.startswith("_") ? Sym.substr(1) : Sym;
562
0
}
563
564
// Performs error checking on all /export arguments.
565
// It also sets ordinals.
566
0
void fixupExports() {
567
0
  // Symbol ordinals must be unique.
568
0
  std::set<uint16_t> Ords;
569
0
  for (Export &E : Config->Exports) {
570
0
    if (E.Ordinal == 0)
571
0
      continue;
572
0
    if (!Ords.insert(E.Ordinal).second)
573
0
      fatal("duplicate export ordinal: " + E.Name);
574
0
  }
575
0
576
0
  for (Export &E : Config->Exports) {
577
0
    Symbol *Sym = E.Sym;
578
0
    if (!E.ForwardTo.empty() || !Sym) {
579
0
      E.SymbolName = E.Name;
580
0
    } else {
581
0
      if (auto *U = dyn_cast<Undefined>(Sym))
582
0
        if (U->WeakAlias)
583
0
          Sym = U->WeakAlias;
584
0
      E.SymbolName = Sym->getName();
585
0
    }
586
0
  }
587
0
588
0
  for (Export &E : Config->Exports) {
589
0
    if (!E.ForwardTo.empty()) {
590
0
      E.ExportName = undecorate(E.Name);
591
0
    } else {
592
0
      E.ExportName = undecorate(E.ExtName.empty() ? E.Name : E.ExtName);
593
0
    }
594
0
  }
595
0
596
0
  // Uniquefy by name.
597
0
  DenseMap<StringRef, Export *> Map(Config->Exports.size());
598
0
  std::vector<Export> V;
599
0
  for (Export &E : Config->Exports) {
600
0
    auto Pair = Map.insert(std::make_pair(E.ExportName, &E));
601
0
    bool Inserted = Pair.second;
602
0
    if (Inserted) {
603
0
      V.push_back(E);
604
0
      continue;
605
0
    }
606
0
    Export *Existing = Pair.first->second;
607
0
    if (E == *Existing || E.Name != Existing->Name)
608
0
      continue;
609
0
    warn("duplicate /export option: " + E.Name);
610
0
  }
611
0
  Config->Exports = std::move(V);
612
0
613
0
  // Sort by name.
614
0
  std::sort(Config->Exports.begin(), Config->Exports.end(),
615
0
            [](const Export &A, const Export &B) {
616
0
              return A.ExportName < B.ExportName;
617
0
            });
618
0
}
619
620
0
void assignExportOrdinals() {
621
0
  // Assign unique ordinals if default (= 0).
622
0
  uint16_t Max = 0;
623
0
  for (Export &E : Config->Exports)
624
0
    Max = std::max(Max, E.Ordinal);
625
0
  for (Export &E : Config->Exports)
626
0
    if (E.Ordinal == 0)
627
0
      E.Ordinal = ++Max;
628
0
}
629
630
// Parses a string in the form of "key=value" and check
631
// if value matches previous values for the same key.
632
0
void checkFailIfMismatch(StringRef Arg) {
633
0
  StringRef K, V;
634
0
  std::tie(K, V) = Arg.split('=');
635
0
  if (K.empty() || V.empty())
636
0
    fatal("/failifmismatch: invalid argument: " + Arg);
637
0
  StringRef Existing = Config->MustMatch[K];
638
0
  if (!Existing.empty() && V != Existing)
639
0
    fatal("/failifmismatch: mismatch detected: " + Existing + " and " + V +
640
0
          " for key " + K);
641
0
  Config->MustMatch[K] = V;
642
0
}
643
644
// Convert Windows resource files (.res files) to a .obj file.
645
0
MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> MBs) {
646
0
  object::WindowsResourceParser Parser;
647
0
648
0
  for (MemoryBufferRef MB : MBs) {
649
0
    std::unique_ptr<object::Binary> Bin = check(object::createBinary(MB));
650
0
    object::WindowsResource *RF = dyn_cast<object::WindowsResource>(Bin.get());
651
0
    if (!RF)
652
0
      fatal("cannot compile non-resource file as resource");
653
0
    if (auto EC = Parser.parse(RF))
654
0
      fatal("failed to parse .res file: " + toString(std::move(EC)));
655
0
  }
656
0
657
0
  Expected<std::unique_ptr<MemoryBuffer>> E =
658
0
      llvm::object::writeWindowsResourceCOFF(Config->Machine, Parser);
659
0
  if (!E)
660
0
    fatal("failed to write .res to COFF: " + toString(E.takeError()));
661
0
662
0
  MemoryBufferRef MBRef = **E;
663
0
  make<std::unique_ptr<MemoryBuffer>>(std::move(*E)); // take ownership
664
0
  return MBRef;
665
0
}
666
667
// Run MSVC link.exe for given in-memory object files.
668
// Command line options are copied from those given to LLD.
669
// This is for the /msvclto option.
670
0
void runMSVCLinker(std::string Rsp, ArrayRef<StringRef> Objects) {
671
0
  // Write the in-memory object files to disk.
672
0
  std::vector<TemporaryFile> Temps;
673
0
  for (StringRef S : Objects) {
674
0
    Temps.emplace_back("lto", "obj", S);
675
0
    Rsp += quote(Temps.back().Path) + "\n";
676
0
  }
677
0
678
0
  log("link.exe " + Rsp);
679
0
680
0
  // Run MSVC link.exe.
681
0
  Temps.emplace_back("lto", "rsp", Rsp);
682
0
  Executor E("link.exe");
683
0
  E.add(Twine("@" + Temps.back().Path));
684
0
  E.run();
685
0
}
686
687
// Create OptTable
688
689
// Create prefix string literals used in Options.td
690
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
691
#include "Options.inc"
692
#undef PREFIX
693
694
// Create table mapping all options defined in Options.td
695
static const llvm::opt::OptTable::Info InfoTable[] = {
696
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
697
  {X1, X2, X10,         X11,         OPT_##ID, llvm::opt::Option::KIND##Class, \
698
   X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
699
#include "Options.inc"
700
#undef OPTION
701
};
702
703
0
COFFOptTable::COFFOptTable() : OptTable(InfoTable, true) {}
704
705
0
static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &Args) {
706
0
  if (auto *Arg = Args.getLastArg(OPT_rsp_quoting)) {
707
0
    StringRef S = Arg->getValue();
708
0
    if (S != "windows" && S != "posix")
709
0
      error("invalid response file quoting: " + S);
710
0
    if (S == "windows")
711
0
      return cl::TokenizeWindowsCommandLine;
712
0
    return cl::TokenizeGNUCommandLine;
713
0
  }
714
0
  // The COFF linker always defaults to Windows quoting.
715
0
  return cl::TokenizeWindowsCommandLine;
716
0
}
717
718
// Parses a given list of options.
719
0
opt::InputArgList ArgParser::parse(ArrayRef<const char *> Argv) {
720
0
  // Make InputArgList from string vectors.
721
0
  unsigned MissingIndex;
722
0
  unsigned MissingCount;
723
0
  SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
724
0
725
0
  // We need to get the quoting style for response files before parsing all
726
0
  // options so we parse here before and ignore all the options but
727
0
  // --rsp-quoting.
728
0
  opt::InputArgList Args = Table.ParseArgs(Vec, MissingIndex, MissingCount);
729
0
730
0
  // Expand response files (arguments in the form of @<filename>)
731
0
  // and then parse the argument again.
732
0
  cl::ExpandResponseFiles(Saver, getQuotingStyle(Args), Vec);
733
0
  Args = Table.ParseArgs(Vec, MissingIndex, MissingCount);
734
0
735
0
  // Print the real command line if response files are expanded.
736
0
  if (Args.hasArg(OPT_verbose) && Argv.size() != Vec.size()) {
737
0
    std::string Msg = "Command line:";
738
0
    for (const char *S : Vec)
739
0
      Msg += " " + std::string(S);
740
0
    message(Msg);
741
0
  }
742
0
743
0
  // Handle /WX early since it converts missing argument warnings to errors.
744
0
  errorHandler().FatalWarnings = Args.hasFlag(OPT_WX, OPT_WX_no, false);
745
0
746
0
  if (MissingCount)
747
0
    fatal(Twine(Args.getArgString(MissingIndex)) + ": missing argument");
748
0
  for (auto *Arg : Args.filtered(OPT_UNKNOWN))
749
0
    warn("ignoring unknown argument: " + Arg->getSpelling());
750
0
  return Args;
751
0
}
752
753
// Tokenizes and parses a given string as command line in .drective section.
754
// /EXPORT options are processed in fastpath.
755
std::pair<opt::InputArgList, std::vector<StringRef>>
756
0
ArgParser::parseDirectives(StringRef S) {
757
0
  std::vector<StringRef> Exports;
758
0
  SmallVector<const char *, 16> Rest;
759
0
760
0
  for (StringRef Tok : tokenize(S)) {
761
0
    if (Tok.startswith_lower("/export:") || Tok.startswith_lower("-export:"))
762
0
      Exports.push_back(Tok.substr(strlen("/export:")));
763
0
    else
764
0
      Rest.push_back(Tok.data());
765
0
  }
766
0
767
0
  // Make InputArgList from unparsed string vectors.
768
0
  unsigned MissingIndex;
769
0
  unsigned MissingCount;
770
0
771
0
  opt::InputArgList Args = Table.ParseArgs(Rest, MissingIndex, MissingCount);
772
0
773
0
  if (MissingCount)
774
0
    fatal(Twine(Args.getArgString(MissingIndex)) + ": missing argument");
775
0
  for (auto *Arg : Args.filtered(OPT_UNKNOWN))
776
0
    warn("ignoring unknown argument: " + Arg->getSpelling());
777
0
  return {std::move(Args), std::move(Exports)};
778
0
}
779
780
// link.exe has an interesting feature. If LINK or _LINK_ environment
781
// variables exist, their contents are handled as command line strings.
782
// So you can pass extra arguments using them.
783
0
opt::InputArgList ArgParser::parseLINK(std::vector<const char *> Argv) {
784
0
  // Concatenate LINK env and command line arguments, and then parse them.
785
0
  if (Optional<std::string> S = Process::GetEnv("LINK")) {
786
0
    std::vector<const char *> V = tokenize(*S);
787
0
    Argv.insert(Argv.begin(), V.begin(), V.end());
788
0
  }
789
0
  if (Optional<std::string> S = Process::GetEnv("_LINK_")) {
790
0
    std::vector<const char *> V = tokenize(*S);
791
0
    Argv.insert(Argv.begin(), V.begin(), V.end());
792
0
  }
793
0
  return parse(Argv);
794
0
}
795
796
0
std::vector<const char *> ArgParser::tokenize(StringRef S) {
797
0
  SmallVector<const char *, 16> Tokens;
798
0
  cl::TokenizeWindowsCommandLine(S, Saver, Tokens);
799
0
  return std::vector<const char *>(Tokens.begin(), Tokens.end());
800
0
}
801
802
0
void printHelp(const char *Argv0) {
803
0
  COFFOptTable().PrintHelp(outs(), Argv0, "LLVM Linker", false);
804
0
}
805
806
} // namespace coff
807
} // namespace lld