[PATCH] GCC side of .eh_frame_hdr support

Jakub Jelinek jakub@redhat.com
Wed Dec 12 09:45:00 GMT 2001


Hi!

This is not completely finished, I'm just looking for comments.
I've briefly tested it and it worked so far.
What's missing is
a) If HAVE_LD_EH_FRAME_HDR is defined, %{!static:--eh-frame-hdr} needs to be
   somehow added to LINK_SPEC. Any suggestions how to do it? Changing
   all Linux/Hurd LINK_SPEC definitions in config.h files is something I'd
   like to avoid, furthermore we need to avoid string concatenation...
b) As dl_iterate_phdr is guaranteed to work for dynamically linked programs
   only (just on IA-64 it works for statically linked too and on other
   platforms it is difficult to do), I think we'll need separate
   crtbeginT.o (T for static) on the platforms where this matters
   (so that crtbegin.o and crtbeginS.o don't call __(de)register_frame*
   for dynamically linked progs/shared libraries, but call it for -static).
   What do you think about it?

2001-12-12  Jakub Jelinek  <jakub@redhat.com>

	* config/ia64/fde-glibc.c (_Unwind_IteratePhdrCallback): Don't
	iterate further if pc falls into current library, but fde was not
	found.
	* config/t-linux (LIB2ADDEH, LIB2ADDEHDEP): Use unwind-dw2-fde-glibc
	frame unwinding on Linux.
	* configure.in: Check for ld.
	(HAVE_LD_EH_FRAME_HDR): Define if ld supports --eh-frame-hdr option.
	* configure, config.in: Rebuilt.
	* unwind-dw2-fde.c: Don't include any headers if this file
	is included from other .c file.
	(_Unwind_Find_FDE): If _Unwind_Find_FDE is defined, make the
	function static.
	* unwind-dw2-fde-glibc.c: New file.
	* crtstuff.c (USE_PT_GNU_EH_FRAME, USE_EH_FRAME_REGISTRY): Define.
	Use it instead of EH_FRAME_SECTION_NAME where appropriate.

--- gcc/config/ia64/fde-glibc.c.jj	Thu Aug  9 18:38:43 2001
+++ gcc/config/ia64/fde-glibc.c	Wed Dec 12 17:53:26 2001
@@ -110,7 +110,9 @@ _Unwind_IteratePhdrCallback (struct dl_p
       else
         goto found;
     }
-  return 0;
+  /* No need to search for further libraries when we know pc is contained
+     in this library.  */
+  return 1;
 
  found:
   *data->segment_base = seg_base;
--- gcc/config/t-linux.jj	Fri Jun  8 11:37:10 2001
+++ gcc/config/t-linux	Wed Dec 12 18:31:19 2001
@@ -9,3 +9,8 @@ TARGET_LIBGCC2_CFLAGS = -fPIC
 # Override t-slibgcc-elf-ver to export some libgcc symbols with
 # the symbol versions that glibc used.
 SHLIB_MAPFILES += $(srcdir)/config/libgcc-glibc.ver
+
+# Use unwind-dw2-fde-glibc
+LIB2ADDEH = $(srcdir)/unwind-dw2.c $(srcdir)/unwind-dw2-fde-glibc.c \
+  $(srcdir)/unwind-sjlj.c
+LIB2ADDEHDEP = unwind.inc unwind-dw2-fde.h unwind-dw2-fde.c
--- gcc/configure.jj	Wed Dec  5 12:08:19 2001
+++ gcc/configure	Wed Dec 12 13:58:08 2001
@@ -6898,6 +6898,92 @@ else
   echo "$ac_t""$gcc_cv_as" 1>&6
 fi
 
+# Figure out what linker we will be using.
+echo $ac_n "checking what linker to use""... $ac_c" 1>&6
+echo "configure:6904: checking what linker to use" >&5
+gcc_cv_ld=
+gcc_cv_gld_major_version=
+gcc_cv_gld_minor_version=
+gcc_cv_ld_gld_srcdir=`echo $srcdir | sed -e 's,/gcc$,,'`/ld
+gcc_cv_ld_bfd_srcdir=`echo $srcdir | sed -e 's,/gcc$,,'`/bfd
+if test -x "$DEFAULT_LINKER"; then
+	gcc_cv_ld="$DEFAULT_LINKER"
+elif test -x "$LD"; then
+	gcc_cv_ld="$LD"
+elif test -x ld$host_exeext; then
+	# Build using linker in the current directory.
+	gcc_cv_ld=./ld$host_exeext
+elif test -f $gcc_cv_ld_gld_srcdir/configure.in -a -f ../ld/Makefile; then
+	# Single tree build which includes ld.
+	for f in $gcc_cv_ld_bfd_srcdir/configure $gcc_cv_ld_gld_srcdir/configure $gcc_cv_ld_gld_srcdir/configure.in $gcc_cv_ld_gld_srcdir/Makefile.in
+	do
+		gcc_cv_gld_version=`grep '^VERSION=[0-9]*\.[0-9]*' $f`
+		if test x$gcc_cv_gld_version != x; then
+			break
+		fi
+	done
+	gcc_cv_gld_major_version=`expr "$gcc_cv_gld_version" : "VERSION=\([0-9]*\)"`
+	gcc_cv_gld_minor_version=`expr "$gcc_cv_gld_version" : "VERSION=[0-9]*\.\([0-9]*\)"`
+fi
+
+if test "x$gcc_cv_ld" = x -a x$host = x$target; then
+	# Native build.
+	# Search the same directories that the installed compiler will
+	# search.  Else we may find the wrong linker and lose.  If we
+	# do not find a suitable linker binary, then try the user's
+	# path.
+	#
+	# Also note we have to check MD_EXEC_PREFIX before checking the
+	# user's path.  Unfortunately, there is no good way to get at the
+	# value of MD_EXEC_PREFIX here.  So we do a brute force search
+	# through all the known MD_EXEC_PREFIX values.  Ugh.  This needs
+	# to be fixed as part of the make/configure rewrite too.
+
+	if test "x$exec_prefix" = xNONE; then
+		if test "x$prefix" = xNONE; then
+			test_prefix=/usr/local
+		else
+			test_prefix=$prefix
+		fi
+	else
+		test_prefix=$exec_prefix
+	fi
+
+	# If the loop below does not find an linker, then use whatever
+	# one we can find in the users's path.
+	# user's path.
+	gcc_cv_ld=ld$host_exeext
+
+	test_dirs="$test_prefix/lib/gcc-lib/$target/$gcc_version \
+		   $test_prefix/lib/gcc-lib/$target \
+		   /usr/lib/gcc/$target/$gcc_version \
+		   /usr/lib/gcc/$target \
+		   $test_prefix/$target/bin/$target/$gcc_version \
+		   $test_prefix/$target/bin \
+		   /usr/libexec \
+		   /usr/ccs/gcc \
+		   /usr/ccs/bin \
+		   /udk/usr/ccs/bin \
+		   /bsd43/usr/lib/cmplrs/cc \
+		   /usr/cross64/usr/bin \
+		   /usr/lib/cmplrs/cc \
+		   /sysv/usr/lib/cmplrs/cc \
+		   /svr4/usr/lib/cmplrs/cc \
+		   /usr/bin"
+
+	for dir in $test_dirs; do
+		if test -f $dir/ld$host_exeext; then
+			gcc_cv_ld=$dir/ld$host_exeext
+			break;
+		fi
+	done
+fi
+if test x$gcc_cv_gld_major_version != x -a x$gcc_cv_gld_minor_version != x; then
+  echo "$ac_t"""newly built ld"" 1>&6
+else
+  echo "$ac_t""$gcc_cv_ld" 1>&6
+fi
+
 # Figure out what nm we will be using.
 echo $ac_n "checking what nm to use""... $ac_c" 1>&6
 echo "configure:6904: checking what nm to use" >&5
@@ -7520,6 +7606,27 @@ EOF
 fi
 echo "$ac_t""$gcc_cv_as_gstabs_flag" 1>&6
 
+echo $ac_n "checking linker PT_GNU_EH_FRAME support""... $ac_c" 1>&6
+echo "configure:7611: checking linker PT_GNU_EH_FRAME support" >&5
+gcc_cv_ld_eh_frame_hdr=no
+if test x$gcc_cv_gld_major_version != x -a x$gcc_cv_gld_minor_version != x; then
+  if test "$gcc_cv_gld_major_version" -eq 2 -a "$gcc_cv_gld_minor_version" -ge 12 -o "$gcc_cv_gld_major_version" -gt 2 && grep 'EMUL = elf' ../ld/Makefile > /dev/null; then
+    gcc_cv_ld_eh_frame_hdr=yes
+  fi
+elif test x$gcc_cv_ld != x; then
+	# Check if linker supports --eh-frame-hdr option
+	if $gcc_cv_ld --help 2>/dev/null | grep eh-frame-hdr > /dev/null; then
+		gcc_cv_ld_eh_frame_hdr=yes
+	fi
+fi
+if test x"$gcc_cv_ld_eh_frame_hdr" = xyes; then
+	cat >> confdefs.h <<\EOF
+#define HAVE_LD_EH_FRAME_HDR 1
+EOF
+
+fi
+echo "$ac_t""$gcc_cv_ld_eh_frame_hdr" 1>&6
+
 if test "$prefix" != "/usr" && test "$prefix" != "/usr/local" ; then
   cat >> confdefs.h <<EOF
 #define PREFIX_INCLUDE_DIR "$prefix/include"
--- gcc/config.in.jj	Wed Dec 12 14:04:06 2001
+++ gcc/config.in	Wed Dec 12 14:04:12 2001
@@ -551,6 +551,9 @@
 /* Define if your assembler supports the --gstabs option. */
 #undef HAVE_AS_GSTABS_DEBUG_FLAG
 
+/* Define if your linker supports --eh-frame-hdr option. */
+#undef HAVE_LD_EH_FRAME_HDR
+
 /* Define 0/1 to force the choice for exception handling model. */
 #undef CONFIG_SJLJ_EXCEPTIONS
 
--- gcc/configure.in.jj	Wed Dec  5 12:08:19 2001
+++ gcc/configure.in	Wed Dec 12 13:56:22 2001
@@ -1251,6 +1251,95 @@ else
   AC_MSG_RESULT($gcc_cv_as)
 fi
 
+# Figure out what linker we will be using.
+AC_MSG_CHECKING(what linker to use)
+gcc_cv_ld=
+gcc_cv_gld_major_version=
+gcc_cv_gld_minor_version=
+gcc_cv_ld_gld_srcdir=`echo $srcdir | sed -e 's,/gcc$,,'`/ld
+gcc_cv_ld_bfd_srcdir=`echo $srcdir | sed -e 's,/gcc$,,'`/bfd
+if test -x "$DEFAULT_LINKER"; then
+	gcc_cv_ld="$DEFAULT_LINKER"
+elif test -x "$LD"; then
+	gcc_cv_ld="$LD"
+elif test -x ld$host_exeext; then
+	# Build using linker in the current directory.
+	gcc_cv_ld=./ld$host_exeext
+elif test -f $gcc_cv_ld_gld_srcdir/configure.in -a -f ../ld/Makefile; then
+	# Single tree build which includes ld.
+	for f in $gcc_cv_ld_bfd_srcdir/configure $gcc_cv_ld_gld_srcdir/configure $gcc_cv_ld_gld_srcdir/configure.in $gcc_cv_ld_gld_srcdir/Makefile.in
+	do
+changequote(,)dnl
+		gcc_cv_gld_version=`grep '^VERSION=[0-9]*\.[0-9]*' $f`
+changequote([,])dnl
+		if test x$gcc_cv_gld_version != x; then
+			break
+		fi
+	done
+changequote(,)dnl
+	gcc_cv_gld_major_version=`expr "$gcc_cv_gld_version" : "VERSION=\([0-9]*\)"`
+	gcc_cv_gld_minor_version=`expr "$gcc_cv_gld_version" : "VERSION=[0-9]*\.\([0-9]*\)"`
+changequote([,])dnl
+fi
+
+if test "x$gcc_cv_ld" = x -a x$host = x$target; then
+	# Native build.
+	# Search the same directories that the installed compiler will
+	# search.  Else we may find the wrong linker and lose.  If we
+	# do not find a suitable linker binary, then try the user's
+	# path.
+	#
+	# Also note we have to check MD_EXEC_PREFIX before checking the
+	# user's path.  Unfortunately, there is no good way to get at the
+	# value of MD_EXEC_PREFIX here.  So we do a brute force search
+	# through all the known MD_EXEC_PREFIX values.  Ugh.  This needs
+	# to be fixed as part of the make/configure rewrite too.
+
+	if test "x$exec_prefix" = xNONE; then
+		if test "x$prefix" = xNONE; then
+			test_prefix=/usr/local
+		else
+			test_prefix=$prefix
+		fi
+	else
+		test_prefix=$exec_prefix
+	fi
+
+	# If the loop below does not find an linker, then use whatever
+	# one we can find in the users's path.
+	# user's path.
+	gcc_cv_ld=ld$host_exeext
+
+	test_dirs="$test_prefix/lib/gcc-lib/$target/$gcc_version \
+		   $test_prefix/lib/gcc-lib/$target \
+		   /usr/lib/gcc/$target/$gcc_version \
+		   /usr/lib/gcc/$target \
+		   $test_prefix/$target/bin/$target/$gcc_version \
+		   $test_prefix/$target/bin \
+		   /usr/libexec \
+		   /usr/ccs/gcc \
+		   /usr/ccs/bin \
+		   /udk/usr/ccs/bin \
+		   /bsd43/usr/lib/cmplrs/cc \
+		   /usr/cross64/usr/bin \
+		   /usr/lib/cmplrs/cc \
+		   /sysv/usr/lib/cmplrs/cc \
+		   /svr4/usr/lib/cmplrs/cc \
+		   /usr/bin"
+
+	for dir in $test_dirs; do
+		if test -f $dir/ld$host_exeext; then
+			gcc_cv_ld=$dir/ld$host_exeext
+			break;
+		fi
+	done
+fi
+if test x$gcc_cv_gld_major_version != x -a x$gcc_cv_gld_minor_version != x; then
+  AC_MSG_RESULT("newly built ld")
+else
+  AC_MSG_RESULT($gcc_cv_ld)
+fi
+
 # Figure out what nm we will be using.
 AC_MSG_CHECKING(what nm to use)
 if test -x nm$host_exeext; then
@@ -1527,7 +1616,7 @@ if test x"$gcc_cv_as_shf_merge" = xyes; 
 fi
 AC_MSG_RESULT($gcc_cv_as_shf_merge)
 
-case "$target" in 
+case "$target" in
   sparc*-*-*)
     AC_CACHE_CHECK([assembler .register pseudo-op support],
 	gcc_cv_as_register_pseudo_op, [
@@ -1794,6 +1883,24 @@ if test x"$gcc_cv_as_gstabs_flag" = xyes
 fi
 AC_MSG_RESULT($gcc_cv_as_gstabs_flag)
 
+AC_MSG_CHECKING(linker PT_GNU_EH_FRAME support)
+gcc_cv_ld_eh_frame_hdr=no
+if test x$gcc_cv_gld_major_version != x -a x$gcc_cv_gld_minor_version != x; then
+  if test "$gcc_cv_gld_major_version" -eq 2 -a "$gcc_cv_gld_minor_version" -ge 12 -o "$gcc_cv_gld_major_version" -gt 2 && grep 'EMUL = elf' ../ld/Makefile > /dev/null; then
+    gcc_cv_ld_eh_frame_hdr=yes
+  fi
+elif test x$gcc_cv_ld != x; then
+	# Check if linker supports --eh-frame-hdr option
+	if $gcc_cv_ld --help 2>/dev/null | grep eh-frame-hdr > /dev/null; then
+		gcc_cv_ld_eh_frame_hdr=yes
+	fi
+fi
+if test x"$gcc_cv_ld_eh_frame_hdr" = xyes; then
+	AC_DEFINE(HAVE_LD_EH_FRAME_HDR, 1,
+[Define if your linker supports --eh-frame-hdr option.])
+fi
+AC_MSG_RESULT($gcc_cv_ld_eh_frame_hdr)
+
 if test "$prefix" != "/usr" && test "$prefix" != "/usr/local" ; then
   AC_DEFINE_UNQUOTED(PREFIX_INCLUDE_DIR, "$prefix/include")
 fi
--- gcc/unwind-dw2-fde.c.jj	Wed Nov 21 13:02:10 2001
+++ gcc/unwind-dw2-fde.c	Wed Dec 12 17:55:41 2001
@@ -28,6 +28,7 @@ along with GCC; see the file COPYING.  I
 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 02111-1307, USA.  */
 
+#ifndef _Unwind_Find_FDE
 #include "tconfig.h"
 #include "tsystem.h"
 #include "dwarf2.h"
@@ -36,6 +37,7 @@ Software Foundation, 59 Temple Place - S
 #include "unwind-pe.h"
 #include "unwind-dw2-fde.h"
 #include "gthr.h"
+#endif
 
 /* The unseen_objects list contains objects that have been registered
    but not yet categorized in any way.  The seen_objects list has had
@@ -963,6 +965,9 @@ search_object (struct object* ob, void *
     }
 }
 
+#ifdef _Unwind_Find_FDE
+static
+#endif
 fde *
 _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
 {
--- gcc/unwind-dw2-fde-glibc.c.jj	Wed Dec 12 15:56:58 2001
+++ gcc/unwind-dw2-fde-glibc.c	Wed Dec 12 19:11:55 2001
@@ -0,0 +1,291 @@
+/* Copyright (C) 2001 Free Software Foundation, Inc.
+   Contributed by Jakub Jelinek <jakub@redhat.com>.
+
+   This file is part of GNU CC.
+
+   GNU CC 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 2, or (at your option)
+   any later version.
+
+   GNU CC 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
+   along with GNU CC; see the file COPYING.  If not, write to
+   the Free Software Foundation, 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+/* As a special exception, if you link this library with other files,
+   some of which are compiled with GCC, to produce an executable,
+   this library does not by itself cause the resulting executable
+   to be covered by the GNU General Public License.
+   This exception does not however invalidate any other reasons why
+   the executable file might be covered by the GNU General Public License.  */
+
+/* Locate the FDE entry for a given address, using PT_GNU_EH_FRAME ELF
+   segment and dl_iterate_phdr to avoid register/deregister calls at
+   DSO load/unload.  */
+
+#include "auto-host.h" /* For HAVE_LD_EH_FRAME_HDR.  */
+#include "tconfig.h"
+#include <stddef.h>
+#include <stdlib.h>
+#include <link.h>
+#include "tsystem.h"
+#include "dwarf2.h"
+#include "unwind.h"
+#define NO_BASE_OF_ENCODED_VALUE
+#include "unwind-pe.h"
+#include "unwind-dw2-fde.h"
+#include "gthr.h"
+
+#if defined(HAVE_LD_EH_FRAME_HDR) \
+    && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
+	|| (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
+
+static fde * _Unwind_Find_registered_FDE (void *pc, struct dwarf_eh_bases *bases);
+
+#define _Unwind_Find_FDE _Unwind_Find_registered_FDE
+#include "unwind-dw2-fde.c"
+#undef _Unwind_Find_FDE
+
+#ifndef PT_GNU_EH_FRAME
+#define PT_GNU_EH_FRAME (PT_LOOS + 0x474e550)
+#endif
+
+struct unw_eh_callback_data
+{
+  _Unwind_Ptr pc;
+  void *tbase;
+  void *dbase;
+  void *func;
+  fde *ret;
+};
+
+struct unw_eh_frame_hdr
+{
+  unsigned char version;
+  unsigned char eh_frame_ptr_enc;
+  unsigned char fde_count_enc;
+  unsigned char table_enc;
+};
+
+/* Like base_of_encoded_value, but take the base from a struct object
+   instead of an _Unwind_Context.  */
+   
+static _Unwind_Ptr
+base_from_cb_data (unsigned char encoding, struct unw_eh_callback_data *data)
+{
+  if (encoding == DW_EH_PE_omit)
+    return 0;
+
+  switch (encoding & 0x70)
+    {
+    case DW_EH_PE_absptr:
+    case DW_EH_PE_pcrel:
+    case DW_EH_PE_aligned:
+      return 0;
+                                 
+    case DW_EH_PE_textrel:
+      return (_Unwind_Ptr) data->tbase;
+    case DW_EH_PE_datarel:
+      return (_Unwind_Ptr) data->dbase;
+    }
+  abort ();
+}
+
+static int
+_Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
+{
+  struct unw_eh_callback_data *data = (struct unw_eh_callback_data *) ptr;
+  const ElfW(Phdr) *phdr, *p_eh_frame_hdr, *p_dynamic;
+  long n, match;
+  _Unwind_Ptr load_base;
+  const unsigned char *p;
+  const struct unw_eh_frame_hdr *hdr;
+  _Unwind_Ptr eh_frame;
+  struct object ob;
+
+  /* Make sure struct dl_phdr_info is at least as big as we need.  */
+  if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
+	     + sizeof (info->dlpi_phnum))
+    return -1;
+
+  match = 0;
+  phdr = info->dlpi_phdr;
+  load_base = info->dlpi_addr;
+  p_eh_frame_hdr = NULL;
+  p_dynamic = NULL;
+
+  /* See if PC falls into one of the loaded segments.  Find the eh_frame
+     segment at the same time.  */
+  for (n = info->dlpi_phnum; --n >= 0; phdr++)
+    {
+      if (phdr->p_type == PT_LOAD)
+	{
+	  _Unwind_Ptr vaddr = phdr->p_vaddr + load_base;
+	  if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
+	    match = 1;
+	}
+      else if (phdr->p_type == PT_GNU_EH_FRAME)
+	p_eh_frame_hdr = phdr;
+      else if (phdr->p_type == PT_DYNAMIC)
+	p_dynamic = phdr;
+    }
+  if (!match || !p_eh_frame_hdr)
+    return 0;
+
+  /* Read .eh_frame_hdr header.  */
+  hdr = (const struct unw_eh_frame_hdr *)
+	(p_eh_frame_hdr->p_vaddr + load_base);
+  if (hdr->version != 1)
+    return 1;
+
+#ifdef CRT_GET_RFIB_DATA
+# ifdef __i386__
+  data->dbase = NULL;
+  if (p_dynamic)
+    {
+      /* For dynamicly linked executables and shared libraries,
+	 DT_PLTGOT is the gp value for that object.  */
+      ElfW(Dyn) *dyn = (ElfW(Dyn) *)(p_dynamic->p_vaddr + load_base);
+      for (; dyn->d_tag != DT_NULL ; dyn++)
+	if (dyn->d_tag == DT_PLTGOT)
+	  {
+	    /* On IA-32, _DYNAMIC is writable and GLIBC has relocated it.  */
+	    data->dbase = (void *) dyn->d_un.d_ptr;
+	    break;
+	  }
+    }
+# else
+#  error What is DW_EH_PE_datarel base on this platform?
+# endif
+#endif
+#ifdef CRT_GET_RFIB_TEXT
+# error What is DW_EH_PE_textrel base on this platform?
+#endif
+
+  p = read_encoded_value_with_base (hdr->eh_frame_ptr_enc,
+				    base_from_cb_data (hdr->eh_frame_ptr_enc,
+						       data),
+				    (const unsigned char *) (hdr + 1),
+				    &eh_frame);
+
+  /* We require here specific table encoding to speed things up.
+     Also, DW_EH_PE_datarel here means using PT_GNU_EH_FRAME start
+     as base, not the processor specific DW_EH_PE_datarel.  */
+  if (hdr->fde_count_enc != DW_EH_PE_omit
+      && hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4))
+    {
+      _Unwind_Ptr fde_count;
+
+      p = read_encoded_value_with_base (hdr->fde_count_enc,
+					base_from_cb_data (hdr->fde_count_enc,
+							   data),
+					p, &fde_count);
+      /* Shouldn't happen.  */
+      if (fde_count == 0)
+	return 1;
+      if ((((_Unwind_Ptr) p) & 3) == 0)
+	{
+	  struct fde_table {
+	    signed initial_loc __attribute__ ((mode (SI)));
+	    signed fde __attribute__ ((mode (SI)));
+	  };
+	  const struct fde_table *table = (const struct fde_table *) p;
+	  size_t lo, hi, mid;
+	  _Unwind_Ptr data_base = (_Unwind_Ptr) hdr;
+	  fde *f;
+	  unsigned int f_enc, f_enc_size;
+	  _Unwind_Ptr range;
+
+	  mid = fde_count - 1;
+	  if (data->pc < table[0].initial_loc + data_base)
+	    return 1;
+	  else if (data->pc < table[mid].initial_loc + data_base)
+	    {
+	      lo = 0;
+	      hi = mid;
+
+	      while (lo < hi)
+		{
+		  mid = (lo + hi) / 2;
+		  if (data->pc < table[mid].initial_loc + data_base)
+		    hi = mid;
+		  else if (data->pc >= table[mid + 1].initial_loc + data_base)
+		    lo = mid + 1;
+		  else
+		    break;
+		}
+
+	      if (lo >= hi)
+		__gxx_abort ();
+	    }
+
+	  f = (fde *) (table[mid].fde + data_base);
+	  f_enc = get_fde_encoding (f);
+	  f_enc_size = size_of_encoded_value (f_enc);
+	  read_encoded_value_with_base (f_enc & 0x0f, 0,
+					&f->pc_begin[f_enc_size], &range);
+	  if (data->pc < table[mid].initial_loc + data_base + range)
+	    data->ret = f;
+	  data->func = (void *) (table[mid].initial_loc + data_base);
+	  return 1;
+	}
+    }
+
+  /* We have no sorted search table, so need to go the slow way.
+     As soon as GLIBC will provide API so to notify that a library has been
+     removed, we could cache this (and thus use search_object).  */
+  ob.pc_begin = NULL;
+  ob.tbase = data->tbase;
+  ob.dbase = data->dbase;
+  ob.u.single = (fde *) eh_frame;
+  ob.s.i = 0;
+  ob.s.b.mixed_encoding = 1;  /* Need to assume worst case.  */
+  data->ret = linear_search_fdes (&ob, (fde *) eh_frame, (void *) data->pc);
+  if (data->ret != NULL)
+    {
+      unsigned int encoding = get_fde_encoding (data->ret);
+      read_encoded_value_with_base (encoding,
+				    base_from_cb_data (encoding, data),
+				    data->ret->pc_begin,
+				    (_Unwind_Ptr *)&data->func);
+    }
+  return 1;
+}
+
+fde *
+_Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases)
+{
+  struct unw_eh_callback_data data;
+  fde *ret;
+
+  ret = _Unwind_Find_registered_FDE (pc, bases);
+  if (ret != NULL)
+    return ret;
+
+  data.pc = (_Unwind_Ptr) pc;
+  data.tbase = NULL;
+  data.dbase = NULL;
+  data.func = NULL;
+  data.ret = NULL;
+
+  if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
+    return NULL;
+
+  if (data.ret)
+    {
+      bases->tbase = data.tbase;
+      bases->dbase = data.dbase;
+      bases->func = data.func;
+    }
+  return data.ret;
+}
+
+#else
+#include "unwind-dw2-fde.c"
+#endif
--- gcc/crtstuff.c.jj	Mon Oct 29 15:05:39 2001
+++ gcc/crtstuff.c	Wed Dec 12 19:19:56 2001
@@ -66,6 +66,18 @@ Software Foundation, 59 Temple Place - S
 # define CRT_CALL_STATIC_FUNCTION(func) func ()
 #endif
 
+#if defined(OBJECT_FORMAT_ELF) && defined(HAVE_LD_EH_FRAME_HDR) \
+    && defined(__GLIBC__) && __GLIBC__ >= 2
+#include <link.h>
+# if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
+     || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
+#  define USE_PT_GNU_EH_FRAME
+# endif
+#endif
+#if defined(EH_FRAME_SECTION_NAME) && !defined(USE_PT_GNU_EH_FRAME)
+# define USE_EH_FRAME_REGISTRY
+#endif
+
 /* We do not want to add the weak attribute to the declarations of these
    routines in unwind-dw2-fde.h because that will cause the definition of
    these symbols to be weak as well.
@@ -245,7 +257,7 @@ __do_global_dtors_aux (void)
       f ();
     }
 
-#ifdef EH_FRAME_SECTION_NAME
+#ifdef USE_EH_FRAME_REGISTRY
 #if defined(CRT_GET_RFIB_TEXT) || defined(CRT_GET_RFIB_DATA)
   /* If we used the new __register_frame_info_bases interface,
      make sure that we deregister from the same place.  */
@@ -274,7 +286,7 @@ fini_dummy (void)
   asm (TEXT_SECTION_ASM_OP);
 }
 
-#if defined(EH_FRAME_SECTION_NAME) || defined(JCR_SECTION_NAME)
+#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
 /* Stick a call to __register_frame_info into the .init section.  For some
    reason calls with no arguments work more reliably in .init, so stick the
    call in another function.  */
@@ -282,7 +294,7 @@ fini_dummy (void)
 static void
 frame_dummy (void)
 {
-#ifdef EH_FRAME_SECTION_NAME
+#ifdef USE_EH_FRAME_REGISTRY
   static struct object object;
 #if defined(CRT_GET_RFIB_TEXT) || defined(CRT_GET_RFIB_DATA)
   void *tbase, *dbase;
@@ -386,13 +398,13 @@ __do_global_dtors (void)
   for (p = __DTOR_LIST__ + 1; (f = *p); p++)
     f ();
 
-#ifdef EH_FRAME_SECTION_NAME
+#ifdef USE_EH_FRAME_REGISTRY
   if (__deregister_frame_info)
     __deregister_frame_info (__EH_FRAME_BEGIN__);
 #endif
 }
 
-#if defined(EH_FRAME_SECTION_NAME) || defined(JCR_SECTION_NAME)
+#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
 /* A helper function for __do_global_ctors, which is in crtend.o.  Here
    in crtbegin.o, we can reference a couple of symbols not visible there.
    Plus, since we're before libgcc.a, we have no problems referencing
@@ -400,7 +412,7 @@ __do_global_dtors (void)
 void
 __do_global_ctors_1(void)
 {
-#ifdef EH_FRAME_SECTION_NAME
+#ifdef USE_EH_FRAME_REGISTRY
   static struct object object;
   if (__register_frame_info)
     __register_frame_info (__EH_FRAME_BEGIN__, &object);
@@ -547,7 +559,7 @@ void
 __do_global_ctors (void)
 {
   func_ptr *p;
-#if defined(EH_FRAME_SECTION_NAME) || defined(JCR_SECTION_NAME)
+#if defined(USE_EH_FRAME_REGISTRY) || defined(JCR_SECTION_NAME)
   __do_global_ctors_1();
 #endif
   for (p = __CTOR_END__ - 1; *p != (func_ptr) -1; p--)

	Jakub



More information about the Gcc-patches mailing list