Skip to content

Commit f87de2a

Browse files
authored
wasm-emscripten-finalize: Handle relocatable code in AsmConstWalker (#2035)
When replacing the first argument to an asm call, allow more complex expressions for expressing the address. This fixes the case where the first argument might be the result of adding a constant to __memory_base.
1 parent b2161e3 commit f87de2a

File tree

4 files changed

+40
-39
lines changed

4 files changed

+40
-39
lines changed

CHANGELOG.md

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

18+
v81
19+
---
20+
21+
- Fix AsmConstWalker handling of string address in arg0 with -fPIC code
22+
1823
v80
1924
---
2025

src/wasm/wasm-emscripten.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -525,30 +525,37 @@ void AsmConstWalker::visitCall(Call* curr) {
525525
auto baseSig = getSig(curr);
526526
auto sig = fixupNameWithSig(curr->target, baseSig);
527527
auto* arg = curr->operands[0];
528-
if (auto* get = arg->dynCast<GetLocal>()) {
529-
// The argument may be a local.get, in which case, the last set in this
530-
// basic block has the value.
531-
auto* set = sets[get->index];
532-
if (set) {
533-
assert(set->index == get->index);
534-
arg = set->value;
535-
}
536-
} else if (auto* value = arg->dynCast<Binary>()) {
537-
// In the dynamic linking case the address of the string constant
538-
// is the result of adding its offset to __memory_base.
539-
// In this case are only looking for the offset with the data segment so
540-
// the RHS of the addition is just what we want.
541-
assert(value->op == AddInt32);
542-
arg = value->right;
543-
}
544-
545-
auto* value = arg->dynCast<Const>();
546-
if (!value)
547-
Fatal() << "Unexpected arg0 type (" << getExpressionName(arg)
528+
while (!arg->dynCast<Const>()) {
529+
if (auto* get = arg->dynCast<GetLocal>()) {
530+
// The argument may be a local.get, in which case, the last set in this
531+
// basic block has the value.
532+
auto* set = sets[get->index];
533+
if (set) {
534+
assert(set->index == get->index);
535+
arg = set->value;
536+
}
537+
} else if (auto* value = arg->dynCast<Binary>()) {
538+
// In the dynamic linking case the address of the string constant
539+
// is the result of adding its offset to __memory_base.
540+
// In this case are only looking for the offset with the data segment so
541+
// the RHS of the addition is just what we want.
542+
assert(value->op == AddInt32);
543+
arg = value->right;
544+
} else {
545+
if (!value) {
546+
Fatal() << "Unexpected arg0 type (" << getExpressionName(arg)
548547
<< ") in call to to: " << import->base;
548+
}
549+
}
550+
}
551+
552+
auto* value = arg->cast<Const>();
549553
auto code = codeForConstAddr(wasm, segmentOffsets, value);
550-
value->value = idLiteralForCode(code);
551554
sigsForCode[code].insert(sig);
555+
556+
// Replace the first argument to the call with a Const index
557+
Builder builder(wasm);
558+
curr->operands[0] = builder.makeConst(idLiteralForCode(code));
552559
}
553560
}
554561

test/lld/em_asm_O0.wast.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@
3434
)
3535
)
3636
(local.set $t1
37-
(i32.const 2)
37+
(i32.const 621)
3838
)
3939
(local.set $t2
40-
(i32.const 1)
40+
(i32.const 601)
4141
)
4242
(drop
4343
(call $emscripten_asm_const_ii
44-
(local.get $t1)
44+
(i32.const 2)
4545
(call $emscripten_asm_const_iii
46-
(local.get $t2)
46+
(i32.const 1)
4747
(i32.const 13)
4848
(i32.const 27)
4949
)

test/lld/em_asm_shared.wast.out

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@
4848
)
4949
(drop
5050
(call $emscripten_asm_const_iii
51-
(i32.add
52-
(local.tee $1
53-
(global.get $gimport$3)
54-
)
55-
(i32.const 0)
56-
)
51+
(i32.const 0)
5752
(i32.add
5853
(local.get $0)
5954
(i32.const 24)
@@ -80,10 +75,7 @@
8075
)
8176
(local.tee $2
8277
(call $emscripten_asm_const_iii
83-
(i32.add
84-
(local.get $1)
85-
(i32.const 1)
86-
)
78+
(i32.const 1)
8779
(i32.add
8880
(local.get $0)
8981
(i32.const 24)
@@ -101,10 +93,7 @@
10193
)
10294
(drop
10395
(call $emscripten_asm_const_iii
104-
(i32.add
105-
(local.get $1)
106-
(i32.const 2)
107-
)
96+
(i32.const 2)
10897
(i32.add
10998
(local.get $0)
11099
(i32.const 24)

0 commit comments

Comments
 (0)