1//===- Writer.cpp ---------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Writer.h"
10#include "Config.h"
11#include "InputChunks.h"
12#include "InputElement.h"
13#include "MapFile.h"
14#include "OutputSections.h"
15#include "OutputSegment.h"
16#include "Relocations.h"
17#include "SymbolTable.h"
18#include "SyntheticSections.h"
19#include "WriterUtils.h"
20#include "lld/Common/Arrays.h"
21#include "lld/Common/CommonLinkerContext.h"
22#include "lld/Common/Strings.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/MapVector.h"
25#include "llvm/ADT/SmallSet.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/StringMap.h"
28#include "llvm/BinaryFormat/Wasm.h"
29#include "llvm/Support/FileOutputBuffer.h"
30#include "llvm/Support/FormatVariadic.h"
31#include "llvm/Support/Parallel.h"
32#include "llvm/Support/RandomNumberGenerator.h"
33#include "llvm/Support/SHA1.h"
34#include "llvm/Support/xxhash.h"
35
36#include <cstdarg>
37#include <list>
38#include <optional>
39
40#define DEBUG_TYPE "lld"
41
42using namespace llvm;
43using namespace llvm::wasm;
44
45namespace lld::wasm {
46static constexpr int stackAlignment = 16;
47static constexpr int heapAlignment = 16;
48
49namespace {
50
51// The writer writes a SymbolTable result to a file.
52class Writer {
53public:
54 void run();
55
56private:
57 void openFile();
58
59 bool needsPassiveInitialization(const OutputSegment *segment);
60 bool hasPassiveInitializedSegments();
61
62 void createSyntheticInitFunctions();
63 void createInitMemoryFunction();
64 void createStartFunction();
65 void createApplyDataRelocationsFunction();
66 void createApplyGlobalRelocationsFunction();
67 void createApplyTLSRelocationsFunction();
68 void createApplyGlobalTLSRelocationsFunction();
69 void createCallCtorsFunction();
70 void createInitTLSFunction();
71 void createCommandExportWrappers();
72 void createCommandExportWrapper(uint32_t functionIndex, DefinedFunction *f);
73
74 void assignIndexes();
75 void populateSymtab();
76 void populateProducers();
77 void populateTargetFeatures();
78 // populateTargetFeatures happens early on so some checks are delayed
79 // until imports and exports are finalized. There are run unstead
80 // in checkImportExportTargetFeatures
81 void checkImportExportTargetFeatures();
82 void calculateInitFunctions();
83 void calculateImports();
84 void calculateExports();
85 void calculateCustomSections();
86 void calculateTypes();
87 void createOutputSegments();
88 void allocateCommonSymbols();
89 OutputSegment *createOutputSegment(StringRef name);
90 void combineActiveOutputSegments();
91 void layoutMemory();
92 void createHeader();
93
94 void addSection(OutputSection *sec);
95
96 void addSections();
97
98 void createCustomSections();
99 void createSyntheticSections();
100 void createSyntheticSectionsPostLayout();
101 void finalizeSections();
102
103 // Custom sections
104 void createRelocSections();
105
106 void writeHeader();
107 void writeSections();
108 void writeBuildId();
109
110 uint64_t fileSize = 0;
111
112 std::vector<WasmInitEntry> initFunctions;
113 llvm::MapVector<StringRef, std::vector<InputChunk *>> customSectionMapping;
114
115 // Stable storage for command export wrapper function name strings.
116 std::list<std::string> commandExportWrapperNames;
117
118 // Elements that are used to construct the final output
119 std::string header;
120 std::vector<OutputSection *> outputSections;
121
122 std::unique_ptr<FileOutputBuffer> buffer;
123
124 std::vector<OutputSegment *> segments;
125 using SegmentKey = std::pair<StringRef, uint32_t>;
126 llvm::SmallDenseMap<SegmentKey, OutputSegment *> segmentMap;
127};
128
129void writeSetTLSBase(const Ctx &ctx, raw_ostream &os) {
130 if (ctx.arg.libcallThreadContext) {
131 writeU8(os, byte: WASM_OPCODE_CALL, msg: "call");
132 writeUleb128(os, number: ctx.sym.setTLSBase->getFunctionIndex(), msg: "function index");
133 } else {
134 writeU8(os, byte: WASM_OPCODE_GLOBAL_SET, msg: "GLOBAL_SET");
135 writeUleb128(os, number: ctx.sym.tlsBase->getGlobalIndex(), msg: "__tls_base");
136 }
137}
138} // anonymous namespace
139
140void Writer::calculateCustomSections() {
141 log(msg: "calculateCustomSections");
142 bool stripDebug = ctx.arg.stripDebug || ctx.arg.stripAll;
143 for (ObjFile *file : ctx.objectFiles) {
144 for (InputChunk *section : file->customSections) {
145 // Exclude COMDAT sections that are not selected for inclusion
146 if (section->discarded)
147 continue;
148 // Ignore empty custom sections. In particular objcopy/strip will
149 // sometimes replace stripped sections with empty custom sections to
150 // avoid section re-numbering.
151 if (section->getSize() == 0)
152 continue;
153 StringRef name = section->name;
154 // These custom sections are known the linker and synthesized rather than
155 // blindly copied.
156 if (name == "linking" || name == "name" || name == "producers" ||
157 name == "target_features" || name.starts_with(Prefix: "reloc."))
158 continue;
159 // These custom sections are generated by `clang -fembed-bitcode`.
160 // These are used by the rust toolchain to ship LTO data along with
161 // compiled object code, but they don't want this included in the linker
162 // output.
163 if (name == ".llvmbc" || name == ".llvmcmd")
164 continue;
165 // Strip debug section in that option was specified.
166 if (stripDebug && name.starts_with(Prefix: ".debug_"))
167 continue;
168 // Otherwise include custom sections by default and concatenate their
169 // contents.
170 customSectionMapping[name].push_back(x: section);
171 }
172 }
173}
174
175void Writer::createCustomSections() {
176 log(msg: "createCustomSections");
177 for (auto &pair : customSectionMapping) {
178 StringRef name = pair.first;
179 LLVM_DEBUG(dbgs() << "createCustomSection: " << name << "\n");
180
181 OutputSection *sec = make<CustomSection>(args: std::string(name), args&: pair.second);
182 if (ctx.arg.relocatable || ctx.arg.emitRelocs) {
183 auto *sym = make<OutputSectionSymbol>(args&: sec);
184 out.linkingSec->addToSymtab(sym);
185 sec->sectionSym = sym;
186 }
187 addSection(sec);
188 }
189}
190
191// Create relocations sections in the final output.
192// These are only created when relocatable output is requested.
193void Writer::createRelocSections() {
194 log(msg: "createRelocSections");
195 // Don't use iterator here since we are adding to OutputSection
196 size_t origSize = outputSections.size();
197 for (size_t i = 0; i < origSize; i++) {
198 LLVM_DEBUG(dbgs() << "check section " << i << "\n");
199 OutputSection *sec = outputSections[i];
200
201 // Count the number of needed sections.
202 uint32_t count = sec->getNumRelocations();
203 if (!count)
204 continue;
205
206 StringRef name;
207 if (sec->type == WASM_SEC_DATA)
208 name = "reloc.DATA";
209 else if (sec->type == WASM_SEC_CODE)
210 name = "reloc.CODE";
211 else if (sec->type == WASM_SEC_CUSTOM)
212 name = saver().save(S: "reloc." + sec->name);
213 else
214 llvm_unreachable(
215 "relocations only supported for code, data, or custom sections");
216
217 addSection(sec: make<RelocSection>(args&: name, args&: sec));
218 }
219}
220
221void Writer::populateProducers() {
222 for (ObjFile *file : ctx.objectFiles) {
223 const WasmProducerInfo &info = file->getWasmObj()->getProducerInfo();
224 out.producersSec->addInfo(info);
225 }
226}
227
228void Writer::writeHeader() {
229 memcpy(dest: buffer->getBufferStart(), src: header.data(), n: header.size());
230}
231
232void Writer::writeSections() {
233 uint8_t *buf = buffer->getBufferStart();
234 parallelForEach(R&: outputSections, Fn: [buf](OutputSection *s) {
235 assert(s->isNeeded());
236 s->writeTo(buf);
237 });
238}
239
240// Computes a hash value of Data using a given hash function.
241// In order to utilize multiple cores, we first split data into 1MB
242// chunks, compute a hash for each chunk, and then compute a hash value
243// of the hash values.
244
245static void
246computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,
247 llvm::ArrayRef<uint8_t> data,
248 std::function<void(uint8_t *dest, ArrayRef<uint8_t> arr)> hashFn) {
249 std::vector<ArrayRef<uint8_t>> chunks = split(arr: data, chunkSize: 1024 * 1024);
250 std::vector<uint8_t> hashes(chunks.size() * hashBuf.size());
251
252 // Compute hash values.
253 parallelFor(Begin: 0, End: chunks.size(), Fn: [&](size_t i) {
254 hashFn(hashes.data() + i * hashBuf.size(), chunks[i]);
255 });
256
257 // Write to the final output buffer.
258 hashFn(hashBuf.data(), hashes);
259}
260
261static void makeUUID(unsigned version, llvm::ArrayRef<uint8_t> fileHash,
262 llvm::MutableArrayRef<uint8_t> output) {
263 assert((version == 4 || version == 5) && "Unknown UUID version");
264 assert(output.size() == 16 && "Wrong size for UUID output");
265 if (version == 5) {
266 // Build a valid v5 UUID from a hardcoded (randomly-generated) namespace
267 // UUID, and the computed hash of the output.
268 std::array<uint8_t, 16> namespaceUUID{0xA1, 0xFA, 0x48, 0x2D, 0x0E, 0x22,
269 0x03, 0x8D, 0x33, 0x8B, 0x52, 0x1C,
270 0xD6, 0xD2, 0x12, 0xB2};
271 SHA1 sha;
272 sha.update(Data: namespaceUUID);
273 sha.update(Data: fileHash);
274 auto s = sha.final();
275 std::copy(first: s.data(), last: &s.data()[output.size()], result: output.data());
276 } else if (version == 4) {
277 if (auto ec = llvm::getRandomBytes(Buffer: output.data(), Size: output.size()))
278 error(msg: "entropy source failure: " + ec.message());
279 }
280 // Set the UUID version and variant fields.
281 // The version is the upper nibble of byte 6 (0b0101xxxx or 0b0100xxxx)
282 output[6] = (static_cast<uint8_t>(version) << 4) | (output[6] & 0xF);
283
284 // The variant is DCE 1.1/ISO 11578 (0b10xxxxxx)
285 output[8] &= 0xBF;
286 output[8] |= 0x80;
287}
288
289void Writer::writeBuildId() {
290 if (!out.buildIdSec->isNeeded())
291 return;
292 if (ctx.arg.buildId == BuildIdKind::Hexstring) {
293 out.buildIdSec->writeBuildId(buf: ctx.arg.buildIdVector);
294 return;
295 }
296
297 // Compute a hash of all sections of the output file.
298 size_t hashSize = out.buildIdSec->hashSize;
299 std::vector<uint8_t> buildId(hashSize);
300 llvm::ArrayRef<uint8_t> buf{buffer->getBufferStart(), size_t(fileSize)};
301
302 switch (ctx.arg.buildId) {
303 case BuildIdKind::Fast: {
304 std::vector<uint8_t> fileHash(8);
305 computeHash(hashBuf: fileHash, data: buf, hashFn: [](uint8_t *dest, ArrayRef<uint8_t> arr) {
306 support::endian::write64le(P: dest, V: xxh3_64bits(data: arr));
307 });
308 makeUUID(version: 5, fileHash, output: buildId);
309 break;
310 }
311 case BuildIdKind::Sha1:
312 computeHash(hashBuf: buildId, data: buf, hashFn: [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
313 memcpy(dest: dest, src: SHA1::hash(Data: arr).data(), n: hashSize);
314 });
315 break;
316 case BuildIdKind::Uuid:
317 makeUUID(version: 4, fileHash: {}, output: buildId);
318 break;
319 default:
320 llvm_unreachable("unknown BuildIdKind");
321 }
322 out.buildIdSec->writeBuildId(buf: buildId);
323}
324
325static void setGlobalPtr(DefinedGlobal *g, uint64_t memoryPtr) {
326 LLVM_DEBUG(dbgs() << "setGlobalPtr " << g->getName() << " -> " << memoryPtr
327 << "\n");
328 g->global->setPointerValue(memoryPtr);
329}
330
331static void checkPageAligned(StringRef name, uint64_t value) {
332 if (value != alignTo(Value: value, Align: ctx.arg.pageSize))
333 error(msg: name + " must be aligned to the page size (" +
334 Twine(ctx.arg.pageSize) + " bytes)");
335}
336
337// Fix the memory layout of the output binary. This assigns memory offsets
338// to each of the input data sections as well as the explicit stack region.
339// The default memory layout is as follows, from low to high.
340//
341// - initialized data (starting at ctx.arg.globalBase)
342// - BSS data (not currently implemented in llvm)
343// - explicit stack (ctx.arg.ZStackSize)
344// - heap start / unallocated
345//
346// The --stack-first option means that stack is placed before any static data.
347// This can be useful since it means that stack overflow traps immediately
348// rather than overwriting global data, but also increases code size since all
349// static data loads and stores requires larger offsets.
350void Writer::layoutMemory() {
351 uint64_t memoryPtr = 0;
352
353 auto placeStack = [&]() {
354 if (ctx.arg.relocatable || ctx.isPic)
355 return;
356 memoryPtr = alignTo(Value: memoryPtr, Align: stackAlignment);
357 if (ctx.sym.stackLow)
358 ctx.sym.stackLow->setVA(memoryPtr);
359 if (ctx.arg.zStackSize != alignTo(Value: ctx.arg.zStackSize, Align: stackAlignment))
360 error(msg: "stack size must be " + Twine(stackAlignment) + "-byte aligned");
361 log(msg: "mem: stack size = " + Twine(ctx.arg.zStackSize));
362 log(msg: "mem: stack base = " + Twine(memoryPtr));
363 memoryPtr += ctx.arg.zStackSize;
364 setGlobalPtr(g: cast<DefinedGlobal>(Val: ctx.sym.stackPointer), memoryPtr);
365 if (ctx.sym.stackHigh)
366 ctx.sym.stackHigh->setVA(memoryPtr);
367 log(msg: "mem: stack top = " + Twine(memoryPtr));
368 };
369
370 if (ctx.arg.stackFirst) {
371 placeStack();
372 if (ctx.arg.globalBase) {
373 if (ctx.arg.globalBase < memoryPtr) {
374 error(msg: "--global-base cannot be less than stack size when --stack-first "
375 "is used");
376 return;
377 }
378 memoryPtr = ctx.arg.globalBase;
379 }
380 } else {
381 memoryPtr = ctx.arg.globalBase;
382 }
383
384 log(msg: "mem: global base = " + Twine(memoryPtr));
385 if (ctx.sym.globalBase)
386 ctx.sym.globalBase->setVA(memoryPtr);
387
388 uint64_t dataStart = memoryPtr;
389
390 // Arbitrarily set __dso_handle handle to point to the start of the data
391 // segments.
392 if (ctx.sym.dsoHandle)
393 ctx.sym.dsoHandle->setVA(dataStart);
394
395 out.dylinkSec->memAlign = 0;
396 uint64_t fixedTLSBase = memoryPtr;
397 for (OutputSegment *seg : segments) {
398 out.dylinkSec->memAlign = std::max(a: out.dylinkSec->memAlign, b: seg->alignment);
399 memoryPtr = alignTo(Value: memoryPtr, Align: 1ULL << seg->alignment);
400 seg->startVA = memoryPtr;
401 log(msg: formatv(Fmt: "mem: {0,-15} offset={1,-8} size={2,-8} align={3}", Vals&: seg->name,
402 Vals&: memoryPtr, Vals&: seg->size, Vals&: seg->alignment));
403
404 if (!ctx.arg.relocatable && seg->isTLS()) {
405 if (ctx.sym.tlsSize) {
406 setGlobalPtr(g: ctx.sym.tlsSize, memoryPtr: seg->size);
407 }
408 if (ctx.sym.tlsAlign) {
409 setGlobalPtr(g: ctx.sym.tlsAlign, memoryPtr: int64_t{1} << seg->alignment);
410 }
411 fixedTLSBase = memoryPtr;
412 }
413
414 if (ctx.sym.rodataStart && seg->name.starts_with(Prefix: ".rodata") &&
415 !ctx.sym.rodataStart->getVA())
416 ctx.sym.rodataStart->setVA(memoryPtr);
417
418 memoryPtr += seg->size;
419
420 // Might get set more than once if segment merging is not enabled.
421 if (ctx.sym.rodataEnd && seg->name.starts_with(Prefix: ".rodata"))
422 ctx.sym.rodataEnd->setVA(memoryPtr);
423 }
424
425 // In single-threaded builds we set __tls_base statically.
426 // Even in the absence of any actual TLS data, this symbol can still be
427 // referenced (for example by __builtin_thread_pointer, which should not
428 // return NULL).
429 if (!ctx.arg.isMultithreaded() && ctx.sym.tlsBase) {
430 setGlobalPtr(g: ctx.sym.tlsBase, memoryPtr: fixedTLSBase);
431 }
432
433 // Make space for the memory initialization flag
434 if (ctx.arg.sharedMemory && hasPassiveInitializedSegments()) {
435 memoryPtr = alignTo(Value: memoryPtr, Align: 4);
436 ctx.sym.initMemoryFlag = symtab->addSyntheticDataSymbol(
437 name: "__wasm_init_memory_flag", flags: WASM_SYMBOL_VISIBILITY_HIDDEN);
438 ctx.sym.initMemoryFlag->markLive();
439 ctx.sym.initMemoryFlag->setVA(memoryPtr);
440 log(msg: formatv(Fmt: "mem: {0,-15} offset={1,-8} size={2,-8} align={3}",
441 Vals: "__wasm_init_memory_flag", Vals&: memoryPtr, Vals: 4, Vals: 4));
442 memoryPtr += 4;
443 }
444
445 if (ctx.sym.dataEnd)
446 ctx.sym.dataEnd->setVA(memoryPtr);
447
448 uint64_t staticDataSize = memoryPtr - dataStart;
449 log(msg: "mem: static data = " + Twine(staticDataSize));
450 if (ctx.isPic)
451 out.dylinkSec->memSize = staticDataSize;
452
453 if (!ctx.arg.stackFirst)
454 placeStack();
455
456 if (ctx.sym.heapBase) {
457 // Set `__heap_base` to follow the end of the stack or global data. The
458 // fact that this comes last means that a malloc/brk implementation can
459 // grow the heap at runtime.
460 // We'll align the heap base here because memory allocators might expect
461 // __heap_base to be aligned already.
462 memoryPtr = alignTo(Value: memoryPtr, Align: heapAlignment);
463 log(msg: "mem: heap base = " + Twine(memoryPtr));
464 ctx.sym.heapBase->setVA(memoryPtr);
465 }
466
467 uint64_t maxMemorySetting = 1ULL << 32;
468 if (ctx.arg.is64.value_or(u: false)) {
469 // TODO: Update once we decide on a reasonable limit here:
470 // https://github.com/WebAssembly/memory64/issues/33
471 maxMemorySetting = 1ULL << 34;
472 }
473
474 if (ctx.arg.initialHeap != 0) {
475 checkPageAligned(name: "initial heap", value: ctx.arg.initialHeap);
476 uint64_t maxInitialHeap = maxMemorySetting - memoryPtr;
477 if (ctx.arg.initialHeap > maxInitialHeap)
478 error(msg: "initial heap too large, cannot be greater than " +
479 Twine(maxInitialHeap));
480 memoryPtr += ctx.arg.initialHeap;
481 }
482
483 if (ctx.arg.initialMemory != 0) {
484 checkPageAligned(name: "initial memory", value: ctx.arg.initialMemory);
485 if (memoryPtr > ctx.arg.initialMemory)
486 error(msg: "initial memory too small, " + Twine(memoryPtr) + " bytes needed");
487 if (ctx.arg.initialMemory > maxMemorySetting)
488 error(msg: "initial memory too large, cannot be greater than " +
489 Twine(maxMemorySetting));
490 memoryPtr = ctx.arg.initialMemory;
491 }
492
493 memoryPtr = alignTo(Value: memoryPtr, Align: ctx.arg.pageSize);
494
495 out.memorySec->numMemoryPages = memoryPtr / ctx.arg.pageSize;
496 log(msg: "mem: total pages = " + Twine(out.memorySec->numMemoryPages));
497
498 if (ctx.sym.heapEnd) {
499 // Set `__heap_end` to follow the end of the statically allocated linear
500 // memory. The fact that this comes last means that a malloc/brk
501 // implementation can grow the heap at runtime.
502 log(msg: "mem: heap end = " + Twine(memoryPtr));
503 ctx.sym.heapEnd->setVA(memoryPtr);
504 }
505
506 uint64_t maxMemory = 0;
507 if (ctx.arg.maxMemory != 0) {
508 checkPageAligned(name: "maximum memory", value: ctx.arg.maxMemory);
509 if (memoryPtr > ctx.arg.maxMemory)
510 error(msg: "maximum memory too small, " + Twine(memoryPtr) + " bytes needed");
511 if (ctx.arg.maxMemory > maxMemorySetting)
512 error(msg: "maximum memory too large, cannot be greater than " +
513 Twine(maxMemorySetting));
514
515 maxMemory = ctx.arg.maxMemory;
516 } else if (ctx.arg.noGrowableMemory) {
517 maxMemory = memoryPtr;
518 }
519
520 // If no maxMemory config was supplied but we are building with
521 // shared memory, we need to pick a sensible upper limit.
522 if (ctx.arg.sharedMemory && maxMemory == 0) {
523 if (ctx.isPic)
524 maxMemory = maxMemorySetting;
525 else
526 maxMemory = memoryPtr;
527 }
528
529 if (maxMemory != 0) {
530 out.memorySec->maxMemoryPages = maxMemory / ctx.arg.pageSize;
531 log(msg: "mem: max pages = " + Twine(out.memorySec->maxMemoryPages));
532 }
533}
534
535void Writer::addSection(OutputSection *sec) {
536 if (!sec->isNeeded())
537 return;
538 log(msg: "addSection: " + toString(section: *sec));
539 sec->sectionIndex = outputSections.size();
540 outputSections.push_back(x: sec);
541}
542
543// If a section name is valid as a C identifier (which is rare because of
544// the leading '.'), linkers are expected to define __start_<secname> and
545// __stop_<secname> symbols. They are at beginning and end of the section,
546// respectively. This is not requested by the ELF standard, but GNU ld and
547// gold provide the feature, and used by many programs.
548static void addStartStopSymbols(const OutputSegment *seg) {
549 StringRef name = seg->name;
550 if (!isValidCIdentifier(s: name))
551 return;
552 LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << name << "\n");
553 uint64_t start = seg->startVA;
554 uint64_t stop = start + seg->size;
555 symtab->addOptionalDataSymbol(name: saver().save(S: "__start_" + name), value: start);
556 symtab->addOptionalDataSymbol(name: saver().save(S: "__stop_" + name), value: stop);
557}
558
559void Writer::addSections() {
560 addSection(sec: out.dylinkSec);
561 addSection(sec: out.typeSec);
562 addSection(sec: out.importSec);
563 addSection(sec: out.functionSec);
564 addSection(sec: out.tableSec);
565 addSection(sec: out.memorySec);
566 addSection(sec: out.tagSec);
567 addSection(sec: out.globalSec);
568 addSection(sec: out.exportSec);
569 addSection(sec: out.startSec);
570 addSection(sec: out.elemSec);
571 addSection(sec: out.dataCountSec);
572
573 addSection(sec: make<CodeSection>(args&: out.functionSec->inputFunctions));
574 addSection(sec: make<DataSection>(args&: segments));
575
576 createCustomSections();
577
578 addSection(sec: out.linkingSec);
579 if (ctx.arg.emitRelocs || ctx.arg.relocatable) {
580 createRelocSections();
581 }
582
583 addSection(sec: out.nameSec);
584 addSection(sec: out.producersSec);
585 addSection(sec: out.targetFeaturesSec);
586 addSection(sec: out.buildIdSec);
587}
588
589void Writer::finalizeSections() {
590 for (OutputSection *s : outputSections) {
591 s->setOffset(fileSize);
592 s->finalizeContents();
593 fileSize += s->getSize();
594 }
595}
596
597void Writer::populateTargetFeatures() {
598 StringMap<std::string> used;
599 StringMap<std::string> disallowed;
600 SmallSet<std::string, 8> &allowed = out.targetFeaturesSec->features;
601 bool tlsUsed = false;
602
603 if (ctx.isPic) {
604 // This should not be necessary because all PIC objects should
605 // contain the `mutable-globals` feature.
606 // TODO (https://github.com/llvm/llvm-project/issues/51681)
607 allowed.insert(V: "mutable-globals");
608 }
609
610 if (ctx.arg.extraFeatures.has_value()) {
611 auto &extraFeatures = *ctx.arg.extraFeatures;
612 allowed.insert_range(R&: extraFeatures);
613 }
614
615 // Only infer used features if user did not specify features
616 bool inferFeatures = !ctx.arg.features.has_value();
617
618 if (!inferFeatures) {
619 auto &explicitFeatures = *ctx.arg.features;
620 allowed.insert_range(R&: explicitFeatures);
621 if (!ctx.arg.checkFeatures)
622 goto done;
623 }
624
625 // Find the sets of used and disallowed features
626 for (ObjFile *file : ctx.objectFiles) {
627 StringRef fileName(file->getName());
628 for (auto &feature : file->getWasmObj()->getTargetFeatures()) {
629 switch (feature.Prefix) {
630 case WASM_FEATURE_PREFIX_USED:
631 used.insert(KV: {feature.Name, std::string(fileName)});
632 break;
633 case WASM_FEATURE_PREFIX_DISALLOWED:
634 disallowed.insert(KV: {feature.Name, std::string(fileName)});
635 break;
636 default:
637 error(msg: "Unrecognized feature policy prefix " +
638 std::to_string(val: feature.Prefix));
639 }
640 }
641
642 // Find TLS data segments
643 auto isTLS = [](InputChunk *segment) {
644 return segment->live && segment->isTLS();
645 };
646 tlsUsed = tlsUsed || llvm::any_of(Range&: file->segments, P: isTLS);
647
648 // Ensure that we're not mixing incompatible thread context models
649 if (ctx.arg.libcallThreadContext &&
650 llvm::any_of(Range: file->getSymbols(), P: [](const auto &sym) {
651 return sym && sym->getName() == "__stack_pointer" &&
652 sym->kind() == Symbol::UndefinedGlobalKind &&
653 sym->importModule && sym->importModule == "env";
654 }))
655 error(msg: fileName + ": object file uses globals for thread context, "
656 "but --cooperative-threading was specified");
657 }
658
659 if (inferFeatures)
660 for (const auto &key : used.keys())
661 allowed.insert(V: std::string(key));
662
663 if (!ctx.arg.checkFeatures)
664 goto done;
665
666 if (ctx.arg.sharedMemory) {
667 if (disallowed.contains(Key: "shared-mem"))
668 error(msg: "--shared-memory is disallowed by " + disallowed["shared-mem"] +
669 " because it was not compiled with 'atomics' or 'bulk-memory' "
670 "features.");
671
672 for (auto feature : {"atomics", "bulk-memory"})
673 if (!allowed.contains(V: feature))
674 error(msg: StringRef("'") + feature +
675 "' feature must be used in order to use shared memory");
676 }
677
678 if (tlsUsed) {
679 if (!allowed.contains(V: "bulk-memory"))
680 error(msg: "'bulk-memory' feature must be used in order to use thread-local "
681 "storage");
682 if (!allowed.contains(V: "atomics") && !ctx.arg.cooperativeThreading)
683 error(msg: "'atomics' feature must be used in order to use thread-local "
684 "storage");
685 }
686
687 // Validate that used features are allowed in output
688 if (!inferFeatures) {
689 for (const auto &feature : used.keys()) {
690 if (!allowed.contains(V: std::string(feature)))
691 error(msg: Twine("Target feature '") + feature + "' used by " +
692 used[feature] + " is not allowed.");
693 }
694 }
695
696 // Validate the disallowed constraints for each file
697 for (ObjFile *file : ctx.objectFiles) {
698 StringRef fileName(file->getName());
699 SmallSet<std::string, 8> objectFeatures;
700 for (const auto &feature : file->getWasmObj()->getTargetFeatures()) {
701 if (feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED)
702 continue;
703 objectFeatures.insert(V: feature.Name);
704 if (disallowed.contains(Key: feature.Name))
705 error(msg: Twine("Target feature '") + feature.Name + "' used in " +
706 fileName + " is disallowed by " + disallowed[feature.Name] +
707 ". Use --no-check-features to suppress.");
708 }
709 }
710
711done:
712 // Normally we don't include bss segments in the binary. In particular if
713 // memory is not being imported then we can assume its zero initialized.
714 // In the case the memory is imported, and we can use the memory.fill
715 // instruction, then we can also avoid including the segments.
716 // Finally, if we are emitting relocations, they may refer to locations within
717 // the bss segments, so these segments need to exist in the binary.
718 if (ctx.arg.emitRelocs ||
719 (ctx.arg.memoryImport.has_value() && !allowed.contains(V: "bulk-memory")))
720 ctx.emitBssSegments = true;
721
722 if (allowed.contains(V: "extended-const"))
723 ctx.arg.extendedConst = true;
724
725 for (auto &feature : allowed)
726 log(msg: "Allowed feature: " + feature);
727}
728
729void Writer::checkImportExportTargetFeatures() {
730 if (ctx.arg.relocatable || !ctx.arg.checkFeatures)
731 return;
732
733 if (!out.targetFeaturesSec->features.contains(V: "mutable-globals")) {
734 for (const Symbol *sym : out.importSec->importedSymbols) {
735 if (auto *global = dyn_cast<GlobalSymbol>(Val: sym)) {
736 if (global->getGlobalType()->Mutable) {
737 error(msg: Twine("mutable global imported but 'mutable-globals' feature "
738 "not present in inputs: `") +
739 toString(sym: *sym) + "`. Use --no-check-features to suppress.");
740 }
741 }
742 }
743 for (const Symbol *sym : out.exportSec->exportedSymbols) {
744 if (auto *global = dyn_cast<GlobalSymbol>(Val: sym)) {
745 if (global->getGlobalType()->Mutable) {
746 error(msg: Twine("mutable global exported but 'mutable-globals' feature "
747 "not present in inputs: `") +
748 toString(sym: *sym) + "`. Use --no-check-features to suppress.");
749 }
750 }
751 }
752 }
753}
754
755static bool shouldImport(Symbol *sym) {
756 // We don't generate imports for data symbols. They however can be imported
757 // as GOT entries.
758 if (isa<DataSymbol>(Val: sym))
759 return false;
760 if (!sym->isLive())
761 return false;
762 if (!sym->isUsedInRegularObj)
763 return false;
764
765 // When a symbol is weakly defined in a shared library we need to allow
766 // it to be overridden by another module so need to both import
767 // and export the symbol.
768 if (ctx.arg.shared && sym->isWeak() && !sym->isUndefined() &&
769 !sym->isHidden())
770 return true;
771 if (sym->isShared())
772 return true;
773 if (!sym->isUndefined())
774 return false;
775 if (sym->isWeak() && !ctx.arg.relocatable && !ctx.isPic)
776 return false;
777
778 // In PIC mode we only need to import functions when they are called directly.
779 // Indirect usage all goes via GOT imports.
780 if (ctx.isPic) {
781 if (auto *f = dyn_cast<UndefinedFunction>(Val: sym))
782 if (!f->isCalledDirectly)
783 return false;
784 }
785
786 if (ctx.isPic || ctx.arg.relocatable || ctx.arg.importUndefined ||
787 ctx.arg.unresolvedSymbols == UnresolvedPolicy::ImportDynamic)
788 return true;
789 if (ctx.arg.allowUndefinedSymbols.contains(key: sym->getName()))
790 return true;
791
792 return sym->isImported();
793}
794
795void Writer::calculateImports() {
796 // Some inputs require that the indirect function table be assigned to table
797 // number 0, so if it is present and is an import, allocate it before any
798 // other tables.
799 if (ctx.sym.indirectFunctionTable &&
800 shouldImport(sym: ctx.sym.indirectFunctionTable))
801 out.importSec->addImport(sym: ctx.sym.indirectFunctionTable);
802
803 for (Symbol *sym : symtab->symbols()) {
804 if (!shouldImport(sym))
805 continue;
806 if (sym == ctx.sym.indirectFunctionTable)
807 continue;
808 LLVM_DEBUG(dbgs() << "import: " << sym->getName() << "\n");
809 out.importSec->addImport(sym);
810 }
811}
812
813void Writer::calculateExports() {
814 if (ctx.arg.relocatable)
815 return;
816
817 if (!ctx.arg.relocatable && ctx.arg.memoryExport.has_value()) {
818 out.exportSec->exports.push_back(
819 x: WasmExport{.Name: *ctx.arg.memoryExport, .Kind: WASM_EXTERNAL_MEMORY, .Index: 0});
820 }
821
822 unsigned globalIndex =
823 out.importSec->getNumImportedGlobals() + out.globalSec->numGlobals();
824
825 bool hasMutableGlobals =
826 out.targetFeaturesSec->features.contains(V: "mutable-globals");
827
828 for (Symbol *sym : symtab->symbols()) {
829 if (!sym->isExported())
830 continue;
831 if (!sym->isLive())
832 continue;
833 if (isa<SharedFunctionSymbol>(Val: sym) || sym->isShared())
834 continue;
835
836 StringRef name = sym->getName();
837 LLVM_DEBUG(dbgs() << "Export: " << name << "\n");
838 WasmExport export_;
839 if (auto *f = dyn_cast<DefinedFunction>(Val: sym)) {
840 if (std::optional<StringRef> exportName = f->function->getExportName()) {
841 name = *exportName;
842 }
843 export_ = {.Name: name, .Kind: WASM_EXTERNAL_FUNCTION, .Index: f->getExportedFunctionIndex()};
844 } else if (auto *g = dyn_cast<DefinedGlobal>(Val: sym)) {
845 if (!hasMutableGlobals && g->getGlobalType()->Mutable && !g->getFile() &&
846 !g->isExportedExplicit()) {
847 // Avoid exporting mutable globals are linker synthesized (e.g.
848 // __stack_pointer or __tls_base) unless they are explicitly exported
849 // from the command line.
850 // Without this check `--export-all` would cause any program using the
851 // stack pointer to export a mutable global even if none of the input
852 // files were built with the `mutable-globals` feature.
853 continue;
854 }
855 export_ = {.Name: name, .Kind: WASM_EXTERNAL_GLOBAL, .Index: g->getGlobalIndex()};
856 } else if (auto *t = dyn_cast<DefinedTag>(Val: sym)) {
857 export_ = {.Name: name, .Kind: WASM_EXTERNAL_TAG, .Index: t->getTagIndex()};
858 } else if (auto *d = dyn_cast<DefinedData>(Val: sym)) {
859 out.globalSec->dataAddressGlobals.push_back(x: d);
860 export_ = {.Name: name, .Kind: WASM_EXTERNAL_GLOBAL, .Index: globalIndex++};
861 } else {
862 auto *t = cast<DefinedTable>(Val: sym);
863 export_ = {.Name: name, .Kind: WASM_EXTERNAL_TABLE, .Index: t->getTableNumber()};
864 }
865
866 out.exportSec->exports.push_back(x: export_);
867 out.exportSec->exportedSymbols.push_back(x: sym);
868 }
869}
870
871void Writer::populateSymtab() {
872 if (!ctx.arg.relocatable && !ctx.arg.emitRelocs)
873 return;
874
875 for (Symbol *sym : symtab->symbols())
876 if (sym->isUsedInRegularObj && sym->isLive() && !sym->isShared())
877 out.linkingSec->addToSymtab(sym);
878
879 for (ObjFile *file : ctx.objectFiles) {
880 LLVM_DEBUG(dbgs() << "Local symtab entries: " << file->getName() << "\n");
881 for (Symbol *sym : file->getSymbols())
882 if (sym->isLocal() && !isa<SectionSymbol>(Val: sym) && sym->isLive())
883 out.linkingSec->addToSymtab(sym);
884 }
885}
886
887void Writer::calculateTypes() {
888 // The output type section is the union of the following sets:
889 // 1. Any signature used in the TYPE relocation
890 // 2. The signatures of all imported functions
891 // 3. The signatures of all defined functions
892 // 4. The signatures of all imported tags
893 // 5. The signatures of all defined tags
894
895 for (ObjFile *file : ctx.objectFiles) {
896 ArrayRef<WasmSignature> types = file->getWasmObj()->types();
897 for (uint32_t i = 0; i < types.size(); i++)
898 if (file->typeIsUsed[i])
899 file->typeMap[i] = out.typeSec->registerType(sig: types[i]);
900 }
901
902 for (const Symbol *sym : out.importSec->importedSymbols) {
903 if (auto *f = dyn_cast<FunctionSymbol>(Val: sym))
904 out.typeSec->registerType(sig: *f->signature);
905 else if (auto *t = dyn_cast<TagSymbol>(Val: sym))
906 out.typeSec->registerType(sig: *t->signature);
907 }
908
909 for (const InputFunction *f : out.functionSec->inputFunctions)
910 out.typeSec->registerType(sig: f->signature);
911
912 for (const InputTag *t : out.tagSec->inputTags)
913 out.typeSec->registerType(sig: t->signature);
914}
915
916// In a command-style link, create a wrapper for each exported symbol
917// which calls the constructors and destructors.
918void Writer::createCommandExportWrappers() {
919 // This logic doesn't currently support Emscripten-style PIC mode.
920 assert(!ctx.isPic);
921
922 // If there are no ctors and there's no libc `__wasm_call_dtors` to
923 // call, don't wrap the exports.
924 if (initFunctions.empty() && ctx.sym.callDtors == nullptr)
925 return;
926
927 std::vector<DefinedFunction *> toWrap;
928
929 for (Symbol *sym : symtab->symbols())
930 if (sym->isExported())
931 if (auto *f = dyn_cast<DefinedFunction>(Val: sym))
932 toWrap.push_back(x: f);
933
934 for (auto *f : toWrap) {
935 auto funcNameStr = (f->getName() + ".command_export").str();
936 commandExportWrapperNames.push_back(x: funcNameStr);
937 const std::string &funcName = commandExportWrapperNames.back();
938
939 auto func = make<SyntheticFunction>(args: *f->getSignature(), args: funcName);
940 if (f->function->getExportName())
941 func->setExportName(f->function->getExportName()->str());
942 else
943 func->setExportName(f->getName().str());
944
945 DefinedFunction *def =
946 symtab->addSyntheticFunction(name: funcName, flags: f->flags, function: func);
947 def->markLive();
948
949 def->flags |= WASM_SYMBOL_EXPORTED;
950 def->flags &= ~WASM_SYMBOL_VISIBILITY_HIDDEN;
951 def->forceExport = f->forceExport;
952
953 f->flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
954 f->flags &= ~WASM_SYMBOL_EXPORTED;
955 f->forceExport = false;
956
957 out.functionSec->addFunction(func);
958
959 createCommandExportWrapper(functionIndex: f->getFunctionIndex(), f: def);
960 }
961}
962
963static void finalizeIndirectFunctionTable() {
964 if (!ctx.sym.indirectFunctionTable)
965 return;
966
967 if (shouldImport(sym: ctx.sym.indirectFunctionTable) &&
968 !ctx.sym.indirectFunctionTable->hasTableNumber()) {
969 // Processing -Bsymbolic relocations resulted in a late requirement that the
970 // indirect function table be present, and we are running in --import-table
971 // mode. Add the table now to the imports section. Otherwise it will be
972 // added to the tables section later in assignIndexes.
973 out.importSec->addImport(sym: ctx.sym.indirectFunctionTable);
974 }
975
976 uint32_t tableSize = ctx.arg.tableBase + out.elemSec->numEntries();
977 WasmLimits limits = {.Flags: 0, .Minimum: tableSize, .Maximum: 0, .PageSize: 0};
978 if (ctx.sym.indirectFunctionTable->isDefined() && !ctx.arg.growableTable) {
979 limits.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
980 limits.Maximum = limits.Minimum;
981 }
982 if (ctx.arg.is64.value_or(u: false))
983 limits.Flags |= WASM_LIMITS_FLAG_IS_64;
984 ctx.sym.indirectFunctionTable->setLimits(limits);
985}
986
987static void scanRelocations() {
988 for (ObjFile *file : ctx.objectFiles) {
989 LLVM_DEBUG(dbgs() << "scanRelocations: " << file->getName() << "\n");
990 for (InputChunk *chunk : file->functions)
991 scanRelocations(chunk);
992 for (InputChunk *chunk : file->segments)
993 scanRelocations(chunk);
994 for (auto &p : file->customSections)
995 scanRelocations(chunk: p);
996 }
997}
998
999void Writer::assignIndexes() {
1000 // Seal the import section, since other index spaces such as function and
1001 // global are effected by the number of imports.
1002 out.importSec->seal();
1003
1004 for (InputFunction *func : ctx.syntheticFunctions)
1005 out.functionSec->addFunction(func);
1006
1007 for (ObjFile *file : ctx.objectFiles) {
1008 LLVM_DEBUG(dbgs() << "Functions: " << file->getName() << "\n");
1009 for (InputFunction *func : file->functions)
1010 out.functionSec->addFunction(func);
1011 }
1012
1013 for (InputGlobal *global : ctx.syntheticGlobals)
1014 out.globalSec->addGlobal(global);
1015
1016 for (ObjFile *file : ctx.objectFiles) {
1017 LLVM_DEBUG(dbgs() << "Globals: " << file->getName() << "\n");
1018 for (InputGlobal *global : file->globals)
1019 out.globalSec->addGlobal(global);
1020 }
1021
1022 for (ObjFile *file : ctx.objectFiles) {
1023 LLVM_DEBUG(dbgs() << "Tags: " << file->getName() << "\n");
1024 for (InputTag *tag : file->tags)
1025 out.tagSec->addTag(tag);
1026 }
1027
1028 for (ObjFile *file : ctx.objectFiles) {
1029 LLVM_DEBUG(dbgs() << "Tables: " << file->getName() << "\n");
1030 for (InputTable *table : file->tables)
1031 out.tableSec->addTable(table);
1032 }
1033
1034 for (InputTable *table : ctx.syntheticTables)
1035 out.tableSec->addTable(table);
1036
1037 out.globalSec->assignIndexes();
1038 out.tableSec->assignIndexes();
1039}
1040
1041static StringRef getOutputDataSegmentName(const InputChunk &seg) {
1042 // We always merge .tbss and .tdata into a single TLS segment so all TLS
1043 // symbols are be relative to single __tls_base.
1044 if (seg.isTLS())
1045 return ".tdata";
1046 if (!ctx.arg.mergeDataSegments)
1047 return seg.name;
1048 if (seg.name.starts_with(Prefix: ".text."))
1049 return ".text";
1050 if (seg.name.starts_with(Prefix: ".data."))
1051 return ".data";
1052 if (seg.name.starts_with(Prefix: ".bss."))
1053 return ".bss";
1054 if (seg.name.starts_with(Prefix: ".rodata."))
1055 return ".rodata";
1056 return seg.name;
1057}
1058
1059OutputSegment *Writer::createOutputSegment(StringRef name) {
1060 LLVM_DEBUG(dbgs() << "new segment: " << name << "\n");
1061 OutputSegment *s = make<OutputSegment>(args&: name);
1062 // In the shared memory case, all data segments must be passive since they
1063 // will be initialized once by the main thread and then shared with other
1064 // threads. In the cooperative threading case, TLS segments need to exist to
1065 // be able to run TLS initialization on spawned threads, so that's managed
1066 // here by flagging TLS as passive as well.
1067 bool needsPassiveInit =
1068 ctx.arg.sharedMemory || (ctx.arg.cooperativeThreading && s->isTLS());
1069 if (needsPassiveInit)
1070 s->initFlags = WASM_DATA_SEGMENT_IS_PASSIVE;
1071 if (!ctx.arg.relocatable && name.starts_with(Prefix: ".bss"))
1072 s->isBss = true;
1073 segments.push_back(x: s);
1074 return s;
1075}
1076
1077void Writer::allocateCommonSymbols() {
1078 if (ctx.arg.relocatable)
1079 return;
1080
1081 std::vector<CommonSymbol *> commons;
1082 for (Symbol *sym : symtab->symbols())
1083 if (auto *c = dyn_cast<CommonSymbol>(Val: sym))
1084 if (c->isLive())
1085 commons.push_back(x: c);
1086
1087 if (commons.empty())
1088 return;
1089
1090 log(msg: "-- allocateCommonSymbols");
1091
1092 uint64_t size = 0;
1093 uint32_t alignLog2 = 0;
1094
1095 for (CommonSymbol *c : commons) {
1096 assert(c->getAlignment() <= 32);
1097 alignLog2 = std::max(a: alignLog2, b: c->getAlignment());
1098 size = alignTo(Value: size, Align: 1ULL << c->getAlignment());
1099 if (size > UINT32_MAX || c->getSize() > UINT32_MAX - size) {
1100 error(msg: "common symbols section size overflow");
1101 return;
1102 }
1103 size += c->getSize();
1104 }
1105
1106 auto *commonSeg = make<SyntheticInputSegment>(args: ".bss.common", args&: alignLog2, args: 0);
1107 commonSeg->setSize(size);
1108 commonSeg->live = true;
1109 ctx.syntheticInputSegments.push_back(Elt: commonSeg);
1110
1111 uint64_t offset = 0;
1112 for (CommonSymbol *c : commons) {
1113 uint64_t size = c->getSize();
1114 uint32_t alignLog2 = c->getAlignment();
1115 offset = alignTo(Value: offset, Align: 1ULL << alignLog2);
1116 log(msg: formatv(Fmt: "allocateCommonSymbol: {0} size={1} align={2} offset={3}",
1117 Vals: c->getName(), Vals&: size, Vals&: alignLog2, Vals&: offset));
1118 replaceSymbol<DefinedData>(s: c, arg: c->getName(), arg&: c->flags, arg: c->getFile(),
1119 arg&: commonSeg, arg&: offset, arg&: size);
1120 offset += size;
1121 }
1122}
1123
1124void Writer::createOutputSegments() {
1125 // In relocatable mode, segments with differing flags must not be coalesced
1126 // into the same output segment; otherwise chunks would inherit flags from
1127 // other chunks sharing the same name (e.g. non-STRINGS strings inheriting
1128 // STRINGS and being corrupted by splitStrings, or non-RETAIN data inheriting
1129 // RETAIN and preventing dead-code elimination).
1130 auto getSegmentKey = [&](StringRef name, uint32_t flags) {
1131 return SegmentKey(name, ctx.arg.relocatable ? flags : 0);
1132 };
1133
1134 for (ObjFile *file : ctx.objectFiles) {
1135 for (InputChunk *segment : file->segments) {
1136 if (!segment->live)
1137 continue;
1138 StringRef name = getOutputDataSegmentName(seg: *segment);
1139 OutputSegment *s = nullptr;
1140 // When running in relocatable mode we can't merge segments that are part
1141 // of comdat groups since the ultimate linker needs to be able exclude or
1142 // include them individually.
1143 if (ctx.arg.relocatable && !segment->getComdatName().empty()) {
1144 s = createOutputSegment(name);
1145 } else {
1146 auto key = getSegmentKey(name, segment->flags);
1147 if (!segmentMap.contains(Val: key))
1148 segmentMap[key] = createOutputSegment(name);
1149 s = segmentMap[key];
1150 }
1151 s->addInputSegment(inSeg: segment);
1152 }
1153 }
1154
1155 // Process synthetic segments
1156 for (InputChunk *segment : ctx.syntheticInputSegments) {
1157 if (!segment->live)
1158 continue;
1159 StringRef name = getOutputDataSegmentName(seg: *segment);
1160 OutputSegment *s = nullptr;
1161 auto key = getSegmentKey(name, segment->flags);
1162 if (!segmentMap.contains(Val: key))
1163 segmentMap[key] = createOutputSegment(name);
1164 s = segmentMap[key];
1165 s->addInputSegment(inSeg: segment);
1166 }
1167
1168 // Sort segments by type, placing .bss last. Note that one requirement of
1169 // this sort is that all eventually-active segments must come first in
1170 // case `combineActiveOutputSegments` is used. When combined the relative
1171 // address of the data segment must be 0 (to be compatible with PIC and a
1172 // lack of extended-const).
1173 llvm::stable_sort(Range&: segments,
1174 C: [](const OutputSegment *a, const OutputSegment *b) {
1175 auto order = [](StringRef name) {
1176 return StringSwitch<int>(name)
1177 .StartsWith(S: ".rodata", Value: 0)
1178 .StartsWith(S: ".data", Value: 1)
1179 .StartsWith(S: ".tdata", Value: 3)
1180 .StartsWith(S: ".bss", Value: 4)
1181 .Default(Value: 2);
1182 };
1183 return order(a->name) < order(b->name);
1184 });
1185
1186 for (size_t i = 0; i < segments.size(); ++i)
1187 segments[i]->index = i;
1188
1189 // Merge MergeInputSections into a single MergeSyntheticSection.
1190 LLVM_DEBUG(dbgs() << "-- finalize input semgments\n");
1191 for (OutputSegment *seg : segments)
1192 seg->finalizeInputSegments();
1193}
1194
1195void Writer::combineActiveOutputSegments() {
1196 // With PIC code we currently only support a single active data segment since
1197 // we only have a single __memory_base to use as our base address. This pass
1198 // combines all active data segments into a single .data segment.
1199 // This restriction does not apply when the extended const extension is
1200 // available: https://github.com/WebAssembly/extended-const
1201 assert(!ctx.arg.extendedConst);
1202 assert(ctx.isPic);
1203 auto isActive = [](const OutputSegment *s) {
1204 return s->requiredInBinary() && s->isActive();
1205 };
1206 if (llvm::count_if(Range&: segments, P: isActive) <= 1)
1207 return;
1208 OutputSegment *combined = make<OutputSegment>(args: ".data");
1209 std::vector<OutputSegment *> newSegments = {combined};
1210 for (OutputSegment *s : segments) {
1211 if (!isActive(s)) {
1212 newSegments.push_back(x: s);
1213 continue;
1214 }
1215 if (combined->inputSegments.empty())
1216 combined->startVA = s->startVA;
1217 bool first = true;
1218 for (InputChunk *inSeg : s->inputSegments) {
1219 if (first)
1220 inSeg->alignment = std::max(a: inSeg->alignment, b: s->alignment);
1221 first = false;
1222#ifndef NDEBUG
1223 uint64_t oldVA = inSeg->getVA();
1224#endif
1225 combined->addInputSegment(inSeg);
1226#ifndef NDEBUG
1227 uint64_t newVA = inSeg->getVA();
1228 LLVM_DEBUG(dbgs() << "added input segment. name=" << inSeg->name
1229 << " oldVA=" << oldVA << " newVA=" << newVA << "\n");
1230 assert(oldVA == newVA);
1231#endif
1232 }
1233 }
1234
1235 segments = std::move(newSegments);
1236
1237 // Fixup indices for any segments that have moved around.
1238 for (size_t i = 0; i < segments.size(); ++i)
1239 segments[i]->index = i;
1240}
1241
1242static void createFunction(DefinedFunction *func, StringRef bodyContent) {
1243 std::string functionBody;
1244 {
1245 raw_string_ostream os(functionBody);
1246 writeUleb128(os, number: bodyContent.size(), msg: "function size");
1247 os << bodyContent;
1248 }
1249 ArrayRef<uint8_t> body = arrayRefFromStringRef(Input: saver().save(S: functionBody));
1250 cast<SyntheticFunction>(Val: func->function)->setBody(body);
1251}
1252
1253bool Writer::needsPassiveInitialization(const OutputSegment *segment) {
1254 // If bulk memory features is supported then we can perform bss initialization
1255 // (via memory.fill) during `__wasm_init_memory`.
1256 if (ctx.arg.memoryImport.has_value() && !segment->requiredInBinary())
1257 return true;
1258 return segment->isPassive();
1259}
1260
1261bool Writer::hasPassiveInitializedSegments() {
1262 return llvm::any_of(Range&: segments, P: [this](const OutputSegment *s) {
1263 return this->needsPassiveInitialization(segment: s);
1264 });
1265}
1266
1267void Writer::createSyntheticInitFunctions() {
1268 if (ctx.arg.relocatable)
1269 return;
1270
1271 static WasmSignature nullSignature = {{}, {}};
1272
1273 createApplyDataRelocationsFunction();
1274
1275 // Passive segments are used to avoid memory being reinitialized on each
1276 // thread's instantiation. These passive segments are initialized and
1277 // dropped in __wasm_init_memory, which is registered as the start function
1278 // We also initialize bss segments (using memory.fill) as part of this
1279 // function.
1280 if (hasPassiveInitializedSegments()) {
1281 ctx.sym.initMemory = symtab->addSyntheticFunction(
1282 name: "__wasm_init_memory", flags: WASM_SYMBOL_VISIBILITY_HIDDEN,
1283 function: make<SyntheticFunction>(args&: nullSignature, args: "__wasm_init_memory"));
1284 ctx.sym.initMemory->markLive();
1285 // __wasm_init_memory uses __tls_base/__wasm_set_tls_base
1286 if (ctx.sym.setTLSBase)
1287 ctx.sym.setTLSBase->markLive();
1288 else if (ctx.arg.sharedMemory)
1289 ctx.sym.tlsBase->markLive();
1290 }
1291
1292 if (ctx.arg.isMultithreaded()) {
1293 if (out.globalSec->needsTLSRelocations()) {
1294 ctx.sym.applyGlobalTLSRelocs = symtab->addSyntheticFunction(
1295 name: "__wasm_apply_global_tls_relocs", flags: WASM_SYMBOL_VISIBILITY_HIDDEN,
1296 function: make<SyntheticFunction>(args&: nullSignature,
1297 args: "__wasm_apply_global_tls_relocs"));
1298 ctx.sym.applyGlobalTLSRelocs->markLive();
1299 // TLS relocations depend on the __tls_base/__wasm_get_tls_base symbols
1300 if (ctx.sym.getTLSBase)
1301 ctx.sym.getTLSBase->markLive();
1302 else if (ctx.arg.sharedMemory)
1303 ctx.sym.tlsBase->markLive();
1304 }
1305
1306 auto hasTLSRelocs = [](const OutputSegment *segment) {
1307 if (segment->isTLS())
1308 for (const auto *is : segment->inputSegments)
1309 if (is->getRelocations().size())
1310 return true;
1311 return false;
1312 };
1313 if (llvm::any_of(Range&: segments, P: hasTLSRelocs)) {
1314 ctx.sym.applyTLSRelocs = symtab->addSyntheticFunction(
1315 name: "__wasm_apply_tls_relocs", flags: WASM_SYMBOL_VISIBILITY_HIDDEN,
1316 function: make<SyntheticFunction>(args&: nullSignature, args: "__wasm_apply_tls_relocs"));
1317 ctx.sym.applyTLSRelocs->markLive();
1318 }
1319 }
1320
1321 if (ctx.isPic && out.globalSec->needsRelocations()) {
1322 ctx.sym.applyGlobalRelocs = symtab->addSyntheticFunction(
1323 name: "__wasm_apply_global_relocs", flags: WASM_SYMBOL_VISIBILITY_HIDDEN,
1324 function: make<SyntheticFunction>(args&: nullSignature, args: "__wasm_apply_global_relocs"));
1325 ctx.sym.applyGlobalRelocs->markLive();
1326 }
1327
1328 // If there is only one start function we can just use that function
1329 // itself as the Wasm start function, otherwise we need to synthesize
1330 // a new function to call them in sequence.
1331 if (ctx.sym.applyGlobalRelocs && ctx.sym.initMemory) {
1332 ctx.sym.startFunction = symtab->addSyntheticFunction(
1333 name: "__wasm_start", flags: WASM_SYMBOL_VISIBILITY_HIDDEN,
1334 function: make<SyntheticFunction>(args&: nullSignature, args: "__wasm_start"));
1335 ctx.sym.startFunction->markLive();
1336 }
1337}
1338
1339void Writer::createInitMemoryFunction() {
1340 LLVM_DEBUG(dbgs() << "createInitMemoryFunction\n");
1341 assert(ctx.sym.initMemory);
1342 assert(hasPassiveInitializedSegments());
1343 uint64_t flagAddress;
1344 if (ctx.arg.sharedMemory) {
1345 assert(ctx.sym.initMemoryFlag);
1346 flagAddress = ctx.sym.initMemoryFlag->getVA();
1347 }
1348 bool is64 = ctx.arg.is64.value_or(u: false);
1349 std::string bodyContent;
1350 {
1351 raw_string_ostream os(bodyContent);
1352 // Initialize memory in a thread-safe manner. The thread that successfully
1353 // increments the flag from 0 to 1 is responsible for performing the memory
1354 // initialization. Other threads go sleep on the flag until the first thread
1355 // finishing initializing memory, increments the flag to 2, and wakes all
1356 // the other threads. Once the flag has been set to 2, subsequently started
1357 // threads will skip the sleep. All threads unconditionally drop their
1358 // passive data segments once memory has been initialized. The generated
1359 // code is as follows:
1360 //
1361 // (func $__wasm_init_memory
1362 // (block $drop
1363 // (block $wait
1364 // (block $init
1365 // (br_table $init $wait $drop
1366 // (i32.atomic.rmw.cmpxchg align=2 offset=0
1367 // (i32.const $__init_memory_flag)
1368 // (i32.const 0)
1369 // (i32.const 1)
1370 // )
1371 // )
1372 // ) ;; $init
1373 // ( ... initialize data segments ... )
1374 // (i32.atomic.store align=2 offset=0
1375 // (i32.const $__init_memory_flag)
1376 // (i32.const 2)
1377 // )
1378 // (drop
1379 // (i32.atomic.notify align=2 offset=0
1380 // (i32.const $__init_memory_flag)
1381 // (i32.const -1u)
1382 // )
1383 // )
1384 // (br $drop)
1385 // ) ;; $wait
1386 // (drop
1387 // (i32.atomic.wait align=2 offset=0
1388 // (i32.const $__init_memory_flag)
1389 // (i32.const 1)
1390 // (i32.const -1)
1391 // )
1392 // )
1393 // ) ;; $drop
1394 // ( ... drop data segments ... )
1395 // )
1396 //
1397 // When we are building with PIC, calculate the flag location using:
1398 //
1399 // (global.get $__memory_base)
1400 // (i32.const $__init_memory_flag)
1401 // (i32.const 1)
1402
1403 // First figure out what locals need to be emitted for this function. Locals
1404 // aren't always needed, though. Map them out here where they're allocated
1405 // based on the same conditions that they're used in various situations
1406 // below. For now all locals have the same type which makes the declaration
1407 // side a bit simpler, and this'll have to get fancier if multiple types of
1408 // locals are ever needed in the future.
1409 unsigned numAddressLocals = 0;
1410 unsigned tlsAddressLocal = -1;
1411 unsigned flagAddressLocal = -1;
1412 if (ctx.isPic && ctx.arg.sharedMemory)
1413 flagAddressLocal = numAddressLocals++;
1414 bool needsTLSAddressLocal =
1415 ctx.isPic && ctx.arg.isMultithreaded() &&
1416 llvm::any_of(Range&: segments, P: [this](const OutputSegment *s) {
1417 return s->isTLS() && needsPassiveInitialization(segment: s);
1418 });
1419 if (needsTLSAddressLocal)
1420 tlsAddressLocal = numAddressLocals++;
1421 writeUleb128(os, number: numAddressLocals ? 1 : 0, msg: "num local groups");
1422 if (numAddressLocals > 0) {
1423 writeUleb128(os, number: numAddressLocals, msg: "num address locals");
1424 writeU8(os, byte: is64 ? WASM_TYPE_I64 : WASM_TYPE_I32, msg: "address type");
1425 }
1426
1427 auto writeGetFlagAddress = [&]() {
1428 if (ctx.isPic) {
1429 writeU8(os, byte: WASM_OPCODE_LOCAL_GET, msg: "local.get");
1430 writeUleb128(os, number: flagAddressLocal, msg: "flag address local index");
1431 } else {
1432 writePtrConst(os, number: flagAddress, is64, msg: "flag address");
1433 }
1434 };
1435
1436 if (ctx.arg.sharedMemory) {
1437 // With PIC code we cache the flag address in a local.
1438 if (ctx.isPic) {
1439 writeU8(os, byte: WASM_OPCODE_GLOBAL_GET, msg: "GLOBAL_GET");
1440 writeUleb128(os, number: ctx.sym.memoryBase->getGlobalIndex(), msg: "memory_base");
1441 writePtrConst(os, number: flagAddress, is64, msg: "flag address");
1442 writeU8(os, byte: is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD, msg: "add");
1443 writeU8(os, byte: WASM_OPCODE_LOCAL_SET, msg: "local.set");
1444 writeUleb128(os, number: flagAddressLocal, msg: "flag address local index");
1445 }
1446
1447 // Set up destination blocks
1448 writeU8(os, byte: WASM_OPCODE_BLOCK, msg: "block $drop");
1449 writeU8(os, byte: WASM_TYPE_NORESULT, msg: "block type");
1450 writeU8(os, byte: WASM_OPCODE_BLOCK, msg: "block $wait");
1451 writeU8(os, byte: WASM_TYPE_NORESULT, msg: "block type");
1452 writeU8(os, byte: WASM_OPCODE_BLOCK, msg: "block $init");
1453 writeU8(os, byte: WASM_TYPE_NORESULT, msg: "block type");
1454
1455 // Atomically check whether we win the race.
1456 writeGetFlagAddress();
1457 writeI32Const(os, number: 0, msg: "expected flag value");
1458 writeI32Const(os, number: 1, msg: "new flag value");
1459 writeU8(os, byte: WASM_OPCODE_ATOMICS_PREFIX, msg: "atomics prefix");
1460 writeUleb128(os, number: WASM_OPCODE_I32_RMW_CMPXCHG, msg: "i32.atomic.rmw.cmpxchg");
1461 writeMemArg(os, alignment: 2, offset: 0);
1462
1463 // Based on the value, decide what to do next.
1464 writeU8(os, byte: WASM_OPCODE_BR_TABLE, msg: "br_table");
1465 writeUleb128(os, number: 2, msg: "label vector length");
1466 writeUleb128(os, number: 0, msg: "label $init");
1467 writeUleb128(os, number: 1, msg: "label $wait");
1468 writeUleb128(os, number: 2, msg: "default label $drop");
1469
1470 // Initialize passive data segments
1471 writeU8(os, byte: WASM_OPCODE_END, msg: "end $init");
1472 }
1473
1474 for (const OutputSegment *s : segments) {
1475 if (needsPassiveInitialization(segment: s)) {
1476 // For passive BSS segments we can simple issue a memory.fill(0).
1477 // For non-BSS segments we do a memory.init. Both these
1478 // instructions take as their first argument the destination
1479 // address.
1480 writePtrConst(os, number: s->startVA, is64, msg: "destination address");
1481 if (ctx.isPic) {
1482 writeU8(os, byte: WASM_OPCODE_GLOBAL_GET, msg: "GLOBAL_GET");
1483 writeUleb128(os, number: ctx.sym.memoryBase->getGlobalIndex(),
1484 msg: "__memory_base");
1485 writeU8(os, byte: is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD,
1486 msg: "i32.add");
1487 }
1488
1489 // When we initialize the TLS segment we also set the TLS base.
1490 // This allows the runtime to use this static copy of the TLS data
1491 // for the first/main thread.
1492 //
1493 // Note that for `--cooperative-threading` this additionally configures
1494 // the `__init_tls_base` global which is the initial TLS value that can
1495 // be used for all new component model tasks. For non-PIC builds this
1496 // global's statically known value is now calculated, so it's updated
1497 // here. For PIC builds the result of the address computation above is
1498 // what's stored into the global.
1499 if (ctx.arg.isMultithreaded() && s->isTLS()) {
1500 if (ctx.isPic) {
1501 // Cache the result of the addition in the TLS address local
1502 writeU8(os, byte: WASM_OPCODE_LOCAL_TEE, msg: "local.tee");
1503 writeUleb128(os, number: tlsAddressLocal, msg: "tls address local");
1504 if (ctx.arg.libcallThreadContext) {
1505 writeU8(os, byte: WASM_OPCODE_LOCAL_GET, msg: "local.get");
1506 writeUleb128(os, number: tlsAddressLocal, msg: "tls address local");
1507 writeU8(os, byte: WASM_OPCODE_GLOBAL_SET, msg: "global.set");
1508 writeUleb128(os, number: ctx.sym.tlsBase->getGlobalIndex(),
1509 msg: "__init_tls_base");
1510 }
1511 } else {
1512 writePtrConst(os, number: s->startVA, is64, msg: "destination address");
1513 if (ctx.arg.libcallThreadContext)
1514 ctx.sym.tlsBase->global->setPointerValue(s->startVA);
1515 }
1516 writeSetTLSBase(ctx, os);
1517 if (ctx.isPic) {
1518 writeU8(os, byte: WASM_OPCODE_LOCAL_GET, msg: "local.get");
1519 writeUleb128(os, number: tlsAddressLocal, msg: "tls address local");
1520 }
1521 }
1522
1523 if (s->isBss) {
1524 writeI32Const(os, number: 0, msg: "fill value");
1525 writePtrConst(os, number: s->size, is64, msg: "memory region size");
1526 writeU8(os, byte: WASM_OPCODE_MISC_PREFIX, msg: "bulk-memory prefix");
1527 writeUleb128(os, number: WASM_OPCODE_MEMORY_FILL, msg: "memory.fill");
1528 writeU8(os, byte: 0, msg: "memory index immediate");
1529 } else {
1530 writeI32Const(os, number: 0, msg: "source segment offset");
1531 writeI32Const(os, number: s->size, msg: "memory region size");
1532 writeU8(os, byte: WASM_OPCODE_MISC_PREFIX, msg: "bulk-memory prefix");
1533 writeUleb128(os, number: WASM_OPCODE_MEMORY_INIT, msg: "memory.init");
1534 writeUleb128(os, number: s->index, msg: "segment index immediate");
1535 writeU8(os, byte: 0, msg: "memory index immediate");
1536 }
1537 }
1538 }
1539
1540 if (ctx.arg.sharedMemory) {
1541 // Set flag to 2 to mark end of initialization
1542 writeGetFlagAddress();
1543 writeI32Const(os, number: 2, msg: "flag value");
1544 writeU8(os, byte: WASM_OPCODE_ATOMICS_PREFIX, msg: "atomics prefix");
1545 writeUleb128(os, number: WASM_OPCODE_I32_ATOMIC_STORE, msg: "i32.atomic.store");
1546 writeMemArg(os, alignment: 2, offset: 0);
1547
1548 // Notify any waiters that memory initialization is complete
1549 writeGetFlagAddress();
1550 writeI32Const(os, number: -1, msg: "number of waiters");
1551 writeU8(os, byte: WASM_OPCODE_ATOMICS_PREFIX, msg: "atomics prefix");
1552 writeUleb128(os, number: WASM_OPCODE_ATOMIC_NOTIFY, msg: "atomic.notify");
1553 writeMemArg(os, alignment: 2, offset: 0);
1554 writeU8(os, byte: WASM_OPCODE_DROP, msg: "drop");
1555
1556 // Branch to drop the segments
1557 writeU8(os, byte: WASM_OPCODE_BR, msg: "br");
1558 writeUleb128(os, number: 1, msg: "label $drop");
1559
1560 // Wait for the winning thread to initialize memory
1561 writeU8(os, byte: WASM_OPCODE_END, msg: "end $wait");
1562 writeGetFlagAddress();
1563 writeI32Const(os, number: 1, msg: "expected flag value");
1564 writeI64Const(os, number: -1, msg: "timeout");
1565
1566 writeU8(os, byte: WASM_OPCODE_ATOMICS_PREFIX, msg: "atomics prefix");
1567 writeUleb128(os, number: WASM_OPCODE_I32_ATOMIC_WAIT, msg: "i32.atomic.wait");
1568 writeMemArg(os, alignment: 2, offset: 0);
1569 writeU8(os, byte: WASM_OPCODE_DROP, msg: "drop");
1570
1571 // Unconditionally drop passive data segments
1572 writeU8(os, byte: WASM_OPCODE_END, msg: "end $drop");
1573 }
1574
1575 for (const OutputSegment *s : segments) {
1576 if (needsPassiveInitialization(segment: s) && !s->isBss) {
1577 // The TLS region should not be dropped since its is needed
1578 // during the initialization of each thread (__wasm_init_tls).
1579 if (ctx.arg.isMultithreaded() && s->isTLS())
1580 continue;
1581 // data.drop instruction
1582 writeU8(os, byte: WASM_OPCODE_MISC_PREFIX, msg: "bulk-memory prefix");
1583 writeUleb128(os, number: WASM_OPCODE_DATA_DROP, msg: "data.drop");
1584 writeUleb128(os, number: s->index, msg: "segment index immediate");
1585 }
1586 }
1587
1588 // End the function
1589 writeU8(os, byte: WASM_OPCODE_END, msg: "END");
1590 }
1591
1592 createFunction(func: ctx.sym.initMemory, bodyContent);
1593}
1594
1595void Writer::createStartFunction() {
1596 // If the start function exists when we have more than one function to call.
1597 if (ctx.sym.initMemory && ctx.sym.applyGlobalRelocs) {
1598 assert(ctx.sym.startFunction);
1599 std::string bodyContent;
1600 {
1601 raw_string_ostream os(bodyContent);
1602 writeUleb128(os, number: 0, msg: "num locals");
1603 writeU8(os, byte: WASM_OPCODE_CALL, msg: "CALL");
1604 writeUleb128(os, number: ctx.sym.applyGlobalRelocs->getFunctionIndex(),
1605 msg: "function index");
1606 writeU8(os, byte: WASM_OPCODE_CALL, msg: "CALL");
1607 writeUleb128(os, number: ctx.sym.initMemory->getFunctionIndex(),
1608 msg: "function index");
1609 writeU8(os, byte: WASM_OPCODE_END, msg: "END");
1610 }
1611 createFunction(func: ctx.sym.startFunction, bodyContent);
1612 } else if (ctx.sym.initMemory) {
1613 ctx.sym.startFunction = ctx.sym.initMemory;
1614 } else if (ctx.sym.applyGlobalRelocs) {
1615 ctx.sym.startFunction = ctx.sym.applyGlobalRelocs;
1616 }
1617}
1618
1619// For -shared (PIC) output, we create create a synthetic function which will
1620// apply any relocations to the data segments on startup. This function is
1621// called `__wasm_apply_data_relocs` and is expected to be called before
1622// any user code (i.e. before `__wasm_call_ctors`).
1623void Writer::createApplyDataRelocationsFunction() {
1624 LLVM_DEBUG(dbgs() << "createApplyDataRelocationsFunction\n");
1625 // First write the body's contents to a string.
1626 std::string bodyContent;
1627 {
1628 raw_string_ostream os(bodyContent);
1629 writeUleb128(os, number: 0, msg: "num locals");
1630 bool generated = false;
1631 for (const OutputSegment *seg : segments)
1632 if (!ctx.arg.isMultithreaded() || !seg->isTLS())
1633 for (const InputChunk *inSeg : seg->inputSegments)
1634 generated |= inSeg->generateRelocationCode(os);
1635
1636 if (!generated) {
1637 LLVM_DEBUG(dbgs() << "skipping empty __wasm_apply_data_relocs\n");
1638 return;
1639 }
1640 writeU8(os, byte: WASM_OPCODE_END, msg: "END");
1641 }
1642
1643 // __wasm_apply_data_relocs
1644 // Function that applies relocations to data segment post-instantiation.
1645 static WasmSignature nullSignature = {{}, {}};
1646 auto def = symtab->addSyntheticFunction(
1647 name: "__wasm_apply_data_relocs",
1648 flags: WASM_SYMBOL_VISIBILITY_DEFAULT | WASM_SYMBOL_EXPORTED,
1649 function: make<SyntheticFunction>(args&: nullSignature, args: "__wasm_apply_data_relocs"));
1650 def->markLive();
1651
1652 createFunction(func: def, bodyContent);
1653}
1654
1655void Writer::createApplyTLSRelocationsFunction() {
1656 LLVM_DEBUG(dbgs() << "createApplyTLSRelocationsFunction\n");
1657 std::string bodyContent;
1658 {
1659 raw_string_ostream os(bodyContent);
1660 writeUleb128(os, number: 0, msg: "num locals");
1661 for (const OutputSegment *seg : segments)
1662 if (seg->isTLS())
1663 for (const InputChunk *inSeg : seg->inputSegments)
1664 inSeg->generateRelocationCode(os);
1665
1666 writeU8(os, byte: WASM_OPCODE_END, msg: "END");
1667 }
1668
1669 createFunction(func: ctx.sym.applyTLSRelocs, bodyContent);
1670}
1671
1672// Similar to createApplyDataRelocationsFunction but generates relocation code
1673// for WebAssembly globals. Because these globals are not shared between threads
1674// these relocation need to run on every thread.
1675void Writer::createApplyGlobalRelocationsFunction() {
1676 // First write the body's contents to a string.
1677 std::string bodyContent;
1678 {
1679 raw_string_ostream os(bodyContent);
1680 writeUleb128(os, number: 0, msg: "num locals");
1681 out.globalSec->generateRelocationCode(os, TLS: false);
1682 writeU8(os, byte: WASM_OPCODE_END, msg: "END");
1683 }
1684
1685 createFunction(func: ctx.sym.applyGlobalRelocs, bodyContent);
1686}
1687
1688// Similar to createApplyGlobalRelocationsFunction but for
1689// TLS symbols. This cannot be run during the start function
1690// but must be delayed until __wasm_init_tls is called.
1691void Writer::createApplyGlobalTLSRelocationsFunction() {
1692 // First write the body's contents to a string.
1693 std::string bodyContent;
1694 {
1695 raw_string_ostream os(bodyContent);
1696 writeUleb128(os, number: 0, msg: "num locals");
1697 out.globalSec->generateRelocationCode(os, TLS: true);
1698 writeU8(os, byte: WASM_OPCODE_END, msg: "END");
1699 }
1700
1701 createFunction(func: ctx.sym.applyGlobalTLSRelocs, bodyContent);
1702}
1703
1704// Create synthetic "__wasm_call_ctors" function based on ctor functions
1705// in input object.
1706void Writer::createCallCtorsFunction() {
1707 // If __wasm_call_ctors isn't referenced, there aren't any ctors, don't
1708 // define the `__wasm_call_ctors` function.
1709 if (!ctx.sym.callCtors->isLive() && initFunctions.empty())
1710 return;
1711
1712 // First write the body's contents to a string.
1713 std::string bodyContent;
1714 {
1715 raw_string_ostream os(bodyContent);
1716 writeUleb128(os, number: 0, msg: "num locals");
1717
1718 // Call constructors
1719 for (const WasmInitEntry &f : initFunctions) {
1720 writeU8(os, byte: WASM_OPCODE_CALL, msg: "CALL");
1721 writeUleb128(os, number: f.sym->getFunctionIndex(), msg: "function index");
1722 for (size_t i = 0; i < f.sym->signature->Returns.size(); i++) {
1723 writeU8(os, byte: WASM_OPCODE_DROP, msg: "DROP");
1724 }
1725 }
1726
1727 writeU8(os, byte: WASM_OPCODE_END, msg: "END");
1728 }
1729
1730 createFunction(func: ctx.sym.callCtors, bodyContent);
1731}
1732
1733// Create a wrapper around a function export which calls the
1734// static constructors and destructors.
1735void Writer::createCommandExportWrapper(uint32_t functionIndex,
1736 DefinedFunction *f) {
1737 // First write the body's contents to a string.
1738 std::string bodyContent;
1739 {
1740 raw_string_ostream os(bodyContent);
1741 writeUleb128(os, number: 0, msg: "num locals");
1742
1743 // Call `__wasm_call_ctors` which call static constructors (and
1744 // applies any runtime relocations in Emscripten-style PIC mode)
1745 if (ctx.sym.callCtors->isLive()) {
1746 writeU8(os, byte: WASM_OPCODE_CALL, msg: "CALL");
1747 writeUleb128(os, number: ctx.sym.callCtors->getFunctionIndex(), msg: "function index");
1748 }
1749
1750 // Call the user's code, leaving any return values on the operand stack.
1751 for (size_t i = 0; i < f->signature->Params.size(); ++i) {
1752 writeU8(os, byte: WASM_OPCODE_LOCAL_GET, msg: "local.get");
1753 writeUleb128(os, number: i, msg: "local index");
1754 }
1755 writeU8(os, byte: WASM_OPCODE_CALL, msg: "CALL");
1756 writeUleb128(os, number: functionIndex, msg: "function index");
1757
1758 // Call the function that calls the destructors.
1759 if (DefinedFunction *callDtors = ctx.sym.callDtors) {
1760 writeU8(os, byte: WASM_OPCODE_CALL, msg: "CALL");
1761 writeUleb128(os, number: callDtors->getFunctionIndex(), msg: "function index");
1762 }
1763
1764 // End the function, returning the return values from the user's code.
1765 writeU8(os, byte: WASM_OPCODE_END, msg: "END");
1766 }
1767
1768 createFunction(func: f, bodyContent);
1769}
1770
1771void Writer::createInitTLSFunction() {
1772 std::string bodyContent;
1773 {
1774 raw_string_ostream os(bodyContent);
1775
1776 OutputSegment *tlsSeg = nullptr;
1777 for (auto *seg : segments) {
1778 if (seg->name == ".tdata") {
1779 tlsSeg = seg;
1780 break;
1781 }
1782 }
1783
1784 writeUleb128(os, number: 0, msg: "num locals");
1785 if (tlsSeg) {
1786 writeU8(os, byte: WASM_OPCODE_LOCAL_GET, msg: "local.get");
1787 writeUleb128(os, number: 0, msg: "local index");
1788 writeSetTLSBase(ctx, os);
1789
1790 // FIXME(wvo): this local needs to be I64 in wasm64, or we need an extend
1791 // op.
1792 writeU8(os, byte: WASM_OPCODE_LOCAL_GET, msg: "local.get");
1793 writeUleb128(os, number: 0, msg: "local index");
1794
1795 writeI32Const(os, number: 0, msg: "segment offset");
1796
1797 writeI32Const(os, number: tlsSeg->size, msg: "memory region size");
1798
1799 writeU8(os, byte: WASM_OPCODE_MISC_PREFIX, msg: "bulk-memory prefix");
1800 writeUleb128(os, number: WASM_OPCODE_MEMORY_INIT, msg: "MEMORY.INIT");
1801 writeUleb128(os, number: tlsSeg->index, msg: "segment index immediate");
1802 writeU8(os, byte: 0, msg: "memory index immediate");
1803 }
1804
1805 if (ctx.sym.applyTLSRelocs) {
1806 writeU8(os, byte: WASM_OPCODE_CALL, msg: "CALL");
1807 writeUleb128(os, number: ctx.sym.applyTLSRelocs->getFunctionIndex(),
1808 msg: "function index");
1809 }
1810
1811 if (ctx.sym.applyGlobalTLSRelocs) {
1812 writeU8(os, byte: WASM_OPCODE_CALL, msg: "CALL");
1813 writeUleb128(os, number: ctx.sym.applyGlobalTLSRelocs->getFunctionIndex(),
1814 msg: "function index");
1815 }
1816 writeU8(os, byte: WASM_OPCODE_END, msg: "end function");
1817 }
1818
1819 createFunction(func: ctx.sym.initTLS, bodyContent);
1820}
1821
1822// Populate InitFunctions vector with init functions from all input objects.
1823// This is then used either when creating the output linking section or to
1824// synthesize the "__wasm_call_ctors" function.
1825void Writer::calculateInitFunctions() {
1826 if (!ctx.arg.relocatable && !ctx.sym.callCtors->isLive())
1827 return;
1828
1829 for (ObjFile *file : ctx.objectFiles) {
1830 const WasmLinkingData &l = file->getWasmObj()->linkingData();
1831 for (const WasmInitFunc &f : l.InitFunctions) {
1832 FunctionSymbol *sym = file->getFunctionSymbol(index: f.Symbol);
1833 // comdat exclusions can cause init functions be discarded.
1834 if (sym->isDiscarded() || !sym->isLive())
1835 continue;
1836 if (sym->signature->Params.size() != 0)
1837 error(msg: "constructor functions cannot take arguments: " + toString(sym: *sym));
1838 LLVM_DEBUG(dbgs() << "initFunctions: " << toString(*sym) << "\n");
1839 initFunctions.emplace_back(args: WasmInitEntry{.sym: sym, .priority: f.Priority});
1840 }
1841 }
1842
1843 // Sort in order of priority (lowest first) so that they are called
1844 // in the correct order.
1845 llvm::stable_sort(Range&: initFunctions,
1846 C: [](const WasmInitEntry &l, const WasmInitEntry &r) {
1847 return l.priority < r.priority;
1848 });
1849}
1850
1851void Writer::createSyntheticSections() {
1852 out.dylinkSec = make<DylinkSection>();
1853 out.typeSec = make<TypeSection>();
1854 out.importSec = make<ImportSection>();
1855 out.functionSec = make<FunctionSection>();
1856 out.tableSec = make<TableSection>();
1857 out.memorySec = make<MemorySection>();
1858 out.tagSec = make<TagSection>();
1859 out.globalSec = make<GlobalSection>();
1860 out.exportSec = make<ExportSection>();
1861 out.startSec = make<StartSection>();
1862 out.elemSec = make<ElemSection>();
1863 out.producersSec = make<ProducersSection>();
1864 out.targetFeaturesSec = make<TargetFeaturesSection>();
1865 out.buildIdSec = make<BuildIdSection>();
1866}
1867
1868void Writer::createSyntheticSectionsPostLayout() {
1869 out.dataCountSec = make<DataCountSection>(args&: segments);
1870 out.linkingSec = make<LinkingSection>(args&: initFunctions, args&: segments);
1871 out.nameSec = make<NameSection>(args&: segments);
1872}
1873
1874void Writer::run() {
1875 // For PIC code the table base is assigned dynamically by the loader.
1876 // For non-PIC, we start at 1 so that accessing table index 0 always traps.
1877 if (!ctx.isPic && ctx.sym.tableBase)
1878 setGlobalPtr(g: cast<DefinedGlobal>(Val: ctx.sym.tableBase), memoryPtr: ctx.arg.tableBase);
1879
1880 log(msg: "-- allocateCommonSymbols");
1881 allocateCommonSymbols();
1882 log(msg: "-- createOutputSegments");
1883 createOutputSegments();
1884 log(msg: "-- createSyntheticSections");
1885 createSyntheticSections();
1886 log(msg: "-- layoutMemory");
1887 layoutMemory();
1888
1889 if (!ctx.arg.relocatable) {
1890 // Create linker synthesized __start_SECNAME/__stop_SECNAME symbols
1891 // This has to be done after memory layout is performed.
1892 for (const OutputSegment *seg : segments) {
1893 addStartStopSymbols(seg);
1894 }
1895 }
1896
1897 for (auto &pair : ctx.arg.exportedSymbols) {
1898 Symbol *sym = symtab->find(name: pair.first());
1899 if (sym && sym->isDefined())
1900 sym->forceExport = true;
1901 }
1902
1903 // Delay reporting errors about explicit exports until after
1904 // addStartStopSymbols which can create optional symbols.
1905 for (auto &name : ctx.arg.requiredExports) {
1906 Symbol *sym = symtab->find(name);
1907 if (!sym || !sym->isDefined()) {
1908 if (ctx.arg.unresolvedSymbols == UnresolvedPolicy::ReportError)
1909 error(msg: Twine("symbol exported via --export not found: ") + name);
1910 if (ctx.arg.unresolvedSymbols == UnresolvedPolicy::Warn)
1911 warn(msg: Twine("symbol exported via --export not found: ") + name);
1912 }
1913 }
1914
1915 log(msg: "-- populateTargetFeatures");
1916 populateTargetFeatures();
1917
1918 // When outputting PIC code each segment lives at at fixes offset from the
1919 // `__memory_base` import. Unless we support the extended const expression we
1920 // can't do addition inside the constant expression, so we much combine the
1921 // segments into a single one that can live at `__memory_base`.
1922 if (ctx.isPic && !ctx.arg.extendedConst) {
1923 log(msg: "-- combineActiveOutputSegments");
1924 combineActiveOutputSegments();
1925 }
1926
1927 log(msg: "-- createSyntheticSectionsPostLayout");
1928 createSyntheticSectionsPostLayout();
1929 log(msg: "-- populateProducers");
1930 populateProducers();
1931 log(msg: "-- calculateImports");
1932 calculateImports();
1933 log(msg: "-- scanRelocations");
1934 scanRelocations();
1935 log(msg: "-- finalizeIndirectFunctionTable");
1936 finalizeIndirectFunctionTable();
1937 log(msg: "-- createSyntheticInitFunctions");
1938 createSyntheticInitFunctions();
1939 log(msg: "-- assignIndexes");
1940 assignIndexes();
1941 log(msg: "-- calculateInitFunctions");
1942 calculateInitFunctions();
1943
1944 if (!ctx.arg.relocatable) {
1945 // Create linker synthesized functions
1946 if (ctx.sym.applyGlobalRelocs) {
1947 createApplyGlobalRelocationsFunction();
1948 }
1949 if (ctx.sym.applyTLSRelocs) {
1950 createApplyTLSRelocationsFunction();
1951 }
1952 if (ctx.sym.applyGlobalTLSRelocs) {
1953 createApplyGlobalTLSRelocationsFunction();
1954 }
1955 if (ctx.sym.initMemory) {
1956 createInitMemoryFunction();
1957 }
1958 createStartFunction();
1959
1960 createCallCtorsFunction();
1961
1962 // Create export wrappers for commands if needed.
1963 //
1964 // If the input contains a call to `__wasm_call_ctors`, either in one of
1965 // the input objects or an explicit export from the command-line, we
1966 // assume ctors and dtors are taken care of already.
1967 if (!ctx.arg.relocatable && !ctx.isPic &&
1968 !ctx.sym.callCtors->isUsedInRegularObj &&
1969 !ctx.sym.callCtors->isExported()) {
1970 log(msg: "-- createCommandExportWrappers");
1971 createCommandExportWrappers();
1972 }
1973 }
1974
1975 if (ctx.sym.initTLS && ctx.sym.initTLS->isLive()) {
1976 log(msg: "-- createInitTLSFunction");
1977 createInitTLSFunction();
1978 }
1979
1980 if (errorCount())
1981 return;
1982
1983 log(msg: "-- calculateTypes");
1984 calculateTypes();
1985 log(msg: "-- calculateExports");
1986 calculateExports();
1987 log(msg: "-- calculateCustomSections");
1988 calculateCustomSections();
1989 log(msg: "-- populateSymtab");
1990 populateSymtab();
1991 log(msg: "-- checkImportExportTargetFeatures");
1992 checkImportExportTargetFeatures();
1993 log(msg: "-- addSections");
1994 addSections();
1995
1996 if (errorHandler().verbose) {
1997 log(msg: "Defined Functions: " + Twine(out.functionSec->inputFunctions.size()));
1998 log(msg: "Defined Globals : " + Twine(out.globalSec->numGlobals()));
1999 log(msg: "Defined Tags : " + Twine(out.tagSec->inputTags.size()));
2000 log(msg: "Defined Tables : " + Twine(out.tableSec->inputTables.size()));
2001 log(msg: "Function Imports : " +
2002 Twine(out.importSec->getNumImportedFunctions()));
2003 log(msg: "Global Imports : " + Twine(out.importSec->getNumImportedGlobals()));
2004 log(msg: "Tag Imports : " + Twine(out.importSec->getNumImportedTags()));
2005 log(msg: "Table Imports : " + Twine(out.importSec->getNumImportedTables()));
2006 }
2007
2008 createHeader();
2009 log(msg: "-- finalizeSections");
2010 finalizeSections();
2011
2012 log(msg: "-- writeMapFile");
2013 writeMapFile(outputSections);
2014
2015 log(msg: "-- openFile");
2016 openFile();
2017 if (errorCount())
2018 return;
2019
2020 writeHeader();
2021
2022 log(msg: "-- writeSections");
2023 writeSections();
2024 writeBuildId();
2025 if (errorCount())
2026 return;
2027
2028 if (Error e = buffer->commit())
2029 fatal(msg: "failed to write output '" + buffer->getPath() +
2030 "': " + toString(E: std::move(e)));
2031}
2032
2033// Open a result file.
2034void Writer::openFile() {
2035 log(msg: "writing: " + ctx.arg.outputFile);
2036
2037 Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
2038 FileOutputBuffer::create(FilePath: ctx.arg.outputFile, Size: fileSize,
2039 Flags: FileOutputBuffer::F_executable);
2040
2041 if (!bufferOrErr)
2042 error(msg: "failed to open " + ctx.arg.outputFile + ": " +
2043 toString(E: bufferOrErr.takeError()));
2044 else
2045 buffer = std::move(*bufferOrErr);
2046}
2047
2048void Writer::createHeader() {
2049 raw_string_ostream os(header);
2050 writeBytes(os, bytes: WasmMagic, count: sizeof(WasmMagic), msg: "wasm magic");
2051 writeU32(os, number: WasmVersion, msg: "wasm version");
2052 fileSize += header.size();
2053}
2054
2055void writeResult() { Writer().run(); }
2056
2057} // namespace lld::wasm
2058