Start adding basic support for emitting the call frame instructions.
authorRafael Espindola <rafael.espindola@gmail.com>
Tue, 28 Dec 2010 04:15:37 +0000 (04:15 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Tue, 28 Dec 2010 04:15:37 +0000 (04:15 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122590 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/MC/MCDwarf.h
lib/MC/MCDwarf.cpp
lib/MC/MCStreamer.cpp
test/MC/ELF/cfi-def-cfa-offset.s [new file with mode: 0644]

index 303376f16e1bed8af938b622cc3637a873d9434b..26c28294abed8106a21a4af71f89cda60e19fec2 100644 (file)
@@ -16,6 +16,7 @@
 #define LLVM_MC_MCDWARF_H
 
 #include "llvm/ADT/StringRef.h"
+#include "llvm/CodeGen/MachineLocation.h" // FIXME
 #include "llvm/MC/MCObjectWriter.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/Dwarf.h"
@@ -227,10 +228,13 @@ namespace llvm {
   };
 
   struct MCDwarfFrameInfo {
+    MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0), Moves(),
+                         PersonalityEncoding(0), LsdaEncoding(0) {}
     MCSymbol *Begin;
     MCSymbol *End;
     const MCSymbol *Personality;
     const MCSymbol *Lsda;
+    std::vector<MachineMove> Moves;
     unsigned PersonalityEncoding;
     unsigned LsdaEncoding;
   };
index cd5141083e361014124af972cb3128194c217d86..6e59a58b04da41351af50a8f00bc552a4c0eb5d5 100644 (file)
@@ -460,9 +460,11 @@ static void EmitFrameMoves(MCStreamer &streamer,
     if (BaseLabel && Label) {
       MCSymbol *ThisSym = Label;
       if (ThisSym != BaseLabel) {
+        // FIXME: We should relax this instead of using a DW_CFA_advance_loc4
+        // for every address change!
         streamer.EmitIntValue(dwarf::DW_CFA_advance_loc4, 1);
         const MCExpr *Length = MakeStartMinusEndExpr(streamer, *BaseLabel,
-                                                     *ThisSym, 4);
+                                                     *ThisSym, 0);
         streamer.EmitValue(Length, 4);
         BaseLabel = ThisSym;
       }
@@ -672,6 +674,8 @@ static MCSymbol *EmitFDE(MCStreamer &streamer,
   streamer.EmitLabel(augmentationEnd);
   // Call Frame Instructions
 
+  EmitFrameMoves(streamer, frame.Moves, frame.Begin, true);
+
   // Padding
   streamer.EmitValueToAlignment(4);
 
index 922a40ce3e712f9ae80a110c3917d375d7193695..048e8866bde3222fb20e4510b87e7d7087f3718c 100644 (file)
@@ -157,7 +157,7 @@ bool MCStreamer::EmitCFIStartProc() {
     report_fatal_error("Starting a frame before finishing the previous one!");
     return true;
   }
-  MCDwarfFrameInfo Frame = {0, 0, 0, 0, 0, 0};
+  MCDwarfFrameInfo Frame;
   Frame.Begin = getContext().CreateTempSymbol();
   EmitLabel(Frame.Begin);
   FrameInfos.push_back(Frame);
@@ -174,6 +174,13 @@ bool MCStreamer::EmitCFIEndProc() {
 
 bool MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
   EnsureValidFrame();
+  MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
+  MCSymbol *Label = getContext().CreateTempSymbol();
+  EmitLabel(Label);
+  MachineLocation Dest(MachineLocation::VirtualFP);
+  MachineLocation Source(MachineLocation::VirtualFP, -Offset);
+  MachineMove Move(Label, Dest, Source);
+  CurFrame->Moves.push_back(Move);
   return false;
 }
 
diff --git a/test/MC/ELF/cfi-def-cfa-offset.s b/test/MC/ELF/cfi-def-cfa-offset.s
new file mode 100644 (file)
index 0000000..b75cc12
--- /dev/null
@@ -0,0 +1,49 @@
+// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | elf-dump  --dump-section-data | FileCheck %s
+
+f:
+       .cfi_startproc
+       subq    $8, %rsp
+       .cfi_def_cfa_offset 16
+        nop
+       addq    $8, %rsp
+       .cfi_def_cfa_offset 8
+       ret
+       .cfi_endproc
+
+// FIXME: This is a correct but really inefficient coding since
+// we use a CFA_advance_loc4 for every address change!
+
+// CHECK:      # Section 0x00000004
+// CHECK-NEXT: (('sh_name', 0x00000012) # '.eh_frame'
+// CHECK-NEXT:  ('sh_type', 0x00000001)
+// CHECK-NEXT:  ('sh_flags', 0x00000002)
+// CHECK-NEXT:  ('sh_addr', 0x00000000)
+// CHECK-NEXT:  ('sh_offset', 0x00000050)
+// CHECK-NEXT:  ('sh_size', 0x00000038)
+// CHECK-NEXT:  ('sh_link', 0x00000000)
+// CHECK-NEXT:  ('sh_info', 0x00000000)
+// CHECK-NEXT:  ('sh_addralign', 0x00000008)
+// CHECK-NEXT:  ('sh_entsize', 0x00000000)
+// CHECK-NEXT:  ('_section_data', '14000000 00000000 017a5200 01781001 1b0c0708 90010000 1c000000 1c000000 00000000 0a000000 00040400 00000e10 04050000 000e0800')
+// CHECK-NEXT: ),
+
+// CHECK:       # Section 0x00000008
+// CHECK-NEXT: (('sh_name', 0x00000036) # '.rela.eh_frame'
+// CHECK-NEXT:  ('sh_type', 0x00000004)
+// CHECK-NEXT:  ('sh_flags', 0x00000000)
+// CHECK-NEXT:  ('sh_addr', 0x00000000)
+// CHECK-NEXT:  ('sh_offset', 0x00000168)
+// CHECK-NEXT:  ('sh_size', 0x00000018)
+// CHECK-NEXT:  ('sh_link', 0x00000006)
+// CHECK-NEXT:  ('sh_info', 0x00000004)
+// CHECK-NEXT:  ('sh_addralign', 0x00000008)
+// CHECK-NEXT:  ('sh_entsize', 0x00000018)
+// CHECK-NEXT:  ('_relocations', [
+// CHECK-NEXT:   # Relocation 0x00000000
+// CHECK-NEXT:   (('r_offset', 0x00000020)
+// CHECK-NEXT:    ('r_sym', 0x00000002)
+// CHECK-NEXT:    ('r_type', 0x00000002)
+// CHECK-NEXT:    ('r_addend', 0x00000000)
+// CHECK-NEXT:   ),
+// CHECK-NEXT:  ])
+// CHECK-NEXT: ),