[PATCH] D153167: [MC] Reject CFI advance_loc separated by a non-private label for Mach-O

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 16 13:12:00 PDT 2023


MaskRay created this revision.
MaskRay added reviewers: efriedma, int3, jyknight, smeenai.
Herald added a subscriber: hiraditya.
Herald added a project: All.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Due to Mach-O's .subsections_via_symbols mechanism, non-private labels cannot
appear between .cfi_startproc/.cfi_endproc. Compilers do not produce such
labels, but hand-written assembly may. Give an error. Unfortunately,
emitDwarfAdvanceFrameAddr generated MCExpr doesn't have location
informatin.

Note: evaluateKnownAbsolute is to force folding A-B to a constant even if A and
B are separate by a non-private label. The function is a workaround for some
Mach-O assembl issues and should generally be avoided.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153167

Files:
  llvm/lib/MC/MCAssembler.cpp
  llvm/test/MC/MachO/cfi-advance-loc-err.s


Index: llvm/test/MC/MachO/cfi-advance-loc-err.s
===================================================================
--- /dev/null
+++ llvm/test/MC/MachO/cfi-advance-loc-err.s
@@ -0,0 +1,21 @@
+# RUN: not llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o /dev/null 2>&1 | FileCheck %s --implicit-check-not=error:
+
+# CHECK-COUNT-4: <unknown>:0: error: invalid CFI advance_loc expression
+
+.section __TEXT,__text
+.globl _foo
+_foo:
+  .cfi_startproc
+  subq $8, %rsp
+  .cfi_adjust_cfa_offset 8
+  subq $8, %rsp
+  .cfi_adjust_cfa_offset 8
+
+tmp0: # non-private label cannot appear here
+  addq $8, %rsp
+  .cfi_adjust_cfa_offset -8
+.tmp1: # non-private label cannot appear here
+  addq $8, %rsp
+  .cfi_adjust_cfa_offset -8
+  retq
+  .cfi_endproc
Index: llvm/lib/MC/MCAssembler.cpp
===================================================================
--- llvm/lib/MC/MCAssembler.cpp
+++ llvm/lib/MC/MCAssembler.cpp
@@ -1110,16 +1110,20 @@
     return WasRelaxed;
 
   MCContext &Context = Layout.getAssembler().getContext();
-  uint64_t OldSize = DF.getContents().size();
-  int64_t AddrDelta;
-  bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
-  assert(Abs && "We created call frame with an invalid expression");
-  (void) Abs;
+  int64_t Value;
+  bool Abs = DF.getAddrDelta().evaluateAsAbsolute(Value, Layout);
+  if (!Abs) {
+    getContext().reportError(DF.getAddrDelta().getLoc(),
+                             "invalid CFI advance_loc expression");
+    return false;
+  }
+
   SmallVectorImpl<char> &Data = DF.getContents();
+  uint64_t OldSize = Data.size();
   Data.clear();
   DF.getFixups().clear();
 
-  MCDwarfFrameEmitter::encodeAdvanceLoc(Context, AddrDelta, Data);
+  MCDwarfFrameEmitter::encodeAdvanceLoc(Context, Value, Data);
   return OldSize != Data.size();
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153167.532276.patch
Type: text/x-patch
Size: 1819 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230616/80606c15/attachment.bin>


More information about the llvm-commits mailing list