Skip to content

Commit 82dd674

Browse files
committed
[lld][WebAssembly]: Defer __wasm_apply_data_relocs decision to writer phase
At the time of createSyntheticSymbols, it isn't obvious if we need these relocations.
1 parent 85f9181 commit 82dd674

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

lld/wasm/Driver.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -917,18 +917,6 @@ static void createSyntheticSymbols() {
917917
is64 ? i64ArgSignature : i32ArgSignature,
918918
"__wasm_init_tls"));
919919
}
920-
921-
if (ctx.isPic ||
922-
config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic ||
923-
!config->isStatic) {
924-
// For PIC code, or when dynamically importing addresses, we create
925-
// synthetic functions that apply relocations. These get called from
926-
// __wasm_call_ctors before the user-level constructors.
927-
WasmSym::applyDataRelocs = symtab->addSyntheticFunction(
928-
"__wasm_apply_data_relocs",
929-
WASM_SYMBOL_VISIBILITY_DEFAULT | WASM_SYMBOL_EXPORTED,
930-
make<SyntheticFunction>(nullSignature, "__wasm_apply_data_relocs"));
931-
}
932920
}
933921

934922
static void createOptionalSymbols() {

lld/wasm/Symbols.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ namespace wasm {
8080
DefinedFunction *WasmSym::callCtors;
8181
DefinedFunction *WasmSym::callDtors;
8282
DefinedFunction *WasmSym::initMemory;
83-
DefinedFunction *WasmSym::applyDataRelocs;
8483
DefinedFunction *WasmSym::applyGlobalRelocs;
8584
DefinedFunction *WasmSym::applyTLSRelocs;
8685
DefinedFunction *WasmSym::applyGlobalTLSRelocs;

lld/wasm/Symbols.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,18 +591,14 @@ struct WasmSym {
591591
// Function that calls the libc/etc. cleanup function.
592592
static DefinedFunction *callDtors;
593593

594-
// __wasm_apply_data_relocs
595-
// Function that applies relocations to data segment post-instantiation.
596-
static DefinedFunction *applyDataRelocs;
597-
598594
// __wasm_apply_global_relocs
599595
// Function that applies relocations to wasm globals post-instantiation.
600596
// Unlike __wasm_apply_data_relocs this needs to run on every thread.
601597
static DefinedFunction *applyGlobalRelocs;
602598

603599
// __wasm_apply_tls_relocs
604-
// Like applyDataRelocs but for TLS section. These must be delayed until
605-
// __wasm_init_tls.
600+
// Like __wasm_apply_data_relocs but for TLS section. These must be
601+
// delayed until __wasm_init_tls.
606602
static DefinedFunction *applyTLSRelocs;
607603

608604
// __wasm_apply_global_tls_relocs

lld/wasm/Writer.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,8 @@ void Writer::createSyntheticInitFunctions() {
11451145

11461146
static WasmSignature nullSignature = {{}, {}};
11471147

1148+
createApplyDataRelocationsFunction();
1149+
11481150
// Passive segments are used to avoid memory being reinitialized on each
11491151
// thread's instantiation. These passive segments are initialized and
11501152
// dropped in __wasm_init_memory, which is registered as the start function
@@ -1467,15 +1469,29 @@ void Writer::createApplyDataRelocationsFunction() {
14671469
{
14681470
raw_string_ostream os(bodyContent);
14691471
writeUleb128(os, 0, "num locals");
1472+
uint64_t off = os.tell();
14701473
for (const OutputSegment *seg : segments)
14711474
if (!config->sharedMemory || !seg->isTLS())
14721475
for (const InputChunk *inSeg : seg->inputSegments)
14731476
inSeg->generateRelocationCode(os);
14741477

1478+
if (off == os.tell()) {
1479+
LLVM_DEBUG(dbgs() << "skipping empty __wasm_apply_data_relocs\n");
1480+
return;
1481+
}
14751482
writeU8(os, WASM_OPCODE_END, "END");
14761483
}
14771484

1478-
createFunction(WasmSym::applyDataRelocs, bodyContent);
1485+
// __wasm_apply_data_relocs
1486+
// Function that applies relocations to data segment post-instantiation.
1487+
static WasmSignature nullSignature = {{}, {}};
1488+
auto def = symtab->addSyntheticFunction(
1489+
"__wasm_apply_data_relocs",
1490+
WASM_SYMBOL_VISIBILITY_DEFAULT | WASM_SYMBOL_EXPORTED,
1491+
make<SyntheticFunction>(nullSignature, "__wasm_apply_data_relocs"));
1492+
def->markLive();
1493+
1494+
createFunction(def, bodyContent);
14791495
}
14801496

14811497
void Writer::createApplyTLSRelocationsFunction() {
@@ -1771,8 +1787,6 @@ void Writer::run() {
17711787

17721788
if (!config->relocatable) {
17731789
// Create linker synthesized functions
1774-
if (WasmSym::applyDataRelocs)
1775-
createApplyDataRelocationsFunction();
17761790
if (WasmSym::applyGlobalRelocs)
17771791
createApplyGlobalRelocationsFunction();
17781792
if (WasmSym::applyTLSRelocs)

0 commit comments

Comments
 (0)