xref: /llvm-project-15.0.7/lld/wasm/Driver.cpp (revision e00799ea)
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 
30 #define DEBUG_TYPE "lld"
31 
32 using namespace llvm;
33 using namespace llvm::sys;
34 using namespace llvm::wasm;
35 
36 using namespace lld;
37 using namespace lld::wasm;
38 
39 Configuration *lld::wasm::Config;
40 
41 namespace {
42 
43 // Create enum with OPT_xxx values for each option in Options.td
44 enum {
45   OPT_INVALID = 0,
46 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
47 #include "Options.inc"
48 #undef OPTION
49 };
50 
51 class LinkerDriver {
52 public:
53   void link(ArrayRef<const char *> ArgsArr);
54 
55 private:
56   void createFiles(opt::InputArgList &Args);
57   void addFile(StringRef Path);
58   void addLibrary(StringRef Name);
59   std::vector<InputFile *> Files;
60   llvm::wasm::WasmGlobal StackPointerGlobal;
61 };
62 } // anonymous namespace
63 
64 bool lld::wasm::link(ArrayRef<const char *> Args, bool CanExitEarly,
65                      raw_ostream &Error) {
66   errorHandler().LogName = Args[0];
67   errorHandler().ErrorOS = &Error;
68   errorHandler().ColorDiagnostics = Error.has_colors();
69   errorHandler().ErrorLimitExceededMsg =
70       "too many errors emitted, stopping now (use "
71       "-error-limit=0 to see all errors)";
72 
73   Config = make<Configuration>();
74   Symtab = make<SymbolTable>();
75 
76   LinkerDriver().link(Args);
77 
78   // Exit immediately if we don't need to return to the caller.
79   // This saves time because the overhead of calling destructors
80   // for all globally-allocated objects is not negligible.
81   if (CanExitEarly)
82     exitLld(errorCount() ? 1 : 0);
83 
84   freeArena();
85   return !errorCount();
86 }
87 
88 // Create prefix string literals used in Options.td
89 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
90 #include "Options.inc"
91 #undef PREFIX
92 
93 // Create table mapping all options defined in Options.td
94 static const opt::OptTable::Info OptInfo[] = {
95 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
96   {X1, X2, X10,         X11,         OPT_##ID, opt::Option::KIND##Class,       \
97    X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
98 #include "Options.inc"
99 #undef OPTION
100 };
101 
102 class WasmOptTable : public llvm::opt::OptTable {
103 public:
104   WasmOptTable() : OptTable(OptInfo) {}
105   opt::InputArgList parse(ArrayRef<const char *> Argv);
106 };
107 
108 // Set color diagnostics according to -color-diagnostics={auto,always,never}
109 // or -no-color-diagnostics flags.
110 static void handleColorDiagnostics(opt::InputArgList &Args) {
111   auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
112                               OPT_no_color_diagnostics);
113   if (!Arg)
114     return;
115 
116   if (Arg->getOption().getID() == OPT_color_diagnostics)
117     errorHandler().ColorDiagnostics = true;
118   else if (Arg->getOption().getID() == OPT_no_color_diagnostics)
119     errorHandler().ColorDiagnostics = false;
120   else {
121     StringRef S = Arg->getValue();
122     if (S == "always")
123       errorHandler().ColorDiagnostics = true;
124     if (S == "never")
125       errorHandler().ColorDiagnostics = false;
126     if (S != "auto")
127       error("unknown option: -color-diagnostics=" + S);
128   }
129 }
130 
131 // Find a file by concatenating given paths.
132 static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) {
133   SmallString<128> S;
134   path::append(S, Path1, Path2);
135   if (fs::exists(S))
136     return S.str().str();
137   return None;
138 }
139 
140 opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> Argv) {
141   SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
142 
143   unsigned MissingIndex;
144   unsigned MissingCount;
145   opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
146 
147   handleColorDiagnostics(Args);
148   for (auto *Arg : Args.filtered(OPT_UNKNOWN))
149     error("unknown argument: " + Arg->getSpelling());
150   return Args;
151 }
152 
153 // Currently we allow a ".imports" to live alongside a library. This can
154 // be used to specify a list of symbols which can be undefined at link
155 // time (imported from the environment.  For example libc.a include an
156 // import file that lists the syscall functions it relies on at runtime.
157 // In the long run this information would be better stored as a symbol
158 // attribute/flag in the object file itself.
159 // See: https://github.com/WebAssembly/tool-conventions/issues/35
160 static void readImportFile(StringRef Filename) {
161   if (Optional<MemoryBufferRef> Buf = readFile(Filename))
162     for (StringRef Sym : args::getLines(*Buf))
163       Config->AllowUndefinedSymbols.insert(Sym);
164 }
165 
166 void LinkerDriver::addFile(StringRef Path) {
167   Optional<MemoryBufferRef> Buffer = readFile(Path);
168   if (!Buffer.hasValue())
169     return;
170   MemoryBufferRef MBRef = *Buffer;
171 
172   if (identify_magic(MBRef.getBuffer()) == file_magic::archive) {
173     SmallString<128> ImportFile = Path;
174     path::replace_extension(ImportFile, ".imports");
175     if (fs::exists(ImportFile))
176       readImportFile(ImportFile.str());
177 
178     Files.push_back(make<ArchiveFile>(MBRef));
179     return;
180   }
181 
182   Files.push_back(make<ObjFile>(MBRef));
183 }
184 
185 // Add a given library by searching it from input search paths.
186 void LinkerDriver::addLibrary(StringRef Name) {
187   for (StringRef Dir : Config->SearchPaths) {
188     if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".a")) {
189       addFile(*S);
190       return;
191     }
192   }
193 
194   error("unable to find library -l" + Name);
195 }
196 
197 void LinkerDriver::createFiles(opt::InputArgList &Args) {
198   for (auto *Arg : Args) {
199     switch (Arg->getOption().getUnaliasedOption().getID()) {
200     case OPT_l:
201       addLibrary(Arg->getValue());
202       break;
203     case OPT_INPUT:
204       addFile(Arg->getValue());
205       break;
206     }
207   }
208 }
209 
210 static StringRef getEntry(opt::InputArgList &Args, StringRef Default) {
211   auto *Arg = Args.getLastArg(OPT_entry, OPT_no_entry);
212   if (!Arg)
213     return Default;
214   if (Arg->getOption().getID() == OPT_no_entry)
215     return "";
216   return Arg->getValue();
217 }
218 
219 static const uint8_t UnreachableFn[] = {
220     0x03 /* ULEB length */, 0x00 /* ULEB num locals */,
221     0x00 /* opcode unreachable */, 0x0b /* opcode end */
222 };
223 
224 // For weak undefined functions, there may be "call" instructions that reference
225 // the symbol. In this case, we need to synthesise a dummy/stub function that
226 // will abort at runtime, so that relocations can still provided an operand to
227 // the call instruction that passes Wasm validation.
228 static void handleWeakUndefines() {
229   for (Symbol *Sym : Symtab->getSymbols()) {
230     if (!Sym->isUndefined() || !Sym->isWeak())
231       continue;
232     auto *FuncSym = dyn_cast<FunctionSymbol>(Sym);
233     if (!FuncSym)
234       continue;
235 
236     // It is possible for undefined functions not to have a signature (eg. if
237     // added via "--undefined"), but weak undefined ones do have a signature.
238     assert(FuncSym->getFunctionType());
239     const WasmSignature &Sig = *FuncSym->getFunctionType();
240 
241     // Add a synthetic dummy for weak undefined functions.  These dummies will
242     // be GC'd if not used as the target of any "call" instructions.
243     Optional<std::string> SymName = demangleItanium(Sym->getName());
244     StringRef StubName =
245         Saver.save("undefined function " +
246                    (SymName ? StringRef(*SymName) : Sym->getName()));
247     SyntheticFunction *Func = make<SyntheticFunction>(Sig, StubName);
248     Func->setBody(UnreachableFn);
249     // Ensure it compares equal to the null pointer, and so that table relocs
250     // don't pull in the stub body (only call-operand relocs should do that).
251     Func->setTableIndex(0);
252     Symtab->SyntheticFunctions.emplace_back(Func);
253     // Hide our dummy to prevent export.
254     uint32_t Flags = WASM_SYMBOL_VISIBILITY_HIDDEN;
255     replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Flags, nullptr, Func);
256   }
257 }
258 
259 void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
260   WasmOptTable Parser;
261   opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
262 
263   // Handle --help
264   if (Args.hasArg(OPT_help)) {
265     Parser.PrintHelp(outs(), ArgsArr[0], "LLVM Linker", false);
266     return;
267   }
268 
269   // Handle --version
270   if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) {
271     outs() << getLLDVersion() << "\n";
272     return;
273   }
274 
275   // Parse and evaluate -mllvm options.
276   std::vector<const char *> V;
277   V.push_back("wasm-ld (LLVM option parsing)");
278   for (auto *Arg : Args.filtered(OPT_mllvm))
279     V.push_back(Arg->getValue());
280   cl::ParseCommandLineOptions(V.size(), V.data());
281 
282   errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
283 
284   Config->AllowUndefined = Args.hasArg(OPT_allow_undefined);
285   Config->CheckSignatures =
286       Args.hasFlag(OPT_check_signatures, OPT_no_check_signatures, false);
287   Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
288   Config->Entry = getEntry(Args, Args.hasArg(OPT_relocatable) ? "" : "_start");
289   Config->ExportTable = Args.hasArg(OPT_export_table);
290   Config->ImportMemory = Args.hasArg(OPT_import_memory);
291   Config->ImportTable = Args.hasArg(OPT_import_table);
292   Config->OutputFile = Args.getLastArgValue(OPT_o);
293   Config->Relocatable = Args.hasArg(OPT_relocatable);
294   Config->GcSections =
295       Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !Config->Relocatable);
296   Config->PrintGcSections =
297       Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
298   Config->SearchPaths = args::getStrings(Args, OPT_L);
299   Config->StripAll = Args.hasArg(OPT_strip_all);
300   Config->StripDebug = Args.hasArg(OPT_strip_debug);
301   errorHandler().Verbose = Args.hasArg(OPT_verbose);
302   ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true);
303 
304   Config->InitialMemory = args::getInteger(Args, OPT_initial_memory, 0);
305   Config->GlobalBase = args::getInteger(Args, OPT_global_base, 1024);
306   Config->MaxMemory = args::getInteger(Args, OPT_max_memory, 0);
307   Config->ZStackSize =
308       args::getZOptionValue(Args, OPT_z, "stack-size", WasmPageSize);
309 
310   if (auto *Arg = Args.getLastArg(OPT_allow_undefined_file))
311     readImportFile(Arg->getValue());
312 
313   if (!Args.hasArg(OPT_INPUT)) {
314     error("no input files");
315     return;
316   }
317 
318   if (Config->OutputFile.empty())
319     error("no output file specified");
320 
321   if (Config->ImportTable && Config->ExportTable)
322     error("--import-table and --export-table may not be used together");
323 
324   if (Config->Relocatable) {
325     if (!Config->Entry.empty())
326       error("entry point specified for relocatable output file");
327     if (Config->GcSections)
328       error("-r and --gc-sections may not be used together");
329     if (Args.hasArg(OPT_undefined))
330       error("-r -and --undefined may not be used together");
331   }
332 
333   Symbol *EntrySym = nullptr;
334   if (!Config->Relocatable) {
335     // Can't export the SP right now because it's mutable, and mutable
336     // globals aren't yet supported in the official binary format.
337     // TODO(sbc): Remove WASM_SYMBOL_VISIBILITY_HIDDEN if/when the
338     // "mutable global" proposal is accepted.
339     StackPointerGlobal.Type = {WASM_TYPE_I32, true};
340     StackPointerGlobal.InitExpr.Value.Int32 = 0;
341     StackPointerGlobal.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
342     InputGlobal *StackPointer = make<InputGlobal>(StackPointerGlobal);
343     StackPointer->Live = true;
344 
345     static WasmSignature NullSignature = {{}, WASM_TYPE_NORESULT};
346 
347     // Add synthetic symbols before any others
348     WasmSym::CallCtors = Symtab->addSyntheticFunction(
349         "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
350         make<SyntheticFunction>(NullSignature, "__wasm_call_ctors"));
351     WasmSym::StackPointer = Symtab->addSyntheticGlobal(
352         "__stack_pointer", WASM_SYMBOL_VISIBILITY_HIDDEN, StackPointer);
353     WasmSym::HeapBase = Symtab->addSyntheticDataSymbol("__heap_base", 0);
354     WasmSym::DsoHandle = Symtab->addSyntheticDataSymbol(
355         "__dso_handle", WASM_SYMBOL_VISIBILITY_HIDDEN);
356     WasmSym::DataEnd = Symtab->addSyntheticDataSymbol("__data_end", 0);
357 
358     if (!Config->Entry.empty())
359       EntrySym = Symtab->addUndefinedFunction(Config->Entry, 0, nullptr,
360                                               &NullSignature);
361 
362     // Handle the `--undefined <sym>` options.
363     for (auto *Arg : Args.filtered(OPT_undefined))
364       Symtab->addUndefinedFunction(Arg->getValue(), 0, nullptr, nullptr);
365   }
366 
367   createFiles(Args);
368   if (errorCount())
369     return;
370 
371   // Add all files to the symbol table. This will add almost all
372   // symbols that we need to the symbol table.
373   for (InputFile *F : Files)
374     Symtab->addFile(F);
375 
376   // Add synthetic dummies for weak undefined functions.
377   if (!Config->Relocatable)
378     handleWeakUndefines();
379 
380   // Make sure we have resolved all symbols.
381   if (!Config->Relocatable && !Config->AllowUndefined) {
382     Symtab->reportRemainingUndefines();
383   } else {
384     // When we allow undefined symbols we cannot include those defined in
385     // -u/--undefined since these undefined symbols have only names and no
386     // function signature, which means they cannot be written to the final
387     // output.
388     for (auto *Arg : Args.filtered(OPT_undefined)) {
389       Symbol *Sym = Symtab->find(Arg->getValue());
390       if (!Sym->isDefined())
391         error("function forced with --undefined not found: " + Sym->getName());
392     }
393   }
394   if (errorCount())
395     return;
396 
397   // Handle --export.
398   for (auto *Arg : Args.filtered(OPT_export)) {
399     StringRef Name = Arg->getValue();
400     Symbol *Sym = Symtab->find(Name);
401     if (Sym && Sym->isDefined())
402       Sym->setHidden(false);
403     else if (!Config->AllowUndefined)
404       error("symbol exported via --export not found: " + Name);
405   }
406 
407   if (EntrySym)
408     EntrySym->setHidden(false);
409 
410   if (errorCount())
411     return;
412 
413   // Do size optimizations: garbage collection
414   markLive();
415 
416   // Write the result to the file.
417   writeResult();
418 }
419