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