1 //===- SyntheticSections.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 // This file contains linker-synthesized sections.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "SyntheticSections.h"
14 
15 #include "InputChunks.h"
16 #include "InputElement.h"
17 #include "OutputSegment.h"
18 #include "SymbolTable.h"
19 #include "llvm/Support/Path.h"
20 
21 using namespace llvm;
22 using namespace llvm::wasm;
23 
24 namespace lld {
25 namespace wasm {
26 
27 OutStruct out;
28 
29 namespace {
30 
31 // Some synthetic sections (e.g. "name" and "linking") have subsections.
32 // Just like the synthetic sections themselves these need to be created before
33 // they can be written out (since they are preceded by their length). This
34 // class is used to create subsections and then write them into the stream
35 // of the parent section.
36 class SubSection {
37 public:
38   explicit SubSection(uint32_t type) : type(type) {}
39 
40   void writeTo(raw_ostream &to) {
41     os.flush();
42     writeUleb128(to, type, "subsection type");
43     writeUleb128(to, body.size(), "subsection size");
44     to.write(body.data(), body.size());
45   }
46 
47 private:
48   uint32_t type;
49   std::string body;
50 
51 public:
52   raw_string_ostream os{body};
53 };
54 
55 } // namespace
56 
57 void DylinkSection::writeBody() {
58   raw_ostream &os = bodyOutputStream;
59 
60   {
61     SubSection sub(WASM_DYLINK_MEM_INFO);
62     writeUleb128(sub.os, memSize, "MemSize");
63     writeUleb128(sub.os, memAlign, "MemAlign");
64     writeUleb128(sub.os, out.elemSec->numEntries(), "TableSize");
65     writeUleb128(sub.os, 0, "TableAlign");
66     sub.writeTo(os);
67   }
68 
69   if (symtab->sharedFiles.size()) {
70     SubSection sub(WASM_DYLINK_NEEDED);
71     writeUleb128(sub.os, symtab->sharedFiles.size(), "Needed");
72     for (auto *so : symtab->sharedFiles)
73       writeStr(sub.os, llvm::sys::path::filename(so->getName()), "so name");
74     sub.writeTo(os);
75   }
76 
77   // Under certain circumstances we need to include extra information about our
78   // exports and/or imports to the dynamic linker.
79   // For exports we need to notify the linker when an export is TLS since the
80   // exported value is relative to __tls_base rather than __memory_base.
81   // For imports we need to notify the dynamic linker when an import is weak
82   // so that knows not to report an error for such symbols.
83   std::vector<const Symbol *> importInfo;
84   std::vector<const Symbol *> exportInfo;
85   for (const Symbol *sym : symtab->getSymbols()) {
86     if (sym->isLive()) {
87       if (sym->isExported() && sym->isTLS() && isa<DefinedData>(sym)) {
88         exportInfo.push_back(sym);
89       }
90       if (sym->isUndefWeak()) {
91         importInfo.push_back(sym);
92       }
93     }
94   }
95 
96   if (!exportInfo.empty()) {
97     SubSection sub(WASM_DYLINK_EXPORT_INFO);
98     writeUleb128(sub.os, exportInfo.size(), "num exports");
99 
100     for (const Symbol *sym : exportInfo) {
101       LLVM_DEBUG(llvm::dbgs() << "export info: " << toString(*sym) << "\n");
102       StringRef name = sym->getName();
103       if (auto *f = dyn_cast<DefinedFunction>(sym)) {
104         if (Optional<StringRef> exportName = f->function->getExportName()) {
105           name = *exportName;
106         }
107       }
108       writeStr(sub.os, name, "sym name");
109       writeUleb128(sub.os, sym->flags, "sym flags");
110     }
111 
112     sub.writeTo(os);
113   }
114 
115   if (!importInfo.empty()) {
116     SubSection sub(WASM_DYLINK_IMPORT_INFO);
117     writeUleb128(sub.os, importInfo.size(), "num imports");
118 
119     for (const Symbol *sym : importInfo) {
120       LLVM_DEBUG(llvm::dbgs() << "imports info: " << toString(*sym) << "\n");
121       StringRef module = sym->importModule.getValueOr(defaultModule);
122       StringRef name = sym->importName.getValueOr(sym->getName());
123       writeStr(sub.os, module, "import module");
124       writeStr(sub.os, name, "import name");
125       writeUleb128(sub.os, sym->flags, "sym flags");
126     }
127 
128     sub.writeTo(os);
129   }
130 }
131 
132 uint32_t TypeSection::registerType(const WasmSignature &sig) {
133   auto pair = typeIndices.insert(std::make_pair(sig, types.size()));
134   if (pair.second) {
135     LLVM_DEBUG(llvm::dbgs() << "type " << toString(sig) << "\n");
136     types.push_back(&sig);
137   }
138   return pair.first->second;
139 }
140 
141 uint32_t TypeSection::lookupType(const WasmSignature &sig) {
142   auto it = typeIndices.find(sig);
143   if (it == typeIndices.end()) {
144     error("type not found: " + toString(sig));
145     return 0;
146   }
147   return it->second;
148 }
149 
150 void TypeSection::writeBody() {
151   writeUleb128(bodyOutputStream, types.size(), "type count");
152   for (const WasmSignature *sig : types)
153     writeSig(bodyOutputStream, *sig);
154 }
155 
156 uint32_t ImportSection::getNumImports() const {
157   assert(isSealed);
158   uint32_t numImports = importedSymbols.size() + gotSymbols.size();
159   if (config->importMemory)
160     ++numImports;
161   return numImports;
162 }
163 
164 void ImportSection::addGOTEntry(Symbol *sym) {
165   assert(!isSealed);
166   if (sym->hasGOTIndex())
167     return;
168   LLVM_DEBUG(dbgs() << "addGOTEntry: " << toString(*sym) << "\n");
169   sym->setGOTIndex(numImportedGlobals++);
170   gotSymbols.push_back(sym);
171 }
172 
173 void ImportSection::addImport(Symbol *sym) {
174   assert(!isSealed);
175   StringRef module = sym->importModule.getValueOr(defaultModule);
176   StringRef name = sym->importName.getValueOr(sym->getName());
177   if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
178     ImportKey<WasmSignature> key(*(f->getSignature()), module, name);
179     auto entry = importedFunctions.try_emplace(key, numImportedFunctions);
180     if (entry.second) {
181       importedSymbols.emplace_back(sym);
182       f->setFunctionIndex(numImportedFunctions++);
183     } else {
184       f->setFunctionIndex(entry.first->second);
185     }
186   } else if (auto *g = dyn_cast<GlobalSymbol>(sym)) {
187     ImportKey<WasmGlobalType> key(*(g->getGlobalType()), module, name);
188     auto entry = importedGlobals.try_emplace(key, numImportedGlobals);
189     if (entry.second) {
190       importedSymbols.emplace_back(sym);
191       g->setGlobalIndex(numImportedGlobals++);
192     } else {
193       g->setGlobalIndex(entry.first->second);
194     }
195   } else if (auto *t = dyn_cast<TagSymbol>(sym)) {
196     ImportKey<WasmSignature> key(*(t->getSignature()), module, name);
197     auto entry = importedTags.try_emplace(key, numImportedTags);
198     if (entry.second) {
199       importedSymbols.emplace_back(sym);
200       t->setTagIndex(numImportedTags++);
201     } else {
202       t->setTagIndex(entry.first->second);
203     }
204   } else {
205     assert(TableSymbol::classof(sym));
206     auto *table = cast<TableSymbol>(sym);
207     ImportKey<WasmTableType> key(*(table->getTableType()), module, name);
208     auto entry = importedTables.try_emplace(key, numImportedTables);
209     if (entry.second) {
210       importedSymbols.emplace_back(sym);
211       table->setTableNumber(numImportedTables++);
212     } else {
213       table->setTableNumber(entry.first->second);
214     }
215   }
216 }
217 
218 void ImportSection::writeBody() {
219   raw_ostream &os = bodyOutputStream;
220 
221   writeUleb128(os, getNumImports(), "import count");
222 
223   bool is64 = config->is64.getValueOr(false);
224 
225   if (config->importMemory) {
226     WasmImport import;
227     import.Module = defaultModule;
228     import.Field = "memory";
229     import.Kind = WASM_EXTERNAL_MEMORY;
230     import.Memory.Flags = 0;
231     import.Memory.Minimum = out.memorySec->numMemoryPages;
232     if (out.memorySec->maxMemoryPages != 0 || config->sharedMemory) {
233       import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
234       import.Memory.Maximum = out.memorySec->maxMemoryPages;
235     }
236     if (config->sharedMemory)
237       import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED;
238     if (is64)
239       import.Memory.Flags |= WASM_LIMITS_FLAG_IS_64;
240     writeImport(os, import);
241   }
242 
243   for (const Symbol *sym : importedSymbols) {
244     WasmImport import;
245     import.Field = sym->importName.getValueOr(sym->getName());
246     import.Module = sym->importModule.getValueOr(defaultModule);
247 
248     if (auto *functionSym = dyn_cast<FunctionSymbol>(sym)) {
249       import.Kind = WASM_EXTERNAL_FUNCTION;
250       import.SigIndex = out.typeSec->lookupType(*functionSym->signature);
251     } else if (auto *globalSym = dyn_cast<GlobalSymbol>(sym)) {
252       import.Kind = WASM_EXTERNAL_GLOBAL;
253       import.Global = *globalSym->getGlobalType();
254     } else if (auto *tagSym = dyn_cast<TagSymbol>(sym)) {
255       import.Kind = WASM_EXTERNAL_TAG;
256       import.SigIndex = out.typeSec->lookupType(*tagSym->signature);
257     } else {
258       auto *tableSym = cast<TableSymbol>(sym);
259       import.Kind = WASM_EXTERNAL_TABLE;
260       import.Table = *tableSym->getTableType();
261     }
262     writeImport(os, import);
263   }
264 
265   for (const Symbol *sym : gotSymbols) {
266     WasmImport import;
267     import.Kind = WASM_EXTERNAL_GLOBAL;
268     auto ptrType = is64 ? WASM_TYPE_I64 : WASM_TYPE_I32;
269     import.Global = {static_cast<uint8_t>(ptrType), true};
270     if (isa<DataSymbol>(sym))
271       import.Module = "GOT.mem";
272     else
273       import.Module = "GOT.func";
274     import.Field = sym->getName();
275     writeImport(os, import);
276   }
277 }
278 
279 void FunctionSection::writeBody() {
280   raw_ostream &os = bodyOutputStream;
281 
282   writeUleb128(os, inputFunctions.size(), "function count");
283   for (const InputFunction *func : inputFunctions)
284     writeUleb128(os, out.typeSec->lookupType(func->signature), "sig index");
285 }
286 
287 void FunctionSection::addFunction(InputFunction *func) {
288   if (!func->live)
289     return;
290   uint32_t functionIndex =
291       out.importSec->getNumImportedFunctions() + inputFunctions.size();
292   inputFunctions.emplace_back(func);
293   func->setFunctionIndex(functionIndex);
294 }
295 
296 void TableSection::writeBody() {
297   raw_ostream &os = bodyOutputStream;
298 
299   writeUleb128(os, inputTables.size(), "table count");
300   for (const InputTable *table : inputTables)
301     writeTableType(os, table->getType());
302 }
303 
304 void TableSection::addTable(InputTable *table) {
305   if (!table->live)
306     return;
307   // Some inputs require that the indirect function table be assigned to table
308   // number 0.
309   if (config->legacyFunctionTable &&
310       isa<DefinedTable>(WasmSym::indirectFunctionTable) &&
311       cast<DefinedTable>(WasmSym::indirectFunctionTable)->table == table) {
312     if (out.importSec->getNumImportedTables()) {
313       // Alack!  Some other input imported a table, meaning that we are unable
314       // to assign table number 0 to the indirect function table.
315       for (const auto *culprit : out.importSec->importedSymbols) {
316         if (isa<UndefinedTable>(culprit)) {
317           error("object file not built with 'reference-types' feature "
318                 "conflicts with import of table " +
319                 culprit->getName() + " by file " +
320                 toString(culprit->getFile()));
321           return;
322         }
323       }
324       llvm_unreachable("failed to find conflicting table import");
325     }
326     inputTables.insert(inputTables.begin(), table);
327     return;
328   }
329   inputTables.push_back(table);
330 }
331 
332 void TableSection::assignIndexes() {
333   uint32_t tableNumber = out.importSec->getNumImportedTables();
334   for (InputTable *t : inputTables)
335     t->assignIndex(tableNumber++);
336 }
337 
338 void MemorySection::writeBody() {
339   raw_ostream &os = bodyOutputStream;
340 
341   bool hasMax = maxMemoryPages != 0 || config->sharedMemory;
342   writeUleb128(os, 1, "memory count");
343   unsigned flags = 0;
344   if (hasMax)
345     flags |= WASM_LIMITS_FLAG_HAS_MAX;
346   if (config->sharedMemory)
347     flags |= WASM_LIMITS_FLAG_IS_SHARED;
348   if (config->is64.getValueOr(false))
349     flags |= WASM_LIMITS_FLAG_IS_64;
350   writeUleb128(os, flags, "memory limits flags");
351   writeUleb128(os, numMemoryPages, "initial pages");
352   if (hasMax)
353     writeUleb128(os, maxMemoryPages, "max pages");
354 }
355 
356 void TagSection::writeBody() {
357   raw_ostream &os = bodyOutputStream;
358 
359   writeUleb128(os, inputTags.size(), "tag count");
360   for (InputTag *t : inputTags) {
361     writeUleb128(os, 0, "tag attribute"); // Reserved "attribute" field
362     writeUleb128(os, out.typeSec->lookupType(t->signature), "sig index");
363   }
364 }
365 
366 void TagSection::addTag(InputTag *tag) {
367   if (!tag->live)
368     return;
369   uint32_t tagIndex = out.importSec->getNumImportedTags() + inputTags.size();
370   LLVM_DEBUG(dbgs() << "addTag: " << tagIndex << "\n");
371   tag->assignIndex(tagIndex);
372   inputTags.push_back(tag);
373 }
374 
375 void GlobalSection::assignIndexes() {
376   uint32_t globalIndex = out.importSec->getNumImportedGlobals();
377   for (InputGlobal *g : inputGlobals)
378     g->assignIndex(globalIndex++);
379   for (Symbol *sym : internalGotSymbols)
380     sym->setGOTIndex(globalIndex++);
381   isSealed = true;
382 }
383 
384 static void ensureIndirectFunctionTable() {
385   if (!WasmSym::indirectFunctionTable)
386     WasmSym::indirectFunctionTable =
387         symtab->resolveIndirectFunctionTable(/*required =*/true);
388 }
389 
390 void GlobalSection::addInternalGOTEntry(Symbol *sym) {
391   assert(!isSealed);
392   if (sym->requiresGOT)
393     return;
394   LLVM_DEBUG(dbgs() << "addInternalGOTEntry: " << sym->getName() << " "
395                     << toString(sym->kind()) << "\n");
396   sym->requiresGOT = true;
397   if (auto *F = dyn_cast<FunctionSymbol>(sym)) {
398     ensureIndirectFunctionTable();
399     out.elemSec->addEntry(F);
400   }
401   internalGotSymbols.push_back(sym);
402 }
403 
404 void GlobalSection::generateRelocationCode(raw_ostream &os, bool TLS) const {
405   bool is64 = config->is64.getValueOr(false);
406   unsigned opcode_ptr_const = is64 ? WASM_OPCODE_I64_CONST
407                                    : WASM_OPCODE_I32_CONST;
408   unsigned opcode_ptr_add = is64 ? WASM_OPCODE_I64_ADD
409                                  : WASM_OPCODE_I32_ADD;
410 
411   for (const Symbol *sym : internalGotSymbols) {
412     if (TLS != sym->isTLS())
413       continue;
414 
415     if (auto *d = dyn_cast<DefinedData>(sym)) {
416       // Get __memory_base
417       writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
418       if (sym->isTLS())
419         writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "__tls_base");
420       else
421         writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(),
422                      "__memory_base");
423 
424       // Add the virtual address of the data symbol
425       writeU8(os, opcode_ptr_const, "CONST");
426       writeSleb128(os, d->getVA(), "offset");
427     } else if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
428       if (f->isStub)
429         continue;
430       // Get __table_base
431       writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
432       writeUleb128(os, WasmSym::tableBase->getGlobalIndex(), "__table_base");
433 
434       // Add the table index to __table_base
435       writeU8(os, opcode_ptr_const, "CONST");
436       writeSleb128(os, f->getTableIndex(), "offset");
437     } else {
438       assert(isa<UndefinedData>(sym));
439       continue;
440     }
441     writeU8(os, opcode_ptr_add, "ADD");
442     writeU8(os, WASM_OPCODE_GLOBAL_SET, "GLOBAL_SET");
443     writeUleb128(os, sym->getGOTIndex(), "got_entry");
444   }
445 }
446 
447 void GlobalSection::writeBody() {
448   raw_ostream &os = bodyOutputStream;
449 
450   writeUleb128(os, numGlobals(), "global count");
451   for (InputGlobal *g : inputGlobals) {
452     writeGlobalType(os, g->getType());
453     writeInitExpr(os, g->getInitExpr());
454   }
455   bool is64 = config->is64.getValueOr(false);
456   uint8_t itype = is64 ? WASM_TYPE_I64 : WASM_TYPE_I32;
457   for (const Symbol *sym : internalGotSymbols) {
458     bool mutable_ = false;
459     if (!sym->isStub) {
460       // In the case of dynamic linking, these global must to be mutable since
461       // they get updated to the correct runtime value during
462       // `__wasm_apply_global_relocs`.
463       if (config->isPic && !sym->isTLS())
464         mutable_ = true;
465       // With multi-theadeding any TLS globals must be mutable since they get
466       // set during `__wasm_apply_global_tls_relocs`
467       if (config->sharedMemory && sym->isTLS())
468         mutable_ = true;
469     }
470     WasmGlobalType type{itype, mutable_};
471     WasmInitExpr initExpr;
472     if (auto *d = dyn_cast<DefinedData>(sym))
473       initExpr = intConst(d->getVA(), is64);
474     else if (auto *f = dyn_cast<FunctionSymbol>(sym))
475       initExpr = intConst(f->isStub ? 0 : f->getTableIndex(), is64);
476     else {
477       assert(isa<UndefinedData>(sym));
478       initExpr = intConst(0, is64);
479     }
480     writeGlobalType(os, type);
481     writeInitExpr(os, initExpr);
482   }
483   for (const DefinedData *sym : dataAddressGlobals) {
484     WasmGlobalType type{itype, false};
485     writeGlobalType(os, type);
486     writeInitExpr(os, intConst(sym->getVA(), is64));
487   }
488 }
489 
490 void GlobalSection::addGlobal(InputGlobal *global) {
491   assert(!isSealed);
492   if (!global->live)
493     return;
494   inputGlobals.push_back(global);
495 }
496 
497 void ExportSection::writeBody() {
498   raw_ostream &os = bodyOutputStream;
499 
500   writeUleb128(os, exports.size(), "export count");
501   for (const WasmExport &export_ : exports)
502     writeExport(os, export_);
503 }
504 
505 bool StartSection::isNeeded() const {
506   return WasmSym::startFunction != nullptr;
507 }
508 
509 void StartSection::writeBody() {
510   raw_ostream &os = bodyOutputStream;
511   writeUleb128(os, WasmSym::startFunction->getFunctionIndex(),
512                "function index");
513 }
514 
515 void ElemSection::addEntry(FunctionSymbol *sym) {
516   // Don't add stub functions to the wasm table.  The address of all stub
517   // functions should be zero and they should they don't appear in the table.
518   // They only exist so that the calls to missing functions can validate.
519   if (sym->hasTableIndex() || sym->isStub)
520     return;
521   sym->setTableIndex(config->tableBase + indirectFunctions.size());
522   indirectFunctions.emplace_back(sym);
523 }
524 
525 void ElemSection::writeBody() {
526   raw_ostream &os = bodyOutputStream;
527 
528   assert(WasmSym::indirectFunctionTable);
529   writeUleb128(os, 1, "segment count");
530   uint32_t tableNumber = WasmSym::indirectFunctionTable->getTableNumber();
531   uint32_t flags = 0;
532   if (tableNumber)
533     flags |= WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER;
534   writeUleb128(os, flags, "elem segment flags");
535   if (flags & WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER)
536     writeUleb128(os, tableNumber, "table number");
537 
538   WasmInitExpr initExpr;
539   if (config->isPic) {
540     initExpr.Opcode = WASM_OPCODE_GLOBAL_GET;
541     initExpr.Value.Global =
542         (config->is64.getValueOr(false) ? WasmSym::tableBase32
543                                         : WasmSym::tableBase)
544             ->getGlobalIndex();
545   } else {
546     initExpr.Opcode = WASM_OPCODE_I32_CONST;
547     initExpr.Value.Int32 = config->tableBase;
548   }
549   writeInitExpr(os, initExpr);
550 
551   if (flags & WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND) {
552     // We only write active function table initializers, for which the elem kind
553     // is specified to be written as 0x00 and interpreted to mean "funcref".
554     const uint8_t elemKind = 0;
555     writeU8(os, elemKind, "elem kind");
556   }
557 
558   writeUleb128(os, indirectFunctions.size(), "elem count");
559   uint32_t tableIndex = config->tableBase;
560   for (const FunctionSymbol *sym : indirectFunctions) {
561     assert(sym->getTableIndex() == tableIndex);
562     writeUleb128(os, sym->getFunctionIndex(), "function index");
563     ++tableIndex;
564   }
565 }
566 
567 DataCountSection::DataCountSection(ArrayRef<OutputSegment *> segments)
568     : SyntheticSection(llvm::wasm::WASM_SEC_DATACOUNT),
569       numSegments(std::count_if(segments.begin(), segments.end(),
570                                 [](OutputSegment *const segment) {
571                                   return segment->requiredInBinary();
572                                 })) {}
573 
574 void DataCountSection::writeBody() {
575   writeUleb128(bodyOutputStream, numSegments, "data count");
576 }
577 
578 bool DataCountSection::isNeeded() const {
579   return numSegments && config->sharedMemory;
580 }
581 
582 void LinkingSection::writeBody() {
583   raw_ostream &os = bodyOutputStream;
584 
585   writeUleb128(os, WasmMetadataVersion, "Version");
586 
587   if (!symtabEntries.empty()) {
588     SubSection sub(WASM_SYMBOL_TABLE);
589     writeUleb128(sub.os, symtabEntries.size(), "num symbols");
590 
591     for (const Symbol *sym : symtabEntries) {
592       assert(sym->isDefined() || sym->isUndefined());
593       WasmSymbolType kind = sym->getWasmType();
594       uint32_t flags = sym->flags;
595 
596       writeU8(sub.os, kind, "sym kind");
597       writeUleb128(sub.os, flags, "sym flags");
598 
599       if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
600         if (auto *d = dyn_cast<DefinedFunction>(sym)) {
601           writeUleb128(sub.os, d->getExportedFunctionIndex(), "index");
602         } else {
603           writeUleb128(sub.os, f->getFunctionIndex(), "index");
604         }
605         if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
606           writeStr(sub.os, sym->getName(), "sym name");
607       } else if (auto *g = dyn_cast<GlobalSymbol>(sym)) {
608         writeUleb128(sub.os, g->getGlobalIndex(), "index");
609         if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
610           writeStr(sub.os, sym->getName(), "sym name");
611       } else if (auto *t = dyn_cast<TagSymbol>(sym)) {
612         writeUleb128(sub.os, t->getTagIndex(), "index");
613         if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
614           writeStr(sub.os, sym->getName(), "sym name");
615       } else if (auto *t = dyn_cast<TableSymbol>(sym)) {
616         writeUleb128(sub.os, t->getTableNumber(), "table number");
617         if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
618           writeStr(sub.os, sym->getName(), "sym name");
619       } else if (isa<DataSymbol>(sym)) {
620         writeStr(sub.os, sym->getName(), "sym name");
621         if (auto *dataSym = dyn_cast<DefinedData>(sym)) {
622           writeUleb128(sub.os, dataSym->getOutputSegmentIndex(), "index");
623           writeUleb128(sub.os, dataSym->getOutputSegmentOffset(),
624                        "data offset");
625           writeUleb128(sub.os, dataSym->getSize(), "data size");
626         }
627       } else {
628         auto *s = cast<OutputSectionSymbol>(sym);
629         writeUleb128(sub.os, s->section->sectionIndex, "sym section index");
630       }
631     }
632 
633     sub.writeTo(os);
634   }
635 
636   if (dataSegments.size()) {
637     SubSection sub(WASM_SEGMENT_INFO);
638     writeUleb128(sub.os, dataSegments.size(), "num data segments");
639     for (const OutputSegment *s : dataSegments) {
640       writeStr(sub.os, s->name, "segment name");
641       writeUleb128(sub.os, s->alignment, "alignment");
642       writeUleb128(sub.os, s->linkingFlags, "flags");
643     }
644     sub.writeTo(os);
645   }
646 
647   if (!initFunctions.empty()) {
648     SubSection sub(WASM_INIT_FUNCS);
649     writeUleb128(sub.os, initFunctions.size(), "num init functions");
650     for (const WasmInitEntry &f : initFunctions) {
651       writeUleb128(sub.os, f.priority, "priority");
652       writeUleb128(sub.os, f.sym->getOutputSymbolIndex(), "function index");
653     }
654     sub.writeTo(os);
655   }
656 
657   struct ComdatEntry {
658     unsigned kind;
659     uint32_t index;
660   };
661   std::map<StringRef, std::vector<ComdatEntry>> comdats;
662 
663   for (const InputFunction *f : out.functionSec->inputFunctions) {
664     StringRef comdat = f->getComdatName();
665     if (!comdat.empty())
666       comdats[comdat].emplace_back(
667           ComdatEntry{WASM_COMDAT_FUNCTION, f->getFunctionIndex()});
668   }
669   for (uint32_t i = 0; i < dataSegments.size(); ++i) {
670     const auto &inputSegments = dataSegments[i]->inputSegments;
671     if (inputSegments.empty())
672       continue;
673     StringRef comdat = inputSegments[0]->getComdatName();
674 #ifndef NDEBUG
675     for (const InputChunk *isec : inputSegments)
676       assert(isec->getComdatName() == comdat);
677 #endif
678     if (!comdat.empty())
679       comdats[comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, i});
680   }
681 
682   if (!comdats.empty()) {
683     SubSection sub(WASM_COMDAT_INFO);
684     writeUleb128(sub.os, comdats.size(), "num comdats");
685     for (const auto &c : comdats) {
686       writeStr(sub.os, c.first, "comdat name");
687       writeUleb128(sub.os, 0, "comdat flags"); // flags for future use
688       writeUleb128(sub.os, c.second.size(), "num entries");
689       for (const ComdatEntry &entry : c.second) {
690         writeU8(sub.os, entry.kind, "entry kind");
691         writeUleb128(sub.os, entry.index, "entry index");
692       }
693     }
694     sub.writeTo(os);
695   }
696 }
697 
698 void LinkingSection::addToSymtab(Symbol *sym) {
699   sym->setOutputSymbolIndex(symtabEntries.size());
700   symtabEntries.emplace_back(sym);
701 }
702 
703 unsigned NameSection::numNamedFunctions() const {
704   unsigned numNames = out.importSec->getNumImportedFunctions();
705 
706   for (const InputFunction *f : out.functionSec->inputFunctions)
707     if (!f->getName().empty() || !f->getDebugName().empty())
708       ++numNames;
709 
710   return numNames;
711 }
712 
713 unsigned NameSection::numNamedGlobals() const {
714   unsigned numNames = out.importSec->getNumImportedGlobals();
715 
716   for (const InputGlobal *g : out.globalSec->inputGlobals)
717     if (!g->getName().empty())
718       ++numNames;
719 
720   numNames += out.globalSec->internalGotSymbols.size();
721   return numNames;
722 }
723 
724 unsigned NameSection::numNamedDataSegments() const {
725   unsigned numNames = 0;
726 
727   for (const OutputSegment *s : segments)
728     if (!s->name.empty() && s->requiredInBinary())
729       ++numNames;
730 
731   return numNames;
732 }
733 
734 // Create the custom "name" section containing debug symbol names.
735 void NameSection::writeBody() {
736   unsigned count = numNamedFunctions();
737   if (count) {
738     SubSection sub(WASM_NAMES_FUNCTION);
739     writeUleb128(sub.os, count, "name count");
740 
741     // Function names appear in function index order.  As it happens
742     // importedSymbols and inputFunctions are numbered in order with imported
743     // functions coming first.
744     for (const Symbol *s : out.importSec->importedSymbols) {
745       if (auto *f = dyn_cast<FunctionSymbol>(s)) {
746         writeUleb128(sub.os, f->getFunctionIndex(), "func index");
747         writeStr(sub.os, toString(*s), "symbol name");
748       }
749     }
750     for (const InputFunction *f : out.functionSec->inputFunctions) {
751       if (!f->getName().empty()) {
752         writeUleb128(sub.os, f->getFunctionIndex(), "func index");
753         if (!f->getDebugName().empty()) {
754           writeStr(sub.os, f->getDebugName(), "symbol name");
755         } else {
756           writeStr(sub.os, maybeDemangleSymbol(f->getName()), "symbol name");
757         }
758       }
759     }
760     sub.writeTo(bodyOutputStream);
761   }
762 
763   count = numNamedGlobals();
764   if (count) {
765     SubSection sub(WASM_NAMES_GLOBAL);
766     writeUleb128(sub.os, count, "name count");
767 
768     for (const Symbol *s : out.importSec->importedSymbols) {
769       if (auto *g = dyn_cast<GlobalSymbol>(s)) {
770         writeUleb128(sub.os, g->getGlobalIndex(), "global index");
771         writeStr(sub.os, toString(*s), "symbol name");
772       }
773     }
774     for (const Symbol *s : out.importSec->gotSymbols) {
775       writeUleb128(sub.os, s->getGOTIndex(), "global index");
776       writeStr(sub.os, toString(*s), "symbol name");
777     }
778     for (const InputGlobal *g : out.globalSec->inputGlobals) {
779       if (!g->getName().empty()) {
780         writeUleb128(sub.os, g->getAssignedIndex(), "global index");
781         writeStr(sub.os, maybeDemangleSymbol(g->getName()), "symbol name");
782       }
783     }
784     for (Symbol *s : out.globalSec->internalGotSymbols) {
785       writeUleb128(sub.os, s->getGOTIndex(), "global index");
786       if (isa<FunctionSymbol>(s))
787         writeStr(sub.os, "GOT.func.internal." + toString(*s), "symbol name");
788       else
789         writeStr(sub.os, "GOT.data.internal." + toString(*s), "symbol name");
790     }
791 
792     sub.writeTo(bodyOutputStream);
793   }
794 
795   count = numNamedDataSegments();
796   if (count) {
797     SubSection sub(WASM_NAMES_DATA_SEGMENT);
798     writeUleb128(sub.os, count, "name count");
799 
800     for (OutputSegment *s : segments) {
801       if (!s->name.empty() && s->requiredInBinary()) {
802         writeUleb128(sub.os, s->index, "global index");
803         writeStr(sub.os, s->name, "segment name");
804       }
805     }
806 
807     sub.writeTo(bodyOutputStream);
808   }
809 }
810 
811 void ProducersSection::addInfo(const WasmProducerInfo &info) {
812   for (auto &producers :
813        {std::make_pair(&info.Languages, &languages),
814         std::make_pair(&info.Tools, &tools), std::make_pair(&info.SDKs, &sDKs)})
815     for (auto &producer : *producers.first)
816       if (producers.second->end() ==
817           llvm::find_if(*producers.second,
818                         [&](std::pair<std::string, std::string> seen) {
819                           return seen.first == producer.first;
820                         }))
821         producers.second->push_back(producer);
822 }
823 
824 void ProducersSection::writeBody() {
825   auto &os = bodyOutputStream;
826   writeUleb128(os, fieldCount(), "field count");
827   for (auto &field :
828        {std::make_pair("language", languages),
829         std::make_pair("processed-by", tools), std::make_pair("sdk", sDKs)}) {
830     if (field.second.empty())
831       continue;
832     writeStr(os, field.first, "field name");
833     writeUleb128(os, field.second.size(), "number of entries");
834     for (auto &entry : field.second) {
835       writeStr(os, entry.first, "producer name");
836       writeStr(os, entry.second, "producer version");
837     }
838   }
839 }
840 
841 void TargetFeaturesSection::writeBody() {
842   SmallVector<std::string, 8> emitted(features.begin(), features.end());
843   llvm::sort(emitted);
844   auto &os = bodyOutputStream;
845   writeUleb128(os, emitted.size(), "feature count");
846   for (auto &feature : emitted) {
847     writeU8(os, WASM_FEATURE_PREFIX_USED, "feature used prefix");
848     writeStr(os, feature, "feature name");
849   }
850 }
851 
852 void RelocSection::writeBody() {
853   uint32_t count = sec->getNumRelocations();
854   assert(sec->sectionIndex != UINT32_MAX);
855   writeUleb128(bodyOutputStream, sec->sectionIndex, "reloc section");
856   writeUleb128(bodyOutputStream, count, "reloc count");
857   sec->writeRelocations(bodyOutputStream);
858 }
859 
860 } // namespace wasm
861 } // namespace lld
862