Skip to content

Commit ca2d695

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

9 files changed

+188
-199
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+
WasmSym sym{};
147+
144148
// True if we are creating position-independent code.
145149
bool isPic = false;
146150

lld/wasm/Driver.cpp

Lines changed: 36 additions & 33 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 {
@@ -945,14 +946,14 @@ static void createSyntheticSymbols() {
945946
true};
946947
static llvm::wasm::WasmGlobalType mutableGlobalTypeI64 = {WASM_TYPE_I64,
947948
true};
948-
WasmSym::callCtors = symtab->addSyntheticFunction(
949+
ctx.sym.callCtors = symtab->addSyntheticFunction(
949950
"__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
950951
make<SyntheticFunction>(nullSignature, "__wasm_call_ctors"));
951952

952953
bool is64 = ctx.arg.is64.value_or(false);
953954

954955
if (ctx.isPic) {
955-
WasmSym::stackPointer =
956+
ctx.sym.stackPointer =
956957
createUndefinedGlobal("__stack_pointer", ctx.arg.is64.value_or(false)
957958
? &mutableGlobalTypeI64
958959
: &mutableGlobalTypeI32);
@@ -962,51 +963,53 @@ static void createSyntheticSymbols() {
962963
// See:
963964
// https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
964965
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();
966+
ctx.sym.memoryBase =
967+
createUndefinedGlobal("__memory_base", globalType);
968+
ctx.sym.tableBase = createUndefinedGlobal("__table_base", globalType);
969+
ctx.sym.memoryBase->markLive();
970+
ctx.sym.tableBase->markLive();
969971
} else {
970972
// For non-PIC code
971-
WasmSym::stackPointer = createGlobalVariable("__stack_pointer", true);
972-
WasmSym::stackPointer->markLive();
973+
ctx.sym.stackPointer = createGlobalVariable("__stack_pointer", true);
974+
ctx.sym.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.sym.tlsBase = createGlobalVariable("__tls_base", true);
979+
ctx.sym.tlsSize = createGlobalVariable("__tls_size", false);
980+
ctx.sym.tlsAlign = createGlobalVariable("__tls_align", false);
981+
ctx.sym.initTLS = symtab->addSyntheticFunction(
980982
"__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
981-
make<SyntheticFunction>(
982-
is64 ? i64ArgSignature : i32ArgSignature,
983-
"__wasm_init_tls"));
983+
make<SyntheticFunction>(is64 ? i64ArgSignature : i32ArgSignature,
984+
"__wasm_init_tls"));
984985
}
985986
}
986987

987988
static void createOptionalSymbols() {
988989
if (ctx.arg.relocatable)
989990
return;
990991

991-
WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
992+
ctx.sym.dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
992993

993994
if (!ctx.arg.shared)
994-
WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
995+
ctx.sym.dataEnd = symtab->addOptionalDataSymbol("__data_end");
995996

996997
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");
998+
ctx.sym.stackLow = symtab->addOptionalDataSymbol("__stack_low");
999+
ctx.sym.stackHigh = symtab->addOptionalDataSymbol("__stack_high");
1000+
ctx.sym.globalBase = symtab->addOptionalDataSymbol("__global_base");
1001+
ctx.sym.heapBase = symtab->addOptionalDataSymbol("__heap_base");
1002+
ctx.sym.heapEnd = symtab->addOptionalDataSymbol("__heap_end");
1003+
ctx.sym.definedMemoryBase =
1004+
symtab->addOptionalDataSymbol("__memory_base");
1005+
ctx.sym.definedTableBase =
1006+
symtab->addOptionalDataSymbol("__table_base");
10041007
}
10051008

1006-
WasmSym::firstPageEnd =
1009+
ctx.sym.firstPageEnd =
10071010
symtab->addOptionalDataSymbol("__wasm_first_page_end");
1008-
if (WasmSym::firstPageEnd)
1009-
WasmSym::firstPageEnd->setVA(ctx.arg.pageSize);
1011+
if (ctx.sym.firstPageEnd)
1012+
ctx.sym.firstPageEnd->setVA(ctx.arg.pageSize);
10101013

10111014
// For non-shared memory programs we still need to define __tls_base since we
10121015
// allow object files built with TLS to be linked into single threaded
@@ -1018,7 +1021,7 @@ static void createOptionalSymbols() {
10181021
// __tls_size and __tls_align are not needed in this case since they are only
10191022
// needed for __wasm_init_tls (which we do not create in this case).
10201023
if (!ctx.arg.sharedMemory)
1021-
WasmSym::tlsBase = createOptionalGlobal("__tls_base", false);
1024+
ctx.sym.tlsBase = createOptionalGlobal("__tls_base", false);
10221025
}
10231026

10241027
static void processStubLibrariesPreLTO() {
@@ -1393,9 +1396,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
13931396
// by libc/etc., because destructors are registered dynamically with
13941397
// `__cxa_atexit` and friends.
13951398
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())) {
1399+
!ctx.sym.callCtors->isUsedInRegularObj &&
1400+
ctx.sym.callCtors->getName() != ctx.arg.entry &&
1401+
!ctx.arg.exportedSymbols.count(ctx.sym.callCtors->getName())) {
13991402
if (Symbol *callDtors =
14001403
handleUndefined("__wasm_call_dtors", "<internal>")) {
14011404
if (auto *callDtorsFunc = dyn_cast<DefinedFunction>(callDtors)) {
@@ -1404,7 +1407,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
14041407
!callDtorsFunc->signature->Returns.empty())) {
14051408
error("__wasm_call_dtors must have no argument or return values");
14061409
}
1407-
WasmSym::callDtors = callDtorsFunc;
1410+
ctx.sym.callDtors = callDtorsFunc;
14081411
} else {
14091412
error("__wasm_call_dtors must be a function");
14101413
}
@@ -1497,7 +1500,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
14971500
markLive();
14981501

14991502
// Provide the indirect function table if needed.
1500-
WasmSym::indirectFunctionTable =
1503+
ctx.sym.indirectFunctionTable =
15011504
symtab->resolveIndirectFunctionTable(/*required =*/false);
15021505

15031506
if (errorCount())

lld/wasm/InputChunks.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,10 @@ 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.sym.tlsBase->getGlobalIndex(), "tls_base");
415415
else
416-
writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(), "memory_base");
416+
writeUleb128(os, ctx.sym.memoryBase->getGlobalIndex(),
417+
"memory_base");
417418
writeU8(os, opcode_ptr_add, "ADD");
418419
}
419420

@@ -436,12 +437,12 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
436437
}
437438
} else {
438439
assert(ctx.isPic);
439-
const GlobalSymbol* baseSymbol = WasmSym::memoryBase;
440+
const GlobalSymbol *baseSymbol = ctx.sym.memoryBase;
440441
if (rel.Type == R_WASM_TABLE_INDEX_I32 ||
441442
rel.Type == R_WASM_TABLE_INDEX_I64)
442-
baseSymbol = WasmSym::tableBase;
443+
baseSymbol = ctx.sym.tableBase;
443444
else if (sym->isTLS())
444-
baseSymbol = WasmSym::tlsBase;
445+
baseSymbol = ctx.sym.tlsBase;
445446
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
446447
writeUleb128(os, baseSymbol->getGlobalIndex(), "base");
447448
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: 3 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.sym.memoryBase->getGlobalIndex(),
128128
"literal (global index)");
129129
if (segment->startVA) {
130130
writePtrConst(os, segment->startVA, is64, "offset");
@@ -137,7 +137,8 @@ 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 =
141+
ctx.sym.memoryBase->getGlobalIndex();
141142
} else {
142143
initExpr = intConst(segment->startVA, is64);
143144
}

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;
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;
551+
DefinedData *stackLow;
552+
DefinedData *stackHigh;
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;
558558

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

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

567567
// __data_end
568568
// Symbol marking the end of the data and bss.
569-
static DefinedData *dataEnd;
569+
DefinedData *dataEnd;
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;
578+
DefinedData *heapEnd;
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;
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;
587587

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

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

596596
// __wasm_call_dtors
597597
// Function that calls the libc/etc. cleanup function.
598-
static DefinedFunction *callDtors;
598+
DefinedFunction *callDtors;
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;
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;
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;
614614

615615
// __wasm_init_tls
616616
// Function that allocates thread-local storage and initializes it.
617-
static DefinedFunction *initTLS;
617+
DefinedFunction *initTLS;
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;
622622

623623
// __dso_handle
624624
// Symbol used in calls to __cxa_atexit to determine current DLL
625-
static DefinedData *dsoHandle;
625+
DefinedData *dsoHandle;
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;
630+
DefinedData *definedTableBase;
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;
635+
DefinedData *definedMemoryBase;
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;
641641
};
642642

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

0 commit comments

Comments
 (0)