xref: /llvm-project-15.0.7/lld/wasm/Writer.cpp (revision 4da8fa45)
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 "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/Object/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 
47 namespace {
48 
49 // The writer writes a SymbolTable result to a file.
50 class Writer {
51 public:
52   void run();
53 
54 private:
55   void openFile();
56 
57   bool needsPassiveInitialization(const OutputSegment *segment);
58   bool hasPassiveInitializedSegments();
59 
60   void createInitMemoryFunction();
61   void createApplyRelocationsFunction();
62   void createCallCtorsFunction();
63   void createInitTLSFunction();
64 
65   void assignIndexes();
66   void populateSymtab();
67   void populateProducers();
68   void populateTargetFeatures();
69   void calculateInitFunctions();
70   void calculateImports();
71   void calculateExports();
72   void calculateCustomSections();
73   void calculateTypes();
74   void createOutputSegments();
75   void layoutMemory();
76   void createHeader();
77 
78   void addSection(OutputSection *sec);
79 
80   void addSections();
81 
82   void createCustomSections();
83   void createSyntheticSections();
84   void finalizeSections();
85 
86   // Custom sections
87   void createRelocSections();
88 
89   void writeHeader();
90   void writeSections();
91 
92   uint64_t fileSize = 0;
93 
94   std::vector<WasmInitEntry> initFunctions;
95   llvm::StringMap<std::vector<InputSection *>> customSectionMapping;
96 
97   // Elements that are used to construct the final output
98   std::string header;
99   std::vector<OutputSection *> outputSections;
100 
101   std::unique_ptr<FileOutputBuffer> buffer;
102 
103   std::vector<OutputSegment *> segments;
104   llvm::SmallDenseMap<StringRef, OutputSegment *> segmentMap;
105 };
106 
107 } // anonymous namespace
108 
109 void Writer::calculateCustomSections() {
110   log("calculateCustomSections");
111   bool stripDebug = config->stripDebug || config->stripAll;
112   for (ObjFile *file : symtab->objectFiles) {
113     for (InputSection *section : file->customSections) {
114       StringRef name = section->getName();
115       // These custom sections are known the linker and synthesized rather than
116       // blindly copied.
117       if (name == "linking" || name == "name" || name == "producers" ||
118           name == "target_features" || name.startswith("reloc."))
119         continue;
120       // These custom sections are generated by `clang -fembed-bitcode`.
121       // These are used by the rust toolchain to ship LTO data along with
122       // compiled object code, but they don't want this included in the linker
123       // output.
124       if (name == ".llvmbc" || name == ".llvmcmd")
125         continue;
126       // Strip debug section in that option was specified.
127       if (stripDebug && name.startswith(".debug_"))
128         continue;
129       // Otherwise include custom sections by default and concatenate their
130       // contents.
131       customSectionMapping[name].push_back(section);
132     }
133   }
134 }
135 
136 void Writer::createCustomSections() {
137   log("createCustomSections");
138   for (auto &pair : customSectionMapping) {
139     StringRef name = pair.first();
140     LLVM_DEBUG(dbgs() << "createCustomSection: " << name << "\n");
141 
142     OutputSection *sec = make<CustomSection>(std::string(name), pair.second);
143     if (config->relocatable || config->emitRelocs) {
144       auto *sym = make<OutputSectionSymbol>(sec);
145       out.linkingSec->addToSymtab(sym);
146       sec->sectionSym = sym;
147     }
148     addSection(sec);
149   }
150 }
151 
152 // Create relocations sections in the final output.
153 // These are only created when relocatable output is requested.
154 void Writer::createRelocSections() {
155   log("createRelocSections");
156   // Don't use iterator here since we are adding to OutputSection
157   size_t origSize = outputSections.size();
158   for (size_t i = 0; i < origSize; i++) {
159     LLVM_DEBUG(dbgs() << "check section " << i << "\n");
160     OutputSection *sec = outputSections[i];
161 
162     // Count the number of needed sections.
163     uint32_t count = sec->getNumRelocations();
164     if (!count)
165       continue;
166 
167     StringRef name;
168     if (sec->type == WASM_SEC_DATA)
169       name = "reloc.DATA";
170     else if (sec->type == WASM_SEC_CODE)
171       name = "reloc.CODE";
172     else if (sec->type == WASM_SEC_CUSTOM)
173       name = saver.save("reloc." + sec->name);
174     else
175       llvm_unreachable(
176           "relocations only supported for code, data, or custom sections");
177 
178     addSection(make<RelocSection>(name, sec));
179   }
180 }
181 
182 void Writer::populateProducers() {
183   for (ObjFile *file : symtab->objectFiles) {
184     const WasmProducerInfo &info = file->getWasmObj()->getProducerInfo();
185     out.producersSec->addInfo(info);
186   }
187 }
188 
189 void Writer::writeHeader() {
190   memcpy(buffer->getBufferStart(), header.data(), header.size());
191 }
192 
193 void Writer::writeSections() {
194   uint8_t *buf = buffer->getBufferStart();
195   parallelForEach(outputSections, [buf](OutputSection *s) {
196     assert(s->isNeeded());
197     s->writeTo(buf);
198   });
199 }
200 
201 // Fix the memory layout of the output binary.  This assigns memory offsets
202 // to each of the input data sections as well as the explicit stack region.
203 // The default memory layout is as follows, from low to high.
204 //
205 //  - initialized data (starting at Config->globalBase)
206 //  - BSS data (not currently implemented in llvm)
207 //  - explicit stack (Config->ZStackSize)
208 //  - heap start / unallocated
209 //
210 // The --stack-first option means that stack is placed before any static data.
211 // This can be useful since it means that stack overflow traps immediately
212 // rather than overwriting global data, but also increases code size since all
213 // static data loads and stores requires larger offsets.
214 void Writer::layoutMemory() {
215   uint64_t memoryPtr = 0;
216 
217   auto placeStack = [&]() {
218     if (config->relocatable || config->isPic)
219       return;
220     memoryPtr = alignTo(memoryPtr, stackAlignment);
221     if (config->zStackSize != alignTo(config->zStackSize, stackAlignment))
222       error("stack size must be " + Twine(stackAlignment) + "-byte aligned");
223     log("mem: stack size  = " + Twine(config->zStackSize));
224     log("mem: stack base  = " + Twine(memoryPtr));
225     memoryPtr += config->zStackSize;
226     auto *sp = cast<DefinedGlobal>(WasmSym::stackPointer);
227     switch (sp->global->global.InitExpr.Opcode) {
228     case WASM_OPCODE_I32_CONST:
229       sp->global->global.InitExpr.Value.Int32 = memoryPtr;
230       break;
231     case WASM_OPCODE_I64_CONST:
232       sp->global->global.InitExpr.Value.Int64 = memoryPtr;
233       break;
234     default:
235       llvm_unreachable("init expr must be i32/i64.const");
236     }
237     log("mem: stack top   = " + Twine(memoryPtr));
238   };
239 
240   if (config->stackFirst) {
241     placeStack();
242   } else {
243     memoryPtr = config->globalBase;
244     log("mem: global base = " + Twine(config->globalBase));
245   }
246 
247   if (WasmSym::globalBase)
248     WasmSym::globalBase->setVirtualAddress(memoryPtr);
249 
250   uint64_t dataStart = memoryPtr;
251 
252   // Arbitrarily set __dso_handle handle to point to the start of the data
253   // segments.
254   if (WasmSym::dsoHandle)
255     WasmSym::dsoHandle->setVirtualAddress(dataStart);
256 
257   out.dylinkSec->memAlign = 0;
258   for (OutputSegment *seg : segments) {
259     out.dylinkSec->memAlign = std::max(out.dylinkSec->memAlign, seg->alignment);
260     memoryPtr = alignTo(memoryPtr, 1ULL << seg->alignment);
261     seg->startVA = memoryPtr;
262     log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", seg->name,
263                 memoryPtr, seg->size, seg->alignment));
264     memoryPtr += seg->size;
265 
266     if (WasmSym::tlsSize && seg->name == ".tdata") {
267       auto *tlsSize = cast<DefinedGlobal>(WasmSym::tlsSize);
268       assert(tlsSize->global->global.InitExpr.Opcode == WASM_OPCODE_I32_CONST);
269       tlsSize->global->global.InitExpr.Value.Int32 = seg->size;
270 
271       auto *tlsAlign = cast<DefinedGlobal>(WasmSym::tlsAlign);
272       assert(tlsAlign->global->global.InitExpr.Opcode == WASM_OPCODE_I32_CONST);
273       tlsAlign->global->global.InitExpr.Value.Int32 = int64_t{1}
274                                                       << seg->alignment;
275     }
276   }
277 
278   // Make space for the memory initialization flag
279   if (WasmSym::initMemoryFlag) {
280     memoryPtr = alignTo(memoryPtr, 4);
281     WasmSym::initMemoryFlag->setVirtualAddress(memoryPtr);
282     log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}",
283                 "__wasm_init_memory_flag", memoryPtr, 4, 4));
284     memoryPtr += 4;
285   }
286 
287   if (WasmSym::dataEnd)
288     WasmSym::dataEnd->setVirtualAddress(memoryPtr);
289 
290   log("mem: static data = " + Twine(memoryPtr - dataStart));
291 
292   if (config->shared) {
293     out.dylinkSec->memSize = memoryPtr;
294     return;
295   }
296 
297   if (!config->stackFirst)
298     placeStack();
299 
300   // Set `__heap_base` to directly follow the end of the stack or global data.
301   // The fact that this comes last means that a malloc/brk implementation
302   // can grow the heap at runtime.
303   log("mem: heap base   = " + Twine(memoryPtr));
304   if (WasmSym::heapBase)
305     WasmSym::heapBase->setVirtualAddress(memoryPtr);
306 
307   uint64_t maxMemorySetting = 1ULL
308                               << (config->is64.getValueOr(false) ? 48 : 32);
309 
310   if (config->initialMemory != 0) {
311     if (config->initialMemory != alignTo(config->initialMemory, WasmPageSize))
312       error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned");
313     if (memoryPtr > config->initialMemory)
314       error("initial memory too small, " + Twine(memoryPtr) + " bytes needed");
315     if (config->initialMemory > maxMemorySetting)
316       error("initial memory too large, cannot be greater than " +
317             Twine(maxMemorySetting));
318     memoryPtr = config->initialMemory;
319   }
320   out.dylinkSec->memSize = memoryPtr;
321   out.memorySec->numMemoryPages =
322       alignTo(memoryPtr, WasmPageSize) / WasmPageSize;
323   log("mem: total pages = " + Twine(out.memorySec->numMemoryPages));
324 
325   // Check max if explicitly supplied or required by shared memory
326   if (config->maxMemory != 0 || config->sharedMemory) {
327     if (config->maxMemory != alignTo(config->maxMemory, WasmPageSize))
328       error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned");
329     if (memoryPtr > config->maxMemory)
330       error("maximum memory too small, " + Twine(memoryPtr) + " bytes needed");
331     if (config->maxMemory > maxMemorySetting)
332       error("maximum memory too large, cannot be greater than " +
333             Twine(maxMemorySetting));
334     out.memorySec->maxMemoryPages = config->maxMemory / WasmPageSize;
335     log("mem: max pages   = " + Twine(out.memorySec->maxMemoryPages));
336   }
337 }
338 
339 void Writer::addSection(OutputSection *sec) {
340   if (!sec->isNeeded())
341     return;
342   log("addSection: " + toString(*sec));
343   sec->sectionIndex = outputSections.size();
344   outputSections.push_back(sec);
345 }
346 
347 // If a section name is valid as a C identifier (which is rare because of
348 // the leading '.'), linkers are expected to define __start_<secname> and
349 // __stop_<secname> symbols. They are at beginning and end of the section,
350 // respectively. This is not requested by the ELF standard, but GNU ld and
351 // gold provide the feature, and used by many programs.
352 static void addStartStopSymbols(const OutputSegment *seg) {
353   StringRef name = seg->name;
354   if (!isValidCIdentifier(name))
355     return;
356   LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << name << "\n");
357   uint64_t start = seg->startVA;
358   uint64_t stop = start + seg->size;
359   symtab->addOptionalDataSymbol(saver.save("__start_" + name), start);
360   symtab->addOptionalDataSymbol(saver.save("__stop_" + name), stop);
361 }
362 
363 void Writer::addSections() {
364   addSection(out.dylinkSec);
365   addSection(out.typeSec);
366   addSection(out.importSec);
367   addSection(out.functionSec);
368   addSection(out.tableSec);
369   addSection(out.memorySec);
370   addSection(out.eventSec);
371   addSection(out.globalSec);
372   addSection(out.exportSec);
373   addSection(out.startSec);
374   addSection(out.elemSec);
375   addSection(out.dataCountSec);
376 
377   addSection(make<CodeSection>(out.functionSec->inputFunctions));
378   addSection(make<DataSection>(segments));
379 
380   createCustomSections();
381 
382   addSection(out.linkingSec);
383   if (config->emitRelocs || config->relocatable) {
384     createRelocSections();
385   }
386 
387   addSection(out.nameSec);
388   addSection(out.producersSec);
389   addSection(out.targetFeaturesSec);
390 }
391 
392 void Writer::finalizeSections() {
393   for (OutputSection *s : outputSections) {
394     s->setOffset(fileSize);
395     s->finalizeContents();
396     fileSize += s->getSize();
397   }
398 }
399 
400 void Writer::populateTargetFeatures() {
401   StringMap<std::string> used;
402   StringMap<std::string> required;
403   StringMap<std::string> disallowed;
404   SmallSet<std::string, 8> &allowed = out.targetFeaturesSec->features;
405   bool tlsUsed = false;
406 
407   // Only infer used features if user did not specify features
408   bool inferFeatures = !config->features.hasValue();
409 
410   if (!inferFeatures) {
411     auto &explicitFeatures = config->features.getValue();
412     allowed.insert(explicitFeatures.begin(), explicitFeatures.end());
413     if (!config->checkFeatures)
414       return;
415   }
416 
417   // Find the sets of used, required, and disallowed features
418   for (ObjFile *file : symtab->objectFiles) {
419     StringRef fileName(file->getName());
420     for (auto &feature : file->getWasmObj()->getTargetFeatures()) {
421       switch (feature.Prefix) {
422       case WASM_FEATURE_PREFIX_USED:
423         used.insert({feature.Name, std::string(fileName)});
424         break;
425       case WASM_FEATURE_PREFIX_REQUIRED:
426         used.insert({feature.Name, std::string(fileName)});
427         required.insert({feature.Name, std::string(fileName)});
428         break;
429       case WASM_FEATURE_PREFIX_DISALLOWED:
430         disallowed.insert({feature.Name, std::string(fileName)});
431         break;
432       default:
433         error("Unrecognized feature policy prefix " +
434               std::to_string(feature.Prefix));
435       }
436     }
437 
438     // Find TLS data segments
439     auto isTLS = [](InputSegment *segment) {
440       StringRef name = segment->getName();
441       return segment->live &&
442              (name.startswith(".tdata") || name.startswith(".tbss"));
443     };
444     tlsUsed = tlsUsed ||
445               std::any_of(file->segments.begin(), file->segments.end(), isTLS);
446   }
447 
448   if (inferFeatures)
449     for (const auto &key : used.keys())
450       allowed.insert(std::string(key));
451 
452   if (!config->relocatable && allowed.count("atomics") &&
453       !config->sharedMemory) {
454     if (inferFeatures)
455       error(Twine("'atomics' feature is used by ") + used["atomics"] +
456             ", so --shared-memory must be used");
457     else
458       error("'atomics' feature is used, so --shared-memory must be used");
459   }
460 
461   if (!config->checkFeatures)
462     return;
463 
464   if (config->sharedMemory) {
465     if (disallowed.count("shared-mem"))
466       error("--shared-memory is disallowed by " + disallowed["shared-mem"] +
467             " because it was not compiled with 'atomics' or 'bulk-memory' "
468             "features.");
469 
470     for (auto feature : {"atomics", "bulk-memory"})
471       if (!allowed.count(feature))
472         error(StringRef("'") + feature +
473               "' feature must be used in order to use shared memory");
474   }
475 
476   if (tlsUsed) {
477     for (auto feature : {"atomics", "bulk-memory"})
478       if (!allowed.count(feature))
479         error(StringRef("'") + feature +
480               "' feature must be used in order to use thread-local storage");
481   }
482 
483   // Validate that used features are allowed in output
484   if (!inferFeatures) {
485     for (auto &feature : used.keys()) {
486       if (!allowed.count(std::string(feature)))
487         error(Twine("Target feature '") + feature + "' used by " +
488               used[feature] + " is not allowed.");
489     }
490   }
491 
492   // Validate the required and disallowed constraints for each file
493   for (ObjFile *file : symtab->objectFiles) {
494     StringRef fileName(file->getName());
495     SmallSet<std::string, 8> objectFeatures;
496     for (auto &feature : file->getWasmObj()->getTargetFeatures()) {
497       if (feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED)
498         continue;
499       objectFeatures.insert(feature.Name);
500       if (disallowed.count(feature.Name))
501         error(Twine("Target feature '") + feature.Name + "' used in " +
502               fileName + " is disallowed by " + disallowed[feature.Name] +
503               ". Use --no-check-features to suppress.");
504     }
505     for (auto &feature : required.keys()) {
506       if (!objectFeatures.count(std::string(feature)))
507         error(Twine("Missing target feature '") + feature + "' in " + fileName +
508               ", required by " + required[feature] +
509               ". Use --no-check-features to suppress.");
510     }
511   }
512 }
513 
514 void Writer::calculateImports() {
515   for (Symbol *sym : symtab->getSymbols()) {
516     if (!sym->isUndefined())
517       continue;
518     if (sym->isWeak() && !config->relocatable)
519       continue;
520     if (!sym->isLive())
521       continue;
522     if (!sym->isUsedInRegularObj)
523       continue;
524     // We don't generate imports for data symbols. They however can be imported
525     // as GOT entries.
526     if (isa<DataSymbol>(sym))
527       continue;
528 
529     LLVM_DEBUG(dbgs() << "import: " << sym->getName() << "\n");
530     out.importSec->addImport(sym);
531   }
532 }
533 
534 void Writer::calculateExports() {
535   if (config->relocatable)
536     return;
537 
538   if (!config->relocatable && !config->importMemory)
539     out.exportSec->exports.push_back(
540         WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0});
541 
542   if (!config->relocatable && config->exportTable)
543     out.exportSec->exports.push_back(
544         WasmExport{functionTableName, WASM_EXTERNAL_TABLE, 0});
545 
546   unsigned globalIndex =
547       out.importSec->getNumImportedGlobals() + out.globalSec->numGlobals();
548 
549   for (Symbol *sym : symtab->getSymbols()) {
550     if (!sym->isExported())
551       continue;
552     if (!sym->isLive())
553       continue;
554 
555     StringRef name = sym->getName();
556     WasmExport export_;
557     if (auto *f = dyn_cast<DefinedFunction>(sym)) {
558       if (Optional<StringRef> exportName = f->function->getExportName()) {
559         name = *exportName;
560       }
561       export_ = {name, WASM_EXTERNAL_FUNCTION, f->getFunctionIndex()};
562     } else if (auto *g = dyn_cast<DefinedGlobal>(sym)) {
563       // TODO(sbc): Remove this check once to mutable global proposal is
564       // implement in all major browsers.
565       // See: https://github.com/WebAssembly/mutable-global
566       if (g->getGlobalType()->Mutable) {
567         // Only __stack_pointer and __tls_base should ever be create as mutable.
568         assert(g == WasmSym::stackPointer || g == WasmSym::tlsBase);
569         continue;
570       }
571       export_ = {name, WASM_EXTERNAL_GLOBAL, g->getGlobalIndex()};
572     } else if (auto *e = dyn_cast<DefinedEvent>(sym)) {
573       export_ = {name, WASM_EXTERNAL_EVENT, e->getEventIndex()};
574     } else {
575       auto *d = cast<DefinedData>(sym);
576       out.globalSec->dataAddressGlobals.push_back(d);
577       export_ = {name, WASM_EXTERNAL_GLOBAL, globalIndex++};
578     }
579 
580     LLVM_DEBUG(dbgs() << "Export: " << name << "\n");
581     out.exportSec->exports.push_back(export_);
582   }
583 }
584 
585 void Writer::populateSymtab() {
586   if (!config->relocatable && !config->emitRelocs)
587     return;
588 
589   for (Symbol *sym : symtab->getSymbols())
590     if (sym->isUsedInRegularObj && sym->isLive())
591       out.linkingSec->addToSymtab(sym);
592 
593   for (ObjFile *file : symtab->objectFiles) {
594     LLVM_DEBUG(dbgs() << "Local symtab entries: " << file->getName() << "\n");
595     for (Symbol *sym : file->getSymbols())
596       if (sym->isLocal() && !isa<SectionSymbol>(sym) && sym->isLive())
597         out.linkingSec->addToSymtab(sym);
598   }
599 }
600 
601 void Writer::calculateTypes() {
602   // The output type section is the union of the following sets:
603   // 1. Any signature used in the TYPE relocation
604   // 2. The signatures of all imported functions
605   // 3. The signatures of all defined functions
606   // 4. The signatures of all imported events
607   // 5. The signatures of all defined events
608 
609   for (ObjFile *file : symtab->objectFiles) {
610     ArrayRef<WasmSignature> types = file->getWasmObj()->types();
611     for (uint32_t i = 0; i < types.size(); i++)
612       if (file->typeIsUsed[i])
613         file->typeMap[i] = out.typeSec->registerType(types[i]);
614   }
615 
616   for (const Symbol *sym : out.importSec->importedSymbols) {
617     if (auto *f = dyn_cast<FunctionSymbol>(sym))
618       out.typeSec->registerType(*f->signature);
619     else if (auto *e = dyn_cast<EventSymbol>(sym))
620       out.typeSec->registerType(*e->signature);
621   }
622 
623   for (const InputFunction *f : out.functionSec->inputFunctions)
624     out.typeSec->registerType(f->signature);
625 
626   for (const InputEvent *e : out.eventSec->inputEvents)
627     out.typeSec->registerType(e->signature);
628 }
629 
630 static void scanRelocations() {
631   for (ObjFile *file : symtab->objectFiles) {
632     LLVM_DEBUG(dbgs() << "scanRelocations: " << file->getName() << "\n");
633     for (InputChunk *chunk : file->functions)
634       scanRelocations(chunk);
635     for (InputChunk *chunk : file->segments)
636       scanRelocations(chunk);
637     for (auto &p : file->customSections)
638       scanRelocations(p);
639   }
640 }
641 
642 void Writer::assignIndexes() {
643   // Seal the import section, since other index spaces such as function and
644   // global are effected by the number of imports.
645   out.importSec->seal();
646 
647   for (InputFunction *func : symtab->syntheticFunctions)
648     out.functionSec->addFunction(func);
649 
650   for (ObjFile *file : symtab->objectFiles) {
651     LLVM_DEBUG(dbgs() << "Functions: " << file->getName() << "\n");
652     for (InputFunction *func : file->functions)
653       out.functionSec->addFunction(func);
654   }
655 
656   for (InputGlobal *global : symtab->syntheticGlobals)
657     out.globalSec->addGlobal(global);
658 
659   for (ObjFile *file : symtab->objectFiles) {
660     LLVM_DEBUG(dbgs() << "Globals: " << file->getName() << "\n");
661     for (InputGlobal *global : file->globals)
662       out.globalSec->addGlobal(global);
663   }
664 
665   for (ObjFile *file : symtab->objectFiles) {
666     LLVM_DEBUG(dbgs() << "Events: " << file->getName() << "\n");
667     for (InputEvent *event : file->events)
668       out.eventSec->addEvent(event);
669   }
670 
671   out.globalSec->assignIndexes();
672 }
673 
674 static StringRef getOutputDataSegmentName(StringRef name) {
675   // With PIC code we currently only support a single data segment since
676   // we only have a single __memory_base to use as our base address.
677   if (config->isPic)
678     return ".data";
679   // We only support one thread-local segment, so we must merge the segments
680   // despite --no-merge-data-segments.
681   // We also need to merge .tbss into .tdata so they share the same offsets.
682   if (name.startswith(".tdata") || name.startswith(".tbss"))
683     return ".tdata";
684   if (!config->mergeDataSegments)
685     return name;
686   if (name.startswith(".text."))
687     return ".text";
688   if (name.startswith(".data."))
689     return ".data";
690   if (name.startswith(".bss."))
691     return ".bss";
692   if (name.startswith(".rodata."))
693     return ".rodata";
694   return name;
695 }
696 
697 void Writer::createOutputSegments() {
698   for (ObjFile *file : symtab->objectFiles) {
699     for (InputSegment *segment : file->segments) {
700       if (!segment->live)
701         continue;
702       StringRef name = getOutputDataSegmentName(segment->getName());
703       OutputSegment *&s = segmentMap[name];
704       if (s == nullptr) {
705         LLVM_DEBUG(dbgs() << "new segment: " << name << "\n");
706         s = make<OutputSegment>(name);
707         if (config->sharedMemory || name == ".tdata")
708           s->initFlags = WASM_SEGMENT_IS_PASSIVE;
709         // Exported memories are guaranteed to be zero-initialized, so no need
710         // to emit data segments for bss sections.
711         // TODO: consider initializing bss sections with memory.fill
712         // instructions when memory is imported and bulk-memory is available.
713         if (!config->importMemory && !config->relocatable &&
714             name.startswith(".bss"))
715           s->isBss = true;
716         segments.push_back(s);
717       }
718       s->addInputSegment(segment);
719       LLVM_DEBUG(dbgs() << "added data: " << name << ": " << s->size << "\n");
720     }
721   }
722 
723   // Sort segments by type, placing .bss last
724   std::stable_sort(segments.begin(), segments.end(),
725                    [](const OutputSegment *a, const OutputSegment *b) {
726                      auto order = [](StringRef name) {
727                        return StringSwitch<int>(name)
728                            .StartsWith(".rodata", 0)
729                            .StartsWith(".data", 1)
730                            .StartsWith(".tdata", 2)
731                            .StartsWith(".bss", 4)
732                            .Default(3);
733                      };
734                      return order(a->name) < order(b->name);
735                    });
736 
737   for (size_t i = 0; i < segments.size(); ++i)
738     segments[i]->index = i;
739 }
740 
741 static void createFunction(DefinedFunction *func, StringRef bodyContent) {
742   std::string functionBody;
743   {
744     raw_string_ostream os(functionBody);
745     writeUleb128(os, bodyContent.size(), "function size");
746     os << bodyContent;
747   }
748   ArrayRef<uint8_t> body = arrayRefFromStringRef(saver.save(functionBody));
749   cast<SyntheticFunction>(func->function)->setBody(body);
750 }
751 
752 bool Writer::needsPassiveInitialization(const OutputSegment *segment) {
753   return segment->initFlags & WASM_SEGMENT_IS_PASSIVE &&
754          segment->name != ".tdata" && !segment->isBss;
755 }
756 
757 bool Writer::hasPassiveInitializedSegments() {
758   return std::find_if(segments.begin(), segments.end(),
759                       [this](const OutputSegment *s) {
760                         return this->needsPassiveInitialization(s);
761                       }) != segments.end();
762 }
763 
764 void Writer::createInitMemoryFunction() {
765   LLVM_DEBUG(dbgs() << "createInitMemoryFunction\n");
766   assert(WasmSym::initMemoryFlag);
767   uint32_t flagAddress = WasmSym::initMemoryFlag->getVirtualAddress();
768   std::string bodyContent;
769   {
770     raw_string_ostream os(bodyContent);
771     writeUleb128(os, 0, "num locals");
772 
773     if (hasPassiveInitializedSegments()) {
774       // Initialize memory in a thread-safe manner. The thread that successfully
775       // increments the flag from 0 to 1 is is responsible for performing the
776       // memory initialization. Other threads go sleep on the flag until the
777       // first thread finishing initializing memory, increments the flag to 2,
778       // and wakes all the other threads. Once the flag has been set to 2,
779       // subsequently started threads will skip the sleep. All threads
780       // unconditionally drop their passive data segments once memory has been
781       // initialized. The generated code is as follows:
782       //
783       // (func $__wasm_init_memory
784       //  (if
785       //   (i32.atomic.rmw.cmpxchg align=2 offset=0
786       //    (i32.const $__init_memory_flag)
787       //    (i32.const 0)
788       //    (i32.const 1)
789       //   )
790       //   (then
791       //    (drop
792       //     (i32.atomic.wait align=2 offset=0
793       //      (i32.const $__init_memory_flag)
794       //      (i32.const 1)
795       //      (i32.const -1)
796       //     )
797       //    )
798       //   )
799       //   (else
800       //    ( ... initialize data segments ... )
801       //    (i32.atomic.store align=2 offset=0
802       //     (i32.const $__init_memory_flag)
803       //     (i32.const 2)
804       //    )
805       //    (drop
806       //     (i32.atomic.notify align=2 offset=0
807       //      (i32.const $__init_memory_flag)
808       //      (i32.const -1u)
809       //     )
810       //    )
811       //   )
812       //  )
813       //  ( ... drop data segments ... )
814       // )
815 
816       // Atomically check whether this is the main thread.
817       writeI32Const(os, flagAddress, "flag address");
818       writeI32Const(os, 0, "expected flag value");
819       writeI32Const(os, 1, "flag value");
820       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
821       writeUleb128(os, WASM_OPCODE_I32_RMW_CMPXCHG, "i32.atomic.rmw.cmpxchg");
822       writeMemArg(os, 2, 0);
823       writeU8(os, WASM_OPCODE_IF, "IF");
824       writeU8(os, WASM_TYPE_NORESULT, "blocktype");
825 
826       // Did not increment 0, so wait for main thread to initialize memory
827       writeI32Const(os, flagAddress, "flag address");
828       writeI32Const(os, 1, "expected flag value");
829       writeI64Const(os, -1, "timeout");
830       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
831       writeUleb128(os, WASM_OPCODE_I32_ATOMIC_WAIT, "i32.atomic.wait");
832       writeMemArg(os, 2, 0);
833       writeU8(os, WASM_OPCODE_DROP, "drop");
834 
835       writeU8(os, WASM_OPCODE_ELSE, "ELSE");
836 
837       // Did increment 0, so conditionally initialize passive data segments
838       for (const OutputSegment *s : segments) {
839         if (needsPassiveInitialization(s)) {
840           // destination address
841           if (config->is64.getValueOr(false)) {
842             writeI64Const(os, s->startVA, "destination address");
843           } else {
844             writeI32Const(os, static_cast<int32_t>(s->startVA),
845                           "destination address");
846           }
847           // source segment offset
848           writeI32Const(os, 0, "segment offset");
849           // memory region size
850           writeI32Const(os, s->size, "memory region size");
851           // memory.init instruction
852           writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
853           writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "memory.init");
854           writeUleb128(os, s->index, "segment index immediate");
855           writeU8(os, 0, "memory index immediate");
856         }
857       }
858 
859       // Set flag to 2 to mark end of initialization
860       writeI32Const(os, flagAddress, "flag address");
861       writeI32Const(os, 2, "flag value");
862       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
863       writeUleb128(os, WASM_OPCODE_I32_ATOMIC_STORE, "i32.atomic.store");
864       writeMemArg(os, 2, 0);
865 
866       // Notify any waiters that memory initialization is complete
867       writeI32Const(os, flagAddress, "flag address");
868       writeI32Const(os, -1, "number of waiters");
869       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
870       writeUleb128(os, WASM_OPCODE_ATOMIC_NOTIFY, "atomic.notify");
871       writeMemArg(os, 2, 0);
872       writeU8(os, WASM_OPCODE_DROP, "drop");
873 
874       writeU8(os, WASM_OPCODE_END, "END");
875 
876       // Unconditionally drop passive data segments
877       for (const OutputSegment *s : segments) {
878         if (needsPassiveInitialization(s)) {
879           // data.drop instruction
880           writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
881           writeUleb128(os, WASM_OPCODE_DATA_DROP, "data.drop");
882           writeUleb128(os, s->index, "segment index immediate");
883         }
884       }
885     }
886     writeU8(os, WASM_OPCODE_END, "END");
887   }
888 
889   createFunction(WasmSym::initMemory, bodyContent);
890 }
891 
892 // For -shared (PIC) output, we create create a synthetic function which will
893 // apply any relocations to the data segments on startup.  This function is
894 // called __wasm_apply_relocs and is added at the beginning of __wasm_call_ctors
895 // before any of the constructors run.
896 void Writer::createApplyRelocationsFunction() {
897   LLVM_DEBUG(dbgs() << "createApplyRelocationsFunction\n");
898   // First write the body's contents to a string.
899   std::string bodyContent;
900   {
901     raw_string_ostream os(bodyContent);
902     writeUleb128(os, 0, "num locals");
903     for (const OutputSegment *seg : segments)
904       for (const InputSegment *inSeg : seg->inputSegments)
905         inSeg->generateRelocationCode(os);
906     writeU8(os, WASM_OPCODE_END, "END");
907   }
908 
909   createFunction(WasmSym::applyRelocs, bodyContent);
910 }
911 
912 // Create synthetic "__wasm_call_ctors" function based on ctor functions
913 // in input object.
914 void Writer::createCallCtorsFunction() {
915   if (!WasmSym::callCtors->isLive())
916     return;
917 
918   // First write the body's contents to a string.
919   std::string bodyContent;
920   {
921     raw_string_ostream os(bodyContent);
922     writeUleb128(os, 0, "num locals");
923 
924     if (config->isPic) {
925       writeU8(os, WASM_OPCODE_CALL, "CALL");
926       writeUleb128(os, WasmSym::applyRelocs->getFunctionIndex(),
927                    "function index");
928     }
929 
930     // Call constructors
931     for (const WasmInitEntry &f : initFunctions) {
932       writeU8(os, WASM_OPCODE_CALL, "CALL");
933       writeUleb128(os, f.sym->getFunctionIndex(), "function index");
934       for (size_t i = 0; i < f.sym->signature->Returns.size(); i++) {
935         writeU8(os, WASM_OPCODE_DROP, "DROP");
936       }
937     }
938     writeU8(os, WASM_OPCODE_END, "END");
939   }
940 
941   createFunction(WasmSym::callCtors, bodyContent);
942 }
943 
944 void Writer::createInitTLSFunction() {
945   if (!WasmSym::initTLS->isLive())
946     return;
947 
948   std::string bodyContent;
949   {
950     raw_string_ostream os(bodyContent);
951 
952     OutputSegment *tlsSeg = nullptr;
953     for (auto *seg : segments) {
954       if (seg->name == ".tdata") {
955         tlsSeg = seg;
956         break;
957       }
958     }
959 
960     writeUleb128(os, 0, "num locals");
961     if (tlsSeg) {
962       writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
963       writeUleb128(os, 0, "local index");
964 
965       writeU8(os, WASM_OPCODE_GLOBAL_SET, "global.set");
966       writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "global index");
967 
968       // FIXME(wvo): this local needs to be I64 in wasm64, or we need an extend op.
969       writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
970       writeUleb128(os, 0, "local index");
971 
972       writeI32Const(os, 0, "segment offset");
973 
974       writeI32Const(os, tlsSeg->size, "memory region size");
975 
976       writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
977       writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "MEMORY.INIT");
978       writeUleb128(os, tlsSeg->index, "segment index immediate");
979       writeU8(os, 0, "memory index immediate");
980     }
981     writeU8(os, WASM_OPCODE_END, "end function");
982   }
983 
984   createFunction(WasmSym::initTLS, bodyContent);
985 }
986 
987 // Populate InitFunctions vector with init functions from all input objects.
988 // This is then used either when creating the output linking section or to
989 // synthesize the "__wasm_call_ctors" function.
990 void Writer::calculateInitFunctions() {
991   if (!config->relocatable && !WasmSym::callCtors->isLive())
992     return;
993 
994   for (ObjFile *file : symtab->objectFiles) {
995     const WasmLinkingData &l = file->getWasmObj()->linkingData();
996     for (const WasmInitFunc &f : l.InitFunctions) {
997       FunctionSymbol *sym = file->getFunctionSymbol(f.Symbol);
998       // comdat exclusions can cause init functions be discarded.
999       if (sym->isDiscarded())
1000         continue;
1001       assert(sym->isLive());
1002       if (sym->signature->Params.size() != 0)
1003         error("constructor functions cannot take arguments: " + toString(*sym));
1004       LLVM_DEBUG(dbgs() << "initFunctions: " << toString(*sym) << "\n");
1005       initFunctions.emplace_back(WasmInitEntry{sym, f.Priority});
1006     }
1007   }
1008 
1009   // Sort in order of priority (lowest first) so that they are called
1010   // in the correct order.
1011   llvm::stable_sort(initFunctions,
1012                     [](const WasmInitEntry &l, const WasmInitEntry &r) {
1013                       return l.priority < r.priority;
1014                     });
1015 }
1016 
1017 void Writer::createSyntheticSections() {
1018   out.dylinkSec = make<DylinkSection>();
1019   out.typeSec = make<TypeSection>();
1020   out.importSec = make<ImportSection>();
1021   out.functionSec = make<FunctionSection>();
1022   out.tableSec = make<TableSection>();
1023   out.memorySec = make<MemorySection>();
1024   out.eventSec = make<EventSection>();
1025   out.globalSec = make<GlobalSection>();
1026   out.exportSec = make<ExportSection>();
1027   out.startSec = make<StartSection>(hasPassiveInitializedSegments());
1028   out.elemSec = make<ElemSection>();
1029   out.dataCountSec = make<DataCountSection>(segments);
1030   out.linkingSec = make<LinkingSection>(initFunctions, segments);
1031   out.nameSec = make<NameSection>();
1032   out.producersSec = make<ProducersSection>();
1033   out.targetFeaturesSec = make<TargetFeaturesSection>();
1034 }
1035 
1036 void Writer::run() {
1037   if (config->relocatable || config->isPic)
1038     config->globalBase = 0;
1039 
1040   // For PIC code the table base is assigned dynamically by the loader.
1041   // For non-PIC, we start at 1 so that accessing table index 0 always traps.
1042   if (!config->isPic) {
1043     config->tableBase = 1;
1044     if (WasmSym::definedTableBase)
1045       WasmSym::definedTableBase->setVirtualAddress(config->tableBase);
1046   }
1047 
1048   log("-- createOutputSegments");
1049   createOutputSegments();
1050   log("-- createSyntheticSections");
1051   createSyntheticSections();
1052   log("-- populateProducers");
1053   populateProducers();
1054   log("-- populateTargetFeatures");
1055   populateTargetFeatures();
1056   log("-- calculateImports");
1057   calculateImports();
1058   log("-- layoutMemory");
1059   layoutMemory();
1060 
1061   if (!config->relocatable) {
1062     // Create linker synthesized __start_SECNAME/__stop_SECNAME symbols
1063     // This has to be done after memory layout is performed.
1064     for (const OutputSegment *seg : segments)
1065       addStartStopSymbols(seg);
1066   }
1067 
1068   log("-- scanRelocations");
1069   scanRelocations();
1070   log("-- assignIndexes");
1071   assignIndexes();
1072   log("-- calculateInitFunctions");
1073   calculateInitFunctions();
1074 
1075   if (!config->relocatable) {
1076     // Create linker synthesized functions
1077     if (config->sharedMemory)
1078       createInitMemoryFunction();
1079     if (config->isPic)
1080       createApplyRelocationsFunction();
1081     createCallCtorsFunction();
1082   }
1083 
1084   if (!config->relocatable && config->sharedMemory && !config->shared)
1085     createInitTLSFunction();
1086 
1087   if (errorCount())
1088     return;
1089 
1090   log("-- calculateTypes");
1091   calculateTypes();
1092   log("-- calculateExports");
1093   calculateExports();
1094   log("-- calculateCustomSections");
1095   calculateCustomSections();
1096   log("-- populateSymtab");
1097   populateSymtab();
1098   log("-- addSections");
1099   addSections();
1100 
1101   if (errorHandler().verbose) {
1102     log("Defined Functions: " + Twine(out.functionSec->inputFunctions.size()));
1103     log("Defined Globals  : " + Twine(out.globalSec->numGlobals()));
1104     log("Defined Events   : " + Twine(out.eventSec->inputEvents.size()));
1105     log("Function Imports : " +
1106         Twine(out.importSec->getNumImportedFunctions()));
1107     log("Global Imports   : " + Twine(out.importSec->getNumImportedGlobals()));
1108     log("Event Imports    : " + Twine(out.importSec->getNumImportedEvents()));
1109     for (ObjFile *file : symtab->objectFiles)
1110       file->dumpInfo();
1111   }
1112 
1113   createHeader();
1114   log("-- finalizeSections");
1115   finalizeSections();
1116 
1117   log("-- openFile");
1118   openFile();
1119   if (errorCount())
1120     return;
1121 
1122   writeHeader();
1123 
1124   log("-- writeSections");
1125   writeSections();
1126   if (errorCount())
1127     return;
1128 
1129   if (Error e = buffer->commit())
1130     fatal("failed to write the output file: " + toString(std::move(e)));
1131 }
1132 
1133 // Open a result file.
1134 void Writer::openFile() {
1135   log("writing: " + config->outputFile);
1136 
1137   Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
1138       FileOutputBuffer::create(config->outputFile, fileSize,
1139                                FileOutputBuffer::F_executable);
1140 
1141   if (!bufferOrErr)
1142     error("failed to open " + config->outputFile + ": " +
1143           toString(bufferOrErr.takeError()));
1144   else
1145     buffer = std::move(*bufferOrErr);
1146 }
1147 
1148 void Writer::createHeader() {
1149   raw_string_ostream os(header);
1150   writeBytes(os, WasmMagic, sizeof(WasmMagic), "wasm magic");
1151   writeU32(os, WasmVersion, "wasm version");
1152   os.flush();
1153   fileSize += header.size();
1154 }
1155 
1156 void writeResult() { Writer().run(); }
1157 
1158 } // namespace wasm
1159 } // namespace lld
1160