[llvm] [Sframe] Support cfi_escape directives compatibly with gnu-gas (PR #161927)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 8 09:27:38 PDT 2025
================
@@ -211,6 +213,152 @@ class SFrameEmitterImpl {
return true;
}
+ // Technically, the escape data could be anything, but it is commonly a dwarf
+ // CFI program. Even then, it could contain an arbitrarily complicated Dwarf
+ // expression. Following gnu-gas, look for certain common cases that could
+ // invalidate an FDE, emit a warning for those sequences, and don't generate
+ // an FDE in those cases. Allow any that are known safe. It is likely that
+ // more thorough test cases could refine this code, but it handles the most
+ // important ones compatibly with gas.
+ bool isCFIEscapeSafe(SFrameFDE &FDE, const SFrameFRE &FRE,
+ const MCCFIInstruction &CFI) {
+ const MCAsmInfo *AI = Streamer.getContext().getAsmInfo();
+ DWARFDataExtractorSimple data(CFI.getValues(), AI->isLittleEndian(),
+ AI->getCodePointerSize());
+
+ // Normally, both alignment factors are extracted from the enclosing Dwarf
+ // FDE or CIE. We don't have one here. Alignments are used for scaling
+ // factors for ops like CFA_def_cfa_offset_sf. But this particular function
+ // is only interested in registers.
+ dwarf::CFIProgram P(/* CodeAlignmentFactor */ 1,
+ /* DataAlignmentFactor*/ 1,
+ Streamer.getContext().getTargetTriple().getArch());
+ uint64_t Offset = 0;
+ if (P.parse(data, &Offset, CFI.getValues().size())) {
+ // Not a parsable dwarf expression. Assume the worst.
+ Streamer.getContext().reportWarning(
+ CFI.getLoc(),
+ "skipping SFrame FDE; .cfi_escape with unknown effects");
+ return false;
+ }
+
+ // This loop deals with are dwarf::CFIProgram::Instructions. Everywhere
+ // else this file deals with MCCFIInstructions..
+ for (const dwarf::CFIProgram::Instruction &I : P) {
+ switch (I.Opcode) {
+ case dwarf::DW_CFA_nop:
+ break;
+ case dwarf::DW_CFA_val_offset: {
+ // First argument is a register. Anything that touches CFA, FP, or RA is
+ // a problem, but allow others through. As an even more special case,
+ // allow SP + 0.
+ auto Reg = I.getOperandAsUnsigned(P, 0);
+ if (!Reg) {
+ Streamer.getContext().reportWarning(
+ CFI.getLoc(),
+ "skipping SFrame FDE; .cfi_escape with unknown effects");
+ }
+ bool SPOk = true;
+ if (*Reg == SPReg) {
+ auto Opnd = I.getOperandAsSigned(P, 1);
+ if (!Opnd || *Opnd != 0)
+ SPOk = false;
+ }
+ if (!SPOk || *Reg == RAReg || *Reg == FPReg) {
+ StringRef RN = *Reg == SPReg
+ ? "SP reg "
+ : (*Reg == FPReg ? "FP reg " : "RA reg ");
+ Streamer.getContext().reportWarning(
+ CFI.getLoc(),
+ Twine(
+ "skipping SFrame FDE; .cfi_escape DW_CFA_val_offset with ") +
+ RN + Twine(*Reg));
+ return false;
+ }
+ } break;
+ case dwarf::DW_CFA_expression: {
+ // First argument is a register. Anything that touches CFA, FP, or RA is
+ // a problem, but allow others through.
+ auto Reg = I.getOperandAsUnsigned(P, 0);
+ if (!Reg) {
+ Streamer.getContext().reportWarning(
+ CFI.getLoc(),
+ "skipping SFrame FDE; .cfi_escape with unknown effects");
+ return false;
+ }
+ if (*Reg == SPReg || *Reg == RAReg || *Reg == FPReg) {
+ StringRef RN = *Reg == SPReg
+ ? "SP reg "
+ : (*Reg == FPReg ? "FP reg " : "RA reg ");
+ Streamer.getContext().reportWarning(
+ CFI.getLoc(),
+ Twine(
+ "skipping SFrame FDE; .cfi_escape DW_CFA_expression with ") +
+ RN + Twine(*Reg));
+ return false;
+ }
+ } break;
+ case dwarf::DW_CFA_GNU_args_size: {
+ auto Size = I.getOperandAsSigned(P, 0);
+ // Zero size doesn't affect the cfa.
+ if (Size && *Size == 0)
+ break;
+ if (FRE.Info.getBaseRegister() != BaseReg::FP) {
+ Streamer.getContext().reportWarning(
+ CFI.getLoc(),
+ Twine("skipping SFrame FDE; .cfi_escape DW_CFA_GNU_args_size "
+ "with non frame-pointer CFA"));
+ return false;
+ }
+ } break;
+ // Cases that gas doesn't specially handle. TODO: Some of these could be
+ // analyzed and handled instead of just punting. But these are uncommon,
+ // or should be written as normal cfi directives. Some will need fixes to
+ // the scaling factor.
+ case dwarf::DW_CFA_advance_loc:
+ case dwarf::DW_CFA_offset:
+ case dwarf::DW_CFA_restore:
+ case dwarf::DW_CFA_set_loc:
+ case dwarf::DW_CFA_advance_loc1:
+ case dwarf::DW_CFA_advance_loc2:
+ case dwarf::DW_CFA_advance_loc4:
+ case dwarf::DW_CFA_offset_extended:
+ case dwarf::DW_CFA_restore_extended:
+ case dwarf::DW_CFA_undefined:
+ case dwarf::DW_CFA_same_value:
+ case dwarf::DW_CFA_register:
+ case dwarf::DW_CFA_remember_state:
+ case dwarf::DW_CFA_restore_state:
+ case dwarf::DW_CFA_def_cfa:
+ case dwarf::DW_CFA_def_cfa_register:
+ case dwarf::DW_CFA_def_cfa_offset:
+ case dwarf::DW_CFA_def_cfa_expression:
+ case dwarf::DW_CFA_offset_extended_sf:
+ case dwarf::DW_CFA_def_cfa_sf:
+ case dwarf::DW_CFA_def_cfa_offset_sf:
+ case dwarf::DW_CFA_val_offset_sf:
+ case dwarf::DW_CFA_val_expression:
+ case dwarf::DW_CFA_MIPS_advance_loc8:
+ case dwarf::DW_CFA_AARCH64_negate_ra_state_with_pc:
+ case dwarf::DW_CFA_AARCH64_negate_ra_state:
+ case dwarf::DW_CFA_LLVM_def_aspace_cfa:
+ case dwarf::DW_CFA_LLVM_def_aspace_cfa_sf:
+ Streamer.getContext().reportWarning(
+ CFI.getLoc(), "skipping SFrame FDE; .cfi_escape "
+ "CFA expression with unknown side effects");
+ return false;
----------------
weiguozhi wrote:
What's the behavior of gcc/gas in this case?
https://github.com/llvm/llvm-project/pull/161927
More information about the llvm-commits
mailing list