From 39eae1d5772099282af656ee2bb0ce388e2e7268 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 10 Apr 2019 11:24:00 -0700 Subject: Handle relocatable code in AsmConstWalker (#1992) In relocatable code the constant offset might be relative to __memory_base. --- src/wasm/wasm-emscripten.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/wasm/wasm-emscripten.cpp b/src/wasm/wasm-emscripten.cpp index 95566f5b9..1b40b871e 100644 --- a/src/wasm/wasm-emscripten.cpp +++ b/src/wasm/wasm-emscripten.cpp @@ -509,20 +509,33 @@ void AsmConstWalker::visitSetLocal(SetLocal* curr) { void AsmConstWalker::visitCall(Call* curr) { auto* import = wasm.getFunction(curr->target); + // Find calls to emscripten_asm_const* functions whose first argument is + // is always a string constant. if (import->imported() && import->base.hasSubstring(EMSCRIPTEN_ASM_CONST)) { auto baseSig = getSig(curr); auto sig = fixupNameWithSig(curr->target, baseSig); auto* arg = curr->operands[0]; - // The argument may be a get, in which case, the last set in this basic block - // has the value. if (auto* get = arg->dynCast()) { + // The argument may be a local.get, in which case, the last set in this + // basic block has the value. auto* set = sets[get->index]; if (set) { assert(set->index == get->index); arg = set->value; } - } - auto* value = arg->cast(); + } else if (auto* value = arg->dynCast()) { + // In the dynamic linking case the address of the string constant + // is the result of adding its offset to __memory_base. + // In this case are only looking for the offset with the data segment so + // the RHS of the addition is just what we want. + assert(value->op == AddInt32); + arg = value->right; + } + + auto* value = arg->dynCast(); + if (!value) + Fatal() << "Unexpected arg0 type (" << getExpressionName(arg) + << ") in call to to: " << import->base; auto code = codeForConstAddr(wasm, segmentOffsets, value); value->value = idLiteralForCode(code); sigsForCode[code].insert(sig); -- cgit v1.2.3