Skip to content
This repository was archived by the owner on Oct 24, 2019. It is now read-only.

Commit 5ecc83a

Browse files
committed
WebAssembly: attempt to fix non-SymbolExpr relocations
Also try hard to extract a symbol from a MCExpr when writing relocations I doubt this is the right way, but hey it emits an object now
1 parent 5486b10 commit 5ecc83a

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lib/MC/WasmObjectWriter.cpp

+25-2
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,8 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
477477

478478
if (SymA && SymA->isVariable()) {
479479
const MCExpr *Expr = SymA->getVariableValue();
480-
const auto *Inner = cast<MCSymbolRefExpr>(Expr);
481-
if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
480+
const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
481+
if (Inner && Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
482482
llvm_unreachable("weakref used in reloc not yet implemented");
483483
}
484484

@@ -564,9 +564,32 @@ static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
564564
Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
565565
}
566566

567+
static const MCSymbolRefExpr* pullSymbol(const MCExpr *TheExpr) {
568+
if (!TheExpr) return nullptr;
569+
const MCSymbolRefExpr* S = dyn_cast<MCSymbolRefExpr>(TheExpr);
570+
if (S) return S;
571+
const MCBinaryExpr* Expr = dyn_cast<MCBinaryExpr>(TheExpr);
572+
if (!Expr) return nullptr;
573+
S = dyn_cast_or_null<MCSymbolRefExpr>(Expr->getLHS());
574+
if (S) return S;
575+
S = dyn_cast_or_null<MCSymbolRefExpr>(Expr->getRHS());
576+
if (S) return S;
577+
S = pullSymbol(Expr->getLHS());
578+
if (S) return S;
579+
S = pullSymbol(Expr->getRHS());
580+
if (S) return S;
581+
return nullptr;
582+
}
583+
567584
static const MCSymbolWasm *ResolveSymbol(const MCSymbolWasm &Symbol) {
568585
if (Symbol.isVariable()) {
569586
const MCExpr *Expr = Symbol.getVariableValue();
587+
if (Expr->getKind() == MCExpr::Binary) {
588+
Expr = pullSymbol(Expr);
589+
if (!Expr) {
590+
llvm_unreachable("can't find a symbol in binary expression\n");
591+
}
592+
}
570593
auto *Inner = cast<MCSymbolRefExpr>(Expr);
571594
return cast<MCSymbolWasm>(&Inner->getSymbol());
572595
}

0 commit comments

Comments
 (0)