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