Skip to content

Commit 90449a5

Browse files
authored
wasm-emscripten-finalize: Internalize mutable __stack_pointer import (#2213)
I'm working on a change to lld that will cause `-pie` binaries to import __stack_pointer, just like -shared do already. Because we don't yet support mutable globals everywhere this change will internalize the import and create a new immutable import that is used to initialize the internal one. This change is part of the fix for: emscripten-core/emscripten#8915
1 parent 3f46ac2 commit 90449a5

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ full changeset diff at the end of each section.
1515
Current Trunk
1616
-------------
1717

18+
- wasm-emscripten-finalize: For -pie binaries that import a mutable stack
19+
pointer we internalize this an import it as immutable.
20+
1821
v86
1922
---
2023

src/tools/wasm-emscripten-finalize.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ int main(int argc, const char* argv[]) {
205205
generator.generatePostInstantiateFunction();
206206
} else {
207207
generator.generateRuntimeFunctions();
208+
generator.internalizeStackPointerGlobal();
208209
generator.generateMemoryGrowthFunction();
209210
// For side modules these gets called via __post_instantiate
210211
if (Function* F = generator.generateAssignGOTEntriesFunction()) {

src/wasm-emscripten.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class EmscriptenGlueGenerator {
4444
// and restore functions.
4545
void replaceStackPointerGlobal();
4646

47+
// Remove the import of a mutable __stack_pointer and instead initialize the
48+
// stack pointer from an immutable import.
49+
void internalizeStackPointerGlobal();
50+
4751
std::string
4852
generateEmscriptenMetadata(Address staticBump,
4953
std::vector<Name> const& initializerFunctions);

src/wasm/wasm-emscripten.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,32 @@ struct RemoveStackPointer : public PostWalker<RemoveStackPointer> {
397397
Global* stackPointer;
398398
};
399399

400+
// lld can sometimes produce a build with an imported mutable __stack_pointer
401+
// (i.e. when linking with -fpie). This method internalizes the
402+
// __stack_pointer and initializes it from an immutable global instead.
403+
// For -shared builds we instead call replaceStackPointerGlobal.
404+
void EmscriptenGlueGenerator::internalizeStackPointerGlobal() {
405+
Global* stackPointer = getStackPointerGlobal();
406+
if (!stackPointer || !stackPointer->imported() || !stackPointer->mutable_) {
407+
return;
408+
}
409+
410+
Name internalName = stackPointer->name;
411+
Name externalName = internalName.c_str() + std::string("_import");
412+
413+
// Rename the imported global, and make it immutable
414+
stackPointer->name = externalName;
415+
stackPointer->mutable_ = false;
416+
wasm.updateMaps();
417+
418+
// Create a new global with the old name that is not imported.
419+
Builder builder(wasm);
420+
auto* init = builder.makeGlobalGet(externalName, stackPointer->type);
421+
auto* sp = builder.makeGlobal(
422+
internalName, stackPointer->type, init, Builder::Mutable);
423+
wasm.addGlobal(sp);
424+
}
425+
400426
void EmscriptenGlueGenerator::replaceStackPointerGlobal() {
401427
Global* stackPointer = getStackPointerGlobal();
402428
if (!stackPointer) {

0 commit comments

Comments
 (0)