@@ -477,8 +477,8 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
477
477
478
478
if (SymA && SymA->isVariable ()) {
479
479
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)
482
482
llvm_unreachable (" weakref used in reloc not yet implemented" );
483
483
}
484
484
@@ -564,9 +564,32 @@ static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
564
564
Stream.pwrite ((char *)Buffer, sizeof (Buffer), Offset);
565
565
}
566
566
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
+
567
584
static const MCSymbolWasm *ResolveSymbol (const MCSymbolWasm &Symbol) {
568
585
if (Symbol.isVariable ()) {
569
586
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
+ }
570
593
auto *Inner = cast<MCSymbolRefExpr>(Expr);
571
594
return cast<MCSymbolWasm>(&Inner->getSymbol ());
572
595
}
0 commit comments