diff options
author | Sam Clegg <sbc@chromium.org> | 2019-07-10 10:37:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-10 10:37:33 -0700 |
commit | 90449a5699567283586460aa127c311f72f81704 (patch) | |
tree | 928e59695bd57f37b25787dc3f09c360d41bd1a5 /src/wasm/wasm-emscripten.cpp | |
parent | 3f46ac22ac0d682f50480b997597e40660dd7a6a (diff) | |
download | binaryen-90449a5699567283586460aa127c311f72f81704.tar.gz binaryen-90449a5699567283586460aa127c311f72f81704.tar.bz2 binaryen-90449a5699567283586460aa127c311f72f81704.zip |
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:
https://github.com/emscripten-core/emscripten/issues/8915
Diffstat (limited to 'src/wasm/wasm-emscripten.cpp')
-rw-r--r-- | src/wasm/wasm-emscripten.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/wasm/wasm-emscripten.cpp b/src/wasm/wasm-emscripten.cpp index 00a3eb6b7..8463354e8 100644 --- a/src/wasm/wasm-emscripten.cpp +++ b/src/wasm/wasm-emscripten.cpp @@ -397,6 +397,32 @@ private: Global* stackPointer; }; +// lld can sometimes produce a build with an imported mutable __stack_pointer +// (i.e. when linking with -fpie). This method internalizes the +// __stack_pointer and initializes it from an immutable global instead. +// For -shared builds we instead call replaceStackPointerGlobal. +void EmscriptenGlueGenerator::internalizeStackPointerGlobal() { + Global* stackPointer = getStackPointerGlobal(); + if (!stackPointer || !stackPointer->imported() || !stackPointer->mutable_) { + return; + } + + Name internalName = stackPointer->name; + Name externalName = internalName.c_str() + std::string("_import"); + + // Rename the imported global, and make it immutable + stackPointer->name = externalName; + stackPointer->mutable_ = false; + wasm.updateMaps(); + + // Create a new global with the old name that is not imported. + Builder builder(wasm); + auto* init = builder.makeGlobalGet(externalName, stackPointer->type); + auto* sp = builder.makeGlobal( + internalName, stackPointer->type, init, Builder::Mutable); + wasm.addGlobal(sp); +} + void EmscriptenGlueGenerator::replaceStackPointerGlobal() { Global* stackPointer = getStackPointerGlobal(); if (!stackPointer) { |