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