Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.
/ swift-llvm Public archive

Commit d7a9e68

Browse files
zhuoweiMaxDesiatov
authored andcommitted
WebAssembly: properly handle MCBinaryExpr in aliased symbols
Swift uses aliased symbols to refer to offsets within metadata structures. eg ``` @"$s12SwiftPrivate28_stdlib_ShardedAtomicCounterVN" = alias %swift.type, bitcast (i32* getelementptr inbounds (<{ i8**, i32, <{ i32, i32, i32, i32, i32, i32, i32 }>*, i32, i32 }>, <{ i8**, i32, <{ i32, i32, i32, i32, i32, i32, i32 }>*, i32, i32 }>* @"$s12SwiftPrivate28_stdlib_ShardedAtomicCounterVMf", i32 0, i32 1) to %swift.type*) ``` My previous commit only got the alias to refer to the correct target symbol, but not at the right offset. This commit properly emits these symbols. With this change, print("hello world") works but print() with anything else is still broken.
1 parent 0c8f940 commit d7a9e68

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

lib/MC/WasmObjectWriter.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,23 @@ static bool isInSymtab(const MCSymbolWasm &Sym) {
11991199
return true;
12001200
}
12011201

1202+
// SwiftWasm: takes a MCSymbolWasm that's an alias expression of the form
1203+
// ((targetSymbol + constantA) - constantB) + constantC...)
1204+
// return the final offset from targetSymbol.
1205+
// if no offset, returns 0.
1206+
static int64_t getAliasedSymbolOffset(const MCSymbolWasm &Symbol,
1207+
const MCAsmLayout &Layout) {
1208+
if (!Symbol.isVariable())
1209+
return 0;
1210+
const MCExpr *Expr = Symbol.getVariableValue();
1211+
MCValue Res;
1212+
if (!Expr->evaluateAsRelocatable(Res, &Layout, nullptr)) {
1213+
Expr->dump();
1214+
report_fatal_error("Can't evaluate alias symbol expression");
1215+
}
1216+
return Res.getConstant();
1217+
}
1218+
12021219
uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
12031220
const MCAsmLayout &Layout) {
12041221
uint64_t StartOffset = W.OS.tell();
@@ -1519,8 +1536,16 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
15191536
LLVM_DEBUG(dbgs() << " -> index:" << WasmIndex << "\n");
15201537
} else if (ResolvedSym->isData()) {
15211538
assert(DataLocations.count(ResolvedSym) > 0);
1522-
const wasm::WasmDataReference &Ref =
1539+
// SwiftWasm: hack: grab the offset
1540+
// Swift has aliases of the form
1541+
// alias = ((symbol + constant) - constant)
1542+
// so we need to evaluate the constants here using MCExpr
1543+
// there's probably a proper way to do this.
1544+
int64_t Offset = getAliasedSymbolOffset(WS, Layout);
1545+
wasm::WasmDataReference Ref =
15231546
DataLocations.find(ResolvedSym)->second;
1547+
Ref.Offset += Offset;
1548+
Ref.Size -= Offset;
15241549
DataLocations[&WS] = Ref;
15251550
LLVM_DEBUG(dbgs() << " -> index:" << Ref.Segment << "\n");
15261551
} else {

0 commit comments

Comments
 (0)