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

source code of lld/wasm/SyntheticSections.cpp