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