Skip to content

Commit 0019b7d

Browse files
anutosh491vgvassilev
authored andcommitted
[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 <v.g.vassilev@gmail.com> (cherry picked from commit 9cbbb74)
1 parent b7b834e commit 0019b7d

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))

0 commit comments

Comments
 (0)