@@ -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+
567584static 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