@@ -475,14 +475,6 @@ bool MCExpr::evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const {
475
475
bool MCExpr::evaluateAsValue (MCValue &Res, const MCAssembler &Asm) const {
476
476
return evaluateAsRelocatableImpl (Res, &Asm, true );
477
477
}
478
- static bool canExpand (const MCSymbol &Sym, bool InSet) {
479
- if (Sym.isWeakExternal ())
480
- return false ;
481
-
482
- if (InSet)
483
- return true ;
484
- return !Sym.isInSection ();
485
- }
486
478
487
479
bool MCExpr::evaluateAsRelocatableImpl (MCValue &Res, const MCAssembler *Asm,
488
480
bool InSet) const {
@@ -500,7 +492,10 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
500
492
const auto Kind = SRE->getKind ();
501
493
bool Layout = Asm && Asm->hasLayout ();
502
494
503
- // Evaluate recursively if this is a variable.
495
+ // If the symbol is equated, resolve the inner expression.
496
+ // However, when two IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY symbols reference
497
+ // each other, we retain the equated symbol to avoid a cyclic definition
498
+ // error.
504
499
if (Sym.isResolving ()) {
505
500
if (Asm && Asm->hasFinalLayout ()) {
506
501
Asm->getContext ().reportError (
@@ -511,13 +506,20 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
511
506
return false ;
512
507
}
513
508
if (Sym.isVariable () && (Kind == MCSymbolRefExpr::VK_None || Layout) &&
514
- canExpand ( Sym, InSet )) {
509
+ ! Sym. isWeakExternal ( )) {
515
510
Sym.setIsResolving (true );
516
511
auto _ = make_scope_exit ([&] { Sym.setIsResolving (false ); });
517
512
bool IsMachO =
518
513
Asm && Asm->getContext ().getAsmInfo ()->hasSubsectionsViaSymbols ();
519
- if (Sym.getVariableValue ()->evaluateAsRelocatableImpl (Res, Asm,
520
- InSet || IsMachO)) {
514
+ if (!Sym.getVariableValue ()->evaluateAsRelocatableImpl (Res, Asm,
515
+ InSet || IsMachO))
516
+ return false ;
517
+ // When generating relocations, if Sym resolves to a symbol relative to a
518
+ // section, relocations are generated against Sym. Treat label differences
519
+ // as constants.
520
+ auto *A = Res.getAddSym ();
521
+ auto *B = Res.getSubSym ();
522
+ if (InSet || !(A && !B && A->isInSection ())) {
521
523
if (Kind != MCSymbolRefExpr::VK_None) {
522
524
if (Res.isAbsolute ()) {
523
525
Res = MCValue::get (&Sym, nullptr , 0 , Kind);
@@ -534,8 +536,6 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
534
536
if (!IsMachO)
535
537
return true ;
536
538
537
- auto *A = Res.getAddSym ();
538
- auto *B = Res.getSubSym ();
539
539
// FIXME: This is small hack. Given
540
540
// a = b + 4
541
541
// .long a
@@ -548,8 +548,6 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
548
548
// Allows aliases with zero offset.
549
549
if (Res.getConstant () == 0 && (!A || !B))
550
550
return true ;
551
- } else {
552
- return false ;
553
551
}
554
552
}
555
553
0 commit comments