diff options
-rw-r--r-- | src/wasm-emscripten.cpp | 11 | ||||
-rw-r--r-- | src/wasm-emscripten.h | 2 | ||||
-rw-r--r-- | src/wasm-linker.cpp | 24 | ||||
-rw-r--r-- | src/wasm-linker.h | 3 |
4 files changed, 21 insertions, 19 deletions
diff --git a/src/wasm-emscripten.cpp b/src/wasm-emscripten.cpp index 4be5e6b43..5fa27b4f1 100644 --- a/src/wasm-emscripten.cpp +++ b/src/wasm-emscripten.cpp @@ -86,17 +86,6 @@ std::vector<Function*> makeDynCallThunks(Module& wasm, std::vector<Name> const& return generatedFunctions; } -void addObjectImports(Module& wasm, std::unordered_set<cashew::IString> const& objectImports) { - for (Name name : objectImports) { - auto import = new Import; - import->name = import->base = name; - import->module = ENV; - import->kind = ExternalKind::Global; - import->globalType = i32; - wasm.addImport(import); - } -} - struct AsmConstWalker : public PostWalker<AsmConstWalker, Visitor<AsmConstWalker>> { Module& wasm; std::unordered_map<Address, Address> segmentsByAddress; // address => segment index diff --git a/src/wasm-emscripten.h b/src/wasm-emscripten.h index f1aa15129..07c470898 100644 --- a/src/wasm-emscripten.h +++ b/src/wasm-emscripten.h @@ -29,8 +29,6 @@ void generateMemoryGrowthFunction(Module&); // signature in the indirect function table. std::vector<Function*> makeDynCallThunks(Module& wasm, std::vector<Name> const& tableSegmentData); -void addObjectImports(Module& wasm, std::unordered_set<cashew::IString> const& objectImports); - void generateEmscriptenMetadata(std::ostream& o, Module& wasm, std::unordered_map<Address, Address> segmentsByAddress, diff --git a/src/wasm-linker.cpp b/src/wasm-linker.cpp index 76bf8942d..a319af889 100644 --- a/src/wasm-linker.cpp +++ b/src/wasm-linker.cpp @@ -48,7 +48,7 @@ void Linker::placeStackPointer(Address stackAllocation) { } } -void Linker::ensureImport(Name target, std::string signature) { +void Linker::ensureFunctionImport(Name target, std::string signature) { if (!out.wasm.checkImport(target)) { auto import = new Import; import->name = import->base = target; @@ -59,13 +59,24 @@ void Linker::ensureImport(Name target, std::string signature) { } } +void Linker::ensureObjectImport(Name target) { + if (!out.wasm.checkImport(target)) { + auto import = new Import; + import->name = import->base = target; + import->module = ENV; + import->kind = ExternalKind::Global; + import->globalType = i32; + out.wasm.addImport(import); + } +} + void Linker::layout() { // Convert calls to undefined functions to call_imports for (const auto& f : out.undefinedFunctionCalls) { Name target = f.first; if (!out.symbolInfo.undefinedFunctions.count(target)) continue; // Create an import for the target if necessary. - ensureImport(target, getSig(*f.second.begin())); + ensureFunctionImport(target, getSig(*f.second.begin())); // Change each call. The target is the same since it's still the name. // Delete and re-allocate the Expression as CallImport to avoid undefined // behavior. @@ -129,6 +140,11 @@ void Linker::layout() { out.wasm.addExport(memoryExport.release()); } + // Add imports for any imported objects + for (const auto& obj : out.symbolInfo.importedObjects) { + ensureObjectImport(obj); + } + // XXX For now, export all functions marked .globl. for (Name name : out.globls) exportFunction(name, false); for (Name name : out.initializerFunctions) exportFunction(name, true); @@ -329,8 +345,6 @@ void Linker::emscriptenGlue(std::ostream& o) { exportFunction(f->name, true); } - emscripten::addObjectImports(out.wasm, out.symbolInfo.importedObjects); - auto staticBump = nextStatic - globalBase; emscripten::generateEmscriptenMetadata(o, out.wasm, segmentsByAddress, staticBump, out.initializerFunctions); } @@ -388,7 +402,7 @@ void Linker::makeDummyFunction() { Function* Linker::getImportThunk(Name name, const FunctionType* funcType) { Name thunkName = std::string("__importThunk_") + name.c_str(); if (Function* thunk = out.wasm.checkFunction(thunkName)) return thunk; - ensureImport(name, getSig(funcType)); + ensureFunctionImport(name, getSig(funcType)); wasm::Builder wasmBuilder(out.wasm); std::vector<NameType> params; Index p = 0; diff --git a/src/wasm-linker.h b/src/wasm-linker.h index 76ea62d7d..608576190 100644 --- a/src/wasm-linker.h +++ b/src/wasm-linker.h @@ -271,7 +271,8 @@ class Linker { // relocation for it to point to the top of the stack. void placeStackPointer(Address stackAllocation); - void ensureImport(Name target, std::string signature); + void ensureFunctionImport(Name target, std::string signature); + void ensureObjectImport(Name target); // Makes sure the table has a single segment, with offset 0, // to which we can add content. |