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