xref: /llvm-project-15.0.7/lld/wasm/Writer.cpp (revision bbef51eb)
1 //===- Writer.cpp ---------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Writer.h"
10 #include "Config.h"
11 #include "InputChunks.h"
12 #include "InputElement.h"
13 #include "MapFile.h"
14 #include "OutputSections.h"
15 #include "OutputSegment.h"
16 #include "Relocations.h"
17 #include "SymbolTable.h"
18 #include "SyntheticSections.h"
19 #include "WriterUtils.h"
20 #include "lld/Common/ErrorHandler.h"
21 #include "lld/Common/Memory.h"
22 #include "lld/Common/Strings.h"
23 #include "llvm/ADT/DenseSet.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringMap.h"
27 #include "llvm/BinaryFormat/Wasm.h"
28 #include "llvm/BinaryFormat/WasmTraits.h"
29 #include "llvm/Support/FileOutputBuffer.h"
30 #include "llvm/Support/Format.h"
31 #include "llvm/Support/FormatVariadic.h"
32 #include "llvm/Support/LEB128.h"
33 #include "llvm/Support/Parallel.h"
34 
35 #include <cstdarg>
36 #include <map>
37 
38 #define DEBUG_TYPE "lld"
39 
40 using namespace llvm;
41 using namespace llvm::wasm;
42 
43 namespace lld {
44 namespace wasm {
45 static constexpr int stackAlignment = 16;
46 static constexpr int heapAlignment = 16;
47 
48 namespace {
49 
50 // The writer writes a SymbolTable result to a file.
51 class Writer {
52 public:
53   void run();
54 
55 private:
56   void openFile();
57 
58   bool needsPassiveInitialization(const OutputSegment *segment);
59   bool hasPassiveInitializedSegments();
60 
61   void createSyntheticInitFunctions();
62   void createInitMemoryFunction();
63   void createStartFunction();
64   void createApplyDataRelocationsFunction();
65   void createApplyGlobalRelocationsFunction();
66   void createApplyGlobalTLSRelocationsFunction();
67   void createCallCtorsFunction();
68   void createInitTLSFunction();
69   void createCommandExportWrappers();
70   void createCommandExportWrapper(uint32_t functionIndex, DefinedFunction *f);
71 
72   void assignIndexes();
73   void populateSymtab();
74   void populateProducers();
75   void populateTargetFeatures();
76   // populateTargetFeatures happens early on so some checks are delayed
77   // until imports and exports are finalized.  There are run unstead
78   // in checkImportExportTargetFeatures
79   void checkImportExportTargetFeatures();
80   void calculateInitFunctions();
81   void calculateImports();
82   void calculateExports();
83   void calculateCustomSections();
84   void calculateTypes();
85   void createOutputSegments();
86   OutputSegment *createOutputSegment(StringRef name);
87   void combineOutputSegments();
88   void layoutMemory();
89   void createHeader();
90 
91   void addSection(OutputSection *sec);
92 
93   void addSections();
94 
95   void createCustomSections();
96   void createSyntheticSections();
97   void createSyntheticSectionsPostLayout();
98   void finalizeSections();
99 
100   // Custom sections
101   void createRelocSections();
102 
103   void writeHeader();
104   void writeSections();
105 
106   uint64_t fileSize = 0;
107 
108   std::vector<WasmInitEntry> initFunctions;
109   llvm::StringMap<std::vector<InputChunk *>> customSectionMapping;
110 
111   // Stable storage for command export wrapper function name strings.
112   std::list<std::string> commandExportWrapperNames;
113 
114   // Elements that are used to construct the final output
115   std::string header;
116   std::vector<OutputSection *> outputSections;
117 
118   std::unique_ptr<FileOutputBuffer> buffer;
119 
120   std::vector<OutputSegment *> segments;
121   llvm::SmallDenseMap<StringRef, OutputSegment *> segmentMap;
122 };
123 
124 } // anonymous namespace
125 
126 void Writer::calculateCustomSections() {
127   log("calculateCustomSections");
128   bool stripDebug = config->stripDebug || config->stripAll;
129   for (ObjFile *file : symtab->objectFiles) {
130     for (InputChunk *section : file->customSections) {
131       // Exclude COMDAT sections that are not selected for inclusion
132       if (section->discarded)
133         continue;
134       StringRef name = section->getName();
135       // These custom sections are known the linker and synthesized rather than
136       // blindly copied.
137       if (name == "linking" || name == "name" || name == "producers" ||
138           name == "target_features" || name.startswith("reloc."))
139         continue;
140       // These custom sections are generated by `clang -fembed-bitcode`.
141       // These are used by the rust toolchain to ship LTO data along with
142       // compiled object code, but they don't want this included in the linker
143       // output.
144       if (name == ".llvmbc" || name == ".llvmcmd")
145         continue;
146       // Strip debug section in that option was specified.
147       if (stripDebug && name.startswith(".debug_"))
148         continue;
149       // Otherwise include custom sections by default and concatenate their
150       // contents.
151       customSectionMapping[name].push_back(section);
152     }
153   }
154 }
155 
156 void Writer::createCustomSections() {
157   log("createCustomSections");
158   for (auto &pair : customSectionMapping) {
159     StringRef name = pair.first();
160     LLVM_DEBUG(dbgs() << "createCustomSection: " << name << "\n");
161 
162     OutputSection *sec = make<CustomSection>(std::string(name), pair.second);
163     if (config->relocatable || config->emitRelocs) {
164       auto *sym = make<OutputSectionSymbol>(sec);
165       out.linkingSec->addToSymtab(sym);
166       sec->sectionSym = sym;
167     }
168     addSection(sec);
169   }
170 }
171 
172 // Create relocations sections in the final output.
173 // These are only created when relocatable output is requested.
174 void Writer::createRelocSections() {
175   log("createRelocSections");
176   // Don't use iterator here since we are adding to OutputSection
177   size_t origSize = outputSections.size();
178   for (size_t i = 0; i < origSize; i++) {
179     LLVM_DEBUG(dbgs() << "check section " << i << "\n");
180     OutputSection *sec = outputSections[i];
181 
182     // Count the number of needed sections.
183     uint32_t count = sec->getNumRelocations();
184     if (!count)
185       continue;
186 
187     StringRef name;
188     if (sec->type == WASM_SEC_DATA)
189       name = "reloc.DATA";
190     else if (sec->type == WASM_SEC_CODE)
191       name = "reloc.CODE";
192     else if (sec->type == WASM_SEC_CUSTOM)
193       name = saver.save("reloc." + sec->name);
194     else
195       llvm_unreachable(
196           "relocations only supported for code, data, or custom sections");
197 
198     addSection(make<RelocSection>(name, sec));
199   }
200 }
201 
202 void Writer::populateProducers() {
203   for (ObjFile *file : symtab->objectFiles) {
204     const WasmProducerInfo &info = file->getWasmObj()->getProducerInfo();
205     out.producersSec->addInfo(info);
206   }
207 }
208 
209 void Writer::writeHeader() {
210   memcpy(buffer->getBufferStart(), header.data(), header.size());
211 }
212 
213 void Writer::writeSections() {
214   uint8_t *buf = buffer->getBufferStart();
215   parallelForEach(outputSections, [buf](OutputSection *s) {
216     assert(s->isNeeded());
217     s->writeTo(buf);
218   });
219 }
220 
221 static void setGlobalPtr(DefinedGlobal *g, uint64_t memoryPtr) {
222   g->global->setPointerValue(memoryPtr);
223 }
224 
225 // Fix the memory layout of the output binary.  This assigns memory offsets
226 // to each of the input data sections as well as the explicit stack region.
227 // The default memory layout is as follows, from low to high.
228 //
229 //  - initialized data (starting at Config->globalBase)
230 //  - BSS data (not currently implemented in llvm)
231 //  - explicit stack (Config->ZStackSize)
232 //  - heap start / unallocated
233 //
234 // The --stack-first option means that stack is placed before any static data.
235 // This can be useful since it means that stack overflow traps immediately
236 // rather than overwriting global data, but also increases code size since all
237 // static data loads and stores requires larger offsets.
238 void Writer::layoutMemory() {
239   uint64_t memoryPtr = 0;
240 
241   auto placeStack = [&]() {
242     if (config->relocatable || config->isPic)
243       return;
244     memoryPtr = alignTo(memoryPtr, stackAlignment);
245     if (config->zStackSize != alignTo(config->zStackSize, stackAlignment))
246       error("stack size must be " + Twine(stackAlignment) + "-byte aligned");
247     log("mem: stack size  = " + Twine(config->zStackSize));
248     log("mem: stack base  = " + Twine(memoryPtr));
249     memoryPtr += config->zStackSize;
250     setGlobalPtr(cast<DefinedGlobal>(WasmSym::stackPointer), memoryPtr);
251     log("mem: stack top   = " + Twine(memoryPtr));
252   };
253 
254   if (config->stackFirst) {
255     placeStack();
256   } else {
257     memoryPtr = config->globalBase;
258     log("mem: global base = " + Twine(config->globalBase));
259   }
260 
261   if (WasmSym::globalBase)
262     WasmSym::globalBase->setVA(memoryPtr);
263 
264   uint64_t dataStart = memoryPtr;
265 
266   // Arbitrarily set __dso_handle handle to point to the start of the data
267   // segments.
268   if (WasmSym::dsoHandle)
269     WasmSym::dsoHandle->setVA(dataStart);
270 
271   out.dylinkSec->memAlign = 0;
272   for (OutputSegment *seg : segments) {
273     out.dylinkSec->memAlign = std::max(out.dylinkSec->memAlign, seg->alignment);
274     memoryPtr = alignTo(memoryPtr, 1ULL << seg->alignment);
275     seg->startVA = memoryPtr;
276     log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", seg->name,
277                 memoryPtr, seg->size, seg->alignment));
278 
279     if (!config->relocatable && seg->isTLS()) {
280       if (config->sharedMemory) {
281         auto *tlsSize = cast<DefinedGlobal>(WasmSym::tlsSize);
282         setGlobalPtr(tlsSize, seg->size);
283 
284         auto *tlsAlign = cast<DefinedGlobal>(WasmSym::tlsAlign);
285         setGlobalPtr(tlsAlign, int64_t{1} << seg->alignment);
286       } else if (WasmSym::tlsBase) {
287         auto *tlsBase = cast<DefinedGlobal>(WasmSym::tlsBase);
288         setGlobalPtr(tlsBase, memoryPtr);
289       }
290     }
291 
292     memoryPtr += seg->size;
293   }
294 
295   // Make space for the memory initialization flag
296   if (config->sharedMemory && hasPassiveInitializedSegments()) {
297     memoryPtr = alignTo(memoryPtr, 4);
298     WasmSym::initMemoryFlag = symtab->addSyntheticDataSymbol(
299         "__wasm_init_memory_flag", WASM_SYMBOL_VISIBILITY_HIDDEN);
300     WasmSym::initMemoryFlag->markLive();
301     WasmSym::initMemoryFlag->setVA(memoryPtr);
302     log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}",
303                 "__wasm_init_memory_flag", memoryPtr, 4, 4));
304     memoryPtr += 4;
305   }
306 
307   if (WasmSym::dataEnd)
308     WasmSym::dataEnd->setVA(memoryPtr);
309 
310   uint64_t staticDataSize = memoryPtr - dataStart;
311   log("mem: static data = " + Twine(staticDataSize));
312   if (config->isPic)
313     out.dylinkSec->memSize = staticDataSize;
314 
315   if (!config->stackFirst)
316     placeStack();
317 
318   if (WasmSym::heapBase) {
319     // Set `__heap_base` to follow the end of the stack or global data. The
320     // fact that this comes last means that a malloc/brk implementation can
321     // grow the heap at runtime.
322     // We'll align the heap base here because memory allocators might expect
323     // __heap_base to be aligned already.
324     memoryPtr = alignTo(memoryPtr, heapAlignment);
325     log("mem: heap base   = " + Twine(memoryPtr));
326     WasmSym::heapBase->setVA(memoryPtr);
327   }
328 
329   uint64_t maxMemorySetting = 1ULL
330                               << (config->is64.getValueOr(false) ? 48 : 32);
331 
332   if (config->initialMemory != 0) {
333     if (config->initialMemory != alignTo(config->initialMemory, WasmPageSize))
334       error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned");
335     if (memoryPtr > config->initialMemory)
336       error("initial memory too small, " + Twine(memoryPtr) + " bytes needed");
337     if (config->initialMemory > maxMemorySetting)
338       error("initial memory too large, cannot be greater than " +
339             Twine(maxMemorySetting));
340     memoryPtr = config->initialMemory;
341   }
342   out.memorySec->numMemoryPages =
343       alignTo(memoryPtr, WasmPageSize) / WasmPageSize;
344   log("mem: total pages = " + Twine(out.memorySec->numMemoryPages));
345 
346   if (config->maxMemory != 0) {
347     if (config->maxMemory != alignTo(config->maxMemory, WasmPageSize))
348       error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned");
349     if (memoryPtr > config->maxMemory)
350       error("maximum memory too small, " + Twine(memoryPtr) + " bytes needed");
351     if (config->maxMemory > maxMemorySetting)
352       error("maximum memory too large, cannot be greater than " +
353             Twine(maxMemorySetting));
354   }
355 
356   // Check max if explicitly supplied or required by shared memory
357   if (config->maxMemory != 0 || config->sharedMemory) {
358     uint64_t max = config->maxMemory;
359     if (max == 0) {
360       // If no maxMemory config was supplied but we are building with
361       // shared memory, we need to pick a sensible upper limit.
362       if (config->isPic)
363         max = maxMemorySetting;
364       else
365         max = alignTo(memoryPtr, WasmPageSize);
366     }
367     out.memorySec->maxMemoryPages = max / WasmPageSize;
368     log("mem: max pages   = " + Twine(out.memorySec->maxMemoryPages));
369   }
370 }
371 
372 void Writer::addSection(OutputSection *sec) {
373   if (!sec->isNeeded())
374     return;
375   log("addSection: " + toString(*sec));
376   sec->sectionIndex = outputSections.size();
377   outputSections.push_back(sec);
378 }
379 
380 // If a section name is valid as a C identifier (which is rare because of
381 // the leading '.'), linkers are expected to define __start_<secname> and
382 // __stop_<secname> symbols. They are at beginning and end of the section,
383 // respectively. This is not requested by the ELF standard, but GNU ld and
384 // gold provide the feature, and used by many programs.
385 static void addStartStopSymbols(const OutputSegment *seg) {
386   StringRef name = seg->name;
387   if (!isValidCIdentifier(name))
388     return;
389   LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << name << "\n");
390   uint64_t start = seg->startVA;
391   uint64_t stop = start + seg->size;
392   symtab->addOptionalDataSymbol(saver.save("__start_" + name), start);
393   symtab->addOptionalDataSymbol(saver.save("__stop_" + name), stop);
394 }
395 
396 void Writer::addSections() {
397   addSection(out.dylinkSec);
398   addSection(out.typeSec);
399   addSection(out.importSec);
400   addSection(out.functionSec);
401   addSection(out.tableSec);
402   addSection(out.memorySec);
403   addSection(out.tagSec);
404   addSection(out.globalSec);
405   addSection(out.exportSec);
406   addSection(out.startSec);
407   addSection(out.elemSec);
408   addSection(out.dataCountSec);
409 
410   addSection(make<CodeSection>(out.functionSec->inputFunctions));
411   addSection(make<DataSection>(segments));
412 
413   createCustomSections();
414 
415   addSection(out.linkingSec);
416   if (config->emitRelocs || config->relocatable) {
417     createRelocSections();
418   }
419 
420   addSection(out.nameSec);
421   addSection(out.producersSec);
422   addSection(out.targetFeaturesSec);
423 }
424 
425 void Writer::finalizeSections() {
426   for (OutputSection *s : outputSections) {
427     s->setOffset(fileSize);
428     s->finalizeContents();
429     fileSize += s->getSize();
430   }
431 }
432 
433 void Writer::populateTargetFeatures() {
434   StringMap<std::string> used;
435   StringMap<std::string> required;
436   StringMap<std::string> disallowed;
437   SmallSet<std::string, 8> &allowed = out.targetFeaturesSec->features;
438   bool tlsUsed = false;
439 
440   if (config->isPic) {
441     // This should not be necessary because all PIC objects should
442     // contain the mutable-globals feature.
443     // TODO(https://bugs.llvm.org/show_bug.cgi?id=52339)
444     allowed.insert("mutable-globals");
445   }
446 
447   // Only infer used features if user did not specify features
448   bool inferFeatures = !config->features.hasValue();
449 
450   if (!inferFeatures) {
451     auto &explicitFeatures = config->features.getValue();
452     allowed.insert(explicitFeatures.begin(), explicitFeatures.end());
453     if (!config->checkFeatures)
454       return;
455   }
456 
457   // Find the sets of used, required, and disallowed features
458   for (ObjFile *file : symtab->objectFiles) {
459     StringRef fileName(file->getName());
460     for (auto &feature : file->getWasmObj()->getTargetFeatures()) {
461       switch (feature.Prefix) {
462       case WASM_FEATURE_PREFIX_USED:
463         used.insert({feature.Name, std::string(fileName)});
464         break;
465       case WASM_FEATURE_PREFIX_REQUIRED:
466         used.insert({feature.Name, std::string(fileName)});
467         required.insert({feature.Name, std::string(fileName)});
468         break;
469       case WASM_FEATURE_PREFIX_DISALLOWED:
470         disallowed.insert({feature.Name, std::string(fileName)});
471         break;
472       default:
473         error("Unrecognized feature policy prefix " +
474               std::to_string(feature.Prefix));
475       }
476     }
477 
478     // Find TLS data segments
479     auto isTLS = [](InputChunk *segment) {
480       return segment->live && segment->isTLS();
481     };
482     tlsUsed = tlsUsed || llvm::any_of(file->segments, isTLS);
483   }
484 
485   if (inferFeatures)
486     for (const auto &key : used.keys())
487       allowed.insert(std::string(key));
488 
489   if (!config->checkFeatures)
490     return;
491 
492   if (config->sharedMemory) {
493     if (disallowed.count("shared-mem"))
494       error("--shared-memory is disallowed by " + disallowed["shared-mem"] +
495             " because it was not compiled with 'atomics' or 'bulk-memory' "
496             "features.");
497 
498     for (auto feature : {"atomics", "bulk-memory"})
499       if (!allowed.count(feature))
500         error(StringRef("'") + feature +
501               "' feature must be used in order to use shared memory");
502   }
503 
504   if (tlsUsed) {
505     for (auto feature : {"atomics", "bulk-memory"})
506       if (!allowed.count(feature))
507         error(StringRef("'") + feature +
508               "' feature must be used in order to use thread-local storage");
509   }
510 
511   // Validate that used features are allowed in output
512   if (!inferFeatures) {
513     for (const auto &feature : used.keys()) {
514       if (!allowed.count(std::string(feature)))
515         error(Twine("Target feature '") + feature + "' used by " +
516               used[feature] + " is not allowed.");
517     }
518   }
519 
520   // Validate the required and disallowed constraints for each file
521   for (ObjFile *file : symtab->objectFiles) {
522     StringRef fileName(file->getName());
523     SmallSet<std::string, 8> objectFeatures;
524     for (const auto &feature : file->getWasmObj()->getTargetFeatures()) {
525       if (feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED)
526         continue;
527       objectFeatures.insert(feature.Name);
528       if (disallowed.count(feature.Name))
529         error(Twine("Target feature '") + feature.Name + "' used in " +
530               fileName + " is disallowed by " + disallowed[feature.Name] +
531               ". Use --no-check-features to suppress.");
532     }
533     for (const auto &feature : required.keys()) {
534       if (!objectFeatures.count(std::string(feature)))
535         error(Twine("Missing target feature '") + feature + "' in " + fileName +
536               ", required by " + required[feature] +
537               ". Use --no-check-features to suppress.");
538     }
539   }
540 
541   // Normally we don't include bss segments in the binary.  In particular if
542   // memory is not being imported then we can assume its zero initialized.
543   // In the case the memory is imported, we and we can use the memory.fill
544   // instrction than we can also avoid inluding the segments.
545   if (config->importMemory && !allowed.count("bulk-memory"))
546     config->emitBssSegments = true;
547 }
548 
549 void Writer::checkImportExportTargetFeatures() {
550   if (config->relocatable || !config->checkFeatures)
551     return;
552 
553   if (out.targetFeaturesSec->features.count("mutable-globals") == 0) {
554     for (const Symbol *sym : out.importSec->importedSymbols) {
555       if (auto *global = dyn_cast<GlobalSymbol>(sym)) {
556         if (global->getGlobalType()->Mutable) {
557           error(Twine("mutable global imported but 'mutable-globals' feature "
558                       "not present in inputs: `") +
559                 toString(*sym) + "`. Use --no-check-features to suppress.");
560         }
561       }
562     }
563     for (const Symbol *sym : out.exportSec->exportedSymbols) {
564       if (isa<GlobalSymbol>(sym)) {
565         error(Twine("mutable global exported but 'mutable-globals' feature "
566                     "not present in inputs: `") +
567               toString(*sym) + "`. Use --no-check-features to suppress.");
568       }
569     }
570   }
571 }
572 
573 static bool shouldImport(Symbol *sym) {
574   // We don't generate imports for data symbols. They however can be imported
575   // as GOT entries.
576   if (isa<DataSymbol>(sym))
577     return false;
578   if (!sym->isLive())
579     return false;
580   if (!sym->isUsedInRegularObj)
581     return false;
582 
583   // When a symbol is weakly defined in a shared library we need to allow
584   // it to be overridden by another module so need to both import
585   // and export the symbol.
586   if (config->shared && sym->isDefined() && sym->isWeak())
587     return true;
588   if (!sym->isUndefined())
589     return false;
590   if (sym->isWeak() && !config->relocatable && !config->isPic)
591     return false;
592 
593   // In PIC mode we only need to import functions when they are called directly.
594   // Indirect usage all goes via GOT imports.
595   if (config->isPic) {
596     if (auto *f = dyn_cast<UndefinedFunction>(sym))
597       if (!f->isCalledDirectly)
598         return false;
599   }
600 
601   if (config->isPic || config->relocatable || config->importUndefined)
602     return true;
603   if (config->allowUndefinedSymbols.count(sym->getName()) != 0)
604     return true;
605 
606   return sym->importName.hasValue();
607 }
608 
609 void Writer::calculateImports() {
610   // Some inputs require that the indirect function table be assigned to table
611   // number 0, so if it is present and is an import, allocate it before any
612   // other tables.
613   if (WasmSym::indirectFunctionTable &&
614       shouldImport(WasmSym::indirectFunctionTable))
615     out.importSec->addImport(WasmSym::indirectFunctionTable);
616 
617   for (Symbol *sym : symtab->getSymbols()) {
618     if (!shouldImport(sym))
619       continue;
620     if (sym == WasmSym::indirectFunctionTable)
621       continue;
622     LLVM_DEBUG(dbgs() << "import: " << sym->getName() << "\n");
623     out.importSec->addImport(sym);
624   }
625 }
626 
627 void Writer::calculateExports() {
628   if (config->relocatable)
629     return;
630 
631   if (!config->relocatable && !config->importMemory)
632     out.exportSec->exports.push_back(
633         WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0});
634 
635   unsigned globalIndex =
636       out.importSec->getNumImportedGlobals() + out.globalSec->numGlobals();
637 
638   for (Symbol *sym : symtab->getSymbols()) {
639     if (!sym->isExported())
640       continue;
641     if (!sym->isLive())
642       continue;
643 
644     StringRef name = sym->getName();
645     WasmExport export_;
646     if (auto *f = dyn_cast<DefinedFunction>(sym)) {
647       if (Optional<StringRef> exportName = f->function->getExportName()) {
648         name = *exportName;
649       }
650       export_ = {name, WASM_EXTERNAL_FUNCTION, f->getExportedFunctionIndex()};
651     } else if (auto *g = dyn_cast<DefinedGlobal>(sym)) {
652       if (g->getGlobalType()->Mutable && !g->getFile() && !g->forceExport) {
653         // Avoid exporting mutable globals are linker synthesized (e.g.
654         // __stack_pointer or __tls_base) unless they are explicitly exported
655         // from the command line.
656         // Without this check `--export-all` would cause any program using the
657         // stack pointer to export a mutable global even if none of the input
658         // files were built with the `mutable-globals` feature.
659         continue;
660       }
661       export_ = {name, WASM_EXTERNAL_GLOBAL, g->getGlobalIndex()};
662     } else if (auto *t = dyn_cast<DefinedTag>(sym)) {
663       export_ = {name, WASM_EXTERNAL_TAG, t->getTagIndex()};
664     } else if (auto *d = dyn_cast<DefinedData>(sym)) {
665       out.globalSec->dataAddressGlobals.push_back(d);
666       export_ = {name, WASM_EXTERNAL_GLOBAL, globalIndex++};
667     } else {
668       auto *t = cast<DefinedTable>(sym);
669       export_ = {name, WASM_EXTERNAL_TABLE, t->getTableNumber()};
670     }
671 
672     LLVM_DEBUG(dbgs() << "Export: " << name << "\n");
673     out.exportSec->exports.push_back(export_);
674     out.exportSec->exportedSymbols.push_back(sym);
675   }
676 }
677 
678 void Writer::populateSymtab() {
679   if (!config->relocatable && !config->emitRelocs)
680     return;
681 
682   for (Symbol *sym : symtab->getSymbols())
683     if (sym->isUsedInRegularObj && sym->isLive())
684       out.linkingSec->addToSymtab(sym);
685 
686   for (ObjFile *file : symtab->objectFiles) {
687     LLVM_DEBUG(dbgs() << "Local symtab entries: " << file->getName() << "\n");
688     for (Symbol *sym : file->getSymbols())
689       if (sym->isLocal() && !isa<SectionSymbol>(sym) && sym->isLive())
690         out.linkingSec->addToSymtab(sym);
691   }
692 }
693 
694 void Writer::calculateTypes() {
695   // The output type section is the union of the following sets:
696   // 1. Any signature used in the TYPE relocation
697   // 2. The signatures of all imported functions
698   // 3. The signatures of all defined functions
699   // 4. The signatures of all imported tags
700   // 5. The signatures of all defined tags
701 
702   for (ObjFile *file : symtab->objectFiles) {
703     ArrayRef<WasmSignature> types = file->getWasmObj()->types();
704     for (uint32_t i = 0; i < types.size(); i++)
705       if (file->typeIsUsed[i])
706         file->typeMap[i] = out.typeSec->registerType(types[i]);
707   }
708 
709   for (const Symbol *sym : out.importSec->importedSymbols) {
710     if (auto *f = dyn_cast<FunctionSymbol>(sym))
711       out.typeSec->registerType(*f->signature);
712     else if (auto *t = dyn_cast<TagSymbol>(sym))
713       out.typeSec->registerType(*t->signature);
714   }
715 
716   for (const InputFunction *f : out.functionSec->inputFunctions)
717     out.typeSec->registerType(f->signature);
718 
719   for (const InputTag *t : out.tagSec->inputTags)
720     out.typeSec->registerType(t->signature);
721 }
722 
723 // In a command-style link, create a wrapper for each exported symbol
724 // which calls the constructors and destructors.
725 void Writer::createCommandExportWrappers() {
726   // This logic doesn't currently support Emscripten-style PIC mode.
727   assert(!config->isPic);
728 
729   // If there are no ctors and there's no libc `__wasm_call_dtors` to
730   // call, don't wrap the exports.
731   if (initFunctions.empty() && WasmSym::callDtors == NULL)
732     return;
733 
734   std::vector<DefinedFunction *> toWrap;
735 
736   for (Symbol *sym : symtab->getSymbols())
737     if (sym->isExported())
738       if (auto *f = dyn_cast<DefinedFunction>(sym))
739         toWrap.push_back(f);
740 
741   for (auto *f : toWrap) {
742     auto funcNameStr = (f->getName() + ".command_export").str();
743     commandExportWrapperNames.push_back(funcNameStr);
744     const std::string &funcName = commandExportWrapperNames.back();
745 
746     auto func = make<SyntheticFunction>(*f->getSignature(), funcName);
747     if (f->function->getExportName().hasValue())
748       func->setExportName(f->function->getExportName()->str());
749     else
750       func->setExportName(f->getName().str());
751 
752     DefinedFunction *def =
753         symtab->addSyntheticFunction(funcName, f->flags, func);
754     def->markLive();
755 
756     def->flags |= WASM_SYMBOL_EXPORTED;
757     def->flags &= ~WASM_SYMBOL_VISIBILITY_HIDDEN;
758     def->forceExport = f->forceExport;
759 
760     f->flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
761     f->flags &= ~WASM_SYMBOL_EXPORTED;
762     f->forceExport = false;
763 
764     out.functionSec->addFunction(func);
765 
766     createCommandExportWrapper(f->getFunctionIndex(), def);
767   }
768 }
769 
770 static void finalizeIndirectFunctionTable() {
771   if (!WasmSym::indirectFunctionTable)
772     return;
773 
774   if (shouldImport(WasmSym::indirectFunctionTable) &&
775       !WasmSym::indirectFunctionTable->hasTableNumber()) {
776     // Processing -Bsymbolic relocations resulted in a late requirement that the
777     // indirect function table be present, and we are running in --import-table
778     // mode.  Add the table now to the imports section.  Otherwise it will be
779     // added to the tables section later in assignIndexes.
780     out.importSec->addImport(WasmSym::indirectFunctionTable);
781   }
782 
783   uint32_t tableSize = config->tableBase + out.elemSec->numEntries();
784   WasmLimits limits = {0, tableSize, 0};
785   if (WasmSym::indirectFunctionTable->isDefined() && !config->growableTable) {
786     limits.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
787     limits.Maximum = limits.Minimum;
788   }
789   WasmSym::indirectFunctionTable->setLimits(limits);
790 }
791 
792 static void scanRelocations() {
793   for (ObjFile *file : symtab->objectFiles) {
794     LLVM_DEBUG(dbgs() << "scanRelocations: " << file->getName() << "\n");
795     for (InputChunk *chunk : file->functions)
796       scanRelocations(chunk);
797     for (InputChunk *chunk : file->segments)
798       scanRelocations(chunk);
799     for (auto &p : file->customSections)
800       scanRelocations(p);
801   }
802 }
803 
804 void Writer::assignIndexes() {
805   // Seal the import section, since other index spaces such as function and
806   // global are effected by the number of imports.
807   out.importSec->seal();
808 
809   for (InputFunction *func : symtab->syntheticFunctions)
810     out.functionSec->addFunction(func);
811 
812   for (ObjFile *file : symtab->objectFiles) {
813     LLVM_DEBUG(dbgs() << "Functions: " << file->getName() << "\n");
814     for (InputFunction *func : file->functions)
815       out.functionSec->addFunction(func);
816   }
817 
818   for (InputGlobal *global : symtab->syntheticGlobals)
819     out.globalSec->addGlobal(global);
820 
821   for (ObjFile *file : symtab->objectFiles) {
822     LLVM_DEBUG(dbgs() << "Globals: " << file->getName() << "\n");
823     for (InputGlobal *global : file->globals)
824       out.globalSec->addGlobal(global);
825   }
826 
827   for (ObjFile *file : symtab->objectFiles) {
828     LLVM_DEBUG(dbgs() << "Tags: " << file->getName() << "\n");
829     for (InputTag *tag : file->tags)
830       out.tagSec->addTag(tag);
831   }
832 
833   for (ObjFile *file : symtab->objectFiles) {
834     LLVM_DEBUG(dbgs() << "Tables: " << file->getName() << "\n");
835     for (InputTable *table : file->tables)
836       out.tableSec->addTable(table);
837   }
838 
839   for (InputTable *table : symtab->syntheticTables)
840     out.tableSec->addTable(table);
841 
842   out.globalSec->assignIndexes();
843   out.tableSec->assignIndexes();
844 }
845 
846 static StringRef getOutputDataSegmentName(const InputChunk &seg) {
847   // We always merge .tbss and .tdata into a single TLS segment so all TLS
848   // symbols are be relative to single __tls_base.
849   if (seg.isTLS())
850     return ".tdata";
851   StringRef name = seg.getName();
852   if (!config->mergeDataSegments)
853     return name;
854   if (name.startswith(".text."))
855     return ".text";
856   if (name.startswith(".data."))
857     return ".data";
858   if (name.startswith(".bss."))
859     return ".bss";
860   if (name.startswith(".rodata."))
861     return ".rodata";
862   return name;
863 }
864 
865 OutputSegment *Writer::createOutputSegment(StringRef name) {
866   LLVM_DEBUG(dbgs() << "new segment: " << name << "\n");
867   OutputSegment *s = make<OutputSegment>(name);
868   if (config->sharedMemory)
869     s->initFlags = WASM_DATA_SEGMENT_IS_PASSIVE;
870   if (!config->relocatable && name.startswith(".bss"))
871     s->isBss = true;
872   segments.push_back(s);
873   return s;
874 }
875 
876 void Writer::createOutputSegments() {
877   for (ObjFile *file : symtab->objectFiles) {
878     for (InputChunk *segment : file->segments) {
879       if (!segment->live)
880         continue;
881       StringRef name = getOutputDataSegmentName(*segment);
882       OutputSegment *s = nullptr;
883       // When running in relocatable mode we can't merge segments that are part
884       // of comdat groups since the ultimate linker needs to be able exclude or
885       // include them individually.
886       if (config->relocatable && !segment->getComdatName().empty()) {
887         s = createOutputSegment(name);
888       } else {
889         if (segmentMap.count(name) == 0)
890           segmentMap[name] = createOutputSegment(name);
891         s = segmentMap[name];
892       }
893       s->addInputSegment(segment);
894     }
895   }
896 
897   // Sort segments by type, placing .bss last
898   std::stable_sort(segments.begin(), segments.end(),
899                    [](const OutputSegment *a, const OutputSegment *b) {
900                      auto order = [](StringRef name) {
901                        return StringSwitch<int>(name)
902                            .StartsWith(".tdata", 0)
903                            .StartsWith(".rodata", 1)
904                            .StartsWith(".data", 2)
905                            .StartsWith(".bss", 4)
906                            .Default(3);
907                      };
908                      return order(a->name) < order(b->name);
909                    });
910 
911   for (size_t i = 0; i < segments.size(); ++i)
912     segments[i]->index = i;
913 
914   // Merge MergeInputSections into a single MergeSyntheticSection.
915   LLVM_DEBUG(dbgs() << "-- finalize input semgments\n");
916   for (OutputSegment *seg : segments)
917     seg->finalizeInputSegments();
918 }
919 
920 void Writer::combineOutputSegments() {
921   // With PIC code we currently only support a single active data segment since
922   // we only have a single __memory_base to use as our base address.  This pass
923   // combines all data segments into a single .data segment.
924   // This restructions can be relaxed once we have extended constant
925   // expressions available:
926   // https://github.com/WebAssembly/extended-const
927   assert(config->isPic && !config->sharedMemory);
928   if (segments.size() <= 1)
929     return;
930   OutputSegment *combined = make<OutputSegment>(".data");
931   combined->startVA = segments[0]->startVA;
932   for (OutputSegment *s : segments) {
933     bool first = true;
934     for (InputChunk *inSeg : s->inputSegments) {
935       if (first)
936         inSeg->alignment = std::max(inSeg->alignment, s->alignment);
937       first = false;
938 #ifndef NDEBUG
939       uint64_t oldVA = inSeg->getVA();
940 #endif
941       combined->addInputSegment(inSeg);
942 #ifndef NDEBUG
943       uint64_t newVA = inSeg->getVA();
944       LLVM_DEBUG(dbgs() << "added input segment. name=" << inSeg->getName()
945                         << " oldVA=" << oldVA << " newVA=" << newVA << "\n");
946       assert(oldVA == newVA);
947 #endif
948     }
949   }
950 
951   segments = {combined};
952 }
953 
954 static void createFunction(DefinedFunction *func, StringRef bodyContent) {
955   std::string functionBody;
956   {
957     raw_string_ostream os(functionBody);
958     writeUleb128(os, bodyContent.size(), "function size");
959     os << bodyContent;
960   }
961   ArrayRef<uint8_t> body = arrayRefFromStringRef(saver.save(functionBody));
962   cast<SyntheticFunction>(func->function)->setBody(body);
963 }
964 
965 bool Writer::needsPassiveInitialization(const OutputSegment *segment) {
966   // TLS segments are initialized separately
967   if (segment->isTLS())
968     return false;
969   // If bulk memory features is supported then we can perform bss initialization
970   // (via memory.fill) during `__wasm_init_memory`.
971   if (config->importMemory && !segment->requiredInBinary())
972     return true;
973   return segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE;
974 }
975 
976 bool Writer::hasPassiveInitializedSegments() {
977   return llvm::any_of(segments, [this](const OutputSegment *s) {
978     return this->needsPassiveInitialization(s);
979   });
980 }
981 
982 void Writer::createSyntheticInitFunctions() {
983   if (config->relocatable)
984     return;
985 
986   static WasmSignature nullSignature = {{}, {}};
987 
988   // Passive segments are used to avoid memory being reinitialized on each
989   // thread's instantiation. These passive segments are initialized and
990   // dropped in __wasm_init_memory, which is registered as the start function
991   // We also initialize bss segments (using memory.fill) as part of this
992   // function.
993   if (hasPassiveInitializedSegments()) {
994     WasmSym::initMemory = symtab->addSyntheticFunction(
995         "__wasm_init_memory", WASM_SYMBOL_VISIBILITY_HIDDEN,
996         make<SyntheticFunction>(nullSignature, "__wasm_init_memory"));
997     WasmSym::initMemory->markLive();
998   }
999 
1000   if (config->sharedMemory && out.globalSec->needsTLSRelocations()) {
1001     WasmSym::applyGlobalTLSRelocs = symtab->addSyntheticFunction(
1002         "__wasm_apply_global_tls_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN,
1003         make<SyntheticFunction>(nullSignature,
1004                                 "__wasm_apply_global_tls_relocs"));
1005     WasmSym::applyGlobalTLSRelocs->markLive();
1006   }
1007 
1008   if (config->isPic) {
1009     // For PIC code we create synthetic functions that apply relocations.
1010     // These get called from __wasm_call_ctors before the user-level
1011     // constructors.
1012     WasmSym::applyDataRelocs = symtab->addSyntheticFunction(
1013         "__wasm_apply_data_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN,
1014         make<SyntheticFunction>(nullSignature, "__wasm_apply_data_relocs"));
1015     WasmSym::applyDataRelocs->markLive();
1016 
1017     if (out.globalSec->needsRelocations()) {
1018       WasmSym::applyGlobalRelocs = symtab->addSyntheticFunction(
1019           "__wasm_apply_global_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN,
1020           make<SyntheticFunction>(nullSignature, "__wasm_apply_global_relocs"));
1021       WasmSym::applyGlobalRelocs->markLive();
1022     }
1023   }
1024 
1025   if (WasmSym::applyGlobalRelocs && WasmSym::initMemory) {
1026     WasmSym::startFunction = symtab->addSyntheticFunction(
1027         "__wasm_start", WASM_SYMBOL_VISIBILITY_HIDDEN,
1028         make<SyntheticFunction>(nullSignature, "__wasm_start"));
1029     WasmSym::startFunction->markLive();
1030   }
1031 }
1032 
1033 void Writer::createInitMemoryFunction() {
1034   LLVM_DEBUG(dbgs() << "createInitMemoryFunction\n");
1035   assert(WasmSym::initMemory);
1036   assert(hasPassiveInitializedSegments());
1037   uint64_t flagAddress;
1038   if (config->sharedMemory) {
1039     assert(WasmSym::initMemoryFlag);
1040     flagAddress = WasmSym::initMemoryFlag->getVA();
1041   }
1042   bool is64 = config->is64.getValueOr(false);
1043   std::string bodyContent;
1044   {
1045     raw_string_ostream os(bodyContent);
1046     // Initialize memory in a thread-safe manner. The thread that successfully
1047     // increments the flag from 0 to 1 is is responsible for performing the
1048     // memory initialization. Other threads go sleep on the flag until the
1049     // first thread finishing initializing memory, increments the flag to 2,
1050     // and wakes all the other threads. Once the flag has been set to 2,
1051     // subsequently started threads will skip the sleep. All threads
1052     // unconditionally drop their passive data segments once memory has been
1053     // initialized. The generated code is as follows:
1054     //
1055     // (func $__wasm_init_memory
1056     //  (block $drop
1057     //   (block $wait
1058     //    (block $init
1059     //     (br_table $init $wait $drop
1060     //      (i32.atomic.rmw.cmpxchg align=2 offset=0
1061     //       (i32.const $__init_memory_flag)
1062     //       (i32.const 0)
1063     //       (i32.const 1)
1064     //      )
1065     //     )
1066     //    ) ;; $init
1067     //    ( ... initialize data segments ... )
1068     //    (i32.atomic.store align=2 offset=0
1069     //     (i32.const $__init_memory_flag)
1070     //     (i32.const 2)
1071     //    )
1072     //    (drop
1073     //     (i32.atomic.notify align=2 offset=0
1074     //      (i32.const $__init_memory_flag)
1075     //      (i32.const -1u)
1076     //     )
1077     //    )
1078     //    (br $drop)
1079     //   ) ;; $wait
1080     //   (drop
1081     //    (i32.atomic.wait align=2 offset=0
1082     //     (i32.const $__init_memory_flag)
1083     //     (i32.const 1)
1084     //     (i32.const -1)
1085     //    )
1086     //   )
1087     //  ) ;; $drop
1088     //  ( ... drop data segments ... )
1089     // )
1090     //
1091     // When we are building with PIC, calculate the flag location using:
1092     //
1093     //    (global.get $__memory_base)
1094     //    (i32.const $__init_memory_flag)
1095     //    (i32.const 1)
1096 
1097     auto writeGetFlagAddress = [&]() {
1098       if (config->isPic) {
1099         writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
1100         writeUleb128(os, 0, "local 0");
1101       } else {
1102         writePtrConst(os, flagAddress, is64, "flag address");
1103       }
1104     };
1105 
1106     if (config->sharedMemory) {
1107       // With PIC code we cache the flag address in local 0
1108       if (config->isPic) {
1109         writeUleb128(os, 1, "num local decls");
1110         writeUleb128(os, 1, "local count");
1111         writeU8(os, is64 ? WASM_TYPE_I64 : WASM_TYPE_I32, "address type");
1112         writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
1113         writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(), "memory_base");
1114         writePtrConst(os, flagAddress, is64, "flag address");
1115         writeU8(os, is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD, "add");
1116         writeU8(os, WASM_OPCODE_LOCAL_SET, "local.set");
1117         writeUleb128(os, 0, "local 0");
1118       } else {
1119         writeUleb128(os, 0, "num locals");
1120       }
1121 
1122       // Set up destination blocks
1123       writeU8(os, WASM_OPCODE_BLOCK, "block $drop");
1124       writeU8(os, WASM_TYPE_NORESULT, "block type");
1125       writeU8(os, WASM_OPCODE_BLOCK, "block $wait");
1126       writeU8(os, WASM_TYPE_NORESULT, "block type");
1127       writeU8(os, WASM_OPCODE_BLOCK, "block $init");
1128       writeU8(os, WASM_TYPE_NORESULT, "block type");
1129 
1130       // Atomically check whether we win the race.
1131       writeGetFlagAddress();
1132       writeI32Const(os, 0, "expected flag value");
1133       writeI32Const(os, 1, "new flag value");
1134       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
1135       writeUleb128(os, WASM_OPCODE_I32_RMW_CMPXCHG, "i32.atomic.rmw.cmpxchg");
1136       writeMemArg(os, 2, 0);
1137 
1138       // Based on the value, decide what to do next.
1139       writeU8(os, WASM_OPCODE_BR_TABLE, "br_table");
1140       writeUleb128(os, 2, "label vector length");
1141       writeUleb128(os, 0, "label $init");
1142       writeUleb128(os, 1, "label $wait");
1143       writeUleb128(os, 2, "default label $drop");
1144 
1145       // Initialize passive data segments
1146       writeU8(os, WASM_OPCODE_END, "end $init");
1147     } else {
1148       writeUleb128(os, 0, "num local decls");
1149     }
1150 
1151     for (const OutputSegment *s : segments) {
1152       if (needsPassiveInitialization(s)) {
1153         // For passive BSS segments we can simple issue a memory.fill(0).
1154         // For non-BSS segments we do a memory.init.  Both these
1155         // instructions take as thier first argument the destination
1156         // address.
1157         writePtrConst(os, s->startVA, is64, "destination address");
1158         if (config->isPic) {
1159           writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
1160           writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(),
1161                        "memory_base");
1162           writeU8(os, is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD,
1163                   "i32.add");
1164         }
1165         if (s->isBss) {
1166           writeI32Const(os, 0, "fill value");
1167           writeI32Const(os, s->size, "memory region size");
1168           writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
1169           writeUleb128(os, WASM_OPCODE_MEMORY_FILL, "memory.fill");
1170           writeU8(os, 0, "memory index immediate");
1171         } else {
1172           writeI32Const(os, 0, "source segment offset");
1173           writeI32Const(os, s->size, "memory region size");
1174           writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
1175           writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "memory.init");
1176           writeUleb128(os, s->index, "segment index immediate");
1177           writeU8(os, 0, "memory index immediate");
1178         }
1179       }
1180     }
1181 
1182     if (config->sharedMemory) {
1183       // Set flag to 2 to mark end of initialization
1184       writeGetFlagAddress();
1185       writeI32Const(os, 2, "flag value");
1186       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
1187       writeUleb128(os, WASM_OPCODE_I32_ATOMIC_STORE, "i32.atomic.store");
1188       writeMemArg(os, 2, 0);
1189 
1190       // Notify any waiters that memory initialization is complete
1191       writeGetFlagAddress();
1192       writeI32Const(os, -1, "number of waiters");
1193       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
1194       writeUleb128(os, WASM_OPCODE_ATOMIC_NOTIFY, "atomic.notify");
1195       writeMemArg(os, 2, 0);
1196       writeU8(os, WASM_OPCODE_DROP, "drop");
1197 
1198       // Branch to drop the segments
1199       writeU8(os, WASM_OPCODE_BR, "br");
1200       writeUleb128(os, 1, "label $drop");
1201 
1202       // Wait for the winning thread to initialize memory
1203       writeU8(os, WASM_OPCODE_END, "end $wait");
1204       writeGetFlagAddress();
1205       writeI32Const(os, 1, "expected flag value");
1206       writeI64Const(os, -1, "timeout");
1207 
1208       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
1209       writeUleb128(os, WASM_OPCODE_I32_ATOMIC_WAIT, "i32.atomic.wait");
1210       writeMemArg(os, 2, 0);
1211       writeU8(os, WASM_OPCODE_DROP, "drop");
1212 
1213       // Unconditionally drop passive data segments
1214       writeU8(os, WASM_OPCODE_END, "end $drop");
1215     }
1216 
1217     for (const OutputSegment *s : segments) {
1218       if (needsPassiveInitialization(s) && !s->isBss) {
1219         // data.drop instruction
1220         writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
1221         writeUleb128(os, WASM_OPCODE_DATA_DROP, "data.drop");
1222         writeUleb128(os, s->index, "segment index immediate");
1223       }
1224     }
1225 
1226     // End the function
1227     writeU8(os, WASM_OPCODE_END, "END");
1228   }
1229 
1230   createFunction(WasmSym::initMemory, bodyContent);
1231 }
1232 
1233 void Writer::createStartFunction() {
1234   if (WasmSym::startFunction) {
1235     std::string bodyContent;
1236     {
1237       raw_string_ostream os(bodyContent);
1238       writeUleb128(os, 0, "num locals");
1239       writeU8(os, WASM_OPCODE_CALL, "CALL");
1240       writeUleb128(os, WasmSym::initMemory->getFunctionIndex(),
1241                    "function index");
1242       writeU8(os, WASM_OPCODE_CALL, "CALL");
1243       writeUleb128(os, WasmSym::applyGlobalRelocs->getFunctionIndex(),
1244                    "function index");
1245       writeU8(os, WASM_OPCODE_END, "END");
1246     }
1247     createFunction(WasmSym::startFunction, bodyContent);
1248   } else if (WasmSym::initMemory) {
1249     WasmSym::startFunction = WasmSym::initMemory;
1250   } else if (WasmSym::applyGlobalRelocs) {
1251     WasmSym::startFunction = WasmSym::applyGlobalRelocs;
1252   }
1253 }
1254 
1255 // For -shared (PIC) output, we create create a synthetic function which will
1256 // apply any relocations to the data segments on startup.  This function is
1257 // called `__wasm_apply_data_relocs` and is added at the beginning of
1258 // `__wasm_call_ctors` before any of the constructors run.
1259 void Writer::createApplyDataRelocationsFunction() {
1260   LLVM_DEBUG(dbgs() << "createApplyDataRelocationsFunction\n");
1261   // First write the body's contents to a string.
1262   std::string bodyContent;
1263   {
1264     raw_string_ostream os(bodyContent);
1265     writeUleb128(os, 0, "num locals");
1266     for (const OutputSegment *seg : segments)
1267       for (const InputChunk *inSeg : seg->inputSegments)
1268         inSeg->generateRelocationCode(os);
1269 
1270     writeU8(os, WASM_OPCODE_END, "END");
1271   }
1272 
1273   createFunction(WasmSym::applyDataRelocs, bodyContent);
1274 }
1275 
1276 // Similar to createApplyDataRelocationsFunction but generates relocation code
1277 // for WebAssembly globals. Because these globals are not shared between threads
1278 // these relocation need to run on every thread.
1279 void Writer::createApplyGlobalRelocationsFunction() {
1280   // First write the body's contents to a string.
1281   std::string bodyContent;
1282   {
1283     raw_string_ostream os(bodyContent);
1284     writeUleb128(os, 0, "num locals");
1285     out.globalSec->generateRelocationCode(os, false);
1286     writeU8(os, WASM_OPCODE_END, "END");
1287   }
1288 
1289   createFunction(WasmSym::applyGlobalRelocs, bodyContent);
1290 }
1291 
1292 // Similar to createApplyGlobalRelocationsFunction but for
1293 // TLS symbols.  This cannot be run during the start function
1294 // but must be delayed until __wasm_init_tls is called.
1295 void Writer::createApplyGlobalTLSRelocationsFunction() {
1296   // First write the body's contents to a string.
1297   std::string bodyContent;
1298   {
1299     raw_string_ostream os(bodyContent);
1300     writeUleb128(os, 0, "num locals");
1301     out.globalSec->generateRelocationCode(os, true);
1302     writeU8(os, WASM_OPCODE_END, "END");
1303   }
1304 
1305   createFunction(WasmSym::applyGlobalTLSRelocs, bodyContent);
1306 }
1307 
1308 // Create synthetic "__wasm_call_ctors" function based on ctor functions
1309 // in input object.
1310 void Writer::createCallCtorsFunction() {
1311   // If __wasm_call_ctors isn't referenced, there aren't any ctors, and we
1312   // aren't calling `__wasm_apply_data_relocs` for Emscripten-style PIC, don't
1313   // define the `__wasm_call_ctors` function.
1314   if (!WasmSym::callCtors->isLive() && !WasmSym::applyDataRelocs &&
1315       initFunctions.empty())
1316     return;
1317 
1318   // First write the body's contents to a string.
1319   std::string bodyContent;
1320   {
1321     raw_string_ostream os(bodyContent);
1322     writeUleb128(os, 0, "num locals");
1323 
1324     if (WasmSym::applyDataRelocs) {
1325       writeU8(os, WASM_OPCODE_CALL, "CALL");
1326       writeUleb128(os, WasmSym::applyDataRelocs->getFunctionIndex(),
1327                    "function index");
1328     }
1329 
1330     // Call constructors
1331     for (const WasmInitEntry &f : initFunctions) {
1332       writeU8(os, WASM_OPCODE_CALL, "CALL");
1333       writeUleb128(os, f.sym->getFunctionIndex(), "function index");
1334       for (size_t i = 0; i < f.sym->signature->Returns.size(); i++) {
1335         writeU8(os, WASM_OPCODE_DROP, "DROP");
1336       }
1337     }
1338 
1339     writeU8(os, WASM_OPCODE_END, "END");
1340   }
1341 
1342   createFunction(WasmSym::callCtors, bodyContent);
1343 }
1344 
1345 // Create a wrapper around a function export which calls the
1346 // static constructors and destructors.
1347 void Writer::createCommandExportWrapper(uint32_t functionIndex,
1348                                         DefinedFunction *f) {
1349   // First write the body's contents to a string.
1350   std::string bodyContent;
1351   {
1352     raw_string_ostream os(bodyContent);
1353     writeUleb128(os, 0, "num locals");
1354 
1355     // Call `__wasm_call_ctors` which call static constructors (and
1356     // applies any runtime relocations in Emscripten-style PIC mode)
1357     if (WasmSym::callCtors->isLive()) {
1358       writeU8(os, WASM_OPCODE_CALL, "CALL");
1359       writeUleb128(os, WasmSym::callCtors->getFunctionIndex(),
1360                    "function index");
1361     }
1362 
1363     // Call the user's code, leaving any return values on the operand stack.
1364     for (size_t i = 0; i < f->signature->Params.size(); ++i) {
1365       writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
1366       writeUleb128(os, i, "local index");
1367     }
1368     writeU8(os, WASM_OPCODE_CALL, "CALL");
1369     writeUleb128(os, functionIndex, "function index");
1370 
1371     // Call the function that calls the destructors.
1372     if (DefinedFunction *callDtors = WasmSym::callDtors) {
1373       writeU8(os, WASM_OPCODE_CALL, "CALL");
1374       writeUleb128(os, callDtors->getFunctionIndex(), "function index");
1375     }
1376 
1377     // End the function, returning the return values from the user's code.
1378     writeU8(os, WASM_OPCODE_END, "END");
1379   }
1380 
1381   createFunction(f, bodyContent);
1382 }
1383 
1384 void Writer::createInitTLSFunction() {
1385   std::string bodyContent;
1386   {
1387     raw_string_ostream os(bodyContent);
1388 
1389     OutputSegment *tlsSeg = nullptr;
1390     for (auto *seg : segments) {
1391       if (seg->name == ".tdata") {
1392         tlsSeg = seg;
1393         break;
1394       }
1395     }
1396 
1397     writeUleb128(os, 0, "num locals");
1398     if (tlsSeg) {
1399       writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
1400       writeUleb128(os, 0, "local index");
1401 
1402       writeU8(os, WASM_OPCODE_GLOBAL_SET, "global.set");
1403       writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "global index");
1404 
1405       // FIXME(wvo): this local needs to be I64 in wasm64, or we need an extend op.
1406       writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
1407       writeUleb128(os, 0, "local index");
1408 
1409       writeI32Const(os, 0, "segment offset");
1410 
1411       writeI32Const(os, tlsSeg->size, "memory region size");
1412 
1413       writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
1414       writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "MEMORY.INIT");
1415       writeUleb128(os, tlsSeg->index, "segment index immediate");
1416       writeU8(os, 0, "memory index immediate");
1417     }
1418 
1419     if (WasmSym::applyGlobalTLSRelocs) {
1420       writeU8(os, WASM_OPCODE_CALL, "CALL");
1421       writeUleb128(os, WasmSym::applyGlobalTLSRelocs->getFunctionIndex(),
1422                    "function index");
1423     }
1424     writeU8(os, WASM_OPCODE_END, "end function");
1425   }
1426 
1427   createFunction(WasmSym::initTLS, bodyContent);
1428 }
1429 
1430 // Populate InitFunctions vector with init functions from all input objects.
1431 // This is then used either when creating the output linking section or to
1432 // synthesize the "__wasm_call_ctors" function.
1433 void Writer::calculateInitFunctions() {
1434   if (!config->relocatable && !WasmSym::callCtors->isLive())
1435     return;
1436 
1437   for (ObjFile *file : symtab->objectFiles) {
1438     const WasmLinkingData &l = file->getWasmObj()->linkingData();
1439     for (const WasmInitFunc &f : l.InitFunctions) {
1440       FunctionSymbol *sym = file->getFunctionSymbol(f.Symbol);
1441       // comdat exclusions can cause init functions be discarded.
1442       if (sym->isDiscarded() || !sym->isLive())
1443         continue;
1444       if (sym->signature->Params.size() != 0)
1445         error("constructor functions cannot take arguments: " + toString(*sym));
1446       LLVM_DEBUG(dbgs() << "initFunctions: " << toString(*sym) << "\n");
1447       initFunctions.emplace_back(WasmInitEntry{sym, f.Priority});
1448     }
1449   }
1450 
1451   // Sort in order of priority (lowest first) so that they are called
1452   // in the correct order.
1453   llvm::stable_sort(initFunctions,
1454                     [](const WasmInitEntry &l, const WasmInitEntry &r) {
1455                       return l.priority < r.priority;
1456                     });
1457 }
1458 
1459 void Writer::createSyntheticSections() {
1460   out.dylinkSec = make<DylinkSection>();
1461   out.typeSec = make<TypeSection>();
1462   out.importSec = make<ImportSection>();
1463   out.functionSec = make<FunctionSection>();
1464   out.tableSec = make<TableSection>();
1465   out.memorySec = make<MemorySection>();
1466   out.tagSec = make<TagSection>();
1467   out.globalSec = make<GlobalSection>();
1468   out.exportSec = make<ExportSection>();
1469   out.startSec = make<StartSection>();
1470   out.elemSec = make<ElemSection>();
1471   out.producersSec = make<ProducersSection>();
1472   out.targetFeaturesSec = make<TargetFeaturesSection>();
1473 }
1474 
1475 void Writer::createSyntheticSectionsPostLayout() {
1476   out.dataCountSec = make<DataCountSection>(segments);
1477   out.linkingSec = make<LinkingSection>(initFunctions, segments);
1478   out.nameSec = make<NameSection>(segments);
1479 }
1480 
1481 void Writer::run() {
1482   if (config->relocatable || config->isPic)
1483     config->globalBase = 0;
1484 
1485   // For PIC code the table base is assigned dynamically by the loader.
1486   // For non-PIC, we start at 1 so that accessing table index 0 always traps.
1487   if (!config->isPic) {
1488     config->tableBase = 1;
1489     if (WasmSym::definedTableBase)
1490       WasmSym::definedTableBase->setVA(config->tableBase);
1491     if (WasmSym::definedTableBase32)
1492       WasmSym::definedTableBase32->setVA(config->tableBase);
1493   }
1494 
1495   log("-- createOutputSegments");
1496   createOutputSegments();
1497   log("-- createSyntheticSections");
1498   createSyntheticSections();
1499   log("-- layoutMemory");
1500   layoutMemory();
1501 
1502   if (!config->relocatable) {
1503     // Create linker synthesized __start_SECNAME/__stop_SECNAME symbols
1504     // This has to be done after memory layout is performed.
1505     for (const OutputSegment *seg : segments) {
1506       addStartStopSymbols(seg);
1507     }
1508   }
1509 
1510   for (auto &pair : config->exportedSymbols) {
1511     Symbol *sym = symtab->find(pair.first());
1512     if (sym && sym->isDefined())
1513       sym->forceExport = true;
1514   }
1515 
1516   // Delay reporting error about explicit exports until after
1517   // addStartStopSymbols which can create optional symbols.
1518   for (auto &name : config->requiredExports) {
1519     Symbol *sym = symtab->find(name);
1520     if (!sym || !sym->isDefined()) {
1521       if (config->unresolvedSymbols == UnresolvedPolicy::ReportError)
1522         error(Twine("symbol exported via --export not found: ") + name);
1523       if (config->unresolvedSymbols == UnresolvedPolicy::Warn)
1524         warn(Twine("symbol exported via --export not found: ") + name);
1525     }
1526   }
1527 
1528   if (config->isPic && !config->sharedMemory) {
1529     // In shared memory mode all data segments are passive and initialized
1530     // via __wasm_init_memory.
1531     log("-- combineOutputSegments");
1532     combineOutputSegments();
1533   }
1534 
1535   log("-- createSyntheticSectionsPostLayout");
1536   createSyntheticSectionsPostLayout();
1537   log("-- populateProducers");
1538   populateProducers();
1539   log("-- calculateImports");
1540   calculateImports();
1541   log("-- scanRelocations");
1542   scanRelocations();
1543   log("-- finalizeIndirectFunctionTable");
1544   finalizeIndirectFunctionTable();
1545   log("-- populateTargetFeatures");
1546   populateTargetFeatures();
1547   log("-- createSyntheticInitFunctions");
1548   createSyntheticInitFunctions();
1549   log("-- assignIndexes");
1550   assignIndexes();
1551   log("-- calculateInitFunctions");
1552   calculateInitFunctions();
1553 
1554   if (!config->relocatable) {
1555     // Create linker synthesized functions
1556     if (WasmSym::applyDataRelocs)
1557       createApplyDataRelocationsFunction();
1558     if (WasmSym::applyGlobalRelocs)
1559       createApplyGlobalRelocationsFunction();
1560     if (WasmSym::applyGlobalTLSRelocs)
1561       createApplyGlobalTLSRelocationsFunction();
1562     if (WasmSym::initMemory)
1563       createInitMemoryFunction();
1564     createStartFunction();
1565 
1566     createCallCtorsFunction();
1567 
1568     // Create export wrappers for commands if needed.
1569     //
1570     // If the input contains a call to `__wasm_call_ctors`, either in one of
1571     // the input objects or an explicit export from the command-line, we
1572     // assume ctors and dtors are taken care of already.
1573     if (!config->relocatable && !config->isPic &&
1574         !WasmSym::callCtors->isUsedInRegularObj &&
1575         !WasmSym::callCtors->isExported()) {
1576       log("-- createCommandExportWrappers");
1577       createCommandExportWrappers();
1578     }
1579   }
1580 
1581   if (WasmSym::initTLS && WasmSym::initTLS->isLive())
1582     createInitTLSFunction();
1583 
1584   if (errorCount())
1585     return;
1586 
1587   log("-- calculateTypes");
1588   calculateTypes();
1589   log("-- calculateExports");
1590   calculateExports();
1591   log("-- calculateCustomSections");
1592   calculateCustomSections();
1593   log("-- populateSymtab");
1594   populateSymtab();
1595   log("-- checkImportExportTargetFeatures");
1596   checkImportExportTargetFeatures();
1597   log("-- addSections");
1598   addSections();
1599 
1600   if (errorHandler().verbose) {
1601     log("Defined Functions: " + Twine(out.functionSec->inputFunctions.size()));
1602     log("Defined Globals  : " + Twine(out.globalSec->numGlobals()));
1603     log("Defined Tags     : " + Twine(out.tagSec->inputTags.size()));
1604     log("Defined Tables   : " + Twine(out.tableSec->inputTables.size()));
1605     log("Function Imports : " +
1606         Twine(out.importSec->getNumImportedFunctions()));
1607     log("Global Imports   : " + Twine(out.importSec->getNumImportedGlobals()));
1608     log("Tag Imports      : " + Twine(out.importSec->getNumImportedTags()));
1609     log("Table Imports    : " + Twine(out.importSec->getNumImportedTables()));
1610   }
1611 
1612   createHeader();
1613   log("-- finalizeSections");
1614   finalizeSections();
1615 
1616   log("-- writeMapFile");
1617   writeMapFile(outputSections);
1618 
1619   log("-- openFile");
1620   openFile();
1621   if (errorCount())
1622     return;
1623 
1624   writeHeader();
1625 
1626   log("-- writeSections");
1627   writeSections();
1628   if (errorCount())
1629     return;
1630 
1631   if (Error e = buffer->commit())
1632     fatal("failed to write the output file: " + toString(std::move(e)));
1633 }
1634 
1635 // Open a result file.
1636 void Writer::openFile() {
1637   log("writing: " + config->outputFile);
1638 
1639   Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
1640       FileOutputBuffer::create(config->outputFile, fileSize,
1641                                FileOutputBuffer::F_executable);
1642 
1643   if (!bufferOrErr)
1644     error("failed to open " + config->outputFile + ": " +
1645           toString(bufferOrErr.takeError()));
1646   else
1647     buffer = std::move(*bufferOrErr);
1648 }
1649 
1650 void Writer::createHeader() {
1651   raw_string_ostream os(header);
1652   writeBytes(os, WasmMagic, sizeof(WasmMagic), "wasm magic");
1653   writeU32(os, WasmVersion, "wasm version");
1654   os.flush();
1655   fileSize += header.size();
1656 }
1657 
1658 void writeResult() { Writer().run(); }
1659 
1660 } // namespace wasm
1661 } // namespace lld
1662