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