PATCH [8/n]: Prepare x32: PR other/48007: Unwind library doesn't work with UNITS_PER_WORD > sizeof (void *)

H.J. Lu hjl.tools@gmail.com
Thu Jun 30 17:57:00 GMT 2011


On Thu, Jun 30, 2011 at 9:08 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Thu, Jun 30, 2011 at 8:03 AM, Jason Merrill <jason@redhat.com> wrote:
>> On 06/30/2011 10:42 AM, H.J. Lu wrote:
>>>
>>> Register may be saved/restored either by address or value. My patch
>>> doesn't change the reg field.  The other way will be
>>>
>>> #ifdef USE_UNWIND_WORD
>>>     _Unwind_Word reg[DWARF_FRAME_REGISTERS+1];
>>> #else
>>>      void *reg[DWARF_FRAME_REGISTERS+1];
>>> #endif
>>>
>>> We need it so that we are binary compatible with the existing
>>> unwind context.  Once we do that we need many
>>>
>>> #ifdef USE_UNWIND_WORD
>>> #endif
>>>
>>> whenever the reg field is accessed since the reg field is changed.
>>
>> But your patch already changes all but one place where reg is accessed.  And
>> we can avoid lots of ifdefs by abstraction with macros/inlines so there's
>> one interface.
>
> I can do that.
>
>> Also, why change SIGNAL_FRAME_BIT?
>>
>
> The current one is
>
>  /* Signal frame context.  */
> #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
>
> It is defined such a strange way to be binary backward compatible.
> Since there is no such a problem with if REG_VALUE_IN_UNWIND_CONTEXT
> is defined, I simply define it as
>
>  /* Signal frame context.  */
> #define SIGNAL_FRAME_BIT ((_Unwind_Word) 1 >> 0)
>

Here is the updated patch.  OK for trunk if there are no regressions?

Thanks.

-- 
H.J.
---
gcc/

2011-06-30  H.J. Lu  <hongjiu.lu@intel.com>

	* config.gcc (libgcc_tm_file): Add i386/value-unwind.h for
	Linux/x86.

	* system.h (REG_VALUE_IN_UNWIND_CONTEXT): Poisoned.

	* unwind-dw2.c (_Unwind_Context_Reg_Val): New.
	(_Unwind_Get_Unwind_Word): Likewise.
	(_Unwind_Get_Unwind_Context_Reg_Val): Likewise.
	(_Unwind_Context): Use _Unwind_Context_Reg_Val on the reg field.
	(SIGNAL_FRAME_BIT): Define if REG_VALUE_IN_UNWIND_CONTEXT is
	defined.
	(_Unwind_IsExtendedContext): Likewise.
	(EXTENDED_CONTEXT_BIT): Don't define if REG_VALUE_IN_UNWIND_CONTEXT
	is defined.
	(_Unwind_GetGR): Updated.
	(_Unwind_SetGR): Likewise.
	(_Unwind_GetGRPtr): Likewise.
	(_Unwind_SetGRPtr): Likewise.
	(_Unwind_SetGRValue): Likewise.
	(_Unwind_GRByValue): Likewise.
	(__frame_state_for): Likewise.
	(uw_install_context_1): Likewise.

	* doc/tm.texi.in: Document REG_VALUE_IN_UNWIND_CONTEXT.
	* doc/tm.texi: Regenerated.

libgcc/

2011-06-30  H.J. Lu  <hongjiu.lu@intel.com>

	* config/i386/value-unwind.h: New.
-------------- next part --------------
gcc/

2011-06-30  H.J. Lu  <hongjiu.lu@intel.com>

	* config.gcc (libgcc_tm_file): Add i386/value-unwind.h for
	Linux/x86.

	* system.h (REG_VALUE_IN_UNWIND_CONTEXT): Poisoned.

	* unwind-dw2.c (_Unwind_Context_Reg_Val): New.
	(_Unwind_Get_Unwind_Word): Likewise.
	(_Unwind_Get_Unwind_Context_Reg_Val): Likewise.
	(_Unwind_Context): Use _Unwind_Context_Reg_Val on the reg field.
	(SIGNAL_FRAME_BIT): Define if REG_VALUE_IN_UNWIND_CONTEXT is
	defined.
	(_Unwind_IsExtendedContext): Likewise.
	(EXTENDED_CONTEXT_BIT): Don't define if REG_VALUE_IN_UNWIND_CONTEXT
	is defined.
	(_Unwind_GetGR): Updated.
	(_Unwind_SetGR): Likewise.
	(_Unwind_GetGRPtr): Likewise.
	(_Unwind_SetGRPtr): Likewise.
	(_Unwind_SetGRValue): Likewise.
	(_Unwind_GRByValue): Likewise.
	(__frame_state_for): Likewise.
	(uw_install_context_1): Likewise.

	* doc/tm.texi.in: Document REG_VALUE_IN_UNWIND_CONTEXT.
	* doc/tm.texi: Regenerated.

libgcc/

2011-06-30  H.J. Lu  <hongjiu.lu@intel.com>

	* config/i386/value-unwind.h: New.

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 73c47d7..2702df2 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -1277,7 +1277,17 @@ i[34567]86-*-linux* | i[34567]86-*-kfreebsd*-gnu | i[34567]86-*-knetbsd*-gnu | i
 		if test x$enable_targets = xall; then
 			tm_file="${tm_file} i386/x86-64.h i386/gnu-user64.h i386/linux64.h"
 			tm_defines="${tm_defines} TARGET_BI_ARCH=1"
-			tmake_file="${tmake_file} i386/t-linux64"
+			case x${enable_x32}${enable_ia32} in
+			xyesyes)
+				tmake_file="${tmake_file} i386/t-linux-x32"
+				;;
+			xyesno)
+				tmake_file="${tmake_file} i386/t-linux64-x32"
+				;;
+			*)
+				tmake_file="${tmake_file} i386/t-linux64"
+				;;
+			esac
 			need_64bit_hwint=yes
 			need_64bit_isa=yes
 			case X"${with_cpu}" in
@@ -1315,7 +1325,18 @@ x86_64-*-linux* | x86_64-*-kfreebsd*-gnu | x86_64-*-knetbsd*-gnu)
 	x86_64-*-kfreebsd*-gnu) tm_file="${tm_file} kfreebsd-gnu.h i386/kfreebsd-gnu64.h" ;;
 	x86_64-*-knetbsd*-gnu) tm_file="${tm_file} knetbsd-gnu.h" ;;
 	esac
-	tmake_file="${tmake_file} i386/t-linux64 i386/t-crtstuff i386/t-crtpc i386/t-crtfm t-dfprules"
+	case x${enable_x32}${enable_ia32} in
+	xyesyes)
+		tmake_file="${tmake_file} i386/t-linux-x32"
+		;;
+	xyesno)
+		tmake_file="${tmake_file} i386/t-linux64-x32"
+		;;
+	*)
+		tmake_file="${tmake_file} i386/t-linux64"
+		;;
+	esac
+	tmake_file="${tmake_file} i386/t-crtstuff i386/t-crtpc i386/t-crtfm t-dfprules"
 	;;
 i[34567]86-pc-msdosdjgpp*)
 	xm_file=i386/xm-djgpp.h
@@ -2630,6 +2651,7 @@ esac
 case ${target} in
 i[34567]86-*-linux* | x86_64-*-linux*)
 	tmake_file="${tmake_file} i386/t-pmm_malloc i386/t-i386"
+	libgcc_tm_file="${libgcc_tm_file} i386/value-unwind.h"
 	;;
 i[34567]86-*-* | x86_64-*-*)
 	tmake_file="${tmake_file} i386/t-gmm_malloc i386/t-i386"
diff --git a/gcc/system.h b/gcc/system.h
index e02cbcd..ed39d93 100644
--- a/gcc/system.h
+++ b/gcc/system.h
@@ -764,7 +764,7 @@ extern void fancy_abort (const char *, int, const char *) ATTRIBUTE_NORETURN;
 /* Target macros only used for code built for the target, that have
    moved to libgcc-tm.h or have never been present elsewhere.  */
  #pragma GCC poison DECLARE_LIBRARY_RENAMES LIBGCC2_GNU_PREFIX		\
-	MD_UNWIND_SUPPORT ENABLE_EXECUTE_STACK
+	MD_UNWIND_SUPPORT ENABLE_EXECUTE_STACK REG_VALUE_IN_UNWIND_CONTEXT
 
 /* Other obsolete target macros, or macros that used to be in target
    headers and were not used, and may be obsolete or may never have
diff --git a/gcc/unwind-dw2.c b/gcc/unwind-dw2.c
index 19da299..c77265c 100644
--- a/gcc/unwind-dw2.c
+++ b/gcc/unwind-dw2.c
@@ -59,20 +59,56 @@
 #define DWARF_REG_TO_UNWIND_COLUMN(REGNO) (REGNO)
 #endif
 
+#ifdef REG_VALUE_IN_UNWIND_CONTEXT
+typedef _Unwind_Word _Unwind_Context_Reg_Val;
+/* Signal frame context.  */
+#define SIGNAL_FRAME_BIT ((_Unwind_Word) 1 >> 0)
+
+#define _Unwind_IsExtendedContext(c) 1
+
+static inline _Unwind_Word
+_Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
+{
+  return val;
+}
+
+static inline _Unwind_Context_Reg_Val
+_Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
+{
+  return val;
+}
+#else
+typedef void *_Unwind_Context_Reg_Val;
+/* Signal frame context.  */
+#define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
+/* Context which has version/args_size/by_value fields.  */
+#define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
+
+#define _Unwind_IsExtendedContext(c) ((c)->flags & EXTENDED_CONTEXT_BIT)
+
+static inline _Unwind_Word
+_Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
+{
+  return (_Unwind_Word) (_Unwind_Internal_Ptr) val;
+}
+
+static inline _Unwind_Context_Reg_Val
+_Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
+{
+  return (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) val;
+}
+#endif
+
 /* This is the register and unwind state for a particular frame.  This
    provides the information necessary to unwind up past a frame and return
    to its caller.  */
 struct _Unwind_Context
 {
-  void *reg[DWARF_FRAME_REGISTERS+1];
+  _Unwind_Context_Reg_Val reg[DWARF_FRAME_REGISTERS+1];
   void *cfa;
   void *ra;
   void *lsda;
   struct dwarf_eh_bases bases;
-  /* Signal frame context.  */
-#define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
-  /* Context which has version/args_size/by_value fields.  */
-#define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
   _Unwind_Word flags;
   /* 0 for now, can be increased when further fields are added to
      struct _Unwind_Context.  */
@@ -143,12 +179,6 @@ _Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
   else
     context->flags &= ~SIGNAL_FRAME_BIT;
 }
-
-static inline _Unwind_Word
-_Unwind_IsExtendedContext (struct _Unwind_Context *context)
-{
-  return context->flags & EXTENDED_CONTEXT_BIT;
-}
 

 /* Get the value of register INDEX as saved in CONTEXT.  */
 
@@ -156,7 +186,7 @@ inline _Unwind_Word
 _Unwind_GetGR (struct _Unwind_Context *context, int index)
 {
   int size;
-  void *ptr;
+  _Unwind_Context_Reg_Val val;
 
 #ifdef DWARF_ZERO_REG
   if (index == DWARF_ZERO_REG)
@@ -166,18 +196,18 @@ _Unwind_GetGR (struct _Unwind_Context *context, int index)
   index = DWARF_REG_TO_UNWIND_COLUMN (index);
   gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
   size = dwarf_reg_size_table[index];
-  ptr = context->reg[index];
+  val = context->reg[index];
 
   if (_Unwind_IsExtendedContext (context) && context->by_value[index])
-    return (_Unwind_Word) (_Unwind_Internal_Ptr) ptr;
+    return _Unwind_Get_Unwind_Word (val);
 
   /* This will segfault if the register hasn't been saved.  */
   if (size == sizeof(_Unwind_Ptr))
-    return * (_Unwind_Ptr *) ptr;
+    return * (_Unwind_Ptr *) (_Unwind_Internal_Ptr) val;
   else
     {
       gcc_assert (size == sizeof(_Unwind_Word));
-      return * (_Unwind_Word *) ptr;
+      return * (_Unwind_Word *) (_Unwind_Internal_Ptr) val;
     }
 }
 
@@ -209,11 +239,11 @@ _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
 
   if (_Unwind_IsExtendedContext (context) && context->by_value[index])
     {
-      context->reg[index] = (void *) (_Unwind_Internal_Ptr) val;
+      context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
       return;
     }
 
-  ptr = context->reg[index];
+  ptr = (void *) (_Unwind_Internal_Ptr) context->reg[index];
 
   if (size == sizeof(_Unwind_Ptr))
     * (_Unwind_Ptr *) ptr = val;
@@ -232,7 +262,7 @@ _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
   index = DWARF_REG_TO_UNWIND_COLUMN (index);
   if (_Unwind_IsExtendedContext (context) && context->by_value[index])
     return &context->reg[index];
-  return context->reg[index];
+  return (void *) (_Unwind_Internal_Ptr) context->reg[index];
 }
 
 /* Set the pointer to a register INDEX as saved in CONTEXT.  */
@@ -243,7 +273,7 @@ _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
   index = DWARF_REG_TO_UNWIND_COLUMN (index);
   if (_Unwind_IsExtendedContext (context))
     context->by_value[index] = 0;
-  context->reg[index] = p;
+  context->reg[index] = (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) p;
 }
 
 /* Overwrite the saved value for register INDEX in CONTEXT with VAL.  */
@@ -254,10 +284,10 @@ _Unwind_SetGRValue (struct _Unwind_Context *context, int index,
 {
   index = DWARF_REG_TO_UNWIND_COLUMN (index);
   gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
-  gcc_assert (dwarf_reg_size_table[index] == sizeof (_Unwind_Ptr));
+  gcc_assert (dwarf_reg_size_table[index] == sizeof (_Unwind_Context_Reg_Val));
 
   context->by_value[index] = 1;
-  context->reg[index] = (void *) (_Unwind_Internal_Ptr) val;
+  context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
 }
 
 /* Return nonzero if register INDEX is stored by value rather than
@@ -1215,7 +1245,9 @@ __frame_state_for (void *pc_target, struct frame_state *state_in)
   int reg;
 
   memset (&context, 0, sizeof (struct _Unwind_Context));
+#ifndef REG_VALUE_IN_UNWIND_CONTEXT
   context.flags = EXTENDED_CONTEXT_BIT;
+#endif
   context.ra = pc_target + 1;
 
   if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
@@ -1453,7 +1485,9 @@ uw_init_context_1 (struct _Unwind_Context *context,
 
   memset (context, 0, sizeof (struct _Unwind_Context));
   context->ra = ra;
+#ifndef REG_VALUE_IN_UNWIND_CONTEXT
   context->flags = EXTENDED_CONTEXT_BIT;
+#endif
 
   code = uw_frame_state_for (context, &fs);
   gcc_assert (code == _URC_NO_REASON);
@@ -1532,8 +1566,8 @@ uw_install_context_1 (struct _Unwind_Context *current,
 
   for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
     {
-      void *c = current->reg[i];
-      void *t = target->reg[i];
+      void *c = (void *) (_Unwind_Internal_Ptr) current->reg[i];
+      void *t = (void *) (_Unwind_Internal_Ptr)target->reg[i];
 
       gcc_assert (current->by_value[i] == 0);
       if (target->by_value[i] && c)
diff --git a/libgcc/config/i386/value-unwind.h b/libgcc/config/i386/value-unwind.h
new file mode 100644
index 0000000..0dceb5c
--- /dev/null
+++ b/libgcc/config/i386/value-unwind.h
@@ -0,0 +1,26 @@
+/* Store register values as _Unwind_Word type in DWARF2 EH unwind context.
+   Copyright (C) 2011
+   Free Software Foundation, Inc.
+
+   This file is part of GCC.
+
+   GCC is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published
+   by the Free Software Foundation; either version 3, or (at your
+   option) any later version.
+
+   GCC is distributed in the hope that it will be useful, but WITHOUT
+   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
+   License for more details.
+
+   You should have received a copy of the GNU General Public License and
+   a copy of the GCC Runtime Library Exception along with this program;
+   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* Define this macro if the target stores register values as _Unwind_Word
+   type in unwind context.  Only enable it for x32.  */
+#if defined __x86_64 && !defined __LP64__
+# define REG_VALUE_IN_UNWIND_CONTEXT
+#endif


More information about the Gcc-patches mailing list