Skip to content

Commit 0304b6d

Browse files
committed
Refactor WasmSym from static globals to per-link context to support multiple wasm-ld invocations
1 parent 9b8f534 commit 0304b6d

File tree

9 files changed

+178
-194
lines changed

9 files changed

+178
-194
lines changed

lld/wasm/Config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class InputTable;
3232
class InputGlobal;
3333
class InputFunction;
3434
class Symbol;
35+
struct WasmSym;
3536

3637
// For --unresolved-symbols.
3738
enum class UnresolvedPolicy { ReportError, Warn, Ignore, ImportDynamic };
@@ -141,6 +142,9 @@ struct Ctx {
141142
llvm::SmallVector<InputGlobal *, 0> syntheticGlobals;
142143
llvm::SmallVector<InputTable *, 0> syntheticTables;
143144

145+
// Linker-generated symbols like __wasm_init_memory, __heap_base, etc.
146+
std::unique_ptr<WasmSym> wasmSym;
147+
144148
// True if we are creating position-independent code.
145149
bool isPic = false;
146150

lld/wasm/Driver.cpp

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ void Ctx::reset() {
7070
isPic = false;
7171
legacyFunctionTable = false;
7272
emitBssSegments = false;
73+
wasmSym.reset();
74+
wasmSym = std::make_unique<WasmSym>();
7375
}
7476

7577
namespace {
@@ -945,14 +947,14 @@ static void createSyntheticSymbols() {
945947
true};
946948
static llvm::wasm::WasmGlobalType mutableGlobalTypeI64 = {WASM_TYPE_I64,
947949
true};
948-
WasmSym::callCtors = symtab->addSyntheticFunction(
950+
ctx.wasmSym->callCtors = symtab->addSyntheticFunction(
949951
"__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
950952
make<SyntheticFunction>(nullSignature, "__wasm_call_ctors"));
951953

952954
bool is64 = ctx.arg.is64.value_or(false);
953955

954956
if (ctx.isPic) {
955-
WasmSym::stackPointer =
957+
ctx.wasmSym->stackPointer =
956958
createUndefinedGlobal("__stack_pointer", ctx.arg.is64.value_or(false)
957959
? &mutableGlobalTypeI64
958960
: &mutableGlobalTypeI32);
@@ -962,21 +964,21 @@ static void createSyntheticSymbols() {
962964
// See:
963965
// https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
964966
auto *globalType = is64 ? &globalTypeI64 : &globalTypeI32;
965-
WasmSym::memoryBase = createUndefinedGlobal("__memory_base", globalType);
966-
WasmSym::tableBase = createUndefinedGlobal("__table_base", globalType);
967-
WasmSym::memoryBase->markLive();
968-
WasmSym::tableBase->markLive();
967+
ctx.wasmSym->memoryBase = createUndefinedGlobal("__memory_base", globalType);
968+
ctx.wasmSym->tableBase = createUndefinedGlobal("__table_base", globalType);
969+
ctx.wasmSym->memoryBase->markLive();
970+
ctx.wasmSym->tableBase->markLive();
969971
} else {
970972
// For non-PIC code
971-
WasmSym::stackPointer = createGlobalVariable("__stack_pointer", true);
972-
WasmSym::stackPointer->markLive();
973+
ctx.wasmSym->stackPointer = createGlobalVariable("__stack_pointer", true);
974+
ctx.wasmSym->stackPointer->markLive();
973975
}
974976

975977
if (ctx.arg.sharedMemory) {
976-
WasmSym::tlsBase = createGlobalVariable("__tls_base", true);
977-
WasmSym::tlsSize = createGlobalVariable("__tls_size", false);
978-
WasmSym::tlsAlign = createGlobalVariable("__tls_align", false);
979-
WasmSym::initTLS = symtab->addSyntheticFunction(
978+
ctx.wasmSym->tlsBase = createGlobalVariable("__tls_base", true);
979+
ctx.wasmSym->tlsSize = createGlobalVariable("__tls_size", false);
980+
ctx.wasmSym->tlsAlign = createGlobalVariable("__tls_align", false);
981+
ctx.wasmSym->initTLS = symtab->addSyntheticFunction(
980982
"__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
981983
make<SyntheticFunction>(
982984
is64 ? i64ArgSignature : i32ArgSignature,
@@ -988,25 +990,25 @@ static void createOptionalSymbols() {
988990
if (ctx.arg.relocatable)
989991
return;
990992

991-
WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
993+
ctx.wasmSym->dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
992994

993995
if (!ctx.arg.shared)
994-
WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
996+
ctx.wasmSym->dataEnd = symtab->addOptionalDataSymbol("__data_end");
995997

996998
if (!ctx.isPic) {
997-
WasmSym::stackLow = symtab->addOptionalDataSymbol("__stack_low");
998-
WasmSym::stackHigh = symtab->addOptionalDataSymbol("__stack_high");
999-
WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
1000-
WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
1001-
WasmSym::heapEnd = symtab->addOptionalDataSymbol("__heap_end");
1002-
WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
1003-
WasmSym::definedTableBase = symtab->addOptionalDataSymbol("__table_base");
999+
ctx.wasmSym->stackLow = symtab->addOptionalDataSymbol("__stack_low");
1000+
ctx.wasmSym->stackHigh = symtab->addOptionalDataSymbol("__stack_high");
1001+
ctx.wasmSym->globalBase = symtab->addOptionalDataSymbol("__global_base");
1002+
ctx.wasmSym->heapBase = symtab->addOptionalDataSymbol("__heap_base");
1003+
ctx.wasmSym->heapEnd = symtab->addOptionalDataSymbol("__heap_end");
1004+
ctx.wasmSym->definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
1005+
ctx.wasmSym->definedTableBase = symtab->addOptionalDataSymbol("__table_base");
10041006
}
10051007

1006-
WasmSym::firstPageEnd =
1008+
ctx.wasmSym->firstPageEnd =
10071009
symtab->addOptionalDataSymbol("__wasm_first_page_end");
1008-
if (WasmSym::firstPageEnd)
1009-
WasmSym::firstPageEnd->setVA(ctx.arg.pageSize);
1010+
if (ctx.wasmSym->firstPageEnd)
1011+
ctx.wasmSym->firstPageEnd->setVA(ctx.arg.pageSize);
10101012

10111013
// For non-shared memory programs we still need to define __tls_base since we
10121014
// allow object files built with TLS to be linked into single threaded
@@ -1018,7 +1020,7 @@ static void createOptionalSymbols() {
10181020
// __tls_size and __tls_align are not needed in this case since they are only
10191021
// needed for __wasm_init_tls (which we do not create in this case).
10201022
if (!ctx.arg.sharedMemory)
1021-
WasmSym::tlsBase = createOptionalGlobal("__tls_base", false);
1023+
ctx.wasmSym->tlsBase = createOptionalGlobal("__tls_base", false);
10221024
}
10231025

10241026
static void processStubLibrariesPreLTO() {
@@ -1393,9 +1395,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
13931395
// by libc/etc., because destructors are registered dynamically with
13941396
// `__cxa_atexit` and friends.
13951397
if (!ctx.arg.relocatable && !ctx.arg.shared &&
1396-
!WasmSym::callCtors->isUsedInRegularObj &&
1397-
WasmSym::callCtors->getName() != ctx.arg.entry &&
1398-
!ctx.arg.exportedSymbols.count(WasmSym::callCtors->getName())) {
1398+
!ctx.wasmSym->callCtors->isUsedInRegularObj &&
1399+
ctx.wasmSym->callCtors->getName() != ctx.arg.entry &&
1400+
!ctx.arg.exportedSymbols.count(ctx.wasmSym->callCtors->getName())) {
13991401
if (Symbol *callDtors =
14001402
handleUndefined("__wasm_call_dtors", "<internal>")) {
14011403
if (auto *callDtorsFunc = dyn_cast<DefinedFunction>(callDtors)) {
@@ -1404,7 +1406,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
14041406
!callDtorsFunc->signature->Returns.empty())) {
14051407
error("__wasm_call_dtors must have no argument or return values");
14061408
}
1407-
WasmSym::callDtors = callDtorsFunc;
1409+
ctx.wasmSym->callDtors = callDtorsFunc;
14081410
} else {
14091411
error("__wasm_call_dtors must be a function");
14101412
}
@@ -1497,7 +1499,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
14971499
markLive();
14981500

14991501
// Provide the indirect function table if needed.
1500-
WasmSym::indirectFunctionTable =
1502+
ctx.wasmSym->indirectFunctionTable =
15011503
symtab->resolveIndirectFunctionTable(/*required =*/false);
15021504

15031505
if (errorCount())

lld/wasm/InputChunks.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,9 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
411411
if (ctx.isPic) {
412412
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
413413
if (isTLS())
414-
writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "tls_base");
414+
writeUleb128(os, ctx.wasmSym->tlsBase->getGlobalIndex(), "tls_base");
415415
else
416-
writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(), "memory_base");
416+
writeUleb128(os, ctx.wasmSym->memoryBase->getGlobalIndex(), "memory_base");
417417
writeU8(os, opcode_ptr_add, "ADD");
418418
}
419419

@@ -436,12 +436,12 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
436436
}
437437
} else {
438438
assert(ctx.isPic);
439-
const GlobalSymbol* baseSymbol = WasmSym::memoryBase;
439+
const GlobalSymbol* baseSymbol = ctx.wasmSym->memoryBase;
440440
if (rel.Type == R_WASM_TABLE_INDEX_I32 ||
441441
rel.Type == R_WASM_TABLE_INDEX_I64)
442-
baseSymbol = WasmSym::tableBase;
442+
baseSymbol = ctx.wasmSym->tableBase;
443443
else if (sym->isTLS())
444-
baseSymbol = WasmSym::tlsBase;
444+
baseSymbol = ctx.wasmSym->tlsBase;
445445
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
446446
writeUleb128(os, baseSymbol->getGlobalIndex(), "base");
447447
writeU8(os, opcode_reloc_const, "CONST");

lld/wasm/MarkLive.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ void MarkLive::run() {
114114
if (sym->isNoStrip() || sym->isExported())
115115
enqueue(sym);
116116

117-
if (WasmSym::callDtors)
118-
enqueue(WasmSym::callDtors);
117+
if (ctx.wasmSym->callDtors)
118+
enqueue(ctx.wasmSym->callDtors);
119119

120120
for (const ObjFile *obj : ctx.objectFiles)
121121
if (obj->isLive()) {
@@ -131,7 +131,7 @@ void MarkLive::run() {
131131
// If we have any non-discarded init functions, mark `__wasm_call_ctors` as
132132
// live so that we assign it an index and call it.
133133
if (isCallCtorsLive())
134-
WasmSym::callCtors->markLive();
134+
ctx.wasmSym->callCtors->markLive();
135135
}
136136

137137
void MarkLive::mark() {

lld/wasm/OutputSections.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void DataSection::finalizeContents() {
124124
if ((segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
125125
if (ctx.isPic && ctx.arg.extendedConst) {
126126
writeU8(os, WASM_OPCODE_GLOBAL_GET, "global get");
127-
writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(),
127+
writeUleb128(os, ctx.wasmSym->memoryBase->getGlobalIndex(),
128128
"literal (global index)");
129129
if (segment->startVA) {
130130
writePtrConst(os, segment->startVA, is64, "offset");
@@ -137,7 +137,7 @@ void DataSection::finalizeContents() {
137137
if (ctx.isPic) {
138138
assert(segment->startVA == 0);
139139
initExpr.Inst.Opcode = WASM_OPCODE_GLOBAL_GET;
140-
initExpr.Inst.Value.Global = WasmSym::memoryBase->getGlobalIndex();
140+
initExpr.Inst.Value.Global = ctx.wasmSym->memoryBase->getGlobalIndex();
141141
} else {
142142
initExpr = intConst(segment->startVA, is64);
143143
}

lld/wasm/Symbols.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,6 @@ std::string toString(wasm::Symbol::Kind kind) {
7777
}
7878

7979
namespace wasm {
80-
DefinedFunction *WasmSym::callCtors;
81-
DefinedFunction *WasmSym::callDtors;
82-
DefinedFunction *WasmSym::initMemory;
83-
DefinedFunction *WasmSym::applyGlobalRelocs;
84-
DefinedFunction *WasmSym::applyTLSRelocs;
85-
DefinedFunction *WasmSym::applyGlobalTLSRelocs;
86-
DefinedFunction *WasmSym::initTLS;
87-
DefinedData *WasmSym::firstPageEnd;
88-
DefinedFunction *WasmSym::startFunction;
89-
DefinedData *WasmSym::dsoHandle;
90-
DefinedData *WasmSym::dataEnd;
91-
DefinedData *WasmSym::globalBase;
92-
DefinedData *WasmSym::heapBase;
93-
DefinedData *WasmSym::heapEnd;
94-
DefinedData *WasmSym::initMemoryFlag;
95-
GlobalSymbol *WasmSym::stackPointer;
96-
DefinedData *WasmSym::stackLow;
97-
DefinedData *WasmSym::stackHigh;
98-
GlobalSymbol *WasmSym::tlsBase;
99-
GlobalSymbol *WasmSym::tlsSize;
100-
GlobalSymbol *WasmSym::tlsAlign;
101-
UndefinedGlobal *WasmSym::tableBase;
102-
DefinedData *WasmSym::definedTableBase;
103-
UndefinedGlobal *WasmSym::memoryBase;
104-
DefinedData *WasmSym::definedMemoryBase;
105-
TableSymbol *WasmSym::indirectFunctionTable;
10680

10781
WasmSymbolType Symbol::getWasmType() const {
10882
if (isa<FunctionSymbol>(this))

lld/wasm/Symbols.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -541,103 +541,103 @@ class LazySymbol : public Symbol {
541541
struct WasmSym {
542542
// __global_base
543543
// Symbol marking the start of the global section.
544-
static DefinedData *globalBase;
544+
DefinedData *globalBase = nullptr;
545545

546546
// __stack_pointer/__stack_low/__stack_high
547547
// Global that holds current value of stack pointer and data symbols marking
548548
// the start and end of the stack region. stackPointer is initialized to
549549
// stackHigh and grows downwards towards stackLow
550-
static GlobalSymbol *stackPointer;
551-
static DefinedData *stackLow;
552-
static DefinedData *stackHigh;
550+
GlobalSymbol *stackPointer = nullptr;
551+
DefinedData *stackLow = nullptr;
552+
DefinedData *stackHigh = nullptr;
553553

554554
// __tls_base
555555
// Global that holds the address of the base of the current thread's
556556
// TLS block.
557-
static GlobalSymbol *tlsBase;
557+
GlobalSymbol *tlsBase = nullptr;
558558

559559
// __tls_size
560560
// Symbol whose value is the size of the TLS block.
561-
static GlobalSymbol *tlsSize;
561+
GlobalSymbol *tlsSize = nullptr;
562562

563563
// __tls_size
564564
// Symbol whose value is the alignment of the TLS block.
565-
static GlobalSymbol *tlsAlign;
565+
GlobalSymbol *tlsAlign = nullptr;
566566

567567
// __data_end
568568
// Symbol marking the end of the data and bss.
569-
static DefinedData *dataEnd;
569+
DefinedData *dataEnd = nullptr;
570570

571571
// __heap_base/__heap_end
572572
// Symbols marking the beginning and end of the "heap". It starts at the end
573573
// of the data, bss and explicit stack, and extends to the end of the linear
574574
// memory allocated by wasm-ld. This region of memory is not used by the
575575
// linked code, so it may be used as a backing store for `sbrk` or `malloc`
576576
// implementations.
577-
static DefinedData *heapBase;
578-
static DefinedData *heapEnd;
577+
DefinedData *heapBase = nullptr;
578+
DefinedData *heapEnd = nullptr;
579579

580580
// __wasm_first_page_end
581581
// A symbol whose address is the end of the first page in memory (if any).
582-
static DefinedData *firstPageEnd;
582+
DefinedData *firstPageEnd = nullptr;
583583

584584
// __wasm_init_memory_flag
585585
// Symbol whose contents are nonzero iff memory has already been initialized.
586-
static DefinedData *initMemoryFlag;
586+
DefinedData *initMemoryFlag = nullptr;
587587

588588
// __wasm_init_memory
589589
// Function that initializes passive data segments during instantiation.
590-
static DefinedFunction *initMemory;
590+
DefinedFunction *initMemory = nullptr;
591591

592592
// __wasm_call_ctors
593593
// Function that directly calls all ctors in priority order.
594-
static DefinedFunction *callCtors;
594+
DefinedFunction *callCtors = nullptr;
595595

596596
// __wasm_call_dtors
597597
// Function that calls the libc/etc. cleanup function.
598-
static DefinedFunction *callDtors;
598+
DefinedFunction *callDtors = nullptr;
599599

600600
// __wasm_apply_global_relocs
601601
// Function that applies relocations to wasm globals post-instantiation.
602602
// Unlike __wasm_apply_data_relocs this needs to run on every thread.
603-
static DefinedFunction *applyGlobalRelocs;
603+
DefinedFunction *applyGlobalRelocs = nullptr;
604604

605605
// __wasm_apply_tls_relocs
606606
// Like __wasm_apply_data_relocs but for TLS section. These must be
607607
// delayed until __wasm_init_tls.
608-
static DefinedFunction *applyTLSRelocs;
608+
DefinedFunction *applyTLSRelocs = nullptr;
609609

610610
// __wasm_apply_global_tls_relocs
611611
// Like applyGlobalRelocs but for globals that hold TLS addresses. These
612612
// must be delayed until __wasm_init_tls.
613-
static DefinedFunction *applyGlobalTLSRelocs;
613+
DefinedFunction *applyGlobalTLSRelocs = nullptr;
614614

615615
// __wasm_init_tls
616616
// Function that allocates thread-local storage and initializes it.
617-
static DefinedFunction *initTLS;
617+
DefinedFunction *initTLS = nullptr;
618618

619619
// Pointer to the function that is to be used in the start section.
620620
// (normally an alias of initMemory, or applyGlobalRelocs).
621-
static DefinedFunction *startFunction;
621+
DefinedFunction *startFunction = nullptr;
622622

623623
// __dso_handle
624624
// Symbol used in calls to __cxa_atexit to determine current DLL
625-
static DefinedData *dsoHandle;
625+
DefinedData *dsoHandle = nullptr;
626626

627627
// __table_base
628628
// Used in PIC code for offset of indirect function table
629-
static UndefinedGlobal *tableBase;
630-
static DefinedData *definedTableBase;
629+
UndefinedGlobal *tableBase = nullptr;
630+
DefinedData *definedTableBase = nullptr;
631631

632632
// __memory_base
633633
// Used in PIC code for offset of global data
634-
static UndefinedGlobal *memoryBase;
635-
static DefinedData *definedMemoryBase;
634+
UndefinedGlobal *memoryBase = nullptr;
635+
DefinedData *definedMemoryBase = nullptr;
636636

637637
// __indirect_function_table
638638
// Used as an address space for function pointers, with each function that is
639639
// used as a function pointer being allocated a slot.
640-
static TableSymbol *indirectFunctionTable;
640+
TableSymbol *indirectFunctionTable = nullptr;
641641
};
642642

643643
// A buffer class that is large enough to hold any Symbol-derived

0 commit comments

Comments
 (0)