This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PATCH: Remove code duplication in dwarf2.c


Hi Nick,

Both _bfd_dwarf2_find_nearest_line and _bfd_dwarf2_find_line have
the same code to follow .gnu_debuglink section.  This patch uses
a function instead of duplicating the code. OK to install?

Thanks.


H.J.
----
2007-06-22  H.J. Lu  <hongjiu.lu@intel.com>

	* dwarf2.c (find_debug_info_follow_debuglink): New.
	(_bfd_dwarf2_find_nearest_line): Use it.
	(_bfd_dwarf2_find_line): Use it.

--- bfd/dwarf2.c.dup	2007-06-22 09:04:48.000000000 -0700
+++ bfd/dwarf2.c	2007-06-22 09:43:17.000000000 -0700
@@ -2210,6 +2210,38 @@ find_debug_info (bfd *abfd, asection *af
   return NULL;
 }
 
+/* Similar to find_debug_info, but follow .gnu_debuglink section
+   and return debug bfd.  */
+
+static asection *
+find_debug_info_follow_debuglink (bfd **abfd_p)
+{
+  char *debug_filename = bfd_follow_gnu_debuglink (*abfd_p, NULL);
+  asection *msec = NULL;
+
+  if (debug_filename != NULL)
+    {
+      bfd *debug_bfd = bfd_openr (debug_filename, NULL);
+
+      if (debug_bfd != NULL
+	  && bfd_check_format (debug_bfd, bfd_object))
+	msec = find_debug_info (debug_bfd, NULL);
+
+      if (msec == NULL)
+	{
+	  if (debug_bfd)
+	    bfd_close (debug_bfd);
+	  /* FIXME: Should we report our failure to follow the
+	     debuglink ?  */
+	  free (debug_filename);
+	}
+      else
+	*abfd_p = debug_bfd;
+    }
+
+  return msec;
+}
+
 /* Unset vmas for loadable sections in STASH.  */
 
 static void
@@ -2369,37 +2401,23 @@ _bfd_dwarf2_find_nearest_line (bfd *abfd
 
   if (! *pinfo)
     {
-      bfd *debug_bfd;
+      bfd *debug_bfd = abfd;
       bfd_size_type total_size;
       asection *msec;
 
       *pinfo = stash;
 
-      msec = find_debug_info (abfd, NULL);
+      msec = find_debug_info (debug_bfd, NULL);
       if (msec == NULL)
 	{
-	  char * debug_filename = bfd_follow_gnu_debuglink (abfd, NULL);
-
-	  if (debug_filename == NULL)
+	  msec = find_debug_info_follow_debuglink (&debug_bfd);
+	  if (msec == NULL)
 	    /* No dwarf2 info, and no gnu_debuglink to follow.
 	       Note that at this point the stash has been allocated, but
 	       contains zeros.  This lets future calls to this function
 	       fail more quickly.  */
 	    goto done;
-
-	  if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
-	      || ! bfd_check_format (debug_bfd, bfd_object)
-	      || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
-	    {
-	      if (debug_bfd)
-		bfd_close (debug_bfd);
-	      /* FIXME: Should we report our failure to follow the debuglink ?  */
-	      free (debug_filename);
-	      goto done;
-	    }
 	}
-      else
-	debug_bfd = abfd;
 
       /* There can be more than one DWARF2 info section in a BFD these days.
 	 Read them all in and produce one large stash.  We do this in two
@@ -2608,37 +2626,23 @@ _bfd_dwarf2_find_line (bfd *abfd,
 
   if (! *pinfo)
     {
-      bfd *debug_bfd;
+      bfd *debug_bfd = abfd;
       bfd_size_type total_size;
       asection *msec;
 
       *pinfo = stash;
 
-      msec = find_debug_info (abfd, NULL);
+      msec = find_debug_info (debug_bfd, NULL);
       if (msec == NULL)
 	{
-	  char * debug_filename = bfd_follow_gnu_debuglink (abfd, NULL);
-
-	  if (debug_filename == NULL)
+	  msec = find_debug_info_follow_debuglink (&debug_bfd);
+	  if (msec == NULL)
 	    /* No dwarf2 info, and no gnu_debuglink to follow.
 	       Note that at this point the stash has been allocated, but
 	       contains zeros.  This lets future calls to this function
 	       fail more quickly.  */
 	    goto done;
-
-	  if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
-	      || ! bfd_check_format (debug_bfd, bfd_object)
-	      || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
-	    {
-	      if (debug_bfd)
-		bfd_close (debug_bfd);
-	      /* FIXME: Should we report our failure to follow the debuglink ?  */
-	      free (debug_filename);
-	      goto done;
-	    }
 	}
-      else
-	debug_bfd = abfd;
 
       /* There can be more than one DWARF2 info section in a BFD these days.
 	 Read them all in and produce one large stash.  We do this in two


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]