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 "InputEvent.h"
17 #include "InputGlobal.h"
18 #include "OutputSegment.h"
19 #include "SymbolTable.h"
20 #include "llvm/Support/Path.h"
21
22 using namespace llvm;
23 using namespace llvm::wasm;
24
25 namespace lld {
26 namespace wasm {
27
28 OutStruct out;
29
30 namespace {
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.
37 class SubSection {
38 public:
SubSection(uint32_t type)39 explicit SubSection(uint32_t type) : type(type) {}
40
writeTo(raw_ostream & to)41 void writeTo(raw_ostream &to) {
42 os.flush();
43 writeUleb128(to, type, "subsection type");
44 writeUleb128(to, body.size(), "subsection size");
45 to.write(body.data(), body.size());
46 }
47
48 private:
49 uint32_t type;
50 std::string body;
51
52 public:
53 raw_string_ostream os{body};
54 };
55
56 } // namespace
57
writeBody()58 void DylinkSection::writeBody() {
59 raw_ostream &os = bodyOutputStream;
60
61 writeUleb128(os, memSize, "MemSize");
62 writeUleb128(os, memAlign, "MemAlign");
63 writeUleb128(os, out.elemSec->numEntries(), "TableSize");
64 writeUleb128(os, 0, "TableAlign");
65 writeUleb128(os, symtab->sharedFiles.size(), "Needed");
66 for (auto *so : symtab->sharedFiles)
67 writeStr(os, llvm::sys::path::filename(so->getName()), "so name");
68 }
69
registerType(const WasmSignature & sig)70 uint32_t TypeSection::registerType(const WasmSignature &sig) {
71 auto pair = typeIndices.insert(std::make_pair(sig, types.size()));
72 if (pair.second) {
73 LLVM_DEBUG(llvm::dbgs() << "type " << toString(sig) << "\n");
74 types.push_back(&sig);
75 }
76 return pair.first->second;
77 }
78
lookupType(const WasmSignature & sig)79 uint32_t TypeSection::lookupType(const WasmSignature &sig) {
80 auto it = typeIndices.find(sig);
81 if (it == typeIndices.end()) {
82 error("type not found: " + toString(sig));
83 return 0;
84 }
85 return it->second;
86 }
87
writeBody()88 void TypeSection::writeBody() {
89 writeUleb128(bodyOutputStream, types.size(), "type count");
90 for (const WasmSignature *sig : types)
91 writeSig(bodyOutputStream, *sig);
92 }
93
getNumImports() const94 uint32_t ImportSection::getNumImports() const {
95 assert(isSealed);
96 uint32_t numImports = importedSymbols.size() + gotSymbols.size();
97 if (config->importMemory)
98 ++numImports;
99 if (config->importTable)
100 ++numImports;
101 return numImports;
102 }
103
addGOTEntry(Symbol * sym)104 void ImportSection::addGOTEntry(Symbol *sym) {
105 assert(!isSealed);
106 if (sym->hasGOTIndex())
107 return;
108 LLVM_DEBUG(dbgs() << "addGOTEntry: " << toString(*sym) << "\n");
109 sym->setGOTIndex(numImportedGlobals++);
110 gotSymbols.push_back(sym);
111 }
112
addImport(Symbol * sym)113 void ImportSection::addImport(Symbol *sym) {
114 assert(!isSealed);
115 importedSymbols.emplace_back(sym);
116 if (auto *f = dyn_cast<FunctionSymbol>(sym))
117 f->setFunctionIndex(numImportedFunctions++);
118 else if (auto *g = dyn_cast<GlobalSymbol>(sym))
119 g->setGlobalIndex(numImportedGlobals++);
120 else
121 cast<EventSymbol>(sym)->setEventIndex(numImportedEvents++);
122 }
123
writeBody()124 void ImportSection::writeBody() {
125 raw_ostream &os = bodyOutputStream;
126
127 writeUleb128(os, getNumImports(), "import count");
128
129 if (config->importMemory) {
130 WasmImport import;
131 import.Module = defaultModule;
132 import.Field = "memory";
133 import.Kind = WASM_EXTERNAL_MEMORY;
134 import.Memory.Flags = 0;
135 import.Memory.Initial = out.memorySec->numMemoryPages;
136 if (out.memorySec->maxMemoryPages != 0 || config->sharedMemory) {
137 import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
138 import.Memory.Maximum = out.memorySec->maxMemoryPages;
139 }
140 if (config->sharedMemory)
141 import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED;
142 if (config->is64)
143 import.Memory.Flags |= WASM_LIMITS_FLAG_IS_64;
144 writeImport(os, import);
145 }
146
147 if (config->importTable) {
148 uint32_t tableSize = config->tableBase + out.elemSec->numEntries();
149 WasmImport import;
150 import.Module = defaultModule;
151 import.Field = functionTableName;
152 import.Kind = WASM_EXTERNAL_TABLE;
153 import.Table.ElemType = WASM_TYPE_FUNCREF;
154 import.Table.Limits = {0, tableSize, 0};
155 writeImport(os, import);
156 }
157
158 for (const Symbol *sym : importedSymbols) {
159 WasmImport import;
160 if (auto *f = dyn_cast<UndefinedFunction>(sym)) {
161 import.Field = f->importName ? *f->importName : sym->getName();
162 import.Module = f->importModule ? *f->importModule : defaultModule;
163 } else if (auto *g = dyn_cast<UndefinedGlobal>(sym)) {
164 import.Field = g->importName ? *g->importName : sym->getName();
165 import.Module = g->importModule ? *g->importModule : defaultModule;
166 } else {
167 import.Field = sym->getName();
168 import.Module = defaultModule;
169 }
170
171 if (auto *functionSym = dyn_cast<FunctionSymbol>(sym)) {
172 import.Kind = WASM_EXTERNAL_FUNCTION;
173 import.SigIndex = out.typeSec->lookupType(*functionSym->signature);
174 } else if (auto *globalSym = dyn_cast<GlobalSymbol>(sym)) {
175 import.Kind = WASM_EXTERNAL_GLOBAL;
176 import.Global = *globalSym->getGlobalType();
177 } else {
178 auto *eventSym = cast<EventSymbol>(sym);
179 import.Kind = WASM_EXTERNAL_EVENT;
180 import.Event.Attribute = eventSym->getEventType()->Attribute;
181 import.Event.SigIndex = out.typeSec->lookupType(*eventSym->signature);
182 }
183 writeImport(os, import);
184 }
185
186 for (const Symbol *sym : gotSymbols) {
187 WasmImport import;
188 import.Kind = WASM_EXTERNAL_GLOBAL;
189 import.Global = {WASM_TYPE_I32, true};
190 if (isa<DataSymbol>(sym))
191 import.Module = "GOT.mem";
192 else
193 import.Module = "GOT.func";
194 import.Field = sym->getName();
195 writeImport(os, import);
196 }
197 }
198
writeBody()199 void FunctionSection::writeBody() {
200 raw_ostream &os = bodyOutputStream;
201
202 writeUleb128(os, inputFunctions.size(), "function count");
203 for (const InputFunction *func : inputFunctions)
204 writeUleb128(os, out.typeSec->lookupType(func->signature), "sig index");
205 }
206
addFunction(InputFunction * func)207 void FunctionSection::addFunction(InputFunction *func) {
208 if (!func->live)
209 return;
210 uint32_t functionIndex =
211 out.importSec->getNumImportedFunctions() + inputFunctions.size();
212 inputFunctions.emplace_back(func);
213 func->setFunctionIndex(functionIndex);
214 }
215
writeBody()216 void TableSection::writeBody() {
217 uint32_t tableSize = config->tableBase + out.elemSec->numEntries();
218
219 raw_ostream &os = bodyOutputStream;
220 writeUleb128(os, 1, "table count");
221 WasmLimits limits;
222 if (config->growableTable)
223 limits = {0, tableSize, 0};
224 else
225 limits = {WASM_LIMITS_FLAG_HAS_MAX, tableSize, tableSize};
226 writeTableType(os, WasmTable{WASM_TYPE_FUNCREF, limits});
227 }
228
writeBody()229 void MemorySection::writeBody() {
230 raw_ostream &os = bodyOutputStream;
231
232 bool hasMax = maxMemoryPages != 0 || config->sharedMemory;
233 writeUleb128(os, 1, "memory count");
234 unsigned flags = 0;
235 if (hasMax)
236 flags |= WASM_LIMITS_FLAG_HAS_MAX;
237 if (config->sharedMemory)
238 flags |= WASM_LIMITS_FLAG_IS_SHARED;
239 if (config->is64)
240 flags |= WASM_LIMITS_FLAG_IS_64;
241 writeUleb128(os, flags, "memory limits flags");
242 writeUleb128(os, numMemoryPages, "initial pages");
243 if (hasMax)
244 writeUleb128(os, maxMemoryPages, "max pages");
245 }
246
writeBody()247 void EventSection::writeBody() {
248 raw_ostream &os = bodyOutputStream;
249
250 writeUleb128(os, inputEvents.size(), "event count");
251 for (InputEvent *e : inputEvents) {
252 e->event.Type.SigIndex = out.typeSec->lookupType(e->signature);
253 writeEvent(os, e->event);
254 }
255 }
256
addEvent(InputEvent * event)257 void EventSection::addEvent(InputEvent *event) {
258 if (!event->live)
259 return;
260 uint32_t eventIndex =
261 out.importSec->getNumImportedEvents() + inputEvents.size();
262 LLVM_DEBUG(dbgs() << "addEvent: " << eventIndex << "\n");
263 event->setEventIndex(eventIndex);
264 inputEvents.push_back(event);
265 }
266
assignIndexes()267 void GlobalSection::assignIndexes() {
268 uint32_t globalIndex = out.importSec->getNumImportedGlobals();
269 for (InputGlobal *g : inputGlobals)
270 g->setGlobalIndex(globalIndex++);
271 for (Symbol *sym : staticGotSymbols)
272 sym->setGOTIndex(globalIndex++);
273 isSealed = true;
274 }
275
addStaticGOTEntry(Symbol * sym)276 void GlobalSection::addStaticGOTEntry(Symbol *sym) {
277 assert(!isSealed);
278 if (sym->requiresGOT)
279 return;
280 LLVM_DEBUG(dbgs() << "addStaticGOTEntry: " << sym->getName() << " "
281 << toString(sym->kind()) << "\n");
282 sym->requiresGOT = true;
283 if (auto *F = dyn_cast<FunctionSymbol>(sym))
284 out.elemSec->addEntry(F);
285 staticGotSymbols.push_back(sym);
286 }
287
writeBody()288 void GlobalSection::writeBody() {
289 raw_ostream &os = bodyOutputStream;
290
291 writeUleb128(os, numGlobals(), "global count");
292 for (InputGlobal *g : inputGlobals)
293 writeGlobal(os, g->global);
294 // TODO(wvo): when do these need I64_CONST?
295 for (const Symbol *sym : staticGotSymbols) {
296 WasmGlobal global;
297 global.Type = {WASM_TYPE_I32, false};
298 global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
299 if (auto *d = dyn_cast<DefinedData>(sym))
300 global.InitExpr.Value.Int32 = d->getVirtualAddress();
301 else if (auto *f = dyn_cast<FunctionSymbol>(sym))
302 global.InitExpr.Value.Int32 = f->getTableIndex();
303 else {
304 assert(isa<UndefinedData>(sym));
305 global.InitExpr.Value.Int32 = 0;
306 }
307 writeGlobal(os, global);
308 }
309 for (const DefinedData *sym : dataAddressGlobals) {
310 WasmGlobal global;
311 global.Type = {WASM_TYPE_I32, false};
312 global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
313 global.InitExpr.Value.Int32 = sym->getVirtualAddress();
314 writeGlobal(os, global);
315 }
316 }
317
addGlobal(InputGlobal * global)318 void GlobalSection::addGlobal(InputGlobal *global) {
319 assert(!isSealed);
320 if (!global->live)
321 return;
322 inputGlobals.push_back(global);
323 }
324
writeBody()325 void ExportSection::writeBody() {
326 raw_ostream &os = bodyOutputStream;
327
328 writeUleb128(os, exports.size(), "export count");
329 for (const WasmExport &export_ : exports)
330 writeExport(os, export_);
331 }
332
isNeeded() const333 bool StartSection::isNeeded() const {
334 return !config->relocatable && hasInitializedSegments && config->sharedMemory;
335 }
336
writeBody()337 void StartSection::writeBody() {
338 raw_ostream &os = bodyOutputStream;
339 writeUleb128(os, WasmSym::initMemory->getFunctionIndex(), "function index");
340 }
341
addEntry(FunctionSymbol * sym)342 void ElemSection::addEntry(FunctionSymbol *sym) {
343 if (sym->hasTableIndex())
344 return;
345 sym->setTableIndex(config->tableBase + indirectFunctions.size());
346 indirectFunctions.emplace_back(sym);
347 }
348
writeBody()349 void ElemSection::writeBody() {
350 raw_ostream &os = bodyOutputStream;
351
352 writeUleb128(os, 1, "segment count");
353 writeUleb128(os, 0, "table index");
354 WasmInitExpr initExpr;
355 if (config->isPic) {
356 initExpr.Opcode = WASM_OPCODE_GLOBAL_GET;
357 initExpr.Value.Global = WasmSym::tableBase->getGlobalIndex();
358 } else {
359 initExpr.Opcode = WASM_OPCODE_I32_CONST;
360 initExpr.Value.Int32 = config->tableBase;
361 }
362 writeInitExpr(os, initExpr);
363 writeUleb128(os, indirectFunctions.size(), "elem count");
364
365 uint32_t tableIndex = config->tableBase;
366 for (const FunctionSymbol *sym : indirectFunctions) {
367 assert(sym->getTableIndex() == tableIndex);
368 writeUleb128(os, sym->getFunctionIndex(), "function index");
369 ++tableIndex;
370 }
371 }
372
DataCountSection(ArrayRef<OutputSegment * > segments)373 DataCountSection::DataCountSection(ArrayRef<OutputSegment *> segments)
374 : SyntheticSection(llvm::wasm::WASM_SEC_DATACOUNT),
375 numSegments(std::count_if(
376 segments.begin(), segments.end(),
377 [](OutputSegment *const segment) { return !segment->isBss; })) {}
378
writeBody()379 void DataCountSection::writeBody() {
380 writeUleb128(bodyOutputStream, numSegments, "data count");
381 }
382
isNeeded() const383 bool DataCountSection::isNeeded() const {
384 return numSegments && config->sharedMemory;
385 }
386
writeBody()387 void LinkingSection::writeBody() {
388 raw_ostream &os = bodyOutputStream;
389
390 writeUleb128(os, WasmMetadataVersion, "Version");
391
392 if (!symtabEntries.empty()) {
393 SubSection sub(WASM_SYMBOL_TABLE);
394 writeUleb128(sub.os, symtabEntries.size(), "num symbols");
395
396 for (const Symbol *sym : symtabEntries) {
397 assert(sym->isDefined() || sym->isUndefined());
398 WasmSymbolType kind = sym->getWasmType();
399 uint32_t flags = sym->flags;
400
401 writeU8(sub.os, kind, "sym kind");
402 writeUleb128(sub.os, flags, "sym flags");
403
404 if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
405 writeUleb128(sub.os, f->getFunctionIndex(), "index");
406 if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
407 writeStr(sub.os, sym->getName(), "sym name");
408 } else if (auto *g = dyn_cast<GlobalSymbol>(sym)) {
409 writeUleb128(sub.os, g->getGlobalIndex(), "index");
410 if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
411 writeStr(sub.os, sym->getName(), "sym name");
412 } else if (auto *e = dyn_cast<EventSymbol>(sym)) {
413 writeUleb128(sub.os, e->getEventIndex(), "index");
414 if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
415 writeStr(sub.os, sym->getName(), "sym name");
416 } else if (isa<DataSymbol>(sym)) {
417 writeStr(sub.os, sym->getName(), "sym name");
418 if (auto *dataSym = dyn_cast<DefinedData>(sym)) {
419 writeUleb128(sub.os, dataSym->getOutputSegmentIndex(), "index");
420 writeUleb128(sub.os, dataSym->getOutputSegmentOffset(),
421 "data offset");
422 writeUleb128(sub.os, dataSym->getSize(), "data size");
423 }
424 } else {
425 auto *s = cast<OutputSectionSymbol>(sym);
426 writeUleb128(sub.os, s->section->sectionIndex, "sym section index");
427 }
428 }
429
430 sub.writeTo(os);
431 }
432
433 if (dataSegments.size()) {
434 SubSection sub(WASM_SEGMENT_INFO);
435 writeUleb128(sub.os, dataSegments.size(), "num data segments");
436 for (const OutputSegment *s : dataSegments) {
437 writeStr(sub.os, s->name, "segment name");
438 writeUleb128(sub.os, s->alignment, "alignment");
439 writeUleb128(sub.os, 0, "flags");
440 }
441 sub.writeTo(os);
442 }
443
444 if (!initFunctions.empty()) {
445 SubSection sub(WASM_INIT_FUNCS);
446 writeUleb128(sub.os, initFunctions.size(), "num init functions");
447 for (const WasmInitEntry &f : initFunctions) {
448 writeUleb128(sub.os, f.priority, "priority");
449 writeUleb128(sub.os, f.sym->getOutputSymbolIndex(), "function index");
450 }
451 sub.writeTo(os);
452 }
453
454 struct ComdatEntry {
455 unsigned kind;
456 uint32_t index;
457 };
458 std::map<StringRef, std::vector<ComdatEntry>> comdats;
459
460 for (const InputFunction *f : out.functionSec->inputFunctions) {
461 StringRef comdat = f->getComdatName();
462 if (!comdat.empty())
463 comdats[comdat].emplace_back(
464 ComdatEntry{WASM_COMDAT_FUNCTION, f->getFunctionIndex()});
465 }
466 for (uint32_t i = 0; i < dataSegments.size(); ++i) {
467 const auto &inputSegments = dataSegments[i]->inputSegments;
468 if (inputSegments.empty())
469 continue;
470 StringRef comdat = inputSegments[0]->getComdatName();
471 #ifndef NDEBUG
472 for (const InputSegment *isec : inputSegments)
473 assert(isec->getComdatName() == comdat);
474 #endif
475 if (!comdat.empty())
476 comdats[comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, i});
477 }
478
479 if (!comdats.empty()) {
480 SubSection sub(WASM_COMDAT_INFO);
481 writeUleb128(sub.os, comdats.size(), "num comdats");
482 for (const auto &c : comdats) {
483 writeStr(sub.os, c.first, "comdat name");
484 writeUleb128(sub.os, 0, "comdat flags"); // flags for future use
485 writeUleb128(sub.os, c.second.size(), "num entries");
486 for (const ComdatEntry &entry : c.second) {
487 writeU8(sub.os, entry.kind, "entry kind");
488 writeUleb128(sub.os, entry.index, "entry index");
489 }
490 }
491 sub.writeTo(os);
492 }
493 }
494
addToSymtab(Symbol * sym)495 void LinkingSection::addToSymtab(Symbol *sym) {
496 sym->setOutputSymbolIndex(symtabEntries.size());
497 symtabEntries.emplace_back(sym);
498 }
499
numNames() const500 unsigned NameSection::numNames() const {
501 unsigned numNames = out.importSec->getNumImportedFunctions();
502 for (const InputFunction *f : out.functionSec->inputFunctions)
503 if (!f->getName().empty() || !f->getDebugName().empty())
504 ++numNames;
505
506 return numNames;
507 }
508
509 // Create the custom "name" section containing debug symbol names.
writeBody()510 void NameSection::writeBody() {
511 SubSection sub(WASM_NAMES_FUNCTION);
512 writeUleb128(sub.os, numNames(), "name count");
513
514 // Names must appear in function index order. As it happens importedSymbols
515 // and inputFunctions are numbered in order with imported functions coming
516 // first.
517 for (const Symbol *s : out.importSec->importedSymbols) {
518 if (auto *f = dyn_cast<FunctionSymbol>(s)) {
519 writeUleb128(sub.os, f->getFunctionIndex(), "func index");
520 writeStr(sub.os, toString(*s), "symbol name");
521 }
522 }
523 for (const InputFunction *f : out.functionSec->inputFunctions) {
524 if (!f->getName().empty()) {
525 writeUleb128(sub.os, f->getFunctionIndex(), "func index");
526 if (!f->getDebugName().empty()) {
527 writeStr(sub.os, f->getDebugName(), "symbol name");
528 } else {
529 writeStr(sub.os, maybeDemangleSymbol(f->getName()), "symbol name");
530 }
531 }
532 }
533
534 sub.writeTo(bodyOutputStream);
535 }
536
addInfo(const WasmProducerInfo & info)537 void ProducersSection::addInfo(const WasmProducerInfo &info) {
538 for (auto &producers :
539 {std::make_pair(&info.Languages, &languages),
540 std::make_pair(&info.Tools, &tools), std::make_pair(&info.SDKs, &sDKs)})
541 for (auto &producer : *producers.first)
542 if (producers.second->end() ==
543 llvm::find_if(*producers.second,
544 [&](std::pair<std::string, std::string> seen) {
545 return seen.first == producer.first;
546 }))
547 producers.second->push_back(producer);
548 }
549
writeBody()550 void ProducersSection::writeBody() {
551 auto &os = bodyOutputStream;
552 writeUleb128(os, fieldCount(), "field count");
553 for (auto &field :
554 {std::make_pair("language", languages),
555 std::make_pair("processed-by", tools), std::make_pair("sdk", sDKs)}) {
556 if (field.second.empty())
557 continue;
558 writeStr(os, field.first, "field name");
559 writeUleb128(os, field.second.size(), "number of entries");
560 for (auto &entry : field.second) {
561 writeStr(os, entry.first, "producer name");
562 writeStr(os, entry.second, "producer version");
563 }
564 }
565 }
566
writeBody()567 void TargetFeaturesSection::writeBody() {
568 SmallVector<std::string, 8> emitted(features.begin(), features.end());
569 llvm::sort(emitted);
570 auto &os = bodyOutputStream;
571 writeUleb128(os, emitted.size(), "feature count");
572 for (auto &feature : emitted) {
573 writeU8(os, WASM_FEATURE_PREFIX_USED, "feature used prefix");
574 writeStr(os, feature, "feature name");
575 }
576 }
577
writeBody()578 void RelocSection::writeBody() {
579 uint32_t count = sec->getNumRelocations();
580 assert(sec->sectionIndex != UINT32_MAX);
581 writeUleb128(bodyOutputStream, sec->sectionIndex, "reloc section");
582 writeUleb128(bodyOutputStream, count, "reloc count");
583 sec->writeRelocations(bodyOutputStream);
584 }
585
586 } // namespace wasm
587 } // namespace lld
588