Skip to content

Commit

Permalink
wasm-emscripten-finalize: Internalize mutable __stack_pointer import (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
sbc100 committed Jul 10, 2019
1 parent 3f46ac2 commit 90449a5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ full changeset diff at the end of each section.
Current Trunk
-------------

- wasm-emscripten-finalize: For -pie binaries that import a mutable stack
pointer we internalize this an import it as immutable.

v86
---

Expand Down
1 change: 1 addition & 0 deletions src/tools/wasm-emscripten-finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ int main(int argc, const char* argv[]) {
generator.generatePostInstantiateFunction();
} else {
generator.generateRuntimeFunctions();
generator.internalizeStackPointerGlobal();
generator.generateMemoryGrowthFunction();
// For side modules these gets called via __post_instantiate
if (Function* F = generator.generateAssignGOTEntriesFunction()) {
Expand Down
4 changes: 4 additions & 0 deletions src/wasm-emscripten.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class EmscriptenGlueGenerator {
// and restore functions.
void replaceStackPointerGlobal();

// Remove the import of a mutable __stack_pointer and instead initialize the
// stack pointer from an immutable import.
void internalizeStackPointerGlobal();

std::string
generateEmscriptenMetadata(Address staticBump,
std::vector<Name> const& initializerFunctions);
Expand Down
26 changes: 26 additions & 0 deletions src/wasm/wasm-emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,32 @@ struct RemoveStackPointer : public PostWalker<RemoveStackPointer> {
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) {
Expand Down

0 comments on commit 90449a5

Please sign in to comment.