Coverage Report

Created: 2018-06-25 02:00

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/Driver.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Driver.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
#include "lld/Common/Driver.h"
11
#include "Config.h"
12
#include "InputChunks.h"
13
#include "InputGlobal.h"
14
#include "MarkLive.h"
15
#include "SymbolTable.h"
16
#include "Writer.h"
17
#include "lld/Common/Args.h"
18
#include "lld/Common/ErrorHandler.h"
19
#include "lld/Common/Memory.h"
20
#include "lld/Common/Strings.h"
21
#include "lld/Common/Threads.h"
22
#include "lld/Common/Version.h"
23
#include "llvm/ADT/Twine.h"
24
#include "llvm/Object/Wasm.h"
25
#include "llvm/Option/ArgList.h"
26
#include "llvm/Support/CommandLine.h"
27
#include "llvm/Support/Path.h"
28
#include "llvm/Support/Process.h"
29
#include "llvm/Support/TargetSelect.h"
30
31
#define DEBUG_TYPE "lld"
32
33
using namespace llvm;
34
using namespace llvm::sys;
35
using namespace llvm::wasm;
36
37
using namespace lld;
38
using namespace lld::wasm;
39
40
Configuration *lld::wasm::Config;
41
42
namespace {
43
44
// Create enum with OPT_xxx values for each option in Options.td
45
enum {
46
  OPT_INVALID = 0,
47
#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
48
#include "Options.inc"
49
#undef OPTION
50
};
51
52
// This function is called on startup. We need this for LTO since
53
// LTO calls LLVM functions to compile bitcode files to native code.
54
// Technically this can be delayed until we read bitcode files, but
55
// we don't bother to do lazily because the initialization is fast.
56
0
static void initLLVM() {
57
0
  InitializeAllTargets();
58
0
  InitializeAllTargetMCs();
59
0
  InitializeAllAsmPrinters();
60
0
  InitializeAllAsmParsers();
61
0
}
62
63
class LinkerDriver {
64
public:
65
  void link(ArrayRef<const char *> ArgsArr);
66
67
private:
68
  void createFiles(opt::InputArgList &Args);
69
  void addFile(StringRef Path);
70
  void addLibrary(StringRef Name);
71
  std::vector<InputFile *> Files;
72
};
73
} // anonymous namespace
74
75
bool lld::wasm::link(ArrayRef<const char *> Args, bool CanExitEarly,
76
0
                     raw_ostream &Error) {
77
0
  errorHandler().LogName = Args[0];
78
0
  errorHandler().ErrorOS = &Error;
79
0
  errorHandler().ColorDiagnostics = Error.has_colors();
80
0
  errorHandler().ErrorLimitExceededMsg =
81
0
      "too many errors emitted, stopping now (use "
82
0
      "-error-limit=0 to see all errors)";
83
0
84
0
  Config = make<Configuration>();
85
0
  Symtab = make<SymbolTable>();
86
0
87
0
  initLLVM();
88
0
  LinkerDriver().link(Args);
89
0
90
0
  // Exit immediately if we don't need to return to the caller.
91
0
  // This saves time because the overhead of calling destructors
92
0
  // for all globally-allocated objects is not negligible.
93
0
  if (CanExitEarly)
94
0
    exitLld(errorCount() ? 1 : 0);
95
0
96
0
  freeArena();
97
0
  return !errorCount();
98
0
}
99
100
// Create prefix string literals used in Options.td
101
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
102
#include "Options.inc"
103
#undef PREFIX
104
105
// Create table mapping all options defined in Options.td
106
static const opt::OptTable::Info OptInfo[] = {
107
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
108
  {X1, X2, X10,         X11,         OPT_##ID, opt::Option::KIND##Class,       \
109
   X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
110
#include "Options.inc"
111
#undef OPTION
112
};
113
114
namespace {
115
class WasmOptTable : public llvm::opt::OptTable {
116
public:
117
0
  WasmOptTable() : OptTable(OptInfo) {}
118
  opt::InputArgList parse(ArrayRef<const char *> Argv);
119
};
120
} // namespace
121
122
// Set color diagnostics according to -color-diagnostics={auto,always,never}
123
// or -no-color-diagnostics flags.
124
0
static void handleColorDiagnostics(opt::InputArgList &Args) {
125
0
  auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
126
0
                              OPT_no_color_diagnostics);
127
0
  if (!Arg)
128
0
    return;
129
0
  if (Arg->getOption().getID() == OPT_color_diagnostics) {
130
0
    errorHandler().ColorDiagnostics = true;
131
0
  } else if (Arg->getOption().getID() == OPT_no_color_diagnostics) {
132
0
    errorHandler().ColorDiagnostics = false;
133
0
  } else {
134
0
    StringRef S = Arg->getValue();
135
0
    if (S == "always")
136
0
      errorHandler().ColorDiagnostics = true;
137
0
    else if (S == "never")
138
0
      errorHandler().ColorDiagnostics = false;
139
0
    else if (S != "auto")
140
0
      error("unknown option: --color-diagnostics=" + S);
141
0
  }
142
0
}
143
144
// Find a file by concatenating given paths.
145
static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) {
146
  SmallString<128> S;
147
  path::append(S, Path1, Path2);
148
  if (fs::exists(S))
149
    return S.str().str();
150
  return None;
151
}
152
153
0
opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> Argv) {
154
0
  SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
155
0
156
0
  unsigned MissingIndex;
157
0
  unsigned MissingCount;
158
0
159
0
  // Expand response files (arguments in the form of @<filename>)
160
0
  cl::ExpandResponseFiles(Saver, cl::TokenizeGNUCommandLine, Vec);
161
0
162
0
  opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
163
0
164
0
  handleColorDiagnostics(Args);
165
0
  for (auto *Arg : Args.filtered(OPT_UNKNOWN))
166
0
    error("unknown argument: " + Arg->getSpelling());
167
0
  return Args;
168
0
}
169
170
// Currently we allow a ".imports" to live alongside a library. This can
171
// be used to specify a list of symbols which can be undefined at link
172
// time (imported from the environment.  For example libc.a include an
173
// import file that lists the syscall functions it relies on at runtime.
174
// In the long run this information would be better stored as a symbol
175
// attribute/flag in the object file itself.
176
// See: https://github.com/WebAssembly/tool-conventions/issues/35
177
0
static void readImportFile(StringRef Filename) {
178
0
  if (Optional<MemoryBufferRef> Buf = readFile(Filename))
179
0
    for (StringRef Sym : args::getLines(*Buf))
180
0
      Config->AllowUndefinedSymbols.insert(Sym);
181
0
}
182
183
0
void LinkerDriver::addFile(StringRef Path) {
184
0
  Optional<MemoryBufferRef> Buffer = readFile(Path);
185
0
  if (!Buffer.hasValue())
186
0
    return;
187
0
  MemoryBufferRef MBRef = *Buffer;
188
0
189
0
  switch (identify_magic(MBRef.getBuffer())) {
190
0
  case file_magic::archive: {
191
0
    SmallString<128> ImportFile = Path;
192
0
    path::replace_extension(ImportFile, ".imports");
193
0
    if (fs::exists(ImportFile))
194
0
      readImportFile(ImportFile.str());
195
0
196
0
    Files.push_back(make<ArchiveFile>(MBRef));
197
0
    return;
198
0
  }
199
0
  case file_magic::bitcode:
200
0
    Files.push_back(make<BitcodeFile>(MBRef));
201
0
    break;
202
0
  default:
203
0
    Files.push_back(make<ObjFile>(MBRef));
204
0
  }
205
0
}
206
207
// Add a given library by searching it from input search paths.
208
0
void LinkerDriver::addLibrary(StringRef Name) {
209
0
  for (StringRef Dir : Config->SearchPaths) {
210
0
    if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".a")) {
211
0
      addFile(*S);
212
0
      return;
213
0
    }
214
0
  }
215
0
216
0
  error("unable to find library -l" + Name);
217
0
}
218
219
0
void LinkerDriver::createFiles(opt::InputArgList &Args) {
220
0
  for (auto *Arg : Args) {
221
0
    switch (Arg->getOption().getUnaliasedOption().getID()) {
222
0
    case OPT_l:
223
0
      addLibrary(Arg->getValue());
224
0
      break;
225
0
    case OPT_INPUT:
226
0
      addFile(Arg->getValue());
227
0
      break;
228
0
    }
229
0
  }
230
0
}
231
232
0
static StringRef getEntry(opt::InputArgList &Args, StringRef Default) {
233
0
  auto *Arg = Args.getLastArg(OPT_entry, OPT_no_entry);
234
0
  if (!Arg)
235
0
    return Default;
236
0
  if (Arg->getOption().getID() == OPT_no_entry)
237
0
    return "";
238
0
  return Arg->getValue();
239
0
}
240
241
static const uint8_t UnreachableFn[] = {
242
    0x03 /* ULEB length */, 0x00 /* ULEB num locals */,
243
    0x00 /* opcode unreachable */, 0x0b /* opcode end */
244
};
245
246
// For weak undefined functions, there may be "call" instructions that reference
247
// the symbol. In this case, we need to synthesise a dummy/stub function that
248
// will abort at runtime, so that relocations can still provided an operand to
249
// the call instruction that passes Wasm validation.
250
0
static void handleWeakUndefines() {
251
0
  for (Symbol *Sym : Symtab->getSymbols()) {
252
0
    if (!Sym->isUndefined() || !Sym->isWeak())
253
0
      continue;
254
0
    auto *FuncSym = dyn_cast<FunctionSymbol>(Sym);
255
0
    if (!FuncSym)
256
0
      continue;
257
0
258
0
    // It is possible for undefined functions not to have a signature (eg. if
259
0
    // added via "--undefined"), but weak undefined ones do have a signature.
260
0
    assert(FuncSym->getFunctionType());
261
0
    const WasmSignature &Sig = *FuncSym->getFunctionType();
262
0
263
0
    // Add a synthetic dummy for weak undefined functions.  These dummies will
264
0
    // be GC'd if not used as the target of any "call" instructions.
265
0
    Optional<std::string> SymName = demangleItanium(Sym->getName());
266
0
    StringRef DebugName =
267
0
        Saver.save("undefined function " +
268
0
                   (SymName ? StringRef(*SymName) : Sym->getName()));
269
0
    SyntheticFunction *Func =
270
0
        make<SyntheticFunction>(Sig, Sym->getName(), DebugName);
271
0
    Func->setBody(UnreachableFn);
272
0
    // Ensure it compares equal to the null pointer, and so that table relocs
273
0
    // don't pull in the stub body (only call-operand relocs should do that).
274
0
    Func->setTableIndex(0);
275
0
    Symtab->SyntheticFunctions.emplace_back(Func);
276
0
    // Hide our dummy to prevent export.
277
0
    uint32_t Flags = WASM_SYMBOL_VISIBILITY_HIDDEN;
278
0
    replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Flags, nullptr, Func);
279
0
  }
280
0
}
281
282
// Force Sym to be entered in the output. Used for -u or equivalent.
283
0
static Symbol *addUndefined(StringRef Name) {
284
0
  Symbol *S = Symtab->addUndefinedFunction(Name, 0, nullptr, nullptr);
285
0
286
0
  // Since symbol S may not be used inside the program, LTO may
287
0
  // eliminate it. Mark the symbol as "used" to prevent it.
288
0
  S->IsUsedInRegularObj = true;
289
0
290
0
  return S;
291
0
}
292
293
0
void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
294
0
  WasmOptTable Parser;
295
0
  opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
296
0
297
0
  // Handle --help
298
0
  if (Args.hasArg(OPT_help)) {
299
0
    Parser.PrintHelp(outs(), ArgsArr[0], "LLVM Linker", false);
300
0
    return;
301
0
  }
302
0
303
0
  // Handle --version
304
0
  if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) {
305
0
    outs() << getLLDVersion() << "\n";
306
0
    return;
307
0
  }
308
0
309
0
  // Parse and evaluate -mllvm options.
310
0
  std::vector<const char *> V;
311
0
  V.push_back("wasm-ld (LLVM option parsing)");
312
0
  for (auto *Arg : Args.filtered(OPT_mllvm))
313
0
    V.push_back(Arg->getValue());
314
0
  cl::ParseCommandLineOptions(V.size(), V.data());
315
0
316
0
  errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
317
0
318
0
  Config->AllowUndefined = Args.hasArg(OPT_allow_undefined);
319
0
  Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
320
0
  Config->DisableVerify = Args.hasArg(OPT_disable_verify);
321
0
  Config->Entry = getEntry(Args, Args.hasArg(OPT_relocatable) ? "" : "_start");
322
0
  Config->ExportAll = Args.hasArg(OPT_export_all);
323
0
  Config->ExportTable = Args.hasArg(OPT_export_table);
324
0
  errorHandler().FatalWarnings =
325
0
      Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
326
0
  Config->ImportMemory = Args.hasArg(OPT_import_memory);
327
0
  Config->ImportTable = Args.hasArg(OPT_import_table);
328
0
  Config->LTOO = args::getInteger(Args, OPT_lto_O, 2);
329
0
  Config->LTOPartitions = args::getInteger(Args, OPT_lto_partitions, 1);
330
0
  Config->Optimize = args::getInteger(Args, OPT_O, 0);
331
0
  Config->OutputFile = Args.getLastArgValue(OPT_o);
332
0
  Config->Relocatable = Args.hasArg(OPT_relocatable);
333
0
  Config->GcSections =
334
0
      Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !Config->Relocatable);
335
0
  Config->MergeDataSegments =
336
0
      Args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
337
0
                   !Config->Relocatable);
338
0
  Config->PrintGcSections =
339
0
      Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
340
0
  Config->SaveTemps = Args.hasArg(OPT_save_temps);
341
0
  Config->SearchPaths = args::getStrings(Args, OPT_L);
342
0
  Config->StripAll = Args.hasArg(OPT_strip_all);
343
0
  Config->StripDebug = Args.hasArg(OPT_strip_debug);
344
0
  Config->StackFirst = Args.hasArg(OPT_stack_first);
345
0
  Config->ThinLTOCacheDir = Args.getLastArgValue(OPT_thinlto_cache_dir);
346
0
  Config->ThinLTOCachePolicy = CHECK(
347
0
      parseCachePruningPolicy(Args.getLastArgValue(OPT_thinlto_cache_policy)),
348
0
      "--thinlto-cache-policy: invalid cache policy");
349
0
  Config->ThinLTOJobs = args::getInteger(Args, OPT_thinlto_jobs, -1u);
350
0
  errorHandler().Verbose = Args.hasArg(OPT_verbose);
351
0
  ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true);
352
0
353
0
  Config->InitialMemory = args::getInteger(Args, OPT_initial_memory, 0);
354
0
  Config->GlobalBase = args::getInteger(Args, OPT_global_base, 1024);
355
0
  Config->MaxMemory = args::getInteger(Args, OPT_max_memory, 0);
356
0
  Config->ZStackSize =
357
0
      args::getZOptionValue(Args, OPT_z, "stack-size", WasmPageSize);
358
0
359
0
  Config->CompressRelocTargets = Config->Optimize > 0 && !Config->Relocatable;
360
0
361
0
  if (Config->LTOO > 3)
362
0
    error("invalid optimization level for LTO: " + Twine(Config->LTOO));
363
0
  if (Config->LTOPartitions == 0)
364
0
    error("--lto-partitions: number of threads must be > 0");
365
0
  if (Config->ThinLTOJobs == 0)
366
0
    error("--thinlto-jobs: number of threads must be > 0");
367
0
368
0
  if (auto *Arg = Args.getLastArg(OPT_allow_undefined_file))
369
0
    readImportFile(Arg->getValue());
370
0
371
0
  if (!Args.hasArg(OPT_INPUT)) {
372
0
    error("no input files");
373
0
    return;
374
0
  }
375
0
376
0
  if (Config->OutputFile.empty())
377
0
    error("no output file specified");
378
0
379
0
  if (Config->ImportTable && Config->ExportTable)
380
0
    error("--import-table and --export-table may not be used together");
381
0
382
0
  if (Config->Relocatable) {
383
0
    if (!Config->Entry.empty())
384
0
      error("entry point specified for relocatable output file");
385
0
    if (Config->GcSections)
386
0
      error("-r and --gc-sections may not be used together");
387
0
    if (Args.hasArg(OPT_undefined))
388
0
      error("-r -and --undefined may not be used together");
389
0
  }
390
0
391
0
  Symbol *EntrySym = nullptr;
392
0
  if (!Config->Relocatable) {
393
0
    llvm::wasm::WasmGlobal Global;
394
0
    Global.Type = {WASM_TYPE_I32, true};
395
0
    Global.InitExpr.Value.Int32 = 0;
396
0
    Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
397
0
    Global.SymbolName = "__stack_pointer";
398
0
    InputGlobal *StackPointer = make<InputGlobal>(Global, nullptr);
399
0
    StackPointer->Live = true;
400
0
401
0
    static WasmSignature NullSignature = {{}, WASM_TYPE_NORESULT};
402
0
403
0
    // Add synthetic symbols before any others
404
0
    WasmSym::CallCtors = Symtab->addSyntheticFunction(
405
0
        "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
406
0
        make<SyntheticFunction>(NullSignature, "__wasm_call_ctors"));
407
0
    // TODO(sbc): Remove WASM_SYMBOL_VISIBILITY_HIDDEN when the mutable global
408
0
    // spec proposal is implemented in all major browsers.
409
0
    // See: https://github.com/WebAssembly/mutable-global
410
0
    WasmSym::StackPointer = Symtab->addSyntheticGlobal(
411
0
        "__stack_pointer", WASM_SYMBOL_VISIBILITY_HIDDEN, StackPointer);
412
0
    WasmSym::HeapBase = Symtab->addSyntheticDataSymbol("__heap_base", 0);
413
0
    WasmSym::DsoHandle = Symtab->addSyntheticDataSymbol(
414
0
        "__dso_handle", WASM_SYMBOL_VISIBILITY_HIDDEN);
415
0
    WasmSym::DataEnd = Symtab->addSyntheticDataSymbol("__data_end", 0);
416
0
417
0
    // For now, since we don't actually use the start function as the
418
0
    // wasm start symbol, we don't need to care about it signature.
419
0
    if (!Config->Entry.empty())
420
0
      EntrySym = addUndefined(Config->Entry);
421
0
422
0
    // Handle the `--undefined <sym>` options.
423
0
    for (auto *Arg : Args.filtered(OPT_undefined))
424
0
      addUndefined(Arg->getValue());
425
0
  }
426
0
427
0
  createFiles(Args);
428
0
  if (errorCount())
429
0
    return;
430
0
431
0
  // Add all files to the symbol table. This will add almost all
432
0
  // symbols that we need to the symbol table.
433
0
  for (InputFile *F : Files)
434
0
    Symtab->addFile(F);
435
0
  if (errorCount())
436
0
    return;
437
0
438
0
  // Add synthetic dummies for weak undefined functions.
439
0
  if (!Config->Relocatable)
440
0
    handleWeakUndefines();
441
0
442
0
  // Do link-time optimization if given files are LLVM bitcode files.
443
0
  // This compiles bitcode files into real object files.
444
0
  Symtab->addCombinedLTOObject();
445
0
  if (errorCount())
446
0
    return;
447
0
448
0
  // Make sure we have resolved all symbols.
449
0
  if (!Config->Relocatable && !Config->AllowUndefined) {
450
0
    Symtab->reportRemainingUndefines();
451
0
  } else {
452
0
    // Even when using --allow-undefined we still want to report the absence of
453
0
    // our initial set of undefined symbols (i.e. the entry point and symbols
454
0
    // specified via --undefined).
455
0
    // Part of the reason for this is that these function don't have signatures
456
0
    // so which means they cannot be written as wasm function imports.
457
0
    for (auto *Arg : Args.filtered(OPT_undefined)) {
458
0
      Symbol *Sym = Symtab->find(Arg->getValue());
459
0
      if (!Sym->isDefined())
460
0
        error("symbol forced with --undefined not found: " + Sym->getName());
461
0
    }
462
0
    if (EntrySym && !EntrySym->isDefined())
463
0
      error("entry symbol not defined (pass --no-entry to supress): " +
464
0
            EntrySym->getName());
465
0
  }
466
0
  if (errorCount())
467
0
    return;
468
0
469
0
  // Handle --export.
470
0
  for (auto *Arg : Args.filtered(OPT_export)) {
471
0
    StringRef Name = Arg->getValue();
472
0
    Symbol *Sym = Symtab->find(Name);
473
0
    if (Sym && Sym->isDefined())
474
0
      Sym->setHidden(false);
475
0
    else if (!Config->AllowUndefined)
476
0
      error("symbol exported via --export not found: " + Name);
477
0
  }
478
0
479
0
  if (EntrySym)
480
0
    EntrySym->setHidden(false);
481
0
482
0
  if (errorCount())
483
0
    return;
484
0
485
0
  // Do size optimizations: garbage collection
486
0
  markLive();
487
0
488
0
  // Write the result to the file.
489
0
  writeResult();
490
0
}