[RFC] Per .section/.subsection .cfi_* directives

Jakub Jelinek jakub@redhat.com
Tue Oct 31 17:32:00 GMT 2006


Hi!

This is a first step in my plan to use assembler .cfi_* directives
in GCC instead of creating its own .eh_frame section.

ATM, we have just one .cfi_* directive sequence in each assembler
source.  But a FDE can only describe a contiguous piece of code
(well, it can be non-contiguous if we don't care about unwind info
for any code in between the chunks we describe) and the advance_loc*
resp. set_loc ops can never decrease the program counter.

This patch changes this, so we have per .section/.subsection
.cfi_* directive streams, thus if we e.g. in inline asm add some
code into another section or subsection, we can use .cfi_* directives
there even if GCC uses .cfi_* directives for unwind info it generates,
and similarly GCC can use that for hot/cold partitioning.

Ok?

2006-10-31  Jakub Jelinek  <jakub@redhat.com>

	* subsegs.h (struct frchain): Add frch_cfi_data field.
	* dw2gencfi.c: Include subsegs.h.
	(cur_fde_data, last_address, cur_cfa_offset, cfa_save_stack): Removed.
	(struct frch_cfi_data): New type.
	(unused_cfi_data): New variable.
	(alloc_fde_entry): Move cur_fde_data, last_address, cur_cfa_offset
	and cfa_save_stack static vars into a structure pointed from
	each frchain.
	(alloc_cfi_insn_data, cfi_new_fde, cfi_end_fde, cfi_set_return_column,
	cfi_add_advance_loc, cfi_add_CFA_def_cfa, cfi_add_CFA_def_cfa_offset,
	cfi_add_CFA_remember_state, cfi_add_CFA_restore_state, dot_cfi,
	dot_cfi_escape, dot_cfi_startproc, dot_cfi_endproc, cfi_finish):
	Likewise.

	* gas/cfi/cfi-common-5.d: New test.
	* gas/cfi/cfi-common-5.s: New.
	* gas/cfi/cfi.exp: Add cfi-common-5 test.

--- gas/subsegs.h.jj	2006-05-04 01:52:15.000000000 +0200
+++ gas/subsegs.h	2006-10-30 18:19:34.000000000 +0100
@@ -40,6 +40,8 @@
 
 #include "obstack.h"
 
+struct frch_cfi_data;
+
 struct frchain			/* control building of a frag chain */
 {				/* FRCH = FRagment CHain control */
   struct frag *frch_root;	/* 1st struct frag in chain, or NULL */
@@ -50,6 +52,7 @@ struct frchain			/* control building of 
   fixS *fix_tail;		/* Last fixup for this subsegment.  */
   struct obstack frch_obstack;	/* for objects in this frag chain */
   fragS *frch_frag_now;		/* frag_now for this subsegment */
+  struct frch_cfi_data *frch_cfi_data;
 };
 
 typedef struct frchain frchainS;
--- gas/dw2gencfi.c.jj	2006-10-30 11:57:15.000000000 +0100
+++ gas/dw2gencfi.c	2006-10-31 11:20:01.000000000 +0100
@@ -21,6 +21,7 @@
 
 #include "as.h"
 #include "dw2gencfi.h"
+#include "subsegs.h"
 
 
 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
@@ -101,11 +103,6 @@ struct cie_entry
 };
 
 
-/* Current open FDE entry.  */
-static struct fde_entry *cur_fde_data;
-static symbolS *last_address;
-static offsetT cur_cfa_offset;
-
 /* List of FDE entries.  */
 static struct fde_entry *all_fde_data;
 static struct fde_entry **last_fde_data = &all_fde_data;
@@ -120,7 +117,16 @@ struct cfa_save_data
   offsetT cfa_offset;
 };
 
-static struct cfa_save_data *cfa_save_stack;
+/* Current open FDE entry.  */
+struct frch_cfi_data
+{
+  struct fde_entry *cur_fde_data;
+  symbolS *last_address;
+  offsetT cur_cfa_offset;
+  struct cfa_save_data *cfa_save_stack;
+};
+
+static struct frch_cfi_data *unused_cfi_data;
 
 /* Construct a new FDE structure and add it to the end of the fde list.  */
 
@@ -129,7 +135,17 @@ alloc_fde_entry (void)
 {
   struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry));
 
-  cur_fde_data = fde;
+  if (unused_cfi_data)
+    {
+      frchain_now->frch_cfi_data = unused_cfi_data;
+      unused_cfi_data = (void *) unused_cfi_data->cur_fde_data;
+      memset (frchain_now->frch_cfi_data, 0,
+	      sizeof (struct frch_cfi_data));
+    }
+  else
+    frchain_now->frch_cfi_data = xcalloc (1, sizeof (struct frch_cfi_data));
+
+  frchain_now->frch_cfi_data->cur_fde_data = fde;
   *last_fde_data = fde;
   last_fde_data = &fde->next;
 
@@ -149,6 +165,7 @@ static struct cfi_insn_data *
 alloc_cfi_insn_data (void)
 {
   struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data));
+  struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data;
 
   *cur_fde_data->last = insn;
   cur_fde_data->last = &insn->next;
@@ -163,7 +180,7 @@ cfi_new_fde (symbolS *label)
 {
   struct fde_entry *fde = alloc_fde_entry ();
   fde->start_address = label;
-  last_address = label;
+  frchain_now->frch_cfi_data->last_address = label;
 }
 
 /* End the currently open FDE.  */
@@ -171,8 +188,10 @@ cfi_new_fde (symbolS *label)
 void 
 cfi_end_fde (symbolS *label)
 {
-  cur_fde_data->end_address = label;
-  cur_fde_data = NULL;
+  frchain_now->frch_cfi_data->cur_fde_data->end_address = label;
+  frchain_now->frch_cfi_data->cur_fde_data = (void *) unused_cfi_data;
+  unused_cfi_data = frchain_now->frch_cfi_data;
+  frchain_now->frch_cfi_data = NULL;
 }
 
 /* Set the return column for the current FDE.  */
@@ -180,7 +199,7 @@ cfi_end_fde (symbolS *label)
 void
 cfi_set_return_column (unsigned regno)
 {
-  cur_fde_data->return_column = regno;
+  frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
 }
 
 /* Universal functions to store new instructions.  */
@@ -239,10 +258,10 @@ cfi_add_advance_loc (symbolS *label)
   struct cfi_insn_data *insn = alloc_cfi_insn_data ();
 
   insn->insn = DW_CFA_advance_loc;
-  insn->u.ll.lab1 = last_address;
+  insn->u.ll.lab1 = frchain_now->frch_cfi_data->last_address;
   insn->u.ll.lab2 = label;
 
-  last_address = label;
+  frchain_now->frch_cfi_data->last_address = label;
 }
 
 /* Add a DW_CFA_offset record to the CFI data.  */
@@ -267,7 +286,7 @@ void
 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
 {
   cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
-  cur_cfa_offset = offset;
+  frchain_now->frch_cfi_data->cur_cfa_offset = offset;
 }
 
 /* Add a DW_CFA_register record to the CFI data.  */
@@ -292,7 +311,7 @@ void
 cfi_add_CFA_def_cfa_offset (offsetT offset)
 {
   cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
-  cur_cfa_offset = offset;
+  frchain_now->frch_cfi_data->cur_cfa_offset = offset;
 }
 
 void
@@ -321,9 +340,9 @@ cfi_add_CFA_remember_state (void)
   cfi_add_CFA_insn (DW_CFA_remember_state);
 
   p = xmalloc (sizeof (*p));
-  p->cfa_offset = cur_cfa_offset;
-  p->next = cfa_save_stack;
-  cfa_save_stack = p;
+  p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset;
+  p->next = frchain_now->frch_cfi_data->cfa_save_stack;
+  frchain_now->frch_cfi_data->cfa_save_stack = p;
 }
 
 void
@@ -333,11 +352,11 @@ cfi_add_CFA_restore_state (void)
 
   cfi_add_CFA_insn (DW_CFA_restore_state);
 
-  p = cfa_save_stack;
+  p = frchain_now->frch_cfi_data->cfa_save_stack;
   if (p)
     {
-      cur_cfa_offset = p->cfa_offset;
-      cfa_save_stack = p->next;
+      frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
+      frchain_now->frch_cfi_data->cfa_save_stack = p->next;
       free (p);
     }
   else
@@ -449,7 +468,7 @@ dot_cfi (int arg)
   offsetT offset;
   unsigned reg1, reg2;
 
-  if (!cur_fde_data)
+  if (frchain_now->frch_cfi_data == NULL)
     {
       as_bad (_("CFI instruction used without previous .cfi_startproc"));
       ignore_rest_of_line ();
@@ -457,8 +476,9 @@ dot_cfi (int arg)
     }
 
   /* If the last address was not at the current PC, advance to current.  */
-  if (symbol_get_frag (last_address) != frag_now
-      || S_GET_VALUE (last_address) != frag_now_fix ())
+  if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
+      || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
+	 != frag_now_fix ())
     cfi_add_advance_loc (symbol_temp_new_now ());
 
   switch (arg)
@@ -474,7 +494,8 @@ dot_cfi (int arg)
       reg1 = cfi_parse_reg ();
       cfi_parse_separator ();
       offset = cfi_parse_const ();
-      cfi_add_CFA_offset (reg1, offset - cur_cfa_offset);
+      cfi_add_CFA_offset (reg1,
+			  offset - frchain_now->frch_cfi_data->cur_cfa_offset);
       break;
 
     case DW_CFA_def_cfa:
@@ -503,7 +524,8 @@ dot_cfi (int arg)
 
     case CFI_adjust_cfa_offset:
       offset = cfi_parse_const ();
-      cfi_add_CFA_def_cfa_offset (cur_cfa_offset + offset);
+      cfi_add_CFA_def_cfa_offset (frchain_now->frch_cfi_data->cur_cfa_offset
+				  + offset);
       break;
 
     case DW_CFA_restore:
@@ -553,7 +575,7 @@ dot_cfi (int arg)
       break;
 
     case CFI_signal_frame:
-      cur_fde_data->signal_frame = 1;
+      frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
       break;
 
     default:
@@ -569,7 +591,7 @@ dot_cfi_escape (int ignored ATTRIBUTE_UN
   struct cfi_escape_data *head, **tail, *e;
   struct cfi_insn_data *insn;
 
-  if (!cur_fde_data)
+  if (frchain_now->frch_cfi_data == NULL)
     {
       as_bad (_("CFI instruction used without previous .cfi_startproc"));
       ignore_rest_of_line ();
@@ -577,8 +599,9 @@ dot_cfi_escape (int ignored ATTRIBUTE_UN
     }
 
   /* If the last address was not at the current PC, advance to current.  */
-  if (symbol_get_frag (last_address) != frag_now
-      || S_GET_VALUE (last_address) != frag_now_fix ())
+  if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
+      || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
+	 != frag_now_fix ())
     cfi_add_advance_loc (symbol_temp_new_now ());
 
   tail = &head;
@@ -605,7 +628,7 @@ dot_cfi_startproc (int ignored ATTRIBUTE
 {
   int simple = 0;
 
-  if (cur_fde_data)
+  if (frchain_now->frch_cfi_data != NULL)
     {
       as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
       ignore_rest_of_line ();
@@ -632,7 +655,7 @@ dot_cfi_startproc (int ignored ATTRIBUTE
     }
   demand_empty_rest_of_line ();
 
-  cur_cfa_offset = 0;
+  frchain_now->frch_cfi_data->cur_cfa_offset = 0;
   if (!simple)
     tc_cfi_frame_initial_instructions ();
 }
@@ -640,7 +663,7 @@ dot_cfi_startproc (int ignored ATTRIBUTE
 static void
 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
 {
-  if (! cur_fde_data)
+  if (frchain_now->frch_cfi_data == NULL)
     {
       as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
       ignore_rest_of_line ();
@@ -1053,12 +1076,6 @@ cfi_finish (void)
   struct fde_entry *fde;
   int save_flag_traditional_format;
 
-  if (cur_fde_data)
-    {
-      as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
-      cur_fde_data->end_address = cur_fde_data->start_address;
-    }
-
   if (all_fde_data == 0)
     return;
 
@@ -1078,6 +1095,12 @@ cfi_finish (void)
       struct cfi_insn_data *first;
       struct cie_entry *cie;
 
+      if (fde->end_address == NULL)
+	{
+	  as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
+	  fde->end_address = fde->start_address;
+	}
+
       cie = select_cie_for_fde (fde, &first);
       output_fde (fde, cie, first, fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
     }
--- gas/testsuite/gas/cfi/cfi-common-5.d.jj	2006-10-31 11:31:49.000000000 +0100
+++ gas/testsuite/gas/cfi/cfi-common-5.d	2006-10-31 12:18:09.000000000 +0100
@@ -0,0 +1,24 @@
+#readelf: -wf
+#name: CFI common 4
+The section .eh_frame contains:
+
+00000000 00000010 00000000 CIE
+  Version:               1
+  Augmentation:          "zR"
+  Code alignment factor: .*
+  Data alignment factor: .*
+  Return address column: .*
+  Augmentation data:     [01]b
+#...
+00000014 00000014 00000018 FDE cie=00000000 pc=.*
+  DW_CFA_advance_loc: 4 to .*
+  DW_CFA_remember_state
+  DW_CFA_advance_loc: 4 to .*
+  DW_CFA_restore_state
+#...
+0000002c 0000001[48] 00000030 FDE cie=00000000 pc=.*
+  DW_CFA_advance_loc: 4 to .*
+  DW_CFA_def_cfa: r0 ofs 16
+  DW_CFA_advance_loc: 4 to .*
+  DW_CFA_def_cfa_offset: 0
+#pass
--- gas/testsuite/gas/cfi/cfi-common-5.s.jj	2006-10-31 11:31:52.000000000 +0100
+++ gas/testsuite/gas/cfi/cfi-common-5.s	2006-10-31 12:11:04.000000000 +0100
@@ -0,0 +1,24 @@
+	.text
+	.cfi_startproc simple
+
+	.subsection 3
+	.cfi_startproc simple
+	.long 0
+	.cfi_def_cfa 0, 16
+	.previous
+
+	.long 0
+	.cfi_remember_state
+
+	.subsection 3
+	.long 0
+	.cfi_adjust_cfa_offset -16
+	.previous
+
+	.long 0
+	.cfi_restore_state
+	.cfi_endproc
+
+	.subsection 3
+	.cfi_endproc
+	.previous
--- gas/testsuite/gas/cfi/cfi.exp.jj	2006-10-30 11:57:16.000000000 +0100
+++ gas/testsuite/gas/cfi/cfi.exp	2006-10-31 12:16:09.000000000 +0100
@@ -72,3 +72,4 @@ run_dump_test "cfi-common-1"
 run_dump_test "cfi-common-2"
 run_dump_test "cfi-common-3"
 run_dump_test "cfi-common-4"
+run_dump_test "cfi-common-5"

	Jakub



More information about the Binutils mailing list