xref: /llvm-project-15.0.7/lld/wasm/Writer.cpp (revision 02f4cfec)
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>(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, fileName});
393         break;
394       case WASM_FEATURE_PREFIX_REQUIRED:
395         used.insert({feature.Name, fileName});
396         required.insert({feature.Name, fileName});
397         break;
398       case WASM_FEATURE_PREFIX_DISALLOWED:
399         disallowed.insert({feature.Name, 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     allowed.insert(used.keys().begin(), used.keys().end());
419 
420   if (allowed.count("atomics") && !config->sharedMemory) {
421     if (inferFeatures)
422       error(Twine("'atomics' feature is used by ") + used["atomics"] +
423             ", so --shared-memory must be used");
424     else
425       error("'atomics' feature is used, so --shared-memory must be used");
426   }
427 
428   if (!config->checkFeatures)
429     return;
430 
431   if (disallowed.count("atomics") && config->sharedMemory)
432     error("'atomics' feature is disallowed by " + disallowed["atomics"] +
433           ", so --shared-memory must not be used");
434 
435   if (!allowed.count("atomics") && config->sharedMemory)
436     error("'atomics' feature must be used in order to use shared "
437           "memory");
438 
439   if (!allowed.count("bulk-memory") && config->sharedMemory)
440     error("'bulk-memory' feature must be used in order to use shared "
441           "memory");
442 
443   if (!allowed.count("bulk-memory") && tlsUsed)
444     error("'bulk-memory' feature must be used in order to use thread-local "
445           "storage");
446 
447   // Validate that used features are allowed in output
448   if (!inferFeatures) {
449     for (auto &feature : used.keys()) {
450       if (!allowed.count(feature))
451         error(Twine("Target feature '") + feature + "' used by " +
452               used[feature] + " is not allowed.");
453     }
454   }
455 
456   // Validate the required and disallowed constraints for each file
457   for (ObjFile *file : symtab->objectFiles) {
458     StringRef fileName(file->getName());
459     SmallSet<std::string, 8> objectFeatures;
460     for (auto &feature : file->getWasmObj()->getTargetFeatures()) {
461       if (feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED)
462         continue;
463       objectFeatures.insert(feature.Name);
464       if (disallowed.count(feature.Name))
465         error(Twine("Target feature '") + feature.Name + "' used in " +
466               fileName + " is disallowed by " + disallowed[feature.Name] +
467               ". Use --no-check-features to suppress.");
468     }
469     for (auto &feature : required.keys()) {
470       if (!objectFeatures.count(feature))
471         error(Twine("Missing target feature '") + feature + "' in " + fileName +
472               ", required by " + required[feature] +
473               ". Use --no-check-features to suppress.");
474     }
475   }
476 }
477 
478 void Writer::calculateImports() {
479   for (Symbol *sym : symtab->getSymbols()) {
480     if (!sym->isUndefined())
481       continue;
482     if (sym->isWeak() && !config->relocatable)
483       continue;
484     if (!sym->isLive())
485       continue;
486     if (!sym->isUsedInRegularObj)
487       continue;
488     // We don't generate imports for data symbols. They however can be imported
489     // as GOT entries.
490     if (isa<DataSymbol>(sym))
491       continue;
492 
493     LLVM_DEBUG(dbgs() << "import: " << sym->getName() << "\n");
494     out.importSec->addImport(sym);
495   }
496 }
497 
498 void Writer::calculateExports() {
499   if (config->relocatable)
500     return;
501 
502   if (!config->relocatable && !config->importMemory)
503     out.exportSec->exports.push_back(
504         WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0});
505 
506   if (!config->relocatable && config->exportTable)
507     out.exportSec->exports.push_back(
508         WasmExport{functionTableName, WASM_EXTERNAL_TABLE, 0});
509 
510   unsigned globalIndex =
511       out.importSec->getNumImportedGlobals() + out.globalSec->numGlobals();
512 
513   for (Symbol *sym : symtab->getSymbols()) {
514     if (!sym->isExported())
515       continue;
516     if (!sym->isLive())
517       continue;
518 
519     StringRef name = sym->getName();
520     WasmExport export_;
521     if (auto *f = dyn_cast<DefinedFunction>(sym)) {
522       export_ = {name, WASM_EXTERNAL_FUNCTION, f->getFunctionIndex()};
523     } else if (auto *g = dyn_cast<DefinedGlobal>(sym)) {
524       // TODO(sbc): Remove this check once to mutable global proposal is
525       // implement in all major browsers.
526       // See: https://github.com/WebAssembly/mutable-global
527       if (g->getGlobalType()->Mutable) {
528         // Only __stack_pointer and __tls_base should ever be create as mutable.
529         assert(g == WasmSym::stackPointer || g == WasmSym::tlsBase);
530         continue;
531       }
532       export_ = {name, WASM_EXTERNAL_GLOBAL, g->getGlobalIndex()};
533     } else if (auto *e = dyn_cast<DefinedEvent>(sym)) {
534       export_ = {name, WASM_EXTERNAL_EVENT, e->getEventIndex()};
535     } else {
536       auto *d = cast<DefinedData>(sym);
537       out.globalSec->dataAddressGlobals.push_back(d);
538       export_ = {name, WASM_EXTERNAL_GLOBAL, globalIndex++};
539     }
540 
541     LLVM_DEBUG(dbgs() << "Export: " << name << "\n");
542     out.exportSec->exports.push_back(export_);
543   }
544 }
545 
546 void Writer::populateSymtab() {
547   if (!config->relocatable && !config->emitRelocs)
548     return;
549 
550   for (Symbol *sym : symtab->getSymbols())
551     if (sym->isUsedInRegularObj && sym->isLive())
552       out.linkingSec->addToSymtab(sym);
553 
554   for (ObjFile *file : symtab->objectFiles) {
555     LLVM_DEBUG(dbgs() << "Local symtab entries: " << file->getName() << "\n");
556     for (Symbol *sym : file->getSymbols())
557       if (sym->isLocal() && !isa<SectionSymbol>(sym) && sym->isLive())
558         out.linkingSec->addToSymtab(sym);
559   }
560 }
561 
562 void Writer::calculateTypes() {
563   // The output type section is the union of the following sets:
564   // 1. Any signature used in the TYPE relocation
565   // 2. The signatures of all imported functions
566   // 3. The signatures of all defined functions
567   // 4. The signatures of all imported events
568   // 5. The signatures of all defined events
569 
570   for (ObjFile *file : symtab->objectFiles) {
571     ArrayRef<WasmSignature> types = file->getWasmObj()->types();
572     for (uint32_t i = 0; i < types.size(); i++)
573       if (file->typeIsUsed[i])
574         file->typeMap[i] = out.typeSec->registerType(types[i]);
575   }
576 
577   for (const Symbol *sym : out.importSec->importedSymbols) {
578     if (auto *f = dyn_cast<FunctionSymbol>(sym))
579       out.typeSec->registerType(*f->signature);
580     else if (auto *e = dyn_cast<EventSymbol>(sym))
581       out.typeSec->registerType(*e->signature);
582   }
583 
584   for (const InputFunction *f : out.functionSec->inputFunctions)
585     out.typeSec->registerType(f->signature);
586 
587   for (const InputEvent *e : out.eventSec->inputEvents)
588     out.typeSec->registerType(e->signature);
589 }
590 
591 static void scanRelocations() {
592   for (ObjFile *file : symtab->objectFiles) {
593     LLVM_DEBUG(dbgs() << "scanRelocations: " << file->getName() << "\n");
594     for (InputChunk *chunk : file->functions)
595       scanRelocations(chunk);
596     for (InputChunk *chunk : file->segments)
597       scanRelocations(chunk);
598     for (auto &p : file->customSections)
599       scanRelocations(p);
600   }
601 }
602 
603 void Writer::assignIndexes() {
604   // Seal the import section, since other index spaces such as function and
605   // global are effected by the number of imports.
606   out.importSec->seal();
607 
608   for (InputFunction *func : symtab->syntheticFunctions)
609     out.functionSec->addFunction(func);
610 
611   for (ObjFile *file : symtab->objectFiles) {
612     LLVM_DEBUG(dbgs() << "Functions: " << file->getName() << "\n");
613     for (InputFunction *func : file->functions)
614       out.functionSec->addFunction(func);
615   }
616 
617   for (InputGlobal *global : symtab->syntheticGlobals)
618     out.globalSec->addGlobal(global);
619 
620   for (ObjFile *file : symtab->objectFiles) {
621     LLVM_DEBUG(dbgs() << "Globals: " << file->getName() << "\n");
622     for (InputGlobal *global : file->globals)
623       out.globalSec->addGlobal(global);
624   }
625 
626   for (ObjFile *file : symtab->objectFiles) {
627     LLVM_DEBUG(dbgs() << "Events: " << file->getName() << "\n");
628     for (InputEvent *event : file->events)
629       out.eventSec->addEvent(event);
630   }
631 
632   out.globalSec->assignIndexes();
633 }
634 
635 static StringRef getOutputDataSegmentName(StringRef name) {
636   // With PIC code we currently only support a single data segment since
637   // we only have a single __memory_base to use as our base address.
638   if (config->isPic)
639     return ".data";
640   // We only support one thread-local segment, so we must merge the segments
641   // despite --no-merge-data-segments.
642   // We also need to merge .tbss into .tdata so they share the same offsets.
643   if (name.startswith(".tdata") || name.startswith(".tbss"))
644     return ".tdata";
645   if (!config->mergeDataSegments)
646     return name;
647   if (name.startswith(".text."))
648     return ".text";
649   if (name.startswith(".data."))
650     return ".data";
651   if (name.startswith(".bss."))
652     return ".bss";
653   if (name.startswith(".rodata."))
654     return ".rodata";
655   return name;
656 }
657 
658 void Writer::createOutputSegments() {
659   for (ObjFile *file : symtab->objectFiles) {
660     for (InputSegment *segment : file->segments) {
661       if (!segment->live)
662         continue;
663       StringRef name = getOutputDataSegmentName(segment->getName());
664       OutputSegment *&s = segmentMap[name];
665       if (s == nullptr) {
666         LLVM_DEBUG(dbgs() << "new segment: " << name << "\n");
667         s = make<OutputSegment>(name);
668         if (config->sharedMemory || name == ".tdata")
669           s->initFlags = WASM_SEGMENT_IS_PASSIVE;
670         // Exported memories are guaranteed to be zero-initialized, so no need
671         // to emit data segments for bss sections.
672         // TODO: consider initializing bss sections with memory.fill
673         // instructions when memory is imported and bulk-memory is available.
674         if (!config->importMemory && !config->relocatable &&
675             name.startswith(".bss"))
676           s->isBss = true;
677         segments.push_back(s);
678       }
679       s->addInputSegment(segment);
680       LLVM_DEBUG(dbgs() << "added data: " << name << ": " << s->size << "\n");
681     }
682   }
683 
684   // Sort segments by type, placing .bss last
685   std::stable_sort(segments.begin(), segments.end(),
686                    [](const OutputSegment *a, const OutputSegment *b) {
687                      auto order = [](StringRef name) {
688                        return StringSwitch<int>(name)
689                            .StartsWith(".rodata", 0)
690                            .StartsWith(".data", 1)
691                            .StartsWith(".tdata", 2)
692                            .StartsWith(".bss", 4)
693                            .Default(3);
694                      };
695                      return order(a->name) < order(b->name);
696                    });
697 
698   for (size_t i = 0; i < segments.size(); ++i)
699     segments[i]->index = i;
700 }
701 
702 static void createFunction(DefinedFunction *func, StringRef bodyContent) {
703   std::string functionBody;
704   {
705     raw_string_ostream os(functionBody);
706     writeUleb128(os, bodyContent.size(), "function size");
707     os << bodyContent;
708   }
709   ArrayRef<uint8_t> body = arrayRefFromStringRef(saver.save(functionBody));
710   cast<SyntheticFunction>(func->function)->setBody(body);
711 }
712 
713 void Writer::createInitMemoryFunction() {
714   LLVM_DEBUG(dbgs() << "createInitMemoryFunction\n");
715   assert(WasmSym::initMemoryFlag);
716   uint32_t flagAddress = WasmSym::initMemoryFlag->getVirtualAddress();
717   std::string bodyContent;
718   {
719     raw_string_ostream os(bodyContent);
720     writeUleb128(os, 0, "num locals");
721 
722     if (segments.size()) {
723       // Initialize memory in a thread-safe manner. The thread that successfully
724       // increments the flag from 0 to 1 is is responsible for performing the
725       // memory initialization. Other threads go sleep on the flag until the
726       // first thread finishing initializing memory, increments the flag to 2,
727       // and wakes all the other threads. Once the flag has been set to 2,
728       // subsequently started threads will skip the sleep. All threads
729       // unconditionally drop their passive data segments once memory has been
730       // initialized. The generated code is as follows:
731       //
732       // (func $__wasm_init_memory
733       //  (if
734       //   (i32.atomic.rmw.cmpxchg align=2 offset=0
735       //    (i32.const $__init_memory_flag)
736       //    (i32.const 0)
737       //    (i32.const 1)
738       //   )
739       //   (then
740       //    (drop
741       //     (i32.atomic.wait align=2 offset=0
742       //      (i32.const $__init_memory_flag)
743       //      (i32.const 1)
744       //      (i32.const -1)
745       //     )
746       //    )
747       //   )
748       //   (else
749       //    ( ... initialize data segments ... )
750       //    (i32.atomic.store align=2 offset=0
751       //     (i32.const $__init_memory_flag)
752       //     (i32.const 2)
753       //    )
754       //    (drop
755       //     (i32.atomic.notify align=2 offset=0
756       //      (i32.const $__init_memory_flag)
757       //      (i32.const -1u)
758       //     )
759       //    )
760       //   )
761       //  )
762       //  ( ... drop data segments ... )
763       // )
764 
765       // Atomically check whether this is the main thread.
766       writeI32Const(os, flagAddress, "flag address");
767       writeI32Const(os, 0, "expected flag value");
768       writeI32Const(os, 1, "flag value");
769       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
770       writeUleb128(os, WASM_OPCODE_I32_RMW_CMPXCHG, "i32.atomic.rmw.cmpxchg");
771       writeMemArg(os, 2, 0);
772       writeU8(os, WASM_OPCODE_IF, "IF");
773       writeU8(os, WASM_TYPE_NORESULT, "blocktype");
774 
775       // Did not increment 0, so wait for main thread to initialize memory
776       writeI32Const(os, flagAddress, "flag address");
777       writeI32Const(os, 1, "expected flag value");
778       writeI64Const(os, -1, "timeout");
779       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
780       writeUleb128(os, WASM_OPCODE_I32_ATOMIC_WAIT, "i32.atomic.wait");
781       writeMemArg(os, 2, 0);
782       writeU8(os, WASM_OPCODE_DROP, "drop");
783 
784       writeU8(os, WASM_OPCODE_ELSE, "ELSE");
785 
786       // Did increment 0, so conditionally initialize passive data segments
787       for (const OutputSegment *s : segments) {
788         if (s->initFlags & WASM_SEGMENT_IS_PASSIVE && s->name != ".tdata") {
789           // destination address
790           writeI32Const(os, s->startVA, "destination address");
791           // source segment offset
792           writeI32Const(os, 0, "segment offset");
793           // memory region size
794           writeI32Const(os, s->size, "memory region size");
795           // memory.init instruction
796           writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
797           writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "memory.init");
798           writeUleb128(os, s->index, "segment index immediate");
799           writeU8(os, 0, "memory index immediate");
800         }
801       }
802 
803       // Set flag to 2 to mark end of initialization
804       writeI32Const(os, flagAddress, "flag address");
805       writeI32Const(os, 2, "flag value");
806       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
807       writeUleb128(os, WASM_OPCODE_I32_ATOMIC_STORE, "i32.atomic.store");
808       writeMemArg(os, 2, 0);
809 
810       // Notify any waiters that memory initialization is complete
811       writeI32Const(os, flagAddress, "flag address");
812       writeI32Const(os, -1, "number of waiters");
813       writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix");
814       writeUleb128(os, WASM_OPCODE_ATOMIC_NOTIFY, "atomic.notify");
815       writeMemArg(os, 2, 0);
816       writeU8(os, WASM_OPCODE_DROP, "drop");
817 
818       writeU8(os, WASM_OPCODE_END, "END");
819 
820       // Unconditionally drop passive data segments
821       for (const OutputSegment *s : segments) {
822         if (s->initFlags & WASM_SEGMENT_IS_PASSIVE && s->name != ".tdata") {
823           // data.drop instruction
824           writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
825           writeUleb128(os, WASM_OPCODE_DATA_DROP, "data.drop");
826           writeUleb128(os, s->index, "segment index immediate");
827         }
828       }
829     }
830     writeU8(os, WASM_OPCODE_END, "END");
831   }
832 
833   createFunction(WasmSym::initMemory, bodyContent);
834 }
835 
836 // For -shared (PIC) output, we create create a synthetic function which will
837 // apply any relocations to the data segments on startup.  This function is
838 // called __wasm_apply_relocs and is added at the beginning of __wasm_call_ctors
839 // before any of the constructors run.
840 void Writer::createApplyRelocationsFunction() {
841   LLVM_DEBUG(dbgs() << "createApplyRelocationsFunction\n");
842   // First write the body's contents to a string.
843   std::string bodyContent;
844   {
845     raw_string_ostream os(bodyContent);
846     writeUleb128(os, 0, "num locals");
847     for (const OutputSegment *seg : segments)
848       for (const InputSegment *inSeg : seg->inputSegments)
849         inSeg->generateRelocationCode(os);
850     writeU8(os, WASM_OPCODE_END, "END");
851   }
852 
853   createFunction(WasmSym::applyRelocs, bodyContent);
854 }
855 
856 // Create synthetic "__wasm_call_ctors" function based on ctor functions
857 // in input object.
858 void Writer::createCallCtorsFunction() {
859   if (!WasmSym::callCtors->isLive())
860     return;
861 
862   // First write the body's contents to a string.
863   std::string bodyContent;
864   {
865     raw_string_ostream os(bodyContent);
866     writeUleb128(os, 0, "num locals");
867 
868     if (config->isPic) {
869       writeU8(os, WASM_OPCODE_CALL, "CALL");
870       writeUleb128(os, WasmSym::applyRelocs->getFunctionIndex(),
871                    "function index");
872     }
873 
874     // Call constructors
875     for (const WasmInitEntry &f : initFunctions) {
876       writeU8(os, WASM_OPCODE_CALL, "CALL");
877       writeUleb128(os, f.sym->getFunctionIndex(), "function index");
878     }
879     writeU8(os, WASM_OPCODE_END, "END");
880   }
881 
882   createFunction(WasmSym::callCtors, bodyContent);
883 }
884 
885 void Writer::createInitTLSFunction() {
886   if (!WasmSym::initTLS->isLive())
887     return;
888 
889   std::string bodyContent;
890   {
891     raw_string_ostream os(bodyContent);
892 
893     OutputSegment *tlsSeg = nullptr;
894     for (auto *seg : segments) {
895       if (seg->name == ".tdata") {
896         tlsSeg = seg;
897         break;
898       }
899     }
900 
901     writeUleb128(os, 0, "num locals");
902     if (tlsSeg) {
903       writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
904       writeUleb128(os, 0, "local index");
905 
906       writeU8(os, WASM_OPCODE_GLOBAL_SET, "global.set");
907       writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "global index");
908 
909       writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
910       writeUleb128(os, 0, "local index");
911 
912       writeI32Const(os, 0, "segment offset");
913 
914       writeI32Const(os, tlsSeg->size, "memory region size");
915 
916       writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
917       writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "MEMORY.INIT");
918       writeUleb128(os, tlsSeg->index, "segment index immediate");
919       writeU8(os, 0, "memory index immediate");
920     }
921     writeU8(os, WASM_OPCODE_END, "end function");
922   }
923 
924   createFunction(WasmSym::initTLS, bodyContent);
925 }
926 
927 // Populate InitFunctions vector with init functions from all input objects.
928 // This is then used either when creating the output linking section or to
929 // synthesize the "__wasm_call_ctors" function.
930 void Writer::calculateInitFunctions() {
931   if (!config->relocatable && !WasmSym::callCtors->isLive())
932     return;
933 
934   for (ObjFile *file : symtab->objectFiles) {
935     const WasmLinkingData &l = file->getWasmObj()->linkingData();
936     for (const WasmInitFunc &f : l.InitFunctions) {
937       FunctionSymbol *sym = file->getFunctionSymbol(f.Symbol);
938       // comdat exclusions can cause init functions be discarded.
939       if (sym->isDiscarded())
940         continue;
941       assert(sym->isLive());
942       if (*sym->signature != WasmSignature{{}, {}})
943         error("invalid signature for init func: " + toString(*sym));
944       LLVM_DEBUG(dbgs() << "initFunctions: " << toString(*sym) << "\n");
945       initFunctions.emplace_back(WasmInitEntry{sym, f.Priority});
946     }
947   }
948 
949   // Sort in order of priority (lowest first) so that they are called
950   // in the correct order.
951   llvm::stable_sort(initFunctions,
952                     [](const WasmInitEntry &l, const WasmInitEntry &r) {
953                       return l.priority < r.priority;
954                     });
955 }
956 
957 void Writer::createSyntheticSections() {
958   out.dylinkSec = make<DylinkSection>();
959   out.typeSec = make<TypeSection>();
960   out.importSec = make<ImportSection>();
961   out.functionSec = make<FunctionSection>();
962   out.tableSec = make<TableSection>();
963   out.memorySec = make<MemorySection>();
964   out.globalSec = make<GlobalSection>();
965   out.eventSec = make<EventSection>();
966   out.exportSec = make<ExportSection>();
967   out.startSec = make<StartSection>(segments.size());
968   out.elemSec = make<ElemSection>();
969   out.dataCountSec = make<DataCountSection>(segments);
970   out.linkingSec = make<LinkingSection>(initFunctions, segments);
971   out.nameSec = make<NameSection>();
972   out.producersSec = make<ProducersSection>();
973   out.targetFeaturesSec = make<TargetFeaturesSection>();
974 }
975 
976 void Writer::run() {
977   if (config->relocatable || config->isPic)
978     config->globalBase = 0;
979 
980   // For PIC code the table base is assigned dynamically by the loader.
981   // For non-PIC, we start at 1 so that accessing table index 0 always traps.
982   if (!config->isPic) {
983     config->tableBase = 1;
984     if (WasmSym::definedTableBase)
985       WasmSym::definedTableBase->setVirtualAddress(config->tableBase);
986   }
987 
988   log("-- createOutputSegments");
989   createOutputSegments();
990   log("-- createSyntheticSections");
991   createSyntheticSections();
992   log("-- populateProducers");
993   populateProducers();
994   log("-- populateTargetFeatures");
995   populateTargetFeatures();
996   log("-- calculateImports");
997   calculateImports();
998   log("-- layoutMemory");
999   layoutMemory();
1000 
1001   if (!config->relocatable) {
1002     // Create linker synthesized __start_SECNAME/__stop_SECNAME symbols
1003     // This has to be done after memory layout is performed.
1004     for (const OutputSegment *seg : segments)
1005       addStartStopSymbols(seg);
1006   }
1007 
1008   log("-- scanRelocations");
1009   scanRelocations();
1010   log("-- assignIndexes");
1011   assignIndexes();
1012   log("-- calculateInitFunctions");
1013   calculateInitFunctions();
1014 
1015   if (!config->relocatable) {
1016     // Create linker synthesized functions
1017     if (config->sharedMemory)
1018       createInitMemoryFunction();
1019     if (config->isPic)
1020       createApplyRelocationsFunction();
1021     createCallCtorsFunction();
1022   }
1023 
1024   if (!config->relocatable && config->sharedMemory && !config->shared)
1025     createInitTLSFunction();
1026 
1027   if (errorCount())
1028     return;
1029 
1030   log("-- calculateTypes");
1031   calculateTypes();
1032   log("-- calculateExports");
1033   calculateExports();
1034   log("-- calculateCustomSections");
1035   calculateCustomSections();
1036   log("-- populateSymtab");
1037   populateSymtab();
1038   log("-- addSections");
1039   addSections();
1040 
1041   if (errorHandler().verbose) {
1042     log("Defined Functions: " + Twine(out.functionSec->inputFunctions.size()));
1043     log("Defined Globals  : " + Twine(out.globalSec->numGlobals()));
1044     log("Defined Events   : " + Twine(out.eventSec->inputEvents.size()));
1045     log("Function Imports : " +
1046         Twine(out.importSec->getNumImportedFunctions()));
1047     log("Global Imports   : " + Twine(out.importSec->getNumImportedGlobals()));
1048     log("Event Imports    : " + Twine(out.importSec->getNumImportedEvents()));
1049     for (ObjFile *file : symtab->objectFiles)
1050       file->dumpInfo();
1051   }
1052 
1053   createHeader();
1054   log("-- finalizeSections");
1055   finalizeSections();
1056 
1057   log("-- openFile");
1058   openFile();
1059   if (errorCount())
1060     return;
1061 
1062   writeHeader();
1063 
1064   log("-- writeSections");
1065   writeSections();
1066   if (errorCount())
1067     return;
1068 
1069   if (Error e = buffer->commit())
1070     fatal("failed to write the output file: " + toString(std::move(e)));
1071 }
1072 
1073 // Open a result file.
1074 void Writer::openFile() {
1075   log("writing: " + config->outputFile);
1076 
1077   Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
1078       FileOutputBuffer::create(config->outputFile, fileSize,
1079                                FileOutputBuffer::F_executable);
1080 
1081   if (!bufferOrErr)
1082     error("failed to open " + config->outputFile + ": " +
1083           toString(bufferOrErr.takeError()));
1084   else
1085     buffer = std::move(*bufferOrErr);
1086 }
1087 
1088 void Writer::createHeader() {
1089   raw_string_ostream os(header);
1090   writeBytes(os, WasmMagic, sizeof(WasmMagic), "wasm magic");
1091   writeU32(os, WasmVersion, "wasm version");
1092   os.flush();
1093   fileSize += header.size();
1094 }
1095 
1096 void writeResult() { Writer().run(); }
1097 
1098 } // namespace wasm
1099 } // namespace lld
1100