Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0019b7d

Browse files
anutosh491vgvassilev
authored andcommittedMay 10, 2025·
[wasm-ld] Refactor WasmSym from static globals to per-link context (#134970)
Towards This change moves WasmSym from a static global struct to an instance owned by Ctx, allowing it to be reset cleanly between linker runs. This enables safe support for multiple invocations of wasm-ld within the same process Changes done - Converted WasmSym from a static struct to a regular struct with instance members. - Added a std::unique_ptr<WasmSym> wasmSym field inside Ctx. - Reset wasmSym in Ctx::reset() to clear state between links. - Replaced all WasmSym:: references with ctx.wasmSym->. - Removed global symbol definitions from Symbols.cpp that are no longer needed. Clearing wasmSym in ctx.reset() ensures a clean slate for each link invocation, preventing symbol leakage across runs—critical when using wasm-ld/lld as a reentrant library where global state can cause subtle, hard-to-debug errors. --------- Co-authored-by: Vassil Vassilev <[email protected]> (cherry picked from commit 9cbbb74)
1 parent b7b834e commit 0019b7d

File tree

9 files changed

+253
-274
lines changed

9 files changed

+253
-274
lines changed
 

‎lld/wasm/Config.h

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class InputTable;
3232
class InputGlobal;
3333
class InputFunction;
3434
class Symbol;
35+
class DefinedData;
36+
class GlobalSymbol;
37+
class DefinedFunction;
38+
class UndefinedGlobal;
39+
class TableSymbol;
3540

3641
// For --unresolved-symbols.
3742
enum class UnresolvedPolicy { ReportError, Warn, Ignore, ImportDynamic };
@@ -139,6 +144,107 @@ struct Ctx {
139144
llvm::SmallVector<InputGlobal *, 0> syntheticGlobals;
140145
llvm::SmallVector<InputTable *, 0> syntheticTables;
141146

147+
// linker-generated symbols
148+
struct WasmSym {
149+
// __global_base
150+
// Symbol marking the start of the global section.
151+
DefinedData *globalBase;
152+
153+
// __stack_pointer/__stack_low/__stack_high
154+
// Global that holds current value of stack pointer and data symbols marking
155+
// the start and end of the stack region. stackPointer is initialized to
156+
// stackHigh and grows downwards towards stackLow
157+
GlobalSymbol *stackPointer;
158+
DefinedData *stackLow;
159+
DefinedData *stackHigh;
160+
161+
// __tls_base
162+
// Global that holds the address of the base of the current thread's
163+
// TLS block.
164+
GlobalSymbol *tlsBase;
165+
166+
// __tls_size
167+
// Symbol whose value is the size of the TLS block.
168+
GlobalSymbol *tlsSize;
169+
170+
// __tls_size
171+
// Symbol whose value is the alignment of the TLS block.
172+
GlobalSymbol *tlsAlign;
173+
174+
// __data_end
175+
// Symbol marking the end of the data and bss.
176+
DefinedData *dataEnd;
177+
178+
// __heap_base/__heap_end
179+
// Symbols marking the beginning and end of the "heap". It starts at the end
180+
// of the data, bss and explicit stack, and extends to the end of the linear
181+
// memory allocated by wasm-ld. This region of memory is not used by the
182+
// linked code, so it may be used as a backing store for `sbrk` or `malloc`
183+
// implementations.
184+
DefinedData *heapBase;
185+
DefinedData *heapEnd;
186+
187+
// __wasm_init_memory_flag
188+
// Symbol whose contents are nonzero iff memory has already been
189+
// initialized.
190+
DefinedData *initMemoryFlag;
191+
192+
// __wasm_init_memory
193+
// Function that initializes passive data segments during instantiation.
194+
DefinedFunction *initMemory;
195+
196+
// __wasm_call_ctors
197+
// Function that directly calls all ctors in priority order.
198+
DefinedFunction *callCtors;
199+
200+
// __wasm_call_dtors
201+
// Function that calls the libc/etc. cleanup function.
202+
DefinedFunction *callDtors;
203+
204+
// __wasm_apply_global_relocs
205+
// Function that applies relocations to wasm globals post-instantiation.
206+
// Unlike __wasm_apply_data_relocs this needs to run on every thread.
207+
DefinedFunction *applyGlobalRelocs;
208+
209+
// __wasm_apply_tls_relocs
210+
// Like __wasm_apply_data_relocs but for TLS section. These must be
211+
// delayed until __wasm_init_tls.
212+
DefinedFunction *applyTLSRelocs;
213+
214+
// __wasm_apply_global_tls_relocs
215+
// Like applyGlobalRelocs but for globals that hold TLS addresses. These
216+
// must be delayed until __wasm_init_tls.
217+
DefinedFunction *applyGlobalTLSRelocs;
218+
219+
// __wasm_init_tls
220+
// Function that allocates thread-local storage and initializes it.
221+
DefinedFunction *initTLS;
222+
223+
// Pointer to the function that is to be used in the start section.
224+
// (normally an alias of initMemory, or applyGlobalRelocs).
225+
DefinedFunction *startFunction;
226+
227+
// __dso_handle
228+
// Symbol used in calls to __cxa_atexit to determine current DLL
229+
DefinedData *dsoHandle;
230+
231+
// __table_base
232+
// Used in PIC code for offset of indirect function table
233+
UndefinedGlobal *tableBase;
234+
DefinedData *definedTableBase;
235+
236+
// __memory_base
237+
// Used in PIC code for offset of global data
238+
UndefinedGlobal *memoryBase;
239+
DefinedData *definedMemoryBase;
240+
241+
// __indirect_function_table
242+
// Used as an address space for function pointers, with each function that
243+
// is used as a function pointer being allocated a slot.
244+
TableSymbol *indirectFunctionTable;
245+
};
246+
WasmSym sym;
247+
142248
// True if we are creating position-independent code.
143249
bool isPic = false;
144250

‎lld/wasm/Driver.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void Ctx::reset() {
7070
isPic = false;
7171
legacyFunctionTable = false;
7272
emitBssSegments = false;
73+
sym = WasmSym{};
7374
}
7475

7576
namespace {
@@ -941,14 +942,14 @@ static void createSyntheticSymbols() {
941942
true};
942943
static llvm::wasm::WasmGlobalType mutableGlobalTypeI64 = {WASM_TYPE_I64,
943944
true};
944-
WasmSym::callCtors = symtab->addSyntheticFunction(
945+
ctx.sym.callCtors = symtab->addSyntheticFunction(
945946
"__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
946947
make<SyntheticFunction>(nullSignature, "__wasm_call_ctors"));
947948

948949
bool is64 = ctx.arg.is64.value_or(false);
949950

950951
if (ctx.isPic) {
951-
WasmSym::stackPointer =
952+
ctx.sym.stackPointer =
952953
createUndefinedGlobal("__stack_pointer", ctx.arg.is64.value_or(false)
953954
? &mutableGlobalTypeI64
954955
: &mutableGlobalTypeI32);
@@ -958,45 +959,44 @@ static void createSyntheticSymbols() {
958959
// See:
959960
// https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
960961
auto *globalType = is64 ? &globalTypeI64 : &globalTypeI32;
961-
WasmSym::memoryBase = createUndefinedGlobal("__memory_base", globalType);
962-
WasmSym::tableBase = createUndefinedGlobal("__table_base", globalType);
963-
WasmSym::memoryBase->markLive();
964-
WasmSym::tableBase->markLive();
962+
ctx.sym.memoryBase = createUndefinedGlobal("__memory_base", globalType);
963+
ctx.sym.tableBase = createUndefinedGlobal("__table_base", globalType);
964+
ctx.sym.memoryBase->markLive();
965+
ctx.sym.tableBase->markLive();
965966
} else {
966967
// For non-PIC code
967-
WasmSym::stackPointer = createGlobalVariable("__stack_pointer", true);
968-
WasmSym::stackPointer->markLive();
968+
ctx.sym.stackPointer = createGlobalVariable("__stack_pointer", true);
969+
ctx.sym.stackPointer->markLive();
969970
}
970971

971972
if (ctx.arg.sharedMemory) {
972-
WasmSym::tlsBase = createGlobalVariable("__tls_base", true);
973-
WasmSym::tlsSize = createGlobalVariable("__tls_size", false);
974-
WasmSym::tlsAlign = createGlobalVariable("__tls_align", false);
975-
WasmSym::initTLS = symtab->addSyntheticFunction(
973+
ctx.sym.tlsBase = createGlobalVariable("__tls_base", true);
974+
ctx.sym.tlsSize = createGlobalVariable("__tls_size", false);
975+
ctx.sym.tlsAlign = createGlobalVariable("__tls_align", false);
976+
ctx.sym.initTLS = symtab->addSyntheticFunction(
976977
"__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
977-
make<SyntheticFunction>(
978-
is64 ? i64ArgSignature : i32ArgSignature,
979-
"__wasm_init_tls"));
978+
make<SyntheticFunction>(is64 ? i64ArgSignature : i32ArgSignature,
979+
"__wasm_init_tls"));
980980
}
981981
}
982982

983983
static void createOptionalSymbols() {
984984
if (ctx.arg.relocatable)
985985
return;
986986

987-
WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
987+
ctx.sym.dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
988988

989989
if (!ctx.arg.shared)
990-
WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
990+
ctx.sym.dataEnd = symtab->addOptionalDataSymbol("__data_end");
991991

992992
if (!ctx.isPic) {
993-
WasmSym::stackLow = symtab->addOptionalDataSymbol("__stack_low");
994-
WasmSym::stackHigh = symtab->addOptionalDataSymbol("__stack_high");
995-
WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
996-
WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
997-
WasmSym::heapEnd = symtab->addOptionalDataSymbol("__heap_end");
998-
WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
999-
WasmSym::definedTableBase = symtab->addOptionalDataSymbol("__table_base");
993+
ctx.sym.stackLow = symtab->addOptionalDataSymbol("__stack_low");
994+
ctx.sym.stackHigh = symtab->addOptionalDataSymbol("__stack_high");
995+
ctx.sym.globalBase = symtab->addOptionalDataSymbol("__global_base");
996+
ctx.sym.heapBase = symtab->addOptionalDataSymbol("__heap_base");
997+
ctx.sym.heapEnd = symtab->addOptionalDataSymbol("__heap_end");
998+
ctx.sym.definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
999+
ctx.sym.definedTableBase = symtab->addOptionalDataSymbol("__table_base");
10001000
}
10011001

10021002
// For non-shared memory programs we still need to define __tls_base since we
@@ -1009,7 +1009,7 @@ static void createOptionalSymbols() {
10091009
// __tls_size and __tls_align are not needed in this case since they are only
10101010
// needed for __wasm_init_tls (which we do not create in this case).
10111011
if (!ctx.arg.sharedMemory)
1012-
WasmSym::tlsBase = createOptionalGlobal("__tls_base", false);
1012+
ctx.sym.tlsBase = createOptionalGlobal("__tls_base", false);
10131013
}
10141014

10151015
static void processStubLibrariesPreLTO() {
@@ -1384,9 +1384,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
13841384
// by libc/etc., because destructors are registered dynamically with
13851385
// `__cxa_atexit` and friends.
13861386
if (!ctx.arg.relocatable && !ctx.arg.shared &&
1387-
!WasmSym::callCtors->isUsedInRegularObj &&
1388-
WasmSym::callCtors->getName() != ctx.arg.entry &&
1389-
!ctx.arg.exportedSymbols.count(WasmSym::callCtors->getName())) {
1387+
!ctx.sym.callCtors->isUsedInRegularObj &&
1388+
ctx.sym.callCtors->getName() != ctx.arg.entry &&
1389+
!ctx.arg.exportedSymbols.count(ctx.sym.callCtors->getName())) {
13901390
if (Symbol *callDtors =
13911391
handleUndefined("__wasm_call_dtors", "<internal>")) {
13921392
if (auto *callDtorsFunc = dyn_cast<DefinedFunction>(callDtors)) {
@@ -1395,7 +1395,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
13951395
!callDtorsFunc->signature->Returns.empty())) {
13961396
error("__wasm_call_dtors must have no argument or return values");
13971397
}
1398-
WasmSym::callDtors = callDtorsFunc;
1398+
ctx.sym.callDtors = callDtorsFunc;
13991399
} else {
14001400
error("__wasm_call_dtors must be a function");
14011401
}
@@ -1488,7 +1488,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
14881488
markLive();
14891489

14901490
// Provide the indirect function table if needed.
1491-
WasmSym::indirectFunctionTable =
1491+
ctx.sym.indirectFunctionTable =
14921492
symtab->resolveIndirectFunctionTable(/*required =*/false);
14931493

14941494
if (errorCount())

‎lld/wasm/InputChunks.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
397397
if (ctx.isPic) {
398398
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
399399
if (isTLS())
400-
writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "tls_base");
400+
writeUleb128(os, ctx.sym.tlsBase->getGlobalIndex(), "tls_base");
401401
else
402-
writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(), "memory_base");
402+
writeUleb128(os, ctx.sym.memoryBase->getGlobalIndex(), "memory_base");
403403
writeU8(os, opcode_ptr_add, "ADD");
404404
}
405405

@@ -422,12 +422,12 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
422422
}
423423
} else {
424424
assert(ctx.isPic);
425-
const GlobalSymbol* baseSymbol = WasmSym::memoryBase;
425+
const GlobalSymbol *baseSymbol = ctx.sym.memoryBase;
426426
if (rel.Type == R_WASM_TABLE_INDEX_I32 ||
427427
rel.Type == R_WASM_TABLE_INDEX_I64)
428-
baseSymbol = WasmSym::tableBase;
428+
baseSymbol = ctx.sym.tableBase;
429429
else if (sym->isTLS())
430-
baseSymbol = WasmSym::tlsBase;
430+
baseSymbol = ctx.sym.tlsBase;
431431
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
432432
writeUleb128(os, baseSymbol->getGlobalIndex(), "base");
433433
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.sym.callDtors)
118+
enqueue(ctx.sym.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.sym.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
@@ -123,7 +123,7 @@ void DataSection::finalizeContents() {
123123
if ((segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
124124
if (ctx.isPic && ctx.arg.extendedConst) {
125125
writeU8(os, WASM_OPCODE_GLOBAL_GET, "global get");
126-
writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(),
126+
writeUleb128(os, ctx.sym.memoryBase->getGlobalIndex(),
127127
"literal (global index)");
128128
if (segment->startVA) {
129129
writePtrConst(os, segment->startVA, is64, "offset");
@@ -136,7 +136,7 @@ void DataSection::finalizeContents() {
136136
if (ctx.isPic) {
137137
assert(segment->startVA == 0);
138138
initExpr.Inst.Opcode = WASM_OPCODE_GLOBAL_GET;
139-
initExpr.Inst.Value.Global = WasmSym::memoryBase->getGlobalIndex();
139+
initExpr.Inst.Value.Global = ctx.sym.memoryBase->getGlobalIndex();
140140
} else {
141141
initExpr = intConst(segment->startVA, is64);
142142
}

‎lld/wasm/Symbols.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +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-
DefinedFunction *WasmSym::startFunction;
88-
DefinedData *WasmSym::dsoHandle;
89-
DefinedData *WasmSym::dataEnd;
90-
DefinedData *WasmSym::globalBase;
91-
DefinedData *WasmSym::heapBase;
92-
DefinedData *WasmSym::heapEnd;
93-
DefinedData *WasmSym::initMemoryFlag;
94-
GlobalSymbol *WasmSym::stackPointer;
95-
DefinedData *WasmSym::stackLow;
96-
DefinedData *WasmSym::stackHigh;
97-
GlobalSymbol *WasmSym::tlsBase;
98-
GlobalSymbol *WasmSym::tlsSize;
99-
GlobalSymbol *WasmSym::tlsAlign;
100-
UndefinedGlobal *WasmSym::tableBase;
101-
DefinedData *WasmSym::definedTableBase;
102-
UndefinedGlobal *WasmSym::memoryBase;
103-
DefinedData *WasmSym::definedMemoryBase;
104-
TableSymbol *WasmSym::indirectFunctionTable;
10580

10681
WasmSymbolType Symbol::getWasmType() const {
10782
if (isa<FunctionSymbol>(this))

‎lld/wasm/Symbols.h

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -537,105 +537,6 @@ class LazySymbol : public Symbol {
537537
const WasmSignature *signature = nullptr;
538538
};
539539

540-
// linker-generated symbols
541-
struct WasmSym {
542-
// __global_base
543-
// Symbol marking the start of the global section.
544-
static DefinedData *globalBase;
545-
546-
// __stack_pointer/__stack_low/__stack_high
547-
// Global that holds current value of stack pointer and data symbols marking
548-
// the start and end of the stack region. stackPointer is initialized to
549-
// stackHigh and grows downwards towards stackLow
550-
static GlobalSymbol *stackPointer;
551-
static DefinedData *stackLow;
552-
static DefinedData *stackHigh;
553-
554-
// __tls_base
555-
// Global that holds the address of the base of the current thread's
556-
// TLS block.
557-
static GlobalSymbol *tlsBase;
558-
559-
// __tls_size
560-
// Symbol whose value is the size of the TLS block.
561-
static GlobalSymbol *tlsSize;
562-
563-
// __tls_size
564-
// Symbol whose value is the alignment of the TLS block.
565-
static GlobalSymbol *tlsAlign;
566-
567-
// __data_end
568-
// Symbol marking the end of the data and bss.
569-
static DefinedData *dataEnd;
570-
571-
// __heap_base/__heap_end
572-
// Symbols marking the beginning and end of the "heap". It starts at the end
573-
// of the data, bss and explicit stack, and extends to the end of the linear
574-
// memory allocated by wasm-ld. This region of memory is not used by the
575-
// linked code, so it may be used as a backing store for `sbrk` or `malloc`
576-
// implementations.
577-
static DefinedData *heapBase;
578-
static DefinedData *heapEnd;
579-
580-
// __wasm_init_memory_flag
581-
// Symbol whose contents are nonzero iff memory has already been initialized.
582-
static DefinedData *initMemoryFlag;
583-
584-
// __wasm_init_memory
585-
// Function that initializes passive data segments during instantiation.
586-
static DefinedFunction *initMemory;
587-
588-
// __wasm_call_ctors
589-
// Function that directly calls all ctors in priority order.
590-
static DefinedFunction *callCtors;
591-
592-
// __wasm_call_dtors
593-
// Function that calls the libc/etc. cleanup function.
594-
static DefinedFunction *callDtors;
595-
596-
// __wasm_apply_global_relocs
597-
// Function that applies relocations to wasm globals post-instantiation.
598-
// Unlike __wasm_apply_data_relocs this needs to run on every thread.
599-
static DefinedFunction *applyGlobalRelocs;
600-
601-
// __wasm_apply_tls_relocs
602-
// Like __wasm_apply_data_relocs but for TLS section. These must be
603-
// delayed until __wasm_init_tls.
604-
static DefinedFunction *applyTLSRelocs;
605-
606-
// __wasm_apply_global_tls_relocs
607-
// Like applyGlobalRelocs but for globals that hold TLS addresses. These
608-
// must be delayed until __wasm_init_tls.
609-
static DefinedFunction *applyGlobalTLSRelocs;
610-
611-
// __wasm_init_tls
612-
// Function that allocates thread-local storage and initializes it.
613-
static DefinedFunction *initTLS;
614-
615-
// Pointer to the function that is to be used in the start section.
616-
// (normally an alias of initMemory, or applyGlobalRelocs).
617-
static DefinedFunction *startFunction;
618-
619-
// __dso_handle
620-
// Symbol used in calls to __cxa_atexit to determine current DLL
621-
static DefinedData *dsoHandle;
622-
623-
// __table_base
624-
// Used in PIC code for offset of indirect function table
625-
static UndefinedGlobal *tableBase;
626-
static DefinedData *definedTableBase;
627-
628-
// __memory_base
629-
// Used in PIC code for offset of global data
630-
static UndefinedGlobal *memoryBase;
631-
static DefinedData *definedMemoryBase;
632-
633-
// __indirect_function_table
634-
// Used as an address space for function pointers, with each function that is
635-
// used as a function pointer being allocated a slot.
636-
static TableSymbol *indirectFunctionTable;
637-
};
638-
639540
// A buffer class that is large enough to hold any Symbol-derived
640541
// object. We allocate memory using this class and instantiate a symbol
641542
// using the placement new.

‎lld/wasm/SyntheticSections.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ void TableSection::addTable(InputTable *table) {
319319
// Some inputs require that the indirect function table be assigned to table
320320
// number 0.
321321
if (ctx.legacyFunctionTable &&
322-
isa<DefinedTable>(WasmSym::indirectFunctionTable) &&
323-
cast<DefinedTable>(WasmSym::indirectFunctionTable)->table == table) {
322+
isa<DefinedTable>(ctx.sym.indirectFunctionTable) &&
323+
cast<DefinedTable>(ctx.sym.indirectFunctionTable)->table == table) {
324324
if (out.importSec->getNumImportedTables()) {
325325
// Alack! Some other input imported a table, meaning that we are unable
326326
// to assign table number 0 to the indirect function table.
@@ -395,8 +395,8 @@ void GlobalSection::assignIndexes() {
395395
}
396396

397397
static void ensureIndirectFunctionTable() {
398-
if (!WasmSym::indirectFunctionTable)
399-
WasmSym::indirectFunctionTable =
398+
if (!ctx.sym.indirectFunctionTable)
399+
ctx.sym.indirectFunctionTable =
400400
symtab->resolveIndirectFunctionTable(/*required =*/true);
401401
}
402402

@@ -430,10 +430,9 @@ void GlobalSection::generateRelocationCode(raw_ostream &os, bool TLS) const {
430430
// Get __memory_base
431431
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
432432
if (sym->isTLS())
433-
writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "__tls_base");
433+
writeUleb128(os, ctx.sym.tlsBase->getGlobalIndex(), "__tls_base");
434434
else
435-
writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(),
436-
"__memory_base");
435+
writeUleb128(os, ctx.sym.memoryBase->getGlobalIndex(), "__memory_base");
437436

438437
// Add the virtual address of the data symbol
439438
writeU8(os, opcode_ptr_const, "CONST");
@@ -443,7 +442,7 @@ void GlobalSection::generateRelocationCode(raw_ostream &os, bool TLS) const {
443442
continue;
444443
// Get __table_base
445444
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
446-
writeUleb128(os, WasmSym::tableBase->getGlobalIndex(), "__table_base");
445+
writeUleb128(os, ctx.sym.tableBase->getGlobalIndex(), "__table_base");
447446

448447
// Add the table index to __table_base
449448
writeU8(os, opcode_ptr_const, "CONST");
@@ -490,13 +489,13 @@ void GlobalSection::writeBody() {
490489
if (ctx.arg.extendedConst && ctx.isPic) {
491490
if (auto *d = dyn_cast<DefinedData>(sym)) {
492491
if (!sym->isTLS()) {
493-
globalIdx = WasmSym::memoryBase->getGlobalIndex();
492+
globalIdx = ctx.sym.memoryBase->getGlobalIndex();
494493
offset = d->getVA();
495494
useExtendedConst = true;
496495
}
497496
} else if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
498497
if (!sym->isStub) {
499-
globalIdx = WasmSym::tableBase->getGlobalIndex();
498+
globalIdx = ctx.sym.tableBase->getGlobalIndex();
500499
offset = f->getTableIndex();
501500
useExtendedConst = true;
502501
}
@@ -550,14 +549,11 @@ void ExportSection::writeBody() {
550549
writeExport(os, export_);
551550
}
552551

553-
bool StartSection::isNeeded() const {
554-
return WasmSym::startFunction != nullptr;
555-
}
552+
bool StartSection::isNeeded() const { return ctx.sym.startFunction != nullptr; }
556553

557554
void StartSection::writeBody() {
558555
raw_ostream &os = bodyOutputStream;
559-
writeUleb128(os, WasmSym::startFunction->getFunctionIndex(),
560-
"function index");
556+
writeUleb128(os, ctx.sym.startFunction->getFunctionIndex(), "function index");
561557
}
562558

563559
void ElemSection::addEntry(FunctionSymbol *sym) {
@@ -573,9 +569,9 @@ void ElemSection::addEntry(FunctionSymbol *sym) {
573569
void ElemSection::writeBody() {
574570
raw_ostream &os = bodyOutputStream;
575571

576-
assert(WasmSym::indirectFunctionTable);
572+
assert(ctx.sym.indirectFunctionTable);
577573
writeUleb128(os, 1, "segment count");
578-
uint32_t tableNumber = WasmSym::indirectFunctionTable->getTableNumber();
574+
uint32_t tableNumber = ctx.sym.indirectFunctionTable->getTableNumber();
579575
uint32_t flags = 0;
580576
if (tableNumber)
581577
flags |= WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER;
@@ -587,7 +583,7 @@ void ElemSection::writeBody() {
587583
initExpr.Extended = false;
588584
if (ctx.isPic) {
589585
initExpr.Inst.Opcode = WASM_OPCODE_GLOBAL_GET;
590-
initExpr.Inst.Value.Global = WasmSym::tableBase->getGlobalIndex();
586+
initExpr.Inst.Value.Global = ctx.sym.tableBase->getGlobalIndex();
591587
} else {
592588
bool is64 = ctx.arg.is64.value_or(false);
593589
initExpr = intConst(ctx.arg.tableBase, is64);

‎lld/wasm/Writer.cpp

Lines changed: 93 additions & 92 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.