Index: lib/MC/MCExpr.cpp =================================================================== --- lib/MC/MCExpr.cpp +++ lib/MC/MCExpr.cpp @@ -566,6 +566,24 @@ A = B = nullptr; } +static bool canFold(const MCAssembler *Asm, const MCSymbolRefExpr *A, + const MCSymbolRefExpr *B, bool InSet) { + if (InSet) + return true; + + if (!Asm->getBackend().requiresDiffExpressionRelocations()) + return true; + + const MCSymbol &CheckSym = A ? A->getSymbol() : B->getSymbol(); + if (!CheckSym.isInSection()) + return true; + + if (!CheckSym.getSection().hasInstructions()) + return true; + + return false; +} + /// Evaluate the result of an add between (conceptually) two MCValues. /// /// This routine conceptually attempts to construct an MCValue: @@ -606,8 +624,7 @@ // the backend requires this to be emitted as individual relocations, unless // the InSet flag is set to get the current difference anyway (used for // example to calculate symbol sizes). - if (Asm && - (InSet || !Asm->getBackend().requiresDiffExpressionRelocations())) { + if (Asm && canFold(Asm, LHS_A, LHS_B, InSet)) { // First, fold out any differences which are fully resolved. By // reassociating terms in // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst). Index: test/DebugInfo/RISCV/relax-debug-frame.ll =================================================================== --- test/DebugInfo/RISCV/relax-debug-frame.ll +++ test/DebugInfo/RISCV/relax-debug-frame.ll @@ -2,10 +2,12 @@ ; RUN: | llvm-readobj -r | FileCheck -check-prefix=RELAX %s ; ; RELAX: .rela.{{eh|debug}}_frame { -; RELAX: R_RISCV_ADD32 -; RELAX: R_RISCV_SUB32 -; RELAX: R_RISCV_SET6 -; RELAX: R_RISCV_SUB6 +; RELAX-NOT: 0x0 R_RISCV_ADD32 +; RELAX-NOT: 0x0 R_RISCV_SUB32 +; RELAX: 0x20 R_RISCV_ADD32 +; RELAX: 0x20 R_RISCV_SUB32 +; RELAX: 0x25 R_RISCV_SET6 +; RELAX: 0x25 R_RISCV_SUB6 source_filename = "frame.c" ; Function Attrs: noinline nounwind optnone