Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lld / wasm / SyntheticSections.cpp
blob3a9c147161a2d38316959f323c1a39cf924743ea
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.
11 //===----------------------------------------------------------------------===//
13 #include "SyntheticSections.h"
15 #include "InputChunks.h"
16 #include "InputElement.h"
17 #include "OutputSegment.h"
18 #include "SymbolTable.h"
19 #include "llvm/Support/Path.h"
20 #include <optional>
22 using namespace llvm;
23 using namespace llvm::wasm;
25 namespace lld::wasm {
27 OutStruct out;
29 namespace {
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) {}
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());
47 private:
48 uint32_t type;
49 std::string body;
51 public:
52 raw_string_ostream os{body};
55 } // namespace
57 bool DylinkSection::isNeeded() const {
58 return config->isPic ||
59 config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic ||
60 !symtab->sharedFiles.empty();
63 void DylinkSection::writeBody() {
64 raw_ostream &os = bodyOutputStream;
67 SubSection sub(WASM_DYLINK_MEM_INFO);
68 writeUleb128(sub.os, memSize, "MemSize");
69 writeUleb128(sub.os, memAlign, "MemAlign");
70 writeUleb128(sub.os, out.elemSec->numEntries(), "TableSize");
71 writeUleb128(sub.os, 0, "TableAlign");
72 sub.writeTo(os);
75 if (symtab->sharedFiles.size()) {
76 SubSection sub(WASM_DYLINK_NEEDED);
77 writeUleb128(sub.os, symtab->sharedFiles.size(), "Needed");
78 for (auto *so : symtab->sharedFiles)
79 writeStr(sub.os, llvm::sys::path::filename(so->getName()), "so name");
80 sub.writeTo(os);
83 // Under certain circumstances we need to include extra information about our
84 // exports and/or imports to the dynamic linker.
85 // For exports we need to notify the linker when an export is TLS since the
86 // exported value is relative to __tls_base rather than __memory_base.
87 // For imports we need to notify the dynamic linker when an import is weak
88 // so that knows not to report an error for such symbols.
89 std::vector<const Symbol *> importInfo;
90 std::vector<const Symbol *> exportInfo;
91 for (const Symbol *sym : symtab->symbols()) {
92 if (sym->isLive()) {
93 if (sym->isExported() && sym->isTLS() && isa<DefinedData>(sym)) {
94 exportInfo.push_back(sym);
96 if (sym->isUndefWeak()) {
97 importInfo.push_back(sym);
102 if (!exportInfo.empty()) {
103 SubSection sub(WASM_DYLINK_EXPORT_INFO);
104 writeUleb128(sub.os, exportInfo.size(), "num exports");
106 for (const Symbol *sym : exportInfo) {
107 LLVM_DEBUG(llvm::dbgs() << "export info: " << toString(*sym) << "\n");
108 StringRef name = sym->getName();
109 if (auto *f = dyn_cast<DefinedFunction>(sym)) {
110 if (std::optional<StringRef> exportName =
111 f->function->getExportName()) {
112 name = *exportName;
115 writeStr(sub.os, name, "sym name");
116 writeUleb128(sub.os, sym->flags, "sym flags");
119 sub.writeTo(os);
122 if (!importInfo.empty()) {
123 SubSection sub(WASM_DYLINK_IMPORT_INFO);
124 writeUleb128(sub.os, importInfo.size(), "num imports");
126 for (const Symbol *sym : importInfo) {
127 LLVM_DEBUG(llvm::dbgs() << "imports info: " << toString(*sym) << "\n");
128 StringRef module = sym->importModule.value_or(defaultModule);
129 StringRef name = sym->importName.value_or(sym->getName());
130 writeStr(sub.os, module, "import module");
131 writeStr(sub.os, name, "import name");
132 writeUleb128(sub.os, sym->flags, "sym flags");
135 sub.writeTo(os);
139 uint32_t TypeSection::registerType(const WasmSignature &sig) {
140 auto pair = typeIndices.insert(std::make_pair(sig, types.size()));
141 if (pair.second) {
142 LLVM_DEBUG(llvm::dbgs() << "type " << toString(sig) << "\n");
143 types.push_back(&sig);
145 return pair.first->second;
148 uint32_t TypeSection::lookupType(const WasmSignature &sig) {
149 auto it = typeIndices.find(sig);
150 if (it == typeIndices.end()) {
151 error("type not found: " + toString(sig));
152 return 0;
154 return it->second;
157 void TypeSection::writeBody() {
158 writeUleb128(bodyOutputStream, types.size(), "type count");
159 for (const WasmSignature *sig : types)
160 writeSig(bodyOutputStream, *sig);
163 uint32_t ImportSection::getNumImports() const {
164 assert(isSealed);
165 uint32_t numImports = importedSymbols.size() + gotSymbols.size();
166 if (config->memoryImport.has_value())
167 ++numImports;
168 return numImports;
171 void ImportSection::addGOTEntry(Symbol *sym) {
172 assert(!isSealed);
173 if (sym->hasGOTIndex())
174 return;
175 LLVM_DEBUG(dbgs() << "addGOTEntry: " << toString(*sym) << "\n");
176 sym->setGOTIndex(numImportedGlobals++);
177 if (config->isPic) {
178 // Any symbol that is assigned an normal GOT entry must be exported
179 // otherwise the dynamic linker won't be able create the entry that contains
180 // it.
181 sym->forceExport = true;
183 gotSymbols.push_back(sym);
186 void ImportSection::addImport(Symbol *sym) {
187 assert(!isSealed);
188 StringRef module = sym->importModule.value_or(defaultModule);
189 StringRef name = sym->importName.value_or(sym->getName());
190 if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
191 ImportKey<WasmSignature> key(*(f->getSignature()), module, name);
192 auto entry = importedFunctions.try_emplace(key, numImportedFunctions);
193 if (entry.second) {
194 importedSymbols.emplace_back(sym);
195 f->setFunctionIndex(numImportedFunctions++);
196 } else {
197 f->setFunctionIndex(entry.first->second);
199 } else if (auto *g = dyn_cast<GlobalSymbol>(sym)) {
200 ImportKey<WasmGlobalType> key(*(g->getGlobalType()), module, name);
201 auto entry = importedGlobals.try_emplace(key, numImportedGlobals);
202 if (entry.second) {
203 importedSymbols.emplace_back(sym);
204 g->setGlobalIndex(numImportedGlobals++);
205 } else {
206 g->setGlobalIndex(entry.first->second);
208 } else if (auto *t = dyn_cast<TagSymbol>(sym)) {
209 ImportKey<WasmSignature> key(*(t->getSignature()), module, name);
210 auto entry = importedTags.try_emplace(key, numImportedTags);
211 if (entry.second) {
212 importedSymbols.emplace_back(sym);
213 t->setTagIndex(numImportedTags++);
214 } else {
215 t->setTagIndex(entry.first->second);
217 } else {
218 assert(TableSymbol::classof(sym));
219 auto *table = cast<TableSymbol>(sym);
220 ImportKey<WasmTableType> key(*(table->getTableType()), module, name);
221 auto entry = importedTables.try_emplace(key, numImportedTables);
222 if (entry.second) {
223 importedSymbols.emplace_back(sym);
224 table->setTableNumber(numImportedTables++);
225 } else {
226 table->setTableNumber(entry.first->second);
231 void ImportSection::writeBody() {
232 raw_ostream &os = bodyOutputStream;
234 writeUleb128(os, getNumImports(), "import count");
236 bool is64 = config->is64.value_or(false);
238 if (config->memoryImport) {
239 WasmImport import;
240 import.Module = config->memoryImport->first;
241 import.Field = config->memoryImport->second;
242 import.Kind = WASM_EXTERNAL_MEMORY;
243 import.Memory.Flags = 0;
244 import.Memory.Minimum = out.memorySec->numMemoryPages;
245 if (out.memorySec->maxMemoryPages != 0 || config->sharedMemory) {
246 import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
247 import.Memory.Maximum = out.memorySec->maxMemoryPages;
249 if (config->sharedMemory)
250 import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED;
251 if (is64)
252 import.Memory.Flags |= WASM_LIMITS_FLAG_IS_64;
253 writeImport(os, import);
256 for (const Symbol *sym : importedSymbols) {
257 WasmImport import;
258 import.Field = sym->importName.value_or(sym->getName());
259 import.Module = sym->importModule.value_or(defaultModule);
261 if (auto *functionSym = dyn_cast<FunctionSymbol>(sym)) {
262 import.Kind = WASM_EXTERNAL_FUNCTION;
263 import.SigIndex = out.typeSec->lookupType(*functionSym->signature);
264 } else if (auto *globalSym = dyn_cast<GlobalSymbol>(sym)) {
265 import.Kind = WASM_EXTERNAL_GLOBAL;
266 import.Global = *globalSym->getGlobalType();
267 } else if (auto *tagSym = dyn_cast<TagSymbol>(sym)) {
268 import.Kind = WASM_EXTERNAL_TAG;
269 import.SigIndex = out.typeSec->lookupType(*tagSym->signature);
270 } else {
271 auto *tableSym = cast<TableSymbol>(sym);
272 import.Kind = WASM_EXTERNAL_TABLE;
273 import.Table = *tableSym->getTableType();
275 writeImport(os, import);
278 for (const Symbol *sym : gotSymbols) {
279 WasmImport import;
280 import.Kind = WASM_EXTERNAL_GLOBAL;
281 auto ptrType = is64 ? WASM_TYPE_I64 : WASM_TYPE_I32;
282 import.Global = {static_cast<uint8_t>(ptrType), true};
283 if (isa<DataSymbol>(sym))
284 import.Module = "GOT.mem";
285 else
286 import.Module = "GOT.func";
287 import.Field = sym->getName();
288 writeImport(os, import);
292 void FunctionSection::writeBody() {
293 raw_ostream &os = bodyOutputStream;
295 writeUleb128(os, inputFunctions.size(), "function count");
296 for (const InputFunction *func : inputFunctions)
297 writeUleb128(os, out.typeSec->lookupType(func->signature), "sig index");
300 void FunctionSection::addFunction(InputFunction *func) {
301 if (!func->live)
302 return;
303 uint32_t functionIndex =
304 out.importSec->getNumImportedFunctions() + inputFunctions.size();
305 inputFunctions.emplace_back(func);
306 func->setFunctionIndex(functionIndex);
309 void TableSection::writeBody() {
310 raw_ostream &os = bodyOutputStream;
312 writeUleb128(os, inputTables.size(), "table count");
313 for (const InputTable *table : inputTables)
314 writeTableType(os, table->getType());
317 void TableSection::addTable(InputTable *table) {
318 if (!table->live)
319 return;
320 // Some inputs require that the indirect function table be assigned to table
321 // number 0.
322 if (config->legacyFunctionTable &&
323 isa<DefinedTable>(WasmSym::indirectFunctionTable) &&
324 cast<DefinedTable>(WasmSym::indirectFunctionTable)->table == table) {
325 if (out.importSec->getNumImportedTables()) {
326 // Alack! Some other input imported a table, meaning that we are unable
327 // to assign table number 0 to the indirect function table.
328 for (const auto *culprit : out.importSec->importedSymbols) {
329 if (isa<UndefinedTable>(culprit)) {
330 error("object file not built with 'reference-types' feature "
331 "conflicts with import of table " +
332 culprit->getName() + " by file " +
333 toString(culprit->getFile()));
334 return;
337 llvm_unreachable("failed to find conflicting table import");
339 inputTables.insert(inputTables.begin(), table);
340 return;
342 inputTables.push_back(table);
345 void TableSection::assignIndexes() {
346 uint32_t tableNumber = out.importSec->getNumImportedTables();
347 for (InputTable *t : inputTables)
348 t->assignIndex(tableNumber++);
351 void MemorySection::writeBody() {
352 raw_ostream &os = bodyOutputStream;
354 bool hasMax = maxMemoryPages != 0 || config->sharedMemory;
355 writeUleb128(os, 1, "memory count");
356 unsigned flags = 0;
357 if (hasMax)
358 flags |= WASM_LIMITS_FLAG_HAS_MAX;
359 if (config->sharedMemory)
360 flags |= WASM_LIMITS_FLAG_IS_SHARED;
361 if (config->is64.value_or(false))
362 flags |= WASM_LIMITS_FLAG_IS_64;
363 writeUleb128(os, flags, "memory limits flags");
364 writeUleb128(os, numMemoryPages, "initial pages");
365 if (hasMax)
366 writeUleb128(os, maxMemoryPages, "max pages");
369 void TagSection::writeBody() {
370 raw_ostream &os = bodyOutputStream;
372 writeUleb128(os, inputTags.size(), "tag count");
373 for (InputTag *t : inputTags) {
374 writeUleb128(os, 0, "tag attribute"); // Reserved "attribute" field
375 writeUleb128(os, out.typeSec->lookupType(t->signature), "sig index");
379 void TagSection::addTag(InputTag *tag) {
380 if (!tag->live)
381 return;
382 uint32_t tagIndex = out.importSec->getNumImportedTags() + inputTags.size();
383 LLVM_DEBUG(dbgs() << "addTag: " << tagIndex << "\n");
384 tag->assignIndex(tagIndex);
385 inputTags.push_back(tag);
388 void GlobalSection::assignIndexes() {
389 uint32_t globalIndex = out.importSec->getNumImportedGlobals();
390 for (InputGlobal *g : inputGlobals)
391 g->assignIndex(globalIndex++);
392 for (Symbol *sym : internalGotSymbols)
393 sym->setGOTIndex(globalIndex++);
394 isSealed = true;
397 static void ensureIndirectFunctionTable() {
398 if (!WasmSym::indirectFunctionTable)
399 WasmSym::indirectFunctionTable =
400 symtab->resolveIndirectFunctionTable(/*required =*/true);
403 void GlobalSection::addInternalGOTEntry(Symbol *sym) {
404 assert(!isSealed);
405 if (sym->requiresGOT)
406 return;
407 LLVM_DEBUG(dbgs() << "addInternalGOTEntry: " << sym->getName() << " "
408 << toString(sym->kind()) << "\n");
409 sym->requiresGOT = true;
410 if (auto *F = dyn_cast<FunctionSymbol>(sym)) {
411 ensureIndirectFunctionTable();
412 out.elemSec->addEntry(F);
414 internalGotSymbols.push_back(sym);
417 void GlobalSection::generateRelocationCode(raw_ostream &os, bool TLS) const {
418 assert(!config->extendedConst);
419 bool is64 = config->is64.value_or(false);
420 unsigned opcode_ptr_const = is64 ? WASM_OPCODE_I64_CONST
421 : WASM_OPCODE_I32_CONST;
422 unsigned opcode_ptr_add = is64 ? WASM_OPCODE_I64_ADD
423 : WASM_OPCODE_I32_ADD;
425 for (const Symbol *sym : internalGotSymbols) {
426 if (TLS != sym->isTLS())
427 continue;
429 if (auto *d = dyn_cast<DefinedData>(sym)) {
430 // Get __memory_base
431 writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
432 if (sym->isTLS())
433 writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "__tls_base");
434 else
435 writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(),
436 "__memory_base");
438 // Add the virtual address of the data symbol
439 writeU8(os, opcode_ptr_const, "CONST");
440 writeSleb128(os, d->getVA(), "offset");
441 } else if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
442 if (f->isStub)
443 continue;
444 // Get __table_base
445 writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
446 writeUleb128(os, WasmSym::tableBase->getGlobalIndex(), "__table_base");
448 // Add the table index to __table_base
449 writeU8(os, opcode_ptr_const, "CONST");
450 writeSleb128(os, f->getTableIndex(), "offset");
451 } else {
452 assert(isa<UndefinedData>(sym));
453 continue;
455 writeU8(os, opcode_ptr_add, "ADD");
456 writeU8(os, WASM_OPCODE_GLOBAL_SET, "GLOBAL_SET");
457 writeUleb128(os, sym->getGOTIndex(), "got_entry");
461 void GlobalSection::writeBody() {
462 raw_ostream &os = bodyOutputStream;
464 writeUleb128(os, numGlobals(), "global count");
465 for (InputGlobal *g : inputGlobals) {
466 writeGlobalType(os, g->getType());
467 writeInitExpr(os, g->getInitExpr());
469 bool is64 = config->is64.value_or(false);
470 uint8_t itype = is64 ? WASM_TYPE_I64 : WASM_TYPE_I32;
471 for (const Symbol *sym : internalGotSymbols) {
472 bool mutable_ = false;
473 if (!sym->isStub) {
474 // In the case of dynamic linking, unless we have 'extended-const'
475 // available, these global must to be mutable since they get updated to
476 // the correct runtime value during `__wasm_apply_global_relocs`.
477 if (!config->extendedConst && config->isPic && !sym->isTLS())
478 mutable_ = true;
479 // With multi-theadeding any TLS globals must be mutable since they get
480 // set during `__wasm_apply_global_tls_relocs`
481 if (config->sharedMemory && sym->isTLS())
482 mutable_ = true;
484 WasmGlobalType type{itype, mutable_};
485 writeGlobalType(os, type);
487 bool useExtendedConst = false;
488 uint32_t globalIdx;
489 int64_t offset;
490 if (config->extendedConst && config->isPic) {
491 if (auto *d = dyn_cast<DefinedData>(sym)) {
492 if (!sym->isTLS()) {
493 globalIdx = WasmSym::memoryBase->getGlobalIndex();
494 offset = d->getVA();
495 useExtendedConst = true;
497 } else if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
498 if (!sym->isStub) {
499 globalIdx = WasmSym::tableBase->getGlobalIndex();
500 offset = f->getTableIndex();
501 useExtendedConst = true;
505 if (useExtendedConst) {
506 // We can use an extended init expression to add a constant
507 // offset of __memory_base/__table_base.
508 writeU8(os, WASM_OPCODE_GLOBAL_GET, "global get");
509 writeUleb128(os, globalIdx, "literal (global index)");
510 if (offset) {
511 writePtrConst(os, offset, is64, "offset");
512 writeU8(os, is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD, "add");
514 writeU8(os, WASM_OPCODE_END, "opcode:end");
515 } else {
516 WasmInitExpr initExpr;
517 if (auto *d = dyn_cast<DefinedData>(sym))
518 initExpr = intConst(d->getVA(), is64);
519 else if (auto *f = dyn_cast<FunctionSymbol>(sym))
520 initExpr = intConst(f->isStub ? 0 : f->getTableIndex(), is64);
521 else {
522 assert(isa<UndefinedData>(sym));
523 initExpr = intConst(0, is64);
525 writeInitExpr(os, initExpr);
528 for (const DefinedData *sym : dataAddressGlobals) {
529 WasmGlobalType type{itype, false};
530 writeGlobalType(os, type);
531 writeInitExpr(os, intConst(sym->getVA(), is64));
535 void GlobalSection::addGlobal(InputGlobal *global) {
536 assert(!isSealed);
537 if (!global->live)
538 return;
539 inputGlobals.push_back(global);
542 void ExportSection::writeBody() {
543 raw_ostream &os = bodyOutputStream;
545 writeUleb128(os, exports.size(), "export count");
546 for (const WasmExport &export_ : exports)
547 writeExport(os, export_);
550 bool StartSection::isNeeded() const {
551 return WasmSym::startFunction != nullptr;
554 void StartSection::writeBody() {
555 raw_ostream &os = bodyOutputStream;
556 writeUleb128(os, WasmSym::startFunction->getFunctionIndex(),
557 "function index");
560 void ElemSection::addEntry(FunctionSymbol *sym) {
561 // Don't add stub functions to the wasm table. The address of all stub
562 // functions should be zero and they should they don't appear in the table.
563 // They only exist so that the calls to missing functions can validate.
564 if (sym->hasTableIndex() || sym->isStub)
565 return;
566 sym->setTableIndex(config->tableBase + indirectFunctions.size());
567 indirectFunctions.emplace_back(sym);
570 void ElemSection::writeBody() {
571 raw_ostream &os = bodyOutputStream;
573 assert(WasmSym::indirectFunctionTable);
574 writeUleb128(os, 1, "segment count");
575 uint32_t tableNumber = WasmSym::indirectFunctionTable->getTableNumber();
576 uint32_t flags = 0;
577 if (tableNumber)
578 flags |= WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER;
579 writeUleb128(os, flags, "elem segment flags");
580 if (flags & WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER)
581 writeUleb128(os, tableNumber, "table number");
583 WasmInitExpr initExpr;
584 initExpr.Extended = false;
585 if (config->isPic) {
586 initExpr.Inst.Opcode = WASM_OPCODE_GLOBAL_GET;
587 initExpr.Inst.Value.Global =
588 (config->is64.value_or(false) ? WasmSym::tableBase32
589 : WasmSym::tableBase)
590 ->getGlobalIndex();
591 } else {
592 initExpr.Inst.Opcode = WASM_OPCODE_I32_CONST;
593 initExpr.Inst.Value.Int32 = config->tableBase;
595 writeInitExpr(os, initExpr);
597 if (flags & WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND) {
598 // We only write active function table initializers, for which the elem kind
599 // is specified to be written as 0x00 and interpreted to mean "funcref".
600 const uint8_t elemKind = 0;
601 writeU8(os, elemKind, "elem kind");
604 writeUleb128(os, indirectFunctions.size(), "elem count");
605 uint32_t tableIndex = config->tableBase;
606 for (const FunctionSymbol *sym : indirectFunctions) {
607 assert(sym->getTableIndex() == tableIndex);
608 (void) tableIndex;
609 writeUleb128(os, sym->getFunctionIndex(), "function index");
610 ++tableIndex;
614 DataCountSection::DataCountSection(ArrayRef<OutputSegment *> segments)
615 : SyntheticSection(llvm::wasm::WASM_SEC_DATACOUNT),
616 numSegments(llvm::count_if(segments, [](OutputSegment *const segment) {
617 return segment->requiredInBinary();
618 })) {}
620 void DataCountSection::writeBody() {
621 writeUleb128(bodyOutputStream, numSegments, "data count");
624 bool DataCountSection::isNeeded() const {
625 return numSegments && config->sharedMemory;
628 void LinkingSection::writeBody() {
629 raw_ostream &os = bodyOutputStream;
631 writeUleb128(os, WasmMetadataVersion, "Version");
633 if (!symtabEntries.empty()) {
634 SubSection sub(WASM_SYMBOL_TABLE);
635 writeUleb128(sub.os, symtabEntries.size(), "num symbols");
637 for (const Symbol *sym : symtabEntries) {
638 assert(sym->isDefined() || sym->isUndefined());
639 WasmSymbolType kind = sym->getWasmType();
640 uint32_t flags = sym->flags;
642 writeU8(sub.os, kind, "sym kind");
643 writeUleb128(sub.os, flags, "sym flags");
645 if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
646 if (auto *d = dyn_cast<DefinedFunction>(sym)) {
647 writeUleb128(sub.os, d->getExportedFunctionIndex(), "index");
648 } else {
649 writeUleb128(sub.os, f->getFunctionIndex(), "index");
651 if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
652 writeStr(sub.os, sym->getName(), "sym name");
653 } else if (auto *g = dyn_cast<GlobalSymbol>(sym)) {
654 writeUleb128(sub.os, g->getGlobalIndex(), "index");
655 if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
656 writeStr(sub.os, sym->getName(), "sym name");
657 } else if (auto *t = dyn_cast<TagSymbol>(sym)) {
658 writeUleb128(sub.os, t->getTagIndex(), "index");
659 if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
660 writeStr(sub.os, sym->getName(), "sym name");
661 } else if (auto *t = dyn_cast<TableSymbol>(sym)) {
662 writeUleb128(sub.os, t->getTableNumber(), "table number");
663 if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
664 writeStr(sub.os, sym->getName(), "sym name");
665 } else if (isa<DataSymbol>(sym)) {
666 writeStr(sub.os, sym->getName(), "sym name");
667 if (auto *dataSym = dyn_cast<DefinedData>(sym)) {
668 if (dataSym->segment) {
669 writeUleb128(sub.os, dataSym->getOutputSegmentIndex(), "index");
670 writeUleb128(sub.os, dataSym->getOutputSegmentOffset(),
671 "data offset");
672 } else {
673 writeUleb128(sub.os, 0, "index");
674 writeUleb128(sub.os, dataSym->getVA(), "data offset");
676 writeUleb128(sub.os, dataSym->getSize(), "data size");
678 } else {
679 auto *s = cast<OutputSectionSymbol>(sym);
680 writeUleb128(sub.os, s->section->sectionIndex, "sym section index");
684 sub.writeTo(os);
687 if (dataSegments.size()) {
688 SubSection sub(WASM_SEGMENT_INFO);
689 writeUleb128(sub.os, dataSegments.size(), "num data segments");
690 for (const OutputSegment *s : dataSegments) {
691 writeStr(sub.os, s->name, "segment name");
692 writeUleb128(sub.os, s->alignment, "alignment");
693 writeUleb128(sub.os, s->linkingFlags, "flags");
695 sub.writeTo(os);
698 if (!initFunctions.empty()) {
699 SubSection sub(WASM_INIT_FUNCS);
700 writeUleb128(sub.os, initFunctions.size(), "num init functions");
701 for (const WasmInitEntry &f : initFunctions) {
702 writeUleb128(sub.os, f.priority, "priority");
703 writeUleb128(sub.os, f.sym->getOutputSymbolIndex(), "function index");
705 sub.writeTo(os);
708 struct ComdatEntry {
709 unsigned kind;
710 uint32_t index;
712 std::map<StringRef, std::vector<ComdatEntry>> comdats;
714 for (const InputFunction *f : out.functionSec->inputFunctions) {
715 StringRef comdat = f->getComdatName();
716 if (!comdat.empty())
717 comdats[comdat].emplace_back(
718 ComdatEntry{WASM_COMDAT_FUNCTION, f->getFunctionIndex()});
720 for (uint32_t i = 0; i < dataSegments.size(); ++i) {
721 const auto &inputSegments = dataSegments[i]->inputSegments;
722 if (inputSegments.empty())
723 continue;
724 StringRef comdat = inputSegments[0]->getComdatName();
725 #ifndef NDEBUG
726 for (const InputChunk *isec : inputSegments)
727 assert(isec->getComdatName() == comdat);
728 #endif
729 if (!comdat.empty())
730 comdats[comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, i});
733 if (!comdats.empty()) {
734 SubSection sub(WASM_COMDAT_INFO);
735 writeUleb128(sub.os, comdats.size(), "num comdats");
736 for (const auto &c : comdats) {
737 writeStr(sub.os, c.first, "comdat name");
738 writeUleb128(sub.os, 0, "comdat flags"); // flags for future use
739 writeUleb128(sub.os, c.second.size(), "num entries");
740 for (const ComdatEntry &entry : c.second) {
741 writeU8(sub.os, entry.kind, "entry kind");
742 writeUleb128(sub.os, entry.index, "entry index");
745 sub.writeTo(os);
749 void LinkingSection::addToSymtab(Symbol *sym) {
750 sym->setOutputSymbolIndex(symtabEntries.size());
751 symtabEntries.emplace_back(sym);
754 unsigned NameSection::numNamedFunctions() const {
755 unsigned numNames = out.importSec->getNumImportedFunctions();
757 for (const InputFunction *f : out.functionSec->inputFunctions)
758 if (!f->name.empty() || !f->debugName.empty())
759 ++numNames;
761 return numNames;
764 unsigned NameSection::numNamedGlobals() const {
765 unsigned numNames = out.importSec->getNumImportedGlobals();
767 for (const InputGlobal *g : out.globalSec->inputGlobals)
768 if (!g->getName().empty())
769 ++numNames;
771 numNames += out.globalSec->internalGotSymbols.size();
772 return numNames;
775 unsigned NameSection::numNamedDataSegments() const {
776 unsigned numNames = 0;
778 for (const OutputSegment *s : segments)
779 if (!s->name.empty() && s->requiredInBinary())
780 ++numNames;
782 return numNames;
785 // Create the custom "name" section containing debug symbol names.
786 void NameSection::writeBody() {
788 SubSection sub(WASM_NAMES_MODULE);
789 StringRef moduleName = config->soName;
790 if (config->soName.empty())
791 moduleName = llvm::sys::path::filename(config->outputFile);
792 writeStr(sub.os, moduleName, "module name");
793 sub.writeTo(bodyOutputStream);
796 unsigned count = numNamedFunctions();
797 if (count) {
798 SubSection sub(WASM_NAMES_FUNCTION);
799 writeUleb128(sub.os, count, "name count");
801 // Function names appear in function index order. As it happens
802 // importedSymbols and inputFunctions are numbered in order with imported
803 // functions coming first.
804 for (const Symbol *s : out.importSec->importedSymbols) {
805 if (auto *f = dyn_cast<FunctionSymbol>(s)) {
806 writeUleb128(sub.os, f->getFunctionIndex(), "func index");
807 writeStr(sub.os, toString(*s), "symbol name");
810 for (const InputFunction *f : out.functionSec->inputFunctions) {
811 if (!f->name.empty()) {
812 writeUleb128(sub.os, f->getFunctionIndex(), "func index");
813 if (!f->debugName.empty()) {
814 writeStr(sub.os, f->debugName, "symbol name");
815 } else {
816 writeStr(sub.os, maybeDemangleSymbol(f->name), "symbol name");
820 sub.writeTo(bodyOutputStream);
823 count = numNamedGlobals();
824 if (count) {
825 SubSection sub(WASM_NAMES_GLOBAL);
826 writeUleb128(sub.os, count, "name count");
828 for (const Symbol *s : out.importSec->importedSymbols) {
829 if (auto *g = dyn_cast<GlobalSymbol>(s)) {
830 writeUleb128(sub.os, g->getGlobalIndex(), "global index");
831 writeStr(sub.os, toString(*s), "symbol name");
834 for (const Symbol *s : out.importSec->gotSymbols) {
835 writeUleb128(sub.os, s->getGOTIndex(), "global index");
836 writeStr(sub.os, toString(*s), "symbol name");
838 for (const InputGlobal *g : out.globalSec->inputGlobals) {
839 if (!g->getName().empty()) {
840 writeUleb128(sub.os, g->getAssignedIndex(), "global index");
841 writeStr(sub.os, maybeDemangleSymbol(g->getName()), "symbol name");
844 for (Symbol *s : out.globalSec->internalGotSymbols) {
845 writeUleb128(sub.os, s->getGOTIndex(), "global index");
846 if (isa<FunctionSymbol>(s))
847 writeStr(sub.os, "GOT.func.internal." + toString(*s), "symbol name");
848 else
849 writeStr(sub.os, "GOT.data.internal." + toString(*s), "symbol name");
852 sub.writeTo(bodyOutputStream);
855 count = numNamedDataSegments();
856 if (count) {
857 SubSection sub(WASM_NAMES_DATA_SEGMENT);
858 writeUleb128(sub.os, count, "name count");
860 for (OutputSegment *s : segments) {
861 if (!s->name.empty() && s->requiredInBinary()) {
862 writeUleb128(sub.os, s->index, "global index");
863 writeStr(sub.os, s->name, "segment name");
867 sub.writeTo(bodyOutputStream);
871 void ProducersSection::addInfo(const WasmProducerInfo &info) {
872 for (auto &producers :
873 {std::make_pair(&info.Languages, &languages),
874 std::make_pair(&info.Tools, &tools), std::make_pair(&info.SDKs, &sDKs)})
875 for (auto &producer : *producers.first)
876 if (llvm::none_of(*producers.second,
877 [&](std::pair<std::string, std::string> seen) {
878 return seen.first == producer.first;
880 producers.second->push_back(producer);
883 void ProducersSection::writeBody() {
884 auto &os = bodyOutputStream;
885 writeUleb128(os, fieldCount(), "field count");
886 for (auto &field :
887 {std::make_pair("language", languages),
888 std::make_pair("processed-by", tools), std::make_pair("sdk", sDKs)}) {
889 if (field.second.empty())
890 continue;
891 writeStr(os, field.first, "field name");
892 writeUleb128(os, field.second.size(), "number of entries");
893 for (auto &entry : field.second) {
894 writeStr(os, entry.first, "producer name");
895 writeStr(os, entry.second, "producer version");
900 void TargetFeaturesSection::writeBody() {
901 SmallVector<std::string, 8> emitted(features.begin(), features.end());
902 llvm::sort(emitted);
903 auto &os = bodyOutputStream;
904 writeUleb128(os, emitted.size(), "feature count");
905 for (auto &feature : emitted) {
906 writeU8(os, WASM_FEATURE_PREFIX_USED, "feature used prefix");
907 writeStr(os, feature, "feature name");
911 void RelocSection::writeBody() {
912 uint32_t count = sec->getNumRelocations();
913 assert(sec->sectionIndex != UINT32_MAX);
914 writeUleb128(bodyOutputStream, sec->sectionIndex, "reloc section");
915 writeUleb128(bodyOutputStream, count, "reloc count");
916 sec->writeRelocations(bodyOutputStream);
919 static size_t getHashSize() {
920 switch (config->buildId) {
921 case BuildIdKind::Fast:
922 case BuildIdKind::Uuid:
923 return 16;
924 case BuildIdKind::Sha1:
925 return 20;
926 case BuildIdKind::Hexstring:
927 return config->buildIdVector.size();
928 case BuildIdKind::None:
929 return 0;
931 llvm_unreachable("build id kind not implemented");
934 BuildIdSection::BuildIdSection()
935 : SyntheticSection(llvm::wasm::WASM_SEC_CUSTOM, buildIdSectionName),
936 hashSize(getHashSize()) {}
938 void BuildIdSection::writeBody() {
939 LLVM_DEBUG(llvm::dbgs() << "BuildId writebody\n");
940 // Write hash size
941 auto &os = bodyOutputStream;
942 writeUleb128(os, hashSize, "build id size");
943 writeBytes(os, std::vector<char>(hashSize, ' ').data(), hashSize,
944 "placeholder");
947 void BuildIdSection::writeBuildId(llvm::ArrayRef<uint8_t> buf) {
948 assert(buf.size() == hashSize);
949 LLVM_DEBUG(dbgs() << "buildid write " << buf.size() << " "
950 << hashPlaceholderPtr << '\n');
951 memcpy(hashPlaceholderPtr, buf.data(), hashSize);
954 } // namespace wasm::lld